bc_components/x25519/
x25519_private_key.rs1use std::rc::Rc;
2use bc_crypto::x25519_new_private_key_using;
3use bc_ur::prelude::*;
4use crate::{ tags, Decrypter, EncapsulationPrivateKey, SymmetricKey, X25519PublicKey };
5use bc_rand::{ SecureRandomNumberGenerator, RandomNumberGenerator };
6use anyhow::{ bail, Result };
7
8#[derive(Clone, PartialEq, Eq, Hash)]
28pub struct X25519PrivateKey([u8; Self::KEY_SIZE]);
29
30impl X25519PrivateKey {
31 pub const KEY_SIZE: usize = 32;
32
33 pub fn new() -> Self {
35 let mut rng = SecureRandomNumberGenerator;
36 Self::new_using(&mut rng)
37 }
38
39 pub fn keypair() -> (X25519PrivateKey, X25519PublicKey) {
41 let private_key = X25519PrivateKey::new();
42 let public_key = private_key.public_key();
43 (private_key, public_key)
44 }
45
46 pub fn keypair_using(rng: &mut impl RandomNumberGenerator) -> (X25519PrivateKey, X25519PublicKey) {
48 let private_key = X25519PrivateKey::new_using(rng);
49 let public_key = private_key.public_key();
50 (private_key, public_key)
51 }
52
53 pub fn new_using(rng: &mut impl RandomNumberGenerator) -> Self {
55 Self(x25519_new_private_key_using(rng))
56 }
57
58 pub const fn from_data(data: [u8; Self::KEY_SIZE]) -> Self {
60 Self(data)
61 }
62
63 pub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self> {
65 let data = data.as_ref();
66 if data.len() != Self::KEY_SIZE {
67 bail!("Invalid X25519 private key size");
68 }
69 let mut arr = [0u8; Self::KEY_SIZE];
70 arr.copy_from_slice(data);
71 Ok(Self::from_data(arr))
72 }
73
74 pub fn data(&self) -> &[u8; Self::KEY_SIZE] {
76 self.into()
77 }
78
79 pub fn from_hex(hex: impl AsRef<str>) -> Self {
85 Self::from_data_ref(hex::decode(hex.as_ref()).unwrap()).unwrap()
86 }
87
88 pub fn hex(&self) -> String {
90 hex::encode(self.data())
91 }
92
93 pub fn public_key(&self) -> X25519PublicKey {
95 X25519PublicKey::from_data(
96 bc_crypto::x25519_public_key_from_private_key(self.into())
97 )
98 }
99
100 pub fn derive_from_key_material(key_material: impl AsRef<[u8]>) -> Self {
102 Self::from_data(bc_crypto::x25519_derive_private_key(key_material))
103 }
104
105 pub fn shared_key_with(&self, public_key: &X25519PublicKey) -> SymmetricKey {
107 SymmetricKey::from_data(bc_crypto::x25519_shared_key(self.into(), public_key.into()))
108 }
109}
110
111impl Decrypter for X25519PrivateKey{
113 fn encapsulation_private_key(&self) -> EncapsulationPrivateKey {
114 EncapsulationPrivateKey::X25519(self.clone())
115 }
116}
117
118impl Default for X25519PrivateKey {
120 fn default() -> Self {
121 Self::new()
122 }
123}
124
125impl<'a> From<&'a X25519PrivateKey> for &'a [u8; X25519PrivateKey::KEY_SIZE] {
127 fn from(value: &'a X25519PrivateKey) -> Self {
128 &value.0
129 }
130}
131
132impl From<Rc<X25519PrivateKey>> for X25519PrivateKey {
134 fn from(value: Rc<X25519PrivateKey>) -> Self {
135 value.as_ref().clone()
136 }
137}
138
139impl AsRef<X25519PrivateKey> for X25519PrivateKey {
141 fn as_ref(&self) -> &Self {
142 self
143 }
144}
145
146impl CBORTagged for X25519PrivateKey {
148 fn cbor_tags() -> Vec<Tag> {
149 tags_for_values(&[tags::TAG_X25519_PRIVATE_KEY])
150 }
151}
152
153impl From<X25519PrivateKey> for CBOR {
155 fn from(value: X25519PrivateKey) -> Self {
156 value.tagged_cbor()
157 }
158}
159
160impl CBORTaggedEncodable for X25519PrivateKey {
162 fn untagged_cbor(&self) -> CBOR {
163 CBOR::to_byte_string(self.data())
164 }
165}
166
167impl TryFrom<CBOR> for X25519PrivateKey {
169 type Error = dcbor::Error;
170
171 fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
172 Self::from_tagged_cbor(cbor)
173 }
174}
175
176impl CBORTaggedDecodable for X25519PrivateKey {
178 fn from_untagged_cbor(untagged_cbor: CBOR) -> dcbor::Result<Self> {
179 let data = CBOR::try_into_byte_string(untagged_cbor)?;
180 Ok(Self::from_data_ref(data)?)
181 }
182}
183
184impl std::fmt::Debug for X25519PrivateKey {
186 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
187 write!(f, "X25519PrivateKey({})", self.hex())
188 }
189}
190
191impl From<&X25519PrivateKey> for X25519PrivateKey {
193 fn from(key: &X25519PrivateKey) -> Self {
194 key.clone()
195 }
196}
197
198impl From<X25519PrivateKey> for Vec<u8> {
200 fn from(key: X25519PrivateKey) -> Self {
201 key.0.to_vec()
202 }
203}
204
205impl From<&X25519PrivateKey> for Vec<u8> {
207 fn from(key: &X25519PrivateKey) -> Self {
208 key.0.to_vec()
209 }
210}