1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use crate::HpkeError;
use digest::generic_array::{typenum::marker_traits::Unsigned, ArrayLength, GenericArray};
use rand::{CryptoRng, RngCore};
pub trait Marshallable {
type OutputSize: ArrayLength<u8>;
fn marshal(&self) -> GenericArray<u8, Self::OutputSize>;
fn size() -> usize {
Self::OutputSize::to_usize()
}
}
pub trait Unmarshallable: Marshallable + Sized {
fn unmarshal(encoded: &[u8]) -> Result<Self, HpkeError>;
}
pub trait KeyExchange {
type PublicKey: Clone + Marshallable + Unmarshallable;
type PrivateKey: Clone + Marshallable + Unmarshallable;
type KexResult: Marshallable;
fn gen_keypair<R: CryptoRng + RngCore>(csprng: &mut R) -> (Self::PrivateKey, Self::PublicKey);
fn sk_to_pk(sk: &Self::PrivateKey) -> Self::PublicKey;
fn kex(sk: &Self::PrivateKey, pk: &Self::PublicKey) -> Result<Self::KexResult, HpkeError>;
}
#[cfg(feature = "p256")]
mod ecdh_nistp;
#[cfg(feature = "p256")]
pub use ecdh_nistp::DhP256;
#[cfg(feature = "x25519-dalek")]
mod x25519;
#[cfg(feature = "x25519-dalek")]
pub use x25519::X25519;