2022-06-09 17:35:23 +00:00
|
|
|
package content_crypt //nolint:golint
|
|
|
|
|
|
|
|
import (
|
2022-06-09 18:53:45 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/lestrrat-go/jwx/v2/jwa"
|
|
|
|
"github.com/lestrrat-go/jwx/v2/jwe/internal/cipher"
|
2022-06-09 17:35:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c Generic) Algorithm() jwa.ContentEncryptionAlgorithm {
|
|
|
|
return c.alg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Generic) Encrypt(cek, plaintext, aad []byte) ([]byte, []byte, []byte, error) {
|
|
|
|
iv, encrypted, tag, err := c.cipher.Encrypt(cek, plaintext, aad)
|
|
|
|
if err != nil {
|
2022-06-09 18:53:45 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf(`failed to crypt content: %w`, err)
|
2022-06-09 17:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return iv, encrypted, tag, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Generic) Decrypt(cek, iv, ciphertext, tag, aad []byte) ([]byte, error) {
|
|
|
|
return c.cipher.Decrypt(cek, iv, ciphertext, tag, aad)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGeneric(alg jwa.ContentEncryptionAlgorithm) (*Generic, error) {
|
|
|
|
c, err := cipher.NewAES(alg)
|
|
|
|
if err != nil {
|
2022-06-09 18:53:45 +00:00
|
|
|
return nil, fmt.Errorf(`aes crypt: failed to create content cipher: %w`, err)
|
2022-06-09 17:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Generic{
|
|
|
|
alg: alg,
|
|
|
|
cipher: c,
|
|
|
|
keysize: c.KeySize(),
|
|
|
|
tagsize: 16,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Generic) KeySize() int {
|
|
|
|
return c.keysize
|
|
|
|
}
|