CryptoBuffer CommonCryptoCipher::EncryptBuffer(const CryptoBuffer& unEncryptedData) { if (m_failure) { AWS_LOGSTREAM_FATAL(CC_LOG_TAG, "Cipher not properly initialized for encryption. Aborting"); return CryptoBuffer(); } CheckInitEncryptor(); size_t lengthWritten = unEncryptedData.GetLength() + (GetBlockSizeBytes() - 1); CryptoBuffer encryptedText(static_cast<size_t>( lengthWritten + (GetBlockSizeBytes() - 1))); CCStatus status = CCCryptorUpdate(m_cryptoHandle, unEncryptedData.GetUnderlyingData(), unEncryptedData.GetLength(), encryptedText.GetUnderlyingData(), encryptedText.GetLength(), &lengthWritten); if (status != kCCSuccess) { m_failure = true; AWS_LOGSTREAM_ERROR(CC_LOG_TAG, "Encryption of buffer failed with status code: " << status); return CryptoBuffer(); } if (lengthWritten < encryptedText.GetLength()) { return CryptoBuffer(encryptedText.GetUnderlyingData(), lengthWritten); } return encryptedText; }
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; }
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); }