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