Skip to content

kem: EK/SS serialization #2219

@tarcieri

Description

@tarcieri

The one remaining gap in the kem API is a way to obtain the bytes serialization of EK and SS (encapsulated key and shared secret respectively), and for EK, a way to deserialize it from bytes before passing it to Decapsulate::decapsulate.

I was first wondering if there's actually a use case for overlapping impls with these traits and if not, perhaps these should be associated types instead? That would make it easier to give them bounds. Something like:

pub trait Encapsulate: TryKeyInit + KeyExport {
    /// Shared secret after being encrypted by this encapsulator
    type EncapsulatedKey: TryKeyInit + KeyExport;

    /// Shared secret value which results after the `EncapsulatedKey` has been decrypted.
    type SharedSecret: AsRef<[u8]>;

    /// Encapsulates a fresh shared secret
    fn encapsulate_with_rng<R>(&self, rng: &mut R) -> Result<(EK, SS), R::Error>
    where
        R: TryCryptoRng + ?Sized;

    /// Encapsulate a fresh shared secret generated using the system's secure RNG.
    #[cfg(feature = "getrandom")]
    fn encapsulate(&self) -> (EK, SS) {
        let Ok(ret) = self.encapsulate_with_rng(&mut SysRng.unwrap_err());
        ret
    }
}

pub trait Decapsulate {
    /// Encapsulator which corresponds to this decapsulator.
    type Encapsulator: Encapsulate<
        EncapsulatedKey = Self::EncapsulatedKey,
        SharedSecret = Self::SharedSecret
    >;

    /// Shared secret after being encrypted by this encapsulator
    type EncapsulatedKey: TryKeyInit + KeyExport;

    /// Shared secret value which results after the `EncapsulatedKey` has been decrypted.
    type SharedSecret: AsRef<[u8]>;

    /// Decapsulates the given encapsulated key
    fn decapsulate(&self, encapsulated_key: &EK) -> SS;

    /// Retrieve the encapsulator associated with this decapsulator.
    fn encapsulator(&self) -> Self::Encapsulator;
}

We could also consider a third trait that removes the duplicated associated types, perhaps (possibly Kem?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions