...
1 package content_crypt
2
3 import (
4 "github.com/lestrrat-go/jwx/jwa"
5 "github.com/lestrrat-go/jwx/jwe/internal/cipher"
6 "github.com/pkg/errors"
7 )
8
9 func (c Generic) Algorithm() jwa.ContentEncryptionAlgorithm {
10 return c.alg
11 }
12
13 func (c Generic) Encrypt(cek, plaintext, aad []byte) ([]byte, []byte, []byte, error) {
14 iv, encrypted, tag, err := c.cipher.Encrypt(cek, plaintext, aad)
15 if err != nil {
16 return nil, nil, nil, errors.Wrap(err, `failed to crypt content`)
17 }
18
19 return iv, encrypted, tag, nil
20 }
21
22 func (c Generic) Decrypt(cek, iv, ciphertext, tag, aad []byte) ([]byte, error) {
23 return c.cipher.Decrypt(cek, iv, ciphertext, tag, aad)
24 }
25
26 func NewGeneric(alg jwa.ContentEncryptionAlgorithm) (*Generic, error) {
27 c, err := cipher.NewAES(alg)
28 if err != nil {
29 return nil, errors.Wrap(err, `aes crypt: failed to create content cipher`)
30 }
31
32 return &Generic{
33 alg: alg,
34 cipher: c,
35 keysize: c.KeySize(),
36 tagsize: 16,
37 }, nil
38 }
39
40 func (c Generic) KeySize() int {
41 return c.keysize
42 }
43
View as plain text