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
148 lines (118 loc) · 5.23 KB
/
AesCipher.CtrImpl.cs
File metadata and controls
148 lines (118 loc) · 5.23 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
#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 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
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 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);
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");
}
CTRCreateCounterArray(output.AsSpan(outputOffset, blockSizedLength));
var bytesWritten = _encryptor.TransformBlock(output, outputOffset, blockSizedLength, output, outputOffset);
Debug.Assert(bytesWritten == blockSizedLength);
ArrayXOR(output, outputOffset, data, offset, length);
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;
}
}
// XOR 2 arrays using Vector<byte>
private static void ArrayXOR(byte[] buffer, int bufferOffset, byte[] data, int offset, int length)
{
var i = 0;
var oneVectorFromEnd = length - Vector<byte>.Count;
for (; i <= oneVectorFromEnd; i += Vector<byte>.Count)
{
var v = new Vector<byte>(buffer, bufferOffset + i) ^ new Vector<byte>(data, offset + i);
v.CopyTo(buffer, bufferOffset + i);
}
for (; i < length; i++)
{
buffer[bufferOffset + i] ^= data[offset + i];
}
}
public void Dispose()
{
_aes.Dispose();
_encryptor.Dispose();
}
}
}
}