/** * \brief ECB mode encryption test with DMA. */ static void ecb_mode_test_dma(void) { printf("\r\n-----------------------------------\r\n"); printf("- 128bit cryptographic key\r\n"); printf("- ECB cipher mode\r\n"); printf("- DMA mode\r\n"); printf("- 4 32bit words with DMA\r\n"); printf("-----------------------------------\r\n"); //! [encryption_mode] 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_ECB_MODE; g_aes_cfg.cfb_size = AES_CFB_SIZE_128; g_aes_cfg.lod = false; aes_set_config(&aes_instance,AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(&aes_instance, key128); /* The initialization vector is not used by the ECB cipher mode. */ dma_start_transfer_job(&example_resource_tx); aes_set_new_message(&aes_instance); aes_clear_new_message(&aes_instance); /* Wait DMA transfer */ while (false == state) { } /* Wait for the end of the encryption process. */ while (!(aes_get_status(&aes_instance) & AES_ENCRYPTION_COMPLETE)) { } state = false; dma_start_transfer_job(&example_resource_rx); /* Wait DMA transfer */ while (false == state) { } if ((ref_cipher_text_ecb[0] != output_data[0]) || (ref_cipher_text_ecb[1] != output_data[1]) || (ref_cipher_text_ecb[2] != output_data[2]) || (ref_cipher_text_ecb[3] != output_data[3])) { printf("\r\nKO!!!\r\n"); } else { printf("\r\nOK!!!\r\n"); } //! [encryption_mode] }
/** * \brief Test ECB mode encryption with DMA. * * \param test Current test case. */ static void run_ecb_mode_test_dma(const struct test_case *test) { /* Configure DMAC. */ configure_dma_aes_wr(); configure_dma_aes_rd(); /* 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_ECB_MODE; g_aes_cfg.cfb_size = AES_CFB_SIZE_128; g_aes_cfg.lod = false; aes_set_config(&aes_instance,AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(&aes_instance, key128); dma_start_transfer_job(&example_resource_tx); aes_set_new_message(&aes_instance); aes_clear_new_message(&aes_instance); /* Wait for the end of the encryption process. */ delay_ms(30); dma_start_transfer_job(&example_resource_rx); delay_ms(30); if ((ref_cipher_text_ecb[0] != output_data[0]) || (ref_cipher_text_ecb[1] != output_data[1]) || (ref_cipher_text_ecb[2] != output_data[2]) || (ref_cipher_text_ecb[3] != output_data[3])) { flag = false; } else { flag = true; } test_assert_true(test, flag == true, "ECB mode encryption with DMA 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("- 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_instance,AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(&aes_instance, key128); /* Set the initialization vector. */ aes_write_init_vector(&aes_instance, init_vector_ctr); aes_set_new_message(&aes_instance); /* Write the data to be ciphered to the input data registers. */ aes_write_input_data(&aes_instance, ref_plain_text); aes_clear_new_message(&aes_instance); /* 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("- 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_MANUAL_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_instance,AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(&aes_instance, key128); /* Set the initialization vector. */ aes_write_init_vector(&aes_instance, init_vector_ctr); /* Write the data to be deciphered to the input data registers. */ aes_write_input_data(&aes_instance, ref_cipher_text_ctr); aes_set_new_message(&aes_instance); aes_start(&aes_instance); aes_clear_new_message(&aes_instance); /* 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_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_instance,AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(&aes_instance, key128); /* Set the initialization vector. */ aes_write_init_vector(&aes_instance, init_vector_ctr); aes_set_new_message(&aes_instance); /* Write the data to be ciphered to the input data registers. */ aes_write_input_data(&aes_instance, ref_plain_text); aes_clear_new_message(&aes_instance); /* Wait for the end of the encryption process. */ delay_ms(30); aes_read_output_data(&aes_instance,output_data); /* 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_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_instance,AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(&aes_instance, key128); /* Set the initialization vector. */ aes_write_init_vector(&aes_instance, init_vector_ctr); aes_set_new_message(&aes_instance); /* Write the data to be deciphered to the input data registers. */ aes_write_input_data(&aes_instance, ref_cipher_text_ctr); aes_clear_new_message(&aes_instance); /* Wait for the end of the decryption process. */ delay_ms(30); aes_read_output_data(&aes_instance,output_data); /* 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 ECB mode encryption and decryption test. */ static void ecb_mode_test(void) { printf("\r\n-----------------------------------\r\n"); printf("- 128bit cryptographic key\r\n"); printf("- ECB cipher mode\r\n"); printf("- Auto start mode\r\n"); printf("- 4 32bit words\r\n"); printf("-----------------------------------\r\n"); //! [encryption_mode] 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_ECB_MODE; g_aes_cfg.cfb_size = AES_CFB_SIZE_128; g_aes_cfg.lod = false; aes_set_config(&aes_instance,AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(&aes_instance, key128); /* The initialization vector is not used by the ECB cipher mode. */ aes_set_new_message(&aes_instance); /* Write the data to be ciphered to the input data registers. */ aes_write_input_data(&aes_instance, ref_plain_text); aes_clear_new_message(&aes_instance); /* Wait for the end of the encryption process. */ while (!(aes_get_status(&aes_instance) & AES_ENCRYPTION_COMPLETE)) { } aes_read_output_data(&aes_instance,output_data); if ((ref_cipher_text_ecb[0] != output_data[0]) || (ref_cipher_text_ecb[1] != output_data[1]) || (ref_cipher_text_ecb[2] != output_data[2]) || (ref_cipher_text_ecb[3] != output_data[3])) { printf("\r\nKO!!!\r\n"); } else { printf("\r\nOK!!!\r\n"); } //! [encryption_mode] printf("\r\n-----------------------------------\r\n"); printf("- 128bit cryptographic key\r\n"); printf("- ECB decipher mode\r\n"); printf("- Auto start mode\r\n"); printf("- 4 32bit words\r\n"); printf("-----------------------------------\r\n"); //! [decryption_mode] 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_ECB_MODE; g_aes_cfg.cfb_size = AES_CFB_SIZE_128; g_aes_cfg.lod = false; aes_set_config(&aes_instance,AES, &g_aes_cfg); /* Set the cryptographic key. */ aes_write_key(&aes_instance, key128); /* The initialization vector is not used by the ECB cipher mode. */ /* Write the data to be deciphered to the input data registers. */ aes_write_input_data(&aes_instance, ref_cipher_text_ecb); /* Wait for the end of the decryption process. */ while (!(aes_get_status(&aes_instance) & AES_ENCRYPTION_COMPLETE)) { } aes_read_output_data(&aes_instance,output_data); /* 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"); } //! [decryption_mode] }