kem.rs (1790B)
1 // Copyright 2023 Cryspen Sarl 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 9 //! This module provides a common interface trait for key 10 //! encapsulation mechanisms (KEMs). 11 12 pub mod arrayref; 13 pub mod owned; 14 pub mod slice; 15 16 #[cfg(feature = "generic-tests")] 17 pub mod tests; 18 19 use rand::CryptoRng; 20 21 /// A KEM keypair. 22 pub type KeyPair<DK, EK> = (DK, EK); 23 24 /// Errors during KEM operations. 25 #[derive(Debug)] 26 pub enum KEMError { 27 /// An error that occurred during key generation. 28 KeyGeneration, 29 /// An error that occurred during encapsulation. 30 Encapsulation, 31 /// An error that occurred during decapsulation. 32 Decapsulation, 33 } 34 35 /// This trait captures the required interface of a key encapsulation 36 /// mechanism (KEM). 37 pub trait KEM { 38 /// The KEM's ciphertext. 39 type Ciphertext; 40 /// The KEM's shared secret. 41 type SharedSecret; 42 /// The KEM's encapsulation key. 43 type EncapsulationKey; 44 /// The KEM's decapsulation key. 45 type DecapsulationKey; 46 47 /// Generate a pair of encapsulation and decapsulation keys. 48 fn generate_key_pair( 49 rng: &mut impl CryptoRng, 50 ) -> Result<KeyPair<Self::DecapsulationKey, Self::EncapsulationKey>, KEMError>; 51 52 /// Encapsulate a shared secret towards a given encapsulation key. 53 fn encapsulate( 54 ek: &Self::EncapsulationKey, 55 rng: &mut impl CryptoRng, 56 ) -> Result<(Self::SharedSecret, Self::Ciphertext), KEMError>; 57 58 /// Decapsulate a shared secret. 59 fn decapsulate( 60 dk: &Self::DecapsulationKey, 61 ctxt: &Self::Ciphertext, 62 ) -> Result<Self::SharedSecret, KEMError>; 63 }