native.js
21.1 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
// Copyright 2012 The Obvious Corporation.
/*
* Tests of the underlying RsaWrap class.
*/
/*
* Modules used
*/
"use strict";
var assert = require("assert");
var fixture = require("./fixture");
var RsaWrap = fixture.RsaWrap;
var ursaNative = fixture.ursaNative;
var textToNid = ursaNative.textToNid;
var nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
/**
* Asserts that two strings are equal, ignoring Windows newline differences
*/
function assertStringEqual(actual, expected, message) {
assert.equal(actual.replace(/\r\n/g, '\n'), expected.replace(/\r\n/g, '\n'), message);
}
describe('native', function() {
it('new', function() {
new RsaWrap();
});
it('setPrivateKeyPem', function() {
var rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PASS_PRIVATE_KEY, fixture.PASSWORD);
});
it('fail_setPrivateKeyPem', function() {
var rsa = new RsaWrap();
function f1() {
rsa.setPrivateKeyPem();
}
assert.throws(f1, /Missing args\[0]\./);
function f2() {
rsa.setPrivateKeyPem("x");
}
assert.throws(f2, /Expected a Buffer in args\[0]\./);
function f3() {
rsa.setPrivateKeyPem(new Buffer("x"));
}
assert.throws(f3, /no start line/);
function f4() {
rsa.setPrivateKeyPem(fixture.PASS_PRIVATE_KEY, undefined);
}
assert.throws(f4, /Expected a Buffer in args\[1]\./);
function f5() {
rsa.setPrivateKeyPem(fixture.PASS_PRIVATE_KEY, "x");
}
assert.throws(f5, /Expected a Buffer in args\[1]\./);
function f6() {
rsa.setPrivateKeyPem(fixture.PASS_PRIVATE_KEY,
new Buffer("INCORRECT PASS"));
}
assert.throws(f6, /bad decrypt/);
// Check for "set once."
function f7() {
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
}
f7();
assert.throws(f7, /Key already set\./);
});
it('setPublicKeyPem', function() {
var rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
});
it('fail_setPublicKeyPem', function() {
var rsa = new RsaWrap();
function f1() {
rsa.setPublicKeyPem();
}
assert.throws(f1, /Missing args\[0]\./);
function f2() {
rsa.setPublicKeyPem("x");
}
assert.throws(f2, /Expected a Buffer in args\[0]\./);
function f3() {
rsa.setPublicKeyPem(new Buffer("x"));
}
assert.throws(f3, /no start line/);
// Check for "set once."
function f4() {
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
}
f4();
assert.throws(f4, /Key already set\./);
});
it('getExponent', function() {
var rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
var value = rsa.getExponent().toString(fixture.HEX);
assert.equal(value, fixture.EXPONENT_HEX);
rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
value = rsa.getExponent().toString(fixture.HEX);
assert.equal(value, fixture.EXPONENT_HEX);
});
it('fail_getExponent', function() {
var rsa = new RsaWrap();
function f1() {
rsa.getExponent();
}
assert.throws(f1, /Key not yet set\./);
});
it('getModulus', function() {
var rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
var value = rsa.getModulus().toString(fixture.HEX);
assert.equal(value, fixture.MODULUS_HEX);
rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
value = rsa.getModulus().toString(fixture.HEX);
assert.equal(value, fixture.MODULUS_HEX);
});
it('fail_getModulus', function() {
var rsa = new RsaWrap();
function f1() {
rsa.getModulus();
}
assert.throws(f1, /Key not yet set\./);
});
it('getPrivateExponent', function() {
var rsa = new RsaWrap();
rsa.createPrivateKeyFromComponents(
fixture.PRIVATE_KEY_COMPONENTS.modulus,
fixture.PRIVATE_KEY_COMPONENTS.exponent,
fixture.PRIVATE_KEY_COMPONENTS.p,
fixture.PRIVATE_KEY_COMPONENTS.q,
fixture.PRIVATE_KEY_COMPONENTS.dp,
fixture.PRIVATE_KEY_COMPONENTS.dq,
fixture.PRIVATE_KEY_COMPONENTS.inverseQ,
fixture.PRIVATE_KEY_COMPONENTS.d);
var value = rsa.getPrivateExponent();
assert.equal(value.toString(fixture.HEX), fixture.PRIVATE_KEY_COMPONENTS.d.toString(fixture.HEX));
});
it('getPrivateKeyPem', function() {
var keyStr = fixture.PRIVATE_KEY.toString(fixture.UTF8);
var rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
var pem = rsa.getPrivateKeyPem().toString(fixture.UTF8);
assertStringEqual(pem, keyStr);
});
it.skip('getPrivateKeyPemWithPassPhrase', function() {
var keyStr = fixture.PASS_PRIVATE_KEY.toString(fixture.UTF8);
var rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PASS_PRIVATE_KEY, fixture.PASSWORD);
var pem = rsa.getPrivateKeyPem(fixture.PASSWORD, fixture.DES_EDE3_CBC).toString(fixture.UTF8);
assertStringEqual(pem, keyStr);
})
it('fail_getPrivateKeyPem', function() {
var rsa = new RsaWrap();
function f1() {
rsa.getPrivateKeyPem();
}
assert.throws(f1, /Key not yet set\./);
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
assert.throws(f1, /Expected a private key\./);
});
it('getPublicKeyPem', function() {
var keyStr = fixture.PUBLIC_KEY.toString(fixture.UTF8);
var rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
var pem = rsa.getPublicKeyPem().toString(fixture.UTF8);
assertStringEqual(pem, keyStr);
rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
pem = rsa.getPublicKeyPem().toString(fixture.UTF8);
assertStringEqual(pem, keyStr);
});
it('fail_getPublicKeyPem', function() {
var rsa = new RsaWrap();
function f1() {
rsa.getPublicKeyPem();
}
assert.throws(f1, /Key not yet set\./);
});
it('privateDecrypt', function() {
var rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
var encoded = new Buffer(fixture.PRIVATE_CIPHERTEXT_HEX, fixture.HEX);
var decoded = rsa.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
encoded = new Buffer(fixture.PRIVATE_OLD_PAD_CIPHER_HEX, fixture.HEX);
decoded = rsa.privateDecrypt(encoded, ursaNative.RSA_PKCS1_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
});
it('fail_privateDecrypt', function() {
var rsa = new RsaWrap();
function f1() {
rsa.privateDecrypt();
}
assert.throws(f1, /Key not yet set\./);
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
assert.throws(f1, /Expected a private key\./);
rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
function f2() {
rsa.privateDecrypt("x", ursaNative.RSA_PKCS1_OAEP_PADDING);
}
assert.throws(f2, /Expected a Buffer in args\[0]\./);
function f3() {
rsa.privateDecrypt(new Buffer("x"), ursaNative.RSA_PKCS1_OAEP_PADDING);
}
assert.throws(f3, /decoding error/);
function f4() {
rsa.privateDecrypt(new Buffer("x"), "str");
}
assert.throws(f4, /Expected a 32-bit integer/);
});
it('publicEncrypt', function() {
// No other reasonable way to test this than to do a round trip.
var plainBuf = new Buffer(fixture.PLAINTEXT, fixture.UTF8);
var priv = new RsaWrap();
priv.setPrivateKeyPem(fixture.PRIVATE_KEY);
var rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
var encoded = rsa.publicEncrypt(plainBuf, ursaNative.RSA_PKCS1_OAEP_PADDING);
var decoded = priv.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
encoded = priv.publicEncrypt(plainBuf, ursaNative.RSA_PKCS1_OAEP_PADDING);
decoded = priv.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
// Test with old-style padding.
encoded = rsa.publicEncrypt(plainBuf, ursaNative.RSA_PKCS1_PADDING);
decoded = priv.privateDecrypt(encoded, ursaNative.RSA_PKCS1_PADDING);
decoded = decoded.toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
});
it('fail_publicEncrypt', function() {
var rsa = new RsaWrap();
function f1() {
rsa.publicEncrypt();
}
assert.throws(f1, /Key not yet set\./);
rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
function f2() {
rsa.publicEncrypt("x", ursaNative.RSA_PKCS1_OAEP_PADDING);
}
assert.throws(f2, /Expected a Buffer in args\[0]\./);
function f3() {
rsa.publicEncrypt(new Buffer(2048), ursaNative.RSA_PKCS1_OAEP_PADDING);
}
assert.throws(f3, /too large/);
function f4() {
rsa.publicEncrypt(new Buffer("x"), "str");
}
assert.throws(f4, /Expected a 32-bit integer/);
});
it('privateEncrypt', function() {
var rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
var plainBuf = new Buffer(fixture.PLAINTEXT, fixture.UTF8);
var encoded = rsa.privateEncrypt(plainBuf, ursaNative.RSA_PKCS1_PADDING).toString(fixture.HEX);
assert.equal(encoded, fixture.PUBLIC_CIPHERTEXT_HEX);
});
it('fail_privateEncrypt', function() {
var rsa = new RsaWrap();
function f1() {
rsa.privateEncrypt();
}
assert.throws(f1, /Key not yet set\./);
rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
assert.throws(f1, /Expected a private key\./);
rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
function f2() {
rsa.privateEncrypt("x", ursaNative.RSA_PKCS1_PADDING);
}
assert.throws(f2, /Expected a Buffer in args\[0]\./);
function f3() {
rsa.privateEncrypt(new Buffer(2048), ursaNative.RSA_PKCS1_PADDING);
}
assert.throws(f3, /too large/);
});
it('publicDecrypt', function() {
var rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
var encoded = new Buffer(fixture.PUBLIC_CIPHERTEXT_HEX, fixture.HEX);
var decoded = rsa.publicDecrypt(encoded, ursaNative.RSA_PKCS1_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
encoded = new Buffer(fixture.PUBLIC_CIPHERTEXT_HEX, fixture.HEX);
decoded = rsa.publicDecrypt(encoded, ursaNative.RSA_PKCS1_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
});
it('fail_publicDecrypt', function() {
var rsa = new RsaWrap();
function f1() {
rsa.publicDecrypt();
}
assert.throws(f1, /Key not yet set\./);
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
function f2() {
rsa.publicDecrypt("x", ursaNative.RSA_PKCS1_PADDING);
}
assert.throws(f2, /Expected a Buffer in args\[0]\./);
function f3() {
rsa.publicDecrypt(new Buffer("x"), ursaNative.RSA_PKCS1_PADDING);
}
assert.throws(f3, /padding_check/);
});
it('generatePrivateKey', function() {
var rsa = new RsaWrap();
rsa.generatePrivateKey(512, 65537);
// Do a round trip check.
var plainBuf = new Buffer(fixture.PLAINTEXT, fixture.UTF8);
var encoded = rsa.publicEncrypt(plainBuf, ursaNative.RSA_PKCS1_OAEP_PADDING);
var decoded = rsa.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
// Extract the public key, and try using it for a round trip.
var pubKey = new RsaWrap();
pubKey.setPublicKeyPem(rsa.getPublicKeyPem());
encoded = pubKey.publicEncrypt(plainBuf, ursaNative.RSA_PKCS1_OAEP_PADDING);
decoded = rsa.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
// Similarly, try decoding with an extracted private key.
var privKey = new RsaWrap();
privKey.setPrivateKeyPem(rsa.getPrivateKeyPem());
decoded = privKey.privateDecrypt(encoded, ursaNative.RSA_PKCS1_OAEP_PADDING).toString(fixture.UTF8);
assert.equal(decoded, fixture.PLAINTEXT);
});
it('fail_generatePrivateKey', function() {
var rsa = new RsaWrap();
function f1() {
rsa.generatePrivateKey();
}
assert.throws(f1, /Missing args\[0]\./);
function f2() {
rsa.generatePrivateKey("x");
}
assert.throws(f2, /Expected a 32-bit integer in args\[0]\./);
function f3() {
rsa.generatePrivateKey(10);
}
assert.throws(f3, /Missing args\[1]\./);
function f4() {
rsa.generatePrivateKey(20, "x");
}
assert.throws(f4, /Expected a 32-bit integer in args\[1]\./);
function f5() {
rsa.generatePrivateKey(512, 2);
}
assert.throws(f5, /Expected odd exponent\./);
function f6() {
rsa.generatePrivateKey(512, 0);
}
assert.throws(f6, /Expected positive exponent\./);
function f7() {
rsa.generatePrivateKey(511, 1);
}
assert.throws(f7, /Expected modulus bit count >= 512\./);
// Use the original f1(), above, for this test.
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
assert.throws(f1, /Key already set\./);
});
it('sign', function() {
var rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
var buf = new Buffer(fixture.FAKE_SHA256_TO_SIGN, fixture.HEX);
var sig = rsa.sign(textToNid(fixture.SHA256), buf);
assert.equal(sig.toString(fixture.HEX), fixture.FAKE_SHA256_SIGNATURE);
buf = new Buffer(fixture.PLAINTEXT_SHA256, fixture.HEX);
sig = rsa.sign(textToNid(fixture.SHA256), buf);
assert.equal(sig.toString(fixture.HEX), fixture.PLAINTEXT_SHA256_SIGNATURE);
});
it('fail_sign', function() {
var rsa = new RsaWrap();
function f1() {
rsa.sign();
}
assert.throws(f1, /Key not yet set\./);
rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
assert.throws(f1, /Expected a private key\./);
rsa = new RsaWrap();
rsa.setPrivateKeyPem(fixture.PRIVATE_KEY);
function f2() {
rsa.sign("x", "x");
}
assert.throws(f2, /Expected a 32-bit integer in args\[0]\./);
function f3() {
rsa.sign(1, "x");
}
assert.throws(f3, /Expected a Buffer in args\[1]\./);
function f4() {
rsa.sign(1, new Buffer(2048));
}
assert.throws(f4, /too big/);
function f5() {
rsa.sign(99999, new Buffer(16));
}
assert.throws(f5, /unknown algorithm/);
});
it('verify', function() {
var rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
var hash = new Buffer(fixture.FAKE_SHA256_TO_SIGN, fixture.HEX);
var sig = new Buffer(fixture.FAKE_SHA256_SIGNATURE, fixture.HEX);
assert.equal(rsa.verify(textToNid(fixture.SHA256), hash, sig), true);
// Private keys should be able to verify too.
hash = new Buffer(fixture.PLAINTEXT_SHA256, fixture.HEX);
sig = new Buffer(fixture.PLAINTEXT_SHA256_SIGNATURE, fixture.HEX);
assert.equal(rsa.verify(textToNid(fixture.SHA256), hash, sig), true);
// Signature mismatch should return false (and not, e.g., throw).
hash = new Buffer(fixture.FAKE_SHA256_TO_SIGN, fixture.HEX);
sig = new Buffer(fixture.PLAINTEXT_SHA256_SIGNATURE, fixture.HEX);
assert.equal(rsa.verify(textToNid(fixture.SHA256), hash, sig), false);
});
it('fail_verify', function() {
var rsa = new RsaWrap();
function f1() {
rsa.verify();
}
assert.throws(f1, /Key not yet set\./);
rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
function f2() {
rsa.verify("x", "x", "x");
}
assert.throws(f2, /Expected a 32-bit integer in args\[0]\./);
function f3() {
rsa.verify(1, "x", "x");
}
assert.throws(f3, /Expected a Buffer in args\[1]\./);
function f4() {
rsa.verify(1, new Buffer(16), "x");
}
assert.throws(f4, /Expected a Buffer in args\[2]\./);
function f5() {
var hash = new Buffer(10);
var sig = new Buffer(5);
hash.fill(0);
sig.fill(0);
rsa.verify(1, hash, sig);
}
assert.throws(f5, /wrong signature length/);
function f6() {
var buf = new Buffer(256);
buf.fill(0);
rsa.verify(1, new Buffer(10), buf);
}
assert.throws(f6, /padding_check/);
function f7() {
var hash = new Buffer(fixture.PLAINTEXT_SHA256, fixture.HEX);
var sig = new Buffer(fixture.PLAINTEXT_SHA256_SIGNATURE, fixture.HEX);
rsa.verify(textToNid(fixture.SHA1), hash, sig);
}
if (nodeVersion < 10) {
assert.throws(f7, /algorithm mismatch/);
} else {
assert.ifError(f7(), true);
}
function f8() {
var hash = new Buffer(fixture.PLAINTEXT_SHA256, fixture.HEX);
var sig = new Buffer(fixture.PLAINTEXT_SHA256_SIGNATURE, fixture.HEX);
rsa.verify(1234567, hash, sig);
}
assert.throws(f8, nodeVersion < 10 ? /algorithm mismatch/ : /unknown algorithm type/);
});
it('textToNid', function() {
// I don't think you can count on the return values being anything
// other than integer values and that aliases should return equal
// values.
function verifyInt(value) {
if (typeof value !== "number") {
throw new Exception("Not a number: " + value);
}
if (value !== Math.floor(value)) {
throw new Exception("Not an integer: " + value);
}
}
verifyInt(textToNid("aes-128-ecb"));
verifyInt(textToNid("md5"));
verifyInt(textToNid("rsa"));
verifyInt(textToNid("sha1"));
verifyInt(textToNid("sha256"));
verifyInt(textToNid("RSA-SHA256"));
verifyInt(textToNid("pkcs7"));
assert.equal(textToNid("RSA-SHA256"), textToNid("sha256WithRSAEncryption"));
assert.equal(textToNid("AES-128-ECB"), textToNid("aes-128-ecb"));
});
it('fail_textToNid', function() {
function f1() {
textToNid();
}
assert.throws(f1, /Missing args\[0\]/);
function f2() {
textToNid(123);
}
assert.throws(f2, /Expected a string in args\[0\]/);
function f3() {
textToNid("blort");
}
assert.throws(f3, /asn1/);
});
it('PSSPadding', function() {
_test_PSSPadding(ursaNative.RSA_PKCS1_SALT_LEN_HLEN);
_test_PSSPadding(ursaNative.RSA_PKCS1_SALT_LEN_RECOVER);
var rsa = new RsaWrap();
rsa.createPublicKeyFromComponents(
new Buffer(fixture.PSS_MODULUS_HEX, fixture.HEX),
new Buffer(fixture.EXPONENT_HEX, fixture.HEX));
var tvhash = new Buffer(fixture.PSS_MHASH_HEX, fixture.HEX);
var tvem = new Buffer(fixture.PSS_EM_HEX, fixture.HEX);
assert.equal(rsa.verifyPSSPadding(
textToNid(fixture.SHA1), tvhash, tvem, ursaNative.RSA_PKCS1_SALT_LEN_HLEN), true);
});
it('fail_PSSPadding', function() {
var rsa = new RsaWrap();
function f1() {
rsa.addPSSPadding();
}
assert.throws(f1, /Key not yet set\./);
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
assert.throws(f1, /Not enough args\./);
var nid = textToNid(fixture.SHA256);
var hash = new Buffer(fixture.FAKE_SHA256_TO_SIGN, fixture.HEX);
var slen = ursaNative.RSA_PKCS1_SALT_LEN_HLEN;
function f2() {
rsa.addPSSPadding("x", hash, slen);
}
assert.throws(f2, /Expected a 32-bit integer in args\[0\]\./);
function f3() {
rsa.addPSSPadding(nid, "x", slen);
}
assert.throws(f3, /Expected a Buffer in args\[1\]\./);
function f4() {
rsa.addPSSPadding(nid, hash, "x");
}
assert.throws(f4, /Expected a 32-bit integer in args\[2\]\./);
function f5() {
rsa.addPSSPadding(nid, hash, 1000000);
}
assert.throws(f5, /data too large for key size/);
function f6() {
rsa.addPSSPadding(nid, hash, -3);
}
assert.throws(f6, /salt length check failed/);
var em = rsa.addPSSPadding(nid, hash, slen);
function f7() {
rsa.verifyPSSPadding();
}
assert.throws(f7, /Not enough args\./);
function f8() {
rsa.verifyPSSPadding("x", hash, em, slen);
}
assert.throws(f8, /Expected a 32-bit integer in args\[0\]\./);
function f9() {
rsa.verifyPSSPadding(nid, "x", em, slen);
}
assert.throws(f9, /Expected a Buffer in args\[1\]\./);
function f10() {
rsa.verifyPSSPadding(nid, hash, "x", slen);
}
assert.throws(f10, /Expected a Buffer in args\[2\]\./);
function f11() {
rsa.verifyPSSPadding(nid, hash, em, "x");
}
assert.throws(f11, /Expected a 32-bit integer in args\[3\]\./);
function f12() {
rsa.verifyPSSPadding(nid, hash, em, 1000000);
}
assert.throws(f12, /data too large/);
function f13() {
rsa.verifyPSSPadding(nid, hash, em, -3);
}
assert.throws(f13, /salt length check failed/);
em[em.length-1] ^= 2;
function f14() {
rsa.verifyPSSPadding(nid, hash, em, slen);
}
assert.throws(f14, /last octet invalid/);
em[em.length-1] ^= 2;
em[1] ^= 2;
assert.throws(f14, /salt length recovery failed/);
});
})
function _test_PSSPadding(slen)
{
var rsa = new RsaWrap();
rsa.setPublicKeyPem(fixture.PUBLIC_KEY);
var nid = textToNid(fixture.SHA256);
var hash = new Buffer(fixture.FAKE_SHA256_TO_SIGN, fixture.HEX);
var em = rsa.addPSSPadding(nid, hash, slen);
assert.equal(rsa.verifyPSSPadding(nid, hash, em, slen), true);
}