g_eli_integrity.c revision 160569
1159307Spjd/*- 2159307Spjd * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3159307Spjd * All rights reserved. 4159307Spjd * 5159307Spjd * Redistribution and use in source and binary forms, with or without 6159307Spjd * modification, are permitted provided that the following conditions 7159307Spjd * are met: 8159307Spjd * 1. Redistributions of source code must retain the above copyright 9159307Spjd * notice, this list of conditions and the following disclaimer. 10159307Spjd * 2. Redistributions in binary form must reproduce the above copyright 11159307Spjd * notice, this list of conditions and the following disclaimer in the 12159307Spjd * documentation and/or other materials provided with the distribution. 13159307Spjd * 14159307Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15159307Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16159307Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17159307Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18159307Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19159307Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20159307Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21159307Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22159307Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23159307Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24159307Spjd * SUCH DAMAGE. 25159307Spjd */ 26159307Spjd 27159307Spjd#include <sys/cdefs.h> 28159307Spjd__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_integrity.c 160569 2006-07-22 10:05:55Z pjd $"); 29159307Spjd 30159307Spjd#include <sys/param.h> 31159307Spjd#include <sys/systm.h> 32159307Spjd#include <sys/kernel.h> 33159307Spjd#include <sys/linker.h> 34159307Spjd#include <sys/module.h> 35159307Spjd#include <sys/lock.h> 36159307Spjd#include <sys/mutex.h> 37159307Spjd#include <sys/bio.h> 38159307Spjd#include <sys/sysctl.h> 39159307Spjd#include <sys/malloc.h> 40159307Spjd#include <sys/kthread.h> 41159307Spjd#include <sys/proc.h> 42159307Spjd#include <sys/sched.h> 43159307Spjd#include <sys/smp.h> 44159307Spjd#include <sys/uio.h> 45159307Spjd#include <sys/vnode.h> 46159307Spjd 47159307Spjd#include <vm/uma.h> 48159307Spjd 49159307Spjd#include <geom/geom.h> 50159307Spjd#include <geom/eli/g_eli.h> 51159307Spjd#include <geom/eli/pkcs5v2.h> 52159307Spjd 53159307Spjd/* 54159307Spjd * The data layout description when integrity verification is configured. 55159307Spjd * 56159307Spjd * One of the most important assumption here is that authenticated data and its 57159307Spjd * HMAC has to be stored in the same place (namely in the same sector) to make 58159307Spjd * it work reliable. 59159307Spjd * The problem is that file systems work only with sectors that are multiple of 60159307Spjd * 512 bytes and a power of two number. 61159307Spjd * My idea to implement it is as follows. 62159307Spjd * Let's store HMAC in sector. This is a must. This leaves us 480 bytes for 63159307Spjd * data. We can't use that directly (ie. we can't create provider with 480 bytes 64159307Spjd * sector size). We need another sector from where we take only 32 bytes of data 65159307Spjd * and we store HMAC of this data as well. This takes two sectors from the 66159307Spjd * original provider at the input and leaves us one sector of authenticated data 67159307Spjd * at the output. Not very efficient, but you got the idea. 68159307Spjd * Now, let's assume, we want to create provider with 4096 bytes sector. 69159307Spjd * To output 4096 bytes of authenticated data we need 8x480 plus 1x256, so we 70159307Spjd * need nine 512-bytes sectors at the input to get one 4096-bytes sector at the 71159307Spjd * output. That's better. With 4096 bytes sector we can use 89% of size of the 72159307Spjd * original provider. I find it as an acceptable cost. 73159307Spjd * The reliability comes from the fact, that every HMAC stored inside the sector 74159307Spjd * is calculated only for the data in the same sector, so its impossible to 75159307Spjd * write new data and leave old HMAC or vice versa. 76159307Spjd * 77159307Spjd * And here is the picture: 78159307Spjd * 79159307Spjd * da0: +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+ 80159307Spjd * |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |256b | 81159307Spjd * |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data | 82159307Spjd * +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+ 83159307Spjd * |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |288 bytes | 84159307Spjd * +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ |224 unused| 85159307Spjd * +----------+ 86159307Spjd * da0.eli: +----+----+----+----+----+----+----+----+----+ 87159307Spjd * |480b|480b|480b|480b|480b|480b|480b|480b|256b| 88159307Spjd * +----+----+----+----+----+----+----+----+----+ 89159307Spjd * | 4096 bytes | 90159307Spjd * +--------------------------------------------+ 91159307Spjd * 92159307Spjd * PS. You can use any sector size with geli(8). My example is using 4kB, 93159307Spjd * because it's most efficient. For 8kB sectors you need 2 extra sectors, 94159307Spjd * so the cost is the same as for 4kB sectors. 95159307Spjd */ 96159307Spjd 97159307Spjd/* 98159307Spjd * Code paths: 99159307Spjd * BIO_READ: 100159307Spjd * g_eli_start -> g_eli_auth_read -> g_io_request -> g_eli_read_done -> g_eli_auth_run -> g_eli_auth_read_done -> g_io_deliver 101159307Spjd * BIO_WRITE: 102159307Spjd * g_eli_start -> g_eli_auth_run -> g_eli_auth_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 103159307Spjd */ 104159307Spjd 105159307SpjdMALLOC_DECLARE(M_ELI); 106159307Spjd 107159307Spjd/* 108159307Spjd * Here we generate key for HMAC. Every sector has its own HMAC key, so it is 109159307Spjd * not possible to copy sectors. 110159307Spjd * We cannot depend on fact, that every sector has its own IV, because different 111159307Spjd * IV doesn't change HMAC, when we use encrypt-then-authenticate method. 112159307Spjd */ 113159307Spjdstatic void 114159307Spjdg_eli_auth_keygen(struct g_eli_softc *sc, off_t offset, u_char *key) 115159307Spjd{ 116159307Spjd SHA256_CTX ctx; 117159307Spjd 118159307Spjd /* Copy precalculated SHA256 context. */ 119159307Spjd bcopy(&sc->sc_akeyctx, &ctx, sizeof(ctx)); 120159307Spjd SHA256_Update(&ctx, (uint8_t *)&offset, sizeof(offset)); 121159307Spjd SHA256_Final(key, &ctx); 122159307Spjd} 123159307Spjd 124159307Spjd/* 125159307Spjd * The function is called after we read and decrypt data. 126159307Spjd * 127159307Spjd * g_eli_start -> g_eli_auth_read -> g_io_request -> g_eli_read_done -> g_eli_auth_run -> G_ELI_AUTH_READ_DONE -> g_io_deliver 128159307Spjd */ 129159307Spjdstatic int 130159307Spjdg_eli_auth_read_done(struct cryptop *crp) 131159307Spjd{ 132159307Spjd struct bio *bp; 133159307Spjd 134159307Spjd if (crp->crp_etype == EAGAIN) { 135159307Spjd if (g_eli_crypto_rerun(crp) == 0) 136159307Spjd return (0); 137159307Spjd } 138159307Spjd bp = (struct bio *)crp->crp_opaque; 139159307Spjd bp->bio_inbed++; 140159307Spjd if (crp->crp_etype == 0) { 141159307Spjd bp->bio_completed += crp->crp_olen; 142159307Spjd G_ELI_DEBUG(3, "Crypto READ request done (%d/%d) (add=%jd completed=%jd).", 143159307Spjd bp->bio_inbed, bp->bio_children, (intmax_t)crp->crp_olen, (intmax_t)bp->bio_completed); 144159307Spjd } else { 145159307Spjd G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.", 146159307Spjd bp->bio_inbed, bp->bio_children, crp->crp_etype); 147159307Spjd if (bp->bio_error == 0) 148159307Spjd bp->bio_error = crp->crp_etype; 149159307Spjd } 150159307Spjd /* 151159307Spjd * Do we have all sectors already? 152159307Spjd */ 153159307Spjd if (bp->bio_inbed < bp->bio_children) 154159307Spjd return (0); 155159307Spjd if (bp->bio_error == 0) { 156159307Spjd struct g_eli_softc *sc; 157159307Spjd u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize; 158159307Spjd u_char *srcdata, *dstdata, *auth; 159159307Spjd off_t coroff, corsize; 160159307Spjd 161159307Spjd /* 162159307Spjd * Verify data integrity based on calculated and read HMACs. 163159307Spjd */ 164159307Spjd sc = bp->bio_to->geom->softc; 165159307Spjd /* Sectorsize of decrypted provider eg. 4096. */ 166159307Spjd decr_secsize = bp->bio_to->sectorsize; 167159307Spjd /* The real sectorsize of encrypted provider, eg. 512. */ 168159307Spjd encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize; 169159307Spjd /* Number of data bytes in one encrypted sector, eg. 480. */ 170159307Spjd data_secsize = sc->sc_data_per_sector; 171159307Spjd /* Number of sectors from decrypted provider, eg. 2. */ 172159307Spjd nsec = bp->bio_length / decr_secsize; 173159307Spjd /* Number of sectors from encrypted provider, eg. 18. */ 174159307Spjd nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize; 175159307Spjd /* Last sector number in every big sector, eg. 9. */ 176159307Spjd lsec = sc->sc_bytes_per_sector / encr_secsize; 177159307Spjd 178159307Spjd srcdata = bp->bio_driver2; 179159307Spjd dstdata = bp->bio_data; 180159307Spjd auth = srcdata + encr_secsize * nsec; 181159307Spjd coroff = -1; 182159307Spjd corsize = 0; 183159307Spjd 184159307Spjd for (i = 1; i <= nsec; i++) { 185159307Spjd data_secsize = sc->sc_data_per_sector; 186159307Spjd if ((i % lsec) == 0) 187159307Spjd data_secsize = decr_secsize % data_secsize; 188159307Spjd if (bcmp(srcdata, auth, sc->sc_alen) != 0) { 189159307Spjd /* 190159307Spjd * Curruption detected, remember the offset if 191159307Spjd * this is the first corrupted sector and 192159307Spjd * increase size. 193159307Spjd */ 194159307Spjd if (bp->bio_error == 0) 195159307Spjd bp->bio_error = -1; 196159307Spjd if (coroff == -1) { 197159307Spjd coroff = bp->bio_offset + 198159307Spjd (dstdata - (u_char *)bp->bio_data); 199159307Spjd } 200159307Spjd corsize += data_secsize; 201159307Spjd } else { 202159307Spjd /* 203159307Spjd * No curruption, good. 204159307Spjd * Report previous corruption if there was one. 205159307Spjd */ 206159307Spjd if (coroff != -1) { 207159307Spjd G_ELI_DEBUG(0, "%s: %jd bytes " 208159307Spjd "corrupted at offset %jd.", 209159307Spjd sc->sc_name, (intmax_t)corsize, 210159307Spjd (intmax_t)coroff); 211159307Spjd coroff = -1; 212159307Spjd corsize = 0; 213159307Spjd } 214159307Spjd bcopy(srcdata + sc->sc_alen, dstdata, 215159307Spjd data_secsize); 216159307Spjd } 217159307Spjd srcdata += encr_secsize; 218159307Spjd dstdata += data_secsize; 219159307Spjd auth += sc->sc_alen; 220159307Spjd } 221159307Spjd /* Report previous corruption if there was one. */ 222159307Spjd if (coroff != -1) { 223159307Spjd G_ELI_DEBUG(0, "%s: %jd bytes corrupted at offset %jd.", 224159307Spjd sc->sc_name, (intmax_t)corsize, (intmax_t)coroff); 225159307Spjd } 226159307Spjd } 227159307Spjd free(bp->bio_driver2, M_ELI); 228159307Spjd bp->bio_driver2 = NULL; 229159307Spjd if (bp->bio_error != 0) { 230159307Spjd if (bp->bio_error == -1) 231159307Spjd bp->bio_error = EINVAL; 232159307Spjd else { 233159307Spjd G_ELI_LOGREQ(0, bp, 234159307Spjd "Crypto READ request failed (error=%d).", 235159307Spjd bp->bio_error); 236159307Spjd } 237159307Spjd bp->bio_completed = 0; 238159307Spjd } 239159307Spjd /* 240159307Spjd * Read is finished, send it up. 241159307Spjd */ 242159307Spjd g_io_deliver(bp, bp->bio_error); 243159307Spjd return (0); 244159307Spjd} 245159307Spjd 246159307Spjd/* 247159307Spjd * The function is called after data encryption. 248159307Spjd * 249159307Spjd * g_eli_start -> g_eli_auth_run -> G_ELI_AUTH_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver 250159307Spjd */ 251159307Spjdstatic int 252159307Spjdg_eli_auth_write_done(struct cryptop *crp) 253159307Spjd{ 254159307Spjd struct g_eli_softc *sc; 255159307Spjd struct g_consumer *cp; 256159307Spjd struct bio *bp, *cbp, *cbp2; 257159307Spjd u_int nsec; 258159307Spjd 259159307Spjd if (crp->crp_etype == EAGAIN) { 260159307Spjd if (g_eli_crypto_rerun(crp) == 0) 261159307Spjd return (0); 262159307Spjd } 263159307Spjd bp = (struct bio *)crp->crp_opaque; 264159307Spjd bp->bio_inbed++; 265159307Spjd if (crp->crp_etype == 0) { 266159307Spjd G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).", 267159307Spjd bp->bio_inbed, bp->bio_children); 268159307Spjd } else { 269159307Spjd G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.", 270159307Spjd bp->bio_inbed, bp->bio_children, crp->crp_etype); 271159307Spjd if (bp->bio_error == 0) 272159307Spjd bp->bio_error = crp->crp_etype; 273159307Spjd } 274159307Spjd /* 275159307Spjd * All sectors are already encrypted? 276159307Spjd */ 277159307Spjd if (bp->bio_inbed < bp->bio_children) 278159307Spjd return (0); 279159307Spjd if (bp->bio_error != 0) { 280159307Spjd G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).", 281159307Spjd bp->bio_error); 282159307Spjd free(bp->bio_driver2, M_ELI); 283159307Spjd bp->bio_driver2 = NULL; 284159307Spjd cbp = bp->bio_driver1; 285159307Spjd bp->bio_driver1 = NULL; 286159307Spjd g_destroy_bio(cbp); 287159307Spjd g_io_deliver(bp, bp->bio_error); 288159307Spjd return (0); 289159307Spjd } 290159307Spjd sc = bp->bio_to->geom->softc; 291159307Spjd cp = LIST_FIRST(&sc->sc_geom->consumer); 292159307Spjd cbp = bp->bio_driver1; 293159307Spjd bp->bio_driver1 = NULL; 294159307Spjd cbp->bio_to = cp->provider; 295159307Spjd cbp->bio_done = g_eli_write_done; 296159307Spjd 297159307Spjd /* Number of sectors from decrypted provider, eg. 1. */ 298159307Spjd nsec = bp->bio_length / bp->bio_to->sectorsize; 299159307Spjd /* Number of sectors from encrypted provider, eg. 9. */ 300159307Spjd nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize; 301159307Spjd 302159307Spjd cbp->bio_length = cp->provider->sectorsize * nsec; 303159307Spjd cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector; 304159307Spjd cbp->bio_data = bp->bio_driver2; 305159307Spjd 306159307Spjd /* 307159307Spjd * We write more than what is requested, so we have to be ready to write 308159307Spjd * more than MAXPHYS. 309159307Spjd */ 310159307Spjd cbp2 = NULL; 311159307Spjd if (cbp->bio_length > MAXPHYS) { 312159307Spjd cbp2 = g_duplicate_bio(bp); 313159307Spjd cbp2->bio_length = cbp->bio_length - MAXPHYS; 314159307Spjd cbp2->bio_data = cbp->bio_data + MAXPHYS; 315159307Spjd cbp2->bio_offset = cbp->bio_offset + MAXPHYS; 316159307Spjd cbp2->bio_to = cp->provider; 317159307Spjd cbp2->bio_done = g_eli_write_done; 318159307Spjd cbp->bio_length = MAXPHYS; 319159307Spjd } 320159307Spjd /* 321159307Spjd * Send encrypted data to the provider. 322159307Spjd */ 323159307Spjd G_ELI_LOGREQ(2, cbp, "Sending request."); 324159307Spjd bp->bio_inbed = 0; 325159307Spjd bp->bio_children = (cbp2 != NULL ? 2 : 1); 326159307Spjd g_io_request(cbp, cp); 327159307Spjd if (cbp2 != NULL) { 328159307Spjd G_ELI_LOGREQ(2, cbp2, "Sending request."); 329159307Spjd g_io_request(cbp2, cp); 330159307Spjd } 331159307Spjd return (0); 332159307Spjd} 333159307Spjd 334159307Spjdvoid 335159307Spjdg_eli_auth_read(struct g_eli_softc *sc, struct bio *bp) 336159307Spjd{ 337159307Spjd struct g_consumer *cp; 338159307Spjd struct bio *cbp, *cbp2; 339159307Spjd size_t size; 340159307Spjd off_t nsec; 341159307Spjd 342159307Spjd bp->bio_pflags = 0; 343159307Spjd 344159307Spjd cp = LIST_FIRST(&sc->sc_geom->consumer); 345159307Spjd cbp = bp->bio_driver1; 346159307Spjd bp->bio_driver1 = NULL; 347159307Spjd cbp->bio_to = cp->provider; 348159307Spjd cbp->bio_done = g_eli_read_done; 349159307Spjd 350159307Spjd /* Number of sectors from decrypted provider, eg. 1. */ 351159307Spjd nsec = bp->bio_length / bp->bio_to->sectorsize; 352159307Spjd /* Number of sectors from encrypted provider, eg. 9. */ 353159307Spjd nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize; 354159307Spjd 355159307Spjd cbp->bio_length = cp->provider->sectorsize * nsec; 356159307Spjd size = cbp->bio_length; 357159307Spjd size += sc->sc_alen * nsec; 358159307Spjd size += sizeof(struct cryptop) * nsec; 359159307Spjd size += sizeof(struct cryptodesc) * nsec * 2; 360159307Spjd size += G_ELI_AUTH_SECKEYLEN * nsec; 361159307Spjd size += sizeof(struct uio) * nsec; 362159307Spjd size += sizeof(struct iovec) * nsec; 363159307Spjd cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector; 364159307Spjd bp->bio_driver2 = malloc(size, M_ELI, M_WAITOK); 365159307Spjd cbp->bio_data = bp->bio_driver2; 366159307Spjd 367159307Spjd /* 368159307Spjd * We read more than what is requested, so we have to be ready to read 369159307Spjd * more than MAXPHYS. 370159307Spjd */ 371159307Spjd cbp2 = NULL; 372159307Spjd if (cbp->bio_length > MAXPHYS) { 373159307Spjd cbp2 = g_duplicate_bio(bp); 374159307Spjd cbp2->bio_length = cbp->bio_length - MAXPHYS; 375159307Spjd cbp2->bio_data = cbp->bio_data + MAXPHYS; 376159307Spjd cbp2->bio_offset = cbp->bio_offset + MAXPHYS; 377159307Spjd cbp2->bio_to = cp->provider; 378159307Spjd cbp2->bio_done = g_eli_read_done; 379159307Spjd cbp->bio_length = MAXPHYS; 380159307Spjd } 381159307Spjd /* 382159307Spjd * Read encrypted data from provider. 383159307Spjd */ 384159307Spjd G_ELI_LOGREQ(2, cbp, "Sending request."); 385159307Spjd g_io_request(cbp, cp); 386159307Spjd if (cbp2 != NULL) { 387159307Spjd G_ELI_LOGREQ(2, cbp2, "Sending request."); 388159307Spjd g_io_request(cbp2, cp); 389159307Spjd } 390159307Spjd} 391159307Spjd 392159307Spjd/* 393159307Spjd * This is the main function responsible for cryptography (ie. communication 394159307Spjd * with crypto(9) subsystem). 395159307Spjd */ 396159307Spjdvoid 397159307Spjdg_eli_auth_run(struct g_eli_worker *wr, struct bio *bp) 398159307Spjd{ 399159307Spjd struct g_eli_softc *sc; 400159307Spjd struct cryptop *crp; 401159307Spjd struct cryptodesc *crde, *crda; 402159307Spjd struct uio *uio; 403159307Spjd struct iovec *iov; 404159307Spjd u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize; 405159307Spjd off_t dstoff; 406159307Spjd int err, error; 407159307Spjd u_char *p, *data, *auth, *authkey, *plaindata; 408159307Spjd 409159307Spjd G_ELI_LOGREQ(3, bp, "%s", __func__); 410159307Spjd 411159307Spjd bp->bio_pflags = wr->w_number; 412159307Spjd sc = wr->w_softc; 413159307Spjd /* Sectorsize of decrypted provider eg. 4096. */ 414159307Spjd decr_secsize = bp->bio_to->sectorsize; 415159307Spjd /* The real sectorsize of encrypted provider, eg. 512. */ 416159307Spjd encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize; 417159307Spjd /* Number of data bytes in one encrypted sector, eg. 480. */ 418159307Spjd data_secsize = sc->sc_data_per_sector; 419159307Spjd /* Number of sectors from decrypted provider, eg. 2. */ 420159307Spjd nsec = bp->bio_length / decr_secsize; 421159307Spjd /* Number of sectors from encrypted provider, eg. 18. */ 422159307Spjd nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize; 423159307Spjd /* Last sector number in every big sector, eg. 9. */ 424159307Spjd lsec = sc->sc_bytes_per_sector / encr_secsize; 425159307Spjd /* Destination offset, used for IV generation. */ 426159307Spjd dstoff = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector; 427159307Spjd 428159343Spjd auth = NULL; /* Silence compiler warning. */ 429159307Spjd plaindata = bp->bio_data; 430159307Spjd if (bp->bio_cmd == BIO_READ) { 431159307Spjd data = bp->bio_driver2; 432159307Spjd auth = data + encr_secsize * nsec; 433159307Spjd p = auth + sc->sc_alen * nsec; 434159307Spjd } else { 435159307Spjd size_t size; 436159307Spjd 437159307Spjd size = encr_secsize * nsec; 438159307Spjd size += sizeof(*crp) * nsec; 439159307Spjd size += sizeof(*crde) * nsec; 440159307Spjd size += sizeof(*crda) * nsec; 441159307Spjd size += G_ELI_AUTH_SECKEYLEN * nsec; 442159307Spjd size += sizeof(*uio) * nsec; 443159307Spjd size += sizeof(*iov) * nsec; 444159307Spjd data = malloc(size, M_ELI, M_WAITOK); 445159307Spjd bp->bio_driver2 = data; 446159307Spjd p = data + encr_secsize * nsec; 447159307Spjd } 448159307Spjd bp->bio_inbed = 0; 449159307Spjd bp->bio_children = nsec; 450159307Spjd 451159307Spjd error = 0; 452159307Spjd for (i = 1; i <= nsec; i++, dstoff += encr_secsize) { 453159307Spjd crp = (struct cryptop *)p; p += sizeof(*crp); 454159307Spjd crde = (struct cryptodesc *)p; p += sizeof(*crde); 455159307Spjd crda = (struct cryptodesc *)p; p += sizeof(*crda); 456159307Spjd authkey = (u_char *)p; p += G_ELI_AUTH_SECKEYLEN; 457159307Spjd uio = (struct uio *)p; p += sizeof(*uio); 458159307Spjd iov = (struct iovec *)p; p += sizeof(*iov); 459159307Spjd 460159307Spjd data_secsize = sc->sc_data_per_sector; 461159307Spjd if ((i % lsec) == 0) 462159307Spjd data_secsize = decr_secsize % data_secsize; 463159307Spjd 464159307Spjd if (bp->bio_cmd == BIO_READ) { 465159307Spjd /* Remember read HMAC. */ 466159307Spjd bcopy(data, auth, sc->sc_alen); 467159307Spjd auth += sc->sc_alen; 468159307Spjd /* TODO: bzero(9) can be commented out later. */ 469159307Spjd bzero(data, sc->sc_alen); 470159307Spjd } else { 471159307Spjd bcopy(plaindata, data + sc->sc_alen, data_secsize); 472159307Spjd plaindata += data_secsize; 473159307Spjd } 474159307Spjd 475159307Spjd iov->iov_len = sc->sc_alen + data_secsize; 476159307Spjd iov->iov_base = data; 477159307Spjd data += encr_secsize; 478159307Spjd 479159307Spjd uio->uio_iov = iov; 480159307Spjd uio->uio_iovcnt = 1; 481159307Spjd uio->uio_segflg = UIO_SYSSPACE; 482159307Spjd uio->uio_resid = iov->iov_len; 483159307Spjd 484159307Spjd crp->crp_sid = wr->w_sid; 485159343Spjd crp->crp_ilen = uio->uio_resid; 486160569Spjd crp->crp_olen = data_secsize; 487159307Spjd crp->crp_opaque = (void *)bp; 488159307Spjd crp->crp_buf = (void *)uio; 489159307Spjd crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL; 490159307Spjd if (g_eli_batch) 491159307Spjd crp->crp_flags |= CRYPTO_F_BATCH; 492159307Spjd if (bp->bio_cmd == BIO_WRITE) { 493159307Spjd crp->crp_callback = g_eli_auth_write_done; 494159307Spjd crp->crp_desc = crde; 495159307Spjd crde->crd_next = crda; 496159307Spjd crda->crd_next = NULL; 497159307Spjd } else { 498159307Spjd crp->crp_callback = g_eli_auth_read_done; 499159307Spjd crp->crp_desc = crda; 500159307Spjd crda->crd_next = crde; 501159307Spjd crde->crd_next = NULL; 502159307Spjd } 503159307Spjd 504159307Spjd crde->crd_skip = sc->sc_alen; 505159307Spjd crde->crd_len = data_secsize; 506159307Spjd crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 507159307Spjd if (bp->bio_cmd == BIO_WRITE) 508159307Spjd crde->crd_flags |= CRD_F_ENCRYPT; 509159307Spjd crde->crd_alg = sc->sc_ealgo; 510159307Spjd crde->crd_key = sc->sc_ekey; 511159307Spjd crde->crd_klen = sc->sc_ekeylen; 512159307Spjd g_eli_crypto_ivgen(sc, dstoff, crde->crd_iv, 513159307Spjd sizeof(crde->crd_iv)); 514159307Spjd 515159307Spjd crda->crd_skip = sc->sc_alen; 516159307Spjd crda->crd_len = data_secsize; 517159307Spjd crda->crd_inject = 0; 518159307Spjd crda->crd_flags = CRD_F_KEY_EXPLICIT; 519159307Spjd crda->crd_alg = sc->sc_aalgo; 520159307Spjd g_eli_auth_keygen(sc, dstoff, authkey); 521159307Spjd crda->crd_key = authkey; 522159307Spjd crda->crd_klen = G_ELI_AUTH_SECKEYLEN * 8; 523159307Spjd 524159307Spjd crp->crp_etype = 0; 525159307Spjd err = crypto_dispatch(crp); 526159307Spjd if (err != 0 && error == 0) 527159307Spjd error = err; 528159307Spjd } 529159307Spjd if (bp->bio_error == 0) 530159307Spjd bp->bio_error = error; 531159307Spjd} 532