chiark / gitweb /
37697bb7dfa7e3db01cf061d99c2afbcc3148d7c
[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 pkcs5 pa|ua VAR ALG                   => worked?  (always 1 for p)
17  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
18  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
19  *
20  *  hbytes hash ALG MESSAGE                      => hash
21  *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
22  *
23  * Refs: HMAC: RFC2104
24  */
25
26 #ifndef HBYTES_H
27 #define HBYTES_H
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/socket.h>
35 #include <sys/uio.h>
36 #include <sys/un.h>
37 #include <arpa/inet.h>
38
39 #include <tcl.h>
40
41 typedef unsigned char Byte;
42
43 /* from hbytes.c */
44
45 int Hbytes_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
46
47 /* Internal representation details: */
48 #define HBYTES_ISEMPTY(hb)    (!(hb)->begin_complex && !(hb)->end_0)
49 #define HBYTES_ISSENTINEL(hb) (!(hb)->begin_complex && (hb)->end_0)
50 #define HBYTES_ISSIMPLE(hb)   ((hb)->begin_complex && (hb)->end_0)
51 #define HBYTES_ISCOMPLEX(hb)  ((hb)->begin_complex && !(hb)->end_0)
52
53 typedef struct {
54   void *begin_complex, *end_0;
55 } HBytes_Value; /* overlays internalRep */
56
57 typedef struct {
58   Byte *dstart; /* always allocated dynamically */
59   int prespace, len, avail;
60   /*        
61    * | SPARE      | USED  | SPARE |
62    * |<-prespace->|<-len->|       |
63    * |            |<----avail---->|
64    *              ^start
65    */
66 } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */
67
68 /* Public interfaces: */
69
70 extern Tcl_ObjType hbytes_type;
71
72 int hbytes_len(const HBytes_Value *v);
73 Byte *hbytes_data(const HBytes_Value *v); /* caller may then modify data! */
74 int hbytes_issentinel(const HBytes_Value *v);
75
76 Byte *hbytes_prepend(HBytes_Value *upd, int el);
77 Byte *hbytes_append(HBytes_Value *upd, int el);
78   /* return value is where to put the data */
79
80 const Byte *hbytes_unprepend(HBytes_Value *upd, int rl);
81 const Byte *hbytes_unappend(HBytes_Value *upd, int rl);
82   /* return value points to the removed data, which remains valid
83    * until next op on the HBytes_Value.  If original value is
84    * shorter than rl or negative, returns 0 and does nothing. */
85
86 void hbytes_empty(HBytes_Value *returns);
87 void hbytes_sentinel(HBytes_Value *returns);
88 void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
89 Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
90 void hbytes_free(const HBytes_Value *frees);
91   /* _empty, _sentinel and _array do not free or read the old value;
92    * _free it first if needed.  _free leaves it garbage, so you
93    * have to call _empty to reuse it.  _arrayspace doesn't fill
94    * the array; you get a pointer and must fill it with data
95    * yourself. */
96
97 /* The value made by hbytes_sentinel should not be passed to
98  * anything except HBYTES_IS..., and hbytes_free. */
99
100 /* from sockaddr.c */
101
102 typedef struct {
103   Byte *begin, *end;
104 } SockAddr_Value;
105
106 extern Tcl_ObjType sockaddr_type;
107
108 void sockaddr_clear(SockAddr_Value*);
109 void sockaddr_create(SockAddr_Value*, const struct sockaddr *addr, int len);
110 int sockaddr_len(const SockAddr_Value*);
111 const struct sockaddr *sockaddr_addr(const SockAddr_Value*);
112 void sockaddr_free(const SockAddr_Value*);
113
114 /* from dgram.c */
115
116 extern Tcl_ObjType sockid_type;
117 typedef struct DgramSocket *DgramSockID;
118
119 /* from hook.c */
120
121 int staticerr(Tcl_Interp *ip, const char *m);
122 int posixerr(Tcl_Interp *ip, int errnoval, const char *m);
123 void objfreeir(Tcl_Obj *o);
124 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
125
126 void obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
127 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
128                                 int l, const char *prefix);
129
130 /* from parse.c */
131
132 typedef struct {
133   HBytes_Value *hb;
134   Tcl_Obj *obj, *var;
135 } HBytes_Var;
136
137 void fini_hbv(Tcl_Interp *ip, int rc, HBytes_Var *agg);
138
139 /* from chop.c */
140
141 /* from enum.c */
142
143 extern Tcl_ObjType enum_nearlytype;
144 extern Tcl_ObjType enum1_nearlytype;
145
146 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
147                                     const void *firstentry, size_t entrysize,
148                                     const char *what);
149 #define enum_lookup_cached(ip,o,table,what)                     \
150     (enum_lookup_cached_func((ip),(o),                          \
151                              &(table)[0],sizeof((table)[0]),    \
152                              (what)))
153   /* table should be a pointer to an array of structs of size
154    * entrysize, the first member of which should be a const char*.
155    * The table should finish with a null const char *.
156    * On error, 0 is returned and the ip->result will have been
157    * set to the error message.
158    */
159
160 int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
161                              const char *opts, const char *what);
162   /* -1 => error */
163
164 /* from crypto.c */
165
166 void memxor(Byte *dest, const Byte *src, int l);
167
168 typedef struct {
169   const char *name;
170   int pad, use_algname;
171 } PadMethod;
172
173 extern Tcl_ObjType blockcipherkey_type;
174
175 /* from algtables.c */
176
177 typedef struct {
178   const char *name;
179   int hashsize, blocksize, statesize;
180   void (*init)(void *state);
181   void (*update)(void *state, const Byte *data, int len);
182   void (*final)(void *state, Byte *digest);
183   void (*oneshot)(Byte *digest, const Byte *data, int len);
184 } HashAlgInfo;
185
186 extern const HashAlgInfo hashalginfos[];
187
188 typedef struct {
189   void (*make_schedule)(void *schedule, const Byte *key, int keylen);
190   void (*crypt)(const void *schedule, const void *in, void *out);
191      /* in and out may be the same, but if they aren't they may not overlap */
192      /* in and out for crypt will have been through block_byteswap */
193 } BlockCipherDirectionInfo;
194
195 typedef struct {
196   const char *name;
197   int blocksize, schedule_size, key_min, key_max;
198   void (*byteswap)(Byte *block);
199   BlockCipherDirectionInfo encrypt, decrypt;
200 } BlockCipherAlgInfo;
201
202 extern const BlockCipherAlgInfo blockcipheralginfos[];
203
204 /* from bcmode.c */
205
206 typedef struct {
207   const char *name;
208   int iv_blocks, buf_blocks;
209   const char *(*encrypt)(Byte *data, int blocks,
210                          const Byte *iv, Byte *buf,
211                          const BlockCipherAlgInfo *alg, int encr,
212                          int blocksize, const void *sch);
213   const char *(*decrypt)(Byte *data, int blocks,
214                          const Byte *iv, Byte *buf,
215                          const BlockCipherAlgInfo *alg, int encr,
216                          int blocksize, const void *sch);
217     /* in each case, *iv is provided, but may be modified */
218 } BlockCipherModeInfo;
219
220 extern const BlockCipherModeInfo blockciphermodeinfos[];
221
222 /* from misc.c */
223
224 int setnonblock(int fd, int isnonblock);
225
226 /* useful macros */
227
228 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
229 #define OBJ_SOCKADDR(o) ((SockAddr_Value*)&(o)->internalRep.twoPtrValue)
230
231 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
232 #define TFREE(f) (Tcl_Free((void*)(f)))
233
234 #endif /*HBYTES_H*/