cryptosoft_xform.c revision 1.22 1 1.22 drochner /* $NetBSD: cryptosoft_xform.c,v 1.22 2011/05/24 19:10:11 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.22 drochner __KERNEL_RCSID(1, "$NetBSD: cryptosoft_xform.c,v 1.22 2011/05/24 19:10:11 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.21 drochner #include <sys/sha2.h>
58 1.22 drochner #include <opencrypto/aesxcbcmac.h>
59 1.1 thorpej
60 1.1 thorpej struct swcr_auth_hash {
61 1.13 drochner const struct auth_hash *auth_hash;
62 1.21 drochner int ctxsize;
63 1.1 thorpej void (*Init)(void *);
64 1.22 drochner void (*Setkey)(void *, const uint8_t *, uint16_t);
65 1.1 thorpej int (*Update)(void *, const uint8_t *, uint16_t);
66 1.1 thorpej void (*Final)(uint8_t *, void *);
67 1.1 thorpej };
68 1.1 thorpej
69 1.1 thorpej struct swcr_enc_xform {
70 1.13 drochner const struct enc_xform *enc_xform;
71 1.5 christos void (*encrypt)(void *, uint8_t *);
72 1.5 christos void (*decrypt)(void *, uint8_t *);
73 1.16 drochner int (*setkey)(uint8_t **, const uint8_t *, int);
74 1.1 thorpej void (*zerokey)(uint8_t **);
75 1.20 drochner void (*reinit)(void *, const uint8_t *, uint8_t *);
76 1.1 thorpej };
77 1.1 thorpej
78 1.1 thorpej struct swcr_comp_algo {
79 1.14 drochner const struct comp_algo *unused_comp_algo;
80 1.1 thorpej uint32_t (*compress)(uint8_t *, uint32_t, uint8_t **);
81 1.14 drochner uint32_t (*decompress)(uint8_t *, uint32_t, uint8_t **, int);
82 1.1 thorpej };
83 1.1 thorpej
84 1.5 christos static void null_encrypt(void *, u_int8_t *);
85 1.5 christos static void null_decrypt(void *, u_int8_t *);
86 1.1 thorpej static int null_setkey(u_int8_t **, const u_int8_t *, int);
87 1.1 thorpej static void null_zerokey(u_int8_t **);
88 1.1 thorpej
89 1.1 thorpej static int des1_setkey(u_int8_t **, const u_int8_t *, int);
90 1.1 thorpej static int des3_setkey(u_int8_t **, const u_int8_t *, int);
91 1.1 thorpej static int blf_setkey(u_int8_t **, const u_int8_t *, int);
92 1.1 thorpej static int cast5_setkey(u_int8_t **, const u_int8_t *, int);
93 1.1 thorpej static int skipjack_setkey(u_int8_t **, const u_int8_t *, int);
94 1.1 thorpej static int rijndael128_setkey(u_int8_t **, const u_int8_t *, int);
95 1.15 drochner static int cml_setkey(u_int8_t **, const u_int8_t *, int);
96 1.18 drochner static int aes_ctr_setkey(u_int8_t **, const u_int8_t *, int);
97 1.5 christos static void des1_encrypt(void *, u_int8_t *);
98 1.5 christos static void des3_encrypt(void *, u_int8_t *);
99 1.5 christos static void blf_encrypt(void *, u_int8_t *);
100 1.5 christos static void cast5_encrypt(void *, u_int8_t *);
101 1.5 christos static void skipjack_encrypt(void *, u_int8_t *);
102 1.5 christos static void rijndael128_encrypt(void *, u_int8_t *);
103 1.15 drochner static void cml_encrypt(void *, u_int8_t *);
104 1.5 christos static void des1_decrypt(void *, u_int8_t *);
105 1.5 christos static void des3_decrypt(void *, u_int8_t *);
106 1.5 christos static void blf_decrypt(void *, u_int8_t *);
107 1.5 christos static void cast5_decrypt(void *, u_int8_t *);
108 1.5 christos static void skipjack_decrypt(void *, u_int8_t *);
109 1.5 christos static void rijndael128_decrypt(void *, u_int8_t *);
110 1.15 drochner static void cml_decrypt(void *, u_int8_t *);
111 1.18 drochner static void aes_ctr_crypt(void *, u_int8_t *);
112 1.1 thorpej static void des1_zerokey(u_int8_t **);
113 1.1 thorpej static void des3_zerokey(u_int8_t **);
114 1.1 thorpej static void blf_zerokey(u_int8_t **);
115 1.1 thorpej static void cast5_zerokey(u_int8_t **);
116 1.1 thorpej static void skipjack_zerokey(u_int8_t **);
117 1.1 thorpej static void rijndael128_zerokey(u_int8_t **);
118 1.15 drochner static void cml_zerokey(u_int8_t **);
119 1.18 drochner static void aes_ctr_zerokey(u_int8_t **);
120 1.20 drochner static void aes_ctr_reinit(void *, const u_int8_t *, u_int8_t *);
121 1.1 thorpej
122 1.1 thorpej static void null_init(void *);
123 1.1 thorpej static int null_update(void *, const u_int8_t *, u_int16_t);
124 1.1 thorpej static void null_final(u_int8_t *, void *);
125 1.1 thorpej
126 1.1 thorpej static int MD5Update_int(void *, const u_int8_t *, u_int16_t);
127 1.1 thorpej static void SHA1Init_int(void *);
128 1.1 thorpej static int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
129 1.1 thorpej static void SHA1Final_int(u_int8_t *, void *);
130 1.1 thorpej
131 1.1 thorpej
132 1.1 thorpej static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
133 1.1 thorpej static int SHA1Update_int(void *, const u_int8_t *, u_int16_t);
134 1.1 thorpej static void SHA1Final_int(u_int8_t *, void *);
135 1.1 thorpej static int RMD160Update_int(void *, const u_int8_t *, u_int16_t);
136 1.1 thorpej static int SHA256Update_int(void *, const u_int8_t *, u_int16_t);
137 1.1 thorpej static int SHA384Update_int(void *, const u_int8_t *, u_int16_t);
138 1.1 thorpej static int SHA512Update_int(void *, const u_int8_t *, u_int16_t);
139 1.1 thorpej
140 1.1 thorpej static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
141 1.14 drochner static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **, int);
142 1.12 darran static u_int32_t gzip_compress(u_int8_t *, u_int32_t, u_int8_t **);
143 1.14 drochner static u_int32_t gzip_decompress(u_int8_t *, u_int32_t, u_int8_t **, int);
144 1.1 thorpej
145 1.1 thorpej /* Encryption instances */
146 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_null = {
147 1.1 thorpej &enc_xform_null,
148 1.1 thorpej null_encrypt,
149 1.1 thorpej null_decrypt,
150 1.1 thorpej null_setkey,
151 1.1 thorpej null_zerokey,
152 1.17 drochner NULL
153 1.1 thorpej };
154 1.1 thorpej
155 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_des = {
156 1.1 thorpej &enc_xform_des,
157 1.1 thorpej des1_encrypt,
158 1.1 thorpej des1_decrypt,
159 1.1 thorpej des1_setkey,
160 1.1 thorpej des1_zerokey,
161 1.17 drochner NULL
162 1.1 thorpej };
163 1.1 thorpej
164 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_3des = {
165 1.1 thorpej &enc_xform_3des,
166 1.1 thorpej des3_encrypt,
167 1.1 thorpej des3_decrypt,
168 1.1 thorpej des3_setkey,
169 1.17 drochner des3_zerokey,
170 1.17 drochner NULL
171 1.1 thorpej };
172 1.1 thorpej
173 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_blf = {
174 1.1 thorpej &enc_xform_blf,
175 1.1 thorpej blf_encrypt,
176 1.1 thorpej blf_decrypt,
177 1.1 thorpej blf_setkey,
178 1.17 drochner blf_zerokey,
179 1.17 drochner NULL
180 1.1 thorpej };
181 1.1 thorpej
182 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_cast5 = {
183 1.1 thorpej &enc_xform_cast5,
184 1.1 thorpej cast5_encrypt,
185 1.1 thorpej cast5_decrypt,
186 1.1 thorpej cast5_setkey,
187 1.17 drochner cast5_zerokey,
188 1.17 drochner NULL
189 1.1 thorpej };
190 1.1 thorpej
191 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_skipjack = {
192 1.1 thorpej &enc_xform_skipjack,
193 1.1 thorpej skipjack_encrypt,
194 1.1 thorpej skipjack_decrypt,
195 1.1 thorpej skipjack_setkey,
196 1.17 drochner skipjack_zerokey,
197 1.17 drochner NULL
198 1.1 thorpej };
199 1.1 thorpej
200 1.1 thorpej static const struct swcr_enc_xform swcr_enc_xform_rijndael128 = {
201 1.1 thorpej &enc_xform_rijndael128,
202 1.1 thorpej rijndael128_encrypt,
203 1.1 thorpej rijndael128_decrypt,
204 1.1 thorpej rijndael128_setkey,
205 1.1 thorpej rijndael128_zerokey,
206 1.17 drochner NULL
207 1.1 thorpej };
208 1.1 thorpej
209 1.18 drochner static const struct swcr_enc_xform swcr_enc_xform_aes_ctr = {
210 1.18 drochner &enc_xform_aes_ctr,
211 1.18 drochner aes_ctr_crypt,
212 1.18 drochner aes_ctr_crypt,
213 1.18 drochner aes_ctr_setkey,
214 1.18 drochner aes_ctr_zerokey,
215 1.18 drochner aes_ctr_reinit
216 1.18 drochner };
217 1.18 drochner
218 1.15 drochner static const struct swcr_enc_xform swcr_enc_xform_camellia = {
219 1.15 drochner &enc_xform_camellia,
220 1.15 drochner cml_encrypt,
221 1.15 drochner cml_decrypt,
222 1.15 drochner cml_setkey,
223 1.17 drochner cml_zerokey,
224 1.17 drochner NULL
225 1.15 drochner };
226 1.15 drochner
227 1.1 thorpej /* Authentication instances */
228 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_null = {
229 1.21 drochner &auth_hash_null, sizeof(int), /* NB: context isn't used */
230 1.22 drochner null_init, NULL, null_update, null_final
231 1.1 thorpej };
232 1.1 thorpej
233 1.7 tls static const struct swcr_auth_hash swcr_auth_hash_hmac_md5 = {
234 1.21 drochner &auth_hash_hmac_md5, sizeof(MD5_CTX),
235 1.22 drochner (void (*) (void *)) MD5Init, NULL, MD5Update_int,
236 1.7 tls (void (*) (u_int8_t *, void *)) MD5Final
237 1.7 tls };
238 1.7 tls
239 1.7 tls static const struct swcr_auth_hash swcr_auth_hash_hmac_sha1 = {
240 1.21 drochner &auth_hash_hmac_sha1, sizeof(SHA1_CTX),
241 1.22 drochner SHA1Init_int, NULL, SHA1Update_int, SHA1Final_int
242 1.7 tls };
243 1.7 tls
244 1.7 tls static const struct swcr_auth_hash swcr_auth_hash_hmac_ripemd_160 = {
245 1.21 drochner &auth_hash_hmac_ripemd_160, sizeof(RMD160_CTX),
246 1.22 drochner (void (*)(void *)) RMD160Init, NULL, RMD160Update_int,
247 1.7 tls (void (*)(u_int8_t *, void *)) RMD160Final
248 1.7 tls };
249 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_md5_96 = {
250 1.21 drochner &auth_hash_hmac_md5_96, sizeof(MD5_CTX),
251 1.22 drochner (void (*) (void *)) MD5Init, NULL, MD5Update_int,
252 1.1 thorpej (void (*) (u_int8_t *, void *)) MD5Final
253 1.1 thorpej };
254 1.1 thorpej
255 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha1_96 = {
256 1.21 drochner &auth_hash_hmac_sha1_96, sizeof(SHA1_CTX),
257 1.22 drochner SHA1Init_int, NULL, SHA1Update_int, SHA1Final_int
258 1.1 thorpej };
259 1.1 thorpej
260 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_ripemd_160_96 = {
261 1.21 drochner &auth_hash_hmac_ripemd_160_96, sizeof(RMD160_CTX),
262 1.22 drochner (void (*)(void *)) RMD160Init, NULL, RMD160Update_int,
263 1.1 thorpej (void (*)(u_int8_t *, void *)) RMD160Final
264 1.1 thorpej };
265 1.1 thorpej
266 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_key_md5 = {
267 1.21 drochner &auth_hash_key_md5, sizeof(MD5_CTX),
268 1.22 drochner (void (*)(void *)) MD5Init, NULL, MD5Update_int,
269 1.1 thorpej (void (*)(u_int8_t *, void *)) MD5Final
270 1.1 thorpej };
271 1.1 thorpej
272 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_key_sha1 = {
273 1.21 drochner &auth_hash_key_sha1, sizeof(SHA1_CTX),
274 1.22 drochner SHA1Init_int, NULL, SHA1Update_int, SHA1Final_int
275 1.1 thorpej };
276 1.1 thorpej
277 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_md5 = {
278 1.21 drochner &auth_hash_md5, sizeof(MD5_CTX),
279 1.22 drochner (void (*) (void *)) MD5Init, NULL, MD5Update_int,
280 1.1 thorpej (void (*) (u_int8_t *, void *)) MD5Final
281 1.1 thorpej };
282 1.1 thorpej
283 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_sha1 = {
284 1.21 drochner &auth_hash_sha1, sizeof(SHA1_CTX),
285 1.22 drochner (void (*)(void *)) SHA1Init, NULL, SHA1Update_int,
286 1.1 thorpej (void (*)(u_int8_t *, void *)) SHA1Final
287 1.1 thorpej };
288 1.1 thorpej
289 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_256 = {
290 1.21 drochner &auth_hash_hmac_sha2_256, sizeof(SHA256_CTX),
291 1.22 drochner (void (*)(void *)) SHA256_Init, NULL, SHA256Update_int,
292 1.1 thorpej (void (*)(u_int8_t *, void *)) SHA256_Final
293 1.1 thorpej };
294 1.1 thorpej
295 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_384 = {
296 1.21 drochner &auth_hash_hmac_sha2_384, sizeof(SHA384_CTX),
297 1.22 drochner (void (*)(void *)) SHA384_Init, NULL, SHA384Update_int,
298 1.1 thorpej (void (*)(u_int8_t *, void *)) SHA384_Final
299 1.1 thorpej };
300 1.1 thorpej
301 1.1 thorpej static const struct swcr_auth_hash swcr_auth_hash_hmac_sha2_512 = {
302 1.21 drochner &auth_hash_hmac_sha2_512, sizeof(SHA512_CTX),
303 1.22 drochner (void (*)(void *)) SHA512_Init, NULL, SHA512Update_int,
304 1.1 thorpej (void (*)(u_int8_t *, void *)) SHA512_Final
305 1.1 thorpej };
306 1.1 thorpej
307 1.22 drochner static const struct swcr_auth_hash swcr_auth_hash_aes_xcbc_mac = {
308 1.22 drochner &auth_hash_aes_xcbc_mac_96, sizeof(aesxcbc_ctx),
309 1.22 drochner null_init,
310 1.22 drochner (void (*)(void *, const u_int8_t *, u_int16_t))aes_xcbc_mac_init,
311 1.22 drochner aes_xcbc_mac_loop, aes_xcbc_mac_result
312 1.22 drochner };
313 1.22 drochner
314 1.1 thorpej /* Compression instance */
315 1.1 thorpej static const struct swcr_comp_algo swcr_comp_algo_deflate = {
316 1.1 thorpej &comp_algo_deflate,
317 1.1 thorpej deflate_compress,
318 1.1 thorpej deflate_decompress
319 1.1 thorpej };
320 1.1 thorpej
321 1.14 drochner static const struct swcr_comp_algo swcr_comp_algo_deflate_nogrow = {
322 1.14 drochner &comp_algo_deflate_nogrow,
323 1.14 drochner deflate_compress,
324 1.14 drochner deflate_decompress
325 1.14 drochner };
326 1.14 drochner
327 1.12 darran static const struct swcr_comp_algo swcr_comp_algo_gzip = {
328 1.12 darran &comp_algo_deflate,
329 1.12 darran gzip_compress,
330 1.12 darran gzip_decompress
331 1.12 darran };
332 1.12 darran
333 1.1 thorpej /*
334 1.1 thorpej * Encryption wrapper routines.
335 1.1 thorpej */
336 1.1 thorpej static void
337 1.5 christos null_encrypt(void *key, u_int8_t *blk)
338 1.1 thorpej {
339 1.1 thorpej }
340 1.1 thorpej static void
341 1.5 christos null_decrypt(void *key, u_int8_t *blk)
342 1.1 thorpej {
343 1.1 thorpej }
344 1.1 thorpej static int
345 1.4 christos null_setkey(u_int8_t **sched, const u_int8_t *key, int len)
346 1.1 thorpej {
347 1.1 thorpej *sched = NULL;
348 1.1 thorpej return 0;
349 1.1 thorpej }
350 1.1 thorpej static void
351 1.1 thorpej null_zerokey(u_int8_t **sched)
352 1.1 thorpej {
353 1.1 thorpej *sched = NULL;
354 1.1 thorpej }
355 1.1 thorpej
356 1.1 thorpej static void
357 1.5 christos des1_encrypt(void *key, u_int8_t *blk)
358 1.1 thorpej {
359 1.1 thorpej des_cblock *cb = (des_cblock *) blk;
360 1.1 thorpej des_key_schedule *p = (des_key_schedule *) key;
361 1.1 thorpej
362 1.1 thorpej des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
363 1.1 thorpej }
364 1.1 thorpej
365 1.1 thorpej static void
366 1.5 christos des1_decrypt(void *key, u_int8_t *blk)
367 1.1 thorpej {
368 1.1 thorpej des_cblock *cb = (des_cblock *) blk;
369 1.1 thorpej des_key_schedule *p = (des_key_schedule *) key;
370 1.1 thorpej
371 1.1 thorpej des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
372 1.1 thorpej }
373 1.1 thorpej
374 1.1 thorpej static int
375 1.4 christos des1_setkey(u_int8_t **sched, const u_int8_t *key, int len)
376 1.1 thorpej {
377 1.1 thorpej des_key_schedule *p;
378 1.1 thorpej int err;
379 1.1 thorpej
380 1.9 cegger p = malloc(sizeof (des_key_schedule),
381 1.8 tls M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
382 1.1 thorpej if (p != NULL) {
383 1.1 thorpej des_set_key((des_cblock *)__UNCONST(key), p[0]);
384 1.1 thorpej err = 0;
385 1.1 thorpej } else
386 1.1 thorpej err = ENOMEM;
387 1.1 thorpej *sched = (u_int8_t *) p;
388 1.1 thorpej return err;
389 1.1 thorpej }
390 1.1 thorpej
391 1.1 thorpej static void
392 1.1 thorpej des1_zerokey(u_int8_t **sched)
393 1.1 thorpej {
394 1.11 cegger memset(*sched, 0, sizeof (des_key_schedule));
395 1.9 cegger free(*sched, M_CRYPTO_DATA);
396 1.1 thorpej *sched = NULL;
397 1.1 thorpej }
398 1.1 thorpej
399 1.1 thorpej static void
400 1.5 christos des3_encrypt(void *key, u_int8_t *blk)
401 1.1 thorpej {
402 1.1 thorpej des_cblock *cb = (des_cblock *) blk;
403 1.1 thorpej des_key_schedule *p = (des_key_schedule *) key;
404 1.1 thorpej
405 1.1 thorpej des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
406 1.1 thorpej }
407 1.1 thorpej
408 1.1 thorpej static void
409 1.5 christos des3_decrypt(void *key, u_int8_t *blk)
410 1.1 thorpej {
411 1.1 thorpej des_cblock *cb = (des_cblock *) blk;
412 1.1 thorpej des_key_schedule *p = (des_key_schedule *) key;
413 1.1 thorpej
414 1.1 thorpej des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
415 1.1 thorpej }
416 1.1 thorpej
417 1.1 thorpej static int
418 1.4 christos des3_setkey(u_int8_t **sched, const u_int8_t *key, int len)
419 1.1 thorpej {
420 1.1 thorpej des_key_schedule *p;
421 1.1 thorpej int err;
422 1.1 thorpej
423 1.9 cegger p = malloc(3*sizeof (des_key_schedule),
424 1.8 tls M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
425 1.1 thorpej if (p != NULL) {
426 1.1 thorpej des_set_key((des_cblock *)__UNCONST(key + 0), p[0]);
427 1.1 thorpej des_set_key((des_cblock *)__UNCONST(key + 8), p[1]);
428 1.1 thorpej des_set_key((des_cblock *)__UNCONST(key + 16), p[2]);
429 1.1 thorpej err = 0;
430 1.1 thorpej } else
431 1.1 thorpej err = ENOMEM;
432 1.1 thorpej *sched = (u_int8_t *) p;
433 1.1 thorpej return err;
434 1.1 thorpej }
435 1.1 thorpej
436 1.1 thorpej static void
437 1.1 thorpej des3_zerokey(u_int8_t **sched)
438 1.1 thorpej {
439 1.11 cegger memset(*sched, 0, 3*sizeof (des_key_schedule));
440 1.9 cegger free(*sched, M_CRYPTO_DATA);
441 1.1 thorpej *sched = NULL;
442 1.1 thorpej }
443 1.1 thorpej
444 1.1 thorpej static void
445 1.5 christos blf_encrypt(void *key, u_int8_t *blk)
446 1.1 thorpej {
447 1.1 thorpej
448 1.1 thorpej BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 1);
449 1.1 thorpej }
450 1.1 thorpej
451 1.1 thorpej static void
452 1.5 christos blf_decrypt(void *key, u_int8_t *blk)
453 1.1 thorpej {
454 1.1 thorpej
455 1.1 thorpej BF_ecb_encrypt(blk, blk, (BF_KEY *)key, 0);
456 1.1 thorpej }
457 1.1 thorpej
458 1.1 thorpej static int
459 1.1 thorpej blf_setkey(u_int8_t **sched, const u_int8_t *key, int len)
460 1.1 thorpej {
461 1.1 thorpej int err;
462 1.1 thorpej
463 1.9 cegger *sched = malloc(sizeof(BF_KEY),
464 1.8 tls M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
465 1.1 thorpej if (*sched != NULL) {
466 1.1 thorpej BF_set_key((BF_KEY *) *sched, len, key);
467 1.1 thorpej err = 0;
468 1.1 thorpej } else
469 1.1 thorpej err = ENOMEM;
470 1.1 thorpej return err;
471 1.1 thorpej }
472 1.1 thorpej
473 1.1 thorpej static void
474 1.1 thorpej blf_zerokey(u_int8_t **sched)
475 1.1 thorpej {
476 1.11 cegger memset(*sched, 0, sizeof(BF_KEY));
477 1.9 cegger free(*sched, M_CRYPTO_DATA);
478 1.1 thorpej *sched = NULL;
479 1.1 thorpej }
480 1.1 thorpej
481 1.1 thorpej static void
482 1.5 christos cast5_encrypt(void *key, u_int8_t *blk)
483 1.1 thorpej {
484 1.1 thorpej cast128_encrypt((cast128_key *) key, blk, blk);
485 1.1 thorpej }
486 1.1 thorpej
487 1.1 thorpej static void
488 1.5 christos cast5_decrypt(void *key, u_int8_t *blk)
489 1.1 thorpej {
490 1.1 thorpej cast128_decrypt((cast128_key *) key, blk, blk);
491 1.1 thorpej }
492 1.1 thorpej
493 1.1 thorpej static int
494 1.1 thorpej cast5_setkey(u_int8_t **sched, const u_int8_t *key, int len)
495 1.1 thorpej {
496 1.1 thorpej int err;
497 1.1 thorpej
498 1.9 cegger *sched = malloc(sizeof(cast128_key), M_CRYPTO_DATA,
499 1.8 tls M_NOWAIT|M_ZERO);
500 1.1 thorpej if (*sched != NULL) {
501 1.1 thorpej cast128_setkey((cast128_key *)*sched, key, len);
502 1.1 thorpej err = 0;
503 1.1 thorpej } else
504 1.1 thorpej err = ENOMEM;
505 1.1 thorpej return err;
506 1.1 thorpej }
507 1.1 thorpej
508 1.1 thorpej static void
509 1.1 thorpej cast5_zerokey(u_int8_t **sched)
510 1.1 thorpej {
511 1.11 cegger memset(*sched, 0, sizeof(cast128_key));
512 1.9 cegger free(*sched, M_CRYPTO_DATA);
513 1.1 thorpej *sched = NULL;
514 1.1 thorpej }
515 1.1 thorpej
516 1.1 thorpej static void
517 1.5 christos skipjack_encrypt(void *key, u_int8_t *blk)
518 1.1 thorpej {
519 1.1 thorpej skipjack_forwards(blk, blk, (u_int8_t **) key);
520 1.1 thorpej }
521 1.1 thorpej
522 1.1 thorpej static void
523 1.5 christos skipjack_decrypt(void *key, u_int8_t *blk)
524 1.1 thorpej {
525 1.1 thorpej skipjack_backwards(blk, blk, (u_int8_t **) key);
526 1.1 thorpej }
527 1.1 thorpej
528 1.1 thorpej static int
529 1.4 christos skipjack_setkey(u_int8_t **sched, const u_int8_t *key, int len)
530 1.1 thorpej {
531 1.1 thorpej int err;
532 1.1 thorpej
533 1.1 thorpej /* NB: allocate all the memory that's needed at once */
534 1.1 thorpej /* XXX assumes bytes are aligned on sizeof(u_char) == 1 boundaries.
535 1.1 thorpej * Will this break a pdp-10, Cray-1, or GE-645 port?
536 1.1 thorpej */
537 1.9 cegger *sched = malloc(10 * (sizeof(u_int8_t *) + 0x100),
538 1.8 tls M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
539 1.1 thorpej
540 1.1 thorpej if (*sched != NULL) {
541 1.1 thorpej
542 1.1 thorpej u_int8_t** key_tables = (u_int8_t**) *sched;
543 1.1 thorpej u_int8_t* table = (u_int8_t*) &key_tables[10];
544 1.1 thorpej int k;
545 1.1 thorpej
546 1.1 thorpej for (k = 0; k < 10; k++) {
547 1.1 thorpej key_tables[k] = table;
548 1.1 thorpej table += 0x100;
549 1.1 thorpej }
550 1.1 thorpej subkey_table_gen(key, (u_int8_t **) *sched);
551 1.1 thorpej err = 0;
552 1.1 thorpej } else
553 1.1 thorpej err = ENOMEM;
554 1.1 thorpej return err;
555 1.1 thorpej }
556 1.1 thorpej
557 1.1 thorpej static void
558 1.1 thorpej skipjack_zerokey(u_int8_t **sched)
559 1.1 thorpej {
560 1.11 cegger memset(*sched, 0, 10 * (sizeof(u_int8_t *) + 0x100));
561 1.9 cegger free(*sched, M_CRYPTO_DATA);
562 1.1 thorpej *sched = NULL;
563 1.1 thorpej }
564 1.1 thorpej
565 1.1 thorpej static void
566 1.5 christos rijndael128_encrypt(void *key, u_int8_t *blk)
567 1.1 thorpej {
568 1.1 thorpej rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
569 1.1 thorpej }
570 1.1 thorpej
571 1.1 thorpej static void
572 1.5 christos rijndael128_decrypt(void *key, u_int8_t *blk)
573 1.1 thorpej {
574 1.1 thorpej rijndael_decrypt((rijndael_ctx *) key, (u_char *) blk,
575 1.1 thorpej (u_char *) blk);
576 1.1 thorpej }
577 1.1 thorpej
578 1.1 thorpej static int
579 1.1 thorpej rijndael128_setkey(u_int8_t **sched, const u_int8_t *key, int len)
580 1.1 thorpej {
581 1.1 thorpej int err;
582 1.1 thorpej
583 1.16 drochner if (len != 16 && len != 24 && len != 32)
584 1.16 drochner return EINVAL;
585 1.9 cegger *sched = malloc(sizeof(rijndael_ctx), M_CRYPTO_DATA,
586 1.8 tls M_NOWAIT|M_ZERO);
587 1.1 thorpej if (*sched != NULL) {
588 1.1 thorpej rijndael_set_key((rijndael_ctx *) *sched, key, len * 8);
589 1.1 thorpej err = 0;
590 1.1 thorpej } else
591 1.1 thorpej err = ENOMEM;
592 1.1 thorpej return err;
593 1.1 thorpej }
594 1.1 thorpej
595 1.1 thorpej static void
596 1.1 thorpej rijndael128_zerokey(u_int8_t **sched)
597 1.1 thorpej {
598 1.11 cegger memset(*sched, 0, sizeof(rijndael_ctx));
599 1.9 cegger free(*sched, M_CRYPTO_DATA);
600 1.1 thorpej *sched = NULL;
601 1.1 thorpej }
602 1.1 thorpej
603 1.15 drochner static void
604 1.15 drochner cml_encrypt(void *key, u_int8_t *blk)
605 1.15 drochner {
606 1.15 drochner
607 1.15 drochner camellia_encrypt(key, blk, blk);
608 1.15 drochner }
609 1.15 drochner
610 1.15 drochner static void
611 1.15 drochner cml_decrypt(void *key, u_int8_t *blk)
612 1.15 drochner {
613 1.15 drochner
614 1.15 drochner camellia_decrypt(key, blk, blk);
615 1.15 drochner }
616 1.15 drochner
617 1.15 drochner static int
618 1.15 drochner cml_setkey(u_int8_t **sched, const u_int8_t *key, int len)
619 1.15 drochner {
620 1.15 drochner int err;
621 1.15 drochner
622 1.15 drochner if (len != 16 && len != 24 && len != 32)
623 1.15 drochner return (EINVAL);
624 1.15 drochner *sched = malloc(sizeof(camellia_ctx), M_CRYPTO_DATA,
625 1.15 drochner M_NOWAIT|M_ZERO);
626 1.15 drochner if (*sched != NULL) {
627 1.15 drochner camellia_set_key((camellia_ctx *) *sched, key, len * 8);
628 1.15 drochner err = 0;
629 1.15 drochner } else
630 1.15 drochner err = ENOMEM;
631 1.15 drochner return err;
632 1.15 drochner }
633 1.15 drochner
634 1.15 drochner static void
635 1.15 drochner cml_zerokey(u_int8_t **sched)
636 1.15 drochner {
637 1.15 drochner
638 1.15 drochner memset(*sched, 0, sizeof(camellia_ctx));
639 1.15 drochner free(*sched, M_CRYPTO_DATA);
640 1.15 drochner *sched = NULL;
641 1.15 drochner }
642 1.15 drochner
643 1.18 drochner #define AESCTR_NONCESIZE 4
644 1.18 drochner #define AESCTR_IVSIZE 8
645 1.18 drochner #define AESCTR_BLOCKSIZE 16
646 1.18 drochner
647 1.18 drochner struct aes_ctr_ctx {
648 1.18 drochner /* need only encryption half */
649 1.18 drochner u_int32_t ac_ek[4*(RIJNDAEL_MAXNR + 1)];
650 1.18 drochner u_int8_t ac_block[AESCTR_BLOCKSIZE];
651 1.18 drochner int ac_nr;
652 1.20 drochner struct {
653 1.20 drochner u_int64_t lastiv;
654 1.20 drochner } ivgenctx;
655 1.18 drochner };
656 1.18 drochner
657 1.18 drochner static void
658 1.18 drochner aes_ctr_crypt(void *key, u_int8_t *blk)
659 1.18 drochner {
660 1.18 drochner struct aes_ctr_ctx *ctx;
661 1.18 drochner u_int8_t keystream[AESCTR_BLOCKSIZE];
662 1.18 drochner int i;
663 1.18 drochner
664 1.18 drochner ctx = key;
665 1.18 drochner /* increment counter */
666 1.18 drochner for (i = AESCTR_BLOCKSIZE - 1;
667 1.18 drochner i >= AESCTR_NONCESIZE + AESCTR_IVSIZE; i--)
668 1.18 drochner if (++ctx->ac_block[i]) /* continue on overflow */
669 1.18 drochner break;
670 1.18 drochner rijndaelEncrypt(ctx->ac_ek, ctx->ac_nr, ctx->ac_block, keystream);
671 1.18 drochner for (i = 0; i < AESCTR_BLOCKSIZE; i++)
672 1.18 drochner blk[i] ^= keystream[i];
673 1.18 drochner memset(keystream, 0, sizeof(keystream));
674 1.18 drochner }
675 1.18 drochner
676 1.18 drochner int
677 1.18 drochner aes_ctr_setkey(u_int8_t **sched, const u_int8_t *key, int len)
678 1.18 drochner {
679 1.18 drochner struct aes_ctr_ctx *ctx;
680 1.18 drochner
681 1.18 drochner if (len < AESCTR_NONCESIZE)
682 1.18 drochner return EINVAL;
683 1.18 drochner
684 1.18 drochner ctx = malloc(sizeof(struct aes_ctr_ctx), M_CRYPTO_DATA,
685 1.18 drochner M_NOWAIT|M_ZERO);
686 1.18 drochner if (!ctx)
687 1.18 drochner return ENOMEM;
688 1.18 drochner ctx->ac_nr = rijndaelKeySetupEnc(ctx->ac_ek, (const u_char *)key,
689 1.18 drochner (len - AESCTR_NONCESIZE) * 8);
690 1.18 drochner if (!ctx->ac_nr) { /* wrong key len */
691 1.18 drochner aes_ctr_zerokey((u_int8_t **)&ctx);
692 1.18 drochner return EINVAL;
693 1.18 drochner }
694 1.18 drochner memcpy(ctx->ac_block, key + len - AESCTR_NONCESIZE, AESCTR_NONCESIZE);
695 1.20 drochner /* random start value for simple counter */
696 1.20 drochner arc4randbytes(&ctx->ivgenctx.lastiv, sizeof(ctx->ivgenctx.lastiv));
697 1.18 drochner *sched = (void *)ctx;
698 1.18 drochner return 0;
699 1.18 drochner }
700 1.18 drochner
701 1.18 drochner void
702 1.18 drochner aes_ctr_zerokey(u_int8_t **sched)
703 1.18 drochner {
704 1.18 drochner
705 1.18 drochner memset(*sched, 0, sizeof(struct aes_ctr_ctx));
706 1.18 drochner free(*sched, M_CRYPTO_DATA);
707 1.18 drochner *sched = NULL;
708 1.18 drochner }
709 1.18 drochner
710 1.18 drochner void
711 1.20 drochner aes_ctr_reinit(void *key, const u_int8_t *iv, u_int8_t *ivout)
712 1.18 drochner {
713 1.18 drochner struct aes_ctr_ctx *ctx = key;
714 1.18 drochner
715 1.20 drochner if (!iv) {
716 1.20 drochner ctx->ivgenctx.lastiv++;
717 1.20 drochner iv = (const u_int8_t *)&ctx->ivgenctx.lastiv;
718 1.20 drochner }
719 1.20 drochner if (ivout)
720 1.20 drochner memcpy(ivout, iv, AESCTR_IVSIZE);
721 1.18 drochner memcpy(ctx->ac_block + AESCTR_NONCESIZE, iv, AESCTR_IVSIZE);
722 1.18 drochner /* reset counter */
723 1.18 drochner memset(ctx->ac_block + AESCTR_NONCESIZE + AESCTR_IVSIZE, 0, 4);
724 1.18 drochner }
725 1.18 drochner
726 1.1 thorpej /*
727 1.1 thorpej * And now for auth.
728 1.1 thorpej */
729 1.1 thorpej
730 1.1 thorpej static void
731 1.4 christos null_init(void *ctx)
732 1.1 thorpej {
733 1.1 thorpej }
734 1.1 thorpej
735 1.1 thorpej static int
736 1.4 christos null_update(void *ctx, const u_int8_t *buf,
737 1.4 christos u_int16_t len)
738 1.1 thorpej {
739 1.1 thorpej return 0;
740 1.1 thorpej }
741 1.1 thorpej
742 1.1 thorpej static void
743 1.4 christos null_final(u_int8_t *buf, void *ctx)
744 1.1 thorpej {
745 1.1 thorpej if (buf != (u_int8_t *) 0)
746 1.11 cegger memset(buf, 0, 12);
747 1.1 thorpej }
748 1.1 thorpej
749 1.1 thorpej static int
750 1.1 thorpej RMD160Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
751 1.1 thorpej {
752 1.1 thorpej RMD160Update(ctx, buf, len);
753 1.1 thorpej return 0;
754 1.1 thorpej }
755 1.1 thorpej
756 1.1 thorpej static int
757 1.1 thorpej MD5Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
758 1.1 thorpej {
759 1.1 thorpej MD5Update(ctx, buf, len);
760 1.1 thorpej return 0;
761 1.1 thorpej }
762 1.1 thorpej
763 1.1 thorpej static void
764 1.1 thorpej SHA1Init_int(void *ctx)
765 1.1 thorpej {
766 1.1 thorpej SHA1Init(ctx);
767 1.1 thorpej }
768 1.1 thorpej
769 1.1 thorpej static int
770 1.1 thorpej SHA1Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
771 1.1 thorpej {
772 1.1 thorpej SHA1Update(ctx, buf, len);
773 1.1 thorpej return 0;
774 1.1 thorpej }
775 1.1 thorpej
776 1.1 thorpej static void
777 1.1 thorpej SHA1Final_int(u_int8_t *blk, void *ctx)
778 1.1 thorpej {
779 1.1 thorpej SHA1Final(blk, ctx);
780 1.1 thorpej }
781 1.1 thorpej
782 1.1 thorpej static int
783 1.1 thorpej SHA256Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
784 1.1 thorpej {
785 1.1 thorpej SHA256_Update(ctx, buf, len);
786 1.1 thorpej return 0;
787 1.1 thorpej }
788 1.1 thorpej
789 1.1 thorpej static int
790 1.1 thorpej SHA384Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
791 1.1 thorpej {
792 1.1 thorpej SHA384_Update(ctx, buf, len);
793 1.1 thorpej return 0;
794 1.1 thorpej }
795 1.1 thorpej
796 1.1 thorpej static int
797 1.1 thorpej SHA512Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
798 1.1 thorpej {
799 1.1 thorpej SHA512_Update(ctx, buf, len);
800 1.1 thorpej return 0;
801 1.1 thorpej }
802 1.1 thorpej
803 1.1 thorpej /*
804 1.1 thorpej * And compression
805 1.1 thorpej */
806 1.1 thorpej
807 1.1 thorpej static u_int32_t
808 1.10 dsl deflate_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
809 1.1 thorpej {
810 1.14 drochner return deflate_global(data, size, 0, out, 0);
811 1.1 thorpej }
812 1.1 thorpej
813 1.1 thorpej static u_int32_t
814 1.14 drochner deflate_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out,
815 1.14 drochner int size_hint)
816 1.1 thorpej {
817 1.14 drochner return deflate_global(data, size, 1, out, size_hint);
818 1.1 thorpej }
819 1.12 darran
820 1.12 darran static u_int32_t
821 1.12 darran gzip_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
822 1.12 darran {
823 1.14 drochner return gzip_global(data, size, 0, out, 0);
824 1.12 darran }
825 1.12 darran
826 1.12 darran static u_int32_t
827 1.14 drochner gzip_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out,
828 1.14 drochner int size_hint)
829 1.12 darran {
830 1.14 drochner return gzip_global(data, size, 1, out, size_hint);
831 1.12 darran }
832