I'm using BouncyCastle to encrypt data in C#, using the AES256 GCM algorithm. For this I'm using the implementation provided by James Tuley. Below is a snippet of this code:
public byte[] SimpleEncrypt(byte[] secretMessage, byte[] key, byte[] nonSecretPayload = null)
{
if (key == null || key.Length != KeyBitSize / 8)
throw new ArgumentException($"Key needs to be {KeyBitSize} bit!", nameof(key));
if (secretMessage == null || secretMessage.Length == 0)
throw new ArgumentException("Secret Message Required!", nameof(secretMessage));
nonSecretPayload = nonSecretPayload ?? new byte[] { };
byte[] nonce = _csprng.RandomBytes(NonceBitSize / 8);
var cipher = new GcmBlockCipher(new AesFastEngine());
var parameters = new AeadParameters(new KeyParameter(key), MacBitSize, nonce, nonSecretPayload);
cipher.Init(true, parameters);
var cipherText = new byte[cipher.GetOutputSize(secretMessage.Length)];
int len = cipher.ProcessBytes(secretMessage, 0, secretMessage.Length, cipherText, 0);
cipher.DoFinal(cipherText, len);
using (var combinedStream = new MemoryStream())
{
using (var binaryWriter = new BinaryWriter(combinedStream))
{
binaryWriter.Write(nonSecretPayload);
binaryWriter.Write(nonce);
binaryWriter.Write(cipherText);
}
return combinedStream.ToArray();
}
}
I need to get the authentication tag (mentioned in RFC 5084). It mentions that the authentication tag is part of the output:
AES-GCM generates two outputs: a ciphertext and message authentication code (also called an authentication tag).
I don't understand though how to get the authentication tag from this code? Can anyone help me out?
nonSecretPayloadis a pass-through parameter, andcipherTextis one of the outputs according to standards, that leaves us with thenonce. Could it be that in the standard, they're referring to thenonceas an "authentication tag"? - Cee McSharpface