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>
49 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/queue.h>
54 #include <sys/rwlock.h>
58 #include <machine/vfp.h>
60 #include <opencrypto/cryptodev.h>
61 #include <cryptodev_if.h>
62 #include <crypto/armv8/armv8_crypto.h>
63 #include <crypto/rijndael/rijndael.h>
65 struct armv8_crypto_softc {
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 int armv8_crypto_cipher_process(struct armv8_crypto_session *,
88 struct cryptodesc *, struct cryptop *);
90 MALLOC_DEFINE(M_ARMV8_CRYPTO, "armv8_crypto", "ARMv8 Crypto Data");
93 armv8_crypto_identify(driver_t *drv, device_t parent)
96 /* NB: order 10 is so we get attached after h/w devices */
97 if (device_find_child(parent, "armv8crypto", -1) == NULL &&
98 BUS_ADD_CHILD(parent, 10, "armv8crypto", -1) == 0)
99 panic("ARMv8 crypto: could not attach");
103 armv8_crypto_probe(device_t dev)
108 reg = READ_SPECIALREG(id_aa64isar0_el1);
110 switch (ID_AA64ISAR0_AES(reg)) {
111 case ID_AA64ISAR0_AES_BASE:
112 case ID_AA64ISAR0_AES_PMULL:
117 device_set_desc_copy(dev, "AES-CBC");
119 /* TODO: Check more fields as we support more features */
125 armv8_crypto_attach(device_t dev)
127 struct armv8_crypto_softc *sc;
130 sc = device_get_softc(dev);
133 sc->cid = crypto_get_driverid(dev, sizeof(struct armv8_crypto_session),
134 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
136 device_printf(dev, "Could not get crypto driver id.\n");
140 rw_init(&sc->lock, "armv8crypto");
142 ctx_mtx = malloc(sizeof(*ctx_mtx) * (mp_maxid + 1), M_ARMV8_CRYPTO,
144 ctx_vfp = malloc(sizeof(*ctx_vfp) * (mp_maxid + 1), M_ARMV8_CRYPTO,
148 ctx_vfp[i] = fpu_kern_alloc_ctx(0);
149 mtx_init(&ctx_mtx[i], "armv8cryptoctx", NULL, MTX_DEF|MTX_NEW);
152 crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
158 armv8_crypto_detach(device_t dev)
160 struct armv8_crypto_softc *sc;
163 sc = device_get_softc(dev);
167 rw_wunlock(&sc->lock);
168 crypto_unregister_all(sc->cid);
170 rw_destroy(&sc->lock);
173 if (ctx_vfp[i] != NULL) {
174 mtx_destroy(&ctx_mtx[i]);
175 fpu_kern_free_ctx(ctx_vfp[i]);
179 free(ctx_mtx, M_ARMV8_CRYPTO);
181 free(ctx_vfp, M_ARMV8_CRYPTO);
188 armv8_crypto_cipher_setup(struct armv8_crypto_session *ses,
189 struct cryptoini *encini)
195 switch (encini->cri_klen) {
197 ses->rounds = AES128_ROUNDS;
200 ses->rounds = AES192_ROUNDS;
203 ses->rounds = AES256_ROUNDS;
206 CRYPTDEB("invalid CBC/ICM/GCM key length");
214 rijndaelKeySetupEnc(ses->enc_schedule, encini->cri_key,
216 rijndaelKeySetupDec(ses->dec_schedule, encini->cri_key,
218 for (i = 0; i < nitems(ses->enc_schedule); i++) {
219 ses->enc_schedule[i] = bswap32(ses->enc_schedule[i]);
220 ses->dec_schedule[i] = bswap32(ses->dec_schedule[i]);
227 armv8_crypto_newsession(device_t dev, crypto_session_t cses,
228 struct cryptoini *cri)
230 struct armv8_crypto_softc *sc;
231 struct armv8_crypto_session *ses;
232 struct cryptoini *encini;
240 sc = device_get_softc(dev);
246 for (; cri != NULL; cri = cri->cri_next) {
247 switch (cri->cri_alg) {
249 if (encini != NULL) {
250 CRYPTDEB("encini already set");
256 CRYPTDEB("unhandled algorithm");
260 if (encini == NULL) {
261 CRYPTDEB("no cipher");
267 rw_wunlock(&sc->lock);
271 ses = crypto_get_driver_session(cses);
272 ses->algo = encini->cri_alg;
274 error = armv8_crypto_cipher_setup(ses, encini);
276 CRYPTDEB("setup failed");
277 rw_wunlock(&sc->lock);
281 rw_wunlock(&sc->lock);
286 armv8_crypto_process(device_t dev, struct cryptop *crp, int hint __unused)
288 struct cryptodesc *crd, *enccrd;
289 struct armv8_crypto_session *ses;
299 if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
304 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
305 switch (crd->crd_alg) {
307 if (enccrd != NULL) {
319 if (enccrd == NULL) {
324 /* We can only handle full blocks for now */
325 if ((enccrd->crd_len % AES_BLOCK_LEN) != 0) {
330 ses = crypto_get_driver_session(crp->crp_session);
331 error = armv8_crypto_cipher_process(ses, enccrd, crp);
334 crp->crp_etype = error;
340 armv8_crypto_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
348 if (crp->crp_flags & CRYPTO_F_IMBUF) {
349 m = (struct mbuf *)crp->crp_buf;
350 if (m->m_next != NULL)
352 addr = mtod(m, uint8_t *);
353 } else if (crp->crp_flags & CRYPTO_F_IOV) {
354 uio = (struct uio *)crp->crp_buf;
355 if (uio->uio_iovcnt != 1)
358 addr = (uint8_t *)iov->iov_base;
360 addr = (uint8_t *)crp->crp_buf;
362 addr += enccrd->crd_skip;
366 addr = malloc(enccrd->crd_len, M_ARMV8_CRYPTO, M_NOWAIT);
369 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
370 enccrd->crd_len, addr);
377 armv8_crypto_cipher_process(struct armv8_crypto_session *ses,
378 struct cryptodesc *enccrd, struct cryptop *crp)
380 struct fpu_kern_ctx *ctx;
382 uint8_t iv[AES_BLOCK_LEN];
387 encflag = (enccrd->crd_flags & CRD_F_ENCRYPT) == CRD_F_ENCRYPT;
389 buf = armv8_crypto_cipher_alloc(enccrd, crp, &allocated);
393 kt = is_fpu_kern_thread(0);
396 fpu_kern_enter(curthread, ctx,
397 FPU_KERN_NORMAL | FPU_KERN_KTHR);
400 if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
401 panic("CRD_F_KEY_EXPLICIT");
404 switch (enccrd->crd_alg) {
406 ivlen = AES_BLOCK_LEN;
412 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
413 bcopy(enccrd->crd_iv, iv, ivlen);
415 arc4rand(iv, ivlen, 0);
417 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
418 crypto_copyback(crp->crp_flags, crp->crp_buf,
419 enccrd->crd_inject, ivlen, iv);
421 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
422 bcopy(enccrd->crd_iv, iv, ivlen);
424 crypto_copydata(crp->crp_flags, crp->crp_buf,
425 enccrd->crd_inject, ivlen, iv);
432 armv8_aes_encrypt_cbc(ses->rounds, ses->enc_schedule,
433 enccrd->crd_len, buf, buf, iv);
435 armv8_aes_decrypt_cbc(ses->rounds, ses->dec_schedule,
436 enccrd->crd_len, buf, iv);
441 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
442 enccrd->crd_len, buf);
445 fpu_kern_leave(curthread, ctx);
449 bzero(buf, enccrd->crd_len);
450 free(buf, M_ARMV8_CRYPTO);
455 static device_method_t armv8_crypto_methods[] = {
456 DEVMETHOD(device_identify, armv8_crypto_identify),
457 DEVMETHOD(device_probe, armv8_crypto_probe),
458 DEVMETHOD(device_attach, armv8_crypto_attach),
459 DEVMETHOD(device_detach, armv8_crypto_detach),
461 DEVMETHOD(cryptodev_newsession, armv8_crypto_newsession),
462 DEVMETHOD(cryptodev_process, armv8_crypto_process),
467 static DEFINE_CLASS_0(armv8crypto, armv8_crypto_driver, armv8_crypto_methods,
468 sizeof(struct armv8_crypto_softc));
469 static devclass_t armv8_crypto_devclass;
471 DRIVER_MODULE(armv8crypto, nexus, armv8_crypto_driver, armv8_crypto_devclass,