Skip to content

Commit f9d6e35

Browse files
jasnellmarco-ippolito
authored andcommitted
test: cleanup and simplify test-crypto-aes-wrap
* Add comment explaining purpose of the test * Eliminate duplicative/extraneous buffer allocations PR-URL: #56748 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 792ce98 commit f9d6e35

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed
Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,60 @@
11
'use strict';
22
constcommon=require('../common');
3-
if(!common.hasCrypto)
3+
if(!common.hasCrypto){
44
common.skip('missing crypto');
5+
}
6+
7+
// Tests that the AES wrap and unwrap functions are working correctly.
58

69
constassert=require('assert');
710
constcrypto=require('crypto');
811

9-
consttest=[
12+
constivShort=Buffer.from('3fd838af','hex');
13+
constivLong=Buffer.from('3fd838af4093d749','hex');
14+
constkey1=Buffer.from('b26f309fbe57e9b3bb6ae5ef31d54450','hex');
15+
constkey2=Buffer.from('40978085d68091f7dfca0d7dfc7a5ee76d2cc7f2f345a304','hex');
16+
constkey3=Buffer.from('29c9eab5ed5ad44134a1437fe2e673b4d88a5b7c72e68454fea08721392b7323','hex');
17+
18+
[
1019
{
1120
algorithm: 'aes128-wrap',
12-
key: 'b26f309fbe57e9b3bb6ae5ef31d54450',
13-
iv: '3fd838af4093d749',
21+
key: key1,
22+
iv: ivLong,
1423
text: '12345678123456781234567812345678'
1524
},
1625
{
1726
algorithm: 'id-aes128-wrap-pad',
18-
key: 'b26f309fbe57e9b3bb6ae5ef31d54450',
19-
iv: '3fd838af',
27+
key: key1,
28+
iv: ivShort,
2029
text: '12345678123456781234567812345678123'
2130
},
2231
{
2332
algorithm: 'aes192-wrap',
24-
key: '40978085d68091f7dfca0d7dfc7a5ee76d2cc7f2f345a304',
25-
iv: '3fd838af4093d749',
33+
key: key2,
34+
iv: ivLong,
2635
text: '12345678123456781234567812345678'
2736
},
2837
{
2938
algorithm: 'id-aes192-wrap-pad',
30-
key: '40978085d68091f7dfca0d7dfc7a5ee76d2cc7f2f345a304',
31-
iv: '3fd838af',
39+
key: key2,
40+
iv: ivShort,
3241
text: '12345678123456781234567812345678123'
3342
},
3443
{
3544
algorithm: 'aes256-wrap',
36-
key: '29c9eab5ed5ad44134a1437fe2e673b4d88a5b7c72e68454fea08721392b7323',
37-
iv: '3fd838af4093d749',
45+
key: key3,
46+
iv: ivLong,
3847
text: '12345678123456781234567812345678'
3948
},
4049
{
4150
algorithm: 'id-aes256-wrap-pad',
42-
key: '29c9eab5ed5ad44134a1437fe2e673b4d88a5b7c72e68454fea08721392b7323',
43-
iv: '3fd838af',
51+
key: key3,
52+
iv: ivShort,
4453
text: '12345678123456781234567812345678123'
4554
},
46-
];
47-
48-
test.forEach((data)=>{
49-
constcipher=crypto.createCipheriv(
50-
data.algorithm,
51-
Buffer.from(data.key,'hex'),
52-
Buffer.from(data.iv,'hex'));
53-
constciphertext=cipher.update(data.text,'utf8');
54-
55-
constdecipher=crypto.createDecipheriv(
56-
data.algorithm,
57-
Buffer.from(data.key,'hex'),
58-
Buffer.from(data.iv,'hex'));
59-
constmsg=decipher.update(ciphertext,'buffer','utf8');
60-
61-
assert.strictEqual(msg,data.text,`${data.algorithm} test case failed`);
55+
].forEach(({ algorithm, key, iv, text })=>{
56+
constcipher=crypto.createCipheriv(algorithm,key,iv);
57+
constdecipher=crypto.createDecipheriv(algorithm,key,iv);
58+
constmsg=decipher.update(cipher.update(text,'utf8'),'buffer','utf8');
59+
assert.strictEqual(msg,text,`${algorithm} test case failed`);
6260
});

0 commit comments

Comments
(0)