|
9 | 9 |
|
10 | 10 | namespace NetworkLibrary.Components.Crypto.Algorithms |
11 | 11 | { |
12 | | -#if NETSTANDARD2_1_OR_GREATER |
13 | | - internal class AesGcmAlgorithm : IAesAlgorithm |
14 | | - { |
15 | | - |
16 | | - public int DecryptorInputBlockSize => 16; |
17 | | - |
18 | | - public int DecryptorOutputBlockSize => 16; |
19 | | - |
20 | | - public int EncryptorInputBlockSize => 16; |
21 | | - |
22 | | - public int EncryptorOutputBlockSize => 16; |
23 | | - byte[] iv; |
24 | | - |
25 | | - private AesGcm aes; |
26 | | - private AesGcm aes2; |
27 | | - const int nonceSize = 12; |
28 | | - const int tagSize = 14; |
29 | | - public AesGcmAlgorithm(byte[] Key, byte[] IV) |
30 | | - { |
31 | | - aes = new AesGcm(Key); |
32 | | - aes2 = new AesGcm(Key); |
33 | | - iv = IV; |
34 | | - } |
35 | | - |
36 | | - public byte[] Decrypt(byte[] message) |
37 | | - { |
38 | | - return Decrypt(message, 0, message.Length); |
39 | | - } |
40 | | - |
41 | | - public byte[] Decrypt(byte[] buffer, int offset, int count) |
42 | | - { |
43 | | - var buff = BufferPool.RentBuffer(count + 256); |
44 | | - int am = DecryptInto(buffer, offset, count, buff, 0); |
45 | | - var res = ByteCopy.ToArray(buff, 0, am); |
46 | | - BufferPool.ReturnBuffer(buff); |
47 | | - return res; |
48 | | - } |
49 | | - |
50 | | - public void Dispose() |
51 | | - { |
52 | | - aes?.Dispose(); |
53 | | - aes2?.Dispose(); |
54 | | - } |
55 | | - |
56 | | - public byte[] Encrypt(byte[] message) |
57 | | - { |
58 | | - return Encrypt(message, 0, message.Length); |
59 | | - } |
60 | | - |
61 | | - public byte[] Encrypt(byte[] buffer, int offset, int count) |
62 | | - { |
63 | | - var buff = BufferPool.RentBuffer(count + 256); |
64 | | - int am = EncryptInto(buffer, offset, count, buff, 0); |
65 | | - var res = ByteCopy.ToArray(buff, 0, am); |
66 | | - BufferPool.ReturnBuffer(buff); |
67 | | - return res; |
68 | | - } |
69 | | - |
70 | | - public int GetEncriptorOutputSize(int inputSize) |
71 | | - { |
72 | | - return nonceSize + tagSize + inputSize; |
73 | | - } |
74 | | - |
75 | | - public int EncryptInto(byte[] data, int offset, int count, byte[] output, int outputOffset) |
76 | | - { |
77 | | - int cipherSize = count; |
78 | | - var non = GenerateNextNonce(out int nonceSize); |
79 | | - |
80 | | - int encryptedDataLength = nonceSize + tagSize + cipherSize; |
81 | | - Span<byte> encryptedData; |
82 | | - unsafe |
83 | | - { |
84 | | - fixed (byte* buffer = &output[outputOffset]) |
85 | | - { |
86 | | - encryptedData = new Span<byte>(buffer, encryptedDataLength); |
87 | | - } |
88 | | - } |
89 | | - Buffer.BlockCopy(non, 4, output, outputOffset, nonceSize); |
90 | | - |
91 | | - var nonce = encryptedData.Slice(0, nonceSize); |
92 | | - var tag = encryptedData.Slice(nonceSize + cipherSize, tagSize); |
93 | | - var cipherBytes = encryptedData.Slice(nonceSize, cipherSize); |
94 | | - |
95 | | - // RandomNumberGenerator.Fill(nonce); |
96 | | - |
97 | | - unsafe |
98 | | - { |
99 | | - fixed (byte* buffer = &data[offset]) |
100 | | - aes.Encrypt(non, new ReadOnlySpan<byte>(buffer, count), cipherBytes, tag); |
101 | | - } |
102 | | - |
103 | | - return encryptedDataLength; |
104 | | - } |
105 | | - public int EncryptInto(byte[] data, int offset, int count, |
106 | | - byte[] data1, int offset1, int count1, byte[] output, int outputOffset) |
107 | | - { |
108 | | - { |
109 | | - var non = GenerateNextNonce(out int nonceSize); |
110 | | - |
111 | | - int cipherSize = count + count1; |
112 | | - |
113 | | - int encryptedDataLength = tagSize + nonceSize + cipherSize; |
114 | | - Span<byte> encryptedData = new Span<byte>(output); |
115 | | - unsafe |
116 | | - { |
117 | | - fixed (byte* buffer = &output[outputOffset]) |
118 | | - { |
119 | | - encryptedData = new Span<byte>(buffer, encryptedDataLength); |
120 | | - } |
121 | | - } |
122 | | - Buffer.BlockCopy(non, 4, output, outputOffset, nonceSize); |
123 | | - |
124 | | - var nonce = encryptedData.Slice(0, nonceSize); |
125 | | - var tag = encryptedData.Slice(nonceSize + cipherSize, tagSize); |
126 | | - var cipherBytes = encryptedData.Slice(nonceSize, cipherSize); |
127 | | - |
128 | | - byte[] b = BufferPool.RentBuffer(cipherSize); |
129 | | - Buffer.BlockCopy(data, offset, b, 0, count); |
130 | | - Buffer.BlockCopy(data1, offset1, b, count, count1); |
131 | | - // Generate secure nonce |
132 | | - |
133 | | - unsafe |
134 | | - { |
135 | | - fixed (byte* buffer = &b[0]) |
136 | | - aes.Encrypt(non, new ReadOnlySpan<byte>(buffer, cipherSize), cipherBytes, tag); |
137 | | - |
138 | | - } |
139 | | - BufferPool.ReturnBuffer(b); |
140 | | - return encryptedDataLength; |
141 | | - } |
142 | | - } |
143 | | - public int DecryptInto(byte[] data, int offset, int count, byte[] output, int outputOffset) |
144 | | - { |
145 | | - Span<byte> encryptedData; |
146 | | - unsafe |
147 | | - { |
148 | | - fixed (byte* buffer = &data[offset]) |
149 | | - { |
150 | | - encryptedData = new Span<byte>(buffer, count); |
151 | | - } |
152 | | - } |
153 | | - |
154 | | - int oldOff = offset; |
155 | | - var nonce = ParseNonce(data, ref offset, ref count).AsSpan(); |
156 | | - int nonceSize = offset - oldOff; |
157 | | - |
158 | | - int cipherSize = encryptedData.Length - nonceSize - tagSize; |
159 | | - |
160 | | - // var nonce = encryptedData.Slice(0, nonceSize); |
161 | | - var tag = encryptedData.Slice(nonceSize + cipherSize, tagSize); |
162 | | - var cipherBytes = encryptedData.Slice(nonceSize, cipherSize); |
163 | | - |
164 | | - unsafe |
165 | | - { |
166 | | - fixed (byte* buffer = &output[outputOffset]) |
167 | | - { |
168 | | - aes2.Decrypt(nonce, cipherBytes, tag, new Span<byte>(buffer, cipherSize)); |
169 | | - } |
170 | | - } |
171 | | - |
172 | | - return cipherSize; |
173 | | - } |
174 | | - |
175 | | - |
176 | | - [ThreadStatic] |
177 | | - static byte[] nonce; |
178 | | - long ctr = 0; |
179 | | - |
180 | | - private byte[] GetEmptyNonceArray() |
181 | | - { |
182 | | - if (true || nonce == null) |
183 | | - { |
184 | | - nonce = new byte[12]; |
185 | | - nonce[0] = iv[0]; |
186 | | - nonce[1] = iv[1]; |
187 | | - nonce[2] = iv[2]; |
188 | | - nonce[3] = iv[3]; |
189 | | - return nonce; |
190 | | - |
191 | | - } |
192 | | - int off = 4; |
193 | | - PrimitiveEncoder.WriteFixedInt64(nonce, ref off, 0); |
194 | | - return nonce; |
195 | | - } |
196 | | - private byte[] GenerateNextNonce(out int encodedCount) |
197 | | - { |
198 | | - long n = Interlocked.Increment(ref ctr); |
199 | | - var nonce = GetEmptyNonceArray();// fill random 4 in begining and 8 0s after |
200 | | - int off = 4; |
201 | | - |
202 | | - PrimitiveEncoder.WriteInt64(nonce, ref off, n);// encode counter |
203 | | - encodedCount = off - 4; |
204 | | - return nonce; |
205 | | - } |
206 | | - private byte[] ParseNonce(byte[] source, ref int sourceOffset, ref int sourceCount) |
207 | | - { |
208 | | - var nonce = GetEmptyNonceArray(); |
209 | | - int oldOff = sourceOffset; |
210 | | - PrimitiveEncoder.ReadInt64(source, ref sourceOffset); |
211 | | - |
212 | | - int n = sourceOffset - oldOff; |
213 | | - Buffer.BlockCopy(source, oldOff, nonce, 4, n); |
214 | | - sourceCount -= n; |
215 | 12 |
|
216 | | - return nonce; |
217 | | - } |
218 | | - |
219 | | - } |
220 | | - |
221 | | -#endif |
222 | | -#if NET5_0_OR_GREATER |
| 13 | +#if NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER |
223 | 14 | internal class AesGcmAlgorithm : IAesAlgorithm |
224 | 15 | { |
225 | 16 |
|
|
0 commit comments