Skip to content

Commit e49f3e9

Browse files
jasnelladuh95
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 <yagiz@nizipli.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 85f7bbf commit e49f3e9

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

test/parallel/test-crypto-aes-wrap.js

+28-30
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,60 @@
11
'use strict';
22
const common = 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
const assert = require('assert');
710
const crypto = require('crypto');
811

9-
const test = [
12+
const ivShort = Buffer.from('3fd838af', 'hex');
13+
const ivLong = Buffer.from('3fd838af4093d749', 'hex');
14+
const key1 = Buffer.from('b26f309fbe57e9b3bb6ae5ef31d54450', 'hex');
15+
const key2 = Buffer.from('40978085d68091f7dfca0d7dfc7a5ee76d2cc7f2f345a304', 'hex');
16+
const key3 = 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-
const cipher = crypto.createCipheriv(
50-
data.algorithm,
51-
Buffer.from(data.key, 'hex'),
52-
Buffer.from(data.iv, 'hex'));
53-
const ciphertext = cipher.update(data.text, 'utf8');
54-
55-
const decipher = crypto.createDecipheriv(
56-
data.algorithm,
57-
Buffer.from(data.key, 'hex'),
58-
Buffer.from(data.iv, 'hex'));
59-
const msg = decipher.update(ciphertext, 'buffer', 'utf8');
60-
61-
assert.strictEqual(msg, data.text, `${data.algorithm} test case failed`);
55+
].forEach(({ algorithm, key, iv, text }) => {
56+
const cipher = crypto.createCipheriv(algorithm, key, iv);
57+
const decipher = crypto.createDecipheriv(algorithm, key, iv);
58+
const msg = decipher.update(cipher.update(text, 'utf8'), 'buffer', 'utf8');
59+
assert.strictEqual(msg, text, `${algorithm} test case failed`);
6260
});

0 commit comments

Comments
 (0)