Skip to main content

Pke

Trait Pke 

Source
pub trait Pke {
    type PublicKey: AsRef<[u8]> + Clone;
    type SecretKey: Zeroize + AsRef<[u8]> + Clone;
    type Ciphertext: AsRef<[u8]> + Clone;

    // Required methods
    fn name() -> &'static str;
    fn keypair<R: CryptoRng + RngCore>(
        rng: &mut R,
    ) -> Result<(Self::PublicKey, Self::SecretKey)>;
    fn encrypt<R: RngCore + CryptoRng>(
        pk_recipient: &Self::PublicKey,
        plaintext: &[u8],
        aad: Option<&[u8]>,
        rng: &mut R,
    ) -> Result<Self::Ciphertext>;
    fn decrypt(
        sk_recipient: &Self::SecretKey,
        ciphertext: &Self::Ciphertext,
        aad: Option<&[u8]>,
    ) -> Result<Vec<u8>>;
}
Expand description

Trait for Public Key Encryption schemes.

Required Associated Types§

Source

type PublicKey: AsRef<[u8]> + Clone

Public key type for the PKE scheme. Expected to be a byte representation that can be deserialized.

Source

type SecretKey: Zeroize + AsRef<[u8]> + Clone

Secret key type for the PKE scheme. Expected to be a byte representation that can be deserialized.

Source

type Ciphertext: AsRef<[u8]> + Clone

Ciphertext type produced by the PKE scheme. This is typically a Vec<u8> containing the serialized ciphertext components.

Required Methods§

Source

fn name() -> &'static str

Returns the PKE algorithm name.

Source

fn keypair<R: CryptoRng + RngCore>( rng: &mut R, ) -> Result<(Self::PublicKey, Self::SecretKey)>

Generates a new key pair for the PKE scheme.

Source

fn encrypt<R: RngCore + CryptoRng>( pk_recipient: &Self::PublicKey, plaintext: &[u8], aad: Option<&[u8]>, rng: &mut R, ) -> Result<Self::Ciphertext>

Encrypts a plaintext message using the recipient’s public key.

§Arguments
  • pk_recipient - The recipient’s public key.
  • plaintext - The message to encrypt.
  • aad - Optional Associated Additional Data to be authenticated.
  • rng - A cryptographically secure random number generator.
§Returns

The resulting ciphertext as a Vec<u8>.

Source

fn decrypt( sk_recipient: &Self::SecretKey, ciphertext: &Self::Ciphertext, aad: Option<&[u8]>, ) -> Result<Vec<u8>>

Decrypts a ciphertext using the recipient’s secret key.

§Arguments
  • sk_recipient - The recipient’s secret key.
  • ciphertext - The ciphertext to decrypt.
  • aad - Optional Associated Additional Data that was authenticated.
§Returns

The original plaintext as a Vec<u8> if decryption and authentication (if applicable) succeed.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§