g_eli.c (159306) | g_eli.c (159307) |
---|---|
1/*- 2 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> |
28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli.c 159306 2006-06-05 21:25:19Z pjd $"); | 28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli.c 159307 2006-06-05 21:38:54Z pjd $"); |
29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/kernel.h> 33#include <sys/linker.h> 34#include <sys/module.h> 35#include <sys/lock.h> 36#include <sys/mutex.h> --- 34 unchanged lines hidden (view full) --- 71u_int g_eli_overwrites = 5; 72TUNABLE_INT("kern.geom.eli.overwrites", &g_eli_overwrites); 73SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RW, &g_eli_overwrites, 74 0, "Number of times on-disk keys should be overwritten when destroying them"); 75static u_int g_eli_threads = 0; 76TUNABLE_INT("kern.geom.eli.threads", &g_eli_threads); 77SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RW, &g_eli_threads, 0, 78 "Number of threads doing crypto work"); | 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/kernel.h> 33#include <sys/linker.h> 34#include <sys/module.h> 35#include <sys/lock.h> 36#include <sys/mutex.h> --- 34 unchanged lines hidden (view full) --- 71u_int g_eli_overwrites = 5; 72TUNABLE_INT("kern.geom.eli.overwrites", &g_eli_overwrites); 73SYSCTL_UINT(_kern_geom_eli, OID_AUTO, overwrites, CTLFLAG_RW, &g_eli_overwrites, 74 0, "Number of times on-disk keys should be overwritten when destroying them"); 75static u_int g_eli_threads = 0; 76TUNABLE_INT("kern.geom.eli.threads", &g_eli_threads); 77SYSCTL_UINT(_kern_geom_eli, OID_AUTO, threads, CTLFLAG_RW, &g_eli_threads, 0, 78 "Number of threads doing crypto work"); |
79u_int g_eli_batch = 0; 80TUNABLE_INT("kern.geom.eli.batch", &g_eli_batch); 81SYSCTL_UINT(_kern_geom_eli, OID_AUTO, batch, CTLFLAG_RW, &g_eli_batch, 0, 82 "Use crypto operations batching"); |
|
79 80static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp, 81 struct g_geom *gp); | 83 84static int g_eli_destroy_geom(struct gctl_req *req, struct g_class *mp, 85 struct g_geom *gp); |
82static void g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp); | |
83 84static g_taste_t g_eli_taste; 85static g_dumpconf_t g_eli_dumpconf; 86 87struct g_class g_eli_class = { 88 .name = G_ELI_CLASS_NAME, 89 .version = G_VERSION, 90 .ctlreq = g_eli_config, --- 11 unchanged lines hidden (view full) --- 102 */ 103 104 105/* 106 * EAGAIN from crypto(9) means, that we were probably balanced to another crypto 107 * accelerator or something like this. 108 * The function updates the SID and rerun the operation. 109 */ | 86 87static g_taste_t g_eli_taste; 88static g_dumpconf_t g_eli_dumpconf; 89 90struct g_class g_eli_class = { 91 .name = G_ELI_CLASS_NAME, 92 .version = G_VERSION, 93 .ctlreq = g_eli_config, --- 11 unchanged lines hidden (view full) --- 105 */ 106 107 108/* 109 * EAGAIN from crypto(9) means, that we were probably balanced to another crypto 110 * accelerator or something like this. 111 * The function updates the SID and rerun the operation. 112 */ |
110static int | 113int |
111g_eli_crypto_rerun(struct cryptop *crp) 112{ 113 struct g_eli_softc *sc; 114 struct g_eli_worker *wr; 115 struct bio *bp; 116 int error; 117 118 bp = (struct bio *)crp->crp_opaque; --- 16 unchanged lines hidden (view full) --- 135 return (error); 136} 137 138/* 139 * The function is called afer reading encrypted data from the provider. 140 * 141 * g_eli_start -> g_io_request -> G_ELI_READ_DONE -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 142 */ | 114g_eli_crypto_rerun(struct cryptop *crp) 115{ 116 struct g_eli_softc *sc; 117 struct g_eli_worker *wr; 118 struct bio *bp; 119 int error; 120 121 bp = (struct bio *)crp->crp_opaque; --- 16 unchanged lines hidden (view full) --- 138 return (error); 139} 140 141/* 142 * The function is called afer reading encrypted data from the provider. 143 * 144 * g_eli_start -> g_io_request -> G_ELI_READ_DONE -> g_eli_crypto_run -> g_eli_crypto_read_done -> g_io_deliver 145 */ |
143static void | 146void |
144g_eli_read_done(struct bio *bp) 145{ 146 struct g_eli_softc *sc; 147 struct bio *pbp; 148 149 G_ELI_LOGREQ(2, bp, "Request done."); 150 pbp = bp->bio_parent; 151 if (pbp->bio_error == 0) 152 pbp->bio_error = bp->bio_error; | 147g_eli_read_done(struct bio *bp) 148{ 149 struct g_eli_softc *sc; 150 struct bio *pbp; 151 152 G_ELI_LOGREQ(2, bp, "Request done."); 153 pbp = bp->bio_parent; 154 if (pbp->bio_error == 0) 155 pbp->bio_error = bp->bio_error; |
156 /* 157 * Do we have all sectors already? 158 */ 159 pbp->bio_inbed++; 160 if (pbp->bio_inbed < pbp->bio_children) 161 return; |
|
153 g_destroy_bio(bp); 154 if (pbp->bio_error != 0) { 155 G_ELI_LOGREQ(0, pbp, "%s() failed", __func__); 156 pbp->bio_completed = 0; | 162 g_destroy_bio(bp); 163 if (pbp->bio_error != 0) { 164 G_ELI_LOGREQ(0, pbp, "%s() failed", __func__); 165 pbp->bio_completed = 0; |
166 if (pbp->bio_driver2 != NULL) { 167 free(pbp->bio_driver2, M_ELI); 168 pbp->bio_driver2 = NULL; 169 } |
|
157 g_io_deliver(pbp, pbp->bio_error); 158 return; 159 } 160 sc = pbp->bio_to->geom->softc; 161 mtx_lock(&sc->sc_queue_mtx); 162 bioq_insert_tail(&sc->sc_queue, pbp); 163 mtx_unlock(&sc->sc_queue_mtx); 164 wakeup(sc); 165} 166 167/* | 170 g_io_deliver(pbp, pbp->bio_error); 171 return; 172 } 173 sc = pbp->bio_to->geom->softc; 174 mtx_lock(&sc->sc_queue_mtx); 175 bioq_insert_tail(&sc->sc_queue, pbp); 176 mtx_unlock(&sc->sc_queue_mtx); 177 wakeup(sc); 178} 179 180/* |
168 * The function is called after we read and decrypt data. 169 * 170 * g_eli_start -> g_io_request -> g_eli_read_done -> g_eli_crypto_run -> G_ELI_CRYPTO_READ_DONE -> g_io_deliver 171 */ 172static int 173g_eli_crypto_read_done(struct cryptop *crp) 174{ 175 struct bio *bp; 176 177 if (crp->crp_etype == EAGAIN) { 178 if (g_eli_crypto_rerun(crp) == 0) 179 return (0); 180 } 181 bp = (struct bio *)crp->crp_opaque; 182 bp->bio_inbed++; 183 if (crp->crp_etype == 0) { 184 G_ELI_DEBUG(3, "Crypto READ request done (%d/%d).", 185 bp->bio_inbed, bp->bio_children); 186 bp->bio_completed += crp->crp_olen; 187 } else { 188 G_ELI_DEBUG(1, "Crypto READ request failed (%d/%d) error=%d.", 189 bp->bio_inbed, bp->bio_children, crp->crp_etype); 190 if (bp->bio_error == 0) 191 bp->bio_error = crp->crp_etype; 192 } 193 /* 194 * Do we have all sectors already? 195 */ 196 if (bp->bio_inbed < bp->bio_children) 197 return (0); 198 free(bp->bio_driver2, M_ELI); 199 bp->bio_driver2 = NULL; 200 if (bp->bio_error != 0) { 201 G_ELI_LOGREQ(0, bp, "Crypto READ request failed (error=%d).", 202 bp->bio_error); 203 bp->bio_completed = 0; 204 } 205 /* 206 * Read is finished, send it up. 207 */ 208 g_io_deliver(bp, bp->bio_error); 209 return (0); 210} 211 212/* | |
213 * The function is called after we encrypt and write data. 214 * 215 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver 216 */ | 181 * The function is called after we encrypt and write data. 182 * 183 * g_eli_start -> g_eli_crypto_run -> g_eli_crypto_write_done -> g_io_request -> G_ELI_WRITE_DONE -> g_io_deliver 184 */ |
217static void | 185void |
218g_eli_write_done(struct bio *bp) 219{ 220 struct bio *pbp; 221 222 G_ELI_LOGREQ(2, bp, "Request done."); 223 pbp = bp->bio_parent; | 186g_eli_write_done(struct bio *bp) 187{ 188 struct bio *pbp; 189 190 G_ELI_LOGREQ(2, bp, "Request done."); 191 pbp = bp->bio_parent; |
224 if (pbp->bio_error == 0) 225 pbp->bio_error = bp->bio_error; | 192 if (pbp->bio_error == 0) { 193 if (bp->bio_error != 0) 194 pbp->bio_error = bp->bio_error; 195 } 196 /* 197 * Do we have all sectors already? 198 */ 199 pbp->bio_inbed++; 200 if (pbp->bio_inbed < pbp->bio_children) 201 return; |
226 free(pbp->bio_driver2, M_ELI); 227 pbp->bio_driver2 = NULL; | 202 free(pbp->bio_driver2, M_ELI); 203 pbp->bio_driver2 = NULL; |
228 if (pbp->bio_error == 0) 229 pbp->bio_completed = pbp->bio_length; 230 else { | 204 if (pbp->bio_error != 0) { |
231 G_ELI_LOGREQ(0, pbp, "Crypto WRITE request failed (error=%d).", 232 pbp->bio_error); 233 pbp->bio_completed = 0; 234 } 235 g_destroy_bio(bp); 236 /* 237 * Write is finished, send it up. 238 */ | 205 G_ELI_LOGREQ(0, pbp, "Crypto WRITE request failed (error=%d).", 206 pbp->bio_error); 207 pbp->bio_completed = 0; 208 } 209 g_destroy_bio(bp); 210 /* 211 * Write is finished, send it up. 212 */ |
213 pbp->bio_completed = pbp->bio_length; |
|
239 g_io_deliver(pbp, pbp->bio_error); 240} 241 242/* | 214 g_io_deliver(pbp, pbp->bio_error); 215} 216 217/* |
243 * The function is called after data encryption. 244 * 245 * g_eli_start -> g_eli_crypto_run -> G_ELI_CRYPTO_WRITE_DONE -> g_io_request -> g_eli_write_done -> g_io_deliver 246 */ 247static int 248g_eli_crypto_write_done(struct cryptop *crp) 249{ 250 struct g_geom *gp; 251 struct g_consumer *cp; 252 struct bio *bp, *cbp; 253 254 if (crp->crp_etype == EAGAIN) { 255 if (g_eli_crypto_rerun(crp) == 0) 256 return (0); 257 } 258 bp = (struct bio *)crp->crp_opaque; 259 bp->bio_inbed++; 260 if (crp->crp_etype == 0) { 261 G_ELI_DEBUG(3, "Crypto WRITE request done (%d/%d).", 262 bp->bio_inbed, bp->bio_children); 263 } else { 264 G_ELI_DEBUG(1, "Crypto WRITE request failed (%d/%d) error=%d.", 265 bp->bio_inbed, bp->bio_children, crp->crp_etype); 266 if (bp->bio_error == 0) 267 bp->bio_error = crp->crp_etype; 268 } 269 /* 270 * All sectors are already encrypted? 271 */ 272 if (bp->bio_inbed < bp->bio_children) 273 return (0); 274 bp->bio_inbed = 0; 275 bp->bio_children = 1; 276 cbp = bp->bio_driver1; 277 bp->bio_driver1 = NULL; 278 if (bp->bio_error != 0) { 279 G_ELI_LOGREQ(0, bp, "Crypto WRITE request failed (error=%d).", 280 bp->bio_error); 281 free(bp->bio_driver2, M_ELI); 282 bp->bio_driver2 = NULL; 283 g_destroy_bio(cbp); 284 g_io_deliver(bp, bp->bio_error); 285 return (0); 286 } 287 cbp->bio_data = bp->bio_driver2; 288 cbp->bio_done = g_eli_write_done; 289 gp = bp->bio_to->geom; 290 cp = LIST_FIRST(&gp->consumer); 291 cbp->bio_to = cp->provider; 292 G_ELI_LOGREQ(2, cbp, "Sending request."); 293 /* 294 * Send encrypted data to the provider. 295 */ 296 g_io_request(cbp, cp); 297 return (0); 298} 299 300/* | |
301 * This function should never be called, but GEOM made as it set ->orphan() 302 * method for every geom. 303 */ 304static void 305g_eli_orphan_spoil_assert(struct g_consumer *cp) 306{ 307 308 panic("Function %s() called for %s.", __func__, cp->geom->name); --- 45 unchanged lines hidden (view full) --- 354 } 355 cbp = g_clone_bio(bp); 356 if (cbp == NULL) { 357 g_io_deliver(bp, ENOMEM); 358 return; 359 } 360 switch (bp->bio_cmd) { 361 case BIO_READ: | 218 * This function should never be called, but GEOM made as it set ->orphan() 219 * method for every geom. 220 */ 221static void 222g_eli_orphan_spoil_assert(struct g_consumer *cp) 223{ 224 225 panic("Function %s() called for %s.", __func__, cp->geom->name); --- 45 unchanged lines hidden (view full) --- 271 } 272 cbp = g_clone_bio(bp); 273 if (cbp == NULL) { 274 g_io_deliver(bp, ENOMEM); 275 return; 276 } 277 switch (bp->bio_cmd) { 278 case BIO_READ: |
362 cbp->bio_done = g_eli_read_done; 363 cp = LIST_FIRST(&sc->sc_geom->consumer); 364 cbp->bio_to = cp->provider; 365 G_ELI_LOGREQ(2, cbp, "Sending request."); 366 /* 367 * Read encrypted data from provider. 368 */ 369 g_io_request(cbp, cp); 370 break; | 279 if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) { 280 bp->bio_driver2 = NULL; 281 cbp->bio_done = g_eli_read_done; 282 cp = LIST_FIRST(&sc->sc_geom->consumer); 283 cbp->bio_to = cp->provider; 284 G_ELI_LOGREQ(2, cbp, "Sending request."); 285 /* 286 * Read encrypted data from provider. 287 */ 288 g_io_request(cbp, cp); 289 break; 290 } 291 bp->bio_pflags = 255; 292 /* FALLTHROUGH */ |
371 case BIO_WRITE: 372 bp->bio_driver1 = cbp; 373 mtx_lock(&sc->sc_queue_mtx); 374 bioq_insert_tail(&sc->sc_queue, bp); 375 mtx_unlock(&sc->sc_queue_mtx); 376 wakeup(sc); 377 break; 378 case BIO_GETATTR: --- 28 unchanged lines hidden (view full) --- 407 mtx_unlock_spin(&sched_lock); 408 409 G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm); 410 411 for (;;) { 412 mtx_lock(&sc->sc_queue_mtx); 413 bp = bioq_takefirst(&sc->sc_queue); 414 if (bp == NULL) { | 293 case BIO_WRITE: 294 bp->bio_driver1 = cbp; 295 mtx_lock(&sc->sc_queue_mtx); 296 bioq_insert_tail(&sc->sc_queue, bp); 297 mtx_unlock(&sc->sc_queue_mtx); 298 wakeup(sc); 299 break; 300 case BIO_GETATTR: --- 28 unchanged lines hidden (view full) --- 329 mtx_unlock_spin(&sched_lock); 330 331 G_ELI_DEBUG(1, "Thread %s started.", curthread->td_proc->p_comm); 332 333 for (;;) { 334 mtx_lock(&sc->sc_queue_mtx); 335 bp = bioq_takefirst(&sc->sc_queue); 336 if (bp == NULL) { |
415 if ((sc->sc_flags & G_ELI_FLAG_DESTROY) != 0) { | 337 if (sc->sc_flags & G_ELI_FLAG_DESTROY) { |
416 LIST_REMOVE(wr, w_next); 417 crypto_freesession(wr->w_sid); 418 free(wr, M_ELI); 419 G_ELI_DEBUG(1, "Thread %s exiting.", 420 curthread->td_proc->p_comm); 421 wakeup(&sc->sc_workers); 422 mtx_unlock(&sc->sc_queue_mtx); 423 kthread_exit(0); 424 } 425 msleep(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, 426 "geli:w", 0); 427 continue; 428 } 429 mtx_unlock(&sc->sc_queue_mtx); | 338 LIST_REMOVE(wr, w_next); 339 crypto_freesession(wr->w_sid); 340 free(wr, M_ELI); 341 G_ELI_DEBUG(1, "Thread %s exiting.", 342 curthread->td_proc->p_comm); 343 wakeup(&sc->sc_workers); 344 mtx_unlock(&sc->sc_queue_mtx); 345 kthread_exit(0); 346 } 347 msleep(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, 348 "geli:w", 0); 349 continue; 350 } 351 mtx_unlock(&sc->sc_queue_mtx); |
430 g_eli_crypto_run(wr, bp); | 352 if (bp->bio_cmd == BIO_READ && bp->bio_pflags == 255) 353 g_eli_auth_read(sc, bp); 354 else if (sc->sc_flags & G_ELI_FLAG_AUTH) 355 g_eli_auth_run(wr, bp); 356 else 357 g_eli_crypto_run(wr, bp); |
431 } 432} 433 434/* 435 * Here we generate IV. It is unique for every sector. 436 */ | 358 } 359} 360 361/* 362 * Here we generate IV. It is unique for every sector. 363 */ |
437static void | 364void |
438g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv, 439 size_t size) 440{ 441 u_char hash[SHA256_DIGEST_LENGTH]; 442 SHA256_CTX ctx; 443 444 /* Copy precalculated SHA256 context for IV-Key. */ 445 bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx)); 446 SHA256_Update(&ctx, (uint8_t *)&offset, sizeof(offset)); 447 SHA256_Final(hash, &ctx); 448 bcopy(hash, iv, size); 449} 450 | 365g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv, 366 size_t size) 367{ 368 u_char hash[SHA256_DIGEST_LENGTH]; 369 SHA256_CTX ctx; 370 371 /* Copy precalculated SHA256 context for IV-Key. */ 372 bcopy(&sc->sc_ivctx, &ctx, sizeof(ctx)); 373 SHA256_Update(&ctx, (uint8_t *)&offset, sizeof(offset)); 374 SHA256_Final(hash, &ctx); 375 bcopy(hash, iv, size); 376} 377 |
451/* 452 * This is the main function responsible for cryptography (ie. communication 453 * with crypto(9) subsystem). 454 */ 455static void 456g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp) 457{ 458 struct g_eli_softc *sc; 459 struct cryptop *crp; 460 struct cryptodesc *crd; 461 struct uio *uio; 462 struct iovec *iov; 463 u_int i, nsec, add, secsize; 464 int err, error; 465 size_t size; 466 u_char *p, *data; 467 468 G_ELI_LOGREQ(3, bp, "%s", __func__); 469 470 bp->bio_pflags = wr->w_number; 471 sc = wr->w_softc; 472 secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize; 473 nsec = bp->bio_length / secsize; 474 475 /* 476 * Calculate how much memory do we need. 477 * We need separate crypto operation for every single sector. 478 * It is much faster to calculate total amount of needed memory here and 479 * do the allocation once instead of allocating memory in pieces (many, 480 * many pieces). 481 */ 482 size = sizeof(*crp) * nsec; 483 size += sizeof(*crd) * nsec; 484 size += sizeof(*uio) * nsec; 485 size += sizeof(*iov) * nsec; 486 /* 487 * If we write the data we cannot destroy current bio_data content, 488 * so we need to allocate more memory for encrypted data. 489 */ 490 if (bp->bio_cmd == BIO_WRITE) 491 size += bp->bio_length; 492 p = malloc(size, M_ELI, M_WAITOK); 493 494 bp->bio_inbed = 0; 495 bp->bio_children = nsec; 496 bp->bio_driver2 = p; 497 498 if (bp->bio_cmd == BIO_READ) 499 data = bp->bio_data; 500 else { 501 data = p; 502 p += bp->bio_length; 503 bcopy(bp->bio_data, data, bp->bio_length); 504 } 505 506 error = 0; 507 for (i = 0, add = 0; i < nsec; i++, add += secsize) { 508 crp = (struct cryptop *)p; p += sizeof(*crp); 509 crd = (struct cryptodesc *)p; p += sizeof(*crd); 510 uio = (struct uio *)p; p += sizeof(*uio); 511 iov = (struct iovec *)p; p += sizeof(*iov); 512 513 iov->iov_len = secsize; 514 iov->iov_base = data; 515 data += secsize; 516 517 uio->uio_iov = iov; 518 uio->uio_iovcnt = 1; 519 uio->uio_segflg = UIO_SYSSPACE; 520 uio->uio_resid = secsize; 521 522 crp->crp_sid = wr->w_sid; 523 crp->crp_ilen = secsize; 524 crp->crp_olen = secsize; 525 crp->crp_opaque = (void *)bp; 526 crp->crp_buf = (void *)uio; 527 if (bp->bio_cmd == BIO_WRITE) 528 crp->crp_callback = g_eli_crypto_write_done; 529 else /* if (bp->bio_cmd == BIO_READ) */ 530 crp->crp_callback = g_eli_crypto_read_done; 531 crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIFSYNC | CRYPTO_F_REL; 532 crp->crp_desc = crd; 533 534 crd->crd_skip = 0; 535 crd->crd_len = secsize; 536 crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT; 537 if (bp->bio_cmd == BIO_WRITE) 538 crd->crd_flags |= CRD_F_ENCRYPT; 539 crd->crd_alg = sc->sc_algo; 540 crd->crd_key = sc->sc_datakey; 541 crd->crd_klen = sc->sc_keylen; 542 g_eli_crypto_ivgen(sc, bp->bio_offset + add, crd->crd_iv, 543 sizeof(crd->crd_iv)); 544 crd->crd_next = NULL; 545 546 crp->crp_etype = 0; 547 err = crypto_dispatch(crp); 548 if (error == 0) 549 error = err; 550 } 551 if (bp->bio_error == 0) 552 bp->bio_error = error; 553} 554 | |
555int 556g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 557 struct g_eli_metadata *md) 558{ 559 struct g_geom *gp; 560 struct g_consumer *cp; 561 u_char *buf = NULL; 562 int error; --- 91 unchanged lines hidden (view full) --- 654g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp, 655 const struct g_eli_metadata *md, const u_char *mkey, int nkey) 656{ 657 struct g_eli_softc *sc; 658 struct g_eli_worker *wr; 659 struct g_geom *gp; 660 struct g_provider *pp; 661 struct g_consumer *cp; | 378int 379g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 380 struct g_eli_metadata *md) 381{ 382 struct g_geom *gp; 383 struct g_consumer *cp; 384 u_char *buf = NULL; 385 int error; --- 91 unchanged lines hidden (view full) --- 477g_eli_create(struct gctl_req *req, struct g_class *mp, struct g_provider *bpp, 478 const struct g_eli_metadata *md, const u_char *mkey, int nkey) 479{ 480 struct g_eli_softc *sc; 481 struct g_eli_worker *wr; 482 struct g_geom *gp; 483 struct g_provider *pp; 484 struct g_consumer *cp; |
662 struct cryptoini cri; | 485 struct cryptoini crie, cria; |
663 u_int i, threads; 664 int error; 665 666 G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX); 667 668 gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX); 669 gp->softc = NULL; /* for a moment */ 670 --- 12 unchanged lines hidden (view full) --- 683 if (md->md_flags & G_ELI_FLAG_WO_DETACH) 684 gp->access = g_eli_access; 685 else 686 gp->access = g_std_access; 687 gp->dumpconf = g_eli_dumpconf; 688 689 sc->sc_crypto = G_ELI_CRYPTO_SW; 690 sc->sc_flags = md->md_flags; | 486 u_int i, threads; 487 int error; 488 489 G_ELI_DEBUG(1, "Creating device %s%s.", bpp->name, G_ELI_SUFFIX); 490 491 gp = g_new_geomf(mp, "%s%s", bpp->name, G_ELI_SUFFIX); 492 gp->softc = NULL; /* for a moment */ 493 --- 12 unchanged lines hidden (view full) --- 506 if (md->md_flags & G_ELI_FLAG_WO_DETACH) 507 gp->access = g_eli_access; 508 else 509 gp->access = g_std_access; 510 gp->dumpconf = g_eli_dumpconf; 511 512 sc->sc_crypto = G_ELI_CRYPTO_SW; 513 sc->sc_flags = md->md_flags; |
691 sc->sc_algo = md->md_algo; | 514 sc->sc_ealgo = md->md_ealgo; |
692 sc->sc_nkey = nkey; 693 /* 694 * Remember the keys in our softc structure. 695 */ | 515 sc->sc_nkey = nkey; 516 /* 517 * Remember the keys in our softc structure. 518 */ |
696 bcopy(mkey, sc->sc_ivkey, sizeof(sc->sc_ivkey)); 697 mkey += sizeof(sc->sc_ivkey); 698 bcopy(mkey, sc->sc_datakey, sizeof(sc->sc_datakey)); 699 sc->sc_keylen = md->md_keylen; | 519 g_eli_mkey_propagate(sc, mkey); 520 sc->sc_ekeylen = md->md_keylen; |
700 | 521 |
522 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 523 sc->sc_akeylen = sizeof(sc->sc_akey) * 8; 524 sc->sc_aalgo = md->md_aalgo; 525 sc->sc_alen = g_eli_hashlen(sc->sc_aalgo); 526 527 sc->sc_data_per_sector = bpp->sectorsize - sc->sc_alen; 528 /* 529 * Some hash functions (like SHA1 and RIPEMD160) generates hash 530 * which length is not multiple of 128 bits, but we want data 531 * length to be multiple of 128, so we can encrypt without 532 * padding. The line below rounds down data length to multiple 533 * of 128 bits. 534 */ 535 sc->sc_data_per_sector -= sc->sc_data_per_sector % 16; 536 537 sc->sc_bytes_per_sector = 538 (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1; 539 sc->sc_bytes_per_sector *= bpp->sectorsize; 540 /* 541 * Precalculate SHA256 for HMAC key generation. 542 * This is expensive operation and we can do it only once now or 543 * for every access to sector, so now will be much better. 544 */ 545 SHA256_Init(&sc->sc_akeyctx); 546 SHA256_Update(&sc->sc_akeyctx, sc->sc_akey, 547 sizeof(sc->sc_akey)); 548 } 549 |
|
701 /* 702 * Precalculate SHA256 for IV generation. 703 * This is expensive operation and we can do it only once now or for 704 * every access to sector, so now will be much better. 705 */ 706 SHA256_Init(&sc->sc_ivctx); 707 SHA256_Update(&sc->sc_ivctx, sc->sc_ivkey, sizeof(sc->sc_ivkey)); 708 --- 30 unchanged lines hidden (view full) --- 739 G_ELI_DEBUG(1, "Cannot access %s (error=%d).", 740 bpp->name, error); 741 } 742 goto failed; 743 } 744 745 LIST_INIT(&sc->sc_workers); 746 | 550 /* 551 * Precalculate SHA256 for IV generation. 552 * This is expensive operation and we can do it only once now or for 553 * every access to sector, so now will be much better. 554 */ 555 SHA256_Init(&sc->sc_ivctx); 556 SHA256_Update(&sc->sc_ivctx, sc->sc_ivkey, sizeof(sc->sc_ivkey)); 557 --- 30 unchanged lines hidden (view full) --- 588 G_ELI_DEBUG(1, "Cannot access %s (error=%d).", 589 bpp->name, error); 590 } 591 goto failed; 592 } 593 594 LIST_INIT(&sc->sc_workers); 595 |
747 bzero(&cri, sizeof(cri)); 748 cri.cri_alg = sc->sc_algo; 749 cri.cri_klen = sc->sc_keylen; 750 cri.cri_key = sc->sc_datakey; | 596 bzero(&crie, sizeof(crie)); 597 crie.cri_alg = sc->sc_ealgo; 598 crie.cri_klen = sc->sc_ekeylen; 599 crie.cri_key = sc->sc_ekey; 600 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 601 bzero(&cria, sizeof(cria)); 602 cria.cri_alg = sc->sc_aalgo; 603 cria.cri_klen = sc->sc_akeylen; 604 cria.cri_key = sc->sc_akey; 605 crie.cri_next = &cria; 606 } |
751 752 threads = g_eli_threads; 753 if (threads == 0) 754 threads = mp_ncpus; 755 else if (threads > mp_ncpus) { 756 /* There is really no need for too many worker threads. */ 757 threads = mp_ncpus; 758 G_ELI_DEBUG(0, "Reducing number of threads to %u.", threads); 759 } 760 for (i = 0; i < threads; i++) { 761 wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO); 762 wr->w_softc = sc; 763 wr->w_number = i; 764 765 /* 766 * If this is the first pass, try to get hardware support. 767 * Use software cryptography, if we cannot get it. 768 */ 769 if (i == 0) { | 607 608 threads = g_eli_threads; 609 if (threads == 0) 610 threads = mp_ncpus; 611 else if (threads > mp_ncpus) { 612 /* There is really no need for too many worker threads. */ 613 threads = mp_ncpus; 614 G_ELI_DEBUG(0, "Reducing number of threads to %u.", threads); 615 } 616 for (i = 0; i < threads; i++) { 617 wr = malloc(sizeof(*wr), M_ELI, M_WAITOK | M_ZERO); 618 wr->w_softc = sc; 619 wr->w_number = i; 620 621 /* 622 * If this is the first pass, try to get hardware support. 623 * Use software cryptography, if we cannot get it. 624 */ 625 if (i == 0) { |
770 error = crypto_newsession(&wr->w_sid, &cri, 1); | 626 error = crypto_newsession(&wr->w_sid, &crie, 1); |
771 if (error == 0) 772 sc->sc_crypto = G_ELI_CRYPTO_HW; 773 } 774 if (sc->sc_crypto == G_ELI_CRYPTO_SW) | 627 if (error == 0) 628 sc->sc_crypto = G_ELI_CRYPTO_HW; 629 } 630 if (sc->sc_crypto == G_ELI_CRYPTO_SW) |
775 error = crypto_newsession(&wr->w_sid, &cri, 0); | 631 error = crypto_newsession(&wr->w_sid, &crie, 0); |
776 if (error != 0) { 777 free(wr, M_ELI); 778 if (req != NULL) { 779 gctl_error(req, "Cannot set up crypto session " 780 "for %s (error=%d).", bpp->name, error); 781 } else { 782 G_ELI_DEBUG(1, "Cannot set up crypto session " 783 "for %s (error=%d).", bpp->name, error); --- 22 unchanged lines hidden (view full) --- 806 } 807 808 /* 809 * Create decrypted provider. 810 */ 811 pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX); 812 pp->sectorsize = md->md_sectorsize; 813 pp->mediasize = bpp->mediasize; | 632 if (error != 0) { 633 free(wr, M_ELI); 634 if (req != NULL) { 635 gctl_error(req, "Cannot set up crypto session " 636 "for %s (error=%d).", bpp->name, error); 637 } else { 638 G_ELI_DEBUG(1, "Cannot set up crypto session " 639 "for %s (error=%d).", bpp->name, error); --- 22 unchanged lines hidden (view full) --- 662 } 663 664 /* 665 * Create decrypted provider. 666 */ 667 pp = g_new_providerf(gp, "%s%s", bpp->name, G_ELI_SUFFIX); 668 pp->sectorsize = md->md_sectorsize; 669 pp->mediasize = bpp->mediasize; |
814 if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) | 670 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) |
815 pp->mediasize -= bpp->sectorsize; | 671 pp->mediasize -= bpp->sectorsize; |
816 pp->mediasize -= (pp->mediasize % pp->sectorsize); | 672 if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) 673 pp->mediasize -= (pp->mediasize % pp->sectorsize); 674 else { 675 pp->mediasize /= sc->sc_bytes_per_sector; 676 pp->mediasize *= pp->sectorsize; 677 } 678 |
817 g_error_provider(pp, 0); 818 819 G_ELI_DEBUG(0, "Device %s created.", pp->name); | 679 g_error_provider(pp, 0); 680 681 G_ELI_DEBUG(0, "Device %s created.", pp->name); |
820 G_ELI_DEBUG(0, " Cipher: %s", g_eli_algo2str(sc->sc_algo)); 821 G_ELI_DEBUG(0, "Key length: %u", sc->sc_keylen); | 682 G_ELI_DEBUG(0, "Encryption: %s %u", g_eli_algo2str(sc->sc_ealgo), 683 sc->sc_ekeylen); 684 if (sc->sc_flags & G_ELI_FLAG_AUTH) 685 G_ELI_DEBUG(0, " Integrity: %s", g_eli_algo2str(sc->sc_aalgo)); |
822 G_ELI_DEBUG(0, " Crypto: %s", 823 sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware"); 824 return (gp); 825failed: 826 mtx_lock(&sc->sc_queue_mtx); 827 sc->sc_flags |= G_ELI_FLAG_DESTROY; 828 wakeup(sc); 829 /* --- 161 unchanged lines hidden (view full) --- 991 if (md.md_version > G_ELI_VERSION) { 992 printf("geom_eli.ko module is too old to handle %s.\n", 993 pp->name); 994 return (NULL); 995 } 996 if (md.md_provsize != pp->mediasize) 997 return (NULL); 998 /* Should we attach it on boot? */ | 686 G_ELI_DEBUG(0, " Crypto: %s", 687 sc->sc_crypto == G_ELI_CRYPTO_SW ? "software" : "hardware"); 688 return (gp); 689failed: 690 mtx_lock(&sc->sc_queue_mtx); 691 sc->sc_flags |= G_ELI_FLAG_DESTROY; 692 wakeup(sc); 693 /* --- 161 unchanged lines hidden (view full) --- 855 if (md.md_version > G_ELI_VERSION) { 856 printf("geom_eli.ko module is too old to handle %s.\n", 857 pp->name); 858 return (NULL); 859 } 860 if (md.md_provsize != pp->mediasize) 861 return (NULL); 862 /* Should we attach it on boot? */ |
999 if ((md.md_flags & G_ELI_FLAG_BOOT) == 0) | 863 if (!(md.md_flags & G_ELI_FLAG_BOOT)) |
1000 return (NULL); 1001 if (md.md_keys == 0x00) { 1002 G_ELI_DEBUG(0, "No valid keys on %s.", pp->name); 1003 return (NULL); 1004 } 1005 if (md.md_iterations == -1) { 1006 /* If there is no passphrase, we try only once. */ 1007 tries = 1; --- 105 unchanged lines hidden (view full) --- 1113 return; /* Nothing here. */ 1114 sbuf_printf(sb, "%s<Flags>", indent); 1115 if (sc->sc_flags == 0) 1116 sbuf_printf(sb, "NONE"); 1117 else { 1118 int first = 1; 1119 1120#define ADD_FLAG(flag, name) do { \ | 864 return (NULL); 865 if (md.md_keys == 0x00) { 866 G_ELI_DEBUG(0, "No valid keys on %s.", pp->name); 867 return (NULL); 868 } 869 if (md.md_iterations == -1) { 870 /* If there is no passphrase, we try only once. */ 871 tries = 1; --- 105 unchanged lines hidden (view full) --- 977 return; /* Nothing here. */ 978 sbuf_printf(sb, "%s<Flags>", indent); 979 if (sc->sc_flags == 0) 980 sbuf_printf(sb, "NONE"); 981 else { 982 int first = 1; 983 984#define ADD_FLAG(flag, name) do { \ |
1121 if ((sc->sc_flags & (flag)) != 0) { \ | 985 if (sc->sc_flags & (flag)) { \ |
1122 if (!first) \ 1123 sbuf_printf(sb, ", "); \ 1124 else \ 1125 first = 0; \ 1126 sbuf_printf(sb, name); \ 1127 } \ 1128} while (0) 1129 ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME"); 1130 ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT"); 1131 ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH"); 1132 ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH"); | 986 if (!first) \ 987 sbuf_printf(sb, ", "); \ 988 else \ 989 first = 0; \ 990 sbuf_printf(sb, name); \ 991 } \ 992} while (0) 993 ADD_FLAG(G_ELI_FLAG_ONETIME, "ONETIME"); 994 ADD_FLAG(G_ELI_FLAG_BOOT, "BOOT"); 995 ADD_FLAG(G_ELI_FLAG_WO_DETACH, "W-DETACH"); 996 ADD_FLAG(G_ELI_FLAG_RW_DETACH, "RW-DETACH"); |
997 ADD_FLAG(G_ELI_FLAG_AUTH, "AUTH"); |
|
1133 ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN"); 1134 ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY"); 1135#undef ADD_FLAG 1136 } 1137 sbuf_printf(sb, "</Flags>\n"); 1138 | 998 ADD_FLAG(G_ELI_FLAG_WOPEN, "W-OPEN"); 999 ADD_FLAG(G_ELI_FLAG_DESTROY, "DESTROY"); 1000#undef ADD_FLAG 1001 } 1002 sbuf_printf(sb, "</Flags>\n"); 1003 |
1139 if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) { | 1004 if (!(sc->sc_flags & G_ELI_FLAG_ONETIME)) { |
1140 sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent, 1141 sc->sc_nkey); 1142 } 1143 sbuf_printf(sb, "%s<Crypto>", indent); 1144 switch (sc->sc_crypto) { 1145 case G_ELI_CRYPTO_HW: 1146 sbuf_printf(sb, "hardware"); 1147 break; 1148 case G_ELI_CRYPTO_SW: 1149 sbuf_printf(sb, "software"); 1150 break; 1151 default: 1152 sbuf_printf(sb, "UNKNOWN"); 1153 break; 1154 } 1155 sbuf_printf(sb, "</Crypto>\n"); | 1005 sbuf_printf(sb, "%s<UsedKey>%u</UsedKey>\n", indent, 1006 sc->sc_nkey); 1007 } 1008 sbuf_printf(sb, "%s<Crypto>", indent); 1009 switch (sc->sc_crypto) { 1010 case G_ELI_CRYPTO_HW: 1011 sbuf_printf(sb, "hardware"); 1012 break; 1013 case G_ELI_CRYPTO_SW: 1014 sbuf_printf(sb, "software"); 1015 break; 1016 default: 1017 sbuf_printf(sb, "UNKNOWN"); 1018 break; 1019 } 1020 sbuf_printf(sb, "</Crypto>\n"); |
1156 sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent, sc->sc_keylen); 1157 sbuf_printf(sb, "%s<Cipher>%s</Cipher>\n", indent, 1158 g_eli_algo2str(sc->sc_algo)); | 1021 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 1022 sbuf_printf(sb, 1023 "%s<AuthenticationAlgorithm>%s</AuthenticationAlgorithm>\n", 1024 indent, g_eli_algo2str(sc->sc_aalgo)); 1025 } 1026 sbuf_printf(sb, "%s<KeyLength>%u</KeyLength>\n", indent, 1027 sc->sc_ekeylen); 1028 sbuf_printf(sb, "%s<EncryptionAlgorithm>%s</EncryptionAlgorithm>\n", indent, 1029 g_eli_algo2str(sc->sc_ealgo)); |
1159} 1160 1161DECLARE_GEOM_CLASS(g_eli_class, g_eli); 1162MODULE_DEPEND(geom_eli, crypto, 1, 1, 1); | 1030} 1031 1032DECLARE_GEOM_CLASS(g_eli_class, g_eli); 1033MODULE_DEPEND(geom_eli, crypto, 1, 1, 1); |