1 /* Transform module - bulk data transformation */
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. */
15 /* Required key length in bytes */
16 #define REQUIRED_KEYLEN ((512+64+32)/8)
21 struct transform_if ops;
22 uint32_t max_seq_skew;
25 struct transform_inst {
26 struct transform_inst_if ops;
27 struct keyInstance cryptkey;
28 struct keyInstance mackey;
39 static bool_t transform_setkey(void *sst, uint8_t *key, uint32_t keylen)
41 struct transform_inst *ti=sst;
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);
52 printf("Setting key to: ");
53 for (i=0; i<keylen; i++)
54 printf("%02x",key[i]);
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;
70 static void transform_delkey(void *sst)
72 struct transform_inst *ti=sst;
74 memset(&ti->cryptkey,0,sizeof(ti->cryptkey));
75 memset(&ti->mackey,0,sizeof(ti->mackey));
79 static uint32_t transform_forward(void *sst, struct buffer_if *buf,
82 struct transform_inst *ti=sst;
91 *errmsg="transform unkeyed";
96 *(uint32_t *)buf_prepend(buf,4)=htonl(ti->sendseq);
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 */
106 padp=buf_append(buf,padlen);
107 memset(padp,padlen,padlen);
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.) */
116 serpent_encrypt(&ti->mackey,iv,macacc);
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)
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);
128 serpent_encrypt(&ti->mackey,macacc,macacc);
129 memcpy(buf_append(buf,16),macacc,16);
131 /* Serpent-CBC. We expand the ID as for CBCMAC, do the encryption,
132 and prepend the IV before increasing it. */
135 serpent_encrypt(&ti->cryptkey,iv,iv);
137 /* CBC: each block is XORed with the previous encrypted block (or the IV)
138 before being encrypted. */
140 for (n=(uint32_t *)buf->start; n<(uint32_t *)(buf->start+buf->size); n+=4)
146 serpent_encrypt(&ti->cryptkey,n,n);
150 *(uint32_t *)buf_prepend(buf,4)=ti->cryptiv;
156 static uint32_t transform_reverse(void *sst, struct buffer_if *buf,
159 struct transform_inst *ti=sst;
163 uint32_t seqnum, skew;
166 uint32_t macplain[4];
169 uint32_t *macexpected;
172 *errmsg="transform unkeyed";
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)
183 pct[0]=n[0]; pct[1]=n[1]; pct[2]=n[2]; pct[3]=n[3];
184 serpent_decrypt(&ti->cryptkey,n,n);
189 iv[0]=pct[0]; iv[1]=pct[1]; iv[2]=pct[2]; iv[3]=pct[3];
193 macexpected=buf_unappend(buf,16);
196 serpent_encrypt(&ti->mackey,iv,macacc);
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)
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);
208 serpent_encrypt(&ti->mackey,macacc,macacc);
209 if (memcmp(macexpected,macacc,16)!=0) {
210 *errmsg="invalid MAC";
214 /* PKCS5, stolen from IWJ */
216 padp=buf_unappend(buf,1);
218 if (!padlen || (padlen > PKCS5_MASK+1)) {
219 *errmsg="pkcs5: invalid length";
223 padp=buf_unappend(buf,padlen-1);
224 for (i=0; i<padlen-1; i++) {
225 if (*++padp != padlen) {
226 *errmsg="pkcs5: corrupted padding";
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;
237 ti->lastrecvseq=seqnum;
238 } else if ((0-skew)<10) {
242 *errmsg="seqnum: too much skew";
249 static void transform_destroy(void *sst)
251 struct transform_inst *st=sst;
253 memset(st,0,sizeof(*st)); /* Destroy key material */
257 static struct transform_inst_if *transform_create(void *sst)
259 struct transform_inst *ti;
260 struct transform *st=sst;
262 ti=safe_malloc(sizeof(*ti),"transform_create");
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;
277 static list_t *transform_apply(closure_t *self, struct cloc loc,
278 dict_t *context, list_t *args)
280 struct transform *st;
284 st=safe_malloc(sizeof(*st),"serpent");
285 st->cl.description="serpent-cbc256";
286 st->cl.type=CL_TRANSFORM;
288 st->cl.interface=&st->ops;
290 st->ops.max_start_pad=28; /* 4byte seqnum, 16byte pad, 4byte MACIV,
292 st->ops.max_end_pad=16; /* 16byte CBCMAC */
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;
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");
304 dict=item->data.dict;
305 st->max_seq_skew=dict_read_number(dict, "max-sequence-skew",
306 False, "serpent-cbc256", loc, 10);
308 return new_closure(&st->cl);
311 init_module transform_module;
312 void transform_module(dict_t *dict)
314 struct keyInstance k;
316 uint32_t plaintext[4];
317 uint32_t ciphertext[4];
319 /* Serpent self-test */
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");
333 serpent_decrypt(&k,ciphertext,plaintext);
334 if (plaintext[0]!=0 ||
338 fatal("transform_module: serpent failed self-test (decrypt)\n");
341 add_closure(dict,"serpent256-cbc",transform_apply);