/************************************************************************************************** * @fn sblInit * * @brief Boot Loader initialization. * * input parameters * * None. * * output parameters * * None. * * @return TRUE if there is a valid RC image; FALSE otherwise. */ uint8 sblInit(void) { aesLoadKey(); #if defined AES_TEST_VECS uint8 checkSig[KEY_BLENGTH] = { 0xB9, 0xD7, 0x89, 0x67, 0x04, 0xBC, 0xFA, 0x20, 0xB2, 0x10, 0x36, 0x74, 0x45, 0xF9, 0x83, 0xD6 }; imgHdr.len = 1; while (1) { aesSignature(testSig); if (memcmp(testSig, checkSig, KEY_BLENGTH)) // If signature generated is not value expected. { ASM_NOP; } } #else SBL_READ_IMG_HDR(); return checkRC(); #endif }
/************************************************************************************************** * @fn imgAuth * * @brief Run the AES CTR decryption over the image area specified. * @brief Run the AES CRC-MAC calculation over the image specified and set the image * ready-to-run if the Signature is verified. * * input parameters * * @param imgSel - Image select: 0 for Image-A and 1 for Image-B. * @param imgHdr - Pointer to the Image Header corresponding to the image select. * * output parameters * * None. * * @return TRUE if the image Signature verifies; FALSE otherwise. */ static uint8 imgAuth(uint8 imgSel, img_hdr_t *imgHdr) { if ((imgHdr->crc0 == 0) || (imgHdr->crc0 == 0xFFFF)) { return FALSE; } if (imgHdr->crc0 != imgHdr->crc1) { aesLoadKey(); aes_hdr_t aesHdr; HalFlashRead(ImgPageBeg[imgSel], BEM_AES_OSET, (uint8 *)&aesHdr, sizeof(aes_hdr_t)); // Image length in 3 bytes, MSB to LSB order - the Signature bytes are not to be included. uint32 imageLen = (uint32)HAL_FLASH_PAGE_SIZE * imgHdr->res[0] - KEY_BLENGTH; aesHdr.spare[0] = ((uint8 *)&imageLen)[0]; aesHdr.spare[1] = ((uint8 *)&imageLen)[1]; aesHdr.spare[2] = ((uint8 *)&imageLen)[2]; aesHdr.spare[3] = imgHdr->res[0]; if (aesSignature(imgSel, &aesHdr)) { uint16 crc[2] = { 0xFFFF, imgHdr->crc0 }; BEM_NVM_SET(ImgPageBeg[imgSel], (uint8 *)crc, 4); BEM_NVM_GET(ImgPageBeg[imgSel], (uint8 *)&imgHdr, 4); } } return (imgHdr->crc0 == imgHdr->crc1); }
/************************************************************************************************** * @fn imgCrypt * * @brief Run the AES CTR decryption over the image area specified. * * input parameters * * @param imgSel - Image select: 0 for Image-A and 1 for Image-B. * @param imgHdr - Pointer to the Image Header corresponding to the image select. * * output parameters * * None. * * @return None. */ static void imgCrypt(uint8 imgSel, img_hdr_t *imgHdr) { aesLoadKey(); uint8 pgEnd = imgHdr->res[0] + ImgPageBeg[imgSel]; if (imgSel == 0) { pgEnd += ImgPageLen[1]; } for (uint8 pgNum = ImgPageBeg[imgSel]; pgNum < pgEnd; ) { BEM_NVM_GET(pgNum, pageBuf, HAL_FLASH_PAGE_SIZE); // EBL is used to encrypt the image in SBL_RW_BUF_LEN blocks, so it must be decrypted likewise. for (uint8 blk = 0; blk < (HAL_FLASH_PAGE_SIZE / SBL_RW_BUF_LEN); blk++) { if ((pgNum == ImgPageBeg[imgSel]) && (blk == 0)) { aesCrypt(1, pageBuf + ((uint16)SBL_RW_BUF_LEN * blk)); } else { aesCrypt(0, pageBuf + ((uint16)SBL_RW_BUF_LEN * blk)); } } HalFlashErase(pgNum); BEM_NVM_SET(pgNum, pageBuf, HAL_FLASH_PAGE_SIZE); pgNum++; if ((imgSel == 0) && (pgNum == ImgPageBeg[1])) { pgNum += ImgPageLen[1]; } } }