Debian Codegraph @

crypto_nP/montgomery25519/donna_c64/smult.c

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