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 #include "unaligned.h"
17 /* Required key length in bytes */
18 #define REQUIRED_KEYLEN ((512+64+32)/8)
22 struct transform_if ops
;
23 uint32_t max_seq_skew
;
26 struct transform_inst
{
27 struct transform_inst_if ops
;
28 struct keyInstance cryptkey
;
29 struct keyInstance mackey
;
40 static bool_t
transform_setkey(void *sst
, uint8_t *key
, int32_t keylen
)
42 struct transform_inst
*ti
=sst
;
44 if (keylen
<REQUIRED_KEYLEN
) {
45 Message(M_ERR
,"transform_create: insufficient key material supplied "
46 "(need %d bytes, got %d)\n",REQUIRED_KEYLEN
,keylen
);
53 printf("Setting key to: ");
54 for (i
=0; i
<keylen
; i
++)
55 printf("%02x",key
[i
]);
60 serpent_makekey(&ti
->cryptkey
,256,key
);
61 serpent_makekey(&ti
->mackey
,256,key
+32);
62 ti
->cryptiv
=get_uint32(key
+64);
63 ti
->maciv
=get_uint32(key
+68);
64 ti
->sendseq
=get_uint32(key
+72);
65 ti
->lastrecvseq
=ti
->sendseq
;
71 static bool_t
transform_valid(void *sst
)
73 struct transform_inst
*ti
=sst
;
78 static void transform_delkey(void *sst
)
80 struct transform_inst
*ti
=sst
;
82 FILLZERO(ti
->cryptkey
);
87 static uint32_t transform_forward(void *sst
, struct buffer_if
*buf
,
90 struct transform_inst
*ti
=sst
;
100 *errmsg
="transform unkeyed";
104 /* Sequence number */
105 buf_prepend_uint32(buf
,ti
->sendseq
);
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 */
115 padp
=buf_append(buf
,padlen
);
116 memset(padp
,padlen
,padlen
);
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.) */
124 put_uint32(iv
, ti
->maciv
);
125 serpent_encrypt(&ti
->mackey
,iv
,macacc
);
127 /* CBCMAC: encrypt in CBC mode. The MAC is the last encrypted
128 block encrypted once again. */
129 for (n
=buf
->start
; n
<buf
->start
+buf
->size
; n
+=16)
131 for (i
= 0; i
< 16; i
++)
132 macplain
[i
] = macacc
[i
] ^ n
[i
];
133 serpent_encrypt(&ti
->mackey
,macplain
,macacc
);
135 serpent_encrypt(&ti
->mackey
,macacc
,macacc
);
136 memcpy(buf_append(buf
,16),macacc
,16);
138 /* Serpent-CBC. We expand the ID as for CBCMAC, do the encryption,
139 and prepend the IV before increasing it. */
141 put_uint32(iv
, ti
->cryptiv
);
142 serpent_encrypt(&ti
->cryptkey
,iv
,iv
);
144 /* CBC: each block is XORed with the previous encrypted block (or the IV)
145 before being encrypted. */
148 for (n
=buf
->start
; n
<buf
->start
+buf
->size
; n
+=16)
150 for (i
= 0; i
< 16; i
++)
152 serpent_encrypt(&ti
->cryptkey
,n
,n
);
156 buf_prepend_uint32(buf
,ti
->cryptiv
);
161 static uint32_t transform_reverse(void *sst
, struct buffer_if
*buf
,
164 struct transform_inst
*ti
=sst
;
168 uint32_t seqnum
, skew
;
171 uint8_t macplain
[16];
174 uint8_t *macexpected
;
177 *errmsg
="transform unkeyed";
181 if (buf
->size
< 4 + 16 + 16) {
182 *errmsg
="msg too short";
189 uint32_t ivword
= buf_unprepend_uint32(buf
);
190 put_uint32(iv
, ivword
);
192 /* Assert bufsize is multiple of blocksize */
194 *errmsg
="msg not multiple of cipher blocksize";
197 serpent_encrypt(&ti
->cryptkey
,iv
,iv
);
198 for (n
=buf
->start
; n
<buf
->start
+buf
->size
; n
+=16)
200 for (i
= 0; i
< 16; i
++)
202 serpent_decrypt(&ti
->cryptkey
,n
,n
);
203 for (i
= 0; i
< 16; i
++)
209 macexpected
=buf_unappend(buf
,16);
211 put_uint32(iv
, ti
->maciv
);
212 serpent_encrypt(&ti
->mackey
,iv
,macacc
);
214 /* CBCMAC: encrypt in CBC mode. The MAC is the last encrypted
215 block encrypted once again. */
216 for (n
=buf
->start
; n
<buf
->start
+buf
->size
; n
+=16)
218 for (i
= 0; i
< 16; i
++)
219 macplain
[i
] = macacc
[i
] ^ n
[i
];
220 serpent_encrypt(&ti
->mackey
,macplain
,macacc
);
222 serpent_encrypt(&ti
->mackey
,macacc
,macacc
);
223 if (!consttime_memeq(macexpected
,macacc
,16)!=0) {
224 *errmsg
="invalid MAC";
228 /* PKCS5, stolen from IWJ */
230 padp
=buf_unappend(buf
,1);
232 if (!padlen
|| (padlen
> PKCS5_MASK
+1)) {
233 *errmsg
="pkcs5: invalid length";
237 buf_unappend(buf
,padlen
-1);
239 /* Sequence number must be within max_skew of lastrecvseq; lastrecvseq
240 is only allowed to increase. */
241 seqnum
=buf_unprepend_uint32(buf
);
242 skew
=seqnum
-ti
->lastrecvseq
;
243 if (skew
<0x8fffffff) {
245 ti
->lastrecvseq
=seqnum
;
246 } else if ((0-skew
)<ti
->max_skew
) {
250 *errmsg
="seqnum: too much skew";
257 static void transform_destroy(void *sst
)
259 struct transform_inst
*st
=sst
;
261 FILLZERO(*st
); /* Destroy key material */
265 static struct transform_inst_if
*transform_create(void *sst
)
267 struct transform_inst
*ti
;
268 struct transform
*st
=sst
;
270 ti
=safe_malloc(sizeof(*ti
),"transform_create");
274 ti
->ops
.setkey
=transform_setkey
;
275 ti
->ops
.valid
=transform_valid
;
276 ti
->ops
.delkey
=transform_delkey
;
277 ti
->ops
.forwards
=transform_forward
;
278 ti
->ops
.reverse
=transform_reverse
;
279 ti
->ops
.destroy
=transform_destroy
;
280 ti
->max_skew
=st
->max_seq_skew
;
286 static list_t
*transform_apply(closure_t
*self
, struct cloc loc
,
287 dict_t
*context
, list_t
*args
)
289 struct transform
*st
;
293 st
=safe_malloc(sizeof(*st
),"serpent");
294 st
->cl
.description
="serpent-cbc256";
295 st
->cl
.type
=CL_TRANSFORM
;
297 st
->cl
.interface
=&st
->ops
;
299 st
->ops
.max_start_pad
=28; /* 4byte seqnum, 16byte pad, 4byte MACIV,
301 st
->ops
.max_end_pad
=16; /* 16byte CBCMAC */
303 /* We need 256*2 bits for serpent keys, 32 bits for CBC-IV and 32 bits
304 for CBCMAC-IV, and 32 bits for init sequence number */
305 st
->ops
.keylen
=REQUIRED_KEYLEN
;
306 st
->ops
.create
=transform_create
;
308 /* First parameter must be a dict */
309 item
=list_elem(args
,0);
310 if (!item
|| item
->type
!=t_dict
)
311 cfgfatal(loc
,"userv-ipif","parameter must be a dictionary\n");
313 dict
=item
->data
.dict
;
314 st
->max_seq_skew
=dict_read_number(dict
, "max-sequence-skew",
315 False
, "serpent-cbc256", loc
, 10);
317 return new_closure(&st
->cl
);
320 void transform_module(dict_t
*dict
)
322 struct keyInstance k
;
324 uint8_t plaintext
[16];
325 uint8_t ciphertext
[16];
330 * This test pattern is taken directly from the Serpent test
331 * vectors, to ensure we have all endianness issues correct. -sgt
334 /* Serpent self-test */
336 "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff"
337 "\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00",
339 serpent_makekey(&k
,256,data
);
342 "\x01\x23\x45\x67\x89\xab\xcd\xef\xfe\xdc\xba\x98\x76\x54\x32\x10",
344 serpent_encrypt(&k
,plaintext
,ciphertext
);
346 if (memcmp(ciphertext
, "\xca\x7f\xa1\x93\xe3\xeb\x9e\x99"
347 "\xbd\x87\xe3\xaf\x3c\x9a\xdf\x93", 16)) {
348 fatal("transform_module: serpent failed self-test (encrypt)");
350 serpent_decrypt(&k
,ciphertext
,plaintext
);
351 if (memcmp(plaintext
, "\x01\x23\x45\x67\x89\xab\xcd\xef"
352 "\xfe\xdc\xba\x98\x76\x54\x32\x10", 16)) {
353 fatal("transform_module: serpent failed self-test (decrypt)");
356 add_closure(dict
,"serpent256-cbc",transform_apply
);
358 #ifdef TEST_WHOLE_TRANSFORM
360 struct transform
*tr
;
362 struct buffer_if buf
;
363 const char text
[] = "This is a piece of test text.";
364 char keymaterial
[76] =
365 "Seventy-six bytes i"
366 "n four rows of 19; "
367 "this looks almost l"
368 "ike a poem but not.";
372 tr
= malloc(sizeof(struct transform
));
373 tr
->max_seq_skew
= 20;
374 ti
= transform_create(tr
);
376 transform_setkey(ti
, keymaterial
, 76);
378 buf
.base
= malloc(4096);
379 buffer_init(&buf
, 2048);
380 memcpy(buf_append(&buf
, sizeof(text
)), text
, sizeof(text
));
381 if (transform_forward(ti
, &buf
, &errmsg
)) {
382 fatal("transform_forward test: %s", errmsg
);
384 printf("transformed text is:\n");
385 for (i
= 0; i
< buf
.size
; i
++)
386 printf("%02x%c", buf
.start
[i
],
387 (i
%16==15 || i
==buf
.size
-1 ?
'\n' : ' '));
388 if (transform_reverse(ti
, &buf
, &errmsg
)) {
389 fatal("transform_reverse test: %s", errmsg
);
391 printf("transform reversal worked OK\n");