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
5 * Copyright (c) 2020 Ampere Computing
8 * Portions of this software were developed by John-Mark Gurney
9 * under sponsorship of the FreeBSD Foundation and
10 * Rubicon Communications, LLC (Netgate).
12 * This software was developed by Andrew Turner under
13 * sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * This is based on the aesni code.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/endian.h>
50 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <sys/mutex.h>
54 #include <sys/queue.h>
55 #include <sys/rwlock.h>
59 #include <machine/vfp.h>
61 #include <opencrypto/cryptodev.h>
62 #include <opencrypto/gmac.h>
63 #include <cryptodev_if.h>
64 #include <crypto/armv8/armv8_crypto.h>
65 #include <crypto/rijndael/rijndael.h>
67 struct armv8_crypto_softc {
74 static struct mtx *ctx_mtx;
75 static struct fpu_kern_ctx **ctx_vfp;
77 #define AQUIRE_CTX(i, ctx) \
79 (i) = PCPU_GET(cpuid); \
80 mtx_lock(&ctx_mtx[(i)]); \
81 (ctx) = ctx_vfp[(i)]; \
83 #define RELEASE_CTX(i, ctx) \
85 mtx_unlock(&ctx_mtx[(i)]); \
90 static int armv8_crypto_cipher_process(struct armv8_crypto_session *,
93 MALLOC_DEFINE(M_ARMV8_CRYPTO, "armv8_crypto", "ARMv8 Crypto Data");
96 armv8_crypto_identify(driver_t *drv, device_t parent)
99 /* NB: order 10 is so we get attached after h/w devices */
100 if (device_find_child(parent, "armv8crypto", -1) == NULL &&
101 BUS_ADD_CHILD(parent, 10, "armv8crypto", -1) == 0)
102 panic("ARMv8 crypto: could not attach");
106 armv8_crypto_probe(device_t dev)
111 reg = READ_SPECIALREG(id_aa64isar0_el1);
113 switch (ID_AA64ISAR0_AES_VAL(reg)) {
114 case ID_AA64ISAR0_AES_BASE:
116 device_set_desc(dev, "AES-CBC,AES-XTS");
118 case ID_AA64ISAR0_AES_PMULL:
120 device_set_desc(dev, "AES-CBC,AES-XTS,AES-GCM");
124 case ID_AA64ISAR0_AES_NONE:
125 device_printf(dev, "CPU lacks AES instructions\n");
129 /* TODO: Check more fields as we support more features */
135 armv8_crypto_attach(device_t dev)
137 struct armv8_crypto_softc *sc;
141 sc = device_get_softc(dev);
144 reg = READ_SPECIALREG(id_aa64isar0_el1);
146 if (ID_AA64ISAR0_AES_VAL(reg) == ID_AA64ISAR0_AES_PMULL)
149 sc->cid = crypto_get_driverid(dev, sizeof(struct armv8_crypto_session),
150 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC | CRYPTOCAP_F_ACCEL_SOFTWARE);
152 device_printf(dev, "Could not get crypto driver id.\n");
156 rw_init(&sc->lock, "armv8crypto");
158 ctx_mtx = malloc(sizeof(*ctx_mtx) * (mp_maxid + 1), M_ARMV8_CRYPTO,
160 ctx_vfp = malloc(sizeof(*ctx_vfp) * (mp_maxid + 1), M_ARMV8_CRYPTO,
164 ctx_vfp[i] = fpu_kern_alloc_ctx(0);
165 mtx_init(&ctx_mtx[i], "armv8cryptoctx", NULL, MTX_DEF|MTX_NEW);
172 armv8_crypto_detach(device_t dev)
174 struct armv8_crypto_softc *sc;
177 sc = device_get_softc(dev);
181 rw_wunlock(&sc->lock);
182 crypto_unregister_all(sc->cid);
184 rw_destroy(&sc->lock);
187 if (ctx_vfp[i] != NULL) {
188 mtx_destroy(&ctx_mtx[i]);
189 fpu_kern_free_ctx(ctx_vfp[i]);
193 free(ctx_mtx, M_ARMV8_CRYPTO);
195 free(ctx_vfp, M_ARMV8_CRYPTO);
201 #define SUPPORTED_SES (CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD)
204 armv8_crypto_probesession(device_t dev,
205 const struct crypto_session_params *csp)
207 struct armv8_crypto_softc *sc;
209 sc = device_get_softc(dev);
211 if ((csp->csp_flags & ~(SUPPORTED_SES)) != 0)
214 switch (csp->csp_mode) {
216 switch (csp->csp_cipher_alg) {
217 case CRYPTO_AES_NIST_GCM_16:
220 if (csp->csp_ivlen != AES_GCM_IV_LEN)
222 if (csp->csp_auth_mlen != 0 &&
223 csp->csp_auth_mlen != GMAC_DIGEST_LEN)
225 switch (csp->csp_cipher_klen * 8) {
238 case CSP_MODE_CIPHER:
239 switch (csp->csp_cipher_alg) {
241 if (csp->csp_ivlen != AES_BLOCK_LEN)
243 switch (csp->csp_cipher_klen * 8) {
253 if (csp->csp_ivlen != AES_XTS_IV_LEN)
255 switch (csp->csp_cipher_klen * 8) {
270 return (CRYPTODEV_PROBE_ACCEL_SOFTWARE);
274 armv8_crypto_cipher_setup(struct armv8_crypto_session *ses,
275 const struct crypto_session_params *csp, const uint8_t *key, int keylen)
278 struct fpu_kern_ctx *ctx;
281 if (csp->csp_cipher_alg == CRYPTO_AES_XTS)
284 switch (keylen * 8) {
293 kt = is_fpu_kern_thread(0);
296 fpu_kern_enter(curthread, ctx,
297 FPU_KERN_NORMAL | FPU_KERN_KTHR);
300 aes_v8_set_encrypt_key(key,
301 keylen * 8, &ses->enc_schedule);
303 if ((csp->csp_cipher_alg == CRYPTO_AES_XTS) ||
304 (csp->csp_cipher_alg == CRYPTO_AES_CBC))
305 aes_v8_set_decrypt_key(key,
306 keylen * 8, &ses->dec_schedule);
308 if (csp->csp_cipher_alg == CRYPTO_AES_XTS)
309 aes_v8_set_encrypt_key(key + keylen, keylen * 8, &ses->xts_schedule);
311 if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16) {
312 memset(H.c, 0, sizeof(H.c));
313 aes_v8_encrypt(H.c, H.c, &ses->enc_schedule);
314 H.u[0] = bswap64(H.u[0]);
315 H.u[1] = bswap64(H.u[1]);
316 gcm_init_v8(ses->Htable, H.u);
320 fpu_kern_leave(curthread, ctx);
328 armv8_crypto_newsession(device_t dev, crypto_session_t cses,
329 const struct crypto_session_params *csp)
331 struct armv8_crypto_softc *sc;
332 struct armv8_crypto_session *ses;
335 sc = device_get_softc(dev);
338 rw_wunlock(&sc->lock);
342 ses = crypto_get_driver_session(cses);
343 error = armv8_crypto_cipher_setup(ses, csp, csp->csp_cipher_key,
344 csp->csp_cipher_klen);
345 rw_wunlock(&sc->lock);
350 armv8_crypto_process(device_t dev, struct cryptop *crp, int hint __unused)
352 struct armv8_crypto_session *ses;
354 ses = crypto_get_driver_session(crp->crp_session);
355 crp->crp_etype = armv8_crypto_cipher_process(ses, crp);
361 armv8_crypto_cipher_alloc(struct cryptop *crp, int start, int length, int *allocated)
365 addr = crypto_contiguous_subsegment(crp, start, length);
370 addr = malloc(crp->crp_payload_length, M_ARMV8_CRYPTO, M_NOWAIT);
373 crypto_copydata(crp, start, length, addr);
380 armv8_crypto_cipher_process(struct armv8_crypto_session *ses,
383 const struct crypto_session_params *csp;
384 struct fpu_kern_ctx *ctx;
385 uint8_t *buf, *authbuf, *outbuf;
386 uint8_t iv[AES_BLOCK_LEN], tag[GMAC_DIGEST_LEN];
387 int allocated, authallocated, outallocated, i;
393 csp = crypto_get_params(crp->crp_session);
394 encflag = CRYPTO_OP_IS_ENCRYPT(crp->crp_op);
402 buf = armv8_crypto_cipher_alloc(crp, crp->crp_payload_start,
403 crp->crp_payload_length, &allocated);
407 if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16) {
408 if (crp->crp_aad != NULL)
409 authbuf = crp->crp_aad;
411 authbuf = armv8_crypto_cipher_alloc(crp, crp->crp_aad_start,
412 crp->crp_aad_length, &authallocated);
413 if (authbuf == NULL) {
419 if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
420 outbuf = crypto_buffer_contiguous_subsegment(&crp->crp_obuf,
421 crp->crp_payload_output_start, crp->crp_payload_length);
422 if (outbuf == NULL) {
427 outbuf = malloc(crp->crp_payload_length,
428 M_ARMV8_CRYPTO, M_NOWAIT);
429 if (outbuf == NULL) {
442 kt = is_fpu_kern_thread(0);
445 fpu_kern_enter(curthread, ctx,
446 FPU_KERN_NORMAL | FPU_KERN_KTHR);
449 if (crp->crp_cipher_key != NULL) {
450 armv8_crypto_cipher_setup(ses, csp, crp->crp_cipher_key,
451 csp->csp_cipher_klen);
454 crypto_read_iv(crp, iv);
457 switch (csp->csp_cipher_alg) {
459 if ((crp->crp_payload_length % AES_BLOCK_LEN) != 0) {
464 armv8_aes_encrypt_cbc(&ses->enc_schedule,
465 crp->crp_payload_length, buf, buf, iv);
467 armv8_aes_decrypt_cbc(&ses->dec_schedule,
468 crp->crp_payload_length, buf, iv);
472 armv8_aes_encrypt_xts(&ses->enc_schedule,
473 &ses->xts_schedule.aes_key, crp->crp_payload_length, buf,
476 armv8_aes_decrypt_xts(&ses->dec_schedule,
477 &ses->xts_schedule.aes_key, crp->crp_payload_length, buf,
480 case CRYPTO_AES_NIST_GCM_16:
482 memset(tag, 0, sizeof(tag));
483 armv8_aes_encrypt_gcm(&ses->enc_schedule,
484 crp->crp_payload_length,
486 crp->crp_aad_length, authbuf,
487 tag, iv, ses->Htable);
488 crypto_copyback(crp, crp->crp_digest_start, sizeof(tag),
491 crypto_copydata(crp, crp->crp_digest_start, sizeof(tag),
493 if (armv8_aes_decrypt_gcm(&ses->enc_schedule,
494 crp->crp_payload_length,
496 crp->crp_aad_length, authbuf,
497 tag, iv, ses->Htable) != 0) {
506 crypto_copyback(crp, CRYPTO_HAS_OUTPUT_BUFFER(crp) ?
507 crp->crp_payload_output_start : crp->crp_payload_start,
508 crp->crp_payload_length, outbuf);
513 fpu_kern_leave(curthread, ctx);
518 zfree(buf, M_ARMV8_CRYPTO);
520 zfree(authbuf, M_ARMV8_CRYPTO);
522 zfree(outbuf, M_ARMV8_CRYPTO);
523 explicit_bzero(iv, sizeof(iv));
524 explicit_bzero(tag, sizeof(tag));
529 static device_method_t armv8_crypto_methods[] = {
530 DEVMETHOD(device_identify, armv8_crypto_identify),
531 DEVMETHOD(device_probe, armv8_crypto_probe),
532 DEVMETHOD(device_attach, armv8_crypto_attach),
533 DEVMETHOD(device_detach, armv8_crypto_detach),
535 DEVMETHOD(cryptodev_probesession, armv8_crypto_probesession),
536 DEVMETHOD(cryptodev_newsession, armv8_crypto_newsession),
537 DEVMETHOD(cryptodev_process, armv8_crypto_process),
542 static DEFINE_CLASS_0(armv8crypto, armv8_crypto_driver, armv8_crypto_methods,
543 sizeof(struct armv8_crypto_softc));
544 static devclass_t armv8_crypto_devclass;
546 DRIVER_MODULE(armv8crypto, nexus, armv8_crypto_driver, armv8_crypto_devclass,