package hpke
import (
)
type AEAD interface {
ID() uint16
keySize() int
nonceSize() int
aead(key []byte) (cipher.AEAD, error)
}
func ( uint16) (AEAD, error) {
switch {
case 0x0001:
return AES128GCM(), nil
case 0x0002:
return AES256GCM(), nil
case 0x0003:
return ChaCha20Poly1305(), nil
case 0xFFFF:
return ExportOnly(), nil
default:
return nil, fmt.Errorf("unsupported AEAD %04x", )
}
}
func () AEAD { return aes128GCM }
func () AEAD { return aes256GCM }
func () AEAD { return chacha20poly1305AEAD }
func () AEAD { return exportOnlyAEAD{} }
type aead struct {
nK int
nN int
new func([]byte) (cipher.AEAD, error)
id uint16
}
var aes128GCM = &aead{
nK: 128 / 8,
nN: 96 / 8,
new: newAESGCM,
id: 0x0001,
}
var aes256GCM = &aead{
nK: 256 / 8,
nN: 96 / 8,
new: newAESGCM,
id: 0x0002,
}
var chacha20poly1305AEAD = &aead{
nK: chacha20poly1305.KeySize,
nN: chacha20poly1305.NonceSize,
new: chacha20poly1305.New,
id: 0x0003,
}
func ( *aead) () uint16 {
return .id
}
func ( *aead) ( []byte) (cipher.AEAD, error) {
if len() != .nK {
return nil, errors.New("invalid key size")
}
return .new()
}
func ( *aead) () int {
return .nK
}
func ( *aead) () int {
return .nN
}
type exportOnlyAEAD struct{}
func (exportOnlyAEAD) () uint16 {
return 0xFFFF
}
func (exportOnlyAEAD) ( []byte) (cipher.AEAD, error) {
return nil, nil
}
func (exportOnlyAEAD) () int {
return 0
}
func (exportOnlyAEAD) () int {
return 0
}