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
7 * Portions of this software were developed by John-Mark Gurney
8 * under sponsorship of the FreeBSD Foundation and
9 * Rubicon Communications, LLC (Netgate).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/libkern.h>
42 #include <sys/module.h>
43 #include <sys/malloc.h>
44 #include <sys/rwlock.h>
48 #include <crypto/aesni/aesni.h>
49 #include <cryptodev_if.h>
50 #include <opencrypto/gmac.h>
55 TAILQ_HEAD(aesni_sessions_head, aesni_session) sessions;
59 static int aesni_newsession(device_t, uint32_t *sidp, struct cryptoini *cri);
60 static int aesni_freesession(device_t, uint64_t tid);
61 static void aesni_freesession_locked(struct aesni_softc *sc,
62 struct aesni_session *ses);
63 static int aesni_cipher_setup(struct aesni_session *ses,
64 struct cryptoini *encini);
65 static int aesni_cipher_process(struct aesni_session *ses,
66 struct cryptodesc *enccrd, struct cryptodesc *authcrd, struct cryptop *crp);
68 MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
71 aesni_identify(driver_t *drv, device_t parent)
74 /* NB: order 10 is so we get attached after h/w devices */
75 if (device_find_child(parent, "aesni", -1) == NULL &&
76 BUS_ADD_CHILD(parent, 10, "aesni", -1) == 0)
77 panic("aesni: could not attach");
81 aesni_probe(device_t dev)
84 if ((cpu_feature2 & CPUID2_AESNI) == 0) {
85 device_printf(dev, "No AESNI support.\n");
89 if ((cpu_feature2 & CPUID2_SSE41) == 0) {
90 device_printf(dev, "No SSE4.1 support.\n");
94 device_set_desc_copy(dev, "AES-CBC,AES-XTS,AES-GCM,AES-ICM");
99 aesni_attach(device_t dev)
101 struct aesni_softc *sc;
103 sc = device_get_softc(dev);
104 TAILQ_INIT(&sc->sessions);
106 sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE |
109 device_printf(dev, "Could not get crypto driver id.\n");
113 rw_init(&sc->lock, "aesni_lock");
114 crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
115 crypto_register(sc->cid, CRYPTO_AES_ICM, 0, 0);
116 crypto_register(sc->cid, CRYPTO_AES_NIST_GCM_16, 0, 0);
117 crypto_register(sc->cid, CRYPTO_AES_128_NIST_GMAC, 0, 0);
118 crypto_register(sc->cid, CRYPTO_AES_192_NIST_GMAC, 0, 0);
119 crypto_register(sc->cid, CRYPTO_AES_256_NIST_GMAC, 0, 0);
120 crypto_register(sc->cid, CRYPTO_AES_XTS, 0, 0);
125 aesni_detach(device_t dev)
127 struct aesni_softc *sc;
128 struct aesni_session *ses;
130 sc = device_get_softc(dev);
132 TAILQ_FOREACH(ses, &sc->sessions, next) {
134 rw_wunlock(&sc->lock);
136 "Cannot detach, sessions still active.\n");
140 while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) {
141 TAILQ_REMOVE(&sc->sessions, ses, next);
142 fpu_kern_free_ctx(ses->fpu_ctx);
145 rw_wunlock(&sc->lock);
146 rw_destroy(&sc->lock);
147 crypto_unregister_all(sc->cid);
152 aesni_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
154 struct aesni_softc *sc;
155 struct aesni_session *ses;
156 struct cryptoini *encini;
159 if (sidp == NULL || cri == NULL) {
160 CRYPTDEB("no sidp or cri");
164 sc = device_get_softc(dev);
167 for (; cri != NULL; cri = cri->cri_next) {
168 switch (cri->cri_alg) {
172 case CRYPTO_AES_NIST_GCM_16:
173 if (encini != NULL) {
174 CRYPTDEB("encini already set");
179 case CRYPTO_AES_128_NIST_GMAC:
180 case CRYPTO_AES_192_NIST_GMAC:
181 case CRYPTO_AES_256_NIST_GMAC:
183 * nothing to do here, maybe in the future cache some
188 CRYPTDEB("unhandled algorithm");
192 if (encini == NULL) {
193 CRYPTDEB("no cipher");
199 * Free sessions goes first, so if first session is used, we need to
202 ses = TAILQ_FIRST(&sc->sessions);
203 if (ses == NULL || ses->used) {
204 ses = malloc(sizeof(*ses), M_AESNI, M_NOWAIT | M_ZERO);
206 rw_wunlock(&sc->lock);
209 ses->fpu_ctx = fpu_kern_alloc_ctx(FPU_KERN_NOWAIT);
210 if (ses->fpu_ctx == NULL) {
212 rw_wunlock(&sc->lock);
217 TAILQ_REMOVE(&sc->sessions, ses, next);
220 TAILQ_INSERT_TAIL(&sc->sessions, ses, next);
221 rw_wunlock(&sc->lock);
222 ses->algo = encini->cri_alg;
224 error = aesni_cipher_setup(ses, encini);
226 CRYPTDEB("setup failed");
228 aesni_freesession_locked(sc, ses);
229 rw_wunlock(&sc->lock);
238 aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses)
240 struct fpu_kern_ctx *ctx;
244 TAILQ_REMOVE(&sc->sessions, ses, next);
246 *ses = (struct aesni_session){};
249 TAILQ_INSERT_HEAD(&sc->sessions, ses, next);
253 aesni_freesession(device_t dev, uint64_t tid)
255 struct aesni_softc *sc;
256 struct aesni_session *ses;
259 sc = device_get_softc(dev);
260 sid = ((uint32_t)tid) & 0xffffffff;
262 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
267 rw_wunlock(&sc->lock);
270 aesni_freesession_locked(sc, ses);
271 rw_wunlock(&sc->lock);
276 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
278 struct aesni_softc *sc = device_get_softc(dev);
279 struct aesni_session *ses = NULL;
280 struct cryptodesc *crd, *enccrd, *authcrd;
292 if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
297 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
298 switch (crd->crd_alg) {
302 if (enccrd != NULL) {
309 case CRYPTO_AES_NIST_GCM_16:
310 if (enccrd != NULL) {
318 case CRYPTO_AES_128_NIST_GMAC:
319 case CRYPTO_AES_192_NIST_GMAC:
320 case CRYPTO_AES_256_NIST_GMAC:
321 if (authcrd != NULL) {
335 if (enccrd == NULL || (needauth && authcrd == NULL)) {
340 /* CBC & XTS can only handle full blocks for now */
341 if ((enccrd->crd_alg == CRYPTO_AES_CBC || enccrd->crd_alg ==
342 CRYPTO_AES_XTS) && (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
348 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
349 if (ses->id == (crp->crp_sid & 0xffffffff))
352 rw_runlock(&sc->lock);
358 error = aesni_cipher_process(ses, enccrd, authcrd, crp);
363 crp->crp_etype = error;
369 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
377 if (crp->crp_flags & CRYPTO_F_IMBUF) {
378 m = (struct mbuf *)crp->crp_buf;
379 if (m->m_next != NULL)
381 addr = mtod(m, uint8_t *);
382 } else if (crp->crp_flags & CRYPTO_F_IOV) {
383 uio = (struct uio *)crp->crp_buf;
384 if (uio->uio_iovcnt != 1)
387 addr = (uint8_t *)iov->iov_base;
389 addr = (uint8_t *)crp->crp_buf;
391 addr += enccrd->crd_skip;
395 addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
398 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
399 enccrd->crd_len, addr);
405 static device_method_t aesni_methods[] = {
406 DEVMETHOD(device_identify, aesni_identify),
407 DEVMETHOD(device_probe, aesni_probe),
408 DEVMETHOD(device_attach, aesni_attach),
409 DEVMETHOD(device_detach, aesni_detach),
411 DEVMETHOD(cryptodev_newsession, aesni_newsession),
412 DEVMETHOD(cryptodev_freesession, aesni_freesession),
413 DEVMETHOD(cryptodev_process, aesni_process),
418 static driver_t aesni_driver = {
421 sizeof(struct aesni_softc),
423 static devclass_t aesni_devclass;
425 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
426 MODULE_VERSION(aesni, 1);
427 MODULE_DEPEND(aesni, crypto, 1, 1, 1);
430 aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini)
436 error = fpu_kern_enter(td, ses->fpu_ctx, FPU_KERN_NORMAL |
440 error = aesni_cipher_setup_common(ses, encini->cri_key,
442 fpu_kern_leave(td, ses->fpu_ctx);
447 * authcrd contains the associated date.
450 aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
451 struct cryptodesc *authcrd, struct cryptop *crp)
453 uint8_t iv[AES_BLOCK_LEN];
454 uint8_t tag[GMAC_DIGEST_LEN];
456 uint8_t *buf, *authbuf;
457 int error, allocated, authallocated;
460 encflag = (enccrd->crd_flags & CRD_F_ENCRYPT) == CRD_F_ENCRYPT;
462 if ((enccrd->crd_alg == CRYPTO_AES_ICM ||
463 enccrd->crd_alg == CRYPTO_AES_NIST_GCM_16) &&
464 (enccrd->crd_flags & CRD_F_IV_EXPLICIT) == 0)
467 buf = aesni_cipher_alloc(enccrd, crp, &allocated);
473 if (authcrd != NULL) {
474 authbuf = aesni_cipher_alloc(authcrd, crp, &authallocated);
475 if (authbuf == NULL) {
482 error = fpu_kern_enter(td, ses->fpu_ctx, FPU_KERN_NORMAL |
487 if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
488 error = aesni_cipher_setup_common(ses, enccrd->crd_key,
494 /* XXX - validate that enccrd and authcrd have/use same key? */
495 switch (enccrd->crd_alg) {
498 ivlen = AES_BLOCK_LEN;
503 case CRYPTO_AES_NIST_GCM_16:
504 ivlen = 12; /* should support arbitarily larger */
510 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
511 bcopy(enccrd->crd_iv, iv, ivlen);
513 arc4rand(iv, ivlen, 0);
515 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
516 crypto_copyback(crp->crp_flags, crp->crp_buf,
517 enccrd->crd_inject, ivlen, iv);
519 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
520 bcopy(enccrd->crd_iv, iv, ivlen);
522 crypto_copydata(crp->crp_flags, crp->crp_buf,
523 enccrd->crd_inject, ivlen, iv);
526 if (authcrd != NULL && !encflag)
527 crypto_copydata(crp->crp_flags, crp->crp_buf,
528 authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
530 bzero(tag, sizeof tag);
536 aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
537 enccrd->crd_len, buf, buf, iv);
539 aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
540 enccrd->crd_len, buf, iv);
543 /* encryption & decryption are the same */
544 aesni_encrypt_icm(ses->rounds, ses->enc_schedule,
545 enccrd->crd_len, buf, buf, iv);
549 aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
550 ses->xts_schedule, enccrd->crd_len, buf, buf,
553 aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
554 ses->xts_schedule, enccrd->crd_len, buf, buf,
557 case CRYPTO_AES_NIST_GCM_16:
559 AES_GCM_encrypt(buf, buf, authbuf, iv, tag,
560 enccrd->crd_len, authcrd->crd_len, ivlen,
561 ses->enc_schedule, ses->rounds);
563 if (!AES_GCM_decrypt(buf, buf, authbuf, iv, tag,
564 enccrd->crd_len, authcrd->crd_len, ivlen,
565 ses->enc_schedule, ses->rounds))
572 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
573 enccrd->crd_len, buf);
575 if (!error && authcrd != NULL) {
576 crypto_copyback(crp->crp_flags, crp->crp_buf,
577 authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
581 fpu_kern_leave(td, ses->fpu_ctx);
584 bzero(buf, enccrd->crd_len);
588 free(authbuf, M_AESNI);