// Copyright 2025 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package hpkeimport ()// The AEAD is one of the three components of an HPKE ciphersuite, implementing// symmetric encryption.typeAEADinterface {ID() uint16 keySize() int nonceSize() int aead(key []byte) (cipher.AEAD, error)}// NewAEAD returns the AEAD implementation for the given AEAD ID.//// Applications are encouraged to use specific implementations like [AES128GCM]// or [ChaCha20Poly1305] instead, unless runtime agility is required.func ( uint16) (AEAD, error) {switch {case0x0001: // AES-128-GCMreturnAES128GCM(), nilcase0x0002: // AES-256-GCMreturnAES256GCM(), nilcase0x0003: // ChaCha20Poly1305returnChaCha20Poly1305(), nilcase0xFFFF: // Export-onlyreturnExportOnly(), nildefault:returnnil, fmt.Errorf("unsupported AEAD %04x", ) }}// AES128GCM returns an AES-128-GCM AEAD implementation.func () AEAD { returnaes128GCM }// AES256GCM returns an AES-256-GCM AEAD implementation.func () AEAD { returnaes256GCM }// ChaCha20Poly1305 returns a ChaCha20Poly1305 AEAD implementation.func () AEAD { returnchacha20poly1305AEAD }// ExportOnly returns a placeholder AEAD implementation that cannot encrypt or// decrypt, but only export secrets with [Sender.Export] or [Recipient.Export].//// When this is used, [Sender.Seal] and [Recipient.Open] return errors.func () AEAD { returnexportOnlyAEAD{} }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) {iflen() != .nK {returnnil, errors.New("invalid key size") }return .new()}func ( *aead) () int {return .nK}func ( *aead) () int {return .nN}type exportOnlyAEAD struct{}func (exportOnlyAEAD) () uint16 {return0xFFFF}func (exportOnlyAEAD) ( []byte) (cipher.AEAD, error) {returnnil, nil}func (exportOnlyAEAD) () int {return0}func (exportOnlyAEAD) () int {return0}
The pages are generated with Goldsv0.8.3-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.