]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libsec/port/curve25519.c
libsec: generalize pbkdf2_hmac_sha1() to pbkdf2_x() passing the hmac as an argument
[plan9front.git] / sys / src / libsec / port / curve25519.c
1 /* Copyright 2008, Google Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * curve25519: Curve25519 elliptic curve, public key function
31  *
32  * http://code.google.com/p/curve25519-donna/
33  *
34  * Adam Langley <agl@imperialviolet.org>
35  *
36  * Derived from public domain C code by Daniel J. Bernstein <djb@cr.yp.to>
37  *
38  * More information about curve25519 can be found here
39  *   http://cr.yp.to/ecdh.html
40  *
41  * djb's sample implementation of curve25519 is written in a special assembly
42  * language called qhasm and uses the floating point registers.
43  *
44  * This is, almost, a clean room reimplementation from the curve25519 paper. It
45  * uses many of the tricks described therein. Only the crecip function is taken
46  * from the sample implementation.
47  */
48 #include <u.h>
49 #include <libc.h>
50 #include <libsec.h>
51
52 typedef vlong felem;
53
54 /* Sum two numbers: output += in */
55 static void fsum(felem *output, felem *in) {
56   unsigned i;
57   for (i = 0; i < 10; i += 2) {
58     output[0+i] = (output[0+i] + in[0+i]);
59     output[1+i] = (output[1+i] + in[1+i]);
60   }
61 }
62
63 /* Find the difference of two numbers: output = in - output
64  * (note the order of the arguments!)
65  */
66 static void fdifference(felem *output, felem *in) {
67   unsigned i;
68   for (i = 0; i < 10; ++i) {
69     output[i] = (in[i] - output[i]);
70   }
71 }
72
73 /* Multiply a number my a scalar: output = in * scalar */
74 static void fscalar_product(felem *output, felem *in, felem scalar) {
75   unsigned i;
76   for (i = 0; i < 10; ++i) {
77     output[i] = in[i] * scalar;
78   }
79 }
80
81 /* Multiply two numbers: output = in2 * in
82  *
83  * output must be distinct to both inputs. The inputs are reduced coefficient
84  * form, the output is not.
85  */
86 static void fproduct(felem *output, felem *in2, felem *in) {
87   output[0] =      in2[0] * in[0];
88   output[1] =      in2[0] * in[1] +
89                    in2[1] * in[0];
90   output[2] =  2 * in2[1] * in[1] +
91                    in2[0] * in[2] +
92                    in2[2] * in[0];
93   output[3] =      in2[1] * in[2] +
94                    in2[2] * in[1] +
95                    in2[0] * in[3] +
96                    in2[3] * in[0];
97   output[4] =      in2[2] * in[2] +
98                2 * (in2[1] * in[3] +
99                     in2[3] * in[1]) +
100                    in2[0] * in[4] +
101                    in2[4] * in[0];
102   output[5] =      in2[2] * in[3] +
103                    in2[3] * in[2] +
104                    in2[1] * in[4] +
105                    in2[4] * in[1] +
106                    in2[0] * in[5] +
107                    in2[5] * in[0];
108   output[6] =  2 * (in2[3] * in[3] +
109                     in2[1] * in[5] +
110                     in2[5] * in[1]) +
111                    in2[2] * in[4] +
112                    in2[4] * in[2] +
113                    in2[0] * in[6] +
114                    in2[6] * in[0];
115   output[7] =      in2[3] * in[4] +
116                    in2[4] * in[3] +
117                    in2[2] * in[5] +
118                    in2[5] * in[2] +
119                    in2[1] * in[6] +
120                    in2[6] * in[1] +
121                    in2[0] * in[7] +
122                    in2[7] * in[0];
123   output[8] =      in2[4] * in[4] +
124                2 * (in2[3] * in[5] +
125                     in2[5] * in[3] +
126                     in2[1] * in[7] +
127                     in2[7] * in[1]) +
128                    in2[2] * in[6] +
129                    in2[6] * in[2] +
130                    in2[0] * in[8] +
131                    in2[8] * in[0];
132   output[9] =      in2[4] * in[5] +
133                    in2[5] * in[4] +
134                    in2[3] * in[6] +
135                    in2[6] * in[3] +
136                    in2[2] * in[7] +
137                    in2[7] * in[2] +
138                    in2[1] * in[8] +
139                    in2[8] * in[1] +
140                    in2[0] * in[9] +
141                    in2[9] * in[0];
142   output[10] = 2 * (in2[5] * in[5] +
143                     in2[3] * in[7] +
144                     in2[7] * in[3] +
145                     in2[1] * in[9] +
146                     in2[9] * in[1]) +
147                    in2[4] * in[6] +
148                    in2[6] * in[4] +
149                    in2[2] * in[8] +
150                    in2[8] * in[2];
151   output[11] =     in2[5] * in[6] +
152                    in2[6] * in[5] +
153                    in2[4] * in[7] +
154                    in2[7] * in[4] +
155                    in2[3] * in[8] +
156                    in2[8] * in[3] +
157                    in2[2] * in[9] +
158                    in2[9] * in[2];
159   output[12] =     in2[6] * in[6] +
160                2 * (in2[5] * in[7] +
161                     in2[7] * in[5] +
162                     in2[3] * in[9] +
163                     in2[9] * in[3]) +
164                    in2[4] * in[8] +
165                    in2[8] * in[4];
166   output[13] =     in2[6] * in[7] +
167                    in2[7] * in[6] +
168                    in2[5] * in[8] +
169                    in2[8] * in[5] +
170                    in2[4] * in[9] +
171                    in2[9] * in[4];
172   output[14] = 2 * (in2[7] * in[7] +
173                     in2[5] * in[9] +
174                     in2[9] * in[5]) +
175                    in2[6] * in[8] +
176                    in2[8] * in[6];
177   output[15] =     in2[7] * in[8] +
178                    in2[8] * in[7] +
179                    in2[6] * in[9] +
180                    in2[9] * in[6];
181   output[16] =     in2[8] * in[8] +
182                2 * (in2[7] * in[9] +
183                     in2[9] * in[7]);
184   output[17] =     in2[8] * in[9] +
185                    in2[9] * in[8];
186   output[18] = 2 * in2[9] * in[9];
187 }
188
189 /* Reduce a long form to a short form by taking the input mod 2^255 - 19. */
190 static void freduce_degree(felem *output) {
191   output[8] += 19 * output[18];
192   output[7] += 19 * output[17];
193   output[6] += 19 * output[16];
194   output[5] += 19 * output[15];
195   output[4] += 19 * output[14];
196   output[3] += 19 * output[13];
197   output[2] += 19 * output[12];
198   output[1] += 19 * output[11];
199   output[0] += 19 * output[10];
200 }
201
202 /* Reduce all coefficients of the short form input to be -2**25 <= x <= 2**25
203  */
204 static void freduce_coefficients(felem *output) {
205   unsigned i;
206   do {
207     output[10] = 0;
208
209     for (i = 0; i < 10; i += 2) {
210       felem over = output[i] / 0x2000000l;
211       felem over2 = (over + ((over >> 63) * 2) + 1) / 2;
212       output[i+1] += over2;
213       output[i] -= over2 * 0x4000000l;
214
215       over = output[i+1] / 0x2000000;
216       output[i+2] += over;
217       output[i+1] -= over * 0x2000000;
218     }
219     output[0] += 19 * output[10];
220   } while (output[10]);
221 }
222
223 /* A helpful wrapper around fproduct: output = in * in2.
224  *
225  * output must be distinct to both inputs. The output is reduced degree and
226  * reduced coefficient.
227  */
228 static void
229 fmul(felem *output, felem *in, felem *in2) {
230   felem t[19];
231   fproduct(t, in, in2);
232   freduce_degree(t);
233   freduce_coefficients(t);
234   memcpy(output, t, sizeof(felem) * 10);
235 }
236
237 static void fsquare_inner(felem *output, felem *in) {
238   felem tmp;
239   output[0] =      in[0] * in[0];
240   output[1] =  2 * in[0] * in[1];
241   output[2] =  2 * (in[1] * in[1] +
242                     in[0] * in[2]);
243   output[3] =  2 * (in[1] * in[2] +
244                     in[0] * in[3]);
245   output[4] =      in[2] * in[2] +
246                4 * in[1] * in[3] +
247                2 * in[0] * in[4];
248   output[5] =  2 * (in[2] * in[3] +
249                     in[1] * in[4] +
250                     in[0] * in[5]);
251   output[6] =  2 * (in[3] * in[3] +
252                     in[2] * in[4] +
253                     in[0] * in[6] +
254                 2 * in[1] * in[5]);
255   output[7] =  2 * (in[3] * in[4] +
256                     in[2] * in[5] +
257                     in[1] * in[6] +
258                     in[0] * in[7]);
259   tmp = in[1] * in[7] + in[3] * in[5];
260   output[8] =      in[4] * in[4] +
261                2 * (in[2] * in[6] +
262                     in[0] * in[8] +
263                         2 * tmp);
264   output[9] =  2 * (in[4] * in[5] +
265                     in[3] * in[6] +
266                     in[2] * in[7] +
267                     in[1] * in[8] +
268                     in[0] * in[9]);
269   tmp = in[3] * in[7] + in[1] * in[9];
270   output[10] = 2 * (in[5] * in[5] +
271                    in[4] * in[6] +
272                    in[2] * in[8] +
273                        2 * tmp);
274   output[11] = 2 * (in[5] * in[6] +
275                     in[4] * in[7] +
276                     in[3] * in[8] +
277                     in[2] * in[9]);
278   output[12] =     in[6] * in[6] +
279                2 * (in[4] * in[8] +
280                 2 * (in[5] * in[7] +
281                      in[3] * in[9]));
282   output[13] = 2 * (in[6] * in[7] +
283                     in[5] * in[8] +
284                     in[4] * in[9]);
285   output[14] = 2 * (in[7] * in[7] +
286                     in[6] * in[8] +
287                 2 * in[5] * in[9]);
288   output[15] = 2 * (in[7] * in[8] +
289                     in[6] * in[9]);
290   output[16] =     in[8] * in[8] +
291                4 * in[7] * in[9];
292   output[17] = 2 * in[8] * in[9];
293   output[18] = 2 * in[9] * in[9];
294 }
295
296 static void
297 fsquare(felem *output, felem *in) {
298   felem t[19];
299   fsquare_inner(t, in);
300   freduce_degree(t);
301   freduce_coefficients(t);
302   memcpy(output, t, sizeof(felem) * 10);
303 }
304
305 /* Take a little-endian, 32-byte number and expand it into polynomial form */
306 static void
307 fexpand(felem *output, uchar *input) {
308 #define F(n,start,shift,mask) \
309   output[n] = ((((felem) input[start + 0]) | \
310                 ((felem) input[start + 1]) << 8 | \
311                 ((felem) input[start + 2]) << 16 | \
312                 ((felem) input[start + 3]) << 24) >> shift) & mask;
313   F(0, 0, 0, 0x3ffffff);
314   F(1, 3, 2, 0x1ffffff);
315   F(2, 6, 3, 0x3ffffff);
316   F(3, 9, 5, 0x1ffffff);
317   F(4, 12, 6, 0x3ffffff);
318   F(5, 16, 0, 0x1ffffff);
319   F(6, 19, 1, 0x3ffffff);
320   F(7, 22, 3, 0x1ffffff);
321   F(8, 25, 4, 0x3ffffff);
322   F(9, 28, 6, 0x1ffffff);
323 #undef F
324 }
325
326 /* Take a fully reduced polynomial form number and contract it into a
327  * little-endian, 32-byte array
328  */
329 static void
330 fcontract(uchar *output, felem *input) {
331   int i;
332
333   do {
334     for (i = 0; i < 9; ++i) {
335       if ((i & 1) == 1) {
336         while (input[i] < 0) {
337           input[i] += 0x2000000;
338           input[i + 1]--;
339         }
340       } else {
341         while (input[i] < 0) {
342           input[i] += 0x4000000;
343           input[i + 1]--;
344         }
345       }
346     }
347     while (input[9] < 0) {
348       input[9] += 0x2000000;
349       input[0] -= 19;
350     }
351   } while (input[0] < 0);
352
353   input[1] <<= 2;
354   input[2] <<= 3;
355   input[3] <<= 5;
356   input[4] <<= 6;
357   input[6] <<= 1;
358   input[7] <<= 3;
359   input[8] <<= 4;
360   input[9] <<= 6;
361 #define F(i, s) \
362   output[s+0] |=  input[i] & 0xff; \
363   output[s+1]  = (input[i] >> 8) & 0xff; \
364   output[s+2]  = (input[i] >> 16) & 0xff; \
365   output[s+3]  = (input[i] >> 24) & 0xff;
366   output[0] = 0;
367   output[16] = 0;
368   F(0,0);
369   F(1,3);
370   F(2,6);
371   F(3,9);
372   F(4,12);
373   F(5,16);
374   F(6,19);
375   F(7,22);
376   F(8,25);
377   F(9,28);
378 #undef F
379 }
380
381 /* Input: Q, Q', Q-Q'
382  * Output: 2Q, Q+Q'
383  *
384  *   x2 z3: long form
385  *   x3 z3: long form
386  *   x z: short form, destroyed
387  *   xprime zprime: short form, destroyed
388  *   qmqp: short form, preserved
389  */
390 static void fmonty(felem *x2, felem *z2,  /* output 2Q */
391                    felem *x3, felem *z3,  /* output Q + Q' */
392                    felem *x, felem *z,    /* input Q */
393                    felem *xprime, felem *zprime,  /* input Q' */
394                    felem *qmqp /* input Q - Q' */) {
395   felem origx[10], origxprime[10], zzz[19], xx[19], zz[19], xxprime[19],
396         zzprime[19], zzzprime[19], xxxprime[19];
397
398   memcpy(origx, x, 10 * sizeof(felem));
399   fsum(x, z);
400   fdifference(z, origx);  // does x - z
401
402   memcpy(origxprime, xprime, sizeof(felem) * 10);
403   fsum(xprime, zprime);
404   fdifference(zprime, origxprime);
405   fproduct(xxprime, xprime, z);
406   fproduct(zzprime, x, zprime);
407   freduce_degree(xxprime);
408   freduce_coefficients(xxprime);
409   freduce_degree(zzprime);
410   freduce_coefficients(zzprime);
411   memcpy(origxprime, xxprime, sizeof(felem) * 10);
412   fsum(xxprime, zzprime);
413   fdifference(zzprime, origxprime);
414   fsquare(xxxprime, xxprime);
415   fsquare(zzzprime, zzprime);
416   fproduct(zzprime, zzzprime, qmqp);
417   freduce_degree(zzprime);
418   freduce_coefficients(zzprime);
419   memcpy(x3, xxxprime, sizeof(felem) * 10);
420   memcpy(z3, zzprime, sizeof(felem) * 10);
421
422   fsquare(xx, x);
423   fsquare(zz, z);
424   fproduct(x2, xx, zz);
425   freduce_degree(x2);
426   freduce_coefficients(x2);
427   fdifference(zz, xx);  // does zz = xx - zz
428   memset(zzz + 10, 0, sizeof(felem) * 9);
429   fscalar_product(zzz, zz, 121665);
430   freduce_degree(zzz);
431   freduce_coefficients(zzz);
432   fsum(zzz, xx);
433   fproduct(z2, zz, zzz);
434   freduce_degree(z2);
435   freduce_coefficients(z2);
436 }
437
438 /* Calculates nQ where Q is the x-coordinate of a point on the curve
439  *
440  *   resultx/resultz: the x coordinate of the resulting curve point (short form)
441  *   n: a little endian, 32-byte number
442  *   q: a point of the curve (short form)
443  */
444 static void
445 cmult(felem *resultx, felem *resultz, uchar *n, felem *q) {
446   felem a[19] = {0}, b[19] = {1}, c[19] = {1}, d[19] = {0};
447   felem *nqpqx = a, *nqpqz = b, *nqx = c, *nqz = d, *t;
448   felem e[19] = {0}, f[19] = {1}, g[19] = {0}, h[19] = {1};
449   felem *nqpqx2 = e, *nqpqz2 = f, *nqx2 = g, *nqz2 = h;
450
451   unsigned i, j;
452
453   memcpy(nqpqx, q, sizeof(felem) * 10);
454
455   for (i = 0; i < 32; ++i) {
456     uchar byte = n[31 - i];
457     for (j = 0; j < 8; ++j) {
458       if (byte & 0x80) {
459         fmonty(nqpqx2, nqpqz2,
460                nqx2, nqz2,
461                nqpqx, nqpqz,
462                nqx, nqz,
463                q);
464       } else {
465         fmonty(nqx2, nqz2,
466                nqpqx2, nqpqz2,
467                nqx, nqz,
468                nqpqx, nqpqz,
469                q);
470       }
471
472       t = nqx;
473       nqx = nqx2;
474       nqx2 = t;
475       t = nqz;
476       nqz = nqz2;
477       nqz2 = t;
478       t = nqpqx;
479       nqpqx = nqpqx2;
480       nqpqx2 = t;
481       t = nqpqz;
482       nqpqz = nqpqz2;
483       nqpqz2 = t;
484
485       byte <<= 1;
486     }
487   }
488
489   memcpy(resultx, nqx, sizeof(felem) * 10);
490   memcpy(resultz, nqz, sizeof(felem) * 10);
491 }
492
493 // -----------------------------------------------------------------------------
494 // Shamelessly copied from djb's code
495 // -----------------------------------------------------------------------------
496 static void
497 crecip(felem *out, felem *z) {
498   felem z2[10];
499   felem z9[10];
500   felem z11[10];
501   felem z2_5_0[10];
502   felem z2_10_0[10];
503   felem z2_20_0[10];
504   felem z2_50_0[10];
505   felem z2_100_0[10];
506   felem t0[10];
507   felem t1[10];
508   int i;
509
510   /* 2 */ fsquare(z2,z);
511   /* 4 */ fsquare(t1,z2);
512   /* 8 */ fsquare(t0,t1);
513   /* 9 */ fmul(z9,t0,z);
514   /* 11 */ fmul(z11,z9,z2);
515   /* 22 */ fsquare(t0,z11);
516   /* 2^5 - 2^0 = 31 */ fmul(z2_5_0,t0,z9);
517
518   /* 2^6 - 2^1 */ fsquare(t0,z2_5_0);
519   /* 2^7 - 2^2 */ fsquare(t1,t0);
520   /* 2^8 - 2^3 */ fsquare(t0,t1);
521   /* 2^9 - 2^4 */ fsquare(t1,t0);
522   /* 2^10 - 2^5 */ fsquare(t0,t1);
523   /* 2^10 - 2^0 */ fmul(z2_10_0,t0,z2_5_0);
524
525   /* 2^11 - 2^1 */ fsquare(t0,z2_10_0);
526   /* 2^12 - 2^2 */ fsquare(t1,t0);
527   /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
528   /* 2^20 - 2^0 */ fmul(z2_20_0,t1,z2_10_0);
529
530   /* 2^21 - 2^1 */ fsquare(t0,z2_20_0);
531   /* 2^22 - 2^2 */ fsquare(t1,t0);
532   /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
533   /* 2^40 - 2^0 */ fmul(t0,t1,z2_20_0);
534
535   /* 2^41 - 2^1 */ fsquare(t1,t0);
536   /* 2^42 - 2^2 */ fsquare(t0,t1);
537   /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t1,t0); fsquare(t0,t1); }
538   /* 2^50 - 2^0 */ fmul(z2_50_0,t0,z2_10_0);
539
540   /* 2^51 - 2^1 */ fsquare(t0,z2_50_0);
541   /* 2^52 - 2^2 */ fsquare(t1,t0);
542   /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
543   /* 2^100 - 2^0 */ fmul(z2_100_0,t1,z2_50_0);
544
545   /* 2^101 - 2^1 */ fsquare(t1,z2_100_0);
546   /* 2^102 - 2^2 */ fsquare(t0,t1);
547   /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { fsquare(t1,t0); fsquare(t0,t1); }
548   /* 2^200 - 2^0 */ fmul(t1,t0,z2_100_0);
549
550   /* 2^201 - 2^1 */ fsquare(t0,t1);
551   /* 2^202 - 2^2 */ fsquare(t1,t0);
552   /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); }
553   /* 2^250 - 2^0 */ fmul(t0,t1,z2_50_0);
554
555   /* 2^251 - 2^1 */ fsquare(t1,t0);
556   /* 2^252 - 2^2 */ fsquare(t0,t1);
557   /* 2^253 - 2^3 */ fsquare(t1,t0);
558   /* 2^254 - 2^4 */ fsquare(t0,t1);
559   /* 2^255 - 2^5 */ fsquare(t1,t0);
560   /* 2^255 - 21 */ fmul(out,t1,z11);
561 }
562
563 void
564 curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]) {
565   felem bp[10], x[10], z[10], zmone[10];
566   fexpand(bp, basepoint);
567   cmult(x, z, secret, bp);
568   crecip(zmone, z);
569   fmul(z, x, zmone);
570   fcontract(mypublic, z);
571 }