github.com/lestrrat-go/jwx/v2@v2.0.21/jwe/internal/content_crypt/content_crypt.go (about)

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