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