cryptosoft_xform.c revision 1.17 1 1.17 drochner /* $NetBSD: cryptosoft_xform.c,v 1.17 2011/05/23 13:46:54 drochner Exp $ */
2 1.1 thorpej /* $FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $ */
3 1.1 thorpej /* $OpenBSD: xform.c,v 1.19 2002/08/16 22:47:25 dhartmei Exp $ */
4 1.1 thorpej
5 1.1 thorpej /*
6 1.1 thorpej * The authors of this code are John Ioannidis (ji (at) tla.org),
7 1.1 thorpej * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
8 1.1 thorpej * Niels Provos (provos (at) physnet.uni-hamburg.de).
9 1.1 thorpej *
10 1.1 thorpej * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
11 1.1 thorpej * in November 1995.
12 1.1 thorpej *
13 1.1 thorpej * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
14 1.1 thorpej * by Angelos D. Keromytis.
15 1.1 thorpej *
16 1.1 thorpej * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17 1.1 thorpej * and Niels Provos.
18 1.1 thorpej *
19 1.1 thorpej * Additional features in 1999 by Angelos D. Keromytis.
20 1.1 thorpej *
21 1.1 thorpej * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
22 1.1 thorpej * Angelos D. Keromytis and Niels Provos.
23 1.1 thorpej *
24 1.1 thorpej * Copyright (C) 2001, Angelos D. Keromytis.
25 1.1 thorpej *
26 1.1 thorpej * Permission to use, copy, and modify this software with or without fee
27 1.1 thorpej * is hereby granted, provided that this entire notice is included in
28 1.1 thorpej * all copies of any software which is or includes a copy or
29 1.1 thorpej * modification of this software.
30 1.1 thorpej * You may use this code under the GNU public license if you so wish. Please
31 1.1 thorpej * contribute changes back to the authors under this freer than GPL license
32 1.1 thorpej * so that we may further the use of strong encryption without limitations to
33 1.1 thorpej * all.
34 1.1 thorpej *
35 1.1 thorpej * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
36 1.1 thorpej * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
37 1.1 thorpej * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
38 1.1 thorpej * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
39 1.1 thorpej * PURPOSE.
40 1.1 thorpej */
41 1.1 thorpej
42 1.1 thorpej #include <sys/cdefs.h>
43 1.17 drochner __KERNEL_RCSID(1, "$NetBSD: cryptosoft_xform.c,v 1.17 2011/05/23 13:46:54 drochner Exp $");
44 1.1 thorpej
45 1.1 thorpej #include <crypto/blowfish/blowfish.h>
46 1.1 thorpej #include <crypto/cast128/cast128.h>
47 1.1 thorpej #include <crypto/des/des.h>
48 1.1 thorpej #include <crypto/rijndael/rijndael.h>
49 1.1 thorpej #include <crypto/skipjack/skipjack.h>
50 1.15 drochner #include <crypto/camellia/camellia.h>
51 1.1 thorpej
52 1.1 thorpej #include <opencrypto/deflate.h>
53 1.1 thorpej
54 1.1 thorpej #include <sys/md5.h>
55 1.3 christos #include <sys/rmd160.h>
56 1.1 thorpej #include <sys/sha1.h>
57 1.1 thorpej
58 1.1 thorpej struct swcr_auth_hash {
59 1.13 drochner const struct auth_hash *auth_hash;
60 1.1 thorpej void (*Init)(void *);
61 1.1 thorpej int (*Update)(void *, const uint8_t *, uint16_t);
62 1.1 thorpej void (*Final)(uint8_t *, void *);
63 1.1 thorpej };
64 1.1 thorpej
65 1.1 thorpej struct swcr_enc_xform {
66 1.13 drochner const struct enc_xform *enc_xform;
67 1.5 christos void (*encrypt)(void *, uint8_t *);
68 1.5 christos void (*decrypt)(void *, uint8_t *);
69 1.16 drochner int (*setkey)(uint8_t **, const uint8_t *, int);
70 1.1 thorpej void (*zerokey)(uint8_t **);
71 1.17 drochner void (*reinit)(void *, const uint8_t *);
72 1.1 thorpej };
73 1.1 thorpej
74 1.1 thorpej struct swcr_comp_algo {
75 1.14 drochner const struct comp_algo *unused_comp_algo;
76 1.1 thorpej uint32_t (*compress)(uint8_t *, uint32_t, uint8_t **);
77 1.14 drochner uint32_t (*decompress)(uint8_t *, uint32_t, uint8_t **, int);
78 1.1 thorpej };
79 1.1 thorpej
80 1.5 christos static void null_encrypt(void *, u_int8_t *);
81 1.5 christos static void null_decrypt(void *, u_int8_t *);
82 1.1 thorpej static int null_setkey(u_int8_t **, const u_int8_t *, int);
83 1.1 thorpej static void null_zerokey(u_int8_t **);
84 1.1 thorpej
85 1.1 thorpej static int des1_setkey(u_int8_t **, const u_int8_t *, int);
86 1.1 thorpej static int des3_setkey(u_int8_t **, const u_int8_t *, int);
87 1.1 thorpej static int blf_setkey(u_int8_t **, const u_int8_t *, int);
88 1.1 thorpej static int cast5_setkey(u_int8_t **, const u_int8_t *, int);
89 1.1 thorpej static int skipjack_setkey(u_int8_t **, const u_int8_t *, int);
90 1.1 thorpej static int rijndael128_setkey(u_int8_t **, const u_int8_t *, int);
91 1.15 drochner static int cml_setkey(u_int8_t **, const u_int8_t *, int);
92 1.5 christos static void des1_encrypt(void *, u_int8_t *);
93 1.5 christos static void des3_encrypt(void *, u_int8_t *);
94 1.5 christos static void blf_encrypt(void *, u_int8_t *);
95 1.5 christos static void cast5_encrypt(void *, u_int8_t *);
96 1.5 christos static void skipjack_encrypt(void *, u_int8_t *);
97 1.5 christos static void rijndael128_encrypt(void *, u_int8_t *);
98 1.15 drochner static void cml_encrypt(void *, u_int8_t *);
99 1.5 christos static void des1_decrypt(void *, u_int8_t *);
100 1.5 christos static void des3_decrypt(void *, u_int8_t *);
101 1.5 christos static void blf_decrypt(void *, u_int8_t *);
102 1.5 christos static void cast5_decrypt(void *, u_int8_t *);
103 1.5 christos static void skipjack_decrypt(void *, u_int8_t *);
104 1.5 christos static void rijndael128_decrypt(void *, u_int8_t *);
105 1.15 drochner static void cml_decrypt(void *, u_int8_t *);
106 1.1 thorpej static void des1_zerokey(u_int8_t **);
107 1.1 thorpej static void des3_zerokey(u_int8_t **);
108 1.1 thorpej static void blf_zerokey(u_int8_t **);
109 1.1 thorpej static void cast5_zerokey(u_int8_t **);
110 1.1 thorpej static void skipjack_zerokey(u_int8_t **);
111 1.1 thorpej static void rijndael128_zerokey(u_int8_t **);
112 1.15 drochner static void cml_zerokey(u_int8_t **);
113 1.1 thorpej
114 1.1 thorpej static void null_init(void *);
115 1.1 thorpej static int null_update(void *, const u_int8_t *, u_int16_t);
116 1.1 thorpej static void null_final(u_int8_t *, void *);
117 1.1 thorpej
118 1.1 thorpej static int MD5Update_int(void *, const u_int8_t *, u_int16_t);
119 1.1 thorpej static void SHA1Init_int(void *);
120 1.1 thorpej static int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
121 1.1 thorpej static void SHA1Final_int(u_int8_t *, void *);
122 1.1 thorpej
123 1.1 thorpej
124 1.1 thorpej static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
125 1.1 thorpej static int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
126 1.1 thorpej static void SHA1Final_int(u_int8_t *, void *);
127 1.1 thorpej static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
128 1.1 thorpej static int SHA256Update_int(void *, const u_int8_t *, u_int16_t);
129 1.1 thorpej static int SHA384Update_int(void *, const u_int8_t *, u_int16_t);
130 1.1 thorpej static int SHA512Update_int(void *, const u_int8_t *, u_int16_t);
131 1.1 thorpej
132 1.1 thorpej static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
133 1.14 drochner static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **, int);
134 1.12 darran static u_int32_t gzip_compress(u_int8_t *, u_int32_t, u_int8_t **);
135 1.14 drochner static u_int32_t gzip_decompress(u_int8_t *, u_int32_t, u_int8_t **, int);
136 1.1 thorpej
137 1.1 thorpej /* Encryption instances */
138 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_null = {
139 1.1 thorpej &enc_xform_null,
140 1.1 thorpej null_encrypt,
141 1.1 thorpej null_decrypt,
142 1.1 thorpej null_setkey,
143 1.1 thorpej null_zerokey,
144 1.17 drochner NULL
145 1.1 thorpej };
146 1.1 thorpej
147 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_des = {
148 1.1 thorpej &enc_xform_des,
149 1.1 thorpej des1_encrypt,
150 1.1 thorpej des1_decrypt,
151 1.1 thorpej des1_setkey,
152 1.1 thorpej des1_zerokey,
153 1.17 drochner NULL
154 1.1 thorpej };
155 1.1 thorpej
156 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_3des = {
157 1.1 thorpej &enc_xform_3des,
158 1.1 thorpej des3_encrypt,
159 1.1 thorpej des3_decrypt,
160 1.1 thorpej des3_setkey,
161 1.17 drochner des3_zerokey,
162 1.17 drochner NULL
163 1.1 thorpej };
164 1.1 thorpej
165 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_blf = {
166 1.1 thorpej &enc_xform_blf,
167 1.1 thorpej blf_encrypt,
168 1.1 thorpej blf_decrypt,
169 1.1 thorpej blf_setkey,
170 1.17 drochner blf_zerokey,
171 1.17 drochner NULL
172 1.1 thorpej };
173 1.1 thorpej
174 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_cast5 = {
175 1.1 thorpej &enc_xform_cast5,
176 1.1 thorpej cast5_encrypt,
177 1.1 thorpej cast5_decrypt,
178 1.1 thorpej cast5_setkey,
179 1.17 drochner cast5_zerokey,
180 1.17 drochner NULL
181 1.1 thorpej };
182 1.1 thorpej
183 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_skipjack = {
184 1.1 thorpej &enc_xform_skipjack,
185 1.1 thorpej skipjack_encrypt,
186 1.1 thorpej skipjack_decrypt,
187 1.1 thorpej skipjack_setkey,
188 1.17 drochner skipjack_zerokey,
189 1.17 drochner NULL
190 1.1 thorpej };
191 1.1 thorpej
192 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_rijndael128 = {
193 1.1 thorpej &enc_xform_rijndael128,
194 1.1 thorpej rijndael128_encrypt,
195 1.1 thorpej rijndael128_decrypt,
196 1.1 thorpej rijndael128_setkey,
197 1.1 thorpej rijndael128_zerokey,
198 1.17 drochner NULL
199 1.1 thorpej };
200 1.1 thorpej
201 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_arc4 = {
202 1.1 thorpej &enc_xform_arc4,
203 1.1 thorpej NULL,
204 1.1 thorpej NULL,
205 1.1 thorpej NULL,
206 1.1 thorpej NULL,
207 1.17 drochner NULL
208 1.1 thorpej };
209 1.1 thorpej
210 1.15 drochner static const struct swcr_enc_xform swcr_enc_xform_camellia = {
211 1.15 drochner &enc_xform_camellia,
212 1.15 drochner cml_encrypt,
213 1.15 drochner cml_decrypt,
214 1.15 drochner cml_setkey,
215 1.17 drochner cml_zerokey,
216 1.17 drochner NULL
217 1.15 drochner };
218 1.15 drochner
219 1.1 thorpej /* Authentication instances */
220 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_null = {
221 1.1 thorpej &auth_hash_null,
222 1.1 thorpej null_init, null_update, null_final
223 1.1 thorpej };
224 1.1 thorpej
225 1.7 tls static const struct swcr_auth_hash swcr_auth_hash_hmac_md5 = {
226 1.7 tls &auth_hash_hmac_md5,
227 1.7 tls (void (*) (void *)) MD5Init, MD5Update_int,
228 1.7 tls (void (*) (u_int8_t *, void *)) MD5Final
229 1.7 tls };
230 1.7 tls
231 1.7 tls static const struct swcr_auth_hash swcr_auth_hash_hmac_sha1 = {
232 1.7 tls &auth_hash_hmac_sha1,
233 1.7 tls SHA1Init_int, SHA1Update_int, SHA1Final_int
234 1.7 tls };
235 1.7 tls
236 1.7 tls static const struct swcr_auth_hash swcr_auth_hash_hmac_ripemd_160 = {
237 1.7 tls &auth_hash_hmac_ripemd_160,
238 1.7 tls (void (*)(void *)) RMD160Init, RMD160Update_int,
239 1.7 tls (void (*)(u_int8_t *, void *)) RMD160Final
240 1.7 tls };
241 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_md5_96 = {
242 1.1 thorpej &auth_hash_hmac_md5_96,
243 1.1 thorpej (void (*) (void *)) MD5Init, MD5Update_int,
244 1.1 thorpej (void (*) (u_int8_t *, void *)) MD5Final
245 1.1 thorpej };
246 1.1 thorpej
247 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha1_96 = {
248 1.1 thorpej &auth_hash_hmac_sha1_96,
249 1.1 thorpej SHA1Init_int, SHA1Update_int, SHA1Final_int
250 1.1 thorpej };
251 1.1 thorpej
252 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_ripemd_160_96 = {
253 1.1 thorpej &auth_hash_hmac_ripemd_160_96,
254 1.1 thorpej (void (*)(void *)) RMD160Init, RMD160Update_int,
255 1.1 thorpej (void (*)(u_int8_t *, void *)) RMD160Final
256 1.1 thorpej };
257 1.1 thorpej
258 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_key_md5 = {
259 1.1 thorpej &auth_hash_key_md5,
260 1.1 thorpej (void (*)(void *)) MD5Init, MD5Update_int,
261 1.1 thorpej (void (*)(u_int8_t *, void *)) MD5Final
262 1.1 thorpej };
263 1.1 thorpej
264 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_key_sha1 = {
265 1.1 thorpej &auth_hash_key_sha1,
266 1.1 thorpej SHA1Init_int, SHA1Update_int, SHA1Final_int
267 1.1 thorpej };
268 1.1 thorpej
269 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_md5 = {
270 1.1 thorpej &auth_hash_md5,
271 1.1 thorpej (void (*) (void *)) MD5Init, MD5Update_int,
272 1.1 thorpej (void (*) (u_int8_t *, void *)) MD5Final
273 1.1 thorpej };
274 1.1 thorpej
275 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_sha1 = {
276 1.1 thorpej &auth_hash_sha1,
277 1.1 thorpej (void (*)(void *)) SHA1Init, SHA1Update_int,
278 1.1 thorpej (void (*)(u_int8_t *, void *)) SHA1Final
279 1.1 thorpej };
280 1.1 thorpej
281 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_256 = {
282 1.1 thorpej &auth_hash_hmac_sha2_256,
283 1.1 thorpej (void (*)(void *)) SHA256_Init, SHA256Update_int,
284 1.1 thorpej (void (*)(u_int8_t *, void *)) SHA256_Final
285 1.1 thorpej };
286 1.1 thorpej
287 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_384 = {
288 1.1 thorpej &auth_hash_hmac_sha2_384,
289 1.1 thorpej (void (*)(void *)) SHA384_Init, SHA384Update_int,
290 1.1 thorpej (void (*)(u_int8_t *, void *)) SHA384_Final
291 1.1 thorpej };
292 1.1 thorpej
293 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_512 = {
294 1.1 thorpej &auth_hash_hmac_sha2_384,
295 1.1 thorpej (void (*)(void *)) SHA512_Init, SHA512Update_int,
296 1.1 thorpej (void (*)(u_int8_t *, void *)) SHA512_Final
297 1.1 thorpej };
298 1.1 thorpej
299 1.1 thorpej /* Compression instance */
300 1.1 thorpej static const struct swcr_comp_algo swcr_comp_algo_deflate = {
301 1.1 thorpej &comp_algo_deflate,
302 1.1 thorpej deflate_compress,
303 1.1 thorpej deflate_decompress
304 1.1 thorpej };
305 1.1 thorpej
306 1.14 drochner static const struct swcr_comp_algo swcr_comp_algo_deflate_nogrow = {
307 1.14 drochner &comp_algo_deflate_nogrow,
308 1.14 drochner deflate_compress,
309 1.14 drochner deflate_decompress
310 1.14 drochner };
311 1.14 drochner
312 1.12 darran static const struct swcr_comp_algo swcr_comp_algo_gzip = {
313 1.12 darran &comp_algo_deflate,
314 1.12 darran gzip_compress,
315 1.12 darran gzip_decompress
316 1.12 darran };
317 1.12 darran
318 1.1 thorpej /*
319 1.1 thorpej * Encryption wrapper routines.
320 1.1 thorpej */
321 1.1 thorpej static void
322 1.5 christos null_encrypt(void *key, u_int8_t *blk)
323 1.1 thorpej {
324 1.1 thorpej }
325 1.1 thorpej static void
326 1.5 christos null_decrypt(void *key, u_int8_t *blk)
327 1.1 thorpej {
328 1.1 thorpej }
329 1.1 thorpej static int
330 1.4 christos null_setkey(u_int8_t **sched, const u_int8_t *key, int len)
331 1.1 thorpej {
332 1.1 thorpej *sched = NULL;
333 1.1 thorpej return 0;
334 1.1 thorpej }
335 1.1 thorpej static void
336 1.1 thorpej null_zerokey(u_int8_t **sched)
337 1.1 thorpej {
338 1.1 thorpej *sched = NULL;
339 1.1 thorpej }
340 1.1 thorpej
341 1.1 thorpej static void
342 1.5 christos des1_encrypt(void *key, u_int8_t *blk)
343 1.1 thorpej {
344 1.1 thorpej des_cblock *cb = (des_cblock *) blk;
345 1.1 thorpej des_key_schedule *p = (des_key_schedule *) key;
346 1.1 thorpej
347 1.1 thorpej des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
348 1.1 thorpej }
349 1.1 thorpej
350 1.1 thorpej static void
351 1.5 christos des1_decrypt(void *key, u_int8_t *blk)
352 1.1 thorpej {
353 1.1 thorpej des_cblock *cb = (des_cblock *) blk;
354 1.1 thorpej des_key_schedule *p = (des_key_schedule *) key;
355 1.1 thorpej
356 1.1 thorpej des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
357 1.1 thorpej }
358 1.1 thorpej
359 1.1 thorpej static int
360 1.4 christos des1_setkey(u_int8_t **sched, const u_int8_t *key, int len)
361 1.1 thorpej {
362 1.1 thorpej des_key_schedule *p;
363 1.1 thorpej int err;
364 1.1 thorpej
365 1.9 cegger p = malloc(sizeof (des_key_schedule),
366 1.8 tls M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
367 1.1 thorpej if (p != NULL) {
368 1.1 thorpej des_set_key((des_cblock *)__UNCONST(key), p[0]);
369 1.1 thorpej err = 0;
370 1.1 thorpej } else
371 1.1 thorpej err = ENOMEM;
372 1.1 thorpej *sched = (u_int8_t *) p;
373 1.1 thorpej return err;
374 1.1 thorpej }
375 1.1 thorpej
376 1.1 thorpej static void
377 1.1 thorpej des1_zerokey(u_int8_t **sched)
378 1.1 thorpej {
379 1.11 cegger memset(*sched, 0, sizeof (des_key_schedule));
380 1.9 cegger free(*sched, M_CRYPTO_DATA);
381 1.1 thorpej *sched = NULL;
382 1.1 thorpej }
383 1.1 thorpej
384 1.1 thorpej static void
385 1.5 christos des3_encrypt(void *key, u_int8_t *blk)
386 1.1 thorpej {
387 1.1 thorpej des_cblock *cb = (des_cblock *) blk;
388 1.1 thorpej des_key_schedule *p = (des_key_schedule *) key;
389 1.1 thorpej
390 1.1 thorpej des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
391 1.1 thorpej }
392 1.1 thorpej
393 1.1 thorpej static void
394 1.5 christos des3_decrypt(void *key, u_int8_t *blk)
395 1.1 thorpej {
396 1.1 thorpej des_cblock *cb = (des_cblock *) blk;
397 1.1 thorpej des_key_schedule *p = (des_key_schedule *) key;
398 1.1 thorpej
399 1.1 thorpej des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
400 1.1 thorpej }
401 1.1 thorpej
402 1.1 thorpej static int
403 1.4 christos des3_setkey(u_int8_t **sched, const u_int8_t *key, int len)
404 1.1 thorpej {
405 1.1 thorpej des_key_schedule *p;
406 1.1 thorpej int err;
407 1.1 thorpej
408 1.9 cegger p = malloc(3*sizeof (des_key_schedule),
409 1.8 tls M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
410 1.1 thorpej if (p != NULL) {
411 1.1 thorpej des_set_key((des_cblock *)__UNCONST(key + 0), p[0]);
412 1.1 thorpej des_set_key((des_cblock *)__UNCONST(key + 8), p[1]);
413 1.1 thorpej des_set_key((des_cblock *)__UNCONST(key + 16), p[2]);
414 1.1 thorpej err = 0;
415 1.1 thorpej } else
416 1.1 thorpej err = ENOMEM;
417 1.1 thorpej *sched = (u_int8_t *) p;
418 1.1 thorpej return err;
419 1.1 thorpej }
420 1.1 thorpej
421 1.1 thorpej static void
422 1.1 thorpej des3_zerokey(u_int8_t **sched)
423 1.1 thorpej {
424 1.11 cegger memset(*sched, 0, 3*sizeof (des_key_schedule));
425 1.9 cegger free(*sched, M_CRYPTO_DATA);
426 1.1 thorpej *sched = NULL;
427 1.1 thorpej }
428 1.1 thorpej
429 1.1 thorpej static void
430 1.5 christos blf_encrypt(void *key, u_int8_t *blk)
431 1.1 thorpej {
432 1.1 thorpej
433 1.1 thorpej BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 1);
434 1.1 thorpej }
435 1.1 thorpej
436 1.1 thorpej static void
437 1.5 christos blf_decrypt(void *key, u_int8_t *blk)
438 1.1 thorpej {
439 1.1 thorpej
440 1.1 thorpej BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 0);
441 1.1 thorpej }
442 1.1 thorpej
443 1.1 thorpej static int
444 1.1 thorpej blf_setkey(u_int8_t **sched, const u_int8_t *key, int len)
445 1.1 thorpej {
446 1.1 thorpej int err;
447 1.1 thorpej
448 1.9 cegger *sched = malloc(sizeof(BF_KEY),
449 1.8 tls M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
450 1.1 thorpej if (*sched != NULL) {
451 1.1 thorpej BF_set_key((BF_KEY *) *sched, len, key);
452 1.1 thorpej err = 0;
453 1.1 thorpej } else
454 1.1 thorpej err = ENOMEM;
455 1.1 thorpej return err;
456 1.1 thorpej }
457 1.1 thorpej
458 1.1 thorpej static void
459 1.1 thorpej blf_zerokey(u_int8_t **sched)
460 1.1 thorpej {
461 1.11 cegger memset(*sched, 0, sizeof(BF_KEY));
462 1.9 cegger free(*sched, M_CRYPTO_DATA);
463 1.1 thorpej *sched = NULL;
464 1.1 thorpej }
465 1.1 thorpej
466 1.1 thorpej static void
467 1.5 christos cast5_encrypt(void *key, u_int8_t *blk)
468 1.1 thorpej {
469 1.1 thorpej cast128_encrypt((cast128_key *) key, blk, blk);
470 1.1 thorpej }
471 1.1 thorpej
472 1.1 thorpej static void
473 1.5 christos cast5_decrypt(void *key, u_int8_t *blk)
474 1.1 thorpej {
475 1.1 thorpej cast128_decrypt((cast128_key *) key, blk, blk);
476 1.1 thorpej }
477 1.1 thorpej
478 1.1 thorpej static int
479 1.1 thorpej cast5_setkey(u_int8_t **sched, const u_int8_t *key, int len)
480 1.1 thorpej {
481 1.1 thorpej int err;
482 1.1 thorpej
483 1.9 cegger *sched = malloc(sizeof(cast128_key), M_CRYPTO_DATA,
484 1.8 tls M_NOWAIT|M_ZERO);
485 1.1 thorpej if (*sched != NULL) {
486 1.1 thorpej cast128_setkey((cast128_key *)*sched, key, len);
487 1.1 thorpej err = 0;
488 1.1 thorpej } else
489 1.1 thorpej err = ENOMEM;
490 1.1 thorpej return err;
491 1.1 thorpej }
492 1.1 thorpej
493 1.1 thorpej static void
494 1.1 thorpej cast5_zerokey(u_int8_t **sched)
495 1.1 thorpej {
496 1.11 cegger memset(*sched, 0, sizeof(cast128_key));
497 1.9 cegger free(*sched, M_CRYPTO_DATA);
498 1.1 thorpej *sched = NULL;
499 1.1 thorpej }
500 1.1 thorpej
501 1.1 thorpej static void
502 1.5 christos skipjack_encrypt(void *key, u_int8_t *blk)
503 1.1 thorpej {
504 1.1 thorpej skipjack_forwards(blk, blk, (u_int8_t **) key);
505 1.1 thorpej }
506 1.1 thorpej
507 1.1 thorpej static void
508 1.5 christos skipjack_decrypt(void *key, u_int8_t *blk)
509 1.1 thorpej {
510 1.1 thorpej skipjack_backwards(blk, blk, (u_int8_t **) key);
511 1.1 thorpej }
512 1.1 thorpej
513 1.1 thorpej static int
514 1.4 christos skipjack_setkey(u_int8_t **sched, const u_int8_t *key, int len)
515 1.1 thorpej {
516 1.1 thorpej int err;
517 1.1 thorpej
518 1.1 thorpej /* NB: allocate all the memory that's needed at once */
519 1.1 thorpej /* XXX assumes bytes are aligned on sizeof(u_char) == 1 boundaries.
520 1.1 thorpej * Will this break a pdp-10, Cray-1, or GE-645 port?
521 1.1 thorpej */
522 1.9 cegger *sched = malloc(10 * (sizeof(u_int8_t *) + 0x100),
523 1.8 tls M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
524 1.1 thorpej
525 1.1 thorpej if (*sched != NULL) {
526 1.1 thorpej
527 1.1 thorpej u_int8_t** key_tables = (u_int8_t**) *sched;
528 1.1 thorpej u_int8_t* table = (u_int8_t*) &key_tables[10];
529 1.1 thorpej int k;
530 1.1 thorpej
531 1.1 thorpej for (k = 0; k < 10; k++) {
532 1.1 thorpej key_tables[k] = table;
533 1.1 thorpej table += 0x100;
534 1.1 thorpej }
535 1.1 thorpej subkey_table_gen(key, (u_int8_t **) *sched);
536 1.1 thorpej err = 0;
537 1.1 thorpej } else
538 1.1 thorpej err = ENOMEM;
539 1.1 thorpej return err;
540 1.1 thorpej }
541 1.1 thorpej
542 1.1 thorpej static void
543 1.1 thorpej skipjack_zerokey(u_int8_t **sched)
544 1.1 thorpej {
545 1.11 cegger memset(*sched, 0, 10 * (sizeof(u_int8_t *) + 0x100));
546 1.9 cegger free(*sched, M_CRYPTO_DATA);
547 1.1 thorpej *sched = NULL;
548 1.1 thorpej }
549 1.1 thorpej
550 1.1 thorpej static void
551 1.5 christos rijndael128_encrypt(void *key, u_int8_t *blk)
552 1.1 thorpej {
553 1.1 thorpej rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
554 1.1 thorpej }
555 1.1 thorpej
556 1.1 thorpej static void
557 1.5 christos rijndael128_decrypt(void *key, u_int8_t *blk)
558 1.1 thorpej {
559 1.1 thorpej rijndael_decrypt((rijndael_ctx *) key, (u_char *) blk,
560 1.1 thorpej (u_char *) blk);
561 1.1 thorpej }
562 1.1 thorpej
563 1.1 thorpej static int
564 1.1 thorpej rijndael128_setkey(u_int8_t **sched, const u_int8_t *key, int len)
565 1.1 thorpej {
566 1.1 thorpej int err;
567 1.1 thorpej
568 1.16 drochner if (len != 16 && len != 24 && len != 32)
569 1.16 drochner return EINVAL;
570 1.9 cegger *sched = malloc(sizeof(rijndael_ctx), M_CRYPTO_DATA,
571 1.8 tls M_NOWAIT|M_ZERO);
572 1.1 thorpej if (*sched != NULL) {
573 1.1 thorpej rijndael_set_key((rijndael_ctx *) *sched, key, len * 8);
574 1.1 thorpej err = 0;
575 1.1 thorpej } else
576 1.1 thorpej err = ENOMEM;
577 1.1 thorpej return err;
578 1.1 thorpej }
579 1.1 thorpej
580 1.1 thorpej static void
581 1.1 thorpej rijndael128_zerokey(u_int8_t **sched)
582 1.1 thorpej {
583 1.11 cegger memset(*sched, 0, sizeof(rijndael_ctx));
584 1.9 cegger free(*sched, M_CRYPTO_DATA);
585 1.1 thorpej *sched = NULL;
586 1.1 thorpej }
587 1.1 thorpej
588 1.15 drochner static void
589 1.15 drochner cml_encrypt(void *key, u_int8_t *blk)
590 1.15 drochner {
591 1.15 drochner
592 1.15 drochner camellia_encrypt(key, blk, blk);
593 1.15 drochner }
594 1.15 drochner
595 1.15 drochner static void
596 1.15 drochner cml_decrypt(void *key, u_int8_t *blk)
597 1.15 drochner {
598 1.15 drochner
599 1.15 drochner camellia_decrypt(key, blk, blk);
600 1.15 drochner }
601 1.15 drochner
602 1.15 drochner static int
603 1.15 drochner cml_setkey(u_int8_t **sched, const u_int8_t *key, int len)
604 1.15 drochner {
605 1.15 drochner int err;
606 1.15 drochner
607 1.15 drochner if (len != 16 && len != 24 && len != 32)
608 1.15 drochner return (EINVAL);
609 1.15 drochner *sched = malloc(sizeof(camellia_ctx), M_CRYPTO_DATA,
610 1.15 drochner M_NOWAIT|M_ZERO);
611 1.15 drochner if (*sched != NULL) {
612 1.15 drochner camellia_set_key((camellia_ctx *) *sched, key, len * 8);
613 1.15 drochner err = 0;
614 1.15 drochner } else
615 1.15 drochner err = ENOMEM;
616 1.15 drochner return err;
617 1.15 drochner }
618 1.15 drochner
619 1.15 drochner static void
620 1.15 drochner cml_zerokey(u_int8_t **sched)
621 1.15 drochner {
622 1.15 drochner
623 1.15 drochner memset(*sched, 0, sizeof(camellia_ctx));
624 1.15 drochner free(*sched, M_CRYPTO_DATA);
625 1.15 drochner *sched = NULL;
626 1.15 drochner }
627 1.15 drochner
628 1.1 thorpej /*
629 1.1 thorpej * And now for auth.
630 1.1 thorpej */
631 1.1 thorpej
632 1.1 thorpej static void
633 1.4 christos null_init(void *ctx)
634 1.1 thorpej {
635 1.1 thorpej }
636 1.1 thorpej
637 1.1 thorpej static int
638 1.4 christos null_update(void *ctx, const u_int8_t *buf,
639 1.4 christos u_int16_t len)
640 1.1 thorpej {
641 1.1 thorpej return 0;
642 1.1 thorpej }
643 1.1 thorpej
644 1.1 thorpej static void
645 1.4 christos null_final(u_int8_t *buf, void *ctx)
646 1.1 thorpej {
647 1.1 thorpej if (buf != (u_int8_t *) 0)
648 1.11 cegger memset(buf, 0, 12);
649 1.1 thorpej }
650 1.1 thorpej
651 1.1 thorpej static int
652 1.1 thorpej RMD160Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
653 1.1 thorpej {
654 1.1 thorpej RMD160Update(ctx, buf, len);
655 1.1 thorpej return 0;
656 1.1 thorpej }
657 1.1 thorpej
658 1.1 thorpej static int
659 1.1 thorpej MD5Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
660 1.1 thorpej {
661 1.1 thorpej MD5Update(ctx, buf, len);
662 1.1 thorpej return 0;
663 1.1 thorpej }
664 1.1 thorpej
665 1.1 thorpej static void
666 1.1 thorpej SHA1Init_int(void *ctx)
667 1.1 thorpej {
668 1.1 thorpej SHA1Init(ctx);
669 1.1 thorpej }
670 1.1 thorpej
671 1.1 thorpej static int
672 1.1 thorpej SHA1Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
673 1.1 thorpej {
674 1.1 thorpej SHA1Update(ctx, buf, len);
675 1.1 thorpej return 0;
676 1.1 thorpej }
677 1.1 thorpej
678 1.1 thorpej static void
679 1.1 thorpej SHA1Final_int(u_int8_t *blk, void *ctx)
680 1.1 thorpej {
681 1.1 thorpej SHA1Final(blk, ctx);
682 1.1 thorpej }
683 1.1 thorpej
684 1.1 thorpej static int
685 1.1 thorpej SHA256Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
686 1.1 thorpej {
687 1.1 thorpej SHA256_Update(ctx, buf, len);
688 1.1 thorpej return 0;
689 1.1 thorpej }
690 1.1 thorpej
691 1.1 thorpej static int
692 1.1 thorpej SHA384Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
693 1.1 thorpej {
694 1.1 thorpej SHA384_Update(ctx, buf, len);
695 1.1 thorpej return 0;
696 1.1 thorpej }
697 1.1 thorpej
698 1.1 thorpej static int
699 1.1 thorpej SHA512Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
700 1.1 thorpej {
701 1.1 thorpej SHA512_Update(ctx, buf, len);
702 1.1 thorpej return 0;
703 1.1 thorpej }
704 1.1 thorpej
705 1.1 thorpej /*
706 1.1 thorpej * And compression
707 1.1 thorpej */
708 1.1 thorpej
709 1.1 thorpej static u_int32_t
710 1.10 dsl deflate_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
711 1.1 thorpej {
712 1.14 drochner return deflate_global(data, size, 0, out, 0);
713 1.1 thorpej }
714 1.1 thorpej
715 1.1 thorpej static u_int32_t
716 1.14 drochner deflate_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out,
717 1.14 drochner int size_hint)
718 1.1 thorpej {
719 1.14 drochner return deflate_global(data, size, 1, out, size_hint);
720 1.1 thorpej }
721 1.12 darran
722 1.12 darran static u_int32_t
723 1.12 darran gzip_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
724 1.12 darran {
725 1.14 drochner return gzip_global(data, size, 0, out, 0);
726 1.12 darran }
727 1.12 darran
728 1.12 darran static u_int32_t
729 1.14 drochner gzip_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out,
730 1.14 drochner int size_hint)
731 1.12 darran {
732 1.14 drochner return gzip_global(data, size, 1, out, size_hint);
733 1.12 darran }
734