chiark / gitweb /
algorithms.c: Add bindings for HSalsa20/r and HChaCha/r.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 7 Apr 2017 18:20:53 +0000 (19:20 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 7 Apr 2017 20:26:42 +0000 (21:26 +0100)
algorithms.c

index 4561b9ae34e9b5d503fbac233d9993dd3474b3bd..2f9889decd6e33a6f52b90aed19a464a937bd808 100644 (file)
@@ -1535,6 +1535,35 @@ static PyTypeObject poly1305hash_pytype_skel = {
   0                                    /* @tp_is_gc@ */
 };
 
+/*----- Special snowflake for HSalsa and HChaCha --------------------------*/
+
+#define DEF_HDANCE(DANCE, HDANCE, dance, hdance)                       \
+  static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg)    \
+  {                                                                    \
+    dance##_ctx dance;                                                 \
+    char *k, *n;                                                       \
+    int ksz, nsz;                                                      \
+    PyObject *rc;                                                      \
+    if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf",                 \
+                         &k, &ksz, &n, &nsz))                          \
+      goto end;                                                                \
+    if (ksz != DANCE##_KEYSZ) VALERR("bad key length");                        \
+    if (nsz != HDANCE##_INSZ) VALERR("bad input length");              \
+    rc = bytestring_pywrap(0, HSALSA20_OUTSZ);                         \
+    dance##_init(&dance, k, ksz, 0);                                   \
+    hdance##_prf(&dance, n, PyString_AS_STRING(rc));                   \
+    return (rc);                                                       \
+  end:                                                                 \
+    return (0);                                                                \
+  }
+
+DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20)
+DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012)
+DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208)
+
+DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20)
+DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12)
+DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8)
 
 /*----- Pseudorandom permutations -----------------------------------------*/
 
@@ -1801,6 +1830,17 @@ toschnorr(N) -> M: convert work factor to Schnorr group order")
 toif(N) -> M: convert work factor to integer factorization problem size")
   METH (_KeySZ_toec,           "\
 toec(N) -> M: convert work factor to elliptic curve group order")
+  METH (_KeySZ_toec,           "\
+toec(N) -> M: convert work factor to elliptic curve group order")
+#define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
+" #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
+  METH_HDANCE(hsalsa20, "HSalsa20")
+  METH_HDANCE(hsalsa2012, "HSalsa20/12")
+  METH_HDANCE(hsalsa208, "HSalsa20/8")
+  METH_HDANCE(hchacha20, "HChaCha20")
+  METH_HDANCE(hchacha12, "HChaCha12")
+  METH_HDANCE(hchacha8, "HChaCha8")
+#undef METH_DANCE
 #undef METHNAME
   { 0 }
 };