]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/crypto/aesni/aesni.c
OpenCrypto: Convert sessions to opaque handles instead of integers
[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/sha256.h>
54
55 #include <opencrypto/cryptodev.h>
56 #include <opencrypto/gmac.h>
57 #include <cryptodev_if.h>
58
59 #include <machine/md_var.h>
60 #include <machine/specialreg.h>
61 #if defined(__i386__)
62 #include <machine/npx.h>
63 #elif defined(__amd64__)
64 #include <machine/fpu.h>
65 #endif
66
67 static struct mtx_padalign *ctx_mtx;
68 static struct fpu_kern_ctx **ctx_fpu;
69
70 struct aesni_softc {
71         int32_t cid;
72         bool    has_aes;
73         bool    has_sha;
74 };
75
76 #define ACQUIRE_CTX(i, ctx)                                     \
77         do {                                                    \
78                 (i) = PCPU_GET(cpuid);                          \
79                 mtx_lock(&ctx_mtx[(i)]);                        \
80                 (ctx) = ctx_fpu[(i)];                           \
81         } while (0)
82 #define RELEASE_CTX(i, ctx)                                     \
83         do {                                                    \
84                 mtx_unlock(&ctx_mtx[(i)]);                      \
85                 (i) = -1;                                       \
86                 (ctx) = NULL;                                   \
87         } while (0)
88
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,
98     struct cryptop *crp);
99
100 MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
101
102 static void
103 aesni_identify(driver_t *drv, device_t parent)
104 {
105
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");
110 }
111
112 static void
113 detect_cpu_features(bool *has_aes, bool *has_sha)
114 {
115
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);
120 }
121
122 static int
123 aesni_probe(device_t dev)
124 {
125         bool has_aes, has_sha;
126
127         detect_cpu_features(&has_aes, &has_sha);
128         if (!has_aes && !has_sha) {
129                 device_printf(dev, "No AES or SHA support.\n");
130                 return (EINVAL);
131         } else if (has_aes && has_sha)
132                 device_set_desc(dev,
133                     "AES-CBC,AES-XTS,AES-GCM,AES-ICM,SHA1,SHA256");
134         else if (has_aes)
135                 device_set_desc(dev, "AES-CBC,AES-XTS,AES-GCM,AES-ICM");
136         else
137                 device_set_desc(dev, "SHA1,SHA256");
138
139         return (0);
140 }
141
142 static void
143 aesni_cleanctx(void)
144 {
145         int i;
146
147         /* XXX - no way to return driverid */
148         CPU_FOREACH(i) {
149                 if (ctx_fpu[i] != NULL) {
150                         mtx_destroy(&ctx_mtx[i]);
151                         fpu_kern_free_ctx(ctx_fpu[i]);
152                 }
153                 ctx_fpu[i] = NULL;
154         }
155         free(ctx_mtx, M_AESNI);
156         ctx_mtx = NULL;
157         free(ctx_fpu, M_AESNI);
158         ctx_fpu = NULL;
159 }
160
161 static int
162 aesni_attach(device_t dev)
163 {
164         struct aesni_softc *sc;
165         int i;
166
167         sc = device_get_softc(dev);
168
169         sc->cid = crypto_get_driverid(dev, sizeof(struct aesni_session),
170             CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
171         if (sc->cid < 0) {
172                 device_printf(dev, "Could not get crypto driver id.\n");
173                 return (ENOMEM);
174         }
175
176         ctx_mtx = malloc(sizeof *ctx_mtx * (mp_maxid + 1), M_AESNI,
177             M_WAITOK|M_ZERO);
178         ctx_fpu = malloc(sizeof *ctx_fpu * (mp_maxid + 1), M_AESNI,
179             M_WAITOK|M_ZERO);
180
181         CPU_FOREACH(i) {
182                 ctx_fpu[i] = fpu_kern_alloc_ctx(0);
183                 mtx_init(&ctx_mtx[i], "anifpumtx", NULL, MTX_DEF|MTX_NEW);
184         }
185
186         detect_cpu_features(&sc->has_aes, &sc->has_sha);
187         if (sc->has_aes) {
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);
195         }
196         if (sc->has_sha) {
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);
200         }
201         return (0);
202 }
203
204 static int
205 aesni_detach(device_t dev)
206 {
207         struct aesni_softc *sc;
208
209         sc = device_get_softc(dev);
210
211         crypto_unregister_all(sc->cid);
212
213         aesni_cleanctx();
214
215         return (0);
216 }
217
218 static int
219 aesni_newsession(device_t dev, crypto_session_t cses, struct cryptoini *cri)
220 {
221         struct aesni_softc *sc;
222         struct aesni_session *ses;
223         struct cryptoini *encini, *authini;
224         bool gcm_hash, gcm;
225         int error;
226
227         KASSERT(cses != NULL, ("EDOOFUS"));
228         if (cri == NULL) {
229                 CRYPTDEB("no cri");
230                 return (EINVAL);
231         }
232
233         sc = device_get_softc(dev);
234
235         ses = crypto_get_driver_session(cses);
236
237         authini = NULL;
238         encini = NULL;
239         gcm = false;
240         gcm_hash = false;
241         for (; cri != NULL; cri = cri->cri_next) {
242                 switch (cri->cri_alg) {
243                 case CRYPTO_AES_NIST_GCM_16:
244                         gcm = true;
245                         /* FALLTHROUGH */
246                 case CRYPTO_AES_CBC:
247                 case CRYPTO_AES_ICM:
248                 case CRYPTO_AES_XTS:
249                         if (!sc->has_aes)
250                                 goto unhandled;
251                         if (encini != NULL) {
252                                 CRYPTDEB("encini already set");
253                                 return (EINVAL);
254                         }
255                         encini = cri;
256                         break;
257                 case CRYPTO_AES_128_NIST_GMAC:
258                 case CRYPTO_AES_192_NIST_GMAC:
259                 case CRYPTO_AES_256_NIST_GMAC:
260                         /*
261                          * nothing to do here, maybe in the future cache some
262                          * values for GHASH
263                          */
264                         gcm_hash = true;
265                         break;
266                 case CRYPTO_SHA1:
267                 case CRYPTO_SHA1_HMAC:
268                 case CRYPTO_SHA2_256_HMAC:
269                         if (!sc->has_sha)
270                                 goto unhandled;
271                         if (authini != NULL) {
272                                 CRYPTDEB("authini already set");
273                                 return (EINVAL);
274                         }
275                         authini = cri;
276                         break;
277                 default:
278 unhandled:
279                         CRYPTDEB("unhandled algorithm");
280                         return (EINVAL);
281                 }
282         }
283         if (encini == NULL && authini == NULL) {
284                 CRYPTDEB("no cipher");
285                 return (EINVAL);
286         }
287         /*
288          * GMAC algorithms are only supported with simultaneous GCM.  Likewise
289          * GCM is not supported without GMAC.
290          */
291         if (gcm_hash != gcm)
292                 return (EINVAL);
293
294         if (encini != NULL)
295                 ses->algo = encini->cri_alg;
296         if (authini != NULL)
297                 ses->auth_algo = authini->cri_alg;
298
299         error = aesni_cipher_setup(ses, encini, authini);
300         if (error != 0) {
301                 CRYPTDEB("setup failed");
302                 return (error);
303         }
304
305         return (0);
306 }
307
308 static int
309 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
310 {
311         struct aesni_softc *sc;
312         struct aesni_session *ses;
313         struct cryptodesc *crd, *enccrd, *authcrd;
314         int error, needauth;
315
316         sc = device_get_softc(dev);
317         ses = NULL;
318         error = 0;
319         enccrd = NULL;
320         authcrd = NULL;
321         needauth = 0;
322
323         /* Sanity check. */
324         if (crp == NULL)
325                 return (EINVAL);
326
327         if (crp->crp_callback == NULL || crp->crp_desc == NULL ||
328             crp->crp_session == NULL) {
329                 error = EINVAL;
330                 goto out;
331         }
332
333         for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
334                 switch (crd->crd_alg) {
335                 case CRYPTO_AES_NIST_GCM_16:
336                         needauth = 1;
337                         /* FALLTHROUGH */
338                 case CRYPTO_AES_CBC:
339                 case CRYPTO_AES_ICM:
340                 case CRYPTO_AES_XTS:
341                         if (enccrd != NULL) {
342                                 error = EINVAL;
343                                 goto out;
344                         }
345                         enccrd = crd;
346                         break;
347
348                 case CRYPTO_AES_128_NIST_GMAC:
349                 case CRYPTO_AES_192_NIST_GMAC:
350                 case CRYPTO_AES_256_NIST_GMAC:
351                 case CRYPTO_SHA1:
352                 case CRYPTO_SHA1_HMAC:
353                 case CRYPTO_SHA2_256_HMAC:
354                         if (authcrd != NULL) {
355                                 error = EINVAL;
356                                 goto out;
357                         }
358                         authcrd = crd;
359                         break;
360
361                 default:
362                         error = EINVAL;
363                         goto out;
364                 }
365         }
366
367         if ((enccrd == NULL && authcrd == NULL) ||
368             (needauth && authcrd == NULL)) {
369                 error = EINVAL;
370                 goto out;
371         }
372
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) {
377                 error = EINVAL;
378                 goto out;
379         }
380
381         ses = crypto_get_driver_session(crp->crp_session);
382         KASSERT(ses != NULL, ("EDOOFUS"));
383
384         error = aesni_cipher_process(ses, enccrd, authcrd, crp);
385         if (error != 0)
386                 goto out;
387
388 out:
389         crp->crp_etype = error;
390         crypto_done(crp);
391         return (error);
392 }
393
394 static uint8_t *
395 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
396     bool *allocated)
397 {
398         struct mbuf *m;
399         struct uio *uio;
400         struct iovec *iov;
401         uint8_t *addr;
402
403         if (crp->crp_flags & CRYPTO_F_IMBUF) {
404                 m = (struct mbuf *)crp->crp_buf;
405                 if (m->m_next != NULL)
406                         goto alloc;
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)
411                         goto alloc;
412                 iov = uio->uio_iov;
413                 addr = (uint8_t *)iov->iov_base;
414         } else
415                 addr = (uint8_t *)crp->crp_buf;
416         *allocated = false;
417         addr += enccrd->crd_skip;
418         return (addr);
419
420 alloc:
421         addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
422         if (addr != NULL) {
423                 *allocated = true;
424                 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
425                     enccrd->crd_len, addr);
426         } else
427                 *allocated = false;
428         return (addr);
429 }
430
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),
436
437         DEVMETHOD(cryptodev_newsession, aesni_newsession),
438         DEVMETHOD(cryptodev_process, aesni_process),
439
440         DEVMETHOD_END
441 };
442
443 static driver_t aesni_driver = {
444         "aesni",
445         aesni_methods,
446         sizeof(struct aesni_softc),
447 };
448 static devclass_t aesni_devclass;
449
450 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
451 MODULE_VERSION(aesni, 1);
452 MODULE_DEPEND(aesni, crypto, 1, 1, 1);
453
454 static int
455 aesni_authprepare(struct aesni_session *ses, int klen, const void *cri_key)
456 {
457         int keylen;
458
459         if (klen % 8 != 0)
460                 return (EINVAL);
461         keylen = klen / 8;
462         if (keylen > sizeof(ses->hmac_key))
463                 return (EINVAL);
464         if (ses->auth_algo == CRYPTO_SHA1 && keylen > 0)
465                 return (EINVAL);
466         memcpy(ses->hmac_key, cri_key, keylen);
467         return (0);
468 }
469
470 static int
471 aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini,
472     struct cryptoini *authini)
473 {
474         struct fpu_kern_ctx *ctx;
475         int kt, ctxidx, error;
476
477         switch (ses->auth_algo) {
478         case CRYPTO_SHA1:
479         case CRYPTO_SHA1_HMAC:
480         case CRYPTO_SHA2_256_HMAC:
481                 error = aesni_authprepare(ses, authini->cri_klen,
482                     authini->cri_key);
483                 if (error != 0)
484                         return (error);
485                 ses->mlen = authini->cri_mlen;
486         }
487
488         kt = is_fpu_kern_thread(0) || (encini == NULL);
489         if (!kt) {
490                 ACQUIRE_CTX(ctxidx, ctx);
491                 fpu_kern_enter(curthread, ctx,
492                     FPU_KERN_NORMAL | FPU_KERN_KTHR);
493         }
494
495         error = 0;
496         if (encini != NULL)
497                 error = aesni_cipher_setup_common(ses, encini->cri_key,
498                     encini->cri_klen);
499
500         if (!kt) {
501                 fpu_kern_leave(curthread, ctx);
502                 RELEASE_CTX(ctxidx, ctx);
503         }
504         return (error);
505 }
506
507 static int
508 intel_sha1_update(void *vctx, const void *vdata, u_int datalen)
509 {
510         struct sha1_ctxt *ctx = vctx;
511         const char *data = vdata;
512         size_t gaplen;
513         size_t gapstart;
514         size_t off;
515         size_t copysiz;
516         u_int blocks;
517
518         off = 0;
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);
524                 off += blocks * 64;
525         }
526
527         while (off < datalen) {
528                 gapstart = ctx->count % 64;
529                 gaplen = 64 - gapstart;
530
531                 copysiz = (gaplen < datalen - off) ? gaplen : datalen - off;
532                 bcopy(&data[off], &ctx->m.b8[gapstart], copysiz);
533                 ctx->count += copysiz;
534                 ctx->count %= 64;
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);
538                 off += copysiz;
539         }
540         return (0);
541 }
542
543 static void
544 SHA1_Finalize_fn(void *digest, void *ctx)
545 {
546         sha1_result(ctx, digest);
547 }
548
549 static int
550 intel_sha256_update(void *vctx, const void *vdata, u_int len)
551 {
552         SHA256_CTX *ctx = vctx;
553         uint64_t bitlen;
554         uint32_t r;
555         u_int blocks;
556         const unsigned char *src = vdata;
557
558         /* Number of bytes left in the buffer from previous updates */
559         r = (ctx->count >> 3) & 0x3f;
560
561         /* Convert the length into a number of bits */
562         bitlen = len << 3;
563
564         /* Update number of bits */
565         ctx->count += bitlen;
566
567         /* Handle the case where we don't need to perform any transforms */
568         if (len < 64 - r) {
569                 memcpy(&ctx->buf[r], src, len);
570                 return (0);
571         }
572
573         /* Finish the current block */
574         memcpy(&ctx->buf[r], src, 64 - r);
575         intel_sha256_step(ctx->state, ctx->buf, 1);
576         src += 64 - r;
577         len -= 64 - r;
578
579         /* Perform complete blocks */
580         if (len >= 64) {
581                 blocks = len / 64;
582                 intel_sha256_step(ctx->state, src, blocks);
583                 src += blocks * 64;
584                 len -= blocks * 64;
585         }
586
587         /* Copy left over data into buffer */
588         memcpy(ctx->buf, src, len);
589         return (0);
590 }
591
592 static void
593 SHA256_Finalize_fn(void *digest, void *ctx)
594 {
595         SHA256_Final(digest, ctx);
596 }
597
598 /*
599  * Compute the HASH( (key ^ xorbyte) || buf )
600  */
601 static void
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)
606 {
607         size_t i;
608
609         for (i = 0; i < 64; i++)
610                 key[i] ^= xorbyte;
611         update(ctx, key, 64);
612         for (i = 0; i < 64; i++)
613                 key[i] ^= xorbyte;
614
615         crypto_apply(crpflags, __DECONST(void *, buf), off, buflen,
616             __DECONST(int (*)(void *, void *, u_int), update), ctx);
617         finalize(res, ctx);
618 }
619
620 static int
621 aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
622     struct cryptodesc *authcrd, struct cryptop *crp)
623 {
624         struct fpu_kern_ctx *ctx;
625         int error, ctxidx;
626         bool kt;
627
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)
632                         return (EINVAL);
633         }
634
635         ctx = NULL;
636         ctxidx = 0;
637         error = 0;
638         kt = is_fpu_kern_thread(0);
639         if (!kt) {
640                 ACQUIRE_CTX(ctxidx, ctx);
641                 fpu_kern_enter(curthread, ctx,
642                     FPU_KERN_NORMAL | FPU_KERN_KTHR);
643         }
644
645         /* Do work */
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);
650                 else
651                         error = aesni_cipher_mac(ses, authcrd, crp);
652                 if (error != 0)
653                         goto out;
654                 /* Perform the second operation */
655                 if (crp->crp_desc == enccrd)
656                         error = aesni_cipher_mac(ses, authcrd, crp);
657                 else
658                         error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
659         } else if (enccrd != NULL)
660                 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
661         else
662                 error = aesni_cipher_mac(ses, authcrd, crp);
663
664         if (error != 0)
665                 goto out;
666
667 out:
668         if (!kt) {
669                 fpu_kern_leave(curthread, ctx);
670                 RELEASE_CTX(ctxidx, ctx);
671         }
672         return (error);
673 }
674
675 static int
676 aesni_cipher_crypt(struct aesni_session *ses, struct cryptodesc *enccrd,
677         struct cryptodesc *authcrd, struct cryptop *crp)
678 {
679         uint8_t iv[AES_BLOCK_LEN], tag[GMAC_DIGEST_LEN], *buf, *authbuf;
680         int error, ivlen;
681         bool encflag, allocated, authallocated;
682
683         KASSERT(ses->algo != CRYPTO_AES_NIST_GCM_16 || authcrd != NULL,
684             ("AES_NIST_GCM_16 must include MAC descriptor"));
685
686         ivlen = 0;
687         authbuf = NULL;
688
689         buf = aesni_cipher_alloc(enccrd, crp, &allocated);
690         if (buf == NULL)
691                 return (ENOMEM);
692
693         authallocated = false;
694         if (ses->algo == CRYPTO_AES_NIST_GCM_16) {
695                 authbuf = aesni_cipher_alloc(authcrd, crp, &authallocated);
696                 if (authbuf == NULL) {
697                         error = ENOMEM;
698                         goto out;
699                 }
700         }
701
702         error = 0;
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,
706                     enccrd->crd_klen);
707                 if (error != 0)
708                         goto out;
709         }
710
711         switch (enccrd->crd_alg) {
712         case CRYPTO_AES_CBC:
713         case CRYPTO_AES_ICM:
714                 ivlen = AES_BLOCK_LEN;
715                 break;
716         case CRYPTO_AES_XTS:
717                 ivlen = 8;
718                 break;
719         case CRYPTO_AES_NIST_GCM_16:
720                 ivlen = 12;     /* should support arbitarily larger */
721                 break;
722         }
723
724         /* Setup iv */
725         if (encflag) {
726                 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
727                         bcopy(enccrd->crd_iv, iv, ivlen);
728                 else
729                         arc4rand(iv, ivlen, 0);
730                 
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);
734         } else {
735                 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
736                         bcopy(enccrd->crd_iv, iv, ivlen);
737                 else
738                         crypto_copydata(crp->crp_flags, crp->crp_buf,
739                             enccrd->crd_inject, ivlen, iv);
740         }
741
742         switch (ses->algo) {
743         case CRYPTO_AES_CBC:
744                 if (encflag)
745                         aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
746                             enccrd->crd_len, buf, buf, iv);
747                 else
748                         aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
749                             enccrd->crd_len, buf, iv);
750                 break;
751         case CRYPTO_AES_ICM:
752                 /* encryption & decryption are the same */
753                 aesni_encrypt_icm(ses->rounds, ses->enc_schedule,
754                     enccrd->crd_len, buf, buf, iv);
755                 break;
756         case CRYPTO_AES_XTS:
757                 if (encflag)
758                         aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
759                             ses->xts_schedule, enccrd->crd_len, buf, buf,
760                             iv);
761                 else
762                         aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
763                             ses->xts_schedule, enccrd->crd_len, buf, buf,
764                             iv);
765                 break;
766         case CRYPTO_AES_NIST_GCM_16:
767                 if (!encflag)
768                         crypto_copydata(crp->crp_flags, crp->crp_buf,
769                             authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
770                 else
771                         bzero(tag, sizeof tag);
772
773                 if (encflag) {
774                         AES_GCM_encrypt(buf, buf, authbuf, iv, tag,
775                             enccrd->crd_len, authcrd->crd_len, ivlen,
776                             ses->enc_schedule, ses->rounds);
777
778                         if (authcrd != NULL)
779                                 crypto_copyback(crp->crp_flags, crp->crp_buf,
780                                     authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
781                 } else {
782                         if (!AES_GCM_decrypt(buf, buf, authbuf, iv, tag,
783                             enccrd->crd_len, authcrd->crd_len, ivlen,
784                             ses->enc_schedule, ses->rounds))
785                                 error = EBADMSG;
786                 }
787                 break;
788         }
789
790         if (allocated)
791                 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
792                     enccrd->crd_len, buf);
793
794 out:
795         if (allocated) {
796                 explicit_bzero(buf, enccrd->crd_len);
797                 free(buf, M_AESNI);
798         }
799         if (authallocated) {
800                 explicit_bzero(authbuf, authcrd->crd_len);
801                 free(authbuf, M_AESNI);
802         }
803         return (error);
804 }
805
806 static int
807 aesni_cipher_mac(struct aesni_session *ses, struct cryptodesc *crd,
808     struct cryptop *crp)
809 {
810         union {
811                 struct SHA256Context sha2 __aligned(16);
812                 struct sha1_ctxt sha1 __aligned(16);
813         } sctx;
814         uint32_t res[SHA2_256_HASH_LEN / sizeof(uint32_t)];
815         int hashlen, error;
816
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));
820                 return (EINVAL);
821         }
822         if ((crd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
823                 error = aesni_authprepare(ses, crd->crd_klen, crd->crd_key);
824                 if (error != 0)
825                         return (error);
826         }
827
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);
840                 break;
841         case CRYPTO_SHA1:
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);
848                 break;
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,
860                     0);
861                 break;
862         default:
863                 /*
864                  * AES-GMAC authentication is verified while processing the
865                  * enccrd
866                  */
867                 return (0);
868         }
869
870         if (ses->mlen != 0 && ses->mlen < hashlen)
871                 hashlen = ses->mlen;
872
873         crypto_copyback(crp->crp_flags, crp->crp_buf, crd->crd_inject, hashlen,
874             (void *)res);
875         return (0);
876 }