aesni.c revision 323871
1106688Simp/*-
2106688Simp * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3106688Simp * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
4106688Simp * All rights reserved.
5106688Simp *
6109348Smtm * Redistribution and use in source and binary forms, with or without
7197139Shrs * modification, are permitted provided that the following conditions
8159126Sthompsa * are met:
9180564Sdougb * 1. Redistributions of source code must retain the above copyright
10106688Simp *    notice, this list of conditions and the following disclaimer.
11106688Simp * 2. Redistributions in binary form must reproduce the above copyright
12106688Simp *    notice, this list of conditions and the following disclaimer in the
13106688Simp *    documentation and/or other materials provided with the distribution.
14230099Sdougb *
15151809Syar * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16106688Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17216744Sdougb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18220962Sdougb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19216744Sdougb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20220962Sdougb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21220962Sdougb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22220962Sdougb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23220962Sdougb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24220962Sdougb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25220962Sdougb * SUCH DAMAGE.
26220962Sdougb */
27220962Sdougb
28220962Sdougb#include <sys/cdefs.h>
29238416Skevlo__FBSDID("$FreeBSD: stable/10/sys/crypto/aesni/aesni.c 323871 2017-09-21 19:30:32Z marius $");
30216744Sdougb
31220962Sdougb#include <sys/param.h>
32220962Sdougb#include <sys/systm.h>
33263963Smjg#include <sys/kernel.h>
34263963Smjg#include <sys/kobj.h>
35216744Sdougb#include <sys/libkern.h>
36263963Smjg#include <sys/lock.h>
37216744Sdougb#include <sys/module.h>
38216744Sdougb#include <sys/malloc.h>
39216744Sdougb#include <sys/rwlock.h>
40106688Simp#include <sys/bus.h>
41106688Simp#include <sys/uio.h>
42#include <sys/smp.h>
43#include <crypto/aesni/aesni.h>
44#include <cryptodev_if.h>
45
46static struct mtx_padalign *ctx_mtx;
47static struct fpu_kern_ctx **ctx_fpu;
48
49struct aesni_softc {
50	int	dieing;
51	int32_t cid;
52	uint32_t sid;
53	TAILQ_HEAD(aesni_sessions_head, aesni_session) sessions;
54	struct rwlock lock;
55};
56
57#define AQUIRE_CTX(i, ctx)					\
58	do {							\
59		(i) = PCPU_GET(cpuid);				\
60		mtx_lock(&ctx_mtx[(i)]);			\
61		(ctx) = ctx_fpu[(i)];				\
62	} while (0)
63#define RELEASE_CTX(i, ctx)					\
64	do {							\
65		mtx_unlock(&ctx_mtx[(i)]);			\
66		(i) = -1;					\
67		(ctx) = NULL;					\
68	} while (0)
69
70static int aesni_newsession(device_t, uint32_t *sidp, struct cryptoini *cri);
71static int aesni_freesession(device_t, uint64_t tid);
72static void aesni_freesession_locked(struct aesni_softc *sc,
73    struct aesni_session *ses);
74static int aesni_cipher_setup(struct aesni_session *ses,
75    struct cryptoini *encini);
76static int aesni_cipher_process(struct aesni_session *ses,
77    struct cryptodesc *enccrd, struct cryptop *crp);
78
79MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
80
81static void
82aesni_identify(driver_t *drv, device_t parent)
83{
84
85	/* NB: order 10 is so we get attached after h/w devices */
86	if (device_find_child(parent, "aesni", -1) == NULL &&
87	    BUS_ADD_CHILD(parent, 10, "aesni", -1) == 0)
88		panic("aesni: could not attach");
89}
90
91static int
92aesni_probe(device_t dev)
93{
94
95	if ((cpu_feature2 & CPUID2_AESNI) == 0) {
96		device_printf(dev, "No AESNI support.\n");
97		return (EINVAL);
98	}
99
100	if ((cpu_feature & CPUID_SSE2) == 0) {
101		device_printf(dev, "No SSE2 support but AESNI!?!\n");
102		return (EINVAL);
103	}
104
105	device_set_desc_copy(dev, "AES-CBC,AES-XTS");
106	return (0);
107}
108
109static void
110aensi_cleanctx(void)
111{
112	int i;
113
114	/* XXX - no way to return driverid */
115	CPU_FOREACH(i) {
116		if (ctx_fpu[i] != NULL) {
117			mtx_destroy(&ctx_mtx[i]);
118			fpu_kern_free_ctx(ctx_fpu[i]);
119		}
120		ctx_fpu[i] = NULL;
121	}
122	free(ctx_mtx, M_AESNI);
123	ctx_mtx = NULL;
124	free(ctx_fpu, M_AESNI);
125	ctx_fpu = NULL;
126}
127
128static int
129aesni_attach(device_t dev)
130{
131	struct aesni_softc *sc;
132	int i;
133
134	sc = device_get_softc(dev);
135	sc->dieing = 0;
136	TAILQ_INIT(&sc->sessions);
137	sc->sid = 1;
138
139	sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE |
140	    CRYPTOCAP_F_SYNC);
141	if (sc->cid < 0) {
142		device_printf(dev, "Could not get crypto driver id.\n");
143		return (ENOMEM);
144	}
145
146	ctx_mtx = malloc(sizeof *ctx_mtx * (mp_maxid + 1), M_AESNI,
147	    M_WAITOK|M_ZERO);
148	ctx_fpu = malloc(sizeof *ctx_fpu * (mp_maxid + 1), M_AESNI,
149	    M_WAITOK|M_ZERO);
150
151	CPU_FOREACH(i) {
152		ctx_fpu[i] = fpu_kern_alloc_ctx(0);
153		mtx_init(&ctx_mtx[i], "anifpumtx", NULL, MTX_DEF|MTX_NEW);
154	}
155
156	rw_init(&sc->lock, "aesni_lock");
157	crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
158	crypto_register(sc->cid, CRYPTO_AES_XTS, 0, 0);
159	return (0);
160}
161
162static int
163aesni_detach(device_t dev)
164{
165	struct aesni_softc *sc;
166	struct aesni_session *ses;
167
168	sc = device_get_softc(dev);
169
170	rw_wlock(&sc->lock);
171	TAILQ_FOREACH(ses, &sc->sessions, next) {
172		if (ses->used) {
173			rw_wunlock(&sc->lock);
174			device_printf(dev,
175			    "Cannot detach, sessions still active.\n");
176			return (EBUSY);
177		}
178	}
179	sc->dieing = 1;
180	while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) {
181		TAILQ_REMOVE(&sc->sessions, ses, next);
182		free(ses, M_AESNI);
183	}
184	rw_wunlock(&sc->lock);
185	crypto_unregister_all(sc->cid);
186
187	rw_destroy(&sc->lock);
188
189	aensi_cleanctx();
190
191	return (0);
192}
193
194static int
195aesni_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
196{
197	struct aesni_softc *sc;
198	struct aesni_session *ses;
199	struct cryptoini *encini;
200	int error;
201
202	if (sidp == NULL || cri == NULL)
203		return (EINVAL);
204
205	sc = device_get_softc(dev);
206	if (sc->dieing)
207		return (EINVAL);
208
209	ses = NULL;
210	encini = NULL;
211	for (; cri != NULL; cri = cri->cri_next) {
212		switch (cri->cri_alg) {
213		case CRYPTO_AES_CBC:
214		case CRYPTO_AES_XTS:
215			if (encini != NULL)
216				return (EINVAL);
217			encini = cri;
218			break;
219		default:
220			return (EINVAL);
221		}
222	}
223	if (encini == NULL)
224		return (EINVAL);
225
226	rw_wlock(&sc->lock);
227	if (sc->dieing) {
228		rw_wunlock(&sc->lock);
229		return (EINVAL);
230	}
231	/*
232	 * Free sessions goes first, so if first session is used, we need to
233	 * allocate one.
234	 */
235	ses = TAILQ_FIRST(&sc->sessions);
236	if (ses == NULL || ses->used) {
237		ses = malloc(sizeof(*ses), M_AESNI, M_NOWAIT | M_ZERO);
238		if (ses == NULL) {
239			rw_wunlock(&sc->lock);
240			return (ENOMEM);
241		}
242		ses->id = sc->sid++;
243	} else {
244		TAILQ_REMOVE(&sc->sessions, ses, next);
245	}
246	ses->used = 1;
247	TAILQ_INSERT_TAIL(&sc->sessions, ses, next);
248	rw_wunlock(&sc->lock);
249	ses->algo = encini->cri_alg;
250
251	error = aesni_cipher_setup(ses, encini);
252	if (error != 0) {
253		rw_wlock(&sc->lock);
254		aesni_freesession_locked(sc, ses);
255		rw_wunlock(&sc->lock);
256		return (error);
257	}
258
259	*sidp = ses->id;
260	return (0);
261}
262
263static void
264aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses)
265{
266	uint32_t sid;
267
268	rw_assert(&sc->lock, RA_WLOCKED);
269
270	sid = ses->id;
271	TAILQ_REMOVE(&sc->sessions, ses, next);
272	bzero(ses, sizeof(*ses));
273	ses->id = sid;
274	TAILQ_INSERT_HEAD(&sc->sessions, ses, next);
275}
276
277static int
278aesni_freesession(device_t dev, uint64_t tid)
279{
280	struct aesni_softc *sc;
281	struct aesni_session *ses;
282	uint32_t sid;
283
284	sc = device_get_softc(dev);
285	sid = ((uint32_t)tid) & 0xffffffff;
286	rw_wlock(&sc->lock);
287	TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
288		if (ses->id == sid)
289			break;
290	}
291	if (ses == NULL) {
292		rw_wunlock(&sc->lock);
293		return (EINVAL);
294	}
295	aesni_freesession_locked(sc, ses);
296	rw_wunlock(&sc->lock);
297	return (0);
298}
299
300static int
301aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
302{
303	struct aesni_softc *sc = device_get_softc(dev);
304	struct aesni_session *ses = NULL;
305	struct cryptodesc *crd, *enccrd;
306	int error;
307
308	error = 0;
309	enccrd = NULL;
310
311	/* Sanity check. */
312	if (crp == NULL)
313		return (EINVAL);
314
315	if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
316		error = EINVAL;
317		goto out;
318	}
319
320	for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
321		switch (crd->crd_alg) {
322		case CRYPTO_AES_CBC:
323		case CRYPTO_AES_XTS:
324			if (enccrd != NULL) {
325				error = EINVAL;
326				goto out;
327			}
328			enccrd = crd;
329			break;
330		default:
331			return (EINVAL);
332		}
333	}
334	if (enccrd == NULL || (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
335		error = EINVAL;
336		goto out;
337	}
338
339	rw_rlock(&sc->lock);
340	TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
341		if (ses->id == (crp->crp_sid & 0xffffffff))
342			break;
343	}
344	rw_runlock(&sc->lock);
345	if (ses == NULL) {
346		error = EINVAL;
347		goto out;
348	}
349
350	error = aesni_cipher_process(ses, enccrd, crp);
351	if (error != 0)
352		goto out;
353
354out:
355	crp->crp_etype = error;
356	crypto_done(crp);
357	return (error);
358}
359
360uint8_t *
361aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
362    int *allocated)
363{
364	struct uio *uio;
365	struct iovec *iov;
366	uint8_t *addr;
367
368	if (crp->crp_flags & CRYPTO_F_IMBUF)
369		goto alloc;
370	else if (crp->crp_flags & CRYPTO_F_IOV) {
371		uio = (struct uio *)crp->crp_buf;
372		if (uio->uio_iovcnt != 1)
373			goto alloc;
374		iov = uio->uio_iov;
375		addr = (u_char *)iov->iov_base + enccrd->crd_skip;
376	} else
377		addr = (u_char *)crp->crp_buf;
378	*allocated = 0;
379	return (addr);
380
381alloc:
382	addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
383	if (addr != NULL) {
384		*allocated = 1;
385		crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
386		    enccrd->crd_len, addr);
387	} else
388		*allocated = 0;
389	return (addr);
390}
391
392static device_method_t aesni_methods[] = {
393	DEVMETHOD(device_identify, aesni_identify),
394	DEVMETHOD(device_probe, aesni_probe),
395	DEVMETHOD(device_attach, aesni_attach),
396	DEVMETHOD(device_detach, aesni_detach),
397
398	DEVMETHOD(cryptodev_newsession, aesni_newsession),
399	DEVMETHOD(cryptodev_freesession, aesni_freesession),
400	DEVMETHOD(cryptodev_process, aesni_process),
401
402	{0, 0},
403};
404
405static driver_t aesni_driver = {
406	"aesni",
407	aesni_methods,
408	sizeof(struct aesni_softc),
409};
410static devclass_t aesni_devclass;
411
412DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
413MODULE_VERSION(aesni, 1);
414MODULE_DEPEND(aesni, crypto, 1, 1, 1);
415
416static int
417aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini)
418{
419	struct fpu_kern_ctx *ctx;
420	int error;
421	int kt, ctxidx;
422
423	kt = is_fpu_kern_thread(0);
424	if (!kt) {
425		AQUIRE_CTX(ctxidx, ctx);
426		error = fpu_kern_enter(curthread, ctx,
427		    FPU_KERN_NORMAL | FPU_KERN_KTHR);
428		if (error != 0)
429			goto out;
430	}
431
432	error = aesni_cipher_setup_common(ses, encini->cri_key,
433	    encini->cri_klen);
434
435	if (!kt) {
436		fpu_kern_leave(curthread, ctx);
437out:
438		RELEASE_CTX(ctxidx, ctx);
439	}
440	return (error);
441}
442
443static int
444aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
445    struct cryptop *crp)
446{
447	struct fpu_kern_ctx *ctx;
448	uint8_t *buf;
449	int error, allocated;
450	int kt, ctxidx;
451
452	buf = aesni_cipher_alloc(enccrd, crp, &allocated);
453	if (buf == NULL)
454		return (ENOMEM);
455
456	error = 0;
457	kt = is_fpu_kern_thread(0);
458	if (!kt) {
459		AQUIRE_CTX(ctxidx, ctx);
460		error = fpu_kern_enter(curthread, ctx,
461		    FPU_KERN_NORMAL|FPU_KERN_KTHR);
462		if (error != 0)
463			goto out2;
464	}
465
466	if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
467		error = aesni_cipher_setup_common(ses, enccrd->crd_key,
468		    enccrd->crd_klen);
469		if (error != 0)
470			goto out;
471	}
472
473	if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0) {
474		if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
475			bcopy(enccrd->crd_iv, ses->iv, AES_BLOCK_LEN);
476		if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
477			crypto_copyback(crp->crp_flags, crp->crp_buf,
478			    enccrd->crd_inject, AES_BLOCK_LEN, ses->iv);
479		if (ses->algo == CRYPTO_AES_CBC) {
480			aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
481			    enccrd->crd_len, buf, buf, ses->iv);
482		} else /* if (ses->algo == CRYPTO_AES_XTS) */ {
483			aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
484			    ses->xts_schedule, enccrd->crd_len, buf, buf,
485			    ses->iv);
486		}
487	} else {
488		if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
489			bcopy(enccrd->crd_iv, ses->iv, AES_BLOCK_LEN);
490		else
491			crypto_copydata(crp->crp_flags, crp->crp_buf,
492			    enccrd->crd_inject, AES_BLOCK_LEN, ses->iv);
493		if (ses->algo == CRYPTO_AES_CBC) {
494			aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
495			    enccrd->crd_len, buf, ses->iv);
496		} else /* if (ses->algo == CRYPTO_AES_XTS) */ {
497			aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
498			    ses->xts_schedule, enccrd->crd_len, buf, buf,
499			    ses->iv);
500		}
501	}
502	if (allocated)
503		crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
504		    enccrd->crd_len, buf);
505	if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0)
506		crypto_copydata(crp->crp_flags, crp->crp_buf,
507		    enccrd->crd_skip + enccrd->crd_len - AES_BLOCK_LEN,
508		    AES_BLOCK_LEN, ses->iv);
509out:
510	if (!kt) {
511		fpu_kern_leave(curthread, ctx);
512out2:
513		RELEASE_CTX(ctxidx, ctx);
514	}
515
516	if (allocated) {
517		bzero(buf, enccrd->crd_len);
518		free(buf, M_AESNI);
519	}
520	return (error);
521}
522