FreeBSD/sys/geom/eli/g_eli_privacy.c
/*
 * This is the main function responsible for cryptography (ie. communication
 * with crypto(9) subsystem).
 *
 * BIO_READ:
 *	g_eli_start -> g_eli_crypto_read -> g_io_request -> g_eli_read_done -> G_ELI_CRYPTO_RUN -> g_eli_crypto_read_done -> g_io_deliver
 * BIO_WRITE:
 *	g_eli_start -> G_ELI_CRYPTO_RUN -> g_eli_crypto_write_done -> g_io_request -> g_eli_write_done -> g_io_deliver
 */
This is the main function responsible for cryptography  subsystem).


This is the main function responsible for cryptography subsystem ) .
void
g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp)
{
	struct g_eli_softc *sc;
	struct cryptop *crp;
	struct cryptodesc *crd;
	u_int i, nsec, secsize;
	off_t dstoff;
	size_t size;
	u_char *p, *data;
	int error;
	G_ELI_LOGREQ(3, bp, "%s", __func__);
	bp->bio_pflags = wr->w_number;
	sc = wr->w_softc;
	secsize = LIST_FIRST(&sc->sc_geom->provider)->sectorsize;
	nsec = bp->bio_length / secsize;
	size = sizeof(*crp) * nsec;
	size += sizeof(*crd) * nsec;
	if (bp->bio_cmd == BIO_WRITE)
		size += bp->bio_length;
	p = malloc(size, M_ELI, M_WAITOK);
	bp->bio_inbed = 0;
	bp->bio_children = nsec;
	bp->bio_driver2 = p;
	if (bp->bio_cmd == BIO_READ)
		data = bp->bio_data;
	else {
		data = p;
		p += bp->bio_length;
		bcopy(bp->bio_data, data, bp->bio_length);
	}
	for (i = 0, dstoff = bp->bio_offset; i < nsec; i++, dstoff += secsize) {
		crp = (struct cryptop *)p;	p += sizeof(*crp);
		crd = (struct cryptodesc *)p;	p += sizeof(*crd);
		crp->crp_sid = wr->w_sid;
		crp->crp_ilen = secsize;
		crp->crp_olen = secsize;
		crp->crp_opaque = (void *)bp;
		crp->crp_buf = (void *)data;
		data += secsize;
		if (bp->bio_cmd == BIO_WRITE)
			crp->crp_callback = g_eli_crypto_write_done;
		else 
			crp->crp_callback = g_eli_crypto_read_done;
		crp->crp_flags = CRYPTO_F_CBIFSYNC;
		if (g_eli_batch)
			crp->crp_flags |= CRYPTO_F_BATCH;
		crp->crp_desc = crd;
		crd->crd_skip = 0;
		crd->crd_len = secsize;
		crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
		if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0)
			crd->crd_flags |= CRD_F_KEY_EXPLICIT;
		if (bp->bio_cmd == BIO_WRITE)
			crd->crd_flags |= CRD_F_ENCRYPT;
		crd->crd_alg = sc->sc_ealgo;
		crd->crd_key = g_eli_key_hold(sc, dstoff, secsize);
		crd->crd_klen = sc->sc_ekeylen;
		if (sc->sc_ealgo == CRYPTO_AES_XTS)
			crd->crd_klen <<= 1;
		g_eli_crypto_ivgen(sc, dstoff, crd->crd_iv,
		    sizeof(crd->crd_iv));
		crd->crd_next = NULL;
		crp->crp_etype = 0;
		error = crypto_dispatch(crp);
		KASSERT(error == 0, ("crypto_dispatch() failed (error=%d)",
		    error));
	}
}
void g_eli_crypto_run ( struct g_eli_worker * wr , struct bio * bp ) { struct g_eli_softc * sc ; struct cryptop * crp ; struct cryptodesc * crd ; u_int i , nsec , secsize ; off_t dstoff ; size_t size ; u_char * p , * data ; int error ; G_ELI_LOGREQ ( 3 , bp , " " , __func__ ) ; bp -> bio_pflags = wr -> w_number ; sc = wr -> w_softc ; secsize = LIST_FIRST ( & sc -> sc_geom -> provider ) -> sectorsize ; nsec = bp -> bio_length / secsize ; size = sizeof ( * crp ) * nsec ; size += sizeof ( * crd ) * nsec ; if ( bp -> bio_cmd == BIO_WRITE ) size += bp -> bio_length ; p = malloc ( size , M_ELI , M_WAITOK ) ; bp -> bio_inbed = 0 ; bp -> bio_children = nsec ; bp -> bio_driver2 = p ; if ( bp -> bio_cmd == BIO_READ ) data = bp -> bio_data ; else { data = p ; p += bp -> bio_length ; bcopy ( bp -> bio_data , data , bp -> bio_length ) ; } for ( i = 0 , dstoff = bp -> bio_offset ; i < nsec ; i ++ , dstoff += secsize ) { crp = ( struct cryptop * ) p ; p += sizeof ( * crp ) ; crd = ( struct cryptodesc * ) p ; p += sizeof ( * crd ) ; crp -> crp_sid = wr -> w_sid ; crp -> crp_ilen = secsize ; crp -> crp_olen = secsize ; crp -> crp_opaque = ( void * ) bp ; crp -> crp_buf = ( void * ) data ; data += secsize ; if ( bp -> bio_cmd == BIO_WRITE ) crp -> crp_callback = g_eli_crypto_write_done ; else crp -> crp_callback = g_eli_crypto_read_done ; crp -> crp_flags = CRYPTO_F_CBIFSYNC ; if ( g_eli_batch ) crp -> crp_flags |= CRYPTO_F_BATCH ; crp -> crp_desc = crd ; crd -> crd_skip = 0 ; crd -> crd_len = secsize ; crd -> crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT ; if ( ( sc -> sc_flags & G_ELI_FLAG_SINGLE_KEY ) == 0 ) crd -> crd_flags |= CRD_F_KEY_EXPLICIT ; if ( bp -> bio_cmd == BIO_WRITE ) crd -> crd_flags |= CRD_F_ENCRYPT ; crd -> crd_alg = sc -> sc_ealgo ; crd -> crd_key = g_eli_key_hold ( sc , dstoff , secsize ) ; crd -> crd_klen = sc -> sc_ekeylen ; if ( sc -> sc_ealgo == CRYPTO_AES_XTS ) crd -> crd_klen <<= 1 ; g_eli_crypto_ivgen ( sc , dstoff , crd -> crd_iv , sizeof ( crd -> crd_iv ) ) ; crd -> crd_next = NULL ; crp -> crp_etype = 0 ; error = crypto_dispatch ( crp ) ; KASSERT ( error == 0 , ( " " , error ) ) ; } }
This is the main function responsible for cryptography (ie.
This is the main function responsible for cryptography ( ie .
/*
	 * Calculate how much memory do we need.
	 * We need separate crypto operation for every single sector.
	 * It is much faster to calculate total amount of needed memory here and
	 * do the allocation once instead of allocating memory in pieces (many,
	 * many pieces).
	 */ /*
	 * If we write the data we cannot destroy current bio_data content,
	 * so we need to allocate more memory for encrypted data.
	 */ /* if (bp->bio_cmd == BIO_READ) */