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