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 "GFShareSplit(THRESHOLD, SECRET, [rng = rand]): binary-field sharing:\n\
178 split secret into shares.",
180 0, /* @tp_traverse@ */
182 0, /* @tp_richcompare@ */
183 0, /* @tp_weaklistoffset@ */
185 0, /* @tp_iternext@ */
186 gfsharesplit_pymethods
, /* @tp_methods@ */
187 0, /* @tp_members@ */
191 0, /* @tp_descr_get@ */
192 0, /* @tp_descr_set@ */
193 0, /* @tp_dictoffset@ */
195 PyType_GenericAlloc
, /* @tp_alloc@ */
196 gfsharesplit_pynew
, /* @tp_new@ */
201 static PyObject
*gfsharejoin_pynew(PyTypeObject
*ty
,
202 PyObject
*arg
, PyObject
*kw
)
206 char *kwlist
[] = { "threshold", "size", 0 };
207 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:new", kwlist
,
208 convuint
, &t
, convuint
, &sz
))
210 if (!t
|| t
> 255) VALERR("threshold must be nonzero and < 256");
211 s
= (gfshare_pyobj
*)ty
->tp_alloc(ty
, 0);
212 gfshare_create(&s
->s
, t
, sz
);
213 return ((PyObject
*)s
);
218 static PyObject
*gfsmeth_addedp(PyObject
*me
, PyObject
*arg
)
221 if (!PyArg_ParseTuple(arg
, "O&:addedp", convuint
, &i
)) goto end
;
222 if (i
> 254) VALERR("index must be < 255");
223 return (getbool(gfshare_addedp(GFSHARE_S(me
), i
)));
228 static PyObject
*gfsmeth_add(PyObject
*me
, PyObject
*arg
)
233 if (!PyArg_ParseTuple(arg
, "O&s#:add", convuint
, &i
, &p
, &n
)) goto end
;
234 if (i
> 254) VALERR("index must be < 255");
235 if (n
!= GFSHARE_S(me
)->sz
) VALERR("bad share size");
236 if (gfshare_addedp(GFSHARE_S(me
), i
)) VALERR("this share already added");
237 if (GFSHARE_S(me
)->i
>= GFSHARE_S(me
)->t
) VALERR("enough shares already");
238 gfshare_add(GFSHARE_S(me
), i
, p
);
239 return (PyInt_FromLong(GFSHARE_S(me
)->t
- GFSHARE_S(me
)->i
));
244 static PyObject
*gfsmeth_combine(PyObject
*me
, PyObject
*arg
)
247 if (!PyArg_ParseTuple(arg
, ":combine")) goto end
;
248 if (GFSHARE_S(me
)->i
< GFSHARE_S(me
)->t
) VALERR("not enough shares yet");
249 rc
= bytestring_pywrap(0, GFSHARE_S(me
)->sz
);
250 gfshare_combine(GFSHARE_S(me
), PyString_AS_STRING(rc
));
255 static PyMethodDef gfsharejoin_pymethods
[] = {
256 #define METHNAME(name) gfsmeth_##name
257 METH (addedp
, "S.addedp(I) -> BOOL")
258 METH (add
, "S.add(I, SHARE) -> REMAIN")
259 METH (combine
, "S.combine() -> SECRET")
264 static PyObject
*gfsget_remain(PyObject
*me
, void *hunoz
)
265 { return (PyInt_FromLong(GFSHARE_S(me
)->t
- GFSHARE_S(me
)->i
)); }
267 static PyGetSetDef gfsharejoin_pygetset
[]= {
268 #define GETSETNAME(op, name) gfs##op##_##name
269 GET (remain
, "S.remain -> REMAIN")
274 static PyTypeObject gfsharejoin_pytype_skel
= {
275 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
276 "GFShareJoin", /* @tp_name@ */
277 sizeof(gfshare_pyobj
), /* @tp_basicsize@ */
278 0, /* @tp_itemsize@ */
280 gfshare_pydealloc
, /* @tp_dealloc@ */
282 0, /* @tp_getattr@ */
283 0, /* @tp_setattr@ */
284 0, /* @tp_compare@ */
286 0, /* @tp_as_number@ */
287 0, /* @tp_as_sequence@ */
288 0, /* @tp_as_mapping@ */
292 0, /* @tp_getattro@ */
293 0, /* @tp_setattro@ */
294 0, /* @tp_as_buffer@ */
295 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
299 "GFShareJoin(THRESHOLD, SIZE): binary field sharing:\n\
300 join shares to recover secret.",
302 0, /* @tp_traverse@ */
304 0, /* @tp_richcompare@ */
305 0, /* @tp_weaklistoffset@ */
307 0, /* @tp_iternext@ */
308 gfsharejoin_pymethods
, /* @tp_methods@ */
309 0, /* @tp_members@ */
310 gfsharejoin_pygetset
, /* @tp_getset@ */
313 0, /* @tp_descr_get@ */
314 0, /* @tp_descr_set@ */
315 0, /* @tp_dictoffset@ */
317 PyType_GenericAlloc
, /* @tp_alloc@ */
318 gfsharejoin_pynew
, /* @tp_new@ */
323 /*----- Prime-field secret-sharing ----------------------------------------*/
325 typedef struct share_pyobj
{
331 *share_pytype
, *sharesplit_pytype
, *sharejoin_pytype
;
332 #define SHARE_PYCHECK(o) PyObject_TypeCheck((o), share_pytype)
333 #define SHARESPLIT_PYCHECK(o) PyObject_TypeCheck((o), sharesplit_pytype)
334 #define SHAREJOIN_PYCHECK(o) PyObject_TypeCheck((o), sharejoin_pytype)
335 #define SHARE_S(o) (&((share_pyobj *)(o))->s)
337 static void share_pydealloc(PyObject
*me
)
339 share_destroy(SHARE_S(me
));
343 static PyObject
*sget_threshold(PyObject
*me
, void *hunoz
)
344 { return (PyInt_FromLong(SHARE_S(me
)->t
)); }
345 static PyObject
*sget_modulus(PyObject
*me
, void *hunoz
)
346 { return (mp_pywrap(SHARE_S(me
)->p
)); }
348 static PyGetSetDef share_pygetset
[]= {
349 #define GETSETNAME(op, name) s##op##_##name
350 GET (threshold
, "S.threshold -> THRESHOLD")
351 GET (modulus
, "S.modulus -> MODULUS")
356 static PyTypeObject share_pytype_skel
= {
357 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
358 "Share", /* @tp_name@ */
359 sizeof(share_pyobj
), /* @tp_basicsize@ */
360 0, /* @tp_itemsize@ */
362 share_pydealloc
, /* @tp_dealloc@ */
364 0, /* @tp_getattr@ */
365 0, /* @tp_setattr@ */
366 0, /* @tp_compare@ */
368 0, /* @tp_as_number@ */
369 0, /* @tp_as_sequence@ */
370 0, /* @tp_as_mapping@ */
374 0, /* @tp_getattro@ */
375 0, /* @tp_setattro@ */
376 0, /* @tp_as_buffer@ */
377 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
381 "Prime-field secret sharing base class.",
383 0, /* @tp_traverse@ */
385 0, /* @tp_richcompare@ */
386 0, /* @tp_weaklistoffset@ */
388 0, /* @tp_iternext@ */
389 0, /* @tp_methods@ */
390 0, /* @tp_members@ */
391 share_pygetset
, /* @tp_getset@ */
394 0, /* @tp_descr_get@ */
395 0, /* @tp_descr_set@ */
396 0, /* @tp_dictoffset@ */
398 PyType_GenericAlloc
, /* @tp_alloc@ */
399 abstract_pynew
, /* @tp_new@ */
404 static PyObject
*sharesplit_pynew(PyTypeObject
*ty
,
405 PyObject
*arg
, PyObject
*kw
)
409 grand
*r
= &rand_global
;
412 char *kwlist
[] = { "threshold", "secret", "modulus", "rng", 0 };
413 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&|O&O&:new", kwlist
,
414 convuint
, &t
, convmp
, &sec
,
415 convmp
, &m
, convgrand
, &r
))
417 if (!t
) VALERR("threshold must be nonzero");
418 s
= (share_pyobj
*)ty
->tp_alloc(ty
, 0);
419 share_create(&s
->s
, t
);
421 share_mkshares(&s
->s
, r
, sec
);
423 return ((PyObject
*)s
);
430 static PyObject
*smeth_get(PyObject
*me
, PyObject
*arg
)
434 if (!PyArg_ParseTuple(arg
, "O&:get", convuint
, &i
)) goto end
;
435 rc
= mp_pywrap(share_get(SHARE_S(me
), MP_NEW
, i
));
440 static PyMethodDef sharesplit_pymethods
[] = {
441 #define METHNAME(name) smeth_##name
442 METH (get
, "S.get(I) -> SHARE")
447 static PyTypeObject sharesplit_pytype_skel
= {
448 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
449 "ShareSplit", /* @tp_name@ */
450 sizeof(share_pyobj
), /* @tp_basicsize@ */
451 0, /* @tp_itemsize@ */
453 share_pydealloc
, /* @tp_dealloc@ */
455 0, /* @tp_getattr@ */
456 0, /* @tp_setattr@ */
457 0, /* @tp_compare@ */
459 0, /* @tp_as_number@ */
460 0, /* @tp_as_sequence@ */
461 0, /* @tp_as_mapping@ */
465 0, /* @tp_getattro@ */
466 0, /* @tp_setattro@ */
467 0, /* @tp_as_buffer@ */
468 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
472 "ShareSplit(THRESHOLD, SECRET, [modulus = ?], [rng = rand]):\n\
473 prime field secret sharing: split secret into shares.",
475 0, /* @tp_traverse@ */
477 0, /* @tp_richcompare@ */
478 0, /* @tp_weaklistoffset@ */
480 0, /* @tp_iternext@ */
481 sharesplit_pymethods
, /* @tp_methods@ */
482 0, /* @tp_members@ */
486 0, /* @tp_descr_get@ */
487 0, /* @tp_descr_set@ */
488 0, /* @tp_dictoffset@ */
490 PyType_GenericAlloc
, /* @tp_alloc@ */
491 sharesplit_pynew
, /* @tp_new@ */
496 static PyObject
*sharejoin_pynew(PyTypeObject
*ty
,
497 PyObject
*arg
, PyObject
*kw
)
502 char *kwlist
[] = { "threshold", "modulus", 0 };
503 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:new", kwlist
,
504 convuint
, &t
, convmp
, &m
))
506 if (!t
) VALERR("threshold must be nonzero");
507 s
= (share_pyobj
*)ty
->tp_alloc(ty
, 0);
508 share_create(&s
->s
, t
);
510 return ((PyObject
*)s
);
516 static PyObject
*smeth_addedp(PyObject
*me
, PyObject
*arg
)
519 if (!PyArg_ParseTuple(arg
, "O&:addedp", convuint
, &i
)) goto end
;
520 return (getbool(share_addedp(SHARE_S(me
), i
)));
525 static PyObject
*smeth_add(PyObject
*me
, PyObject
*arg
)
530 if (!PyArg_ParseTuple(arg
, "O&O&:add", convuint
, &i
, convmp
, &s
)) goto end
;
531 if (MP_NEGP(s
) || MP_CMP(s
, >=, SHARE_S(me
)->p
))
532 VALERR("share out of range");
533 if (share_addedp(SHARE_S(me
), i
)) VALERR("this share already added");
534 if (SHARE_S(me
)->i
>= SHARE_S(me
)->t
) VALERR("enough shares already");
535 share_add(SHARE_S(me
), i
, s
);
536 rc
= PyInt_FromLong(SHARE_S(me
)->t
- SHARE_S(me
)->i
);
542 static PyObject
*smeth_combine(PyObject
*me
, PyObject
*arg
)
545 if (!PyArg_ParseTuple(arg
, ":combine")) goto end
;
546 if (SHARE_S(me
)->i
< SHARE_S(me
)->t
) VALERR("not enough shares yet");
547 rc
= mp_pywrap(share_combine(SHARE_S(me
)));
552 static PyMethodDef sharejoin_pymethods
[] = {
553 #define METHNAME(name) smeth_##name
554 METH (addedp
, "S.addedp(I) -> BOOL")
555 METH (add
, "S.add(I, SHARE) -> REMAIN")
556 METH (combine
, "S.combine() -> SECRET")
561 static PyObject
*sget_remain(PyObject
*me
, void *hunoz
)
562 { return (PyInt_FromLong(SHARE_S(me
)->t
- SHARE_S(me
)->i
)); }
564 static PyGetSetDef sharejoin_pygetset
[]= {
565 #define GETSETNAME(op, name) s##op##_##name
566 GET (remain
, "S.remain -> REMAIN")
571 static PyTypeObject sharejoin_pytype_skel
= {
572 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
573 "ShareJoin", /* @tp_name@ */
574 sizeof(share_pyobj
), /* @tp_basicsize@ */
575 0, /* @tp_itemsize@ */
577 share_pydealloc
, /* @tp_dealloc@ */
579 0, /* @tp_getattr@ */
580 0, /* @tp_setattr@ */
581 0, /* @tp_compare@ */
583 0, /* @tp_as_number@ */
584 0, /* @tp_as_sequence@ */
585 0, /* @tp_as_mapping@ */
589 0, /* @tp_getattro@ */
590 0, /* @tp_setattro@ */
591 0, /* @tp_as_buffer@ */
592 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
596 "ShareJoin(THRESHOLD, MODULUS): prime field secret sharing:\n\
597 join shares to recover secret.",
599 0, /* @tp_traverse@ */
601 0, /* @tp_richcompare@ */
602 0, /* @tp_weaklistoffset@ */
604 0, /* @tp_iternext@ */
605 sharejoin_pymethods
, /* @tp_methods@ */
606 0, /* @tp_members@ */
607 sharejoin_pygetset
, /* @tp_getset@ */
610 0, /* @tp_descr_get@ */
611 0, /* @tp_descr_set@ */
612 0, /* @tp_dictoffset@ */
614 PyType_GenericAlloc
, /* @tp_alloc@ */
615 sharejoin_pynew
, /* @tp_new@ */
620 /*----- Global stuff ------------------------------------------------------*/
622 void share_pyinit(void)
624 INITTYPE(gfshare
, root
);
625 INITTYPE(gfsharesplit
, gfshare
);
626 INITTYPE(gfsharejoin
, gfshare
);
627 INITTYPE(share
, root
);
628 INITTYPE(sharesplit
, share
);
629 INITTYPE(sharejoin
, share
);
632 void share_pyinsert(PyObject
*mod
)
634 INSERT("GFShare", gfshare_pytype
);
635 INSERT("GFShareSplit", gfsharesplit_pytype
);
636 INSERT("GFShareJoin", gfsharejoin_pytype
);
637 INSERT("Share", share_pytype
);
638 INSERT("ShareSplit", sharesplit_pytype
);
639 INSERT("ShareJoin", sharejoin_pytype
);
642 /*----- That's all, folks -------------------------------------------------*/