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