forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_crypt.go
59 lines (49 loc) · 1.58 KB
/
content_crypt.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
package jwe
import (
"github.com/lestrrat-go/jwx/internal/debug"
"github.com/lestrrat-go/jwx/jwa"
"github.com/pkg/errors"
)
func (c GenericContentCrypt) Algorithm() jwa.ContentEncryptionAlgorithm {
return c.alg
}
func (c GenericContentCrypt) Encrypt(cek, plaintext, aad []byte) ([]byte, []byte, []byte, error) {
if debug.Enabled {
debug.Printf("ContentCrypt.Encrypt: cek = %x (%d)", cek, len(cek))
debug.Printf("ContentCrypt.Encrypt: ciphertext = %x (%d)", plaintext, len(plaintext))
debug.Printf("ContentCrypt.Encrypt: aad = %x (%d)", aad, len(aad))
}
iv, encrypted, tag, err := c.cipher.encrypt(cek, plaintext, aad)
if err != nil {
if debug.Enabled {
debug.Printf("cipher.encrypt failed")
}
return nil, nil, nil, errors.Wrap(err, `failed to crypt content`)
}
return iv, encrypted, tag, nil
}
func (c GenericContentCrypt) Decrypt(cek, iv, ciphertext, tag, aad []byte) ([]byte, error) {
return c.cipher.decrypt(cek, iv, ciphertext, tag, aad)
}
func NewAesCrypt(alg jwa.ContentEncryptionAlgorithm) (*GenericContentCrypt, error) {
if debug.Enabled {
debug.Printf("AES Crypt: alg = %s", alg)
}
cipher, err := NewAesContentCipher(alg)
if err != nil {
return nil, errors.Wrap(err, `aes crypt: failed to create content cipher`)
}
if debug.Enabled {
debug.Printf("AES Crypt: cipher.keysize = %d", cipher.KeySize())
}
return &GenericContentCrypt{
alg: alg,
cipher: cipher,
cekgen: NewRandomKeyGenerate(cipher.KeySize() * 2),
keysize: cipher.KeySize() * 2,
tagsize: 16,
}, nil
}
func (c GenericContentCrypt) KeySize() int {
return c.keysize
}