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