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
use super::{super::ops::*, PUBLIC_KEY_LEN};
use core;
use crate::{der, digest, error, pkcs8, rand, signature, signature_impl};
use untrusted;
use super::digest::*;
pub struct KeyPair {
private_scalar: Scalar,
private_prefix: Prefix,
public_key: PublicKey,
}
impl<'a> KeyPair {
pub fn generate_pkcs8(
rng: &rand::SecureRandom,
) -> Result<[u8; PKCS8_V2_LEN], error::Unspecified> {
let mut seed = [0u8; SEED_LEN];
rng.fill(&mut seed)?;
let key_pair = Self::from_seed_(&seed);
let mut bytes = [0; PKCS8_V2_LEN];
pkcs8::wrap_key_(
&PKCS8_TEMPLATE,
&seed[..],
key_pair.public_key_bytes(),
&mut bytes[..],
);
Ok(bytes)
}
pub fn from_pkcs8(input: untrusted::Input) -> Result<Self, error::Unspecified> {
let (seed, public_key) = unwrap_pkcs8(pkcs8::Version::V2Only, input)?;
Self::from_seed_and_public_key(seed, public_key.unwrap())
}
pub fn from_pkcs8_maybe_unchecked(input: untrusted::Input) -> Result<Self, error::Unspecified> {
let (seed, public_key) = unwrap_pkcs8(pkcs8::Version::V1OrV2, input)?;
if let Some(public_key) = public_key {
Self::from_seed_and_public_key(seed, public_key)
} else {
Self::from_seed_unchecked(seed)
}
}
pub fn from_seed_and_public_key(
seed: untrusted::Input, public_key: untrusted::Input,
) -> Result<Self, error::Unspecified> {
let pair = Self::from_seed_unchecked(seed)?;
if public_key != pair.public_key_bytes() {
return Err(error::Unspecified);
}
Ok(pair)
}
pub fn from_seed_unchecked(seed: untrusted::Input) -> Result<Self, error::Unspecified> {
let seed = slice_as_array_ref!(seed.as_slice_less_safe(), SEED_LEN)?;
Ok(Self::from_seed_(seed))
}
fn from_seed_(seed: &Seed) -> Self {
let h = digest::digest(&digest::SHA512, seed);
let (scalar_encoded, prefix_encoded) = h.as_ref().split_at(SCALAR_LEN);
let mut scalar = [0u8; SCALAR_LEN];
scalar.copy_from_slice(&scalar_encoded);
unsafe { GFp_x25519_sc_mask(&mut scalar) };
let mut prefix = [0u8; PREFIX_LEN];
prefix.copy_from_slice(prefix_encoded);
let mut a = ExtPoint::new_at_infinity();
unsafe {
GFp_x25519_ge_scalarmult_base(&mut a, &scalar);
}
Self {
private_scalar: scalar,
private_prefix: prefix,
public_key: a.into_encoded_point(),
}
}
pub fn public_key_bytes(&'a self) -> &'a [u8] { &self.public_key }
pub fn sign(&self, msg: &[u8]) -> signature::Signature {
let mut signature_bytes = [0u8; SIGNATURE_LEN];
{
let (signature_r, signature_s) = signature_bytes.split_at_mut(ELEM_LEN);
let signature_r = slice_as_array_ref_mut!(signature_r, ELEM_LEN).unwrap();
let signature_s = slice_as_array_ref_mut!(signature_s, SCALAR_LEN).unwrap();
let nonce = {
let mut ctx = digest::Context::new(&digest::SHA512);
ctx.update(&self.private_prefix);
ctx.update(msg);
ctx.finish()
};
let nonce = digest_scalar(nonce);
let mut r = ExtPoint::new_at_infinity();
unsafe {
GFp_x25519_ge_scalarmult_base(&mut r, &nonce);
}
*signature_r = r.into_encoded_point();
let hram_digest = eddsa_digest(signature_r, &self.public_key, msg);
let hram = digest_scalar(hram_digest);
unsafe {
GFp_x25519_sc_muladd(signature_s, &hram, &self.private_scalar, &nonce);
}
}
signature_impl::signature_from_bytes(&signature_bytes)
}
}
fn unwrap_pkcs8(
version: pkcs8::Version, input: untrusted::Input,
) -> Result<(untrusted::Input, Option<untrusted::Input>), error::Unspecified> {
let (private_key, public_key) = pkcs8::unwrap_key(&PKCS8_TEMPLATE, version, input)?;
let private_key = private_key.read_all(error::Unspecified, |input| {
der::expect_tag_and_get_value(input, der::Tag::OctetString)
})?;
Ok((private_key, public_key))
}
extern "C" {
fn GFp_x25519_ge_scalarmult_base(h: &mut ExtPoint, a: &Seed);
fn GFp_x25519_sc_mask(a: &mut Scalar);
fn GFp_x25519_sc_muladd(s: &mut Scalar, a: &Scalar, b: &Scalar, c: &Scalar);
}
type PublicKey = [u8; PUBLIC_KEY_LEN];
type Prefix = [u8; PREFIX_LEN];
const PREFIX_LEN: usize = digest::SHA512_OUTPUT_LEN - SCALAR_LEN;
const SIGNATURE_LEN: usize = ELEM_LEN + SCALAR_LEN;
type Seed = [u8; SEED_LEN];
const SEED_LEN: usize = 32;
static PKCS8_TEMPLATE: pkcs8::Template = pkcs8::Template {
bytes: include_bytes!("ed25519_pkcs8_v2_template.der"),
alg_id_range: core::ops::Range { start: 7, end: 12 },
curve_id_index: 0,
private_key_index: 0x10,
};
pub const PKCS8_V2_LEN: usize = 0x55;