Lines Matching full:cc

72 	struct crypt_config *cc;  member
100 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
102 void (*dtr)(struct crypt_config *cc);
103 int (*init)(struct crypt_config *cc);
104 int (*wipe)(struct crypt_config *cc);
105 int (*generator)(struct crypt_config *cc, u8 *iv,
107 int (*post)(struct crypt_config *cc, u8 *iv,
242 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
245 static bool crypt_integrity_aead(struct crypt_config *cc);
250 static struct crypto_skcipher *any_tfm(struct crypt_config *cc) in any_tfm() argument
252 return cc->cipher_tfm.tfms[0]; in any_tfm()
255 static struct crypto_aead *any_tfm_aead(struct crypt_config *cc) in any_tfm_aead() argument
257 return cc->cipher_tfm.tfms_aead[0]; in any_tfm_aead()
316 static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain_gen() argument
319 memset(iv, 0, cc->iv_size); in crypt_iv_plain_gen()
325 static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64_gen() argument
328 memset(iv, 0, cc->iv_size); in crypt_iv_plain64_gen()
334 static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64be_gen() argument
337 memset(iv, 0, cc->iv_size); in crypt_iv_plain64be_gen()
339 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector); in crypt_iv_plain64be_gen()
344 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_essiv_gen() argument
351 memset(iv, 0, cc->iv_size); in crypt_iv_essiv_gen()
357 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_benbi_ctr() argument
363 if (crypt_integrity_aead(cc)) in crypt_iv_benbi_ctr()
364 bs = crypto_aead_blocksize(any_tfm_aead(cc)); in crypt_iv_benbi_ctr()
366 bs = crypto_skcipher_blocksize(any_tfm(cc)); in crypt_iv_benbi_ctr()
383 cc->iv_gen_private.benbi.shift = 9 - log; in crypt_iv_benbi_ctr()
388 static void crypt_iv_benbi_dtr(struct crypt_config *cc) in crypt_iv_benbi_dtr() argument
392 static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_benbi_gen() argument
397 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ in crypt_iv_benbi_gen()
399 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); in crypt_iv_benbi_gen()
400 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); in crypt_iv_benbi_gen()
405 static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_null_gen() argument
408 memset(iv, 0, cc->iv_size); in crypt_iv_null_gen()
413 static void crypt_iv_lmk_dtr(struct crypt_config *cc) in crypt_iv_lmk_dtr() argument
415 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_dtr()
425 static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_lmk_ctr() argument
428 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_ctr()
430 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_lmk_ctr()
443 if (cc->key_parts == cc->tfms_count) { in crypt_iv_lmk_ctr()
450 crypt_iv_lmk_dtr(cc); in crypt_iv_lmk_ctr()
458 static int crypt_iv_lmk_init(struct crypt_config *cc) in crypt_iv_lmk_init() argument
460 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_init()
461 int subkey_size = cc->key_size / cc->key_parts; in crypt_iv_lmk_init()
465 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), in crypt_iv_lmk_init()
471 static int crypt_iv_lmk_wipe(struct crypt_config *cc) in crypt_iv_lmk_wipe() argument
473 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_wipe()
481 static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_one() argument
485 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_one()
524 memcpy(iv, &md5state.hash, cc->iv_size); in crypt_iv_lmk_one()
529 static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_gen() argument
537 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_lmk_gen()
539 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset); in crypt_iv_lmk_gen()
542 memset(iv, 0, cc->iv_size); in crypt_iv_lmk_gen()
547 static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_post() argument
557 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_lmk_post()
559 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset); in crypt_iv_lmk_post()
563 crypto_xor(dst + sg->offset, iv, cc->iv_size); in crypt_iv_lmk_post()
569 static void crypt_iv_tcw_dtr(struct crypt_config *cc) in crypt_iv_tcw_dtr() argument
571 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_dtr()
583 static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_tcw_ctr() argument
586 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_ctr()
588 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_tcw_ctr()
593 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { in crypt_iv_tcw_ctr()
605 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); in crypt_iv_tcw_ctr()
608 crypt_iv_tcw_dtr(cc); in crypt_iv_tcw_ctr()
616 static int crypt_iv_tcw_init(struct crypt_config *cc) in crypt_iv_tcw_init() argument
618 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_init()
619 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; in crypt_iv_tcw_init()
621 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); in crypt_iv_tcw_init()
622 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], in crypt_iv_tcw_init()
628 static int crypt_iv_tcw_wipe(struct crypt_config *cc) in crypt_iv_tcw_wipe() argument
630 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_wipe()
632 memset(tcw->iv_seed, 0, cc->iv_size); in crypt_iv_tcw_wipe()
638 static int crypt_iv_tcw_whitening(struct crypt_config *cc, in crypt_iv_tcw_whitening() argument
642 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_whitening()
676 static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_gen() argument
680 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_gen()
687 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_tcw_gen()
689 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset); in crypt_iv_tcw_gen()
695 if (cc->iv_size > 8) in crypt_iv_tcw_gen()
697 cc->iv_size - 8); in crypt_iv_tcw_gen()
702 static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_post() argument
713 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_tcw_post()
715 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset); in crypt_iv_tcw_post()
721 static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_random_gen() argument
725 get_random_bytes(iv, cc->iv_size); in crypt_iv_random_gen()
729 static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_eboiv_ctr() argument
732 if (crypt_integrity_aead(cc)) { in crypt_iv_eboiv_ctr()
737 if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) { in crypt_iv_eboiv_ctr()
745 static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_eboiv_gen() argument
748 struct crypto_skcipher *tfm = any_tfm(cc); in crypt_iv_eboiv_gen()
759 req = kmalloc(reqsize + cc->iv_size, GFP_NOIO); in crypt_iv_eboiv_gen()
766 memset(buf, 0, cc->iv_size); in crypt_iv_eboiv_gen()
767 *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size); in crypt_iv_eboiv_gen()
769 sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size); in crypt_iv_eboiv_gen()
770 sg_init_one(&dst, iv, cc->iv_size); in crypt_iv_eboiv_gen()
771 skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf); in crypt_iv_eboiv_gen()
779 static void crypt_iv_elephant_dtr(struct crypt_config *cc) in crypt_iv_elephant_dtr() argument
781 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_dtr()
787 static int crypt_iv_elephant_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_elephant_ctr() argument
790 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_ctr()
801 r = crypt_iv_eboiv_ctr(cc, ti, NULL); in crypt_iv_elephant_ctr()
803 crypt_iv_elephant_dtr(cc); in crypt_iv_elephant_ctr()
951 static int crypt_iv_elephant(struct crypt_config *cc, struct dm_crypt_request *dmreq) in crypt_iv_elephant() argument
953 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant()
969 *(__le64 *)es = cpu_to_le64(dmreq->iv_sector * cc->sector_size); in crypt_iv_elephant()
987 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_elephant()
993 sg2 = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_elephant()
995 memcpy(data_offset, data2 + sg2->offset, cc->sector_size); in crypt_iv_elephant()
1000 diffuser_disk_to_cpu((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1001 diffuser_b_decrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1002 diffuser_a_decrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1003 diffuser_cpu_to_disk((__le32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1006 for (i = 0; i < (cc->sector_size / 32); i++) in crypt_iv_elephant()
1010 diffuser_disk_to_cpu((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1011 diffuser_a_encrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1012 diffuser_b_encrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1013 diffuser_cpu_to_disk((__le32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1024 static int crypt_iv_elephant_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_elephant_gen() argument
1030 r = crypt_iv_elephant(cc, dmreq); in crypt_iv_elephant_gen()
1035 return crypt_iv_eboiv_gen(cc, iv, dmreq); in crypt_iv_elephant_gen()
1038 static int crypt_iv_elephant_post(struct crypt_config *cc, u8 *iv, in crypt_iv_elephant_post() argument
1042 return crypt_iv_elephant(cc, dmreq); in crypt_iv_elephant_post()
1047 static int crypt_iv_elephant_init(struct crypt_config *cc) in crypt_iv_elephant_init() argument
1049 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_init()
1050 int key_offset = cc->key_size - cc->key_extra_size; in crypt_iv_elephant_init()
1052 return crypto_skcipher_setkey(elephant->tfm, &cc->key[key_offset], cc->key_extra_size); in crypt_iv_elephant_init()
1055 static int crypt_iv_elephant_wipe(struct crypt_config *cc) in crypt_iv_elephant_wipe() argument
1057 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_wipe()
1060 memset(key, 0, cc->key_extra_size); in crypt_iv_elephant_wipe()
1061 return crypto_skcipher_setkey(elephant->tfm, key, cc->key_extra_size); in crypt_iv_elephant_wipe()
1129 static bool crypt_integrity_aead(struct crypt_config *cc) in crypt_integrity_aead() argument
1131 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_integrity_aead()
1134 static bool crypt_integrity_hmac(struct crypt_config *cc) in crypt_integrity_hmac() argument
1136 return crypt_integrity_aead(cc) && cc->key_mac_size; in crypt_integrity_hmac()
1140 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, in crypt_get_sg_data() argument
1143 if (unlikely(crypt_integrity_aead(cc))) in crypt_get_sg_data()
1155 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size) in dm_crypt_integrity_io_alloc()
1162 tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift); in dm_crypt_integrity_io_alloc()
1164 bip->bip_iter.bi_sector = io->cc->start + io->sector; in dm_crypt_integrity_io_alloc()
1174 static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti) in crypt_integrity_ctr() argument
1177 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk); in crypt_integrity_ctr()
1186 if (bi->tag_size != cc->on_disk_tag_size || in crypt_integrity_ctr()
1187 bi->tuple_size != cc->on_disk_tag_size) { in crypt_integrity_ctr()
1191 if (1 << bi->interval_exp != cc->sector_size) { in crypt_integrity_ctr()
1196 if (crypt_integrity_aead(cc)) { in crypt_integrity_ctr()
1197 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size; in crypt_integrity_ctr()
1199 cc->integrity_tag_size, cc->integrity_iv_size); in crypt_integrity_ctr()
1201 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) { in crypt_integrity_ctr()
1205 } else if (cc->integrity_iv_size) in crypt_integrity_ctr()
1207 cc->integrity_iv_size); in crypt_integrity_ctr()
1209 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) { in crypt_integrity_ctr()
1221 static void crypt_convert_init(struct crypt_config *cc, in crypt_convert_init() argument
1232 ctx->cc_sector = sector + cc->iv_offset; in crypt_convert_init()
1236 static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, in dmreq_of_req() argument
1239 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); in dmreq_of_req()
1242 static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq) in req_of_dmreq() argument
1244 return (void *)((char *)dmreq - cc->dmreq_start); in req_of_dmreq()
1247 static u8 *iv_of_dmreq(struct crypt_config *cc, in iv_of_dmreq() argument
1250 if (crypt_integrity_aead(cc)) in iv_of_dmreq()
1252 crypto_aead_alignmask(any_tfm_aead(cc)) + 1); in iv_of_dmreq()
1255 crypto_skcipher_alignmask(any_tfm(cc)) + 1); in iv_of_dmreq()
1258 static u8 *org_iv_of_dmreq(struct crypt_config *cc, in org_iv_of_dmreq() argument
1261 return iv_of_dmreq(cc, dmreq) + cc->iv_size; in org_iv_of_dmreq()
1264 static __le64 *org_sector_of_dmreq(struct crypt_config *cc, in org_sector_of_dmreq() argument
1267 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size; in org_sector_of_dmreq()
1272 static unsigned int *org_tag_of_dmreq(struct crypt_config *cc, in org_tag_of_dmreq() argument
1275 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + in org_tag_of_dmreq()
1276 cc->iv_size + sizeof(uint64_t); in org_tag_of_dmreq()
1281 static void *tag_from_dmreq(struct crypt_config *cc, in tag_from_dmreq() argument
1287 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) * in tag_from_dmreq()
1288 cc->on_disk_tag_size]; in tag_from_dmreq()
1291 static void *iv_tag_from_dmreq(struct crypt_config *cc, in iv_tag_from_dmreq() argument
1294 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size; in iv_tag_from_dmreq()
1297 static int crypt_convert_block_aead(struct crypt_config *cc, in crypt_convert_block_aead() argument
1309 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size); in crypt_convert_block_aead()
1312 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_aead()
1315 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_aead()
1317 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_aead()
1318 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_aead()
1321 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_aead()
1323 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1324 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_aead()
1326 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1327 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1328 tag = tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1329 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1338 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1339 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_aead()
1340 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1344 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1345 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_aead()
1346 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1348 if (cc->iv_gen_ops) { in crypt_convert_block_aead()
1350 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_aead()
1351 memcpy(org_iv, tag_iv, cc->iv_size); in crypt_convert_block_aead()
1353 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_aead()
1357 if (cc->integrity_iv_size) in crypt_convert_block_aead()
1358 memcpy(tag_iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1361 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1364 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size); in crypt_convert_block_aead()
1367 cc->sector_size, iv); in crypt_convert_block_aead()
1369 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size) in crypt_convert_block_aead()
1370 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0, in crypt_convert_block_aead()
1371 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size)); in crypt_convert_block_aead()
1374 cc->sector_size + cc->integrity_tag_size, iv); in crypt_convert_block_aead()
1387 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_aead()
1388 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_aead()
1390 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_aead()
1391 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_aead()
1396 static int crypt_convert_block_skcipher(struct crypt_config *cc, in crypt_convert_block_skcipher() argument
1410 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_skcipher()
1413 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_skcipher()
1415 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1416 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_skcipher()
1419 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_skcipher()
1421 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1422 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1423 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1425 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1426 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_skcipher()
1433 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_skcipher()
1436 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_skcipher()
1438 if (cc->iv_gen_ops) { in crypt_convert_block_skcipher()
1440 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_skcipher()
1441 memcpy(org_iv, tag_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1443 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1447 if (test_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1450 if (cc->integrity_iv_size) in crypt_convert_block_skcipher()
1451 memcpy(tag_iv, org_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1454 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_skcipher()
1457 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv); in crypt_convert_block_skcipher()
1464 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_skcipher()
1465 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1467 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_skcipher()
1468 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_skcipher()
1475 static int crypt_alloc_req_skcipher(struct crypt_config *cc, in crypt_alloc_req_skcipher() argument
1478 unsigned int key_index = ctx->cc_sector & (cc->tfms_count - 1); in crypt_alloc_req_skcipher()
1481 ctx->r.req = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); in crypt_alloc_req_skcipher()
1486 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]); in crypt_alloc_req_skcipher()
1494 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req)); in crypt_alloc_req_skcipher()
1499 static int crypt_alloc_req_aead(struct crypt_config *cc, in crypt_alloc_req_aead() argument
1503 ctx->r.req_aead = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); in crypt_alloc_req_aead()
1508 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_req_aead()
1516 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead)); in crypt_alloc_req_aead()
1521 static int crypt_alloc_req(struct crypt_config *cc, in crypt_alloc_req() argument
1524 if (crypt_integrity_aead(cc)) in crypt_alloc_req()
1525 return crypt_alloc_req_aead(cc, ctx); in crypt_alloc_req()
1527 return crypt_alloc_req_skcipher(cc, ctx); in crypt_alloc_req()
1530 static void crypt_free_req_skcipher(struct crypt_config *cc, in crypt_free_req_skcipher() argument
1533 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_skcipher()
1536 mempool_free(req, &cc->req_pool); in crypt_free_req_skcipher()
1539 static void crypt_free_req_aead(struct crypt_config *cc, in crypt_free_req_aead() argument
1542 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_aead()
1545 mempool_free(req, &cc->req_pool); in crypt_free_req_aead()
1548 static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio) in crypt_free_req() argument
1550 if (crypt_integrity_aead(cc)) in crypt_free_req()
1551 crypt_free_req_aead(cc, req, base_bio); in crypt_free_req()
1553 crypt_free_req_skcipher(cc, req, base_bio); in crypt_free_req()
1559 static blk_status_t crypt_convert(struct crypt_config *cc, in crypt_convert() argument
1563 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT; in crypt_convert()
1576 r = crypt_alloc_req(cc, ctx); in crypt_convert()
1584 if (crypt_integrity_aead(cc)) in crypt_convert()
1585 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset); in crypt_convert()
1587 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset); in crypt_convert()
1653 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1677 struct crypt_config *cc = io->cc; in crypt_alloc_buffer() local
1686 mutex_lock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1688 clone = bio_alloc_bioset(cc->dev->bdev, nr_iovecs, io->base_bio->bi_opf, in crypt_alloc_buffer()
1689 GFP_NOIO, &cc->bs); in crypt_alloc_buffer()
1710 pages = mempool_alloc(&cc->page_pool, gfp_mask); in crypt_alloc_buffer()
1712 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1727 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1733 mutex_unlock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1738 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) in crypt_free_buffer_pages() argument
1747 mempool_free(&fi.folio->page, &cc->page_pool); in crypt_free_buffer_pages()
1752 static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, in crypt_io_init() argument
1755 io->cc = cc; in crypt_io_init()
1784 struct crypt_config *cc = io->cc; in crypt_dec_pending() local
1792 crypt_free_req(cc, io->ctx.r.req, base_bio); in crypt_dec_pending()
1795 mempool_free(io->integrity_metadata, &io->cc->tag_pool); in crypt_dec_pending()
1811 queue_work(cc->io_queue, &io->work); in crypt_dec_pending()
1838 struct crypt_config *cc = io->cc; in crypt_endio() local
1846 crypt_free_buffer_pages(cc, clone); in crypt_endio()
1866 struct crypt_config *cc = io->cc; in kcryptd_io_read() local
1875 clone = bio_alloc_clone(cc->dev->bdev, io->base_bio, gfp, &cc->bs); in kcryptd_io_read()
1883 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_io_read()
1907 struct crypt_config *cc = io->cc; in kcryptd_queue_read() local
1910 queue_work(cc->io_queue, &io->work); in kcryptd_queue_read()
1924 struct crypt_config *cc = data; in dmcrypt_write() local
1931 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1934 if (!RB_EMPTY_ROOT(&cc->write_tree)) in dmcrypt_write()
1939 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1949 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1953 write_tree = cc->write_tree; in dmcrypt_write()
1954 cc->write_tree = RB_ROOT; in dmcrypt_write()
1955 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1978 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_io_submit() local
1984 crypt_free_buffer_pages(cc, clone); in kcryptd_crypt_write_io_submit()
1993 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_crypt_write_io_submit()
1995 if ((likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) || in kcryptd_crypt_write_io_submit()
1996 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) { in kcryptd_crypt_write_io_submit()
2001 spin_lock_irqsave(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
2002 if (RB_EMPTY_ROOT(&cc->write_tree)) in kcryptd_crypt_write_io_submit()
2003 wake_up_process(cc->write_thread); in kcryptd_crypt_write_io_submit()
2004 rbp = &cc->write_tree.rb_node; in kcryptd_crypt_write_io_submit()
2015 rb_insert_color(&io->rb_node, &cc->write_tree); in kcryptd_crypt_write_io_submit()
2016 spin_unlock_irqrestore(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
2019 static bool kcryptd_crypt_write_inline(struct crypt_config *cc, in kcryptd_crypt_write_inline() argument
2023 if (!test_bit(DM_CRYPT_WRITE_INLINE, &cc->flags)) in kcryptd_crypt_write_inline()
2043 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_continue() local
2052 r = crypt_convert(cc, &io->ctx, true, false); in kcryptd_crypt_write_continue()
2056 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_crypt_write_continue()
2073 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_convert() local
2084 crypt_convert_init(cc, ctx, NULL, io->base_bio, sector); in kcryptd_crypt_write_convert()
2098 r = crypt_convert(cc, ctx, in kcryptd_crypt_write_convert()
2099 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_write_convert()
2107 queue_work(cc->crypt_queue, &io->work); in kcryptd_crypt_write_convert()
2113 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_crypt_write_convert()
2137 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_continue() local
2143 r = crypt_convert(cc, &io->ctx, true, false); in kcryptd_crypt_read_continue()
2155 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_convert() local
2160 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, in kcryptd_crypt_read_convert()
2163 r = crypt_convert(cc, &io->ctx, in kcryptd_crypt_read_convert()
2164 test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_read_convert()
2171 queue_work(cc->crypt_queue, &io->work); in kcryptd_crypt_read_convert()
2188 struct crypt_config *cc = io->cc; in kcryptd_async_done() local
2200 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) in kcryptd_async_done()
2201 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq); in kcryptd_async_done()
2204 sector_t s = le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)); in kcryptd_async_done()
2214 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); in kcryptd_async_done()
2228 if (kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_async_done()
2253 struct crypt_config *cc = io->cc; in kcryptd_queue_crypt() local
2255 if ((bio_data_dir(io->base_bio) == READ && test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) || in kcryptd_queue_crypt()
2256 (bio_data_dir(io->base_bio) == WRITE && test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags))) { in kcryptd_queue_crypt()
2274 queue_work(cc->crypt_queue, &io->work); in kcryptd_queue_crypt()
2277 static void crypt_free_tfms_aead(struct crypt_config *cc) in crypt_free_tfms_aead() argument
2279 if (!cc->cipher_tfm.tfms_aead) in crypt_free_tfms_aead()
2282 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_free_tfms_aead()
2283 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]); in crypt_free_tfms_aead()
2284 cc->cipher_tfm.tfms_aead[0] = NULL; in crypt_free_tfms_aead()
2287 kfree(cc->cipher_tfm.tfms_aead); in crypt_free_tfms_aead()
2288 cc->cipher_tfm.tfms_aead = NULL; in crypt_free_tfms_aead()
2291 static void crypt_free_tfms_skcipher(struct crypt_config *cc) in crypt_free_tfms_skcipher() argument
2295 if (!cc->cipher_tfm.tfms) in crypt_free_tfms_skcipher()
2298 for (i = 0; i < cc->tfms_count; i++) in crypt_free_tfms_skcipher()
2299 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_free_tfms_skcipher()
2300 crypto_free_skcipher(cc->cipher_tfm.tfms[i]); in crypt_free_tfms_skcipher()
2301 cc->cipher_tfm.tfms[i] = NULL; in crypt_free_tfms_skcipher()
2304 kfree(cc->cipher_tfm.tfms); in crypt_free_tfms_skcipher()
2305 cc->cipher_tfm.tfms = NULL; in crypt_free_tfms_skcipher()
2308 static void crypt_free_tfms(struct crypt_config *cc) in crypt_free_tfms() argument
2310 if (crypt_integrity_aead(cc)) in crypt_free_tfms()
2311 crypt_free_tfms_aead(cc); in crypt_free_tfms()
2313 crypt_free_tfms_skcipher(cc); in crypt_free_tfms()
2316 static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_skcipher() argument
2321 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count, in crypt_alloc_tfms_skcipher()
2324 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_skcipher()
2327 for (i = 0; i < cc->tfms_count; i++) { in crypt_alloc_tfms_skcipher()
2328 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, in crypt_alloc_tfms_skcipher()
2330 if (IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_alloc_tfms_skcipher()
2331 err = PTR_ERR(cc->cipher_tfm.tfms[i]); in crypt_alloc_tfms_skcipher()
2332 crypt_free_tfms(cc); in crypt_alloc_tfms_skcipher()
2343 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name); in crypt_alloc_tfms_skcipher()
2347 static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_aead() argument
2351 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL); in crypt_alloc_tfms_aead()
2352 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_aead()
2355 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, in crypt_alloc_tfms_aead()
2357 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_alloc_tfms_aead()
2358 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_tfms_aead()
2359 crypt_free_tfms(cc); in crypt_alloc_tfms_aead()
2364 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name); in crypt_alloc_tfms_aead()
2368 static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms() argument
2370 if (crypt_integrity_aead(cc)) in crypt_alloc_tfms()
2371 return crypt_alloc_tfms_aead(cc, ciphermode); in crypt_alloc_tfms()
2373 return crypt_alloc_tfms_skcipher(cc, ciphermode); in crypt_alloc_tfms()
2376 static unsigned int crypt_subkey_size(struct crypt_config *cc) in crypt_subkey_size() argument
2378 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); in crypt_subkey_size()
2381 static unsigned int crypt_authenckey_size(struct crypt_config *cc) in crypt_authenckey_size() argument
2383 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param)); in crypt_authenckey_size()
2389 * This funcion converts cc->key to this special format.
2408 static int crypt_setkey(struct crypt_config *cc) in crypt_setkey() argument
2414 subkey_size = crypt_subkey_size(cc); in crypt_setkey()
2416 if (crypt_integrity_hmac(cc)) { in crypt_setkey()
2417 if (subkey_size < cc->key_mac_size) in crypt_setkey()
2420 crypt_copy_authenckey(cc->authenc_key, cc->key, in crypt_setkey()
2421 subkey_size - cc->key_mac_size, in crypt_setkey()
2422 cc->key_mac_size); in crypt_setkey()
2425 for (i = 0; i < cc->tfms_count; i++) { in crypt_setkey()
2426 if (crypt_integrity_hmac(cc)) in crypt_setkey()
2427 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
2428 cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
2429 else if (crypt_integrity_aead(cc)) in crypt_setkey()
2430 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
2431 cc->key + (i * subkey_size), in crypt_setkey()
2434 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i], in crypt_setkey()
2435 cc->key + (i * subkey_size), in crypt_setkey()
2441 if (crypt_integrity_hmac(cc)) in crypt_setkey()
2442 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
2457 static int set_key_user(struct crypt_config *cc, struct key *key) in set_key_user() argument
2465 if (cc->key_size != ukp->datalen) in set_key_user()
2468 memcpy(cc->key, ukp->data, cc->key_size); in set_key_user()
2473 static int set_key_encrypted(struct crypt_config *cc, struct key *key) in set_key_encrypted() argument
2481 if (cc->key_size != ekp->decrypted_datalen) in set_key_encrypted()
2484 memcpy(cc->key, ekp->decrypted_data, cc->key_size); in set_key_encrypted()
2489 static int set_key_trusted(struct crypt_config *cc, struct key *key) in set_key_trusted() argument
2497 if (cc->key_size != tkp->key_len) in set_key_trusted()
2500 memcpy(cc->key, tkp->key, cc->key_size); in set_key_trusted()
2505 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2511 int (*set_key)(struct crypt_config *cc, struct key *key); in crypt_set_keyring_key()
2557 ret = set_key(cc, key); in crypt_set_keyring_key()
2569 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2571 ret = crypt_setkey(cc); in crypt_set_keyring_key()
2574 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2575 kfree_sensitive(cc->key_string); in crypt_set_keyring_key()
2576 cc->key_string = new_key_string; in crypt_set_keyring_key()
2608 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2620 static int crypt_set_key(struct crypt_config *cc, char *key) in crypt_set_key() argument
2626 if (!cc->key_size && strcmp(key, "-")) in crypt_set_key()
2631 r = crypt_set_keyring_key(cc, key + 1); in crypt_set_key()
2636 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2639 kfree_sensitive(cc->key_string); in crypt_set_key()
2640 cc->key_string = NULL; in crypt_set_key()
2643 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) in crypt_set_key()
2646 r = crypt_setkey(cc); in crypt_set_key()
2648 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2657 static int crypt_wipe_key(struct crypt_config *cc) in crypt_wipe_key() argument
2661 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_wipe_key()
2662 get_random_bytes(&cc->key, cc->key_size); in crypt_wipe_key()
2665 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { in crypt_wipe_key()
2666 r = cc->iv_gen_ops->wipe(cc); in crypt_wipe_key()
2671 kfree_sensitive(cc->key_string); in crypt_wipe_key()
2672 cc->key_string = NULL; in crypt_wipe_key()
2673 r = crypt_setkey(cc); in crypt_wipe_key()
2674 memset(&cc->key, 0, cc->key_size * sizeof(u8)); in crypt_wipe_key()
2694 struct crypt_config *cc = pool_data; in crypt_page_alloc() local
2702 if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) >= dm_crypt_pages_per_client) && in crypt_page_alloc()
2708 percpu_counter_add(&cc->n_allocated_pages, 1); in crypt_page_alloc()
2715 struct crypt_config *cc = pool_data; in crypt_page_free() local
2718 percpu_counter_sub(&cc->n_allocated_pages, 1); in crypt_page_free()
2723 struct crypt_config *cc = ti->private; in crypt_dtr() local
2727 if (!cc) in crypt_dtr()
2730 if (cc->write_thread) in crypt_dtr()
2731 kthread_stop(cc->write_thread); in crypt_dtr()
2733 if (cc->io_queue) in crypt_dtr()
2734 destroy_workqueue(cc->io_queue); in crypt_dtr()
2735 if (cc->crypt_queue) in crypt_dtr()
2736 destroy_workqueue(cc->crypt_queue); in crypt_dtr()
2738 crypt_free_tfms(cc); in crypt_dtr()
2740 bioset_exit(&cc->bs); in crypt_dtr()
2742 mempool_exit(&cc->page_pool); in crypt_dtr()
2743 mempool_exit(&cc->req_pool); in crypt_dtr()
2744 mempool_exit(&cc->tag_pool); in crypt_dtr()
2746 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0); in crypt_dtr()
2747 percpu_counter_destroy(&cc->n_allocated_pages); in crypt_dtr()
2749 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) in crypt_dtr()
2750 cc->iv_gen_ops->dtr(cc); in crypt_dtr()
2752 if (cc->dev) in crypt_dtr()
2753 dm_put_device(ti, cc->dev); in crypt_dtr()
2755 kfree_sensitive(cc->cipher_string); in crypt_dtr()
2756 kfree_sensitive(cc->key_string); in crypt_dtr()
2757 kfree_sensitive(cc->cipher_auth); in crypt_dtr()
2758 kfree_sensitive(cc->authenc_key); in crypt_dtr()
2760 mutex_destroy(&cc->bio_alloc_lock); in crypt_dtr()
2763 kfree_sensitive(cc); in crypt_dtr()
2776 struct crypt_config *cc = ti->private; in crypt_ctr_ivmode() local
2778 if (crypt_integrity_aead(cc)) in crypt_ctr_ivmode()
2779 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_ivmode()
2781 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_ivmode()
2783 if (cc->iv_size) in crypt_ctr_ivmode()
2785 cc->iv_size = max(cc->iv_size, in crypt_ctr_ivmode()
2794 cc->iv_gen_ops = NULL; in crypt_ctr_ivmode()
2796 cc->iv_gen_ops = &crypt_iv_plain_ops; in crypt_ctr_ivmode()
2798 cc->iv_gen_ops = &crypt_iv_plain64_ops; in crypt_ctr_ivmode()
2800 cc->iv_gen_ops = &crypt_iv_plain64be_ops; in crypt_ctr_ivmode()
2802 cc->iv_gen_ops = &crypt_iv_essiv_ops; in crypt_ctr_ivmode()
2804 cc->iv_gen_ops = &crypt_iv_benbi_ops; in crypt_ctr_ivmode()
2806 cc->iv_gen_ops = &crypt_iv_null_ops; in crypt_ctr_ivmode()
2808 cc->iv_gen_ops = &crypt_iv_eboiv_ops; in crypt_ctr_ivmode()
2810 cc->iv_gen_ops = &crypt_iv_elephant_ops; in crypt_ctr_ivmode()
2811 cc->key_parts = 2; in crypt_ctr_ivmode()
2812 cc->key_extra_size = cc->key_size / 2; in crypt_ctr_ivmode()
2813 if (cc->key_extra_size > ELEPHANT_MAX_KEY_SIZE) in crypt_ctr_ivmode()
2815 set_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags); in crypt_ctr_ivmode()
2817 cc->iv_gen_ops = &crypt_iv_lmk_ops; in crypt_ctr_ivmode()
2824 if (cc->key_size % cc->key_parts) { in crypt_ctr_ivmode()
2825 cc->key_parts++; in crypt_ctr_ivmode()
2826 cc->key_extra_size = cc->key_size / cc->key_parts; in crypt_ctr_ivmode()
2829 cc->iv_gen_ops = &crypt_iv_tcw_ops; in crypt_ctr_ivmode()
2830 cc->key_parts += 2; /* IV + whitening */ in crypt_ctr_ivmode()
2831 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; in crypt_ctr_ivmode()
2833 cc->iv_gen_ops = &crypt_iv_random_ops; in crypt_ctr_ivmode()
2835 cc->integrity_iv_size = cc->iv_size; in crypt_ctr_ivmode()
2849 static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) in crypt_ctr_auth_cipher() argument
2873 cc->key_mac_size = crypto_ahash_digestsize(mac); in crypt_ctr_auth_cipher()
2876 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL); in crypt_ctr_auth_cipher()
2877 if (!cc->authenc_key) in crypt_ctr_auth_cipher()
2886 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_new() local
2890 cc->tfms_count = 1; in crypt_ctr_cipher_new()
2914 if (crypt_integrity_aead(cc)) { in crypt_ctr_cipher_new()
2915 ret = crypt_ctr_auth_cipher(cc, cipher_api); in crypt_ctr_cipher_new()
2923 cc->tfms_count = 64; in crypt_ctr_cipher_new()
2939 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_new()
2942 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_new()
2948 if (crypt_integrity_aead(cc)) in crypt_ctr_cipher_new()
2949 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_cipher_new()
2951 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_cipher_new()
2959 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_old() local
2965 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) { in crypt_ctr_cipher_old()
2979 cc->tfms_count = 1; in crypt_ctr_cipher_old()
2980 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || in crypt_ctr_cipher_old()
2981 !is_power_of_2(cc->tfms_count)) { in crypt_ctr_cipher_old()
2985 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_old()
3027 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_old()
3043 struct crypt_config *cc = ti->private; in crypt_ctr_cipher() local
3047 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); in crypt_ctr_cipher()
3048 if (!cc->cipher_string) { in crypt_ctr_cipher()
3066 ret = crypt_set_key(cc, key); in crypt_ctr_cipher()
3073 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { in crypt_ctr_cipher()
3074 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); in crypt_ctr_cipher()
3082 if (cc->iv_gen_ops && cc->iv_gen_ops->init) { in crypt_ctr_cipher()
3083 ret = cc->iv_gen_ops->init(cc); in crypt_ctr_cipher()
3091 if (cc->key_string) in crypt_ctr_cipher()
3092 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_ctr_cipher()
3099 struct crypt_config *cc = ti->private; in crypt_ctr_optional() local
3128 set_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_ctr_optional()
3131 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_ctr_optional()
3133 set_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); in crypt_ctr_optional()
3135 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_ctr_optional()
3141 cc->on_disk_tag_size = val; in crypt_ctr_optional()
3144 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_ctr_optional()
3150 cc->cipher_auth = kstrdup(sval, GFP_KERNEL); in crypt_ctr_optional()
3151 if (!cc->cipher_auth) in crypt_ctr_optional()
3153 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { in crypt_ctr_optional()
3154 if (cc->sector_size < (1 << SECTOR_SHIFT) || in crypt_ctr_optional()
3155 cc->sector_size > 4096 || in crypt_ctr_optional()
3156 (cc->sector_size & (cc->sector_size - 1))) { in crypt_ctr_optional()
3160 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) { in crypt_ctr_optional()
3164 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT; in crypt_ctr_optional()
3166 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_ctr_optional()
3180 struct crypt_config *cc = ti->private; in crypt_report_zones() local
3182 return dm_report_zones(cc->dev->bdev, cc->start, in crypt_report_zones()
3183 cc->start + dm_target_offset(ti, args->next_sector), in crypt_report_zones()
3196 struct crypt_config *cc; in crypt_ctr() local
3216 cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL); in crypt_ctr()
3217 if (!cc) { in crypt_ctr()
3221 cc->key_size = key_size; in crypt_ctr()
3222 cc->sector_size = (1 << SECTOR_SHIFT); in crypt_ctr()
3223 cc->sector_shift = 0; in crypt_ctr()
3225 ti->private = cc; in crypt_ctr()
3232 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); in crypt_ctr()
3247 if (crypt_integrity_aead(cc)) { in crypt_ctr()
3248 cc->dmreq_start = sizeof(struct aead_request); in crypt_ctr()
3249 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc)); in crypt_ctr()
3250 align_mask = crypto_aead_alignmask(any_tfm_aead(cc)); in crypt_ctr()
3252 cc->dmreq_start = sizeof(struct skcipher_request); in crypt_ctr()
3253 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); in crypt_ctr()
3254 align_mask = crypto_skcipher_alignmask(any_tfm(cc)); in crypt_ctr()
3256 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); in crypt_ctr()
3260 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) in crypt_ctr()
3273 iv_size_padding + cc->iv_size + in crypt_ctr()
3274 cc->iv_size + in crypt_ctr()
3278 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size); in crypt_ctr()
3284 cc->per_bio_data_size = ti->per_io_data_size = in crypt_ctr()
3285 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size, in crypt_ctr()
3288 ret = mempool_init(&cc->page_pool, BIO_MAX_VECS, crypt_page_alloc, crypt_page_free, cc); in crypt_ctr()
3294 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS); in crypt_ctr()
3300 mutex_init(&cc->bio_alloc_lock); in crypt_ctr()
3304 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { in crypt_ctr()
3308 cc->iv_offset = tmpll; in crypt_ctr()
3310 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); in crypt_ctr()
3321 cc->start = tmpll; in crypt_ctr()
3323 if (bdev_is_zoned(cc->dev->bdev)) { in crypt_ctr()
3329 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_ctr()
3330 set_bit(DM_CRYPT_WRITE_INLINE, &cc->flags); in crypt_ctr()
3347 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) { in crypt_ctr()
3348 ret = crypt_integrity_ctr(cc, ti); in crypt_ctr()
3352 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size; in crypt_ctr()
3353 if (!cc->tag_pool_max_sectors) in crypt_ctr()
3354 cc->tag_pool_max_sectors = 1; in crypt_ctr()
3356 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS, in crypt_ctr()
3357 cc->tag_pool_max_sectors * cc->on_disk_tag_size); in crypt_ctr()
3363 cc->tag_pool_max_sectors <<= cc->sector_shift; in crypt_ctr()
3367 cc->io_queue = alloc_workqueue("kcryptd_io/%s", WQ_MEM_RECLAIM, 1, devname); in crypt_ctr()
3368 if (!cc->io_queue) { in crypt_ctr()
3373 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_ctr()
3374 cc->crypt_queue = alloc_workqueue("kcryptd/%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, in crypt_ctr()
3377 cc->crypt_queue = alloc_workqueue("kcryptd/%s", in crypt_ctr()
3380 if (!cc->crypt_queue) { in crypt_ctr()
3385 spin_lock_init(&cc->write_thread_lock); in crypt_ctr()
3386 cc->write_tree = RB_ROOT; in crypt_ctr()
3388 cc->write_thread = kthread_run(dmcrypt_write, cc, "dmcrypt_write/%s", devname); in crypt_ctr()
3389 if (IS_ERR(cc->write_thread)) { in crypt_ctr()
3390 ret = PTR_ERR(cc->write_thread); in crypt_ctr()
3391 cc->write_thread = NULL; in crypt_ctr()
3412 struct crypt_config *cc = ti->private; in crypt_map() local
3421 bio_set_dev(bio, cc->dev->bdev); in crypt_map()
3423 bio->bi_iter.bi_sector = cc->start + in crypt_map()
3432 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size)) in crypt_map()
3439 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) in crypt_map()
3442 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) in crypt_map()
3445 io = dm_per_bio_data(bio, cc->per_bio_data_size); in crypt_map()
3446 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); in crypt_map()
3448 if (cc->on_disk_tag_size) { in crypt_map()
3449 unsigned int tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift); in crypt_map()
3457 if (bio_sectors(bio) > cc->tag_pool_max_sectors) in crypt_map()
3458 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors); in crypt_map()
3459 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO); in crypt_map()
3464 if (crypt_integrity_aead(cc)) in crypt_map()
3486 struct crypt_config *cc = ti->private; in crypt_status() local
3496 DMEMIT("%s ", cc->cipher_string); in crypt_status()
3498 if (cc->key_size > 0) { in crypt_status()
3499 if (cc->key_string) in crypt_status()
3500 DMEMIT(":%u:%s", cc->key_size, cc->key_string); in crypt_status()
3502 for (i = 0; i < cc->key_size; i++) { in crypt_status()
3503 DMEMIT("%c%c", hex2asc(cc->key[i] >> 4), in crypt_status()
3504 hex2asc(cc->key[i] & 0xf)); in crypt_status()
3510 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, in crypt_status()
3511 cc->dev->name, (unsigned long long)cc->start); in crypt_status()
3514 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_status()
3515 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_status()
3516 num_feature_args += test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); in crypt_status()
3517 num_feature_args += test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_status()
3518 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT); in crypt_status()
3519 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_status()
3520 if (cc->on_disk_tag_size) in crypt_status()
3526 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_status()
3528 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) in crypt_status()
3530 if (test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) in crypt_status()
3532 if (test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) in crypt_status()
3534 if (cc->on_disk_tag_size) in crypt_status()
3535 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth); in crypt_status()
3536 if (cc->sector_size != (1 << SECTOR_SHIFT)) in crypt_status()
3537 DMEMIT(" sector_size:%d", cc->sector_size); in crypt_status()
3538 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_status()
3546 DMEMIT(",same_cpu_crypt=%c", test_bit(DM_CRYPT_SAME_CPU, &cc->flags) ? 'y' : 'n'); in crypt_status()
3547 DMEMIT(",submit_from_crypt_cpus=%c", test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags) ? in crypt_status()
3549 DMEMIT(",no_read_workqueue=%c", test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags) ? in crypt_status()
3551 DMEMIT(",no_write_workqueue=%c", test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags) ? in crypt_status()
3553 DMEMIT(",iv_large_sectors=%c", test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags) ? in crypt_status()
3556 if (cc->on_disk_tag_size) in crypt_status()
3558 cc->on_disk_tag_size, cc->cipher_auth); in crypt_status()
3559 if (cc->sector_size != (1 << SECTOR_SHIFT)) in crypt_status()
3560 DMEMIT(",sector_size=%d", cc->sector_size); in crypt_status()
3561 if (cc->cipher_string) in crypt_status()
3562 DMEMIT(",cipher_string=%s", cc->cipher_string); in crypt_status()
3564 DMEMIT(",key_size=%u", cc->key_size); in crypt_status()
3565 DMEMIT(",key_parts=%u", cc->key_parts); in crypt_status()
3566 DMEMIT(",key_extra_size=%u", cc->key_extra_size); in crypt_status()
3567 DMEMIT(",key_mac_size=%u", cc->key_mac_size); in crypt_status()
3575 struct crypt_config *cc = ti->private; in crypt_postsuspend() local
3577 set_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_postsuspend()
3582 struct crypt_config *cc = ti->private; in crypt_preresume() local
3584 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { in crypt_preresume()
3594 struct crypt_config *cc = ti->private; in crypt_resume() local
3596 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_resume()
3606 struct crypt_config *cc = ti->private; in crypt_message() local
3613 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { in crypt_message()
3620 if (key_size < 0 || cc->key_size != key_size) { in crypt_message()
3625 ret = crypt_set_key(cc, argv[2]); in crypt_message()
3628 if (cc->iv_gen_ops && cc->iv_gen_ops->init) in crypt_message()
3629 ret = cc->iv_gen_ops->init(cc); in crypt_message()
3631 if (cc->key_string) in crypt_message()
3632 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_message()
3636 return crypt_wipe_key(cc); in crypt_message()
3647 struct crypt_config *cc = ti->private; in crypt_iterate_devices() local
3649 return fn(ti, cc->dev, cc->start, ti->len, data); in crypt_iterate_devices()
3654 struct crypt_config *cc = ti->private; in crypt_io_hints() local
3665 max_t(unsigned int, limits->logical_block_size, cc->sector_size); in crypt_io_hints()
3667 max_t(unsigned int, limits->physical_block_size, cc->sector_size); in crypt_io_hints()
3668 limits->io_min = max_t(unsigned int, limits->io_min, cc->sector_size); in crypt_io_hints()