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 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 * Permission to use, copy, and modify this software with or without fee
20 * is hereby granted, provided that this entire notice is included in
21 * all source code copies of any software which is or includes a copy or
22 * modification of this software.
24 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
25 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
26 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
27 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/sysctl.h>
40 #include <sys/errno.h>
41 #include <sys/random.h>
42 #include <sys/kernel.h>
45 #include <sys/rwlock.h>
46 #include <sys/endian.h>
47 #include <sys/limits.h>
48 #include <sys/mutex.h>
50 #include <crypto/sha1.h>
51 #include <opencrypto/rmd160.h>
53 #include <opencrypto/cryptodev.h>
54 #include <opencrypto/xform.h>
58 #include "cryptodev_if.h"
63 struct auth_hash *sw_axf;
69 struct enc_xform *sw_exf;
73 struct comp_algo *sw_cxf;
78 int (*swcr_process)(struct swcr_session *, struct cryptop *);
80 struct swcr_auth swcr_auth;
81 struct swcr_encdec swcr_encdec;
82 struct swcr_compdec swcr_compdec;
85 static int32_t swcr_id;
87 static void swcr_freesession(device_t dev, crypto_session_t cses);
89 /* Used for CRYPTO_NULL_CBC. */
91 swcr_null(struct swcr_session *ses, struct cryptop *crp)
98 * Apply a symmetric encryption/decryption algorithm.
101 swcr_encdec(struct swcr_session *ses, struct cryptop *crp)
103 unsigned char iv[EALG_MAX_BLOCK_LEN], blk[EALG_MAX_BLOCK_LEN];
104 unsigned char *ivp, *nivp, iv2[EALG_MAX_BLOCK_LEN];
105 const struct crypto_session_params *csp;
106 struct swcr_encdec *sw;
107 struct enc_xform *exf;
108 int i, blks, inlen, ivlen, outlen, resid;
109 struct crypto_buffer_cursor cc_in, cc_out;
110 const unsigned char *inblk;
111 unsigned char *outblk;
117 sw = &ses->swcr_encdec;
121 if (exf->native_blocksize == 0) {
122 /* Check for non-padded data */
123 if ((crp->crp_payload_length % exf->blocksize) != 0)
126 blks = exf->blocksize;
128 blks = exf->native_blocksize;
130 if (exf == &enc_xform_aes_icm &&
131 (crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
134 if (crp->crp_cipher_key != NULL) {
135 csp = crypto_get_params(crp->crp_session);
136 error = exf->setkey(sw->sw_kschedule,
137 crp->crp_cipher_key, csp->csp_cipher_klen);
142 crypto_read_iv(crp, iv);
146 * xforms that provide a reinit method perform all IV
147 * handling themselves.
149 exf->reinit(sw->sw_kschedule, iv);
154 crypto_cursor_init(&cc_in, &crp->crp_buf);
155 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
156 inlen = crypto_cursor_seglen(&cc_in);
157 inblk = crypto_cursor_segbase(&cc_in);
158 if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
159 crypto_cursor_init(&cc_out, &crp->crp_obuf);
160 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
163 outlen = crypto_cursor_seglen(&cc_out);
164 outblk = crypto_cursor_segbase(&cc_out);
166 resid = crp->crp_payload_length;
167 encrypting = CRYPTO_OP_IS_ENCRYPT(crp->crp_op);
170 * Loop through encrypting blocks. 'inlen' is the remaining
171 * length of the current segment in the input buffer.
172 * 'outlen' is the remaining length of current segment in the
175 while (resid >= blks) {
177 * If the current block is not contained within the
178 * current input/output segment, use 'blk' as a local
182 crypto_cursor_copydata(&cc_in, blks, blk);
189 * Ciphers without a 'reinit' hook are assumed to be
190 * used in CBC mode where the chaining is done here.
192 if (exf->reinit != NULL) {
194 exf->encrypt(sw->sw_kschedule, inblk, outblk);
196 exf->decrypt(sw->sw_kschedule, inblk, outblk);
197 } else if (encrypting) {
198 /* XOR with previous block */
199 for (i = 0; i < blks; i++)
200 outblk[i] = inblk[i] ^ ivp[i];
202 exf->encrypt(sw->sw_kschedule, outblk, outblk);
205 * Keep encrypted block for XOR'ing
208 memcpy(iv, outblk, blks);
210 } else { /* decrypt */
212 * Keep encrypted block for XOR'ing
215 nivp = (ivp == iv) ? iv2 : iv;
216 memcpy(nivp, inblk, blks);
218 exf->decrypt(sw->sw_kschedule, inblk, outblk);
220 /* XOR with previous block */
221 for (i = 0; i < blks; i++)
228 inlen = crypto_cursor_seglen(&cc_in);
229 inblk = crypto_cursor_segbase(&cc_in);
231 crypto_cursor_advance(&cc_in, blks);
237 crypto_cursor_copyback(&cc_out, blks, blk);
238 outlen = crypto_cursor_seglen(&cc_out);
239 outblk = crypto_cursor_segbase(&cc_out);
241 crypto_cursor_advance(&cc_out, blks);
249 /* Handle trailing partial block for stream ciphers. */
251 KASSERT(exf->native_blocksize != 0,
252 ("%s: partial block of %d bytes for cipher %s",
253 __func__, i, exf->name));
254 KASSERT(exf->reinit != NULL,
255 ("%s: partial block cipher %s without reinit hook",
256 __func__, exf->name));
257 KASSERT(resid < blks, ("%s: partial block too big", __func__));
259 inlen = crypto_cursor_seglen(&cc_in);
260 outlen = crypto_cursor_seglen(&cc_out);
262 crypto_cursor_copydata(&cc_in, resid, blk);
265 inblk = crypto_cursor_segbase(&cc_in);
269 outblk = crypto_cursor_segbase(&cc_out);
271 exf->encrypt_last(sw->sw_kschedule, inblk, outblk,
274 exf->decrypt_last(sw->sw_kschedule, inblk, outblk,
277 crypto_cursor_copyback(&cc_out, resid, blk);
280 explicit_bzero(blk, sizeof(blk));
281 explicit_bzero(iv, sizeof(iv));
282 explicit_bzero(iv2, sizeof(iv2));
287 swcr_authprepare(struct auth_hash *axf, struct swcr_auth *sw,
288 const uint8_t *key, int klen)
292 case CRYPTO_SHA1_HMAC:
293 case CRYPTO_SHA2_224_HMAC:
294 case CRYPTO_SHA2_256_HMAC:
295 case CRYPTO_SHA2_384_HMAC:
296 case CRYPTO_SHA2_512_HMAC:
297 case CRYPTO_NULL_HMAC:
298 case CRYPTO_RIPEMD160_HMAC:
299 hmac_init_ipad(axf, key, klen, sw->sw_ictx);
300 hmac_init_opad(axf, key, klen, sw->sw_octx);
302 case CRYPTO_POLY1305:
305 axf->Setkey(sw->sw_ictx, key, klen);
306 axf->Init(sw->sw_ictx);
309 panic("%s: algorithm %d doesn't use keys", __func__, axf->type);
314 * Compute or verify hash.
317 swcr_authcompute(struct swcr_session *ses, struct cryptop *crp)
319 u_char aalg[HASH_MAX_LEN];
320 const struct crypto_session_params *csp;
321 struct swcr_auth *sw;
322 struct auth_hash *axf;
326 sw = &ses->swcr_auth;
330 if (crp->crp_auth_key != NULL) {
331 csp = crypto_get_params(crp->crp_session);
332 swcr_authprepare(axf, sw, crp->crp_auth_key,
336 bcopy(sw->sw_ictx, &ctx, axf->ctxsize);
338 if (crp->crp_aad != NULL)
339 err = axf->Update(&ctx, crp->crp_aad, crp->crp_aad_length);
341 err = crypto_apply(crp, crp->crp_aad_start, crp->crp_aad_length,
346 if (CRYPTO_HAS_OUTPUT_BUFFER(crp) &&
347 CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
348 err = crypto_apply_buf(&crp->crp_obuf,
349 crp->crp_payload_output_start, crp->crp_payload_length,
352 err = crypto_apply(crp, crp->crp_payload_start,
353 crp->crp_payload_length, axf->Update, &ctx);
357 axf->Final(aalg, &ctx);
358 if (sw->sw_octx != NULL) {
359 bcopy(sw->sw_octx, &ctx, axf->ctxsize);
360 axf->Update(&ctx, aalg, axf->hashsize);
361 axf->Final(aalg, &ctx);
364 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
365 u_char uaalg[HASH_MAX_LEN];
367 crypto_copydata(crp, crp->crp_digest_start, sw->sw_mlen, uaalg);
368 if (timingsafe_bcmp(aalg, uaalg, sw->sw_mlen) != 0)
370 explicit_bzero(uaalg, sizeof(uaalg));
372 /* Inject the authentication data */
373 crypto_copyback(crp, crp->crp_digest_start, sw->sw_mlen, aalg);
375 explicit_bzero(aalg, sizeof(aalg));
377 explicit_bzero(&ctx, sizeof(ctx));
381 CTASSERT(INT_MAX <= (1ll<<39) - 256); /* GCM: plain text < 2^39-256 */
382 CTASSERT(INT_MAX <= (uint64_t)-1); /* GCM: associated data <= 2^64-1 */
385 swcr_gmac(struct swcr_session *ses, struct cryptop *crp)
387 uint32_t blkbuf[howmany(AES_BLOCK_LEN, sizeof(uint32_t))];
388 u_char *blk = (u_char *)blkbuf;
389 u_char tag[GMAC_DIGEST_LEN];
390 u_char iv[AES_BLOCK_LEN];
391 struct crypto_buffer_cursor cc;
394 struct swcr_auth *swa;
395 struct auth_hash *axf;
397 int blksz, error, ivlen, len, 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 len = crypto_cursor_seglen(&cc);
417 inblk = crypto_cursor_segbase(&cc);
418 len = rounddown(MIN(len, resid), blksz);
419 crypto_cursor_advance(&cc, len);
422 crypto_cursor_copydata(&cc, len, blk);
425 axf->Update(&ctx, inblk, len);
428 memset(blk, 0, blksz);
429 crypto_cursor_copydata(&cc, resid, blk);
430 axf->Update(&ctx, blk, blksz);
434 memset(blk, 0, blksz);
435 blkp = (uint32_t *)blk + 1;
436 *blkp = htobe32(crp->crp_payload_length * 8);
437 axf->Update(&ctx, blk, blksz);
440 axf->Final(tag, &ctx);
443 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
444 u_char tag2[GMAC_DIGEST_LEN];
446 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
448 if (timingsafe_bcmp(tag, tag2, swa->sw_mlen) != 0)
450 explicit_bzero(tag2, sizeof(tag2));
452 /* Inject the authentication data */
453 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
455 explicit_bzero(blkbuf, sizeof(blkbuf));
456 explicit_bzero(tag, sizeof(tag));
457 explicit_bzero(iv, sizeof(iv));
462 swcr_gcm(struct swcr_session *ses, struct cryptop *crp)
464 uint32_t blkbuf[howmany(AES_BLOCK_LEN, sizeof(uint32_t))];
465 u_char *blk = (u_char *)blkbuf;
466 u_char tag[GMAC_DIGEST_LEN];
467 u_char iv[AES_BLOCK_LEN];
468 struct crypto_buffer_cursor cc_in, cc_out;
472 struct swcr_auth *swa;
473 struct swcr_encdec *swe;
474 struct auth_hash *axf;
475 struct enc_xform *exf;
477 int blksz, error, ivlen, len, r, resid;
479 swa = &ses->swcr_auth;
482 bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
483 blksz = GMAC_BLOCK_LEN;
484 KASSERT(axf->blocksize == blksz, ("%s: axf block size mismatch",
487 swe = &ses->swcr_encdec;
489 KASSERT(axf->blocksize == exf->native_blocksize,
490 ("%s: blocksize mismatch", __func__));
492 if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
495 /* Initialize the IV */
496 ivlen = AES_GCM_IV_LEN;
497 bcopy(crp->crp_iv, iv, ivlen);
499 /* Supply MAC with IV */
500 axf->Reinit(&ctx, iv, ivlen);
502 /* Supply MAC with AAD */
503 if (crp->crp_aad != NULL) {
504 len = rounddown(crp->crp_aad_length, blksz);
506 axf->Update(&ctx, crp->crp_aad, len);
507 if (crp->crp_aad_length != len) {
508 memset(blk, 0, blksz);
509 memcpy(blk, (char *)crp->crp_aad + len,
510 crp->crp_aad_length - len);
511 axf->Update(&ctx, blk, blksz);
514 crypto_cursor_init(&cc_in, &crp->crp_buf);
515 crypto_cursor_advance(&cc_in, crp->crp_aad_start);
516 for (resid = crp->crp_aad_length; resid >= blksz;
518 len = crypto_cursor_seglen(&cc_in);
520 inblk = crypto_cursor_segbase(&cc_in);
521 len = rounddown(MIN(len, resid), blksz);
522 crypto_cursor_advance(&cc_in, len);
525 crypto_cursor_copydata(&cc_in, len, blk);
528 axf->Update(&ctx, inblk, len);
531 memset(blk, 0, blksz);
532 crypto_cursor_copydata(&cc_in, resid, blk);
533 axf->Update(&ctx, blk, blksz);
537 exf->reinit(swe->sw_kschedule, iv);
539 /* Do encryption with MAC */
540 crypto_cursor_init(&cc_in, &crp->crp_buf);
541 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
542 if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
543 crypto_cursor_init(&cc_out, &crp->crp_obuf);
544 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
547 for (resid = crp->crp_payload_length; resid >= blksz; resid -= blksz) {
548 if (crypto_cursor_seglen(&cc_in) < blksz) {
549 crypto_cursor_copydata(&cc_in, blksz, blk);
552 inblk = crypto_cursor_segbase(&cc_in);
553 crypto_cursor_advance(&cc_in, blksz);
555 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
556 if (crypto_cursor_seglen(&cc_out) < blksz)
559 outblk = crypto_cursor_segbase(&cc_out);
560 exf->encrypt(swe->sw_kschedule, inblk, outblk);
561 axf->Update(&ctx, outblk, blksz);
563 crypto_cursor_copyback(&cc_out, blksz, blk);
565 crypto_cursor_advance(&cc_out, blksz);
567 axf->Update(&ctx, inblk, blksz);
571 crypto_cursor_copydata(&cc_in, resid, blk);
572 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
573 exf->encrypt_last(swe->sw_kschedule, blk, blk, resid);
574 crypto_cursor_copyback(&cc_out, resid, blk);
576 axf->Update(&ctx, blk, resid);
580 memset(blk, 0, blksz);
581 blkp = (uint32_t *)blk + 1;
582 *blkp = htobe32(crp->crp_aad_length * 8);
583 blkp = (uint32_t *)blk + 3;
584 *blkp = htobe32(crp->crp_payload_length * 8);
585 axf->Update(&ctx, blk, blksz);
588 axf->Final(tag, &ctx);
592 if (!CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
593 u_char tag2[GMAC_DIGEST_LEN];
595 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen, tag2);
597 r = timingsafe_bcmp(tag, tag2, swa->sw_mlen);
598 explicit_bzero(tag2, sizeof(tag2));
604 /* tag matches, decrypt data */
605 crypto_cursor_init(&cc_in, &crp->crp_buf);
606 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
607 for (resid = crp->crp_payload_length; resid > blksz;
609 if (crypto_cursor_seglen(&cc_in) < blksz) {
610 crypto_cursor_copydata(&cc_in, blksz, blk);
613 inblk = crypto_cursor_segbase(&cc_in);
614 crypto_cursor_advance(&cc_in, blksz);
616 if (crypto_cursor_seglen(&cc_out) < blksz)
619 outblk = crypto_cursor_segbase(&cc_out);
620 exf->decrypt(swe->sw_kschedule, inblk, outblk);
622 crypto_cursor_copyback(&cc_out, blksz, blk);
624 crypto_cursor_advance(&cc_out, blksz);
627 crypto_cursor_copydata(&cc_in, resid, blk);
628 exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
629 crypto_cursor_copyback(&cc_out, resid, blk);
632 /* Inject the authentication data */
633 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
637 explicit_bzero(blkbuf, sizeof(blkbuf));
638 explicit_bzero(tag, sizeof(tag));
639 explicit_bzero(iv, sizeof(iv));
645 swcr_ccm_cbc_mac(struct swcr_session *ses, struct cryptop *crp)
647 u_char tag[AES_CBC_MAC_HASH_LEN];
648 u_char iv[AES_BLOCK_LEN];
650 struct swcr_auth *swa;
651 struct auth_hash *axf;
654 swa = &ses->swcr_auth;
657 bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
659 /* Initialize the IV */
660 ivlen = AES_CCM_IV_LEN;
661 crypto_read_iv(crp, iv);
664 * AES CCM-CBC-MAC needs to know the length of both the auth
665 * data and payload data before doing the auth computation.
667 ctx.aes_cbc_mac_ctx.authDataLength = crp->crp_payload_length;
668 ctx.aes_cbc_mac_ctx.cryptDataLength = 0;
670 axf->Reinit(&ctx, iv, ivlen);
671 if (crp->crp_aad != NULL)
672 error = axf->Update(&ctx, crp->crp_aad, crp->crp_aad_length);
674 error = crypto_apply(crp, crp->crp_payload_start,
675 crp->crp_payload_length, axf->Update, &ctx);
680 axf->Final(tag, &ctx);
682 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
683 u_char tag2[AES_CBC_MAC_HASH_LEN];
685 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
687 if (timingsafe_bcmp(tag, tag2, swa->sw_mlen) != 0)
689 explicit_bzero(tag2, sizeof(tag));
691 /* Inject the authentication data */
692 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
694 explicit_bzero(tag, sizeof(tag));
695 explicit_bzero(iv, sizeof(iv));
700 swcr_ccm(struct swcr_session *ses, struct cryptop *crp)
702 uint32_t blkbuf[howmany(AES_BLOCK_LEN, sizeof(uint32_t))];
703 u_char *blk = (u_char *)blkbuf;
704 u_char tag[AES_CBC_MAC_HASH_LEN];
705 u_char iv[AES_BLOCK_LEN];
706 struct crypto_buffer_cursor cc_in, cc_out;
710 struct swcr_auth *swa;
711 struct swcr_encdec *swe;
712 struct auth_hash *axf;
713 struct enc_xform *exf;
714 int blksz, error, ivlen, r, resid;
716 swa = &ses->swcr_auth;
719 bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
720 blksz = AES_BLOCK_LEN;
721 KASSERT(axf->blocksize == blksz, ("%s: axf block size mismatch",
724 swe = &ses->swcr_encdec;
726 KASSERT(axf->blocksize == exf->native_blocksize,
727 ("%s: blocksize mismatch", __func__));
729 if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
732 /* Initialize the IV */
733 ivlen = AES_CCM_IV_LEN;
734 bcopy(crp->crp_iv, iv, ivlen);
737 * AES CCM-CBC-MAC needs to know the length of both the auth
738 * data and payload data before doing the auth computation.
740 ctx.aes_cbc_mac_ctx.authDataLength = crp->crp_aad_length;
741 ctx.aes_cbc_mac_ctx.cryptDataLength = crp->crp_payload_length;
743 /* Supply MAC with IV */
744 axf->Reinit(&ctx, iv, ivlen);
746 /* Supply MAC with AAD */
747 if (crp->crp_aad != NULL)
748 error = axf->Update(&ctx, crp->crp_aad, crp->crp_aad_length);
750 error = crypto_apply(crp, crp->crp_aad_start,
751 crp->crp_aad_length, axf->Update, &ctx);
755 exf->reinit(swe->sw_kschedule, iv);
757 /* Do encryption/decryption with MAC */
758 crypto_cursor_init(&cc_in, &crp->crp_buf);
759 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
760 if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
761 crypto_cursor_init(&cc_out, &crp->crp_obuf);
762 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
765 for (resid = crp->crp_payload_length; resid >= blksz; resid -= blksz) {
766 if (crypto_cursor_seglen(&cc_in) < blksz) {
767 crypto_cursor_copydata(&cc_in, blksz, blk);
770 inblk = crypto_cursor_segbase(&cc_in);
771 crypto_cursor_advance(&cc_in, blksz);
773 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
774 if (crypto_cursor_seglen(&cc_out) < blksz)
777 outblk = crypto_cursor_segbase(&cc_out);
778 axf->Update(&ctx, inblk, blksz);
779 exf->encrypt(swe->sw_kschedule, inblk, outblk);
781 crypto_cursor_copyback(&cc_out, blksz, blk);
783 crypto_cursor_advance(&cc_out, blksz);
786 * One of the problems with CCM+CBC is that
787 * the authentication is done on the
788 * unencrypted data. As a result, we have to
789 * decrypt the data twice: once to generate
790 * the tag and a second time after the tag is
793 exf->decrypt(swe->sw_kschedule, inblk, blk);
794 axf->Update(&ctx, blk, blksz);
798 crypto_cursor_copydata(&cc_in, resid, blk);
799 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
800 axf->Update(&ctx, blk, resid);
801 exf->encrypt_last(swe->sw_kschedule, blk, blk, resid);
802 crypto_cursor_copyback(&cc_out, resid, blk);
804 exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
805 axf->Update(&ctx, blk, resid);
810 axf->Final(tag, &ctx);
814 if (!CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
815 u_char tag2[AES_CBC_MAC_HASH_LEN];
817 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
820 r = timingsafe_bcmp(tag, tag2, swa->sw_mlen);
821 explicit_bzero(tag2, sizeof(tag2));
827 /* tag matches, decrypt data */
828 exf->reinit(swe->sw_kschedule, iv);
829 crypto_cursor_init(&cc_in, &crp->crp_buf);
830 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
831 for (resid = crp->crp_payload_length; resid > blksz;
833 if (crypto_cursor_seglen(&cc_in) < blksz) {
834 crypto_cursor_copydata(&cc_in, blksz, blk);
837 inblk = crypto_cursor_segbase(&cc_in);
838 crypto_cursor_advance(&cc_in, blksz);
840 if (crypto_cursor_seglen(&cc_out) < blksz)
843 outblk = crypto_cursor_segbase(&cc_out);
844 exf->decrypt(swe->sw_kschedule, inblk, outblk);
846 crypto_cursor_copyback(&cc_out, blksz, blk);
848 crypto_cursor_advance(&cc_out, blksz);
851 crypto_cursor_copydata(&cc_in, resid, blk);
852 exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
853 crypto_cursor_copyback(&cc_out, resid, blk);
856 /* Inject the authentication data */
857 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
861 explicit_bzero(blkbuf, sizeof(blkbuf));
862 explicit_bzero(tag, sizeof(tag));
863 explicit_bzero(iv, sizeof(iv));
868 * Apply a cipher and a digest to perform EtA.
871 swcr_eta(struct swcr_session *ses, struct cryptop *crp)
875 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
876 error = swcr_encdec(ses, crp);
878 error = swcr_authcompute(ses, crp);
880 error = swcr_authcompute(ses, crp);
882 error = swcr_encdec(ses, crp);
888 * Apply a compression/decompression algorithm
891 swcr_compdec(struct swcr_session *ses, struct cryptop *crp)
893 u_int8_t *data, *out;
894 struct comp_algo *cxf;
898 cxf = ses->swcr_compdec.sw_cxf;
900 /* We must handle the whole buffer of data in one time
901 * then if there is not all the data in the mbuf, we must
905 data = malloc(crp->crp_payload_length, M_CRYPTO_DATA, M_NOWAIT);
908 crypto_copydata(crp, crp->crp_payload_start, crp->crp_payload_length,
911 if (CRYPTO_OP_IS_COMPRESS(crp->crp_op))
912 result = cxf->compress(data, crp->crp_payload_length, &out);
914 result = cxf->decompress(data, crp->crp_payload_length, &out);
916 free(data, M_CRYPTO_DATA);
919 crp->crp_olen = result;
921 /* Check the compressed size when doing compression */
922 if (CRYPTO_OP_IS_COMPRESS(crp->crp_op)) {
923 if (result >= crp->crp_payload_length) {
924 /* Compression was useless, we lost time */
925 free(out, M_CRYPTO_DATA);
930 /* Copy back the (de)compressed data. m_copyback is
931 * extending the mbuf as necessary.
933 crypto_copyback(crp, crp->crp_payload_start, result, out);
934 if (result < crp->crp_payload_length) {
935 switch (crp->crp_buf.cb_type) {
936 case CRYPTO_BUF_MBUF:
937 adj = result - crp->crp_payload_length;
938 m_adj(crp->crp_buf.cb_mbuf, adj);
940 case CRYPTO_BUF_UIO: {
941 struct uio *uio = crp->crp_buf.cb_uio;
944 adj = crp->crp_payload_length - result;
945 ind = uio->uio_iovcnt - 1;
947 while (adj > 0 && ind >= 0) {
948 if (adj < uio->uio_iov[ind].iov_len) {
949 uio->uio_iov[ind].iov_len -= adj;
953 adj -= uio->uio_iov[ind].iov_len;
954 uio->uio_iov[ind].iov_len = 0;
960 case CRYPTO_BUF_VMPAGE:
961 adj = crp->crp_payload_length - result;
962 crp->crp_buf.cb_vm_page_len -= adj;
968 free(out, M_CRYPTO_DATA);
973 swcr_setup_cipher(struct swcr_session *ses,
974 const struct crypto_session_params *csp)
976 struct swcr_encdec *swe;
977 struct enc_xform *txf;
980 swe = &ses->swcr_encdec;
981 txf = crypto_cipher(csp);
982 MPASS(txf->ivsize == csp->csp_ivlen);
983 if (txf->ctxsize != 0) {
984 swe->sw_kschedule = malloc(txf->ctxsize, M_CRYPTO_DATA,
986 if (swe->sw_kschedule == NULL)
989 if (csp->csp_cipher_key != NULL) {
990 error = txf->setkey(swe->sw_kschedule,
991 csp->csp_cipher_key, csp->csp_cipher_klen);
1000 swcr_setup_auth(struct swcr_session *ses,
1001 const struct crypto_session_params *csp)
1003 struct swcr_auth *swa;
1004 struct auth_hash *axf;
1006 swa = &ses->swcr_auth;
1008 axf = crypto_auth_hash(csp);
1010 if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1012 if (csp->csp_auth_mlen == 0)
1013 swa->sw_mlen = axf->hashsize;
1015 swa->sw_mlen = csp->csp_auth_mlen;
1016 swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1017 if (swa->sw_ictx == NULL)
1020 switch (csp->csp_auth_alg) {
1021 case CRYPTO_SHA1_HMAC:
1022 case CRYPTO_SHA2_224_HMAC:
1023 case CRYPTO_SHA2_256_HMAC:
1024 case CRYPTO_SHA2_384_HMAC:
1025 case CRYPTO_SHA2_512_HMAC:
1026 case CRYPTO_NULL_HMAC:
1027 case CRYPTO_RIPEMD160_HMAC:
1028 swa->sw_octx = malloc(axf->ctxsize, M_CRYPTO_DATA,
1030 if (swa->sw_octx == NULL)
1033 if (csp->csp_auth_key != NULL) {
1034 swcr_authprepare(axf, swa, csp->csp_auth_key,
1035 csp->csp_auth_klen);
1038 if (csp->csp_mode == CSP_MODE_DIGEST)
1039 ses->swcr_process = swcr_authcompute;
1042 case CRYPTO_SHA2_224:
1043 case CRYPTO_SHA2_256:
1044 case CRYPTO_SHA2_384:
1045 case CRYPTO_SHA2_512:
1046 axf->Init(swa->sw_ictx);
1047 if (csp->csp_mode == CSP_MODE_DIGEST)
1048 ses->swcr_process = swcr_authcompute;
1050 case CRYPTO_AES_NIST_GMAC:
1051 axf->Init(swa->sw_ictx);
1052 axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
1053 csp->csp_auth_klen);
1054 if (csp->csp_mode == CSP_MODE_DIGEST)
1055 ses->swcr_process = swcr_gmac;
1057 case CRYPTO_POLY1305:
1058 case CRYPTO_BLAKE2B:
1059 case CRYPTO_BLAKE2S:
1061 * Blake2b and Blake2s support an optional key but do
1064 if (csp->csp_auth_klen == 0 || csp->csp_auth_key != NULL)
1065 axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
1066 csp->csp_auth_klen);
1067 axf->Init(swa->sw_ictx);
1068 if (csp->csp_mode == CSP_MODE_DIGEST)
1069 ses->swcr_process = swcr_authcompute;
1071 case CRYPTO_AES_CCM_CBC_MAC:
1072 axf->Init(swa->sw_ictx);
1073 axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
1074 csp->csp_auth_klen);
1075 if (csp->csp_mode == CSP_MODE_DIGEST)
1076 ses->swcr_process = swcr_ccm_cbc_mac;
1084 swcr_setup_gcm(struct swcr_session *ses,
1085 const struct crypto_session_params *csp)
1087 struct swcr_auth *swa;
1088 struct auth_hash *axf;
1090 if (csp->csp_ivlen != AES_GCM_IV_LEN)
1093 /* First, setup the auth side. */
1094 swa = &ses->swcr_auth;
1095 switch (csp->csp_cipher_klen * 8) {
1097 axf = &auth_hash_nist_gmac_aes_128;
1100 axf = &auth_hash_nist_gmac_aes_192;
1103 axf = &auth_hash_nist_gmac_aes_256;
1109 if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1111 if (csp->csp_auth_mlen == 0)
1112 swa->sw_mlen = axf->hashsize;
1114 swa->sw_mlen = csp->csp_auth_mlen;
1115 swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1116 if (swa->sw_ictx == NULL)
1118 axf->Init(swa->sw_ictx);
1119 if (csp->csp_cipher_key != NULL)
1120 axf->Setkey(swa->sw_ictx, csp->csp_cipher_key,
1121 csp->csp_cipher_klen);
1123 /* Second, setup the cipher side. */
1124 return (swcr_setup_cipher(ses, csp));
1128 swcr_setup_ccm(struct swcr_session *ses,
1129 const struct crypto_session_params *csp)
1131 struct swcr_auth *swa;
1132 struct auth_hash *axf;
1134 if (csp->csp_ivlen != AES_CCM_IV_LEN)
1137 /* First, setup the auth side. */
1138 swa = &ses->swcr_auth;
1139 switch (csp->csp_cipher_klen * 8) {
1141 axf = &auth_hash_ccm_cbc_mac_128;
1144 axf = &auth_hash_ccm_cbc_mac_192;
1147 axf = &auth_hash_ccm_cbc_mac_256;
1153 if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1155 if (csp->csp_auth_mlen == 0)
1156 swa->sw_mlen = axf->hashsize;
1158 swa->sw_mlen = csp->csp_auth_mlen;
1159 swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1160 if (swa->sw_ictx == NULL)
1162 axf->Init(swa->sw_ictx);
1163 if (csp->csp_cipher_key != NULL)
1164 axf->Setkey(swa->sw_ictx, csp->csp_cipher_key,
1165 csp->csp_cipher_klen);
1167 /* Second, setup the cipher side. */
1168 return (swcr_setup_cipher(ses, csp));
1172 swcr_auth_supported(const struct crypto_session_params *csp)
1174 struct auth_hash *axf;
1176 axf = crypto_auth_hash(csp);
1179 switch (csp->csp_auth_alg) {
1180 case CRYPTO_SHA1_HMAC:
1181 case CRYPTO_SHA2_224_HMAC:
1182 case CRYPTO_SHA2_256_HMAC:
1183 case CRYPTO_SHA2_384_HMAC:
1184 case CRYPTO_SHA2_512_HMAC:
1185 case CRYPTO_NULL_HMAC:
1186 case CRYPTO_RIPEMD160_HMAC:
1188 case CRYPTO_AES_NIST_GMAC:
1189 switch (csp->csp_auth_klen * 8) {
1197 if (csp->csp_auth_key == NULL)
1199 if (csp->csp_ivlen != AES_GCM_IV_LEN)
1202 case CRYPTO_POLY1305:
1203 if (csp->csp_auth_klen != POLY1305_KEY_LEN)
1206 case CRYPTO_AES_CCM_CBC_MAC:
1207 switch (csp->csp_auth_klen * 8) {
1215 if (csp->csp_auth_key == NULL)
1217 if (csp->csp_ivlen != AES_CCM_IV_LEN)
1225 swcr_cipher_supported(const struct crypto_session_params *csp)
1227 struct enc_xform *txf;
1229 txf = crypto_cipher(csp);
1232 if (csp->csp_cipher_alg != CRYPTO_NULL_CBC &&
1233 txf->ivsize != csp->csp_ivlen)
1239 swcr_probesession(device_t dev, const struct crypto_session_params *csp)
1242 if ((csp->csp_flags & ~(CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD)) !=
1245 switch (csp->csp_mode) {
1246 case CSP_MODE_COMPRESS:
1247 switch (csp->csp_cipher_alg) {
1248 case CRYPTO_DEFLATE_COMP:
1254 case CSP_MODE_CIPHER:
1255 switch (csp->csp_cipher_alg) {
1256 case CRYPTO_AES_NIST_GCM_16:
1257 case CRYPTO_AES_CCM_16:
1260 if (!swcr_cipher_supported(csp))
1265 case CSP_MODE_DIGEST:
1266 if (!swcr_auth_supported(csp))
1270 switch (csp->csp_cipher_alg) {
1271 case CRYPTO_AES_NIST_GCM_16:
1272 case CRYPTO_AES_CCM_16:
1279 /* AEAD algorithms cannot be used for EtA. */
1280 switch (csp->csp_cipher_alg) {
1281 case CRYPTO_AES_NIST_GCM_16:
1282 case CRYPTO_AES_CCM_16:
1285 switch (csp->csp_auth_alg) {
1286 case CRYPTO_AES_NIST_GMAC:
1287 case CRYPTO_AES_CCM_CBC_MAC:
1291 if (!swcr_cipher_supported(csp) ||
1292 !swcr_auth_supported(csp))
1299 return (CRYPTODEV_PROBE_SOFTWARE);
1303 * Generate a new software session.
1306 swcr_newsession(device_t dev, crypto_session_t cses,
1307 const struct crypto_session_params *csp)
1309 struct swcr_session *ses;
1310 struct swcr_encdec *swe;
1311 struct swcr_auth *swa;
1312 struct comp_algo *cxf;
1315 ses = crypto_get_driver_session(cses);
1316 mtx_init(&ses->swcr_lock, "swcr session lock", NULL, MTX_DEF);
1319 swe = &ses->swcr_encdec;
1320 swa = &ses->swcr_auth;
1321 switch (csp->csp_mode) {
1322 case CSP_MODE_COMPRESS:
1323 switch (csp->csp_cipher_alg) {
1324 case CRYPTO_DEFLATE_COMP:
1325 cxf = &comp_algo_deflate;
1329 panic("bad compression algo");
1332 ses->swcr_compdec.sw_cxf = cxf;
1333 ses->swcr_process = swcr_compdec;
1335 case CSP_MODE_CIPHER:
1336 switch (csp->csp_cipher_alg) {
1337 case CRYPTO_NULL_CBC:
1338 ses->swcr_process = swcr_null;
1341 case CRYPTO_AES_NIST_GCM_16:
1342 case CRYPTO_AES_CCM_16:
1343 panic("bad cipher algo");
1346 error = swcr_setup_cipher(ses, csp);
1348 ses->swcr_process = swcr_encdec;
1351 case CSP_MODE_DIGEST:
1352 error = swcr_setup_auth(ses, csp);
1355 switch (csp->csp_cipher_alg) {
1356 case CRYPTO_AES_NIST_GCM_16:
1357 error = swcr_setup_gcm(ses, csp);
1359 ses->swcr_process = swcr_gcm;
1361 case CRYPTO_AES_CCM_16:
1362 error = swcr_setup_ccm(ses, csp);
1364 ses->swcr_process = swcr_ccm;
1368 panic("bad aead algo");
1374 switch (csp->csp_cipher_alg) {
1375 case CRYPTO_AES_NIST_GCM_16:
1376 case CRYPTO_AES_CCM_16:
1377 panic("bad eta cipher algo");
1379 switch (csp->csp_auth_alg) {
1380 case CRYPTO_AES_NIST_GMAC:
1381 case CRYPTO_AES_CCM_CBC_MAC:
1382 panic("bad eta auth algo");
1386 error = swcr_setup_auth(ses, csp);
1389 if (csp->csp_cipher_alg == CRYPTO_NULL_CBC) {
1390 /* Effectively degrade to digest mode. */
1391 ses->swcr_process = swcr_authcompute;
1395 error = swcr_setup_cipher(ses, csp);
1397 ses->swcr_process = swcr_eta;
1404 swcr_freesession(dev, cses);
1409 swcr_freesession(device_t dev, crypto_session_t cses)
1411 struct swcr_session *ses;
1413 ses = crypto_get_driver_session(cses);
1415 mtx_destroy(&ses->swcr_lock);
1417 zfree(ses->swcr_encdec.sw_kschedule, M_CRYPTO_DATA);
1418 zfree(ses->swcr_auth.sw_ictx, M_CRYPTO_DATA);
1419 zfree(ses->swcr_auth.sw_octx, M_CRYPTO_DATA);
1423 * Process a software request.
1426 swcr_process(device_t dev, struct cryptop *crp, int hint)
1428 struct swcr_session *ses;
1430 ses = crypto_get_driver_session(crp->crp_session);
1431 mtx_lock(&ses->swcr_lock);
1433 crp->crp_etype = ses->swcr_process(ses, crp);
1435 mtx_unlock(&ses->swcr_lock);
1441 swcr_identify(driver_t *drv, device_t parent)
1443 /* NB: order 10 is so we get attached after h/w devices */
1444 if (device_find_child(parent, "cryptosoft", -1) == NULL &&
1445 BUS_ADD_CHILD(parent, 10, "cryptosoft", 0) == 0)
1446 panic("cryptosoft: could not attach");
1450 swcr_probe(device_t dev)
1452 device_set_desc(dev, "software crypto");
1453 return (BUS_PROBE_NOWILDCARD);
1457 swcr_attach(device_t dev)
1460 swcr_id = crypto_get_driverid(dev, sizeof(struct swcr_session),
1461 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC);
1463 device_printf(dev, "cannot initialize!");
1471 swcr_detach(device_t dev)
1473 crypto_unregister_all(swcr_id);
1477 static device_method_t swcr_methods[] = {
1478 DEVMETHOD(device_identify, swcr_identify),
1479 DEVMETHOD(device_probe, swcr_probe),
1480 DEVMETHOD(device_attach, swcr_attach),
1481 DEVMETHOD(device_detach, swcr_detach),
1483 DEVMETHOD(cryptodev_probesession, swcr_probesession),
1484 DEVMETHOD(cryptodev_newsession, swcr_newsession),
1485 DEVMETHOD(cryptodev_freesession,swcr_freesession),
1486 DEVMETHOD(cryptodev_process, swcr_process),
1491 static driver_t swcr_driver = {
1494 0, /* NB: no softc */
1496 static devclass_t swcr_devclass;
1499 * NB: We explicitly reference the crypto module so we
1500 * get the necessary ordering when built as a loadable
1501 * module. This is required because we bundle the crypto
1502 * module code together with the cryptosoft driver (otherwise
1503 * normal module dependencies would handle things).
1505 extern int crypto_modevent(struct module *, int, void *);
1506 /* XXX where to attach */
1507 DRIVER_MODULE(cryptosoft, nexus, swcr_driver, swcr_devclass, crypto_modevent,0);
1508 MODULE_VERSION(cryptosoft, 1);
1509 MODULE_DEPEND(cryptosoft, crypto, 1, 1, 1);