4 * hbytes raw2h BINARY => hex
5 * hbytes h2raw HEX => binary
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)
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 * hbytes zeroes COUNT => COUNT zero bytes
21 * hbytes random COUNT => COUNT random bytes
22 * hbytes xor VAR VALUE $VAR (+)= VALUE
24 * hbytes ushort2h LONG => LONG must be <2^16, returns as hex
25 * hbytes h2ushort HEX => |HEX| must be 2 bytes, returns as ulong
28 * => -2 A is lexically earlier than B and not a prefix of B (A<B)
29 * -1 A is prefix of B but not equal (A<B)
31 * +1 A is B plus a nonempty suffix (ie, A has B as a prefix)
32 * +2 A is lexically later than B and does not have B as a prefix
34 * hbytes pad pa|ua VAR ALG METH [METHARGS] => worked? (always 1 for p)
35 * hbytes pad pn|un VAR BS METH [METHARGS] => worked? (always 1 for p)
36 * hbytes pad pa|pn VAR ALG|BS pkcs5 => 1
37 * hbytes pad ua|un VAR ALG|BS pkcs5 => worked?
38 * hbytes pad pa|pn VAR ALG|BS rfc2406 NXTHDR => 1
39 * hbytes pad ua|un VAR ALG|BS rfc2406 NXTHDRVAR => worked?
41 * hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
42 * hbytes blockcipher mac MSG ALG KEY MODE IV => final block
43 * hbytes blockcipher prop PROPERTY ALG => property value
45 * hbytes hash ALG MESSAGE => hash
46 * hbytes hmac ALG MESSAGE KEY [MACLENGTH] => mac
47 * hbytes hash-prop PROPERTY ALG => property value
49 * ulong ul2int ULONG => INT can fail if >INT_MAX
50 * ulong int2ul INT => ULONG can fail if <0
51 * ulong mask A B => A & B
52 * ulong add A B => A + B (mod 2^32)
53 * ulong subtract A B => A - B (mod 2^32)
54 * ulong compare A B => 0 -1 (A<B) +1 (A>B)
55 * ulong shift l|r ULONG BITS fails if BITS >32
57 * ulong ul2bitfields VALUE [SIZE TYPE [TYPE-ARG...] ...] => 0/1
58 * ulong bitfields2ul BASE [SIZE TYPE [TYPE-ARG...] ...] => ULONG
59 * goes from left (MSbit) to right (LSbit) where
60 * SIZE is size in bits
61 * TYPE [TYPE-ARGS...] is as below
65 * uint VARNAME/VALUE (VARNAME if ul2bitfields;
66 * ulong VARNAME/VALUE VALUE if bitfields2ul)
70 * A maskmap is a slightly efficient mapping from addresses to
71 * arbitrary data values. An address is a number of octets expressed
72 * as an hbytes. All the addresses covered by the same maskmap
73 * should have the same length (even though constructing the maskmap
74 * does not involve specifying that length).
76 * hbytes mask-map lookup MAP-VAR ADDRESS [DEFAULT] => DATA
77 * Error on missing default or if any prefix longer than ADDRESS.
79 * hbytes mask-map amend MAP-VAR PREFIX PREFIX-LENGTH DATA
80 * Sets all of the addresses in PREFIX/PREFIX-LENGTH to the
84 * A maskmap MAP is [list [list PREFIX PREFIX-LENGTH DATA]].
85 * The list is sorted by ascending PREFIX and entries do not overlap.
86 * Each PREFIX in the map is truncated to the shortest number of
87 * pairs of hex digits which can represent it.
91 * HBYTES BLOCKCIPHER CRYPTFAIL CRYPT block cipher mode failed somehow (!)
92 * HBYTES BLOCKCIPHER CRYPTFAIL MAC HMAC failed somehow (!)
93 * HBYTES BLOCKCIPHER LENGTH block cipher input has unsuitable length
94 * HBYTES BLOCKCIPHER PARAMS key or iv not suitable
95 * HBYTES HMAC PARAMS key, input or output size not suitable
96 * HBYTES LENGTH OVERRUN block too long
97 * HBYTES LENGTH RANGE input length or offset is -ve or silly
98 * HBYTES LENGTH UNDERRUN block too short (or offset too big)
99 * HBYTES LENGTH MISMATCH when blocks must be exactly same length
100 * HBYTES SYNTAX supposed hex block had wrong syntax
101 * HBYTES VALUE OVERFLOW value to be conv'd to hex too big/long
102 * HBYTES MASKMAP NOMATCH no addr/mask matches address for lookup
103 * HBYTES MASKMAP UNDERRUN addr value for lookup is too short
104 * HBYTES MASKMAP SYNTAX LLENGTH value for mask-map entry not llength==3
105 * HBYTES MASKMAP SYNTAX UNDERRUN value for mask-map entry hex too short
106 * HBYTES MASKMAP SYNTAX OVERRUN actual mask-map entry hex too short
107 * HBYTES MASKMAP SYNTAX ORDER actual mask-map is disordered
108 * HBYTES MASKMAP SYNTAX EXCLBITS mask-map has 1-bits outside prefix len
109 * SOCKADDR AFUNIX LENGTH path for AF_UNIX socket too long
110 * SOCKADDR SYNTAX IPV4 bad IPv4 socket address &/or port
111 * SOCKADDR SYNTAX OTHER bad socket addr, couldn't tell what kind
112 * ULONG BITCOUNT NEGATIVE -ve bitcount specified where not allowed
113 * ULONG BITCOUNT OVERRUN attempt to use more than 32 bits
114 * ULONG BITCOUNT UNDERRUN bitfields add up to less than 32
115 * ULONG VALUE NEGATIVE attempt convert -ve integers to ulong
116 * ULONG VALUE OVERFLOW converted value does not fit in result
117 * TUNTAP IFNAME LENGTH tun/tap interface name too long
118 * TUNTAP MTU OVERRUN tun/tap mtu limited to 2^16 bytes
120 * Refs: HMAC: RFC2104 */
130 #include <sys/socket.h>
133 #include <arpa/inet.h>
135 #include <tcl8.3/tcl.h>
139 typedef unsigned char Byte;
143 int Hbytes_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
145 /* Internal representation details: */
146 #define HBYTES_ISEMPTY(hb) (!(hb)->begin_complex && !(hb)->end_0)
147 #define HBYTES_ISSENTINEL(hb) (!(hb)->begin_complex && (hb)->end_0)
148 #define HBYTES_ISSIMPLE(hb) ((hb)->begin_complex && (hb)->end_0)
149 #define HBYTES_ISCOMPLEX(hb) ((hb)->begin_complex && !(hb)->end_0)
152 void *begin_complex, *end_0;
153 } HBytes_Value; /* overlays internalRep */
156 Byte *dstart; /* always allocated dynamically */
157 int prespace, len, avail;
159 * | SPARE | USED | SPARE |
160 * |<-prespace->|<-len->| |
161 * | |<----avail---->|
164 } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */
166 /* Public interfaces: */
168 extern Tcl_ObjType hbytes_type;
170 int hbytes_len(const HBytes_Value *v);
171 Byte *hbytes_data(const HBytes_Value *v); /* caller may then modify data! */
172 int hbytes_issentinel(const HBytes_Value *v);
174 Byte *hbytes_prepend(HBytes_Value *upd, int el);
175 Byte *hbytes_append(HBytes_Value *upd, int el);
176 /* return value is where to put the data */
178 const Byte *hbytes_unprepend(HBytes_Value *upd, int rl);
179 const Byte *hbytes_unappend(HBytes_Value *upd, int rl);
180 /* return value points to the removed data, which remains valid
181 * until next op on the HBytes_Value. If original value is
182 * shorter than rl or negative, returns 0 and does nothing. */
184 void hbytes_empty(HBytes_Value *returns);
185 void hbytes_sentinel(HBytes_Value *returns);
186 void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
187 Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
188 void hbytes_free(const HBytes_Value *frees);
189 /* _empty, _sentinel and _array do not free or read the old value;
190 * _free it first if needed. _free leaves it garbage, so you
191 * have to call _empty to reuse it. _arrayspace doesn't fill
192 * the array; you get a pointer and must fill it with data
195 /* The value made by hbytes_sentinel should not be passed to
196 * anything except HBYTES_IS..., and hbytes_free. */
198 /* from sockaddr.c */
204 extern Tcl_ObjType sockaddr_type;
206 void sockaddr_clear(SockAddr_Value*);
207 void sockaddr_create(SockAddr_Value*, const struct sockaddr *addr, int len);
208 int sockaddr_len(const SockAddr_Value*);
209 const struct sockaddr *sockaddr_addr(const SockAddr_Value*);
210 void sockaddr_free(const SockAddr_Value*);
212 /* from scriptinv.c */
214 typedef struct { /* semi-opaque - read only, and then only where commented */
215 Tcl_Interp *ip; /* valid, non-0 and useable if set */
216 Tcl_Obj *obj; /* non-0 iff set (but only test for 0/non-0) */
221 void scriptinv_init(ScriptToInvoke *si);
222 int scriptinv_set(ScriptToInvoke *si, Tcl_Interp *ip,
223 Tcl_Obj *newscript, Tcl_Obj *xargs);
224 void scriptinv_cancel(ScriptToInvoke *si); /* then don't invoke */
225 /* no separate free function - just cancel */
227 void scriptinv_invoke(ScriptToInvoke *si, int argc, Tcl_Obj *const *argv);
232 const char *valprefix, *assockey;
233 void (*destroyitem)(Tcl_Interp *ip, void *val);
236 /* The stored struct must start with a single int, conventionally
237 * named `ix'. When the struct is returned for the first time ix must
238 * be -1; on subsequent occasions it must be >=0. ix will be -1 iff
239 * the struct is registered by the iddatatable machinery. */
241 extern Tcl_ObjType tabledataid_nearlytype;
242 int tabledataid_parse(Tcl_Interp *ip, Tcl_Obj *o, const IdDataSpec *idds);
243 void tabledataid_disposing(Tcl_Interp *ip, void *val, const IdDataSpec *idds);
244 /* call this when you destroy the struct, to remove its name;
245 * _disposing is idempotent */
254 extern const IdDataSpec adnstcl_queries, adnstcl_resolvers;
258 extern const IdDataSpec dgram_socks;
259 int newfdposixerr(Tcl_Interp *ip, int fd, const char *m);
263 extern const IdDataSpec tuntap_socks;
267 int staticerr(Tcl_Interp *ip, const char *m, const char *ec);
268 int posixerr(Tcl_Interp *ip, int errnoval, const char *m);
269 void objfreeir(Tcl_Obj *o);
270 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
272 void obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
273 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
274 int l, const char *prefix);
276 void obj_updatestr_vstringls(Tcl_Obj *o, ...);
277 /* const char*, int, const char*, int, ..., (const char*)0 */
278 void obj_updatestr_string_len(Tcl_Obj *o, const char *str, int l);
279 void obj_updatestr_string(Tcl_Obj *o, const char *str);
288 void init_somethingv(Something_Var *sth);
289 void fini_somethingv(Tcl_Interp *ip, int rc, Something_Var *sth);
290 int pat_somethingv(Tcl_Interp *ip, Tcl_Obj *var,
291 Something_Var *sth, Tcl_ObjType *type);
300 typedef struct MaskMap_Value MaskMap_Value;
307 extern Tcl_ObjType maskmap_type;
310 /* only do_... functions declared in tables.h */
314 Tcl_ObjType ulong_type;
318 extern Tcl_ObjType enum_nearlytype;
319 extern Tcl_ObjType enum1_nearlytype;
321 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
322 const void *firstentry, size_t entrysize,
324 #define enum_lookup_cached(ip,o,table,what) \
325 (enum_lookup_cached_func((ip),(o), \
326 &(table)[0],sizeof((table)[0]), \
328 /* table should be a pointer to an array of structs of size
329 * entrysize, the first member of which should be a const char*.
330 * The table should finish with a null const char *.
331 * On error, 0 is returned and the ip->result will have been
332 * set to the error message.
335 int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
336 const char *opts, const char *what);
341 void memxor(Byte *dest, const Byte *src, int l);
345 int pad, use_algname;
348 extern Tcl_ObjType blockcipherkey_type;
350 /* from algtables.c */
355 } BlockCipherPropInfo, HashAlgPropInfo;
359 int hashsize, blocksize, statesize;
360 void (*init)(void *state);
361 void (*update)(void *state, const void *data, int len);
362 void (*final)(void *state, void *digest);
363 void (*oneshot)(void *digest, const void *data, int len);
366 extern const HashAlgInfo hashalginfos[];
369 void (*make_schedule)(void *schedule, const void *key, int keylen);
370 void (*crypt)(const void *schedule, const void *in, void *out);
371 /* in and out may be the same, but if they aren't they may not overlap */
372 /* in and out for crypt will have been through block_byteswap */
373 } BlockCipherPerDirectionInfo;
377 int blocksize, schedule_size, key_min, key_max;
378 BlockCipherPerDirectionInfo encrypt, decrypt;
379 } BlockCipherAlgInfo;
381 extern const BlockCipherAlgInfo blockcipheralginfos[];
387 int iv_blocks, buf_blocks, mac_blocks;
389 /* Each function is allowed to use up to buf_blocks * blocksize
390 * bytes of space in buf. data is blocks * blocksize bytes
391 * long. data should be modified in place by encrypt and decrypt;
392 * modes may not change the size of data. iv is always provided and
393 * is always of length iv_blocks * blocksize; encrypt and
394 * decrypt may modify the iv value (in which case the Tcl caller
395 * will get the modified IV) but this is not recommended. mac
396 * should leave the mac, which must be mac_blocks * blocksize
397 * bytes, in buf. (Therefore mac_blocks must be at least
400 const char *(*encrypt)(Byte *data, int nblocks,
401 const Byte *iv, Byte *buf,
402 const BlockCipherAlgInfo *alg, int encr,
404 const char *(*decrypt)(Byte *data, int nblocks,
405 const Byte *iv, Byte *buf,
406 const BlockCipherAlgInfo *alg, int encr,
408 const char *(*mac)(const Byte *data, int nblocks,
409 const Byte *iv, Byte *buf,
410 const BlockCipherAlgInfo *alg,
412 } BlockCipherModeInfo;
414 extern const BlockCipherModeInfo blockciphermodeinfos[];
418 int setnonblock(int fd, int isnonblock);
422 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
423 #define OBJ_SOCKADDR(o) ((SockAddr_Value*)&(o)->internalRep.twoPtrValue)
425 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
426 #define TFREE(f) (Tcl_Free((void*)(f)))
427 #define TREALLOC(p,l) ((void*)Tcl_Realloc((void*)(p),(l)))