chiark / gitweb /
Import release 0.03
[secnet.git] / transform.c
1 /* Transform module - bulk data transformation */
2
3 /* For now it's hard-coded to do sequence
4    number/pkcs5/serpent-cbcmac/serpent with a 256 bit key for each
5    instance of serpent. We also require key material for the IVs for
6    cbcmac and cbc. Hack: we're not using full 128-bit IVs, we're just
7    using 32 bits and encrypting to get the full IV to save space in
8    the packets sent over the wire. */
9
10 #include <stdio.h>
11 #include "secnet.h"
12 #include "util.h"
13 #include "serpent.h"
14
15 /* Required key length in bytes */
16 #define REQUIRED_KEYLEN ((512+64+32)/8)
17
18 struct transform {
19     closure_t cl;
20     uint32_t line;
21     struct transform_if ops;
22     uint32_t max_seq_skew;
23 };
24
25 struct transform_inst {
26     struct transform_inst_if ops;
27     struct keyInstance cryptkey;
28     struct keyInstance mackey;
29     uint32_t cryptiv;
30     uint32_t maciv;
31     uint32_t sendseq;
32     uint32_t lastrecvseq;
33     uint32_t max_skew;
34     bool_t keyed;
35 };
36
37 #define PKCS5_MASK 15
38
39 static bool_t transform_setkey(void *sst, uint8_t *key, uint32_t keylen)
40 {
41     struct transform_inst *ti=sst;
42
43     if (keylen<REQUIRED_KEYLEN) {
44         Message(M_ERROR,"transform_create: insufficient key material supplied "
45                 "(need %d bytes, got %d)\n",REQUIRED_KEYLEN,keylen);
46         return False;
47     }
48
49 #if 0
50     {
51         int i;
52         printf("Setting key to: ");
53         for (i=0; i<keylen; i++)
54             printf("%02x",key[i]);
55         printf("\n");
56     }
57 #endif /* 0 */
58
59     serpent_makekey(&ti->cryptkey,256,key);
60     serpent_makekey(&ti->mackey,256,key+32);
61     ti->cryptiv=*(uint32_t *)(key+64);
62     ti->maciv=*(uint32_t *)(key+68);
63     ti->sendseq=*(uint32_t *)(key+72);
64     ti->lastrecvseq=ti->sendseq;
65     ti->keyed=True;
66
67     return True;
68 }
69
70 static void transform_delkey(void *sst)
71 {
72     struct transform_inst *ti=sst;
73
74     memset(&ti->cryptkey,0,sizeof(ti->cryptkey));
75     memset(&ti->mackey,0,sizeof(ti->mackey));
76     ti->keyed=False;
77 }
78
79 static uint32_t transform_forward(void *sst, struct buffer_if *buf,
80                                   char **errmsg)
81 {
82     struct transform_inst *ti=sst;
83     uint8_t *padp;
84     int padlen;
85     uint32_t iv[4];
86     uint32_t macplain[4];
87     uint32_t macacc[4];
88     uint32_t *n, *p;
89
90     if (!ti->keyed) {
91         *errmsg="transform unkeyed";
92         return 1;
93     }
94
95     /* Sequence number */
96     *(uint32_t *)buf_prepend(buf,4)=htonl(ti->sendseq);
97     ti->sendseq++;
98
99     /* PKCS5, stolen from IWJ */
100                                     /* eg with blocksize=4 mask=3 mask+2=5   */
101                                     /* msgsize    20    21    22    23   24  */
102     padlen= PKCS5_MASK-buf->size;   /*           -17   -18   -19   -16  -17  */
103     padlen &= PKCS5_MASK;           /*             3     2     1     0    3  */
104     padlen++;                       /*             4     3     2     1    4  */
105
106     padp=buf_append(buf,padlen);
107     memset(padp,padlen,padlen);
108
109     /* Serpent-CBCMAC. We expand the IV from 32-bit to 128-bit using
110        one encryption. Then we do the MAC and append the result. We don't
111        bother sending the IV - it's the same each time. (If we wanted to send
112        it we've have to add 16 bytes to each message, not 4, so that the
113        message stays a multiple of 16 bytes long.) */
114     memset(iv,0,16);
115     iv[0]=ti->maciv;
116     serpent_encrypt(&ti->mackey,iv,macacc);
117
118     /* CBCMAC: encrypt in CBC mode. The MAC is the last encrypted
119        block encrypted once again. */
120     for (n=(uint32_t *)buf->start; n<(uint32_t *)(buf->start+buf->size); n+=4)
121     {
122         macplain[0]=macacc[0]^n[0];
123         macplain[1]=macacc[1]^n[1];
124         macplain[2]=macacc[2]^n[2];
125         macplain[3]=macacc[3]^n[3];
126         serpent_encrypt(&ti->mackey,macplain,macacc);
127     }
128     serpent_encrypt(&ti->mackey,macacc,macacc);
129     memcpy(buf_append(buf,16),macacc,16);
130
131     /* Serpent-CBC. We expand the ID as for CBCMAC, do the encryption,
132        and prepend the IV before increasing it. */
133     memset(iv,0,16);
134     iv[0]=ti->cryptiv;
135     serpent_encrypt(&ti->cryptkey,iv,iv);
136
137     /* CBC: each block is XORed with the previous encrypted block (or the IV)
138        before being encrypted. */
139     p=iv;
140     for (n=(uint32_t *)buf->start; n<(uint32_t *)(buf->start+buf->size); n+=4)
141     {
142         n[0]=p[0]^n[0];
143         n[1]=p[1]^n[1];
144         n[2]=p[2]^n[2];
145         n[3]=p[3]^n[3];
146         serpent_encrypt(&ti->cryptkey,n,n);
147         p=n;
148     }
149
150     *(uint32_t *)buf_prepend(buf,4)=ti->cryptiv;
151     ti->cryptiv++;
152
153     return 0;
154 }
155
156 static uint32_t transform_reverse(void *sst, struct buffer_if *buf,
157                                   char **errmsg)
158 {
159     struct transform_inst *ti=sst;
160     uint8_t *padp;
161     unsigned padlen;
162     int i;
163     uint32_t seqnum, skew;
164     uint32_t iv[4];
165     uint32_t pct[4];
166     uint32_t macplain[4];
167     uint32_t macacc[4];
168     uint32_t *n;
169     uint32_t *macexpected;
170
171     if (!ti->keyed) {
172         *errmsg="transform unkeyed";
173         return 1;
174     }
175
176     /* CBC */
177     memset(iv,0,16);
178     iv[0]=*(uint32_t *)buf_unprepend(buf,4);
179     serpent_encrypt(&ti->cryptkey,iv,iv);
180     /* XXX assert bufsize is multiple of blocksize */
181     for (n=(uint32_t *)buf->start; n<(uint32_t *)(buf->start+buf->size); n+=4)
182     {
183         pct[0]=n[0]; pct[1]=n[1]; pct[2]=n[2]; pct[3]=n[3];
184         serpent_decrypt(&ti->cryptkey,n,n);
185         n[0]=iv[0]^n[0];
186         n[1]=iv[1]^n[1];
187         n[2]=iv[2]^n[2];
188         n[3]=iv[3]^n[3];
189         iv[0]=pct[0]; iv[1]=pct[1]; iv[2]=pct[2]; iv[3]=pct[3];
190     }
191
192     /* CBCMAC */
193     macexpected=buf_unappend(buf,16);
194     memset(iv,0,16);
195     iv[0]=ti->maciv;
196     serpent_encrypt(&ti->mackey,iv,macacc);
197
198     /* CBCMAC: encrypt in CBC mode. The MAC is the last encrypted
199        block encrypted once again. */
200     for (n=(uint32_t *)buf->start; n<(uint32_t *)(buf->start+buf->size); n+=4)
201     {
202         macplain[0]=macacc[0]^n[0];
203         macplain[1]=macacc[1]^n[1];
204         macplain[2]=macacc[2]^n[2];
205         macplain[3]=macacc[3]^n[3];
206         serpent_encrypt(&ti->mackey,macplain,macacc);
207     }
208     serpent_encrypt(&ti->mackey,macacc,macacc);
209     if (memcmp(macexpected,macacc,16)!=0) {
210         *errmsg="invalid MAC";
211         return 1;
212     }
213
214     /* PKCS5, stolen from IWJ */
215
216     padp=buf_unappend(buf,1);
217     padlen=*padp;
218     if (!padlen || (padlen > PKCS5_MASK+1)) {
219         *errmsg="pkcs5: invalid length";
220         return 1;
221     }
222
223     padp=buf_unappend(buf,padlen-1);
224     for (i=0; i<padlen-1; i++) {
225         if (*++padp != padlen) {
226             *errmsg="pkcs5: corrupted padding";
227             return 1;
228         }
229     }
230
231     /* Sequence number must be within max_skew of lastrecvseq; lastrecvseq
232        is only allowed to increase. */
233     seqnum=ntohl(*(uint32_t *)buf_unprepend(buf,4));
234     skew=seqnum-ti->lastrecvseq;
235     if (skew<10) {
236         /* Ok */
237         ti->lastrecvseq=seqnum;
238     } else if ((0-skew)<10) {
239         /* Ok */
240     } else {
241         /* Too much skew */
242         *errmsg="seqnum: too much skew";
243         return 1;
244     }
245     
246     return 0;
247 }
248
249 static void transform_destroy(void *sst)
250 {
251     struct transform_inst *st=sst;
252
253     memset(st,0,sizeof(*st)); /* Destroy key material */
254     free(st);
255 }
256
257 static struct transform_inst_if *transform_create(void *sst)
258 {
259     struct transform_inst *ti;
260     struct transform *st=sst;
261
262     ti=safe_malloc(sizeof(*ti),"transform_create");
263     /* mlock XXX */
264
265     ti->ops.st=ti;
266     ti->ops.setkey=transform_setkey;
267     ti->ops.delkey=transform_delkey;
268     ti->ops.forwards=transform_forward;
269     ti->ops.reverse=transform_reverse;
270     ti->ops.destroy=transform_destroy;
271     ti->max_skew=st->max_seq_skew;
272     ti->keyed=False;
273
274     return &ti->ops;
275 }
276
277 static list_t *transform_apply(closure_t *self, struct cloc loc,
278                                dict_t *context, list_t *args)
279 {
280     struct transform *st;
281     item_t *item;
282     dict_t *dict;
283
284     st=safe_malloc(sizeof(*st),"serpent");
285     st->cl.description="serpent-cbc256";
286     st->cl.type=CL_TRANSFORM;
287     st->cl.apply=NULL;
288     st->cl.interface=&st->ops;
289     st->ops.st=st;
290     st->ops.max_start_pad=28; /* 4byte seqnum, 16byte pad, 4byte MACIV,
291                                  4byte IV */
292     st->ops.max_end_pad=16; /* 16byte CBCMAC */
293
294     /* We need 256*2 bits for serpent keys, 32 bits for CBC-IV and 32 bits
295        for CBCMAC-IV, and 32 bits for init sequence number */
296     st->ops.keylen=REQUIRED_KEYLEN;
297     st->ops.create=transform_create;
298
299     /* First parameter must be a dict */
300     item=list_elem(args,0);
301     if (!item || item->type!=t_dict)
302         cfgfatal(loc,"userv-ipif","parameter must be a dictionary\n");
303     
304     dict=item->data.dict;
305     st->max_seq_skew=dict_read_number(dict, "max-sequence-skew",
306                                       False, "serpent-cbc256", loc, 10);
307
308     return new_closure(&st->cl);
309 }
310
311 init_module transform_module;
312 void transform_module(dict_t *dict)
313 {
314     struct keyInstance k;
315     uint8_t data[32];
316     uint32_t plaintext[4];
317     uint32_t ciphertext[4];
318
319     /* Serpent self-test */
320     memset(data,0,32);
321     serpent_makekey(&k,256,data);
322     plaintext[0]=0x00000000;
323     plaintext[1]=0x00000001;
324     plaintext[2]=0x00000002;
325     plaintext[3]=0x00000003;
326     serpent_encrypt(&k,plaintext,ciphertext);
327     if (ciphertext[3]!=0x7ca73bb0 ||
328         ciphertext[2]!=0x83C31E69 ||
329         ciphertext[1]!=0xec52bd82 ||
330         ciphertext[0]!=0x27a46120) {
331         fatal("transform_module: serpent failed self-test (encrypt)\n");
332     }
333     serpent_decrypt(&k,ciphertext,plaintext);
334     if (plaintext[0]!=0 ||
335         plaintext[1]!=1 ||
336         plaintext[2]!=2 ||
337         plaintext[3]!=3) {
338         fatal("transform_module: serpent failed self-test (decrypt)\n");
339     }
340
341     add_closure(dict,"serpent256-cbc",transform_apply);
342 }