/
test-webcrypto-derivekey-cfrg.js
188 lines (168 loc) Β· 4.49 KB
/
test-webcrypto-derivekey-cfrg.js
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
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const { webcrypto } = require('crypto');
const { subtle } = webcrypto;
const kTests = [
{
name: 'X25519',
size: 32,
pkcs8: '302e020100300506032b656e04220420c8838e76d057dfb7d8c95a69e138160ad' +
'd6373fd71a4d276bb56e3a81b64ff61',
spki: '302a300506032b656e0321001cf2b1e6022ec537371ed7f53e54fa1154d83e98eb' +
'64ea51fae5b3307cfe9706',
result: '2768409dfab99ec23b8c89b93ff5880295f76176088f89e43dfebe7ea1950008'
},
{
name: 'X448',
size: 56,
pkcs8: '3046020100300506032b656f043a043858c7d29a3eb519b29d00cfb191bb64fc6' +
'd8a42d8f17176272b89f2272d1819295c6525c0829671b052ef0727530f188e31' +
'd0cc53bf26929e',
spki: '3042300506032b656f033900b604a1d1a5cd1d9426d561ef630a9eb16cbe69d5b9' +
'ca615edc53633efb52ea31e6e6a0a1dbacc6e76cbce6482d7e4ba3d55d9e802765' +
'ce6f',
result: 'f0f6c5f17f94f4291eab7178866d37ec8906dd6c514143dc85be7cf28deff39b'
},
];
async function prepareKeys() {
const keys = {};
await Promise.all(
kTests.map(async ({ name, size, pkcs8, spki, result }) => {
const [
privateKey,
publicKey,
] = await Promise.all([
subtle.importKey(
'pkcs8',
Buffer.from(pkcs8, 'hex'),
{ name },
true,
['deriveKey', 'deriveBits']),
subtle.importKey(
'spki',
Buffer.from(spki, 'hex'),
{ name },
true,
[]),
]);
keys[name] = {
privateKey,
publicKey,
size,
result,
};
}));
return keys;
}
(async function() {
const keys = await prepareKeys();
const otherArgs = [
{ name: 'HMAC', hash: 'SHA-256', length: 256 },
true,
['sign', 'verify']];
await Promise.all(
Object.keys(keys).map(async (name) => {
const { result, privateKey, publicKey } = keys[name];
{
// Good parameters
const key = await subtle.deriveKey({
name,
public: publicKey
}, privateKey, ...otherArgs);
const raw = await subtle.exportKey('raw', key);
assert.strictEqual(Buffer.from(raw).toString('hex'), result);
}
{
// Case insensitivity
const key = await subtle.deriveKey({
name: name.toLowerCase(),
public: publicKey
}, privateKey, {
name: 'HmAc',
hash: 'SHA-256',
length: 256
}, true, ['sign', 'verify']);
const raw = await subtle.exportKey('raw', key);
assert.strictEqual(Buffer.from(raw).toString('hex'), result);
}
}));
// Error tests
{
// Missing public property
await assert.rejects(
subtle.deriveKey(
{ name: 'X448' },
keys.X448.privateKey,
...otherArgs),
{ code: 'ERR_INVALID_ARG_TYPE' });
}
{
// The public property is not a CryptoKey
await assert.rejects(
subtle.deriveKey(
{
name: 'X448',
public: { message: 'Not a CryptoKey' }
},
keys.X448.privateKey,
...otherArgs),
{ code: 'ERR_INVALID_ARG_TYPE' });
}
{
// Mismatched named curves
await assert.rejects(
subtle.deriveKey(
{
name: 'X448',
public: keys.X25519.publicKey
},
keys.X448.privateKey,
...otherArgs),
{ message: 'The public and private keys must be of the same type' });
}
{
// Base key is not a private key
await assert.rejects(
subtle.deriveKey(
{
name: 'X448',
public: keys.X448.publicKey
},
keys.X448.publicKey,
...otherArgs),
{ name: 'InvalidAccessError' });
}
{
// Public is not a public key
await assert.rejects(
subtle.deriveKey(
{
name: 'X448',
public: keys.X448.privateKey
},
keys.X448.privateKey,
...otherArgs),
{ name: 'InvalidAccessError' });
}
{
// Public is a secret key
const keyData = webcrypto.getRandomValues(new Uint8Array(32));
const key = await subtle.importKey(
'raw',
keyData,
{ name: 'AES-CBC', length: 256 },
false, ['encrypt']);
await assert.rejects(
subtle.deriveKey(
{
name: 'X448',
public: key
},
keys.X448.publicKey,
...otherArgs),
{ name: 'InvalidAccessError' });
}
})().then(common.mustCall());