|
1 | 1 | 'use strict'; |
2 | 2 | const common = require('../common'); |
3 | | -if (!common.hasCrypto) |
| 3 | +if (!common.hasCrypto) { |
4 | 4 | common.skip('missing crypto'); |
| 5 | +} |
| 6 | + |
| 7 | +// Tests that the AES wrap and unwrap functions are working correctly. |
5 | 8 |
|
6 | 9 | const assert = require('assert'); |
7 | 10 | const crypto = require('crypto'); |
8 | 11 |
|
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 | +[ |
10 | 19 | { |
11 | 20 | algorithm: 'aes128-wrap', |
12 | | - key: 'b26f309fbe57e9b3bb6ae5ef31d54450', |
13 | | - iv: '3fd838af4093d749', |
| 21 | + key: key1, |
| 22 | + iv: ivLong, |
14 | 23 | text: '12345678123456781234567812345678' |
15 | 24 | }, |
16 | 25 | { |
17 | 26 | algorithm: 'id-aes128-wrap-pad', |
18 | | - key: 'b26f309fbe57e9b3bb6ae5ef31d54450', |
19 | | - iv: '3fd838af', |
| 27 | + key: key1, |
| 28 | + iv: ivShort, |
20 | 29 | text: '12345678123456781234567812345678123' |
21 | 30 | }, |
22 | 31 | { |
23 | 32 | algorithm: 'aes192-wrap', |
24 | | - key: '40978085d68091f7dfca0d7dfc7a5ee76d2cc7f2f345a304', |
25 | | - iv: '3fd838af4093d749', |
| 33 | + key: key2, |
| 34 | + iv: ivLong, |
26 | 35 | text: '12345678123456781234567812345678' |
27 | 36 | }, |
28 | 37 | { |
29 | 38 | algorithm: 'id-aes192-wrap-pad', |
30 | | - key: '40978085d68091f7dfca0d7dfc7a5ee76d2cc7f2f345a304', |
31 | | - iv: '3fd838af', |
| 39 | + key: key2, |
| 40 | + iv: ivShort, |
32 | 41 | text: '12345678123456781234567812345678123' |
33 | 42 | }, |
34 | 43 | { |
35 | 44 | algorithm: 'aes256-wrap', |
36 | | - key: '29c9eab5ed5ad44134a1437fe2e673b4d88a5b7c72e68454fea08721392b7323', |
37 | | - iv: '3fd838af4093d749', |
| 45 | + key: key3, |
| 46 | + iv: ivLong, |
38 | 47 | text: '12345678123456781234567812345678' |
39 | 48 | }, |
40 | 49 | { |
41 | 50 | algorithm: 'id-aes256-wrap-pad', |
42 | | - key: '29c9eab5ed5ad44134a1437fe2e673b4d88a5b7c72e68454fea08721392b7323', |
43 | | - iv: '3fd838af', |
| 51 | + key: key3, |
| 52 | + iv: ivShort, |
44 | 53 | text: '12345678123456781234567812345678123' |
45 | 54 | }, |
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`); |
62 | 60 | }); |
0 commit comments