4using System.Threading.Tasks;
14 private readonly
static BigInteger p = BigInteger.Pow(2, 130) - 5;
15 private readonly BigInteger r;
16 private readonly BigInteger s;
26 throw new ArgumentException(
"Poly1305 keys must be 32 bytes (256 bits) long.", nameof(Key));
28 byte[] rBin =
new byte[(Key[15] & 0x80) != 0 ? 17 : 16];
29 byte[] sBin =
new byte[(Key[31] & 0x80) != 0 ? 17 : 16];
31 Array.Copy(Key, 0, rBin, 0, 16);
32 Array.Copy(Key, 16, sBin, 0, 16);
42 this.r =
new BigInteger(rBin);
43 this.s =
new BigInteger(sBin);
69 byte[] Bin =
new byte[17];
71 BigInteger Accumulator = BigInteger.Zero;
75 j = Math.Min(c - i, 16);
76 Array.Copy(Data, i, Bin, 0, j);
82 Accumulator = BigInteger.Remainder((Accumulator +
new BigInteger(Bin)) * this.r, p);
85 Accumulator += this.s;
87 byte[] Result = Accumulator.ToByteArray();
89 if (Result.Length != 16)
90 Array.Resize(ref Result, 16);
100 public async Task<byte[]>
CalcMac(Stream Data)
103 long c = Data.Length;
104 byte[] Bin =
new byte[17];
106 BigInteger Accumulator = BigInteger.Zero;
110 j = (int)Math.Min(c - i, 16);
111 if (j != await Data.ReadAsync(Bin, 0, j))
112 throw new IOException(
"Unexpected end of file.");
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).