xref: /freebsd/sys/crypto/armv8/armv8_crypto.c (revision c1a3d7f20696ab5b72eee45863f3e04410d81fc8)
1 /*-
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  * All rights reserved.
6  *
7  * Portions of this software were developed by John-Mark Gurney
8  * under sponsorship of the FreeBSD Foundation and
9  * Rubicon Communications, LLC (Netgate).
10  *
11  * This software was developed by Andrew Turner under
12  * sponsorship from the FreeBSD Foundation.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35 
36 /*
37  * This is based on the aesni code.
38  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/bus.h>
47 #include <sys/endian.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/queue.h>
54 #include <sys/rwlock.h>
55 #include <sys/smp.h>
56 #include <sys/uio.h>
57 
58 #include <machine/vfp.h>
59 
60 #include <opencrypto/cryptodev.h>
61 #include <cryptodev_if.h>
62 #include <crypto/armv8/armv8_crypto.h>
63 #include <crypto/rijndael/rijndael.h>
64 
65 struct armv8_crypto_softc {
66 	int		dieing;
67 	int32_t		cid;
68 	struct rwlock	lock;
69 };
70 
71 static struct mtx *ctx_mtx;
72 static struct fpu_kern_ctx **ctx_vfp;
73 
74 #define AQUIRE_CTX(i, ctx)					\
75 	do {							\
76 		(i) = PCPU_GET(cpuid);				\
77 		mtx_lock(&ctx_mtx[(i)]);			\
78 		(ctx) = ctx_vfp[(i)];				\
79 	} while (0)
80 #define RELEASE_CTX(i, ctx)					\
81 	do {							\
82 		mtx_unlock(&ctx_mtx[(i)]);			\
83 		(i) = -1;					\
84 		(ctx) = NULL;					\
85 	} while (0)
86 
87 static int armv8_crypto_cipher_process(struct armv8_crypto_session *,
88     struct cryptop *);
89 
90 MALLOC_DEFINE(M_ARMV8_CRYPTO, "armv8_crypto", "ARMv8 Crypto Data");
91 
92 static void
93 armv8_crypto_identify(driver_t *drv, device_t parent)
94 {
95 
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");
100 }
101 
102 static int
103 armv8_crypto_probe(device_t dev)
104 {
105 	uint64_t reg;
106 	int ret = ENXIO;
107 
108 	reg = READ_SPECIALREG(id_aa64isar0_el1);
109 
110 	switch (ID_AA64ISAR0_AES_VAL(reg)) {
111 	case ID_AA64ISAR0_AES_BASE:
112 	case ID_AA64ISAR0_AES_PMULL:
113 		ret = 0;
114 		break;
115 	case ID_AA64ISAR0_AES_NONE:
116 		device_printf(dev, "CPU lacks AES instructions");
117 		break;
118 	}
119 
120 	device_set_desc_copy(dev, "AES-CBC,AES-XTS");
121 
122 	/* TODO: Check more fields as we support more features */
123 
124 	return (ret);
125 }
126 
127 static int
128 armv8_crypto_attach(device_t dev)
129 {
130 	struct armv8_crypto_softc *sc;
131 	int i;
132 
133 	sc = device_get_softc(dev);
134 	sc->dieing = 0;
135 
136 	sc->cid = crypto_get_driverid(dev, sizeof(struct armv8_crypto_session),
137 	    CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC | CRYPTOCAP_F_ACCEL_SOFTWARE);
138 	if (sc->cid < 0) {
139 		device_printf(dev, "Could not get crypto driver id.\n");
140 		return (ENOMEM);
141 	}
142 
143 	rw_init(&sc->lock, "armv8crypto");
144 
145 	ctx_mtx = malloc(sizeof(*ctx_mtx) * (mp_maxid + 1), M_ARMV8_CRYPTO,
146 	    M_WAITOK|M_ZERO);
147 	ctx_vfp = malloc(sizeof(*ctx_vfp) * (mp_maxid + 1), M_ARMV8_CRYPTO,
148 	    M_WAITOK|M_ZERO);
149 
150 	CPU_FOREACH(i) {
151 		ctx_vfp[i] = fpu_kern_alloc_ctx(0);
152 		mtx_init(&ctx_mtx[i], "armv8cryptoctx", NULL, MTX_DEF|MTX_NEW);
153 	}
154 
155 	return (0);
156 }
157 
158 static int
159 armv8_crypto_detach(device_t dev)
160 {
161 	struct armv8_crypto_softc *sc;
162 	int i;
163 
164 	sc = device_get_softc(dev);
165 
166 	rw_wlock(&sc->lock);
167 	sc->dieing = 1;
168 	rw_wunlock(&sc->lock);
169 	crypto_unregister_all(sc->cid);
170 
171 	rw_destroy(&sc->lock);
172 
173 	CPU_FOREACH(i) {
174 		if (ctx_vfp[i] != NULL) {
175 			mtx_destroy(&ctx_mtx[i]);
176 			fpu_kern_free_ctx(ctx_vfp[i]);
177 		}
178 		ctx_vfp[i] = NULL;
179 	}
180 	free(ctx_mtx, M_ARMV8_CRYPTO);
181 	ctx_mtx = NULL;
182 	free(ctx_vfp, M_ARMV8_CRYPTO);
183 	ctx_vfp = NULL;
184 
185 	return (0);
186 }
187 
188 static int
189 armv8_crypto_probesession(device_t dev,
190     const struct crypto_session_params *csp)
191 {
192 
193 	if (csp->csp_flags != 0)
194 		return (EINVAL);
195 	switch (csp->csp_mode) {
196 	case CSP_MODE_CIPHER:
197 		switch (csp->csp_cipher_alg) {
198 		case CRYPTO_AES_CBC:
199 			if (csp->csp_ivlen != AES_BLOCK_LEN)
200 				return (EINVAL);
201 			switch (csp->csp_cipher_klen * 8) {
202 			case 128:
203 			case 192:
204 			case 256:
205 				break;
206 			default:
207 				return (EINVAL);
208 			}
209 			break;
210 		case CRYPTO_AES_XTS:
211 			if (csp->csp_ivlen != AES_XTS_IV_LEN)
212 				return (EINVAL);
213 			switch (csp->csp_cipher_klen * 8) {
214 			case 256:
215 			case 512:
216 				break;
217 			default:
218 				return (EINVAL);
219 			}
220 			break;
221 		default:
222 			return (EINVAL);
223 		}
224 		break;
225 	default:
226 		return (EINVAL);
227 	}
228 	return (CRYPTODEV_PROBE_ACCEL_SOFTWARE);
229 }
230 
231 static void
232 armv8_crypto_cipher_setup(struct armv8_crypto_session *ses,
233     const struct crypto_session_params *csp, const uint8_t *key, int keylen)
234 {
235 	int i;
236 
237 	if (csp->csp_cipher_alg == CRYPTO_AES_XTS)
238 		keylen /= 2;
239 
240 	switch (keylen * 8) {
241 	case 128:
242 		ses->rounds = AES128_ROUNDS;
243 		break;
244 	case 192:
245 		ses->rounds = AES192_ROUNDS;
246 		break;
247 	case 256:
248 		ses->rounds = AES256_ROUNDS;
249 		break;
250 	default:
251 		panic("invalid AES key length");
252 	}
253 
254 	rijndaelKeySetupEnc(ses->enc_schedule, key, keylen * 8);
255 	rijndaelKeySetupDec(ses->dec_schedule, key, keylen * 8);
256 	if (csp->csp_cipher_alg == CRYPTO_AES_XTS)
257 		rijndaelKeySetupEnc(ses->xts_schedule, key + keylen, keylen * 8);
258 
259 	for (i = 0; i < nitems(ses->enc_schedule); i++) {
260 		ses->enc_schedule[i] = bswap32(ses->enc_schedule[i]);
261 		ses->dec_schedule[i] = bswap32(ses->dec_schedule[i]);
262 		if (csp->csp_cipher_alg == CRYPTO_AES_XTS)
263 			ses->xts_schedule[i] = bswap32(ses->xts_schedule[i]);
264 	}
265 }
266 
267 static int
268 armv8_crypto_newsession(device_t dev, crypto_session_t cses,
269     const struct crypto_session_params *csp)
270 {
271 	struct armv8_crypto_softc *sc;
272 	struct armv8_crypto_session *ses;
273 
274 	sc = device_get_softc(dev);
275 	rw_wlock(&sc->lock);
276 	if (sc->dieing) {
277 		rw_wunlock(&sc->lock);
278 		return (EINVAL);
279 	}
280 
281 	ses = crypto_get_driver_session(cses);
282 	armv8_crypto_cipher_setup(ses, csp, csp->csp_cipher_key,
283 	    csp->csp_cipher_klen);
284 	rw_wunlock(&sc->lock);
285 	return (0);
286 }
287 
288 static int
289 armv8_crypto_process(device_t dev, struct cryptop *crp, int hint __unused)
290 {
291 	struct armv8_crypto_session *ses;
292 	int error;
293 
294 	/* We can only handle full blocks for now */
295 	if ((crp->crp_payload_length % AES_BLOCK_LEN) != 0) {
296 		error = EINVAL;
297 		goto out;
298 	}
299 
300 	ses = crypto_get_driver_session(crp->crp_session);
301 	error = armv8_crypto_cipher_process(ses, crp);
302 
303 out:
304 	crp->crp_etype = error;
305 	crypto_done(crp);
306 	return (0);
307 }
308 
309 static uint8_t *
310 armv8_crypto_cipher_alloc(struct cryptop *crp, int *allocated)
311 {
312 	uint8_t *addr;
313 
314 	addr = crypto_contiguous_subsegment(crp, crp->crp_payload_start,
315 	    crp->crp_payload_length);
316 	if (addr != NULL) {
317 		*allocated = 0;
318 		return (addr);
319 	}
320 	addr = malloc(crp->crp_payload_length, M_ARMV8_CRYPTO, M_NOWAIT);
321 	if (addr != NULL) {
322 		*allocated = 1;
323 		crypto_copydata(crp, crp->crp_payload_start,
324 		    crp->crp_payload_length, addr);
325 	} else
326 		*allocated = 0;
327 	return (addr);
328 }
329 
330 static int
331 armv8_crypto_cipher_process(struct armv8_crypto_session *ses,
332     struct cryptop *crp)
333 {
334 	const struct crypto_session_params *csp;
335 	struct fpu_kern_ctx *ctx;
336 	uint8_t *buf;
337 	uint8_t iv[AES_BLOCK_LEN];
338 	int allocated, i;
339 	int encflag;
340 	int kt;
341 
342 	csp = crypto_get_params(crp->crp_session);
343 	encflag = CRYPTO_OP_IS_ENCRYPT(crp->crp_op);
344 
345 	buf = armv8_crypto_cipher_alloc(crp, &allocated);
346 	if (buf == NULL)
347 		return (ENOMEM);
348 
349 	kt = is_fpu_kern_thread(0);
350 	if (!kt) {
351 		AQUIRE_CTX(i, ctx);
352 		fpu_kern_enter(curthread, ctx,
353 		    FPU_KERN_NORMAL | FPU_KERN_KTHR);
354 	}
355 
356 	if (crp->crp_cipher_key != NULL) {
357 		armv8_crypto_cipher_setup(ses, csp, crp->crp_cipher_key,
358 		    csp->csp_cipher_klen);
359 	}
360 
361 	crypto_read_iv(crp, iv);
362 
363 	/* Do work */
364 	switch (csp->csp_cipher_alg) {
365 	case CRYPTO_AES_CBC:
366 		if (encflag)
367 			armv8_aes_encrypt_cbc(ses->rounds, ses->enc_schedule,
368 			    crp->crp_payload_length, buf, buf, iv);
369 		else
370 			armv8_aes_decrypt_cbc(ses->rounds, ses->dec_schedule,
371 			    crp->crp_payload_length, buf, iv);
372 		break;
373 	case CRYPTO_AES_XTS:
374 		if (encflag)
375 			armv8_aes_encrypt_xts(ses->rounds, ses->enc_schedule,
376 			    ses->xts_schedule, crp->crp_payload_length, buf,
377 			    buf, iv);
378 		else
379 			armv8_aes_decrypt_xts(ses->rounds, ses->dec_schedule,
380 			    ses->xts_schedule, crp->crp_payload_length, buf,
381 			    buf, iv);
382 		break;
383 	}
384 
385 	if (allocated)
386 		crypto_copyback(crp, crp->crp_payload_start,
387 		    crp->crp_payload_length, buf);
388 
389 	if (!kt) {
390 		fpu_kern_leave(curthread, ctx);
391 		RELEASE_CTX(i, ctx);
392 	}
393 	if (allocated)
394 		zfree(buf, M_ARMV8_CRYPTO);
395 	return (0);
396 }
397 
398 static device_method_t armv8_crypto_methods[] = {
399 	DEVMETHOD(device_identify,	armv8_crypto_identify),
400 	DEVMETHOD(device_probe,		armv8_crypto_probe),
401 	DEVMETHOD(device_attach,	armv8_crypto_attach),
402 	DEVMETHOD(device_detach,	armv8_crypto_detach),
403 
404 	DEVMETHOD(cryptodev_probesession, armv8_crypto_probesession),
405 	DEVMETHOD(cryptodev_newsession,	armv8_crypto_newsession),
406 	DEVMETHOD(cryptodev_process,	armv8_crypto_process),
407 
408 	DEVMETHOD_END,
409 };
410 
411 static DEFINE_CLASS_0(armv8crypto, armv8_crypto_driver, armv8_crypto_methods,
412     sizeof(struct armv8_crypto_softc));
413 static devclass_t armv8_crypto_devclass;
414 
415 DRIVER_MODULE(armv8crypto, nexus, armv8_crypto_driver, armv8_crypto_devclass,
416     0, 0);
417