1 /* $FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $ */
2 /* $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $ */
5 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
7 * This code was written by Angelos D. Keromytis in Athens, Greece, in
8 * February 2000. Network Security Technologies Inc. (NSTI) kindly
9 * supported the development of this code.
11 * Copyright (c) 2000, 2001 Angelos D. Keromytis
13 * Permission to use, copy, and modify this software with or without fee
14 * is hereby granted, provided that this entire notice is included in
15 * all source code copies of any software which is or includes a copy or
16 * modification of this software.
18 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
20 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
21 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
25 #include <sys/param.h>
26 #include <sys/systm.h>
27 #include <sys/malloc.h>
29 #include <sys/sysctl.h>
30 #include <sys/errno.h>
31 #include <sys/random.h>
32 #include <sys/kernel.h>
35 #include <crypto/blowfish/blowfish.h>
36 #include <crypto/cast128/cast128.h>
37 #include <crypto/sha1.h>
38 #include <opencrypto/rmd160.h>
39 #include <opencrypto/skipjack.h>
42 #include <opencrypto/cryptodev.h>
43 #include <opencrypto/cryptosoft.h>
44 #include <opencrypto/xform.h>
46 u_int8_t hmac_ipad_buffer[64] = {
47 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
48 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
49 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
50 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
51 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
52 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
53 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
54 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
57 u_int8_t hmac_opad_buffer[64] = {
58 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
59 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
60 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
61 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
62 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
63 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
64 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C,
65 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C, 0x5C
69 struct swcr_data **swcr_sessions = NULL;
70 u_int32_t swcr_sesnum = 0;
73 #define COPYBACK(x, a, b, c, d) \
74 (x) == CRYPTO_BUF_MBUF ? m_copyback((struct mbuf *)a,b,c,d) \
75 : cuio_copyback((struct uio *)a,b,c,d)
76 #define COPYDATA(x, a, b, c, d) \
77 (x) == CRYPTO_BUF_MBUF ? m_copydata((struct mbuf *)a,b,c,d) \
78 : cuio_copydata((struct uio *)a,b,c,d)
80 static int swcr_encdec(struct cryptodesc *, struct swcr_data *, caddr_t, int);
81 static int swcr_authcompute(struct cryptop *crp, struct cryptodesc *crd,
82 struct swcr_data *sw, caddr_t buf, int outtype);
83 static int swcr_compdec(struct cryptodesc *, struct swcr_data *, caddr_t, int);
84 static int swcr_process(void *, struct cryptop *, int);
85 static int swcr_newsession(void *, u_int32_t *, struct cryptoini *);
86 static int swcr_freesession(void *, u_int64_t);
89 * NB: These came over from openbsd and are kept private
90 * to the crypto code for now.
92 extern int m_apply(struct mbuf *m, int off, int len,
93 int (*f)(caddr_t, caddr_t, unsigned int), caddr_t fstate);
96 * Apply a symmetric encryption/decryption algorithm.
99 swcr_encdec(struct cryptodesc *crd, struct swcr_data *sw, caddr_t buf,
102 unsigned char iv[EALG_MAX_BLOCK_LEN], blk[EALG_MAX_BLOCK_LEN], *idat;
103 unsigned char *ivp, piv[EALG_MAX_BLOCK_LEN];
104 struct enc_xform *exf;
108 blks = exf->blocksize;
110 /* Check for non-padded data */
111 if (crd->crd_len % blks)
114 /* Initialize the IV */
115 if (crd->crd_flags & CRD_F_ENCRYPT) {
116 /* IV explicitly provided ? */
117 if (crd->crd_flags & CRD_F_IV_EXPLICIT)
118 bcopy(crd->crd_iv, iv, blks);
122 i + sizeof (u_int32_t) < EALG_MAX_BLOCK_LEN;
123 i += sizeof (u_int32_t)) {
124 u_int32_t temp = arc4random();
126 bcopy(&temp, iv + i, sizeof(u_int32_t));
129 * What if the block size is not a multiple
130 * of sizeof (u_int32_t), which is the size of
131 * what arc4random() returns ?
133 if (EALG_MAX_BLOCK_LEN % sizeof (u_int32_t) != 0) {
134 u_int32_t temp = arc4random();
136 bcopy (&temp, iv + i,
137 EALG_MAX_BLOCK_LEN - i);
141 /* Do we need to write the IV */
142 if (!(crd->crd_flags & CRD_F_IV_PRESENT)) {
143 COPYBACK(outtype, buf, crd->crd_inject, blks, iv);
146 } else { /* Decryption */
147 /* IV explicitly provided ? */
148 if (crd->crd_flags & CRD_F_IV_EXPLICIT)
149 bcopy(crd->crd_iv, iv, blks);
152 COPYDATA(outtype, buf, crd->crd_inject, blks, iv);
158 if (outtype == CRYPTO_BUF_CONTIG) {
159 if (crd->crd_flags & CRD_F_ENCRYPT) {
160 for (i = crd->crd_skip;
161 i < crd->crd_skip + crd->crd_len; i += blks) {
162 /* XOR with the IV/previous block, as appropriate. */
163 if (i == crd->crd_skip)
164 for (k = 0; k < blks; k++)
165 buf[i + k] ^= ivp[k];
167 for (k = 0; k < blks; k++)
168 buf[i + k] ^= buf[i + k - blks];
169 exf->encrypt(sw->sw_kschedule, buf + i);
171 } else { /* Decrypt */
173 * Start at the end, so we don't need to keep the encrypted
174 * block as the IV for the next block.
176 for (i = crd->crd_skip + crd->crd_len - blks;
177 i >= crd->crd_skip; i -= blks) {
178 exf->decrypt(sw->sw_kschedule, buf + i);
180 /* XOR with the IV/previous block, as appropriate */
181 if (i == crd->crd_skip)
182 for (k = 0; k < blks; k++)
183 buf[i + k] ^= ivp[k];
185 for (k = 0; k < blks; k++)
186 buf[i + k] ^= buf[i + k - blks];
191 } else if (outtype == CRYPTO_BUF_MBUF) {
192 struct mbuf *m = (struct mbuf *) buf;
194 /* Find beginning of data */
195 m = m_getptr(m, crd->crd_skip, &k);
203 * If there's insufficient data at the end of
204 * an mbuf, we have to do some copying.
206 if (m->m_len < k + blks && m->m_len != k) {
207 m_copydata(m, k, blks, blk);
209 /* Actual encryption/decryption */
210 if (crd->crd_flags & CRD_F_ENCRYPT) {
211 /* XOR with previous block */
212 for (j = 0; j < blks; j++)
215 exf->encrypt(sw->sw_kschedule, blk);
218 * Keep encrypted block for XOR'ing
221 bcopy(blk, iv, blks);
223 } else { /* decrypt */
225 * Keep encrypted block for XOR'ing
229 bcopy(blk, piv, blks);
231 bcopy(blk, iv, blks);
233 exf->decrypt(sw->sw_kschedule, blk);
235 /* XOR with previous block */
236 for (j = 0; j < blks; j++)
240 bcopy(piv, iv, blks);
245 /* Copy back decrypted block */
246 m_copyback(m, k, blks, blk);
248 /* Advance pointer */
249 m = m_getptr(m, k + blks, &k);
255 /* Could be done... */
260 /* Skip possibly empty mbufs */
262 for (m = m->m_next; m && m->m_len == 0;
273 * Warning: idat may point to garbage here, but
274 * we only use it in the while() loop, only if
275 * there are indeed enough data.
277 idat = mtod(m, unsigned char *) + k;
279 while (m->m_len >= k + blks && i > 0) {
280 if (crd->crd_flags & CRD_F_ENCRYPT) {
281 /* XOR with previous block/IV */
282 for (j = 0; j < blks; j++)
285 exf->encrypt(sw->sw_kschedule, idat);
287 } else { /* decrypt */
289 * Keep encrypted block to be used
290 * in next block's processing.
293 bcopy(idat, piv, blks);
295 bcopy(idat, iv, blks);
297 exf->decrypt(sw->sw_kschedule, idat);
299 /* XOR with previous block/IV */
300 for (j = 0; j < blks; j++)
304 bcopy(piv, iv, blks);
315 return 0; /* Done with mbuf encryption/decryption */
316 } else if (outtype == CRYPTO_BUF_IOV) {
317 struct uio *uio = (struct uio *) buf;
320 /* Find beginning of data */
321 iov = cuio_getptr(uio, crd->crd_skip, &k);
329 * If there's insufficient data at the end of
330 * an iovec, we have to do some copying.
332 if (iov->iov_len < k + blks && iov->iov_len != k) {
333 cuio_copydata(uio, k, blks, blk);
335 /* Actual encryption/decryption */
336 if (crd->crd_flags & CRD_F_ENCRYPT) {
337 /* XOR with previous block */
338 for (j = 0; j < blks; j++)
341 exf->encrypt(sw->sw_kschedule, blk);
344 * Keep encrypted block for XOR'ing
347 bcopy(blk, iv, blks);
349 } else { /* decrypt */
351 * Keep encrypted block for XOR'ing
355 bcopy(blk, piv, blks);
357 bcopy(blk, iv, blks);
359 exf->decrypt(sw->sw_kschedule, blk);
361 /* XOR with previous block */
362 for (j = 0; j < blks; j++)
366 bcopy(piv, iv, blks);
371 /* Copy back decrypted block */
372 cuio_copyback(uio, k, blks, blk);
374 /* Advance pointer */
375 iov = cuio_getptr(uio, k + blks, &k);
381 /* Could be done... */
387 * Warning: idat may point to garbage here, but
388 * we only use it in the while() loop, only if
389 * there are indeed enough data.
391 idat = (char *)iov->iov_base + k;
393 while (iov->iov_len >= k + blks && i > 0) {
394 if (crd->crd_flags & CRD_F_ENCRYPT) {
395 /* XOR with previous block/IV */
396 for (j = 0; j < blks; j++)
399 exf->encrypt(sw->sw_kschedule, idat);
401 } else { /* decrypt */
403 * Keep encrypted block to be used
404 * in next block's processing.
407 bcopy(idat, piv, blks);
409 bcopy(idat, iv, blks);
411 exf->decrypt(sw->sw_kschedule, idat);
413 /* XOR with previous block/IV */
414 for (j = 0; j < blks; j++)
418 bcopy(piv, iv, blks);
429 return 0; /* Done with mbuf encryption/decryption */
437 * Compute keyed-hash authenticator.
440 swcr_authcompute(struct cryptop *crp, struct cryptodesc *crd,
441 struct swcr_data *sw, caddr_t buf, int outtype)
443 unsigned char aalg[AALG_MAX_RESULT_LEN];
444 struct auth_hash *axf;
448 if (sw->sw_ictx == 0)
453 bcopy(sw->sw_ictx, &ctx, axf->ctxsize);
456 case CRYPTO_BUF_CONTIG:
457 axf->Update(&ctx, buf + crd->crd_skip, crd->crd_len);
459 case CRYPTO_BUF_MBUF:
460 err = m_apply((struct mbuf *) buf, crd->crd_skip, crd->crd_len,
461 (int (*)(caddr_t, caddr_t, unsigned int)) axf->Update,
471 switch (sw->sw_alg) {
472 case CRYPTO_MD5_HMAC:
473 case CRYPTO_SHA1_HMAC:
474 case CRYPTO_SHA2_HMAC:
475 case CRYPTO_RIPEMD160_HMAC:
476 if (sw->sw_octx == NULL)
479 axf->Final(aalg, &ctx);
480 bcopy(sw->sw_octx, &ctx, axf->ctxsize);
481 axf->Update(&ctx, aalg, axf->hashsize);
482 axf->Final(aalg, &ctx);
485 case CRYPTO_MD5_KPDK:
486 case CRYPTO_SHA1_KPDK:
487 if (sw->sw_octx == NULL)
490 axf->Update(&ctx, sw->sw_octx, sw->sw_klen);
491 axf->Final(aalg, &ctx);
494 case CRYPTO_NULL_HMAC:
495 axf->Final(aalg, &ctx);
499 /* Inject the authentication data */
500 if (outtype == CRYPTO_BUF_CONTIG)
501 bcopy(aalg, buf + crd->crd_inject, axf->authsize);
503 m_copyback((struct mbuf *) buf, crd->crd_inject,
504 axf->authsize, aalg);
509 * Apply a compression/decompression algorithm
512 swcr_compdec(struct cryptodesc *crd, struct swcr_data *sw,
513 caddr_t buf, int outtype)
515 u_int8_t *data, *out;
516 struct comp_algo *cxf;
522 /* We must handle the whole buffer of data in one time
523 * then if there is not all the data in the mbuf, we must
527 MALLOC(data, u_int8_t *, crd->crd_len, M_CRYPTO_DATA, M_NOWAIT);
530 COPYDATA(outtype, buf, crd->crd_skip, crd->crd_len, data);
532 if (crd->crd_flags & CRD_F_COMP)
533 result = cxf->compress(data, crd->crd_len, &out);
535 result = cxf->decompress(data, crd->crd_len, &out);
537 FREE(data, M_CRYPTO_DATA);
541 /* Copy back the (de)compressed data. m_copyback is
542 * extending the mbuf as necessary.
544 sw->sw_size = result;
545 /* Check the compressed size when doing compression */
546 if (crd->crd_flags & CRD_F_COMP) {
547 if (result > crd->crd_len) {
548 /* Compression was useless, we lost time */
549 FREE(out, M_CRYPTO_DATA);
554 COPYBACK(outtype, buf, crd->crd_skip, result, out);
555 if (result < crd->crd_len) {
556 adj = result - crd->crd_len;
557 if (outtype == CRYPTO_BUF_MBUF) {
558 adj = result - crd->crd_len;
559 m_adj((struct mbuf *)buf, adj);
561 struct uio *uio = (struct uio *)buf;
564 adj = crd->crd_len - result;
565 ind = uio->uio_iovcnt - 1;
567 while (adj > 0 && ind >= 0) {
568 if (adj < uio->uio_iov[ind].iov_len) {
569 uio->uio_iov[ind].iov_len -= adj;
573 adj -= uio->uio_iov[ind].iov_len;
574 uio->uio_iov[ind].iov_len = 0;
580 FREE(out, M_CRYPTO_DATA);
585 * Generate a new software session.
588 swcr_newsession(void *arg, u_int32_t *sid, struct cryptoini *cri)
590 struct swcr_data **swd;
591 struct auth_hash *axf;
592 struct enc_xform *txf;
593 struct comp_algo *cxf;
597 if (sid == NULL || cri == NULL)
601 for (i = 1; i < swcr_sesnum; i++)
602 if (swcr_sessions[i] == NULL)
605 i = 1; /* NB: to silence compiler warning */
607 if (swcr_sessions == NULL || i == swcr_sesnum) {
608 if (swcr_sessions == NULL) {
609 i = 1; /* We leave swcr_sessions[0] empty */
610 swcr_sesnum = CRYPTO_SW_SESSIONS;
614 swd = malloc(swcr_sesnum * sizeof(struct swcr_data *),
615 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
617 /* Reset session number */
618 if (swcr_sesnum == CRYPTO_SW_SESSIONS)
625 /* Copy existing sessions */
627 bcopy(swcr_sessions, swd,
628 (swcr_sesnum / 2) * sizeof(struct swcr_data *));
629 free(swcr_sessions, M_CRYPTO_DATA);
635 swd = &swcr_sessions[i];
639 MALLOC(*swd, struct swcr_data *, sizeof(struct swcr_data),
640 M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
642 swcr_freesession(NULL, i);
646 switch (cri->cri_alg) {
648 txf = &enc_xform_des;
650 case CRYPTO_3DES_CBC:
651 txf = &enc_xform_3des;
654 txf = &enc_xform_blf;
656 case CRYPTO_CAST_CBC:
657 txf = &enc_xform_cast5;
659 case CRYPTO_SKIPJACK_CBC:
660 txf = &enc_xform_skipjack;
662 case CRYPTO_RIJNDAEL128_CBC:
663 txf = &enc_xform_rijndael128;
665 case CRYPTO_NULL_CBC:
666 txf = &enc_xform_null;
669 error = txf->setkey(&((*swd)->sw_kschedule),
670 cri->cri_key, cri->cri_klen / 8);
672 swcr_freesession(NULL, i);
675 (*swd)->sw_exf = txf;
678 case CRYPTO_MD5_HMAC:
679 axf = &auth_hash_hmac_md5_96;
681 case CRYPTO_SHA1_HMAC:
682 axf = &auth_hash_hmac_sha1_96;
684 case CRYPTO_SHA2_HMAC:
685 if (cri->cri_klen == 256)
686 axf = &auth_hash_hmac_sha2_256;
687 else if (cri->cri_klen == 384)
688 axf = &auth_hash_hmac_sha2_384;
689 else if (cri->cri_klen == 512)
690 axf = &auth_hash_hmac_sha2_512;
692 swcr_freesession(NULL, i);
696 case CRYPTO_NULL_HMAC:
697 axf = &auth_hash_null;
699 case CRYPTO_RIPEMD160_HMAC:
700 axf = &auth_hash_hmac_ripemd_160_96;
702 (*swd)->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA,
704 if ((*swd)->sw_ictx == NULL) {
705 swcr_freesession(NULL, i);
709 (*swd)->sw_octx = malloc(axf->ctxsize, M_CRYPTO_DATA,
711 if ((*swd)->sw_octx == NULL) {
712 swcr_freesession(NULL, i);
716 for (k = 0; k < cri->cri_klen / 8; k++)
717 cri->cri_key[k] ^= HMAC_IPAD_VAL;
719 axf->Init((*swd)->sw_ictx);
720 axf->Update((*swd)->sw_ictx, cri->cri_key,
722 axf->Update((*swd)->sw_ictx, hmac_ipad_buffer,
723 HMAC_BLOCK_LEN - (cri->cri_klen / 8));
725 for (k = 0; k < cri->cri_klen / 8; k++)
726 cri->cri_key[k] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
728 axf->Init((*swd)->sw_octx);
729 axf->Update((*swd)->sw_octx, cri->cri_key,
731 axf->Update((*swd)->sw_octx, hmac_opad_buffer,
732 HMAC_BLOCK_LEN - (cri->cri_klen / 8));
734 for (k = 0; k < cri->cri_klen / 8; k++)
735 cri->cri_key[k] ^= HMAC_OPAD_VAL;
736 (*swd)->sw_axf = axf;
739 case CRYPTO_MD5_KPDK:
740 axf = &auth_hash_key_md5;
743 case CRYPTO_SHA1_KPDK:
744 axf = &auth_hash_key_sha1;
746 (*swd)->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA,
748 if ((*swd)->sw_ictx == NULL) {
749 swcr_freesession(NULL, i);
753 /* Store the key so we can "append" it to the payload */
754 (*swd)->sw_octx = malloc(cri->cri_klen / 8, M_CRYPTO_DATA,
756 if ((*swd)->sw_octx == NULL) {
757 swcr_freesession(NULL, i);
761 (*swd)->sw_klen = cri->cri_klen / 8;
762 bcopy(cri->cri_key, (*swd)->sw_octx, cri->cri_klen / 8);
763 axf->Init((*swd)->sw_ictx);
764 axf->Update((*swd)->sw_ictx, cri->cri_key,
766 axf->Final(NULL, (*swd)->sw_ictx);
767 (*swd)->sw_axf = axf;
771 axf = &auth_hash_md5;
775 axf = &auth_hash_sha1;
777 (*swd)->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA,
779 if ((*swd)->sw_ictx == NULL) {
780 swcr_freesession(NULL, i);
784 axf->Init((*swd)->sw_ictx);
785 (*swd)->sw_axf = axf;
788 case CRYPTO_DEFLATE_COMP:
789 cxf = &comp_algo_deflate;
790 (*swd)->sw_cxf = cxf;
793 swcr_freesession(NULL, i);
797 (*swd)->sw_alg = cri->cri_alg;
799 swd = &((*swd)->sw_next);
808 swcr_freesession(void *arg, u_int64_t tid)
810 struct swcr_data *swd;
811 struct enc_xform *txf;
812 struct auth_hash *axf;
813 struct comp_algo *cxf;
814 u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
816 if (sid > swcr_sesnum || swcr_sessions == NULL ||
817 swcr_sessions[sid] == NULL)
820 /* Silently accept and return */
824 while ((swd = swcr_sessions[sid]) != NULL) {
825 swcr_sessions[sid] = swd->sw_next;
827 switch (swd->sw_alg) {
829 case CRYPTO_3DES_CBC:
831 case CRYPTO_CAST_CBC:
832 case CRYPTO_SKIPJACK_CBC:
833 case CRYPTO_RIJNDAEL128_CBC:
834 case CRYPTO_NULL_CBC:
837 if (swd->sw_kschedule)
838 txf->zerokey(&(swd->sw_kschedule));
841 case CRYPTO_MD5_HMAC:
842 case CRYPTO_SHA1_HMAC:
843 case CRYPTO_SHA2_HMAC:
844 case CRYPTO_RIPEMD160_HMAC:
845 case CRYPTO_NULL_HMAC:
849 bzero(swd->sw_ictx, axf->ctxsize);
850 free(swd->sw_ictx, M_CRYPTO_DATA);
853 bzero(swd->sw_octx, axf->ctxsize);
854 free(swd->sw_octx, M_CRYPTO_DATA);
858 case CRYPTO_MD5_KPDK:
859 case CRYPTO_SHA1_KPDK:
863 bzero(swd->sw_ictx, axf->ctxsize);
864 free(swd->sw_ictx, M_CRYPTO_DATA);
867 bzero(swd->sw_octx, swd->sw_klen);
868 free(swd->sw_octx, M_CRYPTO_DATA);
877 free(swd->sw_ictx, M_CRYPTO_DATA);
880 case CRYPTO_DEFLATE_COMP:
885 FREE(swd, M_CRYPTO_DATA);
891 * Process a software request.
894 swcr_process(void *arg, struct cryptop *crp, int hint)
896 struct cryptodesc *crd;
897 struct swcr_data *sw;
905 if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
906 crp->crp_etype = EINVAL;
910 lid = crp->crp_sid & 0xffffffff;
911 if (lid >= swcr_sesnum || lid == 0 || swcr_sessions[lid] == NULL) {
912 crp->crp_etype = ENOENT;
916 if (crp->crp_flags & CRYPTO_F_IMBUF) {
917 type = CRYPTO_BUF_MBUF;
918 } else if (crp->crp_flags & CRYPTO_F_IOV) {
919 type = CRYPTO_BUF_IOV;
921 type = CRYPTO_BUF_CONTIG;
924 /* Go through crypto descriptors, processing as we go */
925 for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
927 * Find the crypto context.
929 * XXX Note that the logic here prevents us from having
930 * XXX the same algorithm multiple times in a session
931 * XXX (or rather, we can but it won't give us the right
932 * XXX results). To do that, we'd need some way of differentiating
933 * XXX between the various instances of an algorithm (so we can
934 * XXX locate the correct crypto context).
936 for (sw = swcr_sessions[lid];
937 sw && sw->sw_alg != crd->crd_alg;
941 /* No such context ? */
943 crp->crp_etype = EINVAL;
946 switch (sw->sw_alg) {
948 case CRYPTO_3DES_CBC:
950 case CRYPTO_CAST_CBC:
951 case CRYPTO_SKIPJACK_CBC:
952 case CRYPTO_RIJNDAEL128_CBC:
953 if ((crp->crp_etype = swcr_encdec(crd, sw,
954 crp->crp_buf, type)) != 0)
957 case CRYPTO_NULL_CBC:
960 case CRYPTO_MD5_HMAC:
961 case CRYPTO_SHA1_HMAC:
962 case CRYPTO_SHA2_HMAC:
963 case CRYPTO_RIPEMD160_HMAC:
964 case CRYPTO_NULL_HMAC:
965 case CRYPTO_MD5_KPDK:
966 case CRYPTO_SHA1_KPDK:
969 if ((crp->crp_etype = swcr_authcompute(crp, crd, sw,
970 crp->crp_buf, type)) != 0)
974 case CRYPTO_DEFLATE_COMP:
975 if ((crp->crp_etype = swcr_compdec(crd, sw,
976 crp->crp_buf, type)) != 0)
979 crp->crp_olen = (int)sw->sw_size;
983 /* Unknown/unsupported algorithm */
984 crp->crp_etype = EINVAL;
995 * Initialize the driver, called from the kernel main().
1000 swcr_id = crypto_get_driverid(CRYPTOCAP_F_SOFTWARE);
1002 panic("Software crypto device cannot initialize!");
1003 crypto_register(swcr_id, CRYPTO_DES_CBC,
1004 0, 0, swcr_newsession, swcr_freesession, swcr_process, NULL);
1005 #define REGISTER(alg) \
1006 crypto_register(swcr_id, alg, 0,0,NULL,NULL,NULL,NULL)
1007 REGISTER(CRYPTO_3DES_CBC);
1008 REGISTER(CRYPTO_BLF_CBC);
1009 REGISTER(CRYPTO_CAST_CBC);
1010 REGISTER(CRYPTO_SKIPJACK_CBC);
1011 REGISTER(CRYPTO_NULL_CBC);
1012 REGISTER(CRYPTO_MD5_HMAC);
1013 REGISTER(CRYPTO_SHA1_HMAC);
1014 REGISTER(CRYPTO_SHA2_HMAC);
1015 REGISTER(CRYPTO_RIPEMD160_HMAC);
1016 REGISTER(CRYPTO_NULL_HMAC);
1017 REGISTER(CRYPTO_MD5_KPDK);
1018 REGISTER(CRYPTO_SHA1_KPDK);
1019 REGISTER(CRYPTO_MD5);
1020 REGISTER(CRYPTO_SHA1);
1021 REGISTER(CRYPTO_RIJNDAEL128_CBC);
1022 REGISTER(CRYPTO_DEFLATE_COMP);
1025 SYSINIT(cryptosoft_init, SI_SUB_PSEUDO, SI_ORDER_ANY, swcr_init, NULL)