chiark / gitweb /
Abolish serpent-l. Use PKCS#1 v1.5 RSA padding but without hash OID for secnet.
[chiark-tcl.git] / base / chiark-tcl.h
1 /*
2  */
3 /*
4  *  hbytes raw2h BINARY                          => hex
5  *  hbytes h2raw HEX                             => binary
6  *
7  *  hbytes length VALUE                          => count
8  *  hbytes prepend VAR [VALUE ...]         = set VAR [concat VALUE ... $VAR]
9  *  hbytes append VAR [VALUE ...]          = set VAR [concat $VAR VALUE ...]
10  *  hbytes concat VAR [VALUE ...]          = set VAR [concat VALUE ...]
11  *  hbytes unprepend VAR PREFIXLENGTH            => prefix (removed from VAR)
12  *  hbytes unappend VAR SUFFIXLENGTH             => suffix (removed from VAR)
13  *  hbytes chopto VAR NEWVARLENGTH               => suffix (removed from VAR)
14  *                                                  (too short? error)
15  *
16  *  hbytes range VALUE START SIZE                => substring (or error)
17  *  hbytes overwrite VAR START VALUE
18  *  hbytes trimleft VALUE                        removes any leading 0 octets
19  *  hbytes repeat VALUE COUNT                    => COUNT copies of VALUE
20  *
21  *  hbytes h2ulong HEX                           => ulong  (HEX len must be 4)
22  *  hbytes ulong2h UL                            => hex
23  *
24  *  ulong ul2bitfields VALUE [SIZE TYPE [TYPE-ARG...] ...]  => 0/1
25  *  ulong bitfields2ul BASE  [SIZE TYPE [TYPE-ARG...] ...]  => ULONG
26  *      goes from left (MSbit) to right (LSbit) where
27  *            SIZE is size in bits
28  *            TYPE [TYPE-ARGS...] is as below
29  *               zero
30  *               ignore
31  *               fixed ULONG-VALUE
32  *               uint VARNAME/VALUE         (VARNAME if ul2bitfields;
33  *               ulong VARNAME/VALUE         VALUE if bitfields2ul)
34  *
35  *  ulong ul2int ULONG    => INT            can fail if >INT_MAX
36  *  ulong int2ul INT      => ULONG          can fail if <0
37  *
38  *  hbytes shift l|r ULONG BITS             fails if BITS >32
39  *  hbytes mask A B                         => A & B
40  *
41  *  hbytes compare A B
42  *      =>  -2   A is lexically earlier than B and not a prefix of B  (A<B)
43  *          -1   A is prefix of B but not equal                       (A<B)
44  *           0   A == B
45  *          +1   A is B plus a nonempty suffix (ie, A has B as a prefix)
46  *          +2   A is lexically later than B and does not have B as a prefix
47  *
48  *  hbytes pkcs5 pa|ua VAR ALG                   => worked?  (always 1 for p)
49  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
50  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
51  *
52  *  hbytes hash ALG MESSAGE                      => hash
53  *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
54  *
55  * Refs: HMAC: RFC2104
56  */
57
58 #ifndef HBYTES_H
59 #define HBYTES_H
60
61 #include <assert.h>
62 #include <stdlib.h>
63 #include <errno.h>
64 #include <unistd.h>
65 #include <fcntl.h>
66 #include <sys/socket.h>
67 #include <sys/uio.h>
68 #include <sys/un.h>
69 #include <arpa/inet.h>
70
71 #include <tcl.h>
72
73 typedef unsigned char Byte;
74
75 /* from hbytes.c */
76
77 int Hbytes_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
78
79 /* Internal representation details: */
80 #define HBYTES_ISEMPTY(hb)    (!(hb)->begin_complex && !(hb)->end_0)
81 #define HBYTES_ISSENTINEL(hb) (!(hb)->begin_complex && (hb)->end_0)
82 #define HBYTES_ISSIMPLE(hb)   ((hb)->begin_complex && (hb)->end_0)
83 #define HBYTES_ISCOMPLEX(hb)  ((hb)->begin_complex && !(hb)->end_0)
84
85 typedef struct {
86   void *begin_complex, *end_0;
87 } HBytes_Value; /* overlays internalRep */
88
89 typedef struct {
90   Byte *dstart; /* always allocated dynamically */
91   int prespace, len, avail;
92   /*        
93    * | SPARE      | USED  | SPARE |
94    * |<-prespace->|<-len->|       |
95    * |            |<----avail---->|
96    *              ^start
97    */
98 } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */
99
100 /* Public interfaces: */
101
102 extern Tcl_ObjType hbytes_type;
103
104 int hbytes_len(const HBytes_Value *v);
105 Byte *hbytes_data(const HBytes_Value *v); /* caller may then modify data! */
106 int hbytes_issentinel(const HBytes_Value *v);
107
108 Byte *hbytes_prepend(HBytes_Value *upd, int el);
109 Byte *hbytes_append(HBytes_Value *upd, int el);
110   /* return value is where to put the data */
111
112 const Byte *hbytes_unprepend(HBytes_Value *upd, int rl);
113 const Byte *hbytes_unappend(HBytes_Value *upd, int rl);
114   /* return value points to the removed data, which remains valid
115    * until next op on the HBytes_Value.  If original value is
116    * shorter than rl or negative, returns 0 and does nothing. */
117
118 void hbytes_empty(HBytes_Value *returns);
119 void hbytes_sentinel(HBytes_Value *returns);
120 void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
121 Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
122 void hbytes_free(const HBytes_Value *frees);
123   /* _empty, _sentinel and _array do not free or read the old value;
124    * _free it first if needed.  _free leaves it garbage, so you
125    * have to call _empty to reuse it.  _arrayspace doesn't fill
126    * the array; you get a pointer and must fill it with data
127    * yourself. */
128
129 /* The value made by hbytes_sentinel should not be passed to
130  * anything except HBYTES_IS..., and hbytes_free. */
131
132 /* from sockaddr.c */
133
134 typedef struct {
135   Byte *begin, *end;
136 } SockAddr_Value;
137
138 extern Tcl_ObjType sockaddr_type;
139
140 void sockaddr_clear(SockAddr_Value*);
141 void sockaddr_create(SockAddr_Value*, const struct sockaddr *addr, int len);
142 int sockaddr_len(const SockAddr_Value*);
143 const struct sockaddr *sockaddr_addr(const SockAddr_Value*);
144 void sockaddr_free(const SockAddr_Value*);
145
146 /* from dgram.c */
147
148 extern Tcl_ObjType dgramsockid_type;
149 typedef struct DgramSocket *DgramSockID;
150
151 /* from hook.c */
152
153 int staticerr(Tcl_Interp *ip, const char *m);
154 int posixerr(Tcl_Interp *ip, int errnoval, const char *m);
155 void objfreeir(Tcl_Obj *o);
156 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
157
158 void obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
159 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
160                                 int l, const char *prefix);
161
162 void obj_updatestr_vstringls(Tcl_Obj *o, ...);
163   /* const char*, int, const char*, int, ..., (const char*)0 */
164 void obj_updatestr_string_len(Tcl_Obj *o, const char *str, int l);
165 void obj_updatestr_string(Tcl_Obj *o, const char *str);
166
167 /* from parse.c */
168
169 typedef struct {
170   HBytes_Value *hb;
171   Tcl_Obj *obj, *var;
172 } HBytes_Var;
173
174 void fini_hbv(Tcl_Interp *ip, int rc, HBytes_Var *agg);
175
176 /* from chop.c */
177   /* only do_... functions declared in tables.h */
178
179 /* from ulong.c */
180
181 Tcl_ObjType ulong_type;
182
183 /* from enum.c */
184
185 extern Tcl_ObjType enum_nearlytype;
186 extern Tcl_ObjType enum1_nearlytype;
187
188 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
189                                     const void *firstentry, size_t entrysize,
190                                     const char *what);
191 #define enum_lookup_cached(ip,o,table,what)                     \
192     (enum_lookup_cached_func((ip),(o),                          \
193                              &(table)[0],sizeof((table)[0]),    \
194                              (what)))
195   /* table should be a pointer to an array of structs of size
196    * entrysize, the first member of which should be a const char*.
197    * The table should finish with a null const char *.
198    * On error, 0 is returned and the ip->result will have been
199    * set to the error message.
200    */
201
202 int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
203                              const char *opts, const char *what);
204   /* -1 => error */
205
206 /* from crypto.c */
207
208 void memxor(Byte *dest, const Byte *src, int l);
209
210 typedef struct {
211   const char *name;
212   int pad, use_algname;
213 } PadMethod;
214
215 extern Tcl_ObjType blockcipherkey_type;
216
217 /* from algtables.c */
218
219 typedef struct {
220   const char *name;
221   int hashsize, blocksize, statesize;
222   void (*init)(void *state);
223   void (*update)(void *state, const void *data, int len);
224   void (*final)(void *state, void *digest);
225   void (*oneshot)(void *digest, const void *data, int len);
226 } HashAlgInfo;
227
228 extern const HashAlgInfo hashalginfos[];
229
230 typedef struct {
231   void (*make_schedule)(void *schedule, const void *key, int keylen);
232   void (*crypt)(const void *schedule, const void *in, void *out);
233      /* in and out may be the same, but if they aren't they may not overlap */
234      /* in and out for crypt will have been through block_byteswap */
235 } BlockCipherDirectionInfo;
236
237 typedef struct {
238   const char *name;
239   int blocksize, schedule_size, key_min, key_max;
240   void (*byteswap)(void *block);
241   BlockCipherDirectionInfo encrypt, decrypt;
242 } BlockCipherAlgInfo;
243
244 extern const BlockCipherAlgInfo blockcipheralginfos[];
245
246 /* from bcmode.c */
247
248 typedef struct {
249   const char *name;
250   int iv_blocks, buf_blocks;
251   const char *(*encrypt)(Byte *data, int blocks,
252                          const Byte *iv, Byte *buf,
253                          const BlockCipherAlgInfo *alg, int encr,
254                          int blocksize, const void *sch);
255   const char *(*decrypt)(Byte *data, int blocks,
256                          const Byte *iv, Byte *buf,
257                          const BlockCipherAlgInfo *alg, int encr,
258                          int blocksize, const void *sch);
259     /* in each case, *iv is provided, but may be modified */
260 } BlockCipherModeInfo;
261
262 extern const BlockCipherModeInfo blockciphermodeinfos[];
263
264 /* from misc.c */
265
266 int setnonblock(int fd, int isnonblock);
267
268 /* useful macros */
269
270 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
271 #define OBJ_SOCKADDR(o) ((SockAddr_Value*)&(o)->internalRep.twoPtrValue)
272
273 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
274 #define TFREE(f) (Tcl_Free((void*)(f)))
275
276 #endif /*HBYTES_H*/