void svCrypto::AESCryptPacket(svAESCrypt mode, svPacket &pkt) { uint32_t length = pkt.GetPayloadLength(); uint8_t *payload = pkt.GetPayload(); switch (mode) { case svAES_ENCRYPT: if (length % aes_key_bits) { pkt.SetPad((uint8_t) (aes_key_bits - (length % aes_key_bits))); RAND_bytes(payload + length, (uint32_t)pkt.GetPad()); length += (uint32_t)pkt.GetPad(); pkt.SetPayloadLength((uint16_t)length); } else pkt.SetPad(0); AESCrypt(mode, aes_key_encrypt, payload, length, payload); break; case svAES_DECRYPT: AESCrypt(mode, aes_key_decrypt, payload, length, payload); pkt.SetPayloadLength(pkt.GetPayloadLength() - pkt.GetPad()); break; } }
//***************************************************************************** // //! main - calls Crypt function after populating either from pre- defined vector //! or from User //! //! \param none //! //! \return none //! //***************************************************************************** void main() { unsigned int uiConfig,uiKeySize,*puiKey1,*puiData,*puiResult=NULL, uiDataLength,uiIV[4]={0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c}; #ifdef USER_INPUT unsigned int uiCharCount; unsigned char* pucResult; #else unsigned int *puiIV; puiIV=&uiIV[0]; #endif BoardInit(); // // Configuring UART for Receiving input and displaying output // 1. PinMux setting // 2. Initialize UART // 3. Displaying Banner // PinMuxConfig(); InitTerm(); DisplayBanner(APP_NAME); // // Enable AES Module // MAP_PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK); // // Enable AES interrupts. // MAP_AESIntRegister(AES_BASE, AESIntHandler); #ifdef USER_INPUT while(FOREVER) { // // Read values either from User or from Vector based on macro USER_INPUT // defined or not // // // Read the values from the user over uart and Populate the variables // puiData = ReadFromUser(&uiConfig,&uiKeySize,&puiKey1,&uiDataLength,\ &puiResult); if((puiData == NULL) || (puiResult == NULL)) { continue; } #else // // Load Default values // puiData = LoadDefaultValues(AES_CFG_DIR_ENCRYPT | AES_CFG_MODE_CBC , &uiConfig,&uiKeySize,&puiIV,&puiKey1, &uiDataLength,&puiResult); if((puiData == NULL) || (puiResult == NULL)) { while(FOREVER); } #endif // // Carry out Encryption // UART_PRINT("\n\r Encryption in progress...."); AESCrypt(uiConfig,uiKeySize,puiKey1,puiData,puiResult,uiDataLength,uiIV); UART_PRINT("\n\r Encryption done, cipher text created"); // // Copy Result into Data Vector to continue with Decryption. and change // config value // memcpy(puiData,puiResult,uiDataLength); uiConfig &= ~(1 << 2); // // Carry out Decryption // UART_PRINT("\n\r\n\r Decryption in progress...."); AESCrypt(uiConfig,uiKeySize,puiKey1,puiData,puiResult,uiDataLength,uiIV); UART_PRINT("\n\r Decryption done"); // // Display/Verify Result // #ifdef USER_INPUT // // Display Plain Text // UART_PRINT("\n\r Text after decryption "); pucResult = (unsigned char *)puiResult; for(uiCharCount=0;uiCharCount<uiDataLength;uiCharCount++) { UART_PRINT("%c",*(pucResult+uiCharCount)); } UART_PRINT("\n\r"); if(puiResult) { free(puiResult); } if(puiData) { free(puiData); } } #else // // Compare Cipher Text and Plain Text with the expected values from // predefined vector // if(memcmp(puiData,psAESCBCTestVectors.pui32CipherText, psAESCBCTestVectors.ui32DataLength)==0) { UART_PRINT("\n\r\n\r Encryption verification Successful"); } else { UART_PRINT("\n\r\n\r Error in Encryption"); } if(memcmp(puiResult,psAESCBCTestVectors.pui32PlainText, psAESCBCTestVectors.ui32DataLength)==0) { UART_PRINT("\n\r Decryption verification Successful"); } else { UART_PRINT("\n\r\n\r Error in Decryption"); } while(FOREVER); #endif }
QByteArray VanityDB::getEncryptedData(QString password) { return AESCrypt(data.toLocal8Bit(), password); }
bool VanityDB::setEncryptedData(const QByteArray input, QString password) { QString decryptedData = QString(AESCrypt(input, password, true)); return setData(decryptedData); }