cryptosoft.c revision 1.36 1 1.36 drochner /* $NetBSD: cryptosoft.c,v 1.36 2011/05/24 19:10:10 drochner Exp $ */
2 1.1 jonathan /* $FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $ */
3 1.1 jonathan /* $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $ */
4 1.1 jonathan
5 1.1 jonathan /*
6 1.1 jonathan * The author of this code is Angelos D. Keromytis (angelos (at) cis.upenn.edu)
7 1.1 jonathan *
8 1.1 jonathan * This code was written by Angelos D. Keromytis in Athens, Greece, in
9 1.1 jonathan * February 2000. Network Security Technologies Inc. (NSTI) kindly
10 1.1 jonathan * supported the development of this code.
11 1.1 jonathan *
12 1.1 jonathan * Copyright (c) 2000, 2001 Angelos D. Keromytis
13 1.1 jonathan *
14 1.1 jonathan * Permission to use, copy, and modify this software with or without fee
15 1.1 jonathan * is hereby granted, provided that this entire notice is included in
16 1.1 jonathan * all source code copies of any software which is or includes a copy or
17 1.1 jonathan * modification of this software.
18 1.1 jonathan *
19 1.1 jonathan * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
20 1.1 jonathan * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
21 1.1 jonathan * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
22 1.1 jonathan * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
23 1.1 jonathan * PURPOSE.
24 1.1 jonathan */
25 1.1 jonathan
26 1.1 jonathan #include <sys/cdefs.h>
27 1.36 drochner __KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.36 2011/05/24 19:10:10 drochner Exp $");
28 1.1 jonathan
29 1.1 jonathan #include <sys/param.h>
30 1.1 jonathan #include <sys/systm.h>
31 1.1 jonathan #include <sys/malloc.h>
32 1.1 jonathan #include <sys/mbuf.h>
33 1.1 jonathan #include <sys/sysctl.h>
34 1.1 jonathan #include <sys/errno.h>
35 1.5 jonathan
36 1.20 tls #include "opt_ocf.h"
37 1.1 jonathan #include <opencrypto/cryptodev.h>
38 1.1 jonathan #include <opencrypto/cryptosoft.h>
39 1.1 jonathan #include <opencrypto/xform.h>
40 1.1 jonathan
41 1.10 thorpej #include <opencrypto/cryptosoft_xform.c>
42 1.1 jonathan
43 1.10 thorpej union authctx {
44 1.10 thorpej MD5_CTX md5ctx;
45 1.10 thorpej SHA1_CTX sha1ctx;
46 1.10 thorpej RMD160_CTX rmd160ctx;
47 1.10 thorpej SHA256_CTX sha256ctx;
48 1.10 thorpej SHA384_CTX sha384ctx;
49 1.10 thorpej SHA512_CTX sha512ctx;
50 1.36 drochner aesxcbc_ctx aesxcbcctx;
51 1.1 jonathan };
52 1.1 jonathan
53 1.1 jonathan struct swcr_data **swcr_sessions = NULL;
54 1.1 jonathan u_int32_t swcr_sesnum = 0;
55 1.1 jonathan int32_t swcr_id = -1;
56 1.1 jonathan
57 1.1 jonathan #define COPYBACK(x, a, b, c, d) \
58 1.1 jonathan (x) == CRYPTO_BUF_MBUF ? m_copyback((struct mbuf *)a,b,c,d) \
59 1.1 jonathan : cuio_copyback((struct uio *)a,b,c,d)
60 1.1 jonathan #define COPYDATA(x, a, b, c, d) \
61 1.1 jonathan (x) == CRYPTO_BUF_MBUF ? m_copydata((struct mbuf *)a,b,c,d) \
62 1.1 jonathan : cuio_copydata((struct uio *)a,b,c,d)
63 1.1 jonathan
64 1.27 drochner static int swcr_encdec(struct cryptodesc *, const struct swcr_data *, void *, int);
65 1.27 drochner static int swcr_compdec(struct cryptodesc *, const struct swcr_data *, void *, int, int *);
66 1.1 jonathan static int swcr_process(void *, struct cryptop *, int);
67 1.1 jonathan static int swcr_newsession(void *, u_int32_t *, struct cryptoini *);
68 1.1 jonathan static int swcr_freesession(void *, u_int64_t);
69 1.1 jonathan
70 1.1 jonathan /*
71 1.1 jonathan * Apply a symmetric encryption/decryption algorithm.
72 1.1 jonathan */
73 1.1 jonathan static int
74 1.27 drochner swcr_encdec(struct cryptodesc *crd, const struct swcr_data *sw, void *bufv,
75 1.1 jonathan int outtype)
76 1.1 jonathan {
77 1.17 christos char *buf = bufv;
78 1.1 jonathan unsigned char iv[EALG_MAX_BLOCK_LEN], blk[EALG_MAX_BLOCK_LEN], *idat;
79 1.1 jonathan unsigned char *ivp, piv[EALG_MAX_BLOCK_LEN];
80 1.10 thorpej const struct swcr_enc_xform *exf;
81 1.32 drochner int i, k, j, blks, ivlen;
82 1.1 jonathan int count, ind;
83 1.1 jonathan
84 1.1 jonathan exf = sw->sw_exf;
85 1.10 thorpej blks = exf->enc_xform->blocksize;
86 1.32 drochner ivlen = exf->enc_xform->ivsize;
87 1.32 drochner KASSERT(exf->reinit ? ivlen <= blks : ivlen == blks);
88 1.1 jonathan
89 1.1 jonathan /* Check for non-padded data */
90 1.1 jonathan if (crd->crd_len % blks)
91 1.1 jonathan return EINVAL;
92 1.1 jonathan
93 1.1 jonathan /* Initialize the IV */
94 1.1 jonathan if (crd->crd_flags & CRD_F_ENCRYPT) {
95 1.1 jonathan /* IV explicitly provided ? */
96 1.34 drochner if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
97 1.32 drochner memcpy(iv, crd->crd_iv, ivlen);
98 1.34 drochner if (exf->reinit)
99 1.34 drochner exf->reinit(sw->sw_kschedule, iv, 0);
100 1.34 drochner } else if (exf->reinit) {
101 1.34 drochner exf->reinit(sw->sw_kschedule, 0, iv);
102 1.34 drochner } else {
103 1.1 jonathan /* Get random IV */
104 1.1 jonathan for (i = 0;
105 1.31 drochner i + sizeof (u_int32_t) <= EALG_MAX_BLOCK_LEN;
106 1.1 jonathan i += sizeof (u_int32_t)) {
107 1.1 jonathan u_int32_t temp = arc4random();
108 1.1 jonathan
109 1.25 tsutsui memcpy(iv + i, &temp, sizeof(u_int32_t));
110 1.1 jonathan }
111 1.1 jonathan /*
112 1.1 jonathan * What if the block size is not a multiple
113 1.1 jonathan * of sizeof (u_int32_t), which is the size of
114 1.1 jonathan * what arc4random() returns ?
115 1.1 jonathan */
116 1.1 jonathan if (EALG_MAX_BLOCK_LEN % sizeof (u_int32_t) != 0) {
117 1.1 jonathan u_int32_t temp = arc4random();
118 1.1 jonathan
119 1.1 jonathan bcopy (&temp, iv + i,
120 1.1 jonathan EALG_MAX_BLOCK_LEN - i);
121 1.1 jonathan }
122 1.1 jonathan }
123 1.1 jonathan
124 1.1 jonathan /* Do we need to write the IV */
125 1.1 jonathan if (!(crd->crd_flags & CRD_F_IV_PRESENT)) {
126 1.32 drochner COPYBACK(outtype, buf, crd->crd_inject, ivlen, iv);
127 1.1 jonathan }
128 1.1 jonathan
129 1.1 jonathan } else { /* Decryption */
130 1.1 jonathan /* IV explicitly provided ? */
131 1.1 jonathan if (crd->crd_flags & CRD_F_IV_EXPLICIT)
132 1.32 drochner memcpy(iv, crd->crd_iv, ivlen);
133 1.1 jonathan else {
134 1.1 jonathan /* Get IV off buf */
135 1.32 drochner COPYDATA(outtype, buf, crd->crd_inject, ivlen, iv);
136 1.1 jonathan }
137 1.34 drochner if (exf->reinit)
138 1.34 drochner exf->reinit(sw->sw_kschedule, iv, 0);
139 1.1 jonathan }
140 1.1 jonathan
141 1.1 jonathan ivp = iv;
142 1.1 jonathan
143 1.1 jonathan if (outtype == CRYPTO_BUF_CONTIG) {
144 1.32 drochner if (exf->reinit) {
145 1.32 drochner for (i = crd->crd_skip;
146 1.32 drochner i < crd->crd_skip + crd->crd_len; i += blks) {
147 1.32 drochner if (crd->crd_flags & CRD_F_ENCRYPT) {
148 1.32 drochner exf->encrypt(sw->sw_kschedule, buf + i);
149 1.32 drochner } else {
150 1.32 drochner exf->decrypt(sw->sw_kschedule, buf + i);
151 1.32 drochner }
152 1.32 drochner }
153 1.32 drochner } else if (crd->crd_flags & CRD_F_ENCRYPT) {
154 1.1 jonathan for (i = crd->crd_skip;
155 1.1 jonathan i < crd->crd_skip + crd->crd_len; i += blks) {
156 1.1 jonathan /* XOR with the IV/previous block, as appropriate. */
157 1.1 jonathan if (i == crd->crd_skip)
158 1.1 jonathan for (k = 0; k < blks; k++)
159 1.1 jonathan buf[i + k] ^= ivp[k];
160 1.1 jonathan else
161 1.1 jonathan for (k = 0; k < blks; k++)
162 1.1 jonathan buf[i + k] ^= buf[i + k - blks];
163 1.1 jonathan exf->encrypt(sw->sw_kschedule, buf + i);
164 1.1 jonathan }
165 1.1 jonathan } else { /* Decrypt */
166 1.1 jonathan /*
167 1.1 jonathan * Start at the end, so we don't need to keep the encrypted
168 1.1 jonathan * block as the IV for the next block.
169 1.1 jonathan */
170 1.1 jonathan for (i = crd->crd_skip + crd->crd_len - blks;
171 1.1 jonathan i >= crd->crd_skip; i -= blks) {
172 1.1 jonathan exf->decrypt(sw->sw_kschedule, buf + i);
173 1.1 jonathan
174 1.1 jonathan /* XOR with the IV/previous block, as appropriate */
175 1.1 jonathan if (i == crd->crd_skip)
176 1.1 jonathan for (k = 0; k < blks; k++)
177 1.1 jonathan buf[i + k] ^= ivp[k];
178 1.1 jonathan else
179 1.1 jonathan for (k = 0; k < blks; k++)
180 1.1 jonathan buf[i + k] ^= buf[i + k - blks];
181 1.1 jonathan }
182 1.1 jonathan }
183 1.1 jonathan
184 1.1 jonathan return 0;
185 1.1 jonathan } else if (outtype == CRYPTO_BUF_MBUF) {
186 1.1 jonathan struct mbuf *m = (struct mbuf *) buf;
187 1.1 jonathan
188 1.1 jonathan /* Find beginning of data */
189 1.1 jonathan m = m_getptr(m, crd->crd_skip, &k);
190 1.1 jonathan if (m == NULL)
191 1.1 jonathan return EINVAL;
192 1.1 jonathan
193 1.1 jonathan i = crd->crd_len;
194 1.1 jonathan
195 1.1 jonathan while (i > 0) {
196 1.1 jonathan /*
197 1.1 jonathan * If there's insufficient data at the end of
198 1.1 jonathan * an mbuf, we have to do some copying.
199 1.1 jonathan */
200 1.1 jonathan if (m->m_len < k + blks && m->m_len != k) {
201 1.1 jonathan m_copydata(m, k, blks, blk);
202 1.1 jonathan
203 1.1 jonathan /* Actual encryption/decryption */
204 1.32 drochner if (exf->reinit) {
205 1.32 drochner if (crd->crd_flags & CRD_F_ENCRYPT) {
206 1.32 drochner exf->encrypt(sw->sw_kschedule,
207 1.32 drochner blk);
208 1.32 drochner } else {
209 1.32 drochner exf->decrypt(sw->sw_kschedule,
210 1.32 drochner blk);
211 1.32 drochner }
212 1.32 drochner } else if (crd->crd_flags & CRD_F_ENCRYPT) {
213 1.1 jonathan /* XOR with previous block */
214 1.1 jonathan for (j = 0; j < blks; j++)
215 1.1 jonathan blk[j] ^= ivp[j];
216 1.1 jonathan
217 1.1 jonathan exf->encrypt(sw->sw_kschedule, blk);
218 1.1 jonathan
219 1.1 jonathan /*
220 1.1 jonathan * Keep encrypted block for XOR'ing
221 1.1 jonathan * with next block
222 1.1 jonathan */
223 1.25 tsutsui memcpy(iv, blk, blks);
224 1.1 jonathan ivp = iv;
225 1.1 jonathan } else { /* decrypt */
226 1.1 jonathan /*
227 1.1 jonathan * Keep encrypted block for XOR'ing
228 1.1 jonathan * with next block
229 1.1 jonathan */
230 1.1 jonathan if (ivp == iv)
231 1.25 tsutsui memcpy(piv, blk, blks);
232 1.1 jonathan else
233 1.25 tsutsui memcpy(iv, blk, blks);
234 1.1 jonathan
235 1.1 jonathan exf->decrypt(sw->sw_kschedule, blk);
236 1.1 jonathan
237 1.1 jonathan /* XOR with previous block */
238 1.1 jonathan for (j = 0; j < blks; j++)
239 1.1 jonathan blk[j] ^= ivp[j];
240 1.1 jonathan
241 1.1 jonathan if (ivp == iv)
242 1.25 tsutsui memcpy(iv, piv, blks);
243 1.1 jonathan else
244 1.1 jonathan ivp = iv;
245 1.1 jonathan }
246 1.1 jonathan
247 1.1 jonathan /* Copy back decrypted block */
248 1.1 jonathan m_copyback(m, k, blks, blk);
249 1.1 jonathan
250 1.1 jonathan /* Advance pointer */
251 1.1 jonathan m = m_getptr(m, k + blks, &k);
252 1.1 jonathan if (m == NULL)
253 1.1 jonathan return EINVAL;
254 1.1 jonathan
255 1.1 jonathan i -= blks;
256 1.1 jonathan
257 1.1 jonathan /* Could be done... */
258 1.1 jonathan if (i == 0)
259 1.1 jonathan break;
260 1.1 jonathan }
261 1.1 jonathan
262 1.1 jonathan /* Skip possibly empty mbufs */
263 1.1 jonathan if (k == m->m_len) {
264 1.1 jonathan for (m = m->m_next; m && m->m_len == 0;
265 1.1 jonathan m = m->m_next)
266 1.1 jonathan ;
267 1.1 jonathan k = 0;
268 1.1 jonathan }
269 1.1 jonathan
270 1.1 jonathan /* Sanity check */
271 1.1 jonathan if (m == NULL)
272 1.1 jonathan return EINVAL;
273 1.1 jonathan
274 1.1 jonathan /*
275 1.1 jonathan * Warning: idat may point to garbage here, but
276 1.1 jonathan * we only use it in the while() loop, only if
277 1.1 jonathan * there are indeed enough data.
278 1.1 jonathan */
279 1.1 jonathan idat = mtod(m, unsigned char *) + k;
280 1.1 jonathan
281 1.1 jonathan while (m->m_len >= k + blks && i > 0) {
282 1.32 drochner if (exf->reinit) {
283 1.32 drochner if (crd->crd_flags & CRD_F_ENCRYPT) {
284 1.32 drochner exf->encrypt(sw->sw_kschedule,
285 1.32 drochner idat);
286 1.32 drochner } else {
287 1.32 drochner exf->decrypt(sw->sw_kschedule,
288 1.32 drochner idat);
289 1.32 drochner }
290 1.32 drochner } else if (crd->crd_flags & CRD_F_ENCRYPT) {
291 1.1 jonathan /* XOR with previous block/IV */
292 1.1 jonathan for (j = 0; j < blks; j++)
293 1.1 jonathan idat[j] ^= ivp[j];
294 1.1 jonathan
295 1.1 jonathan exf->encrypt(sw->sw_kschedule, idat);
296 1.1 jonathan ivp = idat;
297 1.1 jonathan } else { /* decrypt */
298 1.1 jonathan /*
299 1.1 jonathan * Keep encrypted block to be used
300 1.1 jonathan * in next block's processing.
301 1.1 jonathan */
302 1.1 jonathan if (ivp == iv)
303 1.25 tsutsui memcpy(piv, idat, blks);
304 1.1 jonathan else
305 1.25 tsutsui memcpy(iv, idat, blks);
306 1.1 jonathan
307 1.1 jonathan exf->decrypt(sw->sw_kschedule, idat);
308 1.1 jonathan
309 1.1 jonathan /* XOR with previous block/IV */
310 1.1 jonathan for (j = 0; j < blks; j++)
311 1.1 jonathan idat[j] ^= ivp[j];
312 1.1 jonathan
313 1.1 jonathan if (ivp == iv)
314 1.25 tsutsui memcpy(iv, piv, blks);
315 1.1 jonathan else
316 1.1 jonathan ivp = iv;
317 1.1 jonathan }
318 1.1 jonathan
319 1.1 jonathan idat += blks;
320 1.1 jonathan k += blks;
321 1.1 jonathan i -= blks;
322 1.1 jonathan }
323 1.1 jonathan }
324 1.1 jonathan
325 1.1 jonathan return 0; /* Done with mbuf encryption/decryption */
326 1.1 jonathan } else if (outtype == CRYPTO_BUF_IOV) {
327 1.1 jonathan struct uio *uio = (struct uio *) buf;
328 1.1 jonathan
329 1.1 jonathan /* Find beginning of data */
330 1.1 jonathan count = crd->crd_skip;
331 1.1 jonathan ind = cuio_getptr(uio, count, &k);
332 1.1 jonathan if (ind == -1)
333 1.1 jonathan return EINVAL;
334 1.1 jonathan
335 1.1 jonathan i = crd->crd_len;
336 1.1 jonathan
337 1.1 jonathan while (i > 0) {
338 1.1 jonathan /*
339 1.1 jonathan * If there's insufficient data at the end,
340 1.1 jonathan * we have to do some copying.
341 1.1 jonathan */
342 1.1 jonathan if (uio->uio_iov[ind].iov_len < k + blks &&
343 1.1 jonathan uio->uio_iov[ind].iov_len != k) {
344 1.1 jonathan cuio_copydata(uio, k, blks, blk);
345 1.1 jonathan
346 1.1 jonathan /* Actual encryption/decryption */
347 1.32 drochner if (exf->reinit) {
348 1.32 drochner if (crd->crd_flags & CRD_F_ENCRYPT) {
349 1.32 drochner exf->encrypt(sw->sw_kschedule,
350 1.32 drochner blk);
351 1.32 drochner } else {
352 1.32 drochner exf->decrypt(sw->sw_kschedule,
353 1.32 drochner blk);
354 1.32 drochner }
355 1.32 drochner } else if (crd->crd_flags & CRD_F_ENCRYPT) {
356 1.1 jonathan /* XOR with previous block */
357 1.1 jonathan for (j = 0; j < blks; j++)
358 1.1 jonathan blk[j] ^= ivp[j];
359 1.1 jonathan
360 1.1 jonathan exf->encrypt(sw->sw_kschedule, blk);
361 1.1 jonathan
362 1.1 jonathan /*
363 1.1 jonathan * Keep encrypted block for XOR'ing
364 1.1 jonathan * with next block
365 1.1 jonathan */
366 1.25 tsutsui memcpy(iv, blk, blks);
367 1.1 jonathan ivp = iv;
368 1.1 jonathan } else { /* decrypt */
369 1.1 jonathan /*
370 1.1 jonathan * Keep encrypted block for XOR'ing
371 1.1 jonathan * with next block
372 1.1 jonathan */
373 1.1 jonathan if (ivp == iv)
374 1.25 tsutsui memcpy(piv, blk, blks);
375 1.1 jonathan else
376 1.25 tsutsui memcpy(iv, blk, blks);
377 1.1 jonathan
378 1.1 jonathan exf->decrypt(sw->sw_kschedule, blk);
379 1.1 jonathan
380 1.1 jonathan /* XOR with previous block */
381 1.1 jonathan for (j = 0; j < blks; j++)
382 1.1 jonathan blk[j] ^= ivp[j];
383 1.1 jonathan
384 1.1 jonathan if (ivp == iv)
385 1.25 tsutsui memcpy(iv, piv, blks);
386 1.1 jonathan else
387 1.1 jonathan ivp = iv;
388 1.1 jonathan }
389 1.1 jonathan
390 1.1 jonathan /* Copy back decrypted block */
391 1.1 jonathan cuio_copyback(uio, k, blks, blk);
392 1.1 jonathan
393 1.1 jonathan count += blks;
394 1.1 jonathan
395 1.1 jonathan /* Advance pointer */
396 1.1 jonathan ind = cuio_getptr(uio, count, &k);
397 1.1 jonathan if (ind == -1)
398 1.1 jonathan return (EINVAL);
399 1.1 jonathan
400 1.1 jonathan i -= blks;
401 1.1 jonathan
402 1.1 jonathan /* Could be done... */
403 1.1 jonathan if (i == 0)
404 1.1 jonathan break;
405 1.1 jonathan }
406 1.1 jonathan
407 1.1 jonathan /*
408 1.1 jonathan * Warning: idat may point to garbage here, but
409 1.1 jonathan * we only use it in the while() loop, only if
410 1.1 jonathan * there are indeed enough data.
411 1.1 jonathan */
412 1.17 christos idat = ((char *)uio->uio_iov[ind].iov_base) + k;
413 1.1 jonathan
414 1.1 jonathan while (uio->uio_iov[ind].iov_len >= k + blks &&
415 1.1 jonathan i > 0) {
416 1.32 drochner if (exf->reinit) {
417 1.32 drochner if (crd->crd_flags & CRD_F_ENCRYPT) {
418 1.32 drochner exf->encrypt(sw->sw_kschedule,
419 1.32 drochner idat);
420 1.32 drochner } else {
421 1.32 drochner exf->decrypt(sw->sw_kschedule,
422 1.32 drochner idat);
423 1.32 drochner }
424 1.32 drochner } else if (crd->crd_flags & CRD_F_ENCRYPT) {
425 1.1 jonathan /* XOR with previous block/IV */
426 1.1 jonathan for (j = 0; j < blks; j++)
427 1.1 jonathan idat[j] ^= ivp[j];
428 1.1 jonathan
429 1.1 jonathan exf->encrypt(sw->sw_kschedule, idat);
430 1.1 jonathan ivp = idat;
431 1.1 jonathan } else { /* decrypt */
432 1.1 jonathan /*
433 1.1 jonathan * Keep encrypted block to be used
434 1.1 jonathan * in next block's processing.
435 1.1 jonathan */
436 1.1 jonathan if (ivp == iv)
437 1.25 tsutsui memcpy(piv, idat, blks);
438 1.1 jonathan else
439 1.25 tsutsui memcpy(iv, idat, blks);
440 1.1 jonathan
441 1.1 jonathan exf->decrypt(sw->sw_kschedule, idat);
442 1.1 jonathan
443 1.1 jonathan /* XOR with previous block/IV */
444 1.1 jonathan for (j = 0; j < blks; j++)
445 1.1 jonathan idat[j] ^= ivp[j];
446 1.1 jonathan
447 1.1 jonathan if (ivp == iv)
448 1.25 tsutsui memcpy(iv, piv, blks);
449 1.1 jonathan else
450 1.1 jonathan ivp = iv;
451 1.1 jonathan }
452 1.1 jonathan
453 1.1 jonathan idat += blks;
454 1.1 jonathan count += blks;
455 1.1 jonathan k += blks;
456 1.1 jonathan i -= blks;
457 1.1 jonathan }
458 1.1 jonathan }
459 1.1 jonathan return 0; /* Done with mbuf encryption/decryption */
460 1.1 jonathan }
461 1.1 jonathan
462 1.1 jonathan /* Unreachable */
463 1.1 jonathan return EINVAL;
464 1.1 jonathan }
465 1.1 jonathan
466 1.1 jonathan /*
467 1.1 jonathan * Compute keyed-hash authenticator.
468 1.1 jonathan */
469 1.16 daniel int
470 1.1 jonathan swcr_authcompute(struct cryptop *crp, struct cryptodesc *crd,
471 1.27 drochner const struct swcr_data *sw, void *buf, int outtype)
472 1.1 jonathan {
473 1.1 jonathan unsigned char aalg[AALG_MAX_RESULT_LEN];
474 1.10 thorpej const struct swcr_auth_hash *axf;
475 1.1 jonathan union authctx ctx;
476 1.1 jonathan int err;
477 1.1 jonathan
478 1.1 jonathan if (sw->sw_ictx == 0)
479 1.1 jonathan return EINVAL;
480 1.1 jonathan
481 1.1 jonathan axf = sw->sw_axf;
482 1.1 jonathan
483 1.35 drochner memcpy(&ctx, sw->sw_ictx, axf->ctxsize);
484 1.1 jonathan
485 1.1 jonathan switch (outtype) {
486 1.1 jonathan case CRYPTO_BUF_CONTIG:
487 1.17 christos axf->Update(&ctx, (char *)buf + crd->crd_skip, crd->crd_len);
488 1.1 jonathan break;
489 1.1 jonathan case CRYPTO_BUF_MBUF:
490 1.1 jonathan err = m_apply((struct mbuf *) buf, crd->crd_skip, crd->crd_len,
491 1.17 christos (int (*)(void*, void *, unsigned int)) axf->Update,
492 1.17 christos (void *) &ctx);
493 1.1 jonathan if (err)
494 1.1 jonathan return err;
495 1.1 jonathan break;
496 1.1 jonathan case CRYPTO_BUF_IOV:
497 1.2 jonathan err = cuio_apply((struct uio *) buf, crd->crd_skip,
498 1.2 jonathan crd->crd_len,
499 1.17 christos (int (*)(void *, void *, unsigned int)) axf->Update,
500 1.17 christos (void *) &ctx);
501 1.2 jonathan if (err) {
502 1.2 jonathan return err;
503 1.2 jonathan }
504 1.2 jonathan break;
505 1.1 jonathan default:
506 1.1 jonathan return EINVAL;
507 1.1 jonathan }
508 1.1 jonathan
509 1.1 jonathan switch (sw->sw_alg) {
510 1.1 jonathan case CRYPTO_MD5_HMAC:
511 1.19 tls case CRYPTO_MD5_HMAC_96:
512 1.1 jonathan case CRYPTO_SHA1_HMAC:
513 1.19 tls case CRYPTO_SHA1_HMAC_96:
514 1.29 drochner case CRYPTO_SHA2_256_HMAC:
515 1.29 drochner case CRYPTO_SHA2_384_HMAC:
516 1.29 drochner case CRYPTO_SHA2_512_HMAC:
517 1.1 jonathan case CRYPTO_RIPEMD160_HMAC:
518 1.19 tls case CRYPTO_RIPEMD160_HMAC_96:
519 1.1 jonathan if (sw->sw_octx == NULL)
520 1.1 jonathan return EINVAL;
521 1.1 jonathan
522 1.1 jonathan axf->Final(aalg, &ctx);
523 1.35 drochner memcpy(&ctx, sw->sw_octx, axf->ctxsize);
524 1.10 thorpej axf->Update(&ctx, aalg, axf->auth_hash->hashsize);
525 1.1 jonathan axf->Final(aalg, &ctx);
526 1.1 jonathan break;
527 1.1 jonathan
528 1.1 jonathan case CRYPTO_MD5_KPDK:
529 1.1 jonathan case CRYPTO_SHA1_KPDK:
530 1.1 jonathan if (sw->sw_octx == NULL)
531 1.1 jonathan return EINVAL;
532 1.1 jonathan
533 1.1 jonathan axf->Update(&ctx, sw->sw_octx, sw->sw_klen);
534 1.1 jonathan axf->Final(aalg, &ctx);
535 1.1 jonathan break;
536 1.1 jonathan
537 1.1 jonathan case CRYPTO_NULL_HMAC:
538 1.1 jonathan case CRYPTO_MD5:
539 1.1 jonathan case CRYPTO_SHA1:
540 1.36 drochner case CRYPTO_AES_XCBC_MAC_96:
541 1.1 jonathan axf->Final(aalg, &ctx);
542 1.1 jonathan break;
543 1.1 jonathan }
544 1.1 jonathan
545 1.1 jonathan /* Inject the authentication data */
546 1.2 jonathan switch (outtype) {
547 1.2 jonathan case CRYPTO_BUF_CONTIG:
548 1.17 christos (void)memcpy((char *)buf + crd->crd_inject, aalg,
549 1.17 christos axf->auth_hash->authsize);
550 1.2 jonathan break;
551 1.2 jonathan case CRYPTO_BUF_MBUF:
552 1.1 jonathan m_copyback((struct mbuf *) buf, crd->crd_inject,
553 1.10 thorpej axf->auth_hash->authsize, aalg);
554 1.2 jonathan break;
555 1.2 jonathan case CRYPTO_BUF_IOV:
556 1.25 tsutsui memcpy(crp->crp_mac, aalg, axf->auth_hash->authsize);
557 1.2 jonathan break;
558 1.2 jonathan default:
559 1.2 jonathan return EINVAL;
560 1.2 jonathan }
561 1.1 jonathan return 0;
562 1.1 jonathan }
563 1.1 jonathan
564 1.1 jonathan /*
565 1.1 jonathan * Apply a compression/decompression algorithm
566 1.1 jonathan */
567 1.1 jonathan static int
568 1.27 drochner swcr_compdec(struct cryptodesc *crd, const struct swcr_data *sw,
569 1.27 drochner void *buf, int outtype, int *res_size)
570 1.1 jonathan {
571 1.1 jonathan u_int8_t *data, *out;
572 1.10 thorpej const struct swcr_comp_algo *cxf;
573 1.1 jonathan int adj;
574 1.1 jonathan u_int32_t result;
575 1.1 jonathan
576 1.1 jonathan cxf = sw->sw_cxf;
577 1.1 jonathan
578 1.1 jonathan /* We must handle the whole buffer of data in one time
579 1.1 jonathan * then if there is not all the data in the mbuf, we must
580 1.1 jonathan * copy in a buffer.
581 1.1 jonathan */
582 1.1 jonathan
583 1.12 christos data = malloc(crd->crd_len, M_CRYPTO_DATA, M_NOWAIT);
584 1.1 jonathan if (data == NULL)
585 1.1 jonathan return (EINVAL);
586 1.1 jonathan COPYDATA(outtype, buf, crd->crd_skip, crd->crd_len, data);
587 1.1 jonathan
588 1.1 jonathan if (crd->crd_flags & CRD_F_COMP)
589 1.1 jonathan result = cxf->compress(data, crd->crd_len, &out);
590 1.1 jonathan else
591 1.28 drochner result = cxf->decompress(data, crd->crd_len, &out,
592 1.28 drochner *res_size);
593 1.1 jonathan
594 1.21 cegger free(data, M_CRYPTO_DATA);
595 1.1 jonathan if (result == 0)
596 1.1 jonathan return EINVAL;
597 1.1 jonathan
598 1.1 jonathan /* Copy back the (de)compressed data. m_copyback is
599 1.1 jonathan * extending the mbuf as necessary.
600 1.1 jonathan */
601 1.27 drochner *res_size = (int)result;
602 1.1 jonathan /* Check the compressed size when doing compression */
603 1.28 drochner if (crd->crd_flags & CRD_F_COMP &&
604 1.28 drochner sw->sw_alg == CRYPTO_DEFLATE_COMP_NOGROW &&
605 1.28 drochner result >= crd->crd_len) {
606 1.1 jonathan /* Compression was useless, we lost time */
607 1.21 cegger free(out, M_CRYPTO_DATA);
608 1.1 jonathan return 0;
609 1.1 jonathan }
610 1.1 jonathan
611 1.1 jonathan COPYBACK(outtype, buf, crd->crd_skip, result, out);
612 1.1 jonathan if (result < crd->crd_len) {
613 1.1 jonathan adj = result - crd->crd_len;
614 1.1 jonathan if (outtype == CRYPTO_BUF_MBUF) {
615 1.1 jonathan adj = result - crd->crd_len;
616 1.1 jonathan m_adj((struct mbuf *)buf, adj);
617 1.1 jonathan }
618 1.24 darran /* Don't adjust the iov_len, it breaks the kmem_free */
619 1.1 jonathan }
620 1.21 cegger free(out, M_CRYPTO_DATA);
621 1.1 jonathan return 0;
622 1.1 jonathan }
623 1.1 jonathan
624 1.1 jonathan /*
625 1.1 jonathan * Generate a new software session.
626 1.1 jonathan */
627 1.1 jonathan static int
628 1.15 christos swcr_newsession(void *arg, u_int32_t *sid, struct cryptoini *cri)
629 1.1 jonathan {
630 1.1 jonathan struct swcr_data **swd;
631 1.10 thorpej const struct swcr_auth_hash *axf;
632 1.10 thorpej const struct swcr_enc_xform *txf;
633 1.10 thorpej const struct swcr_comp_algo *cxf;
634 1.1 jonathan u_int32_t i;
635 1.1 jonathan int k, error;
636 1.1 jonathan
637 1.1 jonathan if (sid == NULL || cri == NULL)
638 1.1 jonathan return EINVAL;
639 1.1 jonathan
640 1.1 jonathan if (swcr_sessions) {
641 1.1 jonathan for (i = 1; i < swcr_sesnum; i++)
642 1.1 jonathan if (swcr_sessions[i] == NULL)
643 1.1 jonathan break;
644 1.1 jonathan } else
645 1.1 jonathan i = 1; /* NB: to silence compiler warning */
646 1.1 jonathan
647 1.1 jonathan if (swcr_sessions == NULL || i == swcr_sesnum) {
648 1.1 jonathan if (swcr_sessions == NULL) {
649 1.1 jonathan i = 1; /* We leave swcr_sessions[0] empty */
650 1.1 jonathan swcr_sesnum = CRYPTO_SW_SESSIONS;
651 1.1 jonathan } else
652 1.1 jonathan swcr_sesnum *= 2;
653 1.1 jonathan
654 1.1 jonathan swd = malloc(swcr_sesnum * sizeof(struct swcr_data *),
655 1.1 jonathan M_CRYPTO_DATA, M_NOWAIT);
656 1.1 jonathan if (swd == NULL) {
657 1.1 jonathan /* Reset session number */
658 1.1 jonathan if (swcr_sesnum == CRYPTO_SW_SESSIONS)
659 1.1 jonathan swcr_sesnum = 0;
660 1.1 jonathan else
661 1.1 jonathan swcr_sesnum /= 2;
662 1.1 jonathan return ENOBUFS;
663 1.1 jonathan }
664 1.1 jonathan
665 1.22 cegger memset(swd, 0, swcr_sesnum * sizeof(struct swcr_data *));
666 1.1 jonathan
667 1.1 jonathan /* Copy existing sessions */
668 1.1 jonathan if (swcr_sessions) {
669 1.25 tsutsui memcpy(swd, swcr_sessions,
670 1.1 jonathan (swcr_sesnum / 2) * sizeof(struct swcr_data *));
671 1.1 jonathan free(swcr_sessions, M_CRYPTO_DATA);
672 1.1 jonathan }
673 1.1 jonathan
674 1.1 jonathan swcr_sessions = swd;
675 1.1 jonathan }
676 1.1 jonathan
677 1.1 jonathan swd = &swcr_sessions[i];
678 1.1 jonathan *sid = i;
679 1.1 jonathan
680 1.1 jonathan while (cri) {
681 1.13 dsl *swd = malloc(sizeof **swd, M_CRYPTO_DATA, M_NOWAIT);
682 1.1 jonathan if (*swd == NULL) {
683 1.1 jonathan swcr_freesession(NULL, i);
684 1.1 jonathan return ENOBUFS;
685 1.1 jonathan }
686 1.22 cegger memset(*swd, 0, sizeof(struct swcr_data));
687 1.1 jonathan
688 1.1 jonathan switch (cri->cri_alg) {
689 1.1 jonathan case CRYPTO_DES_CBC:
690 1.10 thorpej txf = &swcr_enc_xform_des;
691 1.1 jonathan goto enccommon;
692 1.1 jonathan case CRYPTO_3DES_CBC:
693 1.10 thorpej txf = &swcr_enc_xform_3des;
694 1.1 jonathan goto enccommon;
695 1.1 jonathan case CRYPTO_BLF_CBC:
696 1.10 thorpej txf = &swcr_enc_xform_blf;
697 1.1 jonathan goto enccommon;
698 1.1 jonathan case CRYPTO_CAST_CBC:
699 1.10 thorpej txf = &swcr_enc_xform_cast5;
700 1.1 jonathan goto enccommon;
701 1.1 jonathan case CRYPTO_SKIPJACK_CBC:
702 1.10 thorpej txf = &swcr_enc_xform_skipjack;
703 1.1 jonathan goto enccommon;
704 1.1 jonathan case CRYPTO_RIJNDAEL128_CBC:
705 1.10 thorpej txf = &swcr_enc_xform_rijndael128;
706 1.1 jonathan goto enccommon;
707 1.30 drochner case CRYPTO_CAMELLIA_CBC:
708 1.30 drochner txf = &swcr_enc_xform_camellia;
709 1.30 drochner goto enccommon;
710 1.33 drochner case CRYPTO_AES_CTR:
711 1.33 drochner txf = &swcr_enc_xform_aes_ctr;
712 1.33 drochner goto enccommon;
713 1.1 jonathan case CRYPTO_NULL_CBC:
714 1.10 thorpej txf = &swcr_enc_xform_null;
715 1.1 jonathan goto enccommon;
716 1.1 jonathan enccommon:
717 1.1 jonathan error = txf->setkey(&((*swd)->sw_kschedule),
718 1.1 jonathan cri->cri_key, cri->cri_klen / 8);
719 1.1 jonathan if (error) {
720 1.1 jonathan swcr_freesession(NULL, i);
721 1.1 jonathan return error;
722 1.1 jonathan }
723 1.1 jonathan (*swd)->sw_exf = txf;
724 1.1 jonathan break;
725 1.1 jonathan
726 1.1 jonathan case CRYPTO_MD5_HMAC:
727 1.19 tls axf = &swcr_auth_hash_hmac_md5;
728 1.19 tls goto authcommon;
729 1.19 tls case CRYPTO_MD5_HMAC_96:
730 1.10 thorpej axf = &swcr_auth_hash_hmac_md5_96;
731 1.1 jonathan goto authcommon;
732 1.1 jonathan case CRYPTO_SHA1_HMAC:
733 1.19 tls axf = &swcr_auth_hash_hmac_sha1;
734 1.19 tls goto authcommon;
735 1.19 tls case CRYPTO_SHA1_HMAC_96:
736 1.10 thorpej axf = &swcr_auth_hash_hmac_sha1_96;
737 1.1 jonathan goto authcommon;
738 1.29 drochner case CRYPTO_SHA2_256_HMAC:
739 1.29 drochner axf = &swcr_auth_hash_hmac_sha2_256;
740 1.29 drochner goto authcommon;
741 1.29 drochner case CRYPTO_SHA2_384_HMAC:
742 1.29 drochner axf = &swcr_auth_hash_hmac_sha2_384;
743 1.29 drochner goto authcommon;
744 1.29 drochner case CRYPTO_SHA2_512_HMAC:
745 1.29 drochner axf = &swcr_auth_hash_hmac_sha2_512;
746 1.1 jonathan goto authcommon;
747 1.1 jonathan case CRYPTO_NULL_HMAC:
748 1.10 thorpej axf = &swcr_auth_hash_null;
749 1.1 jonathan goto authcommon;
750 1.1 jonathan case CRYPTO_RIPEMD160_HMAC:
751 1.19 tls axf = &swcr_auth_hash_hmac_ripemd_160;
752 1.19 tls goto authcommon;
753 1.19 tls case CRYPTO_RIPEMD160_HMAC_96:
754 1.10 thorpej axf = &swcr_auth_hash_hmac_ripemd_160_96;
755 1.19 tls goto authcommon; /* leave this for safety */
756 1.1 jonathan authcommon:
757 1.35 drochner (*swd)->sw_ictx = malloc(axf->ctxsize,
758 1.10 thorpej M_CRYPTO_DATA, M_NOWAIT);
759 1.1 jonathan if ((*swd)->sw_ictx == NULL) {
760 1.1 jonathan swcr_freesession(NULL, i);
761 1.1 jonathan return ENOBUFS;
762 1.1 jonathan }
763 1.1 jonathan
764 1.35 drochner (*swd)->sw_octx = malloc(axf->ctxsize,
765 1.10 thorpej M_CRYPTO_DATA, M_NOWAIT);
766 1.1 jonathan if ((*swd)->sw_octx == NULL) {
767 1.1 jonathan swcr_freesession(NULL, i);
768 1.1 jonathan return ENOBUFS;
769 1.1 jonathan }
770 1.1 jonathan
771 1.1 jonathan for (k = 0; k < cri->cri_klen / 8; k++)
772 1.1 jonathan cri->cri_key[k] ^= HMAC_IPAD_VAL;
773 1.1 jonathan
774 1.1 jonathan axf->Init((*swd)->sw_ictx);
775 1.1 jonathan axf->Update((*swd)->sw_ictx, cri->cri_key,
776 1.1 jonathan cri->cri_klen / 8);
777 1.1 jonathan axf->Update((*swd)->sw_ictx, hmac_ipad_buffer,
778 1.29 drochner axf->auth_hash->blocksize - (cri->cri_klen / 8));
779 1.1 jonathan
780 1.1 jonathan for (k = 0; k < cri->cri_klen / 8; k++)
781 1.1 jonathan cri->cri_key[k] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
782 1.1 jonathan
783 1.1 jonathan axf->Init((*swd)->sw_octx);
784 1.1 jonathan axf->Update((*swd)->sw_octx, cri->cri_key,
785 1.1 jonathan cri->cri_klen / 8);
786 1.1 jonathan axf->Update((*swd)->sw_octx, hmac_opad_buffer,
787 1.29 drochner axf->auth_hash->blocksize - (cri->cri_klen / 8));
788 1.1 jonathan
789 1.1 jonathan for (k = 0; k < cri->cri_klen / 8; k++)
790 1.1 jonathan cri->cri_key[k] ^= HMAC_OPAD_VAL;
791 1.1 jonathan (*swd)->sw_axf = axf;
792 1.1 jonathan break;
793 1.1 jonathan
794 1.1 jonathan case CRYPTO_MD5_KPDK:
795 1.10 thorpej axf = &swcr_auth_hash_key_md5;
796 1.1 jonathan goto auth2common;
797 1.1 jonathan
798 1.1 jonathan case CRYPTO_SHA1_KPDK:
799 1.10 thorpej axf = &swcr_auth_hash_key_sha1;
800 1.1 jonathan auth2common:
801 1.35 drochner (*swd)->sw_ictx = malloc(axf->ctxsize,
802 1.10 thorpej M_CRYPTO_DATA, M_NOWAIT);
803 1.1 jonathan if ((*swd)->sw_ictx == NULL) {
804 1.1 jonathan swcr_freesession(NULL, i);
805 1.1 jonathan return ENOBUFS;
806 1.1 jonathan }
807 1.1 jonathan
808 1.1 jonathan /* Store the key so we can "append" it to the payload */
809 1.1 jonathan (*swd)->sw_octx = malloc(cri->cri_klen / 8, M_CRYPTO_DATA,
810 1.1 jonathan M_NOWAIT);
811 1.1 jonathan if ((*swd)->sw_octx == NULL) {
812 1.1 jonathan swcr_freesession(NULL, i);
813 1.1 jonathan return ENOBUFS;
814 1.1 jonathan }
815 1.1 jonathan
816 1.1 jonathan (*swd)->sw_klen = cri->cri_klen / 8;
817 1.25 tsutsui memcpy((*swd)->sw_octx, cri->cri_key, cri->cri_klen / 8);
818 1.1 jonathan axf->Init((*swd)->sw_ictx);
819 1.1 jonathan axf->Update((*swd)->sw_ictx, cri->cri_key,
820 1.1 jonathan cri->cri_klen / 8);
821 1.1 jonathan axf->Final(NULL, (*swd)->sw_ictx);
822 1.1 jonathan (*swd)->sw_axf = axf;
823 1.1 jonathan break;
824 1.1 jonathan
825 1.1 jonathan case CRYPTO_MD5:
826 1.10 thorpej axf = &swcr_auth_hash_md5;
827 1.1 jonathan goto auth3common;
828 1.1 jonathan
829 1.1 jonathan case CRYPTO_SHA1:
830 1.10 thorpej axf = &swcr_auth_hash_sha1;
831 1.1 jonathan auth3common:
832 1.35 drochner (*swd)->sw_ictx = malloc(axf->ctxsize,
833 1.10 thorpej M_CRYPTO_DATA, M_NOWAIT);
834 1.1 jonathan if ((*swd)->sw_ictx == NULL) {
835 1.1 jonathan swcr_freesession(NULL, i);
836 1.1 jonathan return ENOBUFS;
837 1.1 jonathan }
838 1.1 jonathan
839 1.1 jonathan axf->Init((*swd)->sw_ictx);
840 1.1 jonathan (*swd)->sw_axf = axf;
841 1.1 jonathan break;
842 1.1 jonathan
843 1.36 drochner case CRYPTO_AES_XCBC_MAC_96:
844 1.36 drochner axf = &swcr_auth_hash_aes_xcbc_mac;
845 1.36 drochner (*swd)->sw_ictx = malloc(axf->ctxsize,
846 1.36 drochner M_CRYPTO_DATA, M_NOWAIT);
847 1.36 drochner if ((*swd)->sw_ictx == NULL) {
848 1.36 drochner swcr_freesession(NULL, i);
849 1.36 drochner return ENOBUFS;
850 1.36 drochner }
851 1.36 drochner axf->Init((*swd)->sw_ictx);
852 1.36 drochner axf->Setkey((*swd)->sw_ictx,
853 1.36 drochner cri->cri_key, cri->cri_klen / 8);
854 1.36 drochner (*swd)->sw_axf = axf;
855 1.36 drochner break;
856 1.36 drochner
857 1.1 jonathan case CRYPTO_DEFLATE_COMP:
858 1.10 thorpej cxf = &swcr_comp_algo_deflate;
859 1.1 jonathan (*swd)->sw_cxf = cxf;
860 1.1 jonathan break;
861 1.24 darran
862 1.28 drochner case CRYPTO_DEFLATE_COMP_NOGROW:
863 1.28 drochner cxf = &swcr_comp_algo_deflate_nogrow;
864 1.28 drochner (*swd)->sw_cxf = cxf;
865 1.28 drochner break;
866 1.28 drochner
867 1.24 darran case CRYPTO_GZIP_COMP:
868 1.24 darran cxf = &swcr_comp_algo_gzip;
869 1.24 darran (*swd)->sw_cxf = cxf;
870 1.24 darran break;
871 1.1 jonathan default:
872 1.1 jonathan swcr_freesession(NULL, i);
873 1.1 jonathan return EINVAL;
874 1.1 jonathan }
875 1.1 jonathan
876 1.1 jonathan (*swd)->sw_alg = cri->cri_alg;
877 1.1 jonathan cri = cri->cri_next;
878 1.1 jonathan swd = &((*swd)->sw_next);
879 1.1 jonathan }
880 1.1 jonathan return 0;
881 1.1 jonathan }
882 1.1 jonathan
883 1.1 jonathan /*
884 1.1 jonathan * Free a session.
885 1.1 jonathan */
886 1.1 jonathan static int
887 1.15 christos swcr_freesession(void *arg, u_int64_t tid)
888 1.1 jonathan {
889 1.1 jonathan struct swcr_data *swd;
890 1.10 thorpej const struct swcr_enc_xform *txf;
891 1.10 thorpej const struct swcr_auth_hash *axf;
892 1.10 thorpej const struct swcr_comp_algo *cxf;
893 1.1 jonathan u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
894 1.1 jonathan
895 1.1 jonathan if (sid > swcr_sesnum || swcr_sessions == NULL ||
896 1.1 jonathan swcr_sessions[sid] == NULL)
897 1.1 jonathan return EINVAL;
898 1.1 jonathan
899 1.1 jonathan /* Silently accept and return */
900 1.1 jonathan if (sid == 0)
901 1.1 jonathan return 0;
902 1.1 jonathan
903 1.1 jonathan while ((swd = swcr_sessions[sid]) != NULL) {
904 1.1 jonathan swcr_sessions[sid] = swd->sw_next;
905 1.1 jonathan
906 1.1 jonathan switch (swd->sw_alg) {
907 1.1 jonathan case CRYPTO_DES_CBC:
908 1.1 jonathan case CRYPTO_3DES_CBC:
909 1.1 jonathan case CRYPTO_BLF_CBC:
910 1.1 jonathan case CRYPTO_CAST_CBC:
911 1.1 jonathan case CRYPTO_SKIPJACK_CBC:
912 1.1 jonathan case CRYPTO_RIJNDAEL128_CBC:
913 1.30 drochner case CRYPTO_CAMELLIA_CBC:
914 1.33 drochner case CRYPTO_AES_CTR:
915 1.1 jonathan case CRYPTO_NULL_CBC:
916 1.1 jonathan txf = swd->sw_exf;
917 1.1 jonathan
918 1.1 jonathan if (swd->sw_kschedule)
919 1.1 jonathan txf->zerokey(&(swd->sw_kschedule));
920 1.1 jonathan break;
921 1.1 jonathan
922 1.1 jonathan case CRYPTO_MD5_HMAC:
923 1.19 tls case CRYPTO_MD5_HMAC_96:
924 1.1 jonathan case CRYPTO_SHA1_HMAC:
925 1.19 tls case CRYPTO_SHA1_HMAC_96:
926 1.29 drochner case CRYPTO_SHA2_256_HMAC:
927 1.29 drochner case CRYPTO_SHA2_384_HMAC:
928 1.29 drochner case CRYPTO_SHA2_512_HMAC:
929 1.1 jonathan case CRYPTO_RIPEMD160_HMAC:
930 1.19 tls case CRYPTO_RIPEMD160_HMAC_96:
931 1.1 jonathan case CRYPTO_NULL_HMAC:
932 1.1 jonathan axf = swd->sw_axf;
933 1.1 jonathan
934 1.1 jonathan if (swd->sw_ictx) {
935 1.35 drochner memset(swd->sw_ictx, 0, axf->ctxsize);
936 1.1 jonathan free(swd->sw_ictx, M_CRYPTO_DATA);
937 1.1 jonathan }
938 1.1 jonathan if (swd->sw_octx) {
939 1.35 drochner memset(swd->sw_octx, 0, axf->ctxsize);
940 1.1 jonathan free(swd->sw_octx, M_CRYPTO_DATA);
941 1.1 jonathan }
942 1.1 jonathan break;
943 1.1 jonathan
944 1.1 jonathan case CRYPTO_MD5_KPDK:
945 1.1 jonathan case CRYPTO_SHA1_KPDK:
946 1.1 jonathan axf = swd->sw_axf;
947 1.1 jonathan
948 1.1 jonathan if (swd->sw_ictx) {
949 1.35 drochner memset(swd->sw_ictx, 0, axf->ctxsize);
950 1.1 jonathan free(swd->sw_ictx, M_CRYPTO_DATA);
951 1.1 jonathan }
952 1.1 jonathan if (swd->sw_octx) {
953 1.22 cegger memset(swd->sw_octx, 0, swd->sw_klen);
954 1.1 jonathan free(swd->sw_octx, M_CRYPTO_DATA);
955 1.1 jonathan }
956 1.1 jonathan break;
957 1.1 jonathan
958 1.1 jonathan case CRYPTO_MD5:
959 1.1 jonathan case CRYPTO_SHA1:
960 1.36 drochner case CRYPTO_AES_XCBC_MAC_96:
961 1.1 jonathan axf = swd->sw_axf;
962 1.1 jonathan
963 1.1 jonathan if (swd->sw_ictx)
964 1.1 jonathan free(swd->sw_ictx, M_CRYPTO_DATA);
965 1.1 jonathan break;
966 1.1 jonathan
967 1.1 jonathan case CRYPTO_DEFLATE_COMP:
968 1.28 drochner case CRYPTO_DEFLATE_COMP_NOGROW:
969 1.24 darran case CRYPTO_GZIP_COMP:
970 1.1 jonathan cxf = swd->sw_cxf;
971 1.1 jonathan break;
972 1.1 jonathan }
973 1.1 jonathan
974 1.21 cegger free(swd, M_CRYPTO_DATA);
975 1.1 jonathan }
976 1.1 jonathan return 0;
977 1.1 jonathan }
978 1.1 jonathan
979 1.1 jonathan /*
980 1.1 jonathan * Process a software request.
981 1.1 jonathan */
982 1.1 jonathan static int
983 1.15 christos swcr_process(void *arg, struct cryptop *crp, int hint)
984 1.1 jonathan {
985 1.1 jonathan struct cryptodesc *crd;
986 1.1 jonathan struct swcr_data *sw;
987 1.1 jonathan u_int32_t lid;
988 1.1 jonathan int type;
989 1.1 jonathan
990 1.1 jonathan /* Sanity check */
991 1.1 jonathan if (crp == NULL)
992 1.1 jonathan return EINVAL;
993 1.1 jonathan
994 1.1 jonathan if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
995 1.1 jonathan crp->crp_etype = EINVAL;
996 1.1 jonathan goto done;
997 1.1 jonathan }
998 1.1 jonathan
999 1.1 jonathan lid = crp->crp_sid & 0xffffffff;
1000 1.1 jonathan if (lid >= swcr_sesnum || lid == 0 || swcr_sessions[lid] == NULL) {
1001 1.1 jonathan crp->crp_etype = ENOENT;
1002 1.1 jonathan goto done;
1003 1.1 jonathan }
1004 1.1 jonathan
1005 1.1 jonathan if (crp->crp_flags & CRYPTO_F_IMBUF) {
1006 1.1 jonathan type = CRYPTO_BUF_MBUF;
1007 1.1 jonathan } else if (crp->crp_flags & CRYPTO_F_IOV) {
1008 1.1 jonathan type = CRYPTO_BUF_IOV;
1009 1.1 jonathan } else {
1010 1.1 jonathan type = CRYPTO_BUF_CONTIG;
1011 1.1 jonathan }
1012 1.1 jonathan
1013 1.1 jonathan /* Go through crypto descriptors, processing as we go */
1014 1.1 jonathan for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
1015 1.1 jonathan /*
1016 1.1 jonathan * Find the crypto context.
1017 1.1 jonathan *
1018 1.1 jonathan * XXX Note that the logic here prevents us from having
1019 1.1 jonathan * XXX the same algorithm multiple times in a session
1020 1.1 jonathan * XXX (or rather, we can but it won't give us the right
1021 1.1 jonathan * XXX results). To do that, we'd need some way of differentiating
1022 1.1 jonathan * XXX between the various instances of an algorithm (so we can
1023 1.1 jonathan * XXX locate the correct crypto context).
1024 1.1 jonathan */
1025 1.1 jonathan for (sw = swcr_sessions[lid];
1026 1.1 jonathan sw && sw->sw_alg != crd->crd_alg;
1027 1.1 jonathan sw = sw->sw_next)
1028 1.1 jonathan ;
1029 1.1 jonathan
1030 1.1 jonathan /* No such context ? */
1031 1.1 jonathan if (sw == NULL) {
1032 1.1 jonathan crp->crp_etype = EINVAL;
1033 1.1 jonathan goto done;
1034 1.1 jonathan }
1035 1.1 jonathan
1036 1.1 jonathan switch (sw->sw_alg) {
1037 1.1 jonathan case CRYPTO_DES_CBC:
1038 1.1 jonathan case CRYPTO_3DES_CBC:
1039 1.1 jonathan case CRYPTO_BLF_CBC:
1040 1.1 jonathan case CRYPTO_CAST_CBC:
1041 1.1 jonathan case CRYPTO_SKIPJACK_CBC:
1042 1.1 jonathan case CRYPTO_RIJNDAEL128_CBC:
1043 1.30 drochner case CRYPTO_CAMELLIA_CBC:
1044 1.33 drochner case CRYPTO_AES_CTR:
1045 1.1 jonathan if ((crp->crp_etype = swcr_encdec(crd, sw,
1046 1.1 jonathan crp->crp_buf, type)) != 0)
1047 1.1 jonathan goto done;
1048 1.1 jonathan break;
1049 1.1 jonathan case CRYPTO_NULL_CBC:
1050 1.1 jonathan crp->crp_etype = 0;
1051 1.1 jonathan break;
1052 1.1 jonathan case CRYPTO_MD5_HMAC:
1053 1.19 tls case CRYPTO_MD5_HMAC_96:
1054 1.1 jonathan case CRYPTO_SHA1_HMAC:
1055 1.19 tls case CRYPTO_SHA1_HMAC_96:
1056 1.29 drochner case CRYPTO_SHA2_256_HMAC:
1057 1.29 drochner case CRYPTO_SHA2_384_HMAC:
1058 1.29 drochner case CRYPTO_SHA2_512_HMAC:
1059 1.1 jonathan case CRYPTO_RIPEMD160_HMAC:
1060 1.19 tls case CRYPTO_RIPEMD160_HMAC_96:
1061 1.1 jonathan case CRYPTO_NULL_HMAC:
1062 1.1 jonathan case CRYPTO_MD5_KPDK:
1063 1.1 jonathan case CRYPTO_SHA1_KPDK:
1064 1.1 jonathan case CRYPTO_MD5:
1065 1.1 jonathan case CRYPTO_SHA1:
1066 1.36 drochner case CRYPTO_AES_XCBC_MAC_96:
1067 1.1 jonathan if ((crp->crp_etype = swcr_authcompute(crp, crd, sw,
1068 1.1 jonathan crp->crp_buf, type)) != 0)
1069 1.1 jonathan goto done;
1070 1.1 jonathan break;
1071 1.1 jonathan
1072 1.1 jonathan case CRYPTO_DEFLATE_COMP:
1073 1.28 drochner case CRYPTO_DEFLATE_COMP_NOGROW:
1074 1.24 darran case CRYPTO_GZIP_COMP:
1075 1.24 darran DPRINTF(("swcr_process: compdec for %d\n", sw->sw_alg));
1076 1.9 perry if ((crp->crp_etype = swcr_compdec(crd, sw,
1077 1.27 drochner crp->crp_buf, type, &crp->crp_olen)) != 0)
1078 1.1 jonathan goto done;
1079 1.1 jonathan break;
1080 1.1 jonathan
1081 1.1 jonathan default:
1082 1.1 jonathan /* Unknown/unsupported algorithm */
1083 1.1 jonathan crp->crp_etype = EINVAL;
1084 1.1 jonathan goto done;
1085 1.1 jonathan }
1086 1.1 jonathan }
1087 1.1 jonathan
1088 1.1 jonathan done:
1089 1.26 jakllsch DPRINTF(("request %p done\n", crp));
1090 1.1 jonathan crypto_done(crp);
1091 1.1 jonathan return 0;
1092 1.1 jonathan }
1093 1.1 jonathan
1094 1.10 thorpej static void
1095 1.1 jonathan swcr_init(void)
1096 1.1 jonathan {
1097 1.1 jonathan swcr_id = crypto_get_driverid(CRYPTOCAP_F_SOFTWARE);
1098 1.1 jonathan if (swcr_id < 0) {
1099 1.1 jonathan /* This should never happen */
1100 1.1 jonathan panic("Software crypto device cannot initialize!");
1101 1.1 jonathan }
1102 1.1 jonathan
1103 1.1 jonathan crypto_register(swcr_id, CRYPTO_DES_CBC,
1104 1.1 jonathan 0, 0, swcr_newsession, swcr_freesession, swcr_process, NULL);
1105 1.1 jonathan #define REGISTER(alg) \
1106 1.1 jonathan crypto_register(swcr_id, alg, 0, 0, NULL, NULL, NULL, NULL)
1107 1.1 jonathan
1108 1.1 jonathan REGISTER(CRYPTO_3DES_CBC);
1109 1.1 jonathan REGISTER(CRYPTO_BLF_CBC);
1110 1.1 jonathan REGISTER(CRYPTO_CAST_CBC);
1111 1.1 jonathan REGISTER(CRYPTO_SKIPJACK_CBC);
1112 1.30 drochner REGISTER(CRYPTO_CAMELLIA_CBC);
1113 1.33 drochner REGISTER(CRYPTO_AES_CTR);
1114 1.1 jonathan REGISTER(CRYPTO_NULL_CBC);
1115 1.1 jonathan REGISTER(CRYPTO_MD5_HMAC);
1116 1.19 tls REGISTER(CRYPTO_MD5_HMAC_96);
1117 1.1 jonathan REGISTER(CRYPTO_SHA1_HMAC);
1118 1.19 tls REGISTER(CRYPTO_SHA1_HMAC_96);
1119 1.29 drochner REGISTER(CRYPTO_SHA2_256_HMAC);
1120 1.29 drochner REGISTER(CRYPTO_SHA2_384_HMAC);
1121 1.29 drochner REGISTER(CRYPTO_SHA2_512_HMAC);
1122 1.1 jonathan REGISTER(CRYPTO_RIPEMD160_HMAC);
1123 1.19 tls REGISTER(CRYPTO_RIPEMD160_HMAC_96);
1124 1.1 jonathan REGISTER(CRYPTO_NULL_HMAC);
1125 1.1 jonathan REGISTER(CRYPTO_MD5_KPDK);
1126 1.1 jonathan REGISTER(CRYPTO_SHA1_KPDK);
1127 1.1 jonathan REGISTER(CRYPTO_MD5);
1128 1.1 jonathan REGISTER(CRYPTO_SHA1);
1129 1.36 drochner REGISTER(CRYPTO_AES_XCBC_MAC_96);
1130 1.1 jonathan REGISTER(CRYPTO_RIJNDAEL128_CBC);
1131 1.1 jonathan REGISTER(CRYPTO_DEFLATE_COMP);
1132 1.28 drochner REGISTER(CRYPTO_DEFLATE_COMP_NOGROW);
1133 1.24 darran REGISTER(CRYPTO_GZIP_COMP);
1134 1.1 jonathan #undef REGISTER
1135 1.1 jonathan }
1136 1.1 jonathan
1137 1.10 thorpej
1138 1.10 thorpej /*
1139 1.10 thorpej * Pseudo-device init routine for software crypto.
1140 1.10 thorpej */
1141 1.11 thorpej void swcryptoattach(int);
1142 1.10 thorpej
1143 1.10 thorpej void
1144 1.15 christos swcryptoattach(int num)
1145 1.10 thorpej {
1146 1.10 thorpej
1147 1.10 thorpej swcr_init();
1148 1.10 thorpej }
1149