new functionality from cinap including dh and ecc stuff. Reference: /n/atom/patch/applied/secadditions Date: Sun Sep 20 22:43:11 CES 2015 Signed-off-by: quanstro@quanstro.net --- /sys/src/libsec/port/curve25519_dh.c Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/curve25519_dh.c Sun Sep 20 22:42:15 2015 @@ -0,0 +1,34 @@ +#include "os.h" +#include +#include + +static uchar nine[32] = {9}; + +void +curve25519_dh_new(uchar x[32], uchar y[32]) +{ + uchar b; + + /* new public/private key pair */ + genrandom(x, 32); + b = x[31]; + x[0] &= ~7; /* clear bit 0,1,2 */ + x[31] = 0x40 | (b & 0x7f); /* set bit 254, clear bit 255 */ + curve25519(y, x, nine); + + /* bit 255 is always 0, so make it random */ + y[31] |= b & 0x80; +} + +void +curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]) +{ + /* remove the random bit */ + y[31] &= 0x7f; + + /* calculate dhx key */ + curve25519(z, x, y); + + memset(x, 0, 32); + memset(y, 0, 32); +} --- /sys/src/libsec/port/curve25519.c Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/curve25519.c Sun Sep 20 22:42:16 2015 @@ -0,0 +1,571 @@ +/* Copyright 2008, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * curve25519: Curve25519 elliptic curve, public key function + * + * http://code.google.com/p/curve25519-donna/ + * + * Adam Langley + * + * Derived from public domain C code by Daniel J. Bernstein + * + * More information about curve25519 can be found here + * http://cr.yp.to/ecdh.html + * + * djb's sample implementation of curve25519 is written in a special assembly + * language called qhasm and uses the floating point registers. + * + * This is, almost, a clean room reimplementation from the curve25519 paper. It + * uses many of the tricks described therein. Only the crecip function is taken + * from the sample implementation. + */ +#include +#include +#include + +typedef vlong felem; + +/* Sum two numbers: output += in */ +static void fsum(felem *output, felem *in) { + unsigned i; + for (i = 0; i < 10; i += 2) { + output[0+i] = (output[0+i] + in[0+i]); + output[1+i] = (output[1+i] + in[1+i]); + } +} + +/* Find the difference of two numbers: output = in - output + * (note the order of the arguments!) + */ +static void fdifference(felem *output, felem *in) { + unsigned i; + for (i = 0; i < 10; ++i) { + output[i] = (in[i] - output[i]); + } +} + +/* Multiply a number my a scalar: output = in * scalar */ +static void fscalar_product(felem *output, felem *in, felem scalar) { + unsigned i; + for (i = 0; i < 10; ++i) { + output[i] = in[i] * scalar; + } +} + +/* Multiply two numbers: output = in2 * in + * + * output must be distinct to both inputs. The inputs are reduced coefficient + * form, the output is not. + */ +static void fproduct(felem *output, felem *in2, felem *in) { + output[0] = in2[0] * in[0]; + output[1] = in2[0] * in[1] + + in2[1] * in[0]; + output[2] = 2 * in2[1] * in[1] + + in2[0] * in[2] + + in2[2] * in[0]; + output[3] = in2[1] * in[2] + + in2[2] * in[1] + + in2[0] * in[3] + + in2[3] * in[0]; + output[4] = in2[2] * in[2] + + 2 * (in2[1] * in[3] + + in2[3] * in[1]) + + in2[0] * in[4] + + in2[4] * in[0]; + output[5] = in2[2] * in[3] + + in2[3] * in[2] + + in2[1] * in[4] + + in2[4] * in[1] + + in2[0] * in[5] + + in2[5] * in[0]; + output[6] = 2 * (in2[3] * in[3] + + in2[1] * in[5] + + in2[5] * in[1]) + + in2[2] * in[4] + + in2[4] * in[2] + + in2[0] * in[6] + + in2[6] * in[0]; + output[7] = in2[3] * in[4] + + in2[4] * in[3] + + in2[2] * in[5] + + in2[5] * in[2] + + in2[1] * in[6] + + in2[6] * in[1] + + in2[0] * in[7] + + in2[7] * in[0]; + output[8] = in2[4] * in[4] + + 2 * (in2[3] * in[5] + + in2[5] * in[3] + + in2[1] * in[7] + + in2[7] * in[1]) + + in2[2] * in[6] + + in2[6] * in[2] + + in2[0] * in[8] + + in2[8] * in[0]; + output[9] = in2[4] * in[5] + + in2[5] * in[4] + + in2[3] * in[6] + + in2[6] * in[3] + + in2[2] * in[7] + + in2[7] * in[2] + + in2[1] * in[8] + + in2[8] * in[1] + + in2[0] * in[9] + + in2[9] * in[0]; + output[10] = 2 * (in2[5] * in[5] + + in2[3] * in[7] + + in2[7] * in[3] + + in2[1] * in[9] + + in2[9] * in[1]) + + in2[4] * in[6] + + in2[6] * in[4] + + in2[2] * in[8] + + in2[8] * in[2]; + output[11] = in2[5] * in[6] + + in2[6] * in[5] + + in2[4] * in[7] + + in2[7] * in[4] + + in2[3] * in[8] + + in2[8] * in[3] + + in2[2] * in[9] + + in2[9] * in[2]; + output[12] = in2[6] * in[6] + + 2 * (in2[5] * in[7] + + in2[7] * in[5] + + in2[3] * in[9] + + in2[9] * in[3]) + + in2[4] * in[8] + + in2[8] * in[4]; + output[13] = in2[6] * in[7] + + in2[7] * in[6] + + in2[5] * in[8] + + in2[8] * in[5] + + in2[4] * in[9] + + in2[9] * in[4]; + output[14] = 2 * (in2[7] * in[7] + + in2[5] * in[9] + + in2[9] * in[5]) + + in2[6] * in[8] + + in2[8] * in[6]; + output[15] = in2[7] * in[8] + + in2[8] * in[7] + + in2[6] * in[9] + + in2[9] * in[6]; + output[16] = in2[8] * in[8] + + 2 * (in2[7] * in[9] + + in2[9] * in[7]); + output[17] = in2[8] * in[9] + + in2[9] * in[8]; + output[18] = 2 * in2[9] * in[9]; +} + +/* Reduce a long form to a short form by taking the input mod 2^255 - 19. */ +static void freduce_degree(felem *output) { + output[8] += 19 * output[18]; + output[7] += 19 * output[17]; + output[6] += 19 * output[16]; + output[5] += 19 * output[15]; + output[4] += 19 * output[14]; + output[3] += 19 * output[13]; + output[2] += 19 * output[12]; + output[1] += 19 * output[11]; + output[0] += 19 * output[10]; +} + +/* Reduce all coefficients of the short form input to be -2**25 <= x <= 2**25 + */ +static void freduce_coefficients(felem *output) { + unsigned i; + do { + output[10] = 0; + + for (i = 0; i < 10; i += 2) { + felem over = output[i] / 0x2000000l; + felem over2 = (over + ((over >> 63) * 2) + 1) / 2; + output[i+1] += over2; + output[i] -= over2 * 0x4000000l; + + over = output[i+1] / 0x2000000; + output[i+2] += over; + output[i+1] -= over * 0x2000000; + } + output[0] += 19 * output[10]; + } while (output[10]); +} + +/* A helpful wrapper around fproduct: output = in * in2. + * + * output must be distinct to both inputs. The output is reduced degree and + * reduced coefficient. + */ +static void +fmul(felem *output, felem *in, felem *in2) { + felem t[19]; + fproduct(t, in, in2); + freduce_degree(t); + freduce_coefficients(t); + memcpy(output, t, sizeof(felem) * 10); +} + +static void fsquare_inner(felem *output, felem *in) { + felem tmp; + output[0] = in[0] * in[0]; + output[1] = 2 * in[0] * in[1]; + output[2] = 2 * (in[1] * in[1] + + in[0] * in[2]); + output[3] = 2 * (in[1] * in[2] + + in[0] * in[3]); + output[4] = in[2] * in[2] + + 4 * in[1] * in[3] + + 2 * in[0] * in[4]; + output[5] = 2 * (in[2] * in[3] + + in[1] * in[4] + + in[0] * in[5]); + output[6] = 2 * (in[3] * in[3] + + in[2] * in[4] + + in[0] * in[6] + + 2 * in[1] * in[5]); + output[7] = 2 * (in[3] * in[4] + + in[2] * in[5] + + in[1] * in[6] + + in[0] * in[7]); + tmp = in[1] * in[7] + in[3] * in[5]; + output[8] = in[4] * in[4] + + 2 * (in[2] * in[6] + + in[0] * in[8] + + 2 * tmp); + output[9] = 2 * (in[4] * in[5] + + in[3] * in[6] + + in[2] * in[7] + + in[1] * in[8] + + in[0] * in[9]); + tmp = in[3] * in[7] + in[1] * in[9]; + output[10] = 2 * (in[5] * in[5] + + in[4] * in[6] + + in[2] * in[8] + + 2 * tmp); + output[11] = 2 * (in[5] * in[6] + + in[4] * in[7] + + in[3] * in[8] + + in[2] * in[9]); + output[12] = in[6] * in[6] + + 2 * (in[4] * in[8] + + 2 * (in[5] * in[7] + + in[3] * in[9])); + output[13] = 2 * (in[6] * in[7] + + in[5] * in[8] + + in[4] * in[9]); + output[14] = 2 * (in[7] * in[7] + + in[6] * in[8] + + 2 * in[5] * in[9]); + output[15] = 2 * (in[7] * in[8] + + in[6] * in[9]); + output[16] = in[8] * in[8] + + 4 * in[7] * in[9]; + output[17] = 2 * in[8] * in[9]; + output[18] = 2 * in[9] * in[9]; +} + +static void +fsquare(felem *output, felem *in) { + felem t[19]; + fsquare_inner(t, in); + freduce_degree(t); + freduce_coefficients(t); + memcpy(output, t, sizeof(felem) * 10); +} + +/* Take a little-endian, 32-byte number and expand it into polynomial form */ +static void +fexpand(felem *output, uchar *input) { +#define F(n,start,shift,mask) \ + output[n] = ((((felem) input[start + 0]) | \ + ((felem) input[start + 1]) << 8 | \ + ((felem) input[start + 2]) << 16 | \ + ((felem) input[start + 3]) << 24) >> shift) & mask; + F(0, 0, 0, 0x3ffffff); + F(1, 3, 2, 0x1ffffff); + F(2, 6, 3, 0x3ffffff); + F(3, 9, 5, 0x1ffffff); + F(4, 12, 6, 0x3ffffff); + F(5, 16, 0, 0x1ffffff); + F(6, 19, 1, 0x3ffffff); + F(7, 22, 3, 0x1ffffff); + F(8, 25, 4, 0x3ffffff); + F(9, 28, 6, 0x1ffffff); +#undef F +} + +/* Take a fully reduced polynomial form number and contract it into a + * little-endian, 32-byte array + */ +static void +fcontract(uchar *output, felem *input) { + int i; + + do { + for (i = 0; i < 9; ++i) { + if ((i & 1) == 1) { + while (input[i] < 0) { + input[i] += 0x2000000; + input[i + 1]--; + } + } else { + while (input[i] < 0) { + input[i] += 0x4000000; + input[i + 1]--; + } + } + } + while (input[9] < 0) { + input[9] += 0x2000000; + input[0] -= 19; + } + } while (input[0] < 0); + + input[1] <<= 2; + input[2] <<= 3; + input[3] <<= 5; + input[4] <<= 6; + input[6] <<= 1; + input[7] <<= 3; + input[8] <<= 4; + input[9] <<= 6; +#define F(i, s) \ + output[s+0] |= input[i] & 0xff; \ + output[s+1] = (input[i] >> 8) & 0xff; \ + output[s+2] = (input[i] >> 16) & 0xff; \ + output[s+3] = (input[i] >> 24) & 0xff; + output[0] = 0; + output[16] = 0; + F(0,0); + F(1,3); + F(2,6); + F(3,9); + F(4,12); + F(5,16); + F(6,19); + F(7,22); + F(8,25); + F(9,28); +#undef F +} + +/* Input: Q, Q', Q-Q' + * Output: 2Q, Q+Q' + * + * x2 z3: long form + * x3 z3: long form + * x z: short form, destroyed + * xprime zprime: short form, destroyed + * qmqp: short form, preserved + */ +static void fmonty(felem *x2, felem *z2, /* output 2Q */ + felem *x3, felem *z3, /* output Q + Q' */ + felem *x, felem *z, /* input Q */ + felem *xprime, felem *zprime, /* input Q' */ + felem *qmqp /* input Q - Q' */) { + felem origx[10], origxprime[10], zzz[19], xx[19], zz[19], xxprime[19], + zzprime[19], zzzprime[19], xxxprime[19]; + + memcpy(origx, x, 10 * sizeof(felem)); + fsum(x, z); + fdifference(z, origx); // does x - z + + memcpy(origxprime, xprime, sizeof(felem) * 10); + fsum(xprime, zprime); + fdifference(zprime, origxprime); + fproduct(xxprime, xprime, z); + fproduct(zzprime, x, zprime); + freduce_degree(xxprime); + freduce_coefficients(xxprime); + freduce_degree(zzprime); + freduce_coefficients(zzprime); + memcpy(origxprime, xxprime, sizeof(felem) * 10); + fsum(xxprime, zzprime); + fdifference(zzprime, origxprime); + fsquare(xxxprime, xxprime); + fsquare(zzzprime, zzprime); + fproduct(zzprime, zzzprime, qmqp); + freduce_degree(zzprime); + freduce_coefficients(zzprime); + memcpy(x3, xxxprime, sizeof(felem) * 10); + memcpy(z3, zzprime, sizeof(felem) * 10); + + fsquare(xx, x); + fsquare(zz, z); + fproduct(x2, xx, zz); + freduce_degree(x2); + freduce_coefficients(x2); + fdifference(zz, xx); // does zz = xx - zz + memset(zzz + 10, 0, sizeof(felem) * 9); + fscalar_product(zzz, zz, 121665); + freduce_degree(zzz); + freduce_coefficients(zzz); + fsum(zzz, xx); + fproduct(z2, zz, zzz); + freduce_degree(z2); + freduce_coefficients(z2); +} + +/* Calculates nQ where Q is the x-coordinate of a point on the curve + * + * resultx/resultz: the x coordinate of the resulting curve point (short form) + * n: a little endian, 32-byte number + * q: a point of the curve (short form) + */ +static void +cmult(felem *resultx, felem *resultz, uchar *n, felem *q) { + felem a[19] = {0}, b[19] = {1}, c[19] = {1}, d[19] = {0}; + felem *nqpqx = a, *nqpqz = b, *nqx = c, *nqz = d, *t; + felem e[19] = {0}, f[19] = {1}, g[19] = {0}, h[19] = {1}; + felem *nqpqx2 = e, *nqpqz2 = f, *nqx2 = g, *nqz2 = h; + + unsigned i, j; + + memcpy(nqpqx, q, sizeof(felem) * 10); + + for (i = 0; i < 32; ++i) { + uchar byte = n[31 - i]; + for (j = 0; j < 8; ++j) { + if (byte & 0x80) { + fmonty(nqpqx2, nqpqz2, + nqx2, nqz2, + nqpqx, nqpqz, + nqx, nqz, + q); + } else { + fmonty(nqx2, nqz2, + nqpqx2, nqpqz2, + nqx, nqz, + nqpqx, nqpqz, + q); + } + + t = nqx; + nqx = nqx2; + nqx2 = t; + t = nqz; + nqz = nqz2; + nqz2 = t; + t = nqpqx; + nqpqx = nqpqx2; + nqpqx2 = t; + t = nqpqz; + nqpqz = nqpqz2; + nqpqz2 = t; + + byte <<= 1; + } + } + + memcpy(resultx, nqx, sizeof(felem) * 10); + memcpy(resultz, nqz, sizeof(felem) * 10); +} + +// ----------------------------------------------------------------------------- +// Shamelessly copied from djb's code +// ----------------------------------------------------------------------------- +static void +crecip(felem *out, felem *z) { + felem z2[10]; + felem z9[10]; + felem z11[10]; + felem z2_5_0[10]; + felem z2_10_0[10]; + felem z2_20_0[10]; + felem z2_50_0[10]; + felem z2_100_0[10]; + felem t0[10]; + felem t1[10]; + int i; + + /* 2 */ fsquare(z2,z); + /* 4 */ fsquare(t1,z2); + /* 8 */ fsquare(t0,t1); + /* 9 */ fmul(z9,t0,z); + /* 11 */ fmul(z11,z9,z2); + /* 22 */ fsquare(t0,z11); + /* 2^5 - 2^0 = 31 */ fmul(z2_5_0,t0,z9); + + /* 2^6 - 2^1 */ fsquare(t0,z2_5_0); + /* 2^7 - 2^2 */ fsquare(t1,t0); + /* 2^8 - 2^3 */ fsquare(t0,t1); + /* 2^9 - 2^4 */ fsquare(t1,t0); + /* 2^10 - 2^5 */ fsquare(t0,t1); + /* 2^10 - 2^0 */ fmul(z2_10_0,t0,z2_5_0); + + /* 2^11 - 2^1 */ fsquare(t0,z2_10_0); + /* 2^12 - 2^2 */ fsquare(t1,t0); + /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^20 - 2^0 */ fmul(z2_20_0,t1,z2_10_0); + + /* 2^21 - 2^1 */ fsquare(t0,z2_20_0); + /* 2^22 - 2^2 */ fsquare(t1,t0); + /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^40 - 2^0 */ fmul(t0,t1,z2_20_0); + + /* 2^41 - 2^1 */ fsquare(t1,t0); + /* 2^42 - 2^2 */ fsquare(t0,t1); + /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t1,t0); fsquare(t0,t1); } + /* 2^50 - 2^0 */ fmul(z2_50_0,t0,z2_10_0); + + /* 2^51 - 2^1 */ fsquare(t0,z2_50_0); + /* 2^52 - 2^2 */ fsquare(t1,t0); + /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^100 - 2^0 */ fmul(z2_100_0,t1,z2_50_0); + + /* 2^101 - 2^1 */ fsquare(t1,z2_100_0); + /* 2^102 - 2^2 */ fsquare(t0,t1); + /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { fsquare(t1,t0); fsquare(t0,t1); } + /* 2^200 - 2^0 */ fmul(t1,t0,z2_100_0); + + /* 2^201 - 2^1 */ fsquare(t0,t1); + /* 2^202 - 2^2 */ fsquare(t1,t0); + /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^250 - 2^0 */ fmul(t0,t1,z2_50_0); + + /* 2^251 - 2^1 */ fsquare(t1,t0); + /* 2^252 - 2^2 */ fsquare(t0,t1); + /* 2^253 - 2^3 */ fsquare(t1,t0); + /* 2^254 - 2^4 */ fsquare(t0,t1); + /* 2^255 - 2^5 */ fsquare(t1,t0); + /* 2^255 - 21 */ fmul(out,t1,z11); +} + +void +curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]) { + felem bp[10], x[10], z[10], zmone[10]; + fexpand(bp, basepoint); + cmult(x, z, secret, bp); + crecip(zmone, z); + fmul(z, x, zmone); + fcontract(mypublic, z); +} --- /sys/src/libsec/port/dh.c Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/dh.c Sun Sep 20 22:42:17 2015 @@ -0,0 +1,74 @@ +#include "os.h" +#include +#include + +mpint* +dh_new(DHstate *dh, mpint *p, mpint *q, mpint *g) +{ + mpint *pm1; + int n; + + memset(dh, 0, sizeof(*dh)); + if(mpcmp(g, mpone) <= 0) + return nil; + + n = mpsignif(p); + pm1 = mpnew(n); + mpsub(p, mpone, pm1); + dh->p = mpcopy(p); + dh->g = mpcopy(g); + dh->q = mpcopy(q != nil ? q : pm1); + dh->x = mpnew(mpsignif(dh->q)); + dh->y = mpnew(n); + for(;;){ + mpnrand(dh->q, genrandom, dh->x); + mpexp(dh->g, dh->x, dh->p, dh->y); + if(mpcmp(dh->y, mpone) > 0 && mpcmp(dh->y, pm1) < 0) + break; + } + mpfree(pm1); + + return dh->y; +} + +mpint* +dh_finish(DHstate *dh, mpint *y) +{ + mpint *k = nil; + + if(y == nil || dh->x == nil || dh->p == nil || dh->q == nil) + goto Out; + + /* y > 1 */ + if(mpcmp(y, mpone) <= 0) + goto Out; + + k = mpnew(mpsignif(dh->p)); + + /* y < p-1 */ + mpsub(dh->p, mpone, k); + if(mpcmp(y, k) >= 0){ +Bad: + mpfree(k); + k = nil; + goto Out; + } + + /* y**q % p == 1 if q < p-1 */ + if(mpcmp(dh->q, k) < 0){ + mpexp(y, dh->q, dh->p, k); + if(mpcmp(k, mpone) != 0) + goto Bad; + } + + mpexp(y, dh->x, dh->p, k); + +Out: + mpfree(dh->p); + mpfree(dh->q); + mpfree(dh->g); + mpfree(dh->x); + mpfree(dh->y); + memset(dh, 0, sizeof(*dh)); + return k; +} --- /sys/src/libsec/port/ecc.c Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/ecc.c Sun Sep 20 22:42:18 2015 @@ -0,0 +1,544 @@ +#include "os.h" +#include +#include +#include + +void +ecassign(ECdomain *, ECpoint *a, ECpoint *b) +{ + b->inf = a->inf; + mpassign(a->x, b->x); + mpassign(a->y, b->y); +} + +void +ecadd(ECdomain *dom, ECpoint *a, ECpoint *b, ECpoint *s) +{ + mpint *l, *k, *sx, *sy; + + if(a->inf && b->inf){ + s->inf = 1; + return; + } + if(a->inf){ + ecassign(dom, b, s); + return; + } + if(b->inf){ + ecassign(dom, a, s); + return; + } + if(mpcmp(a->x, b->x) == 0 && (mpcmp(a->y, mpzero) == 0 || mpcmp(a->y, b->y) != 0)){ + s->inf = 1; + return; + } + l = mpnew(0); + k = mpnew(0); + sx = mpnew(0); + sy = mpnew(0); + if(mpcmp(a->x, b->x) == 0 && mpcmp(a->y, b->y) == 0){ + mpadd(mpone, mptwo, k); + mpmul(a->x, a->x, l); + mpmul(l, k, l); + mpadd(l, dom->a, l); + mpleft(a->y, 1, k); + mpmod(k, dom->p, k); + mpinvert(k, dom->p, k); + mpmul(k, l, l); + mpmod(l, dom->p, l); + + mpleft(a->x, 1, k); + mpmul(l, l, sx); + mpsub(sx, k, sx); + mpmod(sx, dom->p, sx); + + mpsub(a->x, sx, sy); + mpmul(l, sy, sy); + mpsub(sy, a->y, sy); + mpmod(sy, dom->p, sy); + mpassign(sx, s->x); + mpassign(sy, s->y); + mpfree(sx); + mpfree(sy); + mpfree(l); + mpfree(k); + return; + } + mpsub(b->y, a->y, l); + mpmod(l, dom->p, l); + mpsub(b->x, a->x, k); + mpmod(k, dom->p, k); + mpinvert(k, dom->p, k); + mpmul(k, l, l); + mpmod(l, dom->p, l); + + mpmul(l, l, sx); + mpsub(sx, a->x, sx); + mpsub(sx, b->x, sx); + mpmod(sx, dom->p, sx); + + mpsub(a->x, sx, sy); + mpmul(sy, l, sy); + mpsub(sy, a->y, sy); + mpmod(sy, dom->p, sy); + + mpassign(sx, s->x); + mpassign(sy, s->y); + mpfree(sx); + mpfree(sy); + mpfree(l); + mpfree(k); +} + +void +ecmul(ECdomain *dom, ECpoint *a, mpint *k, ECpoint *s) +{ + ECpoint ns, na; + mpint *l; + + if(a->inf || mpcmp(k, mpzero) == 0){ + s->inf = 1; + return; + } + ns.inf = 1; + ns.x = mpnew(0); + ns.y = mpnew(0); + na.x = mpnew(0); + na.y = mpnew(0); + ecassign(dom, a, &na); + l = mpcopy(k); + l->sign = 1; + while(mpcmp(l, mpzero) != 0){ + if(l->p[0] & 1) + ecadd(dom, &na, &ns, &ns); + ecadd(dom, &na, &na, &na); + mpright(l, 1, l); + } + if(k->sign < 0){ + ns.y->sign = -1; + mpmod(ns.y, dom->p, ns.y); + } + ecassign(dom, &ns, s); + mpfree(ns.x); + mpfree(ns.y); + mpfree(na.x); + mpfree(na.y); +} + +int +ecverify(ECdomain *dom, ECpoint *a) +{ + mpint *p, *q; + int r; + + if(a->inf) + return 1; + + p = mpnew(0); + q = mpnew(0); + mpmul(a->y, a->y, p); + mpmod(p, dom->p, p); + mpmul(a->x, a->x, q); + mpadd(q, dom->a, q); + mpmul(a->x, q, q); + mpadd(q, dom->b, q); + mpmod(q, dom->p, q); + r = mpcmp(p, q); + mpfree(p); + mpfree(q); + return r == 0; +} + +int +ecpubverify(ECdomain *dom, ECpub *a) +{ + ECpoint p; + int r; + + if(a->inf) + return 0; + if(!ecverify(dom, a)) + return 0; + p.x = mpnew(0); + p.y = mpnew(0); + ecmul(dom, a, dom->n, &p); + r = p.inf; + mpfree(p.x); + mpfree(p.y); + return r; +} + +static void +fixnibble(uchar *a) +{ + if(*a >= 'a') + *a -= 'a'-10; + else if(*a >= 'A') + *a -= 'A'-10; + else + *a -= '0'; +} + +static int +octet(char **s) +{ + uchar c, d; + + c = *(*s)++; + if(!isxdigit(c)) + return -1; + d = *(*s)++; + if(!isxdigit(d)) + return -1; + fixnibble(&c); + fixnibble(&d); + return (c << 4) | d; +} + +static mpint* +halfpt(ECdomain *dom, char *s, char **rptr, mpint *out) +{ + char *buf, *r; + int n; + mpint *ret; + + n = ((mpsignif(dom->p)+7)/8)*2; + if(strlen(s) < n) + return 0; + buf = malloc(n+1); + buf[n] = 0; + memcpy(buf, s, n); + ret = strtomp(buf, &r, 16, out); + *rptr = s + (r - buf); + free(buf); + return ret; +} + +static int +mpleg(mpint *a, mpint *b) +{ + int r, k; + mpint *m, *n, *t; + + r = 1; + m = mpcopy(a); + n = mpcopy(b); + for(;;){ + if(mpcmp(m, n) > 0) + mpmod(m, n, m); + if(mpcmp(m, mpzero) == 0){ + r = 0; + break; + } + if(mpcmp(m, mpone) == 0) + break; + k = mplowbits0(m); + if(k > 0){ + if(k & 1) + switch(n->p[0] & 15){ + case 3: case 5: case 11: case 13: + r = -r; + } + mpright(m, k, m); + } + if((n->p[0] & 3) == 3 && (m->p[0] & 3) == 3) + r = -r; + t = m; + m = n; + n = t; + } + mpfree(m); + mpfree(n); + return r; +} + +static int +mpsqrt(mpint *n, mpint *p, mpint *r) +{ + mpint *a, *t, *s, *xp, *xq, *yp, *yq, *zp, *zq, *N; + + if(mpleg(n, p) == -1) + return 0; + a = mpnew(0); + t = mpnew(0); + s = mpnew(0); + N = mpnew(0); + xp = mpnew(0); + xq = mpnew(0); + yp = mpnew(0); + yq = mpnew(0); + zp = mpnew(0); + zq = mpnew(0); + for(;;){ + for(;;){ + mprand(mpsignif(p), genrandom, a); + if(mpcmp(a, mpzero) > 0 && mpcmp(a, p) < 0) + break; + } + mpmul(a, a, t); + mpsub(t, n, t); + mpmod(t, p, t); + if(mpleg(t, p) == -1) + break; + } + mpadd(p, mpone, N); + mpright(N, 1, N); + mpmul(a, a, t); + mpsub(t, n, t); + mpassign(a, xp); + uitomp(1, xq); + uitomp(1, yp); + uitomp(0, yq); + while(mpcmp(N, mpzero) != 0){ + if(N->p[0] & 1){ + mpmul(xp, yp, zp); + mpmul(xq, yq, zq); + mpmul(zq, t, zq); + mpadd(zp, zq, zp); + mpmod(zp, p, zp); + mpmul(xp, yq, zq); + mpmul(xq, yp, s); + mpadd(zq, s, zq); + mpmod(zq, p, yq); + mpassign(zp, yp); + } + mpmul(xp, xp, zp); + mpmul(xq, xq, zq); + mpmul(zq, t, zq); + mpadd(zp, zq, zp); + mpmod(zp, p, zp); + mpmul(xp, xq, zq); + mpadd(zq, zq, zq); + mpmod(zq, p, xq); + mpassign(zp, xp); + mpright(N, 1, N); + } + if(mpcmp(yq, mpzero) != 0) + abort(); + mpassign(yp, r); + mpfree(a); + mpfree(t); + mpfree(s); + mpfree(N); + mpfree(xp); + mpfree(xq); + mpfree(yp); + mpfree(yq); + mpfree(zp); + mpfree(zq); + return 1; +} + +ECpoint* +strtoec(ECdomain *dom, char *s, char **rptr, ECpoint *ret) +{ + int allocd, o; + mpint *r; + + allocd = 0; + if(ret == nil){ + allocd = 1; + ret = mallocz(sizeof(*ret), 1); + if(ret == nil) + return nil; + ret->x = mpnew(0); + ret->y = mpnew(0); + } + o = 0; + switch(octet(&s)){ + case 0: + ret->inf = 1; + return ret; + case 3: + o = 1; + case 2: + if(halfpt(dom, s, &s, ret->x) == nil) + goto err; + r = mpnew(0); + mpmul(ret->x, ret->x, r); + mpadd(r, dom->a, r); + mpmul(r, ret->x, r); + mpadd(r, dom->b, r); + if(!mpsqrt(r, dom->p, r)){ + mpfree(r); + goto err; + } + if((r->p[0] & 1) != o) + mpsub(dom->p, r, r); + mpassign(r, ret->y); + mpfree(r); + if(!ecverify(dom, ret)) + goto err; + return ret; + case 4: + if(halfpt(dom, s, &s, ret->x) == nil) + goto err; + if(halfpt(dom, s, &s, ret->y) == nil) + goto err; + if(!ecverify(dom, ret)) + goto err; + return ret; + } +err: + if(rptr) + *rptr = s; + if(allocd){ + mpfree(ret->x); + mpfree(ret->y); + free(ret); + } + return nil; +} + +ECpriv* +ecgen(ECdomain *dom, ECpriv *p) +{ + if(p == nil){ + p = mallocz(sizeof(*p), 1); + if(p == nil) + return nil; + p->x = mpnew(0); + p->y = mpnew(0); + p->d = mpnew(0); + } + for(;;){ + mprand(mpsignif(dom->n), genrandom, p->d); + if(mpcmp(p->d, mpzero) > 0 && mpcmp(p->d, dom->n) < 0) + break; + } + ecmul(dom, dom->G, p->d, p); + return p; +} + +void +ecdsasign(ECdomain *dom, ECpriv *priv, uchar *dig, int len, mpint *r, mpint *s) +{ + ECpriv tmp; + mpint *E, *t; + + tmp.x = mpnew(0); + tmp.y = mpnew(0); + tmp.d = mpnew(0); + E = betomp(dig, len, nil); + t = mpnew(0); + if(mpsignif(dom->n) < 8*len) + mpright(E, 8*len - mpsignif(dom->n), E); + for(;;){ + ecgen(dom, &tmp); + mpmod(tmp.x, dom->n, r); + if(mpcmp(r, mpzero) == 0) + continue; + mpmul(r, priv->d, s); + mpadd(E, s, s); + mpinvert(tmp.d, dom->n, t); + mpmul(s, t, s); + mpmod(s, dom->n, s); + if(mpcmp(s, mpzero) != 0) + break; + } + mpfree(t); + mpfree(E); + mpfree(tmp.x); + mpfree(tmp.y); + mpfree(tmp.d); +} + +int +ecdsaverify(ECdomain *dom, ECpub *pub, uchar *dig, int len, mpint *r, mpint *s) +{ + mpint *E, *t, *u1, *u2; + ECpoint R, S; + int ret; + + if(mpcmp(r, mpone) < 0 || mpcmp(s, mpone) < 0 || mpcmp(r, dom->n) >= 0 || mpcmp(r, dom->n) >= 0) + return 0; + E = betomp(dig, len, nil); + if(mpsignif(dom->n) < 8*len) + mpright(E, 8*len - mpsignif(dom->n), E); + t = mpnew(0); + u1 = mpnew(0); + u2 = mpnew(0); + R.x = mpnew(0); + R.y = mpnew(0); + S.x = mpnew(0); + S.y = mpnew(0); + mpinvert(s, dom->n, t); + mpmul(E, t, u1); + mpmod(u1, dom->n, u1); + mpmul(r, t, u2); + mpmod(u2, dom->n, u2); + ecmul(dom, dom->G, u1, &R); + ecmul(dom, pub, u2, &S); + ecadd(dom, &R, &S, &R); + ret = 0; + if(!R.inf){ + mpmod(R.x, dom->n, t); + ret = mpcmp(r, t) == 0; + } + mpfree(t); + mpfree(u1); + mpfree(u2); + mpfree(R.x); + mpfree(R.y); + mpfree(S.x); + mpfree(S.y); + return ret; +} + +static char *code = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + +void +base58enc(uchar *src, char *dst, int len) +{ + mpint *n, *r, *b; + char *sdst, t; + + sdst = dst; + n = betomp(src, len, nil); + b = uitomp(58, nil); + r = mpnew(0); + while(mpcmp(n, mpzero) != 0){ + mpdiv(n, b, n, r); + *dst++ = code[mptoui(r)]; + } + for(; *src == 0; src++) + *dst++ = code[0]; + dst--; + while(dst > sdst){ + t = *sdst; + *sdst++ = *dst; + *dst-- = t; + } +} + +int +base58dec(char *src, uchar *dst, int len) +{ + mpint *n, *b, *r; + char *t; + int l; + + n = mpnew(0); + r = mpnew(0); + b = uitomp(58, nil); + for(; *src; src++){ + t = strchr(code, *src); + if(t == nil){ + mpfree(n); + mpfree(r); + mpfree(b); + werrstr("invalid base58 char"); + return -1; + } + uitomp(t - code, r); + mpmul(n, b, n); + mpadd(n, r, n); + } + memset(dst, 0, len); + l = (mpsignif(n) + 7) / 8; + mptobe(n, dst + (len - l), l, nil); + mpfree(n); + mpfree(r); + mpfree(b); + return 0; +} --- /sys/src/libsec/port/hkdf.c Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/hkdf.c Sun Sep 20 22:42:18 2015 @@ -0,0 +1,39 @@ +#include "os.h" +#include +#include + +/* rfc5869 */ +void +hkdf_x(salt, nsalt, info, ninfo, key, nkey, d, dlen, x, xlen) + uchar *salt, *info, *key, *d; + ulong nsalt, ninfo, nkey, dlen; + DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*); + int xlen; +{ + uchar prk[256], tmp[256], cnt; + DigestState *ds; + + assert(xlen <= sizeof(tmp)); + + memset(tmp, 0, xlen); + if(nsalt == 0){ + salt = tmp; + nsalt = xlen; + } + /* note that salt and key are swapped in this case */ + (*x)(key, nkey, salt, nsalt, prk, nil); + ds = nil; + for(cnt=1;; cnt++) { + if(ninfo > 0) + ds = (*x)(info, ninfo, prk, xlen, nil, ds); + (*x)(&cnt, 1, prk, xlen, tmp, ds); + if(dlen <= xlen){ + memmove(d, tmp, dlen); + break; + } + memmove(d, tmp, xlen); + dlen -= xlen; + d += xlen; + ds = (*x)(tmp, xlen, prk, xlen, nil, nil); + } +} --- /sys/src/libsec/port/pbkdf2.c Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/pbkdf2.c Sun Sep 20 22:42:19 2015 @@ -0,0 +1,35 @@ +#include "os.h" +#include +#include + +/* rfc2898 */ +void +pbkdf2_x(p, plen, s, slen, rounds, d, dlen, x, xlen) + uchar *p, *s, *d; + ulong plen, slen, dlen, rounds; + DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*); + int xlen; +{ + uchar block[256], tmp[256]; + ulong i, j, k, n; + DigestState *ds; + + assert(xlen <= sizeof(tmp)); + + for(i = 1; dlen > 0; i++, d += n, dlen -= n){ + tmp[3] = i; + tmp[2] = i >> 8; + tmp[1] = i >> 16; + tmp[0] = i >> 24; + ds = (*x)(s, slen, p, plen, nil, nil); + (*x)(tmp, 4, p, plen, block, ds); + memmove(tmp, block, xlen); + for(j = 1; j < rounds; j++){ + (*x)(tmp, xlen, p, plen, tmp, nil); + for(k=0; k xlen ? xlen : dlen; + memmove(d, block, n); + } +} --- /sys/src/libsec/port/mkfile Sun Sep 20 22:42:21 2015 +++ /sys/src/libsec/port/mkfile Sun Sep 20 22:42:22 2015 @@ -17,6 +17,13 @@ egsign.c egverify.c \ dsagen.c dsaalloc.c dsaprivtopub.c dsasign.c dsaverify.c \ tlshand.c thumb.c readcert.c \ + aes_xts.c \ + ecc.c\ + dh.c\ + curve25519.c\ + curve25519_dh.c\ + pbkdf2.c\ + hkdf.c\ ALLOFILES=${CFILES:%.c=%.$O} --- /sys/include/libsec.h Sun Sep 20 22:42:24 2015 +++ /sys/include/libsec.h Sun Sep 20 22:42:25 2015 @@ -405,3 +405,75 @@ /* readcert.c */ uchar *readcert(char *filename, int *pcertlen); PEMChain*readcertchain(char *filename); + +/* aes_xts.c */ +int aes_xts_encrypt(u32int tweak[], u32int ecb[], vlong sectorNumber, uchar *input, uchar *output, usize len) ; +int aes_xts_decrypt(u32int tweak[], u32int ecb[], vlong sectorNumber, uchar *input, uchar *output, usize len); + +typedef struct ECpoint{ + int inf; + mpint *x; + mpint *y; +} ECpoint; + +typedef ECpoint ECpub; +typedef struct ECpriv{ + ECpoint; + mpint *d; +} ECpriv; + +typedef struct ECdomain{ + mpint *p; + mpint *a; + mpint *b; + ECpoint *G; + mpint *n; + mpint *h; +} ECdomain; + +void ecassign(ECdomain *, ECpoint *old, ECpoint *new); +void ecadd(ECdomain *, ECpoint *a, ECpoint *b, ECpoint *s); +void ecmul(ECdomain *, ECpoint *a, mpint *k, ECpoint *s); +ECpoint* strtoec(ECdomain *, char *, char **, ECpoint *); +ECpriv* ecgen(ECdomain *, ECpriv*); +int ecverify(ECdomain *, ECpoint *); +int ecpubverify(ECdomain *, ECpub *); +void ecdsasign(ECdomain *, ECpriv *, uchar *, int, mpint *, mpint *); +int ecdsaverify(ECdomain *, ECpub *, uchar *, int, mpint *, mpint *); +void base58enc(uchar *, char *, int); +int base58dec(char *, uchar *, int); + +/* + * Diffie-Hellman key exchange + */ + +typedef struct DHstate DHstate; +struct DHstate +{ + mpint *g; /* base g */ + mpint *p; /* large prime */ + mpint *q; /* subgroup prime */ + mpint *x; /* random secret */ + mpint *y; /* public key y = g**x % p */ +}; + +/* generate new public key: y = g**x % p */ +mpint* dh_new(DHstate *dh, mpint *p, mpint *q, mpint *g); + +/* calculate shared key: k = y**x % p */ +mpint* dh_finish(DHstate *dh, mpint *y); + +/* Curve25519 elliptic curve, public key function */ +void curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]); + +/* Curve25519 diffie hellman */ +void curve25519_dh_new(uchar x[32], uchar y[32]); +void curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]); + +/* password-based key derivation function 2 (rfc2898) */ +void pbkdf2_x(uchar *p, ulong plen, uchar *s, ulong slen, ulong rounds, uchar *d, ulong dlen, + DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen); + +/* hmac-based key derivation function (rfc5869) */ +void hkdf_x(uchar *salt, ulong nsalt, uchar *info, ulong ninfo, uchar *key, ulong nkey, uchar *d, ulong dlen, + DigestState* (*x)(uchar*, ulong, uchar*, ulong, uchar*, DigestState*), int xlen);