Skip to main content

Aead

Trait Aead 

Source
pub trait Aead: Sized + Aead {
    const HPKE_AEAD_ID: u16;
    const MAX_PLAINTEXT_SIZE: u64;
    const MAX_CIPHERTEXT_SIZE: u64;

    // Required method
    fn new(key: &[u8]) -> Result<Self, HpkeError>;

    // Provided methods
    fn seal(
        &self,
        in_out: &mut [u8],
        nonce: &[u8],
        associated_data: &[u8],
    ) -> Result<Hash, HpkeError> { ... }
    fn open(
        &self,
        in_out: &mut [u8],
        nonce: &[u8],
        associated_data: &[u8],
        tag: &[u8],
    ) -> Result<(), HpkeError> { ... }
}
Expand description

HPKE ciphersuite binding for an AEAD algorithm.

An AEAD is the symmetric cipher used to seal and open messages. This trait only adds what HPKE itself needs on top of [crypto::Aead] (from which the nonce size, tag size, and the underlying cipher operations are inherited):

§Example

use hpke::aead::{Aead, Aes256Gcm, ExportOnly};

let cipher = <Aes256Gcm as Aead>::new(&[0x42u8; 32]).unwrap();
assert_eq!(Aes256Gcm::HPKE_AEAD_ID, 0x0002);
assert_eq!(ExportOnly::HPKE_AEAD_ID, 0xffff);

§Custom AEADs

Implement [crypto::Aead] for the cipher’s operations, then implement this trait to give it an HPKE identifier, key size, and message length limits. Any nonce and tag size is supported as long as KEY_SIZE is at most 64 bytes and NONCE_SIZE is between 8 and 64 bytes; larger values fail to compile when the cipher is used with new_sender or new_recipient.

Required Associated Constants§

Source

const HPKE_AEAD_ID: u16

The HPKE AEAD identifier (draft-ietf-hpke-hpke-05 Section 7.3, IANA “HPKE AEAD Identifiers”), used to build the ciphersuite suite_id.

Source

const MAX_PLAINTEXT_SIZE: u64

Maximum length, in bytes, of a plaintext passed to Aead::seal, i.e. P_MAX ([RFC 5116]).

HPKE refuses to encrypt longer plaintexts with HpkeError::MessageLimitReached: exceeding P_MAX would void the confidentiality and integrity guarantees of the AEAD (draft-ietf-hpke-hpke-05 Section 5.2). There is no default; every AEAD must state its limit.

Source

const MAX_CIPHERTEXT_SIZE: u64

Maximum length, in bytes, of a ciphertext passed to Aead::open, i.e. C_MAX ([RFC 5116]).

HPKE refuses to open longer ciphertexts with HpkeError::MessageLimitReached (draft-ietf-hpke-hpke-05 Section 5.2). There is no default; every AEAD must state its limit.

Required Methods§

Source

fn new(key: &[u8]) -> Result<Self, HpkeError>

Instantiates the cipher from KEY_SIZE raw key bytes, as produced by the HPKE key schedule.

§Errors

Returns HpkeError::InvalidKey when key does not have exactly [crypto::Aead::KEY_SIZE] bytes.

Provided Methods§

Source

fn seal( &self, in_out: &mut [u8], nonce: &[u8], associated_data: &[u8], ) -> Result<Hash, HpkeError>

Encrypts in_out in place and returns the detached authentication tag, forwarding to [crypto::Aead::encrypt_in_place].

§Errors

Returns HpkeError::NotSupported when the cipher is the ExportOnly pseudo-AEAD.

Source

fn open( &self, in_out: &mut [u8], nonce: &[u8], associated_data: &[u8], tag: &[u8], ) -> Result<(), HpkeError>

Decrypts in_out in place using the detached authentication tag, forwarding to [crypto::Aead::decrypt_in_place].

§Errors

Returns HpkeError::OpenError when authentication fails, or HpkeError::NotSupported when the cipher is the ExportOnly pseudo-AEAD.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Aead for Aes256Gcm

AES-256-GCM (AEAD id 0x0002).

Source§

const HPKE_AEAD_ID: u16 = 0x0002

Source§

const MAX_PLAINTEXT_SIZE: u64 = AES_GCM_MAX_PLAINTEXT_SIZE

Source§

const MAX_CIPHERTEXT_SIZE: u64 = AES_GCM_MAX_CIPHERTEXT_SIZE

Source§

impl Aead for ChaCha20Poly1305

ChaCha20-Poly1305 (AEAD id 0x0003).

Source§

const HPKE_AEAD_ID: u16 = 0x0003

Source§

const MAX_PLAINTEXT_SIZE: u64 = CHACHA20_POLY1305_MAX_PLAINTEXT_SIZE

Source§

const MAX_CIPHERTEXT_SIZE: u64 = CHACHA20_POLY1305_MAX_CIPHERTEXT_SIZE

Source§

impl Aead for ExportOnly