-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathAesCcmTests.cs
More file actions
214 lines (180 loc) · 12.3 KB
/
AesCcmTests.cs
File metadata and controls
214 lines (180 loc) · 12.3 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Waher.Security.DTLS.Ciphers;
namespace Waher.Security.DTLS.Test
{
/// <summary>
/// Test cases defined in:
/// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38c.pdf
/// </summary>
[TestClass]
public class AesCcmTests
{
[TestMethod]
public void Test_01_Encrypt_Example1_C1()
{
// §C.1
using AesCcm AesCcm = new AesCcm(128, 32 / 8, 56 / 8);
byte[] Ciphertext = AesCcm.Encrypt(
new byte[] { 0x20, 0x21, 0x22, 0x23 }, // P
new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
AssertEqual(new byte[] { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, Ciphertext);
}
public static void AssertEqual(byte[] Expected, byte[] A)
{
if (A is null)
throw new ArgumentException("Array is null.", nameof(A));
int i, c = Expected.Length;
if (c != A.Length)
throw new Exception("Arrays of different length");
for (i = 0; i < c; i++)
Assert.AreEqual(Expected[i], A[i]);
}
[TestMethod]
public void Test_02_Encrypt_Example2_C2()
{
// §C.2
using AesCcm AesCcm = new AesCcm(128, 48 / 8, 64 / 8);
byte[] Ciphertext = AesCcm.Encrypt(
new byte[] { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f }, // P
new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
AssertEqual(new byte[] { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, Ciphertext);
}
[TestMethod]
public void Test_03_Encrypt_Example3_C3()
{
// §C.3
using AesCcm AesCcm = new AesCcm(128, 64 / 8, 96 / 8);
byte[] Ciphertext = AesCcm.Encrypt(
new byte[] { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 }, // P
new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 }, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
AssertEqual(new byte[] { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }, Ciphertext);
}
[TestMethod]
public void Test_04_Encrypt_Example4_C4()
{
// §C.4
using AesCcm AesCcm = new AesCcm(128, 112 / 8, 104 / 8);
byte[] AD = new byte[65536];
int i;
for (i = 0; i < 65536; i++)
AD[i] = (byte)i;
byte[] Ciphertext = AesCcm.Encrypt(
new byte[] { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f }, // P
AD, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
AssertEqual(new byte[] { 0x69, 0x91, 0x5d, 0xad, 0x1e, 0x84, 0xc6, 0x37, 0x6a, 0x68, 0xc2, 0x96, 0x7e, 0x4d, 0xab, 0x61, 0x5a, 0xe0, 0xfd, 0x1f, 0xae, 0xc4, 0x4c, 0xc4, 0x84, 0x82, 0x85, 0x29, 0x46, 0x3c, 0xcf, 0x72, 0xb4, 0xac, 0x6b, 0xec, 0x93, 0xe8, 0x59, 0x8e, 0x7f, 0x0d, 0xad, 0xbc, 0xea, 0x5b }, Ciphertext);
}
[TestMethod]
public void Test_05_Decrypt_Example1_C1()
{
// §C.1
using AesCcm AesCcm = new AesCcm(128, 32 / 8, 56 / 8);
byte[] Plainttext = AesCcm.AuthenticateAndDecrypt(
new byte[] { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, // C
new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
AssertEqual(new byte[] { 0x20, 0x21, 0x22, 0x23 }, Plainttext);
}
[TestMethod]
public void Test_06_Decrypt_Example2_C2()
{
// §C.2
using AesCcm AesCcm = new AesCcm(128, 48 / 8, 64 / 8);
byte[] Plainttext = AesCcm.AuthenticateAndDecrypt(
new byte[] { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, // C
new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
AssertEqual(new byte[] { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f }, Plainttext);
}
[TestMethod]
public void Test_07_Decrypt_Example3_C3()
{
// §C.3
using AesCcm AesCcm = new AesCcm(128, 64 / 8, 96 / 8);
byte[] Plainttext = AesCcm.AuthenticateAndDecrypt(
new byte[] { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }, // C
new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 }, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
AssertEqual(new byte[] { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 }, Plainttext);
}
[TestMethod]
public void Test_08_Decrypt_Example4_C4()
{
// §C.4
using AesCcm AesCcm = new AesCcm(128, 112 / 8, 104 / 8);
byte[] AD = new byte[65536];
int i;
for (i = 0; i < 65536; i++)
AD[i] = (byte)i;
byte[] Plainttext = AesCcm.AuthenticateAndDecrypt(
new byte[] { 0x69, 0x91, 0x5d, 0xad, 0x1e, 0x84, 0xc6, 0x37, 0x6a, 0x68, 0xc2, 0x96, 0x7e, 0x4d, 0xab, 0x61, 0x5a, 0xe0, 0xfd, 0x1f, 0xae, 0xc4, 0x4c, 0xc4, 0x84, 0x82, 0x85, 0x29, 0x46, 0x3c, 0xcf, 0x72, 0xb4, 0xac, 0x6b, 0xec, 0x93, 0xe8, 0x59, 0x8e, 0x7f, 0x0d, 0xad, 0xbc, 0xea, 0x5b }, // C
AD, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
AssertEqual(new byte[] { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f }, Plainttext);
}
[TestMethod]
public void Test_09_Invalid_Example1_C1()
{
// §C.1
using AesCcm AesCcm = new AesCcm(128, 32 / 8, 56 / 8);
byte[] Plainttext = AesCcm.AuthenticateAndDecrypt(
new byte[] { 0x72, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, // C
new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
Assert.IsNull(Plainttext);
}
[TestMethod]
public void Test_10_Invalid_Example2_C2()
{
// §C.2
using AesCcm AesCcm = new AesCcm(128, 48 / 8, 64 / 8);
byte[] Plainttext = AesCcm.AuthenticateAndDecrypt(
new byte[] { 0xd3, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, // C
new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
Assert.IsNull(Plainttext);
}
[TestMethod]
public void Test_11_Invalid_Example3_C3()
{
// §C.3
using AesCcm AesCcm = new AesCcm(128, 64 / 8, 96 / 8);
byte[] Plainttext = AesCcm.AuthenticateAndDecrypt(
new byte[] { 0xe4, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }, // C
new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 }, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
Assert.IsNull(Plainttext);
}
[TestMethod]
public void Test_12_Invalid_Example4_C4()
{
// §C.4
using AesCcm AesCcm = new AesCcm(128, 112 / 8, 104 / 8);
byte[] AD = new byte[65536];
int i;
for (i = 0; i < 65536; i++)
AD[i] = (byte)i;
byte[] Plainttext = AesCcm.AuthenticateAndDecrypt(
new byte[] { 0x6a, 0x91, 0x5d, 0xad, 0x1e, 0x84, 0xc6, 0x37, 0x6a, 0x68, 0xc2, 0x96, 0x7e, 0x4d, 0xab, 0x61, 0x5a, 0xe0, 0xfd, 0x1f, 0xae, 0xc4, 0x4c, 0xc4, 0x84, 0x82, 0x85, 0x29, 0x46, 0x3c, 0xcf, 0x72, 0xb4, 0xac, 0x6b, 0xec, 0x93, 0xe8, 0x59, 0x8e, 0x7f, 0x0d, 0xad, 0xbc, 0xea, 0x5b }, // C
AD, // A
new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c }, // N
new byte[] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }); // K
Assert.IsNull(Plainttext);
}
}
}