X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/e21f11bb2a7263033cb017793b6ec8ef33c91966..2dd9cb9cdfcec3238c9fb75e3fe7abd122132c5c:/algorithms.c diff --git a/algorithms.c b/algorithms.c index 888ceb0..d190a08 100644 --- a/algorithms.c +++ b/algorithms.c @@ -33,9 +33,27 @@ PRIVATE_SYMBOLS; /*----- Key sizes ---------------------------------------------------------*/ -PyTypeObject *keysz_pytype; -PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype; -PyObject *sha_pyobj, *has160_pyobj; +static PyTypeObject *keysz_pytype; +static PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype; + +typedef struct keysz_pyobj { + PyObject_HEAD + int dfl; +} keysz_pyobj; + +typedef struct keyszrange_pyobj { + PyObject_HEAD + int dfl; + int min, max, mod; +} keyszrange_pyobj; + +typedef struct keyszset_pyobj { + PyObject_HEAD + int dfl; + PyObject *set; +} keyszset_pyobj; + +#define KEYSZ_PYCHECK(o) PyObject_TypeCheck((o), keysz_pytype) #ifndef KSZ_OPMASK # define KSZ_OPMASK 0x1f @@ -175,9 +193,9 @@ static PyObject *ksget_min(PyObject *me, void *hunoz) { PyObject *set = ((keyszset_pyobj *)me)->set; int i, n, y, x = -1; - n = PyTuple_Size(set); + n = PyTuple_GET_SIZE(set); for (i = 0; i < n; i++) { - y = PyInt_AsLong(PyTuple_GetItem(set, i)); + y = PyInt_AS_LONG(PyTuple_GET_ITEM(set, i)); if (x == -1 || y < x) x = y; } return (PyInt_FromLong(x)); @@ -187,9 +205,9 @@ static PyObject *ksget_max(PyObject *me, void *hunoz) { PyObject *set = ((keyszset_pyobj *)me)->set; int i, n, y, x = -1; - n = PyTuple_Size(set); + n = PyTuple_GET_SIZE(set); for (i = 0; i < n; i++) { - y = PyInt_AsLong(PyTuple_GetItem(set, i)); + y = PyInt_AS_LONG(PyTuple_GET_ITEM(set, i)); if (y > x) x = y; } return (PyInt_FromLong(x)); @@ -198,7 +216,7 @@ static PyObject *ksget_max(PyObject *me, void *hunoz) static PyMemberDef keysz_pymembers[] = { #define MEMBERSTRUCT keysz_pyobj #define default dfl /* ugh! */ - MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size") + MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size") #undef default #undef MEMBERSTRUCT { 0 } @@ -206,33 +224,33 @@ static PyMemberDef keysz_pymembers[] = { static PyGetSetDef keyszany_pygetset[] = { #define GETSETNAME(op, name) ka##op##_##name - GET (min, "KSZ.min -> smallest allowed key size") - GET (max, "KSZ.max -> largest allowed key size") + GET (min, "KSZ.min -> smallest allowed key size") + GET (max, "KSZ.max -> largest allowed key size") #undef GETSETNAME { 0 } }; static PyMemberDef keyszrange_pymembers[] = { #define MEMBERSTRUCT keyszrange_pyobj - MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size") - MEMBER(max, T_INT, READONLY, "KSZ.max -> largest allowed key size") - MEMBER(mod, T_INT, READONLY, - "KSZ.mod -> key size must be a multiple of this") + MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size") + MEMBER(max, T_INT, READONLY, "KSZ.max -> largest allowed key size") + MEMBER(mod, T_INT, READONLY, + "KSZ.mod -> key size must be a multiple of this") #undef MEMBERSTRUCT { 0 } }; static PyGetSetDef keyszset_pygetset[] = { #define GETSETNAME(op, name) ks##op##_##name - GET (min, "KSZ.min -> smallest allowed key size") - GET (max, "KSZ.max -> largest allowed key size") + GET (min, "KSZ.min -> smallest allowed key size") + GET (max, "KSZ.max -> largest allowed key size") #undef GETSETNAME { 0 } }; static PyMemberDef keyszset_pymembers[] = { #define MEMBERSTRUCT keyszset_pyobj - MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes") + MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes") #undef MEMBERSTRUCT { 0 } }; @@ -262,7 +280,7 @@ static PyTypeObject keysz_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Key size constraints. Abstract.", + "Key size constraints. Abstract.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -310,8 +328,8 @@ static PyTypeObject keyszany_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"KeySZAny(DEFAULT)\n\ - Key size constraints. This object imposes no constraints on size.", + "KeySZAny(DEFAULT)\n" + " Key size constraints. This object imposes no constraints on size.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -359,9 +377,9 @@ static PyTypeObject keyszrange_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n\ - Key size constraints. Key size must be between MIN and MAX inclusive,\n\ - and be a multiple of MOD.", + "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n" + " Key size constraints: size must be between MIN and MAX inclusive, and\n" + " be a multiple of MOD.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -409,8 +427,8 @@ static PyTypeObject keyszset_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"KeySZSet(DEFAULT, SEQ)\n\ - Key size constraints. Key size must be DEFAULT or one in SEQ.", + "KeySZSet(DEFAULT, SEQ)\n" + " Key size constraints: size must be DEFAULT or an element of SEQ.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -453,12 +471,27 @@ KSZCONVOP(toec) /*----- Symmetric encryption ----------------------------------------------*/ -PyTypeObject *gccipher_pytype, *gcipher_pytype; +static PyTypeObject *gccipher_pytype, *gcipher_pytype; + +typedef struct gccipher_pyobj { + PyHeapTypeObject ty; + gccipher *cc; +} gccipher_pyobj; + +#define GCCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gccipher_pytype) +#define GCCIPHER_CC(o) (((gccipher_pyobj *)(o))->cc) + +typedef struct gcipher_pyobj { + PyObject_HEAD + gcipher *c; +} gcipher_pyobj; + +#define GCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gcipher_pytype) +#define GCIPHER_C(o) (((gcipher_pyobj *)(o))->c) CONVFUNC(gccipher, gccipher *, GCCIPHER_CC) -CONVFUNC(gcipher, gcipher *, GCIPHER_C) -PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c) +static PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c) { gcipher_pyobj *g; if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c)); @@ -596,21 +629,21 @@ end: static PyGetSetDef gccipher_pygetset[] = { #define GETSETNAME(op, name) gcc##op##_##name - GET (keysz, "CC.keysz -> acceptable key sizes") - GET (blksz, "CC.blksz -> block size, or zero") - GET (name, "CC.name -> name of this kind of cipher") + GET (keysz, "CC.keysz -> acceptable key sizes") + GET (blksz, "CC.blksz -> block size, or zero") + GET (name, "CC.name -> name of this kind of cipher") #undef GETSETNAME { 0 } }; static PyMethodDef gcipher_pymethods[] = { #define METHNAME(name) gcmeth_##name - METH (encrypt, "C.encrypt(PT) -> CT") - METH (enczero, "C.enczero(N) -> CT") - METH (decrypt, "C.decrypt(CT) -> PT") - METH (deczero, "C.deczero(N) -> PT") - METH (setiv, "C.setiv(IV)") - METH (bdry, "C.bdry()") + METH (encrypt, "C.encrypt(PT) -> CT") + METH (enczero, "C.enczero(N) -> CT") + METH (decrypt, "C.decrypt(CT) -> PT") + METH (deczero, "C.deczero(N) -> PT") + METH (setiv, "C.setiv(IV)") + METH (bdry, "C.bdry()") #undef METHNAME { 0 } }; @@ -640,7 +673,7 @@ static PyTypeObject gccipher_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Symmetric cipher metaclass.", + "Symmetric cipher metaclass.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -688,7 +721,7 @@ static PyTypeObject gcipher_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Symmetric cipher, abstract base class.", + "Symmetric cipher, abstract base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -713,15 +746,111 @@ static PyTypeObject gcipher_pytype_skel = { /*----- Authenticated encryption ------------------------------------------*/ -PyTypeObject *gcaead_pytype, *gaeadkey_pytype; -PyTypeObject *gcaeadaad_pytype, *gaeadaad_pytype; -PyTypeObject *gcaeadenc_pytype, *gaeadenc_pytype; -PyTypeObject *gcaeaddec_pytype, *gaeaddec_pytype; +static PyTypeObject *gcaead_pytype, *gaeadkey_pytype; +static PyTypeObject *gcaeadaad_pytype, *gaeadaad_pytype; +static PyTypeObject *gcaeadenc_pytype, *gaeadenc_pytype; +static PyTypeObject *gcaeaddec_pytype, *gaeaddec_pytype; + +typedef struct gcaead_pyobj { + PyHeapTypeObject ty; + gcaead *aec; + struct gcaeadaad_pyobj *aad; + struct gcaeadenc_pyobj *enc; + struct gcaeaddec_pyobj *dec; +} gcaead_pyobj; + +#define GCAEAD_PYCHECK(o) PyObject_TypeCheck((o), gcaead_pytype) +#define GCAEAD_AEC(o) (((gcaead_pyobj *)(o))->aec) +#define GCAEAD_AAD(o) (((gcaead_pyobj *)(o))->aad) +#define GCAEAD_ENC(o) (((gcaead_pyobj *)(o))->enc) +#define GCAEAD_DEC(o) (((gcaead_pyobj *)(o))->dec) +static PyObject *gcaead_pywrap(gcaead *); + +typedef struct gaeadkey_pyobj { + PyObject_HEAD + gaead_key *k; +} gaeadkey_pyobj; + +#define GAEADKEY_PYCHECK(o) PyObject_TypeCheck((o), gaeadkey_pytype) +#define GAEADKEY_K(o) (((gaeadkey_pyobj *)(o))->k) + +typedef struct gcaeadaad_pyobj { + PyHeapTypeObject ty; + gcaead_pyobj *key; +} gcaeadaad_pyobj; + +#define GCAEADAAD_KEY(o) (((gcaeadaad_pyobj *)(o))->key) +static PyObject *gaeadaad_pywrap(PyObject *, gaead_aad *, unsigned, size_t); + +typedef struct gaeadaad_pyobj { + PyObject_HEAD + gaead_aad *a; + unsigned f; +#define AEADF_DEAD 32768u + size_t hsz, hlen; +} gaeadaad_pyobj; -CONVFUNC(gcaead, gcaead *, GCAEAD_AEC) -CONVFUNC(gaeadkey, gaead_key *, GAEADKEY_K) +#define GAEADAAD_PYCHECK(o) PyObject_TypeCheck((o), gaeadaad_pytype) +#define GAEADAAD_A(o) (((gaeadaad_pyobj *)(o))->a) +#define GAEADAAD_F(o) (((gaeadaad_pyobj *)(o))->f) +#define GAEADAAD_HSZ(o) (((gaeadaad_pyobj *)(o))->hsz) +#define GAEADAAD_HLEN(o) (((gaeadaad_pyobj *)(o))->hlen) + +typedef struct gcaeadenc_pyobj { + PyHeapTypeObject ty; + gcaead_pyobj *key; +} gcaeadenc_pyobj; -PyObject *gaeadkey_pywrap(PyObject *cobj, gaead_key *k) +#define GCAEADENC_KEY(o) (((gcaeadenc_pyobj *)(o))->key) +static PyObject *gaeadenc_pywrap(PyObject *, gaead_enc *, unsigned, + size_t, size_t, size_t); + +typedef struct gaeadenc_pyobj { + PyObject_HEAD + gaead_enc *e; + gaeadaad_pyobj *aad; + unsigned f; + size_t hsz, msz, tsz; + size_t mlen; +} gaeadenc_pyobj; + +#define GAEADENC_PYCHECK(o) PyObject_TypeCheck((o), gaeadenc_pytype) +#define GAEADENC_AAD(o) (((gaeadenc_pyobj *)(o))->aad) +#define GAEADENC_E(o) (((gaeadenc_pyobj *)(o))->e) +#define GAEADENC_F(o) (((gaeadenc_pyobj *)(o))->f) +#define GAEADENC_HSZ(o) (((gaeadenc_pyobj *)(o))->hsz) +#define GAEADENC_MSZ(o) (((gaeadenc_pyobj *)(o))->msz) +#define GAEADENC_TSZ(o) (((gaeadenc_pyobj *)(o))->tsz) +#define GAEADENC_MLEN(o) (((gaeadenc_pyobj *)(o))->mlen) + +typedef struct gcaeaddec_pyobj { + PyHeapTypeObject ty; + gcaead_pyobj *key; +} gcaeaddec_pyobj; + +#define GCAEADDEC_KEY(o) (((gcaeaddec_pyobj *)(o))->key) +static PyObject *gaeaddec_pywrap(PyObject *, gaead_dec *, unsigned, + size_t, size_t, size_t); + +typedef struct gaeaddec_pyobj { + PyObject_HEAD + gaead_dec *d; + gaeadaad_pyobj *aad; + unsigned f; + size_t hsz, csz, tsz; + size_t clen; +} gaeaddec_pyobj; + +#define GAEADDEC_PYCHECK(o) PyObject_TypeCheck((o), gaeaddec_pytype) +#define GAEADDEC_AAD(o) (((gaeaddec_pyobj *)(o))->aad) +#define GAEADDEC_D(o) (((gaeaddec_pyobj *)(o))->d) +#define GAEADDEC_F(o) (((gaeaddec_pyobj *)(o))->f) +#define GAEADDEC_HSZ(o) (((gaeaddec_pyobj *)(o))->hsz) +#define GAEADDEC_CSZ(o) (((gaeaddec_pyobj *)(o))->csz) +#define GAEADDEC_TSZ(o) (((gaeaddec_pyobj *)(o))->tsz) +#define GAEADDEC_CLEN(o) (((gaeaddec_pyobj *)(o))->clen) + +static PyObject *gaeadkey_pywrap(PyObject *cobj, gaead_key *k) { gaeadkey_pyobj *gk; @@ -748,7 +877,7 @@ end: return (0); } -PyObject *gcaead_pywrap(gcaead *aec) +static PyObject *gcaead_pywrap(gcaead *aec) { gcaead_pyobj *gck; gcaeadaad_pyobj *gca; @@ -811,14 +940,14 @@ static PyObject *gcaeget_flags(PyObject *me, void *hunoz) static PyGetSetDef gcaead_pygetset[] = { #define GETSETNAME(op, name) gcae##op##_##name - GET (keysz, "AEC.keysz -> acceptable key sizes") - GET (noncesz, "AEC.noncesz -> acceptable nonce sizes") - GET (tagsz, "AEC.tagsz -> acceptable tag sizes") - GET (blksz, "AEC.blksz -> block size, or zero") - GET (bufsz, "AEC.bufsz -> amount of data buffered internally") - GET (ohd, "AEC.ohd -> maximum encryption overhead") - GET (name, "AEC.name -> name of this kind of AEAD scheme") - GET (flags, "AEC.flags -> mask of `AEADF_...' flags") + GET (keysz, "AEC.keysz -> acceptable key sizes") + GET (noncesz, "AEC.noncesz -> acceptable nonce sizes") + GET (tagsz, "AEC.tagsz -> acceptable tag sizes") + GET (blksz, "AEC.blksz -> block size, or zero") + GET (bufsz, "AEC.bufsz -> amount of data buffered internally") + GET (ohd, "AEC.ohd -> maximum encryption overhead") + GET (name, "AEC.name -> name of this kind of AEAD scheme") + GET (flags, "AEC.flags -> mask of `AEADF_...' flags") #undef GETSETNAME { 0 } }; @@ -915,15 +1044,15 @@ end: static PyMethodDef gaeadkey_pymethods[] = { #define METHNAME(name) gaekmeth_##name - METH (aad, "KEY.aad() -> AAD") - KWMETH(enc, "KEY.enc(NONCE, [hsz], [msz], [tsz]) -> ENC") - KWMETH(dec, "KEY.dec(NONCE, [hsz], [csz], [tsz]) -> DEC") + METH (aad, "KEY.aad() -> AAD") + KWMETH(enc, "KEY.enc(NONCE, [hsz], [msz], [tsz]) -> ENC") + KWMETH(dec, "KEY.dec(NONCE, [hsz], [csz], [tsz]) -> DEC") #undef METHNAME { 0 } }; -PyObject *gaeadaad_pywrap(PyObject *cobj, gaead_aad *a, - unsigned f, size_t hsz) +static PyObject *gaeadaad_pywrap(PyObject *cobj, gaead_aad *a, + unsigned f, size_t hsz) { gaeadaad_pyobj *ga; @@ -973,8 +1102,8 @@ static PyObject *gaeaget_hlen(PyObject *me, void *hunoz) static PyGetSetDef gaeadaad_pygetset[] = { #define GETSETNAME(op, name) gaea##op##_##name - GET (hsz, "AAD.hsz -> precommitted header length or `None'") - GET (hlen, "AAD.hlen -> header length so far") + GET (hsz, "AAD.hsz -> precommitted header length or `None'") + GET (hlen, "AAD.hlen -> header length so far") #undef GETSETNAME { 0 } }; @@ -1054,21 +1183,21 @@ static PyObject *gaeameth_hashstrz(PyObject *me, PyObject *arg) static PyMethodDef gaeadaad_pymethods[] = { #define METHNAME(name) gaeameth_##name - METH (copy, "AAD.copy() -> AAD'") - METH (hash, "AAD.hash(H)") + METH (copy, "AAD.copy() -> AAD'") + METH (hash, "AAD.hash(H)") #define METHU_(n, W, w) METH(hashu##w, "AAD.hashu" #w "(WORD)") DOUINTCONV(METHU_) #undef METHU_ #define METHBUF_(n, W, w) METH(hashbuf##w, "AAD.hashbuf" #w "(BYTES)") DOUINTCONV(METHBUF_) #undef METHBUF_ - METH (hashstrz, "AAD.hashstrz(STRING)") + METH (hashstrz, "AAD.hashstrz(STRING)") #undef METHNAME { 0 } }; -PyObject *gaeadenc_pywrap(PyObject *cobj, gaead_enc *e, unsigned f, - size_t hsz, size_t msz, size_t tsz) +static PyObject *gaeadenc_pywrap(PyObject *cobj, gaead_enc *e, unsigned f, + size_t hsz, size_t msz, size_t tsz) { gaeadenc_pyobj *ge; @@ -1110,10 +1239,10 @@ static PyObject *gaeeget_mlen(PyObject *me, void *hunoz) static PyGetSetDef gaeadenc_pygetset[] = { #define GETSETNAME(op, name) gaee##op##_##name - GET (hsz, "ENC.hsz -> precommitted header length or `None'") - GET (msz, "ENC.msz -> precommitted message length or `None'") - GET (tsz, "ENC.tsz -> precommitted tag length or `None'") - GET (mlen, "ENC.mlen -> message length so far") + GET (hsz, "ENC.hsz -> precommitted header length or `None'") + GET (msz, "ENC.msz -> precommitted message length or `None'") + GET (tsz, "ENC.tsz -> precommitted tag length or `None'") + GET (mlen, "ENC.mlen -> message length so far") #undef GETSETNAME { 0 } }; @@ -1235,16 +1364,16 @@ end: static PyMethodDef gaeadenc_pymethods[] = { #define METHNAME(name) gaeemeth_##name - METH (aad, "ENC.aad() -> AAD") - KWMETH(reinit, "ENC.reinit(NONCE, [hsz], [msz], [tsz])") - METH (encrypt, "ENC.encrypt(MSG) -> CT") - KWMETH(done, "ENC.done([tsz], [aad]) -> CT, TAG") + METH (aad, "ENC.aad() -> AAD") + KWMETH(reinit, "ENC.reinit(NONCE, [hsz], [msz], [tsz])") + METH (encrypt, "ENC.encrypt(MSG) -> CT") + KWMETH(done, "ENC.done([tsz], [aad]) -> CT, TAG") #undef METHNAME { 0 } }; -PyObject *gaeaddec_pywrap(PyObject *cobj, gaead_dec *d, unsigned f, - size_t hsz, size_t csz, size_t tsz) +static PyObject *gaeaddec_pywrap(PyObject *cobj, gaead_dec *d, unsigned f, + size_t hsz, size_t csz, size_t tsz) { gaeaddec_pyobj *gd; assert(cobj); Py_INCREF(cobj); @@ -1285,10 +1414,10 @@ static PyObject *gaedget_clen(PyObject *me, void *hunoz) static PyGetSetDef gaeaddec_pygetset[] = { #define GETSETNAME(op, name) gaed##op##_##name - GET (hsz, "DEC.hsz -> precommitted header length or `None'") - GET (csz, "DEC.csz -> precommitted ciphertext length or `None'") - GET (tsz, "DEC.tsz -> precommitted tag length or `None'") - GET (clen, "DEC.clen -> ciphertext length so far") + GET (hsz, "DEC.hsz -> precommitted header length or `None'") + GET (csz, "DEC.csz -> precommitted ciphertext length or `None'") + GET (tsz, "DEC.tsz -> precommitted tag length or `None'") + GET (clen, "DEC.clen -> ciphertext length so far") #undef GETSETNAME { 0 } }; @@ -1398,10 +1527,10 @@ end: static PyMethodDef gaeaddec_pymethods[] = { #define METHNAME(name) gaedmeth_##name - METH (aad, "DEC.aad() -> AAD") - KWMETH(reinit, "DEC.reinit(NONCE, [hsz], [csz], [tsz])") - METH (decrypt, "DEC.decrypt(CT) -> MSG") - KWMETH(done, "DEC.done(TAG, [aad]) -> MSG | None") + METH (aad, "DEC.aad() -> AAD") + KWMETH(reinit, "DEC.reinit(NONCE, [hsz], [csz], [tsz])") + METH (decrypt, "DEC.decrypt(CT) -> MSG") + KWMETH(done, "DEC.done(TAG, [aad]) -> MSG | None") #undef METHNAME { 0 } }; @@ -1431,7 +1560,7 @@ static PyTypeObject gcaead_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Authenticated encryption (key) metaclass.", + "Authenticated encryption (key) metaclass.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1479,7 +1608,7 @@ static PyTypeObject gaeadkey_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Authenticated encryption key.", + "Authenticated encryption key.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1527,7 +1656,7 @@ static PyTypeObject gcaeadaad_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Authenticated encryption additional-data hash metaclass.", + "Authenticated encryption additional-data hash metaclass.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1575,7 +1704,7 @@ static PyTypeObject gaeadaad_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Authenticated encryption AAD hash.", + "Authenticated encryption AAD hash.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1623,7 +1752,7 @@ static PyTypeObject gcaeadenc_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Authenticated encryption operation metaclass.", + "Authenticated encryption operation metaclass.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1671,7 +1800,7 @@ static PyTypeObject gaeadenc_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Authenticated encryption operation.", + "Authenticated encryption operation.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1719,7 +1848,7 @@ static PyTypeObject gcaeaddec_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Authenticated decryption operation metaclass.", + "Authenticated decryption operation metaclass.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1767,7 +1896,7 @@ static PyTypeObject gaeaddec_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Authenticated decryption operation.", + "Authenticated decryption operation.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -1792,7 +1921,17 @@ static PyTypeObject gaeaddec_pytype_skel = { /*----- Hash functions ----------------------------------------------------*/ -PyTypeObject *gchash_pytype, *ghash_pytype; +PyTypeObject *gchash_pytype; +static PyTypeObject *ghash_pytype; +PyObject *sha_pyobj, *has160_pyobj; + +typedef struct ghash_pyobj { + PyObject_HEAD + ghash *h; +} ghash_pyobj; + +#define GHASH_PYCHECK(o) PyObject_TypeCheck((o), ghash_pytype) +#define GHASH_H(o) (((ghash_pyobj *)(o))->h) CONVFUNC(gchash, gchash *, GCHASH_CH) CONVFUNC(ghash, ghash *, GHASH_H) @@ -1807,7 +1946,7 @@ end: return (0); } -PyObject *gchash_pywrap(gchash *ch) +static PyObject *gchash_pywrap(gchash *ch) { gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name); g->ch = ch; @@ -1911,25 +2050,25 @@ static PyObject *ghmeth_done(PyObject *me, PyObject *arg) static PyGetSetDef gchash_pygetset[] = { #define GETSETNAME(op, name) gch##op##_##name - GET (bufsz, "CH.bufsz -> hash buffer size, or zero") - GET (hashsz, "CH.hashsz -> hash output size") - GET (name, "CH.name -> name of this kind of hash") + GET (bufsz, "CH.bufsz -> hash buffer size, or zero") + GET (hashsz, "CH.hashsz -> hash output size") + GET (name, "CH.name -> name of this kind of hash") #undef GETSETNAME { 0 } }; static PyMethodDef ghash_pymethods[] = { #define METHNAME(name) ghmeth_##name - METH (copy, "H.copy() -> HH") - METH (hash, "H.hash(M)") + METH (copy, "H.copy() -> HH") + METH (hash, "H.hash(M)") #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)") DOUINTCONV(METHU_) #undef METHU_ #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)") DOUINTCONV(METHBUF_) #undef METHBUF_ - METH (hashstrz, "H.hashstrz(STRING)") - METH (done, "H.done() -> HASH") + METH (hashstrz, "H.hashstrz(STRING)") + METH (done, "H.done() -> HASH") #undef METHNAME { 0 } }; @@ -1959,7 +2098,7 @@ static PyTypeObject gchash_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Hash function metaclass.", + "Hash function metaclass.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -2007,7 +2146,7 @@ static PyTypeObject ghash_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Hash function, abstract base class.", + "Hash function, abstract base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -2032,11 +2171,30 @@ static PyTypeObject ghash_pytype_skel = { /*----- Message authentication --------------------------------------------*/ -PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype; +static PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype; + +typedef struct gcmac_pyobj { + PyHeapTypeObject ty; + gcmac *cm; +} gcmac_pyobj; +#define GCMAC_PYCHECK(o) PyObject_TypeCheck((o), gcmac_pytype) +#define GCMAC_CM(o) (((gcmac_pyobj *)(o))->cm) +#define GCMAC_F(o) (((gcmac_pyobj *)(o))->f) CONVFUNC(gcmac, gcmac *, GCMAC_CM) -CONVFUNC(gmac, gmac *, GMAC_M) -CONVFUNC(gmhash, ghash *, GHASH_H) +static PyObject *gmac_pywrap(PyObject *, gmac *); + +typedef struct gmac_pyobj { + PyHeapTypeObject ty; + gmac *m; +} gmac_pyobj; + +extern PyTypeObject *gmac_pytype; +#define GMAC_PYCHECK(o) PyObject_TypeCheck((o), gmac_pytype) +#define GMAC_M(o) (((gmac_pyobj *)(o))->m) +#define GMAC_F(o) (((gmac_pyobj *)(o))->f) +extern PyObject *gmac_pywrap(PyObject *, gmac *); +extern int convgmac(PyObject *, void *); static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { @@ -2065,7 +2223,7 @@ static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) return ((PyObject *)g); } -PyObject *gcmac_pywrap(gcmac *cm) +static PyObject *gcmac_pywrap(gcmac *cm) { gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name); g->cm = cm; @@ -2082,7 +2240,7 @@ PyObject *gcmac_pywrap(gcmac *cm) return ((PyObject *)g); } -PyObject *gmac_pywrap(PyObject *cobj, gmac *m) +static PyObject *gmac_pywrap(PyObject *cobj, gmac *m) { gmac_pyobj *g; if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m)); @@ -2122,9 +2280,9 @@ static PyObject *gcmget_tagsz(PyObject *me, void *hunoz) static PyGetSetDef gcmac_pygetset[] = { #define GETSETNAME(op, name) gcm##op##_##name - GET (keysz, "CM.keysz -> acceptable key sizes") - GET (tagsz, "CM.tagsz -> MAC output size") - GET (name, "CM.name -> name of this kind of MAC") + GET (keysz, "CM.keysz -> acceptable key sizes") + GET (tagsz, "CM.tagsz -> MAC output size") + GET (name, "CM.name -> name of this kind of MAC") #undef GETSETNAME { 0 } }; @@ -2154,7 +2312,7 @@ static PyTypeObject gcmac_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Message authentication code metametaclass.", + "Message authentication code metametaclass.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -2202,7 +2360,7 @@ static PyTypeObject gmac_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Message authentication code metaclass, abstract base class.", + "Message authentication code metaclass, abstract base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -2250,7 +2408,7 @@ static PyTypeObject gmhash_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Message authentication code, abstract base class.", + "Message authentication code, abstract base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -2458,29 +2616,29 @@ end: static PyGetSetDef poly1305cls_pygetset[] = { #define GETSETNAME(op, name) poly1305cls##op##_##name - GET (keysz, "PC.keysz -> acceptable key sizes") - GET (masksz, "PC.masksz -> mask size") - GET (tagsz, "PC.tagsz -> MAC output size") - GET (name, "PC.name -> name of this kind of MAC") + GET (keysz, "PC.keysz -> acceptable key sizes") + GET (masksz, "PC.masksz -> mask size") + GET (tagsz, "PC.tagsz -> MAC output size") + GET (name, "PC.name -> name of this kind of MAC") #undef GETSETNAME { 0 } }; static PyMethodDef poly1305hash_pymethods[] = { #define METHNAME(name) polymeth_##name - METH (copy, "P.copy() -> PP") - METH (hash, "P.hash(M)") + METH (copy, "P.copy() -> PP") + METH (hash, "P.hash(M)") #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)") DOUINTCONV(METHU_) #undef METHU_ #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)") DOUINTCONV(METHBUF_) #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") + 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 { 0 } }; @@ -2510,7 +2668,7 @@ static PyTypeObject poly1305cls_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Poly1305 metametaclass. Best not to ask.", + "Poly1305 metametaclass. Best not to ask.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -2558,7 +2716,7 @@ static PyTypeObject poly1305key_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"poly1305(K): Poly1305 key.", + "poly1305(K): Poly1305 key.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -2606,7 +2764,7 @@ static PyTypeObject poly1305hash_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Poly1305 MAC context base class.", + "Poly1305 MAC context base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -2769,17 +2927,17 @@ end: static PyGetSetDef kxvik_pygetset[] = { #define GETSETNAME(op, name) kxvik##op##_##name - GETSET(nround, "KECCAK.nround -> number of rounds") + GETSET(nround, "KECCAK.nround -> number of rounds") #undef GETSETNAME { 0 } }; static PyMethodDef kxvik_pymethods[] = { #define METHNAME(func) kxvikmeth_##func - METH (copy, "KECCAK.copy() -> KECCAK'") - METH (mix, "KECCAK.mix(DATA)") - METH (extract, "KECCAK.extract(NOCTETS)") - METH (step, "KECCAK.step()") + METH (copy, "KECCAK.copy() -> KECCAK'") + METH (mix, "KECCAK.mix(DATA)") + METH (extract, "KECCAK.extract(NOCTETS)") + METH (step, "KECCAK.step()") #undef METHNAME { 0 } }; @@ -2809,7 +2967,7 @@ static PyTypeObject kxvik_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Keccak1600([nround = 24]): Keccak-p[1600, n] state.", + "Keccak1600([nround = 24]): Keccak-p[1600, n] state.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -3005,28 +3163,28 @@ static PyObject *shakeget_state(PyObject *me, void *hunoz) static PyGetSetDef shake_pygetset[] = { #define GETSETNAME(op, name) shake##op##_##name - GET (rate, "S.rate -> rate, in bytes") - GET (buffered, "S.buffered -> amount currently buffered") - GET (state, "S.state -> `absorb', `squeeze', `dead'") + GET (rate, "S.rate -> rate, in bytes") + GET (buffered, "S.buffered -> amount currently buffered") + GET (state, "S.state -> `absorb', `squeeze', `dead'") #undef GETSETNAME { 0 } }; static PyMethodDef shake_pymethods[] = { #define METHNAME(func) shakemeth_##func - METH (copy, "S.copy() -> SS") - METH (hash, "S.hash(M)") + METH (copy, "S.copy() -> SS") + METH (hash, "S.hash(M)") #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)") DOUINTCONV(METHU_) #undef METHU_ #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)") DOUINTCONV(METHBUF_) #undef METHBUF_ - METH (hashstrz, "S.hashstrz(STRING)") - METH (xof, "S.xof()") - METH (done, "S.done(LEN) ->H") - METH (get, "S.get(LEN) -> H") - METH (mask, "S.mask(M) -> C") + METH (hashstrz, "S.hashstrz(STRING)") + METH (xof, "S.xof()") + METH (done, "S.done(LEN) -> H") + METH (get, "S.get(LEN) -> H") + METH (mask, "S.mask(M) -> C") #undef METHNAME { 0 } }; @@ -3056,7 +3214,7 @@ static PyTypeObject shake_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"SHAKE/cSHAKE base class.", + "SHAKE/cSHAKE/KMAC base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -3104,7 +3262,7 @@ static PyTypeObject shake128_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.", + "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -3152,7 +3310,7 @@ static PyTypeObject shake256_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.", + "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -3309,17 +3467,17 @@ end: static PyGetSetDef gcprp_pygetset[] = { #define GETSETNAME(op, name) gcp##op##_##name - GET (keysz, "CP.keysz -> acceptable key sizes") - GET (blksz, "CP.blksz -> block size") - GET (name, "CP.name -> name of this kind of PRP") + GET (keysz, "CP.keysz -> acceptable key sizes") + GET (blksz, "CP.blksz -> block size") + GET (name, "CP.name -> name of this kind of PRP") #undef GETSETNAME { 0 } }; static PyMethodDef gprp_pymethods[] = { #define METHNAME(name) gpmeth_##name - METH (encrypt, "P.encrypt(PT) -> CT") - METH (decrypt, "P.decrypt(CT) -> PT") + METH (encrypt, "P.encrypt(PT) -> CT") + METH (decrypt, "P.decrypt(CT) -> PT") #undef METHNAME { 0 } }; @@ -3349,7 +3507,7 @@ static PyTypeObject gcprp_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Pseudorandom permutation metaclass.", + "Pseudorandom permutation metaclass.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -3397,7 +3555,7 @@ static PyTypeObject gprp_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Pseudorandom permutation, abstract base class.", + "Pseudorandom permutation, abstract base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -3424,26 +3582,26 @@ static PyTypeObject gprp_pytype_skel = { static PyMethodDef methods[] = { #define METHNAME(func) meth_##func - METH (_KeySZ_fromdl, "\ -fromdl(N) -> M: convert integer discrete log field size to work factor") - METH (_KeySZ_fromschnorr, "\ -fromschnorr(N) -> M: convert Schnorr group order to work factor") - METH (_KeySZ_fromif, "\ -fromif(N) -> M: convert integer factorization problem size to work factor") - METH (_KeySZ_fromec, "\ -fromec(N) -> M: convert elliptic curve group order to work factor") - METH (_KeySZ_todl, "\ -todl(N) -> M: convert work factor to integer discrete log field size") - METH (_KeySZ_toschnorr, "\ -toschnorr(N) -> M: convert work factor to Schnorr group order") - METH (_KeySZ_toif, "\ -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 (_KeySZ_fromdl, "fromdl(N) -> M: " + "convert integer discrete log field size to work factor") + METH (_KeySZ_fromschnorr, "fromschnorr(N) -> M: " + "convert Schnorr group order to work factor") + METH (_KeySZ_fromif, "fromif(N) -> M: " + "convert integer factorization problem size to work factor") + METH (_KeySZ_fromec, "fromec(N) -> M: " + "convert elliptic curve group order to work factor") + METH (_KeySZ_todl, "todl(N) -> M: " + "convert work factor to integer discrete log field size") + METH (_KeySZ_toschnorr, "toschnorr(N) -> M: " + "convert work factor to Schnorr group order") + METH (_KeySZ_toif, "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")