Lines Matching full:cc
71 struct crypt_config *cc; member
97 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
99 void (*dtr)(struct crypt_config *cc);
100 int (*init)(struct crypt_config *cc);
101 int (*wipe)(struct crypt_config *cc);
102 int (*generator)(struct crypt_config *cc, u8 *iv,
104 int (*post)(struct crypt_config *cc, u8 *iv,
239 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
242 static bool crypt_integrity_aead(struct crypt_config *cc);
247 static struct crypto_skcipher *any_tfm(struct crypt_config *cc) in any_tfm() argument
249 return cc->cipher_tfm.tfms[0]; in any_tfm()
252 static struct crypto_aead *any_tfm_aead(struct crypt_config *cc) in any_tfm_aead() argument
254 return cc->cipher_tfm.tfms_aead[0]; in any_tfm_aead()
313 static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain_gen() argument
316 memset(iv, 0, cc->iv_size); in crypt_iv_plain_gen()
322 static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64_gen() argument
325 memset(iv, 0, cc->iv_size); in crypt_iv_plain64_gen()
331 static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64be_gen() argument
334 memset(iv, 0, cc->iv_size); in crypt_iv_plain64be_gen()
336 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector); in crypt_iv_plain64be_gen()
341 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_essiv_gen() argument
348 memset(iv, 0, cc->iv_size); in crypt_iv_essiv_gen()
354 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_benbi_ctr() argument
360 if (crypt_integrity_aead(cc)) in crypt_iv_benbi_ctr()
361 bs = crypto_aead_blocksize(any_tfm_aead(cc)); in crypt_iv_benbi_ctr()
363 bs = crypto_skcipher_blocksize(any_tfm(cc)); in crypt_iv_benbi_ctr()
379 cc->iv_gen_private.benbi.shift = 9 - log; in crypt_iv_benbi_ctr()
384 static void crypt_iv_benbi_dtr(struct crypt_config *cc) in crypt_iv_benbi_dtr() argument
388 static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_benbi_gen() argument
393 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ in crypt_iv_benbi_gen()
395 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); in crypt_iv_benbi_gen()
396 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); in crypt_iv_benbi_gen()
401 static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_null_gen() argument
404 memset(iv, 0, cc->iv_size); in crypt_iv_null_gen()
409 static void crypt_iv_lmk_dtr(struct crypt_config *cc) in crypt_iv_lmk_dtr() argument
411 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_dtr()
421 static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_lmk_ctr() argument
424 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_ctr()
426 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_lmk_ctr()
439 if (cc->key_parts == cc->tfms_count) { in crypt_iv_lmk_ctr()
446 crypt_iv_lmk_dtr(cc); in crypt_iv_lmk_ctr()
454 static int crypt_iv_lmk_init(struct crypt_config *cc) in crypt_iv_lmk_init() argument
456 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_init()
457 int subkey_size = cc->key_size / cc->key_parts; in crypt_iv_lmk_init()
461 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), in crypt_iv_lmk_init()
467 static int crypt_iv_lmk_wipe(struct crypt_config *cc) in crypt_iv_lmk_wipe() argument
469 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_wipe()
477 static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_one() argument
481 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_one()
520 memcpy(iv, &md5state.hash, cc->iv_size); in crypt_iv_lmk_one()
525 static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_gen() argument
533 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_lmk_gen()
535 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset); in crypt_iv_lmk_gen()
538 memset(iv, 0, cc->iv_size); in crypt_iv_lmk_gen()
543 static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_post() argument
553 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_lmk_post()
555 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset); in crypt_iv_lmk_post()
559 crypto_xor(dst + sg->offset, iv, cc->iv_size); in crypt_iv_lmk_post()
565 static void crypt_iv_tcw_dtr(struct crypt_config *cc) in crypt_iv_tcw_dtr() argument
567 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_dtr()
579 static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_tcw_ctr() argument
582 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_ctr()
584 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_tcw_ctr()
589 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { in crypt_iv_tcw_ctr()
601 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); in crypt_iv_tcw_ctr()
604 crypt_iv_tcw_dtr(cc); in crypt_iv_tcw_ctr()
612 static int crypt_iv_tcw_init(struct crypt_config *cc) in crypt_iv_tcw_init() argument
614 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_init()
615 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; in crypt_iv_tcw_init()
617 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); in crypt_iv_tcw_init()
618 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], in crypt_iv_tcw_init()
624 static int crypt_iv_tcw_wipe(struct crypt_config *cc) in crypt_iv_tcw_wipe() argument
626 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_wipe()
628 memset(tcw->iv_seed, 0, cc->iv_size); in crypt_iv_tcw_wipe()
634 static int crypt_iv_tcw_whitening(struct crypt_config *cc, in crypt_iv_tcw_whitening() argument
638 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_whitening()
672 static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_gen() argument
676 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_gen()
683 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_tcw_gen()
685 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset); in crypt_iv_tcw_gen()
691 if (cc->iv_size > 8) in crypt_iv_tcw_gen()
693 cc->iv_size - 8); in crypt_iv_tcw_gen()
698 static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_post() argument
709 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_tcw_post()
711 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset); in crypt_iv_tcw_post()
717 static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_random_gen() argument
721 get_random_bytes(iv, cc->iv_size); in crypt_iv_random_gen()
725 static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_eboiv_ctr() argument
728 if (crypt_integrity_aead(cc)) { in crypt_iv_eboiv_ctr()
733 if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) { in crypt_iv_eboiv_ctr()
742 static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_eboiv_gen() argument
751 req = skcipher_request_alloc(any_tfm(cc), GFP_NOIO); in crypt_iv_eboiv_gen()
755 memset(buf, 0, cc->iv_size); in crypt_iv_eboiv_gen()
756 *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size); in crypt_iv_eboiv_gen()
758 sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size); in crypt_iv_eboiv_gen()
759 sg_init_one(&dst, iv, cc->iv_size); in crypt_iv_eboiv_gen()
760 skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf); in crypt_iv_eboiv_gen()
768 static void crypt_iv_elephant_dtr(struct crypt_config *cc) in crypt_iv_elephant_dtr() argument
770 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_dtr()
776 static int crypt_iv_elephant_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_elephant_ctr() argument
779 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_ctr()
790 r = crypt_iv_eboiv_ctr(cc, ti, NULL); in crypt_iv_elephant_ctr()
792 crypt_iv_elephant_dtr(cc); in crypt_iv_elephant_ctr()
940 static int crypt_iv_elephant(struct crypt_config *cc, struct dm_crypt_request *dmreq) in crypt_iv_elephant() argument
942 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant()
958 *(__le64 *)es = cpu_to_le64(dmreq->iv_sector * cc->sector_size); in crypt_iv_elephant()
976 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_elephant()
982 sg2 = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_elephant()
984 memcpy(data_offset, data2 + sg2->offset, cc->sector_size); in crypt_iv_elephant()
989 diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
990 diffuser_b_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
991 diffuser_a_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
992 diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
995 for (i = 0; i < (cc->sector_size / 32); i++) in crypt_iv_elephant()
999 diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1000 diffuser_a_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1001 diffuser_b_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1002 diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1013 static int crypt_iv_elephant_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_elephant_gen() argument
1019 r = crypt_iv_elephant(cc, dmreq); in crypt_iv_elephant_gen()
1024 return crypt_iv_eboiv_gen(cc, iv, dmreq); in crypt_iv_elephant_gen()
1027 static int crypt_iv_elephant_post(struct crypt_config *cc, u8 *iv, in crypt_iv_elephant_post() argument
1031 return crypt_iv_elephant(cc, dmreq); in crypt_iv_elephant_post()
1036 static int crypt_iv_elephant_init(struct crypt_config *cc) in crypt_iv_elephant_init() argument
1038 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_init()
1039 int key_offset = cc->key_size - cc->key_extra_size; in crypt_iv_elephant_init()
1041 return crypto_skcipher_setkey(elephant->tfm, &cc->key[key_offset], cc->key_extra_size); in crypt_iv_elephant_init()
1044 static int crypt_iv_elephant_wipe(struct crypt_config *cc) in crypt_iv_elephant_wipe() argument
1046 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_wipe()
1049 memset(key, 0, cc->key_extra_size); in crypt_iv_elephant_wipe()
1050 return crypto_skcipher_setkey(elephant->tfm, key, cc->key_extra_size); in crypt_iv_elephant_wipe()
1118 static bool crypt_integrity_aead(struct crypt_config *cc) in crypt_integrity_aead() argument
1120 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_integrity_aead()
1123 static bool crypt_integrity_hmac(struct crypt_config *cc) in crypt_integrity_hmac() argument
1125 return crypt_integrity_aead(cc) && cc->key_mac_size; in crypt_integrity_hmac()
1129 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, in crypt_get_sg_data() argument
1132 if (unlikely(crypt_integrity_aead(cc))) in crypt_get_sg_data()
1144 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size) in dm_crypt_integrity_io_alloc()
1151 tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift); in dm_crypt_integrity_io_alloc()
1154 bip->bip_iter.bi_sector = io->cc->start + io->sector; in dm_crypt_integrity_io_alloc()
1164 static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti) in crypt_integrity_ctr() argument
1167 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk); in crypt_integrity_ctr()
1176 if (bi->tag_size != cc->on_disk_tag_size || in crypt_integrity_ctr()
1177 bi->tuple_size != cc->on_disk_tag_size) { in crypt_integrity_ctr()
1181 if (1 << bi->interval_exp != cc->sector_size) { in crypt_integrity_ctr()
1186 if (crypt_integrity_aead(cc)) { in crypt_integrity_ctr()
1187 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size; in crypt_integrity_ctr()
1189 cc->integrity_tag_size, cc->integrity_iv_size); in crypt_integrity_ctr()
1191 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) { in crypt_integrity_ctr()
1195 } else if (cc->integrity_iv_size) in crypt_integrity_ctr()
1197 cc->integrity_iv_size); in crypt_integrity_ctr()
1199 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) { in crypt_integrity_ctr()
1211 static void crypt_convert_init(struct crypt_config *cc, in crypt_convert_init() argument
1222 ctx->cc_sector = sector + cc->iv_offset; in crypt_convert_init()
1226 static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, in dmreq_of_req() argument
1229 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); in dmreq_of_req()
1232 static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq) in req_of_dmreq() argument
1234 return (void *)((char *)dmreq - cc->dmreq_start); in req_of_dmreq()
1237 static u8 *iv_of_dmreq(struct crypt_config *cc, in iv_of_dmreq() argument
1240 if (crypt_integrity_aead(cc)) in iv_of_dmreq()
1242 crypto_aead_alignmask(any_tfm_aead(cc)) + 1); in iv_of_dmreq()
1245 crypto_skcipher_alignmask(any_tfm(cc)) + 1); in iv_of_dmreq()
1248 static u8 *org_iv_of_dmreq(struct crypt_config *cc, in org_iv_of_dmreq() argument
1251 return iv_of_dmreq(cc, dmreq) + cc->iv_size; in org_iv_of_dmreq()
1254 static __le64 *org_sector_of_dmreq(struct crypt_config *cc, in org_sector_of_dmreq() argument
1257 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size; in org_sector_of_dmreq()
1261 static unsigned int *org_tag_of_dmreq(struct crypt_config *cc, in org_tag_of_dmreq() argument
1264 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + in org_tag_of_dmreq()
1265 cc->iv_size + sizeof(uint64_t); in org_tag_of_dmreq()
1269 static void *tag_from_dmreq(struct crypt_config *cc, in tag_from_dmreq() argument
1275 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) * in tag_from_dmreq()
1276 cc->on_disk_tag_size]; in tag_from_dmreq()
1279 static void *iv_tag_from_dmreq(struct crypt_config *cc, in iv_tag_from_dmreq() argument
1282 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size; in iv_tag_from_dmreq()
1285 static int crypt_convert_block_aead(struct crypt_config *cc, in crypt_convert_block_aead() argument
1297 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size); in crypt_convert_block_aead()
1300 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_aead()
1303 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_aead()
1305 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_aead()
1306 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_aead()
1309 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_aead()
1311 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1312 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_aead()
1314 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1315 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1316 tag = tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1317 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1326 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1327 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_aead()
1328 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1332 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1333 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_aead()
1334 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1336 if (cc->iv_gen_ops) { in crypt_convert_block_aead()
1338 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_aead()
1339 memcpy(org_iv, tag_iv, cc->iv_size); in crypt_convert_block_aead()
1341 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_aead()
1345 if (cc->integrity_iv_size) in crypt_convert_block_aead()
1346 memcpy(tag_iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1349 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1352 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size); in crypt_convert_block_aead()
1355 cc->sector_size, iv); in crypt_convert_block_aead()
1357 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size) in crypt_convert_block_aead()
1358 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0, in crypt_convert_block_aead()
1359 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size)); in crypt_convert_block_aead()
1362 cc->sector_size + cc->integrity_tag_size, iv); in crypt_convert_block_aead()
1375 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_aead()
1376 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_aead()
1378 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_aead()
1379 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_aead()
1384 static int crypt_convert_block_skcipher(struct crypt_config *cc, in crypt_convert_block_skcipher() argument
1398 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_skcipher()
1401 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_skcipher()
1403 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1404 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_skcipher()
1407 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_skcipher()
1409 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1410 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1411 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1413 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1414 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_skcipher()
1421 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_skcipher()
1424 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_skcipher()
1426 if (cc->iv_gen_ops) { in crypt_convert_block_skcipher()
1428 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_skcipher()
1429 memcpy(org_iv, tag_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1431 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1435 if (test_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1438 if (cc->integrity_iv_size) in crypt_convert_block_skcipher()
1439 memcpy(tag_iv, org_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1442 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_skcipher()
1445 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv); in crypt_convert_block_skcipher()
1452 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_skcipher()
1453 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1455 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_skcipher()
1456 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_skcipher()
1464 static int crypt_alloc_req_skcipher(struct crypt_config *cc, in crypt_alloc_req_skcipher() argument
1467 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1); in crypt_alloc_req_skcipher()
1470 ctx->r.req = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); in crypt_alloc_req_skcipher()
1475 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]); in crypt_alloc_req_skcipher()
1483 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req)); in crypt_alloc_req_skcipher()
1488 static int crypt_alloc_req_aead(struct crypt_config *cc, in crypt_alloc_req_aead() argument
1492 ctx->r.req_aead = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); in crypt_alloc_req_aead()
1497 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_req_aead()
1505 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead)); in crypt_alloc_req_aead()
1510 static int crypt_alloc_req(struct crypt_config *cc, in crypt_alloc_req() argument
1513 if (crypt_integrity_aead(cc)) in crypt_alloc_req()
1514 return crypt_alloc_req_aead(cc, ctx); in crypt_alloc_req()
1516 return crypt_alloc_req_skcipher(cc, ctx); in crypt_alloc_req()
1519 static void crypt_free_req_skcipher(struct crypt_config *cc, in crypt_free_req_skcipher() argument
1522 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_skcipher()
1525 mempool_free(req, &cc->req_pool); in crypt_free_req_skcipher()
1528 static void crypt_free_req_aead(struct crypt_config *cc, in crypt_free_req_aead() argument
1531 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_aead()
1534 mempool_free(req, &cc->req_pool); in crypt_free_req_aead()
1537 static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio) in crypt_free_req() argument
1539 if (crypt_integrity_aead(cc)) in crypt_free_req()
1540 crypt_free_req_aead(cc, req, base_bio); in crypt_free_req()
1542 crypt_free_req_skcipher(cc, req, base_bio); in crypt_free_req()
1548 static blk_status_t crypt_convert(struct crypt_config *cc, in crypt_convert() argument
1552 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT; in crypt_convert()
1565 r = crypt_alloc_req(cc, ctx); in crypt_convert()
1573 if (crypt_integrity_aead(cc)) in crypt_convert()
1574 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset); in crypt_convert()
1576 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset); in crypt_convert()
1642 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1663 struct crypt_config *cc = io->cc; in crypt_alloc_buffer() local
1672 mutex_lock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1674 clone = bio_alloc_bioset(cc->dev->bdev, nr_iovecs, io->base_bio->bi_opf, in crypt_alloc_buffer()
1675 GFP_NOIO, &cc->bs); in crypt_alloc_buffer()
1682 page = mempool_alloc(&cc->page_pool, gfp_mask); in crypt_alloc_buffer()
1684 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1699 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1705 mutex_unlock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1710 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) in crypt_free_buffer_pages() argument
1717 mempool_free(bv->bv_page, &cc->page_pool); in crypt_free_buffer_pages()
1721 static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, in crypt_io_init() argument
1724 io->cc = cc; in crypt_io_init()
1751 struct crypt_config *cc = io->cc; in crypt_dec_pending() local
1759 crypt_free_req(cc, io->ctx.r.req, base_bio); in crypt_dec_pending()
1762 mempool_free(io->integrity_metadata, &io->cc->tag_pool); in crypt_dec_pending()
1783 queue_work(cc->io_queue, &io->work); in crypt_dec_pending()
1806 struct crypt_config *cc = io->cc; in crypt_endio() local
1814 crypt_free_buffer_pages(cc, clone); in crypt_endio()
1834 struct crypt_config *cc = io->cc; in kcryptd_io_read() local
1843 clone = bio_alloc_clone(cc->dev->bdev, io->base_bio, gfp, &cc->bs); in kcryptd_io_read()
1851 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_io_read()
1875 struct crypt_config *cc = io->cc; in kcryptd_queue_read() local
1878 queue_work(cc->io_queue, &io->work); in kcryptd_queue_read()
1892 struct crypt_config *cc = data; in dmcrypt_write() local
1899 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1902 if (!RB_EMPTY_ROOT(&cc->write_tree)) in dmcrypt_write()
1907 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1917 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1921 write_tree = cc->write_tree; in dmcrypt_write()
1922 cc->write_tree = RB_ROOT; in dmcrypt_write()
1923 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1945 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_io_submit() local
1951 crypt_free_buffer_pages(cc, clone); in kcryptd_crypt_write_io_submit()
1960 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_crypt_write_io_submit()
1962 if ((likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) || in kcryptd_crypt_write_io_submit()
1963 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) { in kcryptd_crypt_write_io_submit()
1968 spin_lock_irqsave(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
1969 if (RB_EMPTY_ROOT(&cc->write_tree)) in kcryptd_crypt_write_io_submit()
1970 wake_up_process(cc->write_thread); in kcryptd_crypt_write_io_submit()
1971 rbp = &cc->write_tree.rb_node; in kcryptd_crypt_write_io_submit()
1982 rb_insert_color(&io->rb_node, &cc->write_tree); in kcryptd_crypt_write_io_submit()
1983 spin_unlock_irqrestore(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
1986 static bool kcryptd_crypt_write_inline(struct crypt_config *cc, in kcryptd_crypt_write_inline() argument
1990 if (!test_bit(DM_CRYPT_WRITE_INLINE, &cc->flags)) in kcryptd_crypt_write_inline()
2010 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_continue() local
2019 r = crypt_convert(cc, &io->ctx, true, false); in kcryptd_crypt_write_continue()
2023 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_crypt_write_continue()
2040 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_convert() local
2051 crypt_convert_init(cc, ctx, NULL, io->base_bio, sector); in kcryptd_crypt_write_convert()
2065 r = crypt_convert(cc, ctx, in kcryptd_crypt_write_convert()
2066 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_write_convert()
2074 queue_work(cc->crypt_queue, &io->work); in kcryptd_crypt_write_convert()
2080 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_crypt_write_convert()
2104 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_continue() local
2110 r = crypt_convert(cc, &io->ctx, true, false); in kcryptd_crypt_read_continue()
2122 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_convert() local
2127 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, in kcryptd_crypt_read_convert()
2130 r = crypt_convert(cc, &io->ctx, in kcryptd_crypt_read_convert()
2131 test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_read_convert()
2138 queue_work(cc->crypt_queue, &io->work); in kcryptd_crypt_read_convert()
2156 struct crypt_config *cc = io->cc; in kcryptd_async_done() local
2168 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) in kcryptd_async_done()
2169 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq); in kcryptd_async_done()
2172 sector_t s = le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)); in kcryptd_async_done()
2182 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); in kcryptd_async_done()
2196 if (kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_async_done()
2221 struct crypt_config *cc = io->cc; in kcryptd_queue_crypt() local
2223 if ((bio_data_dir(io->base_bio) == READ && test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) || in kcryptd_queue_crypt()
2224 (bio_data_dir(io->base_bio) == WRITE && test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags))) { in kcryptd_queue_crypt()
2241 queue_work(cc->crypt_queue, &io->work); in kcryptd_queue_crypt()
2244 static void crypt_free_tfms_aead(struct crypt_config *cc) in crypt_free_tfms_aead() argument
2246 if (!cc->cipher_tfm.tfms_aead) in crypt_free_tfms_aead()
2249 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_free_tfms_aead()
2250 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]); in crypt_free_tfms_aead()
2251 cc->cipher_tfm.tfms_aead[0] = NULL; in crypt_free_tfms_aead()
2254 kfree(cc->cipher_tfm.tfms_aead); in crypt_free_tfms_aead()
2255 cc->cipher_tfm.tfms_aead = NULL; in crypt_free_tfms_aead()
2258 static void crypt_free_tfms_skcipher(struct crypt_config *cc) in crypt_free_tfms_skcipher() argument
2262 if (!cc->cipher_tfm.tfms) in crypt_free_tfms_skcipher()
2265 for (i = 0; i < cc->tfms_count; i++) in crypt_free_tfms_skcipher()
2266 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_free_tfms_skcipher()
2267 crypto_free_skcipher(cc->cipher_tfm.tfms[i]); in crypt_free_tfms_skcipher()
2268 cc->cipher_tfm.tfms[i] = NULL; in crypt_free_tfms_skcipher()
2271 kfree(cc->cipher_tfm.tfms); in crypt_free_tfms_skcipher()
2272 cc->cipher_tfm.tfms = NULL; in crypt_free_tfms_skcipher()
2275 static void crypt_free_tfms(struct crypt_config *cc) in crypt_free_tfms() argument
2277 if (crypt_integrity_aead(cc)) in crypt_free_tfms()
2278 crypt_free_tfms_aead(cc); in crypt_free_tfms()
2280 crypt_free_tfms_skcipher(cc); in crypt_free_tfms()
2283 static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_skcipher() argument
2288 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count, in crypt_alloc_tfms_skcipher()
2291 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_skcipher()
2294 for (i = 0; i < cc->tfms_count; i++) { in crypt_alloc_tfms_skcipher()
2295 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, in crypt_alloc_tfms_skcipher()
2297 if (IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_alloc_tfms_skcipher()
2298 err = PTR_ERR(cc->cipher_tfm.tfms[i]); in crypt_alloc_tfms_skcipher()
2299 crypt_free_tfms(cc); in crypt_alloc_tfms_skcipher()
2310 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name); in crypt_alloc_tfms_skcipher()
2314 static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_aead() argument
2318 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL); in crypt_alloc_tfms_aead()
2319 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_aead()
2322 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, in crypt_alloc_tfms_aead()
2324 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_alloc_tfms_aead()
2325 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_tfms_aead()
2326 crypt_free_tfms(cc); in crypt_alloc_tfms_aead()
2331 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name); in crypt_alloc_tfms_aead()
2335 static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms() argument
2337 if (crypt_integrity_aead(cc)) in crypt_alloc_tfms()
2338 return crypt_alloc_tfms_aead(cc, ciphermode); in crypt_alloc_tfms()
2340 return crypt_alloc_tfms_skcipher(cc, ciphermode); in crypt_alloc_tfms()
2343 static unsigned crypt_subkey_size(struct crypt_config *cc) in crypt_subkey_size() argument
2345 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); in crypt_subkey_size()
2348 static unsigned crypt_authenckey_size(struct crypt_config *cc) in crypt_authenckey_size() argument
2350 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param)); in crypt_authenckey_size()
2356 * This funcion converts cc->key to this special format.
2375 static int crypt_setkey(struct crypt_config *cc) in crypt_setkey() argument
2381 subkey_size = crypt_subkey_size(cc); in crypt_setkey()
2383 if (crypt_integrity_hmac(cc)) { in crypt_setkey()
2384 if (subkey_size < cc->key_mac_size) in crypt_setkey()
2387 crypt_copy_authenckey(cc->authenc_key, cc->key, in crypt_setkey()
2388 subkey_size - cc->key_mac_size, in crypt_setkey()
2389 cc->key_mac_size); in crypt_setkey()
2392 for (i = 0; i < cc->tfms_count; i++) { in crypt_setkey()
2393 if (crypt_integrity_hmac(cc)) in crypt_setkey()
2394 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
2395 cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
2396 else if (crypt_integrity_aead(cc)) in crypt_setkey()
2397 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
2398 cc->key + (i * subkey_size), in crypt_setkey()
2401 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i], in crypt_setkey()
2402 cc->key + (i * subkey_size), in crypt_setkey()
2408 if (crypt_integrity_hmac(cc)) in crypt_setkey()
2409 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
2424 static int set_key_user(struct crypt_config *cc, struct key *key) in set_key_user() argument
2432 if (cc->key_size != ukp->datalen) in set_key_user()
2435 memcpy(cc->key, ukp->data, cc->key_size); in set_key_user()
2440 static int set_key_encrypted(struct crypt_config *cc, struct key *key) in set_key_encrypted() argument
2448 if (cc->key_size != ekp->decrypted_datalen) in set_key_encrypted()
2451 memcpy(cc->key, ekp->decrypted_data, cc->key_size); in set_key_encrypted()
2456 static int set_key_trusted(struct crypt_config *cc, struct key *key) in set_key_trusted() argument
2464 if (cc->key_size != tkp->key_len) in set_key_trusted()
2467 memcpy(cc->key, tkp->key, cc->key_size); in set_key_trusted()
2472 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2478 int (*set_key)(struct crypt_config *cc, struct key *key); in crypt_set_keyring_key()
2524 ret = set_key(cc, key); in crypt_set_keyring_key()
2536 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2538 ret = crypt_setkey(cc); in crypt_set_keyring_key()
2541 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2542 kfree_sensitive(cc->key_string); in crypt_set_keyring_key()
2543 cc->key_string = new_key_string; in crypt_set_keyring_key()
2575 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2587 static int crypt_set_key(struct crypt_config *cc, char *key) in crypt_set_key() argument
2593 if (!cc->key_size && strcmp(key, "-")) in crypt_set_key()
2598 r = crypt_set_keyring_key(cc, key + 1); in crypt_set_key()
2603 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2606 kfree_sensitive(cc->key_string); in crypt_set_key()
2607 cc->key_string = NULL; in crypt_set_key()
2610 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) in crypt_set_key()
2613 r = crypt_setkey(cc); in crypt_set_key()
2615 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2624 static int crypt_wipe_key(struct crypt_config *cc) in crypt_wipe_key() argument
2628 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_wipe_key()
2629 get_random_bytes(&cc->key, cc->key_size); in crypt_wipe_key()
2632 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { in crypt_wipe_key()
2633 r = cc->iv_gen_ops->wipe(cc); in crypt_wipe_key()
2638 kfree_sensitive(cc->key_string); in crypt_wipe_key()
2639 cc->key_string = NULL; in crypt_wipe_key()
2640 r = crypt_setkey(cc); in crypt_wipe_key()
2641 memset(&cc->key, 0, cc->key_size * sizeof(u8)); in crypt_wipe_key()
2661 struct crypt_config *cc = pool_data; in crypt_page_alloc() local
2669 if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) >= dm_crypt_pages_per_client) && in crypt_page_alloc()
2675 percpu_counter_add(&cc->n_allocated_pages, 1); in crypt_page_alloc()
2682 struct crypt_config *cc = pool_data; in crypt_page_free() local
2685 percpu_counter_sub(&cc->n_allocated_pages, 1); in crypt_page_free()
2690 struct crypt_config *cc = ti->private; in crypt_dtr() local
2694 if (!cc) in crypt_dtr()
2697 if (cc->write_thread) in crypt_dtr()
2698 kthread_stop(cc->write_thread); in crypt_dtr()
2700 if (cc->io_queue) in crypt_dtr()
2701 destroy_workqueue(cc->io_queue); in crypt_dtr()
2702 if (cc->crypt_queue) in crypt_dtr()
2703 destroy_workqueue(cc->crypt_queue); in crypt_dtr()
2705 crypt_free_tfms(cc); in crypt_dtr()
2707 bioset_exit(&cc->bs); in crypt_dtr()
2709 mempool_exit(&cc->page_pool); in crypt_dtr()
2710 mempool_exit(&cc->req_pool); in crypt_dtr()
2711 mempool_exit(&cc->tag_pool); in crypt_dtr()
2713 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0); in crypt_dtr()
2714 percpu_counter_destroy(&cc->n_allocated_pages); in crypt_dtr()
2716 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) in crypt_dtr()
2717 cc->iv_gen_ops->dtr(cc); in crypt_dtr()
2719 if (cc->dev) in crypt_dtr()
2720 dm_put_device(ti, cc->dev); in crypt_dtr()
2722 kfree_sensitive(cc->cipher_string); in crypt_dtr()
2723 kfree_sensitive(cc->key_string); in crypt_dtr()
2724 kfree_sensitive(cc->cipher_auth); in crypt_dtr()
2725 kfree_sensitive(cc->authenc_key); in crypt_dtr()
2727 mutex_destroy(&cc->bio_alloc_lock); in crypt_dtr()
2730 kfree_sensitive(cc); in crypt_dtr()
2743 struct crypt_config *cc = ti->private; in crypt_ctr_ivmode() local
2745 if (crypt_integrity_aead(cc)) in crypt_ctr_ivmode()
2746 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_ivmode()
2748 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_ivmode()
2750 if (cc->iv_size) in crypt_ctr_ivmode()
2752 cc->iv_size = max(cc->iv_size, in crypt_ctr_ivmode()
2761 cc->iv_gen_ops = NULL; in crypt_ctr_ivmode()
2763 cc->iv_gen_ops = &crypt_iv_plain_ops; in crypt_ctr_ivmode()
2765 cc->iv_gen_ops = &crypt_iv_plain64_ops; in crypt_ctr_ivmode()
2767 cc->iv_gen_ops = &crypt_iv_plain64be_ops; in crypt_ctr_ivmode()
2769 cc->iv_gen_ops = &crypt_iv_essiv_ops; in crypt_ctr_ivmode()
2771 cc->iv_gen_ops = &crypt_iv_benbi_ops; in crypt_ctr_ivmode()
2773 cc->iv_gen_ops = &crypt_iv_null_ops; in crypt_ctr_ivmode()
2775 cc->iv_gen_ops = &crypt_iv_eboiv_ops; in crypt_ctr_ivmode()
2777 cc->iv_gen_ops = &crypt_iv_elephant_ops; in crypt_ctr_ivmode()
2778 cc->key_parts = 2; in crypt_ctr_ivmode()
2779 cc->key_extra_size = cc->key_size / 2; in crypt_ctr_ivmode()
2780 if (cc->key_extra_size > ELEPHANT_MAX_KEY_SIZE) in crypt_ctr_ivmode()
2782 set_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags); in crypt_ctr_ivmode()
2784 cc->iv_gen_ops = &crypt_iv_lmk_ops; in crypt_ctr_ivmode()
2791 if (cc->key_size % cc->key_parts) { in crypt_ctr_ivmode()
2792 cc->key_parts++; in crypt_ctr_ivmode()
2793 cc->key_extra_size = cc->key_size / cc->key_parts; in crypt_ctr_ivmode()
2796 cc->iv_gen_ops = &crypt_iv_tcw_ops; in crypt_ctr_ivmode()
2797 cc->key_parts += 2; /* IV + whitening */ in crypt_ctr_ivmode()
2798 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; in crypt_ctr_ivmode()
2800 cc->iv_gen_ops = &crypt_iv_random_ops; in crypt_ctr_ivmode()
2802 cc->integrity_iv_size = cc->iv_size; in crypt_ctr_ivmode()
2816 static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) in crypt_ctr_auth_cipher() argument
2840 cc->key_mac_size = crypto_ahash_digestsize(mac); in crypt_ctr_auth_cipher()
2843 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL); in crypt_ctr_auth_cipher()
2844 if (!cc->authenc_key) in crypt_ctr_auth_cipher()
2853 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_new() local
2857 cc->tfms_count = 1; in crypt_ctr_cipher_new()
2881 if (crypt_integrity_aead(cc)) { in crypt_ctr_cipher_new()
2882 ret = crypt_ctr_auth_cipher(cc, cipher_api); in crypt_ctr_cipher_new()
2890 cc->tfms_count = 64; in crypt_ctr_cipher_new()
2906 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_new()
2909 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_new()
2915 if (crypt_integrity_aead(cc)) in crypt_ctr_cipher_new()
2916 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_cipher_new()
2918 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_cipher_new()
2926 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_old() local
2932 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) { in crypt_ctr_cipher_old()
2946 cc->tfms_count = 1; in crypt_ctr_cipher_old()
2947 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || in crypt_ctr_cipher_old()
2948 !is_power_of_2(cc->tfms_count)) { in crypt_ctr_cipher_old()
2952 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_old()
2994 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_old()
3010 struct crypt_config *cc = ti->private; in crypt_ctr_cipher() local
3014 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); in crypt_ctr_cipher()
3015 if (!cc->cipher_string) { in crypt_ctr_cipher()
3033 ret = crypt_set_key(cc, key); in crypt_ctr_cipher()
3040 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { in crypt_ctr_cipher()
3041 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); in crypt_ctr_cipher()
3049 if (cc->iv_gen_ops && cc->iv_gen_ops->init) { in crypt_ctr_cipher()
3050 ret = cc->iv_gen_ops->init(cc); in crypt_ctr_cipher()
3058 if (cc->key_string) in crypt_ctr_cipher()
3059 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_ctr_cipher()
3066 struct crypt_config *cc = ti->private; in crypt_ctr_optional() local
3095 set_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_ctr_optional()
3098 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_ctr_optional()
3100 set_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); in crypt_ctr_optional()
3102 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_ctr_optional()
3108 cc->on_disk_tag_size = val; in crypt_ctr_optional()
3111 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_ctr_optional()
3117 cc->cipher_auth = kstrdup(sval, GFP_KERNEL); in crypt_ctr_optional()
3118 if (!cc->cipher_auth) in crypt_ctr_optional()
3120 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { in crypt_ctr_optional()
3121 if (cc->sector_size < (1 << SECTOR_SHIFT) || in crypt_ctr_optional()
3122 cc->sector_size > 4096 || in crypt_ctr_optional()
3123 (cc->sector_size & (cc->sector_size - 1))) { in crypt_ctr_optional()
3127 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) { in crypt_ctr_optional()
3131 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT; in crypt_ctr_optional()
3133 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_ctr_optional()
3147 struct crypt_config *cc = ti->private; in crypt_report_zones() local
3149 return dm_report_zones(cc->dev->bdev, cc->start, in crypt_report_zones()
3150 cc->start + dm_target_offset(ti, args->next_sector), in crypt_report_zones()
3163 struct crypt_config *cc; in crypt_ctr() local
3183 cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL); in crypt_ctr()
3184 if (!cc) { in crypt_ctr()
3188 cc->key_size = key_size; in crypt_ctr()
3189 cc->sector_size = (1 << SECTOR_SHIFT); in crypt_ctr()
3190 cc->sector_shift = 0; in crypt_ctr()
3192 ti->private = cc; in crypt_ctr()
3199 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); in crypt_ctr()
3214 if (crypt_integrity_aead(cc)) { in crypt_ctr()
3215 cc->dmreq_start = sizeof(struct aead_request); in crypt_ctr()
3216 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc)); in crypt_ctr()
3217 align_mask = crypto_aead_alignmask(any_tfm_aead(cc)); in crypt_ctr()
3219 cc->dmreq_start = sizeof(struct skcipher_request); in crypt_ctr()
3220 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); in crypt_ctr()
3221 align_mask = crypto_skcipher_alignmask(any_tfm(cc)); in crypt_ctr()
3223 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); in crypt_ctr()
3227 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) in crypt_ctr()
3240 iv_size_padding + cc->iv_size + in crypt_ctr()
3241 cc->iv_size + in crypt_ctr()
3245 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size); in crypt_ctr()
3251 cc->per_bio_data_size = ti->per_io_data_size = in crypt_ctr()
3252 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size, in crypt_ctr()
3255 ret = mempool_init(&cc->page_pool, BIO_MAX_VECS, crypt_page_alloc, crypt_page_free, cc); in crypt_ctr()
3261 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS); in crypt_ctr()
3267 mutex_init(&cc->bio_alloc_lock); in crypt_ctr()
3271 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { in crypt_ctr()
3275 cc->iv_offset = tmpll; in crypt_ctr()
3277 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); in crypt_ctr()
3288 cc->start = tmpll; in crypt_ctr()
3290 if (bdev_is_zoned(cc->dev->bdev)) { in crypt_ctr()
3296 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_ctr()
3297 set_bit(DM_CRYPT_WRITE_INLINE, &cc->flags); in crypt_ctr()
3314 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) { in crypt_ctr()
3315 ret = crypt_integrity_ctr(cc, ti); in crypt_ctr()
3319 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size; in crypt_ctr()
3320 if (!cc->tag_pool_max_sectors) in crypt_ctr()
3321 cc->tag_pool_max_sectors = 1; in crypt_ctr()
3323 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS, in crypt_ctr()
3324 cc->tag_pool_max_sectors * cc->on_disk_tag_size); in crypt_ctr()
3330 cc->tag_pool_max_sectors <<= cc->sector_shift; in crypt_ctr()
3334 cc->io_queue = alloc_workqueue("kcryptd_io/%s", WQ_MEM_RECLAIM, 1, devname); in crypt_ctr()
3335 if (!cc->io_queue) { in crypt_ctr()
3340 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_ctr()
3341 cc->crypt_queue = alloc_workqueue("kcryptd/%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, in crypt_ctr()
3344 cc->crypt_queue = alloc_workqueue("kcryptd/%s", in crypt_ctr()
3347 if (!cc->crypt_queue) { in crypt_ctr()
3352 spin_lock_init(&cc->write_thread_lock); in crypt_ctr()
3353 cc->write_tree = RB_ROOT; in crypt_ctr()
3355 cc->write_thread = kthread_run(dmcrypt_write, cc, "dmcrypt_write/%s", devname); in crypt_ctr()
3356 if (IS_ERR(cc->write_thread)) { in crypt_ctr()
3357 ret = PTR_ERR(cc->write_thread); in crypt_ctr()
3358 cc->write_thread = NULL; in crypt_ctr()
3379 struct crypt_config *cc = ti->private; in crypt_map() local
3388 bio_set_dev(bio, cc->dev->bdev); in crypt_map()
3390 bio->bi_iter.bi_sector = cc->start + in crypt_map()
3399 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size)) in crypt_map()
3406 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) in crypt_map()
3409 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) in crypt_map()
3412 io = dm_per_bio_data(bio, cc->per_bio_data_size); in crypt_map()
3413 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); in crypt_map()
3415 if (cc->on_disk_tag_size) { in crypt_map()
3416 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift); in crypt_map()
3421 if (bio_sectors(bio) > cc->tag_pool_max_sectors) in crypt_map()
3422 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors); in crypt_map()
3423 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO); in crypt_map()
3428 if (crypt_integrity_aead(cc)) in crypt_map()
3450 struct crypt_config *cc = ti->private; in crypt_status() local
3460 DMEMIT("%s ", cc->cipher_string); in crypt_status()
3462 if (cc->key_size > 0) { in crypt_status()
3463 if (cc->key_string) in crypt_status()
3464 DMEMIT(":%u:%s", cc->key_size, cc->key_string); in crypt_status()
3466 for (i = 0; i < cc->key_size; i++) { in crypt_status()
3467 DMEMIT("%c%c", hex2asc(cc->key[i] >> 4), in crypt_status()
3468 hex2asc(cc->key[i] & 0xf)); in crypt_status()
3474 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, in crypt_status()
3475 cc->dev->name, (unsigned long long)cc->start); in crypt_status()
3478 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_status()
3479 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_status()
3480 num_feature_args += test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); in crypt_status()
3481 num_feature_args += test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_status()
3482 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT); in crypt_status()
3483 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_status()
3484 if (cc->on_disk_tag_size) in crypt_status()
3490 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_status()
3492 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) in crypt_status()
3494 if (test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) in crypt_status()
3496 if (test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) in crypt_status()
3498 if (cc->on_disk_tag_size) in crypt_status()
3499 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth); in crypt_status()
3500 if (cc->sector_size != (1 << SECTOR_SHIFT)) in crypt_status()
3501 DMEMIT(" sector_size:%d", cc->sector_size); in crypt_status()
3502 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_status()
3510 DMEMIT(",same_cpu_crypt=%c", test_bit(DM_CRYPT_SAME_CPU, &cc->flags) ? 'y' : 'n'); in crypt_status()
3511 DMEMIT(",submit_from_crypt_cpus=%c", test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags) ? in crypt_status()
3513 DMEMIT(",no_read_workqueue=%c", test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags) ? in crypt_status()
3515 DMEMIT(",no_write_workqueue=%c", test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags) ? in crypt_status()
3517 DMEMIT(",iv_large_sectors=%c", test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags) ? in crypt_status()
3520 if (cc->on_disk_tag_size) in crypt_status()
3522 cc->on_disk_tag_size, cc->cipher_auth); in crypt_status()
3523 if (cc->sector_size != (1 << SECTOR_SHIFT)) in crypt_status()
3524 DMEMIT(",sector_size=%d", cc->sector_size); in crypt_status()
3525 if (cc->cipher_string) in crypt_status()
3526 DMEMIT(",cipher_string=%s", cc->cipher_string); in crypt_status()
3528 DMEMIT(",key_size=%u", cc->key_size); in crypt_status()
3529 DMEMIT(",key_parts=%u", cc->key_parts); in crypt_status()
3530 DMEMIT(",key_extra_size=%u", cc->key_extra_size); in crypt_status()
3531 DMEMIT(",key_mac_size=%u", cc->key_mac_size); in crypt_status()
3539 struct crypt_config *cc = ti->private; in crypt_postsuspend() local
3541 set_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_postsuspend()
3546 struct crypt_config *cc = ti->private; in crypt_preresume() local
3548 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { in crypt_preresume()
3558 struct crypt_config *cc = ti->private; in crypt_resume() local
3560 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_resume()
3570 struct crypt_config *cc = ti->private; in crypt_message() local
3577 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { in crypt_message()
3584 if (key_size < 0 || cc->key_size != key_size) { in crypt_message()
3589 ret = crypt_set_key(cc, argv[2]); in crypt_message()
3592 if (cc->iv_gen_ops && cc->iv_gen_ops->init) in crypt_message()
3593 ret = cc->iv_gen_ops->init(cc); in crypt_message()
3595 if (cc->key_string) in crypt_message()
3596 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_message()
3600 return crypt_wipe_key(cc); in crypt_message()
3611 struct crypt_config *cc = ti->private; in crypt_iterate_devices() local
3613 return fn(ti, cc->dev, cc->start, ti->len, data); in crypt_iterate_devices()
3618 struct crypt_config *cc = ti->private; in crypt_io_hints() local
3629 max_t(unsigned, limits->logical_block_size, cc->sector_size); in crypt_io_hints()
3631 max_t(unsigned, limits->physical_block_size, cc->sector_size); in crypt_io_hints()
3632 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size); in crypt_io_hints()