2 * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
4 * Copyright (c) 2014 The FreeBSD Foundation
5 * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
8 * Portions of this software were developed by John-Mark Gurney
9 * under sponsorship of the FreeBSD Foundation and
10 * Rubicon Communications, LLC (Netgate).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/libkern.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
50 #include <crypto/aesni/aesni.h>
51 #include <crypto/aesni/sha_sse.h>
52 #include <crypto/sha1.h>
53 #include <crypto/sha2/sha256.h>
55 #include <opencrypto/cryptodev.h>
56 #include <opencrypto/gmac.h>
57 #include <cryptodev_if.h>
59 #include <machine/md_var.h>
60 #include <machine/specialreg.h>
62 #include <machine/npx.h>
63 #elif defined(__amd64__)
64 #include <machine/fpu.h>
67 static struct mtx_padalign *ctx_mtx;
68 static struct fpu_kern_ctx **ctx_fpu;
76 #define ACQUIRE_CTX(i, ctx) \
78 (i) = PCPU_GET(cpuid); \
79 mtx_lock(&ctx_mtx[(i)]); \
80 (ctx) = ctx_fpu[(i)]; \
82 #define RELEASE_CTX(i, ctx) \
84 mtx_unlock(&ctx_mtx[(i)]); \
89 static int aesni_newsession(device_t, crypto_session_t cses,
90 struct cryptoini *cri);
91 static int aesni_cipher_setup(struct aesni_session *ses,
92 struct cryptoini *encini, struct cryptoini *authini);
93 static int aesni_cipher_process(struct aesni_session *ses,
94 struct cryptodesc *enccrd, struct cryptodesc *authcrd, struct cryptop *crp);
95 static int aesni_cipher_crypt(struct aesni_session *ses,
96 struct cryptodesc *enccrd, struct cryptodesc *authcrd, struct cryptop *crp);
97 static int aesni_cipher_mac(struct aesni_session *ses, struct cryptodesc *crd,
100 MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
103 aesni_identify(driver_t *drv, device_t parent)
106 /* NB: order 10 is so we get attached after h/w devices */
107 if (device_find_child(parent, "aesni", -1) == NULL &&
108 BUS_ADD_CHILD(parent, 10, "aesni", -1) == 0)
109 panic("aesni: could not attach");
113 detect_cpu_features(bool *has_aes, bool *has_sha)
116 *has_aes = ((cpu_feature2 & CPUID2_AESNI) != 0 &&
117 (cpu_feature2 & CPUID2_SSE41) != 0);
118 *has_sha = ((cpu_stdext_feature & CPUID_STDEXT_SHA) != 0 &&
119 (cpu_feature2 & CPUID2_SSSE3) != 0);
123 aesni_probe(device_t dev)
125 bool has_aes, has_sha;
127 detect_cpu_features(&has_aes, &has_sha);
128 if (!has_aes && !has_sha) {
129 device_printf(dev, "No AES or SHA support.\n");
131 } else if (has_aes && has_sha)
133 "AES-CBC,AES-XTS,AES-GCM,AES-ICM,SHA1,SHA256");
135 device_set_desc(dev, "AES-CBC,AES-XTS,AES-GCM,AES-ICM");
137 device_set_desc(dev, "SHA1,SHA256");
147 /* XXX - no way to return driverid */
149 if (ctx_fpu[i] != NULL) {
150 mtx_destroy(&ctx_mtx[i]);
151 fpu_kern_free_ctx(ctx_fpu[i]);
155 free(ctx_mtx, M_AESNI);
157 free(ctx_fpu, M_AESNI);
162 aesni_attach(device_t dev)
164 struct aesni_softc *sc;
167 sc = device_get_softc(dev);
169 sc->cid = crypto_get_driverid(dev, sizeof(struct aesni_session),
170 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
172 device_printf(dev, "Could not get crypto driver id.\n");
176 ctx_mtx = malloc(sizeof *ctx_mtx * (mp_maxid + 1), M_AESNI,
178 ctx_fpu = malloc(sizeof *ctx_fpu * (mp_maxid + 1), M_AESNI,
182 ctx_fpu[i] = fpu_kern_alloc_ctx(0);
183 mtx_init(&ctx_mtx[i], "anifpumtx", NULL, MTX_DEF|MTX_NEW);
186 detect_cpu_features(&sc->has_aes, &sc->has_sha);
188 crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
189 crypto_register(sc->cid, CRYPTO_AES_ICM, 0, 0);
190 crypto_register(sc->cid, CRYPTO_AES_NIST_GCM_16, 0, 0);
191 crypto_register(sc->cid, CRYPTO_AES_128_NIST_GMAC, 0, 0);
192 crypto_register(sc->cid, CRYPTO_AES_192_NIST_GMAC, 0, 0);
193 crypto_register(sc->cid, CRYPTO_AES_256_NIST_GMAC, 0, 0);
194 crypto_register(sc->cid, CRYPTO_AES_XTS, 0, 0);
197 crypto_register(sc->cid, CRYPTO_SHA1, 0, 0);
198 crypto_register(sc->cid, CRYPTO_SHA1_HMAC, 0, 0);
199 crypto_register(sc->cid, CRYPTO_SHA2_256_HMAC, 0, 0);
205 aesni_detach(device_t dev)
207 struct aesni_softc *sc;
209 sc = device_get_softc(dev);
211 crypto_unregister_all(sc->cid);
219 aesni_newsession(device_t dev, crypto_session_t cses, struct cryptoini *cri)
221 struct aesni_softc *sc;
222 struct aesni_session *ses;
223 struct cryptoini *encini, *authini;
227 KASSERT(cses != NULL, ("EDOOFUS"));
233 sc = device_get_softc(dev);
235 ses = crypto_get_driver_session(cses);
241 for (; cri != NULL; cri = cri->cri_next) {
242 switch (cri->cri_alg) {
243 case CRYPTO_AES_NIST_GCM_16:
251 if (encini != NULL) {
252 CRYPTDEB("encini already set");
257 case CRYPTO_AES_128_NIST_GMAC:
258 case CRYPTO_AES_192_NIST_GMAC:
259 case CRYPTO_AES_256_NIST_GMAC:
261 * nothing to do here, maybe in the future cache some
267 case CRYPTO_SHA1_HMAC:
268 case CRYPTO_SHA2_256_HMAC:
271 if (authini != NULL) {
272 CRYPTDEB("authini already set");
279 CRYPTDEB("unhandled algorithm");
283 if (encini == NULL && authini == NULL) {
284 CRYPTDEB("no cipher");
288 * GMAC algorithms are only supported with simultaneous GCM. Likewise
289 * GCM is not supported without GMAC.
295 ses->algo = encini->cri_alg;
297 ses->auth_algo = authini->cri_alg;
299 error = aesni_cipher_setup(ses, encini, authini);
301 CRYPTDEB("setup failed");
309 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
311 struct aesni_softc *sc;
312 struct aesni_session *ses;
313 struct cryptodesc *crd, *enccrd, *authcrd;
316 sc = device_get_softc(dev);
327 if (crp->crp_callback == NULL || crp->crp_desc == NULL ||
328 crp->crp_session == NULL) {
333 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
334 switch (crd->crd_alg) {
335 case CRYPTO_AES_NIST_GCM_16:
341 if (enccrd != NULL) {
348 case CRYPTO_AES_128_NIST_GMAC:
349 case CRYPTO_AES_192_NIST_GMAC:
350 case CRYPTO_AES_256_NIST_GMAC:
352 case CRYPTO_SHA1_HMAC:
353 case CRYPTO_SHA2_256_HMAC:
354 if (authcrd != NULL) {
367 if ((enccrd == NULL && authcrd == NULL) ||
368 (needauth && authcrd == NULL)) {
373 /* CBC & XTS can only handle full blocks for now */
374 if (enccrd != NULL && (enccrd->crd_alg == CRYPTO_AES_CBC ||
375 enccrd->crd_alg == CRYPTO_AES_XTS) &&
376 (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
381 ses = crypto_get_driver_session(crp->crp_session);
382 KASSERT(ses != NULL, ("EDOOFUS"));
384 error = aesni_cipher_process(ses, enccrd, authcrd, crp);
389 crp->crp_etype = error;
395 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
403 if (crp->crp_flags & CRYPTO_F_IMBUF) {
404 m = (struct mbuf *)crp->crp_buf;
405 if (m->m_next != NULL)
407 addr = mtod(m, uint8_t *);
408 } else if (crp->crp_flags & CRYPTO_F_IOV) {
409 uio = (struct uio *)crp->crp_buf;
410 if (uio->uio_iovcnt != 1)
413 addr = (uint8_t *)iov->iov_base;
415 addr = (uint8_t *)crp->crp_buf;
417 addr += enccrd->crd_skip;
421 addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
424 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
425 enccrd->crd_len, addr);
431 static device_method_t aesni_methods[] = {
432 DEVMETHOD(device_identify, aesni_identify),
433 DEVMETHOD(device_probe, aesni_probe),
434 DEVMETHOD(device_attach, aesni_attach),
435 DEVMETHOD(device_detach, aesni_detach),
437 DEVMETHOD(cryptodev_newsession, aesni_newsession),
438 DEVMETHOD(cryptodev_process, aesni_process),
443 static driver_t aesni_driver = {
446 sizeof(struct aesni_softc),
448 static devclass_t aesni_devclass;
450 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
451 MODULE_VERSION(aesni, 1);
452 MODULE_DEPEND(aesni, crypto, 1, 1, 1);
455 aesni_authprepare(struct aesni_session *ses, int klen, const void *cri_key)
462 if (keylen > sizeof(ses->hmac_key))
464 if (ses->auth_algo == CRYPTO_SHA1 && keylen > 0)
466 memcpy(ses->hmac_key, cri_key, keylen);
471 aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini,
472 struct cryptoini *authini)
474 struct fpu_kern_ctx *ctx;
475 int kt, ctxidx, error;
477 switch (ses->auth_algo) {
479 case CRYPTO_SHA1_HMAC:
480 case CRYPTO_SHA2_256_HMAC:
481 error = aesni_authprepare(ses, authini->cri_klen,
485 ses->mlen = authini->cri_mlen;
488 kt = is_fpu_kern_thread(0) || (encini == NULL);
490 ACQUIRE_CTX(ctxidx, ctx);
491 fpu_kern_enter(curthread, ctx,
492 FPU_KERN_NORMAL | FPU_KERN_KTHR);
497 error = aesni_cipher_setup_common(ses, encini->cri_key,
501 fpu_kern_leave(curthread, ctx);
502 RELEASE_CTX(ctxidx, ctx);
508 intel_sha1_update(void *vctx, const void *vdata, u_int datalen)
510 struct sha1_ctxt *ctx = vctx;
511 const char *data = vdata;
519 /* Do any aligned blocks without redundant copying. */
520 if (datalen >= 64 && ctx->count % 64 == 0) {
521 blocks = datalen / 64;
522 ctx->c.b64[0] += blocks * 64 * 8;
523 intel_sha1_step(ctx->h.b32, data + off, blocks);
527 while (off < datalen) {
528 gapstart = ctx->count % 64;
529 gaplen = 64 - gapstart;
531 copysiz = (gaplen < datalen - off) ? gaplen : datalen - off;
532 bcopy(&data[off], &ctx->m.b8[gapstart], copysiz);
533 ctx->count += copysiz;
535 ctx->c.b64[0] += copysiz * 8;
536 if (ctx->count % 64 == 0)
537 intel_sha1_step(ctx->h.b32, (void *)ctx->m.b8, 1);
544 SHA1_Finalize_fn(void *digest, void *ctx)
546 sha1_result(ctx, digest);
550 intel_sha256_update(void *vctx, const void *vdata, u_int len)
552 SHA256_CTX *ctx = vctx;
556 const unsigned char *src = vdata;
558 /* Number of bytes left in the buffer from previous updates */
559 r = (ctx->count >> 3) & 0x3f;
561 /* Convert the length into a number of bits */
564 /* Update number of bits */
565 ctx->count += bitlen;
567 /* Handle the case where we don't need to perform any transforms */
569 memcpy(&ctx->buf[r], src, len);
573 /* Finish the current block */
574 memcpy(&ctx->buf[r], src, 64 - r);
575 intel_sha256_step(ctx->state, ctx->buf, 1);
579 /* Perform complete blocks */
582 intel_sha256_step(ctx->state, src, blocks);
587 /* Copy left over data into buffer */
588 memcpy(ctx->buf, src, len);
593 SHA256_Finalize_fn(void *digest, void *ctx)
595 SHA256_Final(digest, ctx);
599 * Compute the HASH( (key ^ xorbyte) || buf )
602 hmac_internal(void *ctx, uint32_t *res,
603 int (*update)(void *, const void *, u_int),
604 void (*finalize)(void *, void *), uint8_t *key, uint8_t xorbyte,
605 const void *buf, size_t off, size_t buflen, int crpflags)
609 for (i = 0; i < 64; i++)
611 update(ctx, key, 64);
612 for (i = 0; i < 64; i++)
615 crypto_apply(crpflags, __DECONST(void *, buf), off, buflen,
616 __DECONST(int (*)(void *, void *, u_int), update), ctx);
621 aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
622 struct cryptodesc *authcrd, struct cryptop *crp)
624 struct fpu_kern_ctx *ctx;
628 if (enccrd != NULL) {
629 if ((enccrd->crd_alg == CRYPTO_AES_ICM ||
630 enccrd->crd_alg == CRYPTO_AES_NIST_GCM_16) &&
631 (enccrd->crd_flags & CRD_F_IV_EXPLICIT) == 0)
638 kt = is_fpu_kern_thread(0);
640 ACQUIRE_CTX(ctxidx, ctx);
641 fpu_kern_enter(curthread, ctx,
642 FPU_KERN_NORMAL | FPU_KERN_KTHR);
646 if (enccrd != NULL && authcrd != NULL) {
647 /* Perform the first operation */
648 if (crp->crp_desc == enccrd)
649 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
651 error = aesni_cipher_mac(ses, authcrd, crp);
654 /* Perform the second operation */
655 if (crp->crp_desc == enccrd)
656 error = aesni_cipher_mac(ses, authcrd, crp);
658 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
659 } else if (enccrd != NULL)
660 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
662 error = aesni_cipher_mac(ses, authcrd, crp);
669 fpu_kern_leave(curthread, ctx);
670 RELEASE_CTX(ctxidx, ctx);
676 aesni_cipher_crypt(struct aesni_session *ses, struct cryptodesc *enccrd,
677 struct cryptodesc *authcrd, struct cryptop *crp)
679 uint8_t iv[AES_BLOCK_LEN], tag[GMAC_DIGEST_LEN], *buf, *authbuf;
681 bool encflag, allocated, authallocated;
683 KASSERT(ses->algo != CRYPTO_AES_NIST_GCM_16 || authcrd != NULL,
684 ("AES_NIST_GCM_16 must include MAC descriptor"));
689 buf = aesni_cipher_alloc(enccrd, crp, &allocated);
693 authallocated = false;
694 if (ses->algo == CRYPTO_AES_NIST_GCM_16) {
695 authbuf = aesni_cipher_alloc(authcrd, crp, &authallocated);
696 if (authbuf == NULL) {
703 encflag = (enccrd->crd_flags & CRD_F_ENCRYPT) == CRD_F_ENCRYPT;
704 if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
705 error = aesni_cipher_setup_common(ses, enccrd->crd_key,
711 switch (enccrd->crd_alg) {
714 ivlen = AES_BLOCK_LEN;
719 case CRYPTO_AES_NIST_GCM_16:
720 ivlen = 12; /* should support arbitarily larger */
726 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
727 bcopy(enccrd->crd_iv, iv, ivlen);
729 arc4rand(iv, ivlen, 0);
731 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
732 crypto_copyback(crp->crp_flags, crp->crp_buf,
733 enccrd->crd_inject, ivlen, iv);
735 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
736 bcopy(enccrd->crd_iv, iv, ivlen);
738 crypto_copydata(crp->crp_flags, crp->crp_buf,
739 enccrd->crd_inject, ivlen, iv);
745 aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
746 enccrd->crd_len, buf, buf, iv);
748 aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
749 enccrd->crd_len, buf, iv);
752 /* encryption & decryption are the same */
753 aesni_encrypt_icm(ses->rounds, ses->enc_schedule,
754 enccrd->crd_len, buf, buf, iv);
758 aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
759 ses->xts_schedule, enccrd->crd_len, buf, buf,
762 aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
763 ses->xts_schedule, enccrd->crd_len, buf, buf,
766 case CRYPTO_AES_NIST_GCM_16:
768 crypto_copydata(crp->crp_flags, crp->crp_buf,
769 authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
771 bzero(tag, sizeof tag);
774 AES_GCM_encrypt(buf, buf, authbuf, iv, tag,
775 enccrd->crd_len, authcrd->crd_len, ivlen,
776 ses->enc_schedule, ses->rounds);
779 crypto_copyback(crp->crp_flags, crp->crp_buf,
780 authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
782 if (!AES_GCM_decrypt(buf, buf, authbuf, iv, tag,
783 enccrd->crd_len, authcrd->crd_len, ivlen,
784 ses->enc_schedule, ses->rounds))
791 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
792 enccrd->crd_len, buf);
796 explicit_bzero(buf, enccrd->crd_len);
800 explicit_bzero(authbuf, authcrd->crd_len);
801 free(authbuf, M_AESNI);
807 aesni_cipher_mac(struct aesni_session *ses, struct cryptodesc *crd,
811 struct SHA256Context sha2 __aligned(16);
812 struct sha1_ctxt sha1 __aligned(16);
814 uint32_t res[SHA2_256_HASH_LEN / sizeof(uint32_t)];
817 if ((crd->crd_flags & ~CRD_F_KEY_EXPLICIT) != 0) {
818 CRYPTDEB("%s: Unsupported MAC flags: 0x%x", __func__,
819 (crd->crd_flags & ~CRD_F_KEY_EXPLICIT));
822 if ((crd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
823 error = aesni_authprepare(ses, crd->crd_klen, crd->crd_key);
828 switch (ses->auth_algo) {
829 case CRYPTO_SHA1_HMAC:
830 hashlen = SHA1_HASH_LEN;
831 /* Inner hash: (K ^ IPAD) || data */
832 sha1_init(&sctx.sha1);
833 hmac_internal(&sctx.sha1, res, intel_sha1_update,
834 SHA1_Finalize_fn, ses->hmac_key, 0x36, crp->crp_buf,
835 crd->crd_skip, crd->crd_len, crp->crp_flags);
836 /* Outer hash: (K ^ OPAD) || inner hash */
837 sha1_init(&sctx.sha1);
838 hmac_internal(&sctx.sha1, res, intel_sha1_update,
839 SHA1_Finalize_fn, ses->hmac_key, 0x5C, res, 0, hashlen, 0);
842 hashlen = SHA1_HASH_LEN;
843 sha1_init(&sctx.sha1);
844 crypto_apply(crp->crp_flags, crp->crp_buf, crd->crd_skip,
845 crd->crd_len, __DECONST(int (*)(void *, void *, u_int),
846 intel_sha1_update), &sctx.sha1);
847 sha1_result(&sctx.sha1, (void *)res);
849 case CRYPTO_SHA2_256_HMAC:
850 hashlen = SHA2_256_HASH_LEN;
851 /* Inner hash: (K ^ IPAD) || data */
852 SHA256_Init(&sctx.sha2);
853 hmac_internal(&sctx.sha2, res, intel_sha256_update,
854 SHA256_Finalize_fn, ses->hmac_key, 0x36, crp->crp_buf,
855 crd->crd_skip, crd->crd_len, crp->crp_flags);
856 /* Outer hash: (K ^ OPAD) || inner hash */
857 SHA256_Init(&sctx.sha2);
858 hmac_internal(&sctx.sha2, res, intel_sha256_update,
859 SHA256_Finalize_fn, ses->hmac_key, 0x5C, res, 0, hashlen,
864 * AES-GMAC authentication is verified while processing the
870 if (ses->mlen != 0 && ses->mlen < hashlen)
873 crypto_copyback(crp->crp_flags, crp->crp_buf, crd->crd_inject, hashlen,