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 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/kobj.h> 35 #include <sys/libkern.h> 36 #include <sys/lock.h> 37 #include <sys/module.h> 38 #include <sys/malloc.h> 39 #include <sys/rwlock.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 rwlock 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, 10, "aesni", -1) == 0) 66 panic("aesni: could not attach"); 67 } 68 69 static int 70 aesni_probe(device_t dev) 71 { 72 73 if ((cpu_feature2 & CPUID2_AESNI) == 0) { 74 device_printf(dev, "No AESNI support.\n"); 75 return (EINVAL); 76 } 77 device_set_desc_copy(dev, "AES-CBC,AES-XTS"); 78 return (0); 79 } 80 81 static int 82 aesni_attach(device_t dev) 83 { 84 struct aesni_softc *sc; 85 86 sc = device_get_softc(dev); 87 TAILQ_INIT(&sc->sessions); 88 sc->sid = 1; 89 sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE); 90 if (sc->cid < 0) { 91 device_printf(dev, "Could not get crypto driver id.\n"); 92 return (ENOMEM); 93 } 94 95 rw_init(&sc->lock, "aesni_lock"); 96 crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0); 97 crypto_register(sc->cid, CRYPTO_AES_XTS, 0, 0); 98 return (0); 99 } 100 101 static int 102 aesni_detach(device_t dev) 103 { 104 struct aesni_softc *sc; 105 struct aesni_session *ses; 106 107 sc = device_get_softc(dev); 108 rw_wlock(&sc->lock); 109 TAILQ_FOREACH(ses, &sc->sessions, next) { 110 if (ses->used) { 111 rw_wunlock(&sc->lock); 112 device_printf(dev, 113 "Cannot detach, sessions still active.\n"); 114 return (EBUSY); 115 } 116 } 117 while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) { 118 TAILQ_REMOVE(&sc->sessions, ses, next); 119 free(ses, M_AESNI); 120 } 121 rw_wunlock(&sc->lock); 122 rw_destroy(&sc->lock); 123 crypto_unregister_all(sc->cid); 124 return (0); 125 } 126 127 static int 128 aesni_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri) 129 { 130 struct aesni_softc *sc; 131 struct aesni_session *ses; 132 struct cryptoini *encini; 133 int error; 134 135 if (sidp == NULL || cri == NULL) 136 return (EINVAL); 137 138 sc = device_get_softc(dev); 139 ses = NULL; 140 encini = NULL; 141 for (; cri != NULL; cri = cri->cri_next) { 142 switch (cri->cri_alg) { 143 case CRYPTO_AES_CBC: 144 case CRYPTO_AES_XTS: 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 rw_wlock(&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 = malloc(sizeof(*ses), M_AESNI, M_NOWAIT | M_ZERO); 164 if (ses == NULL) { 165 rw_wunlock(&sc->lock); 166 return (ENOMEM); 167 } 168 KASSERT(((uintptr_t)ses) % 0x10 == 0, 169 ("malloc returned unaligned pointer")); 170 ses->id = sc->sid++; 171 } else { 172 TAILQ_REMOVE(&sc->sessions, ses, next); 173 } 174 ses->used = 1; 175 TAILQ_INSERT_TAIL(&sc->sessions, ses, next); 176 rw_wunlock(&sc->lock); 177 ses->algo = encini->cri_alg; 178 179 error = aesni_cipher_setup(ses, encini); 180 if (error != 0) { 181 rw_wlock(&sc->lock); 182 aesni_freesession_locked(sc, ses); 183 rw_wunlock(&sc->lock); 184 return (error); 185 } 186 187 *sidp = ses->id; 188 return (0); 189 } 190 191 static void 192 aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses) 193 { 194 uint32_t sid; 195 196 sid = ses->id; 197 TAILQ_REMOVE(&sc->sessions, ses, next); 198 bzero(ses, sizeof(*ses)); 199 ses->id = sid; 200 TAILQ_INSERT_HEAD(&sc->sessions, ses, next); 201 } 202 203 static int 204 aesni_freesession(device_t dev, uint64_t tid) 205 { 206 struct aesni_softc *sc; 207 struct aesni_session *ses; 208 uint32_t sid; 209 210 sc = device_get_softc(dev); 211 sid = ((uint32_t)tid) & 0xffffffff; 212 rw_wlock(&sc->lock); 213 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) { 214 if (ses->id == sid) 215 break; 216 } 217 if (ses == NULL) { 218 rw_wunlock(&sc->lock); 219 return (EINVAL); 220 } 221 aesni_freesession_locked(sc, ses); 222 rw_wunlock(&sc->lock); 223 return (0); 224 } 225 226 static int 227 aesni_process(device_t dev, struct cryptop *crp, int hint __unused) 228 { 229 struct aesni_softc *sc = device_get_softc(dev); 230 struct aesni_session *ses = NULL; 231 struct cryptodesc *crd, *enccrd; 232 int error; 233 234 error = 0; 235 enccrd = NULL; 236 237 /* Sanity check. */ 238 if (crp == NULL) 239 return (EINVAL); 240 241 if (crp->crp_callback == NULL || crp->crp_desc == NULL) { 242 error = EINVAL; 243 goto out; 244 } 245 246 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) { 247 switch (crd->crd_alg) { 248 case CRYPTO_AES_CBC: 249 case CRYPTO_AES_XTS: 250 if (enccrd != NULL) { 251 error = EINVAL; 252 goto out; 253 } 254 enccrd = crd; 255 break; 256 default: 257 return (EINVAL); 258 } 259 } 260 if (enccrd == NULL || (enccrd->crd_len % AES_BLOCK_LEN) != 0) { 261 error = EINVAL; 262 goto out; 263 } 264 265 rw_rlock(&sc->lock); 266 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) { 267 if (ses->id == (crp->crp_sid & 0xffffffff)) 268 break; 269 } 270 rw_runlock(&sc->lock); 271 if (ses == NULL) { 272 error = EINVAL; 273 goto out; 274 } 275 276 error = aesni_cipher_process(ses, enccrd, crp); 277 if (error != 0) 278 goto out; 279 280 out: 281 crp->crp_etype = error; 282 crypto_done(crp); 283 return (error); 284 } 285 286 uint8_t * 287 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp, 288 int *allocated) 289 { 290 struct uio *uio; 291 struct iovec *iov; 292 uint8_t *addr; 293 294 if (crp->crp_flags & CRYPTO_F_IMBUF) 295 goto alloc; 296 else if (crp->crp_flags & CRYPTO_F_IOV) { 297 uio = (struct uio *)crp->crp_buf; 298 if (uio->uio_iovcnt != 1) 299 goto alloc; 300 iov = uio->uio_iov; 301 addr = (u_char *)iov->iov_base + enccrd->crd_skip; 302 } else 303 addr = (u_char *)crp->crp_buf; 304 *allocated = 0; 305 return (addr); 306 307 alloc: 308 addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT); 309 if (addr != NULL) { 310 *allocated = 1; 311 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip, 312 enccrd->crd_len, addr); 313 } else 314 *allocated = 0; 315 return (addr); 316 } 317 318 static device_method_t aesni_methods[] = { 319 DEVMETHOD(device_identify, aesni_identify), 320 DEVMETHOD(device_probe, aesni_probe), 321 DEVMETHOD(device_attach, aesni_attach), 322 DEVMETHOD(device_detach, aesni_detach), 323 324 DEVMETHOD(cryptodev_newsession, aesni_newsession), 325 DEVMETHOD(cryptodev_freesession, aesni_freesession), 326 DEVMETHOD(cryptodev_process, aesni_process), 327 328 {0, 0}, 329 }; 330 331 static driver_t aesni_driver = { 332 "aesni", 333 aesni_methods, 334 sizeof(struct aesni_softc), 335 }; 336 static devclass_t aesni_devclass; 337 338 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0); 339 MODULE_VERSION(aesni, 1); 340 MODULE_DEPEND(aesni, crypto, 1, 1, 1); 341