Hello everyone,

I was wondering if I could get some feedback on a wrapper that I wrote
to simplify CBC AES.  Right now the only problem that I see is that
I'm over-allocating the array on the decrypt method by a maximum of 15
bytes (AES::BLOCKSIZE-1)

This email is kinda stemming from the confusion about the initial IV values.

A little background on the usage of the class:  It is used to encrypt
packet data for a game I am working on.  The plaintext of the packet
gets encrypted with the Encrypt() method and then RSAES<OAEP<SHA>> is
used to encrypt the AES Key and IV which is added to the packet
header.  Any comments/suggestions/criticisms would be greatly
appreciated.


#include "crypt/aes.h"
#include "crypt/modes.h"

typedef boost::shared_array<char> SHCHAR;
class CAESProvider
{
        static CryptoPP::AutoSeededRandomPool  m_RPool; //instantiated in .cpp

public:
        struct AESResult
        {
                static const size_t KEYBLOCKSIZE =
CryptoPP::AES::DEFAULT_KEYLENGTH+CryptoPP::AES::BLOCKSIZE;
                AESResult(const char *key,const char *iv)
                {
                        if(key && iv)
                        {
                                
memcpy(Key(),key,CryptoPP::AES::DEFAULT_KEYLENGTH);
                                memcpy(IV(),iv,CryptoPP::AES::BLOCKSIZE);
                        }
                }
                char KEYBLOCK[KEYBLOCKSIZE];

                char *Key() { return KEYBLOCK; }
                char *IV() { return KEYBLOCK+CryptoPP::AES::DEFAULT_KEYLENGTH; }
                SHCHAR Data;
                unsigned DataLength;

        };
        static unsigned GetCypherTextLength(unsigned size)
        {
                return size+size%CryptoPP::AES::BLOCKSIZE;
        }
        static AESResult Encrypt(const char* src,unsigned length)
        {
                AESResult result(NULL,NULL);

                m_RPool.GenerateBlock((byte 
*)result.Key(),CryptoPP::AES::DEFAULT_KEYLENGTH);
                m_RPool.GenerateBlock((byte 
*)result.IV(),CryptoPP::AES::BLOCKSIZE);

                result.DataLength = GetCypherTextLength(length);
                result.Data = SHCHAR(new char[result.DataLength]);
                

                CryptoPP::AES::Encryption aesEncryption((byte *)result.Key(),
CryptoPP::AES::DEFAULT_KEYLENGTH);
                CryptoPP::CBC_Mode_ExternalCipher::Encryption
cbcEncryption(aesEncryption, (byte *)result.IV());

                CryptoPP::ArraySink *buffSink = new CryptoPP::ArraySink((byte
*)result.Data.get(),result.DataLength);
                CryptoPP::StreamTransformationFilter 
cbcFilter(cbcEncryption,buffSink);

                cbcFilter.Put((byte *)src,length);
                cbcFilter.MessageEnd();

                _ASSERTE(buffSink->TotalPutLength() == result.DataLength);


                return result;
                
        }
        static AESResult Decrypt(const char *src,unsigned length,const char
*Key,const char *IV)
        {
                AESResult result(NULL,NULL);
                result.Data = SHCHAR(new char[length]);
                CryptoPP::AES::Decryption aesDecryption((byte *)Key,
CryptoPP::AES::DEFAULT_KEYLENGTH);
                CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(
aesDecryption, (byte *)IV);

                

                CryptoPP::ArraySink *buffSink = new CryptoPP::ArraySink((byte
*)result.Data.get(),length);

                CryptoPP::StreamTransformationFilter 
stfDecryptor(cbcDecryption, buffSink);
                stfDecryptor.Put((byte *)src,length );
                stfDecryptor.MessageEnd();

                result.DataLength = (unsigned)buffSink->TotalPutLength();
                
                return result;
                
        }

};


Thanks,

Mikhail Davidov
DigiPen '09

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [EMAIL PROTECTED]
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
-~----------~----~----~----~------~----~------~--~---

Reply via email to