Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Poly1305.cs
1using System;
2using System.IO;
3using System.Numerics;
4using System.Threading.Tasks;
6
8{
13 public class Poly1305
14 {
15 private readonly static BigInteger p = BigInteger.Pow(2, 130) - 5;
16 private readonly BigInteger r;
17 private readonly BigInteger s;
18
24 public Poly1305(byte[] Key)
25 {
26 if (Key.Length != 32)
27 throw new ArgumentException("Poly1305 keys must be 32 bytes (256 bits) long.", nameof(Key));
28
29 byte[] rBin = new byte[(Key[15] & 0x80) != 0 ? 17 : 16];
30 byte[] sBin = new byte[(Key[31] & 0x80) != 0 ? 17 : 16];
31
32 Buffer.BlockCopy(Key, 0, rBin, 0, 16);
33 Buffer.BlockCopy(Key, 16, sBin, 0, 16);
34
35 rBin[3] &= 15;
36 rBin[7] &= 15;
37 rBin[11] &= 15;
38 rBin[15] &= 15;
39 rBin[4] &= 252;
40 rBin[8] &= 252;
41 rBin[12] &= 252;
42
43 this.r = new BigInteger(rBin);
44 this.s = new BigInteger(sBin);
45 }
46
54 public static Poly1305 FromChaCha20(byte[] Key, byte[] Nonce)
55 {
56 ChaCha20 Cipher = new ChaCha20(Key, 0, Nonce);
57 byte[] Key2 = Cipher.GetBytes(32);
58 return new Poly1305(Key2);
59 }
60
66 public byte[] CalcMac(byte[] Data)
67 {
68 int i = 0;
69 int c = Data.Length;
70 byte[] Bin = new byte[17];
71 int j;
72 BigInteger Accumulator = BigInteger.Zero;
73
74 while (i < c)
75 {
76 j = Math.Min(c - i, 16);
77 Buffer.BlockCopy(Data, i, Bin, 0, j);
78 i += j;
79 Bin[j++] = 1;
80 while (j < 17)
81 Bin[j++] = 0;
82
83 Accumulator = BigInteger.Remainder((Accumulator + new BigInteger(Bin)) * this.r, p);
84 }
85
86 Accumulator += this.s;
87
88 byte[] Result = Accumulator.ToByteArray();
89
90 if (Result.Length != 16)
91 Array.Resize(ref Result, 16);
92
93 return Result;
94 }
95
101 public async Task<byte[]> CalcMac(Stream Data)
102 {
103 long i = 0;
104 long c = Data.Length;
105 byte[] Bin = new byte[17];
106 int j;
107 BigInteger Accumulator = BigInteger.Zero;
108
109 while (i < c)
110 {
111 j = (int)Math.Min(c - i, 16);
112 await Data.ReadAllAsync(Bin, 0, j);
113
114 i += j;
115 Bin[j++] = 1;
116 while (j < 17)
117 Bin[j++] = 0;
118
119 Accumulator = BigInteger.Remainder((Accumulator + new BigInteger(Bin)) * this.r, p);
120 }
121
122 Accumulator += this.s;
123
124 byte[] Result = Accumulator.ToByteArray();
125
126 if (Result.Length != 16)
127 Array.Resize(ref Result, 16);
128
129 return Result;
130 }
131
132 }
133}
ChaCha20 encryptor, as defined in RFC 8439: https://tools.ietf.org/html/rfc8439
Definition: ChaCha20.cs:12
byte[] GetBytes(int NrBytes)
Gets the next number of bytes in the stream.
Definition: ChaCha20.cs:160
Poly1305 authenticator, as defined in RFC 8439: https://tools.ietf.org/html/rfc8439
Definition: Poly1305.cs:14
Poly1305(byte[] Key)
Poly1305 authenticator, as defined in RFC 8439: https://tools.ietf.org/html/rfc8439
Definition: Poly1305.cs:24
static Poly1305 FromChaCha20(byte[] Key, byte[] Nonce)
Prepares a Poly1305 instance using ChaCha20 and Counter=0 to generate the corresponding key.
Definition: Poly1305.cs:54
byte[] CalcMac(byte[] Data)
Calculates a message authentication code (MAC).
Definition: Poly1305.cs:66
async Task< byte[]> CalcMac(Stream Data)
Calculates a message authentication code (MAC).
Definition: Poly1305.cs:101