/************************************************************************************************** * @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]; } } }
int main(int argc, char** argv) { uint8_t *key = (uint8_t *)"01234567899876543210"; if(argc > 2) usage(); else if(argc == 2) key = (uint8_t *)argv[1]; uint32_t salt[] = {12345, 54321}; string user, pass; uint8_t *testText = NULL; int len; uint8_t *cifrado; bool exit = false; string aux ; PracticaCaso::SQLiteMap * SQLiteMap = new PracticaCaso::SQLiteMap("logins.db"); AESUtil aesCrypt(key, salt); cout << "#############################################\n"; cout << "# Login password database store util #\n"; cout << "#############################################\n"; while(!exit) { cout << "Insert login: "******"Insert password: "******"Inserted in database!" << endl; cout << "continue?[y/n]"; cin >> aux; if(strcmp(aux.c_str(),"n") == 0) exit = true; } return 0; }
string encryptMsg(string textDec) { //neccesary variables for AES encryptation uint32_t salt[] = {12345, 54321}; //to salt the AES. mmmmmmm... tasty :D uint8_t *auxPass = NULL; int len; uint8_t *cipherPass; uint8_t *key = (uint8_t *)"0123456789ABCDEF0123"; AESUtil aesCrypt(key, salt); string msg; vector<string> commandVec; //encrypt the message len = textDec.size(); auxPass = (uint8_t *)textDec.c_str(); cipherPass = aesCrypt.encrypt(auxPass, &len); //cast unsigned char to char and then to string :s, double casting yai! msg = (string)(char *)cipherPass; return msg; }
/************************************************************************************************** * @fn sblProc * * @brief Process the SB command and received buffer. * * input parameters * * None. * * output parameters * * None. * * @return None. */ static void sblProc(void) { uint16 t16 = BUILD_UINT16(sbBuf[SBL_REQ_ADDR_LSB], sbBuf[SBL_REQ_ADDR_MSB]) + SBL_ADDR_BEG; uint8 len = 1, rsp = SBL_SUCCESS; switch (sbBuf[RPC_POS_CMD1]) { case SBL_WRITE_CMD: if ((t16 >= SBL_ADDR_BEG) && (t16 <= SBL_ADDR_END)) { if ((t16 % SBL_PAGE_LEN) == 0) { HalFlashErase(t16 / SBL_PAGE_LEN); } if (SBL_SECURE) { if (t16 == SBL_ADDR_IMG_HDR) { if (!imgHdrCheck(sbBuf + SBL_REQ_DAT0)) { rsp = SBL_FAILURE; break; } aesCrypt(1, sbBuf + SBL_REQ_DAT0); } else { aesCrypt(0, sbBuf + SBL_REQ_DAT0); } } SBL_NVM_SET(t16, (sbBuf + SBL_REQ_DAT0), SBL_RW_BUF_LEN); // Immediately read back what was written to keep the 'imgHdr' variable in sync with flash. if (t16 == SBL_ADDR_IMG_HDR) { SBL_READ_IMG_HDR(); } } else { rsp = SBL_FAILURE; } break; case SBL_READ_CMD: if ((t16 >= SBL_ADDR_BEG) && (t16 <= SBL_ADDR_END)) { len = SBL_RW_BUF_LEN + SBL_READ_HDR_LEN; sbBuf[SBL_RSP_ADDR_MSB] = sbBuf[SBL_REQ_ADDR_MSB]; sbBuf[SBL_RSP_ADDR_LSB] = sbBuf[SBL_REQ_ADDR_LSB]; SBL_NVM_GET(t16, (sbBuf + SBL_RSP_DAT0), SBL_RW_BUF_LEN); if (SBL_SECURE || (SBL_SIGNER && signMode)) { if (t16 == SBL_ADDR_IMG_HDR) { aesCrypt(1, sbBuf + SBL_RSP_DAT0); } else { aesCrypt(0, sbBuf + SBL_RSP_DAT0); } } } else { rsp = SBL_FAILURE; } break; case SBL_ENABLE_CMD: if (SBL_SIGNER) // A Signer must never enable the image for clean read back with crc[1]=0xFFFF. { signMode = FALSE; // PC Tool read back must be un-encrypted after downloading a new image. } else if (!SBL_SECURE) { imgHdr.crc[1] = imgHdr.crc[0]; imgHdr.crc[0] = 0xFFFF; SBL_NVM_SET(SBL_ADDR_CRC, imgHdr.crc, sizeof(imgHdr.crc)); SBL_READ_IMG_HDR(); } else if (!checkRC()) { rsp = SBL_VALIDATE_FAILED; } break; case SBL_HANDSHAKE_CMD: break; case SBL_SIGNATURE_CMD: len = ((rsp = procSignatureCmd((sbBuf + RPC_POS_DAT0 + 1))) == SBL_SUCCESS) ? 17 : 1; break; default: rsp = SBL_FAILURE; break; } sbBuf[RPC_POS_LEN] = len; sbBuf[RPC_POS_CMD1] |= SBL_RSP_MASK; sbBuf[RPC_POS_DAT0] = rsp; }