1 /* $OpenBSD: xform.c,v 1.16 2001/08/28 12:20:43 ben Exp $ */
3 * The authors of this code are John Ioannidis (ji@tla.org),
4 * Angelos D. Keromytis (kermit@csd.uch.gr) and
5 * Niels Provos (provos@physnet.uni-hamburg.de).
7 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
10 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
11 * by Angelos D. Keromytis.
13 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
16 * Additional features in 1999 by Angelos D. Keromytis.
18 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
19 * Angelos D. Keromytis and Niels Provos.
21 * Copyright (C) 2001, Angelos D. Keromytis.
23 * Permission to use, copy, and modify this software with or without fee
24 * is hereby granted, provided that this entire notice is included in
25 * all copies of any software which is or includes a copy or
26 * modification of this software.
27 * You may use this code under the GNU public license if you so wish. Please
28 * contribute changes back to the authors under this freer than GPL license
29 * so that we may further the use of strong encryption without limitations to
32 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
33 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
34 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
35 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/sysctl.h>
46 #include <sys/errno.h>
48 #include <sys/kernel.h>
49 #include <machine/cpu.h>
51 #include <crypto/blowfish/blowfish.h>
52 #include <crypto/des/des.h>
53 #include <crypto/sha1.h>
55 #include <opencrypto/cast.h>
56 #include <opencrypto/deflate.h>
57 #include <opencrypto/rijndael.h>
58 #include <opencrypto/rmd160.h>
59 #include <opencrypto/skipjack.h>
63 #include <opencrypto/cryptodev.h>
64 #include <opencrypto/xform.h>
66 static void null_encrypt(caddr_t, u_int8_t *);
67 static void null_decrypt(caddr_t, u_int8_t *);
68 static int null_setkey(u_int8_t **, u_int8_t *, int);
69 static void null_zerokey(u_int8_t **);
71 static int des1_setkey(u_int8_t **, u_int8_t *, int);
72 static int des3_setkey(u_int8_t **, u_int8_t *, int);
73 static int blf_setkey(u_int8_t **, u_int8_t *, int);
74 static int cast5_setkey(u_int8_t **, u_int8_t *, int);
75 static int skipjack_setkey(u_int8_t **, u_int8_t *, int);
76 static int rijndael128_setkey(u_int8_t **, u_int8_t *, int);
77 static void des1_encrypt(caddr_t, u_int8_t *);
78 static void des3_encrypt(caddr_t, u_int8_t *);
79 static void blf_encrypt(caddr_t, u_int8_t *);
80 static void cast5_encrypt(caddr_t, u_int8_t *);
81 static void skipjack_encrypt(caddr_t, u_int8_t *);
82 static void rijndael128_encrypt(caddr_t, u_int8_t *);
83 static void des1_decrypt(caddr_t, u_int8_t *);
84 static void des3_decrypt(caddr_t, u_int8_t *);
85 static void blf_decrypt(caddr_t, u_int8_t *);
86 static void cast5_decrypt(caddr_t, u_int8_t *);
87 static void skipjack_decrypt(caddr_t, u_int8_t *);
88 static void rijndael128_decrypt(caddr_t, u_int8_t *);
89 static void des1_zerokey(u_int8_t **);
90 static void des3_zerokey(u_int8_t **);
91 static void blf_zerokey(u_int8_t **);
92 static void cast5_zerokey(u_int8_t **);
93 static void skipjack_zerokey(u_int8_t **);
94 static void rijndael128_zerokey(u_int8_t **);
96 static void null_init(void *);
97 static int null_update(void *, u_int8_t *, u_int16_t);
98 static void null_final(u_int8_t *, void *);
99 static int MD5Update_int(void *, u_int8_t *, u_int16_t);
100 static void SHA1Init_int(void *);
101 static int SHA1Update_int(void *, u_int8_t *, u_int16_t);
102 static void SHA1Final_int(u_int8_t *, void *);
103 static int RMD160Update_int(void *, u_int8_t *, u_int16_t);
104 static int SHA256Update_int(void *, u_int8_t *, u_int16_t);
105 static int SHA384Update_int(void *, u_int8_t *, u_int16_t);
106 static int SHA512Update_int(void *, u_int8_t *, u_int16_t);
108 static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
109 static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
111 MALLOC_DEFINE(M_XDATA, "xform", "xform data buffers");
113 /* Encryption instances */
114 struct enc_xform enc_xform_null = {
115 CRYPTO_NULL_CBC, "NULL",
116 /* NB: blocksize of 4 is to generate a properly aligned ESP header */
117 4, 0, 256, /* 2048 bits, max key */
124 struct enc_xform enc_xform_des = {
125 CRYPTO_DES_CBC, "DES",
133 struct enc_xform enc_xform_3des = {
134 CRYPTO_3DES_CBC, "3DES",
142 struct enc_xform enc_xform_blf = {
143 CRYPTO_BLF_CBC, "Blowfish",
144 8, 5, 56 /* 448 bits, max key */,
151 struct enc_xform enc_xform_cast5 = {
152 CRYPTO_CAST_CBC, "CAST-128",
160 struct enc_xform enc_xform_skipjack = {
161 CRYPTO_SKIPJACK_CBC, "Skipjack",
169 struct enc_xform enc_xform_rijndael128 = {
170 CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
178 struct enc_xform enc_xform_arc4 = {
187 /* Authentication instances */
188 struct auth_hash auth_hash_null = {
189 CRYPTO_NULL_HMAC, "NULL-HMAC",
190 0, 0, 12, sizeof(int), /* NB: context isn't used */
191 null_init, null_update, null_final
194 struct auth_hash auth_hash_hmac_md5_96 = {
195 CRYPTO_MD5_HMAC, "HMAC-MD5",
196 16, 16, 12, sizeof(MD5_CTX),
197 (void (*) (void *)) MD5Init, MD5Update_int,
198 (void (*) (u_int8_t *, void *)) MD5Final
201 struct auth_hash auth_hash_hmac_sha1_96 = {
202 CRYPTO_SHA1_HMAC, "HMAC-SHA1",
203 20, 20, 12, sizeof(SHA1_CTX),
204 SHA1Init_int, SHA1Update_int, SHA1Final_int
207 struct auth_hash auth_hash_hmac_ripemd_160_96 = {
208 CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
209 20, 20, 12, sizeof(RMD160_CTX),
210 (void (*)(void *)) RMD160Init, RMD160Update_int,
211 (void (*)(u_int8_t *, void *)) RMD160Final
214 struct auth_hash auth_hash_key_md5 = {
215 CRYPTO_MD5_KPDK, "Keyed MD5",
216 0, 16, 12, sizeof(MD5_CTX),
217 (void (*)(void *)) MD5Init, MD5Update_int,
218 (void (*)(u_int8_t *, void *)) MD5Final
221 struct auth_hash auth_hash_key_sha1 = {
222 CRYPTO_SHA1_KPDK, "Keyed SHA1",
223 0, 20, 12, sizeof(SHA1_CTX),
224 SHA1Init_int, SHA1Update_int, SHA1Final_int
227 struct auth_hash auth_hash_hmac_sha2_256 = {
228 CRYPTO_SHA2_HMAC, "HMAC-SHA2",
229 32, 32, 12, sizeof(SHA256_CTX),
230 (void (*)(void *)) SHA256_Init, SHA256Update_int,
231 (void (*)(u_int8_t *, void *)) SHA256_Final
234 struct auth_hash auth_hash_hmac_sha2_384 = {
235 CRYPTO_SHA2_HMAC, "HMAC-SHA2-384",
236 48, 48, 12, sizeof(SHA384_CTX),
237 (void (*)(void *)) SHA384_Init, SHA384Update_int,
238 (void (*)(u_int8_t *, void *)) SHA384_Final
241 struct auth_hash auth_hash_hmac_sha2_512 = {
242 CRYPTO_SHA2_HMAC, "HMAC-SHA2-512",
243 64, 64, 12, sizeof(SHA512_CTX),
244 (void (*)(void *)) SHA512_Init, SHA512Update_int,
245 (void (*)(u_int8_t *, void *)) SHA512_Final
248 /* Compression instance */
249 struct comp_algo comp_algo_deflate = {
250 CRYPTO_DEFLATE_COMP, "Deflate",
251 90, deflate_compress,
256 * Encryption wrapper routines.
259 null_encrypt(caddr_t key, u_int8_t *blk)
263 null_decrypt(caddr_t key, u_int8_t *blk)
267 null_setkey(u_int8_t **sched, u_int8_t *key, int len)
273 null_zerokey(u_int8_t **sched)
279 des1_encrypt(caddr_t key, u_int8_t *blk)
281 des_cblock *cb = (des_cblock *) blk;
282 des_key_schedule *p = (des_key_schedule *) key;
284 des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
288 des1_decrypt(caddr_t key, u_int8_t *blk)
290 des_cblock *cb = (des_cblock *) blk;
291 des_key_schedule *p = (des_key_schedule *) key;
293 des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
297 des1_setkey(u_int8_t **sched, u_int8_t *key, int len)
302 MALLOC(p, des_key_schedule *, sizeof (des_key_schedule),
303 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
305 des_set_key((des_cblock *) key, p[0]);
309 *sched = (u_int8_t *) p;
314 des1_zerokey(u_int8_t **sched)
316 bzero(*sched, sizeof (des_key_schedule));
317 FREE(*sched, M_CRYPTO_DATA);
322 des3_encrypt(caddr_t key, u_int8_t *blk)
324 des_cblock *cb = (des_cblock *) blk;
325 des_key_schedule *p = (des_key_schedule *) key;
327 des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
331 des3_decrypt(caddr_t key, u_int8_t *blk)
333 des_cblock *cb = (des_cblock *) blk;
334 des_key_schedule *p = (des_key_schedule *) key;
336 des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
340 des3_setkey(u_int8_t **sched, u_int8_t *key, int len)
345 MALLOC(p, des_key_schedule *, 3*sizeof (des_key_schedule),
346 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
348 des_set_key((des_cblock *)(key + 0), p[0]);
349 des_set_key((des_cblock *)(key + 8), p[1]);
350 des_set_key((des_cblock *)(key + 16), p[2]);
354 *sched = (u_int8_t *) p;
359 des3_zerokey(u_int8_t **sched)
361 bzero(*sched, 3*sizeof (des_key_schedule));
362 FREE(*sched, M_CRYPTO_DATA);
367 blf_encrypt(caddr_t key, u_int8_t *blk)
371 memcpy(t, blk, sizeof (t));
374 /* NB: BF_encrypt expects the block in host order! */
375 BF_encrypt(t, (BF_KEY *) key);
378 memcpy(blk, t, sizeof (t));
382 blf_decrypt(caddr_t key, u_int8_t *blk)
386 memcpy(t, blk, sizeof (t));
389 /* NB: BF_decrypt expects the block in host order! */
390 BF_decrypt(t, (BF_KEY *) key);
393 memcpy(blk, t, sizeof (t));
397 blf_setkey(u_int8_t **sched, u_int8_t *key, int len)
401 MALLOC(*sched, u_int8_t *, sizeof(BF_KEY),
402 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
403 if (*sched != NULL) {
404 BF_set_key((BF_KEY *) *sched, len, key);
412 blf_zerokey(u_int8_t **sched)
414 bzero(*sched, sizeof(BF_KEY));
415 FREE(*sched, M_CRYPTO_DATA);
420 cast5_encrypt(caddr_t key, u_int8_t *blk)
422 cast_encrypt((cast_key *) key, blk, blk);
426 cast5_decrypt(caddr_t key, u_int8_t *blk)
428 cast_decrypt((cast_key *) key, blk, blk);
432 cast5_setkey(u_int8_t **sched, u_int8_t *key, int len)
436 MALLOC(*sched, u_int8_t *, sizeof(cast_key), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
437 if (*sched != NULL) {
438 cast_setkey((cast_key *)*sched, key, len);
446 cast5_zerokey(u_int8_t **sched)
448 bzero(*sched, sizeof(cast_key));
449 FREE(*sched, M_CRYPTO_DATA);
454 skipjack_encrypt(caddr_t key, u_int8_t *blk)
456 skipjack_forwards(blk, blk, (u_int8_t **) key);
460 skipjack_decrypt(caddr_t key, u_int8_t *blk)
462 skipjack_backwards(blk, blk, (u_int8_t **) key);
466 skipjack_setkey(u_int8_t **sched, u_int8_t *key, int len)
470 /* NB: allocate all the memory that's needed at once */
471 MALLOC(*sched, u_int8_t *, 10 * (sizeof(u_int8_t *) + 0x100),
472 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
473 if (*sched != NULL) {
474 u_int8_t** key_tables = (u_int8_t**) *sched;
475 u_int8_t* table = (u_int8_t*) &key_tables[10];
478 for (k = 0; k < 10; k++) {
479 key_tables[k] = table;
482 subkey_table_gen(key, (u_int8_t **) *sched);
490 skipjack_zerokey(u_int8_t **sched)
492 bzero(*sched, 10 * (sizeof(u_int8_t *) + 0x100));
493 FREE(*sched, M_CRYPTO_DATA);
498 rijndael128_encrypt(caddr_t key, u_int8_t *blk)
500 rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
504 rijndael128_decrypt(caddr_t key, u_int8_t *blk)
506 rijndael_decrypt(((rijndael_ctx *) key) + 1, (u_char *) blk,
511 rijndael128_setkey(u_int8_t **sched, u_int8_t *key, int len)
515 MALLOC(*sched, u_int8_t *, 2 * sizeof(rijndael_ctx), M_CRYPTO_DATA,
517 if (*sched != NULL) {
518 rijndael_set_key((rijndael_ctx *) *sched, (u_char *) key, len * 8, 1);
519 rijndael_set_key(((rijndael_ctx *) *sched) + 1, (u_char *) key,
528 rijndael128_zerokey(u_int8_t **sched)
530 bzero(*sched, 2 * sizeof(rijndael_ctx));
531 FREE(*sched, M_CRYPTO_DATA);
545 null_update(void *ctx, u_int8_t *buf, u_int16_t len)
551 null_final(u_int8_t *buf, void *ctx)
553 if (buf != (u_int8_t *) 0)
558 RMD160Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
560 RMD160Update(ctx, buf, len);
565 MD5Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
567 MD5Update(ctx, buf, len);
572 SHA1Init_int(void *ctx)
578 SHA1Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
580 SHA1Update(ctx, buf, len);
585 SHA1Final_int(u_int8_t *blk, void *ctx)
591 SHA256Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
593 SHA256_Update(ctx, buf, len);
598 SHA384Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
600 SHA384_Update(ctx, buf, len);
605 SHA512Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
607 SHA512_Update(ctx, buf, len);
616 deflate_compress(data, size, out)
621 return deflate_global(data, size, 0, out);
625 deflate_decompress(data, size, out)
630 return deflate_global(data, size, 1, out);