]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/crypto/aesni/aesni.c
Copy libevent sources to contrib
[FreeBSD/FreeBSD.git] / sys / crypto / aesni / aesni.c
1 /*-
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>
6  * All rights reserved.
7  *
8  * Portions of this software were developed by John-Mark Gurney
9  * under sponsorship of the FreeBSD Foundation and
10  * Rubicon Communications, LLC (Netgate).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/kobj.h>
41 #include <sys/libkern.h>
42 #include <sys/lock.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
45 #include <sys/bus.h>
46 #include <sys/uio.h>
47 #include <sys/mbuf.h>
48 #include <sys/smp.h>
49
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>
55
56 #include <opencrypto/cryptodev.h>
57 #include <opencrypto/gmac.h>
58 #include <cryptodev_if.h>
59
60 #include <machine/md_var.h>
61 #include <machine/specialreg.h>
62 #if defined(__i386__)
63 #include <machine/npx.h>
64 #elif defined(__amd64__)
65 #include <machine/fpu.h>
66 #endif
67
68 static struct mtx_padalign *ctx_mtx;
69 static struct fpu_kern_ctx **ctx_fpu;
70
71 struct aesni_softc {
72         int32_t cid;
73         bool    has_aes;
74         bool    has_sha;
75 };
76
77 #define ACQUIRE_CTX(i, ctx)                                     \
78         do {                                                    \
79                 (i) = PCPU_GET(cpuid);                          \
80                 mtx_lock(&ctx_mtx[(i)]);                        \
81                 (ctx) = ctx_fpu[(i)];                           \
82         } while (0)
83 #define RELEASE_CTX(i, ctx)                                     \
84         do {                                                    \
85                 mtx_unlock(&ctx_mtx[(i)]);                      \
86                 (i) = -1;                                       \
87                 (ctx) = NULL;                                   \
88         } while (0)
89
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,
99     struct cryptop *crp);
100
101 MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
102
103 static void
104 aesni_identify(driver_t *drv, device_t parent)
105 {
106
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");
111 }
112
113 static void
114 detect_cpu_features(bool *has_aes, bool *has_sha)
115 {
116
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);
121 }
122
123 static int
124 aesni_probe(device_t dev)
125 {
126         bool has_aes, has_sha;
127
128         detect_cpu_features(&has_aes, &has_sha);
129         if (!has_aes && !has_sha) {
130                 device_printf(dev, "No AES or SHA support.\n");
131                 return (EINVAL);
132         } else if (has_aes && has_sha)
133                 device_set_desc(dev,
134                     "AES-CBC,AES-XTS,AES-GCM,AES-ICM,SHA1,SHA256");
135         else if (has_aes)
136                 device_set_desc(dev, "AES-CBC,AES-XTS,AES-GCM,AES-ICM");
137         else
138                 device_set_desc(dev, "SHA1,SHA256");
139
140         return (0);
141 }
142
143 static void
144 aesni_cleanctx(void)
145 {
146         int i;
147
148         /* XXX - no way to return driverid */
149         CPU_FOREACH(i) {
150                 if (ctx_fpu[i] != NULL) {
151                         mtx_destroy(&ctx_mtx[i]);
152                         fpu_kern_free_ctx(ctx_fpu[i]);
153                 }
154                 ctx_fpu[i] = NULL;
155         }
156         free(ctx_mtx, M_AESNI);
157         ctx_mtx = NULL;
158         free(ctx_fpu, M_AESNI);
159         ctx_fpu = NULL;
160 }
161
162 static int
163 aesni_attach(device_t dev)
164 {
165         struct aesni_softc *sc;
166         int i;
167
168         sc = device_get_softc(dev);
169
170         sc->cid = crypto_get_driverid(dev, sizeof(struct aesni_session),
171             CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
172         if (sc->cid < 0) {
173                 device_printf(dev, "Could not get crypto driver id.\n");
174                 return (ENOMEM);
175         }
176
177         ctx_mtx = malloc(sizeof *ctx_mtx * (mp_maxid + 1), M_AESNI,
178             M_WAITOK|M_ZERO);
179         ctx_fpu = malloc(sizeof *ctx_fpu * (mp_maxid + 1), M_AESNI,
180             M_WAITOK|M_ZERO);
181
182         CPU_FOREACH(i) {
183                 ctx_fpu[i] = fpu_kern_alloc_ctx(0);
184                 mtx_init(&ctx_mtx[i], "anifpumtx", NULL, MTX_DEF|MTX_NEW);
185         }
186
187         detect_cpu_features(&sc->has_aes, &sc->has_sha);
188         if (sc->has_aes) {
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);
196         }
197         if (sc->has_sha) {
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);
204         }
205         return (0);
206 }
207
208 static int
209 aesni_detach(device_t dev)
210 {
211         struct aesni_softc *sc;
212
213         sc = device_get_softc(dev);
214
215         crypto_unregister_all(sc->cid);
216
217         aesni_cleanctx();
218
219         return (0);
220 }
221
222 static int
223 aesni_newsession(device_t dev, crypto_session_t cses, struct cryptoini *cri)
224 {
225         struct aesni_softc *sc;
226         struct aesni_session *ses;
227         struct cryptoini *encini, *authini;
228         bool gcm_hash, gcm;
229         int error;
230
231         KASSERT(cses != NULL, ("EDOOFUS"));
232         if (cri == NULL) {
233                 CRYPTDEB("no cri");
234                 return (EINVAL);
235         }
236
237         sc = device_get_softc(dev);
238
239         ses = crypto_get_driver_session(cses);
240
241         authini = NULL;
242         encini = NULL;
243         gcm = false;
244         gcm_hash = false;
245         for (; cri != NULL; cri = cri->cri_next) {
246                 switch (cri->cri_alg) {
247                 case CRYPTO_AES_NIST_GCM_16:
248                         gcm = true;
249                         /* FALLTHROUGH */
250                 case CRYPTO_AES_CBC:
251                 case CRYPTO_AES_ICM:
252                 case CRYPTO_AES_XTS:
253                         if (!sc->has_aes)
254                                 goto unhandled;
255                         if (encini != NULL) {
256                                 CRYPTDEB("encini already set");
257                                 return (EINVAL);
258                         }
259                         encini = cri;
260                         break;
261                 case CRYPTO_AES_128_NIST_GMAC:
262                 case CRYPTO_AES_192_NIST_GMAC:
263                 case CRYPTO_AES_256_NIST_GMAC:
264                         /*
265                          * nothing to do here, maybe in the future cache some
266                          * values for GHASH
267                          */
268                         gcm_hash = true;
269                         break;
270                 case CRYPTO_SHA1:
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:
276                         if (!sc->has_sha)
277                                 goto unhandled;
278                         if (authini != NULL) {
279                                 CRYPTDEB("authini already set");
280                                 return (EINVAL);
281                         }
282                         authini = cri;
283                         break;
284                 default:
285 unhandled:
286                         CRYPTDEB("unhandled algorithm");
287                         return (EINVAL);
288                 }
289         }
290         if (encini == NULL && authini == NULL) {
291                 CRYPTDEB("no cipher");
292                 return (EINVAL);
293         }
294         /*
295          * GMAC algorithms are only supported with simultaneous GCM.  Likewise
296          * GCM is not supported without GMAC.
297          */
298         if (gcm_hash != gcm)
299                 return (EINVAL);
300
301         if (encini != NULL)
302                 ses->algo = encini->cri_alg;
303         if (authini != NULL)
304                 ses->auth_algo = authini->cri_alg;
305
306         error = aesni_cipher_setup(ses, encini, authini);
307         if (error != 0) {
308                 CRYPTDEB("setup failed");
309                 return (error);
310         }
311
312         return (0);
313 }
314
315 static int
316 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
317 {
318         struct aesni_session *ses;
319         struct cryptodesc *crd, *enccrd, *authcrd;
320         int error, needauth;
321
322         ses = NULL;
323         error = 0;
324         enccrd = NULL;
325         authcrd = NULL;
326         needauth = 0;
327
328         /* Sanity check. */
329         if (crp == NULL)
330                 return (EINVAL);
331
332         if (crp->crp_callback == NULL || crp->crp_desc == NULL ||
333             crp->crp_session == NULL) {
334                 error = EINVAL;
335                 goto out;
336         }
337
338         for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
339                 switch (crd->crd_alg) {
340                 case CRYPTO_AES_NIST_GCM_16:
341                         needauth = 1;
342                         /* FALLTHROUGH */
343                 case CRYPTO_AES_CBC:
344                 case CRYPTO_AES_ICM:
345                 case CRYPTO_AES_XTS:
346                         if (enccrd != NULL) {
347                                 error = EINVAL;
348                                 goto out;
349                         }
350                         enccrd = crd;
351                         break;
352
353                 case CRYPTO_AES_128_NIST_GMAC:
354                 case CRYPTO_AES_192_NIST_GMAC:
355                 case CRYPTO_AES_256_NIST_GMAC:
356                 case CRYPTO_SHA1:
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) {
363                                 error = EINVAL;
364                                 goto out;
365                         }
366                         authcrd = crd;
367                         break;
368
369                 default:
370                         error = EINVAL;
371                         goto out;
372                 }
373         }
374
375         if ((enccrd == NULL && authcrd == NULL) ||
376             (needauth && authcrd == NULL)) {
377                 error = EINVAL;
378                 goto out;
379         }
380
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) {
385                 error = EINVAL;
386                 goto out;
387         }
388
389         ses = crypto_get_driver_session(crp->crp_session);
390         KASSERT(ses != NULL, ("EDOOFUS"));
391
392         error = aesni_cipher_process(ses, enccrd, authcrd, crp);
393         if (error != 0)
394                 goto out;
395
396 out:
397         crp->crp_etype = error;
398         crypto_done(crp);
399         return (error);
400 }
401
402 static uint8_t *
403 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
404     bool *allocated)
405 {
406         struct mbuf *m;
407         struct uio *uio;
408         struct iovec *iov;
409         uint8_t *addr;
410
411         if (crp->crp_flags & CRYPTO_F_IMBUF) {
412                 m = (struct mbuf *)crp->crp_buf;
413                 if (m->m_next != NULL)
414                         goto alloc;
415                 addr = mtod(m, uint8_t *);
416         } else if (crp->crp_flags & CRYPTO_F_IOV) {
417                 uio = (struct uio *)crp->crp_buf;
418                 if (uio->uio_iovcnt != 1)
419                         goto alloc;
420                 iov = uio->uio_iov;
421                 addr = (uint8_t *)iov->iov_base;
422         } else
423                 addr = (uint8_t *)crp->crp_buf;
424         *allocated = false;
425         addr += enccrd->crd_skip;
426         return (addr);
427
428 alloc:
429         addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
430         if (addr != NULL) {
431                 *allocated = true;
432                 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
433                     enccrd->crd_len, addr);
434         } else
435                 *allocated = false;
436         return (addr);
437 }
438
439 static device_method_t aesni_methods[] = {
440         DEVMETHOD(device_identify, aesni_identify),
441         DEVMETHOD(device_probe, aesni_probe),
442         DEVMETHOD(device_attach, aesni_attach),
443         DEVMETHOD(device_detach, aesni_detach),
444
445         DEVMETHOD(cryptodev_newsession, aesni_newsession),
446         DEVMETHOD(cryptodev_process, aesni_process),
447
448         DEVMETHOD_END
449 };
450
451 static driver_t aesni_driver = {
452         "aesni",
453         aesni_methods,
454         sizeof(struct aesni_softc),
455 };
456 static devclass_t aesni_devclass;
457
458 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
459 MODULE_VERSION(aesni, 1);
460 MODULE_DEPEND(aesni, crypto, 1, 1, 1);
461
462 static int
463 aesni_authprepare(struct aesni_session *ses, int klen, const void *cri_key)
464 {
465         int keylen;
466
467         if (klen % 8 != 0)
468                 return (EINVAL);
469         keylen = klen / 8;
470         if (keylen > sizeof(ses->hmac_key))
471                 return (EINVAL);
472         if (ses->auth_algo == CRYPTO_SHA1 && keylen > 0)
473                 return (EINVAL);
474         memcpy(ses->hmac_key, cri_key, keylen);
475         return (0);
476 }
477
478 static int
479 aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini,
480     struct cryptoini *authini)
481 {
482         struct fpu_kern_ctx *ctx;
483         int kt, ctxidx, error;
484
485         switch (ses->auth_algo) {
486         case CRYPTO_SHA1:
487         case CRYPTO_SHA1_HMAC:
488         case CRYPTO_SHA2_224:
489         case CRYPTO_SHA2_224_HMAC:
490         case CRYPTO_SHA2_256:
491         case CRYPTO_SHA2_256_HMAC:
492                 error = aesni_authprepare(ses, authini->cri_klen,
493                     authini->cri_key);
494                 if (error != 0)
495                         return (error);
496                 ses->mlen = authini->cri_mlen;
497         }
498
499         kt = is_fpu_kern_thread(0) || (encini == NULL);
500         if (!kt) {
501                 ACQUIRE_CTX(ctxidx, ctx);
502                 fpu_kern_enter(curthread, ctx,
503                     FPU_KERN_NORMAL | FPU_KERN_KTHR);
504         }
505
506         error = 0;
507         if (encini != NULL)
508                 error = aesni_cipher_setup_common(ses, encini->cri_key,
509                     encini->cri_klen);
510
511         if (!kt) {
512                 fpu_kern_leave(curthread, ctx);
513                 RELEASE_CTX(ctxidx, ctx);
514         }
515         return (error);
516 }
517
518 static int
519 intel_sha1_update(void *vctx, const void *vdata, u_int datalen)
520 {
521         struct sha1_ctxt *ctx = vctx;
522         const char *data = vdata;
523         size_t gaplen;
524         size_t gapstart;
525         size_t off;
526         size_t copysiz;
527         u_int blocks;
528
529         off = 0;
530         /* Do any aligned blocks without redundant copying. */
531         if (datalen >= 64 && ctx->count % 64 == 0) {
532                 blocks = datalen / 64;
533                 ctx->c.b64[0] += blocks * 64 * 8;
534                 intel_sha1_step(ctx->h.b32, data + off, blocks);
535                 off += blocks * 64;
536         }
537
538         while (off < datalen) {
539                 gapstart = ctx->count % 64;
540                 gaplen = 64 - gapstart;
541
542                 copysiz = (gaplen < datalen - off) ? gaplen : datalen - off;
543                 bcopy(&data[off], &ctx->m.b8[gapstart], copysiz);
544                 ctx->count += copysiz;
545                 ctx->count %= 64;
546                 ctx->c.b64[0] += copysiz * 8;
547                 if (ctx->count % 64 == 0)
548                         intel_sha1_step(ctx->h.b32, (void *)ctx->m.b8, 1);
549                 off += copysiz;
550         }
551         return (0);
552 }
553
554 static void
555 SHA1_Init_fn(void *ctx)
556 {
557         sha1_init(ctx);
558 }
559
560 static void
561 SHA1_Finalize_fn(void *digest, void *ctx)
562 {
563         sha1_result(ctx, digest);
564 }
565
566 static int
567 intel_sha256_update(void *vctx, const void *vdata, u_int len)
568 {
569         SHA256_CTX *ctx = vctx;
570         uint64_t bitlen;
571         uint32_t r;
572         u_int blocks;
573         const unsigned char *src = vdata;
574
575         /* Number of bytes left in the buffer from previous updates */
576         r = (ctx->count >> 3) & 0x3f;
577
578         /* Convert the length into a number of bits */
579         bitlen = len << 3;
580
581         /* Update number of bits */
582         ctx->count += bitlen;
583
584         /* Handle the case where we don't need to perform any transforms */
585         if (len < 64 - r) {
586                 memcpy(&ctx->buf[r], src, len);
587                 return (0);
588         }
589
590         /* Finish the current block */
591         memcpy(&ctx->buf[r], src, 64 - r);
592         intel_sha256_step(ctx->state, ctx->buf, 1);
593         src += 64 - r;
594         len -= 64 - r;
595
596         /* Perform complete blocks */
597         if (len >= 64) {
598                 blocks = len / 64;
599                 intel_sha256_step(ctx->state, src, blocks);
600                 src += blocks * 64;
601                 len -= blocks * 64;
602         }
603
604         /* Copy left over data into buffer */
605         memcpy(ctx->buf, src, len);
606         return (0);
607 }
608
609 static void
610 SHA224_Init_fn(void *ctx)
611 {
612         SHA224_Init(ctx);
613 }
614
615 static void
616 SHA224_Finalize_fn(void *digest, void *ctx)
617 {
618         SHA224_Final(digest, ctx);
619 }
620
621 static void
622 SHA256_Init_fn(void *ctx)
623 {
624         SHA256_Init(ctx);
625 }
626
627 static void
628 SHA256_Finalize_fn(void *digest, void *ctx)
629 {
630         SHA256_Final(digest, ctx);
631 }
632
633 /*
634  * Compute the HASH( (key ^ xorbyte) || buf )
635  */
636 static void
637 hmac_internal(void *ctx, uint32_t *res,
638         int (*update)(void *, const void *, u_int),
639         void (*finalize)(void *, void *), uint8_t *key, uint8_t xorbyte,
640         const void *buf, size_t off, size_t buflen, int crpflags)
641 {
642         size_t i;
643
644         for (i = 0; i < 64; i++)
645                 key[i] ^= xorbyte;
646         update(ctx, key, 64);
647         for (i = 0; i < 64; i++)
648                 key[i] ^= xorbyte;
649
650         crypto_apply(crpflags, __DECONST(void *, buf), off, buflen,
651             __DECONST(int (*)(void *, void *, u_int), update), ctx);
652         finalize(res, ctx);
653 }
654
655 static int
656 aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
657     struct cryptodesc *authcrd, struct cryptop *crp)
658 {
659         struct fpu_kern_ctx *ctx;
660         int error, ctxidx;
661         bool kt;
662
663         if (enccrd != NULL) {
664                 if ((enccrd->crd_alg == CRYPTO_AES_ICM ||
665                     enccrd->crd_alg == CRYPTO_AES_NIST_GCM_16) &&
666                     (enccrd->crd_flags & CRD_F_IV_EXPLICIT) == 0)
667                         return (EINVAL);
668         }
669
670         ctx = NULL;
671         ctxidx = 0;
672         error = 0;
673         kt = is_fpu_kern_thread(0);
674         if (!kt) {
675                 ACQUIRE_CTX(ctxidx, ctx);
676                 fpu_kern_enter(curthread, ctx,
677                     FPU_KERN_NORMAL | FPU_KERN_KTHR);
678         }
679
680         /* Do work */
681         if (enccrd != NULL && authcrd != NULL) {
682                 /* Perform the first operation */
683                 if (crp->crp_desc == enccrd)
684                         error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
685                 else
686                         error = aesni_cipher_mac(ses, authcrd, crp);
687                 if (error != 0)
688                         goto out;
689                 /* Perform the second operation */
690                 if (crp->crp_desc == enccrd)
691                         error = aesni_cipher_mac(ses, authcrd, crp);
692                 else
693                         error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
694         } else if (enccrd != NULL)
695                 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
696         else
697                 error = aesni_cipher_mac(ses, authcrd, crp);
698
699         if (error != 0)
700                 goto out;
701
702 out:
703         if (!kt) {
704                 fpu_kern_leave(curthread, ctx);
705                 RELEASE_CTX(ctxidx, ctx);
706         }
707         return (error);
708 }
709
710 static int
711 aesni_cipher_crypt(struct aesni_session *ses, struct cryptodesc *enccrd,
712         struct cryptodesc *authcrd, struct cryptop *crp)
713 {
714         uint8_t iv[AES_BLOCK_LEN], tag[GMAC_DIGEST_LEN], *buf, *authbuf;
715         int error, ivlen;
716         bool encflag, allocated, authallocated;
717
718         KASSERT(ses->algo != CRYPTO_AES_NIST_GCM_16 || authcrd != NULL,
719             ("AES_NIST_GCM_16 must include MAC descriptor"));
720
721         ivlen = 0;
722         authbuf = NULL;
723
724         buf = aesni_cipher_alloc(enccrd, crp, &allocated);
725         if (buf == NULL)
726                 return (ENOMEM);
727
728         authallocated = false;
729         if (ses->algo == CRYPTO_AES_NIST_GCM_16) {
730                 authbuf = aesni_cipher_alloc(authcrd, crp, &authallocated);
731                 if (authbuf == NULL) {
732                         error = ENOMEM;
733                         goto out;
734                 }
735         }
736
737         error = 0;
738         encflag = (enccrd->crd_flags & CRD_F_ENCRYPT) == CRD_F_ENCRYPT;
739         if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
740                 error = aesni_cipher_setup_common(ses, enccrd->crd_key,
741                     enccrd->crd_klen);
742                 if (error != 0)
743                         goto out;
744         }
745
746         switch (enccrd->crd_alg) {
747         case CRYPTO_AES_CBC:
748         case CRYPTO_AES_ICM:
749                 ivlen = AES_BLOCK_LEN;
750                 break;
751         case CRYPTO_AES_XTS:
752                 ivlen = 8;
753                 break;
754         case CRYPTO_AES_NIST_GCM_16:
755                 ivlen = 12;     /* should support arbitarily larger */
756                 break;
757         }
758
759         /* Setup iv */
760         if (encflag) {
761                 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
762                         bcopy(enccrd->crd_iv, iv, ivlen);
763                 else
764                         arc4rand(iv, ivlen, 0);
765                 
766                 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
767                         crypto_copyback(crp->crp_flags, crp->crp_buf,
768                             enccrd->crd_inject, ivlen, iv);
769         } else {
770                 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
771                         bcopy(enccrd->crd_iv, iv, ivlen);
772                 else
773                         crypto_copydata(crp->crp_flags, crp->crp_buf,
774                             enccrd->crd_inject, ivlen, iv);
775         }
776
777         switch (ses->algo) {
778         case CRYPTO_AES_CBC:
779                 if (encflag)
780                         aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
781                             enccrd->crd_len, buf, buf, iv);
782                 else
783                         aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
784                             enccrd->crd_len, buf, iv);
785                 break;
786         case CRYPTO_AES_ICM:
787                 /* encryption & decryption are the same */
788                 aesni_encrypt_icm(ses->rounds, ses->enc_schedule,
789                     enccrd->crd_len, buf, buf, iv);
790                 break;
791         case CRYPTO_AES_XTS:
792                 if (encflag)
793                         aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
794                             ses->xts_schedule, enccrd->crd_len, buf, buf,
795                             iv);
796                 else
797                         aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
798                             ses->xts_schedule, enccrd->crd_len, buf, buf,
799                             iv);
800                 break;
801         case CRYPTO_AES_NIST_GCM_16:
802                 if (!encflag)
803                         crypto_copydata(crp->crp_flags, crp->crp_buf,
804                             authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
805                 else
806                         bzero(tag, sizeof tag);
807
808                 if (encflag) {
809                         AES_GCM_encrypt(buf, buf, authbuf, iv, tag,
810                             enccrd->crd_len, authcrd->crd_len, ivlen,
811                             ses->enc_schedule, ses->rounds);
812
813                         if (authcrd != NULL)
814                                 crypto_copyback(crp->crp_flags, crp->crp_buf,
815                                     authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
816                 } else {
817                         if (!AES_GCM_decrypt(buf, buf, authbuf, iv, tag,
818                             enccrd->crd_len, authcrd->crd_len, ivlen,
819                             ses->enc_schedule, ses->rounds))
820                                 error = EBADMSG;
821                 }
822                 break;
823         }
824
825         if (allocated)
826                 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
827                     enccrd->crd_len, buf);
828
829 out:
830         if (allocated) {
831                 explicit_bzero(buf, enccrd->crd_len);
832                 free(buf, M_AESNI);
833         }
834         if (authallocated) {
835                 explicit_bzero(authbuf, authcrd->crd_len);
836                 free(authbuf, M_AESNI);
837         }
838         return (error);
839 }
840
841 static int
842 aesni_cipher_mac(struct aesni_session *ses, struct cryptodesc *crd,
843     struct cryptop *crp)
844 {
845         union {
846                 struct SHA256Context sha2 __aligned(16);
847                 struct sha1_ctxt sha1 __aligned(16);
848         } sctx;
849         uint32_t res[SHA2_256_HASH_LEN / sizeof(uint32_t)];
850         int hashlen, error;
851         void *ctx;
852         void (*InitFn)(void *);
853         int (*UpdateFn)(void *, const void *, unsigned);
854         void (*FinalizeFn)(void *, void *);
855
856         bool hmac;
857
858         if ((crd->crd_flags & ~CRD_F_KEY_EXPLICIT) != 0) {
859                 CRYPTDEB("%s: Unsupported MAC flags: 0x%x", __func__,
860                     (crd->crd_flags & ~CRD_F_KEY_EXPLICIT));
861                 return (EINVAL);
862         }
863         if ((crd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
864                 error = aesni_authprepare(ses, crd->crd_klen, crd->crd_key);
865                 if (error != 0)
866                         return (error);
867         }
868
869         hmac = false;
870         switch (ses->auth_algo) {
871         case CRYPTO_SHA1_HMAC:
872                 hmac = true;
873                 /* FALLTHROUGH */
874         case CRYPTO_SHA1:
875                 hashlen = SHA1_HASH_LEN;
876                 InitFn = SHA1_Init_fn;
877                 UpdateFn = intel_sha1_update;
878                 FinalizeFn = SHA1_Finalize_fn;
879                 ctx = &sctx.sha1;
880                 break;
881
882         case CRYPTO_SHA2_256_HMAC:
883                 hmac = true;
884                 /* FALLTHROUGH */
885         case CRYPTO_SHA2_256:
886                 hashlen = SHA2_256_HASH_LEN;
887                 InitFn = SHA256_Init_fn;
888                 UpdateFn = intel_sha256_update;
889                 FinalizeFn = SHA256_Finalize_fn;
890                 ctx = &sctx.sha2;
891                 break;
892
893         case CRYPTO_SHA2_224_HMAC:
894                 hmac = true;
895                 /* FALLTHROUGH */
896         case CRYPTO_SHA2_224:
897                 hashlen = SHA2_224_HASH_LEN;
898                 InitFn = SHA224_Init_fn;
899                 UpdateFn = intel_sha256_update;
900                 FinalizeFn = SHA224_Finalize_fn;
901                 ctx = &sctx.sha2;
902                 break;
903         default:
904                 /*
905                  * AES-GMAC authentication is verified while processing the
906                  * enccrd
907                  */
908                 return (0);
909         }
910
911         if (hmac) {
912                 /* Inner hash: (K ^ IPAD) || data */
913                 InitFn(ctx);
914                 hmac_internal(ctx, res, UpdateFn, FinalizeFn, ses->hmac_key,
915                     0x36, crp->crp_buf, crd->crd_skip, crd->crd_len,
916                     crp->crp_flags);
917                 /* Outer hash: (K ^ OPAD) || inner hash */
918                 InitFn(ctx);
919                 hmac_internal(ctx, res, UpdateFn, FinalizeFn, ses->hmac_key,
920                     0x5C, res, 0, hashlen, 0);
921         } else {
922                 InitFn(ctx);
923                 crypto_apply(crp->crp_flags, crp->crp_buf, crd->crd_skip,
924                     crd->crd_len, __DECONST(int (*)(void *, void *, u_int),
925                     UpdateFn), ctx);
926                 FinalizeFn(res, ctx);
927         }
928
929         if (ses->mlen != 0 && ses->mlen < hashlen)
930                 hashlen = ses->mlen;
931
932         crypto_copyback(crp->crp_flags, crp->crp_buf, crd->crd_inject, hashlen,
933             (void *)res);
934         return (0);
935 }