1 /* $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $ */
4 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
5 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
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
12 * Copyright (c) 2014-2021 The FreeBSD Foundation
13 * All rights reserved.
15 * Portions of this software were developed by John-Mark Gurney
16 * under sponsorship of the FreeBSD Foundation and
17 * Rubicon Communications, LLC (Netgate).
19 * Portions of this software were developed by Ararat River
20 * Consulting, LLC under sponsorship of the FreeBSD Foundation.
22 * Permission to use, copy, and modify this software with or without fee
23 * is hereby granted, provided that this entire notice is included in
24 * all source code copies of any software which is or includes a copy or
25 * modification of this software.
27 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
28 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
29 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
30 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/sysctl.h>
43 #include <sys/errno.h>
44 #include <sys/random.h>
45 #include <sys/kernel.h>
48 #include <sys/rwlock.h>
49 #include <sys/endian.h>
50 #include <sys/limits.h>
51 #include <sys/mutex.h>
53 #include <crypto/sha1.h>
54 #include <opencrypto/rmd160.h>
56 #include <opencrypto/cryptodev.h>
57 #include <opencrypto/xform.h>
61 #include "cryptodev_if.h"
66 const struct auth_hash *sw_axf;
72 const struct enc_xform *sw_exf;
76 const struct comp_algo *sw_cxf;
81 int (*swcr_process)(struct swcr_session *, struct cryptop *);
83 struct swcr_auth swcr_auth;
84 struct swcr_encdec swcr_encdec;
85 struct swcr_compdec swcr_compdec;
88 static int32_t swcr_id;
90 static void swcr_freesession(device_t dev, crypto_session_t cses);
92 /* Used for CRYPTO_NULL_CBC. */
94 swcr_null(struct swcr_session *ses, struct cryptop *crp)
101 * Apply a symmetric encryption/decryption algorithm.
104 swcr_encdec(struct swcr_session *ses, struct cryptop *crp)
106 unsigned char iv[EALG_MAX_BLOCK_LEN], blk[EALG_MAX_BLOCK_LEN];
107 unsigned char *ivp, *nivp, iv2[EALG_MAX_BLOCK_LEN];
108 const struct crypto_session_params *csp;
109 const struct enc_xform *exf;
110 struct swcr_encdec *sw;
111 size_t inlen, outlen;
113 struct crypto_buffer_cursor cc_in, cc_out;
114 const unsigned char *inblk;
115 unsigned char *outblk;
121 sw = &ses->swcr_encdec;
123 csp = crypto_get_params(crp->crp_session);
125 if (exf->native_blocksize == 0) {
126 /* Check for non-padded data */
127 if ((crp->crp_payload_length % exf->blocksize) != 0)
130 blks = exf->blocksize;
132 blks = exf->native_blocksize;
134 if (exf == &enc_xform_aes_icm &&
135 (crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
138 if (crp->crp_cipher_key != NULL) {
139 error = exf->setkey(sw->sw_kschedule,
140 crp->crp_cipher_key, csp->csp_cipher_klen);
145 crypto_read_iv(crp, iv);
149 * xforms that provide a reinit method perform all IV
150 * handling themselves.
152 exf->reinit(sw->sw_kschedule, iv, csp->csp_ivlen);
157 crypto_cursor_init(&cc_in, &crp->crp_buf);
158 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
159 inblk = crypto_cursor_segment(&cc_in, &inlen);
160 if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
161 crypto_cursor_init(&cc_out, &crp->crp_obuf);
162 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
165 outblk = crypto_cursor_segment(&cc_out, &outlen);
167 resid = crp->crp_payload_length;
168 encrypting = CRYPTO_OP_IS_ENCRYPT(crp->crp_op);
171 * Loop through encrypting blocks. 'inlen' is the remaining
172 * length of the current segment in the input buffer.
173 * 'outlen' is the remaining length of current segment in the
176 while (resid >= blks) {
178 * If the current block is not contained within the
179 * current input/output segment, use 'blk' as a local
183 crypto_cursor_copydata(&cc_in, blks, blk);
190 * Ciphers without a 'reinit' hook are assumed to be
191 * used in CBC mode where the chaining is done here.
193 if (exf->reinit != NULL) {
195 exf->encrypt(sw->sw_kschedule, inblk, outblk);
197 exf->decrypt(sw->sw_kschedule, inblk, outblk);
198 } else if (encrypting) {
199 /* XOR with previous block */
200 for (i = 0; i < blks; i++)
201 outblk[i] = inblk[i] ^ ivp[i];
203 exf->encrypt(sw->sw_kschedule, outblk, outblk);
206 * Keep encrypted block for XOR'ing
209 memcpy(iv, outblk, blks);
211 } else { /* decrypt */
213 * Keep encrypted block for XOR'ing
216 nivp = (ivp == iv) ? iv2 : iv;
217 memcpy(nivp, inblk, blks);
219 exf->decrypt(sw->sw_kschedule, inblk, outblk);
221 /* XOR with previous block */
222 for (i = 0; i < blks; i++)
229 inblk = crypto_cursor_segment(&cc_in, &inlen);
231 crypto_cursor_advance(&cc_in, blks);
237 crypto_cursor_copyback(&cc_out, blks, blk);
238 outblk = crypto_cursor_segment(&cc_out, &outlen);
240 crypto_cursor_advance(&cc_out, blks);
248 /* Handle trailing partial block for stream ciphers. */
250 KASSERT(exf->native_blocksize != 0,
251 ("%s: partial block of %d bytes for cipher %s",
252 __func__, i, exf->name));
253 KASSERT(exf->reinit != NULL,
254 ("%s: partial block cipher %s without reinit hook",
255 __func__, exf->name));
256 KASSERT(resid < blks, ("%s: partial block too big", __func__));
258 inblk = crypto_cursor_segment(&cc_in, &inlen);
259 outblk = crypto_cursor_segment(&cc_out, &outlen);
261 crypto_cursor_copydata(&cc_in, resid, blk);
267 exf->encrypt_last(sw->sw_kschedule, inblk, outblk,
270 exf->decrypt_last(sw->sw_kschedule, inblk, outblk,
273 crypto_cursor_copyback(&cc_out, resid, blk);
276 explicit_bzero(blk, sizeof(blk));
277 explicit_bzero(iv, sizeof(iv));
278 explicit_bzero(iv2, sizeof(iv2));
283 swcr_authprepare(const struct auth_hash *axf, struct swcr_auth *sw,
284 const uint8_t *key, int klen)
288 case CRYPTO_SHA1_HMAC:
289 case CRYPTO_SHA2_224_HMAC:
290 case CRYPTO_SHA2_256_HMAC:
291 case CRYPTO_SHA2_384_HMAC:
292 case CRYPTO_SHA2_512_HMAC:
293 case CRYPTO_NULL_HMAC:
294 case CRYPTO_RIPEMD160_HMAC:
295 hmac_init_ipad(axf, key, klen, sw->sw_ictx);
296 hmac_init_opad(axf, key, klen, sw->sw_octx);
298 case CRYPTO_POLY1305:
301 axf->Setkey(sw->sw_ictx, key, klen);
302 axf->Init(sw->sw_ictx);
305 panic("%s: algorithm %d doesn't use keys", __func__, axf->type);
310 * Compute or verify hash.
313 swcr_authcompute(struct swcr_session *ses, struct cryptop *crp)
315 u_char aalg[HASH_MAX_LEN];
316 const struct crypto_session_params *csp;
317 struct swcr_auth *sw;
318 const struct auth_hash *axf;
322 sw = &ses->swcr_auth;
326 csp = crypto_get_params(crp->crp_session);
327 if (crp->crp_auth_key != NULL) {
328 swcr_authprepare(axf, sw, crp->crp_auth_key,
332 bcopy(sw->sw_ictx, &ctx, axf->ctxsize);
334 if (crp->crp_aad != NULL)
335 err = axf->Update(&ctx, crp->crp_aad, crp->crp_aad_length);
337 err = crypto_apply(crp, crp->crp_aad_start, crp->crp_aad_length,
342 if (CRYPTO_HAS_OUTPUT_BUFFER(crp) &&
343 CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
344 err = crypto_apply_buf(&crp->crp_obuf,
345 crp->crp_payload_output_start, crp->crp_payload_length,
348 err = crypto_apply(crp, crp->crp_payload_start,
349 crp->crp_payload_length, axf->Update, &ctx);
353 if (csp->csp_flags & CSP_F_ESN)
354 axf->Update(&ctx, crp->crp_esn, 4);
356 axf->Final(aalg, &ctx);
357 if (sw->sw_octx != NULL) {
358 bcopy(sw->sw_octx, &ctx, axf->ctxsize);
359 axf->Update(&ctx, aalg, axf->hashsize);
360 axf->Final(aalg, &ctx);
363 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
364 u_char uaalg[HASH_MAX_LEN];
366 crypto_copydata(crp, crp->crp_digest_start, sw->sw_mlen, uaalg);
367 if (timingsafe_bcmp(aalg, uaalg, sw->sw_mlen) != 0)
369 explicit_bzero(uaalg, sizeof(uaalg));
371 /* Inject the authentication data */
372 crypto_copyback(crp, crp->crp_digest_start, sw->sw_mlen, aalg);
374 explicit_bzero(aalg, sizeof(aalg));
376 explicit_bzero(&ctx, sizeof(ctx));
380 CTASSERT(INT_MAX <= (1ll<<39) - 256); /* GCM: plain text < 2^39-256 */
381 CTASSERT(INT_MAX <= (uint64_t)-1); /* GCM: associated data <= 2^64-1 */
384 swcr_gmac(struct swcr_session *ses, struct cryptop *crp)
386 uint32_t blkbuf[howmany(AES_BLOCK_LEN, sizeof(uint32_t))];
387 u_char *blk = (u_char *)blkbuf;
388 u_char tag[GMAC_DIGEST_LEN];
389 u_char iv[AES_BLOCK_LEN];
390 struct crypto_buffer_cursor cc;
393 struct swcr_auth *swa;
394 const struct auth_hash *axf;
397 int blksz, error, ivlen, resid;
399 swa = &ses->swcr_auth;
402 bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
403 blksz = GMAC_BLOCK_LEN;
404 KASSERT(axf->blocksize == blksz, ("%s: axf block size mismatch",
407 /* Initialize the IV */
408 ivlen = AES_GCM_IV_LEN;
409 crypto_read_iv(crp, iv);
411 axf->Reinit(&ctx, iv, ivlen);
412 crypto_cursor_init(&cc, &crp->crp_buf);
413 crypto_cursor_advance(&cc, crp->crp_payload_start);
414 for (resid = crp->crp_payload_length; resid >= blksz; resid -= len) {
415 inblk = crypto_cursor_segment(&cc, &len);
417 len = rounddown(MIN(len, resid), blksz);
418 crypto_cursor_advance(&cc, len);
421 crypto_cursor_copydata(&cc, len, blk);
424 axf->Update(&ctx, inblk, len);
427 memset(blk, 0, blksz);
428 crypto_cursor_copydata(&cc, resid, blk);
429 axf->Update(&ctx, blk, blksz);
433 memset(blk, 0, blksz);
434 blkp = (uint32_t *)blk + 1;
435 *blkp = htobe32(crp->crp_payload_length * 8);
436 axf->Update(&ctx, blk, blksz);
439 axf->Final(tag, &ctx);
442 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
443 u_char tag2[GMAC_DIGEST_LEN];
445 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
447 if (timingsafe_bcmp(tag, tag2, swa->sw_mlen) != 0)
449 explicit_bzero(tag2, sizeof(tag2));
451 /* Inject the authentication data */
452 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
454 explicit_bzero(blkbuf, sizeof(blkbuf));
455 explicit_bzero(tag, sizeof(tag));
456 explicit_bzero(iv, sizeof(iv));
461 swcr_gcm(struct swcr_session *ses, struct cryptop *crp)
463 uint32_t blkbuf[howmany(AES_BLOCK_LEN, sizeof(uint32_t))];
464 u_char *blk = (u_char *)blkbuf;
465 u_char tag[GMAC_DIGEST_LEN];
466 struct crypto_buffer_cursor cc_in, cc_out;
470 struct swcr_auth *swa;
471 struct swcr_encdec *swe;
472 const struct auth_hash *axf;
473 const struct enc_xform *exf;
476 int blksz, error, ivlen, r, resid;
478 swa = &ses->swcr_auth;
481 bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
482 blksz = GMAC_BLOCK_LEN;
483 KASSERT(axf->blocksize == blksz, ("%s: axf block size mismatch",
486 swe = &ses->swcr_encdec;
488 KASSERT(axf->blocksize == exf->native_blocksize,
489 ("%s: blocksize mismatch", __func__));
491 if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
494 ivlen = AES_GCM_IV_LEN;
496 /* Supply MAC with IV */
497 axf->Reinit(&ctx, crp->crp_iv, ivlen);
499 /* Supply MAC with AAD */
500 if (crp->crp_aad != NULL) {
501 len = rounddown(crp->crp_aad_length, blksz);
503 axf->Update(&ctx, crp->crp_aad, len);
504 if (crp->crp_aad_length != len) {
505 memset(blk, 0, blksz);
506 memcpy(blk, (char *)crp->crp_aad + len,
507 crp->crp_aad_length - len);
508 axf->Update(&ctx, blk, blksz);
511 crypto_cursor_init(&cc_in, &crp->crp_buf);
512 crypto_cursor_advance(&cc_in, crp->crp_aad_start);
513 for (resid = crp->crp_aad_length; resid >= blksz;
515 inblk = crypto_cursor_segment(&cc_in, &len);
517 len = rounddown(MIN(len, resid), blksz);
518 crypto_cursor_advance(&cc_in, len);
521 crypto_cursor_copydata(&cc_in, len, blk);
524 axf->Update(&ctx, inblk, len);
527 memset(blk, 0, blksz);
528 crypto_cursor_copydata(&cc_in, resid, blk);
529 axf->Update(&ctx, blk, blksz);
533 if (crp->crp_cipher_key != NULL)
534 exf->setkey(swe->sw_kschedule, crp->crp_cipher_key,
535 crypto_get_params(crp->crp_session)->csp_cipher_klen);
536 exf->reinit(swe->sw_kschedule, crp->crp_iv, ivlen);
538 /* Do encryption with MAC */
539 crypto_cursor_init(&cc_in, &crp->crp_buf);
540 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
541 if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
542 crypto_cursor_init(&cc_out, &crp->crp_obuf);
543 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
546 for (resid = crp->crp_payload_length; resid >= blksz; resid -= blksz) {
547 inblk = crypto_cursor_segment(&cc_in, &len);
549 crypto_cursor_copydata(&cc_in, blksz, blk);
552 crypto_cursor_advance(&cc_in, blksz);
554 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
555 outblk = crypto_cursor_segment(&cc_out, &len);
558 exf->encrypt(swe->sw_kschedule, inblk, outblk);
559 axf->Update(&ctx, outblk, blksz);
561 crypto_cursor_copyback(&cc_out, blksz, blk);
563 crypto_cursor_advance(&cc_out, blksz);
565 axf->Update(&ctx, inblk, blksz);
569 crypto_cursor_copydata(&cc_in, resid, blk);
570 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
571 exf->encrypt_last(swe->sw_kschedule, blk, blk, resid);
572 crypto_cursor_copyback(&cc_out, resid, blk);
574 axf->Update(&ctx, blk, resid);
578 memset(blk, 0, blksz);
579 blkp = (uint32_t *)blk + 1;
580 *blkp = htobe32(crp->crp_aad_length * 8);
581 blkp = (uint32_t *)blk + 3;
582 *blkp = htobe32(crp->crp_payload_length * 8);
583 axf->Update(&ctx, blk, blksz);
586 axf->Final(tag, &ctx);
590 if (!CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
591 u_char tag2[GMAC_DIGEST_LEN];
593 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen, tag2);
595 r = timingsafe_bcmp(tag, tag2, swa->sw_mlen);
596 explicit_bzero(tag2, sizeof(tag2));
602 /* tag matches, decrypt data */
603 crypto_cursor_init(&cc_in, &crp->crp_buf);
604 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
605 for (resid = crp->crp_payload_length; resid > blksz;
607 inblk = crypto_cursor_segment(&cc_in, &len);
609 crypto_cursor_copydata(&cc_in, blksz, blk);
612 crypto_cursor_advance(&cc_in, blksz);
613 outblk = crypto_cursor_segment(&cc_out, &len);
616 exf->decrypt(swe->sw_kschedule, inblk, outblk);
618 crypto_cursor_copyback(&cc_out, blksz, blk);
620 crypto_cursor_advance(&cc_out, blksz);
623 crypto_cursor_copydata(&cc_in, resid, blk);
624 exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
625 crypto_cursor_copyback(&cc_out, resid, blk);
628 /* Inject the authentication data */
629 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
633 explicit_bzero(blkbuf, sizeof(blkbuf));
634 explicit_bzero(tag, sizeof(tag));
640 build_ccm_b0(const char *nonce, u_int nonce_length, u_int aad_length,
641 u_int data_length, u_int tag_length, uint8_t *b0)
646 KASSERT(nonce_length >= 7 && nonce_length <= 13,
647 ("nonce_length must be between 7 and 13 bytes"));
650 * Need to determine the L field value. This is the number of
651 * bytes needed to specify the length of the message; the length
652 * is whatever is left in the 16 bytes after specifying flags and
655 L = 15 - nonce_length;
657 flags = ((aad_length > 0) << 6) +
658 (((tag_length - 2) / 2) << 3) +
662 * Now we need to set up the first block, which has flags, nonce,
663 * and the message length.
666 memcpy(b0 + 1, nonce, nonce_length);
667 bp = b0 + 1 + nonce_length;
669 /* Need to copy L' [aka L-1] bytes of data_length */
670 for (uint8_t *dst = b0 + CCM_CBC_BLOCK_LEN - 1; dst >= bp; dst--) {
676 /* NB: OCF only supports AAD lengths < 2^32. */
678 build_ccm_aad_length(u_int aad_length, uint8_t *blk)
680 if (aad_length < ((1 << 16) - (1 << 8))) {
681 be16enc(blk, aad_length);
682 return (sizeof(uint16_t));
686 be32enc(blk + 2, aad_length);
687 return (2 + sizeof(uint32_t));
692 swcr_ccm_cbc_mac(struct swcr_session *ses, struct cryptop *crp)
694 u_char iv[AES_BLOCK_LEN];
695 u_char blk[CCM_CBC_BLOCK_LEN];
696 u_char tag[AES_CBC_MAC_HASH_LEN];
698 const struct crypto_session_params *csp;
699 struct swcr_auth *swa;
700 const struct auth_hash *axf;
701 int error, ivlen, len;
703 csp = crypto_get_params(crp->crp_session);
704 swa = &ses->swcr_auth;
707 bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
709 /* Initialize the IV */
710 ivlen = csp->csp_ivlen;
711 crypto_read_iv(crp, iv);
713 /* Supply MAC with IV */
714 axf->Reinit(&ctx, crp->crp_iv, ivlen);
716 /* Supply MAC with b0. */
717 build_ccm_b0(crp->crp_iv, ivlen, crp->crp_payload_length, 0,
719 axf->Update(&ctx, blk, CCM_CBC_BLOCK_LEN);
721 len = build_ccm_aad_length(crp->crp_payload_length, blk);
722 axf->Update(&ctx, blk, len);
724 crypto_apply(crp, crp->crp_payload_start, crp->crp_payload_length,
728 axf->Final(tag, &ctx);
731 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
732 u_char tag2[AES_CBC_MAC_HASH_LEN];
734 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
736 if (timingsafe_bcmp(tag, tag2, swa->sw_mlen) != 0)
738 explicit_bzero(tag2, sizeof(tag));
740 /* Inject the authentication data */
741 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
743 explicit_bzero(tag, sizeof(tag));
744 explicit_bzero(blk, sizeof(blk));
745 explicit_bzero(iv, sizeof(iv));
750 swcr_ccm(struct swcr_session *ses, struct cryptop *crp)
752 const struct crypto_session_params *csp;
753 uint32_t blkbuf[howmany(AES_BLOCK_LEN, sizeof(uint32_t))];
754 u_char *blk = (u_char *)blkbuf;
755 u_char tag[AES_CBC_MAC_HASH_LEN];
756 struct crypto_buffer_cursor cc_in, cc_out;
760 struct swcr_auth *swa;
761 struct swcr_encdec *swe;
762 const struct auth_hash *axf;
763 const struct enc_xform *exf;
765 int blksz, error, ivlen, r, resid;
767 csp = crypto_get_params(crp->crp_session);
768 swa = &ses->swcr_auth;
771 bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
772 blksz = AES_BLOCK_LEN;
773 KASSERT(axf->blocksize == blksz, ("%s: axf block size mismatch",
776 swe = &ses->swcr_encdec;
778 KASSERT(axf->blocksize == exf->native_blocksize,
779 ("%s: blocksize mismatch", __func__));
781 if (crp->crp_payload_length > ccm_max_payload_length(csp))
784 if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
787 ivlen = csp->csp_ivlen;
789 /* Supply MAC with IV */
790 axf->Reinit(&ctx, crp->crp_iv, ivlen);
792 /* Supply MAC with b0. */
793 _Static_assert(sizeof(blkbuf) >= CCM_CBC_BLOCK_LEN,
794 "blkbuf too small for b0");
795 build_ccm_b0(crp->crp_iv, ivlen, crp->crp_aad_length,
796 crp->crp_payload_length, swa->sw_mlen, blk);
797 axf->Update(&ctx, blk, CCM_CBC_BLOCK_LEN);
799 /* Supply MAC with AAD */
800 if (crp->crp_aad_length != 0) {
801 len = build_ccm_aad_length(crp->crp_aad_length, blk);
802 axf->Update(&ctx, blk, len);
803 if (crp->crp_aad != NULL)
804 axf->Update(&ctx, crp->crp_aad,
805 crp->crp_aad_length);
807 crypto_apply(crp, crp->crp_aad_start,
808 crp->crp_aad_length, axf->Update, &ctx);
810 /* Pad the AAD (including length field) to a full block. */
811 len = (len + crp->crp_aad_length) % CCM_CBC_BLOCK_LEN;
813 len = CCM_CBC_BLOCK_LEN - len;
814 memset(blk, 0, CCM_CBC_BLOCK_LEN);
815 axf->Update(&ctx, blk, len);
819 if (crp->crp_cipher_key != NULL)
820 exf->setkey(swe->sw_kschedule, crp->crp_cipher_key,
821 crypto_get_params(crp->crp_session)->csp_cipher_klen);
822 exf->reinit(swe->sw_kschedule, crp->crp_iv, ivlen);
824 /* Do encryption/decryption with MAC */
825 crypto_cursor_init(&cc_in, &crp->crp_buf);
826 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
827 if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
828 crypto_cursor_init(&cc_out, &crp->crp_obuf);
829 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
832 for (resid = crp->crp_payload_length; resid >= blksz; resid -= blksz) {
833 inblk = crypto_cursor_segment(&cc_in, &len);
835 crypto_cursor_copydata(&cc_in, blksz, blk);
838 crypto_cursor_advance(&cc_in, blksz);
839 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
840 outblk = crypto_cursor_segment(&cc_out, &len);
843 axf->Update(&ctx, inblk, blksz);
844 exf->encrypt(swe->sw_kschedule, inblk, outblk);
846 crypto_cursor_copyback(&cc_out, blksz, blk);
848 crypto_cursor_advance(&cc_out, blksz);
851 * One of the problems with CCM+CBC is that
852 * the authentication is done on the
853 * unencrypted data. As a result, we have to
854 * decrypt the data twice: once to generate
855 * the tag and a second time after the tag is
858 exf->decrypt(swe->sw_kschedule, inblk, blk);
859 axf->Update(&ctx, blk, blksz);
863 crypto_cursor_copydata(&cc_in, resid, blk);
864 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
865 axf->Update(&ctx, blk, resid);
866 exf->encrypt_last(swe->sw_kschedule, blk, blk, resid);
867 crypto_cursor_copyback(&cc_out, resid, blk);
869 exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
870 axf->Update(&ctx, blk, resid);
875 axf->Final(tag, &ctx);
879 if (!CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
880 u_char tag2[AES_CBC_MAC_HASH_LEN];
882 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
885 r = timingsafe_bcmp(tag, tag2, swa->sw_mlen);
886 explicit_bzero(tag2, sizeof(tag2));
892 /* tag matches, decrypt data */
893 exf->reinit(swe->sw_kschedule, crp->crp_iv, ivlen);
894 crypto_cursor_init(&cc_in, &crp->crp_buf);
895 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
896 for (resid = crp->crp_payload_length; resid > blksz;
898 inblk = crypto_cursor_segment(&cc_in, &len);
900 crypto_cursor_copydata(&cc_in, blksz, blk);
903 crypto_cursor_advance(&cc_in, blksz);
904 outblk = crypto_cursor_segment(&cc_out, &len);
907 exf->decrypt(swe->sw_kschedule, inblk, outblk);
909 crypto_cursor_copyback(&cc_out, blksz, blk);
911 crypto_cursor_advance(&cc_out, blksz);
914 crypto_cursor_copydata(&cc_in, resid, blk);
915 exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
916 crypto_cursor_copyback(&cc_out, resid, blk);
919 /* Inject the authentication data */
920 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
924 explicit_bzero(blkbuf, sizeof(blkbuf));
925 explicit_bzero(tag, sizeof(tag));
930 swcr_chacha20_poly1305(struct swcr_session *ses, struct cryptop *crp)
932 const struct crypto_session_params *csp;
933 uint64_t blkbuf[howmany(CHACHA20_NATIVE_BLOCK_LEN, sizeof(uint64_t))];
934 u_char *blk = (u_char *)blkbuf;
935 u_char tag[POLY1305_HASH_LEN];
936 struct crypto_buffer_cursor cc_in, cc_out;
941 struct swcr_auth *swa;
942 struct swcr_encdec *swe;
943 const struct auth_hash *axf;
944 const struct enc_xform *exf;
946 int blksz, error, r, resid;
948 swa = &ses->swcr_auth;
951 swe = &ses->swcr_encdec;
953 blksz = exf->native_blocksize;
954 KASSERT(blksz <= sizeof(blkbuf), ("%s: blocksize mismatch", __func__));
956 if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
959 csp = crypto_get_params(crp->crp_session);
961 /* Generate Poly1305 key. */
962 if (crp->crp_cipher_key != NULL)
963 axf->Setkey(&ctx, crp->crp_cipher_key, csp->csp_cipher_klen);
965 axf->Setkey(&ctx, csp->csp_cipher_key, csp->csp_cipher_klen);
966 axf->Reinit(&ctx, crp->crp_iv, csp->csp_ivlen);
968 /* Supply MAC with AAD */
969 if (crp->crp_aad != NULL)
970 axf->Update(&ctx, crp->crp_aad, crp->crp_aad_length);
972 crypto_apply(crp, crp->crp_aad_start,
973 crp->crp_aad_length, axf->Update, &ctx);
974 if (crp->crp_aad_length % 16 != 0) {
977 axf->Update(&ctx, blk, 16 - crp->crp_aad_length % 16);
980 if (crp->crp_cipher_key != NULL)
981 exf->setkey(swe->sw_kschedule, crp->crp_cipher_key,
982 csp->csp_cipher_klen);
983 exf->reinit(swe->sw_kschedule, crp->crp_iv, csp->csp_ivlen);
985 /* Do encryption with MAC */
986 crypto_cursor_init(&cc_in, &crp->crp_buf);
987 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
988 if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
989 crypto_cursor_init(&cc_out, &crp->crp_obuf);
990 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
993 for (resid = crp->crp_payload_length; resid >= blksz; resid -= blksz) {
994 inblk = crypto_cursor_segment(&cc_in, &len);
996 crypto_cursor_copydata(&cc_in, blksz, blk);
999 crypto_cursor_advance(&cc_in, blksz);
1000 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
1001 outblk = crypto_cursor_segment(&cc_out, &len);
1004 exf->encrypt(swe->sw_kschedule, inblk, outblk);
1005 axf->Update(&ctx, outblk, blksz);
1007 crypto_cursor_copyback(&cc_out, blksz, blk);
1009 crypto_cursor_advance(&cc_out, blksz);
1011 axf->Update(&ctx, inblk, blksz);
1015 crypto_cursor_copydata(&cc_in, resid, blk);
1016 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
1017 exf->encrypt_last(swe->sw_kschedule, blk, blk, resid);
1018 crypto_cursor_copyback(&cc_out, resid, blk);
1020 axf->Update(&ctx, blk, resid);
1021 if (resid % 16 != 0) {
1024 axf->Update(&ctx, blk, 16 - resid % 16);
1029 blkp = (uint64_t *)blk;
1030 blkp[0] = htole64(crp->crp_aad_length);
1031 blkp[1] = htole64(crp->crp_payload_length);
1032 axf->Update(&ctx, blk, sizeof(uint64_t) * 2);
1035 axf->Final(tag, &ctx);
1039 if (!CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
1040 u_char tag2[POLY1305_HASH_LEN];
1042 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen, tag2);
1044 r = timingsafe_bcmp(tag, tag2, swa->sw_mlen);
1045 explicit_bzero(tag2, sizeof(tag2));
1051 /* tag matches, decrypt data */
1052 crypto_cursor_init(&cc_in, &crp->crp_buf);
1053 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
1054 for (resid = crp->crp_payload_length; resid > blksz;
1056 inblk = crypto_cursor_segment(&cc_in, &len);
1058 crypto_cursor_copydata(&cc_in, blksz, blk);
1061 crypto_cursor_advance(&cc_in, blksz);
1062 outblk = crypto_cursor_segment(&cc_out, &len);
1065 exf->decrypt(swe->sw_kschedule, inblk, outblk);
1067 crypto_cursor_copyback(&cc_out, blksz, blk);
1069 crypto_cursor_advance(&cc_out, blksz);
1072 crypto_cursor_copydata(&cc_in, resid, blk);
1073 exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
1074 crypto_cursor_copyback(&cc_out, resid, blk);
1077 /* Inject the authentication data */
1078 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
1082 explicit_bzero(blkbuf, sizeof(blkbuf));
1083 explicit_bzero(tag, sizeof(tag));
1084 explicit_bzero(&ctx, sizeof(ctx));
1089 * Apply a cipher and a digest to perform EtA.
1092 swcr_eta(struct swcr_session *ses, struct cryptop *crp)
1096 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
1097 error = swcr_encdec(ses, crp);
1099 error = swcr_authcompute(ses, crp);
1101 error = swcr_authcompute(ses, crp);
1103 error = swcr_encdec(ses, crp);
1109 * Apply a compression/decompression algorithm
1112 swcr_compdec(struct swcr_session *ses, struct cryptop *crp)
1114 const struct comp_algo *cxf;
1115 uint8_t *data, *out;
1119 cxf = ses->swcr_compdec.sw_cxf;
1121 /* We must handle the whole buffer of data in one time
1122 * then if there is not all the data in the mbuf, we must
1126 data = malloc(crp->crp_payload_length, M_CRYPTO_DATA, M_NOWAIT);
1129 crypto_copydata(crp, crp->crp_payload_start, crp->crp_payload_length,
1132 if (CRYPTO_OP_IS_COMPRESS(crp->crp_op))
1133 result = cxf->compress(data, crp->crp_payload_length, &out);
1135 result = cxf->decompress(data, crp->crp_payload_length, &out);
1137 free(data, M_CRYPTO_DATA);
1140 crp->crp_olen = result;
1142 /* Check the compressed size when doing compression */
1143 if (CRYPTO_OP_IS_COMPRESS(crp->crp_op)) {
1144 if (result >= crp->crp_payload_length) {
1145 /* Compression was useless, we lost time */
1146 free(out, M_CRYPTO_DATA);
1151 /* Copy back the (de)compressed data. m_copyback is
1152 * extending the mbuf as necessary.
1154 crypto_copyback(crp, crp->crp_payload_start, result, out);
1155 if (result < crp->crp_payload_length) {
1156 switch (crp->crp_buf.cb_type) {
1157 case CRYPTO_BUF_MBUF:
1158 case CRYPTO_BUF_SINGLE_MBUF:
1159 adj = result - crp->crp_payload_length;
1160 m_adj(crp->crp_buf.cb_mbuf, adj);
1162 case CRYPTO_BUF_UIO: {
1163 struct uio *uio = crp->crp_buf.cb_uio;
1166 adj = crp->crp_payload_length - result;
1167 ind = uio->uio_iovcnt - 1;
1169 while (adj > 0 && ind >= 0) {
1170 if (adj < uio->uio_iov[ind].iov_len) {
1171 uio->uio_iov[ind].iov_len -= adj;
1175 adj -= uio->uio_iov[ind].iov_len;
1176 uio->uio_iov[ind].iov_len = 0;
1182 case CRYPTO_BUF_VMPAGE:
1183 adj = crp->crp_payload_length - result;
1184 crp->crp_buf.cb_vm_page_len -= adj;
1190 free(out, M_CRYPTO_DATA);
1195 swcr_setup_cipher(struct swcr_session *ses,
1196 const struct crypto_session_params *csp)
1198 struct swcr_encdec *swe;
1199 const struct enc_xform *txf;
1202 swe = &ses->swcr_encdec;
1203 txf = crypto_cipher(csp);
1204 if (txf->ctxsize != 0) {
1205 swe->sw_kschedule = malloc(txf->ctxsize, M_CRYPTO_DATA,
1207 if (swe->sw_kschedule == NULL)
1210 if (csp->csp_cipher_key != NULL) {
1211 error = txf->setkey(swe->sw_kschedule,
1212 csp->csp_cipher_key, csp->csp_cipher_klen);
1221 swcr_setup_auth(struct swcr_session *ses,
1222 const struct crypto_session_params *csp)
1224 struct swcr_auth *swa;
1225 const struct auth_hash *axf;
1227 swa = &ses->swcr_auth;
1229 axf = crypto_auth_hash(csp);
1231 if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1233 if (csp->csp_auth_mlen == 0)
1234 swa->sw_mlen = axf->hashsize;
1236 swa->sw_mlen = csp->csp_auth_mlen;
1237 swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1238 if (swa->sw_ictx == NULL)
1241 switch (csp->csp_auth_alg) {
1242 case CRYPTO_SHA1_HMAC:
1243 case CRYPTO_SHA2_224_HMAC:
1244 case CRYPTO_SHA2_256_HMAC:
1245 case CRYPTO_SHA2_384_HMAC:
1246 case CRYPTO_SHA2_512_HMAC:
1247 case CRYPTO_NULL_HMAC:
1248 case CRYPTO_RIPEMD160_HMAC:
1249 swa->sw_octx = malloc(axf->ctxsize, M_CRYPTO_DATA,
1251 if (swa->sw_octx == NULL)
1254 if (csp->csp_auth_key != NULL) {
1255 swcr_authprepare(axf, swa, csp->csp_auth_key,
1256 csp->csp_auth_klen);
1259 if (csp->csp_mode == CSP_MODE_DIGEST)
1260 ses->swcr_process = swcr_authcompute;
1263 case CRYPTO_SHA2_224:
1264 case CRYPTO_SHA2_256:
1265 case CRYPTO_SHA2_384:
1266 case CRYPTO_SHA2_512:
1267 axf->Init(swa->sw_ictx);
1268 if (csp->csp_mode == CSP_MODE_DIGEST)
1269 ses->swcr_process = swcr_authcompute;
1271 case CRYPTO_AES_NIST_GMAC:
1272 axf->Init(swa->sw_ictx);
1273 axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
1274 csp->csp_auth_klen);
1275 if (csp->csp_mode == CSP_MODE_DIGEST)
1276 ses->swcr_process = swcr_gmac;
1278 case CRYPTO_POLY1305:
1279 case CRYPTO_BLAKE2B:
1280 case CRYPTO_BLAKE2S:
1282 * Blake2b and Blake2s support an optional key but do
1285 if (csp->csp_auth_klen == 0 || csp->csp_auth_key != NULL)
1286 axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
1287 csp->csp_auth_klen);
1288 axf->Init(swa->sw_ictx);
1289 if (csp->csp_mode == CSP_MODE_DIGEST)
1290 ses->swcr_process = swcr_authcompute;
1292 case CRYPTO_AES_CCM_CBC_MAC:
1293 axf->Init(swa->sw_ictx);
1294 axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
1295 csp->csp_auth_klen);
1296 if (csp->csp_mode == CSP_MODE_DIGEST)
1297 ses->swcr_process = swcr_ccm_cbc_mac;
1305 swcr_setup_gcm(struct swcr_session *ses,
1306 const struct crypto_session_params *csp)
1308 struct swcr_auth *swa;
1309 const struct auth_hash *axf;
1311 if (csp->csp_ivlen != AES_GCM_IV_LEN)
1314 /* First, setup the auth side. */
1315 swa = &ses->swcr_auth;
1316 switch (csp->csp_cipher_klen * 8) {
1318 axf = &auth_hash_nist_gmac_aes_128;
1321 axf = &auth_hash_nist_gmac_aes_192;
1324 axf = &auth_hash_nist_gmac_aes_256;
1330 if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1332 if (csp->csp_auth_mlen == 0)
1333 swa->sw_mlen = axf->hashsize;
1335 swa->sw_mlen = csp->csp_auth_mlen;
1336 swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1337 if (swa->sw_ictx == NULL)
1339 axf->Init(swa->sw_ictx);
1340 if (csp->csp_cipher_key != NULL)
1341 axf->Setkey(swa->sw_ictx, csp->csp_cipher_key,
1342 csp->csp_cipher_klen);
1344 /* Second, setup the cipher side. */
1345 return (swcr_setup_cipher(ses, csp));
1349 swcr_setup_ccm(struct swcr_session *ses,
1350 const struct crypto_session_params *csp)
1352 struct swcr_auth *swa;
1353 const struct auth_hash *axf;
1355 /* First, setup the auth side. */
1356 swa = &ses->swcr_auth;
1357 switch (csp->csp_cipher_klen * 8) {
1359 axf = &auth_hash_ccm_cbc_mac_128;
1362 axf = &auth_hash_ccm_cbc_mac_192;
1365 axf = &auth_hash_ccm_cbc_mac_256;
1371 if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1373 if (csp->csp_auth_mlen == 0)
1374 swa->sw_mlen = axf->hashsize;
1376 swa->sw_mlen = csp->csp_auth_mlen;
1377 swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1378 if (swa->sw_ictx == NULL)
1380 axf->Init(swa->sw_ictx);
1381 if (csp->csp_cipher_key != NULL)
1382 axf->Setkey(swa->sw_ictx, csp->csp_cipher_key,
1383 csp->csp_cipher_klen);
1385 /* Second, setup the cipher side. */
1386 return (swcr_setup_cipher(ses, csp));
1390 swcr_setup_chacha20_poly1305(struct swcr_session *ses,
1391 const struct crypto_session_params *csp)
1393 struct swcr_auth *swa;
1394 const struct auth_hash *axf;
1396 if (csp->csp_ivlen != CHACHA20_POLY1305_IV_LEN)
1399 /* First, setup the auth side. */
1400 swa = &ses->swcr_auth;
1401 axf = &auth_hash_chacha20_poly1305;
1403 if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1405 if (csp->csp_auth_mlen == 0)
1406 swa->sw_mlen = axf->hashsize;
1408 swa->sw_mlen = csp->csp_auth_mlen;
1410 /* The auth state is regenerated for each nonce. */
1412 /* Second, setup the cipher side. */
1413 return (swcr_setup_cipher(ses, csp));
1417 swcr_auth_supported(const struct crypto_session_params *csp)
1419 const struct auth_hash *axf;
1421 axf = crypto_auth_hash(csp);
1424 switch (csp->csp_auth_alg) {
1425 case CRYPTO_SHA1_HMAC:
1426 case CRYPTO_SHA2_224_HMAC:
1427 case CRYPTO_SHA2_256_HMAC:
1428 case CRYPTO_SHA2_384_HMAC:
1429 case CRYPTO_SHA2_512_HMAC:
1430 case CRYPTO_NULL_HMAC:
1431 case CRYPTO_RIPEMD160_HMAC:
1433 case CRYPTO_AES_NIST_GMAC:
1434 switch (csp->csp_auth_klen * 8) {
1442 if (csp->csp_auth_key == NULL)
1444 if (csp->csp_ivlen != AES_GCM_IV_LEN)
1447 case CRYPTO_POLY1305:
1448 if (csp->csp_auth_klen != POLY1305_KEY_LEN)
1451 case CRYPTO_AES_CCM_CBC_MAC:
1452 switch (csp->csp_auth_klen * 8) {
1460 if (csp->csp_auth_key == NULL)
1468 swcr_cipher_supported(const struct crypto_session_params *csp)
1470 const struct enc_xform *txf;
1472 txf = crypto_cipher(csp);
1475 if (csp->csp_cipher_alg != CRYPTO_NULL_CBC &&
1476 txf->ivsize != csp->csp_ivlen)
1481 #define SUPPORTED_SES (CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD | CSP_F_ESN)
1484 swcr_probesession(device_t dev, const struct crypto_session_params *csp)
1486 if ((csp->csp_flags & ~(SUPPORTED_SES)) != 0)
1488 switch (csp->csp_mode) {
1489 case CSP_MODE_COMPRESS:
1490 switch (csp->csp_cipher_alg) {
1491 case CRYPTO_DEFLATE_COMP:
1497 case CSP_MODE_CIPHER:
1498 switch (csp->csp_cipher_alg) {
1499 case CRYPTO_AES_NIST_GCM_16:
1500 case CRYPTO_AES_CCM_16:
1501 case CRYPTO_CHACHA20_POLY1305:
1504 if (!swcr_cipher_supported(csp))
1509 case CSP_MODE_DIGEST:
1510 if (!swcr_auth_supported(csp))
1514 switch (csp->csp_cipher_alg) {
1515 case CRYPTO_AES_NIST_GCM_16:
1516 case CRYPTO_AES_CCM_16:
1517 case CRYPTO_CHACHA20_POLY1305:
1524 /* AEAD algorithms cannot be used for EtA. */
1525 switch (csp->csp_cipher_alg) {
1526 case CRYPTO_AES_NIST_GCM_16:
1527 case CRYPTO_AES_CCM_16:
1528 case CRYPTO_CHACHA20_POLY1305:
1531 switch (csp->csp_auth_alg) {
1532 case CRYPTO_AES_NIST_GMAC:
1533 case CRYPTO_AES_CCM_CBC_MAC:
1537 if (!swcr_cipher_supported(csp) ||
1538 !swcr_auth_supported(csp))
1545 return (CRYPTODEV_PROBE_SOFTWARE);
1549 * Generate a new software session.
1552 swcr_newsession(device_t dev, crypto_session_t cses,
1553 const struct crypto_session_params *csp)
1555 struct swcr_session *ses;
1556 struct swcr_encdec *swe;
1557 struct swcr_auth *swa;
1558 const struct comp_algo *cxf;
1561 ses = crypto_get_driver_session(cses);
1562 mtx_init(&ses->swcr_lock, "swcr session lock", NULL, MTX_DEF);
1565 swe = &ses->swcr_encdec;
1566 swa = &ses->swcr_auth;
1567 switch (csp->csp_mode) {
1568 case CSP_MODE_COMPRESS:
1569 switch (csp->csp_cipher_alg) {
1570 case CRYPTO_DEFLATE_COMP:
1571 cxf = &comp_algo_deflate;
1575 panic("bad compression algo");
1578 ses->swcr_compdec.sw_cxf = cxf;
1579 ses->swcr_process = swcr_compdec;
1581 case CSP_MODE_CIPHER:
1582 switch (csp->csp_cipher_alg) {
1583 case CRYPTO_NULL_CBC:
1584 ses->swcr_process = swcr_null;
1587 case CRYPTO_AES_NIST_GCM_16:
1588 case CRYPTO_AES_CCM_16:
1589 case CRYPTO_CHACHA20_POLY1305:
1590 panic("bad cipher algo");
1593 error = swcr_setup_cipher(ses, csp);
1595 ses->swcr_process = swcr_encdec;
1598 case CSP_MODE_DIGEST:
1599 error = swcr_setup_auth(ses, csp);
1602 switch (csp->csp_cipher_alg) {
1603 case CRYPTO_AES_NIST_GCM_16:
1604 error = swcr_setup_gcm(ses, csp);
1606 ses->swcr_process = swcr_gcm;
1608 case CRYPTO_AES_CCM_16:
1609 error = swcr_setup_ccm(ses, csp);
1611 ses->swcr_process = swcr_ccm;
1613 case CRYPTO_CHACHA20_POLY1305:
1614 error = swcr_setup_chacha20_poly1305(ses, csp);
1616 ses->swcr_process = swcr_chacha20_poly1305;
1620 panic("bad aead algo");
1626 switch (csp->csp_cipher_alg) {
1627 case CRYPTO_AES_NIST_GCM_16:
1628 case CRYPTO_AES_CCM_16:
1629 case CRYPTO_CHACHA20_POLY1305:
1630 panic("bad eta cipher algo");
1632 switch (csp->csp_auth_alg) {
1633 case CRYPTO_AES_NIST_GMAC:
1634 case CRYPTO_AES_CCM_CBC_MAC:
1635 panic("bad eta auth algo");
1639 error = swcr_setup_auth(ses, csp);
1642 if (csp->csp_cipher_alg == CRYPTO_NULL_CBC) {
1643 /* Effectively degrade to digest mode. */
1644 ses->swcr_process = swcr_authcompute;
1648 error = swcr_setup_cipher(ses, csp);
1650 ses->swcr_process = swcr_eta;
1657 swcr_freesession(dev, cses);
1662 swcr_freesession(device_t dev, crypto_session_t cses)
1664 struct swcr_session *ses;
1666 ses = crypto_get_driver_session(cses);
1668 mtx_destroy(&ses->swcr_lock);
1670 zfree(ses->swcr_encdec.sw_kschedule, M_CRYPTO_DATA);
1671 zfree(ses->swcr_auth.sw_ictx, M_CRYPTO_DATA);
1672 zfree(ses->swcr_auth.sw_octx, M_CRYPTO_DATA);
1676 * Process a software request.
1679 swcr_process(device_t dev, struct cryptop *crp, int hint)
1681 struct swcr_session *ses;
1683 ses = crypto_get_driver_session(crp->crp_session);
1684 mtx_lock(&ses->swcr_lock);
1686 crp->crp_etype = ses->swcr_process(ses, crp);
1688 mtx_unlock(&ses->swcr_lock);
1694 swcr_identify(driver_t *drv, device_t parent)
1696 /* NB: order 10 is so we get attached after h/w devices */
1697 if (device_find_child(parent, "cryptosoft", -1) == NULL &&
1698 BUS_ADD_CHILD(parent, 10, "cryptosoft", 0) == 0)
1699 panic("cryptosoft: could not attach");
1703 swcr_probe(device_t dev)
1705 device_set_desc(dev, "software crypto");
1707 return (BUS_PROBE_NOWILDCARD);
1711 swcr_attach(device_t dev)
1714 swcr_id = crypto_get_driverid(dev, sizeof(struct swcr_session),
1715 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC);
1717 device_printf(dev, "cannot initialize!");
1725 swcr_detach(device_t dev)
1727 crypto_unregister_all(swcr_id);
1731 static device_method_t swcr_methods[] = {
1732 DEVMETHOD(device_identify, swcr_identify),
1733 DEVMETHOD(device_probe, swcr_probe),
1734 DEVMETHOD(device_attach, swcr_attach),
1735 DEVMETHOD(device_detach, swcr_detach),
1737 DEVMETHOD(cryptodev_probesession, swcr_probesession),
1738 DEVMETHOD(cryptodev_newsession, swcr_newsession),
1739 DEVMETHOD(cryptodev_freesession,swcr_freesession),
1740 DEVMETHOD(cryptodev_process, swcr_process),
1745 static driver_t swcr_driver = {
1748 0, /* NB: no softc */
1750 static devclass_t swcr_devclass;
1753 * NB: We explicitly reference the crypto module so we
1754 * get the necessary ordering when built as a loadable
1755 * module. This is required because we bundle the crypto
1756 * module code together with the cryptosoft driver (otherwise
1757 * normal module dependencies would handle things).
1759 extern int crypto_modevent(struct module *, int, void *);
1760 /* XXX where to attach */
1761 DRIVER_MODULE(cryptosoft, nexus, swcr_driver, swcr_devclass, crypto_modevent,0);
1762 MODULE_VERSION(cryptosoft, 1);
1763 MODULE_DEPEND(cryptosoft, crypto, 1, 1, 1);