chiark / gitweb /
catacomb/__init__.py: Don't print secret bits of keys by default.
[catacomb-python] / pubkey.c
index 4c4eb1dfbc40743ea7c342eea9dafde4a22c0a52..4067ae0ff10219ed5dbff4f24df93be1c4d41af7 100644 (file)
--- a/pubkey.c
+++ b/pubkey.c
@@ -54,26 +54,35 @@ static void dsa_pydealloc(PyObject *me)
 }
 
 static PyObject *dsa_setup(PyTypeObject *ty, PyObject *G, PyObject *u,
-                          PyObject *p, PyObject *rng, PyObject *hash)
+                          PyObject *p, PyObject *rng, PyObject *hash,
+                          void (*calcpub)(group *, ge *, mp *))
 {
   dsa_pyobj *g;
+  ge *pp;
 
   g = PyObject_New(dsa_pyobj, ty);
-  if (GROUP_G(G) != GE_G(p) && !group_samep(GROUP_G(G), GE_G(p)))
-    TYERR("public key not from group");
+  if (p) Py_INCREF(p);
   if (!u) {
     g->d.u = 0;
     u = Py_None;
   } else if ((g->d.u = getmp(u)) == 0)
     goto end;
+  if (!p) {
+    assert(g->d.u); assert(calcpub);
+    pp = G_CREATE(GROUP_G(G));
+    calcpub(GROUP_G(G), pp, g->d.u);
+    p = ge_pywrap(G, pp);
+  } else if (GROUP_G(G) != GE_G(p) && !group_samep(GROUP_G(G), GE_G(p)))
+    TYERR("public key not from group");
   g->d.g = GROUP_G(G);
   g->d.p = GE_X(p);
   g->d.r = GRAND_R(rng);
   g->d.h = GCHASH_CH(hash);
-  g->G = G; Py_INCREF(G); g->u = u; Py_INCREF(u); g->p = p; Py_INCREF(p);
+  g->G = G; Py_INCREF(G); g->u = u; Py_INCREF(u); g->p = p;
   g->rng = rng; Py_INCREF(rng); g->hash = hash; Py_INCREF(hash);
   return ((PyObject *)g);
 end:
+  if (p) Py_DECREF(p);
   FREEOBJ(g);
   return (0);
 }
@@ -81,17 +90,16 @@ end:
 static PyObject *dsapub_pynew(PyTypeObject *ty,
                              PyObject *arg, PyObject *kw)
 {
-  PyObject *G, *p, *u = 0, *rng = rand_pyobj, *hash = sha_pyobj;
+  PyObject *G, *p, *rng = rand_pyobj, *hash = sha_pyobj;
   PyObject *rc = 0;
-  char *kwlist[] = { "G", "p", "u", "hash", "rng", 0 };
+  char *kwlist[] = { "G", "p", "hash", "rng", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!|OO!O!:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!|O!O!:new", kwlist,
                                   group_pytype, &G,
                                   ge_pytype, &p,
-                                  &u,
                                   gchash_pytype, &hash,
                                   grand_pytype, &rng) ||
-      (rc = dsa_setup(dsapub_pytype, G, u, p, rng, hash)) == 0)
+      (rc = dsa_setup(dsapub_pytype, G, 0, p, rng, hash, 0)) == 0)
     goto end;
 end:
   return (rc);
@@ -120,7 +128,7 @@ static PyObject *dsameth_sign(PyObject *me, PyObject *arg, PyObject *kw)
 {
   gdsa_sig s = GDSA_SIG_INIT;
   char *p;
-  int n;
+  Py_ssize_t n;
   mp *k = 0;
   PyObject *rc = 0;
   char *kwlist[] = { "msg", "k", 0 };
@@ -140,7 +148,7 @@ end:
 static PyObject *dsameth_verify(PyObject *me, PyObject *arg)
 {
   char *p;
-  int n;
+  Py_ssize_t n;
   gdsa_sig s = GDSA_SIG_INIT;
   PyObject *rc = 0;
 
@@ -156,20 +164,22 @@ end:
   return (rc);
 }
 
+static void dsa_calcpub(group *g, ge *p, mp *u) { G_EXP(g, p, g->g, u); }
+
 static PyObject *dsapriv_pynew(PyTypeObject *ty,
                               PyObject *arg, PyObject *kw)
 {
-  PyObject *G, *p, *u = Py_None, *rng = rand_pyobj, *hash = sha_pyobj;
+  PyObject *G, *p = 0, *u, *rng = rand_pyobj, *hash = sha_pyobj;
   PyObject *rc = 0;
-  char *kwlist[] = { "G", "p", "u", "hash", "rng", 0 };
+  char *kwlist[] = { "G", "u", "p", "hash", "rng", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!O|O!O!:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O|O!O!O!:new", kwlist,
                                   group_pytype, &G,
-                                  ge_pytype, &p,
                                   &u,
+                                  ge_pytype, &p,
                                   gchash_pytype, &hash,
                                   grand_pytype, &rng) ||
-      (rc = dsa_setup(dsapriv_pytype, G, u, p, rng, hash)) == 0)
+      (rc = dsa_setup(dsapriv_pytype, G, u, p, rng, hash, dsa_calcpub)) == 0)
     goto end;
 end:
   return (rc);
@@ -307,36 +317,43 @@ static PyTypeObject dsapriv_pytype_skel = {
 static PyObject *kcdsapub_pynew(PyTypeObject *ty,
                                PyObject *arg, PyObject *kw)
 {
-  PyObject *G, *p, *u = 0, *rng = rand_pyobj, *hash = has160_pyobj;
+  PyObject *G, *p, *rng = rand_pyobj, *hash = has160_pyobj;
   PyObject *rc = 0;
-  char *kwlist[] = { "G", "p", "u", "hash", "rng", 0 };
+  char *kwlist[] = { "G", "p", "hash", "rng", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!O|O!O!:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!|O!O!:new", kwlist,
                                   group_pytype, &G,
                                   ge_pytype, &p,
-                                  &u,
                                   gchash_pytype, &hash,
                                   grand_pytype, &rng) ||
-      (rc = dsa_setup(kcdsapub_pytype, G, u, p, rng, hash)) == 0)
+      (rc = dsa_setup(kcdsapub_pytype, G, 0, p, rng, hash, 0)) == 0)
     goto end;
 end:
   return (rc);
 }
 
+static void kcdsa_calcpub(group *g, ge *p, mp *u)
+{
+  mp *uinv = mp_modinv(MP_NEW, u, g->r);
+  G_EXP(g, p, g->g, uinv);
+  mp_drop(uinv);
+}
+
 static PyObject *kcdsapriv_pynew(PyTypeObject *ty,
                                 PyObject *arg, PyObject *kw)
 {
-  PyObject *G, *p, *u = Py_None, *rng = rand_pyobj, *hash = has160_pyobj;
+  PyObject *G, *u, *p = 0, *rng = rand_pyobj, *hash = has160_pyobj;
   PyObject *rc = 0;
   char *kwlist[] = { "G", "p", "u", "hash", "rng", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!|OO!O!:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O|O!O!O!:new", kwlist,
                                   group_pytype, &G,
-                                  ge_pytype, &p,
                                   &u,
+                                  ge_pytype, &p,
                                   gchash_pytype, &hash,
                                   grand_pytype, &rng) ||
-      (rc = dsa_setup(kcdsapriv_pytype, G, u, p, rng, hash)) == 0)
+      (rc = dsa_setup(kcdsapriv_pytype, G, u, p,
+                     rng, hash, kcdsa_calcpub)) == 0)
     goto end;
 end:
   return (rc);
@@ -365,7 +382,7 @@ static PyObject *kcdsameth_sign(PyObject *me, PyObject *arg, PyObject *kw)
 {
   gkcdsa_sig s = GKCDSA_SIG_INIT;
   char *p;
-  int n;
+  Py_ssize_t n;
   mp *k = 0;
   PyObject *r = 0, *rc = 0;
   char *kwlist[] = { "msg", "k", 0 };
@@ -388,7 +405,7 @@ end:
 static PyObject *kcdsameth_verify(PyObject *me, PyObject *arg)
 {
   char *p;
-  int n, rn;
+  Py_ssize_t n, rn;
   gkcdsa_sig s = GKCDSA_SIG_INIT;
   PyObject *rc = 0;
 
@@ -865,7 +882,7 @@ static PyObject *meth__p1crypt_encode(PyObject *me,
 {
   pkcs1 p1;
   char *m, *ep;
-  int msz, epsz;
+  Py_ssize_t msz, epsz;
   unsigned long nbits;
   PyObject *rc = 0;
   octet *b = 0;
@@ -894,7 +911,7 @@ static PyObject *meth__p1crypt_decode(PyObject *me,
 {
   pkcs1 p1;
   char *ep;
-  int epsz;
+  Py_ssize_t epsz;
   unsigned long nbits;
   int n;
   PyObject *rc = 0;
@@ -926,7 +943,7 @@ static PyObject *meth__p1sig_encode(PyObject *me,
 {
   pkcs1 p1;
   char *m, *ep;
-  int msz, epsz;
+  Py_ssize_t msz, epsz;
   unsigned long nbits;
   PyObject *rc = 0;
   octet *b = 0;
@@ -955,7 +972,7 @@ static PyObject *meth__p1sig_decode(PyObject *me,
 {
   pkcs1 p1;
   char *ep;
-  int epsz;
+  Py_ssize_t epsz;
   unsigned long nbits;
   int n;
   PyObject *hukairz;
@@ -988,7 +1005,7 @@ static PyObject *meth__oaep_encode(PyObject *me,
 {
   oaep o;
   char *m, *ep;
-  int msz, epsz;
+  Py_ssize_t msz, epsz;
   unsigned long nbits;
   PyObject *rc = 0;
   octet *b = 0;
@@ -1020,7 +1037,7 @@ static PyObject *meth__oaep_decode(PyObject *me,
 {
   oaep o;
   char *ep;
-  int epsz;
+  Py_ssize_t epsz;
   unsigned long nbits;
   int n;
   PyObject *rc = 0;
@@ -1055,7 +1072,7 @@ static PyObject *meth__pss_encode(PyObject *me,
 {
   pss p;
   char *m;
-  int msz;
+  Py_ssize_t msz;
   unsigned long nbits;
   PyObject *rc = 0;
   octet *b = 0;
@@ -1087,7 +1104,7 @@ static PyObject *meth__pss_decode(PyObject *me,
 {
   pss p;
   char *m;
-  int msz;
+  Py_ssize_t msz;
   unsigned long nbits;
   PyObject *rc = 0;
   octet *b = 0;
@@ -1118,6 +1135,91 @@ end:
   return (rc);
 }
 
+/*----- X25519 and related algorithms -------------------------------------*/
+
+static PyObject *meth_x25519(PyObject *me, PyObject *arg)
+{
+  const char *k, *p;
+  Py_ssize_t ksz, psz;
+  PyObject *rc = 0;
+  if (!PyArg_ParseTuple(arg, "s#s#:x25519", &k, &ksz, &p, &psz)) goto end;
+  if (ksz != X25519_KEYSZ) VALERR("bad key length");
+  if (psz != X25519_PUBSZ) VALERR("bad public length");
+  rc = bytestring_pywrap(0, X25519_OUTSZ);
+  x25519((octet *)PyString_AS_STRING(rc),
+        (const octet *)k, (const octet *)p);
+  return (rc);
+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)
+{
+  const char *k;
+  Py_ssize_t ksz;
+  PyObject *rc = 0;
+  if (!PyArg_ParseTuple(arg, "s#:ed25519_pubkey", &k, &ksz)) goto end;
+  rc = bytestring_pywrap(0, ED25519_PUBSZ);
+  ed25519_pubkey((octet *)PyString_AS_STRING(rc), k, ksz);
+  return (rc);
+end:
+  return (0);
+}
+
+static PyObject *meth_ed25519_sign(PyObject *me, PyObject *arg, PyObject *kw)
+{
+  const char *k, *p = 0, *m;
+  Py_ssize_t ksz, psz, msz;
+  PyObject *rc = 0;
+  octet pp[ED25519_PUBSZ];
+  char *kwlist[] = { "key", "msg", "pub", 0 };
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|s#:ed25519_sign", kwlist,
+                                  &k, &ksz, &m, &msz, &p, &psz))
+    goto end;
+  if (p && psz != ED25519_PUBSZ) VALERR("bad public length");
+  if (!p) { p = (const char *)pp; ed25519_pubkey(pp, k, ksz); }
+  rc = bytestring_pywrap(0, ED25519_SIGSZ);
+  ed25519_sign((octet *)PyString_AS_STRING(rc), k, ksz,
+              (const octet *)p, m, msz);
+  return (rc);
+end:
+  return (0);
+}
+
+static PyObject *meth_ed25519_verify(PyObject *me, PyObject *arg)
+{
+  const char *p, *m, *s;
+  Py_ssize_t psz, msz, ssz;
+  PyObject *rc = 0;
+  if (!PyArg_ParseTuple(arg, "s#s#s#:ed25519_verify",
+                       &p, &psz, &m, &msz, &s, &ssz))
+    goto end;
+  if (psz != ED25519_PUBSZ) VALERR("bad public length");
+  if (ssz != ED25519_SIGSZ) VALERR("bad signature length");
+  rc = getbool(!ed25519_verify((const octet *)p, m, msz, (const octet *)s));
+  return (rc);
+end:
+  return (0);
+}
+
 /*----- Global stuff ------------------------------------------------------*/
 
 static PyMethodDef methods[] = {
@@ -1132,6 +1234,16 @@ static PyMethodDef methods[] = {
   KWMETH(_pss_decode,                  0)
   KWMETH(_RSAPriv_generate,            "\
 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,                 "\
+ed25519_sign(KEY, MSG, [PUBLIC]) -> SIG")
+  METH  (ed25519_verify,               "\
+ed25519_verify(PUBLIC, MSG, SIG) -> BOOL")
 #undef METHNAME
   { 0 }
 };