-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUWebCryptography.razor
351 lines (286 loc) · 9.79 KB
/
UWebCryptography.razor
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
@using TestApp.Units
@using Glihm.JSInterop.Browser.WebCryptoAPI.Interfaces
@using Glihm.JSInterop.Browser.WebCryptoAPI.Interfaces.Subtle.SHA
@using Glihm.JSInterop.Browser.WebCryptoAPI.Interfaces.Subtle.EC
@using Glihm.JSInterop.Browser.WebCryptoAPI.Interfaces.Subtle.AES
@using Glihm.JSInterop.Browser.WebCryptoAPI.Interfaces.CryptoKeys
@using Glihm.JSInterop.Browser.WebCryptoAPI.Cryptography
@using Glihm.JSInterop.Browser.WebCryptoAPI.Cryptography.AES
@using Glihm.JSInterop.Browser.WebCryptoAPI.Cryptography.RSA
@using Glihm.JSInterop.Browser.WebCryptoAPI.Cryptography.EC
@using Glihm.JSInterop.Browser.WebCryptoAPI.Cryptography.HMAC
@using Glihm.JSInterop.Browser.WebCryptoAPI.Cryptography.Random
@inject Crypto _crypto
@inject CryptoRandom _cryptoRand
@inject AesFactory _aesFactory
@inject RsaFactory _rsaFactory
@inject EcFactory _ecFactory
@inject HmacFactory _hmacFactory
<div id="main">
<UnitTest TestName="AesGcm"
MethodToBeTested="@this.AesGcmTest" />
<UnitTest TestName="Rsa OAEP"
MethodToBeTested="@this.RsaOaepTest" />
<UnitTest TestName="Rsa SSA"
MethodToBeTested="@this.RsaSsaTest" />
<UnitTest TestName="ECDSA"
MethodToBeTested="@this.EcdsaTest" />
<UnitTest TestName="ECDH"
MethodToBeTested="@this.EcdhTest" />
<UnitTest TestName="HMAC"
MethodToBeTested="@this.HmacTest" />
</div>
@code {
/// <summary>
/// Constructor.
/// </summary>
public UWebCryptography()
{
Task.Run(async () =>
{
await Task.Delay(5000);
await this._crypto.Subtle.DumpKeys();
});
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private async Task<UnitTestResult>
AesGcmTest()
{
await using AesCbc? aesCbc = await this._aesFactory.Create<AesCbc>(extractable: true);
if (aesCbc is not null)
{
string? jwk = await aesCbc.ExportKeyJSONWebKey();
Console.WriteLine(jwk);
byte[]? buf = await aesCbc.ExportKeyRaw();
if (buf is not null)
{
Console.WriteLine(BitConverter.ToString(buf));
}
}
byte[]? aesKey = await this._aesFactory.GenerateRandomIv(32);
if (aesKey is null)
{
return UnitTestResult.Failed("Can't generate aesKey.");
}
await using AesGcm? aesGcm = await this._aesFactory.Create<AesGcm>(aesKey);
if (aesGcm is null)
{
return UnitTestResult.Failed("Can't get AesGcm created.");
}
byte[]? iv = await this._aesFactory.GenerateRandomIv(12);
if (iv is null)
{
return UnitTestResult.Failed("Can't generate iv.");
}
byte[] data = new byte[] { 1, 2, 3 };
byte[]? ciphertext = await aesGcm.Encrypt(data, iv);
if (ciphertext is null)
{
return UnitTestResult.Failed();
}
byte[]? plaintext = await aesGcm.Decrypt(ciphertext, iv);
if (plaintext is null)
{
return UnitTestResult.Failed();
}
if (plaintext.SequenceEqual(data))
{
return UnitTestResult.Passed();
}
else
{
return UnitTestResult.Failed();
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private async Task<UnitTestResult>
RsaOaepTest()
{
// Generate the keypair on the fly.
await using RsaOaep? rsaOaep = await this._rsaFactory.Create<RsaOaep>(
2048,
ShaAlgorithm.SHA512,
extractable: true);
if (rsaOaep is null)
{
return UnitTestResult.Failed("key generation failed.");
}
byte[]? publicKeyPem = await rsaOaep.ExportPublicKey(convertToPem: true);
if (publicKeyPem is not null)
{
Console.WriteLine($"**************PEM***************\n{System.Text.Encoding.ASCII.GetString(publicKeyPem)}");
}
byte[]? privateKeyPem = await rsaOaep.ExportPrivateKey(convertToPem: true);
if (privateKeyPem is not null)
{
Console.WriteLine($"**************PEM***************\n{System.Text.Encoding.ASCII.GetString(privateKeyPem)}");
}
byte[] data = { 1, 2, 3 };
byte[]? ciphertext = await rsaOaep.Encrypt(data);
if (ciphertext is null)
{
return UnitTestResult.Failed("encryption failed.");
}
byte[]? plaintext = await rsaOaep.Decrypt(ciphertext);
if (plaintext is null)
{
return UnitTestResult.Failed("decryption failed.");
}
if (!plaintext.SequenceEqual(data))
{
return UnitTestResult.Failed("decrypted plaintext is not equal to original data");
}
// Test with one public key only.
// Test with one private key only.
// Test importing both public and private key.
return UnitTestResult.Passed();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private async Task<UnitTestResult>
RsaSsaTest()
{
// Generate the keypair on the fly.
await using RsaSsa? rsa = await this._rsaFactory.Create<RsaSsa>(
4096,
ShaAlgorithm.SHA512,
extractable: true);
if (rsa is null)
{
return UnitTestResult.Failed("key generation failed.");
}
byte[] data = { 1, 2, 3 };
byte[]? signature = await rsa.Sign(data);
if (signature is null)
{
return UnitTestResult.Failed("signature failed.");
}
bool isSignatureValid = await rsa.Verify(data, signature);
if (!isSignatureValid)
{
return UnitTestResult.Failed("Signature is not valid.");
}
return UnitTestResult.Passed();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private async Task<UnitTestResult>
EcdsaTest()
{
await using Ecdsa? ecdsa = await this._ecFactory.Create<Ecdsa>(
EcNamedCurve.P_521,
extractable: true);
if (ecdsa is null)
{
return UnitTestResult.Failed("key generation failed.");
}
byte[] data = { 1, 2, 3 };
byte[]? signature = await ecdsa.Sign(data, ShaAlgorithm.SHA512);
if (signature is null)
{
return UnitTestResult.Failed("signature failed.");
}
bool isSignatureValid = await ecdsa.Verify(data, ShaAlgorithm.SHA512, signature);
if (!isSignatureValid)
{
return UnitTestResult.Failed("Signature is not valid.");
}
return UnitTestResult.Passed();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private async Task<UnitTestResult>
HmacTest()
{
byte[] data = { 1, 2, 3 };
byte[]? randomKey = await this._cryptoRand.GenerateRandomValues(128);
if (randomKey is not null)
{
await using Hmac? himport = await this._hmacFactory.Create(randomKey, ShaAlgorithm.SHA512);
if (himport is not null)
{
byte[]? s = await himport.Sign(data);
if (s is not null)
{
bool isOk = await himport.Verify(data, s);
if (!isOk)
{
return UnitTestResult.Failed("Test from imported key failed");
}
}
}
}
await using Hmac? hmac = await this._hmacFactory.Create(
ShaAlgorithm.SHA512,
extractable: true);
if (hmac is null)
{
return UnitTestResult.Failed("key generation failed.");
}
byte[]? signature = await hmac.Sign(data);
if (signature is null)
{
return UnitTestResult.Failed("signature failed.");
}
bool isSignatureValid = await hmac.Verify(data, signature);
if (!isSignatureValid)
{
return UnitTestResult.Failed("Signature is not valid.");
}
return UnitTestResult.Passed();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private async Task<UnitTestResult>
EcdhTest()
{
Ecdh? eAlice = await this._ecFactory.Create<Ecdh>(EcNamedCurve.P_256);
Ecdh? eBob = await this._ecFactory.Create<Ecdh>(EcNamedCurve.P_256);
if (eAlice is null || eBob is null)
{
return UnitTestResult.Failed("Gen failed.");
}
CryptoKeyDescriptor? secretAlice = await eAlice.DeriveAesKey(eBob.GetPublicKeyOrThrow(), AesAlgorithm.GCM);
CryptoKeyDescriptor? secretBob = await eBob.DeriveAesKey(eAlice.GetPublicKeyOrThrow(), AesAlgorithm.GCM);
if (secretAlice is null || secretBob is null)
{
return UnitTestResult.Failed("DeriveAesKey failed.");
}
AesGcm? aesAlice = this._aesFactory.Create<AesGcm>(secretAlice);
AesGcm? aesBob = this._aesFactory.Create<AesGcm>(secretBob);
if (aesAlice is null || aesBob is null)
{
return UnitTestResult.Failed("Aes instanciation failed.");
}
byte[] data = { 1, 2, 3 };
byte[]? iv = await this._cryptoRand.GenerateRandomValues(12);
if (iv is null)
{
return UnitTestResult.Failed("Can't get random IV.");
}
byte[]? cipherAlice = await aesAlice.Encrypt(data, iv);
if (cipherAlice is not null)
{
byte[]? plainBob = await aesBob.Decrypt(cipherAlice, iv);
if (plainBob is null || !plainBob.SequenceEqual(data))
{
return UnitTestResult.Failed("Alice and Bob failed.");
}
}
return UnitTestResult.Passed();
}
}