5 * (c) 2005 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- GF(2^8)-based secret-sharing --------------------------------------*/
33 typedef struct gfshare_pyobj
{
39 *gfshare_pytype
, *gfsharesplit_pytype
, *gfsharejoin_pytype
;
40 #define GFSHARE_PYCHECK(o) PyObject_TypeCheck((o), gfshare_pytype)
41 #define GFSHARESPLIT_PYCHECK(o) PyObject_TypeCheck((o), gfsharesplit_pytype)
42 #define GFSHAREJOIN_PYCHECK(o) PyObject_TypeCheck((o), gfsharejoin_pytype)
43 #define GFSHARE_S(o) (&((gfshare_pyobj *)(o))->s)
45 static void gfshare_pydealloc(PyObject
*me
)
47 gfshare_destroy(GFSHARE_S(me
));
51 static PyObject
*gfsget_threshold(PyObject
*me
, void *hunoz
)
52 { return (PyInt_FromLong(GFSHARE_S(me
)->t
)); }
53 static PyObject
*gfsget_size(PyObject
*me
, void *hunoz
)
54 { return (PyInt_FromLong(GFSHARE_S(me
)->sz
)); }
56 static PyGetSetDef gfshare_pygetset
[]= {
57 #define GETSETNAME(op, name) gfs##op##_##name
58 GET (threshold
, "S.threshold -> THRESHOLD")
59 GET (size
, "S.size -> SECRETSZ")
64 static PyTypeObject gfshare_pytype_skel
= {
65 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
66 "GFShare", /* @tp_name@ */
67 sizeof(gfshare_pyobj
), /* @tp_basicsize@ */
68 0, /* @tp_itemsize@ */
70 gfshare_pydealloc
, /* @tp_dealloc@ */
76 0, /* @tp_as_number@ */
77 0, /* @tp_as_sequence@ */
78 0, /* @tp_as_mapping@ */
82 0, /* @tp_getattro@ */
83 0, /* @tp_setattro@ */
84 0, /* @tp_as_buffer@ */
85 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
89 "Binary-field secret sharing base class.",
91 0, /* @tp_traverse@ */
93 0, /* @tp_richcompare@ */
94 0, /* @tp_weaklistoffset@ */
96 0, /* @tp_iternext@ */
99 gfshare_pygetset
, /* @tp_getset@ */
102 0, /* @tp_descr_get@ */
103 0, /* @tp_descr_set@ */
104 0, /* @tp_dictoffset@ */
106 PyType_GenericAlloc
, /* @tp_alloc@ */
107 abstract_pynew
, /* @tp_new@ */
112 static PyObject
*gfsharesplit_pynew(PyTypeObject
*ty
,
113 PyObject
*arg
, PyObject
*kw
)
118 grand
*r
= &rand_global
;
120 char *kwlist
[] = { "threshold", "secret", "rng", 0 };
121 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&s#|O&:new", kwlist
,
122 convuint
, &t
, &p
, &n
, convgrand
, &r
))
124 if (!t
|| t
> 255) VALERR("threshold must be nonzero and < 256");
125 s
= (gfshare_pyobj
*)ty
->tp_alloc(ty
, 0);
126 gfshare_create(&s
->s
, t
, n
);
127 gfshare_mkshares(&s
->s
, r
, p
);
128 return ((PyObject
*)s
);
133 static PyObject
*gfsmeth_get(PyObject
*me
, PyObject
*arg
)
137 if (!PyArg_ParseTuple(arg
, "O&:get", convuint
, &i
)) goto end
;
138 if (i
>= 255) VALERR("index must be < 255");
139 rc
= bytestring_pywrap(0, GFSHARE_S(me
)->sz
);
140 gfshare_get(GFSHARE_S(me
), i
, PyString_AS_STRING(rc
));
145 static PyMethodDef gfsharesplit_pymethods
[] = {
146 #define METHNAME(name) gfsmeth_##name
147 METH (get
, "S.get(I) -> SHARE")
152 static PyTypeObject gfsharesplit_pytype_skel
= {
153 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
154 "GFShareSplit", /* @tp_name@ */
155 sizeof(gfshare_pyobj
), /* @tp_basicsize@ */
156 0, /* @tp_itemsize@ */
158 gfshare_pydealloc
, /* @tp_dealloc@ */
160 0, /* @tp_getattr@ */
161 0, /* @tp_setattr@ */
162 0, /* @tp_compare@ */
164 0, /* @tp_as_number@ */
165 0, /* @tp_as_sequence@ */
166 0, /* @tp_as_mapping@ */
170 0, /* @tp_getattro@ */
171 0, /* @tp_setattro@ */
172 0, /* @tp_as_buffer@ */
173 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
177 "Binary field secret sharing: split secret into shares.",
179 0, /* @tp_traverse@ */
181 0, /* @tp_richcompare@ */
182 0, /* @tp_weaklistoffset@ */
184 0, /* @tp_iternext@ */
185 gfsharesplit_pymethods
, /* @tp_methods@ */
186 0, /* @tp_members@ */
190 0, /* @tp_descr_get@ */
191 0, /* @tp_descr_set@ */
192 0, /* @tp_dictoffset@ */
194 PyType_GenericAlloc
, /* @tp_alloc@ */
195 gfsharesplit_pynew
, /* @tp_new@ */
200 static PyObject
*gfsharejoin_pynew(PyTypeObject
*ty
,
201 PyObject
*arg
, PyObject
*kw
)
205 char *kwlist
[] = { "threshold", "size", 0 };
206 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:new", kwlist
,
207 convuint
, &t
, convuint
, &sz
))
209 if (!t
|| t
> 255) VALERR("threshold must be nonzero and < 256");
210 s
= (gfshare_pyobj
*)ty
->tp_alloc(ty
, 0);
211 gfshare_create(&s
->s
, t
, sz
);
212 return ((PyObject
*)s
);
217 static PyObject
*gfsmeth_addedp(PyObject
*me
, PyObject
*arg
)
220 if (!PyArg_ParseTuple(arg
, "O&:addedp", convuint
, &i
)) goto end
;
221 if (i
> 254) VALERR("index must be < 255");
222 return (getbool(gfshare_addedp(GFSHARE_S(me
), i
)));
227 static PyObject
*gfsmeth_add(PyObject
*me
, PyObject
*arg
)
232 if (!PyArg_ParseTuple(arg
, "O&s#:add", convuint
, &i
, &p
, &n
)) goto end
;
233 if (i
> 254) VALERR("index must be < 255");
234 if (n
!= GFSHARE_S(me
)->sz
) VALERR("bad share size");
235 if (gfshare_addedp(GFSHARE_S(me
), i
)) VALERR("this share already added");
236 if (GFSHARE_S(me
)->i
>= GFSHARE_S(me
)->t
) VALERR("enough shares already");
237 gfshare_add(GFSHARE_S(me
), i
, p
);
238 return (PyInt_FromLong(GFSHARE_S(me
)->t
- GFSHARE_S(me
)->i
));
243 static PyObject
*gfsmeth_combine(PyObject
*me
, PyObject
*arg
)
246 if (!PyArg_ParseTuple(arg
, ":combine")) goto end
;
247 if (GFSHARE_S(me
)->i
< GFSHARE_S(me
)->t
) VALERR("not enough shares yet");
248 rc
= bytestring_pywrap(0, GFSHARE_S(me
)->sz
);
249 gfshare_combine(GFSHARE_S(me
), PyString_AS_STRING(rc
));
254 static PyMethodDef gfsharejoin_pymethods
[] = {
255 #define METHNAME(name) gfsmeth_##name
256 METH (addedp
, "S.addedp(I) -> BOOL")
257 METH (add
, "S.add(I, SHARE) -> REMAIN")
258 METH (combine
, "S.combine() -> SECRET")
263 static PyObject
*gfsget_remain(PyObject
*me
, void *hunoz
)
264 { return (PyInt_FromLong(GFSHARE_S(me
)->t
- GFSHARE_S(me
)->i
)); }
266 static PyGetSetDef gfsharejoin_pygetset
[]= {
267 #define GETSETNAME(op, name) gfs##op##_##name
268 GET (remain
, "S.remain -> REMAIN")
273 static PyTypeObject gfsharejoin_pytype_skel
= {
274 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
275 "GFShareJoin", /* @tp_name@ */
276 sizeof(gfshare_pyobj
), /* @tp_basicsize@ */
277 0, /* @tp_itemsize@ */
279 gfshare_pydealloc
, /* @tp_dealloc@ */
281 0, /* @tp_getattr@ */
282 0, /* @tp_setattr@ */
283 0, /* @tp_compare@ */
285 0, /* @tp_as_number@ */
286 0, /* @tp_as_sequence@ */
287 0, /* @tp_as_mapping@ */
291 0, /* @tp_getattro@ */
292 0, /* @tp_setattro@ */
293 0, /* @tp_as_buffer@ */
294 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
298 "Binary field secret sharing: join shares to recover secret.",
300 0, /* @tp_traverse@ */
302 0, /* @tp_richcompare@ */
303 0, /* @tp_weaklistoffset@ */
305 0, /* @tp_iternext@ */
306 gfsharejoin_pymethods
, /* @tp_methods@ */
307 0, /* @tp_members@ */
308 gfsharejoin_pygetset
, /* @tp_getset@ */
311 0, /* @tp_descr_get@ */
312 0, /* @tp_descr_set@ */
313 0, /* @tp_dictoffset@ */
315 PyType_GenericAlloc
, /* @tp_alloc@ */
316 gfsharejoin_pynew
, /* @tp_new@ */
321 /*----- Prime-field secret-sharing ----------------------------------------*/
323 typedef struct share_pyobj
{
329 *share_pytype
, *sharesplit_pytype
, *sharejoin_pytype
;
330 #define SHARE_PYCHECK(o) PyObject_TypeCheck((o), share_pytype)
331 #define SHARESPLIT_PYCHECK(o) PyObject_TypeCheck((o), sharesplit_pytype)
332 #define SHAREJOIN_PYCHECK(o) PyObject_TypeCheck((o), sharejoin_pytype)
333 #define SHARE_S(o) (&((share_pyobj *)(o))->s)
335 static void share_pydealloc(PyObject
*me
)
337 share_destroy(SHARE_S(me
));
341 static PyObject
*sget_threshold(PyObject
*me
, void *hunoz
)
342 { return (PyInt_FromLong(SHARE_S(me
)->t
)); }
343 static PyObject
*sget_modulus(PyObject
*me
, void *hunoz
)
344 { return (mp_pywrap(SHARE_S(me
)->p
)); }
346 static PyGetSetDef share_pygetset
[]= {
347 #define GETSETNAME(op, name) s##op##_##name
348 GET (threshold
, "S.threshold -> THRESHOLD")
349 GET (modulus
, "S.modulus -> MODULUS")
354 static PyTypeObject share_pytype_skel
= {
355 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
356 "Share", /* @tp_name@ */
357 sizeof(share_pyobj
), /* @tp_basicsize@ */
358 0, /* @tp_itemsize@ */
360 share_pydealloc
, /* @tp_dealloc@ */
362 0, /* @tp_getattr@ */
363 0, /* @tp_setattr@ */
364 0, /* @tp_compare@ */
366 0, /* @tp_as_number@ */
367 0, /* @tp_as_sequence@ */
368 0, /* @tp_as_mapping@ */
372 0, /* @tp_getattro@ */
373 0, /* @tp_setattro@ */
374 0, /* @tp_as_buffer@ */
375 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
379 "Prime-field secret sharing base class.",
381 0, /* @tp_traverse@ */
383 0, /* @tp_richcompare@ */
384 0, /* @tp_weaklistoffset@ */
386 0, /* @tp_iternext@ */
387 0, /* @tp_methods@ */
388 0, /* @tp_members@ */
389 share_pygetset
, /* @tp_getset@ */
392 0, /* @tp_descr_get@ */
393 0, /* @tp_descr_set@ */
394 0, /* @tp_dictoffset@ */
396 PyType_GenericAlloc
, /* @tp_alloc@ */
397 abstract_pynew
, /* @tp_new@ */
402 static PyObject
*sharesplit_pynew(PyTypeObject
*ty
,
403 PyObject
*arg
, PyObject
*kw
)
407 grand
*r
= &rand_global
;
410 char *kwlist
[] = { "threshold", "secret", "modulus", "rng", 0 };
411 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&|O&O&:new", kwlist
,
412 convuint
, &t
, convmp
, &sec
,
413 convmp
, &m
, convgrand
, &r
))
415 if (!t
) VALERR("threshold must be nonzero");
416 s
= (share_pyobj
*)ty
->tp_alloc(ty
, 0);
417 share_create(&s
->s
, t
);
419 share_mkshares(&s
->s
, r
, sec
);
421 return ((PyObject
*)s
);
428 static PyObject
*smeth_get(PyObject
*me
, PyObject
*arg
)
432 if (!PyArg_ParseTuple(arg
, "O&:get", convuint
, &i
)) goto end
;
433 rc
= mp_pywrap(share_get(SHARE_S(me
), MP_NEW
, i
));
438 static PyMethodDef sharesplit_pymethods
[] = {
439 #define METHNAME(name) smeth_##name
440 METH (get
, "S.get(I) -> SHARE")
445 static PyTypeObject sharesplit_pytype_skel
= {
446 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
447 "ShareSplit", /* @tp_name@ */
448 sizeof(share_pyobj
), /* @tp_basicsize@ */
449 0, /* @tp_itemsize@ */
451 share_pydealloc
, /* @tp_dealloc@ */
453 0, /* @tp_getattr@ */
454 0, /* @tp_setattr@ */
455 0, /* @tp_compare@ */
457 0, /* @tp_as_number@ */
458 0, /* @tp_as_sequence@ */
459 0, /* @tp_as_mapping@ */
463 0, /* @tp_getattro@ */
464 0, /* @tp_setattro@ */
465 0, /* @tp_as_buffer@ */
466 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
470 "Prime field secret sharing: split secret into shares.",
472 0, /* @tp_traverse@ */
474 0, /* @tp_richcompare@ */
475 0, /* @tp_weaklistoffset@ */
477 0, /* @tp_iternext@ */
478 sharesplit_pymethods
, /* @tp_methods@ */
479 0, /* @tp_members@ */
483 0, /* @tp_descr_get@ */
484 0, /* @tp_descr_set@ */
485 0, /* @tp_dictoffset@ */
487 PyType_GenericAlloc
, /* @tp_alloc@ */
488 sharesplit_pynew
, /* @tp_new@ */
493 static PyObject
*sharejoin_pynew(PyTypeObject
*ty
,
494 PyObject
*arg
, PyObject
*kw
)
499 char *kwlist
[] = { "threshold", "modulus", 0 };
500 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:new", kwlist
,
501 convuint
, &t
, convmp
, &m
))
503 if (!t
) VALERR("threshold must be nonzero");
504 s
= (share_pyobj
*)ty
->tp_alloc(ty
, 0);
505 share_create(&s
->s
, t
);
507 return ((PyObject
*)s
);
513 static PyObject
*smeth_addedp(PyObject
*me
, PyObject
*arg
)
516 if (!PyArg_ParseTuple(arg
, "O&:addedp", convuint
, &i
)) goto end
;
517 return (getbool(share_addedp(SHARE_S(me
), i
)));
522 static PyObject
*smeth_add(PyObject
*me
, PyObject
*arg
)
527 if (!PyArg_ParseTuple(arg
, "O&O&:add", convuint
, &i
, convmp
, &s
)) goto end
;
528 if (MP_NEGP(s
) || MP_CMP(s
, >=, SHARE_S(me
)->p
))
529 VALERR("share out of range");
530 if (share_addedp(SHARE_S(me
), i
)) VALERR("this share already added");
531 if (SHARE_S(me
)->i
>= SHARE_S(me
)->t
) VALERR("enough shares already");
532 share_add(SHARE_S(me
), i
, s
);
533 rc
= PyInt_FromLong(SHARE_S(me
)->t
- SHARE_S(me
)->i
);
539 static PyObject
*smeth_combine(PyObject
*me
, PyObject
*arg
)
542 if (!PyArg_ParseTuple(arg
, ":combine")) goto end
;
543 if (SHARE_S(me
)->i
< SHARE_S(me
)->t
) VALERR("not enough shares yet");
544 rc
= mp_pywrap(share_combine(SHARE_S(me
)));
549 static PyMethodDef sharejoin_pymethods
[] = {
550 #define METHNAME(name) smeth_##name
551 METH (addedp
, "S.addedp(I) -> BOOL")
552 METH (add
, "S.add(I, SHARE) -> REMAIN")
553 METH (combine
, "S.combine() -> SECRET")
558 static PyObject
*sget_remain(PyObject
*me
, void *hunoz
)
559 { return (PyInt_FromLong(SHARE_S(me
)->t
- SHARE_S(me
)->i
)); }
561 static PyGetSetDef sharejoin_pygetset
[]= {
562 #define GETSETNAME(op, name) s##op##_##name
563 GET (remain
, "S.remain -> REMAIN")
568 static PyTypeObject sharejoin_pytype_skel
= {
569 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
570 "ShareJoin", /* @tp_name@ */
571 sizeof(share_pyobj
), /* @tp_basicsize@ */
572 0, /* @tp_itemsize@ */
574 share_pydealloc
, /* @tp_dealloc@ */
576 0, /* @tp_getattr@ */
577 0, /* @tp_setattr@ */
578 0, /* @tp_compare@ */
580 0, /* @tp_as_number@ */
581 0, /* @tp_as_sequence@ */
582 0, /* @tp_as_mapping@ */
586 0, /* @tp_getattro@ */
587 0, /* @tp_setattro@ */
588 0, /* @tp_as_buffer@ */
589 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
593 "Prime field secret sharing: join shares to recover secret.",
595 0, /* @tp_traverse@ */
597 0, /* @tp_richcompare@ */
598 0, /* @tp_weaklistoffset@ */
600 0, /* @tp_iternext@ */
601 sharejoin_pymethods
, /* @tp_methods@ */
602 0, /* @tp_members@ */
603 sharejoin_pygetset
, /* @tp_getset@ */
606 0, /* @tp_descr_get@ */
607 0, /* @tp_descr_set@ */
608 0, /* @tp_dictoffset@ */
610 PyType_GenericAlloc
, /* @tp_alloc@ */
611 sharejoin_pynew
, /* @tp_new@ */
616 /*----- Global stuff ------------------------------------------------------*/
618 void share_pyinit(void)
620 INITTYPE(gfshare
, root
);
621 INITTYPE(gfsharesplit
, gfshare
);
622 INITTYPE(gfsharejoin
, gfshare
);
623 INITTYPE(share
, root
);
624 INITTYPE(sharesplit
, share
);
625 INITTYPE(sharejoin
, share
);
628 void share_pyinsert(PyObject
*mod
)
630 INSERT("GFShare", gfshare_pytype
);
631 INSERT("GFShareSplit", gfsharesplit_pytype
);
632 INSERT("GFShareJoin", gfsharejoin_pytype
);
633 INSERT("Share", share_pytype
);
634 INSERT("ShareSplit", sharesplit_pytype
);
635 INSERT("ShareJoin", sharejoin_pytype
);
638 /*----- That's all, folks -------------------------------------------------*/