xref: /freebsd/sys/crypto/armv8/armv8_crypto.c (revision 2ef9ff7dd34a78a7890ba4d6de64da34d9c10942)
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 cryptodesc *, 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(reg)) {
111 	case ID_AA64ISAR0_AES_BASE:
112 	case ID_AA64ISAR0_AES_PMULL:
113 		ret = 0;
114 		break;
115 	}
116 
117 	device_set_desc_copy(dev, "AES-CBC");
118 
119 	/* TODO: Check more fields as we support more features */
120 
121 	return (ret);
122 }
123 
124 static int
125 armv8_crypto_attach(device_t dev)
126 {
127 	struct armv8_crypto_softc *sc;
128 	int i;
129 
130 	sc = device_get_softc(dev);
131 	sc->dieing = 0;
132 
133 	sc->cid = crypto_get_driverid(dev, sizeof(struct armv8_crypto_session),
134 	    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
135 	if (sc->cid < 0) {
136 		device_printf(dev, "Could not get crypto driver id.\n");
137 		return (ENOMEM);
138 	}
139 
140 	rw_init(&sc->lock, "armv8crypto");
141 
142 	ctx_mtx = malloc(sizeof(*ctx_mtx) * (mp_maxid + 1), M_ARMV8_CRYPTO,
143 	    M_WAITOK|M_ZERO);
144 	ctx_vfp = malloc(sizeof(*ctx_vfp) * (mp_maxid + 1), M_ARMV8_CRYPTO,
145 	    M_WAITOK|M_ZERO);
146 
147 	CPU_FOREACH(i) {
148 		ctx_vfp[i] = fpu_kern_alloc_ctx(0);
149 		mtx_init(&ctx_mtx[i], "armv8cryptoctx", NULL, MTX_DEF|MTX_NEW);
150 	}
151 
152 	crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
153 
154 	return (0);
155 }
156 
157 static int
158 armv8_crypto_detach(device_t dev)
159 {
160 	struct armv8_crypto_softc *sc;
161 	int i;
162 
163 	sc = device_get_softc(dev);
164 
165 	rw_wlock(&sc->lock);
166 	sc->dieing = 1;
167 	rw_wunlock(&sc->lock);
168 	crypto_unregister_all(sc->cid);
169 
170 	rw_destroy(&sc->lock);
171 
172 	CPU_FOREACH(i) {
173 		if (ctx_vfp[i] != NULL) {
174 			mtx_destroy(&ctx_mtx[i]);
175 			fpu_kern_free_ctx(ctx_vfp[i]);
176 		}
177 		ctx_vfp[i] = NULL;
178 	}
179 	free(ctx_mtx, M_ARMV8_CRYPTO);
180 	ctx_mtx = NULL;
181 	free(ctx_vfp, M_ARMV8_CRYPTO);
182 	ctx_vfp = NULL;
183 
184 	return (0);
185 }
186 
187 static int
188 armv8_crypto_cipher_setup(struct armv8_crypto_session *ses,
189     struct cryptoini *encini)
190 {
191 	int i;
192 
193 	switch (ses->algo) {
194 	case CRYPTO_AES_CBC:
195 		switch (encini->cri_klen) {
196 		case 128:
197 			ses->rounds = AES128_ROUNDS;
198 			break;
199 		case 192:
200 			ses->rounds = AES192_ROUNDS;
201 			break;
202 		case 256:
203 			ses->rounds = AES256_ROUNDS;
204 			break;
205 		default:
206 			CRYPTDEB("invalid CBC/ICM/GCM key length");
207 			return (EINVAL);
208 		}
209 		break;
210 	default:
211 		return (EINVAL);
212 	}
213 
214 	rijndaelKeySetupEnc(ses->enc_schedule, encini->cri_key,
215 	    encini->cri_klen);
216 	rijndaelKeySetupDec(ses->dec_schedule, encini->cri_key,
217 	    encini->cri_klen);
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]);
221 	}
222 
223 	return (0);
224 }
225 
226 static int
227 armv8_crypto_newsession(device_t dev, crypto_session_t cses,
228     struct cryptoini *cri)
229 {
230 	struct armv8_crypto_softc *sc;
231 	struct armv8_crypto_session *ses;
232 	struct cryptoini *encini;
233 	int error;
234 
235 	if (cri == NULL) {
236 		CRYPTDEB("no cri");
237 		return (EINVAL);
238 	}
239 
240 	sc = device_get_softc(dev);
241 	if (sc->dieing)
242 		return (EINVAL);
243 
244 	ses = NULL;
245 	encini = NULL;
246 	for (; cri != NULL; cri = cri->cri_next) {
247 		switch (cri->cri_alg) {
248 		case CRYPTO_AES_CBC:
249 			if (encini != NULL) {
250 				CRYPTDEB("encini already set");
251 				return (EINVAL);
252 			}
253 			encini = cri;
254 			break;
255 		default:
256 			CRYPTDEB("unhandled algorithm");
257 			return (EINVAL);
258 		}
259 	}
260 	if (encini == NULL) {
261 		CRYPTDEB("no cipher");
262 		return (EINVAL);
263 	}
264 
265 	rw_wlock(&sc->lock);
266 	if (sc->dieing) {
267 		rw_wunlock(&sc->lock);
268 		return (EINVAL);
269 	}
270 
271 	ses = crypto_get_driver_session(cses);
272 	ses->algo = encini->cri_alg;
273 
274 	error = armv8_crypto_cipher_setup(ses, encini);
275 	if (error != 0) {
276 		CRYPTDEB("setup failed");
277 		return (error);
278 	}
279 
280 	return (0);
281 }
282 
283 static int
284 armv8_crypto_process(device_t dev, struct cryptop *crp, int hint __unused)
285 {
286 	struct cryptodesc *crd, *enccrd;
287 	struct armv8_crypto_session *ses;
288 	int error;
289 
290 	error = 0;
291 	enccrd = NULL;
292 
293 	/* Sanity check. */
294 	if (crp == NULL)
295 		return (EINVAL);
296 
297 	if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
298 		error = EINVAL;
299 		goto out;
300 	}
301 
302 	for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
303 		switch (crd->crd_alg) {
304 		case CRYPTO_AES_CBC:
305 			if (enccrd != NULL) {
306 				error = EINVAL;
307 				goto out;
308 			}
309 			enccrd = crd;
310 			break;
311 		default:
312 			error = EINVAL;
313 			goto out;
314 		}
315 	}
316 
317 	if (enccrd == NULL) {
318 		error = EINVAL;
319 		goto out;
320 	}
321 
322 	/* We can only handle full blocks for now */
323 	if ((enccrd->crd_len % AES_BLOCK_LEN) != 0) {
324 		error = EINVAL;
325 		goto out;
326 	}
327 
328 	ses = crypto_get_driver_session(crp->crp_session);
329 	error = armv8_crypto_cipher_process(ses, enccrd, crp);
330 
331 out:
332 	crp->crp_etype = error;
333 	crypto_done(crp);
334 	return (error);
335 }
336 
337 static uint8_t *
338 armv8_crypto_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
339     int *allocated)
340 {
341 	struct mbuf *m;
342 	struct uio *uio;
343 	struct iovec *iov;
344 	uint8_t *addr;
345 
346 	if (crp->crp_flags & CRYPTO_F_IMBUF) {
347 		m = (struct mbuf *)crp->crp_buf;
348 		if (m->m_next != NULL)
349 			goto alloc;
350 		addr = mtod(m, uint8_t *);
351 	} else if (crp->crp_flags & CRYPTO_F_IOV) {
352 		uio = (struct uio *)crp->crp_buf;
353 		if (uio->uio_iovcnt != 1)
354 			goto alloc;
355 		iov = uio->uio_iov;
356 		addr = (uint8_t *)iov->iov_base;
357 	} else
358 		addr = (uint8_t *)crp->crp_buf;
359 	*allocated = 0;
360 	addr += enccrd->crd_skip;
361 	return (addr);
362 
363 alloc:
364 	addr = malloc(enccrd->crd_len, M_ARMV8_CRYPTO, M_NOWAIT);
365 	if (addr != NULL) {
366 		*allocated = 1;
367 		crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
368 		    enccrd->crd_len, addr);
369 	} else
370 		*allocated = 0;
371 	return (addr);
372 }
373 
374 static int
375 armv8_crypto_cipher_process(struct armv8_crypto_session *ses,
376     struct cryptodesc *enccrd, struct cryptop *crp)
377 {
378 	struct fpu_kern_ctx *ctx;
379 	uint8_t *buf;
380 	uint8_t iv[AES_BLOCK_LEN];
381 	int allocated, i;
382 	int encflag, ivlen;
383 	int kt;
384 
385 	encflag = (enccrd->crd_flags & CRD_F_ENCRYPT) == CRD_F_ENCRYPT;
386 
387 	buf = armv8_crypto_cipher_alloc(enccrd, crp, &allocated);
388 	if (buf == NULL)
389 		return (ENOMEM);
390 
391 	kt = is_fpu_kern_thread(0);
392 	if (!kt) {
393 		AQUIRE_CTX(i, ctx);
394 		fpu_kern_enter(curthread, ctx,
395 		    FPU_KERN_NORMAL | FPU_KERN_KTHR);
396 	}
397 
398 	if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
399 		panic("CRD_F_KEY_EXPLICIT");
400 	}
401 
402 	switch (enccrd->crd_alg) {
403 	case CRYPTO_AES_CBC:
404 		ivlen = AES_BLOCK_LEN;
405 		break;
406 	}
407 
408 	/* Setup iv */
409 	if (encflag) {
410 		if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
411 			bcopy(enccrd->crd_iv, iv, ivlen);
412 		else
413 			arc4rand(iv, ivlen, 0);
414 
415 		if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
416 			crypto_copyback(crp->crp_flags, crp->crp_buf,
417 			    enccrd->crd_inject, ivlen, iv);
418 	} else {
419 		if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
420 			bcopy(enccrd->crd_iv, iv, ivlen);
421 		else
422 			crypto_copydata(crp->crp_flags, crp->crp_buf,
423 			    enccrd->crd_inject, ivlen, iv);
424 	}
425 
426 	/* Do work */
427 	switch (ses->algo) {
428 	case CRYPTO_AES_CBC:
429 		if (encflag)
430 			armv8_aes_encrypt_cbc(ses->rounds, ses->enc_schedule,
431 			    enccrd->crd_len, buf, buf, iv);
432 		else
433 			armv8_aes_decrypt_cbc(ses->rounds, ses->dec_schedule,
434 			    enccrd->crd_len, buf, iv);
435 		break;
436 	}
437 
438 	if (allocated)
439 		crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
440 		    enccrd->crd_len, buf);
441 
442 	if (!kt) {
443 		fpu_kern_leave(curthread, ctx);
444 		RELEASE_CTX(i, ctx);
445 	}
446 	if (allocated) {
447 		bzero(buf, enccrd->crd_len);
448 		free(buf, M_ARMV8_CRYPTO);
449 	}
450 	return (0);
451 }
452 
453 static device_method_t armv8_crypto_methods[] = {
454 	DEVMETHOD(device_identify,	armv8_crypto_identify),
455 	DEVMETHOD(device_probe,		armv8_crypto_probe),
456 	DEVMETHOD(device_attach,	armv8_crypto_attach),
457 	DEVMETHOD(device_detach,	armv8_crypto_detach),
458 
459 	DEVMETHOD(cryptodev_newsession,	armv8_crypto_newsession),
460 	DEVMETHOD(cryptodev_process,	armv8_crypto_process),
461 
462 	DEVMETHOD_END,
463 };
464 
465 static DEFINE_CLASS_0(armv8crypto, armv8_crypto_driver, armv8_crypto_methods,
466     sizeof(struct armv8_crypto_softc));
467 static devclass_t armv8_crypto_devclass;
468 
469 DRIVER_MODULE(armv8crypto, nexus, armv8_crypto_driver, armv8_crypto_devclass,
470     0, 0);
471