-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathtest-webcrypto-sign-verify-kmac.js
More file actions
193 lines (168 loc) Β· 4.56 KB
/
test-webcrypto-sign-verify-kmac.js
File metadata and controls
193 lines (168 loc) Β· 4.56 KB
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
189
190
191
192
193
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const { hasOpenSSL } = require('../common/crypto');
if (!hasOpenSSL(3))
common.skip('requires OpenSSL >= 3');
const assert = require('assert');
const { subtle } = globalThis.crypto;
const vectors = require('../fixtures/crypto/kmac')();
async function testVerify({ algorithm,
key,
data,
customization,
outputLength,
expected }) {
const [
verifyKey,
noVerifyKey,
keyPair,
] = await Promise.all([
subtle.importKey(
'raw-secret',
key,
{ name: algorithm },
false,
['verify']),
subtle.importKey(
'raw-secret',
key,
{ name: algorithm },
false,
['sign']),
subtle.generateKey(
'Ed25519',
false,
['sign']),
]);
const signParams = {
name: algorithm,
outputLength,
customization,
};
assert(await subtle.verify(signParams, verifyKey, expected, data));
// Test verification with altered buffers
const copy = Buffer.from(data);
const sigcopy = Buffer.from(expected);
const p = subtle.verify(signParams, verifyKey, sigcopy, copy);
copy[0] = 255 - copy[0];
sigcopy[0] = 255 - sigcopy[0];
assert(await p);
// Test failure when using wrong key
await assert.rejects(
subtle.verify(signParams, noVerifyKey, expected, data), {
message: /Unable to use this key to verify/
});
// Test failure when using the wrong algorithms
await assert.rejects(
subtle.verify(signParams, keyPair.publicKey, expected, data), {
message: /Unable to use this key to verify/
});
// Test failure when signature is altered
{
const copy = Buffer.from(expected);
copy[0] = 255 - copy[0];
assert(!(await subtle.verify(
signParams,
verifyKey,
copy,
data)));
assert(!(await subtle.verify(
signParams,
verifyKey,
copy.slice(1),
data)));
}
// Test failure when data is altered
{
const copy = Buffer.from(data);
copy[0] = 255 - copy[0];
assert(!(await subtle.verify(signParams, verifyKey, expected, copy)));
}
// Test failure when wrong algorithm is used
{
const otherAlgorithm = algorithm === 'KMAC128' ? 'KMAC256' : 'KMAC128';
const keyWithOtherAlgorithm = await subtle.importKey(
'raw-secret',
key,
{ name: otherAlgorithm },
false,
['verify']);
const otherParams = { ...signParams, name: otherAlgorithm };
assert(!(await subtle.verify(otherParams, keyWithOtherAlgorithm, expected, data)));
}
// Test failure when output length is different
{
assert(!(await subtle.verify({
...signParams,
outputLength: outputLength === 256 ? 512 : 256,
}, verifyKey, expected, data)));
}
}
async function testSign({ algorithm,
key,
data,
customization,
outputLength,
expected }) {
const [
signKey,
noSignKey,
keyPair,
] = await Promise.all([
subtle.importKey(
'raw-secret',
key,
{ name: algorithm },
false,
['verify', 'sign']),
subtle.importKey(
'raw-secret',
key,
{ name: algorithm },
false,
['verify']),
subtle.generateKey(
'Ed25519',
false,
['sign']),
]);
const signParams = {
name: algorithm,
outputLength,
customization,
};
{
const sig = await subtle.sign(signParams, signKey, data);
assert.strictEqual(
Buffer.from(sig).toString('hex'),
Buffer.from(expected).toString('hex'));
assert(await subtle.verify(signParams, signKey, sig, data));
}
{
const copy = Buffer.from(data);
const p = subtle.sign(signParams, signKey, copy);
copy[0] = 255 - copy[0];
const sig = await p;
assert(await subtle.verify(signParams, signKey, sig, data));
}
// Test failure when no sign usage
await assert.rejects(
subtle.sign(signParams, noSignKey, data), {
message: /Unable to use this key to sign/
});
// Test failure when using the wrong algorithms
await assert.rejects(
subtle.sign(signParams, keyPair.privateKey, data), {
message: /Unable to use this key to sign/
});
}
(async function() {
const variations = [];
for (const vector of vectors) {
variations.push(testVerify(vector));
variations.push(testSign(vector));
}
await Promise.all(variations);
})().then(common.mustCall());