bc_components/x25519/
x25519_private_key.rs1use std::rc::Rc;
2
3use anyhow::{Result, bail};
4use bc_crypto::x25519_new_private_key_using;
5use bc_rand::{RandomNumberGenerator, SecureRandomNumberGenerator};
6use bc_ur::prelude::*;
7
8use crate::{
9 Decrypter, EncapsulationPrivateKey, 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 bail!("Invalid X25519 private key size");
75 }
76 let mut arr = [0u8; Self::KEY_SIZE];
77 arr.copy_from_slice(data);
78 Ok(Self::from_data(arr))
79 }
80
81 pub fn data(&self) -> &[u8; Self::KEY_SIZE] { self.into() }
83
84 pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
86
87 pub fn from_hex(hex: impl AsRef<str>) -> Self {
94 Self::from_data_ref(hex::decode(hex.as_ref()).unwrap()).unwrap()
95 }
96
97 pub fn hex(&self) -> String { hex::encode(self.data()) }
99
100 pub fn public_key(&self) -> X25519PublicKey {
102 X25519PublicKey::from_data(
103 bc_crypto::x25519_public_key_from_private_key(self.into()),
104 )
105 }
106
107 pub fn derive_from_key_material(key_material: impl AsRef<[u8]>) -> Self {
109 Self::from_data(bc_crypto::derive_agreement_private_key(key_material))
110 }
111
112 pub fn shared_key_with(
115 &self,
116 public_key: &X25519PublicKey,
117 ) -> SymmetricKey {
118 SymmetricKey::from_data(bc_crypto::x25519_shared_key(
119 self.into(),
120 public_key.into(),
121 ))
122 }
123}
124
125impl AsRef<[u8]> for X25519PrivateKey {
126 fn as_ref(&self) -> &[u8] { &self.0 }
127}
128
129impl Decrypter for X25519PrivateKey {
131 fn encapsulation_private_key(&self) -> EncapsulationPrivateKey {
132 EncapsulationPrivateKey::X25519(self.clone())
133 }
134}
135
136impl Default for X25519PrivateKey {
138 fn default() -> Self { Self::new() }
139}
140
141impl<'a> From<&'a X25519PrivateKey> for &'a [u8; X25519PrivateKey::KEY_SIZE] {
144 fn from(value: &'a X25519PrivateKey) -> Self { &value.0 }
145}
146
147impl From<Rc<X25519PrivateKey>> for X25519PrivateKey {
150 fn from(value: Rc<X25519PrivateKey>) -> Self { value.as_ref().clone() }
151}
152
153impl AsRef<X25519PrivateKey> for X25519PrivateKey {
155 fn as_ref(&self) -> &Self { self }
156}
157
158impl CBORTagged for X25519PrivateKey {
160 fn cbor_tags() -> Vec<Tag> {
161 tags_for_values(&[tags::TAG_X25519_PRIVATE_KEY])
162 }
163}
164
165impl From<X25519PrivateKey> for CBOR {
167 fn from(value: X25519PrivateKey) -> Self { value.tagged_cbor() }
168}
169
170impl CBORTaggedEncodable for X25519PrivateKey {
172 fn untagged_cbor(&self) -> CBOR { CBOR::to_byte_string(self.data()) }
173}
174
175impl TryFrom<CBOR> for X25519PrivateKey {
178 type Error = dcbor::Error;
179
180 fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
181 Self::from_tagged_cbor(cbor)
182 }
183}
184
185impl CBORTaggedDecodable for X25519PrivateKey {
187 fn from_untagged_cbor(untagged_cbor: CBOR) -> dcbor::Result<Self> {
188 let data = CBOR::try_into_byte_string(untagged_cbor)?;
189 Ok(Self::from_data_ref(data)?)
190 }
191}
192
193impl std::fmt::Debug for X25519PrivateKey {
195 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
196 write!(f, "X25519PrivateKey({})", self.hex())
197 }
198}
199
200impl From<&X25519PrivateKey> for X25519PrivateKey {
203 fn from(key: &X25519PrivateKey) -> Self { key.clone() }
204}
205
206impl From<X25519PrivateKey> for Vec<u8> {
208 fn from(key: X25519PrivateKey) -> Self { key.0.to_vec() }
209}
210
211impl From<&X25519PrivateKey> for Vec<u8> {
213 fn from(key: &X25519PrivateKey) -> Self { key.0.to_vec() }
214}