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