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