Debian Codegraph @

symmetric.go

raw symbols SCIP
1 /*-
2 * Copyright 2014 Square Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package jose
18
19 import (
20 "crypto/aes"
21 "crypto/cipher"
22 "crypto/hmac"
23 "crypto/rand"
24 "crypto/sha256"
25 "crypto/sha512"
26 "crypto/subtle"
27 "errors"
28 "hash"
29 "io"
30
31 "gopkg.in/square/go-jose.v1/cipher"
32 )
33
34 // Random reader (stubbed out in tests)
35 var randReader = rand.Reader
36
37 // Dummy key cipher for shared symmetric key mode
38 type symmetricKeyCipher struct {
39 key []byte // Pre-shared content-encryption key
40 }
41
42 // Signer/verifier for MAC modes
43 type symmetricMac struct {
44 key []byte
45 }
46
47 // Input/output from an AEAD operation
48 type aeadParts struct {
49 iv, ciphertext, tag []byte
50 }
51
52 // A content cipher based on an AEAD construction
53 type aeadContentCipher struct {
54 keyBytes int
55 authtagBytes int
56 getAead func(key []byte) (cipher.AEAD, error)
57 }
58
59 // Random key generator
60 type randomKeyGenerator struct {
61 size int
62 }
63
64 // Static key generator
65 type staticKeyGenerator struct {
66 key []byte
67 }
68
69 // Create a new content cipher based on AES-GCM
70 func newAESGCM(keySize int) contentCipher {
71 return &aeadContentCipher{
72 keyBytes: keySize,
73 authtagBytes: 16,
74 getAead: func(key []byte) (cipher.AEAD, error) {
75 aes, err := aes.NewCipher(key)
76 if err != nil {
77 return nil, err
78 }
79
80 return cipher.NewGCM(aes)
81 },
82 }
83 }
84
85 // Create a new content cipher based on AES-CBC+HMAC
86 func newAESCBC(keySize int) contentCipher {
87 return &aeadContentCipher{
88 keyBytes: keySize * 2,
89 authtagBytes: 16,
90 getAead: func(key []byte) (cipher.AEAD, error) {
91 return josecipher.NewCBCHMAC(key, aes.NewCipher)
92 },
93 }
94 }
95
96 // Get an AEAD cipher object for the given content encryption algorithm
97 func getContentCipher(alg ContentEncryption) contentCipher {
98 switch alg {
99 case A128GCM:
100 return newAESGCM(16)
101 case A192GCM:
102 return newAESGCM(24)
103 case A256GCM:
104 return newAESGCM(32)
105 case A128CBC_HS256:
106 return newAESCBC(16)
107 case A192CBC_HS384:
108 return newAESCBC(24)
109 case A256CBC_HS512:
110 return newAESCBC(32)
111 default:
112 return nil
113 }
114 }
115
116 // newSymmetricRecipient creates a JWE encrypter based on AES-GCM key wrap.
117 func newSymmetricRecipient(keyAlg KeyAlgorithm, key []byte) (recipientKeyInfo, error) {
118 switch keyAlg {
119 case DIRECT, A128GCMKW, A192GCMKW, A256GCMKW, A128KW, A192KW, A256KW:
120 default:
121 return recipientKeyInfo{}, ErrUnsupportedAlgorithm
122 }
123
124 return recipientKeyInfo{
125 keyAlg: keyAlg,
126 keyEncrypter: &symmetricKeyCipher{
127 key: key,
128 },
129 }, nil
130 }
131
132 // newSymmetricSigner creates a recipientSigInfo based on the given key.
133 func newSymmetricSigner(sigAlg SignatureAlgorithm, key []byte) (recipientSigInfo, error) {
134 // Verify that key management algorithm is supported by this encrypter
135 switch sigAlg {
136 case HS256, HS384, HS512:
137 default:
138 return recipientSigInfo{}, ErrUnsupportedAlgorithm
139 }
140
141 return recipientSigInfo{
142 sigAlg: sigAlg,
143 signer: &symmetricMac{
144 key: key,
145 },
146 }, nil
147 }
148
149 // Generate a random key for the given content cipher
150 func (ctx randomKeyGenerator) genKey() ([]byte, rawHeader, error) {
151 key := make([]byte, ctx.size)
152 _, err := io.ReadFull(randReader, key)
153 if err != nil {
154 return nil, rawHeader{}, err
155 }
156
157 return key, rawHeader{}, nil
158 }
159
160 // Key size for random generator
161 func (ctx randomKeyGenerator) keySize() int {
162 return ctx.size
163 }
164
165 // Generate a static key (for direct mode)
166 func (ctx staticKeyGenerator) genKey() ([]byte, rawHeader, error) {
167 cek := make([]byte, len(ctx.key))
168 copy(cek, ctx.key)
169 return cek, rawHeader{}, nil
170 }
171
172 // Key size for static generator
173 func (ctx staticKeyGenerator) keySize() int {
174 return len(ctx.key)
175 }
176
177 // Get key size for this cipher
178 func (ctx aeadContentCipher) keySize() int {
179 return ctx.keyBytes
180 }
181
182 // Encrypt some data
183 func (ctx aeadContentCipher) encrypt(key, aad, pt []byte) (*aeadParts, error) {
184 // Get a new AEAD instance
185 aead, err := ctx.getAead(key)
186 if err != nil {
187 return nil, err
188 }
189
190 // Initialize a new nonce
191 iv := make([]byte, aead.NonceSize())
192 _, err = io.ReadFull(randReader, iv)
193 if err != nil {
194 return nil, err
195 }
196
197 ciphertextAndTag := aead.Seal(nil, iv, pt, aad)
198 offset := len(ciphertextAndTag) - ctx.authtagBytes
199
200 return &aeadParts{
201 iv: iv,
202 ciphertext: ciphertextAndTag[:offset],
203 tag: ciphertextAndTag[offset:],
204 }, nil
205 }
206
207 // Decrypt some data
208 func (ctx aeadContentCipher) decrypt(key, aad []byte, parts *aeadParts) ([]byte, error) {
209 aead, err := ctx.getAead(key)
210 if err != nil {
211 return nil, err
212 }
213
214 if len(parts.iv) < aead.NonceSize() || len(parts.tag) < ctx.authtagBytes {
215 return nil, ErrCryptoFailure
216 }
217
218 return aead.Open(nil, parts.iv, append(parts.ciphertext, parts.tag...), aad)
219 }
220
221 // Encrypt the content encryption key.
222 func (ctx *symmetricKeyCipher) encryptKey(cek []byte, alg KeyAlgorithm) (recipientInfo, error) {
223 switch alg {
224 case DIRECT:
225 return recipientInfo{
226 header: &rawHeader{},
227 }, nil
228 case A128GCMKW, A192GCMKW, A256GCMKW:
229 aead := newAESGCM(len(ctx.key))
230
231 parts, err := aead.encrypt(ctx.key, []byte{}, cek)
232 if err != nil {
233 return recipientInfo{}, err
234 }
235
236 return recipientInfo{
237 header: &rawHeader{
238 Iv: newBuffer(parts.iv),
239 Tag: newBuffer(parts.tag),
240 },
241 encryptedKey: parts.ciphertext,
242 }, nil
243 case A128KW, A192KW, A256KW:
244 block, err := aes.NewCipher(ctx.key)
245 if err != nil {
246 return recipientInfo{}, err
247 }
248
249 jek, err := josecipher.KeyWrap(block, cek)
250 if err != nil {
251 return recipientInfo{}, err
252 }
253
254 return recipientInfo{
255 encryptedKey: jek,
256 header: &rawHeader{},
257 }, nil
258 }
259
260 return recipientInfo{}, ErrUnsupportedAlgorithm
261 }
262
263 // Decrypt the content encryption key.
264 func (ctx *symmetricKeyCipher) decryptKey(headers rawHeader, recipient *recipientInfo, generator keyGenerator) ([]byte, error) {
265 switch KeyAlgorithm(headers.Alg) {
266 case DIRECT:
267 cek := make([]byte, len(ctx.key))
268 copy(cek, ctx.key)
269 return cek, nil
270 case A128GCMKW, A192GCMKW, A256GCMKW:
271 aead := newAESGCM(len(ctx.key))
272
273 parts := &aeadParts{
274 iv: headers.Iv.bytes(),
275 ciphertext: recipient.encryptedKey,
276 tag: headers.Tag.bytes(),
277 }
278
279 cek, err := aead.decrypt(ctx.key, []byte{}, parts)
280 if err != nil {
281 return nil, err
282 }
283
284 return cek, nil
285 case A128KW, A192KW, A256KW:
286 block, err := aes.NewCipher(ctx.key)
287 if err != nil {
288 return nil, err
289 }
290
291 cek, err := josecipher.KeyUnwrap(block, recipient.encryptedKey)
292 if err != nil {
293 return nil, err
294 }
295 return cek, nil
296 }
297
298 return nil, ErrUnsupportedAlgorithm
299 }
300
301 // Sign the given payload
302 func (ctx symmetricMac) signPayload(payload []byte, alg SignatureAlgorithm) (Signature, error) {
303 mac, err := ctx.hmac(payload, alg)
304 if err != nil {
305 return Signature{}, errors.New("square/go-jose: failed to compute hmac")
306 }
307
308 return Signature{
309 Signature: mac,
310 protected: &rawHeader{},
311 }, nil
312 }
313
314 // Verify the given payload
315 func (ctx symmetricMac) verifyPayload(payload []byte, mac []byte, alg SignatureAlgorithm) error {
316 expected, err := ctx.hmac(payload, alg)
317 if err != nil {
318 return errors.New("square/go-jose: failed to compute hmac")
319 }
320
321 if len(mac) != len(expected) {
322 return errors.New("square/go-jose: invalid hmac")
323 }
324
325 match := subtle.ConstantTimeCompare(mac, expected)
326 if match != 1 {
327 return errors.New("square/go-jose: invalid hmac")
328 }
329
330 return nil
331 }
332
333 // Compute the HMAC based on the given alg value
334 func (ctx symmetricMac) hmac(payload []byte, alg SignatureAlgorithm) ([]byte, error) {
335 var hash func() hash.Hash
336
337 switch alg {
338 case HS256:
339 hash = sha256.New
340 case HS384:
341 hash = sha512.New384
342 case HS512:
343 hash = sha512.New
344 default:
345 return nil, ErrUnsupportedAlgorithm
346 }
347
348 hmac := hmac.New(hash, ctx.key)
349
350 // According to documentation, Write() on hash never fails
351 _, _ = hmac.Write(payload)
352 return hmac.Sum(nil), nil
353 }