Lines Matching refs:cc

75 	struct crypt_config *cc;  member
103 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
105 void (*dtr)(struct crypt_config *cc);
106 int (*init)(struct crypt_config *cc);
107 int (*wipe)(struct crypt_config *cc);
108 int (*generator)(struct crypt_config *cc, u8 *iv,
110 int (*post)(struct crypt_config *cc, u8 *iv,
245 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
248 static bool crypt_integrity_aead(struct crypt_config *cc);
253 static struct crypto_skcipher *any_tfm(struct crypt_config *cc) in any_tfm() argument
255 return cc->cipher_tfm.tfms[0]; in any_tfm()
258 static struct crypto_aead *any_tfm_aead(struct crypt_config *cc) in any_tfm_aead() argument
260 return cc->cipher_tfm.tfms_aead[0]; in any_tfm_aead()
319 static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain_gen() argument
322 memset(iv, 0, cc->iv_size); in crypt_iv_plain_gen()
328 static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64_gen() argument
331 memset(iv, 0, cc->iv_size); in crypt_iv_plain64_gen()
337 static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64be_gen() argument
340 memset(iv, 0, cc->iv_size); in crypt_iv_plain64be_gen()
342 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector); in crypt_iv_plain64be_gen()
347 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_essiv_gen() argument
354 memset(iv, 0, cc->iv_size); in crypt_iv_essiv_gen()
360 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_benbi_ctr() argument
366 if (crypt_integrity_aead(cc)) in crypt_iv_benbi_ctr()
367 bs = crypto_aead_blocksize(any_tfm_aead(cc)); in crypt_iv_benbi_ctr()
369 bs = crypto_skcipher_blocksize(any_tfm(cc)); in crypt_iv_benbi_ctr()
386 cc->iv_gen_private.benbi.shift = 9 - log; in crypt_iv_benbi_ctr()
391 static void crypt_iv_benbi_dtr(struct crypt_config *cc) in crypt_iv_benbi_dtr() argument
395 static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_benbi_gen() argument
400 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ in crypt_iv_benbi_gen()
402 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); in crypt_iv_benbi_gen()
403 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); in crypt_iv_benbi_gen()
408 static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_null_gen() argument
411 memset(iv, 0, cc->iv_size); in crypt_iv_null_gen()
416 static void crypt_iv_lmk_dtr(struct crypt_config *cc) in crypt_iv_lmk_dtr() argument
418 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_dtr()
428 static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_lmk_ctr() argument
431 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_ctr()
433 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_lmk_ctr()
446 if (cc->key_parts == cc->tfms_count) { in crypt_iv_lmk_ctr()
453 crypt_iv_lmk_dtr(cc); in crypt_iv_lmk_ctr()
461 static int crypt_iv_lmk_init(struct crypt_config *cc) in crypt_iv_lmk_init() argument
463 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_init()
464 int subkey_size = cc->key_size / cc->key_parts; in crypt_iv_lmk_init()
468 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), in crypt_iv_lmk_init()
474 static int crypt_iv_lmk_wipe(struct crypt_config *cc) in crypt_iv_lmk_wipe() argument
476 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_wipe()
484 static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_one() argument
488 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_one()
527 memcpy(iv, &md5state.hash, cc->iv_size); in crypt_iv_lmk_one()
532 static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_gen() argument
540 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_lmk_gen()
542 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset); in crypt_iv_lmk_gen()
545 memset(iv, 0, cc->iv_size); in crypt_iv_lmk_gen()
550 static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_post() argument
560 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_lmk_post()
562 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset); in crypt_iv_lmk_post()
566 crypto_xor(dst + sg->offset, iv, cc->iv_size); in crypt_iv_lmk_post()
572 static void crypt_iv_tcw_dtr(struct crypt_config *cc) in crypt_iv_tcw_dtr() argument
574 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_dtr()
586 static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_tcw_ctr() argument
589 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_ctr()
591 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_tcw_ctr()
596 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { in crypt_iv_tcw_ctr()
608 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); in crypt_iv_tcw_ctr()
611 crypt_iv_tcw_dtr(cc); in crypt_iv_tcw_ctr()
619 static int crypt_iv_tcw_init(struct crypt_config *cc) in crypt_iv_tcw_init() argument
621 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_init()
622 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; in crypt_iv_tcw_init()
624 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); in crypt_iv_tcw_init()
625 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], in crypt_iv_tcw_init()
631 static int crypt_iv_tcw_wipe(struct crypt_config *cc) in crypt_iv_tcw_wipe() argument
633 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_wipe()
635 memset(tcw->iv_seed, 0, cc->iv_size); in crypt_iv_tcw_wipe()
641 static int crypt_iv_tcw_whitening(struct crypt_config *cc, in crypt_iv_tcw_whitening() argument
645 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_whitening()
679 static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_gen() argument
683 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_gen()
690 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_tcw_gen()
692 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset); in crypt_iv_tcw_gen()
698 if (cc->iv_size > 8) in crypt_iv_tcw_gen()
700 cc->iv_size - 8); in crypt_iv_tcw_gen()
705 static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_post() argument
716 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_tcw_post()
718 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset); in crypt_iv_tcw_post()
724 static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_random_gen() argument
728 get_random_bytes(iv, cc->iv_size); in crypt_iv_random_gen()
732 static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_eboiv_ctr() argument
735 if (crypt_integrity_aead(cc)) { in crypt_iv_eboiv_ctr()
740 if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) { in crypt_iv_eboiv_ctr()
748 static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_eboiv_gen() argument
751 struct crypto_skcipher *tfm = any_tfm(cc); in crypt_iv_eboiv_gen()
762 req = kmalloc(reqsize + cc->iv_size, GFP_NOIO); in crypt_iv_eboiv_gen()
769 memset(buf, 0, cc->iv_size); in crypt_iv_eboiv_gen()
770 *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size); in crypt_iv_eboiv_gen()
772 sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size); in crypt_iv_eboiv_gen()
773 sg_init_one(&dst, iv, cc->iv_size); in crypt_iv_eboiv_gen()
774 skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf); in crypt_iv_eboiv_gen()
782 static void crypt_iv_elephant_dtr(struct crypt_config *cc) in crypt_iv_elephant_dtr() argument
784 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_dtr()
790 static int crypt_iv_elephant_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_elephant_ctr() argument
793 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_ctr()
804 r = crypt_iv_eboiv_ctr(cc, ti, NULL); in crypt_iv_elephant_ctr()
806 crypt_iv_elephant_dtr(cc); in crypt_iv_elephant_ctr()
954 static int crypt_iv_elephant(struct crypt_config *cc, struct dm_crypt_request *dmreq) in crypt_iv_elephant() argument
956 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant()
972 *(__le64 *)es = cpu_to_le64(dmreq->iv_sector * cc->sector_size); in crypt_iv_elephant()
990 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_elephant()
996 sg2 = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_elephant()
998 memcpy(data_offset, data2 + sg2->offset, cc->sector_size); in crypt_iv_elephant()
1003 diffuser_disk_to_cpu((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1004 diffuser_b_decrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1005 diffuser_a_decrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1006 diffuser_cpu_to_disk((__le32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1009 for (i = 0; i < (cc->sector_size / 32); i++) in crypt_iv_elephant()
1013 diffuser_disk_to_cpu((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1014 diffuser_a_encrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1015 diffuser_b_encrypt((u32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1016 diffuser_cpu_to_disk((__le32 *)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1027 static int crypt_iv_elephant_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_elephant_gen() argument
1033 r = crypt_iv_elephant(cc, dmreq); in crypt_iv_elephant_gen()
1038 return crypt_iv_eboiv_gen(cc, iv, dmreq); in crypt_iv_elephant_gen()
1041 static int crypt_iv_elephant_post(struct crypt_config *cc, u8 *iv, in crypt_iv_elephant_post() argument
1045 return crypt_iv_elephant(cc, dmreq); in crypt_iv_elephant_post()
1050 static int crypt_iv_elephant_init(struct crypt_config *cc) in crypt_iv_elephant_init() argument
1052 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_init()
1053 int key_offset = cc->key_size - cc->key_extra_size; in crypt_iv_elephant_init()
1055 return crypto_skcipher_setkey(elephant->tfm, &cc->key[key_offset], cc->key_extra_size); in crypt_iv_elephant_init()
1058 static int crypt_iv_elephant_wipe(struct crypt_config *cc) in crypt_iv_elephant_wipe() argument
1060 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_wipe()
1063 memset(key, 0, cc->key_extra_size); in crypt_iv_elephant_wipe()
1064 return crypto_skcipher_setkey(elephant->tfm, key, cc->key_extra_size); in crypt_iv_elephant_wipe()
1132 static bool crypt_integrity_aead(struct crypt_config *cc) in crypt_integrity_aead() argument
1134 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_integrity_aead()
1137 static bool crypt_integrity_hmac(struct crypt_config *cc) in crypt_integrity_hmac() argument
1139 return crypt_integrity_aead(cc) && cc->key_mac_size; in crypt_integrity_hmac()
1143 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, in crypt_get_sg_data() argument
1146 if (unlikely(crypt_integrity_aead(cc))) in crypt_get_sg_data()
1158 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size) in dm_crypt_integrity_io_alloc()
1165 tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift); in dm_crypt_integrity_io_alloc()
1167 bip->bip_iter.bi_sector = io->cc->start + io->sector; in dm_crypt_integrity_io_alloc()
1177 static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti) in crypt_integrity_ctr() argument
1180 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk); in crypt_integrity_ctr()
1189 if (bi->tag_size != cc->on_disk_tag_size || in crypt_integrity_ctr()
1190 bi->tuple_size != cc->on_disk_tag_size) { in crypt_integrity_ctr()
1194 if (1 << bi->interval_exp != cc->sector_size) { in crypt_integrity_ctr()
1199 if (crypt_integrity_aead(cc)) { in crypt_integrity_ctr()
1200 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size; in crypt_integrity_ctr()
1202 cc->integrity_tag_size, cc->integrity_iv_size); in crypt_integrity_ctr()
1204 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) { in crypt_integrity_ctr()
1208 } else if (cc->integrity_iv_size) in crypt_integrity_ctr()
1210 cc->integrity_iv_size); in crypt_integrity_ctr()
1212 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) { in crypt_integrity_ctr()
1224 static void crypt_convert_init(struct crypt_config *cc, in crypt_convert_init() argument
1235 ctx->cc_sector = sector + cc->iv_offset; in crypt_convert_init()
1240 static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, in dmreq_of_req() argument
1243 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); in dmreq_of_req()
1246 static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq) in req_of_dmreq() argument
1248 return (void *)((char *)dmreq - cc->dmreq_start); in req_of_dmreq()
1251 static u8 *iv_of_dmreq(struct crypt_config *cc, in iv_of_dmreq() argument
1254 if (crypt_integrity_aead(cc)) in iv_of_dmreq()
1256 crypto_aead_alignmask(any_tfm_aead(cc)) + 1); in iv_of_dmreq()
1259 crypto_skcipher_alignmask(any_tfm(cc)) + 1); in iv_of_dmreq()
1262 static u8 *org_iv_of_dmreq(struct crypt_config *cc, in org_iv_of_dmreq() argument
1265 return iv_of_dmreq(cc, dmreq) + cc->iv_size; in org_iv_of_dmreq()
1268 static __le64 *org_sector_of_dmreq(struct crypt_config *cc, in org_sector_of_dmreq() argument
1271 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size; in org_sector_of_dmreq()
1276 static unsigned int *org_tag_of_dmreq(struct crypt_config *cc, in org_tag_of_dmreq() argument
1279 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + in org_tag_of_dmreq()
1280 cc->iv_size + sizeof(uint64_t); in org_tag_of_dmreq()
1285 static void *tag_from_dmreq(struct crypt_config *cc, in tag_from_dmreq() argument
1291 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) * in tag_from_dmreq()
1292 cc->on_disk_tag_size]; in tag_from_dmreq()
1295 static void *iv_tag_from_dmreq(struct crypt_config *cc, in iv_tag_from_dmreq() argument
1298 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size; in iv_tag_from_dmreq()
1301 static int crypt_convert_block_aead(struct crypt_config *cc, in crypt_convert_block_aead() argument
1313 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size); in crypt_convert_block_aead()
1316 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_aead()
1319 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_aead()
1321 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_aead()
1322 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_aead()
1325 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_aead()
1327 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1328 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_aead()
1330 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1331 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1332 tag = tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1333 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1342 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1343 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_aead()
1344 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1348 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1349 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_aead()
1350 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1352 if (cc->iv_gen_ops) { in crypt_convert_block_aead()
1354 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_aead()
1355 memcpy(org_iv, tag_iv, cc->iv_size); in crypt_convert_block_aead()
1357 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_aead()
1361 if (cc->integrity_iv_size) in crypt_convert_block_aead()
1362 memcpy(tag_iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1365 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1368 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size); in crypt_convert_block_aead()
1371 cc->sector_size, iv); in crypt_convert_block_aead()
1373 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size) in crypt_convert_block_aead()
1374 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0, in crypt_convert_block_aead()
1375 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size)); in crypt_convert_block_aead()
1378 cc->sector_size + cc->integrity_tag_size, iv); in crypt_convert_block_aead()
1394 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_aead()
1395 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_aead()
1397 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_aead()
1398 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_aead()
1403 static int crypt_convert_block_skcipher(struct crypt_config *cc, in crypt_convert_block_skcipher() argument
1417 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_skcipher()
1420 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_skcipher()
1422 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1423 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_skcipher()
1426 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_skcipher()
1428 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1429 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1430 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1432 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1433 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_skcipher()
1440 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_skcipher()
1443 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_skcipher()
1445 if (cc->iv_gen_ops) { in crypt_convert_block_skcipher()
1447 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_skcipher()
1448 memcpy(org_iv, tag_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1450 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1454 if (test_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1457 if (cc->integrity_iv_size) in crypt_convert_block_skcipher()
1458 memcpy(tag_iv, org_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1461 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_skcipher()
1464 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv); in crypt_convert_block_skcipher()
1471 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_skcipher()
1472 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1474 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_skcipher()
1475 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_skcipher()
1482 static int crypt_alloc_req_skcipher(struct crypt_config *cc, in crypt_alloc_req_skcipher() argument
1485 unsigned int key_index = ctx->cc_sector & (cc->tfms_count - 1); in crypt_alloc_req_skcipher()
1488 ctx->r.req = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); in crypt_alloc_req_skcipher()
1493 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]); in crypt_alloc_req_skcipher()
1501 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req)); in crypt_alloc_req_skcipher()
1506 static int crypt_alloc_req_aead(struct crypt_config *cc, in crypt_alloc_req_aead() argument
1510 ctx->r.req_aead = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); in crypt_alloc_req_aead()
1515 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_req_aead()
1523 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead)); in crypt_alloc_req_aead()
1528 static int crypt_alloc_req(struct crypt_config *cc, in crypt_alloc_req() argument
1531 if (crypt_integrity_aead(cc)) in crypt_alloc_req()
1532 return crypt_alloc_req_aead(cc, ctx); in crypt_alloc_req()
1534 return crypt_alloc_req_skcipher(cc, ctx); in crypt_alloc_req()
1537 static void crypt_free_req_skcipher(struct crypt_config *cc, in crypt_free_req_skcipher() argument
1540 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_skcipher()
1543 mempool_free(req, &cc->req_pool); in crypt_free_req_skcipher()
1546 static void crypt_free_req_aead(struct crypt_config *cc, in crypt_free_req_aead() argument
1549 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_aead()
1552 mempool_free(req, &cc->req_pool); in crypt_free_req_aead()
1555 static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio) in crypt_free_req() argument
1557 if (crypt_integrity_aead(cc)) in crypt_free_req()
1558 crypt_free_req_aead(cc, req, base_bio); in crypt_free_req()
1560 crypt_free_req_skcipher(cc, req, base_bio); in crypt_free_req()
1566 static blk_status_t crypt_convert(struct crypt_config *cc, in crypt_convert() argument
1569 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT; in crypt_convert()
1582 r = crypt_alloc_req(cc, ctx); in crypt_convert()
1590 if (crypt_integrity_aead(cc)) in crypt_convert()
1591 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, ctx->tag_offset); in crypt_convert()
1593 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, ctx->tag_offset); in crypt_convert()
1659 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1683 struct crypt_config *cc = io->cc; in crypt_alloc_buffer() local
1692 mutex_lock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1694 clone = bio_alloc_bioset(cc->dev->bdev, nr_iovecs, io->base_bio->bi_opf, in crypt_alloc_buffer()
1695 GFP_NOIO, &cc->bs); in crypt_alloc_buffer()
1708 if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) + in crypt_alloc_buffer()
1715 percpu_counter_add(&cc->n_allocated_pages, 1 << order); in crypt_alloc_buffer()
1722 pages = mempool_alloc(&cc->page_pool, gfp_mask); in crypt_alloc_buffer()
1724 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1739 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1745 mutex_unlock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1750 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) in crypt_free_buffer_pages() argument
1757 percpu_counter_sub(&cc->n_allocated_pages, in crypt_free_buffer_pages()
1761 mempool_free(&fi.folio->page, &cc->page_pool); in crypt_free_buffer_pages()
1767 static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, in crypt_io_init() argument
1770 io->cc = cc; in crypt_io_init()
1795 struct crypt_config *cc = io->cc; in crypt_dec_pending() local
1803 cc->on_disk_tag_size && bio_data_dir(base_bio) == READ) { in crypt_dec_pending()
1812 crypt_free_req(cc, io->ctx.r.req, base_bio); in crypt_dec_pending()
1815 mempool_free(io->integrity_metadata, &io->cc->tag_pool); in crypt_dec_pending()
1844 struct crypt_config *cc = io->cc; in crypt_endio() local
1857 crypt_free_buffer_pages(cc, clone); in crypt_endio()
1876 struct crypt_config *cc = io->cc; in kcryptd_io_read() local
1888 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_io_read()
1889 crypt_convert_init(cc, &io->ctx, clone, clone, io->sector); in kcryptd_io_read()
1901 clone = bio_alloc_clone(cc->dev->bdev, io->base_bio, gfp, &cc->bs); in kcryptd_io_read()
1909 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_io_read()
1933 struct crypt_config *cc = io->cc; in kcryptd_queue_read() local
1936 queue_work(cc->io_queue, &io->work); in kcryptd_queue_read()
1950 struct crypt_config *cc = data; in dmcrypt_write() local
1957 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1960 if (!RB_EMPTY_ROOT(&cc->write_tree)) in dmcrypt_write()
1965 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1975 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1979 write_tree = cc->write_tree; in dmcrypt_write()
1980 cc->write_tree = RB_ROOT; in dmcrypt_write()
1981 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
2004 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_io_submit() local
2010 crypt_free_buffer_pages(cc, clone); in kcryptd_crypt_write_io_submit()
2019 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_crypt_write_io_submit()
2021 if ((likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) || in kcryptd_crypt_write_io_submit()
2022 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) { in kcryptd_crypt_write_io_submit()
2027 spin_lock_irqsave(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
2028 if (RB_EMPTY_ROOT(&cc->write_tree)) in kcryptd_crypt_write_io_submit()
2029 wake_up_process(cc->write_thread); in kcryptd_crypt_write_io_submit()
2030 rbp = &cc->write_tree.rb_node; in kcryptd_crypt_write_io_submit()
2041 rb_insert_color(&io->rb_node, &cc->write_tree); in kcryptd_crypt_write_io_submit()
2042 spin_unlock_irqrestore(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
2045 static bool kcryptd_crypt_write_inline(struct crypt_config *cc, in kcryptd_crypt_write_inline() argument
2049 if (!test_bit(DM_CRYPT_WRITE_INLINE, &cc->flags)) in kcryptd_crypt_write_inline()
2069 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_continue() local
2077 r = crypt_convert(cc, &io->ctx, true, false); in kcryptd_crypt_write_continue()
2081 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_crypt_write_continue()
2096 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_convert() local
2106 crypt_convert_init(cc, ctx, NULL, io->base_bio, io->sector); in kcryptd_crypt_write_convert()
2117 if (crypt_integrity_aead(cc)) { in kcryptd_crypt_write_convert()
2124 r = crypt_convert(cc, ctx, in kcryptd_crypt_write_convert()
2125 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_write_convert()
2133 queue_work(cc->crypt_queue, &io->work); in kcryptd_crypt_write_convert()
2139 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_crypt_write_convert()
2160 crypt_free_buffer_pages(io->cc, io->ctx.bio_in); in kcryptd_crypt_read_done()
2169 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_continue() local
2175 r = crypt_convert(cc, &io->ctx, true, false); in kcryptd_crypt_read_continue()
2187 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_convert() local
2193 io->ctx.cc_sector = io->sector + cc->iv_offset; in kcryptd_crypt_read_convert()
2194 r = crypt_convert(cc, &io->ctx, in kcryptd_crypt_read_convert()
2195 test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_read_convert()
2197 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, in kcryptd_crypt_read_convert()
2200 r = crypt_convert(cc, &io->ctx, in kcryptd_crypt_read_convert()
2201 test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_read_convert()
2209 queue_work(cc->crypt_queue, &io->work); in kcryptd_crypt_read_convert()
2226 struct crypt_config *cc = io->cc; in kcryptd_async_done() local
2238 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) in kcryptd_async_done()
2239 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq); in kcryptd_async_done()
2242 sector_t s = le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)); in kcryptd_async_done()
2255 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); in kcryptd_async_done()
2269 if (kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_async_done()
2289 struct crypt_config *cc = io->cc; in kcryptd_queue_crypt() local
2291 if ((bio_data_dir(io->base_bio) == READ && test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) || in kcryptd_queue_crypt()
2292 (bio_data_dir(io->base_bio) == WRITE && test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags))) { in kcryptd_queue_crypt()
2305 queue_work(cc->crypt_queue, &io->work); in kcryptd_queue_crypt()
2308 static void crypt_free_tfms_aead(struct crypt_config *cc) in crypt_free_tfms_aead() argument
2310 if (!cc->cipher_tfm.tfms_aead) in crypt_free_tfms_aead()
2313 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_free_tfms_aead()
2314 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]); in crypt_free_tfms_aead()
2315 cc->cipher_tfm.tfms_aead[0] = NULL; in crypt_free_tfms_aead()
2318 kfree(cc->cipher_tfm.tfms_aead); in crypt_free_tfms_aead()
2319 cc->cipher_tfm.tfms_aead = NULL; in crypt_free_tfms_aead()
2322 static void crypt_free_tfms_skcipher(struct crypt_config *cc) in crypt_free_tfms_skcipher() argument
2326 if (!cc->cipher_tfm.tfms) in crypt_free_tfms_skcipher()
2329 for (i = 0; i < cc->tfms_count; i++) in crypt_free_tfms_skcipher()
2330 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_free_tfms_skcipher()
2331 crypto_free_skcipher(cc->cipher_tfm.tfms[i]); in crypt_free_tfms_skcipher()
2332 cc->cipher_tfm.tfms[i] = NULL; in crypt_free_tfms_skcipher()
2335 kfree(cc->cipher_tfm.tfms); in crypt_free_tfms_skcipher()
2336 cc->cipher_tfm.tfms = NULL; in crypt_free_tfms_skcipher()
2339 static void crypt_free_tfms(struct crypt_config *cc) in crypt_free_tfms() argument
2341 if (crypt_integrity_aead(cc)) in crypt_free_tfms()
2342 crypt_free_tfms_aead(cc); in crypt_free_tfms()
2344 crypt_free_tfms_skcipher(cc); in crypt_free_tfms()
2347 static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_skcipher() argument
2352 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count, in crypt_alloc_tfms_skcipher()
2355 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_skcipher()
2358 for (i = 0; i < cc->tfms_count; i++) { in crypt_alloc_tfms_skcipher()
2359 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, in crypt_alloc_tfms_skcipher()
2361 if (IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_alloc_tfms_skcipher()
2362 err = PTR_ERR(cc->cipher_tfm.tfms[i]); in crypt_alloc_tfms_skcipher()
2363 crypt_free_tfms(cc); in crypt_alloc_tfms_skcipher()
2374 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name); in crypt_alloc_tfms_skcipher()
2378 static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_aead() argument
2382 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL); in crypt_alloc_tfms_aead()
2383 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_aead()
2386 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, in crypt_alloc_tfms_aead()
2388 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_alloc_tfms_aead()
2389 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_tfms_aead()
2390 crypt_free_tfms(cc); in crypt_alloc_tfms_aead()
2395 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name); in crypt_alloc_tfms_aead()
2399 static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms() argument
2401 if (crypt_integrity_aead(cc)) in crypt_alloc_tfms()
2402 return crypt_alloc_tfms_aead(cc, ciphermode); in crypt_alloc_tfms()
2404 return crypt_alloc_tfms_skcipher(cc, ciphermode); in crypt_alloc_tfms()
2407 static unsigned int crypt_subkey_size(struct crypt_config *cc) in crypt_subkey_size() argument
2409 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); in crypt_subkey_size()
2412 static unsigned int crypt_authenckey_size(struct crypt_config *cc) in crypt_authenckey_size() argument
2414 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param)); in crypt_authenckey_size()
2439 static int crypt_setkey(struct crypt_config *cc) in crypt_setkey() argument
2445 subkey_size = crypt_subkey_size(cc); in crypt_setkey()
2447 if (crypt_integrity_hmac(cc)) { in crypt_setkey()
2448 if (subkey_size < cc->key_mac_size) in crypt_setkey()
2451 crypt_copy_authenckey(cc->authenc_key, cc->key, in crypt_setkey()
2452 subkey_size - cc->key_mac_size, in crypt_setkey()
2453 cc->key_mac_size); in crypt_setkey()
2456 for (i = 0; i < cc->tfms_count; i++) { in crypt_setkey()
2457 if (crypt_integrity_hmac(cc)) in crypt_setkey()
2458 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
2459 cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
2460 else if (crypt_integrity_aead(cc)) in crypt_setkey()
2461 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
2462 cc->key + (i * subkey_size), in crypt_setkey()
2465 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i], in crypt_setkey()
2466 cc->key + (i * subkey_size), in crypt_setkey()
2472 if (crypt_integrity_hmac(cc)) in crypt_setkey()
2473 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
2488 static int set_key_user(struct crypt_config *cc, struct key *key) in set_key_user() argument
2496 if (cc->key_size != ukp->datalen) in set_key_user()
2499 memcpy(cc->key, ukp->data, cc->key_size); in set_key_user()
2504 static int set_key_encrypted(struct crypt_config *cc, struct key *key) in set_key_encrypted() argument
2512 if (cc->key_size != ekp->decrypted_datalen) in set_key_encrypted()
2515 memcpy(cc->key, ekp->decrypted_data, cc->key_size); in set_key_encrypted()
2520 static int set_key_trusted(struct crypt_config *cc, struct key *key) in set_key_trusted() argument
2528 if (cc->key_size != tkp->key_len) in set_key_trusted()
2531 memcpy(cc->key, tkp->key, cc->key_size); in set_key_trusted()
2536 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2542 int (*set_key)(struct crypt_config *cc, struct key *key); in crypt_set_keyring_key()
2588 ret = set_key(cc, key); in crypt_set_keyring_key()
2600 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2602 ret = crypt_setkey(cc); in crypt_set_keyring_key()
2605 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2606 kfree_sensitive(cc->key_string); in crypt_set_keyring_key()
2607 cc->key_string = new_key_string; in crypt_set_keyring_key()
2639 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2651 static int crypt_set_key(struct crypt_config *cc, char *key) in crypt_set_key() argument
2657 if (!cc->key_size && strcmp(key, "-")) in crypt_set_key()
2662 r = crypt_set_keyring_key(cc, key + 1); in crypt_set_key()
2667 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2670 kfree_sensitive(cc->key_string); in crypt_set_key()
2671 cc->key_string = NULL; in crypt_set_key()
2674 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) in crypt_set_key()
2677 r = crypt_setkey(cc); in crypt_set_key()
2679 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2688 static int crypt_wipe_key(struct crypt_config *cc) in crypt_wipe_key() argument
2692 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_wipe_key()
2693 get_random_bytes(&cc->key, cc->key_size); in crypt_wipe_key()
2696 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { in crypt_wipe_key()
2697 r = cc->iv_gen_ops->wipe(cc); in crypt_wipe_key()
2702 kfree_sensitive(cc->key_string); in crypt_wipe_key()
2703 cc->key_string = NULL; in crypt_wipe_key()
2704 r = crypt_setkey(cc); in crypt_wipe_key()
2705 memset(&cc->key, 0, cc->key_size * sizeof(u8)); in crypt_wipe_key()
2725 struct crypt_config *cc = pool_data; in crypt_page_alloc() local
2733 if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) >= dm_crypt_pages_per_client) && in crypt_page_alloc()
2739 percpu_counter_add(&cc->n_allocated_pages, 1); in crypt_page_alloc()
2746 struct crypt_config *cc = pool_data; in crypt_page_free() local
2749 percpu_counter_sub(&cc->n_allocated_pages, 1); in crypt_page_free()
2754 struct crypt_config *cc = ti->private; in crypt_dtr() local
2758 if (!cc) in crypt_dtr()
2761 if (cc->write_thread) in crypt_dtr()
2762 kthread_stop(cc->write_thread); in crypt_dtr()
2764 if (cc->io_queue) in crypt_dtr()
2765 destroy_workqueue(cc->io_queue); in crypt_dtr()
2766 if (cc->crypt_queue) in crypt_dtr()
2767 destroy_workqueue(cc->crypt_queue); in crypt_dtr()
2769 crypt_free_tfms(cc); in crypt_dtr()
2771 bioset_exit(&cc->bs); in crypt_dtr()
2773 mempool_exit(&cc->page_pool); in crypt_dtr()
2774 mempool_exit(&cc->req_pool); in crypt_dtr()
2775 mempool_exit(&cc->tag_pool); in crypt_dtr()
2777 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0); in crypt_dtr()
2778 percpu_counter_destroy(&cc->n_allocated_pages); in crypt_dtr()
2780 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) in crypt_dtr()
2781 cc->iv_gen_ops->dtr(cc); in crypt_dtr()
2783 if (cc->dev) in crypt_dtr()
2784 dm_put_device(ti, cc->dev); in crypt_dtr()
2786 kfree_sensitive(cc->cipher_string); in crypt_dtr()
2787 kfree_sensitive(cc->key_string); in crypt_dtr()
2788 kfree_sensitive(cc->cipher_auth); in crypt_dtr()
2789 kfree_sensitive(cc->authenc_key); in crypt_dtr()
2791 mutex_destroy(&cc->bio_alloc_lock); in crypt_dtr()
2794 kfree_sensitive(cc); in crypt_dtr()
2807 struct crypt_config *cc = ti->private; in crypt_ctr_ivmode() local
2809 if (crypt_integrity_aead(cc)) in crypt_ctr_ivmode()
2810 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_ivmode()
2812 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_ivmode()
2814 if (cc->iv_size) in crypt_ctr_ivmode()
2816 cc->iv_size = max(cc->iv_size, in crypt_ctr_ivmode()
2825 cc->iv_gen_ops = NULL; in crypt_ctr_ivmode()
2827 cc->iv_gen_ops = &crypt_iv_plain_ops; in crypt_ctr_ivmode()
2829 cc->iv_gen_ops = &crypt_iv_plain64_ops; in crypt_ctr_ivmode()
2831 cc->iv_gen_ops = &crypt_iv_plain64be_ops; in crypt_ctr_ivmode()
2833 cc->iv_gen_ops = &crypt_iv_essiv_ops; in crypt_ctr_ivmode()
2835 cc->iv_gen_ops = &crypt_iv_benbi_ops; in crypt_ctr_ivmode()
2837 cc->iv_gen_ops = &crypt_iv_null_ops; in crypt_ctr_ivmode()
2839 cc->iv_gen_ops = &crypt_iv_eboiv_ops; in crypt_ctr_ivmode()
2841 cc->iv_gen_ops = &crypt_iv_elephant_ops; in crypt_ctr_ivmode()
2842 cc->key_parts = 2; in crypt_ctr_ivmode()
2843 cc->key_extra_size = cc->key_size / 2; in crypt_ctr_ivmode()
2844 if (cc->key_extra_size > ELEPHANT_MAX_KEY_SIZE) in crypt_ctr_ivmode()
2846 set_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags); in crypt_ctr_ivmode()
2848 cc->iv_gen_ops = &crypt_iv_lmk_ops; in crypt_ctr_ivmode()
2855 if (cc->key_size % cc->key_parts) { in crypt_ctr_ivmode()
2856 cc->key_parts++; in crypt_ctr_ivmode()
2857 cc->key_extra_size = cc->key_size / cc->key_parts; in crypt_ctr_ivmode()
2860 cc->iv_gen_ops = &crypt_iv_tcw_ops; in crypt_ctr_ivmode()
2861 cc->key_parts += 2; /* IV + whitening */ in crypt_ctr_ivmode()
2862 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; in crypt_ctr_ivmode()
2864 cc->iv_gen_ops = &crypt_iv_random_ops; in crypt_ctr_ivmode()
2866 cc->integrity_iv_size = cc->iv_size; in crypt_ctr_ivmode()
2880 static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) in crypt_ctr_auth_cipher() argument
2904 cc->key_mac_size = crypto_ahash_digestsize(mac); in crypt_ctr_auth_cipher()
2907 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL); in crypt_ctr_auth_cipher()
2908 if (!cc->authenc_key) in crypt_ctr_auth_cipher()
2917 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_new() local
2921 cc->tfms_count = 1; in crypt_ctr_cipher_new()
2945 if (crypt_integrity_aead(cc)) { in crypt_ctr_cipher_new()
2946 ret = crypt_ctr_auth_cipher(cc, cipher_api); in crypt_ctr_cipher_new()
2954 cc->tfms_count = 64; in crypt_ctr_cipher_new()
2970 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_new()
2973 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_new()
2979 if (crypt_integrity_aead(cc)) in crypt_ctr_cipher_new()
2980 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_cipher_new()
2982 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_cipher_new()
2990 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_old() local
2996 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) { in crypt_ctr_cipher_old()
3010 cc->tfms_count = 1; in crypt_ctr_cipher_old()
3011 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || in crypt_ctr_cipher_old()
3012 !is_power_of_2(cc->tfms_count)) { in crypt_ctr_cipher_old()
3016 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_old()
3058 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_old()
3074 struct crypt_config *cc = ti->private; in crypt_ctr_cipher() local
3078 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); in crypt_ctr_cipher()
3079 if (!cc->cipher_string) { in crypt_ctr_cipher()
3097 ret = crypt_set_key(cc, key); in crypt_ctr_cipher()
3104 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { in crypt_ctr_cipher()
3105 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); in crypt_ctr_cipher()
3113 if (cc->iv_gen_ops && cc->iv_gen_ops->init) { in crypt_ctr_cipher()
3114 ret = cc->iv_gen_ops->init(cc); in crypt_ctr_cipher()
3122 if (cc->key_string) in crypt_ctr_cipher()
3123 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_ctr_cipher()
3130 struct crypt_config *cc = ti->private; in crypt_ctr_optional() local
3159 set_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_ctr_optional()
3162 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_ctr_optional()
3164 set_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); in crypt_ctr_optional()
3166 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_ctr_optional()
3172 cc->on_disk_tag_size = val; in crypt_ctr_optional()
3175 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_ctr_optional()
3181 cc->cipher_auth = kstrdup(sval, GFP_KERNEL); in crypt_ctr_optional()
3182 if (!cc->cipher_auth) in crypt_ctr_optional()
3184 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { in crypt_ctr_optional()
3185 if (cc->sector_size < (1 << SECTOR_SHIFT) || in crypt_ctr_optional()
3186 cc->sector_size > 4096 || in crypt_ctr_optional()
3187 (cc->sector_size & (cc->sector_size - 1))) { in crypt_ctr_optional()
3191 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) { in crypt_ctr_optional()
3195 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT; in crypt_ctr_optional()
3197 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_ctr_optional()
3211 struct crypt_config *cc = ti->private; in crypt_report_zones() local
3213 return dm_report_zones(cc->dev->bdev, cc->start, in crypt_report_zones()
3214 cc->start + dm_target_offset(ti, args->next_sector), in crypt_report_zones()
3227 struct crypt_config *cc; in crypt_ctr() local
3247 cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL); in crypt_ctr()
3248 if (!cc) { in crypt_ctr()
3252 cc->key_size = key_size; in crypt_ctr()
3253 cc->sector_size = (1 << SECTOR_SHIFT); in crypt_ctr()
3254 cc->sector_shift = 0; in crypt_ctr()
3256 ti->private = cc; in crypt_ctr()
3263 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); in crypt_ctr()
3278 if (crypt_integrity_aead(cc)) { in crypt_ctr()
3279 cc->dmreq_start = sizeof(struct aead_request); in crypt_ctr()
3280 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc)); in crypt_ctr()
3281 align_mask = crypto_aead_alignmask(any_tfm_aead(cc)); in crypt_ctr()
3283 cc->dmreq_start = sizeof(struct skcipher_request); in crypt_ctr()
3284 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); in crypt_ctr()
3285 align_mask = crypto_skcipher_alignmask(any_tfm(cc)); in crypt_ctr()
3287 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); in crypt_ctr()
3291 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) in crypt_ctr()
3304 iv_size_padding + cc->iv_size + in crypt_ctr()
3305 cc->iv_size + in crypt_ctr()
3309 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size); in crypt_ctr()
3315 cc->per_bio_data_size = ti->per_io_data_size = in crypt_ctr()
3316 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size, in crypt_ctr()
3319 ret = mempool_init(&cc->page_pool, BIO_MAX_VECS, crypt_page_alloc, crypt_page_free, cc); in crypt_ctr()
3325 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS); in crypt_ctr()
3331 mutex_init(&cc->bio_alloc_lock); in crypt_ctr()
3335 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { in crypt_ctr()
3339 cc->iv_offset = tmpll; in crypt_ctr()
3341 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); in crypt_ctr()
3352 cc->start = tmpll; in crypt_ctr()
3354 if (bdev_is_zoned(cc->dev->bdev)) { in crypt_ctr()
3360 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_ctr()
3361 set_bit(DM_CRYPT_WRITE_INLINE, &cc->flags); in crypt_ctr()
3378 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) { in crypt_ctr()
3379 ret = crypt_integrity_ctr(cc, ti); in crypt_ctr()
3383 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size; in crypt_ctr()
3384 if (!cc->tag_pool_max_sectors) in crypt_ctr()
3385 cc->tag_pool_max_sectors = 1; in crypt_ctr()
3387 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS, in crypt_ctr()
3388 cc->tag_pool_max_sectors * cc->on_disk_tag_size); in crypt_ctr()
3394 cc->tag_pool_max_sectors <<= cc->sector_shift; in crypt_ctr()
3398 cc->io_queue = alloc_workqueue("kcryptd_io/%s", WQ_MEM_RECLAIM, 1, devname); in crypt_ctr()
3399 if (!cc->io_queue) { in crypt_ctr()
3404 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_ctr()
3405 cc->crypt_queue = alloc_workqueue("kcryptd/%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, in crypt_ctr()
3408 cc->crypt_queue = alloc_workqueue("kcryptd/%s", in crypt_ctr()
3411 if (!cc->crypt_queue) { in crypt_ctr()
3416 spin_lock_init(&cc->write_thread_lock); in crypt_ctr()
3417 cc->write_tree = RB_ROOT; in crypt_ctr()
3419 cc->write_thread = kthread_run(dmcrypt_write, cc, "dmcrypt_write/%s", devname); in crypt_ctr()
3420 if (IS_ERR(cc->write_thread)) { in crypt_ctr()
3421 ret = PTR_ERR(cc->write_thread); in crypt_ctr()
3422 cc->write_thread = NULL; in crypt_ctr()
3443 struct crypt_config *cc = ti->private; in crypt_map() local
3452 bio_set_dev(bio, cc->dev->bdev); in crypt_map()
3454 bio->bi_iter.bi_sector = cc->start + in crypt_map()
3463 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size)) in crypt_map()
3470 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) in crypt_map()
3473 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) in crypt_map()
3476 io = dm_per_bio_data(bio, cc->per_bio_data_size); in crypt_map()
3477 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); in crypt_map()
3479 if (cc->on_disk_tag_size) { in crypt_map()
3480 unsigned int tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift); in crypt_map()
3488 if (bio_sectors(bio) > cc->tag_pool_max_sectors) in crypt_map()
3489 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors); in crypt_map()
3490 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO); in crypt_map()
3495 if (crypt_integrity_aead(cc)) in crypt_map()
3517 struct crypt_config *cc = ti->private; in crypt_status() local
3527 DMEMIT("%s ", cc->cipher_string); in crypt_status()
3529 if (cc->key_size > 0) { in crypt_status()
3530 if (cc->key_string) in crypt_status()
3531 DMEMIT(":%u:%s", cc->key_size, cc->key_string); in crypt_status()
3533 for (i = 0; i < cc->key_size; i++) { in crypt_status()
3534 DMEMIT("%c%c", hex2asc(cc->key[i] >> 4), in crypt_status()
3535 hex2asc(cc->key[i] & 0xf)); in crypt_status()
3541 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, in crypt_status()
3542 cc->dev->name, (unsigned long long)cc->start); in crypt_status()
3545 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_status()
3546 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_status()
3547 num_feature_args += test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); in crypt_status()
3548 num_feature_args += test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_status()
3549 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT); in crypt_status()
3550 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_status()
3551 if (cc->on_disk_tag_size) in crypt_status()
3557 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_status()
3559 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) in crypt_status()
3561 if (test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) in crypt_status()
3563 if (test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) in crypt_status()
3565 if (cc->on_disk_tag_size) in crypt_status()
3566 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth); in crypt_status()
3567 if (cc->sector_size != (1 << SECTOR_SHIFT)) in crypt_status()
3568 DMEMIT(" sector_size:%d", cc->sector_size); in crypt_status()
3569 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_status()
3577 DMEMIT(",same_cpu_crypt=%c", test_bit(DM_CRYPT_SAME_CPU, &cc->flags) ? 'y' : 'n'); in crypt_status()
3578 DMEMIT(",submit_from_crypt_cpus=%c", test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags) ? in crypt_status()
3580 DMEMIT(",no_read_workqueue=%c", test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags) ? in crypt_status()
3582 DMEMIT(",no_write_workqueue=%c", test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags) ? in crypt_status()
3584 DMEMIT(",iv_large_sectors=%c", test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags) ? in crypt_status()
3587 if (cc->on_disk_tag_size) in crypt_status()
3589 cc->on_disk_tag_size, cc->cipher_auth); in crypt_status()
3590 if (cc->sector_size != (1 << SECTOR_SHIFT)) in crypt_status()
3591 DMEMIT(",sector_size=%d", cc->sector_size); in crypt_status()
3592 if (cc->cipher_string) in crypt_status()
3593 DMEMIT(",cipher_string=%s", cc->cipher_string); in crypt_status()
3595 DMEMIT(",key_size=%u", cc->key_size); in crypt_status()
3596 DMEMIT(",key_parts=%u", cc->key_parts); in crypt_status()
3597 DMEMIT(",key_extra_size=%u", cc->key_extra_size); in crypt_status()
3598 DMEMIT(",key_mac_size=%u", cc->key_mac_size); in crypt_status()
3606 struct crypt_config *cc = ti->private; in crypt_postsuspend() local
3608 set_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_postsuspend()
3613 struct crypt_config *cc = ti->private; in crypt_preresume() local
3615 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { in crypt_preresume()
3625 struct crypt_config *cc = ti->private; in crypt_resume() local
3627 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_resume()
3637 struct crypt_config *cc = ti->private; in crypt_message() local
3644 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { in crypt_message()
3651 if (key_size < 0 || cc->key_size != key_size) { in crypt_message()
3656 ret = crypt_set_key(cc, argv[2]); in crypt_message()
3659 if (cc->iv_gen_ops && cc->iv_gen_ops->init) in crypt_message()
3660 ret = cc->iv_gen_ops->init(cc); in crypt_message()
3662 if (cc->key_string) in crypt_message()
3663 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_message()
3667 return crypt_wipe_key(cc); in crypt_message()
3678 struct crypt_config *cc = ti->private; in crypt_iterate_devices() local
3680 return fn(ti, cc->dev, cc->start, ti->len, data); in crypt_iterate_devices()
3685 struct crypt_config *cc = ti->private; in crypt_io_hints() local
3696 max_t(unsigned int, limits->logical_block_size, cc->sector_size); in crypt_io_hints()
3698 max_t(unsigned int, limits->physical_block_size, cc->sector_size); in crypt_io_hints()
3699 limits->io_min = max_t(unsigned int, limits->io_min, cc->sector_size); in crypt_io_hints()