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::EncryptBuffer(const CryptoBuffer& unEncryptedData) { CheckInitEncryptor(); m_workingKeyBuffer = CryptoBuffer({&m_workingKeyBuffer, (CryptoBuffer*)&unEncryptedData}); return CryptoBuffer(); }
CryptoBuffer AES_KeyWrap_Cipher_CommonCrypto::FinalizeEncryption() { CheckInitEncryptor(); size_t outputBufferLength = GetBlockSizeBytes() + m_workingKeyBuffer.GetLength(); CryptoBuffer outputBuffer(outputBufferLength); CCCryptorStatus status = CCSymmetricKeyWrap(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 wrap failed with status code " << status); return CryptoBuffer(); } return outputBuffer; }