chiark / gitweb /
algorithms.c, etc.: Support the new AEAD abstraction.
[mLib-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     C(ED25519_MAXPERSOSZ),
52   C(ED448_KEYSZ), C(ED448_PUBSZ), C(ED448_SIGSZ), C(ED448_MAXPERSOSZ),
53   C(AEADF_PCHSZ), C(AEADF_PCMSZ), C(AEADF_PCTSZ),
54   C(AEADF_AADNDEP), C(AEADF_AADFIRST), C(AEADF_NOAAD),
55 #define ENTRY(tag, val, str) C(KERR_##tag),
56   KEY_ERRORS(ENTRY)
57 #undef ENTRY
58 #undef C
59   { 0 }
60 };
61
62 PyObject *mexp_common(PyObject *me, PyObject *arg,
63                       size_t efsz,
64                       PyObject *(*id)(PyObject *),
65                       int (*fill)(void *, PyObject *,
66                                   PyObject *, PyObject *),
67                       PyObject *(*exp)(PyObject *, void *, int),
68                       void (*drop)(void *))
69 {
70   int i = 0, j, n, flat;
71   PyObject *qq, *x, *y, *z = 0;
72   char *v = 0, *vv;
73
74   if (PyTuple_Size(arg) == 1)
75     arg = PyTuple_GetItem(arg, 0);
76   Py_INCREF(arg);
77   if (!PySequence_Check(arg)) TYERR("not a sequence");
78   n = PySequence_Size(arg); if (!n) { z = id(me); goto end; }
79   x = PySequence_GetItem(arg, 0);
80   if (PySequence_Check(x))
81     flat = 0;
82   else {
83     if (n % 2) VALERR("must have even number of arguments");
84     n /= 2;
85     flat = 1;
86   }
87   Py_DECREF(x);
88
89   v = xmalloc(n * efsz);
90   for (i = j = 0, vv = v; i < n; i++, vv += efsz) {
91     if (flat) {
92       x = PySequence_GetItem(arg, j++);
93       y = PySequence_GetItem(arg, j++);
94     } else {
95       qq = PySequence_GetItem(arg, j++);
96       if (!qq) goto end;
97       if (!PySequence_Check(qq) || PySequence_Size(qq) != 2) {
98         Py_DECREF(qq);
99         TYERR("want a sequence of pairs");
100       }
101       x = PySequence_GetItem(qq, 0);
102       y = PySequence_GetItem(qq, 1);
103       Py_DECREF(qq);
104     }
105     if (!x || !y) goto end;
106     if (fill(vv, me, x, y)) {
107       Py_DECREF(x);
108       Py_DECREF(y);
109       if (!PyErr_Occurred())
110         PyErr_SetString(PyExc_TypeError, "type mismatch");
111       goto end;
112     }
113     Py_DECREF(x);
114     Py_DECREF(y);
115   }
116   z = exp(me, v, n);
117
118 end:
119   if (v) {
120     for (j = 0, vv = v; j < i; j++, vv += efsz)
121       drop(vv);
122     xfree(v);
123   }
124   Py_DECREF(arg);
125   return (z);
126 }
127
128 static PyObject *smallprimes(void)
129 {
130   PyObject *v = PyList_New(NPRIME);
131   int i;
132
133   for (i = 0; i < NPRIME; i++)
134     PyList_SetItem(v, i, PyInt_FromLong(primetab[i]));
135   return (v);
136 }
137
138 static PyObject *meth__ego(PyObject *me, PyObject *arg)
139 {
140   char *argv0;
141   if (!PyArg_ParseTuple(arg, "s:_ego", &argv0))
142     return (0);
143   if (strcmp(QUIS, "<UNNAMED>") == 0)
144     ego(argv0);
145   RETURN_NONE;
146 }
147
148 static PyMethodDef methods[] = {
149 #define METHNAME(func) meth_##func
150   METH  (_ego,                  "_ego(ARGV0)")
151 #undef METHNAME
152   { 0 }
153 };
154
155 static void init_random(void)
156 {
157 #if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
158   char *seed;
159   uint32 r;
160
161   if (!Py_HashRandomizationFlag) return;
162   seed = getenv("PYTHONHASHSEED");
163   if (!seed || strcmp(seed, "random") == 0) r = GR_WORD(&rand_global);
164   else r = strtoul(seed, 0, 0);
165   if (!r) r = 0xe011f220; /* zero doesn't work well */
166   unihash_setkey(&unihash_global, r);
167 #endif
168 }
169
170 void init_base(void)
171 {
172   PyObject *mod;
173   addmethods(methods);
174   INIT_MODULES;
175   init_random();
176   mod = Py_InitModule("catacomb._base", donemethods());
177   INSERT_MODULES;
178   INSERT("smallprimes", smallprimes());
179   setconstants(mod, consts);
180 }
181
182 /*----- That's all, folks -------------------------------------------------*/