forked from ThalesGroup/gose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_generator_test.go
264 lines (228 loc) · 6.5 KB
/
key_generator_test.go
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2019 Thales e-Security, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gose
import (
"fmt"
"testing"
"github.com/ThalesIgnite/gose/jose"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func BenchmarkRsaSigningKeyGenerator_Generate(b *testing.B) {
setups := map[jose.Alg]int{
jose.AlgRS256: 2048,
jose.AlgRS384: 2048,
jose.AlgRS512: 2048,
}
generator := new(RsaSigningKeyGenerator)
for algo, length := range setups {
b.Run(fmt.Sprintf("%v_%v", algo, length), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = generator.Generate(algo, length, []jose.KeyOps{jose.KeyOpsSign})
}
})
}
}
func BenchmarkECDSASigningKeyGenerator_Generate(b *testing.B) {
setups := map[jose.Alg]int{
jose.AlgES256: 256,
jose.AlgES384: 384,
jose.AlgES512: 512,
}
generator := new(ECDSASigningKeyGenerator)
for algo, length := range setups {
b.Run(fmt.Sprintf("%v_%v", algo, length), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = generator.Generate(algo, []jose.KeyOps{jose.KeyOpsSign})
}
})
}
}
func TestRSAGenerateSigningKeySucceeds(t *testing.T) {
// Setup
generator := new(RsaSigningKeyGenerator)
cases := []struct {
bits int
alg jose.Alg
}{
{
bits: 2048,
alg: jose.AlgRS256,
},
{
bits: 2048,
alg: jose.AlgRS384,
},
{
bits: 2048,
alg: jose.AlgRS512,
},
{
bits: 2048,
alg: jose.AlgPS256,
},
{
bits: 2048,
alg: jose.AlgPS384,
},
{
bits: 2048,
alg: jose.AlgPS512,
},
}
// Act
for _, test := range cases {
key, err := generator.Generate(test.alg, test.bits, []jose.KeyOps{jose.KeyOpsSign})
// Assert
require.Nil(t, err)
require.NotNil(t, key)
jwk, err := key.Jwk()
require.NoError(t, err)
assert.Equal(t, test.alg, jwk.Alg())
}
}
func TestECDSAGenerateSigningKeySucceeds(t *testing.T) {
// Setup
generator := new(ECDSASigningKeyGenerator)
cases := []struct {
bits int
alg jose.Alg
}{
{
bits: 256,
alg: jose.AlgES256,
},
{
bits: 384,
alg: jose.AlgES384,
},
{
bits: 512,
alg: jose.AlgES512,
},
}
// Act
for _, test := range cases {
key, err := generator.Generate(test.alg, []jose.KeyOps{jose.KeyOpsSign})
// Assert
require.Nil(t, err)
require.NotNil(t, key)
jwk, err := key.Jwk()
require.NoError(t, err)
assert.Equal(t, test.alg, jwk.Alg())
}
}
func TestGenerateSigningKeyFailsWhenInvalidAlgorithm(t *testing.T) {
// Setup
generator := new(RsaSigningKeyGenerator)
// Act
key, err := generator.Generate(jose.AlgES256, 2048, []jose.KeyOps{jose.KeyOpsSign})
// Assert
assert.Nil(t, key)
assert.Equal(t, ErrInvalidAlgorithm, err)
}
func TestGenerateSigningKeyFailsWhenInvalidKeySize(t *testing.T) {
// Setup
generator := new(RsaSigningKeyGenerator)
// Act
key, err := generator.Generate(jose.AlgRS256, 1024, []jose.KeyOps{jose.KeyOpsSign})
// Assert
assert.Nil(t, key)
assert.Equal(t, ErrInvalidKeySize, err)
}
func TestGenerateSigningKeyFailsWhenInvalidOperation(t *testing.T) {
// Setup
testCase := [][]jose.KeyOps{
{jose.KeyOpsVerify},
}
generator := new(RsaSigningKeyGenerator)
for _, test := range testCase {
// Act
key, err := generator.Generate(jose.AlgRS256, 2048, test)
// Assert
assert.Nil(t, key)
assert.Equal(t, ErrInvalidOperations, err)
}
}
// TODO: redo these tests, as monkey patching doesn't seem to work here
//
//func TestGenerateSigningKeyFailsWhenGenerateKeyFails(t *testing.T) {
// // Setup
// generator := new(RsaSigningKeyGenerator)
// expectedError := errors.New("Expected error")
// defer monkey.Patch(rsa.GenerateKey, func(reader io.Reader, bits int) (*rsa.PrivateKey, error) {
// t.Log("patching")
// return nil, expectedError
// }).Unpatch()
//
// // Act
// k, e := generator.Generate(jose.AlgRS256, 2048, []jose.KeyOps{jose.KeyOpsSign})
//
// // Assert
// require.Nil(t, k)
// require.Error(t, expectedError, e)
//}
//
//func TestGenerateSigningKeyFailsWhenExponentTooBig(t *testing.T) {
// // Setup
// fakeKey := rsa.PrivateKey{}
// fakeKey.E = math.MaxInt64
// defer monkey.PatchInstanceMethod(reflect.TypeOf(&fakeKey), "Validate",
// func(*rsa.PrivateKey) error { return nil },
// ).Unpatch()
// generator := new(RsaSigningKeyGenerator)
// defer monkey.Patch(rsa.GenerateKey, func(reader io.Reader, bits int) (*rsa.PrivateKey, error) {
// var k rsa.PrivateKey
// k.E = math.MaxInt64
// return &k, nil
// }).Unpatch()
//
// // Act
// k, e := generator.Generate(jose.AlgRS256, 2048, []jose.KeyOps{jose.KeyOpsSign})
//
// // Assert
// require.Error(t, ErrInvalidExponent, e)
// require.Nil(t, k)
//
//}
func TestAuthenticatedEncryptionKeyGenerator_Generate_InvalidAlgorithm(t *testing.T) {
generator := &AuthenticatedEncryptionKeyGenerator{}
key, jwk, err := generator.Generate(jose.AlgES256, []jose.KeyOps{})
assert.Nil(t, key)
assert.Nil(t, jwk)
assert.Equal(t, ErrInvalidAlgorithm, err)
}
func TestAuthenticatedEncryptionKeyGenerator_Generate_NoValidOperations(t *testing.T) {
generator := &AuthenticatedEncryptionKeyGenerator{}
key, jwk, err := generator.Generate(jose.AlgA256GCM, []jose.KeyOps{})
assert.Nil(t, key)
assert.Nil(t, jwk)
assert.Equal(t, ErrInvalidOperations, err)
}
func TestAuthenticatedEncryptionKeyGenerator_Generate(t *testing.T) {
generator := &AuthenticatedEncryptionKeyGenerator{}
for _, alg := range []jose.Alg{jose.AlgA128GCM, jose.AlgA192GCM, jose.AlgA256GCM} {
key, jwk, err := generator.Generate(alg, []jose.KeyOps{jose.KeyOpsDecrypt, jose.KeyOpsEncrypt})
assert.NotNil(t, key)
assert.NotNil(t, jwk)
assert.NoError(t, err)
}
}