forked from sdrapkin/SecurityDriven.Inferno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EtM_Transforms.cs
207 lines (177 loc) · 7.51 KB
/
EtM_Transforms.cs
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
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
namespace SecurityDriven.Inferno
{
using Cipher;
public static class EtM_Transform_Constants
{
const int LOH_THRESHOLD = 85000 - 1;
public const int ETM_CTR_OVERHEAD = EtM_CTR.CONTEXT_TWEAK_LENGTH /* key_tweak */ + EtM_CTR.NONCE_LENGTH /* nonce_tweak */ + EtM_CTR.MAC_LENGTH /* mac */;
public const int INPUT_BLOCK_SIZE = (LOH_THRESHOLD - ETM_CTR_OVERHEAD) / AesConstants.AES_BLOCK_SIZE * AesConstants.AES_BLOCK_SIZE; // largest pre-overhead size below LOH divisible by blocksize (to avoid wasting keystream)
public const int OUTPUT_BLOCK_SIZE = INPUT_BLOCK_SIZE + ETM_CTR_OVERHEAD;
}// class EtM_Transform_Constants
public class EtM_EncryptTransform : ICryptoTransform
{
public bool CanReuseTransform { get { return false; } }
public bool CanTransformMultipleBlocks { get { return true; } }
public int InputBlockSize { get { return EtM_Transform_Constants.INPUT_BLOCK_SIZE; } }
public int OutputBlockSize { get { return EtM_Transform_Constants.OUTPUT_BLOCK_SIZE; } }
byte[] key;
uint currentChunkNumber;
ArraySegment<byte>? salt;
public uint CurrentChunkNumber { get { return this.currentChunkNumber; } }
/// <summary>ctor</summary>
public EtM_EncryptTransform(byte[] key, ArraySegment<byte>? salt = null, uint chunkNumber = 1)
{
if (key == null) throw new ArgumentNullException(nameof(key), nameof(key) + " cannot be null.");
this.key = key;
this.salt = salt;
this.currentChunkNumber = chunkNumber;
}
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
int partialBlockSize = inputCount % EtM_Transform_Constants.INPUT_BLOCK_SIZE;
int fullBlockSize = inputCount - partialBlockSize;
if (partialBlockSize != 0)
throw new Exception("inputCount must be a multiple of input block size (" + EtM_Transform_Constants.INPUT_BLOCK_SIZE.ToString() + ").");
int i = 0, j = 0;
if (fullBlockSize > 0)
{
for (; i < fullBlockSize; i += EtM_Transform_Constants.INPUT_BLOCK_SIZE, j += EtM_Transform_Constants.OUTPUT_BLOCK_SIZE)
{
EtM_CTR.Encrypt(
masterKey: this.key,
plaintext: new ArraySegment<byte>(inputBuffer, inputOffset + i, EtM_Transform_Constants.INPUT_BLOCK_SIZE),
output: outputBuffer,
outputOffset: outputOffset + j,
salt: this.salt,
counter: checked(this.currentChunkNumber++));
}
}
return j;
}// TransformBlock()
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
if (this.key == null) return null; // key would be null if this instance has already been disposed
if (inputCount >= EtM_Transform_Constants.INPUT_BLOCK_SIZE)
throw new Exception("Final input block size must be smaller than " + EtM_Transform_Constants.INPUT_BLOCK_SIZE.ToString() + ".");
byte[] outputBuffer = new byte[EtM_Transform_Constants.ETM_CTR_OVERHEAD + inputCount];
EtM_CTR.Encrypt(
masterKey: this.key,
plaintext: new ArraySegment<byte>(inputBuffer, inputOffset, inputCount),
output: outputBuffer,
outputOffset: 0,
salt: this.salt,
counter: this.currentChunkNumber);
this.Dispose();
return outputBuffer;
}// TransformFinalBlock()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
this.key = null;
}// Dispose()
}// class EtM_EncryptTransform
public class EtM_DecryptTransform : ICryptoTransform
{
public bool CanReuseTransform { get { return false; } }
public bool CanTransformMultipleBlocks { get { return true; } }
public int InputBlockSize { get { return EtM_Transform_Constants.OUTPUT_BLOCK_SIZE; } }
public int OutputBlockSize { get { return EtM_Transform_Constants.INPUT_BLOCK_SIZE; } }
public bool IsComplete { get; private set; }
public bool IsAuthenticateOnly { get; private set; }
byte[] key;
uint currentChunkNumber;
ArraySegment<byte>? salt;
public uint CurrentChunkNumber { get { return this.currentChunkNumber; } }
/// <summary>ctor</summary>
public EtM_DecryptTransform(byte[] key, ArraySegment<byte>? salt = null, uint chunkNumber = 1, bool authenticateOnly = false)
{
if (key == null) throw new ArgumentNullException(nameof(key), nameof(key) + " cannot be null.");
this.key = key;
this.salt = salt;
this.currentChunkNumber = chunkNumber;
this.IsAuthenticateOnly = authenticateOnly;
}
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
int partialBlockSize = inputCount % EtM_Transform_Constants.OUTPUT_BLOCK_SIZE;
int fullBlockSize = inputCount - partialBlockSize;
if (partialBlockSize != 0)
throw new Exception("inputCount must be a multiple of output block size (" + EtM_Transform_Constants.OUTPUT_BLOCK_SIZE.ToString() + ").");
int i = 0, j = 0;
if (fullBlockSize > 0)
{
var authenticateonly = this.IsAuthenticateOnly;
for (; i < fullBlockSize; i += EtM_Transform_Constants.OUTPUT_BLOCK_SIZE, j += EtM_Transform_Constants.INPUT_BLOCK_SIZE)
{
var outputSegment = new ArraySegment<byte>?(new ArraySegment<byte>(outputBuffer, outputOffset + j, EtM_Transform_Constants.INPUT_BLOCK_SIZE));
var cipherText = new ArraySegment<byte>(inputBuffer, inputOffset + i, EtM_Transform_Constants.OUTPUT_BLOCK_SIZE);
if (authenticateonly)
{
if (!EtM_CTR.Authenticate(
masterKey: this.key,
ciphertext: cipherText,
salt: this.salt,
counter: this.currentChunkNumber))
outputSegment = null;
}
else
{
EtM_CTR.Decrypt(
masterKey: this.key,
ciphertext: cipherText,
outputSegment: ref outputSegment,
salt: this.salt,
counter: this.currentChunkNumber);
}
if (outputSegment == null)
{
this.key = null;
throw new CryptographicException("Decryption failed for block " + this.currentChunkNumber.ToString() + ".");
}
this.currentChunkNumber = checked(this.currentChunkNumber + 1);
}
}
return j;
}// TransformBlock()
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
if (this.key == null) return null; // key would be null if this instance has already been disposed, or previously-called TransformBlock() failed
if (inputCount >= EtM_Transform_Constants.OUTPUT_BLOCK_SIZE)
throw new Exception("Final output block size must be smaller than " + EtM_Transform_Constants.OUTPUT_BLOCK_SIZE.ToString() + ".");
if (inputCount < EtM_Transform_Constants.ETM_CTR_OVERHEAD)
throw new Exception("Final output block size must must be at least " + EtM_Transform_Constants.ETM_CTR_OVERHEAD.ToString() + ".");
byte[] outputBuffer = null;
var cipherText = new ArraySegment<byte>(inputBuffer, inputOffset, inputCount);
if (this.IsAuthenticateOnly)
{
if (EtM_CTR.Authenticate(
masterKey: this.key,
ciphertext: cipherText,
salt: this.salt,
counter: this.currentChunkNumber))
outputBuffer = Array.Empty<byte>();
}
else
{
outputBuffer = EtM_CTR.Decrypt(
masterKey: this.key,
ciphertext: cipherText,
salt: this.salt,
counter: this.currentChunkNumber);
}
this.Dispose();
if (outputBuffer == null)
throw new CryptographicException("Decryption failed for block " + this.currentChunkNumber.ToString() + ".");
this.IsComplete = true;
return outputBuffer;
}// TransformFinalBlock()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
this.key = null;
}// Dispose()
}// class EtM_DecryptTransform
}//ns