-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaes_gcm_cryptor.go
137 lines (125 loc) · 4.5 KB
/
aes_gcm_cryptor.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
// 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/cipher"
"crypto/rand"
"io"
"github.com/ThalesGroup/gose/jose"
)
var validEncryptionOpts = []jose.KeyOps{jose.KeyOpsEncrypt}
var validDecryptionOpts = []jose.KeyOps{jose.KeyOpsDecrypt}
var validCryptorOpts = []jose.KeyOps{jose.KeyOpsEncrypt, jose.KeyOpsDecrypt}
// AesGcmCryptor provides AES GCM encryption and decryption functions.
type AesGcmCryptor struct {
kid string
alg jose.Alg
aead cipher.AEAD
opts []jose.KeyOps
rng io.Reader
}
// Kid the key identity
func (cryptor *AesGcmCryptor) Kid() string {
return cryptor.kid
}
// Algorithm the supported algorithm
func (cryptor *AesGcmCryptor) Algorithm() jose.Alg {
return cryptor.alg
}
// GenerateNonce generate a nonce of the correct size for use with GCM encryption/decryption from a random source.
func (cryptor *AesGcmCryptor) GenerateNonce() ([]byte, error) {
nonce := make([]byte, cryptor.aead.NonceSize())
if _, err := cryptor.rng.Read(nonce); err != nil {
return nil, err
}
return nonce, nil
}
// Open decrypt a previously encrypted ciphertext.
func (cryptor *AesGcmCryptor) Open(operation jose.KeyOps, nonce, ciphertext, aad, tag []byte) (plaintext []byte, err error) {
ops := intersection(validDecryptionOpts, cryptor.opts)
if !isSubset(ops, []jose.KeyOps{operation}) {
err = ErrInvalidOperations
return
}
dst := make([]byte, 0, len(ciphertext))
ciphertextAndTag := make([]byte, len(ciphertext)+len(tag))
_ = copy(ciphertextAndTag, ciphertext)
_ = copy(ciphertextAndTag[len(ciphertext):], tag)
if dst, err = cryptor.aead.Open(dst, nonce, ciphertextAndTag, aad); err != nil {
return
}
plaintext = dst
return
}
// Seal encrypt a supplied plaintext and AAD.
func (cryptor *AesGcmCryptor) Seal(operation jose.KeyOps, nonce, plaintext, aad []byte) (ciphertext, tag []byte, err error) {
ops := intersection(validEncryptionOpts, cryptor.opts)
if !isSubset(ops, []jose.KeyOps{operation}) {
err = ErrInvalidOperations
return
}
// If a nil nonce provided, this is interpreted as the encryptor providing the nonce
if nil != nonce && len(nonce) != cryptor.aead.NonceSize() {
err = ErrInvalidNonce
return
}
sz := cryptor.aead.Overhead() + len(plaintext)
// If a nil nonce provided, allocate extra capacity for the nonce to be returned
if nil == nonce {
sz += cryptor.aead.NonceSize()
}
dst := make([]byte, 0, sz)
dst = cryptor.aead.Seal(dst, nonce, plaintext, aad)
ciphertext = dst[:len(plaintext)]
// If the HSM is supplying the IV appended to the end, we return this contained in the tag and extract later
tag = dst[len(plaintext):]
return
}
// NewAesGcmCryptorFromJwk create a new instance of an AesGCmCryptor from a JWK.
func NewAesGcmCryptorFromJwk(jwk jose.Jwk, required []jose.KeyOps) (AeadEncryptionKey, error) {
/* Check jwk can be used to encrypt or decrypt */
ops := intersection(validCryptorOpts, jwk.Ops())
if len(ops) == 0 {
return nil, ErrInvalidOperations
}
/* Load the jwk */
aead, err := LoadSymmetricAEAD(jwk, required)
if err != nil {
return nil, err
}
return &AesGcmCryptor{
kid: jwk.Kid(),
alg: jwk.Alg(),
aead: aead,
rng: rand.Reader,
opts: jwk.Ops(),
}, nil
}
// NewAesGcmCryptor create a new instance of an AesGCmCryptor from the supplied parameters.
func NewAesGcmCryptor(aead cipher.AEAD, rng io.Reader, kid string, alg jose.Alg, operations []jose.KeyOps) (AeadEncryptionKey, error) {
return &AesGcmCryptor{
kid: kid,
alg: alg,
aead: aead,
rng: rng,
opts: operations,
}, nil
}