6621x 系列 AES128 硬件只支持 AES CBC模式。 每次加密的长度为16字节,128位。 超过128位,需要分组进行依次加密。 待加密的数据长度应该对齐为16的倍数。 本例程 假设数据长度为 64x16。 加密为异步形式。 /* 1- creat 3 task ,and test send and recv msg ,detect delay... *** TASK ID *** TASK Descr *** MSG and MSG handle Table *** State *** MSG Parameter Struct 2- set a button interrut , test the delay time between inter and isr 3- demo how to use aes128 to descrpyt a set of data */ // ------------------ AES ------------------ #define aes_start_evt (KE_EVENT_USR_FIRST + 4) #define aes_end_evt (KE_EVENT_USR_FIRST + 5) #define AES_LEN (16*64) uint32_t aes_offset; char aes_key[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15}; char aes_data_in[AES_LEN]; char aes_data_out[AES_LEN]; void appm_aes(const uint8_t *key, const uint8_t *data, uint32_t data_len) { struct gapm_use_enc_block_cmd *enc_cmd = KE_MSG_ALLOC(GAPM_USE_ENC_BLOCK_CMD, TASK_GAPM, TASK_USER_0, gapm_use_enc_block_cmd); // Fill the allocated kernel message enc_cmd->operation = GAPM_USE_ENC_BLOCK; // set operand_1 value memcpy(&enc_cmd->operand_1[0], key, data_len); // set operand_2 value memcpy(&enc_cmd->operand_2[0], data, data_len); // Send the message ke_msg_send(enc_cmd); log_debug("_"); } static int gapm_use_enc_block_ind_handler(ke_msg_id_t const msgid, struct gapm_use_enc_block_ind *param, ke_task_id_t const dest_id, ke_task_id_t const src_id) { memcpy(&aes_data_out[aes_offset], param->result, GAP_KEY_LEN); log_debug("#"); aes_offset += 16; if (aes_offset >= AES_LEN) { ke_event_set(aes_end_evt); } else { appm_aes(aes_key, &aes_data_in[aes_offset], GAP_KEY_LEN); } return KE_MSG_CONSUMED; } void aes_start_evt_callback(void) { int i; ke_event_clear(aes_start_evt); log_debug("\naes crypt start..."); for (i = 0; i < AES_LEN; i++) { aes_data_in[i] = i; } aes_offset = 0; log_debug_array_ex("Data In", aes_data_in, AES_LEN); appm_aes(aes_key, &aes_data_in[aes_offset], GAP_KEY_LEN); } void aes_end_evt_callback(void) { ke_event_clear(aes_end_evt); log_debug("\naes crypt finish..."); log_debug_array_ex("Data Out", aes_data_out, AES_LEN); } void aes_demo(void) { ke_event_callback_set(aes_start_evt, aes_start_evt_callback); ke_event_callback_set(aes_end_evt, aes_end_evt_callback);//aes } // ------------------ AES ------------------ //Task Msg and Handler KE_MSG_HANDLER_TAB(msg_user0) { // GAPC messages {KE_MSG_DEFAULT_HANDLER, (ke_msg_func_t)task_0_default_msg_handler}, {GAPM_USE_ENC_BLOCK_IND, (ke_msg_func_t)gapm_use_enc_block_ind_handler}, {usr_msg_20, (ke_msg_func_t)usr_msg_20_handler}, {usr_msg_21, (ke_msg_func_t)usr_msg_21_handler}, }; 调用 void aes_demo(void); main() { ....... aes_demo(); while (1) { rwip_schedule(); } } 参考例程如附件所示: |