TI bim中使用AES-CBC加解密

时间:2024-10-29 16:00:04
/******************************************************************************* * EXTERNAL FUNCTIONS */ uint8_t rtf_ota_bl_execute_version_fallback(void) { int ret = 0; uint32_t addr = 0x00; uint8_t ota_bl_install_event = RTF_OTA_EVENT_EDGE_REVERT_SUCCESS; uint32_t imgOffset = OTA_BACKUP_IMAGE_ADDRE; uint8_t RevertSystem = RTF_OTA_BL_OTA_REVERTSYSTEM_DISABLE; uint8_t ProvisionalPeriod = 0; uint8_t ota_state = RTF_OTA_EDGE_REVERTED_IMAGE; uint8_t ota_update_image_valid = RTF_OTA_EDGE_OTA_UPDATE_IMAGE_UNVALID; uint8_t OtaBackupFwVersion[4] = {0}; extFlashOpen(); bimDelay(1); memset(imagefinalHash, 0, DIGEST_SIZE); SHA2_open(); for(;;) { memset(image_input_data, 0, IMAGE_BLOCK_SIZE); extFlashRead(imgOffset, IMAGE_BLOCK_SIZE, image_input_data); imgOffset+=IMAGE_BLOCK_SIZE; if(imgOffset == (APP_FLASH_MAX_SIZE + OTA_BACKUP_IMAGE_ADDRE)) { break; }else{ SHA2_addData(image_input_data, IMAGE_BLOCK_SIZE); } } SHA2_finalize(imagefinalHash); SHA2_close(); extFlashClose(); imgOffset=OTA_BACKUP_IMAGE_ADDRE; ret = memcmp(BOOT_NV_Config_buff.BOOT_NV_Buf.OtaBackupImageSHA2, imagefinalHash, DIGEST_SIZE); if (ret != 0) { RevertSystem = RTF_OTA_BL_OTA_REVERTSYSTEM_FAIL; ota_state = RTF_OTA_EDGE_NORMAL_IMAGE; ota_bl_install_event = RTF_OTA_EVENT_EDGE_REVERT_FAILED; updateBootNVSegmentbyType(BOOT_NV_PROVISIONAL_PERIOD, &ProvisionalPeriod, sizeof(uint8_t)); updateBootNVSegmentbyType(BOOT_NV_REVERT_SYSTEM, &RevertSystem, sizeof(uint8_t)); updateBootNVSegmentbyType(BOOT_NV_OTA_STATE, &ota_state, sizeof(uint8_t)); updateBootNVSegmentbyType(BOOT_NV_OTA_UPDATE_IMAGE_VALID, &ota_update_image_valid, sizeof(uint8_t)); updateBootNVSegmentbyType(BOOT_NV_OTA_INSTALL_EVENT, &ota_bl_install_event, sizeof(uint8_t)); updateBootNvRecord(); return BOOT_NV_ERR; } eraseFWStartFlash(); rtf_bl_init_cbc_iv(); for(;;) { memset(image_input_data, 0, IMAGE_BLOCK_SIZE); memset(image_output_data, 0, IMAGE_BLOCK_SIZE); memset(image_backup_data, 0, IMAGE_BLOCK_SIZE); bimExtFlashRead(imgOffset, IMAGE_BLOCK_SIZE, image_input_data); SHA2_open(); execute_aes_cbc_decrypt(image_input_data, image_output_data); update_aes_cbc_iv_value(image_input_data); SHA2_close(); writeFlash(addr, image_output_data, IMAGE_BLOCK_SIZE); readFlash(addr, image_backup_data, IMAGE_BLOCK_SIZE); ret = memcmp(image_backup_data, image_output_data, IMAGE_BLOCK_SIZE); if (ret == 0) { if(imgOffset == (APP_FLASH_MAX_SIZE+OTA_BACKUP_IMAGE_ADDRE)) { break; }else{ addr += IMAGE_BLOCK_SIZE; imgOffset += IMAGE_BLOCK_SIZE; } }else{ return BOOT_NV_ERR; } } RevertSystem = RTF_OTA_BL_OTA_REVERTSYSTEM_ENABLE; //Get Backup firmware version fetchBootNVSegmentbyType(BOOT_NV_OTA_BACKUP_FW_VERSION, OtaBackupFwVersion, sizeof(OtaBackupFwVersion)); updateBootNVSegmentbyType(BOOT_NV_APP_FW_VERSION, OtaBackupFwVersion, sizeof(OtaBackupFwVersion)); updateBootNVSegmentbyType(BOOT_NV_APP_IMAGE_SHA2, imagefinalHash, sizeof(imagefinalHash)); updateBootNVSegmentbyType(BOOT_NV_PROVISIONAL_PERIOD, &ProvisionalPeriod, sizeof(uint8_t)); updateBootNVSegmentbyType(BOOT_NV_REVERT_SYSTEM, &RevertSystem, sizeof(uint8_t)); updateBootNVSegmentbyType(BOOT_NV_OTA_STATE, &ota_state, sizeof(uint8_t)); updateBootNVSegmentbyType(BOOT_NV_OTA_INSTALL_EVENT, &ota_bl_install_event, sizeof(uint8_t)); updateBootNVSegmentbyType(BOOT_NV_OTA_UPDATE_IMAGE_VALID, &ota_update_image_valid, sizeof(uint8_t)); updateBootNvRecord(); jumpToPrgEntry(FW_START_ADDRESS); return 1; } uint8_t rtf_ota_bl_ota_execute_backup_app_image(uint8_t* Version) { int ret = 0; uint32_t addr = 0x00; uint32_t imgOffset = OTA_BACKUP_IMAGE_ADDRE; uint8_t ota_Backup_image_valid = RTF_OTA_EDGE_OTA_BACKUP_IMAGE_VALID; memset(imagefinalHash, 0, DIGEST_SIZE); SHA2_open(); rtf_bl_init_cbc_iv(); for (;;) { memset(image_input_data, 0, IMAGE_BLOCK_SIZE); memset(image_output_data,0,IMAGE_BLOCK_SIZE); ret = readFlash(addr, image_input_data, IMAGE_BLOCK_SIZE); if (ret == FLASH_SUCCESS) { addr += IMAGE_BLOCK_SIZE; if(addr == APP_FLASH_MAX_SIZE) { break; }else{ execute_application_cbc_encrypt(image_input_data, image_output_data); update_aes_cbc_iv_value(image_output_data); SHA2_addData(image_output_data, IMAGE_BLOCK_SIZE); } } else { SHA2_finalize(imagefinalHash); SHA2_close(); ota_Backup_image_valid = RTF_OTA_EDGE_OTA_UPDATE_IMAGE_UNVALID; updateBootNVSegmentbyType(BOOT_NV_OTA_BACKUP_IMAGE_VALID, &ota_Backup_image_valid, sizeof(uint8_t)); updateBootNvRecord(); return 0; } } SHA2_finalize(imagefinalHash); SHA2_close(); addr=0; updateBootNVSegmentbyType(BOOT_NV_APP_IMAGE_SHA2, imagefinalHash, sizeof(imagefinalHash)); bimExtFlashErase(imgOffset, OTA_BACKUP_IMAGE_SIZE); rtf_bl_init_cbc_iv(); for (;;) { memset(image_input_data,0,IMAGE_BLOCK_SIZE); memset(image_output_data,0,IMAGE_BLOCK_SIZE); memset(image_backup_data,0,IMAGE_BLOCK_SIZE); readFlash(addr, image_input_data, IMAGE_BLOCK_SIZE); SHA2_open(); execute_application_cbc_encrypt(image_input_data, image_output_data); update_aes_cbc_iv_value(image_output_data); SHA2_close(); bimExtFlashwrite(imgOffset, IMAGE_BLOCK_SIZE, image_output_data); bimExtFlashRead(imgOffset, IMAGE_BLOCK_SIZE, image_backup_data); ret = memcmp(image_backup_data, image_output_data, IMAGE_BLOCK_SIZE); if (ret == 0) { if(addr == APP_FLASH_MAX_SIZE) { updateBootNVSegmentbyType(BOOT_NV_OTA_BACKUP_IMAGE_SHA2, imagefinalHash, sizeof(imagefinalHash)); break; }else{ addr += IMAGE_BLOCK_SIZE; imgOffset += IMAGE_BLOCK_SIZE; } }else{ ota_Backup_image_valid = RTF_OTA_EDGE_OTA_UPDATE_IMAGE_UNVALID; updateBootNVSegmentbyType(BOOT_NV_OTA_BACKUP_IMAGE_VALID, &ota_Backup_image_valid, sizeof(uint8_t)); updateBootNvRecord(); return 0; } } updateBootNVSegmentbyType(BOOT_NV_OTA_BACKUP_IMAGE_VALID, &ota_Backup_image_valid, sizeof(uint8_t)); updateBootNVSegmentbyType(BOOT_NV_OTA_BACKUP_FW_VERSION, Version, 4); updateBootNvRecord(); return 1; }