From eb8aa4ec8ea84c76dcf06d1647fce1b237cec313 Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Wed, 26 Apr 2017 11:53:05 +0100 Subject: [PATCH] pubkey.c, catacomb/__init__.py: Add bindings for Hamburg's X448. Organization: Straylight/Edgeware From: Mark Wooding --- catacomb-python.h | 1 + catacomb.c | 1 + catacomb/__init__.py | 15 ++++++++++++++- pubkey.c | 20 +++++++++++++++++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/catacomb-python.h b/catacomb-python.h index 41c5292..d7e2543 100644 --- a/catacomb-python.h +++ b/catacomb-python.h @@ -88,6 +88,7 @@ #include #include #include +#include #include #include diff --git a/catacomb.c b/catacomb.c index d64a16e..9c83c9d 100644 --- a/catacomb.c +++ b/catacomb.c @@ -46,6 +46,7 @@ static const struct nameval consts[] = { C(KF_BURN), C(KF_OPT), C(EC_XONLY), C(EC_YBIT), C(EC_LSB), C(EC_CMPR), C(EC_EXPLY), C(EC_SORT), C(X25519_KEYSZ), C(X25519_PUBSZ), C(X25519_OUTSZ), + C(X448_KEYSZ), C(X448_PUBSZ), C(X448_OUTSZ), C(ED25519_KEYSZ), C(ED25519_PUBSZ), C(ED25519_SIGSZ), #define ENTRY(tag, val, str) C(KERR_##tag), KEY_ERRORS(ENTRY) diff --git a/catacomb/__init__.py b/catacomb/__init__.py index 9522528..4c225e7 100644 --- a/catacomb/__init__.py +++ b/catacomb/__init__.py @@ -459,11 +459,15 @@ class _tmp: _augment(RSAPriv, _tmp) ###-------------------------------------------------------------------------- -### Bernstein's elliptic curve crypto. +### Bernstein's elliptic curve crypto and related schemes. X25519_BASE = \ bytes('0900000000000000000000000000000000000000000000000000000000000000') +X448_BASE = \ + bytes('05000000000000000000000000000000000000000000000000000000' + '00000000000000000000000000000000000000000000000000000000') + Z128 = bytes('00000000000000000000000000000000') class _BoxyPub (object): @@ -495,6 +499,15 @@ class X25519Priv (_BoxyPriv, X25519Pub): def _op(me, k, X): return x25519(k, X) def _hashkey(me, z): return hsalsa20_prf(z, Z128) +class X448Pub (_BoxyPub): + _PUBSZ = X448_PUBSZ + _BASE = X448_BASE + +class X448Priv (_BoxyPriv, X448Pub): + _KEYSZ = X448_KEYSZ + def _op(me, k, X): return x448(k, X) + ##def _hashkey(me, z): return ??? + class Ed25519Pub (object): def __init__(me, pub): me.pub = pub diff --git a/pubkey.c b/pubkey.c index 391db31..5292717 100644 --- a/pubkey.c +++ b/pubkey.c @@ -1118,7 +1118,7 @@ end: return (rc); } -/*----- X25519 ------------------------------------------------------------*/ +/*----- X25519 and related algorithms -------------------------------------*/ static PyObject *meth_x25519(PyObject *me, PyObject *arg) { @@ -1136,6 +1136,22 @@ end: return (0); } +static PyObject *meth_x448(PyObject *me, PyObject *arg) +{ + const char *k, *p; + Py_ssize_t ksz, psz; + PyObject *rc = 0; + if (!PyArg_ParseTuple(arg, "s#s#:x448", &k, &ksz, &p, &psz)) goto end; + if (ksz != X448_KEYSZ) VALERR("bad key length"); + if (psz != X448_PUBSZ) VALERR("bad public length"); + rc = bytestring_pywrap(0, X448_OUTSZ); + x448((octet *)PyString_AS_STRING(rc), + (const octet *)k, (const octet *)p); + return (rc); +end: + return (0); +} + /*----- Ed25519 -----------------------------------------------------------*/ static PyObject *meth_ed25519_pubkey(PyObject *me, PyObject *arg) @@ -1203,6 +1219,8 @@ static PyMethodDef methods[] = { generate(NBITS, [event = pgen_nullev, rng = rand, nsteps = 0]) -> R") METH (x25519, "\ x25519(KEY, PUBLIC) -> SHARED") + METH (x448, "\ +x448(KEY, PUBLIC) -> SHARED") METH (ed25519_pubkey, "\ ed25519_pubkey(KEY) -> PUBLIC") KWMETH(ed25519_sign, "\ -- [mdw]