1eaa3b919SPawel Jakub Dawidek /*- 29839c97bSPawel Jakub Dawidek * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3eaa3b919SPawel Jakub Dawidek * All rights reserved. 4eaa3b919SPawel Jakub Dawidek * 5eaa3b919SPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without 6eaa3b919SPawel Jakub Dawidek * modification, are permitted provided that the following conditions 7eaa3b919SPawel Jakub Dawidek * are met: 8eaa3b919SPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright 9eaa3b919SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer. 10eaa3b919SPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright 11eaa3b919SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the 12eaa3b919SPawel Jakub Dawidek * documentation and/or other materials provided with the distribution. 13eaa3b919SPawel Jakub Dawidek * 14eaa3b919SPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15eaa3b919SPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16eaa3b919SPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17eaa3b919SPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18eaa3b919SPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19eaa3b919SPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20eaa3b919SPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21eaa3b919SPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22eaa3b919SPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23eaa3b919SPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24eaa3b919SPawel Jakub Dawidek * SUCH DAMAGE. 25eaa3b919SPawel Jakub Dawidek */ 26eaa3b919SPawel Jakub Dawidek 27eaa3b919SPawel Jakub Dawidek #include <sys/cdefs.h> 28eaa3b919SPawel Jakub Dawidek __FBSDID("$FreeBSD$"); 29eaa3b919SPawel Jakub Dawidek 30eaa3b919SPawel Jakub Dawidek #include <sys/param.h> 31eaa3b919SPawel Jakub Dawidek #include <sys/systm.h> 32eaa3b919SPawel Jakub Dawidek #include <sys/kernel.h> 33eaa3b919SPawel Jakub Dawidek #include <sys/linker.h> 34eaa3b919SPawel Jakub Dawidek #include <sys/module.h> 35eaa3b919SPawel Jakub Dawidek #include <sys/lock.h> 36eaa3b919SPawel Jakub Dawidek #include <sys/mutex.h> 37eaa3b919SPawel Jakub Dawidek #include <sys/bio.h> 38eaa3b919SPawel Jakub Dawidek #include <sys/sysctl.h> 39eaa3b919SPawel Jakub Dawidek #include <sys/malloc.h> 40eaa3b919SPawel Jakub Dawidek #include <sys/kthread.h> 41eaa3b919SPawel Jakub Dawidek #include <sys/proc.h> 42eaa3b919SPawel Jakub Dawidek #include <sys/sched.h> 43eaa3b919SPawel Jakub Dawidek #include <sys/smp.h> 44eaa3b919SPawel Jakub Dawidek #include <sys/uio.h> 45eaa3b919SPawel Jakub Dawidek #include <sys/vnode.h> 46eaa3b919SPawel Jakub Dawidek 47eaa3b919SPawel Jakub Dawidek #include <vm/uma.h> 48eaa3b919SPawel Jakub Dawidek 49eaa3b919SPawel Jakub Dawidek #include <geom/geom.h> 50eaa3b919SPawel Jakub Dawidek #include <geom/eli/g_eli.h> 51eaa3b919SPawel Jakub Dawidek #include <geom/eli/pkcs5v2.h> 52eaa3b919SPawel Jakub Dawidek 53eaa3b919SPawel Jakub Dawidek /* 54eaa3b919SPawel Jakub Dawidek * The data layout description when integrity verification is configured. 55eaa3b919SPawel Jakub Dawidek * 56eaa3b919SPawel Jakub Dawidek * One of the most important assumption here is that authenticated data and its 57eaa3b919SPawel Jakub Dawidek * HMAC has to be stored in the same place (namely in the same sector) to make 58eaa3b919SPawel Jakub Dawidek * it work reliable. 59eaa3b919SPawel Jakub Dawidek * The problem is that file systems work only with sectors that are multiple of 60eaa3b919SPawel Jakub Dawidek * 512 bytes and a power of two number. 61eaa3b919SPawel Jakub Dawidek * My idea to implement it is as follows. 62eaa3b919SPawel Jakub Dawidek * Let's store HMAC in sector. This is a must. This leaves us 480 bytes for 63eaa3b919SPawel Jakub Dawidek * data. We can't use that directly (ie. we can't create provider with 480 bytes 64eaa3b919SPawel Jakub Dawidek * sector size). We need another sector from where we take only 32 bytes of data 65eaa3b919SPawel Jakub Dawidek * and we store HMAC of this data as well. This takes two sectors from the 66eaa3b919SPawel Jakub Dawidek * original provider at the input and leaves us one sector of authenticated data 67eaa3b919SPawel Jakub Dawidek * at the output. Not very efficient, but you got the idea. 68eaa3b919SPawel Jakub Dawidek * Now, let's assume, we want to create provider with 4096 bytes sector. 69eaa3b919SPawel Jakub Dawidek * To output 4096 bytes of authenticated data we need 8x480 plus 1x256, so we 70eaa3b919SPawel Jakub Dawidek * need nine 512-bytes sectors at the input to get one 4096-bytes sector at the 71eaa3b919SPawel Jakub Dawidek * output. That's better. With 4096 bytes sector we can use 89% of size of the 72eaa3b919SPawel Jakub Dawidek * original provider. I find it as an acceptable cost. 73eaa3b919SPawel Jakub Dawidek * The reliability comes from the fact, that every HMAC stored inside the sector 74eaa3b919SPawel Jakub Dawidek * is calculated only for the data in the same sector, so its impossible to 75eaa3b919SPawel Jakub Dawidek * write new data and leave old HMAC or vice versa. 76eaa3b919SPawel Jakub Dawidek * 77eaa3b919SPawel Jakub Dawidek * And here is the picture: 78eaa3b919SPawel Jakub Dawidek * 79eaa3b919SPawel Jakub Dawidek * da0: +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+ 80eaa3b919SPawel Jakub Dawidek * |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |480b| |32b |256b | 81eaa3b919SPawel Jakub Dawidek * |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data| |HMAC|Data | 82eaa3b919SPawel Jakub Dawidek * +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+----+ +----+-----+ 83eaa3b919SPawel Jakub Dawidek * |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |512 bytes| |288 bytes | 84eaa3b919SPawel Jakub Dawidek * +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ +---------+ |224 unused| 85eaa3b919SPawel Jakub Dawidek * +----------+ 86eaa3b919SPawel Jakub Dawidek * da0.eli: +----+----+----+----+----+----+----+----+----+ 87eaa3b919SPawel Jakub Dawidek * |480b|480b|480b|480b|480b|480b|480b|480b|256b| 88eaa3b919SPawel Jakub Dawidek * +----+----+----+----+----+----+----+----+----+ 89eaa3b919SPawel Jakub Dawidek * | 4096 bytes | 90eaa3b919SPawel Jakub Dawidek * +--------------------------------------------+ 91eaa3b919SPawel Jakub Dawidek * 92eaa3b919SPawel Jakub Dawidek * PS. You can use any sector size with geli(8). My example is using 4kB, 93eaa3b919SPawel Jakub Dawidek * because it's most efficient. For 8kB sectors you need 2 extra sectors, 94eaa3b919SPawel Jakub Dawidek * so the cost is the same as for 4kB sectors. 95eaa3b919SPawel Jakub Dawidek */ 96eaa3b919SPawel Jakub Dawidek 97eaa3b919SPawel Jakub Dawidek /* 98eaa3b919SPawel Jakub Dawidek * Code paths: 99eaa3b919SPawel Jakub Dawidek * BIO_READ: 100eaa3b919SPawel Jakub Dawidek * 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 101eaa3b919SPawel Jakub Dawidek * BIO_WRITE: 102eaa3b919SPawel Jakub Dawidek * g_eli_start -> g_eli_auth_run -> g_eli_auth_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 103eaa3b919SPawel Jakub Dawidek */ 104eaa3b919SPawel Jakub Dawidek 105eaa3b919SPawel Jakub Dawidek MALLOC_DECLARE(M_ELI); 106eaa3b919SPawel Jakub Dawidek 107eaa3b919SPawel Jakub Dawidek /* 108eaa3b919SPawel Jakub Dawidek * Here we generate key for HMAC. Every sector has its own HMAC key, so it is 109eaa3b919SPawel Jakub Dawidek * not possible to copy sectors. 110eaa3b919SPawel Jakub Dawidek * We cannot depend on fact, that every sector has its own IV, because different 111eaa3b919SPawel Jakub Dawidek * IV doesn't change HMAC, when we use encrypt-then-authenticate method. 112eaa3b919SPawel Jakub Dawidek */ 113eaa3b919SPawel Jakub Dawidek static void 114eaa3b919SPawel Jakub Dawidek g_eli_auth_keygen(struct g_eli_softc *sc, off_t offset, u_char *key) 115eaa3b919SPawel Jakub Dawidek { 116eaa3b919SPawel Jakub Dawidek SHA256_CTX ctx; 117eaa3b919SPawel Jakub Dawidek 118eaa3b919SPawel Jakub Dawidek /* Copy precalculated SHA256 context. */ 119eaa3b919SPawel Jakub Dawidek bcopy(&sc->sc_akeyctx, &ctx, sizeof(ctx)); 120eaa3b919SPawel Jakub Dawidek SHA256_Update(&ctx, (uint8_t *)&offset, sizeof(offset)); 121eaa3b919SPawel Jakub Dawidek SHA256_Final(key, &ctx); 122eaa3b919SPawel Jakub Dawidek } 123eaa3b919SPawel Jakub Dawidek 124eaa3b919SPawel Jakub Dawidek /* 125eaa3b919SPawel Jakub Dawidek * The function is called after we read and decrypt data. 126eaa3b919SPawel Jakub Dawidek * 127eaa3b919SPawel Jakub Dawidek * 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 128eaa3b919SPawel Jakub Dawidek */ 129eaa3b919SPawel Jakub Dawidek static int 130eaa3b919SPawel Jakub Dawidek g_eli_auth_read_done(struct cryptop *crp) 131eaa3b919SPawel Jakub Dawidek { 132*5ad4a7c7SPawel Jakub Dawidek struct g_eli_softc *sc; 133eaa3b919SPawel Jakub Dawidek struct bio *bp; 134eaa3b919SPawel Jakub Dawidek 135eaa3b919SPawel Jakub Dawidek if (crp->crp_etype == EAGAIN) { 136eaa3b919SPawel Jakub Dawidek if (g_eli_crypto_rerun(crp) == 0) 137eaa3b919SPawel Jakub Dawidek return (0); 138eaa3b919SPawel Jakub Dawidek } 139eaa3b919SPawel Jakub Dawidek bp = (struct bio *)crp->crp_opaque; 140eaa3b919SPawel Jakub Dawidek bp->bio_inbed++; 141eaa3b919SPawel Jakub Dawidek if (crp->crp_etype == 0) { 142eaa3b919SPawel Jakub Dawidek bp->bio_completed += crp->crp_olen; 143eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(3, "Crypto READ request done (%d/%d) (add=%jd completed=%jd).", 144eaa3b919SPawel Jakub Dawidek bp->bio_inbed, bp->bio_children, (intmax_t)crp->crp_olen, (intmax_t)bp->bio_completed); 145eaa3b919SPawel Jakub Dawidek } else { 146eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.", 147eaa3b919SPawel Jakub Dawidek bp->bio_inbed, bp->bio_children, crp->crp_etype); 148eaa3b919SPawel Jakub Dawidek if (bp->bio_error == 0) 149eaa3b919SPawel Jakub Dawidek bp->bio_error = crp->crp_etype; 150eaa3b919SPawel Jakub Dawidek } 151eaa3b919SPawel Jakub Dawidek /* 152eaa3b919SPawel Jakub Dawidek * Do we have all sectors already? 153eaa3b919SPawel Jakub Dawidek */ 154eaa3b919SPawel Jakub Dawidek if (bp->bio_inbed < bp->bio_children) 155eaa3b919SPawel Jakub Dawidek return (0); 156*5ad4a7c7SPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 157eaa3b919SPawel Jakub Dawidek if (bp->bio_error == 0) { 158eaa3b919SPawel Jakub Dawidek u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize; 159eaa3b919SPawel Jakub Dawidek u_char *srcdata, *dstdata, *auth; 160eaa3b919SPawel Jakub Dawidek off_t coroff, corsize; 161eaa3b919SPawel Jakub Dawidek 162eaa3b919SPawel Jakub Dawidek /* 163eaa3b919SPawel Jakub Dawidek * Verify data integrity based on calculated and read HMACs. 164eaa3b919SPawel Jakub Dawidek */ 165eaa3b919SPawel Jakub Dawidek /* Sectorsize of decrypted provider eg. 4096. */ 166eaa3b919SPawel Jakub Dawidek decr_secsize = bp->bio_to->sectorsize; 167eaa3b919SPawel Jakub Dawidek /* The real sectorsize of encrypted provider, eg. 512. */ 168eaa3b919SPawel Jakub Dawidek encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize; 169eaa3b919SPawel Jakub Dawidek /* Number of data bytes in one encrypted sector, eg. 480. */ 170eaa3b919SPawel Jakub Dawidek data_secsize = sc->sc_data_per_sector; 171eaa3b919SPawel Jakub Dawidek /* Number of sectors from decrypted provider, eg. 2. */ 172eaa3b919SPawel Jakub Dawidek nsec = bp->bio_length / decr_secsize; 173eaa3b919SPawel Jakub Dawidek /* Number of sectors from encrypted provider, eg. 18. */ 174eaa3b919SPawel Jakub Dawidek nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize; 175eaa3b919SPawel Jakub Dawidek /* Last sector number in every big sector, eg. 9. */ 176eaa3b919SPawel Jakub Dawidek lsec = sc->sc_bytes_per_sector / encr_secsize; 177eaa3b919SPawel Jakub Dawidek 178eaa3b919SPawel Jakub Dawidek srcdata = bp->bio_driver2; 179eaa3b919SPawel Jakub Dawidek dstdata = bp->bio_data; 180eaa3b919SPawel Jakub Dawidek auth = srcdata + encr_secsize * nsec; 181eaa3b919SPawel Jakub Dawidek coroff = -1; 182eaa3b919SPawel Jakub Dawidek corsize = 0; 183eaa3b919SPawel Jakub Dawidek 184eaa3b919SPawel Jakub Dawidek for (i = 1; i <= nsec; i++) { 185eaa3b919SPawel Jakub Dawidek data_secsize = sc->sc_data_per_sector; 186eaa3b919SPawel Jakub Dawidek if ((i % lsec) == 0) 187eaa3b919SPawel Jakub Dawidek data_secsize = decr_secsize % data_secsize; 188eaa3b919SPawel Jakub Dawidek if (bcmp(srcdata, auth, sc->sc_alen) != 0) { 189eaa3b919SPawel Jakub Dawidek /* 190eaa3b919SPawel Jakub Dawidek * Curruption detected, remember the offset if 191eaa3b919SPawel Jakub Dawidek * this is the first corrupted sector and 192eaa3b919SPawel Jakub Dawidek * increase size. 193eaa3b919SPawel Jakub Dawidek */ 194eaa3b919SPawel Jakub Dawidek if (bp->bio_error == 0) 195eaa3b919SPawel Jakub Dawidek bp->bio_error = -1; 196eaa3b919SPawel Jakub Dawidek if (coroff == -1) { 197eaa3b919SPawel Jakub Dawidek coroff = bp->bio_offset + 198eaa3b919SPawel Jakub Dawidek (dstdata - (u_char *)bp->bio_data); 199eaa3b919SPawel Jakub Dawidek } 200eaa3b919SPawel Jakub Dawidek corsize += data_secsize; 201eaa3b919SPawel Jakub Dawidek } else { 202eaa3b919SPawel Jakub Dawidek /* 203eaa3b919SPawel Jakub Dawidek * No curruption, good. 204eaa3b919SPawel Jakub Dawidek * Report previous corruption if there was one. 205eaa3b919SPawel Jakub Dawidek */ 206eaa3b919SPawel Jakub Dawidek if (coroff != -1) { 207eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, "%s: %jd bytes " 208eaa3b919SPawel Jakub Dawidek "corrupted at offset %jd.", 209eaa3b919SPawel Jakub Dawidek sc->sc_name, (intmax_t)corsize, 210eaa3b919SPawel Jakub Dawidek (intmax_t)coroff); 211eaa3b919SPawel Jakub Dawidek coroff = -1; 212eaa3b919SPawel Jakub Dawidek corsize = 0; 213eaa3b919SPawel Jakub Dawidek } 214eaa3b919SPawel Jakub Dawidek bcopy(srcdata + sc->sc_alen, dstdata, 215eaa3b919SPawel Jakub Dawidek data_secsize); 216eaa3b919SPawel Jakub Dawidek } 217eaa3b919SPawel Jakub Dawidek srcdata += encr_secsize; 218eaa3b919SPawel Jakub Dawidek dstdata += data_secsize; 219eaa3b919SPawel Jakub Dawidek auth += sc->sc_alen; 220eaa3b919SPawel Jakub Dawidek } 221eaa3b919SPawel Jakub Dawidek /* Report previous corruption if there was one. */ 222eaa3b919SPawel Jakub Dawidek if (coroff != -1) { 223eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(0, "%s: %jd bytes corrupted at offset %jd.", 224eaa3b919SPawel Jakub Dawidek sc->sc_name, (intmax_t)corsize, (intmax_t)coroff); 225eaa3b919SPawel Jakub Dawidek } 226eaa3b919SPawel Jakub Dawidek } 227eaa3b919SPawel Jakub Dawidek free(bp->bio_driver2, M_ELI); 228eaa3b919SPawel Jakub Dawidek bp->bio_driver2 = NULL; 229eaa3b919SPawel Jakub Dawidek if (bp->bio_error != 0) { 230eaa3b919SPawel Jakub Dawidek if (bp->bio_error == -1) 231eaa3b919SPawel Jakub Dawidek bp->bio_error = EINVAL; 232eaa3b919SPawel Jakub Dawidek else { 233eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(0, bp, 234eaa3b919SPawel Jakub Dawidek "Crypto READ request failed (error=%d).", 235eaa3b919SPawel Jakub Dawidek bp->bio_error); 236eaa3b919SPawel Jakub Dawidek } 237eaa3b919SPawel Jakub Dawidek bp->bio_completed = 0; 238eaa3b919SPawel Jakub Dawidek } 239eaa3b919SPawel Jakub Dawidek /* 240eaa3b919SPawel Jakub Dawidek * Read is finished, send it up. 241eaa3b919SPawel Jakub Dawidek */ 242eaa3b919SPawel Jakub Dawidek g_io_deliver(bp, bp->bio_error); 243*5ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 244eaa3b919SPawel Jakub Dawidek return (0); 245eaa3b919SPawel Jakub Dawidek } 246eaa3b919SPawel Jakub Dawidek 247eaa3b919SPawel Jakub Dawidek /* 248eaa3b919SPawel Jakub Dawidek * The function is called after data encryption. 249eaa3b919SPawel Jakub Dawidek * 250eaa3b919SPawel Jakub Dawidek * g_eli_start -> g_eli_auth_run -> G_ELI_AUTH_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver 251eaa3b919SPawel Jakub Dawidek */ 252eaa3b919SPawel Jakub Dawidek static int 253eaa3b919SPawel Jakub Dawidek g_eli_auth_write_done(struct cryptop *crp) 254eaa3b919SPawel Jakub Dawidek { 255eaa3b919SPawel Jakub Dawidek struct g_eli_softc *sc; 256eaa3b919SPawel Jakub Dawidek struct g_consumer *cp; 257eaa3b919SPawel Jakub Dawidek struct bio *bp, *cbp, *cbp2; 258eaa3b919SPawel Jakub Dawidek u_int nsec; 259eaa3b919SPawel Jakub Dawidek 260eaa3b919SPawel Jakub Dawidek if (crp->crp_etype == EAGAIN) { 261eaa3b919SPawel Jakub Dawidek if (g_eli_crypto_rerun(crp) == 0) 262eaa3b919SPawel Jakub Dawidek return (0); 263eaa3b919SPawel Jakub Dawidek } 264eaa3b919SPawel Jakub Dawidek bp = (struct bio *)crp->crp_opaque; 265eaa3b919SPawel Jakub Dawidek bp->bio_inbed++; 266eaa3b919SPawel Jakub Dawidek if (crp->crp_etype == 0) { 267eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).", 268eaa3b919SPawel Jakub Dawidek bp->bio_inbed, bp->bio_children); 269eaa3b919SPawel Jakub Dawidek } else { 270eaa3b919SPawel Jakub Dawidek G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.", 271eaa3b919SPawel Jakub Dawidek bp->bio_inbed, bp->bio_children, crp->crp_etype); 272eaa3b919SPawel Jakub Dawidek if (bp->bio_error == 0) 273eaa3b919SPawel Jakub Dawidek bp->bio_error = crp->crp_etype; 274eaa3b919SPawel Jakub Dawidek } 275eaa3b919SPawel Jakub Dawidek /* 276eaa3b919SPawel Jakub Dawidek * All sectors are already encrypted? 277eaa3b919SPawel Jakub Dawidek */ 278eaa3b919SPawel Jakub Dawidek if (bp->bio_inbed < bp->bio_children) 279eaa3b919SPawel Jakub Dawidek return (0); 280*5ad4a7c7SPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 281eaa3b919SPawel Jakub Dawidek if (bp->bio_error != 0) { 282eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).", 283eaa3b919SPawel Jakub Dawidek bp->bio_error); 284eaa3b919SPawel Jakub Dawidek free(bp->bio_driver2, M_ELI); 285eaa3b919SPawel Jakub Dawidek bp->bio_driver2 = NULL; 286eaa3b919SPawel Jakub Dawidek cbp = bp->bio_driver1; 287eaa3b919SPawel Jakub Dawidek bp->bio_driver1 = NULL; 288eaa3b919SPawel Jakub Dawidek g_destroy_bio(cbp); 289eaa3b919SPawel Jakub Dawidek g_io_deliver(bp, bp->bio_error); 290*5ad4a7c7SPawel Jakub Dawidek atomic_subtract_int(&sc->sc_inflight, 1); 291eaa3b919SPawel Jakub Dawidek return (0); 292eaa3b919SPawel Jakub Dawidek } 293eaa3b919SPawel Jakub Dawidek cp = LIST_FIRST(&sc->sc_geom->consumer); 294eaa3b919SPawel Jakub Dawidek cbp = bp->bio_driver1; 295eaa3b919SPawel Jakub Dawidek bp->bio_driver1 = NULL; 296eaa3b919SPawel Jakub Dawidek cbp->bio_to = cp->provider; 297eaa3b919SPawel Jakub Dawidek cbp->bio_done = g_eli_write_done; 298eaa3b919SPawel Jakub Dawidek 299eaa3b919SPawel Jakub Dawidek /* Number of sectors from decrypted provider, eg. 1. */ 300eaa3b919SPawel Jakub Dawidek nsec = bp->bio_length / bp->bio_to->sectorsize; 301eaa3b919SPawel Jakub Dawidek /* Number of sectors from encrypted provider, eg. 9. */ 302eaa3b919SPawel Jakub Dawidek nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize; 303eaa3b919SPawel Jakub Dawidek 304eaa3b919SPawel Jakub Dawidek cbp->bio_length = cp->provider->sectorsize * nsec; 305eaa3b919SPawel Jakub Dawidek cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector; 306eaa3b919SPawel Jakub Dawidek cbp->bio_data = bp->bio_driver2; 307eaa3b919SPawel Jakub Dawidek 308eaa3b919SPawel Jakub Dawidek /* 309eaa3b919SPawel Jakub Dawidek * We write more than what is requested, so we have to be ready to write 310eaa3b919SPawel Jakub Dawidek * more than MAXPHYS. 311eaa3b919SPawel Jakub Dawidek */ 312eaa3b919SPawel Jakub Dawidek cbp2 = NULL; 313eaa3b919SPawel Jakub Dawidek if (cbp->bio_length > MAXPHYS) { 314eaa3b919SPawel Jakub Dawidek cbp2 = g_duplicate_bio(bp); 315eaa3b919SPawel Jakub Dawidek cbp2->bio_length = cbp->bio_length - MAXPHYS; 316eaa3b919SPawel Jakub Dawidek cbp2->bio_data = cbp->bio_data + MAXPHYS; 317eaa3b919SPawel Jakub Dawidek cbp2->bio_offset = cbp->bio_offset + MAXPHYS; 318eaa3b919SPawel Jakub Dawidek cbp2->bio_to = cp->provider; 319eaa3b919SPawel Jakub Dawidek cbp2->bio_done = g_eli_write_done; 320eaa3b919SPawel Jakub Dawidek cbp->bio_length = MAXPHYS; 321eaa3b919SPawel Jakub Dawidek } 322eaa3b919SPawel Jakub Dawidek /* 323eaa3b919SPawel Jakub Dawidek * Send encrypted data to the provider. 324eaa3b919SPawel Jakub Dawidek */ 325eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp, "Sending request."); 326eaa3b919SPawel Jakub Dawidek bp->bio_inbed = 0; 327eaa3b919SPawel Jakub Dawidek bp->bio_children = (cbp2 != NULL ? 2 : 1); 328eaa3b919SPawel Jakub Dawidek g_io_request(cbp, cp); 329eaa3b919SPawel Jakub Dawidek if (cbp2 != NULL) { 330eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp2, "Sending request."); 331eaa3b919SPawel Jakub Dawidek g_io_request(cbp2, cp); 332eaa3b919SPawel Jakub Dawidek } 333eaa3b919SPawel Jakub Dawidek return (0); 334eaa3b919SPawel Jakub Dawidek } 335eaa3b919SPawel Jakub Dawidek 336eaa3b919SPawel Jakub Dawidek void 337eaa3b919SPawel Jakub Dawidek g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp) 338eaa3b919SPawel Jakub Dawidek { 339eaa3b919SPawel Jakub Dawidek struct g_consumer *cp; 340eaa3b919SPawel Jakub Dawidek struct bio *cbp, *cbp2; 341eaa3b919SPawel Jakub Dawidek size_t size; 342eaa3b919SPawel Jakub Dawidek off_t nsec; 343eaa3b919SPawel Jakub Dawidek 344eaa3b919SPawel Jakub Dawidek bp->bio_pflags = 0; 345eaa3b919SPawel Jakub Dawidek 346eaa3b919SPawel Jakub Dawidek cp = LIST_FIRST(&sc->sc_geom->consumer); 347eaa3b919SPawel Jakub Dawidek cbp = bp->bio_driver1; 348eaa3b919SPawel Jakub Dawidek bp->bio_driver1 = NULL; 349eaa3b919SPawel Jakub Dawidek cbp->bio_to = cp->provider; 350eaa3b919SPawel Jakub Dawidek cbp->bio_done = g_eli_read_done; 351eaa3b919SPawel Jakub Dawidek 352eaa3b919SPawel Jakub Dawidek /* Number of sectors from decrypted provider, eg. 1. */ 353eaa3b919SPawel Jakub Dawidek nsec = bp->bio_length / bp->bio_to->sectorsize; 354eaa3b919SPawel Jakub Dawidek /* Number of sectors from encrypted provider, eg. 9. */ 355eaa3b919SPawel Jakub Dawidek nsec = (nsec * sc->sc_bytes_per_sector) / cp->provider->sectorsize; 356eaa3b919SPawel Jakub Dawidek 357eaa3b919SPawel Jakub Dawidek cbp->bio_length = cp->provider->sectorsize * nsec; 358eaa3b919SPawel Jakub Dawidek size = cbp->bio_length; 359eaa3b919SPawel Jakub Dawidek size += sc->sc_alen * nsec; 360eaa3b919SPawel Jakub Dawidek size += sizeof(struct cryptop) * nsec; 361eaa3b919SPawel Jakub Dawidek size += sizeof(struct cryptodesc) * nsec * 2; 362eaa3b919SPawel Jakub Dawidek size += G_ELI_AUTH_SECKEYLEN * nsec; 363eaa3b919SPawel Jakub Dawidek size += sizeof(struct uio) * nsec; 364eaa3b919SPawel Jakub Dawidek size += sizeof(struct iovec) * nsec; 365eaa3b919SPawel Jakub Dawidek cbp->bio_offset = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector; 366eaa3b919SPawel Jakub Dawidek bp->bio_driver2 = malloc(size, M_ELI, M_WAITOK); 367eaa3b919SPawel Jakub Dawidek cbp->bio_data = bp->bio_driver2; 368eaa3b919SPawel Jakub Dawidek 369eaa3b919SPawel Jakub Dawidek /* 370eaa3b919SPawel Jakub Dawidek * We read more than what is requested, so we have to be ready to read 371eaa3b919SPawel Jakub Dawidek * more than MAXPHYS. 372eaa3b919SPawel Jakub Dawidek */ 373eaa3b919SPawel Jakub Dawidek cbp2 = NULL; 374eaa3b919SPawel Jakub Dawidek if (cbp->bio_length > MAXPHYS) { 375eaa3b919SPawel Jakub Dawidek cbp2 = g_duplicate_bio(bp); 376eaa3b919SPawel Jakub Dawidek cbp2->bio_length = cbp->bio_length - MAXPHYS; 377eaa3b919SPawel Jakub Dawidek cbp2->bio_data = cbp->bio_data + MAXPHYS; 378eaa3b919SPawel Jakub Dawidek cbp2->bio_offset = cbp->bio_offset + MAXPHYS; 379eaa3b919SPawel Jakub Dawidek cbp2->bio_to = cp->provider; 380eaa3b919SPawel Jakub Dawidek cbp2->bio_done = g_eli_read_done; 381eaa3b919SPawel Jakub Dawidek cbp->bio_length = MAXPHYS; 382eaa3b919SPawel Jakub Dawidek } 383eaa3b919SPawel Jakub Dawidek /* 384eaa3b919SPawel Jakub Dawidek * Read encrypted data from provider. 385eaa3b919SPawel Jakub Dawidek */ 386eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp, "Sending request."); 387eaa3b919SPawel Jakub Dawidek g_io_request(cbp, cp); 388eaa3b919SPawel Jakub Dawidek if (cbp2 != NULL) { 389eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(2, cbp2, "Sending request."); 390eaa3b919SPawel Jakub Dawidek g_io_request(cbp2, cp); 391eaa3b919SPawel Jakub Dawidek } 392eaa3b919SPawel Jakub Dawidek } 393eaa3b919SPawel Jakub Dawidek 394eaa3b919SPawel Jakub Dawidek /* 395eaa3b919SPawel Jakub Dawidek * This is the main function responsible for cryptography (ie. communication 396eaa3b919SPawel Jakub Dawidek * with crypto(9) subsystem). 397056638c4SPawel Jakub Dawidek * 398056638c4SPawel Jakub Dawidek * BIO_READ: 399056638c4SPawel Jakub Dawidek * 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 400056638c4SPawel Jakub Dawidek * BIO_WRITE: 401056638c4SPawel Jakub Dawidek * g_eli_start -> G_ELI_AUTH_RUN -> g_eli_auth_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver 402eaa3b919SPawel Jakub Dawidek */ 403eaa3b919SPawel Jakub Dawidek void 404eaa3b919SPawel Jakub Dawidek g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp) 405eaa3b919SPawel Jakub Dawidek { 406eaa3b919SPawel Jakub Dawidek struct g_eli_softc *sc; 407eaa3b919SPawel Jakub Dawidek struct cryptop *crp; 408eaa3b919SPawel Jakub Dawidek struct cryptodesc *crde, *crda; 409eaa3b919SPawel Jakub Dawidek struct uio *uio; 410eaa3b919SPawel Jakub Dawidek struct iovec *iov; 411eaa3b919SPawel Jakub Dawidek u_int i, lsec, nsec, data_secsize, decr_secsize, encr_secsize; 412eaa3b919SPawel Jakub Dawidek off_t dstoff; 413eaa3b919SPawel Jakub Dawidek int err, error; 414eaa3b919SPawel Jakub Dawidek u_char *p, *data, *auth, *authkey, *plaindata; 415eaa3b919SPawel Jakub Dawidek 416eaa3b919SPawel Jakub Dawidek G_ELI_LOGREQ(3, bp, "%s", __func__); 417eaa3b919SPawel Jakub Dawidek 418eaa3b919SPawel Jakub Dawidek bp->bio_pflags = wr->w_number; 419eaa3b919SPawel Jakub Dawidek sc = wr->w_softc; 420eaa3b919SPawel Jakub Dawidek /* Sectorsize of decrypted provider eg. 4096. */ 421eaa3b919SPawel Jakub Dawidek decr_secsize = bp->bio_to->sectorsize; 422eaa3b919SPawel Jakub Dawidek /* The real sectorsize of encrypted provider, eg. 512. */ 423eaa3b919SPawel Jakub Dawidek encr_secsize = LIST_FIRST(&sc->sc_geom->consumer)->provider->sectorsize; 424eaa3b919SPawel Jakub Dawidek /* Number of data bytes in one encrypted sector, eg. 480. */ 425eaa3b919SPawel Jakub Dawidek data_secsize = sc->sc_data_per_sector; 426eaa3b919SPawel Jakub Dawidek /* Number of sectors from decrypted provider, eg. 2. */ 427eaa3b919SPawel Jakub Dawidek nsec = bp->bio_length / decr_secsize; 428eaa3b919SPawel Jakub Dawidek /* Number of sectors from encrypted provider, eg. 18. */ 429eaa3b919SPawel Jakub Dawidek nsec = (nsec * sc->sc_bytes_per_sector) / encr_secsize; 430eaa3b919SPawel Jakub Dawidek /* Last sector number in every big sector, eg. 9. */ 431eaa3b919SPawel Jakub Dawidek lsec = sc->sc_bytes_per_sector / encr_secsize; 432eaa3b919SPawel Jakub Dawidek /* Destination offset, used for IV generation. */ 433eaa3b919SPawel Jakub Dawidek dstoff = (bp->bio_offset / bp->bio_to->sectorsize) * sc->sc_bytes_per_sector; 434eaa3b919SPawel Jakub Dawidek 43515d6ee8dSPawel Jakub Dawidek auth = NULL; /* Silence compiler warning. */ 436eaa3b919SPawel Jakub Dawidek plaindata = bp->bio_data; 437eaa3b919SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) { 438eaa3b919SPawel Jakub Dawidek data = bp->bio_driver2; 439eaa3b919SPawel Jakub Dawidek auth = data + encr_secsize * nsec; 440eaa3b919SPawel Jakub Dawidek p = auth + sc->sc_alen * nsec; 441eaa3b919SPawel Jakub Dawidek } else { 442eaa3b919SPawel Jakub Dawidek size_t size; 443eaa3b919SPawel Jakub Dawidek 444eaa3b919SPawel Jakub Dawidek size = encr_secsize * nsec; 445eaa3b919SPawel Jakub Dawidek size += sizeof(*crp) * nsec; 446eaa3b919SPawel Jakub Dawidek size += sizeof(*crde) * nsec; 447eaa3b919SPawel Jakub Dawidek size += sizeof(*crda) * nsec; 448eaa3b919SPawel Jakub Dawidek size += G_ELI_AUTH_SECKEYLEN * nsec; 449eaa3b919SPawel Jakub Dawidek size += sizeof(*uio) * nsec; 450eaa3b919SPawel Jakub Dawidek size += sizeof(*iov) * nsec; 451eaa3b919SPawel Jakub Dawidek data = malloc(size, M_ELI, M_WAITOK); 452eaa3b919SPawel Jakub Dawidek bp->bio_driver2 = data; 453eaa3b919SPawel Jakub Dawidek p = data + encr_secsize * nsec; 454eaa3b919SPawel Jakub Dawidek } 455eaa3b919SPawel Jakub Dawidek bp->bio_inbed = 0; 456eaa3b919SPawel Jakub Dawidek bp->bio_children = nsec; 457eaa3b919SPawel Jakub Dawidek 458eaa3b919SPawel Jakub Dawidek error = 0; 459eaa3b919SPawel Jakub Dawidek for (i = 1; i <= nsec; i++, dstoff += encr_secsize) { 460eaa3b919SPawel Jakub Dawidek crp = (struct cryptop *)p; p += sizeof(*crp); 461eaa3b919SPawel Jakub Dawidek crde = (struct cryptodesc *)p; p += sizeof(*crde); 462eaa3b919SPawel Jakub Dawidek crda = (struct cryptodesc *)p; p += sizeof(*crda); 463eaa3b919SPawel Jakub Dawidek authkey = (u_char *)p; p += G_ELI_AUTH_SECKEYLEN; 464eaa3b919SPawel Jakub Dawidek uio = (struct uio *)p; p += sizeof(*uio); 465eaa3b919SPawel Jakub Dawidek iov = (struct iovec *)p; p += sizeof(*iov); 466eaa3b919SPawel Jakub Dawidek 467eaa3b919SPawel Jakub Dawidek data_secsize = sc->sc_data_per_sector; 468eaa3b919SPawel Jakub Dawidek if ((i % lsec) == 0) 469eaa3b919SPawel Jakub Dawidek data_secsize = decr_secsize % data_secsize; 470eaa3b919SPawel Jakub Dawidek 471eaa3b919SPawel Jakub Dawidek if (bp->bio_cmd == BIO_READ) { 472eaa3b919SPawel Jakub Dawidek /* Remember read HMAC. */ 473eaa3b919SPawel Jakub Dawidek bcopy(data, auth, sc->sc_alen); 474eaa3b919SPawel Jakub Dawidek auth += sc->sc_alen; 475eaa3b919SPawel Jakub Dawidek /* TODO: bzero(9) can be commented out later. */ 476eaa3b919SPawel Jakub Dawidek bzero(data, sc->sc_alen); 477eaa3b919SPawel Jakub Dawidek } else { 478eaa3b919SPawel Jakub Dawidek bcopy(plaindata, data + sc->sc_alen, data_secsize); 479eaa3b919SPawel Jakub Dawidek plaindata += data_secsize; 480eaa3b919SPawel Jakub Dawidek } 481eaa3b919SPawel Jakub Dawidek 482eaa3b919SPawel Jakub Dawidek iov->iov_len = sc->sc_alen + data_secsize; 483eaa3b919SPawel Jakub Dawidek iov->iov_base = data; 484eaa3b919SPawel Jakub Dawidek data += encr_secsize; 485eaa3b919SPawel Jakub Dawidek 486eaa3b919SPawel Jakub Dawidek uio->uio_iov = iov; 487eaa3b919SPawel Jakub Dawidek uio->uio_iovcnt = 1; 488eaa3b919SPawel Jakub Dawidek uio->uio_segflg = UIO_SYSSPACE; 489eaa3b919SPawel Jakub Dawidek uio->uio_resid = iov->iov_len; 490eaa3b919SPawel Jakub Dawidek 491eaa3b919SPawel Jakub Dawidek crp->crp_sid = wr->w_sid; 49215d6ee8dSPawel Jakub Dawidek crp->crp_ilen = uio->uio_resid; 4938cfab1deSPawel Jakub Dawidek crp->crp_olen = data_secsize; 494eaa3b919SPawel Jakub Dawidek crp->crp_opaque = (void *)bp; 495eaa3b919SPawel Jakub Dawidek crp->crp_buf = (void *)uio; 496eaa3b919SPawel Jakub Dawidek crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL; 497eaa3b919SPawel Jakub Dawidek if (g_eli_batch) 498eaa3b919SPawel Jakub Dawidek crp->crp_flags |= CRYPTO_F_BATCH; 499eaa3b919SPawel Jakub Dawidek if (bp->bio_cmd == BIO_WRITE) { 500eaa3b919SPawel Jakub Dawidek crp->crp_callback = g_eli_auth_write_done; 501eaa3b919SPawel Jakub Dawidek crp->crp_desc = crde; 502eaa3b919SPawel Jakub Dawidek crde->crd_next = crda; 503eaa3b919SPawel Jakub Dawidek crda->crd_next = NULL; 504eaa3b919SPawel Jakub Dawidek } else { 505eaa3b919SPawel Jakub Dawidek crp->crp_callback = g_eli_auth_read_done; 506eaa3b919SPawel Jakub Dawidek crp->crp_desc = crda; 507eaa3b919SPawel Jakub Dawidek crda->crd_next = crde; 508eaa3b919SPawel Jakub Dawidek crde->crd_next = NULL; 509eaa3b919SPawel Jakub Dawidek } 510eaa3b919SPawel Jakub Dawidek 511eaa3b919SPawel Jakub Dawidek crde->crd_skip = sc->sc_alen; 512eaa3b919SPawel Jakub Dawidek crde->crd_len = data_secsize; 513eaa3b919SPawel Jakub Dawidek crde->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 514eaa3b919SPawel Jakub Dawidek if (bp->bio_cmd == BIO_WRITE) 515eaa3b919SPawel Jakub Dawidek crde->crd_flags |= CRD_F_ENCRYPT; 516eaa3b919SPawel Jakub Dawidek crde->crd_alg = sc->sc_ealgo; 517c6a26d4cSPawel Jakub Dawidek crde->crd_key = g_eli_crypto_key(sc, dstoff, encr_secsize); 518eaa3b919SPawel Jakub Dawidek crde->crd_klen = sc->sc_ekeylen; 5199a5a1d1eSPawel Jakub Dawidek if (sc->sc_ealgo == CRYPTO_AES_XTS) 5209a5a1d1eSPawel Jakub Dawidek crde->crd_klen <<= 1; 521eaa3b919SPawel Jakub Dawidek g_eli_crypto_ivgen(sc, dstoff, crde->crd_iv, 522eaa3b919SPawel Jakub Dawidek sizeof(crde->crd_iv)); 523eaa3b919SPawel Jakub Dawidek 524eaa3b919SPawel Jakub Dawidek crda->crd_skip = sc->sc_alen; 525eaa3b919SPawel Jakub Dawidek crda->crd_len = data_secsize; 526eaa3b919SPawel Jakub Dawidek crda->crd_inject = 0; 527eaa3b919SPawel Jakub Dawidek crda->crd_flags = CRD_F_KEY_EXPLICIT; 528eaa3b919SPawel Jakub Dawidek crda->crd_alg = sc->sc_aalgo; 529eaa3b919SPawel Jakub Dawidek g_eli_auth_keygen(sc, dstoff, authkey); 530eaa3b919SPawel Jakub Dawidek crda->crd_key = authkey; 531eaa3b919SPawel Jakub Dawidek crda->crd_klen = G_ELI_AUTH_SECKEYLEN * 8; 532eaa3b919SPawel Jakub Dawidek 533eaa3b919SPawel Jakub Dawidek crp->crp_etype = 0; 534eaa3b919SPawel Jakub Dawidek err = crypto_dispatch(crp); 535eaa3b919SPawel Jakub Dawidek if (err != 0 && error == 0) 536eaa3b919SPawel Jakub Dawidek error = err; 537eaa3b919SPawel Jakub Dawidek } 538eaa3b919SPawel Jakub Dawidek if (bp->bio_error == 0) 539eaa3b919SPawel Jakub Dawidek bp->bio_error = error; 540eaa3b919SPawel Jakub Dawidek } 541