1 /* $FreeBSD: src/sys/opencrypto/xform.c,v 1.10 2008/10/23 15:53:51 des 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/camellia/camellia.h>
51 #include <crypto/des/des.h>
52 #include <crypto/rijndael/rijndael.h>
53 #include <crypto/serpent/serpent.h>
54 #include <crypto/sha1.h>
55 #include <crypto/twofish/twofish.h>
57 #include <opencrypto/cast.h>
58 #include <opencrypto/deflate.h>
59 #include <opencrypto/gmac.h>
60 #include <opencrypto/rmd160.h>
61 #include <opencrypto/skipjack.h>
65 #include <opencrypto/cryptodev.h>
66 #include <opencrypto/xform.h>
68 static void null_encrypt(caddr_t, u_int8_t *, u_int8_t *);
69 static void null_decrypt(caddr_t, u_int8_t *, u_int8_t *);
70 static int null_setkey(void *, u_int8_t *, int);
72 static int des1_setkey(void *, u_int8_t *, int);
73 static int des3_setkey(void *, u_int8_t *, int);
74 static int blf_setkey(void *, u_int8_t *, int);
75 static int cast5_setkey(void *, u_int8_t *, int);
76 static int skipjack_setkey(void *, u_int8_t *, int);
77 static int rijndael128_setkey(void *, u_int8_t *, int);
78 static int aes_xts_setkey(void *, u_int8_t *, int);
79 static int aes_ctr_setkey(void *, u_int8_t *, int);
80 static int cml_setkey(void *, u_int8_t *, int);
81 static int twofish128_setkey(void *, u_int8_t *, int);
82 static int serpent128_setkey(void *, u_int8_t *, int);
83 static int twofish_xts_setkey(void *, u_int8_t *, int);
84 static int serpent_xts_setkey(void *, u_int8_t *, int);
86 static void des1_encrypt(caddr_t, u_int8_t *, u_int8_t *);
87 static void des3_encrypt(caddr_t, u_int8_t *, u_int8_t *);
88 static void blf_encrypt(caddr_t, u_int8_t *, u_int8_t *);
89 static void cast5_encrypt(caddr_t, u_int8_t *, u_int8_t *);
90 static void skipjack_encrypt(caddr_t, u_int8_t *, u_int8_t *);
91 static void rijndael128_encrypt(caddr_t, u_int8_t *, u_int8_t *);
92 static void aes_xts_encrypt(caddr_t, u_int8_t *, u_int8_t *);
93 static void cml_encrypt(caddr_t, u_int8_t *, u_int8_t *);
94 static void twofish128_encrypt(caddr_t, u_int8_t *, u_int8_t *);
95 static void serpent128_encrypt(caddr_t, u_int8_t *, u_int8_t *);
96 static void twofish_xts_encrypt(caddr_t, u_int8_t *, u_int8_t *);
97 static void serpent_xts_encrypt(caddr_t, u_int8_t *, u_int8_t *);
99 static void des1_decrypt(caddr_t, u_int8_t *, u_int8_t *);
100 static void des3_decrypt(caddr_t, u_int8_t *, u_int8_t *);
101 static void blf_decrypt(caddr_t, u_int8_t *, u_int8_t *);
102 static void cast5_decrypt(caddr_t, u_int8_t *, u_int8_t *);
103 static void skipjack_decrypt(caddr_t, u_int8_t *, u_int8_t *);
104 static void rijndael128_decrypt(caddr_t, u_int8_t *, u_int8_t *);
105 static void aes_xts_decrypt(caddr_t, u_int8_t *, u_int8_t *);
106 static void cml_decrypt(caddr_t, u_int8_t *, u_int8_t *);
107 static void twofish128_decrypt(caddr_t, u_int8_t *, u_int8_t *);
108 static void serpent128_decrypt(caddr_t, u_int8_t *, u_int8_t *);
109 static void twofish_xts_decrypt(caddr_t, u_int8_t *, u_int8_t *);
110 static void serpent_xts_decrypt(caddr_t, u_int8_t *, u_int8_t *);
112 static void aes_ctr_crypt(caddr_t, u_int8_t *, u_int8_t *);
114 static void aes_ctr_reinit(caddr_t, u_int8_t *);
115 static void aes_xts_reinit(caddr_t, u_int8_t *);
116 static void aes_gcm_reinit(caddr_t, u_int8_t *);
117 static void twofish_xts_reinit(caddr_t, u_int8_t *);
118 static void serpent_xts_reinit(caddr_t, u_int8_t *);
120 static void null_init(void *);
121 static int null_update(void *, u_int8_t *, u_int16_t);
122 static void null_final(u_int8_t *, void *);
123 static int MD5Update_int(void *, u_int8_t *, u_int16_t);
124 static void SHA1Init_int(void *);
125 static int SHA1Update_int(void *, u_int8_t *, u_int16_t);
126 static void SHA1Final_int(u_int8_t *, void *);
127 static int RMD160Update_int(void *, u_int8_t *, u_int16_t);
128 static int SHA256Update_int(void *, u_int8_t *, u_int16_t);
129 static int SHA384Update_int(void *, u_int8_t *, u_int16_t);
130 static int SHA512Update_int(void *, u_int8_t *, u_int16_t);
132 static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
133 static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
135 #define AES_XTS_ALPHA 0x87 /* GF(2^128) generator polynomial */
136 #define AESCTR_NONCESIZE 4
144 u_int32_t ac_ek[4*(14 + 1)];
145 u_int8_t ac_block[AESCTR_BLOCK_LEN];
149 struct twofish_xts_ctx {
154 struct serpent_xts_ctx {
160 static void aes_xts_crypt(struct aes_xts_ctx *, u_int8_t *, u_int8_t *, u_int);
161 static void twofish_xts_crypt(struct twofish_xts_ctx *, u_int8_t *, u_int8_t *,
163 static void serpent_xts_crypt(struct serpent_xts_ctx *, u_int8_t *, u_int8_t *,
166 MALLOC_DEFINE(M_XDATA, "xform", "xform data buffers");
168 /* Encryption instances */
169 struct enc_xform enc_xform_null = {
170 CRYPTO_NULL_CBC, "NULL",
171 /* NB: blocksize of 4 is to generate a properly aligned ESP header */
172 NULL_BLOCK_LEN, NULL_BLOCK_LEN, 0, 256, /* 2048 bits, max key */
173 sizeof(int), /* NB: context isn't used */
180 struct enc_xform enc_xform_des = {
181 CRYPTO_DES_CBC, "DES",
182 DES_BLOCK_LEN, DES_BLOCK_LEN, 8, 8,
183 sizeof(des_key_schedule),
190 struct enc_xform enc_xform_3des = {
191 CRYPTO_3DES_CBC, "3DES",
192 DES3_BLOCK_LEN, DES3_BLOCK_LEN, 24, 24,
193 3 * sizeof(des_key_schedule),
200 struct enc_xform enc_xform_blf = {
201 CRYPTO_BLF_CBC, "Blowfish",
202 BLOWFISH_BLOCK_LEN, BLOWFISH_BLOCK_LEN, 5, 56 /* 448 bits, max key */,
210 struct enc_xform enc_xform_cast5 = {
211 CRYPTO_CAST_CBC, "CAST-128",
212 CAST128_BLOCK_LEN, CAST128_BLOCK_LEN, 5, 16,
220 struct enc_xform enc_xform_skipjack = {
221 CRYPTO_SKIPJACK_CBC, "Skipjack",
222 SKIPJACK_BLOCK_LEN, SKIPJACK_BLOCK_LEN, 10, 10,
223 10 * (sizeof(u_int8_t *) + 0x100), /* NB: all needed memory */
230 struct enc_xform enc_xform_rijndael128 = {
231 CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
232 RIJNDAEL128_BLOCK_LEN, RIJNDAEL128_BLOCK_LEN, 8, 32,
233 sizeof(rijndael_ctx),
240 struct enc_xform enc_xform_aes_xts = {
241 CRYPTO_AES_XTS, "AES-XTS",
242 AES_XTS_BLOCK_LEN, AES_XTS_IV_LEN, 32, 64,
243 sizeof(struct aes_xts_ctx),
250 struct enc_xform enc_xform_aes_ctr = {
251 CRYPTO_AES_CTR, "AES-CTR",
252 AESCTR_BLOCK_LEN, AESCTR_IV_LEN, 16+4, 32+4,
253 sizeof(struct aes_ctr_ctx),
260 struct enc_xform enc_xform_aes_gcm = {
261 CRYPTO_AES_GCM_16, "AES-GCM",
262 AESGCM_BLOCK_LEN, AESGCM_IV_LEN, 16+4, 32+4,
263 sizeof(struct aes_ctr_ctx),
270 struct enc_xform enc_xform_aes_gmac = {
271 CRYPTO_AES_GMAC, "AES-GMAC",
272 AESGMAC_BLOCK_LEN, AESGMAC_IV_LEN, 16+4, 32+4,
273 0, /* NB: no context */
280 struct enc_xform enc_xform_arc4 = {
283 0, /* NB: no context */
290 struct enc_xform enc_xform_camellia = {
291 CRYPTO_CAMELLIA_CBC, "Camellia",
292 CAMELLIA_BLOCK_LEN, CAMELLIA_BLOCK_LEN, 8, 32,
293 sizeof(camellia_ctx),
300 struct enc_xform enc_xform_twofish = {
301 CRYPTO_TWOFISH_CBC, "Twofish",
302 TWOFISH_BLOCK_LEN, TWOFISH_BLOCK_LEN, 8, 32,
310 struct enc_xform enc_xform_serpent = {
311 CRYPTO_SERPENT_CBC, "Serpent",
312 SERPENT_BLOCK_LEN, SERPENT_BLOCK_LEN, 8, 32,
320 struct enc_xform enc_xform_twofish_xts = {
321 CRYPTO_TWOFISH_XTS, "TWOFISH-XTS",
322 TWOFISH_XTS_BLOCK_LEN, TWOFISH_XTS_IV_LEN, 32, 64,
323 sizeof(struct twofish_xts_ctx),
330 struct enc_xform enc_xform_serpent_xts = {
331 CRYPTO_SERPENT_XTS, "SERPENT-XTS",
332 SERPENT_XTS_BLOCK_LEN, SERPENT_XTS_IV_LEN, 32, 64,
333 sizeof(struct serpent_xts_ctx),
341 /* Authentication instances */
342 struct auth_hash auth_hash_null = {
343 CRYPTO_NULL_HMAC, "NULL-HMAC",
344 0, NULL_HASH_LEN, NULL_HMAC_BLOCK_LEN,
345 sizeof(int), /* NB: context isn't used */
346 null_init, NULL, NULL, null_update, null_final
349 struct auth_hash auth_hash_hmac_md5 = {
350 CRYPTO_MD5_HMAC, "HMAC-MD5",
351 16, MD5_HASH_LEN, MD5_HMAC_BLOCK_LEN, sizeof(MD5_CTX),
352 (void (*) (void *)) MD5Init, NULL, NULL,
354 (void (*) (u_int8_t *, void *)) MD5Final
357 struct auth_hash auth_hash_hmac_sha1 = {
358 CRYPTO_SHA1_HMAC, "HMAC-SHA1",
359 20, SHA1_HASH_LEN, SHA1_HMAC_BLOCK_LEN, sizeof(SHA1_CTX),
360 SHA1Init_int, NULL, NULL,
361 SHA1Update_int, SHA1Final_int
364 struct auth_hash auth_hash_hmac_ripemd_160 = {
365 CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
366 20, RIPEMD160_HASH_LEN, RIPEMD160_HMAC_BLOCK_LEN, sizeof(RMD160_CTX),
367 (void (*)(void *)) RMD160Init, NULL, NULL,
369 (void (*)(u_int8_t *, void *)) RMD160Final
372 struct auth_hash auth_hash_key_md5 = {
373 CRYPTO_MD5_KPDK, "Keyed MD5",
374 0, MD5_KPDK_HASH_LEN, 0, sizeof(MD5_CTX),
375 (void (*)(void *)) MD5Init, NULL, NULL,
377 (void (*)(u_int8_t *, void *)) MD5Final
380 struct auth_hash auth_hash_key_sha1 = {
381 CRYPTO_SHA1_KPDK, "Keyed SHA1",
382 0, SHA1_KPDK_HASH_LEN, 0, sizeof(SHA1_CTX),
383 SHA1Init_int, NULL, NULL,
384 SHA1Update_int, SHA1Final_int
387 struct auth_hash auth_hash_hmac_sha2_256 = {
388 CRYPTO_SHA2_256_HMAC, "HMAC-SHA2-256",
389 32, SHA2_256_HASH_LEN, SHA2_256_HMAC_BLOCK_LEN, sizeof(SHA256_CTX),
390 (void (*)(void *)) SHA256_Init, NULL, NULL,
392 (void (*)(u_int8_t *, void *)) SHA256_Final
395 struct auth_hash auth_hash_hmac_sha2_384 = {
396 CRYPTO_SHA2_384_HMAC, "HMAC-SHA2-384",
397 48, SHA2_384_HASH_LEN, SHA2_384_HMAC_BLOCK_LEN, sizeof(SHA384_CTX),
398 (void (*)(void *)) SHA384_Init, NULL, NULL,
400 (void (*)(u_int8_t *, void *)) SHA384_Final
403 struct auth_hash auth_hash_hmac_sha2_512 = {
404 CRYPTO_SHA2_512_HMAC, "HMAC-SHA2-512",
405 64, SHA2_512_HASH_LEN, SHA2_512_HMAC_BLOCK_LEN, sizeof(SHA512_CTX),
406 (void (*)(void *)) SHA512_Init, NULL, NULL,
408 (void (*)(u_int8_t *, void *)) SHA512_Final
411 struct auth_hash auth_hash_gmac_aes_128 = {
412 CRYPTO_AES_128_GMAC, "GMAC-AES-128",
413 16+4, 16, 16, sizeof(AES_GMAC_CTX),
414 (void (*)(void *)) AES_GMAC_Init,
415 (int (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
416 (void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
417 (int (*)(void *, u_int8_t *, u_int16_t)) AES_GMAC_Update,
418 (void (*)(u_int8_t *, void *)) AES_GMAC_Final
421 struct auth_hash auth_hash_gmac_aes_192 = {
422 CRYPTO_AES_192_GMAC, "GMAC-AES-192",
423 24+4, 16, 16, sizeof(AES_GMAC_CTX),
424 (void (*)(void *)) AES_GMAC_Init,
425 (int (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
426 (void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
427 (int (*)(void *, u_int8_t *, u_int16_t)) AES_GMAC_Update,
428 (void (*)(u_int8_t *, void *)) AES_GMAC_Final
431 struct auth_hash auth_hash_gmac_aes_256 = {
432 CRYPTO_AES_256_GMAC, "GMAC-AES-256",
433 32+4, 16, 16, sizeof(AES_GMAC_CTX),
434 (void (*)(void *)) AES_GMAC_Init,
435 (int (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
436 (void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
437 (int (*)(void *, u_int8_t *, u_int16_t)) AES_GMAC_Update,
438 (void (*)(u_int8_t *, void *)) AES_GMAC_Final
441 /* Compression instance */
442 struct comp_algo comp_algo_deflate = {
443 CRYPTO_DEFLATE_COMP, "Deflate",
444 90, deflate_compress,
449 * Encryption wrapper routines.
453 null_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
458 null_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
463 null_setkey(void *sched, u_int8_t *key, int len)
469 des1_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
471 des_cblock *cb = (des_cblock *) blk;
472 des_key_schedule *p = (des_key_schedule *) key;
474 des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
478 des1_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
480 des_cblock *cb = (des_cblock *) blk;
481 des_key_schedule *p = (des_key_schedule *) key;
483 des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
487 des1_setkey(void *sched, u_int8_t *key, int len)
489 return des_set_key((des_cblock *)key, sched);
493 des3_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
495 des_cblock *cb = (des_cblock *) blk;
496 des_key_schedule *p = (des_key_schedule *) key;
498 des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
502 des3_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
504 des_cblock *cb = (des_cblock *) blk;
505 des_key_schedule *p = (des_key_schedule *) key;
507 des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
511 des3_setkey(void *sched, u_int8_t *key, int len)
516 if (des_set_key((des_cblock *)(key + 0), p[0]) < 0 ||
517 des_set_key((des_cblock *)(key + 8), p[1]) < 0 ||
518 des_set_key((des_cblock *)(key + 16), p[2]) < 0)
525 blf_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
529 memcpy(t, blk, sizeof (t));
532 /* NB: BF_encrypt expects the block in host order! */
533 BF_encrypt(t, (BF_KEY *) key);
536 memcpy(blk, t, sizeof (t));
540 blf_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
544 memcpy(t, blk, sizeof (t));
547 /* NB: BF_decrypt expects the block in host order! */
548 BF_decrypt(t, (BF_KEY *) key);
551 memcpy(blk, t, sizeof (t));
555 blf_setkey(void *sched, u_int8_t *key, int len)
557 BF_set_key(sched, len, key);
562 cast5_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
564 cast_encrypt((cast_key *) key, blk, blk);
568 cast5_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
570 cast_decrypt((cast_key *) key, blk, blk);
574 cast5_setkey(void *sched, u_int8_t *key, int len)
576 cast_setkey(sched, key, len);
581 skipjack_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
583 skipjack_forwards(blk, blk, (u_int8_t **) key);
587 skipjack_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
589 skipjack_backwards(blk, blk, (u_int8_t **) key);
593 skipjack_setkey(void *sched, u_int8_t *key, int len)
595 u_int8_t **key_tables = sched;
596 u_int8_t *table = (u_int8_t *)&key_tables[10];
599 for (k = 0; k < 10; k++) {
600 key_tables[k] = table;
603 subkey_table_gen(key, sched);
609 rijndael128_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
611 rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
615 rijndael128_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
617 rijndael_decrypt(((rijndael_ctx *) key), (u_char *) blk,
622 rijndael128_setkey(void *sched, u_int8_t *key, int len)
624 if (len != 16 && len != 24 && len != 32)
627 rijndael_set_key(sched, (u_char *) key, len * 8);
633 aes_xts_reinit(caddr_t key, u_int8_t *iv)
635 struct aes_xts_ctx *ctx = (struct aes_xts_ctx *)key;
643 * Prepare tweak as E_k2(IV). IV is specified as LE representation
644 * of a 64-bit block number which we allow to be passed in directly.
646 /* XXX: possibly use htole64? */
648 /* Last 64 bits of IV are always zero */
649 bzero(iv + AES_XTS_IV_LEN, AES_XTS_IV_LEN);
651 rijndael_encrypt(&ctx->key2, iv, iv);
655 aes_xts_crypt(struct aes_xts_ctx *ctx, u_int8_t *data, u_int8_t *iv,
658 u_int8_t block[AES_XTS_BLOCK_LEN];
659 u_int i, carry_in, carry_out;
661 for (i = 0; i < AES_XTS_BLOCK_LEN; i++)
662 block[i] = data[i] ^ iv[i];
665 rijndael_encrypt(&ctx->key1, block, data);
667 rijndael_decrypt(&ctx->key1, block, data);
669 for (i = 0; i < AES_XTS_BLOCK_LEN; i++)
672 /* Exponentiate tweak */
674 for (i = 0; i < AES_XTS_BLOCK_LEN; i++) {
675 carry_out = iv[i] & 0x80;
676 iv[i] = (iv[i] << 1) | (carry_in ? 1 : 0);
677 carry_in = carry_out;
680 iv[0] ^= AES_XTS_ALPHA;
681 bzero(block, sizeof(block));
685 aes_xts_encrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
687 aes_xts_crypt((struct aes_xts_ctx *)key, data, iv, 1);
691 aes_xts_decrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
693 aes_xts_crypt((struct aes_xts_ctx *)key, data, iv, 0);
697 aes_xts_setkey(void *sched, u_int8_t *key, int len)
699 struct aes_xts_ctx *ctx;
701 if (len != 32 && len != 64)
705 rijndael_set_key(&ctx->key1, key, len * 4);
706 rijndael_set_key(&ctx->key2, key + (len / 2), len * 4);
712 aes_ctr_reinit(caddr_t key, u_int8_t *iv)
714 struct aes_ctr_ctx *ctx;
716 ctx = (struct aes_ctr_ctx *)key;
717 bcopy(iv, iv + AESCTR_NONCESIZE, AESCTR_IV_LEN);
718 bcopy(ctx->ac_block, iv, AESCTR_NONCESIZE);
721 bzero(iv + AESCTR_NONCESIZE + AESCTR_IV_LEN, 4);
725 aes_ctr_crypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
727 struct aes_ctr_ctx *ctx;
728 u_int8_t keystream[AESCTR_BLOCK_LEN];
731 ctx = (struct aes_ctr_ctx *)key;
732 /* increment counter */
733 for (i = AESCTR_BLOCK_LEN - 1;
734 i >= AESCTR_NONCESIZE + AESCTR_IV_LEN; i--)
735 if (++iv[i]) /* continue on overflow */
737 rijndaelEncrypt(ctx->ac_ek, ctx->ac_nr, iv, keystream);
738 for (i = 0; i < AESCTR_BLOCK_LEN; i++)
739 data[i] ^= keystream[i];
740 bzero(keystream, sizeof(keystream));
744 aes_ctr_setkey(void *sched, u_int8_t *key, int len)
746 struct aes_ctr_ctx *ctx;
748 len -= AESCTR_NONCESIZE;
751 if (!(len == 16 || len == 24 || len == 32))
752 return -1; /* invalid key bits */
755 ctx->ac_nr = rijndaelKeySetupEnc(ctx->ac_ek, key, len * 8);
756 if (ctx->ac_nr == 0) {
757 bzero(ctx, sizeof(struct aes_ctr_ctx));
761 bcopy(key + len, ctx->ac_block, AESCTR_NONCESIZE);
767 aes_gcm_reinit(caddr_t key, u_int8_t *iv)
769 struct aes_ctr_ctx *ctx;
771 ctx = (struct aes_ctr_ctx *)key;
772 bcopy(iv, ctx->ac_block + AESCTR_NONCESIZE, AESCTR_IV_LEN);
775 bzero(ctx->ac_block + AESCTR_NONCESIZE + AESCTR_IV_LEN, 4);
776 ctx->ac_block[AESCTR_BLOCK_LEN - 1] = 1; /* GCM starts with 1 */
780 cml_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
782 camellia_encrypt((camellia_ctx *) key, (u_char *) blk, (u_char *) blk);
786 cml_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
788 camellia_decrypt(((camellia_ctx *) key), (u_char *) blk,
793 cml_setkey(void *sched, u_int8_t *key, int len)
795 if (len != 16 && len != 24 && len != 32)
798 camellia_set_key(sched, key, len * 8);
804 twofish128_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
806 twofish_encrypt((twofish_ctx *) key, blk, blk);
810 twofish128_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
812 twofish_decrypt(((twofish_ctx *) key), blk, blk);
816 twofish128_setkey(void *sched, u_int8_t *key, int len)
818 if (len != 16 && len != 24 && len != 32)
821 twofish_set_key(sched, key, len * 8);
827 serpent128_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
829 serpent_encrypt((serpent_ctx *) key, blk, blk);
833 serpent128_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
835 serpent_decrypt(((serpent_ctx *) key), blk, blk);
839 serpent128_setkey(void *sched, u_int8_t *key, int len)
841 if (len != 16 && len != 24 && len != 32)
844 serpent_set_key(sched, key, len * 8);
851 twofish_xts_reinit(caddr_t key, u_int8_t *iv)
853 struct twofish_xts_ctx *ctx = (struct twofish_xts_ctx *)key;
860 * Prepare tweak as E_k2(IV). IV is specified as LE representation
861 * of a 64-bit block number which we allow to be passed in directly.
863 /* XXX: possibly use htole64? */
865 /* Last 64 bits of IV are always zero */
866 bzero(iv + TWOFISH_XTS_IV_LEN, TWOFISH_XTS_IV_LEN);
868 twofish_encrypt(&ctx->key2, iv, iv);
872 twofish_xts_crypt(struct twofish_xts_ctx *ctx, u_int8_t *data, u_int8_t *iv,
875 u_int8_t block[TWOFISH_XTS_BLOCK_LEN];
876 u_int i, carry_in, carry_out;
878 for (i = 0; i < TWOFISH_XTS_BLOCK_LEN; i++)
879 block[i] = data[i] ^ iv[i];
882 twofish_encrypt(&ctx->key1, block, data);
884 twofish_decrypt(&ctx->key1, block, data);
886 for (i = 0; i < TWOFISH_XTS_BLOCK_LEN; i++)
889 /* Exponentiate tweak */
891 for (i = 0; i < TWOFISH_XTS_BLOCK_LEN; i++) {
892 carry_out = iv[i] & 0x80;
893 iv[i] = (iv[i] << 1) | (carry_in ? 1 : 0);
894 carry_in = carry_out;
897 iv[0] ^= AES_XTS_ALPHA;
898 bzero(block, sizeof(block));
902 twofish_xts_encrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
904 twofish_xts_crypt((struct twofish_xts_ctx *)key, data, iv, 1);
908 twofish_xts_decrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
910 twofish_xts_crypt((struct twofish_xts_ctx *)key, data, iv, 0);
914 twofish_xts_setkey(void *sched, u_int8_t *key, int len)
916 struct twofish_xts_ctx *ctx;
918 if (len != 32 && len != 64)
922 twofish_set_key(&ctx->key1, key, len * 4);
923 twofish_set_key(&ctx->key2, key + (len / 2), len * 4);
930 serpent_xts_reinit(caddr_t key, u_int8_t *iv)
932 struct serpent_xts_ctx *ctx = (struct serpent_xts_ctx *)key;
940 * Prepare tweak as E_k2(IV). IV is specified as LE representation
941 * of a 64-bit block number which we allow to be passed in directly.
943 /* XXX: possibly use htole64? */
945 /* Last 64 bits of IV are always zero */
946 bzero(iv + SERPENT_XTS_IV_LEN, SERPENT_XTS_IV_LEN);
948 serpent_encrypt(&ctx->key2, iv, iv);
952 serpent_xts_crypt(struct serpent_xts_ctx *ctx, u_int8_t *data, u_int8_t *iv,
955 u_int8_t block[SERPENT_XTS_BLOCK_LEN];
956 u_int i, carry_in, carry_out;
958 for (i = 0; i < SERPENT_XTS_BLOCK_LEN; i++)
959 block[i] = data[i] ^ iv[i];
962 serpent_encrypt(&ctx->key1, block, data);
964 serpent_decrypt(&ctx->key1, block, data);
966 for (i = 0; i < SERPENT_XTS_BLOCK_LEN; i++)
969 /* Exponentiate tweak */
971 for (i = 0; i < SERPENT_XTS_BLOCK_LEN; i++) {
972 carry_out = iv[i] & 0x80;
973 iv[i] = (iv[i] << 1) | (carry_in ? 1 : 0);
974 carry_in = carry_out;
977 iv[0] ^= AES_XTS_ALPHA;
978 bzero(block, sizeof(block));
982 serpent_xts_encrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
984 serpent_xts_crypt((struct serpent_xts_ctx *)key, data, iv, 1);
988 serpent_xts_decrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
990 serpent_xts_crypt((struct serpent_xts_ctx *)key, data, iv, 0);
994 serpent_xts_setkey(void *sched, u_int8_t *key, int len)
996 struct serpent_xts_ctx *ctx;
998 if (len != 32 && len != 64)
1002 serpent_set_key(&ctx->key1, key, len * 4);
1003 serpent_set_key(&ctx->key2, key + (len / 2), len * 4);
1014 null_init(void *ctx)
1019 null_update(void *ctx, u_int8_t *buf, u_int16_t len)
1025 null_final(u_int8_t *buf, void *ctx)
1032 RMD160Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
1034 RMD160Update(ctx, buf, len);
1039 MD5Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
1041 MD5Update(ctx, buf, len);
1046 SHA1Init_int(void *ctx)
1052 SHA1Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
1054 SHA1Update(ctx, buf, len);
1059 SHA1Final_int(u_int8_t *blk, void *ctx)
1061 SHA1Final(blk, ctx);
1065 SHA256Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
1067 SHA256_Update(ctx, buf, len);
1072 SHA384Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
1074 SHA384_Update(ctx, buf, len);
1079 SHA512Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
1081 SHA512_Update(ctx, buf, len);
1090 deflate_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
1092 return deflate_global(data, size, 0, out);
1096 deflate_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out)
1098 return deflate_global(data, size, 1, out);