So in my quest to make my AES128 code better and not to have memory leaks and the sort I came across something. Someone said "Unless you are forced to use C, you should never use malloc"
So I thought to myself, "Self, I will get rid of my malloc's". And in my Encrypt/Decrypt method I found this. Now i'm told that I should always finish with a delete[] on my byte array.. and I don't see how I would do it... maybe my methodology is wrong
here is how I call the method.
bool DESFireCard::AuthenticateOffline()
{
AES128 aes;
BYTE* RandB;
BYTE buffer[33] = {0x00};
BYTE lenRec = 0;
BYTE IV[16] = {0x00};
BYTE divkey[16];
ByteUtil::StringToHex(key.DIV, divkey);
aes.SetIV(IV);
aes.SetKey(divkey);
int state = rf_ISO14443_4_COS(APDU,2, buffer, &lenRec,2000);
aes.DecryptData(&RandB, &buffer[1], 16);
// LOTS more code HERE
delete[] RandB;
delete[] buffer; // <--crashes here
delete[] IV;
delete[] divkey;
return true;
}
then my decrypt method (which is almost identical to the encrypt method)
void AES128::DecryptData(BYTE** outBlock, const BYTE* inBlock, size_t length)
{
float blockSize = (float)(length/16);
blockSize = ceilf(blockSize);
size_t newLength = (size_t)(blockSize*16);
BYTE* temp = new BYTE[newLength];//(BYTE*)malloc(newLength);
BYTE* padd = new BYTE[newLength];//(BYTE*)malloc(newLength);
memset(temp, 0, newLength);
memcpy(padd, inBlock, length);
DecryptBlock(temp, padd, IV);
for (int i=1; i<blockSize; i++)
{
DecryptBlock(&temp[i*16], &padd[i*16], &temp[(i-1)*16]);
}
*outBlock = new BYTE[newLength]; //(BYTE*)malloc(newLength);
memcpy((*outBlock), temp, newLength);
delete[] temp;
delete[] padd;
}
void AES128::DecryptBlock(BYTE* outBlock, const BYTE* inBlock, const BYTE* cipherBlock)
{
BYTE temp[16] = {0x00};
AES::XorBlock(temp, inBlock);
BYTE expandedKey[176] = {0x00};
BYTE* invExpandedKey;
memcpy(expandedKey, Key, 16);
Galois::expand_key(expandedKey);
Galois::InvertExpandedKey(&invExpandedKey, expandedKey);
AES::XorBlock(temp, invExpandedKey);
AES::InvShiftRows(temp);
AES::InvSubBytes(temp);
for(int i=0x10; i<0xA0; i+=0x10) //change i<0xA0
{
AES::InvDoRound(temp, &invExpandedKey[i]);
}
AES::XorBlock(temp, &invExpandedKey[160]);
AES::XorBlock(temp, cipherBlock);
memcpy(outBlock, temp, 16);
}
so would I put a delete method in my AES128 class (see edit)? or do I put it at the end of me using it in the one method?
or should I put in a "outbuffer" in AES128 then in my destructor delete my outbuffer?
Sorry for a seemingyl newb question but C++ is not my strength (i hope it will be one day though :) )
EDIT1 ok I put in a few deletes and my program crashes saying something AccessViolationException
grar!! i hate that exception. I found out where it crashes, but i don't know why it would crash. Any tips on that? do i need to use delete on pre-allocated memories?
delete[]an array allocated on the stack. – Richard J. Ross III Feb 4 at 16:33BYTE* = new BYTE[size]that the problem goes away. Was that the correct move? – Robert Snyder Feb 4 at 16:37new[]to allocate anddelete[]to deallocate. You can't deallocate a fixed-size array. You can only usedelete[]to deallocate memory allocated withnew[]as in your previous comment :) – Morwenn Feb 4 at 21:13