Commit | Line | Data |
---|---|---|
2fe58dfd SE |
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> | |
469fd1d9 | 11 | #include <string.h> |
2fe58dfd SE |
12 | #include "secnet.h" |
13 | #include "util.h" | |
14 | #include "serpent.h" | |
59635212 | 15 | #include "unaligned.h" |
2fe58dfd SE |
16 | |
17 | /* Required key length in bytes */ | |
18 | #define REQUIRED_KEYLEN ((512+64+32)/8) | |
19 | ||
20 | struct transform { | |
21 | closure_t cl; | |
2fe58dfd SE |
22 | struct transform_if ops; |
23 | uint32_t max_seq_skew; | |
24 | }; | |
25 | ||
26 | struct transform_inst { | |
27 | struct transform_inst_if ops; | |
28 | struct keyInstance cryptkey; | |
29 | struct keyInstance mackey; | |
30 | uint32_t cryptiv; | |
31 | uint32_t maciv; | |
32 | uint32_t sendseq; | |
33 | uint32_t lastrecvseq; | |
34 | uint32_t max_skew; | |
35 | bool_t keyed; | |
36 | }; | |
37 | ||
38 | #define PKCS5_MASK 15 | |
39 | ||
1caa23ff | 40 | static bool_t transform_setkey(void *sst, uint8_t *key, int32_t keylen) |
2fe58dfd SE |
41 | { |
42 | struct transform_inst *ti=sst; | |
43 | ||
44 | if (keylen<REQUIRED_KEYLEN) { | |
469fd1d9 | 45 | Message(M_ERR,"transform_create: insufficient key material supplied " |
2fe58dfd SE |
46 | "(need %d bytes, got %d)\n",REQUIRED_KEYLEN,keylen); |
47 | return False; | |
48 | } | |
49 | ||
50 | #if 0 | |
51 | { | |
52 | int i; | |
53 | printf("Setting key to: "); | |
54 | for (i=0; i<keylen; i++) | |
55 | printf("%02x",key[i]); | |
56 | printf("\n"); | |
57 | } | |
58 | #endif /* 0 */ | |
59 | ||
60 | serpent_makekey(&ti->cryptkey,256,key); | |
61 | serpent_makekey(&ti->mackey,256,key+32); | |
3b83c932 SE |
62 | ti->cryptiv=GET_32BIT_MSB_FIRST(key+64); |
63 | ti->maciv=GET_32BIT_MSB_FIRST(key+68); | |
64 | ti->sendseq=GET_32BIT_MSB_FIRST(key+72); | |
2fe58dfd SE |
65 | ti->lastrecvseq=ti->sendseq; |
66 | ti->keyed=True; | |
67 | ||
68 | return True; | |
69 | } | |
70 | ||
b67dab18 IJ |
71 | static bool_t transform_valid(void *sst) |
72 | { | |
73 | struct transform_inst *ti=sst; | |
74 | ||
75 | return ti->keyed; | |
76 | } | |
77 | ||
2fe58dfd SE |
78 | static void transform_delkey(void *sst) |
79 | { | |
80 | struct transform_inst *ti=sst; | |
81 | ||
076bb54e IJ |
82 | FILLZERO(ti->cryptkey); |
83 | FILLZERO(ti->mackey); | |
2fe58dfd SE |
84 | ti->keyed=False; |
85 | } | |
86 | ||
87 | static uint32_t transform_forward(void *sst, struct buffer_if *buf, | |
fe5e9cc4 | 88 | const char **errmsg) |
2fe58dfd SE |
89 | { |
90 | struct transform_inst *ti=sst; | |
91 | uint8_t *padp; | |
92 | int padlen; | |
3b83c932 SE |
93 | uint8_t iv[16]; |
94 | uint8_t macplain[16]; | |
95 | uint8_t macacc[16]; | |
96 | uint8_t *p, *n; | |
97 | int i; | |
2fe58dfd SE |
98 | |
99 | if (!ti->keyed) { | |
100 | *errmsg="transform unkeyed"; | |
101 | return 1; | |
102 | } | |
103 | ||
104 | /* Sequence number */ | |
59635212 | 105 | buf_prepend_uint32(buf,ti->sendseq); |
2fe58dfd SE |
106 | ti->sendseq++; |
107 | ||
108 | /* PKCS5, stolen from IWJ */ | |
109 | /* eg with blocksize=4 mask=3 mask+2=5 */ | |
110 | /* msgsize 20 21 22 23 24 */ | |
111 | padlen= PKCS5_MASK-buf->size; /* -17 -18 -19 -16 -17 */ | |
112 | padlen &= PKCS5_MASK; /* 3 2 1 0 3 */ | |
113 | padlen++; /* 4 3 2 1 4 */ | |
114 | ||
115 | padp=buf_append(buf,padlen); | |
116 | memset(padp,padlen,padlen); | |
117 | ||
118 | /* Serpent-CBCMAC. We expand the IV from 32-bit to 128-bit using | |
119 | one encryption. Then we do the MAC and append the result. We don't | |
120 | bother sending the IV - it's the same each time. (If we wanted to send | |
121 | it we've have to add 16 bytes to each message, not 4, so that the | |
122 | message stays a multiple of 16 bytes long.) */ | |
123 | memset(iv,0,16); | |
3b83c932 | 124 | PUT_32BIT_MSB_FIRST(iv, ti->maciv); |
2fe58dfd SE |
125 | serpent_encrypt(&ti->mackey,iv,macacc); |
126 | ||
127 | /* CBCMAC: encrypt in CBC mode. The MAC is the last encrypted | |
128 | block encrypted once again. */ | |
3b83c932 | 129 | for (n=buf->start; n<buf->start+buf->size; n+=16) |
2fe58dfd | 130 | { |
3b83c932 SE |
131 | for (i = 0; i < 16; i++) |
132 | macplain[i] = macacc[i] ^ n[i]; | |
2fe58dfd SE |
133 | serpent_encrypt(&ti->mackey,macplain,macacc); |
134 | } | |
135 | serpent_encrypt(&ti->mackey,macacc,macacc); | |
136 | memcpy(buf_append(buf,16),macacc,16); | |
137 | ||
138 | /* Serpent-CBC. We expand the ID as for CBCMAC, do the encryption, | |
139 | and prepend the IV before increasing it. */ | |
140 | memset(iv,0,16); | |
3b83c932 | 141 | PUT_32BIT_MSB_FIRST(iv, ti->cryptiv); |
2fe58dfd SE |
142 | serpent_encrypt(&ti->cryptkey,iv,iv); |
143 | ||
144 | /* CBC: each block is XORed with the previous encrypted block (or the IV) | |
145 | before being encrypted. */ | |
146 | p=iv; | |
3b83c932 SE |
147 | |
148 | for (n=buf->start; n<buf->start+buf->size; n+=16) | |
2fe58dfd | 149 | { |
3b83c932 SE |
150 | for (i = 0; i < 16; i++) |
151 | n[i] ^= p[i]; | |
2fe58dfd SE |
152 | serpent_encrypt(&ti->cryptkey,n,n); |
153 | p=n; | |
154 | } | |
155 | ||
59635212 | 156 | buf_prepend_uint32(buf,ti->cryptiv); |
2fe58dfd | 157 | ti->cryptiv++; |
2fe58dfd SE |
158 | return 0; |
159 | } | |
160 | ||
161 | static uint32_t transform_reverse(void *sst, struct buffer_if *buf, | |
fe5e9cc4 | 162 | const char **errmsg) |
2fe58dfd SE |
163 | { |
164 | struct transform_inst *ti=sst; | |
165 | uint8_t *padp; | |
1caa23ff | 166 | int padlen; |
2fe58dfd SE |
167 | int i; |
168 | uint32_t seqnum, skew; | |
3b83c932 SE |
169 | uint8_t iv[16]; |
170 | uint8_t pct[16]; | |
171 | uint8_t macplain[16]; | |
172 | uint8_t macacc[16]; | |
173 | uint8_t *n; | |
174 | uint8_t *macexpected; | |
2fe58dfd SE |
175 | |
176 | if (!ti->keyed) { | |
177 | *errmsg="transform unkeyed"; | |
178 | return 1; | |
179 | } | |
180 | ||
20138876 IJ |
181 | if (buf->size < 4 + 16 + 16) { |
182 | *errmsg="msg too short"; | |
183 | return 1; | |
184 | } | |
8689b3a9 | 185 | |
2fe58dfd SE |
186 | /* CBC */ |
187 | memset(iv,0,16); | |
3b83c932 SE |
188 | { |
189 | uint32_t ivword = buf_unprepend_uint32(buf); | |
190 | PUT_32BIT_MSB_FIRST(iv, ivword); | |
191 | } | |
8689b3a9 SE |
192 | /* Assert bufsize is multiple of blocksize */ |
193 | if (buf->size&0xf) { | |
194 | *errmsg="msg not multiple of cipher blocksize"; | |
20138876 | 195 | return 1; |
8689b3a9 | 196 | } |
2fe58dfd | 197 | serpent_encrypt(&ti->cryptkey,iv,iv); |
3b83c932 | 198 | for (n=buf->start; n<buf->start+buf->size; n+=16) |
2fe58dfd | 199 | { |
3b83c932 SE |
200 | for (i = 0; i < 16; i++) |
201 | pct[i] = n[i]; | |
2fe58dfd | 202 | serpent_decrypt(&ti->cryptkey,n,n); |
3b83c932 SE |
203 | for (i = 0; i < 16; i++) |
204 | n[i] ^= iv[i]; | |
205 | memcpy(iv, pct, 16); | |
2fe58dfd SE |
206 | } |
207 | ||
208 | /* CBCMAC */ | |
209 | macexpected=buf_unappend(buf,16); | |
210 | memset(iv,0,16); | |
3b83c932 | 211 | PUT_32BIT_MSB_FIRST(iv, ti->maciv); |
2fe58dfd SE |
212 | serpent_encrypt(&ti->mackey,iv,macacc); |
213 | ||
214 | /* CBCMAC: encrypt in CBC mode. The MAC is the last encrypted | |
215 | block encrypted once again. */ | |
3b83c932 | 216 | for (n=buf->start; n<buf->start+buf->size; n+=16) |
2fe58dfd | 217 | { |
3b83c932 SE |
218 | for (i = 0; i < 16; i++) |
219 | macplain[i] = macacc[i] ^ n[i]; | |
2fe58dfd SE |
220 | serpent_encrypt(&ti->mackey,macplain,macacc); |
221 | } | |
222 | serpent_encrypt(&ti->mackey,macacc,macacc); | |
223 | if (memcmp(macexpected,macacc,16)!=0) { | |
224 | *errmsg="invalid MAC"; | |
225 | return 1; | |
226 | } | |
227 | ||
228 | /* PKCS5, stolen from IWJ */ | |
229 | ||
230 | padp=buf_unappend(buf,1); | |
231 | padlen=*padp; | |
232 | if (!padlen || (padlen > PKCS5_MASK+1)) { | |
233 | *errmsg="pkcs5: invalid length"; | |
234 | return 1; | |
235 | } | |
236 | ||
237 | padp=buf_unappend(buf,padlen-1); | |
238 | for (i=0; i<padlen-1; i++) { | |
239 | if (*++padp != padlen) { | |
240 | *errmsg="pkcs5: corrupted padding"; | |
241 | return 1; | |
242 | } | |
243 | } | |
244 | ||
245 | /* Sequence number must be within max_skew of lastrecvseq; lastrecvseq | |
246 | is only allowed to increase. */ | |
59635212 | 247 | seqnum=buf_unprepend_uint32(buf); |
2fe58dfd | 248 | skew=seqnum-ti->lastrecvseq; |
c6f79b17 | 249 | if (skew<0x8fffffff) { |
2fe58dfd SE |
250 | /* Ok */ |
251 | ti->lastrecvseq=seqnum; | |
c6f79b17 | 252 | } else if ((0-skew)<ti->max_skew) { |
2fe58dfd SE |
253 | /* Ok */ |
254 | } else { | |
255 | /* Too much skew */ | |
256 | *errmsg="seqnum: too much skew"; | |
07e4774c | 257 | return 2; |
2fe58dfd SE |
258 | } |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
263 | static void transform_destroy(void *sst) | |
264 | { | |
265 | struct transform_inst *st=sst; | |
266 | ||
076bb54e | 267 | FILLZERO(*st); /* Destroy key material */ |
2fe58dfd SE |
268 | free(st); |
269 | } | |
270 | ||
271 | static struct transform_inst_if *transform_create(void *sst) | |
272 | { | |
273 | struct transform_inst *ti; | |
274 | struct transform *st=sst; | |
275 | ||
276 | ti=safe_malloc(sizeof(*ti),"transform_create"); | |
277 | /* mlock XXX */ | |
278 | ||
279 | ti->ops.st=ti; | |
280 | ti->ops.setkey=transform_setkey; | |
b67dab18 | 281 | ti->ops.valid=transform_valid; |
2fe58dfd SE |
282 | ti->ops.delkey=transform_delkey; |
283 | ti->ops.forwards=transform_forward; | |
284 | ti->ops.reverse=transform_reverse; | |
285 | ti->ops.destroy=transform_destroy; | |
286 | ti->max_skew=st->max_seq_skew; | |
287 | ti->keyed=False; | |
288 | ||
289 | return &ti->ops; | |
290 | } | |
291 | ||
292 | static list_t *transform_apply(closure_t *self, struct cloc loc, | |
293 | dict_t *context, list_t *args) | |
294 | { | |
295 | struct transform *st; | |
296 | item_t *item; | |
297 | dict_t *dict; | |
298 | ||
299 | st=safe_malloc(sizeof(*st),"serpent"); | |
300 | st->cl.description="serpent-cbc256"; | |
301 | st->cl.type=CL_TRANSFORM; | |
302 | st->cl.apply=NULL; | |
303 | st->cl.interface=&st->ops; | |
304 | st->ops.st=st; | |
305 | st->ops.max_start_pad=28; /* 4byte seqnum, 16byte pad, 4byte MACIV, | |
306 | 4byte IV */ | |
307 | st->ops.max_end_pad=16; /* 16byte CBCMAC */ | |
308 | ||
309 | /* We need 256*2 bits for serpent keys, 32 bits for CBC-IV and 32 bits | |
310 | for CBCMAC-IV, and 32 bits for init sequence number */ | |
311 | st->ops.keylen=REQUIRED_KEYLEN; | |
312 | st->ops.create=transform_create; | |
313 | ||
314 | /* First parameter must be a dict */ | |
315 | item=list_elem(args,0); | |
316 | if (!item || item->type!=t_dict) | |
317 | cfgfatal(loc,"userv-ipif","parameter must be a dictionary\n"); | |
318 | ||
319 | dict=item->data.dict; | |
320 | st->max_seq_skew=dict_read_number(dict, "max-sequence-skew", | |
321 | False, "serpent-cbc256", loc, 10); | |
322 | ||
323 | return new_closure(&st->cl); | |
324 | } | |
325 | ||
2fe58dfd SE |
326 | void transform_module(dict_t *dict) |
327 | { | |
328 | struct keyInstance k; | |
329 | uint8_t data[32]; | |
3b83c932 SE |
330 | uint8_t plaintext[16]; |
331 | uint8_t ciphertext[16]; | |
332 | ||
333 | /* | |
334 | * Serpent self-test. | |
335 | * | |
336 | * This test pattern is taken directly from the Serpent test | |
337 | * vectors, to ensure we have all endianness issues correct. -sgt | |
338 | */ | |
2fe58dfd SE |
339 | |
340 | /* Serpent self-test */ | |
3b83c932 SE |
341 | memcpy(data, |
342 | "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff" | |
343 | "\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00", | |
344 | 32); | |
2fe58dfd | 345 | serpent_makekey(&k,256,data); |
3b83c932 SE |
346 | |
347 | memcpy(plaintext, | |
348 | "\x01\x23\x45\x67\x89\xab\xcd\xef\xfe\xdc\xba\x98\x76\x54\x32\x10", | |
349 | 16); | |
2fe58dfd | 350 | serpent_encrypt(&k,plaintext,ciphertext); |
3b83c932 SE |
351 | |
352 | if (memcmp(ciphertext, "\xca\x7f\xa1\x93\xe3\xeb\x9e\x99" | |
353 | "\xbd\x87\xe3\xaf\x3c\x9a\xdf\x93", 16)) { | |
4f5e39ec | 354 | fatal("transform_module: serpent failed self-test (encrypt)"); |
2fe58dfd SE |
355 | } |
356 | serpent_decrypt(&k,ciphertext,plaintext); | |
3b83c932 SE |
357 | if (memcmp(plaintext, "\x01\x23\x45\x67\x89\xab\xcd\xef" |
358 | "\xfe\xdc\xba\x98\x76\x54\x32\x10", 16)) { | |
4f5e39ec | 359 | fatal("transform_module: serpent failed self-test (decrypt)"); |
2fe58dfd SE |
360 | } |
361 | ||
362 | add_closure(dict,"serpent256-cbc",transform_apply); | |
3b83c932 SE |
363 | |
364 | #ifdef TEST_WHOLE_TRANSFORM | |
365 | { | |
366 | struct transform *tr; | |
367 | void *ti; | |
368 | struct buffer_if buf; | |
369 | const char text[] = "This is a piece of test text."; | |
370 | char keymaterial[76] = | |
371 | "Seventy-six bytes i" | |
372 | "n four rows of 19; " | |
373 | "this looks almost l" | |
374 | "ike a poem but not."; | |
375 | const char *errmsg; | |
376 | int i; | |
377 | ||
378 | tr = malloc(sizeof(struct transform)); | |
379 | tr->max_seq_skew = 20; | |
380 | ti = transform_create(tr); | |
381 | ||
382 | transform_setkey(ti, keymaterial, 76); | |
383 | ||
384 | buf.base = malloc(4096); | |
385 | buffer_init(&buf, 2048); | |
386 | memcpy(buf_append(&buf, sizeof(text)), text, sizeof(text)); | |
387 | if (transform_forward(ti, &buf, &errmsg)) { | |
388 | fatal("transform_forward test: %s", errmsg); | |
389 | } | |
390 | printf("transformed text is:\n"); | |
391 | for (i = 0; i < buf.size; i++) | |
392 | printf("%02x%c", buf.start[i], | |
393 | (i%16==15 || i==buf.size-1 ? '\n' : ' ')); | |
394 | if (transform_reverse(ti, &buf, &errmsg)) { | |
395 | fatal("transform_reverse test: %s", errmsg); | |
396 | } | |
397 | printf("transform reversal worked OK\n"); | |
398 | } | |
399 | #endif | |
2fe58dfd | 400 | } |