-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
rsa-pss.test.mjs
142 lines (135 loc) · 4.99 KB
/
rsa-pss.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
import test from 'ava'
import * as crypto from 'crypto'
import { promisify } from 'util'
const generateKeyPair = promisify(crypto.generateKeyPair)
const [major, minor] = process.versions.node.split('.').map((str) => parseInt(str, 10))
const rsaPssParams = major >= 17 || (major === 16 && minor >= 9)
const { FlattenedSign, flattenedVerify } = await import('#dist')
if (rsaPssParams) {
for (const length of [256, 384, 512]) {
test(`valid RSASSA-PSS-Params PS${length}`, async (t) => {
for (const options of [
{ modulusLength: 2048 },
{
modulusLength: 2048,
hashAlgorithm: `sha${length}`,
hash: `sha${length}`,
mgf1HashAlgorithm: `sha${length}`,
mgf1Hash: `sha${length}`,
saltLength: 0,
},
{
modulusLength: 2048,
hashAlgorithm: `sha${length}`,
hash: `sha${length}`,
mgf1HashAlgorithm: `sha${length}`,
mgf1Hash: `sha${length}`,
saltLength: length >> 3,
},
]) {
const { privateKey, publicKey } = await generateKeyPair('rsa-pss', options)
const jws = await new FlattenedSign(new Uint8Array(0))
.setProtectedHeader({ alg: `PS${length}` })
.sign(privateKey)
await flattenedVerify(jws, publicKey)
}
t.pass()
})
test(`invalid saltLength for PS${length}`, async (t) => {
const { privateKey, publicKey } = await generateKeyPair('rsa-pss', {
modulusLength: 2048,
hashAlgorithm: `sha${length}`,
hash: `sha${length}`,
mgf1HashAlgorithm: `sha${length}`,
mgf1Hash: `sha${length}`,
saltLength: (length >> 3) + 1,
})
await t.throwsAsync(
new FlattenedSign(new Uint8Array(0))
.setProtectedHeader({ alg: `PS${length}` })
.sign(privateKey),
{
message: `Invalid key for this operation, its RSA-PSS parameter saltLength does not meet the requirements of "alg" PS${length}`,
instanceOf: TypeError,
},
)
await t.throwsAsync(
flattenedVerify({ header: { alg: `PS${length}` }, payload: '', signature: '' }, publicKey),
{
message: `Invalid key for this operation, its RSA-PSS parameter saltLength does not meet the requirements of "alg" PS${length}`,
instanceOf: TypeError,
},
)
})
test(`invalid hashAlgorithm for PS${length}`, async (t) => {
const { privateKey, publicKey } = await generateKeyPair('rsa-pss', {
modulusLength: 2048,
hashAlgorithm: 'sha1',
hash: 'sha1',
mgf1HashAlgorithm: `sha${length}`,
mgf1Hash: `sha${length}`,
saltLength: length >> 3,
})
await t.throwsAsync(
new FlattenedSign(new Uint8Array(0))
.setProtectedHeader({ alg: `PS${length}` })
.sign(privateKey),
{
message: `Invalid key for this operation, its RSA-PSS parameters do not meet the requirements of "alg" PS${length}`,
instanceOf: TypeError,
},
)
await t.throwsAsync(
flattenedVerify({ header: { alg: `PS${length}` }, payload: '', signature: '' }, publicKey),
{
message: `Invalid key for this operation, its RSA-PSS parameters do not meet the requirements of "alg" PS${length}`,
instanceOf: TypeError,
},
)
})
test(`invalid mgf1HashAlgorithm for PS${length}`, async (t) => {
const { privateKey, publicKey } = await generateKeyPair('rsa-pss', {
modulusLength: 2048,
hashAlgorithm: `sha${length}`,
hash: `sha${length}`,
mgf1HashAlgorithm: 'sha1',
mgf1Hash: 'sha1',
saltLength: length >> 3,
})
await t.throwsAsync(
new FlattenedSign(new Uint8Array(0))
.setProtectedHeader({ alg: `PS${length}` })
.sign(privateKey),
{
message: `Invalid key for this operation, its RSA-PSS parameters do not meet the requirements of "alg" PS${length}`,
instanceOf: TypeError,
},
)
await t.throwsAsync(
flattenedVerify({ header: { alg: `PS${length}` }, payload: '', signature: '' }, publicKey),
{
message: `Invalid key for this operation, its RSA-PSS parameters do not meet the requirements of "alg" PS${length}`,
instanceOf: TypeError,
},
)
})
}
} else {
test('does not support rsa-pss', async (t) => {
const { privateKey, publicKey } = await generateKeyPair('rsa-pss', { modulusLength: 2048 })
await t.throwsAsync(
new FlattenedSign(new Uint8Array(0)).setProtectedHeader({ alg: 'PS256' }).sign(privateKey),
{
message: 'Invalid key for this operation, its asymmetricKeyType must be rsa',
instanceOf: TypeError,
},
)
await t.throwsAsync(
flattenedVerify({ header: { alg: 'PS256' }, payload: '', signature: '' }, publicKey),
{
message: 'Invalid key for this operation, its asymmetricKeyType must be rsa',
instanceOf: TypeError,
},
)
})
}