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