Lines Matching refs:cc
67 struct crypt_config *cc; member
93 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
95 void (*dtr)(struct crypt_config *cc);
96 int (*init)(struct crypt_config *cc);
97 int (*wipe)(struct crypt_config *cc);
98 int (*generator)(struct crypt_config *cc, u8 *iv,
100 int (*post)(struct crypt_config *cc, u8 *iv,
235 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
238 static bool crypt_integrity_aead(struct crypt_config *cc);
243 static struct crypto_skcipher *any_tfm(struct crypt_config *cc) in any_tfm() argument
245 return cc->cipher_tfm.tfms[0]; in any_tfm()
248 static struct crypto_aead *any_tfm_aead(struct crypt_config *cc) in any_tfm_aead() argument
250 return cc->cipher_tfm.tfms_aead[0]; in any_tfm_aead()
309 static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain_gen() argument
312 memset(iv, 0, cc->iv_size); in crypt_iv_plain_gen()
318 static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64_gen() argument
321 memset(iv, 0, cc->iv_size); in crypt_iv_plain64_gen()
327 static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64be_gen() argument
330 memset(iv, 0, cc->iv_size); in crypt_iv_plain64be_gen()
332 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector); in crypt_iv_plain64be_gen()
337 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_essiv_gen() argument
344 memset(iv, 0, cc->iv_size); in crypt_iv_essiv_gen()
350 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_benbi_ctr() argument
356 if (crypt_integrity_aead(cc)) in crypt_iv_benbi_ctr()
357 bs = crypto_aead_blocksize(any_tfm_aead(cc)); in crypt_iv_benbi_ctr()
359 bs = crypto_skcipher_blocksize(any_tfm(cc)); in crypt_iv_benbi_ctr()
375 cc->iv_gen_private.benbi.shift = 9 - log; in crypt_iv_benbi_ctr()
380 static void crypt_iv_benbi_dtr(struct crypt_config *cc) in crypt_iv_benbi_dtr() argument
384 static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_benbi_gen() argument
389 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ in crypt_iv_benbi_gen()
391 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); in crypt_iv_benbi_gen()
392 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); in crypt_iv_benbi_gen()
397 static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_null_gen() argument
400 memset(iv, 0, cc->iv_size); in crypt_iv_null_gen()
405 static void crypt_iv_lmk_dtr(struct crypt_config *cc) in crypt_iv_lmk_dtr() argument
407 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_dtr()
417 static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_lmk_ctr() argument
420 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_ctr()
422 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_lmk_ctr()
435 if (cc->key_parts == cc->tfms_count) { in crypt_iv_lmk_ctr()
442 crypt_iv_lmk_dtr(cc); in crypt_iv_lmk_ctr()
450 static int crypt_iv_lmk_init(struct crypt_config *cc) in crypt_iv_lmk_init() argument
452 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_init()
453 int subkey_size = cc->key_size / cc->key_parts; in crypt_iv_lmk_init()
457 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), in crypt_iv_lmk_init()
463 static int crypt_iv_lmk_wipe(struct crypt_config *cc) in crypt_iv_lmk_wipe() argument
465 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_wipe()
473 static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_one() argument
477 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_one()
516 memcpy(iv, &md5state.hash, cc->iv_size); in crypt_iv_lmk_one()
521 static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_gen() argument
529 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_lmk_gen()
531 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset); in crypt_iv_lmk_gen()
534 memset(iv, 0, cc->iv_size); in crypt_iv_lmk_gen()
539 static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_post() argument
549 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_lmk_post()
551 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset); in crypt_iv_lmk_post()
555 crypto_xor(dst + sg->offset, iv, cc->iv_size); in crypt_iv_lmk_post()
561 static void crypt_iv_tcw_dtr(struct crypt_config *cc) in crypt_iv_tcw_dtr() argument
563 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_dtr()
575 static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_tcw_ctr() argument
578 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_ctr()
580 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_tcw_ctr()
585 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { in crypt_iv_tcw_ctr()
597 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); in crypt_iv_tcw_ctr()
600 crypt_iv_tcw_dtr(cc); in crypt_iv_tcw_ctr()
608 static int crypt_iv_tcw_init(struct crypt_config *cc) in crypt_iv_tcw_init() argument
610 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_init()
611 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; in crypt_iv_tcw_init()
613 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); in crypt_iv_tcw_init()
614 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], in crypt_iv_tcw_init()
620 static int crypt_iv_tcw_wipe(struct crypt_config *cc) in crypt_iv_tcw_wipe() argument
622 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_wipe()
624 memset(tcw->iv_seed, 0, cc->iv_size); in crypt_iv_tcw_wipe()
630 static int crypt_iv_tcw_whitening(struct crypt_config *cc, in crypt_iv_tcw_whitening() argument
634 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_whitening()
668 static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_gen() argument
672 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_gen()
679 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_tcw_gen()
681 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset); in crypt_iv_tcw_gen()
687 if (cc->iv_size > 8) in crypt_iv_tcw_gen()
689 cc->iv_size - 8); in crypt_iv_tcw_gen()
694 static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_post() argument
705 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_tcw_post()
707 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset); in crypt_iv_tcw_post()
713 static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_random_gen() argument
717 get_random_bytes(iv, cc->iv_size); in crypt_iv_random_gen()
721 static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_eboiv_ctr() argument
724 if (crypt_integrity_aead(cc)) { in crypt_iv_eboiv_ctr()
729 if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) { in crypt_iv_eboiv_ctr()
738 static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_eboiv_gen() argument
747 req = skcipher_request_alloc(any_tfm(cc), GFP_NOIO); in crypt_iv_eboiv_gen()
751 memset(buf, 0, cc->iv_size); in crypt_iv_eboiv_gen()
752 *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size); in crypt_iv_eboiv_gen()
754 sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size); in crypt_iv_eboiv_gen()
755 sg_init_one(&dst, iv, cc->iv_size); in crypt_iv_eboiv_gen()
756 skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf); in crypt_iv_eboiv_gen()
764 static void crypt_iv_elephant_dtr(struct crypt_config *cc) in crypt_iv_elephant_dtr() argument
766 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_dtr()
772 static int crypt_iv_elephant_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_elephant_ctr() argument
775 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_ctr()
786 r = crypt_iv_eboiv_ctr(cc, ti, NULL); in crypt_iv_elephant_ctr()
788 crypt_iv_elephant_dtr(cc); in crypt_iv_elephant_ctr()
936 static int crypt_iv_elephant(struct crypt_config *cc, struct dm_crypt_request *dmreq) in crypt_iv_elephant() argument
938 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant()
954 *(__le64 *)es = cpu_to_le64(dmreq->iv_sector * cc->sector_size); in crypt_iv_elephant()
972 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_elephant()
978 sg2 = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_elephant()
980 memcpy(data_offset, data2 + sg2->offset, cc->sector_size); in crypt_iv_elephant()
985 diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
986 diffuser_b_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
987 diffuser_a_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
988 diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
991 for (i = 0; i < (cc->sector_size / 32); i++) in crypt_iv_elephant()
995 diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
996 diffuser_a_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
997 diffuser_b_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
998 diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1009 static int crypt_iv_elephant_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_elephant_gen() argument
1015 r = crypt_iv_elephant(cc, dmreq); in crypt_iv_elephant_gen()
1020 return crypt_iv_eboiv_gen(cc, iv, dmreq); in crypt_iv_elephant_gen()
1023 static int crypt_iv_elephant_post(struct crypt_config *cc, u8 *iv, in crypt_iv_elephant_post() argument
1027 return crypt_iv_elephant(cc, dmreq); in crypt_iv_elephant_post()
1032 static int crypt_iv_elephant_init(struct crypt_config *cc) in crypt_iv_elephant_init() argument
1034 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_init()
1035 int key_offset = cc->key_size - cc->key_extra_size; in crypt_iv_elephant_init()
1037 return crypto_skcipher_setkey(elephant->tfm, &cc->key[key_offset], cc->key_extra_size); in crypt_iv_elephant_init()
1040 static int crypt_iv_elephant_wipe(struct crypt_config *cc) in crypt_iv_elephant_wipe() argument
1042 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_wipe()
1045 memset(key, 0, cc->key_extra_size); in crypt_iv_elephant_wipe()
1046 return crypto_skcipher_setkey(elephant->tfm, key, cc->key_extra_size); in crypt_iv_elephant_wipe()
1114 static bool crypt_integrity_aead(struct crypt_config *cc) in crypt_integrity_aead() argument
1116 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_integrity_aead()
1119 static bool crypt_integrity_hmac(struct crypt_config *cc) in crypt_integrity_hmac() argument
1121 return crypt_integrity_aead(cc) && cc->key_mac_size; in crypt_integrity_hmac()
1125 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, in crypt_get_sg_data() argument
1128 if (unlikely(crypt_integrity_aead(cc))) in crypt_get_sg_data()
1140 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size) in dm_crypt_integrity_io_alloc()
1147 tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift); in dm_crypt_integrity_io_alloc()
1150 bip->bip_iter.bi_sector = io->cc->start + io->sector; in dm_crypt_integrity_io_alloc()
1160 static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti) in crypt_integrity_ctr() argument
1163 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk); in crypt_integrity_ctr()
1172 if (bi->tag_size != cc->on_disk_tag_size || in crypt_integrity_ctr()
1173 bi->tuple_size != cc->on_disk_tag_size) { in crypt_integrity_ctr()
1177 if (1 << bi->interval_exp != cc->sector_size) { in crypt_integrity_ctr()
1182 if (crypt_integrity_aead(cc)) { in crypt_integrity_ctr()
1183 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size; in crypt_integrity_ctr()
1185 cc->integrity_tag_size, cc->integrity_iv_size); in crypt_integrity_ctr()
1187 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) { in crypt_integrity_ctr()
1191 } else if (cc->integrity_iv_size) in crypt_integrity_ctr()
1193 cc->integrity_iv_size); in crypt_integrity_ctr()
1195 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) { in crypt_integrity_ctr()
1207 static void crypt_convert_init(struct crypt_config *cc, in crypt_convert_init() argument
1218 ctx->cc_sector = sector + cc->iv_offset; in crypt_convert_init()
1222 static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, in dmreq_of_req() argument
1225 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); in dmreq_of_req()
1228 static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq) in req_of_dmreq() argument
1230 return (void *)((char *)dmreq - cc->dmreq_start); in req_of_dmreq()
1233 static u8 *iv_of_dmreq(struct crypt_config *cc, in iv_of_dmreq() argument
1236 if (crypt_integrity_aead(cc)) in iv_of_dmreq()
1238 crypto_aead_alignmask(any_tfm_aead(cc)) + 1); in iv_of_dmreq()
1241 crypto_skcipher_alignmask(any_tfm(cc)) + 1); in iv_of_dmreq()
1244 static u8 *org_iv_of_dmreq(struct crypt_config *cc, in org_iv_of_dmreq() argument
1247 return iv_of_dmreq(cc, dmreq) + cc->iv_size; in org_iv_of_dmreq()
1250 static __le64 *org_sector_of_dmreq(struct crypt_config *cc, in org_sector_of_dmreq() argument
1253 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size; in org_sector_of_dmreq()
1257 static unsigned int *org_tag_of_dmreq(struct crypt_config *cc, in org_tag_of_dmreq() argument
1260 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + in org_tag_of_dmreq()
1261 cc->iv_size + sizeof(uint64_t); in org_tag_of_dmreq()
1265 static void *tag_from_dmreq(struct crypt_config *cc, in tag_from_dmreq() argument
1271 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) * in tag_from_dmreq()
1272 cc->on_disk_tag_size]; in tag_from_dmreq()
1275 static void *iv_tag_from_dmreq(struct crypt_config *cc, in iv_tag_from_dmreq() argument
1278 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size; in iv_tag_from_dmreq()
1281 static int crypt_convert_block_aead(struct crypt_config *cc, in crypt_convert_block_aead() argument
1293 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size); in crypt_convert_block_aead()
1296 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_aead()
1299 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_aead()
1301 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_aead()
1302 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_aead()
1305 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_aead()
1307 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1308 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_aead()
1310 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1311 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1312 tag = tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1313 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1322 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1323 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_aead()
1324 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1328 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1329 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_aead()
1330 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1332 if (cc->iv_gen_ops) { in crypt_convert_block_aead()
1334 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_aead()
1335 memcpy(org_iv, tag_iv, cc->iv_size); in crypt_convert_block_aead()
1337 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_aead()
1341 if (cc->integrity_iv_size) in crypt_convert_block_aead()
1342 memcpy(tag_iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1345 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1348 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size); in crypt_convert_block_aead()
1351 cc->sector_size, iv); in crypt_convert_block_aead()
1353 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size) in crypt_convert_block_aead()
1354 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0, in crypt_convert_block_aead()
1355 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size)); in crypt_convert_block_aead()
1358 cc->sector_size + cc->integrity_tag_size, iv); in crypt_convert_block_aead()
1368 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_aead()
1369 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_aead()
1371 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_aead()
1372 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_aead()
1377 static int crypt_convert_block_skcipher(struct crypt_config *cc, in crypt_convert_block_skcipher() argument
1391 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_skcipher()
1394 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_skcipher()
1396 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1397 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_skcipher()
1400 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_skcipher()
1402 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1403 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1404 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1406 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1407 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_skcipher()
1414 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_skcipher()
1417 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_skcipher()
1419 if (cc->iv_gen_ops) { in crypt_convert_block_skcipher()
1421 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_skcipher()
1422 memcpy(org_iv, tag_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1424 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1428 if (test_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1431 if (cc->integrity_iv_size) in crypt_convert_block_skcipher()
1432 memcpy(tag_iv, org_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1435 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_skcipher()
1438 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv); in crypt_convert_block_skcipher()
1445 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_skcipher()
1446 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1448 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_skcipher()
1449 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_skcipher()
1457 static int crypt_alloc_req_skcipher(struct crypt_config *cc, in crypt_alloc_req_skcipher() argument
1460 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1); in crypt_alloc_req_skcipher()
1463 ctx->r.req = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); in crypt_alloc_req_skcipher()
1468 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]); in crypt_alloc_req_skcipher()
1476 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req)); in crypt_alloc_req_skcipher()
1481 static int crypt_alloc_req_aead(struct crypt_config *cc, in crypt_alloc_req_aead() argument
1485 ctx->r.req_aead = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); in crypt_alloc_req_aead()
1490 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_req_aead()
1498 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead)); in crypt_alloc_req_aead()
1503 static int crypt_alloc_req(struct crypt_config *cc, in crypt_alloc_req() argument
1506 if (crypt_integrity_aead(cc)) in crypt_alloc_req()
1507 return crypt_alloc_req_aead(cc, ctx); in crypt_alloc_req()
1509 return crypt_alloc_req_skcipher(cc, ctx); in crypt_alloc_req()
1512 static void crypt_free_req_skcipher(struct crypt_config *cc, in crypt_free_req_skcipher() argument
1515 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_skcipher()
1518 mempool_free(req, &cc->req_pool); in crypt_free_req_skcipher()
1521 static void crypt_free_req_aead(struct crypt_config *cc, in crypt_free_req_aead() argument
1524 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_aead()
1527 mempool_free(req, &cc->req_pool); in crypt_free_req_aead()
1530 static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio) in crypt_free_req() argument
1532 if (crypt_integrity_aead(cc)) in crypt_free_req()
1533 crypt_free_req_aead(cc, req, base_bio); in crypt_free_req()
1535 crypt_free_req_skcipher(cc, req, base_bio); in crypt_free_req()
1541 static blk_status_t crypt_convert(struct crypt_config *cc, in crypt_convert() argument
1545 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT; in crypt_convert()
1558 r = crypt_alloc_req(cc, ctx); in crypt_convert()
1566 if (crypt_integrity_aead(cc)) in crypt_convert()
1567 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset); in crypt_convert()
1569 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset); in crypt_convert()
1635 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1656 struct crypt_config *cc = io->cc; in crypt_alloc_buffer() local
1665 mutex_lock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1667 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs); in crypt_alloc_buffer()
1676 page = mempool_alloc(&cc->page_pool, gfp_mask); in crypt_alloc_buffer()
1678 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1693 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1699 mutex_unlock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1704 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) in crypt_free_buffer_pages() argument
1711 mempool_free(bv->bv_page, &cc->page_pool); in crypt_free_buffer_pages()
1715 static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, in crypt_io_init() argument
1718 io->cc = cc; in crypt_io_init()
1745 struct crypt_config *cc = io->cc; in crypt_dec_pending() local
1753 crypt_free_req(cc, io->ctx.r.req, base_bio); in crypt_dec_pending()
1756 mempool_free(io->integrity_metadata, &io->cc->tag_pool); in crypt_dec_pending()
1777 queue_work(cc->io_queue, &io->work); in crypt_dec_pending()
1800 struct crypt_config *cc = io->cc; in crypt_endio() local
1808 crypt_free_buffer_pages(cc, clone); in crypt_endio()
1826 struct crypt_config *cc = io->cc; in clone_init() local
1830 bio_set_dev(clone, cc->dev->bdev); in clone_init()
1836 struct crypt_config *cc = io->cc; in kcryptd_io_read() local
1845 clone = bio_clone_fast(io->base_bio, gfp, &cc->bs); in kcryptd_io_read()
1852 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_io_read()
1876 struct crypt_config *cc = io->cc; in kcryptd_queue_read() local
1879 queue_work(cc->io_queue, &io->work); in kcryptd_queue_read()
1893 struct crypt_config *cc = data; in dmcrypt_write() local
1900 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1903 if (!RB_EMPTY_ROOT(&cc->write_tree)) in dmcrypt_write()
1908 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1918 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1922 write_tree = cc->write_tree; in dmcrypt_write()
1923 cc->write_tree = RB_ROOT; in dmcrypt_write()
1924 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1946 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_io_submit() local
1952 crypt_free_buffer_pages(cc, clone); in kcryptd_crypt_write_io_submit()
1961 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_crypt_write_io_submit()
1963 if ((likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) || in kcryptd_crypt_write_io_submit()
1964 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) { in kcryptd_crypt_write_io_submit()
1969 spin_lock_irqsave(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
1970 if (RB_EMPTY_ROOT(&cc->write_tree)) in kcryptd_crypt_write_io_submit()
1971 wake_up_process(cc->write_thread); in kcryptd_crypt_write_io_submit()
1972 rbp = &cc->write_tree.rb_node; in kcryptd_crypt_write_io_submit()
1983 rb_insert_color(&io->rb_node, &cc->write_tree); in kcryptd_crypt_write_io_submit()
1984 spin_unlock_irqrestore(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
1987 static bool kcryptd_crypt_write_inline(struct crypt_config *cc, in kcryptd_crypt_write_inline() argument
1991 if (!test_bit(DM_CRYPT_WRITE_INLINE, &cc->flags)) in kcryptd_crypt_write_inline()
2012 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_continue() local
2021 r = crypt_convert(cc, &io->ctx, true, false); in kcryptd_crypt_write_continue()
2025 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_crypt_write_continue()
2042 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_convert() local
2053 crypt_convert_init(cc, ctx, NULL, io->base_bio, sector); in kcryptd_crypt_write_convert()
2067 r = crypt_convert(cc, ctx, in kcryptd_crypt_write_convert()
2068 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_write_convert()
2076 queue_work(cc->crypt_queue, &io->work); in kcryptd_crypt_write_convert()
2082 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_crypt_write_convert()
2106 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_continue() local
2112 r = crypt_convert(cc, &io->ctx, true, false); in kcryptd_crypt_read_continue()
2124 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_convert() local
2129 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, in kcryptd_crypt_read_convert()
2132 r = crypt_convert(cc, &io->ctx, in kcryptd_crypt_read_convert()
2133 test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_read_convert()
2140 queue_work(cc->crypt_queue, &io->work); in kcryptd_crypt_read_convert()
2158 struct crypt_config *cc = io->cc; in kcryptd_async_done() local
2170 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) in kcryptd_async_done()
2171 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq); in kcryptd_async_done()
2176 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq))); in kcryptd_async_done()
2181 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); in kcryptd_async_done()
2195 if (kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_async_done()
2220 struct crypt_config *cc = io->cc; in kcryptd_queue_crypt() local
2222 if ((bio_data_dir(io->base_bio) == READ && test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) || in kcryptd_queue_crypt()
2223 (bio_data_dir(io->base_bio) == WRITE && test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags))) { in kcryptd_queue_crypt()
2240 queue_work(cc->crypt_queue, &io->work); in kcryptd_queue_crypt()
2243 static void crypt_free_tfms_aead(struct crypt_config *cc) in crypt_free_tfms_aead() argument
2245 if (!cc->cipher_tfm.tfms_aead) in crypt_free_tfms_aead()
2248 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_free_tfms_aead()
2249 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]); in crypt_free_tfms_aead()
2250 cc->cipher_tfm.tfms_aead[0] = NULL; in crypt_free_tfms_aead()
2253 kfree(cc->cipher_tfm.tfms_aead); in crypt_free_tfms_aead()
2254 cc->cipher_tfm.tfms_aead = NULL; in crypt_free_tfms_aead()
2257 static void crypt_free_tfms_skcipher(struct crypt_config *cc) in crypt_free_tfms_skcipher() argument
2261 if (!cc->cipher_tfm.tfms) in crypt_free_tfms_skcipher()
2264 for (i = 0; i < cc->tfms_count; i++) in crypt_free_tfms_skcipher()
2265 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_free_tfms_skcipher()
2266 crypto_free_skcipher(cc->cipher_tfm.tfms[i]); in crypt_free_tfms_skcipher()
2267 cc->cipher_tfm.tfms[i] = NULL; in crypt_free_tfms_skcipher()
2270 kfree(cc->cipher_tfm.tfms); in crypt_free_tfms_skcipher()
2271 cc->cipher_tfm.tfms = NULL; in crypt_free_tfms_skcipher()
2274 static void crypt_free_tfms(struct crypt_config *cc) in crypt_free_tfms() argument
2276 if (crypt_integrity_aead(cc)) in crypt_free_tfms()
2277 crypt_free_tfms_aead(cc); in crypt_free_tfms()
2279 crypt_free_tfms_skcipher(cc); in crypt_free_tfms()
2282 static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_skcipher() argument
2287 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count, in crypt_alloc_tfms_skcipher()
2290 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_skcipher()
2293 for (i = 0; i < cc->tfms_count; i++) { in crypt_alloc_tfms_skcipher()
2294 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, in crypt_alloc_tfms_skcipher()
2296 if (IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_alloc_tfms_skcipher()
2297 err = PTR_ERR(cc->cipher_tfm.tfms[i]); in crypt_alloc_tfms_skcipher()
2298 crypt_free_tfms(cc); in crypt_alloc_tfms_skcipher()
2309 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name); in crypt_alloc_tfms_skcipher()
2313 static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_aead() argument
2317 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL); in crypt_alloc_tfms_aead()
2318 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_aead()
2321 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, in crypt_alloc_tfms_aead()
2323 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_alloc_tfms_aead()
2324 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_tfms_aead()
2325 crypt_free_tfms(cc); in crypt_alloc_tfms_aead()
2330 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name); in crypt_alloc_tfms_aead()
2334 static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms() argument
2336 if (crypt_integrity_aead(cc)) in crypt_alloc_tfms()
2337 return crypt_alloc_tfms_aead(cc, ciphermode); in crypt_alloc_tfms()
2339 return crypt_alloc_tfms_skcipher(cc, ciphermode); in crypt_alloc_tfms()
2342 static unsigned crypt_subkey_size(struct crypt_config *cc) in crypt_subkey_size() argument
2344 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); in crypt_subkey_size()
2347 static unsigned crypt_authenckey_size(struct crypt_config *cc) in crypt_authenckey_size() argument
2349 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param)); in crypt_authenckey_size()
2374 static int crypt_setkey(struct crypt_config *cc) in crypt_setkey() argument
2380 subkey_size = crypt_subkey_size(cc); in crypt_setkey()
2382 if (crypt_integrity_hmac(cc)) { in crypt_setkey()
2383 if (subkey_size < cc->key_mac_size) in crypt_setkey()
2386 crypt_copy_authenckey(cc->authenc_key, cc->key, in crypt_setkey()
2387 subkey_size - cc->key_mac_size, in crypt_setkey()
2388 cc->key_mac_size); in crypt_setkey()
2391 for (i = 0; i < cc->tfms_count; i++) { in crypt_setkey()
2392 if (crypt_integrity_hmac(cc)) in crypt_setkey()
2393 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
2394 cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
2395 else if (crypt_integrity_aead(cc)) in crypt_setkey()
2396 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
2397 cc->key + (i * subkey_size), in crypt_setkey()
2400 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i], in crypt_setkey()
2401 cc->key + (i * subkey_size), in crypt_setkey()
2407 if (crypt_integrity_hmac(cc)) in crypt_setkey()
2408 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
2423 static int set_key_user(struct crypt_config *cc, struct key *key) in set_key_user() argument
2431 if (cc->key_size != ukp->datalen) in set_key_user()
2434 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()
2457 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2463 int (*set_key)(struct crypt_config *cc, struct key *key); in crypt_set_keyring_key()
2506 ret = set_key(cc, key); in crypt_set_keyring_key()
2518 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2520 ret = crypt_setkey(cc); in crypt_set_keyring_key()
2523 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2524 kfree_sensitive(cc->key_string); in crypt_set_keyring_key()
2525 cc->key_string = new_key_string; in crypt_set_keyring_key()
2557 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2569 static int crypt_set_key(struct crypt_config *cc, char *key) in crypt_set_key() argument
2575 if (!cc->key_size && strcmp(key, "-")) in crypt_set_key()
2580 r = crypt_set_keyring_key(cc, key + 1); in crypt_set_key()
2585 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2588 kfree_sensitive(cc->key_string); in crypt_set_key()
2589 cc->key_string = NULL; in crypt_set_key()
2592 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) in crypt_set_key()
2595 r = crypt_setkey(cc); in crypt_set_key()
2597 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2606 static int crypt_wipe_key(struct crypt_config *cc) in crypt_wipe_key() argument
2610 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_wipe_key()
2611 get_random_bytes(&cc->key, cc->key_size); in crypt_wipe_key()
2614 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { in crypt_wipe_key()
2615 r = cc->iv_gen_ops->wipe(cc); in crypt_wipe_key()
2620 kfree_sensitive(cc->key_string); in crypt_wipe_key()
2621 cc->key_string = NULL; in crypt_wipe_key()
2622 r = crypt_setkey(cc); in crypt_wipe_key()
2623 memset(&cc->key, 0, cc->key_size * sizeof(u8)); in crypt_wipe_key()
2643 struct crypt_config *cc = pool_data; in crypt_page_alloc() local
2651 if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) >= dm_crypt_pages_per_client) && in crypt_page_alloc()
2657 percpu_counter_add(&cc->n_allocated_pages, 1); in crypt_page_alloc()
2664 struct crypt_config *cc = pool_data; in crypt_page_free() local
2667 percpu_counter_sub(&cc->n_allocated_pages, 1); in crypt_page_free()
2672 struct crypt_config *cc = ti->private; in crypt_dtr() local
2676 if (!cc) in crypt_dtr()
2679 if (cc->write_thread) in crypt_dtr()
2680 kthread_stop(cc->write_thread); in crypt_dtr()
2682 if (cc->io_queue) in crypt_dtr()
2683 destroy_workqueue(cc->io_queue); in crypt_dtr()
2684 if (cc->crypt_queue) in crypt_dtr()
2685 destroy_workqueue(cc->crypt_queue); in crypt_dtr()
2687 crypt_free_tfms(cc); in crypt_dtr()
2689 bioset_exit(&cc->bs); in crypt_dtr()
2691 mempool_exit(&cc->page_pool); in crypt_dtr()
2692 mempool_exit(&cc->req_pool); in crypt_dtr()
2693 mempool_exit(&cc->tag_pool); in crypt_dtr()
2695 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0); in crypt_dtr()
2696 percpu_counter_destroy(&cc->n_allocated_pages); in crypt_dtr()
2698 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) in crypt_dtr()
2699 cc->iv_gen_ops->dtr(cc); in crypt_dtr()
2701 if (cc->dev) in crypt_dtr()
2702 dm_put_device(ti, cc->dev); in crypt_dtr()
2704 kfree_sensitive(cc->cipher_string); in crypt_dtr()
2705 kfree_sensitive(cc->key_string); in crypt_dtr()
2706 kfree_sensitive(cc->cipher_auth); in crypt_dtr()
2707 kfree_sensitive(cc->authenc_key); in crypt_dtr()
2709 mutex_destroy(&cc->bio_alloc_lock); in crypt_dtr()
2712 kfree_sensitive(cc); in crypt_dtr()
2723 struct crypt_config *cc = ti->private; in crypt_ctr_ivmode() local
2725 if (crypt_integrity_aead(cc)) in crypt_ctr_ivmode()
2726 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_ivmode()
2728 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_ivmode()
2730 if (cc->iv_size) in crypt_ctr_ivmode()
2732 cc->iv_size = max(cc->iv_size, in crypt_ctr_ivmode()
2741 cc->iv_gen_ops = NULL; in crypt_ctr_ivmode()
2743 cc->iv_gen_ops = &crypt_iv_plain_ops; in crypt_ctr_ivmode()
2745 cc->iv_gen_ops = &crypt_iv_plain64_ops; in crypt_ctr_ivmode()
2747 cc->iv_gen_ops = &crypt_iv_plain64be_ops; in crypt_ctr_ivmode()
2749 cc->iv_gen_ops = &crypt_iv_essiv_ops; in crypt_ctr_ivmode()
2751 cc->iv_gen_ops = &crypt_iv_benbi_ops; in crypt_ctr_ivmode()
2753 cc->iv_gen_ops = &crypt_iv_null_ops; in crypt_ctr_ivmode()
2755 cc->iv_gen_ops = &crypt_iv_eboiv_ops; in crypt_ctr_ivmode()
2757 cc->iv_gen_ops = &crypt_iv_elephant_ops; in crypt_ctr_ivmode()
2758 cc->key_parts = 2; in crypt_ctr_ivmode()
2759 cc->key_extra_size = cc->key_size / 2; in crypt_ctr_ivmode()
2760 if (cc->key_extra_size > ELEPHANT_MAX_KEY_SIZE) in crypt_ctr_ivmode()
2762 set_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags); in crypt_ctr_ivmode()
2764 cc->iv_gen_ops = &crypt_iv_lmk_ops; in crypt_ctr_ivmode()
2771 if (cc->key_size % cc->key_parts) { in crypt_ctr_ivmode()
2772 cc->key_parts++; in crypt_ctr_ivmode()
2773 cc->key_extra_size = cc->key_size / cc->key_parts; in crypt_ctr_ivmode()
2776 cc->iv_gen_ops = &crypt_iv_tcw_ops; in crypt_ctr_ivmode()
2777 cc->key_parts += 2; /* IV + whitening */ in crypt_ctr_ivmode()
2778 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; in crypt_ctr_ivmode()
2780 cc->iv_gen_ops = &crypt_iv_random_ops; in crypt_ctr_ivmode()
2782 cc->integrity_iv_size = cc->iv_size; in crypt_ctr_ivmode()
2796 static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) in crypt_ctr_auth_cipher() argument
2820 cc->key_mac_size = crypto_ahash_digestsize(mac); in crypt_ctr_auth_cipher()
2823 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL); in crypt_ctr_auth_cipher()
2824 if (!cc->authenc_key) in crypt_ctr_auth_cipher()
2833 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_new() local
2837 cc->tfms_count = 1; in crypt_ctr_cipher_new()
2861 if (crypt_integrity_aead(cc)) { in crypt_ctr_cipher_new()
2862 ret = crypt_ctr_auth_cipher(cc, cipher_api); in crypt_ctr_cipher_new()
2870 cc->tfms_count = 64; in crypt_ctr_cipher_new()
2886 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_new()
2889 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_new()
2895 if (crypt_integrity_aead(cc)) in crypt_ctr_cipher_new()
2896 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_cipher_new()
2898 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_cipher_new()
2906 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_old() local
2912 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) { in crypt_ctr_cipher_old()
2926 cc->tfms_count = 1; in crypt_ctr_cipher_old()
2927 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || in crypt_ctr_cipher_old()
2928 !is_power_of_2(cc->tfms_count)) { in crypt_ctr_cipher_old()
2932 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_old()
2974 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_old()
2990 struct crypt_config *cc = ti->private; in crypt_ctr_cipher() local
2994 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); in crypt_ctr_cipher()
2995 if (!cc->cipher_string) { in crypt_ctr_cipher()
3013 ret = crypt_set_key(cc, key); in crypt_ctr_cipher()
3020 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { in crypt_ctr_cipher()
3021 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); in crypt_ctr_cipher()
3029 if (cc->iv_gen_ops && cc->iv_gen_ops->init) { in crypt_ctr_cipher()
3030 ret = cc->iv_gen_ops->init(cc); in crypt_ctr_cipher()
3038 if (cc->key_string) in crypt_ctr_cipher()
3039 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_ctr_cipher()
3046 struct crypt_config *cc = ti->private; in crypt_ctr_optional() local
3075 set_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_ctr_optional()
3078 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_ctr_optional()
3080 set_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); in crypt_ctr_optional()
3082 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_ctr_optional()
3088 cc->on_disk_tag_size = val; in crypt_ctr_optional()
3091 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_ctr_optional()
3097 cc->cipher_auth = kstrdup(sval, GFP_KERNEL); in crypt_ctr_optional()
3098 if (!cc->cipher_auth) in crypt_ctr_optional()
3100 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { in crypt_ctr_optional()
3101 if (cc->sector_size < (1 << SECTOR_SHIFT) || in crypt_ctr_optional()
3102 cc->sector_size > 4096 || in crypt_ctr_optional()
3103 (cc->sector_size & (cc->sector_size - 1))) { in crypt_ctr_optional()
3107 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) { in crypt_ctr_optional()
3111 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT; in crypt_ctr_optional()
3113 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_ctr_optional()
3128 struct crypt_config *cc = ti->private; in crypt_report_zones() local
3129 sector_t sector = cc->start + dm_target_offset(ti, args->next_sector); in crypt_report_zones()
3131 args->start = cc->start; in crypt_report_zones()
3132 return blkdev_report_zones(cc->dev->bdev, sector, nr_zones, in crypt_report_zones()
3144 struct crypt_config *cc; in crypt_ctr() local
3164 cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL); in crypt_ctr()
3165 if (!cc) { in crypt_ctr()
3169 cc->key_size = key_size; in crypt_ctr()
3170 cc->sector_size = (1 << SECTOR_SHIFT); in crypt_ctr()
3171 cc->sector_shift = 0; in crypt_ctr()
3173 ti->private = cc; in crypt_ctr()
3180 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); in crypt_ctr()
3195 if (crypt_integrity_aead(cc)) { in crypt_ctr()
3196 cc->dmreq_start = sizeof(struct aead_request); in crypt_ctr()
3197 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc)); in crypt_ctr()
3198 align_mask = crypto_aead_alignmask(any_tfm_aead(cc)); in crypt_ctr()
3200 cc->dmreq_start = sizeof(struct skcipher_request); in crypt_ctr()
3201 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); in crypt_ctr()
3202 align_mask = crypto_skcipher_alignmask(any_tfm(cc)); in crypt_ctr()
3204 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); in crypt_ctr()
3208 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) in crypt_ctr()
3221 iv_size_padding + cc->iv_size + in crypt_ctr()
3222 cc->iv_size + in crypt_ctr()
3226 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size); in crypt_ctr()
3232 cc->per_bio_data_size = ti->per_io_data_size = in crypt_ctr()
3233 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size, in crypt_ctr()
3236 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc); in crypt_ctr()
3242 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS); in crypt_ctr()
3248 mutex_init(&cc->bio_alloc_lock); in crypt_ctr()
3252 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { in crypt_ctr()
3256 cc->iv_offset = tmpll; in crypt_ctr()
3258 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); in crypt_ctr()
3269 cc->start = tmpll; in crypt_ctr()
3276 if (bdev_is_zoned(cc->dev->bdev)) { in crypt_ctr()
3277 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_ctr()
3278 set_bit(DM_CRYPT_WRITE_INLINE, &cc->flags); in crypt_ctr()
3281 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) { in crypt_ctr()
3282 ret = crypt_integrity_ctr(cc, ti); in crypt_ctr()
3286 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size; in crypt_ctr()
3287 if (!cc->tag_pool_max_sectors) in crypt_ctr()
3288 cc->tag_pool_max_sectors = 1; in crypt_ctr()
3290 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS, in crypt_ctr()
3291 cc->tag_pool_max_sectors * cc->on_disk_tag_size); in crypt_ctr()
3297 cc->tag_pool_max_sectors <<= cc->sector_shift; in crypt_ctr()
3301 cc->io_queue = alloc_workqueue("kcryptd_io/%s", WQ_MEM_RECLAIM, 1, devname); in crypt_ctr()
3302 if (!cc->io_queue) { in crypt_ctr()
3307 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_ctr()
3308 cc->crypt_queue = alloc_workqueue("kcryptd/%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, in crypt_ctr()
3311 cc->crypt_queue = alloc_workqueue("kcryptd/%s", in crypt_ctr()
3314 if (!cc->crypt_queue) { in crypt_ctr()
3319 spin_lock_init(&cc->write_thread_lock); in crypt_ctr()
3320 cc->write_tree = RB_ROOT; in crypt_ctr()
3322 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname); in crypt_ctr()
3323 if (IS_ERR(cc->write_thread)) { in crypt_ctr()
3324 ret = PTR_ERR(cc->write_thread); in crypt_ctr()
3325 cc->write_thread = NULL; in crypt_ctr()
3329 wake_up_process(cc->write_thread); in crypt_ctr()
3344 struct crypt_config *cc = ti->private; in crypt_map() local
3353 bio_set_dev(bio, cc->dev->bdev); in crypt_map()
3355 bio->bi_iter.bi_sector = cc->start + in crypt_map()
3364 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size)) in crypt_map()
3371 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) in crypt_map()
3374 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) in crypt_map()
3377 io = dm_per_bio_data(bio, cc->per_bio_data_size); in crypt_map()
3378 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); in crypt_map()
3380 if (cc->on_disk_tag_size) { in crypt_map()
3381 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift); in crypt_map()
3386 if (bio_sectors(bio) > cc->tag_pool_max_sectors) in crypt_map()
3387 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors); in crypt_map()
3388 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO); in crypt_map()
3393 if (crypt_integrity_aead(cc)) in crypt_map()
3415 struct crypt_config *cc = ti->private; in crypt_status() local
3425 DMEMIT("%s ", cc->cipher_string); in crypt_status()
3427 if (cc->key_size > 0) { in crypt_status()
3428 if (cc->key_string) in crypt_status()
3429 DMEMIT(":%u:%s", cc->key_size, cc->key_string); in crypt_status()
3431 for (i = 0; i < cc->key_size; i++) { in crypt_status()
3432 DMEMIT("%c%c", hex2asc(cc->key[i] >> 4), in crypt_status()
3433 hex2asc(cc->key[i] & 0xf)); in crypt_status()
3439 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, in crypt_status()
3440 cc->dev->name, (unsigned long long)cc->start); in crypt_status()
3443 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_status()
3444 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_status()
3445 num_feature_args += test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); in crypt_status()
3446 num_feature_args += test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_status()
3447 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT); in crypt_status()
3448 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_status()
3449 if (cc->on_disk_tag_size) in crypt_status()
3455 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_status()
3457 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) in crypt_status()
3459 if (test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) in crypt_status()
3461 if (test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) in crypt_status()
3463 if (cc->on_disk_tag_size) in crypt_status()
3464 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth); in crypt_status()
3465 if (cc->sector_size != (1 << SECTOR_SHIFT)) in crypt_status()
3466 DMEMIT(" sector_size:%d", cc->sector_size); in crypt_status()
3467 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_status()
3477 struct crypt_config *cc = ti->private; in crypt_postsuspend() local
3479 set_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_postsuspend()
3484 struct crypt_config *cc = ti->private; in crypt_preresume() local
3486 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { in crypt_preresume()
3496 struct crypt_config *cc = ti->private; in crypt_resume() local
3498 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_resume()
3508 struct crypt_config *cc = ti->private; in crypt_message() local
3515 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { in crypt_message()
3522 if (key_size < 0 || cc->key_size != key_size) { in crypt_message()
3527 ret = crypt_set_key(cc, argv[2]); in crypt_message()
3530 if (cc->iv_gen_ops && cc->iv_gen_ops->init) in crypt_message()
3531 ret = cc->iv_gen_ops->init(cc); in crypt_message()
3533 if (cc->key_string) in crypt_message()
3534 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_message()
3538 return crypt_wipe_key(cc); in crypt_message()
3549 struct crypt_config *cc = ti->private; in crypt_iterate_devices() local
3551 return fn(ti, cc->dev, cc->start, ti->len, data); in crypt_iterate_devices()
3556 struct crypt_config *cc = ti->private; in crypt_io_hints() local
3567 max_t(unsigned, limits->logical_block_size, cc->sector_size); in crypt_io_hints()
3569 max_t(unsigned, limits->physical_block_size, cc->sector_size); in crypt_io_hints()
3570 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size); in crypt_io_hints()