X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/930d78e3e2045f6f810c73edd81f44321b039ea5..2dd9cb9cdfcec3238c9fb75e3fe7abd122132c5c:/pubkey.c diff --git a/pubkey.c b/pubkey.c index 77f0b93..ad3bcd4 100644 --- a/pubkey.c +++ b/pubkey.c @@ -86,8 +86,7 @@ static PyObject *dsa_setup(PyTypeObject *ty, PyObject *G, PyObject *u, g->rng = rng; Py_INCREF(rng); g->hash = hash; Py_INCREF(hash); return ((PyObject *)g); end: - if (p) Py_DECREF(p); - FREEOBJ(g); + Py_XDECREF(p); FREEOBJ(g); return (0); } @@ -96,9 +95,9 @@ static PyObject *dsapub_pynew(PyTypeObject *ty, { PyObject *G, *p, *rng = rand_pyobj, *hash = sha_pyobj; PyObject *rc = 0; - char *kwlist[] = { "G", "p", "hash", "rng", 0 }; + static const char *const kwlist[] = { "G", "p", "hash", "rng", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!|O!O!:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!|O!O!:new", KWLIST, group_pytype, &G, ge_pytype, &p, gchash_pytype, &hash, @@ -112,7 +111,7 @@ end: static PyObject *dsameth_beginhash(PyObject *me, PyObject *arg) { if (!PyArg_ParseTuple(arg, ":beginhash")) return (0); - return (ghash_pywrap(DSA_HASH(me), gdsa_beginhash(DSA_D(me)), f_freeme)); + return (ghash_pywrap(DSA_HASH(me), gdsa_beginhash(DSA_D(me)))); } static PyObject *dsameth_endhash(PyObject *me, PyObject *arg) @@ -135,9 +134,9 @@ static PyObject *dsameth_sign(PyObject *me, PyObject *arg, PyObject *kw) Py_ssize_t n; mp *k = 0; PyObject *rc = 0; - char *kwlist[] = { "msg", "k", 0 }; + static const char *const kwlist[] = { "msg", "k", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:sign", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:sign", KWLIST, &p, &n, convmp, &k)) goto end; if (n != DSA_D(me)->h->hashsz) @@ -175,9 +174,9 @@ static PyObject *dsapriv_pynew(PyTypeObject *ty, { PyObject *G, *p = 0, *u, *rng = rand_pyobj, *hash = sha_pyobj; PyObject *rc = 0; - char *kwlist[] = { "G", "u", "p", "hash", "rng", 0 }; + static const char *const 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, &u, ge_pytype, &p, @@ -191,16 +190,16 @@ end: static PyMethodDef dsapub_pymethods[] = { #define METHNAME(name) dsameth_##name - METH (beginhash, "D.beginhash() -> hash object") - METH (endhash, "D.endhash(H) -> BYTES") - METH (verify, "D.verify(MSG, (R, S)) -> true/false") + METH (beginhash, "D.beginhash() -> hash object") + METH (endhash, "D.endhash(H) -> BYTES") + METH (verify, "D.verify(MSG, (R, S)) -> true/false") #undef METHNAME { 0 } }; static PyMethodDef dsapriv_pymethods[] = { #define METHNAME(name) dsameth_##name - KWMETH(sign, "D.sign(MSG, [k = K]) -> R, S") + KWMETH(sign, "D.sign(MSG, [k = K]) -> R, S") #undef METHNAME { 0 } }; @@ -247,7 +246,7 @@ static PyTypeObject dsapub_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"DSA public key information.", + "DSAPub(GROUP, P, [hash = sha], [rng = rand]): DSA public key.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -295,7 +294,8 @@ static PyTypeObject dsapriv_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"DSA private key information.", + "DSAPriv(GROUP, U, [p = u G], [hash = sha], [rng = rand]): " + "DSA private key.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -323,9 +323,9 @@ static PyObject *kcdsapub_pynew(PyTypeObject *ty, { PyObject *G, *p, *rng = rand_pyobj, *hash = has160_pyobj; PyObject *rc = 0; - char *kwlist[] = { "G", "p", "hash", "rng", 0 }; + static const char *const kwlist[] = { "G", "p", "hash", "rng", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!|O!O!:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!|O!O!:new", KWLIST, group_pytype, &G, ge_pytype, &p, gchash_pytype, &hash, @@ -348,9 +348,9 @@ static PyObject *kcdsapriv_pynew(PyTypeObject *ty, { PyObject *G, *u, *p = 0, *rng = rand_pyobj, *hash = has160_pyobj; PyObject *rc = 0; - char *kwlist[] = { "G", "u", "p", "hash", "rng", 0 }; + static const char *const 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, &u, ge_pytype, &p, @@ -366,7 +366,7 @@ end: static PyObject *kcdsameth_beginhash(PyObject *me, PyObject *arg) { if (!PyArg_ParseTuple(arg, ":beginhash")) return (0); - return (ghash_pywrap(DSA_HASH(me), gkcdsa_beginhash(DSA_D(me)), f_freeme)); + return (ghash_pywrap(DSA_HASH(me), gkcdsa_beginhash(DSA_D(me)))); } static PyObject *kcdsameth_endhash(PyObject *me, PyObject *arg) @@ -389,9 +389,9 @@ static PyObject *kcdsameth_sign(PyObject *me, PyObject *arg, PyObject *kw) Py_ssize_t n; mp *k = 0; PyObject *r = 0, *rc = 0; - char *kwlist[] = { "msg", "k", 0 }; + static const char *const kwlist[] = { "msg", "k", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:sign", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:sign", KWLIST, &p, &n, convmp, &k)) goto end; if (n != DSA_D(me)->h->hashsz) @@ -428,16 +428,16 @@ end: static PyMethodDef kcdsapub_pymethods[] = { #define METHNAME(name) kcdsameth_##name - METH (beginhash, "D.beginhash() -> hash object") - METH (endhash, "D.endhash(H) -> BYTES") - METH (verify, "D.verify(MSG, (R, S)) -> true/false") + METH (beginhash, "D.beginhash() -> hash object") + METH (endhash, "D.endhash(H) -> BYTES") + METH (verify, "D.verify(MSG, (R, S)) -> true/false") #undef METHNAME { 0 } }; static PyMethodDef kcdsapriv_pymethods[] = { #define METHNAME(name) kcdsameth_##name - KWMETH(sign, "D.sign(MSG, [k = K]) -> R, S") + KWMETH(sign, "D.sign(MSG, [k = K]) -> R, S") #undef METHNAME { 0 } }; @@ -467,7 +467,7 @@ static PyTypeObject kcdsapub_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"KCDSA public key information.", + "KCDSAPub(GROUP, P, [hash = sha], [rng = rand]): KCDSA public key.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -515,7 +515,8 @@ static PyTypeObject kcdsapriv_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"KCDSA private key information.", + "KCDSAPriv(GROUP, U, [p = u G], [hash = sha], [rng = rand]): " + "KCDSA private key.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -569,9 +570,9 @@ static PyObject *rsapub_pynew(PyTypeObject *ty, { rsa_pub rp = { 0 }; rsapub_pyobj *o; - char *kwlist[] = { "n", "e", 0 }; + static const char *const kwlist[] = { "n", "e", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", KWLIST, convmp, &rp.n, convmp, &rp.e)) goto end; if (!MP_ODDP(rp.n)) VALERR("RSA modulus must be even"); @@ -636,10 +637,10 @@ static PyObject *rsapriv_pynew(PyTypeObject *ty, { rsa_priv rp = { 0 }; PyObject *rng = Py_None; - char *kwlist[] = + static const char *const kwlist[] = { "n", "e", "d", "p", "q", "dp", "dq", "q_inv", "rng", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&O&O&O&O&O&O&O&O:new", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&O&O&O&O&O&O&O&O:new", KWLIST, convmp, &rp.n, convmp, &rp.e, convmp, &rp.d, convmp, &rp.p, convmp, &rp.q, @@ -649,7 +650,7 @@ static PyObject *rsapriv_pynew(PyTypeObject *ty, goto end; if ((rp.n && !MP_ODDP(rp.n)) || (rp.p && !MP_ODDP(rp.p)) || - (rp.p && !MP_ODDP(rp.q))) + (rp.q && !MP_ODDP(rp.q))) VALERR("RSA modulus and factors must be odd"); if (rsa_recover(&rp)) VALERR("couldn't construct private key"); if (rng != Py_None && !GRAND_PYCHECK(rng)) @@ -711,9 +712,9 @@ static PyObject *rsameth_privop(PyObject *me, PyObject *arg, PyObject *kw) PyObject *rng = RSA_RNG(me); mp *x = 0; PyObject *rc = 0; - char *kwlist[] = { "x", "rng", 0 }; + static const char *const kwlist[] = { "x", "rng", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O:privop", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O:privop", KWLIST, convmp, &x, &rng)) goto end; if (rng != Py_None && !GRAND_PYCHECK(rng)) @@ -735,11 +736,12 @@ static PyObject *meth__RSAPriv_generate(PyObject *me, mp *e = 0; struct excinfo exc = EXCINFO_INIT; pypgev evt = { { 0 } }; - char *kwlist[] = { "class", "nbits", "event", "rng", "nsteps", "e", 0 }; + static const char *const kwlist[] = + { "class", "nbits", "event", "rng", "nsteps", "e", 0 }; PyObject *rc = 0; evt.exc = &exc; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", KWLIST, &me, convuint, &nbits, convpgev, &evt, convgrand, &r, convuint, &n, convmp, &e)) @@ -757,35 +759,35 @@ end: static PyGetSetDef rsapub_pygetset[] = { #define GETSETNAME(op, name) rsa##op##_##name - GET (n, "R.n -> N") - GET (e, "R.e -> E") + GET (n, "R.n -> N") + GET (e, "R.e -> E") #undef GETSETNAME { 0 } }; static PyMethodDef rsapub_pymethods[] = { #define METHNAME(name) rsameth_##name - METH (pubop, "R.pubop(X) -> X^E (mod N)") + METH (pubop, "R.pubop(X) -> X^E (mod N)") #undef METHNAME { 0 } }; static PyGetSetDef rsapriv_pygetset[] = { #define GETSETNAME(op, name) rsa##op##_##name - GET (d, "R.d -> D") - GET (p, "R.p -> P") - GET (q, "R.q -> Q") - GET (dp, "R.dp -> D mod (P - 1)") - GET (dq, "R.dq -> D mod (Q - 1)") - GET (q_inv, "R.q_inv -> Q^{-1} mod P") - GETSET(rng, "R.rng -> random number source for blinding") + GET (d, "R.d -> D") + GET (p, "R.p -> P") + GET (q, "R.q -> Q") + GET (dp, "R.dp -> D mod (P - 1)") + GET (dq, "R.dq -> D mod (Q - 1)") + GET (q_inv, "R.q_inv -> Q^{-1} mod P") + GETSET(rng, "R.rng -> random number source for blinding") #undef GETSETNAME { 0 } }; static PyMethodDef rsapriv_pymethods[] = { #define METHNAME(name) rsameth_##name - KWMETH(privop, "R.privop(X, [rng = None]) -> X^D (mod N)") + KWMETH(privop, "R.privop(X, [rng = None]) -> X^D (mod N)") #undef METHNAME { 0 } }; @@ -815,7 +817,7 @@ static PyTypeObject rsapub_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"RSA public key information.", + "RSAPub(N, E): RSA public key.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -863,7 +865,8 @@ static PyTypeObject rsapriv_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"RSA private key information.", + "RSAPriv(..., [rng = rand]): RSA private key.\n" + " Keywords: n, e, d, p, q, dp, dq, q_inv; must provide enough", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -899,10 +902,10 @@ static PyObject *meth__p1crypt_encode(PyObject *me, octet *b = 0; size_t sz; mp *x; - char *kwlist[] = { "msg", "nbits", "ep", "rng", 0 }; + static const char *const kwlist[] = { "msg", "nbits", "ep", "rng", 0 }; p1.r = &rand_global; ep = 0; epsz = 0; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|s#O&:encode", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|s#O&:encode", KWLIST, &m, &msz, convulong, &nbits, &ep, &epsz, convgrand, &p1.r)) goto end; @@ -929,10 +932,10 @@ static PyObject *meth__p1crypt_decode(PyObject *me, octet *b = 0; size_t sz; mp *x = 0; - char *kwlist[] = { "ct", "nbits", "ep", "rng", 0 }; + static const char *const kwlist[] = { "ct", "nbits", "ep", "rng", 0 }; p1.r = &rand_global; ep = 0; epsz = 0; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|s#O&:decode", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|s#O&:decode", KWLIST, convmp, &x, convulong, &nbits, &ep, &epsz, convgrand, &p1.r)) goto end; @@ -960,10 +963,10 @@ static PyObject *meth__p1sig_encode(PyObject *me, octet *b = 0; size_t sz; mp *x; - char *kwlist[] = { "msg", "nbits", "ep", "rng", 0 }; + static const char *const kwlist[] = { "msg", "nbits", "ep", "rng", 0 }; p1.r = &rand_global; ep = 0; epsz = 0; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|s#O&:encode", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|s#O&:encode", KWLIST, &m, &msz, convulong, &nbits, &ep, &epsz, convgrand, &p1.r)) goto end; @@ -991,10 +994,11 @@ static PyObject *meth__p1sig_decode(PyObject *me, octet *b = 0; size_t sz; mp *x = 0; - char *kwlist[] = { "msg", "sig", "nbits", "ep", "rng", 0 }; + static const char *const kwlist[] = + { "msg", "sig", "nbits", "ep", "rng", 0 }; p1.r = &rand_global; ep = 0; epsz = 0; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|s#O&:decode", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|s#O&:decode", KWLIST, &hukairz, convmp, &x, convulong, &nbits, &ep, &epsz, convgrand, &p1.r)) goto end; @@ -1022,10 +1026,11 @@ static PyObject *meth__oaep_encode(PyObject *me, octet *b = 0; size_t sz; mp *x; - char *kwlist[] = { "msg", "nbits", "mgf", "hash", "ep", "rng", 0 }; + static const char *const kwlist[] = + { "msg", "nbits", "mgf", "hash", "ep", "rng", 0 }; o.r = &rand_global; o.cc = &sha_mgf; o.ch = &sha; ep = 0; epsz = 0; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|O&O&s#O&:encode", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|O&O&s#O&:encode", KWLIST, &m, &msz, convulong, &nbits, convgccipher, &o.cc, convgchash, &o.ch, @@ -1055,10 +1060,11 @@ static PyObject *meth__oaep_decode(PyObject *me, octet *b = 0; size_t sz; mp *x = 0; - char *kwlist[] = { "ct", "nbits", "mgf", "hash", "ep", "rng", 0 }; + static const char *const kwlist[] = + { "ct", "nbits", "mgf", "hash", "ep", "rng", 0 }; o.r = &rand_global; o.cc = &sha_mgf; o.ch = &sha; ep = 0; epsz = 0; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&O&s#O&:decode", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&O&s#O&:decode", KWLIST, convmp, &x, convulong, &nbits, convgccipher, &o.cc, convgchash, &o.ch, @@ -1089,10 +1095,11 @@ static PyObject *meth__pss_encode(PyObject *me, octet *b = 0; size_t sz; mp *x = 0; - char *kwlist[] = { "msg", "nbits", "mgf", "hash", "saltsz", "rng", 0 }; + static const char *const kwlist[] = + { "msg", "nbits", "mgf", "hash", "saltsz", "rng", 0 }; p.cc = &sha_mgf; p.ch = &sha; p.r = &rand_global; p.ssz = (size_t)-1; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|O&O&O&O&:encode", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|O&O&O&O&:encode", KWLIST, &m, &msz, convulong, &nbits, convgccipher, &p.cc, convgchash, &p.ch, @@ -1122,11 +1129,11 @@ static PyObject *meth__pss_decode(PyObject *me, size_t sz; int n; mp *x = 0; - char *kwlist[] = + static const char *const kwlist[] = { "msg", "sig", "nbits", "mgf", "hash", "saltsz", "rng", 0 }; p.cc = &sha_mgf; p.ch = &sha; p.r = &rand_global; p.ssz = (size_t)-1; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&O&|O&O&O&O&:decode", kwlist, + if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&O&|O&O&O&O&:decode", KWLIST, &m, &msz, convmp, &x, convulong, &nbits, convgccipher, &p.cc, convgchash, &p.ch, @@ -1202,10 +1209,11 @@ XDHS(DEFXDH) int ph = phdflt; \ PyObject *rc = 0; \ octet pp[ED##_PUBSZ]; \ - char *kwlist[] = { "key", "msg", "pub", "perso", "phflag", 0 }; \ + static const char *const kwlist[] = \ + { "key", "msg", "pub", "perso", "phflag", 0 }; \ if (!PyArg_ParseTupleAndKeywords(arg, kw, \ "s#s#|s#s#O&:" #ed "_sign", \ - kwlist, \ + KWLIST, \ &k, &ksz, &m, &msz, &p, &psz, \ &c, &csz, convbool, &ph)) \ goto end; \ @@ -1229,10 +1237,11 @@ XDHS(DEFXDH) Py_ssize_t psz, csz = 0, msz, ssz; \ int ph = phdflt; \ PyObject *rc = 0; \ - char *kwlist[] = { "pub", "msg", "sig", "perso", "phflag", 0 }; \ + static const char *const kwlist[] = \ + { "pub", "msg", "sig", "perso", "phflag", 0 }; \ if (!PyArg_ParseTupleAndKeywords(arg, kw, \ "s#s#s#|s#O&:" #ed "_verify", \ - kwlist, \ + KWLIST, \ &p, &psz, &m, &msz, &s, &ssz, \ &c, &csz, convbool, &ph)) \ goto end; \ @@ -1254,30 +1263,26 @@ EDDSAS(DEFEDDSA) static PyMethodDef methods[] = { #define METHNAME(name) meth_##name - KWMETH(_p1crypt_encode, 0) - KWMETH(_p1crypt_decode, 0) - KWMETH(_p1sig_encode, 0) - KWMETH(_p1sig_decode, 0) - KWMETH(_oaep_encode, 0) - KWMETH(_oaep_decode, 0) - KWMETH(_pss_encode, 0) - KWMETH(_pss_decode, 0) - KWMETH(_RSAPriv_generate, "\ -generate(NBITS, [event = pgen_nullev], [rng = rand], [nsteps = 0]) -> R") + KWMETH(_p1crypt_encode, 0) + KWMETH(_p1crypt_decode, 0) + KWMETH(_p1sig_encode, 0) + KWMETH(_p1sig_decode, 0) + KWMETH(_oaep_encode, 0) + KWMETH(_oaep_decode, 0) + KWMETH(_pss_encode, 0) + KWMETH(_pss_decode, 0) + KWMETH(_RSAPriv_generate, "generate(NBITS, [event = pgen_nullev], " + "[rng = rand], [nsteps = 0]) -> R") #define DEFMETH(X, x) \ - METH (x, "\ -" #x "(KEY, PUBLIC) -> SHARED") + METH (x, "" #x "(KEY, PUBLIC) -> SHARED") XDHS(DEFMETH) #undef DEFMETH #define DEFMETH(ED, ed, phdflt, sigver) \ - METH (ed##_pubkey, "\ -" #ed "_pubkey(KEY) -> PUBLIC") \ - KWMETH(ed##_sign, "\ -" #ed "_sign(KEY, MSG, [pub = PUBLIC], " \ - "[perso = STRING], [phflag = BOOL]) -> SIG") \ - KWMETH(ed##_verify, "\ -" #ed "_verify(PUBLIC, MSG, SIG, " \ - "[perso = STRING], [phflag = BOOL]) -> BOOL") + METH (ed##_pubkey, "" #ed "_pubkey(KEY) -> PUBLIC") \ + KWMETH(ed##_sign, "" #ed "_sign(KEY, MSG, [pub = PUBLIC], " \ + "[perso = STRING], [phflag = BOOL]) -> SIG") \ + KWMETH(ed##_verify, "" #ed "_verify(PUBLIC, MSG, SIG, " \ + "[perso = STRING], [phflag = BOOL]) -> BOOL") EDDSAS(DEFMETH) #undef DEFMETH #undef METHNAME