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/sha224.h>
54 #include <crypto/sha2/sha256.h>
56 #include <opencrypto/cryptodev.h>
57 #include <opencrypto/gmac.h>
58 #include <cryptodev_if.h>
60 #include <machine/md_var.h>
61 #include <machine/specialreg.h>
63 #include <machine/npx.h>
64 #elif defined(__amd64__)
65 #include <machine/fpu.h>
68 static struct mtx_padalign *ctx_mtx;
69 static struct fpu_kern_ctx **ctx_fpu;
77 #define ACQUIRE_CTX(i, ctx) \
79 (i) = PCPU_GET(cpuid); \
80 mtx_lock(&ctx_mtx[(i)]); \
81 (ctx) = ctx_fpu[(i)]; \
83 #define RELEASE_CTX(i, ctx) \
85 mtx_unlock(&ctx_mtx[(i)]); \
90 static int aesni_newsession(device_t, crypto_session_t cses,
91 struct cryptoini *cri);
92 static int aesni_cipher_setup(struct aesni_session *ses,
93 struct cryptoini *encini, struct cryptoini *authini);
94 static int aesni_cipher_process(struct aesni_session *ses,
95 struct cryptodesc *enccrd, struct cryptodesc *authcrd, struct cryptop *crp);
96 static int aesni_cipher_crypt(struct aesni_session *ses,
97 struct cryptodesc *enccrd, struct cryptodesc *authcrd, struct cryptop *crp);
98 static int aesni_cipher_mac(struct aesni_session *ses, struct cryptodesc *crd,
101 MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
104 aesni_identify(driver_t *drv, device_t parent)
107 /* NB: order 10 is so we get attached after h/w devices */
108 if (device_find_child(parent, "aesni", -1) == NULL &&
109 BUS_ADD_CHILD(parent, 10, "aesni", -1) == 0)
110 panic("aesni: could not attach");
114 detect_cpu_features(bool *has_aes, bool *has_sha)
117 *has_aes = ((cpu_feature2 & CPUID2_AESNI) != 0 &&
118 (cpu_feature2 & CPUID2_SSE41) != 0);
119 *has_sha = ((cpu_stdext_feature & CPUID_STDEXT_SHA) != 0 &&
120 (cpu_feature2 & CPUID2_SSSE3) != 0);
124 aesni_probe(device_t dev)
126 bool has_aes, has_sha;
128 detect_cpu_features(&has_aes, &has_sha);
129 if (!has_aes && !has_sha) {
130 device_printf(dev, "No AES or SHA support.\n");
132 } else if (has_aes && has_sha)
134 "AES-CBC,AES-XTS,AES-GCM,AES-ICM,SHA1,SHA256");
136 device_set_desc(dev, "AES-CBC,AES-XTS,AES-GCM,AES-ICM");
138 device_set_desc(dev, "SHA1,SHA256");
148 /* XXX - no way to return driverid */
150 if (ctx_fpu[i] != NULL) {
151 mtx_destroy(&ctx_mtx[i]);
152 fpu_kern_free_ctx(ctx_fpu[i]);
156 free(ctx_mtx, M_AESNI);
158 free(ctx_fpu, M_AESNI);
163 aesni_attach(device_t dev)
165 struct aesni_softc *sc;
168 sc = device_get_softc(dev);
170 sc->cid = crypto_get_driverid(dev, sizeof(struct aesni_session),
171 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
173 device_printf(dev, "Could not get crypto driver id.\n");
177 ctx_mtx = malloc(sizeof *ctx_mtx * (mp_maxid + 1), M_AESNI,
179 ctx_fpu = malloc(sizeof *ctx_fpu * (mp_maxid + 1), M_AESNI,
183 ctx_fpu[i] = fpu_kern_alloc_ctx(0);
184 mtx_init(&ctx_mtx[i], "anifpumtx", NULL, MTX_DEF|MTX_NEW);
187 detect_cpu_features(&sc->has_aes, &sc->has_sha);
189 crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
190 crypto_register(sc->cid, CRYPTO_AES_ICM, 0, 0);
191 crypto_register(sc->cid, CRYPTO_AES_NIST_GCM_16, 0, 0);
192 crypto_register(sc->cid, CRYPTO_AES_128_NIST_GMAC, 0, 0);
193 crypto_register(sc->cid, CRYPTO_AES_192_NIST_GMAC, 0, 0);
194 crypto_register(sc->cid, CRYPTO_AES_256_NIST_GMAC, 0, 0);
195 crypto_register(sc->cid, CRYPTO_AES_XTS, 0, 0);
198 crypto_register(sc->cid, CRYPTO_SHA1, 0, 0);
199 crypto_register(sc->cid, CRYPTO_SHA1_HMAC, 0, 0);
200 crypto_register(sc->cid, CRYPTO_SHA2_224, 0, 0);
201 crypto_register(sc->cid, CRYPTO_SHA2_224_HMAC, 0, 0);
202 crypto_register(sc->cid, CRYPTO_SHA2_256, 0, 0);
203 crypto_register(sc->cid, CRYPTO_SHA2_256_HMAC, 0, 0);
209 aesni_detach(device_t dev)
211 struct aesni_softc *sc;
213 sc = device_get_softc(dev);
215 crypto_unregister_all(sc->cid);
223 aesni_newsession(device_t dev, crypto_session_t cses, struct cryptoini *cri)
225 struct aesni_softc *sc;
226 struct aesni_session *ses;
227 struct cryptoini *encini, *authini;
231 KASSERT(cses != NULL, ("EDOOFUS"));
237 sc = device_get_softc(dev);
239 ses = crypto_get_driver_session(cses);
245 for (; cri != NULL; cri = cri->cri_next) {
246 switch (cri->cri_alg) {
247 case CRYPTO_AES_NIST_GCM_16:
255 if (encini != NULL) {
256 CRYPTDEB("encini already set");
261 case CRYPTO_AES_128_NIST_GMAC:
262 case CRYPTO_AES_192_NIST_GMAC:
263 case CRYPTO_AES_256_NIST_GMAC:
265 * nothing to do here, maybe in the future cache some
271 case CRYPTO_SHA1_HMAC:
272 case CRYPTO_SHA2_224:
273 case CRYPTO_SHA2_224_HMAC:
274 case CRYPTO_SHA2_256:
275 case CRYPTO_SHA2_256_HMAC:
278 if (authini != NULL) {
279 CRYPTDEB("authini already set");
286 CRYPTDEB("unhandled algorithm");
290 if (encini == NULL && authini == NULL) {
291 CRYPTDEB("no cipher");
295 * GMAC algorithms are only supported with simultaneous GCM. Likewise
296 * GCM is not supported without GMAC.
302 ses->algo = encini->cri_alg;
304 ses->auth_algo = authini->cri_alg;
306 error = aesni_cipher_setup(ses, encini, authini);
308 CRYPTDEB("setup failed");
316 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
318 struct aesni_session *ses;
319 struct cryptodesc *crd, *enccrd, *authcrd;
332 if (crp->crp_callback == NULL || crp->crp_desc == NULL ||
333 crp->crp_session == NULL) {
338 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
339 switch (crd->crd_alg) {
340 case CRYPTO_AES_NIST_GCM_16:
346 if (enccrd != NULL) {
353 case CRYPTO_AES_128_NIST_GMAC:
354 case CRYPTO_AES_192_NIST_GMAC:
355 case CRYPTO_AES_256_NIST_GMAC:
357 case CRYPTO_SHA1_HMAC:
358 case CRYPTO_SHA2_224:
359 case CRYPTO_SHA2_224_HMAC:
360 case CRYPTO_SHA2_256:
361 case CRYPTO_SHA2_256_HMAC:
362 if (authcrd != NULL) {
375 if ((enccrd == NULL && authcrd == NULL) ||
376 (needauth && authcrd == NULL)) {
381 /* CBC & XTS can only handle full blocks for now */
382 if (enccrd != NULL && (enccrd->crd_alg == CRYPTO_AES_CBC ||
383 enccrd->crd_alg == CRYPTO_AES_XTS) &&
384 (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
389 ses = crypto_get_driver_session(crp->crp_session);
390 KASSERT(ses != NULL, ("EDOOFUS"));
392 error = aesni_cipher_process(ses, enccrd, authcrd, crp);
397 crp->crp_etype = error;
403 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
408 addr = crypto_contiguous_subsegment(crp->crp_flags,
409 crp->crp_buf, enccrd->crd_skip, enccrd->crd_len);
414 addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
417 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
418 enccrd->crd_len, addr);
424 static device_method_t aesni_methods[] = {
425 DEVMETHOD(device_identify, aesni_identify),
426 DEVMETHOD(device_probe, aesni_probe),
427 DEVMETHOD(device_attach, aesni_attach),
428 DEVMETHOD(device_detach, aesni_detach),
430 DEVMETHOD(cryptodev_newsession, aesni_newsession),
431 DEVMETHOD(cryptodev_process, aesni_process),
436 static driver_t aesni_driver = {
439 sizeof(struct aesni_softc),
441 static devclass_t aesni_devclass;
443 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
444 MODULE_VERSION(aesni, 1);
445 MODULE_DEPEND(aesni, crypto, 1, 1, 1);
448 aesni_authprepare(struct aesni_session *ses, int klen, const void *cri_key)
455 if (keylen > sizeof(ses->hmac_key))
457 if (ses->auth_algo == CRYPTO_SHA1 && keylen > 0)
459 memcpy(ses->hmac_key, cri_key, keylen);
464 aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini,
465 struct cryptoini *authini)
467 struct fpu_kern_ctx *ctx;
468 int kt, ctxidx, error;
470 switch (ses->auth_algo) {
472 case CRYPTO_SHA1_HMAC:
473 case CRYPTO_SHA2_224:
474 case CRYPTO_SHA2_224_HMAC:
475 case CRYPTO_SHA2_256:
476 case CRYPTO_SHA2_256_HMAC:
477 error = aesni_authprepare(ses, authini->cri_klen,
481 ses->mlen = authini->cri_mlen;
484 kt = is_fpu_kern_thread(0) || (encini == NULL);
486 ACQUIRE_CTX(ctxidx, ctx);
487 fpu_kern_enter(curthread, ctx,
488 FPU_KERN_NORMAL | FPU_KERN_KTHR);
493 error = aesni_cipher_setup_common(ses, encini->cri_key,
497 fpu_kern_leave(curthread, ctx);
498 RELEASE_CTX(ctxidx, ctx);
504 intel_sha1_update(void *vctx, const void *vdata, u_int datalen)
506 struct sha1_ctxt *ctx = vctx;
507 const char *data = vdata;
515 /* Do any aligned blocks without redundant copying. */
516 if (datalen >= 64 && ctx->count % 64 == 0) {
517 blocks = datalen / 64;
518 ctx->c.b64[0] += blocks * 64 * 8;
519 intel_sha1_step(ctx->h.b32, data + off, blocks);
523 while (off < datalen) {
524 gapstart = ctx->count % 64;
525 gaplen = 64 - gapstart;
527 copysiz = (gaplen < datalen - off) ? gaplen : datalen - off;
528 bcopy(&data[off], &ctx->m.b8[gapstart], copysiz);
529 ctx->count += copysiz;
531 ctx->c.b64[0] += copysiz * 8;
532 if (ctx->count % 64 == 0)
533 intel_sha1_step(ctx->h.b32, (void *)ctx->m.b8, 1);
540 SHA1_Init_fn(void *ctx)
546 SHA1_Finalize_fn(void *digest, void *ctx)
548 sha1_result(ctx, digest);
552 intel_sha256_update(void *vctx, const void *vdata, u_int len)
554 SHA256_CTX *ctx = vctx;
558 const unsigned char *src = vdata;
560 /* Number of bytes left in the buffer from previous updates */
561 r = (ctx->count >> 3) & 0x3f;
563 /* Convert the length into a number of bits */
566 /* Update number of bits */
567 ctx->count += bitlen;
569 /* Handle the case where we don't need to perform any transforms */
571 memcpy(&ctx->buf[r], src, len);
575 /* Finish the current block */
576 memcpy(&ctx->buf[r], src, 64 - r);
577 intel_sha256_step(ctx->state, ctx->buf, 1);
581 /* Perform complete blocks */
584 intel_sha256_step(ctx->state, src, blocks);
589 /* Copy left over data into buffer */
590 memcpy(ctx->buf, src, len);
595 SHA224_Init_fn(void *ctx)
601 SHA224_Finalize_fn(void *digest, void *ctx)
603 SHA224_Final(digest, ctx);
607 SHA256_Init_fn(void *ctx)
613 SHA256_Finalize_fn(void *digest, void *ctx)
615 SHA256_Final(digest, ctx);
619 * Compute the HASH( (key ^ xorbyte) || buf )
622 hmac_internal(void *ctx, uint32_t *res,
623 int (*update)(void *, const void *, u_int),
624 void (*finalize)(void *, void *), uint8_t *key, uint8_t xorbyte,
625 const void *buf, size_t off, size_t buflen, int crpflags)
629 for (i = 0; i < 64; i++)
631 update(ctx, key, 64);
632 for (i = 0; i < 64; i++)
635 crypto_apply(crpflags, __DECONST(void *, buf), off, buflen,
636 __DECONST(int (*)(void *, void *, u_int), update), ctx);
641 aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
642 struct cryptodesc *authcrd, struct cryptop *crp)
644 struct fpu_kern_ctx *ctx;
648 if (enccrd != NULL) {
649 if ((enccrd->crd_alg == CRYPTO_AES_ICM ||
650 enccrd->crd_alg == CRYPTO_AES_NIST_GCM_16) &&
651 (enccrd->crd_flags & CRD_F_IV_EXPLICIT) == 0)
658 kt = is_fpu_kern_thread(0);
660 ACQUIRE_CTX(ctxidx, ctx);
661 fpu_kern_enter(curthread, ctx,
662 FPU_KERN_NORMAL | FPU_KERN_KTHR);
666 if (enccrd != NULL && authcrd != NULL) {
667 /* Perform the first operation */
668 if (crp->crp_desc == enccrd)
669 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
671 error = aesni_cipher_mac(ses, authcrd, crp);
674 /* Perform the second operation */
675 if (crp->crp_desc == enccrd)
676 error = aesni_cipher_mac(ses, authcrd, crp);
678 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
679 } else if (enccrd != NULL)
680 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
682 error = aesni_cipher_mac(ses, authcrd, crp);
689 fpu_kern_leave(curthread, ctx);
690 RELEASE_CTX(ctxidx, ctx);
696 aesni_cipher_crypt(struct aesni_session *ses, struct cryptodesc *enccrd,
697 struct cryptodesc *authcrd, struct cryptop *crp)
699 uint8_t iv[AES_BLOCK_LEN], tag[GMAC_DIGEST_LEN], *buf, *authbuf;
701 bool encflag, allocated, authallocated;
703 KASSERT(ses->algo != CRYPTO_AES_NIST_GCM_16 || authcrd != NULL,
704 ("AES_NIST_GCM_16 must include MAC descriptor"));
709 buf = aesni_cipher_alloc(enccrd, crp, &allocated);
713 authallocated = false;
714 if (ses->algo == CRYPTO_AES_NIST_GCM_16) {
715 authbuf = aesni_cipher_alloc(authcrd, crp, &authallocated);
716 if (authbuf == NULL) {
723 encflag = (enccrd->crd_flags & CRD_F_ENCRYPT) == CRD_F_ENCRYPT;
724 if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
725 error = aesni_cipher_setup_common(ses, enccrd->crd_key,
731 switch (enccrd->crd_alg) {
734 ivlen = AES_BLOCK_LEN;
739 case CRYPTO_AES_NIST_GCM_16:
740 ivlen = 12; /* should support arbitarily larger */
746 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
747 bcopy(enccrd->crd_iv, iv, ivlen);
749 arc4rand(iv, ivlen, 0);
751 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
752 crypto_copyback(crp->crp_flags, crp->crp_buf,
753 enccrd->crd_inject, ivlen, iv);
755 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
756 bcopy(enccrd->crd_iv, iv, ivlen);
758 crypto_copydata(crp->crp_flags, crp->crp_buf,
759 enccrd->crd_inject, ivlen, iv);
765 aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
766 enccrd->crd_len, buf, buf, iv);
768 aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
769 enccrd->crd_len, buf, iv);
772 /* encryption & decryption are the same */
773 aesni_encrypt_icm(ses->rounds, ses->enc_schedule,
774 enccrd->crd_len, buf, buf, iv);
778 aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
779 ses->xts_schedule, enccrd->crd_len, buf, buf,
782 aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
783 ses->xts_schedule, enccrd->crd_len, buf, buf,
786 case CRYPTO_AES_NIST_GCM_16:
788 crypto_copydata(crp->crp_flags, crp->crp_buf,
789 authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
791 bzero(tag, sizeof tag);
794 AES_GCM_encrypt(buf, buf, authbuf, iv, tag,
795 enccrd->crd_len, authcrd->crd_len, ivlen,
796 ses->enc_schedule, ses->rounds);
799 crypto_copyback(crp->crp_flags, crp->crp_buf,
800 authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
802 if (!AES_GCM_decrypt(buf, buf, authbuf, iv, tag,
803 enccrd->crd_len, authcrd->crd_len, ivlen,
804 ses->enc_schedule, ses->rounds))
811 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
812 enccrd->crd_len, buf);
816 explicit_bzero(buf, enccrd->crd_len);
820 explicit_bzero(authbuf, authcrd->crd_len);
821 free(authbuf, M_AESNI);
827 aesni_cipher_mac(struct aesni_session *ses, struct cryptodesc *crd,
831 struct SHA256Context sha2 __aligned(16);
832 struct sha1_ctxt sha1 __aligned(16);
834 uint32_t res[SHA2_256_HASH_LEN / sizeof(uint32_t)];
837 void (*InitFn)(void *);
838 int (*UpdateFn)(void *, const void *, unsigned);
839 void (*FinalizeFn)(void *, void *);
843 if ((crd->crd_flags & ~CRD_F_KEY_EXPLICIT) != 0) {
844 CRYPTDEB("%s: Unsupported MAC flags: 0x%x", __func__,
845 (crd->crd_flags & ~CRD_F_KEY_EXPLICIT));
848 if ((crd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
849 error = aesni_authprepare(ses, crd->crd_klen, crd->crd_key);
855 switch (ses->auth_algo) {
856 case CRYPTO_SHA1_HMAC:
860 hashlen = SHA1_HASH_LEN;
861 InitFn = SHA1_Init_fn;
862 UpdateFn = intel_sha1_update;
863 FinalizeFn = SHA1_Finalize_fn;
867 case CRYPTO_SHA2_256_HMAC:
870 case CRYPTO_SHA2_256:
871 hashlen = SHA2_256_HASH_LEN;
872 InitFn = SHA256_Init_fn;
873 UpdateFn = intel_sha256_update;
874 FinalizeFn = SHA256_Finalize_fn;
878 case CRYPTO_SHA2_224_HMAC:
881 case CRYPTO_SHA2_224:
882 hashlen = SHA2_224_HASH_LEN;
883 InitFn = SHA224_Init_fn;
884 UpdateFn = intel_sha256_update;
885 FinalizeFn = SHA224_Finalize_fn;
890 * AES-GMAC authentication is verified while processing the
897 /* Inner hash: (K ^ IPAD) || data */
899 hmac_internal(ctx, res, UpdateFn, FinalizeFn, ses->hmac_key,
900 0x36, crp->crp_buf, crd->crd_skip, crd->crd_len,
902 /* Outer hash: (K ^ OPAD) || inner hash */
904 hmac_internal(ctx, res, UpdateFn, FinalizeFn, ses->hmac_key,
905 0x5C, res, 0, hashlen, 0);
908 crypto_apply(crp->crp_flags, crp->crp_buf, crd->crd_skip,
909 crd->crd_len, __DECONST(int (*)(void *, void *, u_int),
911 FinalizeFn(res, ctx);
914 if (ses->mlen != 0 && ses->mlen < hashlen)
917 crypto_copyback(crp->crp_flags, crp->crp_buf, crd->crd_inject, hashlen,