chiark / gitweb /
algorithms.c: Check whether `setiv' and `bdry' are implemented before calling.
[catacomb-python] / algorithms.c
index 3f9d4b77edb97072288e0b10be5bc841cbeb0ae8..5b4f14b0e7d982922a8f0145efd1246c0269ada6 100644 (file)
@@ -566,6 +566,7 @@ static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg)
   Py_ssize_t sz;
 
   if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end;
+  if (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported");
   if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
   if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length");
   GC_SETIV(GCIPHER_C(me), p);
@@ -577,6 +578,7 @@ end:
 static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
 {
   if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
+  if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported");
   if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
   GC_BDRY(GCIPHER_C(me));
   RETURN_ME;
@@ -1297,7 +1299,7 @@ static PyObject *polymeth_hash(PyObject *me, PyObject *arg)
     uint##n x;                                                         \
     octet b[SZ_##W];                                                   \
     if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
-    STORE##W(b, n); poly1305_hash(P1305_CTX(me), b, sizeof(b));                \
+    STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b));                \
     RETURN_ME;                                                         \
   end:                                                                 \
     return (0);                                                                \
@@ -1312,7 +1314,7 @@ DOUINTCONV(POLYMETH_HASHU_)
     octet b[SZ_##W];                                                   \
     if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end;    \
     if (sz > MASK##n) TYERR("string too long");                                \
-    STORE##W(b, n); poly1305_hash(P1305_CTX(me), b, sizeof(b));                \
+    STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b));       \
     poly1305_hash(P1305_CTX(me), p, sz);                               \
     RETURN_ME;                                                         \
   end:                                                                 \
@@ -1335,6 +1337,13 @@ static PyObject *polymeth_flush(PyObject *me, PyObject *arg)
   RETURN_ME;
 }
 
+static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg)
+{
+  if (!PyArg_ParseTuple(arg, ":flushzero")) return (0);
+  poly1305_flushzero(P1305_CTX(me));
+  RETURN_ME;
+}
+
 static PyObject *polymeth_concat(PyObject *me, PyObject *arg)
 {
   PyObject *pre, *suff;
@@ -1385,6 +1394,7 @@ static PyMethodDef poly1305hash_pymethods[] = {
 #undef METHBUF_
   METH (hashstrz,              "P.hashstrz(STRING)")
   METH  (flush,                        "P.flush()")
+  METH  (flushzero,            "P.flushzero()")
   METH  (concat,               "P.concat(PREFIX, SUFFIX)")
   METH (done,                  "P.done() -> TAG")
 #undef METHNAME