aesni(4): Avoid panicking when kmalloc() returns an unaligned pointer.
[dragonfly.git] / sys / crypto / aesni / aesni.c
1 /*-
2  * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * FreeBSD: src/sys/crypto/aesni/aesni.c,v 1.1 2010/07/23 11:00:46 kib Exp
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/kobj.h>
34 #include <sys/libkern.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
38 #include <sys/spinlock.h>
39 #include <sys/spinlock2.h>
40 #include <sys/bus.h>
41 #include <sys/uio.h>
42 #include <crypto/aesni/aesni.h>
43 #include "cryptodev_if.h"
44
45 struct aesni_softc {
46         int32_t cid;
47         uint32_t sid;
48         TAILQ_HEAD(aesni_sessions_head, aesni_session) sessions;
49         struct spinlock lock;
50 };
51
52 static int aesni_newsession(device_t, uint32_t *sidp, struct cryptoini *cri);
53 static int aesni_freesession(device_t, uint64_t tid);
54 static void aesni_freesession_locked(struct aesni_softc *sc,
55     struct aesni_session *ses);
56
57 MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
58
59 static void
60 aesni_identify(driver_t *drv, device_t parent)
61 {
62
63         /* NB: order 10 is so we get attached after h/w devices */
64         if (device_find_child(parent, "aesni", -1) == NULL &&
65             BUS_ADD_CHILD(parent, parent, 10, "aesni", -1) == 0)
66                 panic("aesni: could not attach");
67 }
68
69 static int
70 aesni_probe(device_t dev)
71 {
72         char capp[32];
73
74         if ((cpu_feature2 & CPUID2_AESNI) == 0) {
75                 device_printf(dev, "No AESNI support.\n");
76                 return (EINVAL);
77         }
78         strlcpy(capp, "AES-CBC", sizeof(capp));
79         device_set_desc_copy(dev, capp);
80         return (0);
81 }
82
83 static int
84 aesni_attach(device_t dev)
85 {
86         struct aesni_softc *sc;
87
88         sc = device_get_softc(dev);
89         TAILQ_INIT(&sc->sessions);
90         sc->sid = 1;
91         sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE);
92         if (sc->cid < 0) {
93                 device_printf(dev, "Could not get crypto driver id.\n");
94                 return (ENOMEM);
95         }
96
97         spin_init(&sc->lock);
98         crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
99         return (0);
100 }
101
102 static int
103 aesni_detach(device_t dev)
104 {
105         struct aesni_softc *sc;
106         struct aesni_session *ses;
107
108         sc = device_get_softc(dev);
109         spin_lock(&sc->lock);
110         TAILQ_FOREACH(ses, &sc->sessions, next) {
111                 if (ses->used) {
112                         spin_unlock(&sc->lock);
113                         device_printf(dev,
114                             "Cannot detach, sessions still active.\n");
115                         return (EBUSY);
116                 }
117         }
118         while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) {
119                 TAILQ_REMOVE(&sc->sessions, ses, next);
120                 kfree(ses->freeaddr, M_AESNI);
121         }
122         spin_unlock(&sc->lock);
123         spin_uninit(&sc->lock);
124         crypto_unregister_all(sc->cid);
125         return (0);
126 }
127
128 static int
129 aesni_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
130 {
131         struct aesni_softc *sc;
132         struct aesni_session *ases, *ses;
133         struct cryptoini *encini;
134         int error;
135
136         if (sidp == NULL || cri == NULL)
137                 return (EINVAL);
138
139         sc = device_get_softc(dev);
140         ses = NULL;
141         encini = NULL;
142         for (; cri != NULL; cri = cri->cri_next) {
143                 switch (cri->cri_alg) {
144                 case CRYPTO_AES_CBC:
145                         if (encini != NULL)
146                                 return (EINVAL);
147                         encini = cri;
148                         break;
149                 default:
150                         return (EINVAL);
151                 }
152         }
153         if (encini == NULL)
154                 return (EINVAL);
155
156         spin_lock(&sc->lock);
157         /*
158          * Free sessions goes first, so if first session is used, we need to
159          * allocate one.
160          */
161         ses = TAILQ_FIRST(&sc->sessions);
162         if (ses == NULL || ses->used) {
163                 ses = kmalloc(sizeof(*ses) + 16, M_AESNI, M_NOWAIT | M_ZERO);
164                 if (ses == NULL) {
165                         spin_unlock(&sc->lock);
166                         return (ENOMEM);
167                 }
168                 /* Check if 'ses' is 16-byte aligned. If not, align it. */
169                 if (((uintptr_t)ses & 0xf) != 0) {
170                         ases = AESNI_ALIGN(ses);
171                         ases->freeaddr = ses;
172                         ses = ases;
173                 } else {
174                         ses->freeaddr = ses;
175                 }
176                 ses->id = sc->sid++;
177         } else {
178                 TAILQ_REMOVE(&sc->sessions, ses, next);
179         }
180         ses->used = 1;
181         TAILQ_INSERT_TAIL(&sc->sessions, ses, next);
182         spin_unlock(&sc->lock);
183
184         error = aesni_cipher_setup(ses, encini);
185         if (error != 0) {
186                 spin_lock(&sc->lock);
187                 aesni_freesession_locked(sc, ses);
188                 spin_unlock(&sc->lock);
189                 return (error);
190         }
191
192         *sidp = ses->id;
193         return (0);
194 }
195
196 static void
197 aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses)
198 {
199         uint32_t sid;
200         void *freeaddr;
201
202         sid = ses->id;
203         TAILQ_REMOVE(&sc->sessions, ses, next);
204         freeaddr = ses->freeaddr;
205         bzero(ses, sizeof(*ses));
206         ses->freeaddr = freeaddr;
207         ses->id = sid;
208         TAILQ_INSERT_HEAD(&sc->sessions, ses, next);
209 }
210
211 static int
212 aesni_freesession(device_t dev, uint64_t tid)
213 {
214         struct aesni_softc *sc;
215         struct aesni_session *ses;
216         uint32_t sid;
217
218         sc = device_get_softc(dev);
219         sid = ((uint32_t)tid) & 0xffffffff;
220         spin_lock(&sc->lock);
221         TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
222                 if (ses->id == sid)
223                         break;
224         }
225         if (ses == NULL) {
226                 spin_unlock(&sc->lock);
227                 return (EINVAL);
228         }
229         aesni_freesession_locked(sc, ses);
230         spin_unlock(&sc->lock);
231         return (0);
232 }
233
234 static int
235 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
236 {
237         struct aesni_softc *sc = device_get_softc(dev);
238         struct aesni_session *ses = NULL;
239         struct cryptodesc *crd, *enccrd;
240         int error;
241
242         error = 0;
243         enccrd = NULL;
244
245         /* Sanity check. */
246         if (crp == NULL)
247                 return (EINVAL);
248
249         if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
250                 error = EINVAL;
251                 goto out;
252         }
253
254         for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
255                 switch (crd->crd_alg) {
256                 case CRYPTO_AES_CBC:
257                         if (enccrd != NULL) {
258                                 error = EINVAL;
259                                 goto out;
260                         }
261                         enccrd = crd;
262                         break;
263                 default:
264                         return (EINVAL);
265                 }
266         }
267         if (enccrd == NULL || (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
268                 error = EINVAL;
269                 goto out;
270         }
271
272         spin_lock(&sc->lock); /* XXX: was rd lock */
273         TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
274                 if (ses->id == (crp->crp_sid & 0xffffffff))
275                         break;
276         }
277         spin_unlock(&sc->lock); /* XXX: was rd lock */
278         if (ses == NULL) {
279                 error = EINVAL;
280                 goto out;
281         }
282
283         error = aesni_cipher_process(ses, enccrd, crp);
284         if (error != 0)
285                 goto out;
286
287 out:
288         crp->crp_etype = error;
289         crypto_done(crp);
290         return (error);
291 }
292
293 uint8_t *
294 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
295     int *allocated)
296 {
297         struct uio *uio;
298         struct iovec *iov;
299         uint8_t *addr;
300
301         if (crp->crp_flags & CRYPTO_F_IMBUF)
302                 goto alloc;
303         else if (crp->crp_flags & CRYPTO_F_IOV) {
304                 uio = (struct uio *)crp->crp_buf;
305                 if (uio->uio_iovcnt != 1)
306                         goto alloc;
307                 iov = uio->uio_iov;
308                 addr = (u_char *)iov->iov_base + enccrd->crd_skip;
309         } else
310                 addr = (u_char *)crp->crp_buf;
311         *allocated = 0;
312         return (addr);
313
314 alloc:
315         addr = kmalloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
316         if (addr != NULL) {
317                 *allocated = 1;
318                 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
319                     enccrd->crd_len, addr);
320         } else
321                 *allocated = 0;
322         return (addr);
323 }
324
325 static device_method_t aesni_methods[] = {
326         DEVMETHOD(device_identify, aesni_identify),
327         DEVMETHOD(device_probe, aesni_probe),
328         DEVMETHOD(device_attach, aesni_attach),
329         DEVMETHOD(device_detach, aesni_detach),
330
331         DEVMETHOD(cryptodev_newsession, aesni_newsession),
332         DEVMETHOD(cryptodev_freesession, aesni_freesession),
333         DEVMETHOD(cryptodev_process, aesni_process),
334
335         {0, 0},
336 };
337
338 static driver_t aesni_driver = {
339         "aesni",
340         aesni_methods,
341         sizeof(struct aesni_softc),
342 };
343 static devclass_t aesni_devclass;
344
345 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
346 MODULE_VERSION(aesni, 1);
347 MODULE_DEPEND(aesni, crypto, 1, 1, 1);