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
use core::convert::TryFrom;
use crate::{
constants::SECRETKEY_SEED_LENGTH,
field::{
FieldImplementation as _,
FieldElement,
},
montgomery::MontgomeryPoint,
scalar::Scalar,
Result,
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct PublicKey(pub(crate) MontgomeryPoint);
#[derive(Clone)]
pub struct SecretKey(pub(crate) Scalar);
pub struct SharedSecret(pub(crate) MontgomeryPoint);
impl TryFrom<[u8; 32]> for PublicKey {
type Error = crate::Error;
fn try_from(bytes: [u8; 32]) -> Result<Self> {
let field_element = FieldElement::from_bytes(&bytes)?;
Ok(PublicKey(MontgomeryPoint(field_element)))
}
}
impl<'a> From<&'a SecretKey> for PublicKey {
fn from(secret: &'a SecretKey) -> PublicKey {
let public_point = &secret.0 * &MontgomeryPoint::basepoint();
PublicKey(public_point)
}
}
impl PublicKey {
#[inline]
pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}
}
impl SharedSecret {
#[inline]
pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}
}
impl SecretKey {
pub fn agree(&self, their_public: &PublicKey) -> SharedSecret {
SharedSecret(&self.0 * &their_public.0)
}
pub fn from_seed(seed: &[u8; SECRETKEY_SEED_LENGTH]) -> Self {
Self(clamp_scalar(seed.clone()))
}
pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}
}
fn clamp_scalar(mut scalar: [u8; 32]) -> Scalar {
scalar[0] &= 248;
scalar[31] &= 127;
scalar[31] |= 64;
Scalar(scalar)
}
pub fn make_255_bit(bytes: [u8; 32]) -> FieldElement {
let mut bytes = bytes;
bytes[31] &= 0b0111_1111;
FieldElement::from_unreduced_bytes(&bytes)
}
pub fn x25519(scalar: [u8; 32], input_u: [u8; 32]) -> [u8; 32] {
let scalar = clamp_scalar(scalar);
let secret_key = SecretKey(scalar);
let input_u = make_255_bit(input_u);
let input_point = MontgomeryPoint(input_u);
let public_key = PublicKey(input_point);
let agreed_secret = secret_key.agree(&public_key);
let raw_agreed_secret = agreed_secret.0.to_bytes();
raw_agreed_secret
}
#[cfg(test)]
mod tests {
use core::convert::TryInto;
use super::*;
#[test]
fn direct_agreement() {
let seed1: [u8; 32] = [
0x98, 0xa7, 0x02, 0x22, 0xf0, 0xb8, 0x12, 0x1a,
0xa9, 0xd3, 0x0f, 0x81, 0x3d, 0x68, 0x3f, 0x80,
0x9e, 0x46, 0x2b, 0x46, 0x9c, 0x7f, 0xf8, 0x76,
0x39, 0x49, 0x9b, 0xb9, 0x4e, 0x6d, 0xae, 0x41,
];
let seed2: [u8; 32] = [
0x31, 0xf8, 0x50, 0x42, 0x46, 0x3c, 0x2a, 0x35,
0x5a, 0x20, 0x03, 0xd0, 0x62, 0xad, 0xf5, 0xaa,
0xa1, 0x0b, 0x8c, 0x61, 0xe6, 0x36, 0x06, 0x2a,
0xaa, 0xd1, 0x1c, 0x2a, 0x26, 0x08, 0x34, 0x06,
];
let sk1 = SecretKey::from_seed(&seed1);
let sk2 = SecretKey::from_seed(&seed2);
let pk1 = PublicKey::from(&sk1);
let pk2 = PublicKey::from(&sk2);
let shared1 = sk1.agree(&pk2);
let shared2 = sk2.agree(&pk1);
assert_eq!(shared1.0, shared2.0);
}
fn load_bytes(little_endian_hex_digits: &str) -> [u8; 32] {
hex::decode(little_endian_hex_digits).unwrap().try_into().unwrap()
}
fn rfc_7748_x25519_expected_outputs(
input_scalar: &str,
input_u: &str,
output_u: &str,
) {
let scalar = clamp_scalar(load_bytes(input_scalar));
let secret_key = SecretKey(scalar);
let input_u = make_255_bit(load_bytes(input_u));
let input_point = MontgomeryPoint(input_u);
let public_key = PublicKey(input_point);
let agreed_secret = secret_key.agree(&public_key);
let output_u = FieldElement::from_bytes(&load_bytes(output_u)).unwrap();
let output_point = MontgomeryPoint(output_u);
assert_eq!(agreed_secret.0, output_point);
}
#[test]
fn rfc_7748_x25519_vector_1() {
rfc_7748_x25519_expected_outputs(
"a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4",
"e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c",
"c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552",
);
}
#[test]
fn rfc_7748_x25519_vector_2() {
rfc_7748_x25519_expected_outputs(
"4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d",
"e5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493",
"95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957",
);
}
#[test]
fn rfc_7748_x25519_iterated() {
let s = "0900000000000000000000000000000000000000000000000000000000000000";
let k = load_bytes(s);
let mut u = load_bytes(s);
let mut k = x25519(k, u);
assert_eq!(hex::encode(k), "422c8e7a6227d7bca1350b3e2bb7279f7897b87bb6854b783c60e80311ae3079");
let mut result = [0u8; 32];
(0..999usize).for_each(|_| {
result = x25519(k, u);
u = k;
k = result;
});
assert_eq!(hex::encode(k), "684cf59ba83309552800ef566f2f4d3c1c3887c49360e3875f2eb94d99532c51");
}
}