xref: /openbsd-src/sys/crypto/xform.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: xform.c,v 1.44 2013/08/25 14:26:56 jsing Exp $	*/
2 /*
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).
7  *
8  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9  * in November 1995.
10  *
11  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12  * by Angelos D. Keromytis.
13  *
14  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15  * and Niels Provos.
16  *
17  * Additional features in 1999 by Angelos D. Keromytis.
18  *
19  * AES XTS implementation in 2008 by Damien Miller
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  * Copyright (C) 2008, Damien Miller
27  *
28  * Permission to use, copy, and modify this software with or without fee
29  * is hereby granted, provided that this entire notice is included in
30  * all copies of any software which is or includes a copy or
31  * modification of this software.
32  * You may use this code under the GNU public license if you so wish. Please
33  * contribute changes back to the authors under this freer than GPL license
34  * so that we may further the use of strong encryption without limitations to
35  * all.
36  *
37  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
38  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
39  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
40  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
41  * PURPOSE.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/kernel.h>
49 #include <machine/cpu.h>
50 
51 #include <crypto/md5.h>
52 #include <crypto/sha1.h>
53 #include <crypto/sha2.h>
54 #include <crypto/rmd160.h>
55 #include <crypto/blf.h>
56 #include <crypto/cast.h>
57 #include <crypto/rijndael.h>
58 #include <crypto/cryptodev.h>
59 #include <crypto/xform.h>
60 #include <lib/libz/zlib.h>
61 #include <crypto/gmac.h>
62 
63 extern void des_ecb3_encrypt(caddr_t, caddr_t, caddr_t, caddr_t, caddr_t, int);
64 extern void des_ecb_encrypt(caddr_t, caddr_t, caddr_t, int);
65 
66 int  des_set_key(void *, caddr_t);
67 int  des1_setkey(void *, u_int8_t *, int);
68 int  des3_setkey(void *, u_int8_t *, int);
69 int  blf_setkey(void *, u_int8_t *, int);
70 int  cast5_setkey(void *, u_int8_t *, int);
71 int  rijndael128_setkey(void *, u_int8_t *, int);
72 int  aes_ctr_setkey(void *, u_int8_t *, int);
73 int  aes_xts_setkey(void *, u_int8_t *, int);
74 int  null_setkey(void *, u_int8_t *, int);
75 
76 void des1_encrypt(caddr_t, u_int8_t *);
77 void des3_encrypt(caddr_t, u_int8_t *);
78 void blf_encrypt(caddr_t, u_int8_t *);
79 void cast5_encrypt(caddr_t, u_int8_t *);
80 void rijndael128_encrypt(caddr_t, u_int8_t *);
81 void null_encrypt(caddr_t, u_int8_t *);
82 void aes_xts_encrypt(caddr_t, u_int8_t *);
83 
84 void des1_decrypt(caddr_t, u_int8_t *);
85 void des3_decrypt(caddr_t, u_int8_t *);
86 void blf_decrypt(caddr_t, u_int8_t *);
87 void cast5_decrypt(caddr_t, u_int8_t *);
88 void rijndael128_decrypt(caddr_t, u_int8_t *);
89 void null_decrypt(caddr_t, u_int8_t *);
90 void aes_xts_decrypt(caddr_t, u_int8_t *);
91 
92 void aes_ctr_crypt(caddr_t, u_int8_t *);
93 
94 void aes_ctr_reinit(caddr_t, u_int8_t *);
95 void aes_xts_reinit(caddr_t, u_int8_t *);
96 void aes_gcm_reinit(caddr_t, u_int8_t *);
97 
98 int MD5Update_int(void *, const u_int8_t *, u_int16_t);
99 int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
100 int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
101 int SHA256Update_int(void *, const u_int8_t *, u_int16_t);
102 int SHA384Update_int(void *, const u_int8_t *, u_int16_t);
103 int SHA512Update_int(void *, const u_int8_t *, u_int16_t);
104 
105 u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
106 u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
107 u_int32_t lzs_dummy(u_int8_t *, u_int32_t, u_int8_t **);
108 
109 #define AESCTR_NONCESIZE	4
110 #define AESCTR_IVSIZE		8
111 #define AESCTR_BLOCKSIZE	16
112 
113 struct aes_ctr_ctx {
114 	u_int32_t	ac_ek[4*(AES_MAXROUNDS + 1)];
115 	u_int8_t	ac_block[AESCTR_BLOCKSIZE];
116 	int		ac_nr;
117 };
118 
119 #define AES_XTS_BLOCKSIZE	16
120 #define AES_XTS_IVSIZE		8
121 #define AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
122 
123 struct aes_xts_ctx {
124 	rijndael_ctx key1;
125 	rijndael_ctx key2;
126 	u_int8_t tweak[AES_XTS_BLOCKSIZE];
127 };
128 
129 /* Helper */
130 void aes_xts_crypt(struct aes_xts_ctx *, u_int8_t *, u_int);
131 
132 /* Encryption instances */
133 struct enc_xform enc_xform_des = {
134 	CRYPTO_DES_CBC, "DES",
135 	8, 8, 8, 8, 128,
136 	des1_encrypt,
137 	des1_decrypt,
138 	des1_setkey,
139 	NULL
140 };
141 
142 struct enc_xform enc_xform_3des = {
143 	CRYPTO_3DES_CBC, "3DES",
144 	8, 8, 24, 24, 384,
145 	des3_encrypt,
146 	des3_decrypt,
147 	des3_setkey,
148 	NULL
149 };
150 
151 struct enc_xform enc_xform_blf = {
152 	CRYPTO_BLF_CBC, "Blowfish",
153 	8, 8, 5, 56 /* 448 bits, max key */,
154 	sizeof(blf_ctx),
155 	blf_encrypt,
156 	blf_decrypt,
157 	blf_setkey,
158 	NULL
159 };
160 
161 struct enc_xform enc_xform_cast5 = {
162 	CRYPTO_CAST_CBC, "CAST-128",
163 	8, 8, 5, 16,
164 	sizeof(cast_key),
165 	cast5_encrypt,
166 	cast5_decrypt,
167 	cast5_setkey,
168 	NULL
169 };
170 
171 struct enc_xform enc_xform_rijndael128 = {
172 	CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
173 	16, 16, 16, 32,
174 	sizeof(rijndael_ctx),
175 	rijndael128_encrypt,
176 	rijndael128_decrypt,
177 	rijndael128_setkey,
178 	NULL
179 };
180 
181 struct enc_xform enc_xform_aes_ctr = {
182 	CRYPTO_AES_CTR, "AES-CTR",
183 	16, 8, 16+4, 32+4,
184 	sizeof(struct aes_ctr_ctx),
185 	aes_ctr_crypt,
186 	aes_ctr_crypt,
187 	aes_ctr_setkey,
188 	aes_ctr_reinit
189 };
190 
191 struct enc_xform enc_xform_aes_gcm = {
192 	CRYPTO_AES_GCM_16, "AES-GCM",
193 	1, 8, 16+4, 32+4,
194 	sizeof(struct aes_ctr_ctx),
195 	aes_ctr_crypt,
196 	aes_ctr_crypt,
197 	aes_ctr_setkey,
198 	aes_gcm_reinit
199 };
200 
201 struct enc_xform enc_xform_aes_gmac = {
202 	CRYPTO_AES_GMAC, "AES-GMAC",
203 	1, 8, 16+4, 32+4, 0,
204 	NULL,
205 	NULL,
206 	NULL,
207 	NULL
208 };
209 
210 struct enc_xform enc_xform_aes_xts = {
211 	CRYPTO_AES_XTS, "AES-XTS",
212 	16, 8, 32, 64,
213 	sizeof(struct aes_xts_ctx),
214 	aes_xts_encrypt,
215 	aes_xts_decrypt,
216 	aes_xts_setkey,
217 	aes_xts_reinit
218 };
219 
220 struct enc_xform enc_xform_arc4 = {
221 	CRYPTO_ARC4, "ARC4",
222 	1, 1, 1, 32, 0,
223 	NULL,
224 	NULL,
225 	NULL,
226 	NULL
227 };
228 
229 struct enc_xform enc_xform_null = {
230 	CRYPTO_NULL, "NULL",
231 	4, 0, 0, 256, 0,
232 	null_encrypt,
233 	null_decrypt,
234 	null_setkey,
235 	NULL
236 };
237 
238 /* Authentication instances */
239 struct auth_hash auth_hash_hmac_md5_96 = {
240 	CRYPTO_MD5_HMAC, "HMAC-MD5",
241 	16, 16, 12, sizeof(MD5_CTX), HMAC_MD5_BLOCK_LEN,
242 	(void (*) (void *)) MD5Init, NULL, NULL,
243 	MD5Update_int,
244 	(void (*) (u_int8_t *, void *)) MD5Final
245 };
246 
247 struct auth_hash auth_hash_hmac_sha1_96 = {
248 	CRYPTO_SHA1_HMAC, "HMAC-SHA1",
249 	20, 20, 12, sizeof(SHA1_CTX), HMAC_SHA1_BLOCK_LEN,
250 	(void (*) (void *)) SHA1Init, NULL, NULL,
251 	SHA1Update_int,
252 	(void (*) (u_int8_t *, void *)) SHA1Final
253 };
254 
255 struct auth_hash auth_hash_hmac_ripemd_160_96 = {
256 	CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
257 	20, 20, 12, sizeof(RMD160_CTX), HMAC_RIPEMD160_BLOCK_LEN,
258 	(void (*)(void *)) RMD160Init, NULL, NULL,
259 	RMD160Update_int,
260 	(void (*)(u_int8_t *, void *)) RMD160Final
261 };
262 
263 struct auth_hash auth_hash_hmac_sha2_256_128 = {
264 	CRYPTO_SHA2_256_HMAC, "HMAC-SHA2-256",
265 	32, 32, 16, sizeof(SHA2_CTX), HMAC_SHA2_256_BLOCK_LEN,
266 	(void (*)(void *)) SHA256Init, NULL, NULL,
267 	SHA256Update_int,
268 	(void (*)(u_int8_t *, void *)) SHA256Final
269 };
270 
271 struct auth_hash auth_hash_hmac_sha2_384_192 = {
272 	CRYPTO_SHA2_384_HMAC, "HMAC-SHA2-384",
273 	48, 48, 24, sizeof(SHA2_CTX), HMAC_SHA2_384_BLOCK_LEN,
274 	(void (*)(void *)) SHA384Init, NULL, NULL,
275 	SHA384Update_int,
276 	(void (*)(u_int8_t *, void *)) SHA384Final
277 };
278 
279 struct auth_hash auth_hash_hmac_sha2_512_256 = {
280 	CRYPTO_SHA2_512_HMAC, "HMAC-SHA2-512",
281 	64, 64, 32, sizeof(SHA2_CTX), HMAC_SHA2_512_BLOCK_LEN,
282 	(void (*)(void *)) SHA512Init, NULL, NULL,
283 	SHA512Update_int,
284 	(void (*)(u_int8_t *, void *)) SHA512Final
285 };
286 
287 struct auth_hash auth_hash_gmac_aes_128 = {
288 	CRYPTO_AES_128_GMAC, "GMAC-AES-128",
289 	16+4, 16, 16, sizeof(AES_GMAC_CTX), GMAC_BLOCK_LEN,
290 	(void (*)(void *)) AES_GMAC_Init,
291 	(void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
292 	(void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
293 	(int  (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Update,
294 	(void (*)(u_int8_t *, void *)) AES_GMAC_Final
295 };
296 
297 struct auth_hash auth_hash_gmac_aes_192 = {
298 	CRYPTO_AES_192_GMAC, "GMAC-AES-192",
299 	24+4, 16, 16, sizeof(AES_GMAC_CTX), GMAC_BLOCK_LEN,
300 	(void (*)(void *)) AES_GMAC_Init,
301 	(void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
302 	(void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
303 	(int  (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Update,
304 	(void (*)(u_int8_t *, void *)) AES_GMAC_Final
305 };
306 
307 struct auth_hash auth_hash_gmac_aes_256 = {
308 	CRYPTO_AES_256_GMAC, "GMAC-AES-256",
309 	32+4, 16, 16, sizeof(AES_GMAC_CTX), GMAC_BLOCK_LEN,
310 	(void (*)(void *)) AES_GMAC_Init,
311 	(void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
312 	(void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
313 	(int  (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Update,
314 	(void (*)(u_int8_t *, void *)) AES_GMAC_Final
315 };
316 
317 struct auth_hash auth_hash_key_md5 = {
318 	CRYPTO_MD5_KPDK, "Keyed MD5",
319 	0, 16, 16, sizeof(MD5_CTX), 0,
320 	(void (*)(void *)) MD5Init, NULL, NULL,
321 	MD5Update_int,
322 	(void (*)(u_int8_t *, void *)) MD5Final
323 };
324 
325 struct auth_hash auth_hash_key_sha1 = {
326 	CRYPTO_SHA1_KPDK, "Keyed SHA1",
327 	0, 20, 20, sizeof(SHA1_CTX), 0,
328 	(void (*)(void *)) SHA1Init, NULL, NULL,
329 	SHA1Update_int,
330 	(void (*)(u_int8_t *, void *)) SHA1Final
331 };
332 
333 struct auth_hash auth_hash_md5 = {
334 	CRYPTO_MD5, "MD5",
335 	0, 16, 16, sizeof(MD5_CTX), 0,
336 	(void (*) (void *)) MD5Init, NULL, NULL,
337 	MD5Update_int,
338 	(void (*) (u_int8_t *, void *)) MD5Final
339 };
340 
341 struct auth_hash auth_hash_sha1 = {
342 	CRYPTO_SHA1, "SHA1",
343 	0, 20, 20, sizeof(SHA1_CTX), 0,
344 	(void (*)(void *)) SHA1Init, NULL, NULL,
345 	SHA1Update_int,
346 	(void (*)(u_int8_t *, void *)) SHA1Final
347 };
348 
349 /* Compression instance */
350 struct comp_algo comp_algo_deflate = {
351 	CRYPTO_DEFLATE_COMP, "Deflate",
352 	90, deflate_compress,
353 	deflate_decompress
354 };
355 
356 struct comp_algo comp_algo_lzs = {
357 	CRYPTO_LZS_COMP, "LZS",
358 	90, lzs_dummy,
359 	lzs_dummy
360 };
361 
362 /*
363  * Encryption wrapper routines.
364  */
365 void
366 des1_encrypt(caddr_t key, u_int8_t *blk)
367 {
368 	des_ecb_encrypt(blk, blk, key, 1);
369 }
370 
371 void
372 des1_decrypt(caddr_t key, u_int8_t *blk)
373 {
374 	des_ecb_encrypt(blk, blk, key, 0);
375 }
376 
377 int
378 des1_setkey(void *sched, u_int8_t *key, int len)
379 {
380 	return des_set_key(key, sched);
381 }
382 
383 void
384 des3_encrypt(caddr_t key, u_int8_t *blk)
385 {
386 	des_ecb3_encrypt(blk, blk, key, key + 128, key + 256, 1);
387 }
388 
389 void
390 des3_decrypt(caddr_t key, u_int8_t *blk)
391 {
392 	des_ecb3_encrypt(blk, blk, key + 256, key + 128, key, 0);
393 }
394 
395 int
396 des3_setkey(void *sched, u_int8_t *key, int len)
397 {
398 	if (des_set_key(key, sched) < 0 || des_set_key(key + 8, sched + 128)
399 	    < 0 || des_set_key(key + 16, sched + 256) < 0)
400 		return -1;
401 
402 	return 0;
403 }
404 
405 void
406 blf_encrypt(caddr_t key, u_int8_t *blk)
407 {
408 	blf_ecb_encrypt((blf_ctx *) key, blk, 8);
409 }
410 
411 void
412 blf_decrypt(caddr_t key, u_int8_t *blk)
413 {
414 	blf_ecb_decrypt((blf_ctx *) key, blk, 8);
415 }
416 
417 int
418 blf_setkey(void *sched, u_int8_t *key, int len)
419 {
420 	blf_key((blf_ctx *)sched, key, len);
421 
422 	return 0;
423 }
424 
425 int
426 null_setkey(void *sched, u_int8_t *key, int len)
427 {
428 	return 0;
429 }
430 
431 void
432 null_encrypt(caddr_t key, u_int8_t *blk)
433 {
434 }
435 
436 void
437 null_decrypt(caddr_t key, u_int8_t *blk)
438 {
439 }
440 
441 void
442 cast5_encrypt(caddr_t key, u_int8_t *blk)
443 {
444 	cast_encrypt((cast_key *) key, blk, blk);
445 }
446 
447 void
448 cast5_decrypt(caddr_t key, u_int8_t *blk)
449 {
450 	cast_decrypt((cast_key *) key, blk, blk);
451 }
452 
453 int
454 cast5_setkey(void *sched, u_int8_t *key, int len)
455 {
456 	cast_setkey((cast_key *)sched, key, len);
457 
458 	return 0;
459 }
460 
461 void
462 rijndael128_encrypt(caddr_t key, u_int8_t *blk)
463 {
464 	rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
465 }
466 
467 void
468 rijndael128_decrypt(caddr_t key, u_int8_t *blk)
469 {
470 	rijndael_decrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
471 }
472 
473 int
474 rijndael128_setkey(void *sched, u_int8_t *key, int len)
475 {
476 	return rijndael_set_key((rijndael_ctx *)sched, (u_char *)key, len * 8);
477 }
478 
479 void
480 aes_ctr_reinit(caddr_t key, u_int8_t *iv)
481 {
482 	struct aes_ctr_ctx *ctx;
483 
484 	ctx = (struct aes_ctr_ctx *)key;
485 	bcopy(iv, ctx->ac_block + AESCTR_NONCESIZE, AESCTR_IVSIZE);
486 
487 	/* reset counter */
488 	bzero(ctx->ac_block + AESCTR_NONCESIZE + AESCTR_IVSIZE, 4);
489 }
490 
491 void
492 aes_gcm_reinit(caddr_t key, u_int8_t *iv)
493 {
494 	struct aes_ctr_ctx *ctx;
495 
496 	ctx = (struct aes_ctr_ctx *)key;
497 	bcopy(iv, ctx->ac_block + AESCTR_NONCESIZE, AESCTR_IVSIZE);
498 
499 	/* reset counter */
500 	bzero(ctx->ac_block + AESCTR_NONCESIZE + AESCTR_IVSIZE, 4);
501 	ctx->ac_block[AESCTR_BLOCKSIZE - 1] = 1; /* GCM starts with 1 */
502 }
503 
504 void
505 aes_ctr_crypt(caddr_t key, u_int8_t *data)
506 {
507 	struct aes_ctr_ctx *ctx;
508 	u_int8_t keystream[AESCTR_BLOCKSIZE];
509 	int i;
510 
511 	ctx = (struct aes_ctr_ctx *)key;
512 	/* increment counter */
513 	for (i = AESCTR_BLOCKSIZE - 1;
514 	     i >= AESCTR_NONCESIZE + AESCTR_IVSIZE; i--)
515 		if (++ctx->ac_block[i])   /* continue on overflow */
516 			break;
517 	rijndaelEncrypt(ctx->ac_ek, ctx->ac_nr, ctx->ac_block, keystream);
518 	for (i = 0; i < AESCTR_BLOCKSIZE; i++)
519 		data[i] ^= keystream[i];
520 	explicit_bzero(keystream, sizeof(keystream));
521 }
522 
523 int
524 aes_ctr_setkey(void *sched, u_int8_t *key, int len)
525 {
526 	struct aes_ctr_ctx *ctx;
527 
528 	if (len < AESCTR_NONCESIZE)
529 		return -1;
530 
531 	ctx = (struct aes_ctr_ctx *)sched;
532 	ctx->ac_nr = rijndaelKeySetupEnc(ctx->ac_ek, (u_char *)key,
533 	    (len - AESCTR_NONCESIZE) * 8);
534 	if (ctx->ac_nr == 0)
535 		return -1;
536 	bcopy(key + len - AESCTR_NONCESIZE, ctx->ac_block, AESCTR_NONCESIZE);
537 	return 0;
538 }
539 
540 void
541 aes_xts_reinit(caddr_t key, u_int8_t *iv)
542 {
543 	struct aes_xts_ctx *ctx = (struct aes_xts_ctx *)key;
544 	u_int64_t blocknum;
545 	u_int i;
546 
547 	/*
548 	 * Prepare tweak as E_k2(IV). IV is specified as LE representation
549 	 * of a 64-bit block number which we allow to be passed in directly.
550 	 */
551 	bcopy(iv, &blocknum, AES_XTS_IVSIZE);
552 	for (i = 0; i < AES_XTS_IVSIZE; i++) {
553 		ctx->tweak[i] = blocknum & 0xff;
554 		blocknum >>= 8;
555 	}
556 	/* Last 64 bits of IV are always zero */
557 	bzero(ctx->tweak + AES_XTS_IVSIZE, AES_XTS_IVSIZE);
558 
559 	rijndael_encrypt(&ctx->key2, ctx->tweak, ctx->tweak);
560 }
561 
562 void
563 aes_xts_crypt(struct aes_xts_ctx *ctx, u_int8_t *data, u_int do_encrypt)
564 {
565 	u_int8_t block[AES_XTS_BLOCKSIZE];
566 	u_int i, carry_in, carry_out;
567 
568 	for (i = 0; i < AES_XTS_BLOCKSIZE; i++)
569 		block[i] = data[i] ^ ctx->tweak[i];
570 
571 	if (do_encrypt)
572 		rijndael_encrypt(&ctx->key1, block, data);
573 	else
574 		rijndael_decrypt(&ctx->key1, block, data);
575 
576 	for (i = 0; i < AES_XTS_BLOCKSIZE; i++)
577 		data[i] ^= ctx->tweak[i];
578 
579 	/* Exponentiate tweak */
580 	carry_in = 0;
581 	for (i = 0; i < AES_XTS_BLOCKSIZE; i++) {
582 		carry_out = ctx->tweak[i] & 0x80;
583 		ctx->tweak[i] = (ctx->tweak[i] << 1) | (carry_in ? 1 : 0);
584 		carry_in = carry_out;
585 	}
586 	if (carry_in)
587 		ctx->tweak[0] ^= AES_XTS_ALPHA;
588 	explicit_bzero(block, sizeof(block));
589 }
590 
591 void
592 aes_xts_encrypt(caddr_t key, u_int8_t *data)
593 {
594 	aes_xts_crypt((struct aes_xts_ctx *)key, data, 1);
595 }
596 
597 void
598 aes_xts_decrypt(caddr_t key, u_int8_t *data)
599 {
600 	aes_xts_crypt((struct aes_xts_ctx *)key, data, 0);
601 }
602 
603 int
604 aes_xts_setkey(void *sched, u_int8_t *key, int len)
605 {
606 	struct aes_xts_ctx *ctx;
607 
608 	if (len != 32 && len != 64)
609 		return -1;
610 
611 	ctx = (struct aes_xts_ctx *)sched;
612 
613 	rijndael_set_key(&ctx->key1, key, len * 4);
614 	rijndael_set_key(&ctx->key2, key + (len / 2), len * 4);
615 
616 	return 0;
617 }
618 
619 /*
620  * And now for auth.
621  */
622 
623 int
624 RMD160Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
625 {
626 	RMD160Update(ctx, buf, len);
627 	return 0;
628 }
629 
630 int
631 MD5Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
632 {
633 	MD5Update(ctx, buf, len);
634 	return 0;
635 }
636 
637 int
638 SHA1Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
639 {
640 	SHA1Update(ctx, buf, len);
641 	return 0;
642 }
643 
644 int
645 SHA256Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
646 {
647 	SHA256Update(ctx, buf, len);
648 	return 0;
649 }
650 
651 int
652 SHA384Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
653 {
654 	SHA384Update(ctx, buf, len);
655 	return 0;
656 }
657 
658 int
659 SHA512Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
660 {
661 	SHA512Update(ctx, buf, len);
662 	return 0;
663 }
664 
665 
666 u_int32_t deflate_global(u_int8_t *, u_int32_t, int, u_int8_t **);
667 
668 struct deflate_buf {
669         u_int8_t *out;
670         u_int32_t size;
671         int flag;
672 };
673 
674 /*
675  * And compression
676  */
677 
678 u_int32_t
679 deflate_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
680 {
681 	return deflate_global(data, size, 0, out);
682 }
683 
684 u_int32_t
685 deflate_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out)
686 {
687 	return deflate_global(data, size, 1, out);
688 }
689 
690 u_int32_t
691 lzs_dummy(u_int8_t *data, u_int32_t size, u_int8_t **out)
692 {
693 	*out = NULL;
694 	return (0);
695 }
696