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),
5 * Niels Provos (provos@physnet.uni-hamburg.de) and
6 * Damien Miller (djm@mindrot.org).
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 * AES XTS implementation in 2008 by Damien Miller
21 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
22 * Angelos D. Keromytis and Niels Provos.
24 * Copyright (C) 2001, Angelos D. Keromytis.
26 * Copyright (C) 2008, Damien Miller
27 * Copyright (c) 2014 The FreeBSD Foundation
28 * All rights reserved.
30 * Portions of this software were developed by John-Mark Gurney
31 * under sponsorship of the FreeBSD Foundation and
32 * Rubicon Communications, LLC (Netgate).
34 * Permission to use, copy, and modify this software with or without fee
35 * is hereby granted, provided that this entire notice is included in
36 * all copies of any software which is or includes a copy or
37 * modification of this software.
38 * You may use this code under the GNU public license if you so wish. Please
39 * contribute changes back to the authors under this freer than GPL license
40 * so that we may further the use of strong encryption without limitations to
43 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
44 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
45 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
46 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/malloc.h>
56 #include <sys/sysctl.h>
57 #include <sys/errno.h>
59 #include <sys/kernel.h>
60 #include <machine/cpu.h>
62 #include <crypto/blowfish/blowfish.h>
63 #include <crypto/des/des.h>
64 #include <crypto/rijndael/rijndael.h>
65 #include <crypto/camellia/camellia.h>
66 #include <crypto/sha1.h>
68 #include <opencrypto/cast.h>
69 #include <opencrypto/deflate.h>
70 #include <opencrypto/rmd160.h>
71 #include <opencrypto/skipjack.h>
75 #include <opencrypto/cryptodev.h>
76 #include <opencrypto/xform.h>
78 static int null_setkey(u_int8_t **, u_int8_t *, int);
79 static int des1_setkey(u_int8_t **, u_int8_t *, int);
80 static int des3_setkey(u_int8_t **, u_int8_t *, int);
81 static int blf_setkey(u_int8_t **, u_int8_t *, int);
82 static int cast5_setkey(u_int8_t **, u_int8_t *, int);
83 static int skipjack_setkey(u_int8_t **, u_int8_t *, int);
84 static int rijndael128_setkey(u_int8_t **, u_int8_t *, int);
85 static int aes_icm_setkey(u_int8_t **, u_int8_t *, int);
86 static int aes_xts_setkey(u_int8_t **, u_int8_t *, int);
87 static int cml_setkey(u_int8_t **, u_int8_t *, int);
89 static void null_encrypt(caddr_t, u_int8_t *);
90 static void des1_encrypt(caddr_t, u_int8_t *);
91 static void des3_encrypt(caddr_t, u_int8_t *);
92 static void blf_encrypt(caddr_t, u_int8_t *);
93 static void cast5_encrypt(caddr_t, u_int8_t *);
94 static void skipjack_encrypt(caddr_t, u_int8_t *);
95 static void rijndael128_encrypt(caddr_t, u_int8_t *);
96 static void aes_xts_encrypt(caddr_t, u_int8_t *);
97 static void cml_encrypt(caddr_t, u_int8_t *);
99 static void null_decrypt(caddr_t, u_int8_t *);
100 static void des1_decrypt(caddr_t, u_int8_t *);
101 static void des3_decrypt(caddr_t, u_int8_t *);
102 static void blf_decrypt(caddr_t, u_int8_t *);
103 static void cast5_decrypt(caddr_t, u_int8_t *);
104 static void skipjack_decrypt(caddr_t, u_int8_t *);
105 static void rijndael128_decrypt(caddr_t, u_int8_t *);
106 static void aes_xts_decrypt(caddr_t, u_int8_t *);
107 static void cml_decrypt(caddr_t, u_int8_t *);
109 static void aes_icm_crypt(caddr_t, u_int8_t *);
111 static void null_zerokey(u_int8_t **);
112 static void des1_zerokey(u_int8_t **);
113 static void des3_zerokey(u_int8_t **);
114 static void blf_zerokey(u_int8_t **);
115 static void cast5_zerokey(u_int8_t **);
116 static void skipjack_zerokey(u_int8_t **);
117 static void rijndael128_zerokey(u_int8_t **);
118 static void aes_icm_zerokey(u_int8_t **);
119 static void aes_xts_zerokey(u_int8_t **);
120 static void cml_zerokey(u_int8_t **);
122 static void aes_icm_reinit(caddr_t, u_int8_t *);
123 static void aes_xts_reinit(caddr_t, u_int8_t *);
124 static void aes_gcm_reinit(caddr_t, u_int8_t *);
126 static void null_init(void *);
127 static void null_reinit(void *ctx, const u_int8_t *buf, u_int16_t len);
128 static int null_update(void *, const u_int8_t *, u_int16_t);
129 static void null_final(u_int8_t *, void *);
130 static int MD5Update_int(void *, const u_int8_t *, u_int16_t);
131 static void SHA1Init_int(void *);
132 static int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
133 static void SHA1Final_int(u_int8_t *, void *);
134 static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
135 static int SHA256Update_int(void *, const u_int8_t *, u_int16_t);
136 static int SHA384Update_int(void *, const u_int8_t *, u_int16_t);
137 static int SHA512Update_int(void *, const u_int8_t *, u_int16_t);
139 static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
140 static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
142 #define AESICM_BLOCKSIZE 16
145 u_int32_t ac_ek[4*(RIJNDAEL_MAXNR + 1)];
146 /* ac_block is initalized to IV */
147 u_int8_t ac_block[AESICM_BLOCKSIZE];
151 MALLOC_DEFINE(M_XDATA, "xform", "xform data buffers");
153 /* Encryption instances */
154 struct enc_xform enc_xform_null = {
155 CRYPTO_NULL_CBC, "NULL",
156 /* NB: blocksize of 4 is to generate a properly aligned ESP header */
157 NULL_BLOCK_LEN, NULL_BLOCK_LEN, 0, 256, /* 2048 bits, max key */
165 struct enc_xform enc_xform_des = {
166 CRYPTO_DES_CBC, "DES",
167 DES_BLOCK_LEN, DES_BLOCK_LEN, 8, 8,
175 struct enc_xform enc_xform_3des = {
176 CRYPTO_3DES_CBC, "3DES",
177 DES3_BLOCK_LEN, DES3_BLOCK_LEN, 24, 24,
185 struct enc_xform enc_xform_blf = {
186 CRYPTO_BLF_CBC, "Blowfish",
187 BLOWFISH_BLOCK_LEN, BLOWFISH_BLOCK_LEN, 5, 56 /* 448 bits, max key */,
195 struct enc_xform enc_xform_cast5 = {
196 CRYPTO_CAST_CBC, "CAST-128",
197 CAST128_BLOCK_LEN, CAST128_BLOCK_LEN, 5, 16,
205 struct enc_xform enc_xform_skipjack = {
206 CRYPTO_SKIPJACK_CBC, "Skipjack",
207 SKIPJACK_BLOCK_LEN, SKIPJACK_BLOCK_LEN, 10, 10,
209 skipjack_decrypt, skipjack_setkey,
214 struct enc_xform enc_xform_rijndael128 = {
215 CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
216 RIJNDAEL128_BLOCK_LEN, RIJNDAEL128_BLOCK_LEN, 16, 32,
224 struct enc_xform enc_xform_aes_icm = {
225 CRYPTO_AES_ICM, "AES-ICM",
226 RIJNDAEL128_BLOCK_LEN, RIJNDAEL128_BLOCK_LEN, 16, 32,
234 struct enc_xform enc_xform_aes_nist_gcm = {
235 CRYPTO_AES_NIST_GCM_16, "AES-GCM",
244 struct enc_xform enc_xform_aes_nist_gmac = {
245 CRYPTO_AES_NIST_GMAC, "AES-GMAC",
254 struct enc_xform enc_xform_aes_xts = {
255 CRYPTO_AES_XTS, "AES-XTS",
256 RIJNDAEL128_BLOCK_LEN, 8, 32, 64,
264 struct enc_xform enc_xform_arc4 = {
274 struct enc_xform enc_xform_camellia = {
275 CRYPTO_CAMELLIA_CBC, "Camellia",
276 CAMELLIA_BLOCK_LEN, CAMELLIA_BLOCK_LEN, 8, 32,
284 /* Authentication instances */
285 struct auth_hash auth_hash_null = { /* NB: context isn't used */
286 CRYPTO_NULL_HMAC, "NULL-HMAC",
287 0, NULL_HASH_LEN, sizeof(int), NULL_HMAC_BLOCK_LEN,
288 null_init, null_reinit, null_reinit, null_update, null_final
291 struct auth_hash auth_hash_hmac_md5 = {
292 CRYPTO_MD5_HMAC, "HMAC-MD5",
293 16, MD5_HASH_LEN, sizeof(MD5_CTX), MD5_HMAC_BLOCK_LEN,
294 (void (*) (void *)) MD5Init, NULL, NULL, MD5Update_int,
295 (void (*) (u_int8_t *, void *)) MD5Final
298 struct auth_hash auth_hash_hmac_sha1 = {
299 CRYPTO_SHA1_HMAC, "HMAC-SHA1",
300 20, SHA1_HASH_LEN, sizeof(SHA1_CTX), SHA1_HMAC_BLOCK_LEN,
301 SHA1Init_int, NULL, NULL, SHA1Update_int, SHA1Final_int
304 struct auth_hash auth_hash_hmac_ripemd_160 = {
305 CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
306 20, RIPEMD160_HASH_LEN, sizeof(RMD160_CTX), RIPEMD160_HMAC_BLOCK_LEN,
307 (void (*)(void *)) RMD160Init, NULL, NULL, RMD160Update_int,
308 (void (*)(u_int8_t *, void *)) RMD160Final
311 struct auth_hash auth_hash_key_md5 = {
312 CRYPTO_MD5_KPDK, "Keyed MD5",
313 0, MD5_KPDK_HASH_LEN, sizeof(MD5_CTX), 0,
314 (void (*)(void *)) MD5Init, NULL, NULL, MD5Update_int,
315 (void (*)(u_int8_t *, void *)) MD5Final
318 struct auth_hash auth_hash_key_sha1 = {
319 CRYPTO_SHA1_KPDK, "Keyed SHA1",
320 0, SHA1_KPDK_HASH_LEN, sizeof(SHA1_CTX), 0,
321 SHA1Init_int, NULL, NULL, SHA1Update_int, SHA1Final_int
324 struct auth_hash auth_hash_hmac_sha2_256 = {
325 CRYPTO_SHA2_256_HMAC, "HMAC-SHA2-256",
326 32, SHA2_256_HASH_LEN, sizeof(SHA256_CTX), SHA2_256_HMAC_BLOCK_LEN,
327 (void (*)(void *)) SHA256_Init, NULL, NULL, SHA256Update_int,
328 (void (*)(u_int8_t *, void *)) SHA256_Final
331 struct auth_hash auth_hash_hmac_sha2_384 = {
332 CRYPTO_SHA2_384_HMAC, "HMAC-SHA2-384",
333 48, SHA2_384_HASH_LEN, sizeof(SHA384_CTX), SHA2_384_HMAC_BLOCK_LEN,
334 (void (*)(void *)) SHA384_Init, NULL, NULL, SHA384Update_int,
335 (void (*)(u_int8_t *, void *)) SHA384_Final
338 struct auth_hash auth_hash_hmac_sha2_512 = {
339 CRYPTO_SHA2_512_HMAC, "HMAC-SHA2-512",
340 64, SHA2_512_HASH_LEN, sizeof(SHA512_CTX), SHA2_512_HMAC_BLOCK_LEN,
341 (void (*)(void *)) SHA512_Init, NULL, NULL, SHA512Update_int,
342 (void (*)(u_int8_t *, void *)) SHA512_Final
345 struct auth_hash auth_hash_nist_gmac_aes_128 = {
346 CRYPTO_AES_128_NIST_GMAC, "GMAC-AES-128",
347 16, 16, sizeof(struct aes_gmac_ctx), GMAC_BLOCK_LEN,
348 (void (*)(void *)) AES_GMAC_Init,
349 (void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
350 (void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
351 (int (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Update,
352 (void (*)(u_int8_t *, void *)) AES_GMAC_Final
355 struct auth_hash auth_hash_nist_gmac_aes_192 = {
356 CRYPTO_AES_192_NIST_GMAC, "GMAC-AES-192",
357 24, 16, sizeof(struct aes_gmac_ctx), GMAC_BLOCK_LEN,
358 (void (*)(void *)) AES_GMAC_Init,
359 (void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
360 (void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
361 (int (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Update,
362 (void (*)(u_int8_t *, void *)) AES_GMAC_Final
365 struct auth_hash auth_hash_nist_gmac_aes_256 = {
366 CRYPTO_AES_256_NIST_GMAC, "GMAC-AES-256",
367 32, 16, sizeof(struct aes_gmac_ctx), GMAC_BLOCK_LEN,
368 (void (*)(void *)) AES_GMAC_Init,
369 (void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
370 (void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
371 (int (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Update,
372 (void (*)(u_int8_t *, void *)) AES_GMAC_Final
375 /* Compression instance */
376 struct comp_algo comp_algo_deflate = {
377 CRYPTO_DEFLATE_COMP, "Deflate",
378 90, deflate_compress,
383 * Encryption wrapper routines.
386 null_encrypt(caddr_t key, u_int8_t *blk)
390 null_decrypt(caddr_t key, u_int8_t *blk)
394 null_setkey(u_int8_t **sched, u_int8_t *key, int len)
400 null_zerokey(u_int8_t **sched)
406 des1_encrypt(caddr_t key, u_int8_t *blk)
408 des_cblock *cb = (des_cblock *) blk;
409 des_key_schedule *p = (des_key_schedule *) key;
411 des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
415 des1_decrypt(caddr_t key, u_int8_t *blk)
417 des_cblock *cb = (des_cblock *) blk;
418 des_key_schedule *p = (des_key_schedule *) key;
420 des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
424 des1_setkey(u_int8_t **sched, u_int8_t *key, int len)
429 p = malloc(sizeof (des_key_schedule),
430 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
432 des_set_key((des_cblock *) key, p[0]);
436 *sched = (u_int8_t *) p;
441 des1_zerokey(u_int8_t **sched)
443 bzero(*sched, sizeof (des_key_schedule));
444 free(*sched, M_CRYPTO_DATA);
449 des3_encrypt(caddr_t key, u_int8_t *blk)
451 des_cblock *cb = (des_cblock *) blk;
452 des_key_schedule *p = (des_key_schedule *) key;
454 des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
458 des3_decrypt(caddr_t key, u_int8_t *blk)
460 des_cblock *cb = (des_cblock *) blk;
461 des_key_schedule *p = (des_key_schedule *) key;
463 des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
467 des3_setkey(u_int8_t **sched, u_int8_t *key, int len)
472 p = malloc(3*sizeof (des_key_schedule),
473 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
475 des_set_key((des_cblock *)(key + 0), p[0]);
476 des_set_key((des_cblock *)(key + 8), p[1]);
477 des_set_key((des_cblock *)(key + 16), p[2]);
481 *sched = (u_int8_t *) p;
486 des3_zerokey(u_int8_t **sched)
488 bzero(*sched, 3*sizeof (des_key_schedule));
489 free(*sched, M_CRYPTO_DATA);
494 blf_encrypt(caddr_t key, u_int8_t *blk)
498 memcpy(t, blk, sizeof (t));
501 /* NB: BF_encrypt expects the block in host order! */
502 BF_encrypt(t, (BF_KEY *) key);
505 memcpy(blk, t, sizeof (t));
509 blf_decrypt(caddr_t key, u_int8_t *blk)
513 memcpy(t, blk, sizeof (t));
516 /* NB: BF_decrypt expects the block in host order! */
517 BF_decrypt(t, (BF_KEY *) key);
520 memcpy(blk, t, sizeof (t));
524 blf_setkey(u_int8_t **sched, u_int8_t *key, int len)
528 *sched = malloc(sizeof(BF_KEY),
529 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
530 if (*sched != NULL) {
531 BF_set_key((BF_KEY *) *sched, len, key);
539 blf_zerokey(u_int8_t **sched)
541 bzero(*sched, sizeof(BF_KEY));
542 free(*sched, M_CRYPTO_DATA);
547 cast5_encrypt(caddr_t key, u_int8_t *blk)
549 cast_encrypt((cast_key *) key, blk, blk);
553 cast5_decrypt(caddr_t key, u_int8_t *blk)
555 cast_decrypt((cast_key *) key, blk, blk);
559 cast5_setkey(u_int8_t **sched, u_int8_t *key, int len)
563 *sched = malloc(sizeof(cast_key), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
564 if (*sched != NULL) {
565 cast_setkey((cast_key *)*sched, key, len);
573 cast5_zerokey(u_int8_t **sched)
575 bzero(*sched, sizeof(cast_key));
576 free(*sched, M_CRYPTO_DATA);
581 skipjack_encrypt(caddr_t key, u_int8_t *blk)
583 skipjack_forwards(blk, blk, (u_int8_t **) key);
587 skipjack_decrypt(caddr_t key, u_int8_t *blk)
589 skipjack_backwards(blk, blk, (u_int8_t **) key);
593 skipjack_setkey(u_int8_t **sched, u_int8_t *key, int len)
597 /* NB: allocate all the memory that's needed at once */
598 *sched = malloc(10 * (sizeof(u_int8_t *) + 0x100),
599 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
600 if (*sched != NULL) {
601 u_int8_t** key_tables = (u_int8_t**) *sched;
602 u_int8_t* table = (u_int8_t*) &key_tables[10];
605 for (k = 0; k < 10; k++) {
606 key_tables[k] = table;
609 subkey_table_gen(key, (u_int8_t **) *sched);
617 skipjack_zerokey(u_int8_t **sched)
619 bzero(*sched, 10 * (sizeof(u_int8_t *) + 0x100));
620 free(*sched, M_CRYPTO_DATA);
625 rijndael128_encrypt(caddr_t key, u_int8_t *blk)
627 rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
631 rijndael128_decrypt(caddr_t key, u_int8_t *blk)
633 rijndael_decrypt(((rijndael_ctx *) key), (u_char *) blk,
638 rijndael128_setkey(u_int8_t **sched, u_int8_t *key, int len)
642 if (len != 16 && len != 24 && len != 32)
644 *sched = malloc(sizeof(rijndael_ctx), M_CRYPTO_DATA,
646 if (*sched != NULL) {
647 rijndael_set_key((rijndael_ctx *) *sched, (u_char *) key,
656 rijndael128_zerokey(u_int8_t **sched)
658 bzero(*sched, sizeof(rijndael_ctx));
659 free(*sched, M_CRYPTO_DATA);
664 aes_icm_reinit(caddr_t key, u_int8_t *iv)
666 struct aes_icm_ctx *ctx;
668 ctx = (struct aes_icm_ctx *)key;
669 bcopy(iv, ctx->ac_block, AESICM_BLOCKSIZE);
673 aes_gcm_reinit(caddr_t key, u_int8_t *iv)
675 struct aes_icm_ctx *ctx;
677 aes_icm_reinit(key, iv);
679 ctx = (struct aes_icm_ctx *)key;
680 /* GCM starts with 2 as counter 1 is used for final xor of tag. */
681 bzero(&ctx->ac_block[AESICM_BLOCKSIZE - 4], 4);
682 ctx->ac_block[AESICM_BLOCKSIZE - 1] = 2;
686 aes_icm_crypt(caddr_t key, u_int8_t *data)
688 struct aes_icm_ctx *ctx;
689 u_int8_t keystream[AESICM_BLOCKSIZE];
692 ctx = (struct aes_icm_ctx *)key;
693 rijndaelEncrypt(ctx->ac_ek, ctx->ac_nr, ctx->ac_block, keystream);
694 for (i = 0; i < AESICM_BLOCKSIZE; i++)
695 data[i] ^= keystream[i];
696 explicit_bzero(keystream, sizeof(keystream));
698 /* increment counter */
699 for (i = AESICM_BLOCKSIZE - 1;
701 if (++ctx->ac_block[i]) /* continue on overflow */
706 aes_icm_setkey(u_int8_t **sched, u_int8_t *key, int len)
708 struct aes_icm_ctx *ctx;
710 *sched = malloc(sizeof(struct aes_icm_ctx), M_CRYPTO_DATA,
715 ctx = (struct aes_icm_ctx *)*sched;
716 ctx->ac_nr = rijndaelKeySetupEnc(ctx->ac_ek, (u_char *)key, len * 8);
723 aes_icm_zerokey(u_int8_t **sched)
726 bzero(*sched, sizeof(struct aes_icm_ctx));
727 free(*sched, M_CRYPTO_DATA);
731 #define AES_XTS_BLOCKSIZE 16
732 #define AES_XTS_IVSIZE 8
733 #define AES_XTS_ALPHA 0x87 /* GF(2^128) generator polynomial */
738 u_int8_t tweak[AES_XTS_BLOCKSIZE];
742 aes_xts_reinit(caddr_t key, u_int8_t *iv)
744 struct aes_xts_ctx *ctx = (struct aes_xts_ctx *)key;
749 * Prepare tweak as E_k2(IV). IV is specified as LE representation
750 * of a 64-bit block number which we allow to be passed in directly.
752 bcopy(iv, &blocknum, AES_XTS_IVSIZE);
753 for (i = 0; i < AES_XTS_IVSIZE; i++) {
754 ctx->tweak[i] = blocknum & 0xff;
757 /* Last 64 bits of IV are always zero */
758 bzero(ctx->tweak + AES_XTS_IVSIZE, AES_XTS_IVSIZE);
760 rijndael_encrypt(&ctx->key2, ctx->tweak, ctx->tweak);
764 aes_xts_crypt(struct aes_xts_ctx *ctx, u_int8_t *data, u_int do_encrypt)
766 u_int8_t block[AES_XTS_BLOCKSIZE];
767 u_int i, carry_in, carry_out;
769 for (i = 0; i < AES_XTS_BLOCKSIZE; i++)
770 block[i] = data[i] ^ ctx->tweak[i];
773 rijndael_encrypt(&ctx->key1, block, data);
775 rijndael_decrypt(&ctx->key1, block, data);
777 for (i = 0; i < AES_XTS_BLOCKSIZE; i++)
778 data[i] ^= ctx->tweak[i];
780 /* Exponentiate tweak */
782 for (i = 0; i < AES_XTS_BLOCKSIZE; i++) {
783 carry_out = ctx->tweak[i] & 0x80;
784 ctx->tweak[i] = (ctx->tweak[i] << 1) | (carry_in ? 1 : 0);
785 carry_in = carry_out;
788 ctx->tweak[0] ^= AES_XTS_ALPHA;
789 bzero(block, sizeof(block));
793 aes_xts_encrypt(caddr_t key, u_int8_t *data)
795 aes_xts_crypt((struct aes_xts_ctx *)key, data, 1);
799 aes_xts_decrypt(caddr_t key, u_int8_t *data)
801 aes_xts_crypt((struct aes_xts_ctx *)key, data, 0);
805 aes_xts_setkey(u_int8_t **sched, u_int8_t *key, int len)
807 struct aes_xts_ctx *ctx;
809 if (len != 32 && len != 64)
812 *sched = malloc(sizeof(struct aes_xts_ctx), M_CRYPTO_DATA,
816 ctx = (struct aes_xts_ctx *)*sched;
818 rijndael_set_key(&ctx->key1, key, len * 4);
819 rijndael_set_key(&ctx->key2, key + (len / 2), len * 4);
825 aes_xts_zerokey(u_int8_t **sched)
827 bzero(*sched, sizeof(struct aes_xts_ctx));
828 free(*sched, M_CRYPTO_DATA);
833 cml_encrypt(caddr_t key, u_int8_t *blk)
835 camellia_encrypt((camellia_ctx *) key, (u_char *) blk, (u_char *) blk);
839 cml_decrypt(caddr_t key, u_int8_t *blk)
841 camellia_decrypt(((camellia_ctx *) key), (u_char *) blk,
846 cml_setkey(u_int8_t **sched, u_int8_t *key, int len)
850 if (len != 16 && len != 24 && len != 32)
852 *sched = malloc(sizeof(camellia_ctx), M_CRYPTO_DATA,
854 if (*sched != NULL) {
855 camellia_set_key((camellia_ctx *) *sched, (u_char *) key,
864 cml_zerokey(u_int8_t **sched)
866 bzero(*sched, sizeof(camellia_ctx));
867 free(*sched, M_CRYPTO_DATA);
881 null_reinit(void *ctx, const u_int8_t *buf, u_int16_t len)
886 null_update(void *ctx, const u_int8_t *buf, u_int16_t len)
892 null_final(u_int8_t *buf, void *ctx)
894 if (buf != (u_int8_t *) 0)
899 RMD160Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
901 RMD160Update(ctx, buf, len);
906 MD5Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
908 MD5Update(ctx, buf, len);
913 SHA1Init_int(void *ctx)
919 SHA1Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
921 SHA1Update(ctx, buf, len);
926 SHA1Final_int(u_int8_t *blk, void *ctx)
932 SHA256Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
934 SHA256_Update(ctx, buf, len);
939 SHA384Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
941 SHA384_Update(ctx, buf, len);
946 SHA512Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
948 SHA512_Update(ctx, buf, len);
957 deflate_compress(data, size, out)
962 return deflate_global(data, size, 0, out);
966 deflate_decompress(data, size, out)
971 return deflate_global(data, size, 1, out);