1pub mod ed25519;
7pub mod keymanip;
8pub mod rsa;
9
10pub mod curve25519 {
16 use derive_deftly::Deftly;
17 use educe::Educe;
18 use subtle::ConstantTimeEq;
19
20 use crate::util::ct::derive_deftly_template_PartialEqFromCtEq;
21
22 #[allow(clippy::exhaustive_structs)]
24 #[derive(Clone, Educe)]
25 #[educe(Debug)]
26 pub struct StaticKeypair {
27 #[educe(Debug(ignore))]
29 pub secret: StaticSecret,
30 pub public: PublicKey,
32 }
33
34 pub struct EphemeralSecret(x25519_dalek::EphemeralSecret);
39
40 #[derive(Clone)]
54 pub struct StaticSecret(x25519_dalek::StaticSecret);
55
56 impl ConstantTimeEq for StaticSecret {
57 fn ct_eq(&self, other: &Self) -> subtle::Choice {
58 let Self { 0: self_secret } = self;
59 let Self { 0: other_secret } = other;
60
61 self_secret.as_bytes().ct_eq(other_secret.as_bytes())
62 }
63 }
64
65 #[derive(Clone, Copy, Debug, Eq, Hash, Deftly)]
69 #[allow(clippy::derived_hash_with_manual_eq)]
71 #[derive_deftly(PartialEqFromCtEq)]
72 pub struct PublicKey(x25519_dalek::PublicKey);
73
74 impl ConstantTimeEq for PublicKey {
75 fn ct_eq(&self, other: &Self) -> subtle::Choice {
76 let Self { 0: self_secret } = self;
77 let Self { 0: other_secret } = other;
78
79 self_secret.as_bytes().ct_eq(other_secret.as_bytes())
80 }
81 }
82
83 pub struct SharedSecret(x25519_dalek::SharedSecret);
87
88 impl<'a> From<&'a EphemeralSecret> for PublicKey {
89 fn from(secret: &'a EphemeralSecret) -> Self {
90 Self((&secret.0).into())
91 }
92 }
93
94 impl<'a> From<&'a StaticSecret> for PublicKey {
95 fn from(secret: &'a StaticSecret) -> Self {
96 Self((&secret.0).into())
97 }
98 }
99
100 impl From<[u8; 32]> for StaticSecret {
101 fn from(value: [u8; 32]) -> Self {
102 Self(value.into())
103 }
104 }
105 impl From<[u8; 32]> for PublicKey {
106 fn from(value: [u8; 32]) -> Self {
107 Self(value.into())
108 }
109 }
110
111 impl EphemeralSecret {
112 pub fn random_from_rng<R: ?Sized + rand_core::Rng + rand_core::CryptoRng>(
114 csprng: &mut R,
115 ) -> Self {
116 Self(x25519_dalek::EphemeralSecret::random_from_rng(csprng))
117 }
118 pub fn diffie_hellman(self, their_public: &PublicKey) -> SharedSecret {
120 SharedSecret(self.0.diffie_hellman(&their_public.0))
121 }
122 }
123 impl StaticSecret {
124 pub fn random_from_rng<R: ?Sized + rand_core::Rng + rand_core::CryptoRng>(
126 csprng: &mut R,
127 ) -> Self {
128 Self(x25519_dalek::StaticSecret::random_from_rng(csprng))
129 }
130 pub fn diffie_hellman(&self, their_public: &PublicKey) -> SharedSecret {
132 SharedSecret(self.0.diffie_hellman(&their_public.0))
133 }
134 pub fn to_bytes(&self) -> [u8; 32] {
136 self.0.to_bytes()
137 }
138 pub fn as_bytes(&self) -> &[u8; 32] {
140 self.0.as_bytes()
141 }
142 }
143 impl SharedSecret {
144 pub fn as_bytes(&self) -> &[u8; 32] {
146 self.0.as_bytes()
147 }
148 pub fn was_contributory(&self) -> bool {
152 self.0.was_contributory()
153 }
154 }
155 impl PublicKey {
156 pub fn as_bytes(&self) -> &[u8; 32] {
158 self.0.as_bytes()
159 }
160 pub fn to_bytes(&self) -> [u8; 32] {
162 self.0.to_bytes()
163 }
164 }
165}
166
167pub trait ValidatableSignature {
178 fn is_valid(&self) -> bool;
180
181 fn as_ed25519(&self) -> Option<&ed25519::ValidatableEd25519Signature> {
183 None
184 }
185}
186
187pub fn validate_all_sigs(v: &[Box<dyn ValidatableSignature>]) -> bool {
198 let mut ed_sigs = Vec::new();
201 let mut non_ed_sigs = Vec::new();
202 for sig in v.iter() {
203 match sig.as_ed25519() {
204 Some(ed_sig) => ed_sigs.push(ed_sig),
205 None => non_ed_sigs.push(sig),
206 }
207 }
208
209 let ed_batch_is_valid = crate::pk::ed25519::validate_batch(&ed_sigs[..]);
211
212 ed_batch_is_valid && non_ed_sigs.iter().all(|b| b.is_valid())
214}
215
216#[cfg(test)]
217mod test {
218 #![allow(clippy::bool_assert_comparison)]
220 #![allow(clippy::clone_on_copy)]
221 #![allow(clippy::dbg_macro)]
222 #![allow(clippy::mixed_attributes_style)]
223 #![allow(clippy::print_stderr)]
224 #![allow(clippy::print_stdout)]
225 #![allow(clippy::single_char_pattern)]
226 #![allow(clippy::unwrap_used)]
227 #![allow(clippy::unchecked_time_subtraction)]
228 #![allow(clippy::useless_vec)]
229 #![allow(clippy::needless_pass_by_value)]
230 #![allow(clippy::string_slice)] #[test]
233 fn validatable_ed_sig() {
234 use super::ValidatableSignature;
235 use super::ed25519::{PublicKey, Signature, ValidatableEd25519Signature};
236 use hex_literal::hex;
237 let pk = PublicKey::from_bytes(&hex!(
238 "fc51cd8e6218a1a38da47ed00230f058
239 0816ed13ba3303ac5deb911548908025"
240 ))
241 .unwrap();
242 let sig: Signature = hex!(
243 "6291d657deec24024827e69c3abe01a3
244 0ce548a284743a445e3680d7db5ac3ac
245 18ff9b538d16f290ae67f760984dc659
246 4a7c15e9716ed28dc027beceea1ec40a"
247 )
248 .into();
249
250 let valid = ValidatableEd25519Signature::new(pk, sig, &hex!("af82"));
251 let invalid = ValidatableEd25519Signature::new(pk, sig, &hex!("af83"));
252
253 assert!(valid.is_valid());
254 assert!(!invalid.is_valid());
255 }
256}