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
|
using Hazel.Dtls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security.Cryptography;
using System.Text;
namespace Hazel.UnitTests.Dtls
{
[TestClass]
public class AesGcmRecordProtectedTests
{
private readonly ByteSpan masterSecret;
private readonly ByteSpan serverRandom;
private readonly ByteSpan clientRandom;
private const string TestMessage = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
public AesGcmRecordProtectedTests()
{
this.masterSecret = new byte[48];
this.serverRandom = new byte[32];
this.clientRandom = new byte[32];
using (RandomNumberGenerator random = RandomNumberGenerator.Create())
{
random.GetBytes(this.masterSecret.GetUnderlyingArray());
random.GetBytes(this.serverRandom.GetUnderlyingArray());
random.GetBytes(this.clientRandom.GetUnderlyingArray());
}
}
[TestMethod]
public void ServerCanEncryptAndDecryptData()
{
using (Aes128GcmRecordProtection recordProtection = new Aes128GcmRecordProtection(this.masterSecret, this.serverRandom, this.clientRandom))
{
byte[] messageAsBytes = Encoding.UTF8.GetBytes(TestMessage);
Record record = new Record();
record.ContentType = ContentType.ApplicationData;
record.ProtocolVersion = ProtocolVersion.DTLS1_2;
record.Epoch = 1;
record.SequenceNumber = 124;
record.Length = (ushort)recordProtection.GetEncryptedSize(messageAsBytes.Length);
ByteSpan encrypted = new byte[record.Length];
recordProtection.EncryptServerPlaintext(encrypted, messageAsBytes, ref record);
ByteSpan plaintext = new byte[recordProtection.GetDecryptedSize(encrypted.Length)];
bool couldDecrypt = recordProtection.DecryptCiphertextFromServer(plaintext, encrypted, ref record);
Assert.IsTrue(couldDecrypt);
Assert.AreEqual(messageAsBytes.Length, plaintext.Length);
Assert.AreEqual(TestMessage, Encoding.UTF8.GetString(plaintext.GetUnderlyingArray(), plaintext.Offset, plaintext.Length));
}
}
[TestMethod]
public void ClientCanEncryptAndDecryptData()
{
using (Aes128GcmRecordProtection recordProtection = new Aes128GcmRecordProtection(this.masterSecret, this.serverRandom, this.clientRandom))
{
byte[] messageAsBytes = Encoding.UTF8.GetBytes(TestMessage);
Record record = new Record();
record.ContentType = ContentType.ApplicationData;
record.ProtocolVersion = ProtocolVersion.DTLS1_2;
record.Epoch = 1;
record.SequenceNumber = 124;
record.Length = (ushort)recordProtection.GetEncryptedSize(messageAsBytes.Length);
ByteSpan encrypted = new byte[record.Length];
recordProtection.EncryptClientPlaintext(encrypted, messageAsBytes, ref record);
ByteSpan plaintext = new byte[recordProtection.GetDecryptedSize(encrypted.Length)];
bool couldDecrypt = recordProtection.DecryptCiphertextFromClient(plaintext, encrypted, ref record);
Assert.IsTrue(couldDecrypt);
Assert.AreEqual(messageAsBytes.Length, plaintext.Length);
Assert.AreEqual(TestMessage, Encoding.UTF8.GetString(plaintext.GetUnderlyingArray(), plaintext.Offset, plaintext.Length));
}
}
[TestMethod]
public void ServerDecryptionFailsWhenRecordModified()
{
using (Aes128GcmRecordProtection recordProtection = new Aes128GcmRecordProtection(this.masterSecret, this.serverRandom, this.clientRandom))
{
byte[] messageAsBytes = Encoding.UTF8.GetBytes(TestMessage);
Record originalRecord = new Record();
originalRecord.ContentType = ContentType.ApplicationData;
originalRecord.ProtocolVersion = ProtocolVersion.DTLS1_2;
originalRecord.Epoch = 1;
originalRecord.SequenceNumber = 124;
originalRecord.Length = (ushort)recordProtection.GetEncryptedSize(messageAsBytes.Length);
ByteSpan encrypted = new byte[originalRecord.Length];
recordProtection.EncryptServerPlaintext(encrypted, messageAsBytes, ref originalRecord);
ByteSpan plaintext = new byte[recordProtection.GetDecryptedSize(encrypted.Length)];
Record record = originalRecord;
record.ContentType = ContentType.Handshake;
bool couldDecrypt = recordProtection.DecryptCiphertextFromServer(plaintext, encrypted, ref record);
Assert.IsFalse(couldDecrypt);
record = originalRecord;
record.Epoch++;
couldDecrypt = recordProtection.DecryptCiphertextFromServer(plaintext, encrypted, ref record);
Assert.IsFalse(couldDecrypt);
record = originalRecord;
record.SequenceNumber++;
couldDecrypt = recordProtection.DecryptCiphertextFromServer(plaintext, encrypted, ref record);
Assert.IsFalse(couldDecrypt);
}
}
[TestMethod]
public void ClientDecryptionFailsWhenRecordModified()
{
using (Aes128GcmRecordProtection recordProtection = new Aes128GcmRecordProtection(this.masterSecret, this.serverRandom, this.clientRandom))
{
byte[] messageAsBytes = Encoding.UTF8.GetBytes(TestMessage);
Record originalRecord = new Record();
originalRecord.ContentType = ContentType.ApplicationData;
originalRecord.ProtocolVersion = ProtocolVersion.DTLS1_2;
originalRecord.Epoch = 1;
originalRecord.SequenceNumber = 124;
originalRecord.Length = (ushort)recordProtection.GetEncryptedSize(messageAsBytes.Length);
ByteSpan encrypted = new byte[originalRecord.Length];
recordProtection.EncryptClientPlaintext(encrypted, messageAsBytes, ref originalRecord);
ByteSpan plaintext = new byte[recordProtection.GetDecryptedSize(encrypted.Length)];
Record record = originalRecord;
record.ContentType = ContentType.Handshake;
bool couldDecrypt = recordProtection.DecryptCiphertextFromClient(plaintext, encrypted, ref record);
Assert.IsFalse(couldDecrypt);
record = originalRecord;
record.Epoch++;
couldDecrypt = recordProtection.DecryptCiphertextFromClient(plaintext, encrypted, ref record);
Assert.IsFalse(couldDecrypt);
record = originalRecord;
record.SequenceNumber++;
couldDecrypt = recordProtection.DecryptCiphertextFromClient(plaintext, encrypted, ref record);
Assert.IsFalse(couldDecrypt);
}
}
[TestMethod]
public void ServerEncryptionCanoverlap()
{
using (Aes128GcmRecordProtection recordProtection = new Aes128GcmRecordProtection(this.masterSecret, this.serverRandom, this.clientRandom))
{
ByteSpan messageAsBytes = Encoding.UTF8.GetBytes(TestMessage);
Record record = new Record();
record.ContentType = ContentType.ApplicationData;
record.ProtocolVersion = ProtocolVersion.DTLS1_2;
record.Epoch = 1;
record.SequenceNumber = 124;
record.Length = (ushort)recordProtection.GetEncryptedSize(messageAsBytes.Length);
ByteSpan encrypted = new byte[record.Length];
messageAsBytes.CopyTo(encrypted);
recordProtection.EncryptServerPlaintext(encrypted, encrypted.Slice(0, messageAsBytes.Length), ref record);
ByteSpan plaintext = encrypted.Slice(0, recordProtection.GetDecryptedSize(record.Length));
bool couldDecrypt = recordProtection.DecryptCiphertextFromServer(plaintext, encrypted, ref record);
Assert.IsTrue(couldDecrypt);
Assert.AreEqual(messageAsBytes.Length, plaintext.Length);
Assert.AreEqual(TestMessage, Encoding.UTF8.GetString(plaintext.GetUnderlyingArray(), plaintext.Offset, plaintext.Length));
}
}
[TestMethod]
public void ClientEncryptionCanoverlap()
{
using (Aes128GcmRecordProtection recordProtection = new Aes128GcmRecordProtection(this.masterSecret, this.serverRandom, this.clientRandom))
{
ByteSpan messageAsBytes = Encoding.UTF8.GetBytes(TestMessage);
Record record = new Record();
record.ContentType = ContentType.ApplicationData;
record.ProtocolVersion = ProtocolVersion.DTLS1_2;
record.Epoch = 1;
record.SequenceNumber = 124;
record.Length = (ushort)recordProtection.GetEncryptedSize(messageAsBytes.Length);
ByteSpan encrypted = new byte[record.Length];
messageAsBytes.CopyTo(encrypted);
recordProtection.EncryptClientPlaintext(encrypted, encrypted.Slice(0, messageAsBytes.Length), ref record);
ByteSpan plaintext = encrypted.Slice(0, recordProtection.GetDecryptedSize(record.Length));
bool couldDecrypt = recordProtection.DecryptCiphertextFromClient(plaintext, encrypted, ref record);
Assert.IsTrue(couldDecrypt);
Assert.AreEqual(messageAsBytes.Length, plaintext.Length);
Assert.AreEqual(TestMessage, Encoding.UTF8.GetString(plaintext.GetUnderlyingArray(), plaintext.Offset, plaintext.Length));
}
}
}
}
|