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