int aes_cypher(void *out, const void *in, uint32_t size, const void *iv, const void *key, uint32_t keysize, int mode, int encrypt) { int ret = OK; if ((size & (AES_BLOCK_SIZE-1)) != 0) { return -EINVAL; } if (keysize != 16) { return -EINVAL; } ret = sem_wait(&aes_lock); if (ret < 0) { return ret; } /* AES must be disabled before changing mode, key or IV. */ aes_enable(false); ret = aes_setup_cr(mode, encrypt); if (ret < 0) { goto out; } aes_setkey(key, keysize); if (iv) { aes_setiv(iv); } aes_enable(true); while (size) { aes_encryptblock(out, in); out = (uint8_t *)out + AES_BLOCK_SIZE; in = (uint8_t *)in + AES_BLOCK_SIZE; size -= AES_BLOCK_SIZE; } aes_enable(false); out: sem_post(&aes_lock); return ret; }
/** * \brief The main function. */ int main(void) { //! [setup_init] /* Initialize the system and console*/ system_init(); configure_usart(); //! [setup_config] aes_get_config_defaults(&g_aes_cfg); //! [setup_config] //! [setup_config_defaults] aes_init(&aes_instance,AES, &g_aes_cfg); //! [setup_config_defaults] //! [module_enable] aes_enable(&aes_instance); //! [module_enable] //! [setup_init] //! [encryption_decryption] /* Run ECB mode test*/ ecb_mode_test(); //! [encryption_decryption] while (true) { /* Infinite loop */ } }
/** * \brief Run AES driver unit tests. */ int main(void) { const usart_serial_options_t usart_serial_options = { .baudrate = CONF_TEST_BAUDRATE, .charlength = CONF_TEST_CHARLENGTH, .paritytype = CONF_TEST_PARITY, .stopbits = CONF_TEST_STOPBITS }; sysclk_init(); board_init(); stdio_serial_init(CONF_TEST_USART, &usart_serial_options); /* Enable the AES module. */ aes_get_config_defaults(&g_aes_cfg); aes_init(&g_aes_inst, AESA, &g_aes_cfg); aes_enable(&g_aes_inst); /* Enable AES interrupt. */ aes_set_callback(&g_aes_inst, AES_INTERRUPT_INPUT_BUFFER_READY, aes_callback, 1); /* Define all the test cases. */ DEFINE_TEST_CASE(ecb_mode_test, NULL, run_ecb_mode_test, NULL, "SAM AES ECB mode encryption and decryption test."); DEFINE_TEST_CASE(cbc_mode_test, NULL, run_cbc_mode_test, NULL, "SAM AES CBC mode encryption and decryption test."); DEFINE_TEST_CASE(cfb128_mode_test, NULL, run_cfb128_mode_test, NULL, "SAM AES CFB128 mode encryption and decryption test."); DEFINE_TEST_CASE(ofb_mode_test, NULL, run_ofb_mode_test, NULL, "SAM AES OFB mode encryption and decryption test."); DEFINE_TEST_CASE(ctr_mode_test, NULL, run_ctr_mode_test, NULL, "SAM AES CTR mode encryption and decryption test."); DEFINE_TEST_CASE(ecb_mode_test_pdca, NULL, run_ecb_mode_test_pdca, NULL, "SAM AES ECB mode encryption and decryption with PDCA test."); /* Put test case addresses in an array. */ DEFINE_TEST_ARRAY(aes_tests) = { &ecb_mode_test, &cbc_mode_test, &cfb128_mode_test, &ofb_mode_test, &ctr_mode_test, &ecb_mode_test_pdca, }; /* Define the test suite. */ DEFINE_TEST_SUITE(aes_suite, aes_tests, "SAM AES driver test suite"); /* Run all tests in the test suite. */ test_suite_run(&aes_suite); while (1) { /* Busy-wait forever. */ } }
int up_aesuninitialize(void) { uint32_t regval; aes_enable(false); regval = getreg32(STM32_RCC_AHBENR); regval &= ~RCC_AHBENR_AESEN; putreg32(regval, STM32_RCC_AHBENR); sem_destroy(&aes_lock); return OK; }
int up_aesinitialize(void) { uint32_t regval; sem_init(&aes_lock, 0, 1); regval = getreg32(STM32_RCC_AHBENR); regval |= RCC_AHBENR_AESEN; putreg32(regval, STM32_RCC_AHBENR); aes_enable(false); return OK; }
/** * \brief The main function. */ int main(void) { uint8_t key; /* Initialize the SAM system */ sysclk_init(); board_init(); /* Initialize the console */ configure_console(); /* Output example information */ printf("-- AES Example --\r\n"); printf("-- %s\n\r", BOARD_NAME); printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__); /* Enable the AES module. */ aes_get_config_defaults(&g_aes_cfg); aes_init(AES, &g_aes_cfg); aes_enable(); /* Enable AES interrupt. */ aes_enable_interrupt(AES, AES_INTERRUPT_DATA_READY); /* Display menu */ display_menu(); while (1) { scanf("%c", (char *)&key); switch (key) { case 'h': display_menu(); break; case 'e': printf("GCM mode encryption test.\r\n"); gcm_mode_encryption_test(); break; case 'd': printf("GCM mode decryption test.\r\n"); gcm_mode_decryption_test(); break; default: break; } } }
/** * \brief The main function. */ int main(void) { //! [setup_init] /* Initialize the system and console*/ system_init(); configure_usart(); //! [setup_config] aes_get_config_defaults(&g_aes_cfg); //! [setup_config] //! [setup_config_defaults] aes_init(&aes_instance,AES, &g_aes_cfg); //! [setup_config_defaults] //! [module_enable] aes_enable(&aes_instance); //! [module_enable] //! [module_enable_register] /* Enable AES interrupt. */ aes_register_callback(aes_callback,AES_CALLBACK_ENCRYPTION_COMPLETE); aes_enable_callback(&aes_instance,AES_CALLBACK_ENCRYPTION_COMPLETE); //! [module_enable_register] //! [setup_init] printf("Start test\r\n"); //! [encryption_decryption] //![ECB_MODE] ecb_mode_test(); //![ECB_MODE] //![CBC_MODE] cbc_mode_test(); //![CBC_MODE] //![CFB_MODE] cfb128_mode_test(); //![CFB_MODE] //![OFB_MODE] ofb_mode_test(); //![OFB_MODE] //![CTR_MODE] ctr_mode_test(); //![CTR_MODE] //! [encryption_decryption] while(1) ; }
/** * \brief Run AES driver unit tests. */ int main(void) { /* Initialize the system and console*/ system_init(); configure_usart(); /*Initialize the delay driver*/ delay_init(); /* Enable the AES module. */ aes_get_config_defaults(&g_aes_cfg); aes_init(&aes_instance,AES, &g_aes_cfg); aes_enable(&aes_instance); /* Define all the test cases. */ DEFINE_TEST_CASE(ecb_mode_test, NULL, run_ecb_mode_test, NULL, "SAM AES ECB mode encryption and decryption test."); DEFINE_TEST_CASE(cbc_mode_test, NULL, run_cbc_mode_test, NULL, "SAM AES CBC mode encryption and decryption test."); DEFINE_TEST_CASE(cfb128_mode_test, NULL, run_cfb128_mode_test, NULL, "SAM AES CFB128 mode encryption and decryption test."); DEFINE_TEST_CASE(ofb_mode_test, NULL, run_ofb_mode_test, NULL, "SAM AES OFB mode encryption and decryption test."); DEFINE_TEST_CASE(ctr_mode_test, NULL, run_ctr_mode_test, NULL, "SAM AES CTR mode encryption and decryption test."); DEFINE_TEST_CASE(ecb_mode_test_dma, NULL, run_ecb_mode_test_dma, NULL, "SAM AES ECB mode encryption and decryption with DMA test."); /* Put test case addresses in an array. */ DEFINE_TEST_ARRAY(aes_tests) = { &ecb_mode_test, &cbc_mode_test, &cfb128_mode_test, &ofb_mode_test, &ctr_mode_test, &ecb_mode_test_dma, }; /* Define the test suite. */ DEFINE_TEST_SUITE(aes_suite, aes_tests, "SAM AES driver test suite"); /* Run all tests in the test suite. */ test_suite_run(&aes_suite); while (1) { /* Busy-wait forever. */ } }
/** * \brief The main function. */ int main(void) { //! [setup_init] /* Initialize the system and console*/ system_init(); configure_usart(); //! [setup_dma] /* Configure AES DMA and enable callback */ configure_dma_aes_wr(); configure_dma_aes_rd(); dma_register_callback(&example_resource_tx, transfer_tx_rx_done, DMA_CALLBACK_TRANSFER_DONE); dma_enable_callback(&example_resource_tx, DMA_CALLBACK_TRANSFER_DONE); dma_register_callback(&example_resource_rx, transfer_tx_rx_done, DMA_CALLBACK_TRANSFER_DONE); dma_enable_callback(&example_resource_rx, DMA_CALLBACK_TRANSFER_DONE); //! [setup_dma] //! [setup_config] aes_get_config_defaults(&g_aes_cfg); //! [setup_config] //! [setup_config_defaults] aes_init(&aes_instance,AES, &g_aes_cfg); //! [setup_config_defaults] //! [module_enable] aes_enable(&aes_instance); //! [module_enable] //! [setup_init] //! [encryption_decryption] /* ECB mode encryption test with DMA */ ecb_mode_test_dma(); //! [encryption_decryption] while(1) ; }
/** * \brief The main function. */ int main(void) { uint8_t key; /* Initialize the SAM system */ sysclk_init(); board_init(); /* Initialize the console */ configure_console(); /* Output example information */ printf("-- AES Example --\r\n"); printf("-- %s\n\r", BOARD_NAME); printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__); /* Enable the AES module. */ aes_get_config_defaults(&g_aes_cfg); aes_init(&g_aes_inst, AESA, &g_aes_cfg); aes_enable(&g_aes_inst); /* Enable AES interrupt. */ aes_set_callback(&g_aes_inst, AES_INTERRUPT_INPUT_BUFFER_READY, aes_callback, 1); /* Display menu */ display_menu(); while (1) { scanf("%c", (char *)&key); switch (key) { case 'h': display_menu(); break; case '1': printf("ECB mode encryption and decryption test.\r\n"); ecb_mode_test(); break; case '2': printf("CBC mode encryption and decryption test.\r\n"); cbc_mode_test(); break; case '3': printf("CFB128 mode encryption and decryption test.\r\n"); cfb128_mode_test(); break; case '4': printf("OFB mode encryption and decryption test.\r\n"); ofb_mode_test(); break; case '5': printf("CTR mode encryption and decryption test.\r\n"); ctr_mode_test(); break; case 'd': printf("ECB mode encryption and decryption test with PDCA.\r\n"); ecb_mode_test_pdca(); break; default: break; } } }