bc_components/x25519/
x25519_private_key.rs1use std::rc::Rc;
2
3use bc_crypto::x25519_new_private_key_using;
4use bc_rand::{RandomNumberGenerator, SecureRandomNumberGenerator};
5use bc_ur::prelude::*;
6
7use crate::{
8 Decrypter, Digest, EncapsulationPrivateKey, Error, Reference,
9 ReferenceProvider, Result, SymmetricKey, X25519PublicKey, tags,
10};
11
12#[derive(Clone, PartialEq, Eq, Hash)]
32pub struct X25519PrivateKey([u8; Self::KEY_SIZE]);
33
34impl X25519PrivateKey {
35 pub const KEY_SIZE: usize = 32;
36
37 pub fn new() -> Self {
39 let mut rng = SecureRandomNumberGenerator;
40 Self::new_using(&mut rng)
41 }
42
43 pub fn keypair() -> (X25519PrivateKey, X25519PublicKey) {
46 let private_key = X25519PrivateKey::new();
47 let public_key = private_key.public_key();
48 (private_key, public_key)
49 }
50
51 pub fn keypair_using(
54 rng: &mut impl RandomNumberGenerator,
55 ) -> (X25519PrivateKey, X25519PublicKey) {
56 let private_key = X25519PrivateKey::new_using(rng);
57 let public_key = private_key.public_key();
58 (private_key, public_key)
59 }
60
61 pub fn new_using(rng: &mut impl RandomNumberGenerator) -> Self {
64 Self(x25519_new_private_key_using(rng))
65 }
66
67 pub const fn from_data(data: [u8; Self::KEY_SIZE]) -> Self { Self(data) }
69
70 pub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> {
72 let data = data.as_ref();
73 if data.len() != Self::KEY_SIZE {
74 return Err(Error::invalid_size(
75 "X25519 private key",
76 Self::KEY_SIZE,
77 data.len(),
78 ));
79 }
80 let mut arr = [0u8; Self::KEY_SIZE];
81 arr.copy_from_slice(data);
82 Ok(Self::from_data(arr))
83 }
84
85 pub fn data(&self) -> &[u8; Self::KEY_SIZE] { self.into() }
87
88 pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
90
91 pub fn from_hex(hex: impl AsRef<str>) -> Self {
98 Self::from_data_ref(hex::decode(hex.as_ref()).unwrap()).unwrap()
99 }
100
101 pub fn hex(&self) -> String { hex::encode(self.data()) }
103
104 pub fn public_key(&self) -> X25519PublicKey {
106 X25519PublicKey::from_data(
107 bc_crypto::x25519_public_key_from_private_key(self.into()),
108 )
109 }
110
111 pub fn derive_from_key_material(key_material: impl AsRef<[u8]>) -> Self {
113 Self::from_data(bc_crypto::derive_agreement_private_key(key_material))
114 }
115
116 pub fn shared_key_with(
119 &self,
120 public_key: &X25519PublicKey,
121 ) -> SymmetricKey {
122 SymmetricKey::from_data(bc_crypto::x25519_shared_key(
123 self.into(),
124 public_key.into(),
125 ))
126 }
127}
128
129impl AsRef<[u8]> for X25519PrivateKey {
130 fn as_ref(&self) -> &[u8] { &self.0 }
131}
132
133impl Decrypter for X25519PrivateKey {
135 fn encapsulation_private_key(&self) -> EncapsulationPrivateKey {
136 EncapsulationPrivateKey::X25519(self.clone())
137 }
138}
139
140impl Default for X25519PrivateKey {
142 fn default() -> Self { Self::new() }
143}
144
145impl<'a> From<&'a X25519PrivateKey> for &'a [u8; X25519PrivateKey::KEY_SIZE] {
148 fn from(value: &'a X25519PrivateKey) -> Self { &value.0 }
149}
150
151impl From<Rc<X25519PrivateKey>> for X25519PrivateKey {
154 fn from(value: Rc<X25519PrivateKey>) -> Self { value.as_ref().clone() }
155}
156
157impl AsRef<X25519PrivateKey> for X25519PrivateKey {
159 fn as_ref(&self) -> &Self { self }
160}
161
162impl CBORTagged for X25519PrivateKey {
164 fn cbor_tags() -> Vec<Tag> {
165 tags_for_values(&[tags::TAG_X25519_PRIVATE_KEY])
166 }
167}
168
169impl From<X25519PrivateKey> for CBOR {
171 fn from(value: X25519PrivateKey) -> Self { value.tagged_cbor() }
172}
173
174impl CBORTaggedEncodable for X25519PrivateKey {
176 fn untagged_cbor(&self) -> CBOR { CBOR::to_byte_string(self.data()) }
177}
178
179impl TryFrom<CBOR> for X25519PrivateKey {
182 type Error = dcbor::Error;
183
184 fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
185 Self::from_tagged_cbor(cbor)
186 }
187}
188
189impl CBORTaggedDecodable for X25519PrivateKey {
191 fn from_untagged_cbor(untagged_cbor: CBOR) -> dcbor::Result<Self> {
192 let data = CBOR::try_into_byte_string(untagged_cbor)?;
193 Ok(Self::from_data_ref(data)?)
194 }
195}
196
197impl std::fmt::Debug for X25519PrivateKey {
199 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
200 write!(f, "X25519PrivateKey({})", self.hex())
201 }
202}
203
204impl From<&X25519PrivateKey> for X25519PrivateKey {
207 fn from(key: &X25519PrivateKey) -> Self { key.clone() }
208}
209
210impl From<X25519PrivateKey> for Vec<u8> {
212 fn from(key: X25519PrivateKey) -> Self { key.0.to_vec() }
213}
214
215impl From<&X25519PrivateKey> for Vec<u8> {
217 fn from(key: &X25519PrivateKey) -> Self { key.0.to_vec() }
218}
219
220impl ReferenceProvider for X25519PrivateKey {
222 fn reference(&self) -> Reference {
223 Reference::from_digest(Digest::from_image(
224 self.tagged_cbor().to_cbor_data(),
225 ))
226 }
227}
228
229impl std::fmt::Display for X25519PrivateKey {
230 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
231 write!(f, "X25519PrivateKey({})", self.ref_hex_short())
232 }
233}