forked from moserware/AES-Illustrated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NistKnownAnswerTestFileParser.cs
201 lines (167 loc) · 6.63 KB
/
NistKnownAnswerTestFileParser.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
namespace Moserware.AesIllustrated
{
/// <summary>
/// Parses the AES NIST Known Answer Test (KAT) files.
/// </summary>
internal static class NistKnownAnswerTestFileParser
{
private static CipherMode GetCipherModeFromFileName(string path)
{
string fileMode = Path.GetFileName(path).Substring(0, 3);
try
{
CipherMode result = (CipherMode) Enum.Parse(typeof (CipherMode), fileMode);
return result;
}
catch (ArgumentException)
{
// Hacked way of doing a TryParse
return 0;
}
}
public static bool IsValidTestFileName(string path)
{
return GetCipherModeFromFileName(path) != 0;
}
public static IEnumerable<NistKnownAnswerTestVector> Parse(string path)
{
CipherMode cipherMode = GetCipherModeFromFileName(path);
using (var reader = new StreamReader(path))
{
string firstLine = reader.ReadLine();
NistKnownAnswerTestOperation operation;
switch (firstLine)
{
case "[ENCRYPT]":
operation = NistKnownAnswerTestOperation.Encrypt;
break;
case "[DECRYPT]":
operation = NistKnownAnswerTestOperation.Decrypt;
break;
default:
throw new InvalidDataException("Unknown operation");
}
string currentLine;
while ((currentLine = reader.ReadLine()) != null)
{
if (currentLine.Trim().Length == 0)
{
continue;
}
yield return ReadVector(operation, cipherMode, currentLine, reader);
}
}
}
private static NistKnownAnswerTestVector ReadVector(NistKnownAnswerTestOperation operation,
CipherMode cipherMode, string firstLine, StreamReader reader)
{
string[] countInfo = ParseNameValuePair(firstLine);
if (!"COUNT".Equals(countInfo[0], StringComparison.OrdinalIgnoreCase))
{
throw new InvalidDataException("Expected COUNT as first item");
}
int count = Int32.Parse(countInfo[1], CultureInfo.InvariantCulture);
string name;
byte[] bytes;
int bitLength;
ParseNamedBytes(reader.ReadLine(), out name, out bytes, out bitLength);
if (!"KEY".Equals(name, StringComparison.OrdinalIgnoreCase))
{
throw new InvalidDataException("Expected KEY");
}
byte[] key = bytes;
ParseNamedBytes(reader.ReadLine(), out name, out bytes, out bitLength);
byte[] iv = null;
// IV is optional since ECB doesn't need it
if ("IV".Equals(name, StringComparison.OrdinalIgnoreCase))
{
iv = bytes;
ParseNamedBytes(reader.ReadLine(), out name, out bytes, out bitLength);
}
byte[] plaintext;
byte[] ciphertext;
if (operation == NistKnownAnswerTestOperation.Encrypt)
{
if (!"PLAINTEXT".Equals(name, StringComparison.OrdinalIgnoreCase))
{
throw new InvalidDataException("Expected PLAINTEXT");
}
plaintext = bytes;
ParseNamedBytes(reader.ReadLine(), out name, out bytes, out bitLength);
if (!"CIPHERTEXT".Equals(name, StringComparison.OrdinalIgnoreCase))
{
throw new InvalidDataException("Expected CIPHERTEXT");
}
ciphertext = bytes;
}
else
{
if (!"CIPHERTEXT".Equals(name, StringComparison.OrdinalIgnoreCase))
{
throw new InvalidDataException("Expected CIPHERTEXT");
}
ciphertext = bytes;
ParseNamedBytes(reader.ReadLine(), out name, out bytes, out bitLength);
if (!"PLAINTEXT".Equals(name, StringComparison.OrdinalIgnoreCase))
{
throw new InvalidDataException("Expected PLAINTEXT");
}
plaintext = bytes;
}
return new NistKnownAnswerTestVector(operation, cipherMode, count, key, iv, bitLength, plaintext, ciphertext);
}
private static string[] ParseNameValuePair(string line)
{
return line.Split(new[] {' ', '='}, StringSplitOptions.RemoveEmptyEntries);
}
private static void ParseNamedBytes(string line, out string name, out byte[] bytes, out int bitLength)
{
string[] nameValue = ParseNameValuePair(line);
name = nameValue[0];
if (nameValue[1].Length == 1)
{
bitLength = 1;
bytes = new[] {(byte) (Int32.Parse(nameValue[1])*0x80)};
return;
}
bytes = ByteUtilities.GetBytes(nameValue[1]);
bitLength = bytes.Length*Constants.BitsPerByte;
}
}
internal enum NistKnownAnswerTestOperation
{
Encrypt,
Decrypt
}
/// <summary>
/// Represents a single NIST KAT test vector.
/// </summary>
internal class NistKnownAnswerTestVector
{
public NistKnownAnswerTestVector(NistKnownAnswerTestOperation operation, CipherMode cipherMode, int count,
byte[] key, byte[] iv, int bitLength, byte[] plaintext, byte[] ciphertext)
{
Operation = operation;
CipherMode = cipherMode;
Count = count;
Key = key;
IV = iv;
BitLength = bitLength;
Plaintext = plaintext;
Ciphertext = ciphertext;
}
public NistKnownAnswerTestOperation Operation { get; private set; }
public CipherMode CipherMode { get; private set; }
public int Count { get; private set; }
public byte[] Key { get; private set; }
public byte[] IV { get; private set; }
public byte[] Plaintext { get; private set; }
public byte[] Ciphertext { get; private set; }
public int BitLength { get; private set; }
}
}