uint8_t ecb_decrypt(ecb_state_t *state, aes_ctx_t *ctx, uint8_t *input) { ecb_state_t packet; memcpy(&packet, input, AES_BUF_LEN); aes128_dec(&packet, ctx); const uint16_t checksum = calc_checksum16((uint8_t*)&packet, AES_BUF_LEN-sizeof(uint16_t)); if (checksum != packet.checksum) { return 1; } /* if (packet.message_id <= state->message_id) { */ /* return 2; */ /* } */ if (packet.device_id != state->device_id) { return 3; } /* if (state->message_id % MESSAGE_ID_UPDATE_INTERVAL == 0) { */ /* eeprom_update_dword(get_msg_id_addr(state->device_id), packet.message_id); */ /* } */ memcpy(state, &packet, sizeof(ecb_state_t)); return 0; }
void cbc_decrypt(cbc_state_t *state, aes_ctx_t *ctx) { uint8_t this_cipher[AES_BUF_LEN]; memcpy(this_cipher, state->data, AES_BUF_LEN); aes128_dec(state->data, ctx); xor_buf(state->data, state->iv, AES_BUF_LEN); memcpy(state->iv, this_cipher, AES_BUF_LEN); }
void WaspAES::ECBDecrypt(uint8_t *original_data,uint16_t size,uint16_t keySize){ // In ECB mode is separated from message in blocks of 16 bytes and cipher each one individually uint16_t index,index2; index = 0; index2 = 0; while(index<size){ for (int i =0; i<16;i++){ block_data[i]=original_data[index]; index++; } // Decrypt switch(keySize){ case 128: aes128_dec(block_data, &ctx128); break; case 192: aes192_dec(block_data, &ctx192); break; case 256: aes256_dec(block_data, &ctx256); break; } for (int i = 0; i<16;i++){ original_data[index2] = block_data[i]; index2++; } } }
void testrun_test_aes(void){ uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; uint8_t data[16] = { 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34 }; aes128_ctx_t ctx; aes128_init(key, &ctx); cli_putstr("\r\n\r\n cipher test (FIPS 197):\r\n key: "); cli_hexdump(key, 16); cli_putstr("\r\n plaintext: "); cli_hexdump(data, 16); aes128_enc(data, &ctx); cli_putstr("\r\n ciphertext: "); cli_hexdump(data, 16); aes128_dec(data, &ctx); cli_putstr("\r\n plaintext: "); cli_hexdump(data, 16); cli_putstr("\r\n testing bcal:"); bcgen_ctx_t bcal_ctx; uint8_t r; r = bcal_cipher_init(&aes128_desc, key, 128, &bcal_ctx); cli_putstr("\r\n init = 0x"); cli_hexdump(&r, 1); bcal_cipher_enc(data, &bcal_ctx); cli_putstr("\r\n ciphertext: "); cli_hexdump(data, 16); bcal_cipher_dec(data, &bcal_ctx); cli_putstr("\r\n plaintext: "); cli_hexdump(data, 16); bcal_cipher_free(&bcal_ctx); }
void testrun_test_aes(void){ uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; uint8_t data[16] = { 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34 }; aes128_ctx_t ctx; aes128_init(key, &ctx); cli_putstr_P(PSTR("\r\n\r\n cipher test (FIPS 197):\r\n key: ")); cli_hexdump(key, 16); cli_putstr_P(PSTR("\r\n plaintext: ")); cli_hexdump(data, 16); aes128_enc(data, &ctx); cli_putstr_P(PSTR("\r\n ciphertext: ")); cli_hexdump(data, 16); aes128_dec(data, &ctx); cli_putstr_P(PSTR("\r\n plaintext: ")); cli_hexdump(data, 16); }
int main ( void ) { uint8_t *bufcontents; uint8_t i; uint8_t tv[] = "foobar1"; uint16_t ticker = 0; uint8_t rc; aes128_ctx_t ctx; uint8_t key[] = "0123456789ABCDEF"; uint8_t IV[] = "FEDCBA9876543210"; uint8_t text[] = "Hello rfm12 world. I've gonna cipher you "; drive(LED1); drive(LED2); toggle_output(LED1); uart_init(); _delay_ms(250); _delay_ms(250); sei(); toggle_output(LED2); uart_putstr ("AVR Boot Ok\r\n"); _delay_ms(250); toggle_output(LED2); aes128_init(key,&ctx); while (1) { uart_putstr("key = "); uart_putstr(key); uart_putstr("\r\n"); uart_putstr("text = "); uart_putstr(text); uart_putstr("\r\n"); /* Ciphering in CBC mode */ memxor(text,IV,16); aes128_enc(text,&ctx); memxor(text+16,text,16); aes128_enc(text+16,&ctx); memxor(text+32,text+16,16); aes128_enc(text+32,&ctx); uart_putstr("text ciphered = \r\n"); uart_hexdump(text,sizeof(text)); /* Deciphering in CBC mode */ aes128_dec(text+32,&ctx); memxor(text+32,text+16,16); aes128_dec(text+16,&ctx); memxor(text+16,text,16); aes128_dec(text,&ctx); memxor(text,IV,16); uart_putstr("text unciphered = "); uart_putstr(text); uart_putstr("\r\n"); /* Let change the IV */ IV[0]++; _delay_ms(250); _delay_ms(250); _delay_ms(250); _delay_ms(250); } }
// decrypt single 128bit block. data is assumed to be 16 uint8_t's // key is assumed to be 128bit thus 16 uint8_t's void aes128_dec_single(const uint8_t* key, void* data){ aes128_ctx_t ctx; aes128_init(key, &ctx); aes128_dec(data, &ctx); }
void WaspAES::CBCDecrypt(uint8_t *original_data,uint16_t size, uint8_t *InitialVector,uint16_t keySize){ /////////////////////////// // In CBC mode the message is divided into blocks of 16 bytes, to 1 block // Applied to the XOR and calculated its block cipher with block // Encryption XOR obtained is applied to the second data block in clear // And the result is encrypted with AES given, the result will be the 2nd block // Encryption, so on. // // This function is: // 1 - separated data block of 16 bytes, // 2 - XOR operation is performed with the IV / block precesor // 3 - was called to the encryption function // 4 - Once all blocks are encrypted will join // 5 - forms the encrypted message and returns // /////////////////////////// uint8_t IV[16]; uint8_t Plain_text[16]; uint8_t Previous_block[16]; uint16_t index,index2; index = 0; index2 = 0; //Assign Initial Vector to IV variable assignBlock(IV,InitialVector); while(index < size){ //Decrypt for (int i =0; i<16;i++){ block_data[i]= original_data[index]; index++; } assignBlock(Previous_block,block_data); switch(keySize){ case 128: aes128_dec(block_data, &ctx128); break; case 192: aes192_dec(block_data, &ctx192); break; case 256: aes256_dec(block_data, &ctx256); break; } if (index == 16){ XOR(block_data,IV,Plain_text); }else { XOR(block_data,Previous_block,Plain_text); } for (int i = 0; i<16;i++){ original_data[index2] = Plain_text[i]; index2++; } } }