--- /dev/null Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/ecc.c Tue Jun 23 00:00:00 2026 @@ -0,0 +1,612 @@ +#include "os.h" +#include +#include +#include + +extern void jacobian_affine(mpint *p, + mpint *X, mpint *Y, mpint *Z); +extern void jacobian_dbl(mpint *p, mpint *a, + mpint *X1, mpint *Y1, mpint *Z1, + mpint *X3, mpint *Y3, mpint *Z3); +extern void jacobian_add(mpint *p, mpint *a, + mpint *X1, mpint *Y1, mpint *Z1, + mpint *X2, mpint *Y2, mpint *Z2, + mpint *X3, mpint *Y3, mpint *Z3); + +void +ecassign(ECdomain *dom, ECpoint *a, ECpoint *b) +{ + if((b->inf = a->inf) != 0) + return; + mpassign(a->x, b->x); + mpassign(a->y, b->y); + if(b->z != nil){ + mpassign(a->z != nil ? a->z : mpone, b->z); + return; + } + if(a->z != nil){ + b->z = mpcopy(a->z); + jacobian_affine(dom->p, b->x, b->y, b->z); + mpfree(b->z); + b->z = nil; + } +} + +void +ecadd(ECdomain *dom, ECpoint *a, ECpoint *b, ECpoint *s) +{ + 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(s->z == nil){ + s->z = mpcopy(mpone); + ecadd(dom, a, b, s); + if(!s->inf) + jacobian_affine(dom->p, s->x, s->y, s->z); + mpfree(s->z); + s->z = nil; + return; + } + + if(a == b) + jacobian_dbl(dom->p, dom->a, + a->x, a->y, a->z != nil ? a->z : mpone, + s->x, s->y, s->z); + else + jacobian_add(dom->p, dom->a, + a->x, a->y, a->z != nil ? a->z : mpone, + b->x, b->y, b->z != nil ? b->z : mpone, + s->x, s->y, s->z); + s->inf = mpcmp(s->z, mpzero) == 0; +} + +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); + ns.z = mpnew(0); + na.x = mpnew(0); + na.y = mpnew(0); + na.z = 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.inf){ + ns.y->sign = -1; + mpmod(ns.y, dom->p, ns.y); + } + ecassign(dom, &ns, s); + mpfree(ns.x); + mpfree(ns.y); + mpfree(ns.z); + mpfree(na.x); + mpfree(na.y); + mpfree(na.z); + mpfree(l); +} + +int +ecverify(ECdomain *dom, ECpoint *a) +{ + mpint *p, *q; + int r; + + if(a->inf) + return 1; + + assert(a->z == nil); /* need affine coordinates */ + p = mpnew(0); + q = mpnew(0); + mpmodmul(a->y, a->y, dom->p, p); + mpmodmul(a->x, a->x, dom->p, q); + mpmodadd(q, dom->a, dom->p, q); + mpmodmul(q, a->x, dom->p, q); + mpmodadd(q, dom->b, 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); + p.z = mpnew(0); + ecmul(dom, a, dom->n, &p); + r = p.inf; + mpfree(p.x); + mpfree(p.y); + mpfree(p.z); + 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(;;){ + mpnrand(p, genrandom, a); + if(mpcmp(a, mpzero) > 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); + } + ret->inf = 0; + o = 0; + switch(octet(&s)){ + case 0: + ret->inf = 1; + break; + 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; + break; + 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; + break; + } + if(ret->z != nil && !ret->inf) + mpassign(mpone, ret->z); + 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(;;){ + mpnrand(dom->n, genrandom, p->d); + if(mpcmp(p->d, mpzero) > 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.z = nil; + 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); + mpmodmul(s, t, 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(s, 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); + R.z = mpnew(0); + S.x = mpnew(0); + S.y = mpnew(0); + S.z = mpnew(0); + mpinvert(s, dom->n, t); + mpmodmul(E, t, dom->n, u1); + mpmodmul(r, t, dom->n, u2); + ecmul(dom, &dom->G, u1, &R); + ecmul(dom, pub, u2, &S); + ecadd(dom, &R, &S, &R); + ret = 0; + if(!R.inf){ + jacobian_affine(dom->p, R.x, R.y, R.z); + mpmod(R.x, dom->n, t); + ret = mpcmp(r, t) == 0; + } + mpfree(E); + mpfree(t); + mpfree(u1); + mpfree(u2); + mpfree(R.x); + mpfree(R.y); + mpfree(R.z); + mpfree(S.x); + mpfree(S.y); + mpfree(S.z); + 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-- = 0; + while(dst > sdst){ + t = *sdst; + *sdst++ = *dst; + *dst-- = t; + } +} + +int +base58dec(char *src, uchar *dst, int len) +{ + mpint *n, *b, *r; + char *t; + + 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); + } + mptober(n, dst, len); + mpfree(n); + mpfree(r); + mpfree(b); + return 0; +} + +void +ecdominit(ECdomain *dom, void (*init)(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h)) +{ + memset(dom, 0, sizeof(*dom)); + dom->p = mpnew(0); + dom->a = mpnew(0); + dom->b = mpnew(0); + dom->G.x = mpnew(0); + dom->G.y = mpnew(0); + dom->n = mpnew(0); + dom->h = mpnew(0); + if(init){ + (*init)(dom->p, dom->a, dom->b, dom->G.x, dom->G.y, dom->n, dom->h); + dom->p = mpfield(dom->p); + } +} + +void +ecdomfree(ECdomain *dom) +{ + mpfree(dom->p); + mpfree(dom->a); + mpfree(dom->b); + mpfree(dom->G.x); + mpfree(dom->G.y); + mpfree(dom->n); + mpfree(dom->h); + memset(dom, 0, sizeof(*dom)); +} + +int +ecencodepub(ECdomain *dom, ECpub *pub, uchar *data, int len) +{ + int n; + + n = (mpsignif(dom->p)+7)/8; + if(len < 1 + 2*n) + return 0; + len = 1 + 2*n; + data[0] = 0x04; + mptober(pub->x, data+1, n); + mptober(pub->y, data+1+n, n); + return len; +} + +ECpub* +ecdecodepub(ECdomain *dom, uchar *data, int len) +{ + ECpub *pub; + int n; + + n = (mpsignif(dom->p)+7)/8; + if(len != 1 + 2*n || data[0] != 0x04) + return nil; + pub = mallocz(sizeof(*pub), 1); + if(pub == nil) + return nil; + pub->x = betomp(data+1, n, nil); + pub->y = betomp(data+1+n, n, nil); + if(!ecpubverify(dom, pub)){ + ecpubfree(pub); + pub = nil; + } + return pub; +} + +void +ecpubfree(ECpub *p) +{ + if(p == nil) + return; + mpfree(p->x); + mpfree(p->y); + free(p); +} --- /dev/null Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/secp256r1.mp Tue Jun 23 00:00:00 2026 @@ -0,0 +1,10 @@ +# E: y² = x³ + ax + b +secp256r1(p,a,b,x,y,n,h) { + p = 2^256 - 2^224 + 2^192 + 2^96 - 1; + a = p - 3; + b = 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B; + x = 0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296; + y = 0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5; + n = 0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551; + h = 1; +} --- /dev/null Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/jacobian.mp Tue Jun 23 00:00:00 2026 @@ -0,0 +1,60 @@ +# Elliptic curve group operations in jacobian coordinates: +# x=X/Z^2 +# x=Y/Z^3 + +jacobian_new(x,y,z, X,Y,Z) { + X = x; + Y = y; + Z = z; +} +jacobian_inf(X,Y,Z) { + X,Y,Z = jacobian_new(0,1,0); +} +jacobian_affine(p, X,Y,Z) mod(p) { + if(Z != 0) { + ZZ = Z^2; + ZZZ = ZZ*Z; + X = X / ZZ; + Y = Y / ZZZ; + Z = 1; + } +} +jacobian_dbl(p,a, X1,Y1,Z1, X3,Y3,Z3) mod(p) { + if(Y1 == 0) { + X3,Y3,Z3 = jacobian_inf(); + } else { + XX = X1^2; + YY = Y1^2; + YYYY = YY^2; + ZZ = Z1^2; + S = 2*((X1+YY)^2-XX-YYYY); + M = 3*XX+a*ZZ^2; + Z3 = (Y1+Z1)^2-YY-ZZ; + X3 = M^2-2*S; + Y3 = M*(S-X3)-8*YYYY; + } +} +jacobian_add(p,a, X1,Y1,Z1, X2,Y2,Z2, X3,Y3,Z3) mod(p) { + Z1Z1 = Z1^2; + Z2Z2 = Z2^2; + U1 = X1*Z2Z2; + U2 = X2*Z1Z1; + S1 = Y1*Z2*Z2Z2; + S2 = Y2*Z1*Z1Z1; + if(U1 == U2) { + if(S1 != S2) { + X3,Y3,Z3 = jacobian_inf(); + } else { + X3,Y3,Z3 = jacobian_dbl(p,a, X1,Y1,Z1); + } + } else { + H = U2-U1; + I = (2*H)^2; + J = H*I; + r = 2*(S2-S1); + V = U1*I; + X3 = r^2-J-2*V; + Y3 = r*(V-X3)-2*S1*J; + Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2)*H; + } +} --- /dev/null Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/curve25519.c Tue Jun 23 00:00:00 2026 @@ -0,0 +1,570 @@ +/* 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 "os.h" +#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 +fmult(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 */ fmult(z9,t0,z); + /* 11 */ fmult(z11,z9,z2); + /* 22 */ fsquare(t0,z11); + /* 2^5 - 2^0 = 31 */ fmult(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 */ fmult(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 */ fmult(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 */ fmult(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 */ fmult(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 */ fmult(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 */ fmult(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 */ fmult(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 */ fmult(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); + fmult(z, x, zmone); + fcontract(mypublic, z); +} --- /dev/null Thu Jan 1 00:00:00 1970 +++ /sys/src/libsec/port/curve25519_dh.c Tue Jun 23 00:00:00 2026 @@ -0,0 +1,68 @@ +#include "os.h" +#include +#include + +static uchar nine[32] = {9}; +static uchar zero[32] = {0}; + +int +x25519(uchar out[32], uchar s[32], uchar u[32]) +{ + uchar sf, sl, ul; + + sf = s[0]; + sl = s[31]; + ul = u[31]; + + /* clamp */ + s[0] &= ~7; /* clear bit 0,1,2 */ + s[31] = 0x40 | (s[31] & 0x7f); /* set bit 254, clear bit 255 */ + + /* + Implementations MUST accept non-canonical values and process them as + if they had been reduced modulo the field prime. The non-canonical + values are 2^255 - 19 through 2^255 - 1 for X25519 + */ + u[31] &= 0x7f; + + curve25519(out, s, u); + + s[0] = sf; + s[31] = sl; + u[31] = ul; + + return tsmemcmp(out, zero, 32) != 0; +} + +void +curve25519_dh_new(uchar x[32], uchar y[32]) +{ + /* new public/private key pair */ + genrandom(x, 32); + uchar b = x[31]; + + /* don't check for zero: the scalar is never + zero because of clamping, and the basepoint is not the identity + in the prime-order subgroup(s). */ + x25519(y, x, nine); + + /* bit 255 is always 0, so make it random */ + y[31] |= b & 0x80; +} + +int +curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]) +{ + int r; + + /* remove the random bit */ + y[31] &= 0x7f; + + /* calculate dhx key */ + r = x25519(z, x, y); + + memset(x, 0, 32); + memset(y, 0, 32); + + return r; +} --- /n/sources/plan9/sys/include/libsec.h Sat Jun 14 00:00:00 2025 +++ /sys/include/libsec.h Tue Jun 23 00:00:00 2026 @@ -413,6 +437,56 @@ void dsasigfree(DSAsig*); DSApub* dsaprivtopub(DSApriv*); DSApriv* asn1toDSApriv(uchar*, int); +/* + * elliptic curves + */ +typedef struct ECpoint{ + int inf; + mpint *x; + mpint *y; + mpint *z; /* nil when using affine coordinates */ +} 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 ecdominit(ECdomain *, void (*init)(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h)); +void ecdomfree(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); +ECpub* ecdecodepub(ECdomain *dom, uchar *, int); +int ecencodepub(ECdomain *dom, ECpub *, uchar *, int); +void ecpubfree(ECpub *); + +void secp256r1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h); + +/* x25519 elliptic curve diffie-hellman */ +void curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]); +int x25519(uchar out[32], uchar s[32], uchar u[32]); +void curve25519_dh_new(uchar x[32], uchar y[32]); +int curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]); + /* * TLS */ --- /n/sources/plan9/sys/include/ape/libsec.h Sat Jun 14 00:00:00 2025 +++ /sys/include/ape/libsec.h Tue Jun 23 00:00:00 2026 @@ -372,6 +396,56 @@ void dsasigfree(DSAsig*); DSApub* dsaprivtopub(DSApriv*); DSApriv* asn1toDSApriv(uchar*, int); +/* + * elliptic curves + */ +typedef struct ECpoint{ + int inf; + mpint *x; + mpint *y; + mpint *z; /* nil when using affine coordinates */ +} 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 ecdominit(ECdomain *, void (*init)(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h)); +void ecdomfree(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); +ECpub* ecdecodepub(ECdomain *dom, uchar *, int); +int ecencodepub(ECdomain *dom, ECpub *, uchar *, int); +void ecpubfree(ECpub *); + +void secp256r1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h); + +/* x25519 elliptic curve diffie-hellman */ +void curve25519(uchar mypublic[32], uchar secret[32], uchar basepoint[32]); +int x25519(uchar out[32], uchar s[32], uchar u[32]); +void curve25519_dh_new(uchar x[32], uchar y[32]); +int curve25519_dh_finish(uchar x[32], uchar y[32], uchar z[32]); + /* * TLS */ --- /n/sources/plan9/sys/src/libsec/port/mkfile Sat Jun 14 00:00:00 2025 +++ /sys/src/libsec/port/mkfile Tue Jun 23 00:00:00 2026 @@ -22,6 +22,9 @@ hkdf.c\ tsmemcmp.c\ ccpoly.c\ + ecc.c jacobian.c secp256r1.c curve25519.c curve25519_dh.c\ + +CLEANFILES=secp256r1.c jacobian.c ALLOFILES=${CFILES:%.c=%.$O} @@ -38,5 +41,11 @@ $target + echo '#include ' >> $target + mpc $prereq >> $target + $O.rsatest: rsatest.$O $LD -o $target $prereq --- /dev/null Thu Jan 1 00:00:00 1970 +++ /sys/man/2/ec Tue Jun 23 00:00:00 2026 @@ -0,0 +1,169 @@ +.TH EC 2 +.SH NAME +secp256r1, +ecdominit, +ecdomfree, +ecassign, +ecadd, +ecmul, +strtoec, +ecgen, +ecverify, +ecpubverify, +ecdsasign, +ecdsaverify, +ecencodepub, +ecdecodepub, +ecpubfree \- elliptic curve cryptography +.SH SYNOPSIS +.B #include +.br +.B #include +.br +.B #include +.br +.B #include +.PP +.B +void secp256r1(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h) +.PP +.B +void ecdominit(ECdomain *dom, void (*init)(mpint *p, mpint *a, mpint *b, mpint *x, mpint *y, mpint *n, mpint *h)) +.PP +.B +void ecdomfree(ECdomain *dom) +.PP +.B +void ecassign(ECdomain *dom, ECpoint *old, ECpoint *new) +.PP +.B +void ecadd(ECdomain *dom, ECpoint *a, ECpoint *b, ECpoint *s) +.PP +.B +void ecmul(ECdomain *dom, ECpoint *a, mpint *k, ECpoint *s) +.PP +.B +ECpoint* strtoec(ECdomain *dom, char *s, char **rptr, ECpoint *p) +.PP +.B +ECpriv* ecgen(ECdomain *dom, ECpriv *p) +.PP +.B +int ecverify(ECdomain *dom, ECpoint *p) +.PP +.B +int ecpubverify(ECdomain *dom, ECpub *p) +.PP +.B +void ecdsasign(ECdomain *dom, ECpriv *priv, uchar *dig, int dlen, mpint *r, mpint *s) +.PP +.B +int ecdsaverify(ECdomain *dom, ECpub *pub, uchar *dig, int dlen, mpint *r, mpint *s) +.PP +.B +int ecencodepub(ECdomain *dom, ECpub *pub, uchar *data, int len) +.PP +.B +ECpub* ecdecodepub(ECdomain *dom, uchar *data, int len) +.PP +.B +void ecpubfree(ECpub *p); +.DT +.SH DESCRIPTION +These functions implement elliptic curve cryptography. +An elliptic curve together with cryptographic parameters are specified using an +.B ECdomain +struct. +Points on the curve are represented by +.B ECpoint +structs. +.PP +.B ecdominit +initializes a +.B ECdomain +struct and calls the +.B init +function such as +.B secp256r1 +which fills in the parameters of the curve. +.PP +.B ecdomfree +frees the parameters of the curve and zeros the struct. It does +not free the memory of the struct itself. +.PP +.BR ecassign ", " ecadd " and " ecmul +are analogous to their counterparts in +.IR mp (2). +.PP +.B strtoec +converts a hex string representing an octet string as specified in +.I Standards for Efficient Cryptography (SEC) 1 +to an +.B ECpoint +struct. Both uncompressed and compressed formats are supported. +If +.B rptr +is not +.BR nil , +it is used to return the position in the string where the parser stopped. +If +.BR p " is " nil +space is allocated automatically, else the given struct is used. +.PP +.B ecverify +and +.B ecpubverify +verify that the given point or public key, respectively, is valid. +.PP +.B ecgen +generates a keypair and returns a pointer to it. +If +.BR p " is " nil +space is allocated automatically, else the given struct is used. +.PP +.B ecdsasign +and +.B ecdsaverify +create or verify, respectively, a signature using the ECDSA scheme specified in +.I SEC 1. +It is absolutely vital that +.B dig +is a cryptographic hash to the message. +.B ecdsasign +writes the signature to +.BR r " and " s +which are assumed to be allocated properly. +.PP +.B ecencodepub +and +.B ecdecodepub +handle encoding and decoding of public keys in uncompressed format. +Note that +.B ecdecodepub +also verifies that the public key is valid in the specified domain. +.PP +.B ecpubfree +frees a +.B ECpub +structure and its associated members. +.SH RETURN VALUE +.B *verify +functions return +.B 1 +for a positive result. +Functions returning pointers may return +.B nil +in case of error +.I (e.g. +failing +.IR malloc (2)). +.SH SOURCE +.B /sys/src/libsec/port/ecc.c +.SH SEE ALSO +.IR rsa (2) +.br +.I +Standards for Efficient Cryptography (SEC) 1: Elliptic Curve Cryptography +- Certicom Research, 2009 +.SH HISTORY +This implementation of elliptic curve cryptography first appeared in 9front (June, 2012).