*.c: Use Python's facilities for defining class and static methods.
[catacomb-python] / pubkey.c
CommitLineData
d7ab1bab 1/* -*-c-*-
2 *
d7ab1bab 3 * Public-key cryptography
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
b2687a0a 8/*----- Licensing notice --------------------------------------------------*
d7ab1bab 9 *
10 * This file is part of the Python interface to Catacomb.
11 *
12 * Catacomb/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
b2687a0a 16 *
d7ab1bab 17 * Catacomb/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
b2687a0a 21 *
d7ab1bab 22 * You should have received a copy of the GNU General Public License
23 * along with Catacomb/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27/*----- Header files ------------------------------------------------------*/
28
29#include "catacomb-python.h"
30
31/*----- DSA and similar ---------------------------------------------------*/
32
33typedef struct dsa_pyobj {
34 PyObject_HEAD
35 PyObject *G, *u, *p, *rng, *hash;
36 gdsa d;
37} dsa_pyobj;
38
39static PyTypeObject *dsapub_pytype, *dsapriv_pytype;
40static PyTypeObject *kcdsapub_pytype, *kcdsapriv_pytype;
41#define DSA_D(o) (&((dsa_pyobj *)(o))->d)
42#define DSA_G(o) (((dsa_pyobj *)(o))->G)
43#define DSA_U(o) (((dsa_pyobj *)(o))->u)
44#define DSA_P(o) (((dsa_pyobj *)(o))->p)
45#define DSA_RNG(o) (((dsa_pyobj *)(o))->rng)
46#define DSA_HASH(o) (((dsa_pyobj *)(o))->hash)
47
48static void dsa_pydealloc(PyObject *me)
49{
50 dsa_pyobj *g = (dsa_pyobj *)me;
51 Py_DECREF(g->G); Py_DECREF(g->u); Py_DECREF(g->p);
52 Py_DECREF(g->rng); Py_DECREF(g->hash);
3aa33042 53 FREEOBJ(me);
d7ab1bab 54}
55
56static PyObject *dsa_setup(PyTypeObject *ty, PyObject *G, PyObject *u,
16178155
MW
57 PyObject *p, PyObject *rng, PyObject *hash,
58 void (*calcpub)(group *, ge *, mp *))
d7ab1bab 59{
60 dsa_pyobj *g;
16178155 61 ge *pp;
d7ab1bab 62
63 g = PyObject_New(dsa_pyobj, ty);
16178155 64 if (p) Py_INCREF(p);
d7ab1bab 65 if (!u) {
66 g->d.u = 0;
67 u = Py_None;
ff672277
MW
68 } else {
69 if ((g->d.u = getmp(u)) == 0)
70 goto end;
71 if (MP_PYCHECK(u)) Py_INCREF(u);
72 else u = mp_pywrap(g->d.u);
73 }
16178155
MW
74 if (!p) {
75 assert(g->d.u); assert(calcpub);
76 pp = G_CREATE(GROUP_G(G));
77 calcpub(GROUP_G(G), pp, g->d.u);
78 p = ge_pywrap(G, pp);
79 } else if (GROUP_G(G) != GE_G(p) && !group_samep(GROUP_G(G), GE_G(p)))
80 TYERR("public key not from group");
d7ab1bab 81 g->d.g = GROUP_G(G);
82 g->d.p = GE_X(p);
83 g->d.r = GRAND_R(rng);
84 g->d.h = GCHASH_CH(hash);
ff672277 85 g->G = G; Py_INCREF(G); g->u = u; g->p = p;
9ca1789e 86 g->rng = rng; Py_INCREF(rng); g->hash = hash; Py_INCREF(hash);
d7ab1bab 87 return ((PyObject *)g);
88end:
19a4c416 89 Py_XDECREF(p); FREEOBJ(g);
d7ab1bab 90 return (0);
91}
92
93static PyObject *dsapub_pynew(PyTypeObject *ty,
94 PyObject *arg, PyObject *kw)
95{
16178155 96 PyObject *G, *p, *rng = rand_pyobj, *hash = sha_pyobj;
d7ab1bab 97 PyObject *rc = 0;
827f89d7 98 static const char *const kwlist[] = { "G", "p", "hash", "rng", 0 };
d7ab1bab 99
827f89d7 100 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!|O!O!:new", KWLIST,
d7ab1bab 101 group_pytype, &G,
102 ge_pytype, &p,
d7ab1bab 103 gchash_pytype, &hash,
104 grand_pytype, &rng) ||
16178155 105 (rc = dsa_setup(dsapub_pytype, G, 0, p, rng, hash, 0)) == 0)
d7ab1bab 106 goto end;
107end:
108 return (rc);
109}
110
111static PyObject *dsameth_beginhash(PyObject *me, PyObject *arg)
112{
113 if (!PyArg_ParseTuple(arg, ":beginhash")) return (0);
c41d0371 114 return (ghash_pywrap(DSA_HASH(me), gdsa_beginhash(DSA_D(me))));
d7ab1bab 115}
116
117static PyObject *dsameth_endhash(PyObject *me, PyObject *arg)
118{
119 ghash *h;
120 PyObject *rc;
121 if (!PyArg_ParseTuple(arg, "O&:endhash", convghash, &h)) return (0);
122 gdsa_endhash(DSA_D(me), h);
123 h = GH_COPY(h);
124 rc = bytestring_pywrap(0, GH_CLASS(h)->hashsz);
125 GH_DONE(h, PyString_AS_STRING(rc));
126 GH_DESTROY(h);
127 return (rc);
128}
129
130static PyObject *dsameth_sign(PyObject *me, PyObject *arg, PyObject *kw)
131{
132 gdsa_sig s = GDSA_SIG_INIT;
133 char *p;
6b54260d 134 Py_ssize_t n;
d7ab1bab 135 mp *k = 0;
136 PyObject *rc = 0;
827f89d7 137 static const char *const kwlist[] = { "msg", "k", 0 };
d7ab1bab 138
827f89d7 139 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:sign", KWLIST,
d7ab1bab 140 &p, &n, convmp, &k))
141 goto end;
142 if (n != DSA_D(me)->h->hashsz)
143 VALERR("bad message length (doesn't match hash size)");
144 gdsa_sign(DSA_D(me), &s, p, k);
145 rc = Py_BuildValue("(NN)", mp_pywrap(s.r), mp_pywrap(s.s));
146end:
147 mp_drop(k);
148 return (rc);
149}
150
151static PyObject *dsameth_verify(PyObject *me, PyObject *arg)
152{
153 char *p;
6b54260d 154 Py_ssize_t n;
d7ab1bab 155 gdsa_sig s = GDSA_SIG_INIT;
156 PyObject *rc = 0;
157
158 if (!PyArg_ParseTuple(arg, "s#(O&O&):verify",
159 &p, &n, convmp, &s.r, convmp, &s.s))
160 goto end;
161 if (n != DSA_D(me)->h->hashsz)
162 VALERR("bad message length (doesn't match hash size)");
9ca1789e 163 rc = getbool(!gdsa_verify(DSA_D(me), &s, p));
d7ab1bab 164end:
165 mp_drop(s.r);
166 mp_drop(s.s);
167 return (rc);
168}
169
16178155
MW
170static void dsa_calcpub(group *g, ge *p, mp *u) { G_EXP(g, p, g->g, u); }
171
d7ab1bab 172static PyObject *dsapriv_pynew(PyTypeObject *ty,
173 PyObject *arg, PyObject *kw)
174{
16178155 175 PyObject *G, *p = 0, *u, *rng = rand_pyobj, *hash = sha_pyobj;
d7ab1bab 176 PyObject *rc = 0;
827f89d7 177 static const char *const kwlist[] = { "G", "u", "p", "hash", "rng", 0 };
d7ab1bab 178
827f89d7 179 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O|O!O!O!:new", KWLIST,
d7ab1bab 180 group_pytype, &G,
d7ab1bab 181 &u,
16178155 182 ge_pytype, &p,
d7ab1bab 183 gchash_pytype, &hash,
184 grand_pytype, &rng) ||
16178155 185 (rc = dsa_setup(dsapriv_pytype, G, u, p, rng, hash, dsa_calcpub)) == 0)
d7ab1bab 186 goto end;
187end:
188 return (rc);
189}
190
c90f712e 191static const PyMethodDef dsapub_pymethods[] = {
d7ab1bab 192#define METHNAME(name) dsameth_##name
d96c882e
MW
193 METH (beginhash, "D.beginhash() -> hash object")
194 METH (endhash, "D.endhash(H) -> BYTES")
195 METH (verify, "D.verify(MSG, (R, S)) -> true/false")
d7ab1bab 196#undef METHNAME
197 { 0 }
198};
199
c90f712e 200static const PyMethodDef dsapriv_pymethods[] = {
d7ab1bab 201#define METHNAME(name) dsameth_##name
d96c882e 202 KWMETH(sign, "D.sign(MSG, [k = K]) -> R, S")
d7ab1bab 203#undef METHNAME
204 { 0 }
205};
206
c90f712e 207static const PyMemberDef dsapub_pymembers[] = {
d7ab1bab 208#define MEMBERSTRUCT dsa_pyobj
209 MEMBER(G, T_OBJECT, READONLY, "D.G -> group to work in")
210 MEMBER(p, T_OBJECT, READONLY, "D.p -> public key (group element")
211 MEMBER(rng, T_OBJECT, READONLY, "D.rng -> random number generator")
b2687a0a 212 MEMBER(hash, T_OBJECT, READONLY, "D.hash -> hash class")
d7ab1bab 213#undef MEMBERSTRUCT
214 { 0 }
215};
216
c90f712e 217static const PyMemberDef dsapriv_pymembers[] = {
d7ab1bab 218#define MEMBERSTRUCT dsa_pyobj
219 MEMBER(u, T_OBJECT, READONLY, "D.u -> private key (exponent)")
220#undef MEMBERSTRUCT
221 { 0 }
222};
223
224static PyTypeObject dsapub_pytype_skel = {
6d4db0bf 225 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 226 "DSAPub", /* @tp_name@ */
d7ab1bab 227 sizeof(dsa_pyobj), /* @tp_basicsize@ */
228 0, /* @tp_itemsize@ */
229
230 dsa_pydealloc, /* @tp_dealloc@ */
231 0, /* @tp_print@ */
232 0, /* @tp_getattr@ */
233 0, /* @tp_setattr@ */
234 0, /* @tp_compare@ */
235 0, /* @tp_repr@ */
236 0, /* @tp_as_number@ */
237 0, /* @tp_as_sequence@ */
238 0, /* @tp_as_mapping@ */
239 0, /* @tp_hash@ */
240 0, /* @tp_call@ */
241 0, /* @tp_str@ */
242 0, /* @tp_getattro@ */
243 0, /* @tp_setattro@ */
244 0, /* @tp_as_buffer@ */
245 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
246 Py_TPFLAGS_BASETYPE,
247
248 /* @tp_doc@ */
d96c882e 249 "DSAPub(GROUP, P, [hash = sha], [rng = rand]): DSA public key.",
d7ab1bab 250
251 0, /* @tp_traverse@ */
252 0, /* @tp_clear@ */
253 0, /* @tp_richcompare@ */
254 0, /* @tp_weaklistoffset@ */
255 0, /* @tp_iter@ */
963a6148 256 0, /* @tp_iternext@ */
c90f712e
MW
257 PYMETHODS(dsapub), /* @tp_methods@ */
258 PYMEMBERS(dsapub), /* @tp_members@ */
d7ab1bab 259 0, /* @tp_getset@ */
260 0, /* @tp_base@ */
261 0, /* @tp_dict@ */
262 0, /* @tp_descr_get@ */
263 0, /* @tp_descr_set@ */
264 0, /* @tp_dictoffset@ */
265 0, /* @tp_init@ */
266 PyType_GenericAlloc, /* @tp_alloc@ */
267 dsapub_pynew, /* @tp_new@ */
3aa33042 268 0, /* @tp_free@ */
d7ab1bab 269 0 /* @tp_is_gc@ */
270};
271
272static PyTypeObject dsapriv_pytype_skel = {
6d4db0bf 273 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 274 "DSAPriv", /* @tp_name@ */
d7ab1bab 275 sizeof(dsa_pyobj), /* @tp_basicsize@ */
276 0, /* @tp_itemsize@ */
277
278 0, /* @tp_dealloc@ */
279 0, /* @tp_print@ */
280 0, /* @tp_getattr@ */
281 0, /* @tp_setattr@ */
282 0, /* @tp_compare@ */
283 0, /* @tp_repr@ */
284 0, /* @tp_as_number@ */
285 0, /* @tp_as_sequence@ */
286 0, /* @tp_as_mapping@ */
287 0, /* @tp_hash@ */
288 0, /* @tp_call@ */
289 0, /* @tp_str@ */
290 0, /* @tp_getattro@ */
291 0, /* @tp_setattro@ */
292 0, /* @tp_as_buffer@ */
293 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
294 Py_TPFLAGS_BASETYPE,
295
296 /* @tp_doc@ */
d96c882e
MW
297 "DSAPriv(GROUP, U, [p = u G], [hash = sha], [rng = rand]): "
298 "DSA private key.",
d7ab1bab 299
300 0, /* @tp_traverse@ */
301 0, /* @tp_clear@ */
302 0, /* @tp_richcompare@ */
303 0, /* @tp_weaklistoffset@ */
304 0, /* @tp_iter@ */
963a6148 305 0, /* @tp_iternext@ */
c90f712e
MW
306 PYMETHODS(dsapriv), /* @tp_methods@ */
307 PYMEMBERS(dsapriv), /* @tp_members@ */
d7ab1bab 308 0, /* @tp_getset@ */
309 0, /* @tp_base@ */
310 0, /* @tp_dict@ */
311 0, /* @tp_descr_get@ */
312 0, /* @tp_descr_set@ */
313 0, /* @tp_dictoffset@ */
314 0, /* @tp_init@ */
315 PyType_GenericAlloc, /* @tp_alloc@ */
316 dsapriv_pynew, /* @tp_new@ */
3aa33042 317 0, /* @tp_free@ */
d7ab1bab 318 0 /* @tp_is_gc@ */
319};
320
321static PyObject *kcdsapub_pynew(PyTypeObject *ty,
322 PyObject *arg, PyObject *kw)
323{
16178155 324 PyObject *G, *p, *rng = rand_pyobj, *hash = has160_pyobj;
d7ab1bab 325 PyObject *rc = 0;
827f89d7 326 static const char *const kwlist[] = { "G", "p", "hash", "rng", 0 };
d7ab1bab 327
827f89d7 328 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!|O!O!:new", KWLIST,
d7ab1bab 329 group_pytype, &G,
330 ge_pytype, &p,
d7ab1bab 331 gchash_pytype, &hash,
332 grand_pytype, &rng) ||
16178155 333 (rc = dsa_setup(kcdsapub_pytype, G, 0, p, rng, hash, 0)) == 0)
d7ab1bab 334 goto end;
335end:
336 return (rc);
337}
338
16178155
MW
339static void kcdsa_calcpub(group *g, ge *p, mp *u)
340{
341 mp *uinv = mp_modinv(MP_NEW, u, g->r);
342 G_EXP(g, p, g->g, uinv);
343 mp_drop(uinv);
344}
345
d7ab1bab 346static PyObject *kcdsapriv_pynew(PyTypeObject *ty,
347 PyObject *arg, PyObject *kw)
348{
16178155 349 PyObject *G, *u, *p = 0, *rng = rand_pyobj, *hash = has160_pyobj;
d7ab1bab 350 PyObject *rc = 0;
827f89d7 351 static const char *const kwlist[] = { "G", "u", "p", "hash", "rng", 0 };
d7ab1bab 352
827f89d7 353 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O|O!O!O!:new", KWLIST,
d7ab1bab 354 group_pytype, &G,
d7ab1bab 355 &u,
16178155 356 ge_pytype, &p,
d7ab1bab 357 gchash_pytype, &hash,
358 grand_pytype, &rng) ||
16178155
MW
359 (rc = dsa_setup(kcdsapriv_pytype, G, u, p,
360 rng, hash, kcdsa_calcpub)) == 0)
d7ab1bab 361 goto end;
362end:
363 return (rc);
364}
365
366static PyObject *kcdsameth_beginhash(PyObject *me, PyObject *arg)
367{
368 if (!PyArg_ParseTuple(arg, ":beginhash")) return (0);
c41d0371 369 return (ghash_pywrap(DSA_HASH(me), gkcdsa_beginhash(DSA_D(me))));
d7ab1bab 370}
371
372static PyObject *kcdsameth_endhash(PyObject *me, PyObject *arg)
373{
374 ghash *h;
375 PyObject *rc;
376 if (!PyArg_ParseTuple(arg, "O&:endhash", convghash, &h)) return (0);
377 gkcdsa_endhash(DSA_D(me), h);
378 h = GH_COPY(h);
379 rc = bytestring_pywrap(0, GH_CLASS(h)->hashsz);
380 GH_DONE(h, PyString_AS_STRING(rc));
381 GH_DESTROY(h);
382 return (rc);
383}
384
385static PyObject *kcdsameth_sign(PyObject *me, PyObject *arg, PyObject *kw)
386{
387 gkcdsa_sig s = GKCDSA_SIG_INIT;
388 char *p;
6b54260d 389 Py_ssize_t n;
d7ab1bab 390 mp *k = 0;
391 PyObject *r = 0, *rc = 0;
827f89d7 392 static const char *const kwlist[] = { "msg", "k", 0 };
d7ab1bab 393
827f89d7 394 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:sign", KWLIST,
d7ab1bab 395 &p, &n, convmp, &k))
396 goto end;
397 if (n != DSA_D(me)->h->hashsz)
398 VALERR("bad message length (doesn't match hash size)");
399 r = bytestring_pywrap(0, DSA_D(me)->h->hashsz);
46e6ad89 400 s.r = (octet *)PyString_AS_STRING(r);
d7ab1bab 401 gkcdsa_sign(DSA_D(me), &s, p, k);
9ca1789e 402 rc = Py_BuildValue("(ON)", r, mp_pywrap(s.s));
d7ab1bab 403end:
404 Py_XDECREF(r);
405 mp_drop(k);
406 return (rc);
407}
408
409static PyObject *kcdsameth_verify(PyObject *me, PyObject *arg)
410{
411 char *p;
6b54260d 412 Py_ssize_t n, rn;
d7ab1bab 413 gkcdsa_sig s = GKCDSA_SIG_INIT;
414 PyObject *rc = 0;
415
416 if (!PyArg_ParseTuple(arg, "s#(s#O&):verify",
417 &p, &n, &s.r, &rn, convmp, &s.s))
418 goto end;
419 if (n != DSA_D(me)->h->hashsz)
420 VALERR("bad message length (doesn't match hash size)");
421 if (rn != DSA_D(me)->h->hashsz)
422 VALERR("bad signature `r' length (doesn't match hash size)");
9ca1789e 423 rc = getbool(!gkcdsa_verify(DSA_D(me), &s, p));
d7ab1bab 424end:
425 mp_drop(s.s);
426 return (rc);
427}
428
c90f712e 429static const PyMethodDef kcdsapub_pymethods[] = {
d7ab1bab 430#define METHNAME(name) kcdsameth_##name
d96c882e
MW
431 METH (beginhash, "D.beginhash() -> hash object")
432 METH (endhash, "D.endhash(H) -> BYTES")
433 METH (verify, "D.verify(MSG, (R, S)) -> true/false")
d7ab1bab 434#undef METHNAME
435 { 0 }
436};
437
c90f712e 438static const PyMethodDef kcdsapriv_pymethods[] = {
d7ab1bab 439#define METHNAME(name) kcdsameth_##name
d96c882e 440 KWMETH(sign, "D.sign(MSG, [k = K]) -> R, S")
d7ab1bab 441#undef METHNAME
442 { 0 }
443};
444
445static PyTypeObject kcdsapub_pytype_skel = {
6d4db0bf 446 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 447 "KCDSAPub", /* @tp_name@ */
d7ab1bab 448 sizeof(dsa_pyobj), /* @tp_basicsize@ */
449 0, /* @tp_itemsize@ */
450
451 dsa_pydealloc, /* @tp_dealloc@ */
452 0, /* @tp_print@ */
453 0, /* @tp_getattr@ */
454 0, /* @tp_setattr@ */
455 0, /* @tp_compare@ */
456 0, /* @tp_repr@ */
457 0, /* @tp_as_number@ */
458 0, /* @tp_as_sequence@ */
459 0, /* @tp_as_mapping@ */
460 0, /* @tp_hash@ */
461 0, /* @tp_call@ */
462 0, /* @tp_str@ */
463 0, /* @tp_getattro@ */
464 0, /* @tp_setattro@ */
465 0, /* @tp_as_buffer@ */
466 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
467 Py_TPFLAGS_BASETYPE,
468
469 /* @tp_doc@ */
d96c882e 470 "KCDSAPub(GROUP, P, [hash = sha], [rng = rand]): KCDSA public key.",
d7ab1bab 471
472 0, /* @tp_traverse@ */
473 0, /* @tp_clear@ */
474 0, /* @tp_richcompare@ */
475 0, /* @tp_weaklistoffset@ */
476 0, /* @tp_iter@ */
963a6148 477 0, /* @tp_iternext@ */
c90f712e
MW
478 PYMETHODS(kcdsapub), /* @tp_methods@ */
479 PYMEMBERS(dsapub), /* @tp_members@ */
d7ab1bab 480 0, /* @tp_getset@ */
481 0, /* @tp_base@ */
482 0, /* @tp_dict@ */
483 0, /* @tp_descr_get@ */
484 0, /* @tp_descr_set@ */
485 0, /* @tp_dictoffset@ */
486 0, /* @tp_init@ */
487 PyType_GenericAlloc, /* @tp_alloc@ */
488 kcdsapub_pynew, /* @tp_new@ */
3aa33042 489 0, /* @tp_free@ */
d7ab1bab 490 0 /* @tp_is_gc@ */
491};
492
493static PyTypeObject kcdsapriv_pytype_skel = {
6d4db0bf 494 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 495 "KCDSAPriv", /* @tp_name@ */
d7ab1bab 496 sizeof(dsa_pyobj), /* @tp_basicsize@ */
497 0, /* @tp_itemsize@ */
498
499 0, /* @tp_dealloc@ */
500 0, /* @tp_print@ */
501 0, /* @tp_getattr@ */
502 0, /* @tp_setattr@ */
503 0, /* @tp_compare@ */
504 0, /* @tp_repr@ */
505 0, /* @tp_as_number@ */
506 0, /* @tp_as_sequence@ */
507 0, /* @tp_as_mapping@ */
508 0, /* @tp_hash@ */
509 0, /* @tp_call@ */
510 0, /* @tp_str@ */
511 0, /* @tp_getattro@ */
512 0, /* @tp_setattro@ */
513 0, /* @tp_as_buffer@ */
514 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
515 Py_TPFLAGS_BASETYPE,
516
517 /* @tp_doc@ */
d96c882e
MW
518 "KCDSAPriv(GROUP, U, [p = u G], [hash = sha], [rng = rand]): "
519 "KCDSA private key.",
d7ab1bab 520
521 0, /* @tp_traverse@ */
522 0, /* @tp_clear@ */
523 0, /* @tp_richcompare@ */
524 0, /* @tp_weaklistoffset@ */
525 0, /* @tp_iter@ */
963a6148 526 0, /* @tp_iternext@ */
c90f712e
MW
527 PYMETHODS(kcdsapriv), /* @tp_methods@ */
528 PYMEMBERS(dsapriv), /* @tp_members@ */
d7ab1bab 529 0, /* @tp_getset@ */
530 0, /* @tp_base@ */
531 0, /* @tp_dict@ */
532 0, /* @tp_descr_get@ */
533 0, /* @tp_descr_set@ */
534 0, /* @tp_dictoffset@ */
535 0, /* @tp_init@ */
536 PyType_GenericAlloc, /* @tp_alloc@ */
537 kcdsapriv_pynew, /* @tp_new@ */
3aa33042 538 0, /* @tp_free@ */
d7ab1bab 539 0 /* @tp_is_gc@ */
540};
541
542/*----- RSA ---------------------------------------------------------------*/
543
544typedef struct rsapub_pyobj {
545 PyObject_HEAD
546 rsa_pub pub;
547 rsa_pubctx pubctx;
548} rsapub_pyobj;
549
550#define RSA_PUB(o) (&((rsapub_pyobj *)(o))->pub)
551#define RSA_PUBCTX(o) (&((rsapub_pyobj *)(o))->pubctx)
552
553typedef struct rsapriv_pyobj {
554 PyObject_HEAD
555 rsa_pub pub;
556 rsa_pubctx pubctx;
557 rsa_priv priv;
558 rsa_privctx privctx;
559 PyObject *rng;
560} rsapriv_pyobj;
561
562#define RSA_PRIV(o) (&((rsapriv_pyobj *)(o))->priv)
563#define RSA_PRIVCTX(o) (&((rsapriv_pyobj *)(o))->privctx)
564#define RSA_RNG(o) (((rsapriv_pyobj *)(o))->rng)
565
566static PyTypeObject *rsapub_pytype, *rsapriv_pytype;
567
568static PyObject *rsapub_pynew(PyTypeObject *ty,
569 PyObject *arg, PyObject *kw)
570{
571 rsa_pub rp = { 0 };
572 rsapub_pyobj *o;
827f89d7 573 static const char *const kwlist[] = { "n", "e", 0 };
d7ab1bab 574
827f89d7 575 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", KWLIST,
d7ab1bab 576 convmp, &rp.n, convmp, &rp.e))
577 goto end;
61cc9665 578 if (!MP_ODDP(rp.n)) VALERR("RSA modulus must be even");
d7ab1bab 579 o = (rsapub_pyobj *)ty->tp_alloc(ty, 0);
580 o->pub = rp;
581 rsa_pubcreate(&o->pubctx, &o->pub);
582 return ((PyObject *)o);
583end:
584 rsa_pubfree(&rp);
585 return (0);
586}
587
588static void rsapub_pydealloc(PyObject *me)
589{
590 rsa_pubdestroy(RSA_PUBCTX(me));
591 rsa_pubfree(RSA_PUB(me));
3aa33042 592 FREEOBJ(me);
d7ab1bab 593}
594
595static PyObject *rsaget_n(PyObject *me, void *hunoz)
596 { return mp_pywrap(MP_COPY(RSA_PUB(me)->n)); }
597
598static PyObject *rsaget_e(PyObject *me, void *hunoz)
599 { return mp_pywrap(MP_COPY(RSA_PUB(me)->e)); }
600
601static PyObject *rsameth_pubop(PyObject *me, PyObject *arg)
602{
603 mp *x = 0;
604 PyObject *rc = 0;
605
606 if (!PyArg_ParseTuple(arg, "O&:pubop", convmp, &x)) goto end;
607 rc = mp_pywrap(rsa_pubop(RSA_PUBCTX(me), MP_NEW, x));
608end:
609 mp_drop(x);
610 return (rc);
611}
612
613static PyObject *rsapriv_dopywrap(PyTypeObject *ty,
614 rsa_priv *rp, PyObject *rng)
615{
616 rsapriv_pyobj *o;
617
618 o = (rsapriv_pyobj *)ty->tp_alloc(ty, 0);
619 o->priv = *rp;
620 o->pub.n = rp->n;
621 o->pub.e = rp->e;
622 rsa_privcreate(&o->privctx, &o->priv, &rand_global);
623 rsa_pubcreate(&o->pubctx, &o->pub);
624 if (!rng) {
625 rng = Py_None;
626 Py_INCREF(rng);
627 }
628 o->rng = rng;
629 return ((PyObject *)o);
630}
631
632PyObject *rsapriv_pywrap(rsa_priv *rp)
633 { return rsapriv_dopywrap(rsapriv_pytype, rp, 0); }
634
635static PyObject *rsapriv_pynew(PyTypeObject *ty,
636 PyObject *arg, PyObject *kw)
637{
638 rsa_priv rp = { 0 };
639 PyObject *rng = Py_None;
827f89d7 640 static const char *const kwlist[] =
d7ab1bab 641 { "n", "e", "d", "p", "q", "dp", "dq", "q_inv", "rng", 0 };
642
827f89d7 643 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&O&O&O&O&O&O&O&O:new", KWLIST,
d7ab1bab 644 convmp, &rp.n, convmp, &rp.e,
645 convmp, &rp.d,
646 convmp, &rp.p, convmp, &rp.q,
647 convmp, &rp.dp, convmp, &rp.dq,
648 convmp, &rp.q_inv,
649 &rng))
650 goto end;
61cc9665
MW
651 if ((rp.n && !MP_ODDP(rp.n)) ||
652 (rp.p && !MP_ODDP(rp.p)) ||
43723419 653 (rp.q && !MP_ODDP(rp.q)))
61cc9665 654 VALERR("RSA modulus and factors must be odd");
d7ab1bab 655 if (rsa_recover(&rp)) VALERR("couldn't construct private key");
656 if (rng != Py_None && !GRAND_PYCHECK(rng))
657 TYERR("not a random number source");
658 Py_INCREF(rng);
659 return (rsapriv_dopywrap(ty, &rp, rng));
660end:
661 rsa_privfree(&rp);
662 return (0);
663}
664
665static void rsapriv_pydealloc(PyObject *me)
666{
667 RSA_PRIVCTX(me)->r = &rand_global;
668 rsa_privdestroy(RSA_PRIVCTX(me));
669 rsa_privfree(RSA_PRIV(me));
670 Py_DECREF(RSA_RNG(me));
3aa33042 671 FREEOBJ(me);
d7ab1bab 672}
673
674static PyObject *rsaget_d(PyObject *me, void *hunoz)
675 { return mp_pywrap(MP_COPY(RSA_PRIV(me)->d)); }
676
677static PyObject *rsaget_p(PyObject *me, void *hunoz)
678 { return mp_pywrap(MP_COPY(RSA_PRIV(me)->p)); }
679
680static PyObject *rsaget_q(PyObject *me, void *hunoz)
681 { return mp_pywrap(MP_COPY(RSA_PRIV(me)->q)); }
682
683static PyObject *rsaget_dp(PyObject *me, void *hunoz)
684 { return mp_pywrap(MP_COPY(RSA_PRIV(me)->dp)); }
685
686static PyObject *rsaget_dq(PyObject *me, void *hunoz)
687 { return mp_pywrap(MP_COPY(RSA_PRIV(me)->dq)); }
688
689static PyObject *rsaget_q_inv(PyObject *me, void *hunoz)
690 { return mp_pywrap(MP_COPY(RSA_PRIV(me)->q_inv)); }
691
692static PyObject *rsaget_rng(PyObject *me, void *hunoz)
693 { RETURN_OBJ(RSA_RNG(me)); }
694
695static int rsaset_rng(PyObject *me, PyObject *val, void *hunoz)
696{
697 int rc = -1;
f368b46e
MW
698 if (!val)
699 val = Py_None;
700 else if (val != Py_None && !GRAND_PYCHECK(val))
d7ab1bab 701 TYERR("expected grand or None");
702 Py_DECREF(RSA_RNG(me));
703 RSA_RNG(me) = val;
704 Py_INCREF(val);
705 rc = 0;
706end:
707 return (rc);
708}
709
710static PyObject *rsameth_privop(PyObject *me, PyObject *arg, PyObject *kw)
711{
712 PyObject *rng = RSA_RNG(me);
713 mp *x = 0;
714 PyObject *rc = 0;
827f89d7 715 static const char *const kwlist[] = { "x", "rng", 0 };
d7ab1bab 716
827f89d7 717 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O:privop", KWLIST,
d7ab1bab 718 convmp, &x, &rng))
719 goto end;
720 if (rng != Py_None && !GRAND_PYCHECK(rng))
721 TYERR("not a random number source");
722 RSA_PRIVCTX(me)->r = (rng == Py_None) ? 0 : GRAND_R(rng);
723 rc = mp_pywrap(rsa_privop(RSA_PRIVCTX(me), MP_NEW, x));
724end:
725 mp_drop(x);
726 return (rc);
727}
728
a157093f 729static PyObject *rsameth_generate(PyObject *me, PyObject *arg, PyObject *kw)
d7ab1bab 730{
731 grand *r = &rand_global;
732 unsigned nbits;
733 unsigned n = 0;
734 rsa_priv rp;
084e6c29 735 mp *e = 0;
930d78e3
MW
736 struct excinfo exc = EXCINFO_INIT;
737 pypgev evt = { { 0 } };
827f89d7 738 static const char *const kwlist[] =
a157093f 739 { "nbits", "event", "rng", "nsteps", "e", 0 };
d7ab1bab 740 PyObject *rc = 0;
741
930d78e3 742 evt.exc = &exc;
a157093f
MW
743 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&O&O&O&:generate", KWLIST,
744 convuint, &nbits, convpgev, &evt,
084e6c29
MW
745 convgrand, &r, convuint, &n,
746 convmp, &e))
d7ab1bab 747 goto end;
084e6c29
MW
748 if (e) MP_COPY(e);
749 else e = mp_fromulong(MP_NEW, 65537);
930d78e3
MW
750 if (rsa_gen_e(&rp, nbits, e, r, n, evt.ev.proc, evt.ev.ctx))
751 PGENERR(&exc);
d7ab1bab 752 rc = rsapriv_pywrap(&rp);
753end:
754 droppgev(&evt);
084e6c29 755 mp_drop(e);
d7ab1bab 756 return (rc);
757}
758
c90f712e 759static const PyGetSetDef rsapub_pygetset[] = {
d7ab1bab 760#define GETSETNAME(op, name) rsa##op##_##name
d96c882e
MW
761 GET (n, "R.n -> N")
762 GET (e, "R.e -> E")
d7ab1bab 763#undef GETSETNAME
764 { 0 }
765};
766
c90f712e 767static const PyMethodDef rsapub_pymethods[] = {
d7ab1bab 768#define METHNAME(name) rsameth_##name
d96c882e 769 METH (pubop, "R.pubop(X) -> X^E (mod N)")
d7ab1bab 770#undef METHNAME
771 { 0 }
772};
773
c90f712e 774static const PyGetSetDef rsapriv_pygetset[] = {
d7ab1bab 775#define GETSETNAME(op, name) rsa##op##_##name
d96c882e
MW
776 GET (d, "R.d -> D")
777 GET (p, "R.p -> P")
778 GET (q, "R.q -> Q")
779 GET (dp, "R.dp -> D mod (P - 1)")
780 GET (dq, "R.dq -> D mod (Q - 1)")
781 GET (q_inv, "R.q_inv -> Q^{-1} mod P")
782 GETSET(rng, "R.rng -> random number source for blinding")
d7ab1bab 783#undef GETSETNAME
784 { 0 }
785};
786
c90f712e 787static const PyMethodDef rsapriv_pymethods[] = {
d7ab1bab 788#define METHNAME(name) rsameth_##name
a157093f
MW
789 KWMETH(privop, "R.privop(X, [rng = None]) -> X^D (mod N)")
790 KWSMTH(generate, "generate(NBITS, [event = pgen_nullev], [rng = rand], "
791 "[nsteps = 0]) -> R")
d7ab1bab 792#undef METHNAME
793 { 0 }
794};
795
796static PyTypeObject rsapub_pytype_skel = {
6d4db0bf 797 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 798 "RSAPub", /* @tp_name@ */
d7ab1bab 799 sizeof(rsapub_pyobj), /* @tp_basicsize@ */
800 0, /* @tp_itemsize@ */
801
802 rsapub_pydealloc, /* @tp_dealloc@ */
803 0, /* @tp_print@ */
804 0, /* @tp_getattr@ */
805 0, /* @tp_setattr@ */
806 0, /* @tp_compare@ */
807 0, /* @tp_repr@ */
808 0, /* @tp_as_number@ */
809 0, /* @tp_as_sequence@ */
810 0, /* @tp_as_mapping@ */
811 0, /* @tp_hash@ */
812 0, /* @tp_call@ */
813 0, /* @tp_str@ */
814 0, /* @tp_getattro@ */
815 0, /* @tp_setattro@ */
816 0, /* @tp_as_buffer@ */
817 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
818 Py_TPFLAGS_BASETYPE,
819
820 /* @tp_doc@ */
d96c882e 821 "RSAPub(N, E): RSA public key.",
d7ab1bab 822
823 0, /* @tp_traverse@ */
824 0, /* @tp_clear@ */
825 0, /* @tp_richcompare@ */
826 0, /* @tp_weaklistoffset@ */
827 0, /* @tp_iter@ */
963a6148 828 0, /* @tp_iternext@ */
c90f712e 829 PYMETHODS(rsapub), /* @tp_methods@ */
d7ab1bab 830 0, /* @tp_members@ */
c90f712e 831 PYGETSET(rsapub), /* @tp_getset@ */
d7ab1bab 832 0, /* @tp_base@ */
833 0, /* @tp_dict@ */
834 0, /* @tp_descr_get@ */
835 0, /* @tp_descr_set@ */
836 0, /* @tp_dictoffset@ */
837 0, /* @tp_init@ */
838 PyType_GenericAlloc, /* @tp_alloc@ */
839 rsapub_pynew, /* @tp_new@ */
3aa33042 840 0, /* @tp_free@ */
d7ab1bab 841 0 /* @tp_is_gc@ */
842};
843
844static PyTypeObject rsapriv_pytype_skel = {
6d4db0bf 845 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 846 "RSAPriv", /* @tp_name@ */
d7ab1bab 847 sizeof(rsapriv_pyobj), /* @tp_basicsize@ */
848 0, /* @tp_itemsize@ */
849
850 rsapriv_pydealloc, /* @tp_dealloc@ */
851 0, /* @tp_print@ */
852 0, /* @tp_getattr@ */
853 0, /* @tp_setattr@ */
854 0, /* @tp_compare@ */
855 0, /* @tp_repr@ */
856 0, /* @tp_as_number@ */
857 0, /* @tp_as_sequence@ */
858 0, /* @tp_as_mapping@ */
859 0, /* @tp_hash@ */
860 0, /* @tp_call@ */
861 0, /* @tp_str@ */
862 0, /* @tp_getattro@ */
863 0, /* @tp_setattro@ */
864 0, /* @tp_as_buffer@ */
865 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
866 Py_TPFLAGS_BASETYPE,
867
868 /* @tp_doc@ */
d96c882e
MW
869 "RSAPriv(..., [rng = rand]): RSA private key.\n"
870 " Keywords: n, e, d, p, q, dp, dq, q_inv; must provide enough",
d7ab1bab 871
872 0, /* @tp_traverse@ */
873 0, /* @tp_clear@ */
874 0, /* @tp_richcompare@ */
875 0, /* @tp_weaklistoffset@ */
876 0, /* @tp_iter@ */
963a6148 877 0, /* @tp_iternext@ */
c90f712e 878 PYMETHODS(rsapriv), /* @tp_methods@ */
d7ab1bab 879 0, /* @tp_members@ */
c90f712e 880 PYGETSET(rsapriv), /* @tp_getset@ */
d7ab1bab 881 0, /* @tp_base@ */
882 0, /* @tp_dict@ */
883 0, /* @tp_descr_get@ */
884 0, /* @tp_descr_set@ */
885 0, /* @tp_dictoffset@ */
886 0, /* @tp_init@ */
887 PyType_GenericAlloc, /* @tp_alloc@ */
888 rsapriv_pynew, /* @tp_new@ */
3aa33042 889 0, /* @tp_free@ */
d7ab1bab 890 0 /* @tp_is_gc@ */
891};
892
893/*----- RSA padding schemes -----------------------------------------------*/
894
895static PyObject *meth__p1crypt_encode(PyObject *me,
896 PyObject *arg, PyObject *kw)
897{
898 pkcs1 p1;
899 char *m, *ep;
6b54260d 900 Py_ssize_t msz, epsz;
d7ab1bab 901 unsigned long nbits;
902 PyObject *rc = 0;
903 octet *b = 0;
904 size_t sz;
905 mp *x;
827f89d7 906 static const char *const kwlist[] = { "msg", "nbits", "ep", "rng", 0 };
d7ab1bab 907
908 p1.r = &rand_global; ep = 0; epsz = 0;
827f89d7 909 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|s#O&:encode", KWLIST,
d7ab1bab 910 &m, &msz, convulong, &nbits,
911 &ep, &epsz, convgrand, &p1.r))
912 goto end;
913 sz = (nbits + 7)/8;
914 p1.ep = ep; p1.epsz = epsz;
915 if (epsz + msz + 11 > sz) VALERR("buffer underflow");
916 b = xmalloc(sz);
917 x = pkcs1_cryptencode(MP_NEW, m, msz, b, sz, nbits, &p1);
918 rc = mp_pywrap(x);
919end:
920 xfree(b);
921 return (rc);
922}
923
924static PyObject *meth__p1crypt_decode(PyObject *me,
925 PyObject *arg, PyObject *kw)
926{
927 pkcs1 p1;
928 char *ep;
6b54260d 929 Py_ssize_t epsz;
d7ab1bab 930 unsigned long nbits;
931 int n;
932 PyObject *rc = 0;
933 octet *b = 0;
934 size_t sz;
935 mp *x = 0;
827f89d7 936 static const char *const kwlist[] = { "ct", "nbits", "ep", "rng", 0 };
d7ab1bab 937
938 p1.r = &rand_global; ep = 0; epsz = 0;
827f89d7 939 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|s#O&:decode", KWLIST,
d7ab1bab 940 convmp, &x, convulong, &nbits,
941 &ep, &epsz, convgrand, &p1.r))
942 goto end;
943 sz = (nbits + 7)/8;
944 p1.ep = ep; p1.epsz = epsz;
945 if (epsz + 11 > sz) VALERR("buffer underflow");
946 b = xmalloc(sz);
947 if ((n = pkcs1_cryptdecode(x, b, sz, nbits, &p1)) < 0)
948 VALERR("decryption failed");
949 rc = bytestring_pywrap(b, n);
950end:
951 mp_drop(x);
952 xfree(b);
953 return (rc);
954}
955
956static PyObject *meth__p1sig_encode(PyObject *me,
957 PyObject *arg, PyObject *kw)
958{
959 pkcs1 p1;
960 char *m, *ep;
6b54260d 961 Py_ssize_t msz, epsz;
d7ab1bab 962 unsigned long nbits;
963 PyObject *rc = 0;
964 octet *b = 0;
965 size_t sz;
966 mp *x;
827f89d7 967 static const char *const kwlist[] = { "msg", "nbits", "ep", "rng", 0 };
d7ab1bab 968
969 p1.r = &rand_global; ep = 0; epsz = 0;
827f89d7 970 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|s#O&:encode", KWLIST,
d7ab1bab 971 &m, &msz, convulong, &nbits,
972 &ep, &epsz, convgrand, &p1.r))
973 goto end;
974 sz = (nbits + 7)/8;
975 p1.ep = ep; p1.epsz = epsz;
976 if (epsz + msz + 11 > sz) VALERR("buffer underflow");
977 b = xmalloc(sz);
978 x = pkcs1_sigencode(MP_NEW, m, msz, b, sz, nbits, &p1);
979 rc = mp_pywrap(x);
980end:
981 xfree(b);
982 return (rc);
983}
984
985static PyObject *meth__p1sig_decode(PyObject *me,
986 PyObject *arg, PyObject *kw)
987{
988 pkcs1 p1;
989 char *ep;
6b54260d 990 Py_ssize_t epsz;
d7ab1bab 991 unsigned long nbits;
992 int n;
993 PyObject *hukairz;
994 PyObject *rc = 0;
995 octet *b = 0;
996 size_t sz;
997 mp *x = 0;
827f89d7
MW
998 static const char *const kwlist[] =
999 { "msg", "sig", "nbits", "ep", "rng", 0 };
d7ab1bab 1000
1001 p1.r = &rand_global; ep = 0; epsz = 0;
827f89d7 1002 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|s#O&:decode", KWLIST,
d7ab1bab 1003 &hukairz, convmp, &x, convulong, &nbits,
1004 &ep, &epsz, convgrand, &p1.r))
1005 goto end;
1006 sz = (nbits + 7)/8;
1007 p1.ep = ep; p1.epsz = epsz;
1008 if (epsz + 10 > sz) VALERR("buffer underflow");
1009 b = xmalloc(sz);
1010 if ((n = pkcs1_sigdecode(x, 0, 0, b, sz, nbits, &p1)) < 0)
1011 VALERR("verification failed");
1012 rc = bytestring_pywrap(b, n);
1013end:
1014 mp_drop(x);
1015 xfree(b);
1016 return (rc);
1017}
1018
1019static PyObject *meth__oaep_encode(PyObject *me,
1020 PyObject *arg, PyObject *kw)
1021{
1022 oaep o;
1023 char *m, *ep;
6b54260d 1024 Py_ssize_t msz, epsz;
d7ab1bab 1025 unsigned long nbits;
1026 PyObject *rc = 0;
1027 octet *b = 0;
1028 size_t sz;
1029 mp *x;
827f89d7
MW
1030 static const char *const kwlist[] =
1031 { "msg", "nbits", "mgf", "hash", "ep", "rng", 0 };
d7ab1bab 1032
1033 o.r = &rand_global; o.cc = &sha_mgf; o.ch = &sha; ep = 0; epsz = 0;
827f89d7 1034 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|O&O&s#O&:encode", KWLIST,
d7ab1bab 1035 &m, &msz, convulong, &nbits,
1036 convgccipher, &o.cc,
1037 convgchash, &o.ch,
1038 &ep, &epsz,
1039 convgrand, &o.r))
1040 goto end;
1041 sz = (nbits + 7)/8;
1042 o.ep = ep; o.epsz = epsz;
1043 if (2 * o.ch->hashsz + 2 + msz > sz) VALERR("buffer underflow");
1044 b = xmalloc(sz);
1045 x = oaep_encode(MP_NEW, m, msz, b, sz, nbits, &o);
1046 rc = mp_pywrap(x);
1047end:
1048 xfree(b);
1049 return (rc);
1050}
1051
1052static PyObject *meth__oaep_decode(PyObject *me,
1053 PyObject *arg, PyObject *kw)
1054{
1055 oaep o;
1056 char *ep;
6b54260d 1057 Py_ssize_t epsz;
d7ab1bab 1058 unsigned long nbits;
1059 int n;
1060 PyObject *rc = 0;
1061 octet *b = 0;
1062 size_t sz;
1063 mp *x = 0;
827f89d7
MW
1064 static const char *const kwlist[] =
1065 { "ct", "nbits", "mgf", "hash", "ep", "rng", 0 };
d7ab1bab 1066
1067 o.r = &rand_global; o.cc = &sha_mgf; o.ch = &sha; ep = 0; epsz = 0;
827f89d7 1068 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&O&s#O&:decode", KWLIST,
d7ab1bab 1069 convmp, &x, convulong, &nbits,
1070 convgccipher, &o.cc,
1071 convgchash, &o.ch,
1072 &ep, &epsz,
1073 convgrand, &o.r))
1074 goto end;
1075 sz = (nbits + 7)/8;
1076 o.ep = ep; o.epsz = epsz;
1077 if (2 * o.ch->hashsz > sz) VALERR("buffer underflow");
1078 b = xmalloc(sz);
1079 if ((n = oaep_decode(x, b, sz, nbits, &o)) < 0)
1080 VALERR("decryption failed");
1081 rc = bytestring_pywrap(b, n);
1082end:
1083 mp_drop(x);
1084 xfree(b);
1085 return (rc);
1086}
1087
1088static PyObject *meth__pss_encode(PyObject *me,
1089 PyObject *arg, PyObject *kw)
1090{
1091 pss p;
1092 char *m;
6b54260d 1093 Py_ssize_t msz;
d7ab1bab 1094 unsigned long nbits;
1095 PyObject *rc = 0;
1096 octet *b = 0;
1097 size_t sz;
1098 mp *x = 0;
827f89d7
MW
1099 static const char *const kwlist[] =
1100 { "msg", "nbits", "mgf", "hash", "saltsz", "rng", 0 };
d7ab1bab 1101
1102 p.cc = &sha_mgf; p.ch = &sha; p.r = &rand_global; p.ssz = (size_t)-1;
827f89d7 1103 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|O&O&O&O&:encode", KWLIST,
d7ab1bab 1104 &m, &msz, convulong, &nbits,
1105 convgccipher, &p.cc,
1106 convgchash, &p.ch,
1107 convszt, &p.ssz,
1108 convgrand, &p.r))
1109 goto end;
1110 sz = (nbits + 7)/8;
1111 if (p.ssz == (size_t)-1) p.ssz = p.ch->hashsz;
1112 if (p.ch->hashsz + p.ssz + 2 > sz) VALERR("buffer underflow");
1113 b = xmalloc(sz);
1114 x = pss_encode(MP_NEW, m, msz, b, sz, nbits, &p);
1115 rc = mp_pywrap(x);
1116end:
1117 xfree(b);
1118 return (rc);
1119}
1120
1121static PyObject *meth__pss_decode(PyObject *me,
1122 PyObject *arg, PyObject *kw)
1123{
1124 pss p;
1125 char *m;
6b54260d 1126 Py_ssize_t msz;
d7ab1bab 1127 unsigned long nbits;
1128 PyObject *rc = 0;
1129 octet *b = 0;
1130 size_t sz;
1131 int n;
1132 mp *x = 0;
827f89d7 1133 static const char *const kwlist[] =
d7ab1bab 1134 { "msg", "sig", "nbits", "mgf", "hash", "saltsz", "rng", 0 };
1135
1136 p.cc = &sha_mgf; p.ch = &sha; p.r = &rand_global; p.ssz = (size_t)-1;
827f89d7 1137 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&O&|O&O&O&O&:decode", KWLIST,
d7ab1bab 1138 &m, &msz, convmp, &x, convulong, &nbits,
1139 convgccipher, &p.cc,
1140 convgchash, &p.ch,
1141 convszt, &p.ssz,
1142 convgrand, &p.r))
1143 goto end;
1144 sz = (nbits + 7)/8;
1145 if (p.ssz == (size_t)-1) p.ssz = p.ch->hashsz;
1146 if (p.ch->hashsz + p.ssz + 2 > sz) VALERR("buffer underflow");
1147 b = xmalloc(sz);
1148 if ((n = pss_decode(x, m, msz, b, sz, nbits, &p)) < 0)
1149 VALERR("verification failed");
1150 rc = Py_None; Py_INCREF(rc);
1151end:
1152 mp_drop(x);
1153 xfree(b);
1154 return (rc);
1155}
1156
eb8aa4ec 1157/*----- X25519 and related algorithms -------------------------------------*/
848ba392 1158
34bea186
MW
1159#define XDHS(_) \
1160 _(X25519, x25519) \
1161 _(X448, x448)
1162
1163#define DEFXDH(X, x) \
1164 static PyObject *meth_##x(PyObject *me, PyObject *arg) \
1165 { \
1166 const char *k, *p; \
1167 Py_ssize_t ksz, psz; \
1168 PyObject *rc = 0; \
1169 if (!PyArg_ParseTuple(arg, "s#s#:" #x, &k, &ksz, &p, &psz)) \
1170 goto end; \
1171 if (ksz != X##_KEYSZ) VALERR("bad key length"); \
1172 if (psz != X##_PUBSZ) VALERR("bad public length"); \
1173 rc = bytestring_pywrap(0, X##_OUTSZ); \
1174 x((octet *)PyString_AS_STRING(rc), \
1175 (const octet *)k, (const octet *)p); \
1176 return (rc); \
1177 end: \
1178 return (0); \
1179 }
1180XDHS(DEFXDH)
1181#undef DEFXDH
eb8aa4ec 1182
dca3bdfd 1183/*----- Ed25519 and related algorithms ------------------------------------*/
dafb2da4 1184
dca3bdfd 1185#define EDDSAS(_) \
eee202c3
MW
1186 _(ED25519, ed25519, -1, ctx) \
1187 _(ED448, ed448, 0, )
dafb2da4 1188
5c4c0231 1189#define DEFEDDSA(ED, ed, phdflt, sigver) \
dca3bdfd
MW
1190 \
1191 static PyObject *meth_##ed##_pubkey(PyObject *me, PyObject *arg) \
1192 { \
1193 const char *k; \
1194 Py_ssize_t ksz; \
1195 PyObject *rc = 0; \
1196 if (!PyArg_ParseTuple(arg, "s#:" #ed "_pubkey", &k, &ksz)) \
1197 goto end; \
1198 rc = bytestring_pywrap(0, ED##_PUBSZ); \
1199 ed##_pubkey((octet *)PyString_AS_STRING(rc), k, ksz); \
1200 return (rc); \
1201 end: \
1202 return (0); \
1203 } \
1204 \
1205 static PyObject *meth_##ed##_sign(PyObject *me, PyObject *arg, \
1206 PyObject *kw) \
1207 { \
5c4c0231
MW
1208 const char *k, *p = 0, *c = 0, *m; \
1209 Py_ssize_t ksz, psz, csz = 0, msz; \
1210 int ph = phdflt; \
dca3bdfd
MW
1211 PyObject *rc = 0; \
1212 octet pp[ED##_PUBSZ]; \
827f89d7
MW
1213 static const char *const kwlist[] = \
1214 { "key", "msg", "pub", "perso", "phflag", 0 }; \
dca3bdfd 1215 if (!PyArg_ParseTupleAndKeywords(arg, kw, \
5c4c0231 1216 "s#s#|s#s#O&:" #ed "_sign", \
827f89d7 1217 KWLIST, \
5c4c0231
MW
1218 &k, &ksz, &m, &msz, &p, &psz, \
1219 &c, &csz, convbool, &ph)) \
dca3bdfd
MW
1220 goto end; \
1221 if (p && psz != ED##_PUBSZ) VALERR("bad public length"); \
5c4c0231
MW
1222 if (c && csz > ED##_MAXPERSOSZ) \
1223 VALERR("personalization string too long"); \
1224 if (c && ph == -1) ph = 0; \
dca3bdfd
MW
1225 if (!p) { p = (const char *)pp; ed##_pubkey(pp, k, ksz); } \
1226 rc = bytestring_pywrap(0, ED##_SIGSZ); \
5c4c0231
MW
1227 ed##sigver##_sign((octet *)PyString_AS_STRING(rc), k, ksz, \
1228 (const octet *)p, ph, c, csz, m, msz); \
dca3bdfd
MW
1229 return (rc); \
1230 end: \
1231 return (0); \
1232 } \
1233 \
1234 static PyObject *meth_##ed##_verify(PyObject *me, \
1235 PyObject *arg, PyObject *kw) \
1236 { \
5c4c0231
MW
1237 const char *p, *c = 0, *m, *s; \
1238 Py_ssize_t psz, csz = 0, msz, ssz; \
1239 int ph = phdflt; \
dca3bdfd 1240 PyObject *rc = 0; \
827f89d7
MW
1241 static const char *const kwlist[] = \
1242 { "pub", "msg", "sig", "perso", "phflag", 0 }; \
dca3bdfd 1243 if (!PyArg_ParseTupleAndKeywords(arg, kw, \
5c4c0231 1244 "s#s#s#|s#O&:" #ed "_verify", \
827f89d7 1245 KWLIST, \
5c4c0231
MW
1246 &p, &psz, &m, &msz, &s, &ssz, \
1247 &c, &csz, convbool, &ph)) \
dca3bdfd
MW
1248 goto end; \
1249 if (psz != ED##_PUBSZ) VALERR("bad public length"); \
1250 if (ssz != ED##_SIGSZ) VALERR("bad signature length"); \
5c4c0231
MW
1251 if (c && csz > ED##_MAXPERSOSZ) \
1252 VALERR("personalization string too long"); \
1253 if (c && ph == -1) ph = 0; \
1254 rc = getbool(!ed##sigver##_verify((const octet *)p, ph, c, csz, \
1255 m, msz, (const octet *)s)); \
dca3bdfd
MW
1256 return (rc); \
1257 end: \
1258 return (0); \
1259 }
1260EDDSAS(DEFEDDSA)
1261#undef DEFEDDSA
dafb2da4 1262
d7ab1bab 1263/*----- Global stuff ------------------------------------------------------*/
1264
c90f712e 1265static const PyMethodDef methods[] = {
d7ab1bab 1266#define METHNAME(name) meth_##name
d96c882e
MW
1267 KWMETH(_p1crypt_encode, 0)
1268 KWMETH(_p1crypt_decode, 0)
1269 KWMETH(_p1sig_encode, 0)
1270 KWMETH(_p1sig_decode, 0)
1271 KWMETH(_oaep_encode, 0)
1272 KWMETH(_oaep_decode, 0)
1273 KWMETH(_pss_encode, 0)
1274 KWMETH(_pss_decode, 0)
34bea186 1275#define DEFMETH(X, x) \
d96c882e 1276 METH (x, "" #x "(KEY, PUBLIC) -> SHARED")
34bea186
MW
1277 XDHS(DEFMETH)
1278#undef DEFMETH
5c4c0231 1279#define DEFMETH(ED, ed, phdflt, sigver) \
d96c882e
MW
1280 METH (ed##_pubkey, "" #ed "_pubkey(KEY) -> PUBLIC") \
1281 KWMETH(ed##_sign, "" #ed "_sign(KEY, MSG, [pub = PUBLIC], " \
1282 "[perso = STRING], [phflag = BOOL]) -> SIG") \
1283 KWMETH(ed##_verify, "" #ed "_verify(PUBLIC, MSG, SIG, " \
1284 "[perso = STRING], [phflag = BOOL]) -> BOOL")
dca3bdfd
MW
1285 EDDSAS(DEFMETH)
1286#undef DEFMETH
d7ab1bab 1287#undef METHNAME
1288 { 0 }
1289};
1290
1291void pubkey_pyinit(void)
1292{
1293 INITTYPE(dsapub, root);
1294 INITTYPE(dsapriv, dsapub);
1295 INITTYPE(kcdsapub, root);
1296 INITTYPE(kcdsapriv, kcdsapub);
1297 INITTYPE(rsapub, root);
1298 INITTYPE(rsapriv, rsapub);
1299 addmethods(methods);
1300}
1301
1302void pubkey_pyinsert(PyObject *mod)
1303{
1304 INSERT("DSAPub", dsapub_pytype);
1305 INSERT("DSAPriv", dsapriv_pytype);
1306 INSERT("KCDSAPub", kcdsapub_pytype);
1307 INSERT("KCDSAPriv", kcdsapriv_pytype);
1308 INSERT("RSAPub", rsapub_pytype);
1309 INSERT("RSAPriv", rsapriv_pytype);
1310}
1311
1312/*----- That's all, folks -------------------------------------------------*/