1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
pub use pkcs8::{DecodePrivateKey, DecodePublicKey};
#[cfg(feature = "alloc")]
pub use pkcs8::{spki::EncodePublicKey, EncodePrivateKey};
use core::fmt;
use pkcs8::ObjectIdentifier;
#[cfg(feature = "alloc")]
use pkcs8::der::{Document, SecretDocument};
#[cfg(feature = "pem")]
use {
alloc::string::{String, ToString},
core::str,
};
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
pub const ALGORITHM_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.101.112");
pub const ALGORITHM_ID: pkcs8::AlgorithmIdentifier<'static> = pkcs8::AlgorithmIdentifier {
oid: ALGORITHM_OID,
parameters: None,
};
pub struct KeypairBytes {
pub secret_key: [u8; Self::BYTE_SIZE / 2],
pub public_key: Option<[u8; Self::BYTE_SIZE / 2]>,
}
impl KeypairBytes {
const BYTE_SIZE: usize = 64;
pub fn from_bytes(bytes: &[u8; Self::BYTE_SIZE]) -> Self {
let (sk, pk) = bytes.split_at(Self::BYTE_SIZE / 2);
Self {
secret_key: sk.try_into().expect("secret key size error"),
public_key: Some(pk.try_into().expect("public key size error")),
}
}
pub fn to_bytes(&self) -> Option<[u8; Self::BYTE_SIZE]> {
if let Some(public_key) = &self.public_key {
let mut result = [0u8; Self::BYTE_SIZE];
let (sk, pk) = result.split_at_mut(Self::BYTE_SIZE / 2);
sk.copy_from_slice(&self.secret_key);
pk.copy_from_slice(public_key);
Some(result)
} else {
None
}
}
}
impl DecodePrivateKey for KeypairBytes {}
impl Drop for KeypairBytes {
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
self.secret_key.zeroize()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl EncodePrivateKey for KeypairBytes {
fn to_pkcs8_der(&self) -> pkcs8::Result<SecretDocument> {
let mut private_key = [0u8; 2 + (Self::BYTE_SIZE / 2)];
private_key[0] = 0x04;
private_key[1] = 0x20;
private_key[2..].copy_from_slice(&self.secret_key);
let private_key_info = pkcs8::PrivateKeyInfo {
algorithm: ALGORITHM_ID,
private_key: &private_key,
public_key: self.public_key.as_ref().map(AsRef::as_ref),
};
let result = SecretDocument::encode_msg(&private_key_info)?;
#[cfg(feature = "zeroize")]
private_key.zeroize();
Ok(result)
}
}
impl TryFrom<pkcs8::PrivateKeyInfo<'_>> for KeypairBytes {
type Error = pkcs8::Error;
fn try_from(private_key: pkcs8::PrivateKeyInfo<'_>) -> pkcs8::Result<Self> {
private_key.algorithm.assert_algorithm_oid(ALGORITHM_OID)?;
if private_key.algorithm.parameters.is_some() {
return Err(pkcs8::Error::ParametersMalformed);
}
let secret_key = match private_key.private_key {
[0x04, 0x20, rest @ ..] => rest.try_into().map_err(|_| pkcs8::Error::KeyMalformed),
_ => Err(pkcs8::Error::KeyMalformed),
}?;
let public_key = private_key
.public_key
.map(|bytes| bytes.try_into().map_err(|_| pkcs8::Error::KeyMalformed))
.transpose()?;
Ok(Self {
secret_key,
public_key,
})
}
}
impl TryFrom<&[u8]> for KeypairBytes {
type Error = pkcs8::Error;
fn try_from(der_bytes: &[u8]) -> pkcs8::Result<Self> {
Self::from_pkcs8_der(der_bytes)
}
}
impl fmt::Debug for KeypairBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("KeypairBytes")
.field("public_key", &self.public_key)
.finish_non_exhaustive()
}
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl str::FromStr for KeypairBytes {
type Err = pkcs8::Error;
fn from_str(pem: &str) -> pkcs8::Result<Self> {
Self::from_pkcs8_pem(pem)
}
}
pub struct PublicKeyBytes(pub [u8; Self::BYTE_SIZE]);
impl PublicKeyBytes {
const BYTE_SIZE: usize = 32;
pub fn to_bytes(&self) -> [u8; Self::BYTE_SIZE] {
self.0
}
}
impl AsRef<[u8; Self::BYTE_SIZE]> for PublicKeyBytes {
fn as_ref(&self) -> &[u8; Self::BYTE_SIZE] {
&self.0
}
}
impl DecodePublicKey for PublicKeyBytes {}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl EncodePublicKey for PublicKeyBytes {
fn to_public_key_der(&self) -> pkcs8::spki::Result<Document> {
pkcs8::SubjectPublicKeyInfo {
algorithm: ALGORITHM_ID,
subject_public_key: &self.0,
}
.try_into()
}
}
impl TryFrom<pkcs8::spki::SubjectPublicKeyInfo<'_>> for PublicKeyBytes {
type Error = pkcs8::spki::Error;
fn try_from(spki: pkcs8::spki::SubjectPublicKeyInfo<'_>) -> pkcs8::spki::Result<Self> {
spki.algorithm.assert_algorithm_oid(ALGORITHM_OID)?;
if spki.algorithm.parameters.is_some() {
return Err(pkcs8::spki::Error::KeyMalformed);
}
spki.subject_public_key
.try_into()
.map(Self)
.map_err(|_| pkcs8::spki::Error::KeyMalformed)
}
}
impl TryFrom<&[u8]> for PublicKeyBytes {
type Error = pkcs8::spki::Error;
fn try_from(der_bytes: &[u8]) -> pkcs8::spki::Result<Self> {
Self::from_public_key_der(der_bytes)
}
}
impl TryFrom<KeypairBytes> for PublicKeyBytes {
type Error = pkcs8::spki::Error;
fn try_from(keypair: KeypairBytes) -> pkcs8::spki::Result<PublicKeyBytes> {
PublicKeyBytes::try_from(&keypair)
}
}
impl TryFrom<&KeypairBytes> for PublicKeyBytes {
type Error = pkcs8::spki::Error;
fn try_from(keypair: &KeypairBytes) -> pkcs8::spki::Result<PublicKeyBytes> {
keypair
.public_key
.map(PublicKeyBytes)
.ok_or(pkcs8::spki::Error::KeyMalformed)
}
}
impl fmt::Debug for PublicKeyBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("PublicKeyBytes(")?;
for &byte in self.as_ref() {
write!(f, "{:02X}", byte)?;
}
f.write_str(")")
}
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl str::FromStr for PublicKeyBytes {
type Err = pkcs8::spki::Error;
fn from_str(pem: &str) -> pkcs8::spki::Result<Self> {
Self::from_public_key_pem(pem)
}
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl ToString for PublicKeyBytes {
fn to_string(&self) -> String {
self.to_public_key_pem(Default::default())
.expect("PEM serialization error")
}
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
#[cfg(test)]
mod tests {
use super::KeypairBytes;
use hex_literal::hex;
const SECRET_KEY_BYTES: [u8; 32] =
hex!("D4EE72DBF913584AD5B6D8F1F769F8AD3AFE7C28CBF1D4FBE097A88F44755842");
const PUBLIC_KEY_BYTES: [u8; 32] =
hex!("19BF44096984CDFE8541BAC167DC3B96C85086AA30B6B6CB0C5C38AD703166E1");
#[test]
fn to_bytes() {
let valid_keypair = KeypairBytes {
secret_key: SECRET_KEY_BYTES,
public_key: Some(PUBLIC_KEY_BYTES),
};
assert_eq!(
valid_keypair.to_bytes().unwrap(),
hex!("D4EE72DBF913584AD5B6D8F1F769F8AD3AFE7C28CBF1D4FBE097A88F4475584219BF44096984CDFE8541BAC167DC3B96C85086AA30B6B6CB0C5C38AD703166E1")
);
let invalid_keypair = KeypairBytes {
secret_key: SECRET_KEY_BYTES,
public_key: None,
};
assert_eq!(invalid_keypair.to_bytes(), None);
}
}