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