int32_t zina::aesCbcDecrypt(const string& key, const string& IV, const string& cryptText, string* plainText) { LOGGER(DEBUGGING, __func__, " -->"); if (IV.size() != AES_BLOCK_SIZE) { LOGGER(ERROR, __func__, " <-- IV wrong block size."); return WRONG_BLK_SIZE; } uint8_t* outBuffer = new uint8_t[cryptText.size()]; memcpy(outBuffer, cryptText.data(), cryptText.size()); uint8_t ivTemp[AES_BLOCK_SIZE]; // copy IV, AES code modifies IV buffer memcpy(ivTemp, IV.data(), AES_BLOCK_SIZE); AESdecrypt aes; if (key.size() == 16) aes.key128((const uint8_t*)key.data()); else if (key.size() == 32) aes.key256((const uint8_t*)key.data()); else { LOGGER(ERROR, __func__, " <-- Unsupported key size: ", key.size()); delete[] outBuffer; return UNSUPPORTED_KEY_SIZE; } aes.cbc_decrypt(outBuffer, outBuffer, static_cast<int>(cryptText.size()), ivTemp); plainText->assign((const char*)outBuffer, cryptText.size()); delete[] outBuffer; LOGGER(DEBUGGING, __func__, " <--"); return SUCCESS; }
Plaintext Sekrit::DoDecrypt() const { // Same notes apply here as in DoEncrypt() unsigned char tempiv[sizeof(m_impl->header.iv)]; memcpy(tempiv, m_impl->header.iv, sizeof(tempiv) ); Plaintext plaintext; plaintext.resize(m_impl->header.size); assert(plaintext.size() <= std::numeric_limits<size_t>::max() ); AESdecrypt crypt; crypt.key256(m_impl->key); crypt.cbc_decrypt(&m_impl->data[0], static_cast<unsigned char*>(plaintext.data()), static_cast<int>(plaintext.size()), tempiv); plaintext.resize(m_impl->header.realsize); return plaintext; }