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