1 /* $FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $ */
2 /* $OpenBSD: xform.c,v 1.16 2001/08/28 12:20:43 ben Exp $ */
4 * The authors of this code are John Ioannidis (ji@tla.org),
5 * Angelos D. Keromytis (kermit@csd.uch.gr) and
6 * Niels Provos (provos@physnet.uni-hamburg.de).
8 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17 * Additional features in 1999 by Angelos D. Keromytis.
19 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
20 * Angelos D. Keromytis and Niels Provos.
22 * Copyright (C) 2001, Angelos D. Keromytis.
24 * Permission to use, copy, and modify this software with or without fee
25 * is hereby granted, provided that this entire notice is included in
26 * all copies of any software which is or includes a copy or
27 * modification of this software.
28 * You may use this code under the GNU public license if you so wish. Please
29 * contribute changes back to the authors under this freer than GPL license
30 * so that we may further the use of strong encryption without limitations to
33 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
34 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
35 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
36 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/sysctl.h>
44 #include <sys/errno.h>
46 #include <sys/kernel.h>
47 #include <machine/cpu.h>
49 #include <crypto/blowfish/blowfish.h>
50 #include <crypto/des/des.h>
51 #include <crypto/sha1.h>
53 #include <opencrypto/cast.h>
54 #include <opencrypto/deflate.h>
55 #include <opencrypto/rijndael.h>
56 #include <opencrypto/rmd160.h>
57 #include <opencrypto/skipjack.h>
61 #include <opencrypto/cryptodev.h>
62 #include <opencrypto/xform.h>
64 static void null_encrypt(caddr_t, u_int8_t *);
65 static void null_decrypt(caddr_t, u_int8_t *);
66 static int null_setkey(u_int8_t **, u_int8_t *, int);
67 static void null_zerokey(u_int8_t **);
69 static int des1_setkey(u_int8_t **, u_int8_t *, int);
70 static int des3_setkey(u_int8_t **, u_int8_t *, int);
71 static int blf_setkey(u_int8_t **, u_int8_t *, int);
72 static int cast5_setkey(u_int8_t **, u_int8_t *, int);
73 static int skipjack_setkey(u_int8_t **, u_int8_t *, int);
74 static int rijndael128_setkey(u_int8_t **, u_int8_t *, int);
75 static void des1_encrypt(caddr_t, u_int8_t *);
76 static void des3_encrypt(caddr_t, u_int8_t *);
77 static void blf_encrypt(caddr_t, u_int8_t *);
78 static void cast5_encrypt(caddr_t, u_int8_t *);
79 static void skipjack_encrypt(caddr_t, u_int8_t *);
80 static void rijndael128_encrypt(caddr_t, u_int8_t *);
81 static void des1_decrypt(caddr_t, u_int8_t *);
82 static void des3_decrypt(caddr_t, u_int8_t *);
83 static void blf_decrypt(caddr_t, u_int8_t *);
84 static void cast5_decrypt(caddr_t, u_int8_t *);
85 static void skipjack_decrypt(caddr_t, u_int8_t *);
86 static void rijndael128_decrypt(caddr_t, u_int8_t *);
87 static void des1_zerokey(u_int8_t **);
88 static void des3_zerokey(u_int8_t **);
89 static void blf_zerokey(u_int8_t **);
90 static void cast5_zerokey(u_int8_t **);
91 static void skipjack_zerokey(u_int8_t **);
92 static void rijndael128_zerokey(u_int8_t **);
94 static void null_init(void *);
95 static int null_update(void *, u_int8_t *, u_int16_t);
96 static void null_final(u_int8_t *, void *);
97 static int MD5Update_int(void *, u_int8_t *, u_int16_t);
98 static void SHA1Init_int(void *);
99 static int SHA1Update_int(void *, u_int8_t *, u_int16_t);
100 static void SHA1Final_int(u_int8_t *, void *);
101 static int RMD160Update_int(void *, u_int8_t *, u_int16_t);
102 static int SHA256Update_int(void *, u_int8_t *, u_int16_t);
103 static int SHA384Update_int(void *, u_int8_t *, u_int16_t);
104 static int SHA512Update_int(void *, u_int8_t *, u_int16_t);
106 static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
107 static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
109 MALLOC_DEFINE(M_XDATA, "xform", "xform data buffers");
111 /* Encryption instances */
112 struct enc_xform enc_xform_null = {
113 CRYPTO_NULL_CBC, "NULL",
114 /* NB: blocksize of 4 is to generate a properly aligned ESP header */
115 4, 0, 256, /* 2048 bits, max key */
122 struct enc_xform enc_xform_des = {
123 CRYPTO_DES_CBC, "DES",
131 struct enc_xform enc_xform_3des = {
132 CRYPTO_3DES_CBC, "3DES",
140 struct enc_xform enc_xform_blf = {
141 CRYPTO_BLF_CBC, "Blowfish",
142 8, 5, 56 /* 448 bits, max key */,
149 struct enc_xform enc_xform_cast5 = {
150 CRYPTO_CAST_CBC, "CAST-128",
158 struct enc_xform enc_xform_skipjack = {
159 CRYPTO_SKIPJACK_CBC, "Skipjack",
167 struct enc_xform enc_xform_rijndael128 = {
168 CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
176 struct enc_xform enc_xform_arc4 = {
185 /* Authentication instances */
186 struct auth_hash auth_hash_null = {
187 CRYPTO_NULL_HMAC, "NULL-HMAC",
188 0, 0, 12, sizeof(int), /* NB: context isn't used */
189 null_init, null_update, null_final
192 struct auth_hash auth_hash_hmac_md5_96 = {
193 CRYPTO_MD5_HMAC, "HMAC-MD5",
194 16, 16, 12, sizeof(MD5_CTX),
195 (void (*) (void *)) MD5Init, MD5Update_int,
196 (void (*) (u_int8_t *, void *)) MD5Final
199 struct auth_hash auth_hash_hmac_sha1_96 = {
200 CRYPTO_SHA1_HMAC, "HMAC-SHA1",
201 20, 20, 12, sizeof(SHA1_CTX),
202 SHA1Init_int, SHA1Update_int, SHA1Final_int
205 struct auth_hash auth_hash_hmac_ripemd_160_96 = {
206 CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
207 20, 20, 12, sizeof(RMD160_CTX),
208 (void (*)(void *)) RMD160Init, RMD160Update_int,
209 (void (*)(u_int8_t *, void *)) RMD160Final
212 struct auth_hash auth_hash_key_md5 = {
213 CRYPTO_MD5_KPDK, "Keyed MD5",
214 0, 16, 12, sizeof(MD5_CTX),
215 (void (*)(void *)) MD5Init, MD5Update_int,
216 (void (*)(u_int8_t *, void *)) MD5Final
219 struct auth_hash auth_hash_key_sha1 = {
220 CRYPTO_SHA1_KPDK, "Keyed SHA1",
221 0, 20, 12, sizeof(SHA1_CTX),
222 SHA1Init_int, SHA1Update_int, SHA1Final_int
225 struct auth_hash auth_hash_hmac_sha2_256 = {
226 CRYPTO_SHA2_HMAC, "HMAC-SHA2",
227 32, 32, 12, sizeof(SHA256_CTX),
228 (void (*)(void *)) SHA256_Init, SHA256Update_int,
229 (void (*)(u_int8_t *, void *)) SHA256_Final
232 struct auth_hash auth_hash_hmac_sha2_384 = {
233 CRYPTO_SHA2_HMAC, "HMAC-SHA2-384",
234 48, 48, 12, sizeof(SHA384_CTX),
235 (void (*)(void *)) SHA384_Init, SHA384Update_int,
236 (void (*)(u_int8_t *, void *)) SHA384_Final
239 struct auth_hash auth_hash_hmac_sha2_512 = {
240 CRYPTO_SHA2_HMAC, "HMAC-SHA2-512",
241 64, 64, 12, sizeof(SHA512_CTX),
242 (void (*)(void *)) SHA512_Init, SHA512Update_int,
243 (void (*)(u_int8_t *, void *)) SHA512_Final
246 /* Compression instance */
247 struct comp_algo comp_algo_deflate = {
248 CRYPTO_DEFLATE_COMP, "Deflate",
249 90, deflate_compress,
254 * Encryption wrapper routines.
257 null_encrypt(caddr_t key, u_int8_t *blk)
261 null_decrypt(caddr_t key, u_int8_t *blk)
265 null_setkey(u_int8_t **sched, u_int8_t *key, int len)
271 null_zerokey(u_int8_t **sched)
277 des1_encrypt(caddr_t key, u_int8_t *blk)
279 des_cblock *cb = (des_cblock *) blk;
280 des_key_schedule *p = (des_key_schedule *) key;
282 des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
286 des1_decrypt(caddr_t key, u_int8_t *blk)
288 des_cblock *cb = (des_cblock *) blk;
289 des_key_schedule *p = (des_key_schedule *) key;
291 des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
295 des1_setkey(u_int8_t **sched, u_int8_t *key, int len)
300 MALLOC(p, des_key_schedule *, sizeof (des_key_schedule),
301 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
303 des_set_key((des_cblock *) key, p[0]);
307 *sched = (u_int8_t *) p;
312 des1_zerokey(u_int8_t **sched)
314 bzero(*sched, sizeof (des_key_schedule));
315 FREE(*sched, M_CRYPTO_DATA);
320 des3_encrypt(caddr_t key, u_int8_t *blk)
322 des_cblock *cb = (des_cblock *) blk;
323 des_key_schedule *p = (des_key_schedule *) key;
325 des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
329 des3_decrypt(caddr_t key, u_int8_t *blk)
331 des_cblock *cb = (des_cblock *) blk;
332 des_key_schedule *p = (des_key_schedule *) key;
334 des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
338 des3_setkey(u_int8_t **sched, u_int8_t *key, int len)
343 MALLOC(p, des_key_schedule *, 3*sizeof (des_key_schedule),
344 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
346 des_set_key((des_cblock *)(key + 0), p[0]);
347 des_set_key((des_cblock *)(key + 8), p[1]);
348 des_set_key((des_cblock *)(key + 16), p[2]);
352 *sched = (u_int8_t *) p;
357 des3_zerokey(u_int8_t **sched)
359 bzero(*sched, 3*sizeof (des_key_schedule));
360 FREE(*sched, M_CRYPTO_DATA);
365 blf_encrypt(caddr_t key, u_int8_t *blk)
369 memcpy(t, blk, sizeof (t));
372 /* NB: BF_encrypt expects the block in host order! */
373 BF_encrypt(t, (BF_KEY *) key);
376 memcpy(blk, t, sizeof (t));
380 blf_decrypt(caddr_t key, u_int8_t *blk)
384 memcpy(t, blk, sizeof (t));
387 /* NB: BF_decrypt expects the block in host order! */
388 BF_decrypt(t, (BF_KEY *) key);
391 memcpy(blk, t, sizeof (t));
395 blf_setkey(u_int8_t **sched, u_int8_t *key, int len)
399 MALLOC(*sched, u_int8_t *, sizeof(BF_KEY),
400 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
401 if (*sched != NULL) {
402 BF_set_key((BF_KEY *) *sched, len, key);
410 blf_zerokey(u_int8_t **sched)
412 bzero(*sched, sizeof(BF_KEY));
413 FREE(*sched, M_CRYPTO_DATA);
418 cast5_encrypt(caddr_t key, u_int8_t *blk)
420 cast_encrypt((cast_key *) key, blk, blk);
424 cast5_decrypt(caddr_t key, u_int8_t *blk)
426 cast_decrypt((cast_key *) key, blk, blk);
430 cast5_setkey(u_int8_t **sched, u_int8_t *key, int len)
434 MALLOC(*sched, u_int8_t *, sizeof(cast_key), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
435 if (*sched != NULL) {
436 cast_setkey((cast_key *)*sched, key, len);
444 cast5_zerokey(u_int8_t **sched)
446 bzero(*sched, sizeof(cast_key));
447 FREE(*sched, M_CRYPTO_DATA);
452 skipjack_encrypt(caddr_t key, u_int8_t *blk)
454 skipjack_forwards(blk, blk, (u_int8_t **) key);
458 skipjack_decrypt(caddr_t key, u_int8_t *blk)
460 skipjack_backwards(blk, blk, (u_int8_t **) key);
464 skipjack_setkey(u_int8_t **sched, u_int8_t *key, int len)
468 /* NB: allocate all the memory that's needed at once */
469 MALLOC(*sched, u_int8_t *, 10 * (sizeof(u_int8_t *) + 0x100),
470 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
471 if (*sched != NULL) {
472 u_int8_t** key_tables = (u_int8_t**) *sched;
473 u_int8_t* table = (u_int8_t*) &key_tables[10];
476 for (k = 0; k < 10; k++) {
477 key_tables[k] = table;
480 subkey_table_gen(key, (u_int8_t **) *sched);
488 skipjack_zerokey(u_int8_t **sched)
490 bzero(*sched, 10 * (sizeof(u_int8_t *) + 0x100));
491 FREE(*sched, M_CRYPTO_DATA);
496 rijndael128_encrypt(caddr_t key, u_int8_t *blk)
498 rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
502 rijndael128_decrypt(caddr_t key, u_int8_t *blk)
504 rijndael_decrypt(((rijndael_ctx *) key) + 1, (u_char *) blk,
509 rijndael128_setkey(u_int8_t **sched, u_int8_t *key, int len)
513 MALLOC(*sched, u_int8_t *, 2 * sizeof(rijndael_ctx), M_CRYPTO_DATA,
515 if (*sched != NULL) {
516 rijndael_set_key((rijndael_ctx *) *sched, (u_char *) key, len * 8, 1);
517 rijndael_set_key(((rijndael_ctx *) *sched) + 1, (u_char *) key,
526 rijndael128_zerokey(u_int8_t **sched)
528 bzero(*sched, 2 * sizeof(rijndael_ctx));
529 FREE(*sched, M_CRYPTO_DATA);
543 null_update(void *ctx, u_int8_t *buf, u_int16_t len)
549 null_final(u_int8_t *buf, void *ctx)
551 if (buf != (u_int8_t *) 0)
556 RMD160Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
558 RMD160Update(ctx, buf, len);
563 MD5Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
565 MD5Update(ctx, buf, len);
570 SHA1Init_int(void *ctx)
576 SHA1Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
578 SHA1Update(ctx, buf, len);
583 SHA1Final_int(u_int8_t *blk, void *ctx)
589 SHA256Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
591 SHA256_Update(ctx, buf, len);
596 SHA384Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
598 SHA384_Update(ctx, buf, len);
603 SHA512Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
605 SHA512_Update(ctx, buf, len);
614 deflate_compress(data, size, out)
619 return deflate_global(data, size, 0, out);
623 deflate_decompress(data, size, out)
628 return deflate_global(data, size, 1, out);