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 78 if ((cpu_feature & CPUID_SSE2) == 0) { 79 device_printf(dev, "No SSE2 support but AESNI!?!\n"); 80 return (EINVAL); 81 } 82 83 device_set_desc_copy(dev, "AES-CBC,AES-XTS"); 84 return (0); 85 } 86 87 static int 88 aesni_attach(device_t dev) 89 { 90 struct aesni_softc *sc; 91 92 sc = device_get_softc(dev); 93 TAILQ_INIT(&sc->sessions); 94 sc->sid = 1; 95 sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE); 96 if (sc->cid < 0) { 97 device_printf(dev, "Could not get crypto driver id.\n"); 98 return (ENOMEM); 99 } 100 101 rw_init(&sc->lock, "aesni_lock"); 102 crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0); 103 crypto_register(sc->cid, CRYPTO_AES_XTS, 0, 0); 104 return (0); 105 } 106 107 static int 108 aesni_detach(device_t dev) 109 { 110 struct aesni_softc *sc; 111 struct aesni_session *ses; 112 113 sc = device_get_softc(dev); 114 rw_wlock(&sc->lock); 115 TAILQ_FOREACH(ses, &sc->sessions, next) { 116 if (ses->used) { 117 rw_wunlock(&sc->lock); 118 device_printf(dev, 119 "Cannot detach, sessions still active.\n"); 120 return (EBUSY); 121 } 122 } 123 while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) { 124 TAILQ_REMOVE(&sc->sessions, ses, next); 125 fpu_kern_free_ctx(ses->fpu_ctx); 126 free(ses, M_AESNI); 127 } 128 rw_wunlock(&sc->lock); 129 rw_destroy(&sc->lock); 130 crypto_unregister_all(sc->cid); 131 return (0); 132 } 133 134 static int 135 aesni_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri) 136 { 137 struct aesni_softc *sc; 138 struct aesni_session *ses; 139 struct cryptoini *encini; 140 int error; 141 142 if (sidp == NULL || cri == NULL) 143 return (EINVAL); 144 145 sc = device_get_softc(dev); 146 ses = NULL; 147 encini = NULL; 148 for (; cri != NULL; cri = cri->cri_next) { 149 switch (cri->cri_alg) { 150 case CRYPTO_AES_CBC: 151 case CRYPTO_AES_XTS: 152 if (encini != NULL) 153 return (EINVAL); 154 encini = cri; 155 break; 156 default: 157 return (EINVAL); 158 } 159 } 160 if (encini == NULL) 161 return (EINVAL); 162 163 rw_wlock(&sc->lock); 164 /* 165 * Free sessions goes first, so if first session is used, we need to 166 * allocate one. 167 */ 168 ses = TAILQ_FIRST(&sc->sessions); 169 if (ses == NULL || ses->used) { 170 ses = malloc(sizeof(*ses), M_AESNI, M_NOWAIT | M_ZERO); 171 if (ses == NULL) { 172 rw_wunlock(&sc->lock); 173 return (ENOMEM); 174 } 175 ses->fpu_ctx = fpu_kern_alloc_ctx(FPU_KERN_NORMAL | 176 FPU_KERN_NOWAIT); 177 if (ses->fpu_ctx == NULL) { 178 free(ses, M_AESNI); 179 rw_wunlock(&sc->lock); 180 return (ENOMEM); 181 } 182 ses->id = sc->sid++; 183 } else { 184 TAILQ_REMOVE(&sc->sessions, ses, next); 185 } 186 ses->used = 1; 187 TAILQ_INSERT_TAIL(&sc->sessions, ses, next); 188 rw_wunlock(&sc->lock); 189 ses->algo = encini->cri_alg; 190 191 error = aesni_cipher_setup(ses, encini); 192 if (error != 0) { 193 rw_wlock(&sc->lock); 194 aesni_freesession_locked(sc, ses); 195 rw_wunlock(&sc->lock); 196 return (error); 197 } 198 199 *sidp = ses->id; 200 return (0); 201 } 202 203 static void 204 aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses) 205 { 206 struct fpu_kern_ctx *ctx; 207 uint32_t sid; 208 209 sid = ses->id; 210 TAILQ_REMOVE(&sc->sessions, ses, next); 211 ctx = ses->fpu_ctx; 212 bzero(ses, sizeof(*ses)); 213 ses->id = sid; 214 ses->fpu_ctx = ctx; 215 TAILQ_INSERT_HEAD(&sc->sessions, ses, next); 216 } 217 218 static int 219 aesni_freesession(device_t dev, uint64_t tid) 220 { 221 struct aesni_softc *sc; 222 struct aesni_session *ses; 223 uint32_t sid; 224 225 sc = device_get_softc(dev); 226 sid = ((uint32_t)tid) & 0xffffffff; 227 rw_wlock(&sc->lock); 228 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) { 229 if (ses->id == sid) 230 break; 231 } 232 if (ses == NULL) { 233 rw_wunlock(&sc->lock); 234 return (EINVAL); 235 } 236 aesni_freesession_locked(sc, ses); 237 rw_wunlock(&sc->lock); 238 return (0); 239 } 240 241 static int 242 aesni_process(device_t dev, struct cryptop *crp, int hint __unused) 243 { 244 struct aesni_softc *sc = device_get_softc(dev); 245 struct aesni_session *ses = NULL; 246 struct cryptodesc *crd, *enccrd; 247 int error; 248 249 error = 0; 250 enccrd = NULL; 251 252 /* Sanity check. */ 253 if (crp == NULL) 254 return (EINVAL); 255 256 if (crp->crp_callback == NULL || crp->crp_desc == NULL) { 257 error = EINVAL; 258 goto out; 259 } 260 261 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) { 262 switch (crd->crd_alg) { 263 case CRYPTO_AES_CBC: 264 case CRYPTO_AES_XTS: 265 if (enccrd != NULL) { 266 error = EINVAL; 267 goto out; 268 } 269 enccrd = crd; 270 break; 271 default: 272 return (EINVAL); 273 } 274 } 275 if (enccrd == NULL || (enccrd->crd_len % AES_BLOCK_LEN) != 0) { 276 error = EINVAL; 277 goto out; 278 } 279 280 rw_rlock(&sc->lock); 281 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) { 282 if (ses->id == (crp->crp_sid & 0xffffffff)) 283 break; 284 } 285 rw_runlock(&sc->lock); 286 if (ses == NULL) { 287 error = EINVAL; 288 goto out; 289 } 290 291 error = aesni_cipher_process(ses, enccrd, crp); 292 if (error != 0) 293 goto out; 294 295 out: 296 crp->crp_etype = error; 297 crypto_done(crp); 298 return (error); 299 } 300 301 uint8_t * 302 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp, 303 int *allocated) 304 { 305 struct uio *uio; 306 struct iovec *iov; 307 uint8_t *addr; 308 309 if (crp->crp_flags & CRYPTO_F_IMBUF) 310 goto alloc; 311 else if (crp->crp_flags & CRYPTO_F_IOV) { 312 uio = (struct uio *)crp->crp_buf; 313 if (uio->uio_iovcnt != 1) 314 goto alloc; 315 iov = uio->uio_iov; 316 addr = (u_char *)iov->iov_base + enccrd->crd_skip; 317 } else 318 addr = (u_char *)crp->crp_buf; 319 *allocated = 0; 320 return (addr); 321 322 alloc: 323 addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT); 324 if (addr != NULL) { 325 *allocated = 1; 326 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip, 327 enccrd->crd_len, addr); 328 } else 329 *allocated = 0; 330 return (addr); 331 } 332 333 static device_method_t aesni_methods[] = { 334 DEVMETHOD(device_identify, aesni_identify), 335 DEVMETHOD(device_probe, aesni_probe), 336 DEVMETHOD(device_attach, aesni_attach), 337 DEVMETHOD(device_detach, aesni_detach), 338 339 DEVMETHOD(cryptodev_newsession, aesni_newsession), 340 DEVMETHOD(cryptodev_freesession, aesni_freesession), 341 DEVMETHOD(cryptodev_process, aesni_process), 342 343 {0, 0}, 344 }; 345 346 static driver_t aesni_driver = { 347 "aesni", 348 aesni_methods, 349 sizeof(struct aesni_softc), 350 }; 351 static devclass_t aesni_devclass; 352 353 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0); 354 MODULE_VERSION(aesni, 1); 355 MODULE_DEPEND(aesni, crypto, 1, 1, 1); 356