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