/* * decrypt(uint16_t KeySize,char* Password,uint8_t encrypted_message[], uint8_t size, uint8_t original_message[], uint16_t *original_size, uint8_t mode,uint8_t padding,uint8_t* InitialVector) - decrypts a message using AES Algorithm * * Parameters: * keySize : Size of key to use in AES Algorithm * password: Key to encrypt plaintext message. * * encrypted_message: Ciphertext message after calculating AES Algorithm * size: number of blocks * original_message: Plaintext message after decrypt ciphertext * original_size: size of message after decrypted. * mode: cipher mode, CBC * padding: padding mode to fill blocks, tree ways PKCS5, ZEROS, X923 * initialVector: * * Examples: * AES.decrypt(128,"libelium","Libelium",8,original_message,8,CBC,PKCS5,IV) * */ uint8_t WaspAES::decrypt(uint16_t KeySize,char* Password,uint8_t encrypted_message[], uint8_t size, uint8_t original_message[], uint16_t *original_size, uint8_t mode,uint8_t padding,uint8_t* InitialVector){ o_message decrypted_message; if (init(Password,KeySize)){ uint8_t original_data[size]; for (uint16_t x = 0; x < size; x++){ original_data[x] = encrypted_message[x]; } if (mode == CBC){ CBCDecrypt(original_data,size,InitialVector,KeySize); } decrypted_message = paddingDecrypt(original_data,size,padding); for (uint8_t k = 0; k<decrypted_message.size_txt; k++){ original_message[k] = decrypted_message.txt[k]; } *original_size = decrypted_message.size_txt; return 1; }else{ return 0; } }
int AES128CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const { return CBCDecrypt(dec, iv, data, size, pad, out); }
double encrypt_decrypt_test(int blockCount, int repetitions) { byte_ard Key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; byte_ard IV[] = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }; // Allocate memory for key schedule and generate it. byte_ard Keys[KEY_BYTES*12]; KeyExpansion(Key, Keys); // allocate some buffers to hold data int length = 16*blockCount; unsigned char *buf = (unsigned char *)malloc(length); unsigned char *ecbuf = (unsigned char *)malloc(length); unsigned char *dcbuf = (unsigned char *)malloc(length); // Fill the plaintext buffer with random data of multiples of block length. srand(time(NULL)); for ( int i=0; i<(16*blockCount); i++ ) buf[i] = rand() % 0xFF; // Start the timed test timeval tstart, tstop, tresult; gettimeofday(&tstart,NULL); for( int i=0; i<repetitions; i++ ) { CBCEncrypt( (void *)buf, (void *)ecbuf, length, AUTOPAD, (const u_int32_ard*)Keys, (const u_int16_ard*)IV); CBCDecrypt( (void *)ecbuf, (void *)dcbuf, length, (const u_int32_ard*)Keys, (const u_int16_ard*)IV); } gettimeofday(&tstop,NULL); // Calculate the results timersub(&tstop,&tstart,&tresult); double totaltime = tresult.tv_sec * 1000000.0 + tresult.tv_usec; double timePerOperation = totaltime / repetitions; double timePerBlock = timePerOperation / blockCount; // Check if decryption is consistent with plaintext if ( strncmp((char *)buf, (char *)dcbuf,length) != 0 ) printf("Error in encrypt/decrypt!\n"); // Output timing results. printf("blocks: %d\n", blockCount); printf("Duration: %f usec\n", totaltime); printf("Per operation: %f usec\n", timePerOperation); printf("Per block: %f usec\n", timePerBlock); printf("\n"); /**** // Dont print the blocks except when debugging printf("\nPlaintext : \n"); printBytes2((byte_ard*)buf, length); printf("\nAfter CBC: \n"); printBytes2((byte_ard*)ecbuf, length); printf("\nCBC Decipher: \n"); printBytes2((byte_ard*)dcbuf, length); ****/ return timePerBlock; }