-
Notifications
You must be signed in to change notification settings - Fork 9
/
aes_gcm_cryptor_test.go
183 lines (162 loc) · 6.5 KB
/
aes_gcm_cryptor_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
// Copyright 2024 Thales Group
//
// 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 (
"crypto/rand"
"testing"
"github.com/ThalesGroup/gose/jose"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
fakeKeyMaterial = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
fakeNonce = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
fakeTag = []byte{0x70, 0x7d, 0xcf, 0x80, 0xa1, 0xb9, 0xa6, 0x17, 0x03, 0xe7, 0x95, 0xd4, 0xd1, 0x09, 0xf0, 0xfd}
fakePlaintext = []byte{0x01}
fakeCiphertext = []byte{0xcf}
)
func TestNewAesGcmCryptor_InvalidOps(t *testing.T) {
key := &jose.OctSecretKey{}
key.SetAlg(jose.AlgA256GCM)
key.SetOps([]jose.KeyOps{jose.KeyOpsEncrypt})
cryptor, err := NewAesGcmCryptorFromJwk(key, []jose.KeyOps{jose.KeyOpsDecrypt})
assert.Nil(t, cryptor)
assert.Equal(t, err, ErrInvalidOperations)
}
func TestNewAesGcmCryptor_InvalidKey(t *testing.T) {
key := &jose.OctSecretKey{}
key.SetAlg(jose.AlgES256)
key.SetOps([]jose.KeyOps{jose.KeyOpsEncrypt, jose.KeyOpsDecrypt})
cryptor, err := NewAesGcmCryptorFromJwk(key, []jose.KeyOps{jose.KeyOpsDecrypt})
assert.Nil(t, cryptor)
assert.Equal(t, err, ErrInvalidKeyType)
}
func TestNewAesGcmCryptor(t *testing.T) {
key := &jose.OctSecretKey{}
key.SetAlg(jose.AlgA256GCM)
key.SetOps([]jose.KeyOps{jose.KeyOpsEncrypt, jose.KeyOpsDecrypt})
key.SetKid("something-unique")
key.K.SetBytes(fakeKeyMaterial)
cryptor, err := NewAesGcmCryptorFromJwk(key, []jose.KeyOps{jose.KeyOpsDecrypt})
assert.NotNil(t, cryptor)
assert.NoError(t, err)
}
func TestAesGcmCryptor_GenerateNonce(t *testing.T) {
key := &jose.OctSecretKey{}
key.SetAlg(jose.AlgA256GCM)
key.SetOps([]jose.KeyOps{jose.KeyOpsEncrypt, jose.KeyOpsDecrypt})
key.SetKid("something-unique")
key.K.SetBytes(fakeKeyMaterial)
cryptor, err := NewAesGcmCryptorFromJwk(key, []jose.KeyOps{jose.KeyOpsDecrypt})
require.NotNil(t, cryptor)
require.NoError(t, err)
nonce, err := cryptor.GenerateNonce()
require.NoError(t, err)
assert.Len(t, nonce, 12)
}
func TestAesGcmCryptor_Getters(t *testing.T) {
key := &jose.OctSecretKey{}
key.SetAlg(jose.AlgA256GCM)
key.SetOps([]jose.KeyOps{jose.KeyOpsEncrypt, jose.KeyOpsDecrypt})
key.SetKid("something-unique")
key.K.SetBytes(fakeKeyMaterial)
cryptor, err := NewAesGcmCryptorFromJwk(key, []jose.KeyOps{jose.KeyOpsDecrypt})
require.NotNil(t, cryptor)
require.NoError(t, err)
assert.Equal(t, cryptor.Kid(), "something-unique")
assert.Equal(t, cryptor.Algorithm(), jose.AlgA256GCM)
}
func TestAesGcmCryptor_Seal_InvalidOps(t *testing.T) {
key := &jose.OctSecretKey{}
key.SetAlg(jose.AlgA256GCM)
key.SetOps([]jose.KeyOps{jose.KeyOpsDecrypt})
key.SetKid("something-unique")
key.K.SetBytes(fakeKeyMaterial)
cryptor, err := NewAesGcmCryptorFromJwk(key, []jose.KeyOps{jose.KeyOpsDecrypt})
require.NotNil(t, cryptor)
require.NoError(t, err)
ciphertext, tag, err := cryptor.Seal(jose.KeyOpsEncrypt, fakeNonce, fakePlaintext, nil)
assert.Nil(t, ciphertext)
assert.Nil(t, tag)
assert.Equal(t, err, ErrInvalidOperations)
}
func TestAesGcmCryptor_Seal(t *testing.T) {
key := &jose.OctSecretKey{}
key.SetAlg(jose.AlgA256GCM)
key.SetOps([]jose.KeyOps{jose.KeyOpsEncrypt})
key.SetKid("something-unique")
key.K.SetBytes(fakeKeyMaterial)
cryptor, err := NewAesGcmCryptorFromJwk(key, []jose.KeyOps{jose.KeyOpsEncrypt})
require.NotNil(t, cryptor)
require.NoError(t, err)
ciphertext, tag, err := cryptor.Seal(jose.KeyOpsEncrypt, fakeNonce, fakePlaintext, nil)
assert.Len(t, ciphertext, 1)
assert.Len(t, tag, 16)
assert.NoError(t, err)
}
func TestAesGcmCryptor_Open_InvalidOps(t *testing.T) {
key := &jose.OctSecretKey{}
key.SetAlg(jose.AlgA256GCM)
key.SetOps([]jose.KeyOps{jose.KeyOpsEncrypt})
key.SetKid("something-unique")
key.K.SetBytes(fakeKeyMaterial)
cryptor, err := NewAesGcmCryptorFromJwk(key, []jose.KeyOps{jose.KeyOpsEncrypt})
require.NotNil(t, cryptor)
require.NoError(t, err)
plaintext, err := cryptor.Open(jose.KeyOpsDecrypt, fakeNonce, fakeCiphertext, nil, fakeTag)
assert.Nil(t, plaintext)
assert.Equal(t, err, ErrInvalidOperations)
}
func TestAesGcmCryptor_Open(t *testing.T) {
key := &jose.OctSecretKey{}
key.SetAlg(jose.AlgA256GCM)
key.SetOps([]jose.KeyOps{jose.KeyOpsDecrypt})
key.SetKid("something-unique")
key.K.SetBytes(fakeKeyMaterial)
cryptor, err := NewAesGcmCryptorFromJwk(key, []jose.KeyOps{jose.KeyOpsDecrypt})
require.NotNil(t, cryptor)
require.NoError(t, err)
plaintext, err := cryptor.Open(jose.KeyOpsDecrypt, fakeNonce, fakeCiphertext, nil, fakeTag)
assert.Equal(t, plaintext, fakePlaintext)
assert.NoError(t, err)
}
func TestAesGcmCryptor_RoundTrip(t *testing.T) {
key := &jose.OctSecretKey{}
key.SetAlg(jose.AlgA256GCM)
key.SetOps([]jose.KeyOps{jose.KeyOpsEncrypt, jose.KeyOpsDecrypt})
key.SetKid("something-unique")
key.K.SetBytes(fakeKeyMaterial)
cryptor, err := NewAesGcmCryptorFromJwk(key, []jose.KeyOps{jose.KeyOpsDecrypt, jose.KeyOpsEncrypt})
require.NotNil(t, cryptor)
require.NoError(t, err)
for i := 0; i < 50; i++ {
toSeal := make([]byte, 374)
_, err = rand.Read(toSeal)
require.Nil(t, err)
nonce, err := cryptor.GenerateNonce()
require.NoError(t, err)
ciphertext, tag, err := cryptor.Seal(jose.KeyOpsEncrypt, nonce, toSeal, nil)
require.Nil(t, err)
plaintext, err := cryptor.Open(jose.KeyOpsDecrypt, nonce, ciphertext, nil, tag)
assert.NoError(t, err)
assert.Equal(t, plaintext, toSeal)
}
}