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_NORMAL |
211 if (ses->fpu_ctx == NULL) {
213 rw_wunlock(&sc->lock);
218 TAILQ_REMOVE(&sc->sessions, ses, next);
221 TAILQ_INSERT_TAIL(&sc->sessions, ses, next);
222 rw_wunlock(&sc->lock);
223 ses->algo = encini->cri_alg;
225 error = aesni_cipher_setup(ses, encini);
227 CRYPTDEB("setup failed");
229 aesni_freesession_locked(sc, ses);
230 rw_wunlock(&sc->lock);
239 aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses)
241 struct fpu_kern_ctx *ctx;
245 TAILQ_REMOVE(&sc->sessions, ses, next);
247 *ses = (struct aesni_session){};
250 TAILQ_INSERT_HEAD(&sc->sessions, ses, next);
254 aesni_freesession(device_t dev, uint64_t tid)
256 struct aesni_softc *sc;
257 struct aesni_session *ses;
260 sc = device_get_softc(dev);
261 sid = ((uint32_t)tid) & 0xffffffff;
263 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
268 rw_wunlock(&sc->lock);
271 aesni_freesession_locked(sc, ses);
272 rw_wunlock(&sc->lock);
277 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
279 struct aesni_softc *sc = device_get_softc(dev);
280 struct aesni_session *ses = NULL;
281 struct cryptodesc *crd, *enccrd, *authcrd;
293 if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
298 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
299 switch (crd->crd_alg) {
303 if (enccrd != NULL) {
310 case CRYPTO_AES_NIST_GCM_16:
311 if (enccrd != NULL) {
319 case CRYPTO_AES_128_NIST_GMAC:
320 case CRYPTO_AES_192_NIST_GMAC:
321 case CRYPTO_AES_256_NIST_GMAC:
322 if (authcrd != NULL) {
336 if (enccrd == NULL || (needauth && authcrd == NULL)) {
341 /* CBC & XTS can only handle full blocks for now */
342 if ((enccrd->crd_alg == CRYPTO_AES_CBC || enccrd->crd_alg ==
343 CRYPTO_AES_XTS) && (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
349 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
350 if (ses->id == (crp->crp_sid & 0xffffffff))
353 rw_runlock(&sc->lock);
359 error = aesni_cipher_process(ses, enccrd, authcrd, crp);
364 crp->crp_etype = error;
370 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
378 if (crp->crp_flags & CRYPTO_F_IMBUF) {
379 m = (struct mbuf *)crp->crp_buf;
380 if (m->m_next != NULL)
382 addr = mtod(m, uint8_t *);
383 } else if (crp->crp_flags & CRYPTO_F_IOV) {
384 uio = (struct uio *)crp->crp_buf;
385 if (uio->uio_iovcnt != 1)
388 addr = (uint8_t *)iov->iov_base;
390 addr = (uint8_t *)crp->crp_buf;
392 addr += enccrd->crd_skip;
396 addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
399 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
400 enccrd->crd_len, addr);
406 static device_method_t aesni_methods[] = {
407 DEVMETHOD(device_identify, aesni_identify),
408 DEVMETHOD(device_probe, aesni_probe),
409 DEVMETHOD(device_attach, aesni_attach),
410 DEVMETHOD(device_detach, aesni_detach),
412 DEVMETHOD(cryptodev_newsession, aesni_newsession),
413 DEVMETHOD(cryptodev_freesession, aesni_freesession),
414 DEVMETHOD(cryptodev_process, aesni_process),
419 static driver_t aesni_driver = {
422 sizeof(struct aesni_softc),
424 static devclass_t aesni_devclass;
426 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
427 MODULE_VERSION(aesni, 1);
428 MODULE_DEPEND(aesni, crypto, 1, 1, 1);
431 aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini)
437 error = fpu_kern_enter(td, ses->fpu_ctx, FPU_KERN_NORMAL |
441 error = aesni_cipher_setup_common(ses, encini->cri_key,
443 fpu_kern_leave(td, ses->fpu_ctx);
448 * authcrd contains the associated date.
451 aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
452 struct cryptodesc *authcrd, struct cryptop *crp)
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 */
509 bzero(ses->iv, sizeof ses->iv);
510 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
511 bcopy(enccrd->crd_iv, ses->iv, ivlen);
512 else if (encflag && ((enccrd->crd_flags & CRD_F_IV_PRESENT) != 0))
513 arc4rand(ses->iv, ivlen, 0);
515 crypto_copydata(crp->crp_flags, crp->crp_buf,
516 enccrd->crd_inject, ivlen, ses->iv);
518 if (authcrd != NULL && !encflag)
519 crypto_copydata(crp->crp_flags, crp->crp_buf,
520 authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
522 bzero(tag, sizeof tag);
528 aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
529 enccrd->crd_len, buf, buf, ses->iv);
531 aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
532 enccrd->crd_len, buf, ses->iv);
535 /* encryption & decryption are the same */
536 aesni_encrypt_icm(ses->rounds, ses->enc_schedule,
537 enccrd->crd_len, buf, buf, ses->iv);
541 aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
542 ses->xts_schedule, enccrd->crd_len, buf, buf,
545 aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
546 ses->xts_schedule, enccrd->crd_len, buf, buf,
549 case CRYPTO_AES_NIST_GCM_16:
551 AES_GCM_encrypt(buf, buf, authbuf, ses->iv, tag,
552 enccrd->crd_len, authcrd->crd_len, ivlen,
553 ses->enc_schedule, ses->rounds);
555 if (!AES_GCM_decrypt(buf, buf, authbuf, ses->iv, tag,
556 enccrd->crd_len, authcrd->crd_len, ivlen,
557 ses->enc_schedule, ses->rounds))
564 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
565 enccrd->crd_len, buf);
568 * OpenBSD doesn't copy this back. This primes the IV for the next
569 * chain. Why do we not do it for decrypt?
571 if (encflag && enccrd->crd_alg == CRYPTO_AES_CBC)
572 bcopy(buf + enccrd->crd_len - AES_BLOCK_LEN, ses->iv, AES_BLOCK_LEN);
574 if (!error && authcrd != NULL) {
575 crypto_copyback(crp->crp_flags, crp->crp_buf,
576 authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
580 fpu_kern_leave(td, ses->fpu_ctx);
583 bzero(buf, enccrd->crd_len);
587 free(authbuf, M_AESNI);