1#[cfg(feature = "alloc")]
4use crate::buffer::SecretBytes;
5use crate::{
6 buffer::WriteBuffer,
7 error::Error,
8 generic_array::{typenum::Unsigned, ArrayLength},
9 random::KeyMaterial,
10};
11
12pub trait KeyGen: Sized {
14 fn generate(rng: impl KeyMaterial) -> Result<Self, Error>;
16
17 #[cfg(feature = "getrandom")]
19 fn random() -> Result<Self, Error> {
20 Self::generate(crate::random::default_rng())
21 }
22}
23
24pub trait KeySecretBytes: KeyMeta {
26 fn from_secret_bytes(key: &[u8]) -> Result<Self, Error>
28 where
29 Self: Sized;
30
31 fn with_secret_bytes<O>(&self, f: impl FnOnce(Option<&[u8]>) -> O) -> O;
33}
34
35pub trait ToSecretBytes {
37 fn secret_bytes_length(&self) -> Result<usize, Error>;
39
40 fn write_secret_bytes(&self, out: &mut dyn WriteBuffer) -> Result<(), Error>;
42
43 #[cfg(feature = "alloc")]
44 #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
45 fn to_secret_bytes(&self) -> Result<SecretBytes, Error> {
47 let mut buf = SecretBytes::with_capacity(128);
48 self.write_secret_bytes(&mut buf)?;
49 Ok(buf)
50 }
51}
52
53impl<K> ToSecretBytes for K
54where
55 K: KeySecretBytes,
56{
57 fn secret_bytes_length(&self) -> Result<usize, Error> {
58 Ok(<Self as KeyMeta>::KeySize::USIZE)
59 }
60
61 fn write_secret_bytes(&self, out: &mut dyn WriteBuffer) -> Result<(), Error> {
62 self.with_secret_bytes(|buf| {
63 if let Some(buf) = buf {
64 out.buffer_write(buf)
65 } else {
66 Err(err_msg!(MissingSecretKey))
67 }
68 })
69 }
70}
71
72pub trait KeyPublicBytes: KeypairMeta {
74 fn from_public_bytes(key: &[u8]) -> Result<Self, Error>
76 where
77 Self: Sized;
78
79 fn with_public_bytes<O>(&self, f: impl FnOnce(&[u8]) -> O) -> O;
81}
82
83pub trait ToPublicBytes {
85 fn public_bytes_length(&self) -> Result<usize, Error>;
87
88 fn write_public_bytes(&self, out: &mut dyn WriteBuffer) -> Result<(), Error>;
90
91 #[cfg(feature = "alloc")]
92 #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
93 fn to_public_bytes(&self) -> Result<SecretBytes, Error> {
95 let mut buf = SecretBytes::with_capacity(128);
96 self.write_public_bytes(&mut buf)?;
97 Ok(buf)
98 }
99}
100
101impl<K> ToPublicBytes for K
102where
103 K: KeyPublicBytes,
104{
105 fn public_bytes_length(&self) -> Result<usize, Error> {
106 Ok(<Self as KeypairMeta>::PublicKeySize::USIZE)
107 }
108
109 fn write_public_bytes(&self, out: &mut dyn WriteBuffer) -> Result<(), Error> {
110 self.with_public_bytes(|buf| out.buffer_write(buf))
111 }
112}
113
114pub trait KeypairBytes {
116 fn from_keypair_bytes(key: &[u8]) -> Result<Self, Error>
118 where
119 Self: Sized;
120
121 fn with_keypair_bytes<O>(&self, f: impl FnOnce(Option<&[u8]>) -> O) -> O;
123
124 fn to_keypair_bytes_buffer<B: WriteBuffer>(&self, out: &mut B) -> Result<(), Error> {
126 self.with_keypair_bytes(|buf| {
127 if let Some(buf) = buf {
128 out.buffer_write(buf)
129 } else {
130 Err(err_msg!(MissingSecretKey))
131 }
132 })
133 }
134
135 #[cfg(feature = "alloc")]
136 #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
137 fn to_keypair_bytes(&self) -> Result<SecretBytes, Error> {
139 let mut buf = SecretBytes::with_capacity(128);
140 self.to_keypair_bytes_buffer(&mut buf)?;
141 Ok(buf)
142 }
143}
144
145pub trait KeyMeta {
147 type KeySize: ArrayLength<u8>;
149}
150
151pub trait KeypairMeta: KeyMeta {
153 type PublicKeySize: ArrayLength<u8>;
155 type KeypairSize: ArrayLength<u8>;
157}