2 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
3 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
5 * This code was written by Angelos D. Keromytis in Athens, Greece, in
6 * February 2000. Network Security Technologies Inc. (NSTI) kindly
7 * supported the development of this code.
9 * Copyright (c) 2000, 2001 Angelos D. Keromytis
11 * SMP modifications by Matthew Dillon for the DragonFlyBSD Project
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
24 * $FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.23 2009/02/05 17:43:12 imp Exp $
25 * $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/malloc.h>
32 #include <sys/module.h>
33 #include <sys/sysctl.h>
34 #include <sys/errno.h>
35 #include <sys/random.h>
36 #include <sys/kernel.h>
38 #include <sys/spinlock2.h>
40 #include <crypto/blowfish/blowfish.h>
41 #include <crypto/sha1.h>
42 #include <opencrypto/rmd160.h>
43 #include <opencrypto/cast.h>
44 #include <opencrypto/skipjack.h>
47 #include <opencrypto/cryptodev.h>
48 #include <opencrypto/cryptosoft.h>
49 #include <opencrypto/xform.h>
53 #include "cryptodev_if.h"
55 static int32_t swcr_id;
56 static struct swcr_data **swcr_sessions = NULL;
57 static u_int32_t swcr_sesnum;
58 static u_int32_t swcr_minsesnum = 1;
60 static struct spinlock swcr_spin = SPINLOCK_INITIALIZER(swcr_spin);
62 u_int8_t hmac_ipad_buffer[HMAC_MAX_BLOCK_LEN];
63 u_int8_t hmac_opad_buffer[HMAC_MAX_BLOCK_LEN];
65 static int swcr_encdec(struct cryptodesc *, struct swcr_data *, caddr_t, int);
66 static int swcr_authcompute(struct cryptodesc *, struct swcr_data *, caddr_t, int);
67 static int swcr_compdec(struct cryptodesc *, struct swcr_data *, caddr_t, int);
68 static int swcr_freesession(device_t dev, u_int64_t tid);
69 static int swcr_freesession_slot(struct swcr_data **swdp, u_int32_t sid);
72 * Apply a symmetric encryption/decryption algorithm.
75 swcr_encdec(struct cryptodesc *crd, struct swcr_data *sw, caddr_t buf,
78 unsigned char iv[EALG_MAX_BLOCK_LEN], blk[EALG_MAX_BLOCK_LEN], *idat;
79 unsigned char *ivp, piv[EALG_MAX_BLOCK_LEN];
82 struct enc_xform *exf;
83 int i, k, j, blks, ivlen;
85 int explicit_kschedule;
88 blks = exf->blocksize;
91 /* Check for non-padded data */
92 if (crd->crd_len % blks)
95 /* Initialize the IV */
96 if (crd->crd_flags & CRD_F_ENCRYPT) {
97 /* IV explicitly provided ? */
98 if (crd->crd_flags & CRD_F_IV_EXPLICIT)
99 bcopy(crd->crd_iv, iv, ivlen);
101 karc4rand(iv, ivlen);
103 /* Do we need to write the IV */
104 if (!(crd->crd_flags & CRD_F_IV_PRESENT))
105 crypto_copyback(flags, buf, crd->crd_inject, ivlen, iv);
107 } else { /* Decryption */
108 /* IV explicitly provided ? */
109 if (crd->crd_flags & CRD_F_IV_EXPLICIT)
110 bcopy(crd->crd_iv, iv, ivlen);
113 crypto_copydata(flags, buf, crd->crd_inject, ivlen, iv);
120 * The semantics are seriously broken because the session key
121 * storage was never designed for concurrent ops.
123 if (crd->crd_flags & CRD_F_KEY_EXPLICIT) {
125 explicit_kschedule = 1;
126 error = exf->setkey(&kschedule,
127 crd->crd_key, crd->crd_klen / 8);
131 spin_lock(&swcr_spin);
132 kschedule = sw->sw_kschedule;
133 ++sw->sw_kschedule_refs;
134 spin_unlock(&swcr_spin);
135 explicit_kschedule = 0;
139 * xforms that provide a reinit method perform all IV
140 * handling themselves.
143 exf->reinit(kschedule, iv);
145 if (flags & CRYPTO_F_IMBUF) {
146 struct mbuf *m = (struct mbuf *) buf;
148 /* Find beginning of data */
149 m = m_getptr(m, crd->crd_skip, &k);
159 * If there's insufficient data at the end of
160 * an mbuf, we have to do some copying.
162 if (m->m_len < k + blks && m->m_len != k) {
163 m_copydata(m, k, blks, blk);
165 /* Actual encryption/decryption */
167 if (crd->crd_flags & CRD_F_ENCRYPT) {
168 exf->encrypt(kschedule,
171 exf->decrypt(kschedule,
174 } else if (crd->crd_flags & CRD_F_ENCRYPT) {
175 /* XOR with previous block */
176 for (j = 0; j < blks; j++)
179 exf->encrypt(kschedule, blk, iv);
182 * Keep encrypted block for XOR'ing
185 bcopy(blk, iv, blks);
187 } else { /* decrypt */
189 * Keep encrypted block for XOR'ing
193 bcopy(blk, piv, blks);
195 bcopy(blk, iv, blks);
197 exf->decrypt(kschedule, blk, iv);
199 /* XOR with previous block */
200 for (j = 0; j < blks; j++)
204 bcopy(piv, iv, blks);
209 /* Copy back decrypted block */
210 m_copyback(m, k, blks, blk);
212 /* Advance pointer */
213 m = m_getptr(m, k + blks, &k);
221 /* Could be done... */
226 /* Skip possibly empty mbufs */
228 for (m = m->m_next; m && m->m_len == 0;
241 * Warning: idat may point to garbage here, but
242 * we only use it in the while() loop, only if
243 * there are indeed enough data.
245 idat = mtod(m, unsigned char *) + k;
247 while (m->m_len >= k + blks && i > 0) {
249 if (crd->crd_flags & CRD_F_ENCRYPT) {
250 exf->encrypt(kschedule,
253 exf->decrypt(kschedule,
256 } else if (crd->crd_flags & CRD_F_ENCRYPT) {
257 /* XOR with previous block/IV */
258 for (j = 0; j < blks; j++)
261 exf->encrypt(kschedule, idat, iv);
263 } else { /* decrypt */
265 * Keep encrypted block to be used
266 * in next block's processing.
269 bcopy(idat, piv, blks);
271 bcopy(idat, iv, blks);
273 exf->decrypt(kschedule, idat, iv);
275 /* XOR with previous block/IV */
276 for (j = 0; j < blks; j++)
280 bcopy(piv, iv, blks);
290 error = 0; /* Done with mbuf encryption/decryption */
291 } else if (flags & CRYPTO_F_IOV) {
292 struct uio *uio = (struct uio *) buf;
295 /* Find beginning of data */
296 iov = cuio_getptr(uio, crd->crd_skip, &k);
306 * If there's insufficient data at the end of
307 * an iovec, we have to do some copying.
309 if (iov->iov_len < k + blks && iov->iov_len != k) {
310 cuio_copydata(uio, k, blks, blk);
312 /* Actual encryption/decryption */
314 if (crd->crd_flags & CRD_F_ENCRYPT) {
315 exf->encrypt(kschedule,
318 exf->decrypt(kschedule,
321 } else if (crd->crd_flags & CRD_F_ENCRYPT) {
322 /* XOR with previous block */
323 for (j = 0; j < blks; j++)
326 exf->encrypt(kschedule, blk, iv);
329 * Keep encrypted block for XOR'ing
332 bcopy(blk, iv, blks);
334 } else { /* decrypt */
336 * Keep encrypted block for XOR'ing
340 bcopy(blk, piv, blks);
342 bcopy(blk, iv, blks);
344 exf->decrypt(kschedule, blk, iv);
346 /* XOR with previous block */
347 for (j = 0; j < blks; j++)
351 bcopy(piv, iv, blks);
356 /* Copy back decrypted block */
357 cuio_copyback(uio, k, blks, blk);
359 /* Advance pointer */
360 iov = cuio_getptr(uio, k + blks, &k);
368 /* Could be done... */
374 * Warning: idat may point to garbage here, but
375 * we only use it in the while() loop, only if
376 * there are indeed enough data.
378 idat = (char *)iov->iov_base + k;
380 while (iov->iov_len >= k + blks && i > 0) {
382 if (crd->crd_flags & CRD_F_ENCRYPT) {
383 exf->encrypt(kschedule,
386 exf->decrypt(kschedule,
389 } else if (crd->crd_flags & CRD_F_ENCRYPT) {
390 /* XOR with previous block/IV */
391 for (j = 0; j < blks; j++)
394 exf->encrypt(kschedule, idat, iv);
396 } else { /* decrypt */
398 * Keep encrypted block to be used
399 * in next block's processing.
402 bcopy(idat, piv, blks);
404 bcopy(idat, iv, blks);
406 exf->decrypt(kschedule, idat, iv);
408 /* XOR with previous block/IV */
409 for (j = 0; j < blks; j++)
413 bcopy(piv, iv, blks);
422 if (k == iov->iov_len) {
427 error = 0; /* Done with iovec encryption/decryption */
433 for(i = crd->crd_skip;
434 i < crd->crd_skip + crd->crd_len; i += blks) {
435 if (crd->crd_flags & CRD_F_ENCRYPT) {
436 exf->encrypt(kschedule, buf + i, iv);
438 exf->decrypt(kschedule, buf + i, iv);
441 } else if (crd->crd_flags & CRD_F_ENCRYPT) {
442 for (i = crd->crd_skip;
443 i < crd->crd_skip + crd->crd_len; i += blks) {
444 /* XOR with the IV/previous block, as appropriate. */
445 if (i == crd->crd_skip)
446 for (k = 0; k < blks; k++)
447 buf[i + k] ^= ivp[k];
449 for (k = 0; k < blks; k++)
450 buf[i + k] ^= buf[i + k - blks];
451 exf->encrypt(kschedule, buf + i, iv);
453 } else { /* Decrypt */
455 * Start at the end, so we don't need to keep the
456 * encrypted block as the IV for the next block.
458 for (i = crd->crd_skip + crd->crd_len - blks;
459 i >= crd->crd_skip; i -= blks) {
460 exf->decrypt(kschedule, buf + i, iv);
462 /* XOR with the IV/previous block, as appropriate */
463 if (i == crd->crd_skip)
464 for (k = 0; k < blks; k++)
465 buf[i + k] ^= ivp[k];
467 for (k = 0; k < blks; k++)
468 buf[i + k] ^= buf[i + k - blks];
471 error = 0; /* Done w/contiguous buffer encrypt/decrypt */
475 * Cleanup - explicitly replace the session key if requested
476 * (horrible semantics for concurrent operation)
478 if (explicit_kschedule) {
479 spin_lock(&swcr_spin);
480 if (sw->sw_kschedule && sw->sw_kschedule_refs == 0) {
481 okschedule = sw->sw_kschedule;
482 sw->sw_kschedule = kschedule;
486 spin_unlock(&swcr_spin);
488 exf->zerokey(&okschedule);
490 spin_lock(&swcr_spin);
491 --sw->sw_kschedule_refs;
492 spin_unlock(&swcr_spin);
498 swcr_authprepare(struct auth_hash *axf, struct swcr_data *sw, u_char *key,
506 case CRYPTO_MD5_HMAC:
507 case CRYPTO_SHA1_HMAC:
508 case CRYPTO_SHA2_256_HMAC:
509 case CRYPTO_SHA2_384_HMAC:
510 case CRYPTO_SHA2_512_HMAC:
511 case CRYPTO_NULL_HMAC:
512 case CRYPTO_RIPEMD160_HMAC:
513 for (k = 0; k < klen; k++)
514 key[k] ^= HMAC_IPAD_VAL;
516 axf->Init(sw->sw_ictx);
517 axf->Update(sw->sw_ictx, key, klen);
518 axf->Update(sw->sw_ictx, hmac_ipad_buffer, axf->blocksize - klen);
520 for (k = 0; k < klen; k++)
521 key[k] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
523 axf->Init(sw->sw_octx);
524 axf->Update(sw->sw_octx, key, klen);
525 axf->Update(sw->sw_octx, hmac_opad_buffer, axf->blocksize - klen);
527 for (k = 0; k < klen; k++)
528 key[k] ^= HMAC_OPAD_VAL;
530 case CRYPTO_MD5_KPDK:
531 case CRYPTO_SHA1_KPDK:
533 /* We need a buffer that can hold an md5 and a sha1 result. */
534 u_char buf[SHA1_RESULTLEN];
537 bcopy(key, sw->sw_octx, klen);
538 axf->Init(sw->sw_ictx);
539 axf->Update(sw->sw_ictx, key, klen);
540 axf->Final(buf, sw->sw_ictx);
544 kprintf("%s: CRD_F_KEY_EXPLICIT flag given, but algorithm %d "
545 "doesn't use keys.\n", __func__, axf->type);
550 * Compute keyed-hash authenticator.
553 swcr_authcompute(struct cryptodesc *crd, struct swcr_data *sw, caddr_t buf,
556 unsigned char aalg[HASH_MAX_LEN];
557 struct auth_hash *axf;
561 if (sw->sw_ictx == 0)
566 if (crd->crd_flags & CRD_F_KEY_EXPLICIT)
567 swcr_authprepare(axf, sw, crd->crd_key, crd->crd_klen);
569 bcopy(sw->sw_ictx, &ctx, axf->ctxsize);
571 err = crypto_apply(flags, buf, crd->crd_skip, crd->crd_len,
572 (int (*)(void *, void *, unsigned int))axf->Update, (caddr_t)&ctx);
576 switch (sw->sw_alg) {
577 case CRYPTO_MD5_HMAC:
578 case CRYPTO_SHA1_HMAC:
579 case CRYPTO_SHA2_256_HMAC:
580 case CRYPTO_SHA2_384_HMAC:
581 case CRYPTO_SHA2_512_HMAC:
582 case CRYPTO_RIPEMD160_HMAC:
583 if (sw->sw_octx == NULL)
586 axf->Final(aalg, &ctx);
587 bcopy(sw->sw_octx, &ctx, axf->ctxsize);
588 axf->Update(&ctx, aalg, axf->hashsize);
589 axf->Final(aalg, &ctx);
592 case CRYPTO_MD5_KPDK:
593 case CRYPTO_SHA1_KPDK:
594 if (sw->sw_octx == NULL)
597 axf->Update(&ctx, sw->sw_octx, sw->sw_klen);
598 axf->Final(aalg, &ctx);
601 case CRYPTO_NULL_HMAC:
602 axf->Final(aalg, &ctx);
606 /* Inject the authentication data */
607 crypto_copyback(flags, buf, crd->crd_inject,
608 sw->sw_mlen == 0 ? axf->hashsize : sw->sw_mlen, aalg);
613 * Apply a compression/decompression algorithm
616 swcr_compdec(struct cryptodesc *crd, struct swcr_data *sw,
617 caddr_t buf, int flags)
619 u_int8_t *data, *out;
620 struct comp_algo *cxf;
627 * We must handle the whole buffer of data in one time
628 * then if there is not all the data in the mbuf, we must
631 data = kmalloc(crd->crd_len, M_CRYPTO_DATA, M_INTWAIT);
634 crypto_copydata(flags, buf, crd->crd_skip, crd->crd_len, data);
636 if (crd->crd_flags & CRD_F_COMP)
637 result = cxf->compress(data, crd->crd_len, &out);
639 result = cxf->decompress(data, crd->crd_len, &out);
641 kfree(data, M_CRYPTO_DATA);
645 /* Copy back the (de)compressed data. m_copyback is
646 * extending the mbuf as necessary.
648 sw->sw_size = result;
649 /* Check the compressed size when doing compression */
650 if (crd->crd_flags & CRD_F_COMP) {
651 if (result >= crd->crd_len) {
652 /* Compression was useless, we lost time */
653 kfree(out, M_CRYPTO_DATA);
658 crypto_copyback(flags, buf, crd->crd_skip, result, out);
659 if (result < crd->crd_len) {
660 adj = result - crd->crd_len;
661 if (flags & CRYPTO_F_IMBUF) {
662 adj = result - crd->crd_len;
663 m_adj((struct mbuf *)buf, adj);
664 } else if (flags & CRYPTO_F_IOV) {
665 struct uio *uio = (struct uio *)buf;
668 adj = crd->crd_len - result;
669 ind = uio->uio_iovcnt - 1;
671 while (adj > 0 && ind >= 0) {
672 if (adj < uio->uio_iov[ind].iov_len) {
673 uio->uio_iov[ind].iov_len -= adj;
677 adj -= uio->uio_iov[ind].iov_len;
678 uio->uio_iov[ind].iov_len = 0;
684 kfree(out, M_CRYPTO_DATA);
689 * Generate a new software session.
692 swcr_newsession(device_t dev, u_int32_t *sid, struct cryptoini *cri)
694 struct swcr_data *swd_base;
695 struct swcr_data **swd;
696 struct swcr_data **oswd;
697 struct auth_hash *axf;
698 struct enc_xform *txf;
699 struct comp_algo *cxf;
704 if (sid == NULL || cri == NULL)
711 *swd = kmalloc(sizeof(struct swcr_data),
712 M_CRYPTO_DATA, M_WAITOK | M_ZERO);
714 switch (cri->cri_alg) {
716 txf = &enc_xform_des;
718 case CRYPTO_3DES_CBC:
719 txf = &enc_xform_3des;
722 txf = &enc_xform_blf;
724 case CRYPTO_CAST_CBC:
725 txf = &enc_xform_cast5;
727 case CRYPTO_SKIPJACK_CBC:
728 txf = &enc_xform_skipjack;
730 case CRYPTO_RIJNDAEL128_CBC:
731 txf = &enc_xform_rijndael128;
734 txf = &enc_xform_aes_xts;
737 txf = &enc_xform_aes_ctr;
739 case CRYPTO_CAMELLIA_CBC:
740 txf = &enc_xform_camellia;
742 case CRYPTO_NULL_CBC:
743 txf = &enc_xform_null;
746 if (cri->cri_key != NULL) {
747 error = txf->setkey(&((*swd)->sw_kschedule),
751 swcr_freesession_slot(&swd_base, 0);
755 (*swd)->sw_exf = txf;
758 case CRYPTO_MD5_HMAC:
759 axf = &auth_hash_hmac_md5;
761 case CRYPTO_SHA1_HMAC:
762 axf = &auth_hash_hmac_sha1;
764 case CRYPTO_SHA2_256_HMAC:
765 axf = &auth_hash_hmac_sha2_256;
767 case CRYPTO_SHA2_384_HMAC:
768 axf = &auth_hash_hmac_sha2_384;
770 case CRYPTO_SHA2_512_HMAC:
771 axf = &auth_hash_hmac_sha2_512;
773 case CRYPTO_NULL_HMAC:
774 axf = &auth_hash_null;
776 case CRYPTO_RIPEMD160_HMAC:
777 axf = &auth_hash_hmac_ripemd_160;
779 (*swd)->sw_ictx = kmalloc(axf->ctxsize, M_CRYPTO_DATA,
781 if ((*swd)->sw_ictx == NULL) {
782 swcr_freesession_slot(&swd_base, 0);
786 (*swd)->sw_octx = kmalloc(axf->ctxsize, M_CRYPTO_DATA,
788 if ((*swd)->sw_octx == NULL) {
789 swcr_freesession_slot(&swd_base, 0);
793 if (cri->cri_key != NULL) {
794 swcr_authprepare(axf, *swd, cri->cri_key,
798 (*swd)->sw_mlen = cri->cri_mlen;
799 (*swd)->sw_axf = axf;
802 case CRYPTO_MD5_KPDK:
803 axf = &auth_hash_key_md5;
806 case CRYPTO_SHA1_KPDK:
807 axf = &auth_hash_key_sha1;
809 (*swd)->sw_ictx = kmalloc(axf->ctxsize, M_CRYPTO_DATA,
811 if ((*swd)->sw_ictx == NULL) {
812 swcr_freesession_slot(&swd_base, 0);
816 (*swd)->sw_octx = kmalloc(cri->cri_klen / 8,
817 M_CRYPTO_DATA, M_WAITOK);
818 if ((*swd)->sw_octx == NULL) {
819 swcr_freesession_slot(&swd_base, 0);
823 /* Store the key so we can "append" it to the payload */
824 if (cri->cri_key != NULL) {
825 swcr_authprepare(axf, *swd, cri->cri_key,
829 (*swd)->sw_mlen = cri->cri_mlen;
830 (*swd)->sw_axf = axf;
834 axf = &auth_hash_md5;
838 axf = &auth_hash_sha1;
840 (*swd)->sw_ictx = kmalloc(axf->ctxsize, M_CRYPTO_DATA,
842 if ((*swd)->sw_ictx == NULL) {
843 swcr_freesession_slot(&swd_base, 0);
847 axf->Init((*swd)->sw_ictx);
848 (*swd)->sw_mlen = cri->cri_mlen;
849 (*swd)->sw_axf = axf;
852 case CRYPTO_DEFLATE_COMP:
853 cxf = &comp_algo_deflate;
854 (*swd)->sw_cxf = cxf;
857 swcr_freesession_slot(&swd_base, 0);
861 (*swd)->sw_alg = cri->cri_alg;
863 swd = &((*swd)->sw_next);
868 * Atomically allocate a session
870 spin_lock(&swcr_spin);
871 for (i = swcr_minsesnum; i < swcr_sesnum; ++i) {
872 if (swcr_sessions[i] == NULL)
875 if (i < swcr_sesnum) {
876 swcr_sessions[i] = swd_base;
877 swcr_minsesnum = i + 1;
878 spin_unlock(&swcr_spin);
882 spin_unlock(&swcr_spin);
885 * A larger allocation is required, reallocate the array
886 * and replace, checking for SMP races.
888 if (n < CRYPTO_SW_SESSIONS)
889 n = CRYPTO_SW_SESSIONS;
892 swd = kmalloc(n * sizeof(struct swcr_data *),
893 M_CRYPTO_DATA, M_WAITOK | M_ZERO);
895 spin_lock(&swcr_spin);
896 if (swcr_sesnum >= n) {
897 spin_unlock(&swcr_spin);
898 kfree(swd, M_CRYPTO_DATA);
899 } else if (swcr_sesnum) {
900 bcopy(swcr_sessions, swd,
901 swcr_sesnum * sizeof(struct swcr_data *));
902 oswd = swcr_sessions;
905 spin_unlock(&swcr_spin);
906 kfree(oswd, M_CRYPTO_DATA);
910 spin_unlock(&swcr_spin);
922 swcr_freesession(device_t dev, u_int64_t tid)
924 u_int32_t sid = CRYPTO_SESID2LID(tid);
926 if (sid > swcr_sesnum || swcr_sessions == NULL ||
927 swcr_sessions[sid] == NULL) {
931 /* Silently accept and return */
935 return(swcr_freesession_slot(&swcr_sessions[sid], sid));
940 swcr_freesession_slot(struct swcr_data **swdp, u_int32_t sid)
942 struct enc_xform *txf;
943 struct auth_hash *axf;
944 struct comp_algo *cxf;
945 struct swcr_data *swd;
946 struct swcr_data *swnext;
949 * Protect session detachment with the spinlock.
951 spin_lock(&swcr_spin);
954 if (sid && swcr_minsesnum > sid)
955 swcr_minsesnum = sid;
956 spin_unlock(&swcr_spin);
959 * Clean up at our leisure.
961 while ((swd = swnext) != NULL) {
962 swnext = swd->sw_next;
966 switch (swd->sw_alg) {
968 case CRYPTO_3DES_CBC:
970 case CRYPTO_CAST_CBC:
971 case CRYPTO_SKIPJACK_CBC:
972 case CRYPTO_RIJNDAEL128_CBC:
975 case CRYPTO_CAMELLIA_CBC:
976 case CRYPTO_NULL_CBC:
979 if (swd->sw_kschedule)
980 txf->zerokey(&(swd->sw_kschedule));
983 case CRYPTO_MD5_HMAC:
984 case CRYPTO_SHA1_HMAC:
985 case CRYPTO_SHA2_256_HMAC:
986 case CRYPTO_SHA2_384_HMAC:
987 case CRYPTO_SHA2_512_HMAC:
988 case CRYPTO_RIPEMD160_HMAC:
989 case CRYPTO_NULL_HMAC:
993 bzero(swd->sw_ictx, axf->ctxsize);
994 kfree(swd->sw_ictx, M_CRYPTO_DATA);
997 bzero(swd->sw_octx, axf->ctxsize);
998 kfree(swd->sw_octx, M_CRYPTO_DATA);
1002 case CRYPTO_MD5_KPDK:
1003 case CRYPTO_SHA1_KPDK:
1007 bzero(swd->sw_ictx, axf->ctxsize);
1008 kfree(swd->sw_ictx, M_CRYPTO_DATA);
1011 bzero(swd->sw_octx, swd->sw_klen);
1012 kfree(swd->sw_octx, M_CRYPTO_DATA);
1021 bzero(swd->sw_ictx, axf->ctxsize);
1022 kfree(swd->sw_ictx, M_CRYPTO_DATA);
1026 case CRYPTO_DEFLATE_COMP:
1031 //FREE(swd, M_CRYPTO_DATA);
1032 kfree(swd, M_CRYPTO_DATA);
1038 * Process a software request.
1041 swcr_process(device_t dev, struct cryptop *crp, int hint)
1043 struct cryptodesc *crd;
1044 struct swcr_data *sw;
1051 if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
1052 crp->crp_etype = EINVAL;
1056 lid = crp->crp_sid & 0xffffffff;
1057 if (lid >= swcr_sesnum || lid == 0 || swcr_sessions[lid] == NULL) {
1058 crp->crp_etype = ENOENT;
1062 /* Go through crypto descriptors, processing as we go */
1063 for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
1065 * Find the crypto context.
1067 * XXX Note that the logic here prevents us from having
1068 * XXX the same algorithm multiple times in a session
1069 * XXX (or rather, we can but it won't give us the right
1070 * XXX results). To do that, we'd need some way of differentiating
1071 * XXX between the various instances of an algorithm (so we can
1072 * XXX locate the correct crypto context).
1074 for (sw = swcr_sessions[lid];
1075 sw && sw->sw_alg != crd->crd_alg;
1079 /* No such context ? */
1081 crp->crp_etype = EINVAL;
1084 switch (sw->sw_alg) {
1085 case CRYPTO_DES_CBC:
1086 case CRYPTO_3DES_CBC:
1087 case CRYPTO_BLF_CBC:
1088 case CRYPTO_CAST_CBC:
1089 case CRYPTO_SKIPJACK_CBC:
1090 case CRYPTO_RIJNDAEL128_CBC:
1091 case CRYPTO_AES_XTS:
1092 case CRYPTO_AES_CTR:
1093 case CRYPTO_CAMELLIA_CBC:
1094 if ((crp->crp_etype = swcr_encdec(crd, sw,
1095 crp->crp_buf, crp->crp_flags)) != 0)
1098 case CRYPTO_NULL_CBC:
1101 case CRYPTO_MD5_HMAC:
1102 case CRYPTO_SHA1_HMAC:
1103 case CRYPTO_SHA2_256_HMAC:
1104 case CRYPTO_SHA2_384_HMAC:
1105 case CRYPTO_SHA2_512_HMAC:
1106 case CRYPTO_RIPEMD160_HMAC:
1107 case CRYPTO_NULL_HMAC:
1108 case CRYPTO_MD5_KPDK:
1109 case CRYPTO_SHA1_KPDK:
1112 if ((crp->crp_etype = swcr_authcompute(crd, sw,
1113 crp->crp_buf, crp->crp_flags)) != 0)
1117 case CRYPTO_DEFLATE_COMP:
1118 if ((crp->crp_etype = swcr_compdec(crd, sw,
1119 crp->crp_buf, crp->crp_flags)) != 0)
1122 crp->crp_olen = (int)sw->sw_size;
1126 /* Unknown/unsupported algorithm */
1127 crp->crp_etype = EINVAL;
1139 swcr_identify(driver_t *drv, device_t parent)
1141 /* NB: order 10 is so we get attached after h/w devices */
1142 /* XXX: wouldn't bet about this BUS_ADD_CHILD correctness */
1143 if (device_find_child(parent, "cryptosoft", -1) == NULL &&
1144 BUS_ADD_CHILD(parent, parent, 10, "cryptosoft", -1) == 0)
1145 panic("cryptosoft: could not attach");
1149 swcr_probe(device_t dev)
1151 device_set_desc(dev, "software crypto");
1156 swcr_attach(device_t dev)
1158 memset(hmac_ipad_buffer, HMAC_IPAD_VAL, HMAC_MAX_BLOCK_LEN);
1159 memset(hmac_opad_buffer, HMAC_OPAD_VAL, HMAC_MAX_BLOCK_LEN);
1161 swcr_id = crypto_get_driverid(dev, CRYPTOCAP_F_SOFTWARE |
1165 device_printf(dev, "cannot initialize!");
1168 #define REGISTER(alg) \
1169 crypto_register(swcr_id, alg, 0,0)
1170 REGISTER(CRYPTO_DES_CBC);
1171 REGISTER(CRYPTO_3DES_CBC);
1172 REGISTER(CRYPTO_BLF_CBC);
1173 REGISTER(CRYPTO_CAST_CBC);
1174 REGISTER(CRYPTO_SKIPJACK_CBC);
1175 REGISTER(CRYPTO_NULL_CBC);
1176 REGISTER(CRYPTO_MD5_HMAC);
1177 REGISTER(CRYPTO_SHA1_HMAC);
1178 REGISTER(CRYPTO_SHA2_256_HMAC);
1179 REGISTER(CRYPTO_SHA2_384_HMAC);
1180 REGISTER(CRYPTO_SHA2_512_HMAC);
1181 REGISTER(CRYPTO_RIPEMD160_HMAC);
1182 REGISTER(CRYPTO_NULL_HMAC);
1183 REGISTER(CRYPTO_MD5_KPDK);
1184 REGISTER(CRYPTO_SHA1_KPDK);
1185 REGISTER(CRYPTO_MD5);
1186 REGISTER(CRYPTO_SHA1);
1187 REGISTER(CRYPTO_RIJNDAEL128_CBC);
1188 REGISTER(CRYPTO_AES_XTS);
1189 REGISTER(CRYPTO_AES_CTR);
1190 REGISTER(CRYPTO_CAMELLIA_CBC);
1191 REGISTER(CRYPTO_DEFLATE_COMP);
1198 swcr_detach(device_t dev)
1200 crypto_unregister_all(swcr_id);
1201 if (swcr_sessions != NULL)
1202 kfree(swcr_sessions, M_CRYPTO_DATA);
1206 static device_method_t swcr_methods[] = {
1207 DEVMETHOD(device_identify, swcr_identify),
1208 DEVMETHOD(device_probe, swcr_probe),
1209 DEVMETHOD(device_attach, swcr_attach),
1210 DEVMETHOD(device_detach, swcr_detach),
1212 DEVMETHOD(cryptodev_newsession, swcr_newsession),
1213 DEVMETHOD(cryptodev_freesession,swcr_freesession),
1214 DEVMETHOD(cryptodev_process, swcr_process),
1219 static driver_t swcr_driver = {
1222 0, /* NB: no softc */
1224 static devclass_t swcr_devclass;
1227 * NB: We explicitly reference the crypto module so we
1228 * get the necessary ordering when built as a loadable
1229 * module. This is required because we bundle the crypto
1230 * module code together with the cryptosoft driver (otherwise
1231 * normal module dependencies would handle things).
1233 extern int crypto_modevent(struct module *, int, void *);
1234 /* XXX where to attach */
1235 DRIVER_MODULE(cryptosoft, nexus, swcr_driver, swcr_devclass, crypto_modevent,0);
1236 MODULE_VERSION(cryptosoft, 1);
1237 MODULE_DEPEND(cryptosoft, crypto, 1, 1, 1);