cryptimitives/key/
mod.rs

1#[cfg(feature = "std")]
2use std::fmt::Debug;
3
4#[cfg(not(feature = "std"))]
5use alloc::fmt::Debug;
6
7use cryptraits::{
8    convert::{FromBytes, Len, ToVec},
9    key::{Generate, KeyPair as KeypairTrait, SecretKey},
10    key_exchange::DiffieHellman,
11    signature::{Sign, Verify},
12};
13use rand_core::{CryptoRng, OsRng, RngCore};
14
15use zeroize::Zeroize;
16
17use crate::errors::{KeyPairError, SignatureError};
18
19pub mod ed25519;
20pub mod util;
21pub mod x25519_ristretto;
22
23#[derive(Clone, PartialEq)]
24pub struct KeyPair<SK>
25where
26    SK: SecretKey,
27{
28    secret: SK,
29    public: SK::PK,
30}
31
32impl<SK> Len for KeyPair<SK>
33where
34    SK: SecretKey + Len,
35    SK::PK: Len,
36{
37    const LEN: usize = SK::LEN + <<SK as SecretKey>::PK as Len>::LEN;
38}
39
40impl<SK> KeypairTrait for KeyPair<SK>
41where
42    SK: SecretKey,
43{
44    type SK = SK;
45
46    fn public(&self) -> &<Self::SK as SecretKey>::PK {
47        &self.public
48    }
49
50    fn to_public(&self) -> SK::PK {
51        self.public
52    }
53
54    fn secret(&self) -> &Self::SK {
55        &self.secret
56    }
57}
58
59impl<SK> Generate for KeyPair<SK>
60where
61    SK: SecretKey + Generate,
62{
63    fn generate_with<R>(csprng: R) -> Self
64    where
65        R: CryptoRng + RngCore,
66    {
67        let secret: SK = SK::generate_with(csprng);
68        let public = secret.to_public();
69
70        KeyPair { secret, public }
71    }
72
73    fn generate() -> Self {
74        Self::generate_with(&mut OsRng)
75    }
76}
77
78impl<SK> Zeroize for KeyPair<SK>
79where
80    SK: SecretKey,
81{
82    fn zeroize(&mut self) {
83        self.secret.zeroize();
84    }
85}
86
87impl<SK> Drop for KeyPair<SK>
88where
89    SK: SecretKey,
90{
91    fn drop(&mut self) {
92        self.zeroize();
93    }
94}
95
96impl<SK> FromBytes for KeyPair<SK>
97where
98    SK: SecretKey + FromBytes + Len,
99    SK::PK: FromBytes + Len,
100    KeyPairError: From<<SK as FromBytes>::E> + From<<<SK as SecretKey>::PK as FromBytes>::E>,
101{
102    type E = KeyPairError;
103    fn from_bytes(bytes: &[u8]) -> Result<Self, Self::E> {
104        if bytes.len() != Self::LEN {
105            return Err(KeyPairError::BytesLengthError);
106        }
107
108        let secret = SK::from_bytes(&bytes[..SK::LEN])?;
109        let public = SK::PK::from_bytes(&bytes[SK::LEN..])?;
110
111        Ok(KeyPair { secret, public })
112    }
113}
114
115impl<SK> ToVec for KeyPair<SK>
116where
117    SK: SecretKey + ToVec + Len,
118    SK::PK: ToVec + Len,
119{
120    fn to_vec(&self) -> Vec<u8> {
121        let mut bytes: Vec<u8> = Vec::new();
122
123        bytes.extend(self.secret.to_vec());
124        bytes.extend(self.public.to_vec());
125
126        bytes
127    }
128}
129
130impl<SK> Sign for KeyPair<SK>
131where
132    SK: SecretKey + Sign,
133{
134    type SIG = <SK as Sign>::SIG;
135
136    fn sign(&self, data: &[u8]) -> Self::SIG
137    where
138        Self: Sized,
139    {
140        self.secret.sign(data)
141    }
142}
143
144impl<SK> DiffieHellman for KeyPair<SK>
145where
146    SK: SecretKey + Sign + DiffieHellman,
147{
148    type SSK = <SK as DiffieHellman>::SSK;
149    type PK = <SK as DiffieHellman>::PK;
150
151    fn diffie_hellman(&self, peer_public: &Self::PK) -> <SK as DiffieHellman>::SSK {
152        self.secret.diffie_hellman(peer_public)
153    }
154}
155
156impl<SK> Verify for KeyPair<SK>
157where
158    SK: SecretKey,
159    SK::PK: Verify<E = SignatureError>,
160{
161    type E = SignatureError;
162    type SIG = <SK::PK as Verify>::SIG;
163
164    fn verify(&self, data: &[u8], signature: &Self::SIG) -> Result<(), Self::E> {
165        self.public.verify(data, signature)
166    }
167}
168
169impl<SK> Debug for KeyPair<SK>
170where
171    SK: SecretKey,
172{
173    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
174        f.debug_struct("KeyPair")
175            .field("secret", &String::from("<erased>"))
176            .field("public", &self.public)
177            .finish()
178    }
179}
180
181impl<SK> Default for KeyPair<SK>
182where
183    SK: SecretKey + Generate,
184{
185    fn default() -> Self {
186        let secret = SK::generate_with(OsRng);
187        let public = secret.to_public();
188
189        Self { secret, public }
190    }
191}
192
193impl<SK> From<SK> for KeyPair<SK>
194where
195    SK: SecretKey,
196{
197    fn from(secret: SK) -> Self {
198        let public = secret.to_public();
199
200        Self { secret, public }
201    }
202}