cryptosoft_xform.c revision 1.23 1 /* $NetBSD: cryptosoft_xform.c,v 1.23 2011/05/26 21:50:03 drochner Exp $ */
2 /* $FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $ */
3 /* $OpenBSD: xform.c,v 1.19 2002/08/16 22:47:25 dhartmei Exp $ */
4
5 /*
6 * The authors of this code are John Ioannidis (ji (at) tla.org),
7 * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
8 * Niels Provos (provos (at) physnet.uni-hamburg.de).
9 *
10 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
11 * in November 1995.
12 *
13 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
14 * by Angelos D. Keromytis.
15 *
16 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17 * and Niels Provos.
18 *
19 * Additional features in 1999 by Angelos D. Keromytis.
20 *
21 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
22 * Angelos D. Keromytis and Niels Provos.
23 *
24 * Copyright (C) 2001, Angelos D. Keromytis.
25 *
26 * Permission to use, copy, and modify this software with or without fee
27 * is hereby granted, provided that this entire notice is included in
28 * all copies of any software which is or includes a copy or
29 * modification of this software.
30 * You may use this code under the GNU public license if you so wish. Please
31 * contribute changes back to the authors under this freer than GPL license
32 * so that we may further the use of strong encryption without limitations to
33 * all.
34 *
35 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
36 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
37 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
38 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
39 * PURPOSE.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(1, "$NetBSD: cryptosoft_xform.c,v 1.23 2011/05/26 21:50:03 drochner Exp $");
44
45 #include <crypto/blowfish/blowfish.h>
46 #include <crypto/cast128/cast128.h>
47 #include <crypto/des/des.h>
48 #include <crypto/rijndael/rijndael.h>
49 #include <crypto/skipjack/skipjack.h>
50 #include <crypto/camellia/camellia.h>
51
52 #include <opencrypto/deflate.h>
53
54 #include <sys/md5.h>
55 #include <sys/rmd160.h>
56 #include <sys/sha1.h>
57 #include <sys/sha2.h>
58 #include <opencrypto/aesxcbcmac.h>
59 #include <opencrypto/gmac.h>
60
61 struct swcr_auth_hash {
62 const struct auth_hash *auth_hash;
63 int ctxsize;
64 void (*Init)(void *);
65 void (*Setkey)(void *, const uint8_t *, uint16_t);
66 void (*Reinit)(void *, const uint8_t *, uint16_t);
67 int (*Update)(void *, const uint8_t *, uint16_t);
68 void (*Final)(uint8_t *, void *);
69 };
70
71 struct swcr_enc_xform {
72 const struct enc_xform *enc_xform;
73 void (*encrypt)(void *, uint8_t *);
74 void (*decrypt)(void *, uint8_t *);
75 int (*setkey)(uint8_t **, const uint8_t *, int);
76 void (*zerokey)(uint8_t **);
77 void (*reinit)(void *, const uint8_t *, uint8_t *);
78 };
79
80 struct swcr_comp_algo {
81 const struct comp_algo *unused_comp_algo;
82 uint32_t (*compress)(uint8_t *, uint32_t, uint8_t **);
83 uint32_t (*decompress)(uint8_t *, uint32_t, uint8_t **, int);
84 };
85
86 static void null_encrypt(void *, u_int8_t *);
87 static void null_decrypt(void *, u_int8_t *);
88 static int null_setkey(u_int8_t **, const u_int8_t *, int);
89 static void null_zerokey(u_int8_t **);
90
91 static int des1_setkey(u_int8_t **, const u_int8_t *, int);
92 static int des3_setkey(u_int8_t **, const u_int8_t *, int);
93 static int blf_setkey(u_int8_t **, const u_int8_t *, int);
94 static int cast5_setkey(u_int8_t **, const u_int8_t *, int);
95 static int skipjack_setkey(u_int8_t **, const u_int8_t *, int);
96 static int rijndael128_setkey(u_int8_t **, const u_int8_t *, int);
97 static int cml_setkey(u_int8_t **, const u_int8_t *, int);
98 static int aes_ctr_setkey(u_int8_t **, const u_int8_t *, int);
99 static void des1_encrypt(void *, u_int8_t *);
100 static void des3_encrypt(void *, u_int8_t *);
101 static void blf_encrypt(void *, u_int8_t *);
102 static void cast5_encrypt(void *, u_int8_t *);
103 static void skipjack_encrypt(void *, u_int8_t *);
104 static void rijndael128_encrypt(void *, u_int8_t *);
105 static void cml_encrypt(void *, u_int8_t *);
106 static void des1_decrypt(void *, u_int8_t *);
107 static void des3_decrypt(void *, u_int8_t *);
108 static void blf_decrypt(void *, u_int8_t *);
109 static void cast5_decrypt(void *, u_int8_t *);
110 static void skipjack_decrypt(void *, u_int8_t *);
111 static void rijndael128_decrypt(void *, u_int8_t *);
112 static void cml_decrypt(void *, u_int8_t *);
113 static void aes_ctr_crypt(void *, u_int8_t *);
114 static void des1_zerokey(u_int8_t **);
115 static void des3_zerokey(u_int8_t **);
116 static void blf_zerokey(u_int8_t **);
117 static void cast5_zerokey(u_int8_t **);
118 static void skipjack_zerokey(u_int8_t **);
119 static void rijndael128_zerokey(u_int8_t **);
120 static void cml_zerokey(u_int8_t **);
121 static void aes_ctr_zerokey(u_int8_t **);
122 static void aes_ctr_reinit(void *, const u_int8_t *, u_int8_t *);
123 static void aes_gcm_reinit(void *, const u_int8_t *, u_int8_t *);
124
125 static void null_init(void *);
126 static int null_update(void *, const u_int8_t *, u_int16_t);
127 static void null_final(u_int8_t *, void *);
128
129 static int MD5Update_int(void *, const u_int8_t *, u_int16_t);
130 static void SHA1Init_int(void *);
131 static int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
132 static void SHA1Final_int(u_int8_t *, void *);
133
134
135 static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
136 static int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
137 static void SHA1Final_int(u_int8_t *, void *);
138 static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
139 static int SHA256Update_int(void *, const u_int8_t *, u_int16_t);
140 static int SHA384Update_int(void *, const u_int8_t *, u_int16_t);
141 static int SHA512Update_int(void *, const u_int8_t *, u_int16_t);
142
143 static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
144 static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **, int);
145 static u_int32_t gzip_compress(u_int8_t *, u_int32_t, u_int8_t **);
146 static u_int32_t gzip_decompress(u_int8_t *, u_int32_t, u_int8_t **, int);
147
148 /* Encryption instances */
149 static const struct swcr_enc_xform swcr_enc_xform_null = {
150 &enc_xform_null,
151 null_encrypt,
152 null_decrypt,
153 null_setkey,
154 null_zerokey,
155 NULL
156 };
157
158 static const struct swcr_enc_xform swcr_enc_xform_des = {
159 &enc_xform_des,
160 des1_encrypt,
161 des1_decrypt,
162 des1_setkey,
163 des1_zerokey,
164 NULL
165 };
166
167 static const struct swcr_enc_xform swcr_enc_xform_3des = {
168 &enc_xform_3des,
169 des3_encrypt,
170 des3_decrypt,
171 des3_setkey,
172 des3_zerokey,
173 NULL
174 };
175
176 static const struct swcr_enc_xform swcr_enc_xform_blf = {
177 &enc_xform_blf,
178 blf_encrypt,
179 blf_decrypt,
180 blf_setkey,
181 blf_zerokey,
182 NULL
183 };
184
185 static const struct swcr_enc_xform swcr_enc_xform_cast5 = {
186 &enc_xform_cast5,
187 cast5_encrypt,
188 cast5_decrypt,
189 cast5_setkey,
190 cast5_zerokey,
191 NULL
192 };
193
194 static const struct swcr_enc_xform swcr_enc_xform_skipjack = {
195 &enc_xform_skipjack,
196 skipjack_encrypt,
197 skipjack_decrypt,
198 skipjack_setkey,
199 skipjack_zerokey,
200 NULL
201 };
202
203 static const struct swcr_enc_xform swcr_enc_xform_rijndael128 = {
204 &enc_xform_rijndael128,
205 rijndael128_encrypt,
206 rijndael128_decrypt,
207 rijndael128_setkey,
208 rijndael128_zerokey,
209 NULL
210 };
211
212 static const struct swcr_enc_xform swcr_enc_xform_aes_ctr = {
213 &enc_xform_aes_ctr,
214 aes_ctr_crypt,
215 aes_ctr_crypt,
216 aes_ctr_setkey,
217 aes_ctr_zerokey,
218 aes_ctr_reinit
219 };
220
221 static const struct swcr_enc_xform swcr_enc_xform_aes_gcm = {
222 &enc_xform_aes_gcm,
223 aes_ctr_crypt,
224 aes_ctr_crypt,
225 aes_ctr_setkey,
226 aes_ctr_zerokey,
227 aes_gcm_reinit
228 };
229
230 static const struct swcr_enc_xform swcr_enc_xform_aes_gmac = {
231 &enc_xform_aes_gmac,
232 NULL,
233 NULL,
234 NULL,
235 NULL,
236 NULL
237 };
238
239 static const struct swcr_enc_xform swcr_enc_xform_camellia = {
240 &enc_xform_camellia,
241 cml_encrypt,
242 cml_decrypt,
243 cml_setkey,
244 cml_zerokey,
245 NULL
246 };
247
248 /* Authentication instances */
249 static const struct swcr_auth_hash swcr_auth_hash_null = {
250 &auth_hash_null, sizeof(int), /* NB: context isn't used */
251 null_init, NULL, NULL, null_update, null_final
252 };
253
254 static const struct swcr_auth_hash swcr_auth_hash_hmac_md5 = {
255 &auth_hash_hmac_md5, sizeof(MD5_CTX),
256 (void (*) (void *)) MD5Init, NULL, NULL, MD5Update_int,
257 (void (*) (u_int8_t *, void *)) MD5Final
258 };
259
260 static const struct swcr_auth_hash swcr_auth_hash_hmac_sha1 = {
261 &auth_hash_hmac_sha1, sizeof(SHA1_CTX),
262 SHA1Init_int, NULL, NULL, SHA1Update_int, SHA1Final_int
263 };
264
265 static const struct swcr_auth_hash swcr_auth_hash_hmac_ripemd_160 = {
266 &auth_hash_hmac_ripemd_160, sizeof(RMD160_CTX),
267 (void (*)(void *)) RMD160Init, NULL, NULL, RMD160Update_int,
268 (void (*)(u_int8_t *, void *)) RMD160Final
269 };
270 static const struct swcr_auth_hash swcr_auth_hash_hmac_md5_96 = {
271 &auth_hash_hmac_md5_96, sizeof(MD5_CTX),
272 (void (*) (void *)) MD5Init, NULL, NULL, MD5Update_int,
273 (void (*) (u_int8_t *, void *)) MD5Final
274 };
275
276 static const struct swcr_auth_hash swcr_auth_hash_hmac_sha1_96 = {
277 &auth_hash_hmac_sha1_96, sizeof(SHA1_CTX),
278 SHA1Init_int, NULL, NULL, SHA1Update_int, SHA1Final_int
279 };
280
281 static const struct swcr_auth_hash swcr_auth_hash_hmac_ripemd_160_96 = {
282 &auth_hash_hmac_ripemd_160_96, sizeof(RMD160_CTX),
283 (void (*)(void *)) RMD160Init, NULL, NULL, RMD160Update_int,
284 (void (*)(u_int8_t *, void *)) RMD160Final
285 };
286
287 static const struct swcr_auth_hash swcr_auth_hash_key_md5 = {
288 &auth_hash_key_md5, sizeof(MD5_CTX),
289 (void (*)(void *)) MD5Init, NULL, NULL, MD5Update_int,
290 (void (*)(u_int8_t *, void *)) MD5Final
291 };
292
293 static const struct swcr_auth_hash swcr_auth_hash_key_sha1 = {
294 &auth_hash_key_sha1, sizeof(SHA1_CTX),
295 SHA1Init_int, NULL, NULL, SHA1Update_int, SHA1Final_int
296 };
297
298 static const struct swcr_auth_hash swcr_auth_hash_md5 = {
299 &auth_hash_md5, sizeof(MD5_CTX),
300 (void (*) (void *)) MD5Init, NULL, NULL, MD5Update_int,
301 (void (*) (u_int8_t *, void *)) MD5Final
302 };
303
304 static const struct swcr_auth_hash swcr_auth_hash_sha1 = {
305 &auth_hash_sha1, sizeof(SHA1_CTX),
306 (void (*)(void *)) SHA1Init, NULL, NULL, SHA1Update_int,
307 (void (*)(u_int8_t *, void *)) SHA1Final
308 };
309
310 static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_256 = {
311 &auth_hash_hmac_sha2_256, sizeof(SHA256_CTX),
312 (void (*)(void *)) SHA256_Init, NULL, NULL, SHA256Update_int,
313 (void (*)(u_int8_t *, void *)) SHA256_Final
314 };
315
316 static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_384 = {
317 &auth_hash_hmac_sha2_384, sizeof(SHA384_CTX),
318 (void (*)(void *)) SHA384_Init, NULL, NULL, SHA384Update_int,
319 (void (*)(u_int8_t *, void *)) SHA384_Final
320 };
321
322 static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_512 = {
323 &auth_hash_hmac_sha2_512, sizeof(SHA512_CTX),
324 (void (*)(void *)) SHA512_Init, NULL, NULL, SHA512Update_int,
325 (void (*)(u_int8_t *, void *)) SHA512_Final
326 };
327
328 static const struct swcr_auth_hash swcr_auth_hash_aes_xcbc_mac = {
329 &auth_hash_aes_xcbc_mac_96, sizeof(aesxcbc_ctx),
330 null_init,
331 (void (*)(void *, const u_int8_t *, u_int16_t))aes_xcbc_mac_init,
332 NULL, aes_xcbc_mac_loop, aes_xcbc_mac_result
333 };
334
335 static const struct swcr_auth_hash swcr_auth_hash_gmac_aes_128 = {
336 &auth_hash_gmac_aes_128, sizeof(AES_GMAC_CTX),
337 (void (*)(void *))AES_GMAC_Init,
338 (void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Setkey,
339 (void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Reinit,
340 (int (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Update,
341 (void (*)(u_int8_t *, void *))AES_GMAC_Final
342 };
343
344 static const struct swcr_auth_hash swcr_auth_hash_gmac_aes_192 = {
345 &auth_hash_gmac_aes_192, sizeof(AES_GMAC_CTX),
346 (void (*)(void *))AES_GMAC_Init,
347 (void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Setkey,
348 (void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Reinit,
349 (int (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Update,
350 (void (*)(u_int8_t *, void *))AES_GMAC_Final
351 };
352
353 static const struct swcr_auth_hash swcr_auth_hash_gmac_aes_256 = {
354 &auth_hash_gmac_aes_256, sizeof(AES_GMAC_CTX),
355 (void (*)(void *))AES_GMAC_Init,
356 (void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Setkey,
357 (void (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Reinit,
358 (int (*)(void *, const u_int8_t *, u_int16_t))AES_GMAC_Update,
359 (void (*)(u_int8_t *, void *))AES_GMAC_Final
360 };
361
362 /* Compression instance */
363 static const struct swcr_comp_algo swcr_comp_algo_deflate = {
364 &comp_algo_deflate,
365 deflate_compress,
366 deflate_decompress
367 };
368
369 static const struct swcr_comp_algo swcr_comp_algo_deflate_nogrow = {
370 &comp_algo_deflate_nogrow,
371 deflate_compress,
372 deflate_decompress
373 };
374
375 static const struct swcr_comp_algo swcr_comp_algo_gzip = {
376 &comp_algo_deflate,
377 gzip_compress,
378 gzip_decompress
379 };
380
381 /*
382 * Encryption wrapper routines.
383 */
384 static void
385 null_encrypt(void *key, u_int8_t *blk)
386 {
387 }
388 static void
389 null_decrypt(void *key, u_int8_t *blk)
390 {
391 }
392 static int
393 null_setkey(u_int8_t **sched, const u_int8_t *key, int len)
394 {
395 *sched = NULL;
396 return 0;
397 }
398 static void
399 null_zerokey(u_int8_t **sched)
400 {
401 *sched = NULL;
402 }
403
404 static void
405 des1_encrypt(void *key, u_int8_t *blk)
406 {
407 des_cblock *cb = (des_cblock *) blk;
408 des_key_schedule *p = (des_key_schedule *) key;
409
410 des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
411 }
412
413 static void
414 des1_decrypt(void *key, u_int8_t *blk)
415 {
416 des_cblock *cb = (des_cblock *) blk;
417 des_key_schedule *p = (des_key_schedule *) key;
418
419 des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
420 }
421
422 static int
423 des1_setkey(u_int8_t **sched, const u_int8_t *key, int len)
424 {
425 des_key_schedule *p;
426 int err;
427
428 p = malloc(sizeof (des_key_schedule),
429 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
430 if (p != NULL) {
431 des_set_key((des_cblock *)__UNCONST(key), p[0]);
432 err = 0;
433 } else
434 err = ENOMEM;
435 *sched = (u_int8_t *) p;
436 return err;
437 }
438
439 static void
440 des1_zerokey(u_int8_t **sched)
441 {
442 memset(*sched, 0, sizeof (des_key_schedule));
443 free(*sched, M_CRYPTO_DATA);
444 *sched = NULL;
445 }
446
447 static void
448 des3_encrypt(void *key, u_int8_t *blk)
449 {
450 des_cblock *cb = (des_cblock *) blk;
451 des_key_schedule *p = (des_key_schedule *) key;
452
453 des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
454 }
455
456 static void
457 des3_decrypt(void *key, u_int8_t *blk)
458 {
459 des_cblock *cb = (des_cblock *) blk;
460 des_key_schedule *p = (des_key_schedule *) key;
461
462 des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
463 }
464
465 static int
466 des3_setkey(u_int8_t **sched, const u_int8_t *key, int len)
467 {
468 des_key_schedule *p;
469 int err;
470
471 p = malloc(3*sizeof (des_key_schedule),
472 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
473 if (p != NULL) {
474 des_set_key((des_cblock *)__UNCONST(key + 0), p[0]);
475 des_set_key((des_cblock *)__UNCONST(key + 8), p[1]);
476 des_set_key((des_cblock *)__UNCONST(key + 16), p[2]);
477 err = 0;
478 } else
479 err = ENOMEM;
480 *sched = (u_int8_t *) p;
481 return err;
482 }
483
484 static void
485 des3_zerokey(u_int8_t **sched)
486 {
487 memset(*sched, 0, 3*sizeof (des_key_schedule));
488 free(*sched, M_CRYPTO_DATA);
489 *sched = NULL;
490 }
491
492 static void
493 blf_encrypt(void *key, u_int8_t *blk)
494 {
495
496 BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 1);
497 }
498
499 static void
500 blf_decrypt(void *key, u_int8_t *blk)
501 {
502
503 BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 0);
504 }
505
506 static int
507 blf_setkey(u_int8_t **sched, const u_int8_t *key, int len)
508 {
509 int err;
510
511 *sched = malloc(sizeof(BF_KEY),
512 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
513 if (*sched != NULL) {
514 BF_set_key((BF_KEY *) *sched, len, key);
515 err = 0;
516 } else
517 err = ENOMEM;
518 return err;
519 }
520
521 static void
522 blf_zerokey(u_int8_t **sched)
523 {
524 memset(*sched, 0, sizeof(BF_KEY));
525 free(*sched, M_CRYPTO_DATA);
526 *sched = NULL;
527 }
528
529 static void
530 cast5_encrypt(void *key, u_int8_t *blk)
531 {
532 cast128_encrypt((cast128_key *) key, blk, blk);
533 }
534
535 static void
536 cast5_decrypt(void *key, u_int8_t *blk)
537 {
538 cast128_decrypt((cast128_key *) key, blk, blk);
539 }
540
541 static int
542 cast5_setkey(u_int8_t **sched, const u_int8_t *key, int len)
543 {
544 int err;
545
546 *sched = malloc(sizeof(cast128_key), M_CRYPTO_DATA,
547 M_NOWAIT|M_ZERO);
548 if (*sched != NULL) {
549 cast128_setkey((cast128_key *)*sched, key, len);
550 err = 0;
551 } else
552 err = ENOMEM;
553 return err;
554 }
555
556 static void
557 cast5_zerokey(u_int8_t **sched)
558 {
559 memset(*sched, 0, sizeof(cast128_key));
560 free(*sched, M_CRYPTO_DATA);
561 *sched = NULL;
562 }
563
564 static void
565 skipjack_encrypt(void *key, u_int8_t *blk)
566 {
567 skipjack_forwards(blk, blk, (u_int8_t **) key);
568 }
569
570 static void
571 skipjack_decrypt(void *key, u_int8_t *blk)
572 {
573 skipjack_backwards(blk, blk, (u_int8_t **) key);
574 }
575
576 static int
577 skipjack_setkey(u_int8_t **sched, const u_int8_t *key, int len)
578 {
579 int err;
580
581 /* NB: allocate all the memory that's needed at once */
582 /* XXX assumes bytes are aligned on sizeof(u_char) == 1 boundaries.
583 * Will this break a pdp-10, Cray-1, or GE-645 port?
584 */
585 *sched = malloc(10 * (sizeof(u_int8_t *) + 0x100),
586 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
587
588 if (*sched != NULL) {
589
590 u_int8_t** key_tables = (u_int8_t**) *sched;
591 u_int8_t* table = (u_int8_t*) &key_tables[10];
592 int k;
593
594 for (k = 0; k < 10; k++) {
595 key_tables[k] = table;
596 table += 0x100;
597 }
598 subkey_table_gen(key, (u_int8_t **) *sched);
599 err = 0;
600 } else
601 err = ENOMEM;
602 return err;
603 }
604
605 static void
606 skipjack_zerokey(u_int8_t **sched)
607 {
608 memset(*sched, 0, 10 * (sizeof(u_int8_t *) + 0x100));
609 free(*sched, M_CRYPTO_DATA);
610 *sched = NULL;
611 }
612
613 static void
614 rijndael128_encrypt(void *key, u_int8_t *blk)
615 {
616 rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
617 }
618
619 static void
620 rijndael128_decrypt(void *key, u_int8_t *blk)
621 {
622 rijndael_decrypt((rijndael_ctx *) key, (u_char *) blk,
623 (u_char *) blk);
624 }
625
626 static int
627 rijndael128_setkey(u_int8_t **sched, const u_int8_t *key, int len)
628 {
629 int err;
630
631 if (len != 16 && len != 24 && len != 32)
632 return EINVAL;
633 *sched = malloc(sizeof(rijndael_ctx), M_CRYPTO_DATA,
634 M_NOWAIT|M_ZERO);
635 if (*sched != NULL) {
636 rijndael_set_key((rijndael_ctx *) *sched, key, len * 8);
637 err = 0;
638 } else
639 err = ENOMEM;
640 return err;
641 }
642
643 static void
644 rijndael128_zerokey(u_int8_t **sched)
645 {
646 memset(*sched, 0, sizeof(rijndael_ctx));
647 free(*sched, M_CRYPTO_DATA);
648 *sched = NULL;
649 }
650
651 static void
652 cml_encrypt(void *key, u_int8_t *blk)
653 {
654
655 camellia_encrypt(key, blk, blk);
656 }
657
658 static void
659 cml_decrypt(void *key, u_int8_t *blk)
660 {
661
662 camellia_decrypt(key, blk, blk);
663 }
664
665 static int
666 cml_setkey(u_int8_t **sched, const u_int8_t *key, int len)
667 {
668 int err;
669
670 if (len != 16 && len != 24 && len != 32)
671 return (EINVAL);
672 *sched = malloc(sizeof(camellia_ctx), M_CRYPTO_DATA,
673 M_NOWAIT|M_ZERO);
674 if (*sched != NULL) {
675 camellia_set_key((camellia_ctx *) *sched, key, len * 8);
676 err = 0;
677 } else
678 err = ENOMEM;
679 return err;
680 }
681
682 static void
683 cml_zerokey(u_int8_t **sched)
684 {
685
686 memset(*sched, 0, sizeof(camellia_ctx));
687 free(*sched, M_CRYPTO_DATA);
688 *sched = NULL;
689 }
690
691 #define AESCTR_NONCESIZE 4
692 #define AESCTR_IVSIZE 8
693 #define AESCTR_BLOCKSIZE 16
694
695 struct aes_ctr_ctx {
696 /* need only encryption half */
697 u_int32_t ac_ek[4*(RIJNDAEL_MAXNR + 1)];
698 u_int8_t ac_block[AESCTR_BLOCKSIZE];
699 int ac_nr;
700 struct {
701 u_int64_t lastiv;
702 } ivgenctx;
703 };
704
705 static void
706 aes_ctr_crypt(void *key, u_int8_t *blk)
707 {
708 struct aes_ctr_ctx *ctx;
709 u_int8_t keystream[AESCTR_BLOCKSIZE];
710 int i;
711
712 ctx = key;
713 /* increment counter */
714 for (i = AESCTR_BLOCKSIZE - 1;
715 i >= AESCTR_NONCESIZE + AESCTR_IVSIZE; i--)
716 if (++ctx->ac_block[i]) /* continue on overflow */
717 break;
718 rijndaelEncrypt(ctx->ac_ek, ctx->ac_nr, ctx->ac_block, keystream);
719 for (i = 0; i < AESCTR_BLOCKSIZE; i++)
720 blk[i] ^= keystream[i];
721 memset(keystream, 0, sizeof(keystream));
722 }
723
724 int
725 aes_ctr_setkey(u_int8_t **sched, const u_int8_t *key, int len)
726 {
727 struct aes_ctr_ctx *ctx;
728
729 if (len < AESCTR_NONCESIZE)
730 return EINVAL;
731
732 ctx = malloc(sizeof(struct aes_ctr_ctx), M_CRYPTO_DATA,
733 M_NOWAIT|M_ZERO);
734 if (!ctx)
735 return ENOMEM;
736 ctx->ac_nr = rijndaelKeySetupEnc(ctx->ac_ek, (const u_char *)key,
737 (len - AESCTR_NONCESIZE) * 8);
738 if (!ctx->ac_nr) { /* wrong key len */
739 aes_ctr_zerokey((u_int8_t **)&ctx);
740 return EINVAL;
741 }
742 memcpy(ctx->ac_block, key + len - AESCTR_NONCESIZE, AESCTR_NONCESIZE);
743 /* random start value for simple counter */
744 arc4randbytes(&ctx->ivgenctx.lastiv, sizeof(ctx->ivgenctx.lastiv));
745 *sched = (void *)ctx;
746 return 0;
747 }
748
749 void
750 aes_ctr_zerokey(u_int8_t **sched)
751 {
752
753 memset(*sched, 0, sizeof(struct aes_ctr_ctx));
754 free(*sched, M_CRYPTO_DATA);
755 *sched = NULL;
756 }
757
758 void
759 aes_ctr_reinit(void *key, const u_int8_t *iv, u_int8_t *ivout)
760 {
761 struct aes_ctr_ctx *ctx = key;
762
763 if (!iv) {
764 ctx->ivgenctx.lastiv++;
765 iv = (const u_int8_t *)&ctx->ivgenctx.lastiv;
766 }
767 if (ivout)
768 memcpy(ivout, iv, AESCTR_IVSIZE);
769 memcpy(ctx->ac_block + AESCTR_NONCESIZE, iv, AESCTR_IVSIZE);
770 /* reset counter */
771 memset(ctx->ac_block + AESCTR_NONCESIZE + AESCTR_IVSIZE, 0, 4);
772 }
773
774 void
775 aes_gcm_reinit(void *key, const u_int8_t *iv, u_int8_t *ivout)
776 {
777 struct aes_ctr_ctx *ctx = key;
778
779 if (!iv) {
780 ctx->ivgenctx.lastiv++;
781 iv = (const u_int8_t *)&ctx->ivgenctx.lastiv;
782 }
783 if (ivout)
784 memcpy(ivout, iv, AESCTR_IVSIZE);
785 memcpy(ctx->ac_block + AESCTR_NONCESIZE, iv, AESCTR_IVSIZE);
786 /* reset counter */
787 memset(ctx->ac_block + AESCTR_NONCESIZE + AESCTR_IVSIZE, 0, 4);
788 ctx->ac_block[AESCTR_BLOCKSIZE - 1] = 1; /* GCM starts with 1 */
789 }
790
791 /*
792 * And now for auth.
793 */
794
795 static void
796 null_init(void *ctx)
797 {
798 }
799
800 static int
801 null_update(void *ctx, const u_int8_t *buf,
802 u_int16_t len)
803 {
804 return 0;
805 }
806
807 static void
808 null_final(u_int8_t *buf, void *ctx)
809 {
810 if (buf != (u_int8_t *) 0)
811 memset(buf, 0, 12);
812 }
813
814 static int
815 RMD160Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
816 {
817 RMD160Update(ctx, buf, len);
818 return 0;
819 }
820
821 static int
822 MD5Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
823 {
824 MD5Update(ctx, buf, len);
825 return 0;
826 }
827
828 static void
829 SHA1Init_int(void *ctx)
830 {
831 SHA1Init(ctx);
832 }
833
834 static int
835 SHA1Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
836 {
837 SHA1Update(ctx, buf, len);
838 return 0;
839 }
840
841 static void
842 SHA1Final_int(u_int8_t *blk, void *ctx)
843 {
844 SHA1Final(blk, ctx);
845 }
846
847 static int
848 SHA256Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
849 {
850 SHA256_Update(ctx, buf, len);
851 return 0;
852 }
853
854 static int
855 SHA384Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
856 {
857 SHA384_Update(ctx, buf, len);
858 return 0;
859 }
860
861 static int
862 SHA512Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
863 {
864 SHA512_Update(ctx, buf, len);
865 return 0;
866 }
867
868 /*
869 * And compression
870 */
871
872 static u_int32_t
873 deflate_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
874 {
875 return deflate_global(data, size, 0, out, 0);
876 }
877
878 static u_int32_t
879 deflate_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out,
880 int size_hint)
881 {
882 return deflate_global(data, size, 1, out, size_hint);
883 }
884
885 static u_int32_t
886 gzip_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
887 {
888 return gzip_global(data, size, 0, out, 0);
889 }
890
891 static u_int32_t
892 gzip_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out,
893 int size_hint)
894 {
895 return gzip_global(data, size, 1, out, size_hint);
896 }
897