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; |
347 | char *kwlist[] = { "G", "p", "u", "hash", "rng", 0 }; | |
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; | |
731 | pgev evt = { 0 }; | |
732 | char *kwlist[] = { "class", "nbits", "event", "rng", "nsteps", 0 }; | |
733 | PyObject *rc = 0; | |
734 | ||
735 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&:generate", kwlist, | |
736 | &me, convuint, &nbits, convpgev, &evt, | |
737 | convgrand, &r, convuint, &n)) | |
738 | goto end; | |
739 | if (rsa_gen(&rp, nbits, r, n, evt.proc, evt.ctx)) | |
740 | PGENERR; | |
741 | rc = rsapriv_pywrap(&rp); | |
742 | end: | |
743 | droppgev(&evt); | |
744 | return (rc); | |
745 | } | |
746 | ||
747 | static PyGetSetDef rsapub_pygetset[] = { | |
748 | #define GETSETNAME(op, name) rsa##op##_##name | |
749 | GET (n, "R.n -> N") | |
750 | GET (e, "R.e -> E") | |
751 | #undef GETSETNAME | |
752 | { 0 } | |
753 | }; | |
754 | ||
755 | static PyMethodDef rsapub_pymethods[] = { | |
756 | #define METHNAME(name) rsameth_##name | |
757 | METH (pubop, "R.pubop(X) -> X^E (mod N)") | |
758 | #undef METHNAME | |
759 | { 0 } | |
760 | }; | |
761 | ||
762 | static PyGetSetDef rsapriv_pygetset[] = { | |
763 | #define GETSETNAME(op, name) rsa##op##_##name | |
764 | GET (d, "R.d -> D") | |
765 | GET (p, "R.p -> P") | |
766 | GET (q, "R.q -> Q") | |
767 | GET (dp, "R.dp -> D mod (P - 1)") | |
768 | GET (dq, "R.dq -> D mod (Q - 1)") | |
769 | GET (q_inv, "R.q_inv -> Q^{-1} mod P") | |
770 | GETSET(rng, "R.rng -> random number source for blinding") | |
771 | #undef GETSETNAME | |
772 | { 0 } | |
773 | }; | |
774 | ||
775 | static PyMethodDef rsapriv_pymethods[] = { | |
776 | #define METHNAME(name) rsameth_##name | |
777 | KWMETH(privop, "R.privop(X, rng = None) -> X^D (mod N)") | |
778 | #undef METHNAME | |
779 | { 0 } | |
780 | }; | |
781 | ||
782 | static PyTypeObject rsapub_pytype_skel = { | |
6d4db0bf | 783 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 784 | "RSAPub", /* @tp_name@ */ |
d7ab1bab | 785 | sizeof(rsapub_pyobj), /* @tp_basicsize@ */ |
786 | 0, /* @tp_itemsize@ */ | |
787 | ||
788 | rsapub_pydealloc, /* @tp_dealloc@ */ | |
789 | 0, /* @tp_print@ */ | |
790 | 0, /* @tp_getattr@ */ | |
791 | 0, /* @tp_setattr@ */ | |
792 | 0, /* @tp_compare@ */ | |
793 | 0, /* @tp_repr@ */ | |
794 | 0, /* @tp_as_number@ */ | |
795 | 0, /* @tp_as_sequence@ */ | |
796 | 0, /* @tp_as_mapping@ */ | |
797 | 0, /* @tp_hash@ */ | |
798 | 0, /* @tp_call@ */ | |
799 | 0, /* @tp_str@ */ | |
800 | 0, /* @tp_getattro@ */ | |
801 | 0, /* @tp_setattro@ */ | |
802 | 0, /* @tp_as_buffer@ */ | |
803 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
804 | Py_TPFLAGS_BASETYPE, | |
805 | ||
806 | /* @tp_doc@ */ | |
807 | "RSA public key information.", | |
808 | ||
809 | 0, /* @tp_traverse@ */ | |
810 | 0, /* @tp_clear@ */ | |
811 | 0, /* @tp_richcompare@ */ | |
812 | 0, /* @tp_weaklistoffset@ */ | |
813 | 0, /* @tp_iter@ */ | |
963a6148 | 814 | 0, /* @tp_iternext@ */ |
d7ab1bab | 815 | rsapub_pymethods, /* @tp_methods@ */ |
816 | 0, /* @tp_members@ */ | |
817 | rsapub_pygetset, /* @tp_getset@ */ | |
818 | 0, /* @tp_base@ */ | |
819 | 0, /* @tp_dict@ */ | |
820 | 0, /* @tp_descr_get@ */ | |
821 | 0, /* @tp_descr_set@ */ | |
822 | 0, /* @tp_dictoffset@ */ | |
823 | 0, /* @tp_init@ */ | |
824 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
825 | rsapub_pynew, /* @tp_new@ */ | |
3aa33042 | 826 | 0, /* @tp_free@ */ |
d7ab1bab | 827 | 0 /* @tp_is_gc@ */ |
828 | }; | |
829 | ||
830 | static PyTypeObject rsapriv_pytype_skel = { | |
6d4db0bf | 831 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 832 | "RSAPriv", /* @tp_name@ */ |
d7ab1bab | 833 | sizeof(rsapriv_pyobj), /* @tp_basicsize@ */ |
834 | 0, /* @tp_itemsize@ */ | |
835 | ||
836 | rsapriv_pydealloc, /* @tp_dealloc@ */ | |
837 | 0, /* @tp_print@ */ | |
838 | 0, /* @tp_getattr@ */ | |
839 | 0, /* @tp_setattr@ */ | |
840 | 0, /* @tp_compare@ */ | |
841 | 0, /* @tp_repr@ */ | |
842 | 0, /* @tp_as_number@ */ | |
843 | 0, /* @tp_as_sequence@ */ | |
844 | 0, /* @tp_as_mapping@ */ | |
845 | 0, /* @tp_hash@ */ | |
846 | 0, /* @tp_call@ */ | |
847 | 0, /* @tp_str@ */ | |
848 | 0, /* @tp_getattro@ */ | |
849 | 0, /* @tp_setattro@ */ | |
850 | 0, /* @tp_as_buffer@ */ | |
851 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
852 | Py_TPFLAGS_BASETYPE, | |
853 | ||
854 | /* @tp_doc@ */ | |
855 | "RSA private key information.", | |
856 | ||
857 | 0, /* @tp_traverse@ */ | |
858 | 0, /* @tp_clear@ */ | |
859 | 0, /* @tp_richcompare@ */ | |
860 | 0, /* @tp_weaklistoffset@ */ | |
861 | 0, /* @tp_iter@ */ | |
963a6148 | 862 | 0, /* @tp_iternext@ */ |
d7ab1bab | 863 | rsapriv_pymethods, /* @tp_methods@ */ |
864 | 0, /* @tp_members@ */ | |
865 | rsapriv_pygetset, /* @tp_getset@ */ | |
866 | 0, /* @tp_base@ */ | |
867 | 0, /* @tp_dict@ */ | |
868 | 0, /* @tp_descr_get@ */ | |
869 | 0, /* @tp_descr_set@ */ | |
870 | 0, /* @tp_dictoffset@ */ | |
871 | 0, /* @tp_init@ */ | |
872 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
873 | rsapriv_pynew, /* @tp_new@ */ | |
3aa33042 | 874 | 0, /* @tp_free@ */ |
d7ab1bab | 875 | 0 /* @tp_is_gc@ */ |
876 | }; | |
877 | ||
878 | /*----- RSA padding schemes -----------------------------------------------*/ | |
879 | ||
880 | static PyObject *meth__p1crypt_encode(PyObject *me, | |
881 | PyObject *arg, PyObject *kw) | |
882 | { | |
883 | pkcs1 p1; | |
884 | char *m, *ep; | |
6b54260d | 885 | Py_ssize_t msz, epsz; |
d7ab1bab | 886 | unsigned long nbits; |
887 | PyObject *rc = 0; | |
888 | octet *b = 0; | |
889 | size_t sz; | |
890 | mp *x; | |
891 | char *kwlist[] = { "msg", "nbits", "ep", "rng", 0 }; | |
892 | ||
893 | p1.r = &rand_global; ep = 0; epsz = 0; | |
894 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|s#O&:encode", kwlist, | |
895 | &m, &msz, convulong, &nbits, | |
896 | &ep, &epsz, convgrand, &p1.r)) | |
897 | goto end; | |
898 | sz = (nbits + 7)/8; | |
899 | p1.ep = ep; p1.epsz = epsz; | |
900 | if (epsz + msz + 11 > sz) VALERR("buffer underflow"); | |
901 | b = xmalloc(sz); | |
902 | x = pkcs1_cryptencode(MP_NEW, m, msz, b, sz, nbits, &p1); | |
903 | rc = mp_pywrap(x); | |
904 | end: | |
905 | xfree(b); | |
906 | return (rc); | |
907 | } | |
908 | ||
909 | static PyObject *meth__p1crypt_decode(PyObject *me, | |
910 | PyObject *arg, PyObject *kw) | |
911 | { | |
912 | pkcs1 p1; | |
913 | char *ep; | |
6b54260d | 914 | Py_ssize_t epsz; |
d7ab1bab | 915 | unsigned long nbits; |
916 | int n; | |
917 | PyObject *rc = 0; | |
918 | octet *b = 0; | |
919 | size_t sz; | |
920 | mp *x = 0; | |
921 | char *kwlist[] = { "ct", "nbits", "ep", "rng", 0 }; | |
922 | ||
923 | p1.r = &rand_global; ep = 0; epsz = 0; | |
924 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|s#O&:decode", kwlist, | |
925 | convmp, &x, convulong, &nbits, | |
926 | &ep, &epsz, convgrand, &p1.r)) | |
927 | goto end; | |
928 | sz = (nbits + 7)/8; | |
929 | p1.ep = ep; p1.epsz = epsz; | |
930 | if (epsz + 11 > sz) VALERR("buffer underflow"); | |
931 | b = xmalloc(sz); | |
932 | if ((n = pkcs1_cryptdecode(x, b, sz, nbits, &p1)) < 0) | |
933 | VALERR("decryption failed"); | |
934 | rc = bytestring_pywrap(b, n); | |
935 | end: | |
936 | mp_drop(x); | |
937 | xfree(b); | |
938 | return (rc); | |
939 | } | |
940 | ||
941 | static PyObject *meth__p1sig_encode(PyObject *me, | |
942 | PyObject *arg, PyObject *kw) | |
943 | { | |
944 | pkcs1 p1; | |
945 | char *m, *ep; | |
6b54260d | 946 | Py_ssize_t msz, epsz; |
d7ab1bab | 947 | unsigned long nbits; |
948 | PyObject *rc = 0; | |
949 | octet *b = 0; | |
950 | size_t sz; | |
951 | mp *x; | |
952 | char *kwlist[] = { "msg", "nbits", "ep", "rng", 0 }; | |
953 | ||
954 | p1.r = &rand_global; ep = 0; epsz = 0; | |
955 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|s#O&:encode", kwlist, | |
956 | &m, &msz, convulong, &nbits, | |
957 | &ep, &epsz, convgrand, &p1.r)) | |
958 | goto end; | |
959 | sz = (nbits + 7)/8; | |
960 | p1.ep = ep; p1.epsz = epsz; | |
961 | if (epsz + msz + 11 > sz) VALERR("buffer underflow"); | |
962 | b = xmalloc(sz); | |
963 | x = pkcs1_sigencode(MP_NEW, m, msz, b, sz, nbits, &p1); | |
964 | rc = mp_pywrap(x); | |
965 | end: | |
966 | xfree(b); | |
967 | return (rc); | |
968 | } | |
969 | ||
970 | static PyObject *meth__p1sig_decode(PyObject *me, | |
971 | PyObject *arg, PyObject *kw) | |
972 | { | |
973 | pkcs1 p1; | |
974 | char *ep; | |
6b54260d | 975 | Py_ssize_t epsz; |
d7ab1bab | 976 | unsigned long nbits; |
977 | int n; | |
978 | PyObject *hukairz; | |
979 | PyObject *rc = 0; | |
980 | octet *b = 0; | |
981 | size_t sz; | |
982 | mp *x = 0; | |
983 | char *kwlist[] = { "msg", "sig", "nbits", "ep", "rng", 0 }; | |
984 | ||
985 | p1.r = &rand_global; ep = 0; epsz = 0; | |
986 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|s#O&:decode", kwlist, | |
987 | &hukairz, convmp, &x, convulong, &nbits, | |
988 | &ep, &epsz, convgrand, &p1.r)) | |
989 | goto end; | |
990 | sz = (nbits + 7)/8; | |
991 | p1.ep = ep; p1.epsz = epsz; | |
992 | if (epsz + 10 > sz) VALERR("buffer underflow"); | |
993 | b = xmalloc(sz); | |
994 | if ((n = pkcs1_sigdecode(x, 0, 0, b, sz, nbits, &p1)) < 0) | |
995 | VALERR("verification failed"); | |
996 | rc = bytestring_pywrap(b, n); | |
997 | end: | |
998 | mp_drop(x); | |
999 | xfree(b); | |
1000 | return (rc); | |
1001 | } | |
1002 | ||
1003 | static PyObject *meth__oaep_encode(PyObject *me, | |
1004 | PyObject *arg, PyObject *kw) | |
1005 | { | |
1006 | oaep o; | |
1007 | char *m, *ep; | |
6b54260d | 1008 | Py_ssize_t msz, epsz; |
d7ab1bab | 1009 | unsigned long nbits; |
1010 | PyObject *rc = 0; | |
1011 | octet *b = 0; | |
1012 | size_t sz; | |
1013 | mp *x; | |
1014 | char *kwlist[] = { "msg", "nbits", "mgf", "hash", "ep", "rng", 0 }; | |
1015 | ||
1016 | o.r = &rand_global; o.cc = &sha_mgf; o.ch = &sha; ep = 0; epsz = 0; | |
1017 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|O&O&s#O&:encode", kwlist, | |
1018 | &m, &msz, convulong, &nbits, | |
1019 | convgccipher, &o.cc, | |
1020 | convgchash, &o.ch, | |
1021 | &ep, &epsz, | |
1022 | convgrand, &o.r)) | |
1023 | goto end; | |
1024 | sz = (nbits + 7)/8; | |
1025 | o.ep = ep; o.epsz = epsz; | |
1026 | if (2 * o.ch->hashsz + 2 + msz > sz) VALERR("buffer underflow"); | |
1027 | b = xmalloc(sz); | |
1028 | x = oaep_encode(MP_NEW, m, msz, b, sz, nbits, &o); | |
1029 | rc = mp_pywrap(x); | |
1030 | end: | |
1031 | xfree(b); | |
1032 | return (rc); | |
1033 | } | |
1034 | ||
1035 | static PyObject *meth__oaep_decode(PyObject *me, | |
1036 | PyObject *arg, PyObject *kw) | |
1037 | { | |
1038 | oaep o; | |
1039 | char *ep; | |
6b54260d | 1040 | Py_ssize_t epsz; |
d7ab1bab | 1041 | unsigned long nbits; |
1042 | int n; | |
1043 | PyObject *rc = 0; | |
1044 | octet *b = 0; | |
1045 | size_t sz; | |
1046 | mp *x = 0; | |
1047 | char *kwlist[] = { "ct", "nbits", "mgf", "hash", "ep", "rng", 0 }; | |
1048 | ||
1049 | o.r = &rand_global; o.cc = &sha_mgf; o.ch = &sha; ep = 0; epsz = 0; | |
1050 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&O&s#O&:decode", kwlist, | |
1051 | convmp, &x, convulong, &nbits, | |
1052 | convgccipher, &o.cc, | |
1053 | convgchash, &o.ch, | |
1054 | &ep, &epsz, | |
1055 | convgrand, &o.r)) | |
1056 | goto end; | |
1057 | sz = (nbits + 7)/8; | |
1058 | o.ep = ep; o.epsz = epsz; | |
1059 | if (2 * o.ch->hashsz > sz) VALERR("buffer underflow"); | |
1060 | b = xmalloc(sz); | |
1061 | if ((n = oaep_decode(x, b, sz, nbits, &o)) < 0) | |
1062 | VALERR("decryption failed"); | |
1063 | rc = bytestring_pywrap(b, n); | |
1064 | end: | |
1065 | mp_drop(x); | |
1066 | xfree(b); | |
1067 | return (rc); | |
1068 | } | |
1069 | ||
1070 | static PyObject *meth__pss_encode(PyObject *me, | |
1071 | PyObject *arg, PyObject *kw) | |
1072 | { | |
1073 | pss p; | |
1074 | char *m; | |
6b54260d | 1075 | Py_ssize_t msz; |
d7ab1bab | 1076 | unsigned long nbits; |
1077 | PyObject *rc = 0; | |
1078 | octet *b = 0; | |
1079 | size_t sz; | |
1080 | mp *x = 0; | |
1081 | char *kwlist[] = { "msg", "nbits", "mgf", "hash", "saltsz", "rng", 0 }; | |
1082 | ||
1083 | p.cc = &sha_mgf; p.ch = &sha; p.r = &rand_global; p.ssz = (size_t)-1; | |
1084 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&|O&O&O&O&:encode", kwlist, | |
1085 | &m, &msz, convulong, &nbits, | |
1086 | convgccipher, &p.cc, | |
1087 | convgchash, &p.ch, | |
1088 | convszt, &p.ssz, | |
1089 | convgrand, &p.r)) | |
1090 | goto end; | |
1091 | sz = (nbits + 7)/8; | |
1092 | if (p.ssz == (size_t)-1) p.ssz = p.ch->hashsz; | |
1093 | if (p.ch->hashsz + p.ssz + 2 > sz) VALERR("buffer underflow"); | |
1094 | b = xmalloc(sz); | |
1095 | x = pss_encode(MP_NEW, m, msz, b, sz, nbits, &p); | |
1096 | rc = mp_pywrap(x); | |
1097 | end: | |
1098 | xfree(b); | |
1099 | return (rc); | |
1100 | } | |
1101 | ||
1102 | static PyObject *meth__pss_decode(PyObject *me, | |
1103 | PyObject *arg, PyObject *kw) | |
1104 | { | |
1105 | pss p; | |
1106 | char *m; | |
6b54260d | 1107 | Py_ssize_t msz; |
d7ab1bab | 1108 | unsigned long nbits; |
1109 | PyObject *rc = 0; | |
1110 | octet *b = 0; | |
1111 | size_t sz; | |
1112 | int n; | |
1113 | mp *x = 0; | |
1114 | char *kwlist[] = | |
1115 | { "msg", "sig", "nbits", "mgf", "hash", "saltsz", "rng", 0 }; | |
1116 | ||
1117 | p.cc = &sha_mgf; p.ch = &sha; p.r = &rand_global; p.ssz = (size_t)-1; | |
1118 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&O&|O&O&O&O&:decode", kwlist, | |
1119 | &m, &msz, convmp, &x, convulong, &nbits, | |
1120 | convgccipher, &p.cc, | |
1121 | convgchash, &p.ch, | |
1122 | convszt, &p.ssz, | |
1123 | convgrand, &p.r)) | |
1124 | goto end; | |
1125 | sz = (nbits + 7)/8; | |
1126 | if (p.ssz == (size_t)-1) p.ssz = p.ch->hashsz; | |
1127 | if (p.ch->hashsz + p.ssz + 2 > sz) VALERR("buffer underflow"); | |
1128 | b = xmalloc(sz); | |
1129 | if ((n = pss_decode(x, m, msz, b, sz, nbits, &p)) < 0) | |
1130 | VALERR("verification failed"); | |
1131 | rc = Py_None; Py_INCREF(rc); | |
1132 | end: | |
1133 | mp_drop(x); | |
1134 | xfree(b); | |
1135 | return (rc); | |
1136 | } | |
1137 | ||
eb8aa4ec | 1138 | /*----- X25519 and related algorithms -------------------------------------*/ |
848ba392 | 1139 | |
34bea186 MW |
1140 | #define XDHS(_) \ |
1141 | _(X25519, x25519) \ | |
1142 | _(X448, x448) | |
1143 | ||
1144 | #define DEFXDH(X, x) \ | |
1145 | static PyObject *meth_##x(PyObject *me, PyObject *arg) \ | |
1146 | { \ | |
1147 | const char *k, *p; \ | |
1148 | Py_ssize_t ksz, psz; \ | |
1149 | PyObject *rc = 0; \ | |
1150 | if (!PyArg_ParseTuple(arg, "s#s#:" #x, &k, &ksz, &p, &psz)) \ | |
1151 | goto end; \ | |
1152 | if (ksz != X##_KEYSZ) VALERR("bad key length"); \ | |
1153 | if (psz != X##_PUBSZ) VALERR("bad public length"); \ | |
1154 | rc = bytestring_pywrap(0, X##_OUTSZ); \ | |
1155 | x((octet *)PyString_AS_STRING(rc), \ | |
1156 | (const octet *)k, (const octet *)p); \ | |
1157 | return (rc); \ | |
1158 | end: \ | |
1159 | return (0); \ | |
1160 | } | |
1161 | XDHS(DEFXDH) | |
1162 | #undef DEFXDH | |
eb8aa4ec | 1163 | |
dafb2da4 MW |
1164 | /*----- Ed25519 -----------------------------------------------------------*/ |
1165 | ||
1166 | static PyObject *meth_ed25519_pubkey(PyObject *me, PyObject *arg) | |
1167 | { | |
1168 | const char *k; | |
1169 | Py_ssize_t ksz; | |
1170 | PyObject *rc = 0; | |
1171 | if (!PyArg_ParseTuple(arg, "s#:ed25519_pubkey", &k, &ksz)) goto end; | |
1172 | rc = bytestring_pywrap(0, ED25519_PUBSZ); | |
1173 | ed25519_pubkey((octet *)PyString_AS_STRING(rc), k, ksz); | |
1174 | return (rc); | |
1175 | end: | |
1176 | return (0); | |
1177 | } | |
1178 | ||
1179 | static PyObject *meth_ed25519_sign(PyObject *me, PyObject *arg, PyObject *kw) | |
1180 | { | |
1181 | const char *k, *p = 0, *m; | |
1182 | Py_ssize_t ksz, psz, msz; | |
1183 | PyObject *rc = 0; | |
1184 | octet pp[ED25519_PUBSZ]; | |
1185 | char *kwlist[] = { "key", "msg", "pub", 0 }; | |
1186 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|s#:ed25519_sign", kwlist, | |
1187 | &k, &ksz, &m, &msz, &p, &psz)) | |
1188 | goto end; | |
1189 | if (p && psz != ED25519_PUBSZ) VALERR("bad public length"); | |
1190 | if (!p) { p = (const char *)pp; ed25519_pubkey(pp, k, ksz); } | |
1191 | rc = bytestring_pywrap(0, ED25519_SIGSZ); | |
1192 | ed25519_sign((octet *)PyString_AS_STRING(rc), k, ksz, | |
1193 | (const octet *)p, m, msz); | |
1194 | return (rc); | |
1195 | end: | |
1196 | return (0); | |
1197 | } | |
1198 | ||
1199 | static PyObject *meth_ed25519_verify(PyObject *me, PyObject *arg) | |
1200 | { | |
1201 | const char *p, *m, *s; | |
1202 | Py_ssize_t psz, msz, ssz; | |
1203 | PyObject *rc = 0; | |
1204 | if (!PyArg_ParseTuple(arg, "s#s#s#:ed25519_verify", | |
1205 | &p, &psz, &m, &msz, &s, &ssz)) | |
1206 | goto end; | |
1207 | if (psz != ED25519_PUBSZ) VALERR("bad public length"); | |
1208 | if (ssz != ED25519_SIGSZ) VALERR("bad signature length"); | |
1209 | rc = getbool(!ed25519_verify((const octet *)p, m, msz, (const octet *)s)); | |
1210 | return (rc); | |
1211 | end: | |
1212 | return (0); | |
1213 | } | |
1214 | ||
d7ab1bab | 1215 | /*----- Global stuff ------------------------------------------------------*/ |
1216 | ||
1217 | static PyMethodDef methods[] = { | |
1218 | #define METHNAME(name) meth_##name | |
1219 | KWMETH(_p1crypt_encode, 0) | |
1220 | KWMETH(_p1crypt_decode, 0) | |
1221 | KWMETH(_p1sig_encode, 0) | |
1222 | KWMETH(_p1sig_decode, 0) | |
1223 | KWMETH(_oaep_encode, 0) | |
1224 | KWMETH(_oaep_decode, 0) | |
1225 | KWMETH(_pss_encode, 0) | |
1226 | KWMETH(_pss_decode, 0) | |
1227 | KWMETH(_RSAPriv_generate, "\ | |
1228 | generate(NBITS, [event = pgen_nullev, rng = rand, nsteps = 0]) -> R") | |
34bea186 MW |
1229 | #define DEFMETH(X, x) \ |
1230 | METH (x, "\ | |
1231 | " #x "(KEY, PUBLIC) -> SHARED") | |
1232 | XDHS(DEFMETH) | |
1233 | #undef DEFMETH | |
dafb2da4 MW |
1234 | METH (ed25519_pubkey, "\ |
1235 | ed25519_pubkey(KEY) -> PUBLIC") | |
1236 | KWMETH(ed25519_sign, "\ | |
1237 | ed25519_sign(KEY, MSG, [PUBLIC]) -> SIG") | |
1238 | METH (ed25519_verify, "\ | |
1239 | ed25519_verify(PUBLIC, MSG, SIG) -> BOOL") | |
d7ab1bab | 1240 | #undef METHNAME |
1241 | { 0 } | |
1242 | }; | |
1243 | ||
1244 | void pubkey_pyinit(void) | |
1245 | { | |
1246 | INITTYPE(dsapub, root); | |
1247 | INITTYPE(dsapriv, dsapub); | |
1248 | INITTYPE(kcdsapub, root); | |
1249 | INITTYPE(kcdsapriv, kcdsapub); | |
1250 | INITTYPE(rsapub, root); | |
1251 | INITTYPE(rsapriv, rsapub); | |
1252 | addmethods(methods); | |
1253 | } | |
1254 | ||
1255 | void pubkey_pyinsert(PyObject *mod) | |
1256 | { | |
1257 | INSERT("DSAPub", dsapub_pytype); | |
1258 | INSERT("DSAPriv", dsapriv_pytype); | |
1259 | INSERT("KCDSAPub", kcdsapub_pytype); | |
1260 | INSERT("KCDSAPriv", kcdsapriv_pytype); | |
1261 | INSERT("RSAPub", rsapub_pytype); | |
1262 | INSERT("RSAPriv", rsapriv_pytype); | |
1263 | } | |
1264 | ||
1265 | /*----- That's all, folks -------------------------------------------------*/ |