/** * \brief Test CTR mode encryption and decryption. * * \param test Current test case. */ static void run_ctr_mode_test(const struct test_case *test) { state = false; /* Configure the AES. */ g_aes_inst.aes_cfg->encrypt_mode = AES_ENCRYPTION; g_aes_inst.aes_cfg->key_size = AES_KEY_SIZE_128; g_aes_inst.aes_cfg->dma_mode = AES_MANUAL_MODE; g_aes_inst.aes_cfg->opmode = AES_CTR_MODE; g_aes_inst.aes_cfg->cfb_size = AES_CFB_SIZE_128; g_aes_inst.aes_cfg->countermeasure_mask = AES_COUNTERMEASURE_TYPE_ALL; aes_set_config(&g_aes_inst); /* Beginning of a new message. */ aes_set_new_message(&g_aes_inst); /* Set the cryptographic key. */ aes_write_key(&g_aes_inst, key128); /* Set the initialization vector. */ aes_write_initvector(&g_aes_inst, init_vector_ctr); /* Write the data to be ciphered to the input data registers. */ aes_write_input_data(&g_aes_inst, ref_plain_text[0]); aes_write_input_data(&g_aes_inst, ref_plain_text[1]); aes_write_input_data(&g_aes_inst, ref_plain_text[2]); aes_write_input_data(&g_aes_inst, ref_plain_text[3]); /* Wait for the end of the encryption process. */ delay_ms(30); /* check the result. */ if ((ref_cipher_text_ctr[0] != output_data[0]) || (ref_cipher_text_ctr[1] != output_data[1]) || (ref_cipher_text_ctr[2] != output_data[2]) || (ref_cipher_text_ctr[3] != output_data[3])) { flag = false; } else { flag = true; } test_assert_true(test, flag == true, "CTR mode encryption not work!"); state = false; /* Configure the AES. */ g_aes_inst.aes_cfg->encrypt_mode = AES_DECRYPTION; g_aes_inst.aes_cfg->key_size = AES_KEY_SIZE_128; g_aes_inst.aes_cfg->dma_mode = AES_MANUAL_MODE; g_aes_inst.aes_cfg->opmode = AES_CTR_MODE; g_aes_inst.aes_cfg->cfb_size = AES_CFB_SIZE_128; g_aes_inst.aes_cfg->countermeasure_mask = AES_COUNTERMEASURE_TYPE_ALL; aes_set_config(&g_aes_inst); /* Beginning of a new message. */ aes_set_new_message(&g_aes_inst); /* Set the cryptographic key. */ aes_write_key(&g_aes_inst, key128); /* Set the initialization vector. */ aes_write_initvector(&g_aes_inst, init_vector_ctr); /* Write the data to be deciphered to the input data registers. */ aes_write_input_data(&g_aes_inst, ref_cipher_text_ctr[0]); aes_write_input_data(&g_aes_inst, ref_cipher_text_ctr[1]); aes_write_input_data(&g_aes_inst, ref_cipher_text_ctr[2]); aes_write_input_data(&g_aes_inst, ref_cipher_text_ctr[3]); /* Wait for the end of the decryption process. */ delay_ms(30); /* check the result. */ if ((ref_plain_text[0] != output_data[0]) || (ref_plain_text[1] != output_data[1]) || (ref_plain_text[2] != output_data[2]) || (ref_plain_text[3] != output_data[3])) { flag = false; } else { flag = true; } test_assert_true(test, flag == true, "CTR mode decryption not work!"); }
/** * \brief CTR mode encryption and decryption test. */ static void ctr_mode_test(void) { printf("\r\n-----------------------------------\r\n"); printf("- 128bit cryptographic key\r\n"); printf("- CTR cipher mode\r\n"); printf("- all counter measures\r\n"); printf("- input of 4 32bit words\r\n"); printf("-----------------------------------\r\n"); state = false; /* Configure the AES. */ g_aes_inst.aes_cfg->encrypt_mode = AES_ENCRYPTION; g_aes_inst.aes_cfg->key_size = AES_KEY_SIZE_128; g_aes_inst.aes_cfg->dma_mode = AES_MANUAL_MODE; g_aes_inst.aes_cfg->opmode = AES_CTR_MODE; g_aes_inst.aes_cfg->cfb_size = AES_CFB_SIZE_128; g_aes_inst.aes_cfg->countermeasure_mask = AES_COUNTERMEASURE_TYPE_ALL; aes_set_config(&g_aes_inst); /* Beginning of a new message. */ aes_set_new_message(&g_aes_inst); /* Set the cryptographic key. */ aes_write_key(&g_aes_inst, key128); /* Set the initialization vector. */ aes_write_initvector(&g_aes_inst, init_vector_ctr); /* Write the data to be ciphered to the input data registers. */ aes_write_input_data(&g_aes_inst, ref_plain_text[0]); aes_write_input_data(&g_aes_inst, ref_plain_text[1]); aes_write_input_data(&g_aes_inst, ref_plain_text[2]); aes_write_input_data(&g_aes_inst, ref_plain_text[3]); /* Wait for the end of the encryption process. */ while (false == state) { } /* check the result. */ if ((ref_cipher_text_ctr[0] != output_data[0]) || (ref_cipher_text_ctr[1] != output_data[1]) || (ref_cipher_text_ctr[2] != output_data[2]) || (ref_cipher_text_ctr[3] != output_data[3])) { printf("\r\nKO!!!\r\n"); } else { printf("\r\nOK!!!\r\n"); } printf("\r\n-----------------------------------\r\n"); printf("- 128bit cryptographic key\r\n"); printf("- CTR decipher mode\r\n"); printf("- all counter measures\r\n"); printf("- input of 4 32bit words\r\n"); printf("-----------------------------------\r\n"); state = false; /* Configure the AES. */ g_aes_inst.aes_cfg->encrypt_mode = AES_DECRYPTION; g_aes_inst.aes_cfg->key_size = AES_KEY_SIZE_128; g_aes_inst.aes_cfg->dma_mode = AES_MANUAL_MODE; g_aes_inst.aes_cfg->opmode = AES_CTR_MODE; g_aes_inst.aes_cfg->cfb_size = AES_CFB_SIZE_128; g_aes_inst.aes_cfg->countermeasure_mask = AES_COUNTERMEASURE_TYPE_ALL; aes_set_config(&g_aes_inst); /* Beginning of a new message. */ aes_set_new_message(&g_aes_inst); /* Set the cryptographic key. */ aes_write_key(&g_aes_inst, key128); /* Set the initialization vector. */ aes_write_initvector(&g_aes_inst, init_vector_ctr); /* Write the data to be deciphered to the input data registers. */ aes_write_input_data(&g_aes_inst, ref_cipher_text_ctr[0]); aes_write_input_data(&g_aes_inst, ref_cipher_text_ctr[1]); aes_write_input_data(&g_aes_inst, ref_cipher_text_ctr[2]); aes_write_input_data(&g_aes_inst, ref_cipher_text_ctr[3]); /* Wait for the end of the decryption process. */ while (false == state) { } /* check the result. */ if ((ref_plain_text[0] != output_data[0]) || (ref_plain_text[1] != output_data[1]) || (ref_plain_text[2] != output_data[2]) || (ref_plain_text[3] != output_data[3])) { printf("\r\nKO!!!\r\n"); } else { printf("\r\nOK!!!\r\n"); } }
/** * \brief Test CTR mode encryption and decryption. * * \param test Current test case. */ static void run_ctr_mode_test(const struct test_case *test) { /* Configure the AES. */ g_aes_cfg.encrypt_mode = AES_ENCRYPTION; g_aes_cfg.key_size = AES_KEY_SIZE_128; g_aes_cfg.start_mode = AES_AUTO_MODE; g_aes_cfg.opmode = AES_CTR_MODE; g_aes_cfg.cfb_size = AES_CFB_SIZE_128; g_aes_cfg.lod = false; aes_set_config(AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(AES, key128); /* Set the initialization vector. */ aes_write_initvector(AES, init_vector_ctr); /* Write the data to be ciphered to the input data registers. */ aes_write_input_data(AES, ref_plain_text); /* Wait for the end of the encryption process. */ delay_ms(30); /* check the result. */ if ((ref_cipher_text_ctr[0] != output_data[0]) || (ref_cipher_text_ctr[1] != output_data[1]) || (ref_cipher_text_ctr[2] != output_data[2]) || (ref_cipher_text_ctr[3] != output_data[3])) { flag = false; } else { flag = true; } test_assert_true(test, flag == true, "CTR mode encryption not work!"); /* Configure the AES. */ g_aes_cfg.encrypt_mode = AES_DECRYPTION; g_aes_cfg.key_size = AES_KEY_SIZE_128; g_aes_cfg.start_mode = AES_AUTO_MODE; g_aes_cfg.opmode = AES_CTR_MODE; g_aes_cfg.cfb_size = AES_CFB_SIZE_128; g_aes_cfg.lod = false; aes_set_config(AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(AES, key128); /* Set the initialization vector. */ aes_write_initvector(AES, init_vector_ctr); /* Write the data to be deciphered to the input data registers. */ aes_write_input_data(AES, ref_cipher_text_ctr); /* Wait for the end of the decryption process. */ delay_ms(30); /* check the result. */ if ((ref_plain_text[0] != output_data[0]) || (ref_plain_text[1] != output_data[1]) || (ref_plain_text[2] != output_data[2]) || (ref_plain_text[3] != output_data[3])) { flag = false; } else { flag = true; } test_assert_true(test, flag == true, "CTR mode decryption not work!"); }
/** * \brief CTR mode encryption and decryption test. */ static void ctr_mode_test(void) { printf("\r\n-----------------------------------\r\n"); printf("- 128bit cryptographic key\r\n"); printf("- CTR cipher mode\r\n"); printf("- Auto start mode\r\n"); printf("- input of 4 32bit words\r\n"); printf("-----------------------------------\r\n"); state = false; /* Configure the AES. */ g_aes_cfg.encrypt_mode = AES_ENCRYPTION; g_aes_cfg.key_size = AES_KEY_SIZE_128; g_aes_cfg.start_mode = AES_AUTO_START; g_aes_cfg.opmode = AES_CTR_MODE; g_aes_cfg.cfb_size = AES_CFB_SIZE_128; g_aes_cfg.lod = false; aes_set_config(AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(AES, key128); /* Set the initialization vector. */ aes_write_initvector(AES, init_vector_ctr); /* Write the data to be ciphered to the input data registers. */ aes_write_input_data(AES, ref_plain_text); /* Wait for the end of the encryption process. */ while (false == state) { } /* check the result. */ if ((ref_cipher_text_ctr[0] != output_data[0]) || (ref_cipher_text_ctr[1] != output_data[1]) || (ref_cipher_text_ctr[2] != output_data[2]) || (ref_cipher_text_ctr[3] != output_data[3])) { printf("\r\nKO!!!\r\n"); } else { printf("\r\nOK!!!\r\n"); } printf("\r\n-----------------------------------\r\n"); printf("- 128bit cryptographic key\r\n"); printf("- CTR decipher mode\r\n"); printf("- Auto start mode\r\n"); printf("- input of 4 32bit words\r\n"); printf("-----------------------------------\r\n"); state = false; /* Configure the AES. */ g_aes_cfg.encrypt_mode = AES_DECRYPTION; g_aes_cfg.key_size = AES_KEY_SIZE_128; g_aes_cfg.start_mode = AES_AUTO_START; g_aes_cfg.opmode = AES_CTR_MODE; g_aes_cfg.cfb_size = AES_CFB_SIZE_128; g_aes_cfg.lod = false; aes_set_config(AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(AES, key128); /* Set the initialization vector. */ aes_write_initvector(AES, init_vector_ctr); /* Write the data to be deciphered to the input data registers. */ aes_write_input_data(AES, ref_cipher_text_ctr); /* Wait for the end of the decryption process. */ while (false == state) { } /* check the result. */ if ((ref_plain_text[0] != output_data[0]) || (ref_plain_text[1] != output_data[1]) || (ref_plain_text[2] != output_data[2]) || (ref_plain_text[3] != output_data[3])) { printf("\r\nKO!!!\r\n"); } else { printf("\r\nOK!!!\r\n"); } }
/** * \brief GCM mode decryption test. */ static void gcm_mode_decryption_test(void) { printf("\r\n-----------------------------------\r\n"); printf("- 256bit cryptographic key\r\n"); printf("- GCM Decryption\r\n"); printf("- Auto start mode\r\n"); printf("- input of 160-bit effective words\r\n"); printf("-----------------------------------\r\n"); /* Configure the AES. */ g_aes_cfg.encrypt_mode = AES_DECRYPTION; g_aes_cfg.key_size = AES_KEY_SIZE_256; g_aes_cfg.start_mode = AES_AUTO_START; g_aes_cfg.opmode = AES_GCM_MODE; g_aes_cfg.cfb_size = AES_CFB_SIZE_128; g_aes_cfg.lod = false; g_aes_cfg.gtag_en = false; aes_set_config(AES, &g_aes_cfg); /* AES-GCM Input Configuration */ gcm_input_data.key = aes_key; gcm_input_data.key_len = AES_KEY_SIZE; gcm_input_data.iv = aes_iv; gcm_input_data.iv_len = AES_IV_SIZE; gcm_input_data.input = aes_cipher_text; gcm_input_data.text_len = AES_PDATA_EFFECTIVE_SIZE; gcm_input_data.aad = aes_aad; gcm_input_data.aad_len = AES_AAD_EFFECTIVE_SIZE; gcm_input_data.output = output_data; gcm_input_data.tag = tag_data; /* Write the key to generate GCMH Subkey */ aes_write_key(AES, gcm_input_data.key); /* Wait for the GCMH to generate */ while (!(aes_read_interrupt_status(AES) & AES_ISR_DATRDY)) { } /* Generate J0 using IV(96 Bits) */ uint32_t i; /* J0 data array */ uint32_t j0[4]; for (i = 0; i < 3; i++) { j0[i] = gcm_input_data.iv[i]; } /* Write the j0 + 1 in IV register */ j0[3] = 0x02000000; aes_write_initvector(AES, (uint32_t *)j0); /* set AADLEN */ aes_write_authen_datalength(AES, gcm_input_data.aad_len); /* Set CLEN */ aes_write_pctext_length(AES, gcm_input_data.text_len); /* Write AAD Data for TAG generation */ uint32_t offset = 0; for (i = 0; i < (AES_AAD_SIZE / 4); i++) { /* write the input data */ aes_write_input_data(AES, (gcm_input_data.aad + offset)); /* Wait till TAG is ready */ while (!(aes_read_interrupt_status(AES) & AES_ISR_DATRDY)) { } /* 16-Byte Boundaries */ offset += 4; } /* Reset offset to zero */ offset = 0; /* Write plain text for cipher text generation */ for (i = 0; i < (AES_PDATA_SIZE / 4); i++) { /* write the input data */ aes_write_input_data(AES, (gcm_input_data.input + offset)); /* Wait for the operation to complete */ while (!(aes_read_interrupt_status(AES) & AES_ISR_DATRDY)) { } aes_read_output_data(AES, (uint32_t *)(gcm_input_data.output + offset)); offset += 4; } if (memcmp(output_data, aes_plain_text, AES_PDATA_EFFECTIVE_SIZE) != 0) { printf("\n\rDEC COMPARE FAILED! "); } else { printf("\n\rDEC COMPARE SUCCESS! "); } }