4using System.Threading.Tasks;
15 private readonly
static BigInteger p = BigInteger.Pow(2, 130) - 5;
16 private readonly BigInteger r;
17 private readonly BigInteger s;
27 throw new ArgumentException(
"Poly1305 keys must be 32 bytes (256 bits) long.", nameof(Key));
29 byte[] rBin =
new byte[(Key[15] & 0x80) != 0 ? 17 : 16];
30 byte[] sBin =
new byte[(Key[31] & 0x80) != 0 ? 17 : 16];
32 Buffer.BlockCopy(Key, 0, rBin, 0, 16);
33 Buffer.BlockCopy(Key, 16, sBin, 0, 16);
43 this.r =
new BigInteger(rBin);
44 this.s =
new BigInteger(sBin);
70 byte[] Bin =
new byte[17];
72 BigInteger Accumulator = BigInteger.Zero;
76 j = Math.Min(c - i, 16);
77 Buffer.BlockCopy(Data, i, Bin, 0, j);
83 Accumulator = BigInteger.Remainder((Accumulator +
new BigInteger(Bin)) * this.r, p);
86 Accumulator += this.s;
88 byte[] Result = Accumulator.ToByteArray();
90 if (Result.Length != 16)
91 Array.Resize(ref Result, 16);
101 public async Task<byte[]>
CalcMac(Stream Data)
104 long c = Data.Length;
105 byte[] Bin =
new byte[17];
107 BigInteger Accumulator = BigInteger.Zero;
111 j = (int)Math.Min(c - i, 16);
112 await Data.ReadAllAsync(Bin, 0, j);
119 Accumulator = BigInteger.Remainder((Accumulator +
new BigInteger(Bin)) * this.r, p);
122 Accumulator += this.s;
124 byte[] Result = Accumulator.ToByteArray();
126 if (Result.Length != 16)
127 Array.Resize(ref Result, 16);
ChaCha20 encryptor, as defined in RFC 8439: https://tools.ietf.org/html/rfc8439
byte[] GetBytes(int NrBytes)
Gets the next number of bytes in the stream.
Poly1305 authenticator, as defined in RFC 8439: https://tools.ietf.org/html/rfc8439
Poly1305(byte[] Key)
Poly1305 authenticator, as defined in RFC 8439: https://tools.ietf.org/html/rfc8439
static Poly1305 FromChaCha20(byte[] Key, byte[] Nonce)
Prepares a Poly1305 instance using ChaCha20 and Counter=0 to generate the corresponding key.
byte[] CalcMac(byte[] Data)
Calculates a message authentication code (MAC).
async Task< byte[]> CalcMac(Stream Data)
Calculates a message authentication code (MAC).