libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_scalarmult / curve25519 / donna_c64 / smult_curve25519_donna_c64.c
blobc10b9ad70e4dd4893eb43d169bbe3d6fcceb3cd8
1 /* Copyright 2008, Google Inc.
2 * All rights reserved.
4 * Code released into the public domain.
6 * curve25519-donna: Curve25519 elliptic curve, public key function
8 * http://code.google.com/p/curve25519-donna/
10 * Adam Langley <agl@imperialviolet.org>
11 * Parts optimised by floodyberry
12 * Derived from public domain C code by Daniel J. Bernstein <djb@cr.yp.to>
14 * More information about curve25519 can be found here
15 * http://cr.yp.to/ecdh.html
17 * djb's sample implementation of curve25519 is written in a special assembly
18 * language called qhasm and uses the floating point registers.
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.
25 #include <string.h>
26 #include <stdint.h>
27 #include "api.h"
29 #ifdef HAVE_TI_MODE
31 typedef uint8_t u8;
32 typedef uint64_t limb;
33 typedef limb felem[5];
34 // This is a special gcc mode for 128-bit integers. It's implemented on 64-bit
35 // platforms only as far as I know.
36 typedef unsigned uint128_t __attribute__((mode(TI)));
38 #undef force_inline
39 #define force_inline inline __attribute__((always_inline))
41 /* Sum two numbers: output += in */
42 static force_inline void
43 fsum(limb *output, const limb *in) {
44 output[0] += in[0];
45 output[1] += in[1];
46 output[2] += in[2];
47 output[3] += in[3];
48 output[4] += in[4];
51 /* Find the difference of two numbers: output = in - output
52 * (note the order of the arguments!)
54 * Assumes that out[i] < 2**52
55 * On return, out[i] < 2**55
57 static force_inline void
58 fdifference_backwards(felem out, const felem in) {
59 /* 152 is 19 << 3 */
60 static const limb two54m152 = (((limb)1) << 54) - 152;
61 static const limb two54m8 = (((limb)1) << 54) - 8;
63 out[0] = in[0] + two54m152 - out[0];
64 out[1] = in[1] + two54m8 - out[1];
65 out[2] = in[2] + two54m8 - out[2];
66 out[3] = in[3] + two54m8 - out[3];
67 out[4] = in[4] + two54m8 - out[4];
70 /* Multiply a number by a scalar: output = in * scalar */
71 static force_inline void
72 fscalar_product(felem output, const felem in, const limb scalar) {
73 uint128_t a;
75 a = ((uint128_t) in[0]) * scalar;
76 output[0] = ((limb)a) & 0x7ffffffffffff;
78 a = ((uint128_t) in[1]) * scalar + ((limb) (a >> 51));
79 output[1] = ((limb)a) & 0x7ffffffffffff;
81 a = ((uint128_t) in[2]) * scalar + ((limb) (a >> 51));
82 output[2] = ((limb)a) & 0x7ffffffffffff;
84 a = ((uint128_t) in[3]) * scalar + ((limb) (a >> 51));
85 output[3] = ((limb)a) & 0x7ffffffffffff;
87 a = ((uint128_t) in[4]) * scalar + ((limb) (a >> 51));
88 output[4] = ((limb)a) & 0x7ffffffffffff;
90 output[0] += (a >> 51) * 19;
93 /* Multiply two numbers: output = in2 * in
95 * output must be distinct to both inputs. The inputs are reduced coefficient
96 * form, the output is not.
98 * Assumes that in[i] < 2**55 and likewise for in2.
99 * On return, output[i] < 2**52
101 static force_inline void
102 fmul(felem output, const felem in2, const felem in) {
103 uint128_t t[5];
104 limb r0,r1,r2,r3,r4,s0,s1,s2,s3,s4,c;
106 r0 = in[0];
107 r1 = in[1];
108 r2 = in[2];
109 r3 = in[3];
110 r4 = in[4];
112 s0 = in2[0];
113 s1 = in2[1];
114 s2 = in2[2];
115 s3 = in2[3];
116 s4 = in2[4];
118 t[0] = ((uint128_t) r0) * s0;
119 t[1] = ((uint128_t) r0) * s1 + ((uint128_t) r1) * s0;
120 t[2] = ((uint128_t) r0) * s2 + ((uint128_t) r2) * s0 + ((uint128_t) r1) * s1;
121 t[3] = ((uint128_t) r0) * s3 + ((uint128_t) r3) * s0 + ((uint128_t) r1) * s2 + ((uint128_t) r2) * s1;
122 t[4] = ((uint128_t) r0) * s4 + ((uint128_t) r4) * s0 + ((uint128_t) r3) * s1 + ((uint128_t) r1) * s3 + ((uint128_t) r2) * s2;
124 r4 *= 19;
125 r1 *= 19;
126 r2 *= 19;
127 r3 *= 19;
129 t[0] += ((uint128_t) r4) * s1 + ((uint128_t) r1) * s4 + ((uint128_t) r2) * s3 + ((uint128_t) r3) * s2;
130 t[1] += ((uint128_t) r4) * s2 + ((uint128_t) r2) * s4 + ((uint128_t) r3) * s3;
131 t[2] += ((uint128_t) r4) * s3 + ((uint128_t) r3) * s4;
132 t[3] += ((uint128_t) r4) * s4;
134 r0 = (limb)t[0] & 0x7ffffffffffff; c = (limb)(t[0] >> 51);
135 t[1] += c; r1 = (limb)t[1] & 0x7ffffffffffff; c = (limb)(t[1] >> 51);
136 t[2] += c; r2 = (limb)t[2] & 0x7ffffffffffff; c = (limb)(t[2] >> 51);
137 t[3] += c; r3 = (limb)t[3] & 0x7ffffffffffff; c = (limb)(t[3] >> 51);
138 t[4] += c; r4 = (limb)t[4] & 0x7ffffffffffff; c = (limb)(t[4] >> 51);
139 r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffff;
140 r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffff;
141 r2 += c;
143 output[0] = r0;
144 output[1] = r1;
145 output[2] = r2;
146 output[3] = r3;
147 output[4] = r4;
150 static force_inline void
151 fsquare_times(felem output, const felem in, limb count) {
152 uint128_t t[5];
153 limb r0,r1,r2,r3,r4,c;
154 limb d0,d1,d2,d4,d419;
156 r0 = in[0];
157 r1 = in[1];
158 r2 = in[2];
159 r3 = in[3];
160 r4 = in[4];
162 do {
163 d0 = r0 * 2;
164 d1 = r1 * 2;
165 d2 = r2 * 2 * 19;
166 d419 = r4 * 19;
167 d4 = d419 * 2;
169 t[0] = ((uint128_t) r0) * r0 + ((uint128_t) d4) * r1 + (((uint128_t) d2) * (r3 ));
170 t[1] = ((uint128_t) d0) * r1 + ((uint128_t) d4) * r2 + (((uint128_t) r3) * (r3 * 19));
171 t[2] = ((uint128_t) d0) * r2 + ((uint128_t) r1) * r1 + (((uint128_t) d4) * (r3 ));
172 t[3] = ((uint128_t) d0) * r3 + ((uint128_t) d1) * r2 + (((uint128_t) r4) * (d419 ));
173 t[4] = ((uint128_t) d0) * r4 + ((uint128_t) d1) * r3 + (((uint128_t) r2) * (r2 ));
175 r0 = (limb)t[0] & 0x7ffffffffffff; c = (limb)(t[0] >> 51);
176 t[1] += c; r1 = (limb)t[1] & 0x7ffffffffffff; c = (limb)(t[1] >> 51);
177 t[2] += c; r2 = (limb)t[2] & 0x7ffffffffffff; c = (limb)(t[2] >> 51);
178 t[3] += c; r3 = (limb)t[3] & 0x7ffffffffffff; c = (limb)(t[3] >> 51);
179 t[4] += c; r4 = (limb)t[4] & 0x7ffffffffffff; c = (limb)(t[4] >> 51);
180 r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffff;
181 r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffff;
182 r2 += c;
183 } while(--count);
185 output[0] = r0;
186 output[1] = r1;
187 output[2] = r2;
188 output[3] = r3;
189 output[4] = r4;
192 /* Take a little-endian, 32-byte number and expand it into polynomial form */
193 static void
194 fexpand(limb *output, const u8 *in) {
195 output[0] = *((const uint64_t *)(in)) & 0x7ffffffffffff;
196 output[1] = (*((const uint64_t *)(in+6)) >> 3) & 0x7ffffffffffff;
197 output[2] = (*((const uint64_t *)(in+12)) >> 6) & 0x7ffffffffffff;
198 output[3] = (*((const uint64_t *)(in+19)) >> 1) & 0x7ffffffffffff;
199 output[4] = (*((const uint64_t *)(in+25)) >> 4) & 0x7ffffffffffff;
202 /* Take a fully reduced polynomial form number and contract it into a
203 * little-endian, 32-byte array
205 static void
206 fcontract(u8 *output, const felem input) {
207 uint128_t t[5];
209 t[0] = input[0];
210 t[1] = input[1];
211 t[2] = input[2];
212 t[3] = input[3];
213 t[4] = input[4];
215 t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff;
216 t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff;
217 t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff;
218 t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff;
219 t[0] += 19 * (t[4] >> 51); t[4] &= 0x7ffffffffffff;
221 t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff;
222 t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff;
223 t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff;
224 t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff;
225 t[0] += 19 * (t[4] >> 51); t[4] &= 0x7ffffffffffff;
227 /* now t is between 0 and 2^255-1, properly carried. */
228 /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
230 t[0] += 19;
232 t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff;
233 t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff;
234 t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff;
235 t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff;
236 t[0] += 19 * (t[4] >> 51); t[4] &= 0x7ffffffffffff;
238 /* now between 19 and 2^255-1 in both cases, and offset by 19. */
240 t[0] += 0x8000000000000 - 19;
241 t[1] += 0x8000000000000 - 1;
242 t[2] += 0x8000000000000 - 1;
243 t[3] += 0x8000000000000 - 1;
244 t[4] += 0x8000000000000 - 1;
246 /* now between 2^255 and 2^256-20, and offset by 2^255. */
248 t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff;
249 t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff;
250 t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff;
251 t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff;
252 t[4] &= 0x7ffffffffffff;
254 *((uint64_t *)(output)) = t[0] | (t[1] << 51);
255 *((uint64_t *)(output+8)) = (t[1] >> 13) | (t[2] << 38);
256 *((uint64_t *)(output+16)) = (t[2] >> 26) | (t[3] << 25);
257 *((uint64_t *)(output+24)) = (t[3] >> 39) | (t[4] << 12);
260 /* Input: Q, Q', Q-Q'
261 * Output: 2Q, Q+Q'
263 * x2 z3: long form
264 * x3 z3: long form
265 * x z: short form, destroyed
266 * xprime zprime: short form, destroyed
267 * qmqp: short form, preserved
269 static void
270 fmonty(limb *x2, limb *z2, /* output 2Q */
271 limb *x3, limb *z3, /* output Q + Q' */
272 limb *x, limb *z, /* input Q */
273 limb *xprime, limb *zprime, /* input Q' */
274 const limb *qmqp /* input Q - Q' */) {
275 limb origx[5], origxprime[5], zzz[5], xx[5], zz[5], xxprime[5],
276 zzprime[5], zzzprime[5];
278 memcpy(origx, x, 5 * sizeof(limb));
279 fsum(x, z);
280 fdifference_backwards(z, origx); // does x - z
282 memcpy(origxprime, xprime, sizeof(limb) * 5);
283 fsum(xprime, zprime);
284 fdifference_backwards(zprime, origxprime);
285 fmul(xxprime, xprime, z);
286 fmul(zzprime, x, zprime);
287 memcpy(origxprime, xxprime, sizeof(limb) * 5);
288 fsum(xxprime, zzprime);
289 fdifference_backwards(zzprime, origxprime);
290 fsquare_times(x3, xxprime, 1);
291 fsquare_times(zzzprime, zzprime, 1);
292 fmul(z3, zzzprime, qmqp);
294 fsquare_times(xx, x, 1);
295 fsquare_times(zz, z, 1);
296 fmul(x2, xx, zz);
297 fdifference_backwards(zz, xx); // does zz = xx - zz
298 fscalar_product(zzz, zz, 121665);
299 fsum(zzz, xx);
300 fmul(z2, zz, zzz);
303 // -----------------------------------------------------------------------------
304 // Maybe swap the contents of two limb arrays (@a and @b), each @len elements
305 // long. Perform the swap iff @swap is non-zero.
307 // This function performs the swap without leaking any side-channel
308 // information.
309 // -----------------------------------------------------------------------------
310 static void
311 swap_conditional(limb a[5], limb b[5], limb iswap) {
312 unsigned i;
313 const limb swap = -iswap;
315 for (i = 0; i < 5; ++i) {
316 const limb x = swap & (a[i] ^ b[i]);
317 a[i] ^= x;
318 b[i] ^= x;
322 /* Calculates nQ where Q is the x-coordinate of a point on the curve
324 * resultx/resultz: the x coordinate of the resulting curve point (short form)
325 * n: a little endian, 32-byte number
326 * q: a point of the curve (short form)
328 static void
329 cmult(limb *resultx, limb *resultz, const u8 *n, const limb *q) {
330 limb a[5] = {0}, b[5] = {1}, c[5] = {1}, d[5] = {0};
331 limb *nqpqx = a, *nqpqz = b, *nqx = c, *nqz = d, *t;
332 limb e[5] = {0}, f[5] = {1}, g[5] = {0}, h[5] = {1};
333 limb *nqpqx2 = e, *nqpqz2 = f, *nqx2 = g, *nqz2 = h;
335 unsigned i, j;
337 memcpy(nqpqx, q, sizeof(limb) * 5);
339 for (i = 0; i < 32; ++i) {
340 u8 byte = n[31 - i];
341 for (j = 0; j < 8; ++j) {
342 const limb bit = byte >> 7;
344 swap_conditional(nqx, nqpqx, bit);
345 swap_conditional(nqz, nqpqz, bit);
346 fmonty(nqx2, nqz2,
347 nqpqx2, nqpqz2,
348 nqx, nqz,
349 nqpqx, nqpqz,
351 swap_conditional(nqx2, nqpqx2, bit);
352 swap_conditional(nqz2, nqpqz2, bit);
354 t = nqx;
355 nqx = nqx2;
356 nqx2 = t;
357 t = nqz;
358 nqz = nqz2;
359 nqz2 = t;
360 t = nqpqx;
361 nqpqx = nqpqx2;
362 nqpqx2 = t;
363 t = nqpqz;
364 nqpqz = nqpqz2;
365 nqpqz2 = t;
367 byte <<= 1;
371 memcpy(resultx, nqx, sizeof(limb) * 5);
372 memcpy(resultz, nqz, sizeof(limb) * 5);
376 // -----------------------------------------------------------------------------
377 // Shamelessly copied from djb's code, tightened a little
378 // -----------------------------------------------------------------------------
379 static void
380 crecip(felem out, const felem z) {
381 felem a,t0,b,c;
383 /* 2 */ fsquare_times(a, z, 1); // a = 2
384 /* 8 */ fsquare_times(t0, a, 2);
385 /* 9 */ fmul(b, t0, z); // b = 9
386 /* 11 */ fmul(a, b, a); // a = 11
387 /* 22 */ fsquare_times(t0, a, 1);
388 /* 2^5 - 2^0 = 31 */ fmul(b, t0, b);
389 /* 2^10 - 2^5 */ fsquare_times(t0, b, 5);
390 /* 2^10 - 2^0 */ fmul(b, t0, b);
391 /* 2^20 - 2^10 */ fsquare_times(t0, b, 10);
392 /* 2^20 - 2^0 */ fmul(c, t0, b);
393 /* 2^40 - 2^20 */ fsquare_times(t0, c, 20);
394 /* 2^40 - 2^0 */ fmul(t0, t0, c);
395 /* 2^50 - 2^10 */ fsquare_times(t0, t0, 10);
396 /* 2^50 - 2^0 */ fmul(b, t0, b);
397 /* 2^100 - 2^50 */ fsquare_times(t0, b, 50);
398 /* 2^100 - 2^0 */ fmul(c, t0, b);
399 /* 2^200 - 2^100 */ fsquare_times(t0, c, 100);
400 /* 2^200 - 2^0 */ fmul(t0, t0, c);
401 /* 2^250 - 2^50 */ fsquare_times(t0, t0, 50);
402 /* 2^250 - 2^0 */ fmul(t0, t0, b);
403 /* 2^255 - 2^5 */ fsquare_times(t0, t0, 5);
404 /* 2^255 - 21 */ fmul(out, t0, a);
408 crypto_scalarmult(u8 *mypublic, const u8 *secret, const u8 *basepoint) {
409 limb bp[5], x[5], z[5], zmone[5];
410 uint8_t e[32];
411 int i;
413 for (i = 0;i < 32;++i) e[i] = secret[i];
414 e[0] &= 248;
415 e[31] &= 127;
416 e[31] |= 64;
418 fexpand(bp, basepoint);
419 cmult(x, z, e, bp);
420 crecip(zmone, z);
421 fmul(z, x, zmone);
422 fcontract(mypublic, z);
423 return 0;
426 #endif