xform_sha2.c revision 158703
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 158703 2006-05-17 18:24:17Z 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>
54104476Ssam#include <crypto/sha1.h>
55104476Ssam
56104476Ssam#include <opencrypto/cast.h>
57104476Ssam#include <opencrypto/deflate.h>
58104476Ssam#include <opencrypto/rmd160.h>
59104476Ssam#include <opencrypto/skipjack.h>
60104476Ssam
61104476Ssam#include <sys/md5.h>
62104476Ssam
63104476Ssam#include <opencrypto/cryptodev.h>
64104476Ssam#include <opencrypto/xform.h>
65104476Ssam
66104476Ssamstatic void null_encrypt(caddr_t, u_int8_t *);
67104476Ssamstatic void null_decrypt(caddr_t, u_int8_t *);
68104476Ssamstatic int null_setkey(u_int8_t **, u_int8_t *, int);
69104476Ssamstatic void null_zerokey(u_int8_t **);
70104476Ssam
71104476Ssamstatic	int des1_setkey(u_int8_t **, u_int8_t *, int);
72104476Ssamstatic	int des3_setkey(u_int8_t **, u_int8_t *, int);
73104476Ssamstatic	int blf_setkey(u_int8_t **, u_int8_t *, int);
74104476Ssamstatic	int cast5_setkey(u_int8_t **, u_int8_t *, int);
75104476Ssamstatic	int skipjack_setkey(u_int8_t **, u_int8_t *, int);
76104476Ssamstatic	int rijndael128_setkey(u_int8_t **, u_int8_t *, int);
77104476Ssamstatic	void des1_encrypt(caddr_t, u_int8_t *);
78104476Ssamstatic	void des3_encrypt(caddr_t, u_int8_t *);
79104476Ssamstatic	void blf_encrypt(caddr_t, u_int8_t *);
80104476Ssamstatic	void cast5_encrypt(caddr_t, u_int8_t *);
81104476Ssamstatic	void skipjack_encrypt(caddr_t, u_int8_t *);
82104476Ssamstatic	void rijndael128_encrypt(caddr_t, u_int8_t *);
83104476Ssamstatic	void des1_decrypt(caddr_t, u_int8_t *);
84104476Ssamstatic	void des3_decrypt(caddr_t, u_int8_t *);
85104476Ssamstatic	void blf_decrypt(caddr_t, u_int8_t *);
86104476Ssamstatic	void cast5_decrypt(caddr_t, u_int8_t *);
87104476Ssamstatic	void skipjack_decrypt(caddr_t, u_int8_t *);
88104476Ssamstatic	void rijndael128_decrypt(caddr_t, u_int8_t *);
89104476Ssamstatic	void des1_zerokey(u_int8_t **);
90104476Ssamstatic	void des3_zerokey(u_int8_t **);
91104476Ssamstatic	void blf_zerokey(u_int8_t **);
92104476Ssamstatic	void cast5_zerokey(u_int8_t **);
93104476Ssamstatic	void skipjack_zerokey(u_int8_t **);
94104476Ssamstatic	void rijndael128_zerokey(u_int8_t **);
95104476Ssam
96104476Ssamstatic	void null_init(void *);
97104476Ssamstatic	int null_update(void *, u_int8_t *, u_int16_t);
98104476Ssamstatic	void null_final(u_int8_t *, void *);
99104476Ssamstatic	int MD5Update_int(void *, u_int8_t *, u_int16_t);
100104476Ssamstatic	void SHA1Init_int(void *);
101104476Ssamstatic	int SHA1Update_int(void *, u_int8_t *, u_int16_t);
102104476Ssamstatic	void SHA1Final_int(u_int8_t *, void *);
103104476Ssamstatic	int RMD160Update_int(void *, u_int8_t *, u_int16_t);
104104476Ssamstatic	int SHA256Update_int(void *, u_int8_t *, u_int16_t);
105104476Ssamstatic	int SHA384Update_int(void *, u_int8_t *, u_int16_t);
106104476Ssamstatic	int SHA512Update_int(void *, u_int8_t *, u_int16_t);
107104476Ssam
108104476Ssamstatic	u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
109104476Ssamstatic	u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
110104476Ssam
111104476SsamMALLOC_DEFINE(M_XDATA, "xform", "xform data buffers");
112104476Ssam
113104476Ssam/* Encryption instances */
114104476Ssamstruct enc_xform enc_xform_null = {
115104476Ssam	CRYPTO_NULL_CBC, "NULL",
116104476Ssam	/* NB: blocksize of 4 is to generate a properly aligned ESP header */
117104476Ssam	4, 0, 256, /* 2048 bits, max key */
118104476Ssam	null_encrypt,
119104476Ssam	null_decrypt,
120104476Ssam	null_setkey,
121104476Ssam	null_zerokey,
122104476Ssam};
123104476Ssam
124104476Ssamstruct enc_xform enc_xform_des = {
125104476Ssam	CRYPTO_DES_CBC, "DES",
126104476Ssam	8, 8, 8,
127104476Ssam	des1_encrypt,
128104476Ssam	des1_decrypt,
129104476Ssam	des1_setkey,
130104476Ssam	des1_zerokey,
131104476Ssam};
132104476Ssam
133104476Ssamstruct enc_xform enc_xform_3des = {
134104476Ssam	CRYPTO_3DES_CBC, "3DES",
135104476Ssam	8, 24, 24,
136104476Ssam	des3_encrypt,
137104476Ssam	des3_decrypt,
138104476Ssam	des3_setkey,
139104476Ssam	des3_zerokey
140104476Ssam};
141104476Ssam
142104476Ssamstruct enc_xform enc_xform_blf = {
143104476Ssam	CRYPTO_BLF_CBC, "Blowfish",
144104476Ssam	8, 5, 56 /* 448 bits, max key */,
145104476Ssam	blf_encrypt,
146104476Ssam	blf_decrypt,
147104476Ssam	blf_setkey,
148104476Ssam	blf_zerokey
149104476Ssam};
150104476Ssam
151104476Ssamstruct enc_xform enc_xform_cast5 = {
152104476Ssam	CRYPTO_CAST_CBC, "CAST-128",
153104476Ssam	8, 5, 16,
154104476Ssam	cast5_encrypt,
155104476Ssam	cast5_decrypt,
156104476Ssam	cast5_setkey,
157104476Ssam	cast5_zerokey
158104476Ssam};
159104476Ssam
160104476Ssamstruct enc_xform enc_xform_skipjack = {
161104476Ssam	CRYPTO_SKIPJACK_CBC, "Skipjack",
162104476Ssam	8, 10, 10,
163104476Ssam	skipjack_encrypt,
164104476Ssam	skipjack_decrypt,
165104476Ssam	skipjack_setkey,
166104476Ssam	skipjack_zerokey
167104476Ssam};
168104476Ssam
169104476Ssamstruct enc_xform enc_xform_rijndael128 = {
170104476Ssam	CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
171104476Ssam	16, 8, 32,
172104476Ssam	rijndael128_encrypt,
173104476Ssam	rijndael128_decrypt,
174104476Ssam	rijndael128_setkey,
175104476Ssam	rijndael128_zerokey,
176104476Ssam};
177104476Ssam
178104476Ssamstruct enc_xform enc_xform_arc4 = {
179104476Ssam	CRYPTO_ARC4, "ARC4",
180104476Ssam	1, 1, 32,
181104476Ssam	NULL,
182104476Ssam	NULL,
183104476Ssam	NULL,
184104476Ssam	NULL,
185104476Ssam};
186104476Ssam
187104476Ssam/* Authentication instances */
188104476Ssamstruct auth_hash auth_hash_null = {
189104476Ssam	CRYPTO_NULL_HMAC, "NULL-HMAC",
190158703Spjd	0, 16, 64, sizeof(int),			/* NB: context isn't used */
191104476Ssam	null_init, null_update, null_final
192104476Ssam};
193104476Ssam
194158703Spjdstruct auth_hash auth_hash_hmac_md5 = {
195104476Ssam	CRYPTO_MD5_HMAC, "HMAC-MD5",
196158703Spjd	16, 16, 64, sizeof(MD5_CTX),
197104476Ssam	(void (*) (void *)) MD5Init, MD5Update_int,
198104476Ssam	(void (*) (u_int8_t *, void *)) MD5Final
199104476Ssam};
200104476Ssam
201158703Spjdstruct auth_hash auth_hash_hmac_sha1 = {
202104476Ssam	CRYPTO_SHA1_HMAC, "HMAC-SHA1",
203158703Spjd	20, 20, 64, sizeof(SHA1_CTX),
204104476Ssam	SHA1Init_int, SHA1Update_int, SHA1Final_int
205104476Ssam};
206104476Ssam
207158703Spjdstruct auth_hash auth_hash_hmac_ripemd_160 = {
208104476Ssam	CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
209158703Spjd	20, 20, 64, sizeof(RMD160_CTX),
210104476Ssam	(void (*)(void *)) RMD160Init, RMD160Update_int,
211104476Ssam	(void (*)(u_int8_t *, void *)) RMD160Final
212104476Ssam};
213104476Ssam
214104476Ssamstruct auth_hash auth_hash_key_md5 = {
215104476Ssam	CRYPTO_MD5_KPDK, "Keyed MD5",
216158703Spjd	0, 16, 0, sizeof(MD5_CTX),
217104476Ssam	(void (*)(void *)) MD5Init, MD5Update_int,
218104476Ssam	(void (*)(u_int8_t *, void *)) MD5Final
219104476Ssam};
220104476Ssam
221104476Ssamstruct auth_hash auth_hash_key_sha1 = {
222104476Ssam	CRYPTO_SHA1_KPDK, "Keyed SHA1",
223158703Spjd	0, 20, 0, sizeof(SHA1_CTX),
224104476Ssam	SHA1Init_int, SHA1Update_int, SHA1Final_int
225104476Ssam};
226104476Ssam
227104476Ssamstruct auth_hash auth_hash_hmac_sha2_256 = {
228158703Spjd	CRYPTO_SHA2_256_HMAC, "HMAC-SHA2-256",
229158703Spjd	32, 32, 64, sizeof(SHA256_CTX),
230104476Ssam	(void (*)(void *)) SHA256_Init, SHA256Update_int,
231104476Ssam	(void (*)(u_int8_t *, void *)) SHA256_Final
232104476Ssam};
233104476Ssam
234104476Ssamstruct auth_hash auth_hash_hmac_sha2_384 = {
235158703Spjd	CRYPTO_SHA2_384_HMAC, "HMAC-SHA2-384",
236158703Spjd	48, 48, 128, sizeof(SHA384_CTX),
237104476Ssam	(void (*)(void *)) SHA384_Init, SHA384Update_int,
238104476Ssam	(void (*)(u_int8_t *, void *)) SHA384_Final
239104476Ssam};
240104476Ssam
241104476Ssamstruct auth_hash auth_hash_hmac_sha2_512 = {
242158703Spjd	CRYPTO_SHA2_512_HMAC, "HMAC-SHA2-512",
243158703Spjd	64, 64, 128, sizeof(SHA512_CTX),
244104476Ssam	(void (*)(void *)) SHA512_Init, SHA512Update_int,
245104476Ssam	(void (*)(u_int8_t *, void *)) SHA512_Final
246104476Ssam};
247104476Ssam
248104476Ssam/* Compression instance */
249104476Ssamstruct comp_algo comp_algo_deflate = {
250104476Ssam	CRYPTO_DEFLATE_COMP, "Deflate",
251104476Ssam	90, deflate_compress,
252104476Ssam	deflate_decompress
253104476Ssam};
254104476Ssam
255104476Ssam/*
256104476Ssam * Encryption wrapper routines.
257104476Ssam */
258104476Ssamstatic void
259104476Ssamnull_encrypt(caddr_t key, u_int8_t *blk)
260104476Ssam{
261104476Ssam}
262104476Ssamstatic void
263104476Ssamnull_decrypt(caddr_t key, u_int8_t *blk)
264104476Ssam{
265104476Ssam}
266104476Ssamstatic int
267104476Ssamnull_setkey(u_int8_t **sched, u_int8_t *key, int len)
268104476Ssam{
269104476Ssam	*sched = NULL;
270104476Ssam	return 0;
271104476Ssam}
272104476Ssamstatic void
273104476Ssamnull_zerokey(u_int8_t **sched)
274104476Ssam{
275104476Ssam	*sched = NULL;
276104476Ssam}
277104476Ssam
278104476Ssamstatic void
279104476Ssamdes1_encrypt(caddr_t key, u_int8_t *blk)
280104476Ssam{
281104476Ssam	des_cblock *cb = (des_cblock *) blk;
282104476Ssam	des_key_schedule *p = (des_key_schedule *) key;
283104476Ssam
284104476Ssam	des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
285104476Ssam}
286104476Ssam
287104476Ssamstatic void
288104476Ssamdes1_decrypt(caddr_t key, u_int8_t *blk)
289104476Ssam{
290104476Ssam	des_cblock *cb = (des_cblock *) blk;
291104476Ssam	des_key_schedule *p = (des_key_schedule *) key;
292104476Ssam
293104476Ssam	des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
294104476Ssam}
295104476Ssam
296104476Ssamstatic int
297104476Ssamdes1_setkey(u_int8_t **sched, u_int8_t *key, int len)
298104476Ssam{
299104476Ssam	des_key_schedule *p;
300104476Ssam	int err;
301104476Ssam
302104476Ssam	MALLOC(p, des_key_schedule *, sizeof (des_key_schedule),
303104476Ssam		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
304104476Ssam	if (p != NULL) {
305104476Ssam		des_set_key((des_cblock *) key, p[0]);
306104476Ssam		err = 0;
307104476Ssam	} else
308104476Ssam		err = ENOMEM;
309104476Ssam	*sched = (u_int8_t *) p;
310104476Ssam	return err;
311104476Ssam}
312104476Ssam
313104476Ssamstatic void
314104476Ssamdes1_zerokey(u_int8_t **sched)
315104476Ssam{
316104476Ssam	bzero(*sched, sizeof (des_key_schedule));
317104476Ssam	FREE(*sched, M_CRYPTO_DATA);
318104476Ssam	*sched = NULL;
319104476Ssam}
320104476Ssam
321104476Ssamstatic void
322104476Ssamdes3_encrypt(caddr_t key, u_int8_t *blk)
323104476Ssam{
324104476Ssam	des_cblock *cb = (des_cblock *) blk;
325104476Ssam	des_key_schedule *p = (des_key_schedule *) key;
326104476Ssam
327104476Ssam	des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
328104476Ssam}
329104476Ssam
330104476Ssamstatic void
331104476Ssamdes3_decrypt(caddr_t key, u_int8_t *blk)
332104476Ssam{
333104476Ssam	des_cblock *cb = (des_cblock *) blk;
334104476Ssam	des_key_schedule *p = (des_key_schedule *) key;
335104476Ssam
336104476Ssam	des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
337104476Ssam}
338104476Ssam
339104476Ssamstatic int
340104476Ssamdes3_setkey(u_int8_t **sched, u_int8_t *key, int len)
341104476Ssam{
342104476Ssam	des_key_schedule *p;
343104476Ssam	int err;
344104476Ssam
345104476Ssam	MALLOC(p, des_key_schedule *, 3*sizeof (des_key_schedule),
346104476Ssam		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
347104476Ssam	if (p != NULL) {
348104476Ssam		des_set_key((des_cblock *)(key +  0), p[0]);
349104476Ssam		des_set_key((des_cblock *)(key +  8), p[1]);
350104476Ssam		des_set_key((des_cblock *)(key + 16), p[2]);
351104476Ssam		err = 0;
352104476Ssam	} else
353104476Ssam		err = ENOMEM;
354104476Ssam	*sched = (u_int8_t *) p;
355104476Ssam	return err;
356104476Ssam}
357104476Ssam
358104476Ssamstatic void
359104476Ssamdes3_zerokey(u_int8_t **sched)
360104476Ssam{
361104476Ssam	bzero(*sched, 3*sizeof (des_key_schedule));
362104476Ssam	FREE(*sched, M_CRYPTO_DATA);
363104476Ssam	*sched = NULL;
364104476Ssam}
365104476Ssam
366104476Ssamstatic void
367104476Ssamblf_encrypt(caddr_t key, u_int8_t *blk)
368104476Ssam{
369104476Ssam	BF_LONG t[2];
370104476Ssam
371104476Ssam	memcpy(t, blk, sizeof (t));
372104476Ssam	t[0] = ntohl(t[0]);
373104476Ssam	t[1] = ntohl(t[1]);
374104476Ssam	/* NB: BF_encrypt expects the block in host order! */
375104476Ssam	BF_encrypt(t, (BF_KEY *) key);
376104476Ssam	t[0] = htonl(t[0]);
377104476Ssam	t[1] = htonl(t[1]);
378104476Ssam	memcpy(blk, t, sizeof (t));
379104476Ssam}
380104476Ssam
381104476Ssamstatic void
382104476Ssamblf_decrypt(caddr_t key, u_int8_t *blk)
383104476Ssam{
384104476Ssam	BF_LONG t[2];
385104476Ssam
386104476Ssam	memcpy(t, blk, sizeof (t));
387104476Ssam	t[0] = ntohl(t[0]);
388104476Ssam	t[1] = ntohl(t[1]);
389104476Ssam	/* NB: BF_decrypt expects the block in host order! */
390104476Ssam	BF_decrypt(t, (BF_KEY *) key);
391104476Ssam	t[0] = htonl(t[0]);
392104476Ssam	t[1] = htonl(t[1]);
393104476Ssam	memcpy(blk, t, sizeof (t));
394104476Ssam}
395104476Ssam
396104476Ssamstatic int
397104476Ssamblf_setkey(u_int8_t **sched, u_int8_t *key, int len)
398104476Ssam{
399104476Ssam	int err;
400104476Ssam
401104476Ssam	MALLOC(*sched, u_int8_t *, sizeof(BF_KEY),
402104476Ssam		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
403104476Ssam	if (*sched != NULL) {
404104476Ssam		BF_set_key((BF_KEY *) *sched, len, key);
405104476Ssam		err = 0;
406104476Ssam	} else
407104476Ssam		err = ENOMEM;
408104476Ssam	return err;
409104476Ssam}
410104476Ssam
411104476Ssamstatic void
412104476Ssamblf_zerokey(u_int8_t **sched)
413104476Ssam{
414104476Ssam	bzero(*sched, sizeof(BF_KEY));
415104476Ssam	FREE(*sched, M_CRYPTO_DATA);
416104476Ssam	*sched = NULL;
417104476Ssam}
418104476Ssam
419104476Ssamstatic void
420104476Ssamcast5_encrypt(caddr_t key, u_int8_t *blk)
421104476Ssam{
422104476Ssam	cast_encrypt((cast_key *) key, blk, blk);
423104476Ssam}
424104476Ssam
425104476Ssamstatic void
426104476Ssamcast5_decrypt(caddr_t key, u_int8_t *blk)
427104476Ssam{
428104476Ssam	cast_decrypt((cast_key *) key, blk, blk);
429104476Ssam}
430104476Ssam
431104476Ssamstatic int
432104476Ssamcast5_setkey(u_int8_t **sched, u_int8_t *key, int len)
433104476Ssam{
434104476Ssam	int err;
435104476Ssam
436104476Ssam	MALLOC(*sched, u_int8_t *, sizeof(cast_key), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
437104476Ssam	if (*sched != NULL) {
438104476Ssam		cast_setkey((cast_key *)*sched, key, len);
439104476Ssam		err = 0;
440104476Ssam	} else
441104476Ssam		err = ENOMEM;
442104476Ssam	return err;
443104476Ssam}
444104476Ssam
445104476Ssamstatic void
446104476Ssamcast5_zerokey(u_int8_t **sched)
447104476Ssam{
448104476Ssam	bzero(*sched, sizeof(cast_key));
449104476Ssam	FREE(*sched, M_CRYPTO_DATA);
450104476Ssam	*sched = NULL;
451104476Ssam}
452104476Ssam
453104476Ssamstatic void
454104476Ssamskipjack_encrypt(caddr_t key, u_int8_t *blk)
455104476Ssam{
456104476Ssam	skipjack_forwards(blk, blk, (u_int8_t **) key);
457104476Ssam}
458104476Ssam
459104476Ssamstatic void
460104476Ssamskipjack_decrypt(caddr_t key, u_int8_t *blk)
461104476Ssam{
462104476Ssam	skipjack_backwards(blk, blk, (u_int8_t **) key);
463104476Ssam}
464104476Ssam
465104476Ssamstatic int
466104476Ssamskipjack_setkey(u_int8_t **sched, u_int8_t *key, int len)
467104476Ssam{
468104476Ssam	int err;
469104476Ssam
470104476Ssam	/* NB: allocate all the memory that's needed at once */
471104476Ssam	MALLOC(*sched, u_int8_t *, 10 * (sizeof(u_int8_t *) + 0x100),
472104476Ssam		M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
473104476Ssam	if (*sched != NULL) {
474104476Ssam		u_int8_t** key_tables = (u_int8_t**) *sched;
475104476Ssam		u_int8_t* table = (u_int8_t*) &key_tables[10];
476104476Ssam		int k;
477104476Ssam
478104476Ssam		for (k = 0; k < 10; k++) {
479104476Ssam			key_tables[k] = table;
480104476Ssam			table += 0x100;
481104476Ssam		}
482104476Ssam		subkey_table_gen(key, (u_int8_t **) *sched);
483104476Ssam		err = 0;
484104476Ssam	} else
485104476Ssam		err = ENOMEM;
486104476Ssam	return err;
487104476Ssam}
488104476Ssam
489104476Ssamstatic void
490104476Ssamskipjack_zerokey(u_int8_t **sched)
491104476Ssam{
492104476Ssam	bzero(*sched, 10 * (sizeof(u_int8_t *) + 0x100));
493104476Ssam	FREE(*sched, M_CRYPTO_DATA);
494104476Ssam	*sched = NULL;
495104476Ssam}
496104476Ssam
497104476Ssamstatic void
498104476Ssamrijndael128_encrypt(caddr_t key, u_int8_t *blk)
499104476Ssam{
500104476Ssam	rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
501104476Ssam}
502104476Ssam
503104476Ssamstatic void
504104476Ssamrijndael128_decrypt(caddr_t key, u_int8_t *blk)
505104476Ssam{
506143408Sume	rijndael_decrypt(((rijndael_ctx *) key), (u_char *) blk,
507104476Ssam	    (u_char *) blk);
508104476Ssam}
509104476Ssam
510104476Ssamstatic int
511104476Ssamrijndael128_setkey(u_int8_t **sched, u_int8_t *key, int len)
512104476Ssam{
513104476Ssam	int err;
514104476Ssam
515149143Spjd	if (len != 16 && len != 24 && len != 32)
516149143Spjd		return (EINVAL);
517143408Sume	MALLOC(*sched, u_int8_t *, sizeof(rijndael_ctx), M_CRYPTO_DATA,
518104476Ssam	    M_NOWAIT|M_ZERO);
519104476Ssam	if (*sched != NULL) {
520143408Sume		rijndael_set_key((rijndael_ctx *) *sched, (u_char *) key,
521143408Sume		    len * 8);
522104476Ssam		err = 0;
523104476Ssam	} else
524104476Ssam		err = ENOMEM;
525104476Ssam	return err;
526104476Ssam}
527104476Ssam
528104476Ssamstatic void
529104476Ssamrijndael128_zerokey(u_int8_t **sched)
530104476Ssam{
531143408Sume	bzero(*sched, sizeof(rijndael_ctx));
532104476Ssam	FREE(*sched, M_CRYPTO_DATA);
533104476Ssam	*sched = NULL;
534104476Ssam}
535104476Ssam
536104476Ssam/*
537104476Ssam * And now for auth.
538104476Ssam */
539104476Ssam
540104476Ssamstatic void
541104476Ssamnull_init(void *ctx)
542104476Ssam{
543104476Ssam}
544104476Ssam
545104476Ssamstatic int
546104476Ssamnull_update(void *ctx, u_int8_t *buf, u_int16_t len)
547104476Ssam{
548104476Ssam	return 0;
549104476Ssam}
550104476Ssam
551104476Ssamstatic void
552104476Ssamnull_final(u_int8_t *buf, void *ctx)
553104476Ssam{
554104476Ssam	if (buf != (u_int8_t *) 0)
555104476Ssam		bzero(buf, 12);
556104476Ssam}
557104476Ssam
558104476Ssamstatic int
559104476SsamRMD160Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
560104476Ssam{
561104476Ssam	RMD160Update(ctx, buf, len);
562104476Ssam	return 0;
563104476Ssam}
564104476Ssam
565104476Ssamstatic int
566104476SsamMD5Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
567104476Ssam{
568104476Ssam	MD5Update(ctx, buf, len);
569104476Ssam	return 0;
570104476Ssam}
571104476Ssam
572104476Ssamstatic void
573104476SsamSHA1Init_int(void *ctx)
574104476Ssam{
575104476Ssam	SHA1Init(ctx);
576104476Ssam}
577104476Ssam
578104476Ssamstatic int
579104476SsamSHA1Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
580104476Ssam{
581104476Ssam	SHA1Update(ctx, buf, len);
582104476Ssam	return 0;
583104476Ssam}
584104476Ssam
585104476Ssamstatic void
586104476SsamSHA1Final_int(u_int8_t *blk, void *ctx)
587104476Ssam{
588104476Ssam	SHA1Final(blk, ctx);
589104476Ssam}
590104476Ssam
591104476Ssamstatic int
592104476SsamSHA256Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
593104476Ssam{
594104476Ssam	SHA256_Update(ctx, buf, len);
595104476Ssam	return 0;
596104476Ssam}
597104476Ssam
598104476Ssamstatic int
599104476SsamSHA384Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
600104476Ssam{
601104476Ssam	SHA384_Update(ctx, buf, len);
602104476Ssam	return 0;
603104476Ssam}
604104476Ssam
605104476Ssamstatic int
606104476SsamSHA512Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
607104476Ssam{
608104476Ssam	SHA512_Update(ctx, buf, len);
609104476Ssam	return 0;
610104476Ssam}
611104476Ssam
612104476Ssam/*
613104476Ssam * And compression
614104476Ssam */
615104476Ssam
616104476Ssamstatic u_int32_t
617104476Ssamdeflate_compress(data, size, out)
618104476Ssam	u_int8_t *data;
619104476Ssam	u_int32_t size;
620104476Ssam	u_int8_t **out;
621104476Ssam{
622104476Ssam	return deflate_global(data, size, 0, out);
623104476Ssam}
624104476Ssam
625104476Ssamstatic u_int32_t
626104476Ssamdeflate_decompress(data, size, out)
627104476Ssam	u_int8_t *data;
628104476Ssam	u_int32_t size;
629104476Ssam	u_int8_t **out;
630104476Ssam{
631104476Ssam	return deflate_global(data, size, 1, out);
632104476Ssam}
633