-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
smoke.test.mjs
202 lines (194 loc) · 6.75 KB
/
smoke.test.mjs
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
194
195
196
197
198
199
200
201
202
import test from 'ava'
import * as crypto from 'crypto'
import { conditional, root, keyRoot } from '../dist.mjs'
const {
FlattenedSign,
flattenedVerify,
base64url: { encode: base64url },
} = await import(root)
const { importJWK, generateKeyPair, generateSecret } = await import(keyRoot)
function pubjwk(jwk) {
const { d, p, q, dp, dq, qi, ...publicJwk } = jwk
return publicJwk
}
test.before(async (t) => {
const p256 = {
ext: false,
crv: 'P-256',
x: 'Sp3KpzPjwcCF04_W2GvSSf-vGDvp3Iv2kQYqAjnMB-Y',
y: 'lZmecT2quXe0i9f7b4qHvDAFDpxs0oxCoJx4tOOqsks',
d: 'hRVo5TGE_d_4tQC1KEQIlCdo9rteZmLSmaMPpFOjeDI',
kty: 'EC',
}
const p384 = {
ext: false,
crv: 'P-384',
x: 'H50cO3PJnVhAoF6_jPKpCl60cnvmoygN2u29jVN19x8C2PymixZS4Y5d1FfMMK3L',
y: 'ATQ-4QWyYTtEaBW3CFQZEX0NdDE5g_9F24B0y2xxQgVmWa5Uz0QerlhzFoYU7Z_F',
d: 'HDUcH8y8xr22EroPYBK3PvpNjA3pCJjvHpBXKejxOiQCoXhZ5PhX_nxb7lU0mlDE',
kty: 'EC',
}
const p521 = {
ext: false,
crv: 'P-521',
x: 'AFy8VTdHeJx7rrUajpeqIjGZsjtx0tftQ7pdL1qkTRpnvY0WVVKXjib3HINNLMA72gA7JujbvEtGvkvoo0P7pGVK',
y: 'AfMRSFv9qfcH_XMHfPoltBMYLhDbS3Pw1GL7NO9SI_vF4JsiAta1Bq6teCl2z8klFtRCWXHfPqEF3cmXS8bDQVoT',
d: 'AL123tYK7y-iViaReLOHe7XxKNSeoUMk4RRmcP6nSVuZrYJHtyYPak4gUmWB6A_GzED3zXkrkcssZEzHrYQILw5c',
kty: 'EC',
}
const secp256k1 = {
ext: false,
crv: 'secp256k1',
x: 'Hr99bGFN0HWT3ZgNNAXvmz-6Wk1HIxwsyFqUZH8PBFc',
y: 'bonc3DRZe51NzuWetY336VmTYdUFvPK7DivSPHeu_CA',
d: 'jv_VrhPomm6_WOzb74xF4eMI0hu9p0W1Zlxi0nz8AFs',
kty: 'EC',
}
const rsa = {
ext: false,
e: 'AQAB',
n: 'rPOe2kdmQY1wN2ADn-EIKsPO_kXotIqPjbZQOoW6sIHPGS8xJqrR60JLTBir5jG8Prrp4_KoEDGm3zYlWb5EJr9BMAVHAUYB2ZMXjdojpIkKeJ_cMI0lLcSUTkKkOA6u2DnVJBlnQgeyVZ927h65cJ5zsnSqG6MHh5tVPAwocEsfH27Tik_Fp-dCidmr_-rCrPfoAuASadvuFr5oAa7aAKdlLz6P0GgmCs2KzTAR6FiBMm48hd1YkrIaGzljKOwCgh0KYA3tnetbOCFEuzsmn9wmQHUA0iPQqYGlFJVfQ64949hQk42uWxCXtaO_wlXqy_TVki5LTQ7hO4reUiMXpQ',
d: 'qhn97b91khmS-dOkHPYNu0nUZv_JDPCOmhlqtPRcFkfFsYZZuCcfyVvthM1rHD9kXuolKf26UBsVfcnaWHaqvtUyPxGhsV3yadSiwPCAR85FDzhjLxlTLL2AA6zFqSC_1Iik2hlm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ccQOBFbqSL6YkXwCBxlkCrzTlhc',
kty: 'RSA',
}
const ed25519 = {
ext: false,
crv: 'Ed25519',
x: 'beguov5KuiEE0a9t_NaKRwnqLp0OIURn2BTYmdLMed4',
d: 'Z1Q1Hgl8PfBvypfTzQOklI5r0y4ImPfWHP4OoIKm_kA',
kty: 'OKP',
}
const ed448 = {
ext: false,
crv: 'Ed448',
x: 'YecSEZalR2McQ2f0yU8mMwLXSzz7eHirRsv0o9lqGhCZMihCPjpLKJ9BHj3u2DTnLtcBXGYmJbYA',
d: 'epoLrq-y89h6gjCEZl39VGKQKqqHlTgt0gp7mMM1bbZOPkToOOusOvxwayZlVU8pRtLbAIxcTK7n',
kty: 'OKP',
}
t.context.keys = {
rsa: {
public: pubjwk(rsa),
private: rsa,
algs: ['RS256', 'RS384', 'RS512', 'PS256', 'PS384', 'PS512'],
},
ed25519: {
public: pubjwk(ed25519),
private: ed25519,
algs: ['EdDSA'],
generate: { crv: 'Ed25519' },
},
ed448: {
public: pubjwk(ed448),
private: ed448,
algs: ['EdDSA'],
generate: { crv: 'Ed448' },
},
p256: {
public: pubjwk(p256),
private: p256,
algs: ['ES256'],
},
secp256k1: {
public: pubjwk(secp256k1),
private: secp256k1,
algs: ['ES256K'],
},
p384: {
public: pubjwk(p384),
private: p384,
algs: ['ES384'],
},
p521: {
public: pubjwk(p521),
private: p521,
algs: ['ES512'],
},
oct256: {
secret: {
ext: false,
kty: 'oct',
k: base64url(crypto.randomFillSync(new Uint8Array(256 >> 3))),
},
algs: ['HS256'],
},
oct384: {
secret: {
ext: false,
kty: 'oct',
k: base64url(crypto.randomFillSync(new Uint8Array(384 >> 3))),
},
algs: ['HS256', 'HS384'],
},
oct512: {
secret: {
ext: false,
kty: 'oct',
k: base64url(crypto.randomFillSync(new Uint8Array(512 >> 3))),
},
algs: ['HS256', 'HS384', 'HS512'],
},
}
})
async function smoke(t, ref, octAsKeyObject = false) {
const fixtures = t.context.keys[ref]
await Promise.all([
...fixtures.algs.map(async (alg) => {
let priv
let pub
if ('secret' in fixtures) {
const secret = await importJWK(fixtures.secret, alg, octAsKeyObject)
if (octAsKeyObject) {
t.false(secret instanceof Uint8Array)
} else {
t.true(secret instanceof Uint8Array)
}
pub = secret
priv = secret
} else {
;[pub, priv] = await Promise.all([
importJWK(fixtures.public, alg),
importJWK(fixtures.private, alg),
])
}
const jws = await new FlattenedSign(crypto.randomFillSync(new Uint8Array(256 >> 3)))
.setProtectedHeader({ alg })
.sign(priv)
await flattenedVerify(jws, pub)
}),
...fixtures.algs.map(async (alg) => {
let priv
let pub
if ('secret' in fixtures) {
const secret = await generateSecret(alg)
pub = secret
priv = secret
} else {
;({ privateKey: priv, publicKey: pub } = await generateKeyPair(alg, fixtures.generate))
}
const jws = await new FlattenedSign(crypto.randomFillSync(new Uint8Array(256 >> 3)))
.setProtectedHeader({ alg })
.sign(priv)
await flattenedVerify(jws, pub)
}),
])
t.pass()
}
smoke.title = (title, ref) => `${ref.toUpperCase()}${title ? ` ${title}` : ''}`
test(smoke, 'rsa')
test(smoke, 'p256')
test(smoke, 'p384')
test(smoke, 'p521')
test(smoke, 'oct256')
test(smoke, 'oct384')
test(smoke, 'oct512')
test(smoke, 'ed25519')
test('as keyobject', smoke, 'oct256', true)
test('as keyobject', smoke, 'oct384', true)
test('as keyobject', smoke, 'oct512', true)
conditional({ webcrypto: 0, electron: 0 })(smoke, 'secp256k1')
conditional({ electron: 0 })(smoke, 'ed448')