ecdh_omr/
curves.rs

1// SPDX-FileCopyrightText: 2024 eaon <eaon@posteo.net>
2// SPDX-License-Identifier: EUPL-1.2
3
4//! Shorthands for compatible elliptic curve implementations.
5
6use rand_core::CryptoRngCore;
7
8use crate::Decoy;
9
10/// An elliptic curve key pair.
11///
12/// Creates a formal relationship between public and secret types.
13pub trait KeyPair {
14    /// An ECDH secret key.
15    type SecretKey;
16    /// An ECDH public key.
17    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    /// Alias-ish type for X25519-dalek key pairs.
38    #[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    /// Alias-ish type for key pairs of RustCrypto's curve-agnostic ECDH implementation.
77    #[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::*;