xform_sha2.c revision 213065
1104476Ssam/*	$OpenBSD: xform.c,v 1.16 2001/08/28 12:20:43 ben Exp $	*/
2139825Simp/*-
3104476Ssam * The authors of this code are John Ioannidis (ji@tla.org),
4104476Ssam * Angelos D. Keromytis (kermit@csd.uch.gr) and
5104476Ssam * Niels Provos (provos@physnet.uni-hamburg.de).
6104476Ssam *
7104476Ssam * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
8104476Ssam * in November 1995.
9104476Ssam *
10104476Ssam * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
11104476Ssam * by Angelos D. Keromytis.
12104476Ssam *
13104476Ssam * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
14104476Ssam * and Niels Provos.
15104476Ssam *
16104476Ssam * Additional features in 1999 by Angelos D. Keromytis.
17104476Ssam *
18104476Ssam * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
19104476Ssam * Angelos D. Keromytis and Niels Provos.
20104476Ssam *
21104476Ssam * Copyright (C) 2001, Angelos D. Keromytis.
22104476Ssam *
23104476Ssam * Permission to use, copy, and modify this software with or without fee
24104476Ssam * is hereby granted, provided that this entire notice is included in
25104476Ssam * all copies of any software which is or includes a copy or
26104476Ssam * modification of this software.
27104476Ssam * You may use this code under the GNU public license if you so wish. Please
28104476Ssam * contribute changes back to the authors under this freer than GPL license
29104476Ssam * so that we may further the use of strong encryption without limitations to
30104476Ssam * all.
31104476Ssam *
32104476Ssam * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
33104476Ssam * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
34104476Ssam * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
35104476Ssam * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
36104476Ssam * PURPOSE.
37104476Ssam */
38104476Ssam
39116191Sobrien#include <sys/cdefs.h>
40116191Sobrien__FBSDID("$FreeBSD: head/sys/opencrypto/xform.c 213065 2010-09-23 11:33:29Z pjd $");
41116191Sobrien
42104476Ssam#include <sys/param.h>
43104476Ssam#include <sys/systm.h>
44104476Ssam#include <sys/malloc.h>
45104476Ssam#include <sys/sysctl.h>
46104476Ssam#include <sys/errno.h>
47104476Ssam#include <sys/time.h>
48104476Ssam#include <sys/kernel.h>
49104476Ssam#include <machine/cpu.h>
50104476Ssam
51104476Ssam#include <crypto/blowfish/blowfish.h>
52104476Ssam#include <crypto/des/des.h>
53143423Sume#include <crypto/rijndael/rijndael.h>
54169425Sgnn#include <crypto/camellia/camellia.h>
55104476Ssam#include <crypto/sha1.h>
56104476Ssam
57104476Ssam#include <opencrypto/cast.h>
58104476Ssam#include <opencrypto/deflate.h>
59104476Ssam#include <opencrypto/rmd160.h>
60104476Ssam#include <opencrypto/skipjack.h>
61104476Ssam
62104476Ssam#include <sys/md5.h>
63104476Ssam
64104476Ssam#include <opencrypto/cryptodev.h>
65104476Ssam#include <opencrypto/xform.h>
66104476Ssam
67104476Ssamstatic void null_encrypt(caddr_t, u_int8_t *);
68104476Ssamstatic void null_decrypt(caddr_t, u_int8_t *);
69104476Ssamstatic int null_setkey(u_int8_t **, u_int8_t *, int);
70104476Ssamstatic void null_zerokey(u_int8_t **);
71104476Ssam
72104476Ssamstatic	int des1_setkey(u_int8_t **, u_int8_t *, int);
73104476Ssamstatic	int des3_setkey(u_int8_t **, u_int8_t *, int);
74104476Ssamstatic	int blf_setkey(u_int8_t **, u_int8_t *, int);
75104476Ssamstatic	int cast5_setkey(u_int8_t **, u_int8_t *, int);
76104476Ssamstatic	int skipjack_setkey(u_int8_t **, u_int8_t *, int);
77104476Ssamstatic	int rijndael128_setkey(u_int8_t **, u_int8_t *, int);
78169425Sgnnstatic	int cml_setkey(u_int8_t **, u_int8_t *, int);
79104476Ssamstatic	void des1_encrypt(caddr_t, u_int8_t *);
80104476Ssamstatic	void des3_encrypt(caddr_t, u_int8_t *);
81104476Ssamstatic	void blf_encrypt(caddr_t, u_int8_t *);
82104476Ssamstatic	void cast5_encrypt(caddr_t, u_int8_t *);
83104476Ssamstatic	void skipjack_encrypt(caddr_t, u_int8_t *);
84104476Ssamstatic	void rijndael128_encrypt(caddr_t, u_int8_t *);
85169425Sgnnstatic	void cml_encrypt(caddr_t, u_int8_t *);
86104476Ssamstatic	void des1_decrypt(caddr_t, u_int8_t *);
87104476Ssamstatic	void des3_decrypt(caddr_t, u_int8_t *);
88104476Ssamstatic	void blf_decrypt(caddr_t, u_int8_t *);
89104476Ssamstatic	void cast5_decrypt(caddr_t, u_int8_t *);
90104476Ssamstatic	void skipjack_decrypt(caddr_t, u_int8_t *);
91104476Ssamstatic	void rijndael128_decrypt(caddr_t, u_int8_t *);
92169425Sgnnstatic	void cml_decrypt(caddr_t, u_int8_t *);
93104476Ssamstatic	void des1_zerokey(u_int8_t **);
94104476Ssamstatic	void des3_zerokey(u_int8_t **);
95104476Ssamstatic	void blf_zerokey(u_int8_t **);
96104476Ssamstatic	void cast5_zerokey(u_int8_t **);
97104476Ssamstatic	void skipjack_zerokey(u_int8_t **);
98104476Ssamstatic	void rijndael128_zerokey(u_int8_t **);
99169425Sgnnstatic	void cml_zerokey(u_int8_t **);
100104476Ssam
101104476Ssamstatic	void null_init(void *);
102104476Ssamstatic	int null_update(void *, u_int8_t *, u_int16_t);
103104476Ssamstatic	void null_final(u_int8_t *, void *);
104104476Ssamstatic	int MD5Update_int(void *, u_int8_t *, u_int16_t);
105104476Ssamstatic	void SHA1Init_int(void *);
106104476Ssamstatic	int SHA1Update_int(void *, u_int8_t *, u_int16_t);
107104476Ssamstatic	void SHA1Final_int(u_int8_t *, void *);
108104476Ssamstatic	int RMD160Update_int(void *, u_int8_t *, u_int16_t);
109104476Ssamstatic	int SHA256Update_int(void *, u_int8_t *, u_int16_t);
110104476Ssamstatic	int SHA384Update_int(void *, u_int8_t *, u_int16_t);
111104476Ssamstatic	int SHA512Update_int(void *, u_int8_t *, u_int16_t);
112104476Ssam
113104476Ssamstatic	u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
114104476Ssamstatic	u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
115104476Ssam
116104476SsamMALLOC_DEFINE(M_XDATA, "xform", "xform data buffers");
117104476Ssam
118104476Ssam/* Encryption instances */
119104476Ssamstruct enc_xform enc_xform_null = {
120104476Ssam	CRYPTO_NULL_CBC, "NULL",
121104476Ssam	/* NB: blocksize of 4 is to generate a properly aligned ESP header */
122159235Spjd	NULL_BLOCK_LEN, 0, 256, /* 2048 bits, max key */
123104476Ssam	null_encrypt,
124104476Ssam	null_decrypt,
125104476Ssam	null_setkey,
126104476Ssam	null_zerokey,
127104476Ssam};
128104476Ssam
129104476Ssamstruct enc_xform enc_xform_des = {
130104476Ssam	CRYPTO_DES_CBC, "DES",
131159235Spjd	DES_BLOCK_LEN, 8, 8,
132104476Ssam	des1_encrypt,
133104476Ssam	des1_decrypt,
134104476Ssam	des1_setkey,
135104476Ssam	des1_zerokey,
136104476Ssam};
137104476Ssam
138104476Ssamstruct enc_xform enc_xform_3des = {
139104476Ssam	CRYPTO_3DES_CBC, "3DES",
140159235Spjd	DES3_BLOCK_LEN, 24, 24,
141104476Ssam	des3_encrypt,
142104476Ssam	des3_decrypt,
143104476Ssam	des3_setkey,
144104476Ssam	des3_zerokey
145104476Ssam};
146104476Ssam
147104476Ssamstruct enc_xform enc_xform_blf = {
148104476Ssam	CRYPTO_BLF_CBC, "Blowfish",
149159235Spjd	BLOWFISH_BLOCK_LEN, 5, 56 /* 448 bits, max key */,
150104476Ssam	blf_encrypt,
151104476Ssam	blf_decrypt,
152104476Ssam	blf_setkey,
153104476Ssam	blf_zerokey
154104476Ssam};
155104476Ssam
156104476Ssamstruct enc_xform enc_xform_cast5 = {
157104476Ssam	CRYPTO_CAST_CBC, "CAST-128",
158159235Spjd	CAST128_BLOCK_LEN, 5, 16,
159104476Ssam	cast5_encrypt,
160104476Ssam	cast5_decrypt,
161104476Ssam	cast5_setkey,
162104476Ssam	cast5_zerokey
163104476Ssam};
164104476Ssam
165104476Ssamstruct enc_xform enc_xform_skipjack = {
166104476Ssam	CRYPTO_SKIPJACK_CBC, "Skipjack",
167159235Spjd	SKIPJACK_BLOCK_LEN, 10, 10,
168104476Ssam	skipjack_encrypt,
169104476Ssam	skipjack_decrypt,
170104476Ssam	skipjack_setkey,
171104476Ssam	skipjack_zerokey
172104476Ssam};
173104476Ssam
174104476Ssamstruct enc_xform enc_xform_rijndael128 = {
175104476Ssam	CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
176159235Spjd	RIJNDAEL128_BLOCK_LEN, 8, 32,
177104476Ssam	rijndael128_encrypt,
178104476Ssam	rijndael128_decrypt,
179104476Ssam	rijndael128_setkey,
180104476Ssam	rijndael128_zerokey,
181104476Ssam};
182104476Ssam
183104476Ssamstruct enc_xform enc_xform_arc4 = {
184104476Ssam	CRYPTO_ARC4, "ARC4",
185104476Ssam	1, 1, 32,
186104476Ssam	NULL,
187104476Ssam	NULL,
188104476Ssam	NULL,
189104476Ssam	NULL,
190104476Ssam};
191104476Ssam
192169425Sgnnstruct enc_xform enc_xform_camellia = {
193169425Sgnn	CRYPTO_CAMELLIA_CBC, "Camellia",
194169425Sgnn	CAMELLIA_BLOCK_LEN, 8, 32,
195169425Sgnn	cml_encrypt,
196169425Sgnn	cml_decrypt,
197169425Sgnn	cml_setkey,
198169425Sgnn	cml_zerokey,
199169425Sgnn};
200169425Sgnn
201104476Ssam/* Authentication instances */
202104476Ssamstruct auth_hash auth_hash_null = {
203104476Ssam	CRYPTO_NULL_HMAC, "NULL-HMAC",
204159235Spjd	0, NULL_HASH_LEN, NULL_HMAC_BLOCK_LEN, sizeof(int),	/* NB: context isn't used */
205104476Ssam	null_init, null_update, null_final
206104476Ssam};
207104476Ssam
208158703Spjdstruct auth_hash auth_hash_hmac_md5 = {
209104476Ssam	CRYPTO_MD5_HMAC, "HMAC-MD5",
210159235Spjd	16, MD5_HASH_LEN, MD5_HMAC_BLOCK_LEN, sizeof(MD5_CTX),
211104476Ssam	(void (*) (void *)) MD5Init, MD5Update_int,
212104476Ssam	(void (*) (u_int8_t *, void *)) MD5Final
213104476Ssam};
214104476Ssam
215158703Spjdstruct auth_hash auth_hash_hmac_sha1 = {
216104476Ssam	CRYPTO_SHA1_HMAC, "HMAC-SHA1",
217159235Spjd	20, SHA1_HASH_LEN, SHA1_HMAC_BLOCK_LEN, sizeof(SHA1_CTX),
218104476Ssam	SHA1Init_int, SHA1Update_int, SHA1Final_int
219104476Ssam};
220104476Ssam
221158703Spjdstruct auth_hash auth_hash_hmac_ripemd_160 = {
222104476Ssam	CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
223159235Spjd	20, RIPEMD160_HASH_LEN, RIPEMD160_HMAC_BLOCK_LEN, sizeof(RMD160_CTX),
224104476Ssam	(void (*)(void *)) RMD160Init, RMD160Update_int,
225104476Ssam	(void (*)(u_int8_t *, void *)) RMD160Final
226104476Ssam};
227104476Ssam
228104476Ssamstruct auth_hash auth_hash_key_md5 = {
229213065Spjd	CRYPTO_MD5_KPDK, "Keyed MD5",
230159235Spjd	0, MD5_KPDK_HASH_LEN, 0, sizeof(MD5_CTX),
231104476Ssam	(void (*)(void *)) MD5Init, MD5Update_int,
232104476Ssam	(void (*)(u_int8_t *, void *)) MD5Final
233104476Ssam};
234104476Ssam
235104476Ssamstruct auth_hash auth_hash_key_sha1 = {
236104476Ssam	CRYPTO_SHA1_KPDK, "Keyed SHA1",
237159235Spjd	0, SHA1_KPDK_HASH_LEN, 0, sizeof(SHA1_CTX),
238104476Ssam	SHA1Init_int, SHA1Update_int, SHA1Final_int
239104476Ssam};
240104476Ssam
241104476Ssamstruct auth_hash auth_hash_hmac_sha2_256 = {
242158703Spjd	CRYPTO_SHA2_256_HMAC, "HMAC-SHA2-256",
243159235Spjd	32, SHA2_256_HASH_LEN, SHA2_256_HMAC_BLOCK_LEN, sizeof(SHA256_CTX),
244104476Ssam	(void (*)(void *)) SHA256_Init, SHA256Update_int,
245104476Ssam	(void (*)(u_int8_t *, void *)) SHA256_Final
246104476Ssam};
247104476Ssam
248104476Ssamstruct auth_hash auth_hash_hmac_sha2_384 = {
249158703Spjd	CRYPTO_SHA2_384_HMAC, "HMAC-SHA2-384",
250159235Spjd	48, SHA2_384_HASH_LEN, SHA2_384_HMAC_BLOCK_LEN, sizeof(SHA384_CTX),
251104476Ssam	(void (*)(void *)) SHA384_Init, SHA384Update_int,
252104476Ssam	(void (*)(u_int8_t *, void *)) SHA384_Final
253104476Ssam};
254104476Ssam
255104476Ssamstruct auth_hash auth_hash_hmac_sha2_512 = {
256158703Spjd	CRYPTO_SHA2_512_HMAC, "HMAC-SHA2-512",
257159235Spjd	64, SHA2_512_HASH_LEN, SHA2_512_HMAC_BLOCK_LEN, sizeof(SHA512_CTX),
258104476Ssam	(void (*)(void *)) SHA512_Init, SHA512Update_int,
259104476Ssam	(void (*)(u_int8_t *, void *)) SHA512_Final
260104476Ssam};
261104476Ssam
262104476Ssam/* Compression instance */
263104476Ssamstruct comp_algo comp_algo_deflate = {
264104476Ssam	CRYPTO_DEFLATE_COMP, "Deflate",
265104476Ssam	90, deflate_compress,
266104476Ssam	deflate_decompress
267104476Ssam};
268104476Ssam
269104476Ssam/*
270104476Ssam * Encryption wrapper routines.
271104476Ssam */
272104476Ssamstatic void
273104476Ssamnull_encrypt(caddr_t key, u_int8_t *blk)
274104476Ssam{
275104476Ssam}
276104476Ssamstatic void
277104476Ssamnull_decrypt(caddr_t key, u_int8_t *blk)
278104476Ssam{
279104476Ssam}
280104476Ssamstatic int
281104476Ssamnull_setkey(u_int8_t **sched, u_int8_t *key, int len)
282104476Ssam{
283104476Ssam	*sched = NULL;
284104476Ssam	return 0;
285104476Ssam}
286104476Ssamstatic void
287104476Ssamnull_zerokey(u_int8_t **sched)
288104476Ssam{
289104476Ssam	*sched = NULL;
290104476Ssam}
291104476Ssam
292104476Ssamstatic void
293104476Ssamdes1_encrypt(caddr_t key, u_int8_t *blk)
294104476Ssam{
295104476Ssam	des_cblock *cb = (des_cblock *) blk;
296104476Ssam	des_key_schedule *p = (des_key_schedule *) key;
297104476Ssam
298104476Ssam	des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
299104476Ssam}
300104476Ssam
301104476Ssamstatic void
302104476Ssamdes1_decrypt(caddr_t key, u_int8_t *blk)
303104476Ssam{
304104476Ssam	des_cblock *cb = (des_cblock *) blk;
305104476Ssam	des_key_schedule *p = (des_key_schedule *) key;
306104476Ssam
307104476Ssam	des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
308104476Ssam}
309104476Ssam
310104476Ssamstatic int
311104476Ssamdes1_setkey(u_int8_t **sched, u_int8_t *key, int len)
312104476Ssam{
313104476Ssam	des_key_schedule *p;
314104476Ssam	int err;
315104476Ssam
316184205Sdes	p = malloc(sizeof (des_key_schedule),
317104476Ssam		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
318104476Ssam	if (p != NULL) {
319104476Ssam		des_set_key((des_cblock *) key, p[0]);
320104476Ssam		err = 0;
321104476Ssam	} else
322104476Ssam		err = ENOMEM;
323104476Ssam	*sched = (u_int8_t *) p;
324104476Ssam	return err;
325104476Ssam}
326104476Ssam
327104476Ssamstatic void
328104476Ssamdes1_zerokey(u_int8_t **sched)
329104476Ssam{
330104476Ssam	bzero(*sched, sizeof (des_key_schedule));
331184205Sdes	free(*sched, M_CRYPTO_DATA);
332104476Ssam	*sched = NULL;
333104476Ssam}
334104476Ssam
335104476Ssamstatic void
336104476Ssamdes3_encrypt(caddr_t key, u_int8_t *blk)
337104476Ssam{
338104476Ssam	des_cblock *cb = (des_cblock *) blk;
339104476Ssam	des_key_schedule *p = (des_key_schedule *) key;
340104476Ssam
341104476Ssam	des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
342104476Ssam}
343104476Ssam
344104476Ssamstatic void
345104476Ssamdes3_decrypt(caddr_t key, u_int8_t *blk)
346104476Ssam{
347104476Ssam	des_cblock *cb = (des_cblock *) blk;
348104476Ssam	des_key_schedule *p = (des_key_schedule *) key;
349104476Ssam
350104476Ssam	des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
351104476Ssam}
352104476Ssam
353104476Ssamstatic int
354104476Ssamdes3_setkey(u_int8_t **sched, u_int8_t *key, int len)
355104476Ssam{
356104476Ssam	des_key_schedule *p;
357104476Ssam	int err;
358104476Ssam
359184205Sdes	p = malloc(3*sizeof (des_key_schedule),
360104476Ssam		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
361104476Ssam	if (p != NULL) {
362104476Ssam		des_set_key((des_cblock *)(key +  0), p[0]);
363104476Ssam		des_set_key((des_cblock *)(key +  8), p[1]);
364104476Ssam		des_set_key((des_cblock *)(key + 16), p[2]);
365104476Ssam		err = 0;
366104476Ssam	} else
367104476Ssam		err = ENOMEM;
368104476Ssam	*sched = (u_int8_t *) p;
369104476Ssam	return err;
370104476Ssam}
371104476Ssam
372104476Ssamstatic void
373104476Ssamdes3_zerokey(u_int8_t **sched)
374104476Ssam{
375104476Ssam	bzero(*sched, 3*sizeof (des_key_schedule));
376184205Sdes	free(*sched, M_CRYPTO_DATA);
377104476Ssam	*sched = NULL;
378104476Ssam}
379104476Ssam
380104476Ssamstatic void
381104476Ssamblf_encrypt(caddr_t key, u_int8_t *blk)
382104476Ssam{
383104476Ssam	BF_LONG t[2];
384104476Ssam
385104476Ssam	memcpy(t, blk, sizeof (t));
386104476Ssam	t[0] = ntohl(t[0]);
387104476Ssam	t[1] = ntohl(t[1]);
388104476Ssam	/* NB: BF_encrypt expects the block in host order! */
389104476Ssam	BF_encrypt(t, (BF_KEY *) key);
390104476Ssam	t[0] = htonl(t[0]);
391104476Ssam	t[1] = htonl(t[1]);
392104476Ssam	memcpy(blk, t, sizeof (t));
393104476Ssam}
394104476Ssam
395104476Ssamstatic void
396104476Ssamblf_decrypt(caddr_t key, u_int8_t *blk)
397104476Ssam{
398104476Ssam	BF_LONG t[2];
399104476Ssam
400104476Ssam	memcpy(t, blk, sizeof (t));
401104476Ssam	t[0] = ntohl(t[0]);
402104476Ssam	t[1] = ntohl(t[1]);
403104476Ssam	/* NB: BF_decrypt expects the block in host order! */
404104476Ssam	BF_decrypt(t, (BF_KEY *) key);
405104476Ssam	t[0] = htonl(t[0]);
406104476Ssam	t[1] = htonl(t[1]);
407104476Ssam	memcpy(blk, t, sizeof (t));
408104476Ssam}
409104476Ssam
410104476Ssamstatic int
411104476Ssamblf_setkey(u_int8_t **sched, u_int8_t *key, int len)
412104476Ssam{
413104476Ssam	int err;
414104476Ssam
415184205Sdes	*sched = malloc(sizeof(BF_KEY),
416104476Ssam		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
417104476Ssam	if (*sched != NULL) {
418104476Ssam		BF_set_key((BF_KEY *) *sched, len, key);
419104476Ssam		err = 0;
420104476Ssam	} else
421104476Ssam		err = ENOMEM;
422104476Ssam	return err;
423104476Ssam}
424104476Ssam
425104476Ssamstatic void
426104476Ssamblf_zerokey(u_int8_t **sched)
427104476Ssam{
428104476Ssam	bzero(*sched, sizeof(BF_KEY));
429184205Sdes	free(*sched, M_CRYPTO_DATA);
430104476Ssam	*sched = NULL;
431104476Ssam}
432104476Ssam
433104476Ssamstatic void
434104476Ssamcast5_encrypt(caddr_t key, u_int8_t *blk)
435104476Ssam{
436104476Ssam	cast_encrypt((cast_key *) key, blk, blk);
437104476Ssam}
438104476Ssam
439104476Ssamstatic void
440104476Ssamcast5_decrypt(caddr_t key, u_int8_t *blk)
441104476Ssam{
442104476Ssam	cast_decrypt((cast_key *) key, blk, blk);
443104476Ssam}
444104476Ssam
445104476Ssamstatic int
446104476Ssamcast5_setkey(u_int8_t **sched, u_int8_t *key, int len)
447104476Ssam{
448104476Ssam	int err;
449104476Ssam
450184205Sdes	*sched = malloc(sizeof(cast_key), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
451104476Ssam	if (*sched != NULL) {
452104476Ssam		cast_setkey((cast_key *)*sched, key, len);
453104476Ssam		err = 0;
454104476Ssam	} else
455104476Ssam		err = ENOMEM;
456104476Ssam	return err;
457104476Ssam}
458104476Ssam
459104476Ssamstatic void
460104476Ssamcast5_zerokey(u_int8_t **sched)
461104476Ssam{
462104476Ssam	bzero(*sched, sizeof(cast_key));
463184205Sdes	free(*sched, M_CRYPTO_DATA);
464104476Ssam	*sched = NULL;
465104476Ssam}
466104476Ssam
467104476Ssamstatic void
468104476Ssamskipjack_encrypt(caddr_t key, u_int8_t *blk)
469104476Ssam{
470104476Ssam	skipjack_forwards(blk, blk, (u_int8_t **) key);
471104476Ssam}
472104476Ssam
473104476Ssamstatic void
474104476Ssamskipjack_decrypt(caddr_t key, u_int8_t *blk)
475104476Ssam{
476104476Ssam	skipjack_backwards(blk, blk, (u_int8_t **) key);
477104476Ssam}
478104476Ssam
479104476Ssamstatic int
480104476Ssamskipjack_setkey(u_int8_t **sched, u_int8_t *key, int len)
481104476Ssam{
482104476Ssam	int err;
483104476Ssam
484104476Ssam	/* NB: allocate all the memory that's needed at once */
485184205Sdes	*sched = malloc(10 * (sizeof(u_int8_t *) + 0x100),
486104476Ssam		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
487104476Ssam	if (*sched != NULL) {
488104476Ssam		u_int8_t** key_tables = (u_int8_t**) *sched;
489104476Ssam		u_int8_t* table = (u_int8_t*) &key_tables[10];
490104476Ssam		int k;
491104476Ssam
492104476Ssam		for (k = 0; k < 10; k++) {
493104476Ssam			key_tables[k] = table;
494104476Ssam			table += 0x100;
495104476Ssam		}
496104476Ssam		subkey_table_gen(key, (u_int8_t **) *sched);
497104476Ssam		err = 0;
498104476Ssam	} else
499104476Ssam		err = ENOMEM;
500104476Ssam	return err;
501104476Ssam}
502104476Ssam
503104476Ssamstatic void
504104476Ssamskipjack_zerokey(u_int8_t **sched)
505104476Ssam{
506104476Ssam	bzero(*sched, 10 * (sizeof(u_int8_t *) + 0x100));
507184205Sdes	free(*sched, M_CRYPTO_DATA);
508104476Ssam	*sched = NULL;
509104476Ssam}
510104476Ssam
511104476Ssamstatic void
512104476Ssamrijndael128_encrypt(caddr_t key, u_int8_t *blk)
513104476Ssam{
514104476Ssam	rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
515104476Ssam}
516104476Ssam
517104476Ssamstatic void
518104476Ssamrijndael128_decrypt(caddr_t key, u_int8_t *blk)
519104476Ssam{
520143408Sume	rijndael_decrypt(((rijndael_ctx *) key), (u_char *) blk,
521104476Ssam	    (u_char *) blk);
522104476Ssam}
523104476Ssam
524104476Ssamstatic int
525104476Ssamrijndael128_setkey(u_int8_t **sched, u_int8_t *key, int len)
526104476Ssam{
527104476Ssam	int err;
528104476Ssam
529149143Spjd	if (len != 16 && len != 24 && len != 32)
530149143Spjd		return (EINVAL);
531184205Sdes	*sched = malloc(sizeof(rijndael_ctx), M_CRYPTO_DATA,
532104476Ssam	    M_NOWAIT|M_ZERO);
533104476Ssam	if (*sched != NULL) {
534143408Sume		rijndael_set_key((rijndael_ctx *) *sched, (u_char *) key,
535143408Sume		    len * 8);
536104476Ssam		err = 0;
537104476Ssam	} else
538104476Ssam		err = ENOMEM;
539104476Ssam	return err;
540104476Ssam}
541104476Ssam
542104476Ssamstatic void
543104476Ssamrijndael128_zerokey(u_int8_t **sched)
544104476Ssam{
545143408Sume	bzero(*sched, sizeof(rijndael_ctx));
546184205Sdes	free(*sched, M_CRYPTO_DATA);
547104476Ssam	*sched = NULL;
548104476Ssam}
549104476Ssam
550169425Sgnnstatic void
551169425Sgnncml_encrypt(caddr_t key, u_int8_t *blk)
552169425Sgnn{
553169425Sgnn	camellia_encrypt((camellia_ctx *) key, (u_char *) blk, (u_char *) blk);
554169425Sgnn}
555169425Sgnn
556169425Sgnnstatic void
557169425Sgnncml_decrypt(caddr_t key, u_int8_t *blk)
558169425Sgnn{
559169425Sgnn	camellia_decrypt(((camellia_ctx *) key), (u_char *) blk,
560169425Sgnn	    (u_char *) blk);
561169425Sgnn}
562169425Sgnn
563169425Sgnnstatic int
564169425Sgnncml_setkey(u_int8_t **sched, u_int8_t *key, int len)
565169425Sgnn{
566169425Sgnn	int err;
567169425Sgnn
568169425Sgnn	if (len != 16 && len != 24 && len != 32)
569169425Sgnn		return (EINVAL);
570184205Sdes	*sched = malloc(sizeof(camellia_ctx), M_CRYPTO_DATA,
571169425Sgnn	    M_NOWAIT|M_ZERO);
572169425Sgnn	if (*sched != NULL) {
573169425Sgnn		camellia_set_key((camellia_ctx *) *sched, (u_char *) key,
574169425Sgnn		    len * 8);
575169425Sgnn		err = 0;
576169425Sgnn	} else
577169425Sgnn		err = ENOMEM;
578169425Sgnn	return err;
579169425Sgnn}
580169425Sgnn
581169425Sgnnstatic void
582169425Sgnncml_zerokey(u_int8_t **sched)
583169425Sgnn{
584169425Sgnn	bzero(*sched, sizeof(camellia_ctx));
585184205Sdes	free(*sched, M_CRYPTO_DATA);
586169425Sgnn	*sched = NULL;
587169425Sgnn}
588169425Sgnn
589104476Ssam/*
590104476Ssam * And now for auth.
591104476Ssam */
592104476Ssam
593104476Ssamstatic void
594104476Ssamnull_init(void *ctx)
595104476Ssam{
596104476Ssam}
597104476Ssam
598104476Ssamstatic int
599104476Ssamnull_update(void *ctx, u_int8_t *buf, u_int16_t len)
600104476Ssam{
601104476Ssam	return 0;
602104476Ssam}
603104476Ssam
604104476Ssamstatic void
605104476Ssamnull_final(u_int8_t *buf, void *ctx)
606104476Ssam{
607104476Ssam	if (buf != (u_int8_t *) 0)
608104476Ssam		bzero(buf, 12);
609104476Ssam}
610104476Ssam
611104476Ssamstatic int
612104476SsamRMD160Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
613104476Ssam{
614104476Ssam	RMD160Update(ctx, buf, len);
615104476Ssam	return 0;
616104476Ssam}
617104476Ssam
618104476Ssamstatic int
619104476SsamMD5Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
620104476Ssam{
621104476Ssam	MD5Update(ctx, buf, len);
622104476Ssam	return 0;
623104476Ssam}
624104476Ssam
625104476Ssamstatic void
626104476SsamSHA1Init_int(void *ctx)
627104476Ssam{
628104476Ssam	SHA1Init(ctx);
629104476Ssam}
630104476Ssam
631104476Ssamstatic int
632104476SsamSHA1Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
633104476Ssam{
634104476Ssam	SHA1Update(ctx, buf, len);
635104476Ssam	return 0;
636104476Ssam}
637104476Ssam
638104476Ssamstatic void
639104476SsamSHA1Final_int(u_int8_t *blk, void *ctx)
640104476Ssam{
641104476Ssam	SHA1Final(blk, ctx);
642104476Ssam}
643104476Ssam
644104476Ssamstatic int
645104476SsamSHA256Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
646104476Ssam{
647104476Ssam	SHA256_Update(ctx, buf, len);
648104476Ssam	return 0;
649104476Ssam}
650104476Ssam
651104476Ssamstatic int
652104476SsamSHA384Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
653104476Ssam{
654104476Ssam	SHA384_Update(ctx, buf, len);
655104476Ssam	return 0;
656104476Ssam}
657104476Ssam
658104476Ssamstatic int
659104476SsamSHA512Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
660104476Ssam{
661104476Ssam	SHA512_Update(ctx, buf, len);
662104476Ssam	return 0;
663104476Ssam}
664104476Ssam
665104476Ssam/*
666104476Ssam * And compression
667104476Ssam */
668104476Ssam
669104476Ssamstatic u_int32_t
670104476Ssamdeflate_compress(data, size, out)
671104476Ssam	u_int8_t *data;
672104476Ssam	u_int32_t size;
673104476Ssam	u_int8_t **out;
674104476Ssam{
675104476Ssam	return deflate_global(data, size, 0, out);
676104476Ssam}
677104476Ssam
678104476Ssamstatic u_int32_t
679104476Ssamdeflate_decompress(data, size, out)
680104476Ssam	u_int8_t *data;
681104476Ssam	u_int32_t size;
682104476Ssam	u_int8_t **out;
683104476Ssam{
684104476Ssam	return deflate_global(data, size, 1, out);
685104476Ssam}
686