X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/c142539dda1897a578bc1abc8ce372ed2a64b7b5..HEAD:/pubkey.c diff --git a/pubkey.c b/pubkey.c index 90a29b3..5680429 100644 --- a/pubkey.c +++ b/pubkey.c @@ -65,8 +65,12 @@ static PyObject *dsa_setup(PyTypeObject *ty, PyObject *G, PyObject *u, if (!u) { g->d.u = 0; u = Py_None; - } else if ((g->d.u = getmp(u)) == 0) - goto end; + } else { + if ((g->d.u = getmp(u)) == 0) + goto end; + if (MP_PYCHECK(u)) Py_INCREF(u); + else u = mp_pywrap(g->d.u); + } if (!p) { assert(g->d.u); assert(calcpub); pp = G_CREATE(GROUP_G(G)); @@ -78,7 +82,7 @@ static PyObject *dsa_setup(PyTypeObject *ty, PyObject *G, PyObject *u, 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; + g->G = G; Py_INCREF(G); g->u = u; g->p = p; g->rng = rng; Py_INCREF(rng); g->hash = hash; Py_INCREF(hash); return ((PyObject *)g); end: @@ -92,9 +96,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, @@ -108,7 +112,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) @@ -131,9 +135,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) @@ -171,9 +175,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, @@ -196,7 +200,7 @@ static PyMethodDef dsapub_pymethods[] = { 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 } }; @@ -243,7 +247,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@ */ @@ -291,7 +295,7 @@ 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@ */ @@ -319,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, @@ -344,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, @@ -362,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) @@ -385,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) @@ -433,7 +437,7 @@ static PyMethodDef kcdsapub_pymethods[] = { 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 } }; @@ -463,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@ */ @@ -511,7 +515,7 @@ 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@ */ @@ -565,9 +569,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"); @@ -632,10 +636,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, @@ -645,7 +649,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)) @@ -707,9 +711,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)) @@ -729,19 +733,22 @@ static PyObject *meth__RSAPriv_generate(PyObject *me, unsigned n = 0; rsa_priv rp; mp *e = 0; - pgev evt = { 0 }; - char *kwlist[] = { "class", "nbits", "event", "rng", "nsteps", "e", 0 }; + struct excinfo exc = EXCINFO_INIT; + pypgev evt = { { 0 } }; + static const char *const kwlist[] = + { "class", "nbits", "event", "rng", "nsteps", "e", 0 }; PyObject *rc = 0; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", kwlist, + evt.exc = &exc; + if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", KWLIST, &me, convuint, &nbits, convpgev, &evt, convgrand, &r, convuint, &n, convmp, &e)) goto end; if (e) MP_COPY(e); else e = mp_fromulong(MP_NEW, 65537); - if (rsa_gen_e(&rp, nbits, e, r, n, evt.proc, evt.ctx)) - PGENERR; + if (rsa_gen_e(&rp, nbits, e, r, n, evt.ev.proc, evt.ev.ctx)) + PGENERR(&exc); rc = rsapriv_pywrap(&rp); end: droppgev(&evt); @@ -779,7 +786,7 @@ static PyGetSetDef rsapriv_pygetset[] = { 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 } }; @@ -809,7 +816,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@ */ @@ -857,7 +864,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@ */ @@ -893,10 +901,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; @@ -923,10 +931,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; @@ -954,10 +962,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; @@ -985,10 +993,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; @@ -1016,10 +1025,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, @@ -1049,10 +1059,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, @@ -1083,10 +1094,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, @@ -1116,11 +1128,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, @@ -1196,10 +1208,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; \ @@ -1223,10 +1236,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; \ @@ -1257,7 +1271,7 @@ static PyMethodDef methods[] = { KWMETH(_pss_encode, 0) KWMETH(_pss_decode, 0) KWMETH(_RSAPriv_generate, "\ -generate(NBITS, [event = pgen_nullev, rng = rand, nsteps = 0]) -> R") +generate(NBITS, [event = pgen_nullev], [rng = rand], [nsteps = 0]) -> R") #define DEFMETH(X, x) \ METH (x, "\ " #x "(KEY, PUBLIC) -> SHARED") @@ -1267,11 +1281,11 @@ generate(NBITS, [event = pgen_nullev, rng = rand, nsteps = 0]) -> R") METH (ed##_pubkey, "\ " #ed "_pubkey(KEY) -> PUBLIC") \ KWMETH(ed##_sign, "\ -" #ed "_sign(KEY, MSG, [pub = PUBLIC, " \ - "perso = STRING, phflag = BOOL]) -> SIG") \ +" #ed "_sign(KEY, MSG, [pub = PUBLIC], " \ + "[perso = STRING], [phflag = BOOL]) -> SIG") \ KWMETH(ed##_verify, "\ " #ed "_verify(PUBLIC, MSG, SIG, " \ - "[perso = STRING, phflag = BOOL]) -> BOOL") + "[perso = STRING], [phflag = BOOL]) -> BOOL") EDDSAS(DEFMETH) #undef DEFMETH #undef METHNAME