using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
namespace Amazon.Extensions.S3.Encryption.Util
{
///
/// Container of utilities methods for AES GCM encryption/decryption
///
internal static class AesGcmUtils
{
///
/// Create a buffered cipher to encrypt or decrypt a stream as it is being read
///
/// forEncryption if true the cipher is initialised for encryption, if false for decryption
/// Key to be used for encryption
/// Nonce to be used for encryption
/// Tag size in bits for the tag appended in the end of the stream
/// Additional associated data
///
internal static IBufferedCipher CreateCipher(bool forEncryption, byte[] key, int tagSize, byte[] nonce, byte[] associatedText)
{
var aesEngine = new AesEngine();
var blockCipher = new GcmBlockCipher(aesEngine);
var aeadBlockCipher = new BufferedAeadBlockCipher(blockCipher);
var parameters = new AeadParameters(new KeyParameter(key), tagSize, nonce, associatedText);
aeadBlockCipher.Init(forEncryption, parameters);
return aeadBlockCipher;
}
}
}