test: cleanup and simplify test-crypto-aes-wrap · nodejs/node@f9d6e35

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.

5869

const assert = require('assert');

710

const crypto = require('crypto');

8119-

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

});