ModeAuth

Struct ModeAuth 

Source
pub struct ModeAuth<S> { /* private fields */ }
Expand description

HPKE Auth mode. Encrypt data to a public key with sender authentication.

§Parameters:

  • pubkey_r: Public key of the recipient.
  • pubkey_s: Public key of the sender.
  • info: Optional additional information for HPKE state. Must be identical for sender and recipient.
  • enc: Encapsulated key, generated by the sender.
  • secret_key_r: Secret key of the recipient.
  • secret_key_s: Secret key of the sender.
  • exporter_context: Contextual information used during secret export.
  • secret_ephemeral: Ephemeral private key for deterministic encapsulation.

§Errors:

An error will be returned if:

  • info is longer than 64 bytes
  • out buffer is longer than S::EXPORT_SECRET_MAXLEN when exporting secrets with Self::export_secret()
  • exporter_context is longer than 64 bytes
  • The internal counter reaches u64::MAX and a call to Self::seal()/Self::open() is made
  • Calling Self::seal() when the role is Role::Recipient
  • Calling Self::open() when the role is Role::Sender
  • Calling Self::open() on a set of messages that does not match the order of how they were Self::seal()’ed (re-ordering)
  • If a shared secret is all-zero.
  • If ikm.len() < 32 when calling derive_keypair() on a suite’s KEM.

§Panics:

A panic will occur if:

§Security:

  • When deriving a keypair deterministically instead of generating it randomly, the input ikm must have at least as much entropy as the security level that is desired. For DHKEM_X25519_SHA256_CHACHA20 this means 256 bits.
  • The ikm used as input for derive_keypair() must never be reused.
  • The secret_ephemeral must never be reused.

§Example:

use orion::hazardous::hpke::{ModeAuth, DHKEM_X25519_SHA256_CHACHA20};
use orion::hazardous::kem::x25519_hkdf_sha256::DhKem;

let (sender_secret, sender_public) = DhKem::generate_keypair()?;
let (recipient_secret, recipient_public) = DhKem::generate_keypair()?;

// Streaming-based API
let mut aead_ct_out0 = [0u8; 32];
let mut aead_ct_out1 = [0u8; 32];
let mut aead_ct_out2 = [0u8; 32];

let (mut hpke_sender, enc) = ModeAuth::<DHKEM_X25519_SHA256_CHACHA20>::new_sender(&recipient_public, b"info parameter", &sender_secret)?;
hpke_sender.seal(&[0u8; 16], b"aad parameter 0", &mut aead_ct_out0)?;
hpke_sender.seal(&[1u8; 16], b"aad parameter 1", &mut aead_ct_out1)?;
hpke_sender.seal(&[2u8; 16], b"aad parameter 2", &mut aead_ct_out2)?;

let mut hpke_recipient = ModeAuth::<DHKEM_X25519_SHA256_CHACHA20>::new_recipient(&enc, &recipient_secret, b"info parameter", &sender_public)?;
let mut aead_pt_out0 = [0u8; 16];
let mut aead_pt_out1 = [0u8; 16];
let mut aead_pt_out2 = [0u8; 16];
hpke_recipient.open(&aead_ct_out0, b"aad parameter 0", &mut aead_pt_out0)?;
hpke_recipient.open(&aead_ct_out1, b"aad parameter 1", &mut aead_pt_out1)?;
hpke_recipient.open(&aead_ct_out2, b"aad parameter 2", &mut aead_pt_out2)?;

assert_eq!(&aead_pt_out0, &[0u8; 16]);
assert_eq!(&aead_pt_out1, &[1u8; 16]);
assert_eq!(&aead_pt_out2, &[2u8; 16]);

// One-shot API
let enc = ModeAuth::<DHKEM_X25519_SHA256_CHACHA20>::auth_seal(&recipient_public, b"info parameter", &sender_secret, &[3u8; 16], b"aad", &mut aead_ct_out0)?;
ModeAuth::<DHKEM_X25519_SHA256_CHACHA20>::auth_open(&enc, &recipient_secret, b"info parameter", &sender_public, &aead_ct_out0, b"aad", &mut aead_pt_out0)?;
assert_eq!(&aead_pt_out0, &[3u8; 16]);

Implementations§

Source§

impl<S> ModeAuth<S>

Source

pub const MODE_ID: u8 = 2u8

HPKE Auth mode ID.

Source§

impl<S: Suite + Auth> ModeAuth<S>

Source

pub fn new_sender( pubkey_r: &S::PublicKey, info: &[u8], secret_key_s: &S::PrivateKey, ) -> Result<(Self, S::EncapsulatedKey), UnknownCryptoError>

Available on crate feature safe_api only.

HPKE Auth mode sender.

Source

pub fn new_sender_deterministic( pubkey_r: &S::PublicKey, info: &[u8], secret_key_s: &S::PrivateKey, secret_ephemeral: S::PrivateKey, ) -> Result<(Self, S::EncapsulatedKey), UnknownCryptoError>

HPKE Auth mode sender with a supplied ephemeral private key, which is taken ownership of.

Source

pub fn new_recipient( enc: &S::EncapsulatedKey, secret_key_r: &S::PrivateKey, info: &[u8], pubkey_s: &S::PublicKey, ) -> Result<Self, UnknownCryptoError>

HPKE Auth mode recipient.

Source

pub fn seal( &mut self, plaintext: &[u8], aad: &[u8], out: &mut [u8], ) -> Result<(), UnknownCryptoError>

Context-aware sealing operations.

Source

pub fn open( &mut self, ciphertext: &[u8], aad: &[u8], out: &mut [u8], ) -> Result<(), UnknownCryptoError>

Context-aware opening operations.

Source

pub fn auth_seal( pubkey_r: &S::PublicKey, info: &[u8], secrety_key_s: &S::PrivateKey, plaintext: &[u8], aad: &[u8], out: &mut [u8], ) -> Result<S::EncapsulatedKey, UnknownCryptoError>

Available on crate feature safe_api only.

One-shot API for HPKE Auth mode Self::seal() operation.

Source

pub fn auth_open( enc: &S::EncapsulatedKey, secret_key_r: &S::PrivateKey, info: &[u8], pubkey_s: &S::PublicKey, ciphertext: &[u8], aad: &[u8], out: &mut [u8], ) -> Result<(), UnknownCryptoError>

One-shot API for HPKE Auth mode Self::open() operation.

Source

pub fn export_secret( &self, exporter_context: &[u8], out: &mut [u8], ) -> Result<(), UnknownCryptoError>

Export secret.

Trait Implementations§

Source§

impl<S: Debug> Debug for ModeAuth<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: PartialEq> PartialEq for ModeAuth<S>

Source§

fn eq(&self, other: &ModeAuth<S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<S> StructuralPartialEq for ModeAuth<S>

Auto Trait Implementations§

§

impl<S> Freeze for ModeAuth<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for ModeAuth<S>
where S: RefUnwindSafe,

§

impl<S> Send for ModeAuth<S>
where S: Send,

§

impl<S> Sync for ModeAuth<S>
where S: Sync,

§

impl<S> Unpin for ModeAuth<S>
where S: Unpin,

§

impl<S> UnwindSafe for ModeAuth<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.