Files
SuperVPN/output/Libraries/BouncyCastle.Crypto/Org/BouncyCastle/Crypto/Macs/GMac.cs
2025-10-09 09:57:24 +09:00

71 lines
1.4 KiB
C#

using System;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
namespace Org.BouncyCastle.Crypto.Macs;
public class GMac : IMac
{
private readonly GcmBlockCipher cipher;
private readonly int macSizeBits;
public string AlgorithmName => cipher.GetUnderlyingCipher().AlgorithmName + "-GMAC";
public GMac(GcmBlockCipher cipher)
: this(cipher, 128)
{
}
public GMac(GcmBlockCipher cipher, int macSizeBits)
{
this.cipher = cipher;
this.macSizeBits = macSizeBits;
}
public void Init(ICipherParameters parameters)
{
if (parameters is ParametersWithIV)
{
ParametersWithIV parametersWithIV = (ParametersWithIV)parameters;
byte[] iV = parametersWithIV.GetIV();
KeyParameter key = (KeyParameter)parametersWithIV.Parameters;
cipher.Init(forEncryption: true, new AeadParameters(key, macSizeBits, iV));
return;
}
throw new ArgumentException("GMAC requires ParametersWithIV");
}
public int GetMacSize()
{
return macSizeBits / 8;
}
public void Update(byte input)
{
cipher.ProcessAadByte(input);
}
public void BlockUpdate(byte[] input, int inOff, int len)
{
cipher.ProcessAadBytes(input, inOff, len);
}
public int DoFinal(byte[] output, int outOff)
{
try
{
return cipher.DoFinal(output, outOff);
}
catch (InvalidCipherTextException ex)
{
throw new InvalidOperationException(ex.ToString());
}
}
public void Reset()
{
cipher.Reset();
}
}