forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAesCipher.CtrImpl.cs
More file actions
195 lines (158 loc) · 7.02 KB
/
Copy pathAesCipher.CtrImpl.cs
File metadata and controls
195 lines (158 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#nullable enable
using System;
using System.Buffers.Binary;
using System.Diagnostics;
using System.Numerics;
using System.Security.Cryptography;
namespace Renci.SshNet.Security.Cryptography.Ciphers
{
public partial class AesCipher
{
private sealed class CtrImpl : BlockCipher, IDisposable
{
private const int KeystreamBufferLength = 4096;
private readonly Aes _aes;
private readonly ICryptoTransform _encryptor;
private ulong _ivUpper; // The upper 64 bits of the IV
private ulong _ivLower; // The lower 64 bits of the IV
private byte[]? _keystreamBuffer;
public CtrImpl(
byte[] key,
byte[] iv)
: base(key, 16, mode: null, padding: null)
{
var aes = Aes.Create();
aes.Key = key;
aes.Mode = System.Security.Cryptography.CipherMode.ECB;
aes.Padding = PaddingMode.None;
_aes = aes;
_encryptor = aes.CreateEncryptor();
_ivLower = BinaryPrimitives.ReadUInt64BigEndian(iv.AsSpan(8));
_ivUpper = BinaryPrimitives.ReadUInt64BigEndian(iv);
}
public override byte[] Encrypt(byte[] input, int offset, int length)
{
return Decrypt(input, offset, length);
}
public override int Encrypt(byte[] input, int offset, int length, byte[] output, int outputOffset)
{
return Decrypt(input, offset, length, output, outputOffset);
}
public override byte[] Decrypt(byte[] input, int offset, int length)
{
ArgumentNullException.ThrowIfNull(input);
var buffer = CTREncryptDecrypt(input, offset, length, output: null, 0);
// adjust output for non-blocksized lengths
if (buffer.Length > length)
{
Array.Resize(ref buffer, length);
}
return buffer;
}
public override int Decrypt(byte[] input, int offset, int length, byte[] output, int outputOffset)
{
ArgumentNullException.ThrowIfNull(input);
ArgumentNullException.ThrowIfNull(output);
_ = CTREncryptDecrypt(input, offset, length, output, outputOffset);
return length;
}
public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
throw new NotImplementedException($"Invalid usage of {nameof(DecryptBlock)}.");
}
public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
throw new NotImplementedException($"Invalid usage of {nameof(EncryptBlock)}.");
}
private byte[] CTREncryptDecrypt(byte[] data, int offset, int length, byte[]? output, int outputOffset)
{
var blockSizedLength = length;
if (blockSizedLength % BlockSize != 0)
{
blockSizedLength += BlockSize - (blockSizedLength % BlockSize);
}
Debug.Assert(blockSizedLength % BlockSize == 0);
byte[] keystream;
int keystreamOffset;
int chunkSize;
if (data == output && offset == outputOffset)
{
keystream = _keystreamBuffer ??= new byte[KeystreamBufferLength];
keystreamOffset = 0;
chunkSize = KeystreamBufferLength;
}
else
{
if (output is null)
{
output = new byte[blockSizedLength];
outputOffset = 0;
}
else if (data.AsSpan(offset, length).Overlaps(output.AsSpan(outputOffset, blockSizedLength)))
{
throw new ArgumentException("Input and output buffers must not overlap (except when identical).");
}
keystream = output;
keystreamOffset = outputOffset;
chunkSize = length;
}
var bytesProcessed = 0;
while (bytesProcessed < length)
{
var bytesThisChunk = Math.Min(chunkSize, length - bytesProcessed);
var blockSizedChunk = (bytesThisChunk + BlockSize - 1) & ~(BlockSize - 1);
CTRCreateCounterArray(keystream.AsSpan(keystreamOffset, blockSizedChunk));
var bytesWritten = _encryptor.TransformBlock(
inputBuffer: keystream,
inputOffset: keystreamOffset,
inputCount: blockSizedChunk,
outputBuffer: keystream,
outputOffset: keystreamOffset);
Debug.Assert(bytesWritten == blockSizedChunk);
ArrayXOR(
dst: output,
dstOffset: outputOffset + bytesProcessed,
a: data,
aOffset: offset + bytesProcessed,
b: keystream,
bOffset: keystreamOffset,
length: bytesThisChunk);
bytesProcessed += bytesThisChunk;
}
return output;
}
// creates the Counter array filled with incrementing copies of IV
private void CTRCreateCounterArray(Span<byte> buffer)
{
Debug.Assert(buffer.Length % 16 == 0);
for (var i = 0; i < buffer.Length; i += 16)
{
BinaryPrimitives.WriteUInt64BigEndian(buffer.Slice(i + 8), _ivLower);
BinaryPrimitives.WriteUInt64BigEndian(buffer.Slice(i), _ivUpper);
_ivLower += 1;
_ivUpper += (_ivLower == 0) ? 1UL : 0UL;
}
}
// dst[i] = a[i] ^ b[i]
private static void ArrayXOR(byte[] dst, int dstOffset, byte[] a, int aOffset, byte[] b, int bOffset, int length)
{
var i = 0;
var oneVectorFromEnd = length - Vector<byte>.Count;
for (; i <= oneVectorFromEnd; i += Vector<byte>.Count)
{
var v = new Vector<byte>(a, aOffset + i) ^ new Vector<byte>(b, bOffset + i);
v.CopyTo(dst, dstOffset + i);
}
for (; i < length; i++)
{
dst[dstOffset + i] = (byte)(a[aOffset + i] ^ b[bOffset + i]);
}
}
public void Dispose()
{
_aes.Dispose();
_encryptor.Dispose();
}
}
}
}