Much wider support for Catacomb in all its glory.
[catacomb-perl] / catacomb-perl.h
CommitLineData
660b443c 1/* -*-c-*-
2 *
a1a90aaf 3 * $Id$
660b443c 4 *
5 * Main header file for Catacomb/Perl
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the Perl interface to Catacomb.
13 *
14 * Catacomb/Perl is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * Catacomb/Perl is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Catacomb/Perl; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
660b443c 29#ifndef CATACOMB_PERL_H
30#define CATACOMB_PERL_H
31
32#ifdef __cplusplus
33 extern "C" {
34#endif
35
36/*----- Header files ------------------------------------------------------*/
37
fcd15e0b 38#include <assert.h>
39
a1a90aaf 40#include <EXTERN.h>
41#include <perl.h>
42#include <XSUB.h>
43
660b443c 44#include <stdio.h>
45#include <stdarg.h>
46#include <stdlib.h>
47#include <string.h>
48
fcd15e0b 49#include <catacomb/blkc.h>
660b443c 50#include <catacomb/gcipher.h>
51#include <catacomb/ghash.h>
52#include <catacomb/gmac.h>
53
54#include <catacomb/grand.h>
55#include <catacomb/fibrand.h>
56#include <catacomb/lcrand.h>
57#include <catacomb/dsarand.h>
58#include <catacomb/rand.h>
59#include <catacomb/noise.h>
60
fcd15e0b 61#include <catacomb/share.h>
62#include <catacomb/gfshare.h>
63
a1a90aaf 64#include <catacomb/key.h>
660b443c 65#include <catacomb/passphrase.h>
66
67#include <catacomb/mp.h>
a1a90aaf 68#include <catacomb/gf.h>
69#include <catacomb/ec.h>
fcd15e0b 70#include <catacomb/rsa.h>
71#include <catacomb/dh.h>
72#include <catacomb/dsa.h>
73#include <catacomb/gdsa.h>
74#include <catacomb/gkcdsa.h>
a1a90aaf 75#include <catacomb/field.h>
f9952aec 76#include <catacomb/group.h>
660b443c 77#include <catacomb/mpint.h>
78#include <catacomb/mpmul.h>
79#include <catacomb/mprand.h>
80#include <catacomb/mpcrt.h>
81#include <catacomb/mpmont.h>
82#include <catacomb/mpbarrett.h>
a1a90aaf 83#include <catacomb/mpreduce.h>
84#include <catacomb/gfreduce.h>
660b443c 85
86#include <catacomb/pfilt.h>
87#include <catacomb/rabin.h>
88#include <catacomb/pgen.h>
89#include <catacomb/limlee.h>
90#include <catacomb/strongprime.h>
91
92/*----- Misc support ------------------------------------------------------*/
93
94struct consttab { const char *name; UV val; };
95
fcd15e0b 96typedef struct cursor {
97 unsigned f;
98 union {
99 HV *hv;
100 struct { AV *av; unsigned i; } a;
101 } u;
102} cursor;
103#define CF_ARRAY 0u
104#define CF_HASH 1u
105#define CF_MUST 1u
106
660b443c 107extern U32 findconst(const struct consttab *cc,
108 const char *pkg, const char *name);
a1a90aaf 109extern void ptrtosv(SV **sv, void *p, const char *type);
110extern void *ptrfromsv(SV *sv, const char *type, const char *what, ...);
fcd15e0b 111extern void *ptrfromsvdflt(SV *sv, const char *type, void *dflt,
112 const char *what);
113extern void c_init(cursor *c, SV *sv);
114extern void c_skip(cursor *c);
115extern SV *c_get(cursor *c, const char *tag, unsigned f);
116extern ge *groupelt(SV *sv, const char *what);
117extern mp *fieldelt(SV *sv, const char *what);
118extern ec *ecpt(SV *sv, const char *what);
119
660b443c 120#define SET(sv, ob, ty) sv_setref_pv((sv), (char *)(ty), (void *)(ob))
121#define MAKE(ob, ty) SET(NEWSV(0, 0), ob, ty)
122#define RET(ob, ty) SET(sv_newmortal(), ob, ty)
fcd15e0b 123#define C_MP(c, tag) mp_fromsv(c_get(c, tag, CF_MUST), tag, 0, 0)
124#define C_PTR(c, tag, type) ptrfromsv(c_get(c, tag, CF_MUST), type, tag)
125#define C_PTRDFLT(c, tag, type, def) \
126 ptrfromsvdflt(c_get(c, tag, 0), type, def, tag)
127#define C_GE(c, tag) groupelt(c_get(c, tag, CF_MUST), tag)
128#define C_FE(c, tag) fieldelt(c_get(c, tag, CF_MUST), tag)
129#define C_EC(c, tag) ecpt(c_get(c, tag, CF_MUST), tag)
130extern void hvput(HV *hv, const char *k, SV *val);
660b443c 131
132/*----- Crypto algorithms -------------------------------------------------*/
133
fcd15e0b 134typedef struct PRPClass {
135 char *name;
136 const octet *ksz;
137 size_t ctxsz;
138 size_t blksz;
139 void (*init)(void *ctx, const void *k, size_t sz);
140 void (*eblk)(const void *ctx, const void *in, void *out);
141 void (*dblk)(const void *ctx, const void *in, void *out);
142} PRPClass;
143
144typedef struct PRP {
145 PRPClass *c;
146} PRP;
147
148extern PRPClass *const prptab[];
149
660b443c 150struct randtab { const char *name; grand *(*rand)(const void *, size_t); };
151
152typedef const octet keysize;
153typedef gmac gMAC;
154typedef gcmac gcMAC;
155
156typedef grand Rand_True, Rand_DSA;
157
fcd15e0b 158typedef rsa_pubctx RSA_Public;
159typedef rsa_privctx RSA_Private;
160
161typedef gfshare Share_GF;
162typedef share Share_Prime;
163
660b443c 164extern const struct randtab mgftab[], ctrtab[], ofbtab[];
165
fcd15e0b 166extern void gdsa_privfromsv(gdsa *g, SV *sv);
167extern void gdsa_pubfromsv(gdsa *g, SV *sv);
660b443c 168extern SV *findrand(const struct randtab *rt, const char *cls,
169 const char *name, SV *k);
170extern void listrand(const struct randtab *rt);
171
a1a90aaf 172/*------ Key mangling -----------------------------------------------------*/
173
fcd15e0b 174typedef struct Key_File {
175 unsigned ref;
176 key_file kf;
177} Key_File;
178
a1a90aaf 179typedef struct Key {
fcd15e0b 180 Key_File *kf;
a1a90aaf 181 key *k;
182} Key;
fcd15e0b 183typedef struct Key_AttrIter {
184 Key_File *kf;
185 key_attriter i;
186} Key_AttrIter;
a1a90aaf 187
188typedef int KeyErr;
fcd15e0b 189typedef key_data Key_DataImpl;
190typedef struct Key_FileIter {
191 Key_File *kf;
192 key_iter i;
193} Key_FileIter;
194typedef sym_iter Key_StructIter;
195typedef key_filter Key_Filter;
196
197extern void keyreport(const char *file, int line, const char *err, void *p);
a1a90aaf 198extern SV *keyerr(int rc);
199
660b443c 200/*------ Multiprecision maths ---------------------------------------------*/
201
a1a90aaf 202typedef mp gf;
660b443c 203typedef mpmont MP_Mont;
204typedef mpbarrett MP_Barrett;
205typedef mpcrt MP_CRT;
206
a1a90aaf 207typedef mpreduce MP_Reduce;
208typedef gfreduce GF_Reduce;
209
a1a90aaf 210typedef ec_curve EC_Curve;
211typedef field Field;
fcd15e0b 212typedef mp fe;
213
214typedef group Group;
a1a90aaf 215
660b443c 216#define XSINTERFACE_FUNC_SETMP(cv, f) \
217 CvXSUBANY(cv).any_dptr = (void (*) _((void *)))(mp_##f)
a1a90aaf 218#define XSINTERFACE_FUNC_SETGF(cv, f) \
219 CvXSUBANY(cv).any_dptr = (void (*) _((void *)))(gf_##f)
660b443c 220
221#define SET_MP(sv, x) SET(sv, x, "Catacomb::MP")
fcd15e0b 222#define MAKE_MP(x) MAKE(x, "Catacomb::MP")
660b443c 223#define RET_MP(x) RET(x, "Catacomb::MP")
224
a1a90aaf 225#define SET_GF(sv, x) SET(sv, x, "Catacomb::GF")
fcd15e0b 226#define MAKE_GF(x) MAKE(x, "Catacomb::GF")
a1a90aaf 227#define RET_GF(x) RET(x, "Catacomb::GF")
228
660b443c 229extern mp *mp_fromiv(mp *d, IV iv);
230extern IV mp_toiv(mp *x);
231extern mp *mp_readsv(mp *m, SV *sv, STRLEN *off, int radix);
232extern int mp_writesv(mp *m, SV *sv, int radix);
fcd15e0b 233extern mp *mp_fromsv(SV *sv, const char *what, int radix, int keep, ...);
234extern int group_writesv(group *g, ge *x, SV *sv);
660b443c 235
236/*----- Prime generation --------------------------------------------------*/
237
238typedef struct { pfilt pf; int rc; } MP_Prime_Filter;
239typedef rabin MP_Prime_Rabin;
240typedef SV MP_Prime_Gen_Proc, MP_Prime_Gen_NullProc;
241typedef struct { pgen_proc *p; void *ctx; } pgmagic, MP_Prime_Gen_MagicProc;
242typedef struct { pgmagic mg; pgen_filterctx f; } MP_Prime_Gen_FilterStepper;
243typedef struct { pgmagic mg; pgen_jumpctx j; pfilt pf; }
244 MP_Prime_Gen_JumpStepper;
245typedef struct { pgmagic mg; rabin r; } MP_Prime_Gen_RabinTester;
246typedef struct pgen_event MP_Prime_Gen_Event;
247
248extern void pgproc_get(SV *sv, pgen_proc **p, void **ctx);
249
f9952aec 250/*----- Other gear --------------------------------------------------------*/
251
fcd15e0b 252extern void names(const char *name);
253
254extern SV *info_field(field *f);
255extern SV *info_curve(ec_curve *c);
256extern SV *info_group(group *g);
257
f9952aec 258extern field *copy_field(field *f);
259extern ec_curve *copy_curve(ec_curve *c);
260extern group *copy_group(group *g);
261
660b443c 262/*----- That's all, folks -------------------------------------------------*/
263
264#ifdef __cplusplus
265 }
266#endif
267
268#endif