chiark / gitweb /
Merge branch '1.1.x'
[catacomb-python] / catacomb.c
1 /* -*-c-*-
2  *
3  * Where the fun begins
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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 /*----- Main code ---------------------------------------------------------*/
32
33 static const struct nameval consts[] = {
34 #define C(x) { #x, x }
35   C(FTY_PRIME), C(FTY_BINARY),
36   C(PGEN_PASS), C(PGEN_FAIL), C(PGEN_BEGIN), C(PGEN_TRY), C(PGEN_DONE),
37   C(PGEN_ABORT),
38   C(MPW_MAX),
39   C(PMODE_READ), C(PMODE_VERIFY),
40   C(KOPEN_READ), C(KOPEN_WRITE), C(KOPEN_NOFILE),
41   C(KEXP_FOREVER), C(KEXP_EXPIRE),
42   C(KF_ENCMASK), C(KENC_BINARY), C(KENC_MP), C(KENC_STRUCT),
43     C(KENC_ENCRYPT), C(KENC_STRING), C(KENC_EC),
44   C(KF_CATMASK), C(KCAT_SYMM), C(KCAT_PRIV), C(KCAT_PUB), C(KCAT_SHARE),
45   C(KF_NONSECRET),
46   C(KF_BURN), C(KF_OPT),
47   C(EC_XONLY), C(EC_YBIT), C(EC_LSB), C(EC_CMPR), C(EC_EXPLY), C(EC_SORT),
48   C(X25519_KEYSZ), C(X25519_PUBSZ), C(X25519_OUTSZ),
49   C(X448_KEYSZ), C(X448_PUBSZ), C(X448_OUTSZ),
50   C(ED25519_KEYSZ), C(ED25519_PUBSZ), C(ED25519_SIGSZ),
51 #define ENTRY(tag, val, str) C(KERR_##tag),
52   KEY_ERRORS(ENTRY)
53 #undef ENTRY
54 #undef C
55   { 0 }
56 };
57
58 PyObject *mexp_common(PyObject *me, PyObject *arg,
59                       size_t efsz,
60                       PyObject *(*id)(PyObject *),
61                       int (*fill)(void *, PyObject *,
62                                   PyObject *, PyObject *),
63                       PyObject *(*exp)(PyObject *, void *, int),
64                       void (*drop)(void *))
65 {
66   int i = 0, j, n, flat;
67   PyObject *qq, *x, *y, *z = 0;
68   char *v = 0, *vv;
69
70   if (PyTuple_Size(arg) == 1)
71     arg = PyTuple_GetItem(arg, 0);
72   Py_INCREF(arg);
73   if (!PySequence_Check(arg)) TYERR("not a sequence");
74   n = PySequence_Size(arg); if (!n) { z = id(me); goto end; }
75   x = PySequence_GetItem(arg, 0);
76   if (PySequence_Check(x))
77     flat = 0;
78   else {
79     if (n % 2) VALERR("must have even number of arguments");
80     n /= 2;
81     flat = 1;
82   }
83   Py_DECREF(x);
84
85   v = xmalloc(n * efsz);
86   for (i = j = 0, vv = v; i < n; i++, vv += efsz) {
87     if (flat) {
88       x = PySequence_GetItem(arg, j++);
89       y = PySequence_GetItem(arg, j++);
90     } else {
91       qq = PySequence_GetItem(arg, j++);
92       if (!qq) goto end;
93       if (!PySequence_Check(qq) || PySequence_Size(qq) != 2) {
94         Py_DECREF(qq);
95         TYERR("want a sequence of pairs");
96       }
97       x = PySequence_GetItem(qq, 0);
98       y = PySequence_GetItem(qq, 1);
99       Py_DECREF(qq);
100     }
101     if (!x || !y) goto end;
102     if (fill(vv, me, x, y)) {
103       Py_DECREF(x);
104       Py_DECREF(y);
105       if (!PyErr_Occurred())
106         PyErr_SetString(PyExc_TypeError, "type mismatch");
107       goto end;
108     }
109     Py_DECREF(x);
110     Py_DECREF(y);
111   }
112   z = exp(me, v, n);
113
114 end:
115   if (v) {
116     for (j = 0, vv = v; j < i; j++, vv += efsz)
117       drop(vv);
118     xfree(v);
119   }
120   Py_DECREF(arg);
121   return (z);
122 }
123
124 static PyObject *smallprimes(void)
125 {
126   PyObject *v = PyList_New(NPRIME);
127   int i;
128
129   for (i = 0; i < NPRIME; i++)
130     PyList_SetItem(v, i, PyInt_FromLong(primetab[i]));
131   return (v);
132 }
133
134 static PyObject *meth__ego(PyObject *me, PyObject *arg)
135 {
136   char *argv0;
137   if (!PyArg_ParseTuple(arg, "s:_ego", &argv0))
138     return (0);
139   if (strcmp(QUIS, "<UNNAMED>") == 0)
140     ego(argv0);
141   RETURN_NONE;
142 }
143
144 static PyMethodDef methods[] = {
145 #define METHNAME(func) meth_##func
146   METH  (_ego,                  "_ego(ARGV0)")
147 #undef METHNAME
148   { 0 }
149 };
150
151 static void init_random(void)
152 {
153 #if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
154   char *seed;
155   uint32 r;
156
157   if (!Py_HashRandomizationFlag) return;
158   seed = getenv("PYTHONHASHSEED");
159   if (!seed || strcmp(seed, "random") == 0) r = GR_WORD(&rand_global);
160   else r = strtoul(seed, 0, 0);
161   if (!r) r = 0xe011f220; /* zero doesn't work well */
162   unihash_setkey(&unihash_global, r);
163 #endif
164 }
165
166 void init_base(void)
167 {
168   PyObject *mod;
169   addmethods(methods);
170   INIT_MODULES;
171   init_random();
172   mod = Py_InitModule("catacomb._base", donemethods());
173   INSERT_MODULES;
174   INSERT("smallprimes", smallprimes());
175   setconstants(mod, consts);
176 }
177
178 /*----- That's all, folks -------------------------------------------------*/