CryptoBuffer CommonCryptoCipher::DecryptBuffer(const CryptoBuffer& encryptedData) { if (m_failure) { AWS_LOGSTREAM_FATAL(CC_LOG_TAG, "Cipher not properly initialized for decryption. Aborting"); return CryptoBuffer(); } CheckInitDecryptor(); size_t lengthWritten = encryptedData.GetLength() + (GetBlockSizeBytes() - 1); CryptoBuffer decryptedText(static_cast<size_t>(lengthWritten)); CCStatus status = CCCryptorUpdate(m_cryptoHandle, encryptedData.GetUnderlyingData(), encryptedData.GetLength(), decryptedText.GetUnderlyingData(), decryptedText.GetLength(), &lengthWritten); if (status != kCCSuccess) { m_failure = true; AWS_LOGSTREAM_ERROR(CC_LOG_TAG, "Decryption of buffer failed with status code: " << status); return CryptoBuffer(); } if (lengthWritten < decryptedText.GetLength()) { return CryptoBuffer(decryptedText.GetUnderlyingData(), static_cast<size_t>(lengthWritten)); } return decryptedText; }
CryptoBuffer AES_KeyWrap_Cipher_CommonCrypto::DecryptBuffer(const CryptoBuffer& encryptedData) { CheckInitDecryptor(); m_workingKeyBuffer = CryptoBuffer({&m_workingKeyBuffer, (CryptoBuffer*)&encryptedData}); return CryptoBuffer(); }
CryptoBuffer CommonCryptoCipher::FinalizeDecryption() { if (m_failure) { AWS_LOGSTREAM_FATAL(CC_LOG_TAG, "Cipher not properly initialized for decryption finalization. Aborting"); return CryptoBuffer(); } CryptoBuffer finalBlock(GetBlockSizeBytes()); size_t writtenSize = static_cast<int>(finalBlock.GetLength()); CCStatus status = CCCryptorFinal(m_cryptoHandle, finalBlock.GetUnderlyingData(), finalBlock.GetLength(), &writtenSize); if (status != kCCSuccess) { m_failure = true; AWS_LOGSTREAM_ERROR(CC_LOG_TAG, "Decryption of buffer failed with status code: " << status); return CryptoBuffer(); } return CryptoBuffer(finalBlock.GetUnderlyingData(), writtenSize); }
CryptoBuffer AES_KeyWrap_Cipher_CommonCrypto::FinalizeDecryption() { CheckInitDecryptor(); size_t outputBufferLength = m_workingKeyBuffer.GetLength() - GetBlockSizeBytes(); CryptoBuffer outputBuffer(outputBufferLength); CCCryptorStatus status = CCSymmetricKeyUnwrap(kCCWRAPAES, CCrfc3394_iv, CCrfc3394_ivLen, m_key.GetUnderlyingData(), m_key.GetLength(), m_workingKeyBuffer.GetUnderlyingData(), m_workingKeyBuffer.GetLength(), outputBuffer.GetUnderlyingData(), &outputBufferLength); if(status != kCCSuccess) { m_failure = true; AWS_LOGSTREAM_ERROR(AES_KEY_WRAP_LOG_TAG, "Key unwrap failed with status code " << status); return CryptoBuffer(); } return outputBuffer; }
void AES_KeyWrap_Cipher_CommonCrypto::Reset() { CommonCryptoCipher::Reset(); m_workingKeyBuffer = CryptoBuffer(); }