2 * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
4 * Copyright (c) 2014,2016 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 * This software was developed by Andrew Turner under
12 * sponsorship from the FreeBSD Foundation.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * This is based on the aesni code.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/endian.h>
48 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/queue.h>
52 #include <sys/rwlock.h>
56 #include <machine/vfp.h>
58 #include <opencrypto/cryptodev.h>
59 #include <cryptodev_if.h>
60 #include <crypto/armv8/armv8_crypto.h>
61 #include <crypto/rijndael/rijndael.h>
63 struct armv8_crypto_softc {
67 TAILQ_HEAD(armv8_crypto_sessions_head, armv8_crypto_session) sessions;
71 static struct mtx *ctx_mtx;
72 static struct fpu_kern_ctx **ctx_vfp;
74 #define AQUIRE_CTX(i, ctx) \
76 (i) = PCPU_GET(cpuid); \
77 mtx_lock(&ctx_mtx[(i)]); \
78 (ctx) = ctx_vfp[(i)]; \
80 #define RELEASE_CTX(i, ctx) \
82 mtx_unlock(&ctx_mtx[(i)]); \
87 static void armv8_crypto_freesession_locked(struct armv8_crypto_softc *,
88 struct armv8_crypto_session *);
89 static int armv8_crypto_cipher_process(struct armv8_crypto_session *,
90 struct cryptodesc *, struct cryptop *);
92 MALLOC_DEFINE(M_ARMV8_CRYPTO, "armv8_crypto", "ARMv8 Crypto Data");
95 armv8_crypto_identify(driver_t *drv, device_t parent)
98 /* NB: order 10 is so we get attached after h/w devices */
99 if (device_find_child(parent, "armv8crypto", -1) == NULL &&
100 BUS_ADD_CHILD(parent, 10, "armv8crypto", -1) == 0)
101 panic("ARMv8 crypto: could not attach");
105 armv8_crypto_probe(device_t dev)
110 reg = READ_SPECIALREG(id_aa64isar0_el1);
112 switch (ID_AA64ISAR0_AES(reg)) {
113 case ID_AA64ISAR0_AES_BASE:
114 case ID_AA64ISAR0_AES_PMULL:
119 device_set_desc_copy(dev, "AES-CBC");
121 /* TODO: Check more fields as we support more features */
127 armv8_crypto_attach(device_t dev)
129 struct armv8_crypto_softc *sc;
132 sc = device_get_softc(dev);
133 TAILQ_INIT(&sc->sessions);
137 sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE |
140 device_printf(dev, "Could not get crypto driver id.\n");
144 rw_init(&sc->lock, "armv8crypto");
146 ctx_mtx = malloc(sizeof(*ctx_mtx) * (mp_maxid + 1), M_ARMV8_CRYPTO,
148 ctx_vfp = malloc(sizeof(*ctx_vfp) * (mp_maxid + 1), M_ARMV8_CRYPTO,
152 ctx_vfp[i] = fpu_kern_alloc_ctx(0);
153 mtx_init(&ctx_mtx[i], "armv8cryptoctx", NULL, MTX_DEF|MTX_NEW);
156 crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
162 armv8_crypto_detach(device_t dev)
164 struct armv8_crypto_softc *sc;
165 struct armv8_crypto_session *ses;
168 sc = device_get_softc(dev);
171 TAILQ_FOREACH(ses, &sc->sessions, next) {
173 rw_wunlock(&sc->lock);
175 "Cannot detach, sessions still active.\n");
180 while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) {
181 TAILQ_REMOVE(&sc->sessions, ses, next);
182 free(ses, M_ARMV8_CRYPTO);
184 rw_wunlock(&sc->lock);
185 crypto_unregister_all(sc->cid);
187 rw_destroy(&sc->lock);
190 if (ctx_vfp[i] != NULL) {
191 mtx_destroy(&ctx_mtx[i]);
192 fpu_kern_free_ctx(ctx_vfp[i]);
196 free(ctx_mtx, M_ARMV8_CRYPTO);
198 free(ctx_vfp, M_ARMV8_CRYPTO);
205 armv8_crypto_cipher_setup(struct armv8_crypto_session *ses,
206 struct cryptoini *encini)
212 switch (encini->cri_klen) {
214 ses->rounds = AES128_ROUNDS;
217 ses->rounds = AES192_ROUNDS;
220 ses->rounds = AES256_ROUNDS;
223 CRYPTDEB("invalid CBC/ICM/GCM key length");
231 rijndaelKeySetupEnc(ses->enc_schedule, encini->cri_key,
233 rijndaelKeySetupDec(ses->dec_schedule, encini->cri_key,
235 for (i = 0; i < nitems(ses->enc_schedule); i++) {
236 ses->enc_schedule[i] = bswap32(ses->enc_schedule[i]);
237 ses->dec_schedule[i] = bswap32(ses->dec_schedule[i]);
244 armv8_crypto_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
246 struct armv8_crypto_softc *sc;
247 struct armv8_crypto_session *ses;
248 struct cryptoini *encini;
251 if (sidp == NULL || cri == NULL) {
252 CRYPTDEB("no sidp or cri");
256 sc = device_get_softc(dev);
262 for (; cri != NULL; cri = cri->cri_next) {
263 switch (cri->cri_alg) {
265 if (encini != NULL) {
266 CRYPTDEB("encini already set");
272 CRYPTDEB("unhandled algorithm");
276 if (encini == NULL) {
277 CRYPTDEB("no cipher");
283 rw_wunlock(&sc->lock);
288 * Free sessions goes first, so if first session is used, we need to
291 ses = TAILQ_FIRST(&sc->sessions);
292 if (ses == NULL || ses->used) {
293 ses = malloc(sizeof(*ses), M_ARMV8_CRYPTO, M_NOWAIT | M_ZERO);
295 rw_wunlock(&sc->lock);
300 TAILQ_REMOVE(&sc->sessions, ses, next);
303 TAILQ_INSERT_TAIL(&sc->sessions, ses, next);
304 rw_wunlock(&sc->lock);
305 ses->algo = encini->cri_alg;
307 error = armv8_crypto_cipher_setup(ses, encini);
309 CRYPTDEB("setup failed");
311 armv8_crypto_freesession_locked(sc, ses);
312 rw_wunlock(&sc->lock);
321 armv8_crypto_freesession_locked(struct armv8_crypto_softc *sc,
322 struct armv8_crypto_session *ses)
326 rw_assert(&sc->lock, RA_WLOCKED);
329 TAILQ_REMOVE(&sc->sessions, ses, next);
330 *ses = (struct armv8_crypto_session){};
332 TAILQ_INSERT_HEAD(&sc->sessions, ses, next);
336 armv8_crypto_freesession(device_t dev, uint64_t tid)
338 struct armv8_crypto_softc *sc;
339 struct armv8_crypto_session *ses;
342 sc = device_get_softc(dev);
343 sid = ((uint32_t)tid) & 0xffffffff;
345 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, armv8_crypto_sessions_head,
351 rw_wunlock(&sc->lock);
354 armv8_crypto_freesession_locked(sc, ses);
355 rw_wunlock(&sc->lock);
361 armv8_crypto_process(device_t dev, struct cryptop *crp, int hint __unused)
363 struct armv8_crypto_softc *sc = device_get_softc(dev);
364 struct cryptodesc *crd, *enccrd;
365 struct armv8_crypto_session *ses;
375 if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
380 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
381 switch (crd->crd_alg) {
383 if (enccrd != NULL) {
395 if (enccrd == NULL) {
400 /* We can only handle full blocks for now */
401 if ((enccrd->crd_len % AES_BLOCK_LEN) != 0) {
407 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, armv8_crypto_sessions_head,
409 if (ses->id == (crp->crp_sid & 0xffffffff))
412 rw_runlock(&sc->lock);
418 error = armv8_crypto_cipher_process(ses, enccrd, crp);
421 crp->crp_etype = error;
427 armv8_crypto_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
435 if (crp->crp_flags & CRYPTO_F_IMBUF) {
436 m = (struct mbuf *)crp->crp_buf;
437 if (m->m_next != NULL)
439 addr = mtod(m, uint8_t *);
440 } else if (crp->crp_flags & CRYPTO_F_IOV) {
441 uio = (struct uio *)crp->crp_buf;
442 if (uio->uio_iovcnt != 1)
445 addr = (uint8_t *)iov->iov_base;
447 addr = (uint8_t *)crp->crp_buf;
449 addr += enccrd->crd_skip;
453 addr = malloc(enccrd->crd_len, M_ARMV8_CRYPTO, M_NOWAIT);
456 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
457 enccrd->crd_len, addr);
464 armv8_crypto_cipher_process(struct armv8_crypto_session *ses,
465 struct cryptodesc *enccrd, struct cryptop *crp)
467 struct fpu_kern_ctx *ctx;
469 uint8_t iv[AES_BLOCK_LEN];
474 encflag = (enccrd->crd_flags & CRD_F_ENCRYPT) == CRD_F_ENCRYPT;
476 buf = armv8_crypto_cipher_alloc(enccrd, crp, &allocated);
480 kt = is_fpu_kern_thread(0);
483 fpu_kern_enter(curthread, ctx,
484 FPU_KERN_NORMAL | FPU_KERN_KTHR);
487 if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
488 panic("CRD_F_KEY_EXPLICIT");
491 switch (enccrd->crd_alg) {
493 ivlen = AES_BLOCK_LEN;
499 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
500 bcopy(enccrd->crd_iv, iv, ivlen);
502 arc4rand(iv, ivlen, 0);
504 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
505 crypto_copyback(crp->crp_flags, crp->crp_buf,
506 enccrd->crd_inject, ivlen, iv);
508 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
509 bcopy(enccrd->crd_iv, iv, ivlen);
511 crypto_copydata(crp->crp_flags, crp->crp_buf,
512 enccrd->crd_inject, ivlen, iv);
519 armv8_aes_encrypt_cbc(ses->rounds, ses->enc_schedule,
520 enccrd->crd_len, buf, buf, iv);
522 armv8_aes_decrypt_cbc(ses->rounds, ses->dec_schedule,
523 enccrd->crd_len, buf, iv);
528 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
529 enccrd->crd_len, buf);
532 fpu_kern_leave(curthread, ctx);
536 bzero(buf, enccrd->crd_len);
537 free(buf, M_ARMV8_CRYPTO);
542 static device_method_t armv8_crypto_methods[] = {
543 DEVMETHOD(device_identify, armv8_crypto_identify),
544 DEVMETHOD(device_probe, armv8_crypto_probe),
545 DEVMETHOD(device_attach, armv8_crypto_attach),
546 DEVMETHOD(device_detach, armv8_crypto_detach),
548 DEVMETHOD(cryptodev_newsession, armv8_crypto_newsession),
549 DEVMETHOD(cryptodev_freesession, armv8_crypto_freesession),
550 DEVMETHOD(cryptodev_process, armv8_crypto_process),
555 static DEFINE_CLASS_0(armv8crypto, armv8_crypto_driver, armv8_crypto_methods,
556 sizeof(struct armv8_crypto_softc));
557 static devclass_t armv8_crypto_devclass;
559 DRIVER_MODULE(armv8crypto, nexus, armv8_crypto_driver, armv8_crypto_devclass,