line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* Copyright 2008, Google Inc. |
2
|
|
|
|
|
|
|
* All rights reserved. |
3
|
|
|
|
|
|
|
* |
4
|
|
|
|
|
|
|
* Code released into the public domain. |
5
|
|
|
|
|
|
|
* |
6
|
|
|
|
|
|
|
* curve25519-donna: Curve25519 elliptic curve, public key function |
7
|
|
|
|
|
|
|
* |
8
|
|
|
|
|
|
|
* http://code.google.com/p/curve25519-donna/ |
9
|
|
|
|
|
|
|
* |
10
|
|
|
|
|
|
|
* Adam Langley |
11
|
|
|
|
|
|
|
* |
12
|
|
|
|
|
|
|
* Derived from public domain C code by Daniel J. Bernstein |
13
|
|
|
|
|
|
|
* |
14
|
|
|
|
|
|
|
* More information about curve25519 can be found here |
15
|
|
|
|
|
|
|
* http://cr.yp.to/ecdh.html |
16
|
|
|
|
|
|
|
* |
17
|
|
|
|
|
|
|
* djb's sample implementation of curve25519 is written in a special assembly |
18
|
|
|
|
|
|
|
* language called qhasm and uses the floating point registers. |
19
|
|
|
|
|
|
|
* |
20
|
|
|
|
|
|
|
* This is, almost, a clean room reimplementation from the curve25519 paper. It |
21
|
|
|
|
|
|
|
* uses many of the tricks described therein. Only the crecip function is taken |
22
|
|
|
|
|
|
|
* from the sample implementation. |
23
|
|
|
|
|
|
|
*/ |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
#include |
26
|
|
|
|
|
|
|
#include |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
typedef uint8_t u8; |
29
|
|
|
|
|
|
|
typedef uint64_t limb; |
30
|
|
|
|
|
|
|
typedef limb felem[5]; |
31
|
|
|
|
|
|
|
// This is a special gcc mode for 128-bit integers. It's implemented on 64-bit |
32
|
|
|
|
|
|
|
// platforms only as far as I know. |
33
|
|
|
|
|
|
|
typedef unsigned uint128_t __attribute__((mode(TI))); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#undef force_inline |
36
|
|
|
|
|
|
|
#define force_inline __attribute__((always_inline)) |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
/* Sum two numbers: output += in */ |
39
|
|
|
|
|
|
|
static inline void force_inline |
40
|
|
|
|
|
|
|
fsum(limb *output, const limb *in) { |
41
|
40980480
|
|
|
|
|
|
output[0] += in[0]; |
42
|
40980480
|
|
|
|
|
|
output[1] += in[1]; |
43
|
40980480
|
|
|
|
|
|
output[2] += in[2]; |
44
|
40980480
|
|
|
|
|
|
output[3] += in[3]; |
45
|
40980480
|
|
|
|
|
|
output[4] += in[4]; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
/* Find the difference of two numbers: output = in - output |
49
|
|
|
|
|
|
|
* (note the order of the arguments!) |
50
|
|
|
|
|
|
|
* |
51
|
|
|
|
|
|
|
* Assumes that out[i] < 2**52 |
52
|
|
|
|
|
|
|
* On return, out[i] < 2**55 |
53
|
|
|
|
|
|
|
*/ |
54
|
|
|
|
|
|
|
static inline void force_inline |
55
|
|
|
|
|
|
|
fdifference_backwards(felem out, const felem in) { |
56
|
|
|
|
|
|
|
/* 152 is 19 << 3 */ |
57
|
|
|
|
|
|
|
static const limb two54m152 = (((limb)1) << 54) - 152; |
58
|
|
|
|
|
|
|
static const limb two54m8 = (((limb)1) << 54) - 8; |
59
|
|
|
|
|
|
|
|
60
|
40980480
|
|
|
|
|
|
out[0] = in[0] + two54m152 - out[0]; |
61
|
40980480
|
|
|
|
|
|
out[1] = in[1] + two54m8 - out[1]; |
62
|
40980480
|
|
|
|
|
|
out[2] = in[2] + two54m8 - out[2]; |
63
|
40980480
|
|
|
|
|
|
out[3] = in[3] + two54m8 - out[3]; |
64
|
40980480
|
|
|
|
|
|
out[4] = in[4] + two54m8 - out[4]; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
/* Multiply a number by a scalar: output = in * scalar */ |
68
|
|
|
|
|
|
|
static inline void force_inline |
69
|
|
|
|
|
|
|
fscalar_product(felem output, const felem in, const limb scalar) { |
70
|
|
|
|
|
|
|
uint128_t a; |
71
|
|
|
|
|
|
|
|
72
|
10245120
|
|
|
|
|
|
a = ((uint128_t) in[0]) * scalar; |
73
|
10245120
|
|
|
|
|
|
output[0] = ((limb)a) & 0x7ffffffffffff; |
74
|
|
|
|
|
|
|
|
75
|
10245120
|
|
|
|
|
|
a = ((uint128_t) in[1]) * scalar + ((limb) (a >> 51)); |
76
|
10245120
|
|
|
|
|
|
output[1] = ((limb)a) & 0x7ffffffffffff; |
77
|
|
|
|
|
|
|
|
78
|
10245120
|
|
|
|
|
|
a = ((uint128_t) in[2]) * scalar + ((limb) (a >> 51)); |
79
|
10245120
|
|
|
|
|
|
output[2] = ((limb)a) & 0x7ffffffffffff; |
80
|
|
|
|
|
|
|
|
81
|
10245120
|
|
|
|
|
|
a = ((uint128_t) in[3]) * scalar + ((limb) (a >> 51)); |
82
|
10245120
|
|
|
|
|
|
output[3] = ((limb)a) & 0x7ffffffffffff; |
83
|
|
|
|
|
|
|
|
84
|
10245120
|
|
|
|
|
|
a = ((uint128_t) in[4]) * scalar + ((limb) (a >> 51)); |
85
|
10245120
|
|
|
|
|
|
output[4] = ((limb)a) & 0x7ffffffffffff; |
86
|
|
|
|
|
|
|
|
87
|
10245120
|
|
|
|
|
|
output[0] += (a >> 51) * 19; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
/* Multiply two numbers: output = in2 * in |
91
|
|
|
|
|
|
|
* |
92
|
|
|
|
|
|
|
* output must be distinct to both inputs. The inputs are reduced coefficient |
93
|
|
|
|
|
|
|
* form, the output is not. |
94
|
|
|
|
|
|
|
* |
95
|
|
|
|
|
|
|
* Assumes that in[i] < 2**55 and likewise for in2. |
96
|
|
|
|
|
|
|
* On return, output[i] < 2**52 |
97
|
|
|
|
|
|
|
*/ |
98
|
|
|
|
|
|
|
static inline void force_inline |
99
|
|
|
|
|
|
|
fmulren(felem output, const felem in2, const felem in) { |
100
|
|
|
|
|
|
|
uint128_t t[5]; |
101
|
|
|
|
|
|
|
limb r0,r1,r2,r3,r4,s0,s1,s2,s3,s4,c; |
102
|
|
|
|
|
|
|
|
103
|
51705840
|
|
|
|
|
|
r0 = in[0]; |
104
|
51705840
|
|
|
|
|
|
r1 = in[1]; |
105
|
51705840
|
|
|
|
|
|
r2 = in[2]; |
106
|
51705840
|
|
|
|
|
|
r3 = in[3]; |
107
|
51705840
|
|
|
|
|
|
r4 = in[4]; |
108
|
|
|
|
|
|
|
|
109
|
51705840
|
|
|
|
|
|
s0 = in2[0]; |
110
|
51705840
|
|
|
|
|
|
s1 = in2[1]; |
111
|
51705840
|
|
|
|
|
|
s2 = in2[2]; |
112
|
51705840
|
|
|
|
|
|
s3 = in2[3]; |
113
|
51705840
|
|
|
|
|
|
s4 = in2[4]; |
114
|
|
|
|
|
|
|
|
115
|
51705840
|
|
|
|
|
|
t[0] = ((uint128_t) r0) * s0; |
116
|
51705840
|
|
|
|
|
|
t[1] = ((uint128_t) r0) * s1 + ((uint128_t) r1) * s0; |
117
|
51705840
|
|
|
|
|
|
t[2] = ((uint128_t) r0) * s2 + ((uint128_t) r2) * s0 + ((uint128_t) r1) * s1; |
118
|
51705840
|
|
|
|
|
|
t[3] = ((uint128_t) r0) * s3 + ((uint128_t) r3) * s0 + ((uint128_t) r1) * s2 + ((uint128_t) r2) * s1; |
119
|
51705840
|
|
|
|
|
|
t[4] = ((uint128_t) r0) * s4 + ((uint128_t) r4) * s0 + ((uint128_t) r3) * s1 + ((uint128_t) r1) * s3 + ((uint128_t) r2) * s2; |
120
|
|
|
|
|
|
|
|
121
|
51705840
|
|
|
|
|
|
r4 *= 19; |
122
|
51705840
|
|
|
|
|
|
r1 *= 19; |
123
|
51705840
|
|
|
|
|
|
r2 *= 19; |
124
|
51705840
|
|
|
|
|
|
r3 *= 19; |
125
|
|
|
|
|
|
|
|
126
|
51705840
|
|
|
|
|
|
t[0] += ((uint128_t) r4) * s1 + ((uint128_t) r1) * s4 + ((uint128_t) r2) * s3 + ((uint128_t) r3) * s2; |
127
|
51705840
|
|
|
|
|
|
t[1] += ((uint128_t) r4) * s2 + ((uint128_t) r2) * s4 + ((uint128_t) r3) * s3; |
128
|
51705840
|
|
|
|
|
|
t[2] += ((uint128_t) r4) * s3 + ((uint128_t) r3) * s4; |
129
|
51705840
|
|
|
|
|
|
t[3] += ((uint128_t) r4) * s4; |
130
|
|
|
|
|
|
|
|
131
|
51705840
|
|
|
|
|
|
r0 = (limb)t[0] & 0x7ffffffffffff; c = (limb)(t[0] >> 51); |
132
|
51705840
|
|
|
|
|
|
t[1] += c; r1 = (limb)t[1] & 0x7ffffffffffff; c = (limb)(t[1] >> 51); |
133
|
51705840
|
|
|
|
|
|
t[2] += c; r2 = (limb)t[2] & 0x7ffffffffffff; c = (limb)(t[2] >> 51); |
134
|
51705840
|
|
|
|
|
|
t[3] += c; r3 = (limb)t[3] & 0x7ffffffffffff; c = (limb)(t[3] >> 51); |
135
|
51705840
|
|
|
|
|
|
t[4] += c; r4 = (limb)t[4] & 0x7ffffffffffff; c = (limb)(t[4] >> 51); |
136
|
51705840
|
|
|
|
|
|
r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffff; |
137
|
51705840
|
|
|
|
|
|
r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffff; |
138
|
51705840
|
|
|
|
|
|
r2 += c; |
139
|
|
|
|
|
|
|
|
140
|
51705840
|
|
|
|
|
|
output[0] = r0; |
141
|
51705840
|
|
|
|
|
|
output[1] = r1; |
142
|
51705840
|
|
|
|
|
|
output[2] = r2; |
143
|
51705840
|
|
|
|
|
|
output[3] = r3; |
144
|
51705840
|
|
|
|
|
|
output[4] = r4; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
static inline void force_inline |
148
|
|
|
|
|
|
|
fsquare_times(felem output, const felem in, limb count) { |
149
|
|
|
|
|
|
|
uint128_t t[5]; |
150
|
|
|
|
|
|
|
limb r0,r1,r2,r3,r4,c; |
151
|
|
|
|
|
|
|
limb d0,d1,d2,d4,d419; |
152
|
|
|
|
|
|
|
|
153
|
41420700
|
|
|
|
|
|
r0 = in[0]; |
154
|
41420700
|
|
|
|
|
|
r1 = in[1]; |
155
|
41420700
|
|
|
|
|
|
r2 = in[2]; |
156
|
41420700
|
|
|
|
|
|
r3 = in[3]; |
157
|
41420700
|
|
|
|
|
|
r4 = in[4]; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
do { |
160
|
51145560
|
|
|
|
|
|
d0 = r0 * 2; |
161
|
51145560
|
|
|
|
|
|
d1 = r1 * 2; |
162
|
51145560
|
|
|
|
|
|
d2 = r2 * 2 * 19; |
163
|
51145560
|
|
|
|
|
|
d419 = r4 * 19; |
164
|
51145560
|
|
|
|
|
|
d4 = d419 * 2; |
165
|
|
|
|
|
|
|
|
166
|
51145560
|
|
|
|
|
|
t[0] = ((uint128_t) r0) * r0 + ((uint128_t) d4) * r1 + (((uint128_t) d2) * (r3 )); |
167
|
51145560
|
|
|
|
|
|
t[1] = ((uint128_t) d0) * r1 + ((uint128_t) d4) * r2 + (((uint128_t) r3) * (r3 * 19)); |
168
|
51145560
|
|
|
|
|
|
t[2] = ((uint128_t) d0) * r2 + ((uint128_t) r1) * r1 + (((uint128_t) d4) * (r3 )); |
169
|
51145560
|
|
|
|
|
|
t[3] = ((uint128_t) d0) * r3 + ((uint128_t) d1) * r2 + (((uint128_t) r4) * (d419 )); |
170
|
51145560
|
|
|
|
|
|
t[4] = ((uint128_t) d0) * r4 + ((uint128_t) d1) * r3 + (((uint128_t) r2) * (r2 )); |
171
|
|
|
|
|
|
|
|
172
|
51145560
|
|
|
|
|
|
r0 = (limb)t[0] & 0x7ffffffffffff; c = (limb)(t[0] >> 51); |
173
|
51145560
|
|
|
|
|
|
t[1] += c; r1 = (limb)t[1] & 0x7ffffffffffff; c = (limb)(t[1] >> 51); |
174
|
51145560
|
|
|
|
|
|
t[2] += c; r2 = (limb)t[2] & 0x7ffffffffffff; c = (limb)(t[2] >> 51); |
175
|
51145560
|
|
|
|
|
|
t[3] += c; r3 = (limb)t[3] & 0x7ffffffffffff; c = (limb)(t[3] >> 51); |
176
|
51145560
|
|
|
|
|
|
t[4] += c; r4 = (limb)t[4] & 0x7ffffffffffff; c = (limb)(t[4] >> 51); |
177
|
51145560
|
|
|
|
|
|
r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffff; |
178
|
51145560
|
|
|
|
|
|
r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffff; |
179
|
51145560
|
|
|
|
|
|
r2 += c; |
180
|
51145560
|
50
|
|
|
|
|
} while(--count); |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
41420700
|
|
|
|
|
|
output[0] = r0; |
183
|
41420700
|
|
|
|
|
|
output[1] = r1; |
184
|
41420700
|
|
|
|
|
|
output[2] = r2; |
185
|
41420700
|
|
|
|
|
|
output[3] = r3; |
186
|
41420700
|
|
|
|
|
|
output[4] = r4; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
/* Load a little-endian 64-bit number */ |
190
|
|
|
|
|
|
|
static limb |
191
|
200100
|
|
|
|
|
|
load_limb(const u8 *in) { |
192
|
|
|
|
|
|
|
return |
193
|
200100
|
|
|
|
|
|
((limb)in[0]) | |
194
|
400200
|
|
|
|
|
|
(((limb)in[1]) << 8) | |
195
|
400200
|
|
|
|
|
|
(((limb)in[2]) << 16) | |
196
|
400200
|
|
|
|
|
|
(((limb)in[3]) << 24) | |
197
|
400200
|
|
|
|
|
|
(((limb)in[4]) << 32) | |
198
|
400200
|
|
|
|
|
|
(((limb)in[5]) << 40) | |
199
|
200100
|
|
|
|
|
|
(((limb)in[6]) << 48) | |
200
|
200100
|
|
|
|
|
|
(((limb)in[7]) << 56); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
static void |
204
|
160080
|
|
|
|
|
|
store_limb(u8 *out, limb in) { |
205
|
160080
|
|
|
|
|
|
out[0] = in & 0xff; |
206
|
160080
|
|
|
|
|
|
out[1] = (in >> 8) & 0xff; |
207
|
160080
|
|
|
|
|
|
out[2] = (in >> 16) & 0xff; |
208
|
160080
|
|
|
|
|
|
out[3] = (in >> 24) & 0xff; |
209
|
160080
|
|
|
|
|
|
out[4] = (in >> 32) & 0xff; |
210
|
160080
|
|
|
|
|
|
out[5] = (in >> 40) & 0xff; |
211
|
160080
|
|
|
|
|
|
out[6] = (in >> 48) & 0xff; |
212
|
160080
|
|
|
|
|
|
out[7] = (in >> 56) & 0xff; |
213
|
160080
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
/* Take a little-endian, 32-byte number and expand it into polynomial form */ |
216
|
|
|
|
|
|
|
static void |
217
|
40020
|
|
|
|
|
|
fexpand(limb *output, const u8 *in) { |
218
|
40020
|
|
|
|
|
|
output[0] = load_limb(in) & 0x7ffffffffffff; |
219
|
40020
|
|
|
|
|
|
output[1] = (load_limb(in+6) >> 3) & 0x7ffffffffffff; |
220
|
40020
|
|
|
|
|
|
output[2] = (load_limb(in+12) >> 6) & 0x7ffffffffffff; |
221
|
40020
|
|
|
|
|
|
output[3] = (load_limb(in+19) >> 1) & 0x7ffffffffffff; |
222
|
40020
|
|
|
|
|
|
output[4] = (load_limb(in+24) >> 12) & 0xfffffffffffff; |
223
|
40020
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
/* Take a fully reduced polynomial form number and contract it into a |
226
|
|
|
|
|
|
|
* little-endian, 32-byte array |
227
|
|
|
|
|
|
|
*/ |
228
|
|
|
|
|
|
|
static void |
229
|
40020
|
|
|
|
|
|
fcontract(u8 *output, const felem input) { |
230
|
|
|
|
|
|
|
uint128_t t[5]; |
231
|
|
|
|
|
|
|
|
232
|
40020
|
|
|
|
|
|
t[0] = input[0]; |
233
|
40020
|
|
|
|
|
|
t[1] = input[1]; |
234
|
40020
|
|
|
|
|
|
t[2] = input[2]; |
235
|
40020
|
|
|
|
|
|
t[3] = input[3]; |
236
|
40020
|
|
|
|
|
|
t[4] = input[4]; |
237
|
|
|
|
|
|
|
|
238
|
40020
|
|
|
|
|
|
t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff; |
239
|
40020
|
|
|
|
|
|
t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff; |
240
|
40020
|
|
|
|
|
|
t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff; |
241
|
40020
|
|
|
|
|
|
t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff; |
242
|
40020
|
|
|
|
|
|
t[0] += 19 * (t[4] >> 51); t[4] &= 0x7ffffffffffff; |
243
|
|
|
|
|
|
|
|
244
|
40020
|
|
|
|
|
|
t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff; |
245
|
40020
|
|
|
|
|
|
t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff; |
246
|
40020
|
|
|
|
|
|
t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff; |
247
|
40020
|
|
|
|
|
|
t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff; |
248
|
40020
|
|
|
|
|
|
t[0] += 19 * (t[4] >> 51); t[4] &= 0x7ffffffffffff; |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
/* now t is between 0 and 2^255-1, properly carried. */ |
251
|
|
|
|
|
|
|
/* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */ |
252
|
|
|
|
|
|
|
|
253
|
40020
|
|
|
|
|
|
t[0] += 19; |
254
|
|
|
|
|
|
|
|
255
|
40020
|
|
|
|
|
|
t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff; |
256
|
40020
|
|
|
|
|
|
t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff; |
257
|
40020
|
|
|
|
|
|
t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff; |
258
|
40020
|
|
|
|
|
|
t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff; |
259
|
40020
|
|
|
|
|
|
t[0] += 19 * (t[4] >> 51); t[4] &= 0x7ffffffffffff; |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
/* now between 19 and 2^255-1 in both cases, and offset by 19. */ |
262
|
|
|
|
|
|
|
|
263
|
40020
|
|
|
|
|
|
t[0] += 0x8000000000000 - 19; |
264
|
40020
|
|
|
|
|
|
t[1] += 0x8000000000000 - 1; |
265
|
40020
|
|
|
|
|
|
t[2] += 0x8000000000000 - 1; |
266
|
40020
|
|
|
|
|
|
t[3] += 0x8000000000000 - 1; |
267
|
40020
|
|
|
|
|
|
t[4] += 0x8000000000000 - 1; |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
/* now between 2^255 and 2^256-20, and offset by 2^255. */ |
270
|
|
|
|
|
|
|
|
271
|
40020
|
|
|
|
|
|
t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff; |
272
|
40020
|
|
|
|
|
|
t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff; |
273
|
40020
|
|
|
|
|
|
t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff; |
274
|
40020
|
|
|
|
|
|
t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff; |
275
|
40020
|
|
|
|
|
|
t[4] &= 0x7ffffffffffff; |
276
|
|
|
|
|
|
|
|
277
|
40020
|
|
|
|
|
|
store_limb(output, t[0] | (t[1] << 51)); |
278
|
40020
|
|
|
|
|
|
store_limb(output+8, (t[1] >> 13) | (t[2] << 38)); |
279
|
40020
|
|
|
|
|
|
store_limb(output+16, (t[2] >> 26) | (t[3] << 25)); |
280
|
40020
|
|
|
|
|
|
store_limb(output+24, (t[3] >> 39) | (t[4] << 12)); |
281
|
40020
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
/* Input: Q, Q', Q-Q' |
284
|
|
|
|
|
|
|
* Output: 2Q, Q+Q' |
285
|
|
|
|
|
|
|
* |
286
|
|
|
|
|
|
|
* x2 z3: long form |
287
|
|
|
|
|
|
|
* x3 z3: long form |
288
|
|
|
|
|
|
|
* x z: short form, destroyed |
289
|
|
|
|
|
|
|
* xprime zprime: short form, destroyed |
290
|
|
|
|
|
|
|
* qmqp: short form, preserved |
291
|
|
|
|
|
|
|
*/ |
292
|
|
|
|
|
|
|
static void |
293
|
10245120
|
|
|
|
|
|
fmonty(limb *x2, limb *z2, /* output 2Q */ |
294
|
|
|
|
|
|
|
limb *x3, limb *z3, /* output Q + Q' */ |
295
|
|
|
|
|
|
|
limb *x, limb *z, /* input Q */ |
296
|
|
|
|
|
|
|
limb *xprime, limb *zprime, /* input Q' */ |
297
|
|
|
|
|
|
|
const limb *qmqp /* input Q - Q' */) { |
298
|
|
|
|
|
|
|
limb origx[5], origxprime[5], zzz[5], xx[5], zz[5], xxprime[5], |
299
|
|
|
|
|
|
|
zzprime[5], zzzprime[5]; |
300
|
|
|
|
|
|
|
|
301
|
10245120
|
|
|
|
|
|
memcpy(origx, x, 5 * sizeof(limb)); |
302
|
|
|
|
|
|
|
fsum(x, z); |
303
|
|
|
|
|
|
|
fdifference_backwards(z, origx); // does x - z |
304
|
|
|
|
|
|
|
|
305
|
10245120
|
|
|
|
|
|
memcpy(origxprime, xprime, sizeof(limb) * 5); |
306
|
|
|
|
|
|
|
fsum(xprime, zprime); |
307
|
|
|
|
|
|
|
fdifference_backwards(zprime, origxprime); |
308
|
|
|
|
|
|
|
fmulren(xxprime, xprime, z); |
309
|
|
|
|
|
|
|
fmulren(zzprime, x, zprime); |
310
|
10245120
|
|
|
|
|
|
memcpy(origxprime, xxprime, sizeof(limb) * 5); |
311
|
|
|
|
|
|
|
fsum(xxprime, zzprime); |
312
|
|
|
|
|
|
|
fdifference_backwards(zzprime, origxprime); |
313
|
|
|
|
|
|
|
fsquare_times(x3, xxprime, 1); |
314
|
|
|
|
|
|
|
fsquare_times(zzzprime, zzprime, 1); |
315
|
|
|
|
|
|
|
fmulren(z3, zzzprime, qmqp); |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
fsquare_times(xx, x, 1); |
318
|
|
|
|
|
|
|
fsquare_times(zz, z, 1); |
319
|
|
|
|
|
|
|
fmulren(x2, xx, zz); |
320
|
|
|
|
|
|
|
fdifference_backwards(zz, xx); // does zz = xx - zz |
321
|
|
|
|
|
|
|
fscalar_product(zzz, zz, 121665); |
322
|
|
|
|
|
|
|
fsum(zzz, xx); |
323
|
|
|
|
|
|
|
fmulren(z2, zz, zzz); |
324
|
10245120
|
|
|
|
|
|
} |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------- |
327
|
|
|
|
|
|
|
// Maybe swap the contents of two limb arrays (@a and @b), each @len elements |
328
|
|
|
|
|
|
|
// long. Perform the swap iff @swap is non-zero. |
329
|
|
|
|
|
|
|
// |
330
|
|
|
|
|
|
|
// This function performs the swap without leaking any side-channel |
331
|
|
|
|
|
|
|
// information. |
332
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------- |
333
|
|
|
|
|
|
|
static void |
334
|
40980480
|
|
|
|
|
|
swap_conditional(limb a[5], limb b[5], limb iswap) { |
335
|
|
|
|
|
|
|
unsigned i; |
336
|
40980480
|
|
|
|
|
|
const limb swap = -iswap; |
337
|
|
|
|
|
|
|
|
338
|
245882880
|
100
|
|
|
|
|
for (i = 0; i < 5; ++i) { |
339
|
204902400
|
|
|
|
|
|
const limb x = swap & (a[i] ^ b[i]); |
340
|
204902400
|
|
|
|
|
|
a[i] ^= x; |
341
|
204902400
|
|
|
|
|
|
b[i] ^= x; |
342
|
|
|
|
|
|
|
} |
343
|
40980480
|
|
|
|
|
|
} |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
/* Calculates nQ where Q is the x-coordinate of a point on the curve |
346
|
|
|
|
|
|
|
* |
347
|
|
|
|
|
|
|
* resultx/resultz: the x coordinate of the resulting curve point (short form) |
348
|
|
|
|
|
|
|
* n: a little endian, 32-byte number |
349
|
|
|
|
|
|
|
* q: a point of the curve (short form) |
350
|
|
|
|
|
|
|
*/ |
351
|
|
|
|
|
|
|
static void |
352
|
40020
|
|
|
|
|
|
cmult(limb *resultx, limb *resultz, const u8 *n, const limb *q) { |
353
|
40020
|
|
|
|
|
|
limb a[5] = {0}, b[5] = {1}, c[5] = {1}, d[5] = {0}; |
354
|
40020
|
|
|
|
|
|
limb *nqpqx = a, *nqpqz = b, *nqx = c, *nqz = d, *t; |
355
|
40020
|
|
|
|
|
|
limb e[5] = {0}, f[5] = {1}, g[5] = {0}, h[5] = {1}; |
356
|
40020
|
|
|
|
|
|
limb *nqpqx2 = e, *nqpqz2 = f, *nqx2 = g, *nqz2 = h; |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
unsigned i, j; |
359
|
|
|
|
|
|
|
|
360
|
40020
|
|
|
|
|
|
memcpy(nqpqx, q, sizeof(limb) * 5); |
361
|
|
|
|
|
|
|
|
362
|
1320660
|
100
|
|
|
|
|
for (i = 0; i < 32; ++i) { |
363
|
1280640
|
|
|
|
|
|
u8 byte = n[31 - i]; |
364
|
11525760
|
100
|
|
|
|
|
for (j = 0; j < 8; ++j) { |
365
|
10245120
|
|
|
|
|
|
const limb bit = byte >> 7; |
366
|
|
|
|
|
|
|
|
367
|
10245120
|
|
|
|
|
|
swap_conditional(nqx, nqpqx, bit); |
368
|
10245120
|
|
|
|
|
|
swap_conditional(nqz, nqpqz, bit); |
369
|
10245120
|
|
|
|
|
|
fmonty(nqx2, nqz2, |
370
|
|
|
|
|
|
|
nqpqx2, nqpqz2, |
371
|
|
|
|
|
|
|
nqx, nqz, |
372
|
|
|
|
|
|
|
nqpqx, nqpqz, |
373
|
|
|
|
|
|
|
q); |
374
|
10245120
|
|
|
|
|
|
swap_conditional(nqx2, nqpqx2, bit); |
375
|
10245120
|
|
|
|
|
|
swap_conditional(nqz2, nqpqz2, bit); |
376
|
|
|
|
|
|
|
|
377
|
10245120
|
|
|
|
|
|
t = nqx; |
378
|
10245120
|
|
|
|
|
|
nqx = nqx2; |
379
|
10245120
|
|
|
|
|
|
nqx2 = t; |
380
|
10245120
|
|
|
|
|
|
t = nqz; |
381
|
10245120
|
|
|
|
|
|
nqz = nqz2; |
382
|
10245120
|
|
|
|
|
|
nqz2 = t; |
383
|
10245120
|
|
|
|
|
|
t = nqpqx; |
384
|
10245120
|
|
|
|
|
|
nqpqx = nqpqx2; |
385
|
10245120
|
|
|
|
|
|
nqpqx2 = t; |
386
|
10245120
|
|
|
|
|
|
t = nqpqz; |
387
|
10245120
|
|
|
|
|
|
nqpqz = nqpqz2; |
388
|
10245120
|
|
|
|
|
|
nqpqz2 = t; |
389
|
|
|
|
|
|
|
|
390
|
10245120
|
|
|
|
|
|
byte <<= 1; |
391
|
|
|
|
|
|
|
} |
392
|
|
|
|
|
|
|
} |
393
|
|
|
|
|
|
|
|
394
|
40020
|
|
|
|
|
|
memcpy(resultx, nqx, sizeof(limb) * 5); |
395
|
40020
|
|
|
|
|
|
memcpy(resultz, nqz, sizeof(limb) * 5); |
396
|
40020
|
|
|
|
|
|
} |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------- |
400
|
|
|
|
|
|
|
// Shamelessly copied from djb's code, tightened a little |
401
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------- |
402
|
|
|
|
|
|
|
static void |
403
|
40020
|
|
|
|
|
|
crecip(felem out, const felem z) { |
404
|
|
|
|
|
|
|
felem a,t0,b,c; |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
/* 2 */ fsquare_times(a, z, 1); // a = 2 |
407
|
|
|
|
|
|
|
/* 8 */ fsquare_times(t0, a, 2); |
408
|
|
|
|
|
|
|
/* 9 */ fmulren(b, t0, z); // b = 9 |
409
|
|
|
|
|
|
|
/* 11 */ fmulren(a, b, a); // a = 11 |
410
|
|
|
|
|
|
|
/* 22 */ fsquare_times(t0, a, 1); |
411
|
|
|
|
|
|
|
/* 2^5 - 2^0 = 31 */ fmulren(b, t0, b); |
412
|
|
|
|
|
|
|
/* 2^10 - 2^5 */ fsquare_times(t0, b, 5); |
413
|
|
|
|
|
|
|
/* 2^10 - 2^0 */ fmulren(b, t0, b); |
414
|
|
|
|
|
|
|
/* 2^20 - 2^10 */ fsquare_times(t0, b, 10); |
415
|
|
|
|
|
|
|
/* 2^20 - 2^0 */ fmulren(c, t0, b); |
416
|
|
|
|
|
|
|
/* 2^40 - 2^20 */ fsquare_times(t0, c, 20); |
417
|
|
|
|
|
|
|
/* 2^40 - 2^0 */ fmulren(t0, t0, c); |
418
|
|
|
|
|
|
|
/* 2^50 - 2^10 */ fsquare_times(t0, t0, 10); |
419
|
|
|
|
|
|
|
/* 2^50 - 2^0 */ fmulren(b, t0, b); |
420
|
|
|
|
|
|
|
/* 2^100 - 2^50 */ fsquare_times(t0, b, 50); |
421
|
|
|
|
|
|
|
/* 2^100 - 2^0 */ fmulren(c, t0, b); |
422
|
|
|
|
|
|
|
/* 2^200 - 2^100 */ fsquare_times(t0, c, 100); |
423
|
|
|
|
|
|
|
/* 2^200 - 2^0 */ fmulren(t0, t0, c); |
424
|
|
|
|
|
|
|
/* 2^250 - 2^50 */ fsquare_times(t0, t0, 50); |
425
|
|
|
|
|
|
|
/* 2^250 - 2^0 */ fmulren(t0, t0, b); |
426
|
|
|
|
|
|
|
/* 2^255 - 2^5 */ fsquare_times(t0, t0, 5); |
427
|
|
|
|
|
|
|
/* 2^255 - 21 */ fmulren(out, t0, a); |
428
|
40020
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
int curve25519_donna(u8 *, const u8 *, const u8 *); |
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
int |
433
|
40020
|
|
|
|
|
|
curve25519_donna(u8 *mypublic, const u8 *secret, const u8 *basepoint) { |
434
|
|
|
|
|
|
|
limb bp[5], x[5], z[5], zmone[5]; |
435
|
|
|
|
|
|
|
uint8_t e[32]; |
436
|
|
|
|
|
|
|
int i; |
437
|
|
|
|
|
|
|
|
438
|
1320660
|
100
|
|
|
|
|
for (i = 0;i < 32;++i) e[i] = secret[i]; |
439
|
40020
|
|
|
|
|
|
e[0] &= 248; |
440
|
40020
|
|
|
|
|
|
e[31] &= 127; |
441
|
40020
|
|
|
|
|
|
e[31] |= 64; |
442
|
|
|
|
|
|
|
|
443
|
40020
|
|
|
|
|
|
fexpand(bp, basepoint); |
444
|
40020
|
|
|
|
|
|
cmult(x, z, e, bp); |
445
|
40020
|
|
|
|
|
|
crecip(zmone, z); |
446
|
|
|
|
|
|
|
fmulren(z, x, zmone); |
447
|
40020
|
|
|
|
|
|
fcontract(mypublic, z); |
448
|
40020
|
|
|
|
|
|
return 0; |
449
|
|
|
|
|
|
|
} |