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