chiark / gitweb /
Serpent seems to work. Byte order is very strange.
[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
32 #include <tcl.h>
33
34 typedef unsigned char Byte;
35
36 /* from hbytes.c */
37
38 /* Internal representation details: */
39 #define HBYTES_ISEMPTY(hb)    (!(hb)->begin_complex && !(hb)->end_0)
40 #define HBYTES_ISSENTINEL(hb) (!(hb)->begin_complex && (hb)->end_0)
41 #define HBYTES_ISSIMPLE(hb)   ((hb)->begin_complex && (hb)->end_0)
42 #define HBYTES_ISCOMPLEX(hb)  ((hb)->begin_complex && !(hb)->end_0)
43
44 typedef struct {
45   void *begin_complex, *end_0;
46 } HBytes_Value; /* overlays internalRep */
47
48 typedef struct {
49   Byte *dstart; /* always allocated dynamically */
50   int prespace, len, avail;
51   /*        
52    * | SPARE      | USED  | SPARE |
53    * |<-prespace->|<-len->|       |
54    * |            |<----avail---->|
55    *              ^start
56    */
57 } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */
58
59 /* Public interfaces: */
60
61 extern Tcl_ObjType hbytes_type;
62
63 int hbytes_len(const HBytes_Value *v);
64 Byte *hbytes_data(const HBytes_Value *v); /* caller may then modify data! */
65 int hbytes_issentinel(const HBytes_Value *v);
66
67 Byte *hbytes_prepend(HBytes_Value *upd, int el);
68 Byte *hbytes_append(HBytes_Value *upd, int el);
69   /* return value is where to put the data */
70
71 const Byte *hbytes_unprepend(HBytes_Value *upd, int rl);
72 const Byte *hbytes_unappend(HBytes_Value *upd, int rl);
73   /* return value points to the removed data, which remains valid
74    * until next op on the HBytes_Value.  If original value is
75    * shorter than rl or negative, returns 0 and does nothing. */
76
77 void hbytes_empty(HBytes_Value *returns);
78 void hbytes_sentinel(HBytes_Value *returns);
79 void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
80 Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
81 void hbytes_free(HBytes_Value *frees);
82   /* _empty, _sentinel and _array do not free or read the old value;
83    * _free it first if needed.  _free leaves it garbage, so you
84    * have to call _empty to reuse it.  _arrayspace doesn't fill
85    * the array; you get a pointer and must fill it with data
86    * yourself. */
87
88 /* The value made by hbytes_sentinel should not be passed to
89  * anything except HBYTES_IS..., and hbytes_free. */
90
91 /* from hook.c */
92
93 int staticerr(Tcl_Interp *ip, const char *m);
94 void objfreeir(Tcl_Obj *o);
95 void obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
96 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
97
98 /* from parse.c */
99
100 typedef struct {
101   HBytes_Value *hb;
102   Tcl_Obj *obj, *var;
103 } HBytes_Var;
104
105 void fini_hbv(Tcl_Interp *ip, int rc, HBytes_Var *agg);
106
107 /* from chop.c */
108
109 /* from enum.c */
110
111 extern Tcl_ObjType enum_nearlytype;
112 extern Tcl_ObjType enum1_nearlytype;
113
114 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
115                                     const void *firstentry, size_t entrysize,
116                                     const char *what);
117 #define enum_lookup_cached(ip,o,table,what)                     \
118     (enum_lookup_cached_func((ip),(o),                          \
119                              &(table)[0],sizeof((table)[0]),    \
120                              (what)))
121   /* table should be a pointer to an array of structs of size
122    * entrysize, the first member of which should be a const char*.
123    * The table should finish with a null const char *.
124    * On error, 0 is returned and the ip->result will have been
125    * set to the error message.
126    */
127
128 int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
129                              const char *opts, const char *what);
130   /* -1 => error */
131
132 /* from crypto.c */
133
134 void memxor(Byte *dest, const Byte *src, int l);
135
136 typedef struct {
137   const char *name;
138   int pad, use_algname;
139 } PadMethod;
140
141 Tcl_ObjType blockcipherkey_type;
142
143 /* from hash.c */
144
145 typedef struct {
146   int blocksize, hashsize;
147 } HashAlgInfo;
148
149 /* from blockciph.c */
150
151 typedef struct {
152   void (*make_schedule)(void *schedule, const Byte *key, int keylen);
153   void (*crypt)(const void *schedule, const void *in, void *out);
154      /* in and out may be the same, but if they aren't they may not overlap */
155      /* in and out for crypt will have been through block_byteswap */
156 } BlockCipherDirectionInfo;
157
158 typedef struct {
159   const char *name;
160   int blocksize, schedule_size, key_min, key_max;
161   void (*byteswap)(Byte *block);
162   BlockCipherDirectionInfo encrypt, decrypt;
163 } BlockCipherAlgInfo;
164
165 extern const BlockCipherAlgInfo blockcipheralginfos[];
166
167 /* from bcmode.c */
168
169 typedef struct {
170   const char *name;
171   int iv_blocks, buf_blocks;
172   const char *(*encrypt)(Byte *data, int blocks,
173                          const Byte *iv, Byte *buf,
174                          const BlockCipherAlgInfo *alg, int encr,
175                          int blocksize, const void *sch);
176   const char *(*decrypt)(Byte *data, int blocks,
177                          const Byte *iv, Byte *buf,
178                          const BlockCipherAlgInfo *alg, int encr,
179                          int blocksize, const void *sch);
180     /* in each case, *iv is provided, but may be modified */
181 } BlockCipherModeInfo;
182
183 extern const BlockCipherModeInfo blockciphermodeinfos[];
184
185 /* useful macros */
186
187 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
188
189 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
190 #define TFREE(f) (Tcl_Free((void*)(f)))
191
192 #endif /*HBYTES_H*/