1use rand_core::CryptoRngCore;
7
8use crate::Decoy;
9
10pub trait KeyPair {
14 type SecretKey;
16 type PublicKey;
18}
19
20pub(crate) mod sealed {
21 use super::*;
22
23 pub trait RandomSecretKey {
24 fn random_secret_key(csprng: &mut impl CryptoRngCore) -> Self;
25 }
26}
27
28#[cfg(feature = "dalek")]
29mod x25519_dalek_aliases {
30 use super::*;
31
32 pub(crate) mod dalek {
33 pub(crate) type PublicKey = x25519_dalek::PublicKey;
34 pub(crate) type StaticSecret = x25519_dalek::StaticSecret;
35 }
36
37 #[derive(Debug, Clone)]
39 pub struct X25519 {}
40
41 impl KeyPair for X25519 {
42 type SecretKey = dalek::StaticSecret;
43 type PublicKey = dalek::PublicKey;
44 }
45
46 impl Decoy for dalek::PublicKey {
47 fn random_decoy(csprng: &mut impl CryptoRngCore) -> Self {
48 let mut bytes = [0u8; 32];
49 csprng.fill_bytes(&mut bytes);
50
51 dalek::PublicKey::from(bytes)
52 }
53 }
54
55 impl sealed::RandomSecretKey for dalek::StaticSecret {
56 fn random_secret_key(csprng: &mut impl CryptoRngCore) -> Self {
57 dalek::StaticSecret::random_from_rng(csprng)
58 }
59 }
60}
61
62#[cfg(feature = "dalek")]
63pub use x25519_dalek_aliases::*;
64
65#[cfg(feature = "rustcrypto-ec")]
66mod elliptic_curve_aliases {
67 use super::*;
68
69 use elliptic_curve::{point::NonIdentity, CurveArithmetic, ProjectivePoint};
70
71 pub(crate) mod rcec {
72 pub(crate) type PublicKey<C> = elliptic_curve::PublicKey<C>;
73 pub(crate) type SecretKey<C> = elliptic_curve::SecretKey<C>;
74 }
75
76 #[derive(Debug, Clone)]
78 pub struct EllipticCurve<C> {
79 marker: std::marker::PhantomData<C>,
80 }
81
82 impl<C: CurveArithmetic> KeyPair for EllipticCurve<C> {
83 type SecretKey = rcec::SecretKey<C>;
84 type PublicKey = rcec::PublicKey<C>;
85 }
86
87 impl<C: CurveArithmetic> Decoy for rcec::PublicKey<C> {
88 fn random_decoy(csprng: &mut impl CryptoRngCore) -> Self {
89 rcec::PublicKey::<C>::from(NonIdentity::<ProjectivePoint<C>>::random(&mut *csprng))
90 }
91 }
92
93 impl<C: CurveArithmetic> sealed::RandomSecretKey for rcec::SecretKey<C> {
94 fn random_secret_key(csprng: &mut impl CryptoRngCore) -> Self {
95 rcec::SecretKey::<C>::random(csprng)
96 }
97 }
98}
99
100#[cfg(feature = "rustcrypto-ec")]
101pub use elliptic_curve_aliases::*;