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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::{kdf::Kdf as KdfTrait, util::KemSuiteId, HpkeError};
use generic_array::{typenum::marker_traits::Unsigned, ArrayLength, GenericArray};
#[cfg(feature = "serde_impls")]
use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};
pub(crate) const MAX_PUBKEY_SIZE: usize = 133;
pub trait Serializable {
type OutputSize: ArrayLength<u8>;
fn to_bytes(&self) -> GenericArray<u8, Self::OutputSize>;
fn size() -> usize {
Self::OutputSize::to_usize()
}
}
pub trait Deserializable: Serializable + Sized {
fn from_bytes(encoded: &[u8]) -> Result<Self, HpkeError>;
}
pub trait KeyExchange {
#[cfg(feature = "serde_impls")]
type PublicKey: Clone
+ Serializable
+ Deserializable
+ SerdeSerialize
+ for<'a> SerdeDeserialize<'a>;
#[cfg(not(feature = "serde_impls"))]
type PublicKey: Clone + Serializable + Deserializable;
#[cfg(feature = "serde_impls")]
type PrivateKey: Clone
+ Serializable
+ Deserializable
+ SerdeSerialize
+ for<'a> SerdeDeserialize<'a>;
#[cfg(not(feature = "serde_impls"))]
type PrivateKey: Clone + Serializable + Deserializable;
#[doc(hidden)]
type KexResult: Serializable;
#[doc(hidden)]
fn sk_to_pk(sk: &Self::PrivateKey) -> Self::PublicKey;
#[doc(hidden)]
fn kex(sk: &Self::PrivateKey, pk: &Self::PublicKey) -> Result<Self::KexResult, HpkeError>;
#[doc(hidden)]
fn derive_keypair<Kdf: KdfTrait>(
suite_id: &KemSuiteId,
ikm: &[u8],
) -> (Self::PrivateKey, Self::PublicKey);
}
#[cfg(feature = "p256")]
pub(crate) mod ecdh_nistp;
#[cfg(feature = "p256")]
pub use ecdh_nistp::DhP256;
#[cfg(feature = "x25519-dalek")]
pub(crate) mod x25519;
#[cfg(feature = "x25519-dalek")]
pub use x25519::X25519;