staticunsignedint num_prealloc_bounce_pg = 32;
module_param(num_prealloc_bounce_pg, uint, 0);
MODULE_PARM_DESC(num_prealloc_bounce_pg, "Number of preallocated bounce pages for the blk-crypto crypto API fallback");
staticunsignedint blk_crypto_num_keyslots = 100;
module_param_named(num_keyslots, blk_crypto_num_keyslots, uint, 0);
MODULE_PARM_DESC(num_keyslots, "Number of keyslots for the blk-crypto crypto API fallback");
staticunsignedint num_prealloc_fallback_crypt_ctxs = 128;
module_param(num_prealloc_fallback_crypt_ctxs, uint, 0);
MODULE_PARM_DESC(num_prealloc_crypt_fallback_ctxs, "Number of preallocated bio fallback crypto contexts for blk-crypto to use during crypto API fallback");
struct bio_fallback_crypt_ctx { struct bio_crypt_ctx crypt_ctx; /* * Copy of the bvec_iter when this bio was submitted. * We only want to en/decrypt the part of the bio as described by the * bvec_iter upon submission because bio might be split before being * resubmitted
*/ struct bvec_iter crypt_iter; union { struct { struct work_struct work; struct bio *bio;
}; struct { void *bi_private_orig;
bio_end_io_t *bi_end_io_orig;
};
};
};
/* * Allocating a crypto tfm during I/O can deadlock, so we have to preallocate * all of a mode's tfms when that mode starts being used. Since each mode may * need all the keyslots at some point, each mode needs its own tfm for each * keyslot; thus, a keyslot may contain tfms for multiple modes. However, to * match the behavior of real inline encryption hardware (which only supports a * single encryption context per keyslot), we only allow one tfm per keyslot to * be used at a time - the rest of the unused tfms have their keys cleared.
*/ static DEFINE_MUTEX(tfms_init_lock); staticbool tfms_inited[BLK_ENCRYPTION_MODE_MAX];
/* * This is the key we set when evicting a keyslot. This *should* be the all 0's * key, but AES-XTS rejects that key, so we use some random bytes instead.
*/ static u8 blank_key[BLK_CRYPTO_MAX_RAW_KEY_SIZE];
union blk_crypto_iv {
__le64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
u8 bytes[BLK_CRYPTO_MAX_IV_SIZE];
};
staticvoid blk_crypto_dun_to_iv(const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE], union blk_crypto_iv *iv)
{ int i;
for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++)
iv->dun[i] = cpu_to_le64(dun[i]);
}
/* * The crypto API fallback's encryption routine. * Allocate a bounce bio for encryption, encrypt the input bio using crypto API, * and replace *bio_ptr with the bounce bio. May split input bio if it's too * large. Returns true on success. Returns false and sets bio->bi_status on * error.
*/ staticbool blk_crypto_fallback_encrypt_bio(struct bio **bio_ptr)
{ struct bio *src_bio, *enc_bio; struct bio_crypt_ctx *bc; struct blk_crypto_keyslot *slot; int data_unit_size; struct skcipher_request *ciph_req = NULL;
DECLARE_CRYPTO_WAIT(wait);
u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; struct scatterlist src, dst; union blk_crypto_iv iv; unsignedint i, j; bool ret = false;
blk_status_t blk_st;
/* Split the bio if it's too big for single page bvec */ if (!blk_crypto_fallback_split_bio_if_needed(bio_ptr)) returnfalse;
src_bio = *bio_ptr;
bc = src_bio->bi_crypt_context;
data_unit_size = bc->bc_key->crypto_cfg.data_unit_size;
/* Allocate bounce bio for encryption */
enc_bio = blk_crypto_fallback_clone_bio(src_bio); if (!enc_bio) {
src_bio->bi_status = BLK_STS_RESOURCE; returnfalse;
}
/* * Get a blk-crypto-fallback keyslot that contains a crypto_skcipher for * this bio's algorithm and key.
*/
blk_st = blk_crypto_get_keyslot(blk_crypto_fallback_profile,
bc->bc_key, &slot); if (blk_st != BLK_STS_OK) {
src_bio->bi_status = blk_st; goto out_put_enc_bio;
}
/* and then allocate an skcipher_request for it */ if (!blk_crypto_fallback_alloc_cipher_req(slot, &ciph_req, &wait)) {
src_bio->bi_status = BLK_STS_RESOURCE; goto out_release_keyslot;
}
/* Encrypt each page in the bounce bio */ for (i = 0; i < enc_bio->bi_vcnt; i++) { struct bio_vec *enc_bvec = &enc_bio->bi_io_vec[i]; struct page *plaintext_page = enc_bvec->bv_page; struct page *ciphertext_page =
mempool_alloc(blk_crypto_bounce_page_pool, GFP_NOIO);
enc_bvec->bv_page = ciphertext_page;
if (!ciphertext_page) {
src_bio->bi_status = BLK_STS_RESOURCE; goto out_free_bounce_pages;
}
out_free_bounce_pages: while (i > 0)
mempool_free(enc_bio->bi_io_vec[--i].bv_page,
blk_crypto_bounce_page_pool);
out_free_ciph_req:
skcipher_request_free(ciph_req);
out_release_keyslot:
blk_crypto_put_keyslot(slot);
out_put_enc_bio: if (enc_bio)
bio_uninit(enc_bio);
kfree(enc_bio); return ret;
}
/* * The crypto API fallback's main decryption routine. * Decrypts input bio in place, and calls bio_endio on the bio.
*/ staticvoid blk_crypto_fallback_decrypt_bio(struct work_struct *work)
{ struct bio_fallback_crypt_ctx *f_ctx =
container_of(work, struct bio_fallback_crypt_ctx, work); struct bio *bio = f_ctx->bio; struct bio_crypt_ctx *bc = &f_ctx->crypt_ctx; struct blk_crypto_keyslot *slot; struct skcipher_request *ciph_req = NULL;
DECLARE_CRYPTO_WAIT(wait);
u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; union blk_crypto_iv iv; struct scatterlist sg; struct bio_vec bv; struct bvec_iter iter; constint data_unit_size = bc->bc_key->crypto_cfg.data_unit_size; unsignedint i;
blk_status_t blk_st;
/* * Get a blk-crypto-fallback keyslot that contains a crypto_skcipher for * this bio's algorithm and key.
*/
blk_st = blk_crypto_get_keyslot(blk_crypto_fallback_profile,
bc->bc_key, &slot); if (blk_st != BLK_STS_OK) {
bio->bi_status = blk_st; goto out_no_keyslot;
}
/* and then allocate an skcipher_request for it */ if (!blk_crypto_fallback_alloc_cipher_req(slot, &ciph_req, &wait)) {
bio->bi_status = BLK_STS_RESOURCE; goto out;
}
/* Decrypt each data unit in the segment */ for (i = 0; i < bv.bv_len; i += data_unit_size) {
blk_crypto_dun_to_iv(curr_dun, &iv); if (crypto_wait_req(crypto_skcipher_decrypt(ciph_req),
&wait)) {
bio->bi_status = BLK_STS_IOERR; goto out;
}
bio_crypt_dun_increment(curr_dun, 1);
sg.offset += data_unit_size;
}
}
/** * blk_crypto_fallback_decrypt_endio - queue bio for fallback decryption * * @bio: the bio to queue * * Restore bi_private and bi_end_io, and queue the bio for decryption into a * workqueue, since this function will be called from an atomic context.
*/ staticvoid blk_crypto_fallback_decrypt_endio(struct bio *bio)
{ struct bio_fallback_crypt_ctx *f_ctx = bio->bi_private;
/* If there was an IO error, don't queue for decrypt. */ if (bio->bi_status) {
mempool_free(f_ctx, bio_fallback_crypt_ctx_pool);
bio_endio(bio); return;
}
/** * blk_crypto_fallback_bio_prep - Prepare a bio to use fallback en/decryption * * @bio_ptr: pointer to the bio to prepare * * If bio is doing a WRITE operation, this splits the bio into two parts if it's * too big (see blk_crypto_fallback_split_bio_if_needed()). It then allocates a * bounce bio for the first part, encrypts it, and updates bio_ptr to point to * the bounce bio. * * For a READ operation, we mark the bio for decryption by using bi_private and * bi_end_io. * * In either case, this function will make the bio look like a regular bio (i.e. * as if no encryption context was ever specified) for the purposes of the rest * of the stack except for blk-integrity (blk-integrity and blk-crypto are not * currently supported together). * * Return: true on success. Sets bio->bi_status and returns false on error.
*/ bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr)
{ struct bio *bio = *bio_ptr; struct bio_crypt_ctx *bc = bio->bi_crypt_context; struct bio_fallback_crypt_ctx *f_ctx;
if (WARN_ON_ONCE(!tfms_inited[bc->bc_key->crypto_cfg.crypto_mode])) { /* User didn't call blk_crypto_start_using_key() first */
bio->bi_status = BLK_STS_IOERR; returnfalse;
}
if (!__blk_crypto_cfg_supported(blk_crypto_fallback_profile,
&bc->bc_key->crypto_cfg)) {
bio->bi_status = BLK_STS_NOTSUPP; returnfalse;
}
if (bio_data_dir(bio) == WRITE) return blk_crypto_fallback_encrypt_bio(bio_ptr);
/* * bio READ case: Set up a f_ctx in the bio's bi_private and set the * bi_end_io appropriately to trigger decryption when the bio is ended.
*/
f_ctx = mempool_alloc(bio_fallback_crypt_ctx_pool, GFP_NOIO);
f_ctx->crypt_ctx = *bc;
f_ctx->crypt_iter = bio->bi_iter;
f_ctx->bi_private_orig = bio->bi_private;
f_ctx->bi_end_io_orig = bio->bi_end_io;
bio->bi_private = (void *)f_ctx;
bio->bi_end_io = blk_crypto_fallback_decrypt_endio;
bio_crypt_free_ctx(bio);
returntrue;
}
int blk_crypto_fallback_evict_key(conststruct blk_crypto_key *key)
{ return __blk_crypto_evict_key(blk_crypto_fallback_profile, key);
}
staticbool blk_crypto_fallback_inited; staticint blk_crypto_fallback_init(void)
{ int i; int err;
if (blk_crypto_fallback_inited) return 0;
get_random_bytes(blank_key, sizeof(blank_key));
err = bioset_init(&crypto_bio_split, 64, 0, 0); if (err) goto out;
/* Dynamic allocation is needed because of lockdep_register_key(). */
blk_crypto_fallback_profile =
kzalloc(sizeof(*blk_crypto_fallback_profile), GFP_KERNEL); if (!blk_crypto_fallback_profile) {
err = -ENOMEM; goto fail_free_bioset;
}
/* All blk-crypto modes have a crypto API fallback. */ for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++)
blk_crypto_fallback_profile->modes_supported[i] = 0xFFFFFFFF;
blk_crypto_fallback_profile->modes_supported[BLK_ENCRYPTION_MODE_INVALID] = 0;
/* * Prepare blk-crypto-fallback for the specified crypto mode. * Returns -ENOPKG if the needed crypto API support is missing.
*/ int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num)
{ constchar *cipher_str = blk_crypto_modes[mode_num].cipher_str; struct blk_crypto_fallback_keyslot *slotp; unsignedint i; int err = 0;
/* * Fast path * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num] * for each i are visible before we try to access them.
*/ if (likely(smp_load_acquire(&tfms_inited[mode_num]))) return 0;
mutex_lock(&tfms_init_lock); if (tfms_inited[mode_num]) goto out;
err = blk_crypto_fallback_init(); if (err) goto out;
for (i = 0; i < blk_crypto_num_keyslots; i++) {
slotp = &blk_crypto_keyslots[i];
slotp->tfms[mode_num] = crypto_alloc_skcipher(cipher_str, 0, 0); if (IS_ERR(slotp->tfms[mode_num])) {
err = PTR_ERR(slotp->tfms[mode_num]); if (err == -ENOENT) {
pr_warn_once("Missing crypto API support for \"%s\"\n",
cipher_str);
err = -ENOPKG;
}
slotp->tfms[mode_num] = NULL; goto out_free_tfms;
}
/* * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num] * for each i are visible before we set tfms_inited[mode_num].
*/
smp_store_release(&tfms_inited[mode_num], true); goto out;
out_free_tfms: for (i = 0; i < blk_crypto_num_keyslots; i++) {
slotp = &blk_crypto_keyslots[i];
crypto_free_skcipher(slotp->tfms[mode_num]);
slotp->tfms[mode_num] = NULL;
}
out:
mutex_unlock(&tfms_init_lock); return err;
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.