|
| 1 | +use crate::FFIMonad; |
| 2 | +use cosmian_crypto_core::{ |
| 3 | + bytes_ser_de::Serializable, |
| 4 | + reexport::rand_core::CryptoRngCore, |
| 5 | + traits::{CyclicGroup, Field, KDF, KEM, NIKE}, |
| 6 | + CryptoCoreError, Sampling, SymmetricKey, |
| 7 | +}; |
| 8 | +use std::{ |
| 9 | + marker::PhantomData, |
| 10 | + ops::{Add, Div, Mul, Neg, Sub}, |
| 11 | +}; |
| 12 | +use zeroize::ZeroizeOnDrop; |
| 13 | + |
| 14 | +#[derive(Debug, Clone, Copy, Default)] |
| 15 | +pub struct MonadicKEM<const KEY_LENGTH: usize, Group: CyclicGroup, Kdf: KDF<KEY_LENGTH>> |
| 16 | +where |
| 17 | + Group::Element: FFIMonad, |
| 18 | + Group::Multiplicity: FFIMonad, |
| 19 | + for<'a> &'a Group::Element: Neg<Output = Group::Element>, |
| 20 | + for<'a, 'b> &'a Group::Element: Add<&'b Group::Element, Output = Group::Element>, |
| 21 | + for<'a, 'b> &'a Group::Element: Sub<&'b Group::Element, Output = Group::Element>, |
| 22 | + for<'a, 'b> &'a Group::Element: Mul<Group::Multiplicity, Output = Group::Element>, |
| 23 | + for<'a, 'b> &'a Group::Element: Mul<&'b Group::Multiplicity, Output = Group::Element>, |
| 24 | + for<'a> &'a Group::Multiplicity: Neg<Output = Group::Multiplicity>, |
| 25 | + for<'a, 'b> &'a Group::Multiplicity: Add<&'b Group::Multiplicity, Output = Group::Multiplicity>, |
| 26 | + for<'a, 'b> &'a Group::Multiplicity: Sub<&'b Group::Multiplicity, Output = Group::Multiplicity>, |
| 27 | + for<'a, 'b> &'a Group::Multiplicity: Mul<&'b Group::Multiplicity, Output = Group::Multiplicity>, |
| 28 | + for<'a, 'b> &'a Group::Multiplicity: Div< |
| 29 | + &'b Group::Multiplicity, |
| 30 | + Output = Result<Group::Multiplicity, <Group::Multiplicity as Field>::InvError>, |
| 31 | + >, |
| 32 | +{ |
| 33 | + data: PhantomData<(Group, Kdf)>, |
| 34 | +} |
| 35 | + |
| 36 | +impl<const KEY_LENGTH: usize, Group: CyclicGroup, Kdf: KDF<KEY_LENGTH>> KEM<KEY_LENGTH> |
| 37 | + for MonadicKEM<KEY_LENGTH, Group, Kdf> |
| 38 | +where |
| 39 | + Group::Element: FFIMonad + Serializable + ZeroizeOnDrop, |
| 40 | + Group::Multiplicity: FFIMonad + Sampling + ZeroizeOnDrop, |
| 41 | + for<'a> &'a Group::Element: Neg<Output = Group::Element>, |
| 42 | + for<'a, 'b> &'a Group::Element: Add<&'b Group::Element, Output = Group::Element>, |
| 43 | + for<'a, 'b> &'a Group::Element: Sub<&'b Group::Element, Output = Group::Element>, |
| 44 | + for<'a, 'b> &'a Group::Element: Mul<Group::Multiplicity, Output = Group::Element>, |
| 45 | + for<'a, 'b> &'a Group::Element: Mul<&'b Group::Multiplicity, Output = Group::Element>, |
| 46 | + for<'a> &'a Group::Multiplicity: Neg<Output = Group::Multiplicity>, |
| 47 | + for<'a, 'b> &'a Group::Multiplicity: Add<&'b Group::Multiplicity, Output = Group::Multiplicity>, |
| 48 | + for<'a, 'b> &'a Group::Multiplicity: Sub<&'b Group::Multiplicity, Output = Group::Multiplicity>, |
| 49 | + for<'a, 'b> &'a Group::Multiplicity: Mul<&'b Group::Multiplicity, Output = Group::Multiplicity>, |
| 50 | + for<'a, 'b> &'a Group::Multiplicity: Div< |
| 51 | + &'b Group::Multiplicity, |
| 52 | + Output = Result<Group::Multiplicity, <Group::Multiplicity as Field>::InvError>, |
| 53 | + >, |
| 54 | +{ |
| 55 | + type Encapsulation = Group::Element; |
| 56 | + |
| 57 | + type EncapsulationKey = Group::Element; |
| 58 | + |
| 59 | + type DecapsulationKey = Group::Multiplicity; |
| 60 | + |
| 61 | + type Error = CryptoCoreError; |
| 62 | + |
| 63 | + fn keygen( |
| 64 | + rng: &mut impl CryptoRngCore, |
| 65 | + ) -> Result<(Self::DecapsulationKey, Self::EncapsulationKey), Self::Error> { |
| 66 | + let (sk, pk) = <Group as NIKE>::keygen(rng)?; |
| 67 | + let sk = sk.manage_error(|e| { |
| 68 | + CryptoCoreError::EllipticCurveError(format!("secret key is in error state: {e}")) |
| 69 | + })?; |
| 70 | + let pk = pk.manage_error(|e| { |
| 71 | + CryptoCoreError::EllipticCurveError(format!("public key is in error state: {e}")) |
| 72 | + })?; |
| 73 | + Ok((sk, pk)) |
| 74 | + } |
| 75 | + |
| 76 | + fn enc( |
| 77 | + ek: &Self::EncapsulationKey, |
| 78 | + rng: &mut impl CryptoRngCore, |
| 79 | + ) -> Result<(SymmetricKey<KEY_LENGTH>, Self::Encapsulation), Self::Error> { |
| 80 | + let (sk, pk) = <Group as NIKE>::keygen(rng)?; |
| 81 | + let ss = (ek * sk).manage_error(|e| { |
| 82 | + CryptoCoreError::EllipticCurveError(format!("shared secret is in error state: {e}")) |
| 83 | + })?; |
| 84 | + let pk = pk.manage_error(|e| { |
| 85 | + CryptoCoreError::EllipticCurveError(format!("public key is in error state: {e}")) |
| 86 | + })?; |
| 87 | + let key = Kdf::derive( |
| 88 | + &ss.serialize() |
| 89 | + .map_err(|e| CryptoCoreError::GenericDeserializationError(e.to_string()))?, |
| 90 | + &[], |
| 91 | + ); |
| 92 | + Ok((key, pk)) |
| 93 | + } |
| 94 | + |
| 95 | + fn dec( |
| 96 | + dk: &Self::DecapsulationKey, |
| 97 | + enc: &Self::Encapsulation, |
| 98 | + ) -> Result<SymmetricKey<KEY_LENGTH>, Self::Error> { |
| 99 | + let ss = <Group as NIKE>::shared_secret(dk, enc)?.manage_error(|e| { |
| 100 | + CryptoCoreError::EllipticCurveError(format!("shared secret is in error state: {e}")) |
| 101 | + })?; |
| 102 | + let key = Kdf::derive( |
| 103 | + &ss.serialize() |
| 104 | + .map_err(|e| CryptoCoreError::GenericDeserializationError(e.to_string()))?, |
| 105 | + &[], |
| 106 | + ); |
| 107 | + Ok(key) |
| 108 | + } |
| 109 | +} |
0 commit comments