Botan 3.10.0
Crypto and TLS for C&
donna.cpp
Go to the documentation of this file.
1/*
2* Based on curve25519-donna-c64.c from https://github.com/agl/curve25519-donna
3* revision 80ad9b9930c9baef5829dd2a235b6b7646d32a8e
4*
5* Further changes
6* (C) 2014,2018 Jack Lloyd
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11/* Copyright 2008, Google Inc.
12* All rights reserved.
13*
14* Code released into the public domain.
15*
16* curve25519-donna: Curve25519 elliptic curve, public key function
17*
18* https://code.google.com/p/curve25519-donna/
19*
20* Adam Langley <agl@imperialviolet.org>
21*
22* Derived from public domain C code by Daniel J. Bernstein <djb@cr.yp.to>
23*
24* More information about curve25519 can be found here
25* https://cr.yp.to/ecdh.html
26*
27* djb's sample implementation of curve25519 is written in a special assembly
28* language called qhasm and uses the floating point registers.
29*
30* This is, almost, a clean room reimplementation from the curve25519 paper. It
31* uses many of the tricks described therein. Only the crecip function is taken
32* from the sample implementation.
33*/
34
35#include <botan/x25519.h>
36
37#include <botan/internal/ct_utils.h>
38#include <botan/internal/donna128.h>
39#include <botan/internal/loadstor.h>
40
41namespace Botan {
42
43namespace {
44
45#if !defined(BOTAN_TARGET_HAS_NATIVE_UINT128)
46typedef donna128 uint128_t;
47#endif
48
49/* Sum two numbers: output += in */
50inline void fsum(uint64_t out[5], const uint64_t in[5]) {
51 out[0] += in[0];
52 out[1] += in[1];
53 out[2] += in[2];
54 out[3] += in[3];
55 out[4] += in[4];
56}
57
58/* Find the difference of two numbers: out = in - out
59* (note the order of the arguments!)
60*
61* Assumes that out[i] < 2**52
62* On return, out[i] < 2**55
63*/
64inline void fdifference_backwards(uint64_t out[5], const uint64_t in[5]) {
65 /* 152 is 19 << 3 */
66 const uint64_t two54m152 = (static_cast<uint64_t>(1) << 54) - 152;
67 const uint64_t two54m8 = (static_cast<uint64_t>(1) << 54) - 8;
68
69 out[0] = in[0] + two54m152 - out[0];
70 out[1] = in[1] + two54m8 - out[1];
71 out[2] = in[2] + two54m8 - out[2];
72 out[3] = in[3] + two54m8 - out[3];
73 out[4] = in[4] + two54m8 - out[4];
74}
75
76inline void fadd_sub(uint64_t x[5], uint64_t y[5]) {
77 // TODO merge these and avoid the tmp array
78 uint64_t tmp[5];
79 copy_mem(tmp, y, 5);
80 fsum(y, x);
81 fdifference_backwards(x, tmp); // does x - z
82}
83
84const uint64_t MASK_63 = 0x7ffffffffffff;
85
86/* Multiply a number by a scalar: out = in * scalar */
87inline void fscalar_product(uint64_t out[5], const uint64_t in[5], const uint64_t scalar) {
88 uint128_t a = uint128_t(in[0]) * scalar;
89 out[0] = a & MASK_63;
90
91 a = uint128_t(in[1]) * scalar + carry_shift(a, 51);
92 out[1] = a & MASK_63;
93
94 a = uint128_t(in[2]) * scalar + carry_shift(a, 51);
95 out[2] = a & MASK_63;
96
97 a = uint128_t(in[3]) * scalar + carry_shift(a, 51);
98 out[3] = a & MASK_63;
99
100 a = uint128_t(in[4]) * scalar + carry_shift(a, 51);
101 out[4] = a & MASK_63;
102
103 out[0] += carry_shift(a, 51) * 19;
104}
105
106/* Multiply two numbers: out = in2 * in
107*
108* out must be distinct to both inputs. The inputs are reduced coefficient
109* form, the output is not.
110*
111* Assumes that in[i] < 2**55 and likewise for in2.
112* On return, out[i] < 2**52
113*/
114inline void fmul(uint64_t out[5], const uint64_t in[5], const uint64_t in2[5]) {
115 const auto s0 = uint128_t(in2[0]);
116 const auto s1 = uint128_t(in2[1]);
117 const auto s2 = uint128_t(in2[2]);
118 const auto s3 = uint128_t(in2[3]);
119 const auto s4 = uint128_t(in2[4]);
120
121 uint64_t r0 = in[0];
122 uint64_t r1 = in[1];
123 uint64_t r2 = in[2];
124 uint64_t r3 = in[3];
125 uint64_t r4 = in[4];
126
127 uint128_t t0 = r0 * s0;
128 uint128_t t1 = r0 * s1 + r1 * s0;
129 uint128_t t2 = r0 * s2 + r2 * s0 + r1 * s1;
130 uint128_t t3 = r0 * s3 + r3 * s0 + r1 * s2 + r2 * s1;
131 uint128_t t4 = r0 * s4 + r4 * s0 + r3 * s1 + r1 * s3 + r2 * s2;
132
133 r4 *= 19;
134 r1 *= 19;
135 r2 *= 19;
136 r3 *= 19;
137
138 t0 += r4 * s1 + r1 * s4 + r2 * s3 + r3 * s2;
139 t1 += r4 * s2 + r2 * s4 + r3 * s3;
140 t2 += r4 * s3 + r3 * s4;
141 t3 += r4 * s4;
142
143 r0 = t0 & MASK_63;
144 t1 += carry_shift(t0, 51);
145 r1 = t1 & MASK_63;
146 t2 += carry_shift(t1, 51);
147 r2 = t2 & MASK_63;
148 t3 += carry_shift(t2, 51);
149 r3 = t3 & MASK_63;
150 t4 += carry_shift(t3, 51);
151 r4 = t4 & MASK_63;
152 uint64_t c = carry_shift(t4, 51);
153
154 r0 += c * 19;
155 c = r0 >> 51U;
156 r0 = r0 & MASK_63;
157 r1 += c;
158 c = r1 >> 51U;
159 r1 = r1 & MASK_63;
160 r2 += c;
161
162 out[0] = r0;
163 out[1] = r1;
164 out[2] = r2;
165 out[3] = r3;
166 out[4] = r4;
167}
168
169inline void fsquare(uint64_t out[5], const uint64_t in[5], size_t count = 1) {
170 uint64_t r0 = in[0];
171 uint64_t r1 = in[1];
172 uint64_t r2 = in[2];
173 uint64_t r3 = in[3];
174 uint64_t r4 = in[4];
175
176 for(size_t i = 0; i != count; ++i) {
177 const uint64_t d0 = r0 * 2;
178 const uint64_t d1 = r1 * 2;
179 const uint64_t d2 = r2 * 2 * 19;
180 const uint64_t d419 = r4 * 19;
181 const uint64_t d4 = d419 * 2;
182
183 uint128_t t0 = uint128_t(r0) * r0 + uint128_t(d4) * r1 + uint128_t(d2) * (r3);
184 uint128_t t1 = uint128_t(d0) * r1 + uint128_t(d4) * r2 + uint128_t(r3) * (r3 * 19);
185 uint128_t t2 = uint128_t(d0) * r2 + uint128_t(r1) * r1 + uint128_t(d4) * (r3);
186 uint128_t t3 = uint128_t(d0) * r3 + uint128_t(d1) * r2 + uint128_t(r4) * (d419);
187 uint128_t t4 = uint128_t(d0) * r4 + uint128_t(d1) * r3 + uint128_t(r2) * (r2);
188
189 r0 = t0 & MASK_63;
190 t1 += carry_shift(t0, 51);
191 r1 = t1 & MASK_63;
192 t2 += carry_shift(t1, 51);
193 r2 = t2 & MASK_63;
194 t3 += carry_shift(t2, 51);
195 r3 = t3 & MASK_63;
196 t4 += carry_shift(t3, 51);
197 r4 = t4 & MASK_63;
198 uint64_t c = carry_shift(t4, 51);
199
200 r0 += c * 19;
201 c = r0 >> 51U;
202 r0 = r0 & MASK_63;
203 r1 += c;
204 c = r1 >> 51U;
205 r1 = r1 & MASK_63;
206 r2 += c;
207 }
208
209 out[0] = r0;
210 out[1] = r1;
211 out[2] = r2;
212 out[3] = r3;
213 out[4] = r4;
214}
215
216/* Take a little-endian, 32-byte number and expand it into polynomial form */
217inline void fexpand(uint64_t* out, const uint8_t* in) {
218 out[0] = load_le<uint64_t>(in, 0) & MASK_63;
219 out[1] = (load_le<uint64_t>(in + 6, 0) >> 3) & MASK_63;
220 out[2] = (load_le<uint64_t>(in + 12, 0) >> 6) & MASK_63;
221 out[3] = (load_le<uint64_t>(in + 19, 0) >> 1) & MASK_63;
222 out[4] = (load_le<uint64_t>(in + 24, 0) >> 12) & MASK_63;
223}
224
225/* Take a fully reduced polynomial form number and contract it into a
226* little-endian, 32-byte array
227*/
228inline void fcontract(uint8_t* out, const uint64_t input[5]) {
229 auto t0 = uint128_t(input[0]);
230 auto t1 = uint128_t(input[1]);
231 auto t2 = uint128_t(input[2]);
232 auto t3 = uint128_t(input[3]);
233 auto t4 = uint128_t(input[4]);
234
235 for(size_t i = 0; i != 2; ++i) {
236 t1 += t0 >> 51U;
237 t0 &= MASK_63;
238 t2 += t1 >> 51U;
239 t1 &= MASK_63;
240 t3 += t2 >> 51U;
241 t2 &= MASK_63;
242 t4 += t3 >> 51U;
243 t3 &= MASK_63;
244 t0 += (t4 >> 51U) * 19;
245 t4 &= MASK_63;
246 }
247
248 /* now t is between 0 and 2^255-1, properly carried. */
249 /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
250
251 t0 += 19;
252
253 t1 += t0 >> 51U;
254 t0 &= MASK_63;
255 t2 += t1 >> 51U;
256 t1 &= MASK_63;
257 t3 += t2 >> 51U;
258 t2 &= MASK_63;
259 t4 += t3 >> 51U;
260 t3 &= MASK_63;
261 t0 += (t4 >> 51U) * 19;
262 t4 &= MASK_63;
263
264 /* now between 19 and 2^255-1 in both cases, and offset by 19. */
265
266 t0 += 0x8000000000000 - 19;
267 t1 += 0x8000000000000 - 1;
268 t2 += 0x8000000000000 - 1;
269 t3 += 0x8000000000000 - 1;
270 t4 += 0x8000000000000 - 1;
271
272 /* now between 2^255 and 2^256-20, and offset by 2^255. */
273
274 t1 += t0 >> 51U;
275 t0 &= MASK_63;
276 t2 += t1 >> 51U;
277 t1 &= MASK_63;
278 t3 += t2 >> 51U;
279 t2 &= MASK_63;
280 t4 += t3 >> 51U;
281 t3 &= MASK_63;
282 t4 &= MASK_63;
283
284 store_le(out,
285 combine_lower(t0, 0, t1, 51),
286 combine_lower(t1, 13, t2, 38),
287 combine_lower(t2, 26, t3, 25),
288 combine_lower(t3, 39, t4, 12));
289}
290
291/* Input: Q, Q', Q-Q'
292* Out: 2Q, Q+Q'
293*
294* result.two_q (2*Q): long form
295* result.q_plus_q_dash (Q + Q): long form
296* in_q: short form, destroyed
297* in_q_dash: short form, destroyed
298* in_q_minus_q_dash: short form, preserved
299*/
300void fmonty(uint64_t result_two_q_x[5],
301 uint64_t result_two_q_z[5],
302 uint64_t result_q_plus_q_dash_x[5],
303 uint64_t result_q_plus_q_dash_z[5],
304 uint64_t in_q_x[5],
305 uint64_t in_q_z[5],
306 uint64_t in_q_dash_x[5],
307 uint64_t in_q_dash_z[5],
308 const uint64_t q_minus_q_dash[5]) {
309 uint64_t zzz[5];
310 uint64_t xx[5];
311 uint64_t zz[5];
312 uint64_t xxprime[5];
313 uint64_t zzprime[5];
314 uint64_t zzzprime[5];
315
316 fadd_sub(in_q_z, in_q_x);
317 fadd_sub(in_q_dash_z, in_q_dash_x);
318
319 fmul(xxprime, in_q_dash_x, in_q_z);
320 fmul(zzprime, in_q_dash_z, in_q_x);
321
322 fadd_sub(zzprime, xxprime);
323
324 fsquare(result_q_plus_q_dash_x, xxprime);
325 fsquare(zzzprime, zzprime);
326 fmul(result_q_plus_q_dash_z, zzzprime, q_minus_q_dash);
327
328 fsquare(xx, in_q_x);
329 fsquare(zz, in_q_z);
330 fmul(result_two_q_x, xx, zz);
331
332 fdifference_backwards(zz, xx); // does zz = xx - zz
333 fscalar_product(zzz, zz, 121665);
334 fsum(zzz, xx);
335
336 fmul(result_two_q_z, zz, zzz);
337}
338
339/*
340* Maybe swap the contents of two uint64_t arrays (@a and @b),
341* Param @iswap is assumed to be either 0 or 1
342*
343* This function performs the swap without leaking any side-channel
344* information.
345*/
346inline void swap_conditional(uint64_t a[5], uint64_t b[5], uint64_t c[5], uint64_t d[5], CT::Mask<uint64_t> swap) {
347 for(size_t i = 0; i < 5; ++i) {
348 const uint64_t x0 = swap.if_set_return(a[i] ^ b[i]);
349 a[i] ^= x0;
350 b[i] ^= x0;
351
352 const uint64_t x1 = swap.if_set_return(c[i] ^ d[i]);
353 c[i] ^= x1;
354 d[i] ^= x1;
355 }
356}
357
358/* Calculates nQ where Q is the x-coordinate of a point on the curve
359*
360* resultx/resultz: the x/z coordinate of the resulting curve point (short form)
361* n: a little endian, 32-byte number
362* q: a point of the curve (short form)
363*/
364void cmult(uint64_t resultx[5], uint64_t resultz[5], const uint8_t n[32], const uint64_t q[5]) {
365 uint64_t a[5] = {0}; // nqpqx
366 uint64_t b[5] = {1}; // npqpz
367 uint64_t c[5] = {1}; // nqx
368 uint64_t d[5] = {0}; // nqz
369 uint64_t e[5] = {0}; // npqqx2
370 uint64_t f[5] = {1}; // npqqz2
371 uint64_t g[5] = {0}; // nqx2
372 uint64_t h[5] = {1}; // nqz2
373
374 copy_mem(a, q, 5);
375
376 for(size_t i = 0; i < 32; ++i) {
377 const uint64_t si = n[31 - i];
378 const auto bit0 = CT::Mask<uint64_t>::expand_bit(si, 7);
379 const auto bit1 = CT::Mask<uint64_t>::expand_bit(si, 6);
380 const auto bit2 = CT::Mask<uint64_t>::expand_bit(si, 5);
381 const auto bit3 = CT::Mask<uint64_t>::expand_bit(si, 4);
382 const auto bit4 = CT::Mask<uint64_t>::expand_bit(si, 3);
383 const auto bit5 = CT::Mask<uint64_t>::expand_bit(si, 2);
384 const auto bit6 = CT::Mask<uint64_t>::expand_bit(si, 1);
385 const auto bit7 = CT::Mask<uint64_t>::expand_bit(si, 0);
386
387 swap_conditional(c, a, d, b, bit0);
388 fmonty(g, h, e, f, c, d, a, b, q);
389
390 swap_conditional(g, e, h, f, bit0 ^ bit1);
391 fmonty(c, d, a, b, g, h, e, f, q);
392
393 swap_conditional(c, a, d, b, bit1 ^ bit2);
394 fmonty(g, h, e, f, c, d, a, b, q);
395
396 swap_conditional(g, e, h, f, bit2 ^ bit3);
397 fmonty(c, d, a, b, g, h, e, f, q);
398
399 swap_conditional(c, a, d, b, bit3 ^ bit4);
400 fmonty(g, h, e, f, c, d, a, b, q);
401
402 swap_conditional(g, e, h, f, bit4 ^ bit5);
403 fmonty(c, d, a, b, g, h, e, f, q);
404
405 swap_conditional(c, a, d, b, bit5 ^ bit6);
406 fmonty(g, h, e, f, c, d, a, b, q);
407
408 swap_conditional(g, e, h, f, bit6 ^ bit7);
409 fmonty(c, d, a, b, g, h, e, f, q);
410
411 swap_conditional(c, a, d, b, bit7);
412 }
413
414 copy_mem(resultx, c, 5);
415 copy_mem(resultz, d, 5);
416}
417
418// -----------------------------------------------------------------------------
419// Shamelessly copied from djb's code, tightened a little
420// -----------------------------------------------------------------------------
421void crecip(uint64_t out[5], const uint64_t z[5]) {
422 uint64_t a[5];
423 uint64_t b[5];
424 uint64_t c[5];
425 uint64_t t0[5];
426
427 fsquare(a, z); // 2
428 fsquare(t0, a, 2); // 8
429 fmul(b, t0, z); // 9
430 fmul(a, b, a); // 11
431 fsquare(t0, a); // 22
432 fmul(b, t0, b); // 2^5 - 2^0 = 31
433 fsquare(t0, b, 5); // 2^10 - 2^5
434 fmul(b, t0, b); // 2^10 - 2^0
435 fsquare(t0, b, 10); // 2^20 - 2^10
436 fmul(c, t0, b); // 2^20 - 2^0
437 fsquare(t0, c, 20); // 2^40 - 2^20
438 fmul(t0, t0, c); // 2^40 - 2^0
439 fsquare(t0, t0, 10); // 2^50 - 2^10
440 fmul(b, t0, b); // 2^50 - 2^0
441 fsquare(t0, b, 50); // 2^100 - 2^50
442 fmul(c, t0, b); // 2^100 - 2^0
443 fsquare(t0, c, 100); // 2^200 - 2^100
444 fmul(t0, t0, c); // 2^200 - 2^0
445 fsquare(t0, t0, 50); // 2^250 - 2^50
446 fmul(t0, t0, b); // 2^250 - 2^0
447 fsquare(t0, t0, 5); // 2^255 - 2^5
448 fmul(out, t0, a); // 2^255 - 21
449}
450
451} // namespace
452
453void curve25519_donna(uint8_t mypublic[32], const uint8_t secret[32], const uint8_t basepoint[32]) {
454 CT::poison(secret, 32);
455 CT::poison(basepoint, 32);
456
457 uint64_t bp[5];
458 uint64_t x[5];
459 uint64_t z[5];
460 uint64_t zmone[5];
461 uint8_t e[32];
462
463 copy_mem(e, secret, 32);
464 e[0] &= 248;
465 e[31] &= 127;
466 e[31] |= 64;
467
468 fexpand(bp, basepoint);
469 cmult(x, z, e, bp);
470 crecip(zmone, z);
471 fmul(z, x, zmone);
472 fcontract(mypublic, z);
473
474 CT::unpoison(secret, 32);
475 CT::unpoison(basepoint, 32);
476 CT::unpoison(mypublic, 32);
477}
478
479} // namespace Botan
static constexpr Mask< T > expand_bit(T v, size_t bit)
Definition ct_utils.h:449
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:65
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:54
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:145
constexpr uint64_t carry_shift(const donna128 &a, size_t shift)
Definition donna128.h:129
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736
constexpr auto load_le(ParamTs &&... params)
Definition loadstor.h:495
void curve25519_donna(uint8_t mypublic[32], const uint8_t secret[32], const uint8_t basepoint[32])
Definition donna.cpp:453
constexpr uint64_t combine_lower(const donna128 &a, size_t s1, const donna128 &b, size_t s2)
Definition donna128.h:133