Lines Matching full:cc
67 struct crypt_config *cc; member
95 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
97 void (*dtr)(struct crypt_config *cc);
98 int (*init)(struct crypt_config *cc);
99 int (*wipe)(struct crypt_config *cc);
100 int (*generator)(struct crypt_config *cc, u8 *iv,
102 int (*post)(struct crypt_config *cc, u8 *iv,
237 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
240 static bool crypt_integrity_aead(struct crypt_config *cc);
245 static struct crypto_skcipher *any_tfm(struct crypt_config *cc) in any_tfm() argument
247 return cc->cipher_tfm.tfms[0]; in any_tfm()
250 static struct crypto_aead *any_tfm_aead(struct crypt_config *cc) in any_tfm_aead() argument
252 return cc->cipher_tfm.tfms_aead[0]; in any_tfm_aead()
311 static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain_gen() argument
314 memset(iv, 0, cc->iv_size); in crypt_iv_plain_gen()
320 static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64_gen() argument
323 memset(iv, 0, cc->iv_size); in crypt_iv_plain64_gen()
329 static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64be_gen() argument
332 memset(iv, 0, cc->iv_size); in crypt_iv_plain64be_gen()
334 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector); in crypt_iv_plain64be_gen()
339 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_essiv_gen() argument
346 memset(iv, 0, cc->iv_size); in crypt_iv_essiv_gen()
352 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_benbi_ctr() argument
358 if (crypt_integrity_aead(cc)) in crypt_iv_benbi_ctr()
359 bs = crypto_aead_blocksize(any_tfm_aead(cc)); in crypt_iv_benbi_ctr()
361 bs = crypto_skcipher_blocksize(any_tfm(cc)); in crypt_iv_benbi_ctr()
377 cc->iv_gen_private.benbi.shift = 9 - log; in crypt_iv_benbi_ctr()
382 static void crypt_iv_benbi_dtr(struct crypt_config *cc) in crypt_iv_benbi_dtr() argument
386 static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_benbi_gen() argument
391 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ in crypt_iv_benbi_gen()
393 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); in crypt_iv_benbi_gen()
394 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); in crypt_iv_benbi_gen()
399 static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_null_gen() argument
402 memset(iv, 0, cc->iv_size); in crypt_iv_null_gen()
407 static void crypt_iv_lmk_dtr(struct crypt_config *cc) in crypt_iv_lmk_dtr() argument
409 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_dtr()
419 static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_lmk_ctr() argument
422 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_ctr()
424 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_lmk_ctr()
437 if (cc->key_parts == cc->tfms_count) { in crypt_iv_lmk_ctr()
444 crypt_iv_lmk_dtr(cc); in crypt_iv_lmk_ctr()
452 static int crypt_iv_lmk_init(struct crypt_config *cc) in crypt_iv_lmk_init() argument
454 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_init()
455 int subkey_size = cc->key_size / cc->key_parts; in crypt_iv_lmk_init()
459 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), in crypt_iv_lmk_init()
465 static int crypt_iv_lmk_wipe(struct crypt_config *cc) in crypt_iv_lmk_wipe() argument
467 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_wipe()
475 static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_one() argument
479 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_one()
518 memcpy(iv, &md5state.hash, cc->iv_size); in crypt_iv_lmk_one()
523 static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_gen() argument
531 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_lmk_gen()
533 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset); in crypt_iv_lmk_gen()
536 memset(iv, 0, cc->iv_size); in crypt_iv_lmk_gen()
541 static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_post() argument
551 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_lmk_post()
553 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset); in crypt_iv_lmk_post()
557 crypto_xor(dst + sg->offset, iv, cc->iv_size); in crypt_iv_lmk_post()
563 static void crypt_iv_tcw_dtr(struct crypt_config *cc) in crypt_iv_tcw_dtr() argument
565 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_dtr()
577 static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_tcw_ctr() argument
580 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_ctr()
582 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_tcw_ctr()
587 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { in crypt_iv_tcw_ctr()
599 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); in crypt_iv_tcw_ctr()
602 crypt_iv_tcw_dtr(cc); in crypt_iv_tcw_ctr()
610 static int crypt_iv_tcw_init(struct crypt_config *cc) in crypt_iv_tcw_init() argument
612 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_init()
613 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; in crypt_iv_tcw_init()
615 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); in crypt_iv_tcw_init()
616 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], in crypt_iv_tcw_init()
622 static int crypt_iv_tcw_wipe(struct crypt_config *cc) in crypt_iv_tcw_wipe() argument
624 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_wipe()
626 memset(tcw->iv_seed, 0, cc->iv_size); in crypt_iv_tcw_wipe()
632 static int crypt_iv_tcw_whitening(struct crypt_config *cc, in crypt_iv_tcw_whitening() argument
636 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_whitening()
670 static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_gen() argument
674 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_gen()
681 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_tcw_gen()
683 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset); in crypt_iv_tcw_gen()
689 if (cc->iv_size > 8) in crypt_iv_tcw_gen()
691 cc->iv_size - 8); in crypt_iv_tcw_gen()
696 static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_post() argument
707 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_tcw_post()
709 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset); in crypt_iv_tcw_post()
715 static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_random_gen() argument
719 get_random_bytes(iv, cc->iv_size); in crypt_iv_random_gen()
723 static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_eboiv_ctr() argument
726 if (crypt_integrity_aead(cc)) { in crypt_iv_eboiv_ctr()
731 if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) { in crypt_iv_eboiv_ctr()
740 static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_eboiv_gen() argument
749 req = skcipher_request_alloc(any_tfm(cc), GFP_NOIO); in crypt_iv_eboiv_gen()
753 memset(buf, 0, cc->iv_size); in crypt_iv_eboiv_gen()
754 *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size); in crypt_iv_eboiv_gen()
756 sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size); in crypt_iv_eboiv_gen()
757 sg_init_one(&dst, iv, cc->iv_size); in crypt_iv_eboiv_gen()
758 skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf); in crypt_iv_eboiv_gen()
766 static void crypt_iv_elephant_dtr(struct crypt_config *cc) in crypt_iv_elephant_dtr() argument
768 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_dtr()
774 static int crypt_iv_elephant_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_elephant_ctr() argument
777 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_ctr()
788 r = crypt_iv_eboiv_ctr(cc, ti, NULL); in crypt_iv_elephant_ctr()
790 crypt_iv_elephant_dtr(cc); in crypt_iv_elephant_ctr()
938 static int crypt_iv_elephant(struct crypt_config *cc, struct dm_crypt_request *dmreq) in crypt_iv_elephant() argument
940 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant()
956 *(__le64 *)es = cpu_to_le64(dmreq->iv_sector * cc->sector_size); in crypt_iv_elephant()
974 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_elephant()
980 sg2 = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_elephant()
982 memcpy(data_offset, data2 + sg2->offset, cc->sector_size); in crypt_iv_elephant()
987 diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
988 diffuser_b_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
989 diffuser_a_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
990 diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
993 for (i = 0; i < (cc->sector_size / 32); i++) in crypt_iv_elephant()
997 diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
998 diffuser_a_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
999 diffuser_b_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1000 diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32)); in crypt_iv_elephant()
1011 static int crypt_iv_elephant_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_elephant_gen() argument
1017 r = crypt_iv_elephant(cc, dmreq); in crypt_iv_elephant_gen()
1022 return crypt_iv_eboiv_gen(cc, iv, dmreq); in crypt_iv_elephant_gen()
1025 static int crypt_iv_elephant_post(struct crypt_config *cc, u8 *iv, in crypt_iv_elephant_post() argument
1029 return crypt_iv_elephant(cc, dmreq); in crypt_iv_elephant_post()
1034 static int crypt_iv_elephant_init(struct crypt_config *cc) in crypt_iv_elephant_init() argument
1036 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_init()
1037 int key_offset = cc->key_size - cc->key_extra_size; in crypt_iv_elephant_init()
1039 return crypto_skcipher_setkey(elephant->tfm, &cc->key[key_offset], cc->key_extra_size); in crypt_iv_elephant_init()
1042 static int crypt_iv_elephant_wipe(struct crypt_config *cc) in crypt_iv_elephant_wipe() argument
1044 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; in crypt_iv_elephant_wipe()
1047 memset(key, 0, cc->key_extra_size); in crypt_iv_elephant_wipe()
1048 return crypto_skcipher_setkey(elephant->tfm, key, cc->key_extra_size); in crypt_iv_elephant_wipe()
1116 static bool crypt_integrity_aead(struct crypt_config *cc) in crypt_integrity_aead() argument
1118 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_integrity_aead()
1121 static bool crypt_integrity_hmac(struct crypt_config *cc) in crypt_integrity_hmac() argument
1123 return crypt_integrity_aead(cc) && cc->key_mac_size; in crypt_integrity_hmac()
1127 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, in crypt_get_sg_data() argument
1130 if (unlikely(crypt_integrity_aead(cc))) in crypt_get_sg_data()
1142 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size) in dm_crypt_integrity_io_alloc()
1149 tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift); in dm_crypt_integrity_io_alloc()
1152 bip->bip_iter.bi_sector = io->cc->start + io->sector; in dm_crypt_integrity_io_alloc()
1162 static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti) in crypt_integrity_ctr() argument
1165 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk); in crypt_integrity_ctr()
1174 if (bi->tag_size != cc->on_disk_tag_size || in crypt_integrity_ctr()
1175 bi->tuple_size != cc->on_disk_tag_size) { in crypt_integrity_ctr()
1179 if (1 << bi->interval_exp != cc->sector_size) { in crypt_integrity_ctr()
1184 if (crypt_integrity_aead(cc)) { in crypt_integrity_ctr()
1185 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size; in crypt_integrity_ctr()
1187 cc->integrity_tag_size, cc->integrity_iv_size); in crypt_integrity_ctr()
1189 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) { in crypt_integrity_ctr()
1193 } else if (cc->integrity_iv_size) in crypt_integrity_ctr()
1195 cc->integrity_iv_size); in crypt_integrity_ctr()
1197 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) { in crypt_integrity_ctr()
1209 static void crypt_convert_init(struct crypt_config *cc, in crypt_convert_init() argument
1220 ctx->cc_sector = sector + cc->iv_offset; in crypt_convert_init()
1224 static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, in dmreq_of_req() argument
1227 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); in dmreq_of_req()
1230 static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq) in req_of_dmreq() argument
1232 return (void *)((char *)dmreq - cc->dmreq_start); in req_of_dmreq()
1235 static u8 *iv_of_dmreq(struct crypt_config *cc, in iv_of_dmreq() argument
1238 if (crypt_integrity_aead(cc)) in iv_of_dmreq()
1240 crypto_aead_alignmask(any_tfm_aead(cc)) + 1); in iv_of_dmreq()
1243 crypto_skcipher_alignmask(any_tfm(cc)) + 1); in iv_of_dmreq()
1246 static u8 *org_iv_of_dmreq(struct crypt_config *cc, in org_iv_of_dmreq() argument
1249 return iv_of_dmreq(cc, dmreq) + cc->iv_size; in org_iv_of_dmreq()
1252 static __le64 *org_sector_of_dmreq(struct crypt_config *cc, in org_sector_of_dmreq() argument
1255 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size; in org_sector_of_dmreq()
1259 static unsigned int *org_tag_of_dmreq(struct crypt_config *cc, in org_tag_of_dmreq() argument
1262 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + in org_tag_of_dmreq()
1263 cc->iv_size + sizeof(uint64_t); in org_tag_of_dmreq()
1267 static void *tag_from_dmreq(struct crypt_config *cc, in tag_from_dmreq() argument
1273 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) * in tag_from_dmreq()
1274 cc->on_disk_tag_size]; in tag_from_dmreq()
1277 static void *iv_tag_from_dmreq(struct crypt_config *cc, in iv_tag_from_dmreq() argument
1280 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size; in iv_tag_from_dmreq()
1283 static int crypt_convert_block_aead(struct crypt_config *cc, in crypt_convert_block_aead() argument
1295 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size); in crypt_convert_block_aead()
1298 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_aead()
1301 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_aead()
1303 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_aead()
1304 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_aead()
1307 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_aead()
1309 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1310 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_aead()
1312 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1313 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1314 tag = tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1315 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1324 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1325 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_aead()
1326 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1330 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1331 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_aead()
1332 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1334 if (cc->iv_gen_ops) { in crypt_convert_block_aead()
1336 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_aead()
1337 memcpy(org_iv, tag_iv, cc->iv_size); in crypt_convert_block_aead()
1339 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_aead()
1343 if (cc->integrity_iv_size) in crypt_convert_block_aead()
1344 memcpy(tag_iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1347 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1350 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size); in crypt_convert_block_aead()
1353 cc->sector_size, iv); in crypt_convert_block_aead()
1355 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size) in crypt_convert_block_aead()
1356 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0, in crypt_convert_block_aead()
1357 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size)); in crypt_convert_block_aead()
1360 cc->sector_size + cc->integrity_tag_size, iv); in crypt_convert_block_aead()
1370 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_aead()
1371 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_aead()
1373 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_aead()
1374 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_aead()
1379 static int crypt_convert_block_skcipher(struct crypt_config *cc, in crypt_convert_block_skcipher() argument
1393 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_skcipher()
1396 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_skcipher()
1398 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1399 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_skcipher()
1402 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_skcipher()
1404 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1405 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1406 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1408 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1409 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_skcipher()
1416 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_skcipher()
1419 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_skcipher()
1421 if (cc->iv_gen_ops) { in crypt_convert_block_skcipher()
1423 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_skcipher()
1424 memcpy(org_iv, tag_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1426 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1430 if (test_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1433 if (cc->integrity_iv_size) in crypt_convert_block_skcipher()
1434 memcpy(tag_iv, org_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1437 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_skcipher()
1440 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv); in crypt_convert_block_skcipher()
1447 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_skcipher()
1448 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1450 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_skcipher()
1451 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_skcipher()
1459 static int crypt_alloc_req_skcipher(struct crypt_config *cc, in crypt_alloc_req_skcipher() argument
1462 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1); in crypt_alloc_req_skcipher()
1465 ctx->r.req = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); in crypt_alloc_req_skcipher()
1470 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]); in crypt_alloc_req_skcipher()
1478 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req)); in crypt_alloc_req_skcipher()
1483 static int crypt_alloc_req_aead(struct crypt_config *cc, in crypt_alloc_req_aead() argument
1487 ctx->r.req_aead = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); in crypt_alloc_req_aead()
1492 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_req_aead()
1500 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead)); in crypt_alloc_req_aead()
1505 static int crypt_alloc_req(struct crypt_config *cc, in crypt_alloc_req() argument
1508 if (crypt_integrity_aead(cc)) in crypt_alloc_req()
1509 return crypt_alloc_req_aead(cc, ctx); in crypt_alloc_req()
1511 return crypt_alloc_req_skcipher(cc, ctx); in crypt_alloc_req()
1514 static void crypt_free_req_skcipher(struct crypt_config *cc, in crypt_free_req_skcipher() argument
1517 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_skcipher()
1520 mempool_free(req, &cc->req_pool); in crypt_free_req_skcipher()
1523 static void crypt_free_req_aead(struct crypt_config *cc, in crypt_free_req_aead() argument
1526 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_aead()
1529 mempool_free(req, &cc->req_pool); in crypt_free_req_aead()
1532 static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio) in crypt_free_req() argument
1534 if (crypt_integrity_aead(cc)) in crypt_free_req()
1535 crypt_free_req_aead(cc, req, base_bio); in crypt_free_req()
1537 crypt_free_req_skcipher(cc, req, base_bio); in crypt_free_req()
1543 static blk_status_t crypt_convert(struct crypt_config *cc, in crypt_convert() argument
1547 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT; in crypt_convert()
1560 r = crypt_alloc_req(cc, ctx); in crypt_convert()
1568 if (crypt_integrity_aead(cc)) in crypt_convert()
1569 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset); in crypt_convert()
1571 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset); in crypt_convert()
1637 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1658 struct crypt_config *cc = io->cc; in crypt_alloc_buffer() local
1667 mutex_lock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1669 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs); in crypt_alloc_buffer()
1678 page = mempool_alloc(&cc->page_pool, gfp_mask); in crypt_alloc_buffer()
1680 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1695 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1701 mutex_unlock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1706 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) in crypt_free_buffer_pages() argument
1713 mempool_free(bv->bv_page, &cc->page_pool); in crypt_free_buffer_pages()
1717 static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, in crypt_io_init() argument
1720 io->cc = cc; in crypt_io_init()
1748 struct crypt_config *cc = io->cc; in crypt_dec_pending() local
1756 crypt_free_req(cc, io->ctx.r.req, base_bio); in crypt_dec_pending()
1759 mempool_free(io->integrity_metadata, &io->cc->tag_pool); in crypt_dec_pending()
1775 queue_work(cc->io_queue, &io->work); in crypt_dec_pending()
1802 struct crypt_config *cc = io->cc; in crypt_endio() local
1810 crypt_free_buffer_pages(cc, clone); in crypt_endio()
1828 struct crypt_config *cc = io->cc; in clone_init() local
1832 bio_set_dev(clone, cc->dev->bdev); in clone_init()
1838 struct crypt_config *cc = io->cc; in kcryptd_io_read() local
1847 clone = bio_clone_fast(io->base_bio, gfp, &cc->bs); in kcryptd_io_read()
1854 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_io_read()
1878 struct crypt_config *cc = io->cc; in kcryptd_queue_read() local
1881 queue_work(cc->io_queue, &io->work); in kcryptd_queue_read()
1895 struct crypt_config *cc = data; in dmcrypt_write() local
1902 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1905 if (!RB_EMPTY_ROOT(&cc->write_tree)) in dmcrypt_write()
1910 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1920 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1924 write_tree = cc->write_tree; in dmcrypt_write()
1925 cc->write_tree = RB_ROOT; in dmcrypt_write()
1926 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1949 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_io_submit() local
1955 crypt_free_buffer_pages(cc, clone); in kcryptd_crypt_write_io_submit()
1964 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_crypt_write_io_submit()
1966 if ((likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) || in kcryptd_crypt_write_io_submit()
1967 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) { in kcryptd_crypt_write_io_submit()
1972 spin_lock_irqsave(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
1973 if (RB_EMPTY_ROOT(&cc->write_tree)) in kcryptd_crypt_write_io_submit()
1974 wake_up_process(cc->write_thread); in kcryptd_crypt_write_io_submit()
1975 rbp = &cc->write_tree.rb_node; in kcryptd_crypt_write_io_submit()
1986 rb_insert_color(&io->rb_node, &cc->write_tree); in kcryptd_crypt_write_io_submit()
1987 spin_unlock_irqrestore(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
1990 static bool kcryptd_crypt_write_inline(struct crypt_config *cc, in kcryptd_crypt_write_inline() argument
1994 if (!test_bit(DM_CRYPT_WRITE_INLINE, &cc->flags)) in kcryptd_crypt_write_inline()
2015 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_continue() local
2024 r = crypt_convert(cc, &io->ctx, true, false); in kcryptd_crypt_write_continue()
2028 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_crypt_write_continue()
2045 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_convert() local
2056 crypt_convert_init(cc, ctx, NULL, io->base_bio, sector); in kcryptd_crypt_write_convert()
2070 r = crypt_convert(cc, ctx, in kcryptd_crypt_write_convert()
2071 test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_write_convert()
2079 queue_work(cc->crypt_queue, &io->work); in kcryptd_crypt_write_convert()
2085 if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_crypt_write_convert()
2109 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_continue() local
2115 r = crypt_convert(cc, &io->ctx, true, false); in kcryptd_crypt_read_continue()
2127 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_convert() local
2132 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, in kcryptd_crypt_read_convert()
2135 r = crypt_convert(cc, &io->ctx, in kcryptd_crypt_read_convert()
2136 test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags), true); in kcryptd_crypt_read_convert()
2143 queue_work(cc->crypt_queue, &io->work); in kcryptd_crypt_read_convert()
2161 struct crypt_config *cc = io->cc; in kcryptd_async_done() local
2173 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) in kcryptd_async_done()
2174 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq); in kcryptd_async_done()
2179 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq))); in kcryptd_async_done()
2184 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); in kcryptd_async_done()
2198 if (kcryptd_crypt_write_inline(cc, ctx)) { in kcryptd_async_done()
2223 struct crypt_config *cc = io->cc; in kcryptd_queue_crypt() local
2225 if ((bio_data_dir(io->base_bio) == READ && test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) || in kcryptd_queue_crypt()
2226 (bio_data_dir(io->base_bio) == WRITE && test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags))) { in kcryptd_queue_crypt()
2244 queue_work(cc->crypt_queue, &io->work); in kcryptd_queue_crypt()
2247 static void crypt_free_tfms_aead(struct crypt_config *cc) in crypt_free_tfms_aead() argument
2249 if (!cc->cipher_tfm.tfms_aead) in crypt_free_tfms_aead()
2252 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_free_tfms_aead()
2253 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]); in crypt_free_tfms_aead()
2254 cc->cipher_tfm.tfms_aead[0] = NULL; in crypt_free_tfms_aead()
2257 kfree(cc->cipher_tfm.tfms_aead); in crypt_free_tfms_aead()
2258 cc->cipher_tfm.tfms_aead = NULL; in crypt_free_tfms_aead()
2261 static void crypt_free_tfms_skcipher(struct crypt_config *cc) in crypt_free_tfms_skcipher() argument
2265 if (!cc->cipher_tfm.tfms) in crypt_free_tfms_skcipher()
2268 for (i = 0; i < cc->tfms_count; i++) in crypt_free_tfms_skcipher()
2269 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_free_tfms_skcipher()
2270 crypto_free_skcipher(cc->cipher_tfm.tfms[i]); in crypt_free_tfms_skcipher()
2271 cc->cipher_tfm.tfms[i] = NULL; in crypt_free_tfms_skcipher()
2274 kfree(cc->cipher_tfm.tfms); in crypt_free_tfms_skcipher()
2275 cc->cipher_tfm.tfms = NULL; in crypt_free_tfms_skcipher()
2278 static void crypt_free_tfms(struct crypt_config *cc) in crypt_free_tfms() argument
2280 if (crypt_integrity_aead(cc)) in crypt_free_tfms()
2281 crypt_free_tfms_aead(cc); in crypt_free_tfms()
2283 crypt_free_tfms_skcipher(cc); in crypt_free_tfms()
2286 static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_skcipher() argument
2291 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count, in crypt_alloc_tfms_skcipher()
2294 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_skcipher()
2297 for (i = 0; i < cc->tfms_count; i++) { in crypt_alloc_tfms_skcipher()
2298 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, in crypt_alloc_tfms_skcipher()
2300 if (IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_alloc_tfms_skcipher()
2301 err = PTR_ERR(cc->cipher_tfm.tfms[i]); in crypt_alloc_tfms_skcipher()
2302 crypt_free_tfms(cc); in crypt_alloc_tfms_skcipher()
2313 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name); in crypt_alloc_tfms_skcipher()
2317 static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_aead() argument
2321 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL); in crypt_alloc_tfms_aead()
2322 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_aead()
2325 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, in crypt_alloc_tfms_aead()
2327 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_alloc_tfms_aead()
2328 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_tfms_aead()
2329 crypt_free_tfms(cc); in crypt_alloc_tfms_aead()
2334 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name); in crypt_alloc_tfms_aead()
2338 static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms() argument
2340 if (crypt_integrity_aead(cc)) in crypt_alloc_tfms()
2341 return crypt_alloc_tfms_aead(cc, ciphermode); in crypt_alloc_tfms()
2343 return crypt_alloc_tfms_skcipher(cc, ciphermode); in crypt_alloc_tfms()
2346 static unsigned crypt_subkey_size(struct crypt_config *cc) in crypt_subkey_size() argument
2348 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); in crypt_subkey_size()
2351 static unsigned crypt_authenckey_size(struct crypt_config *cc) in crypt_authenckey_size() argument
2353 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param)); in crypt_authenckey_size()
2359 * This funcion converts cc->key to this special format.
2378 static int crypt_setkey(struct crypt_config *cc) in crypt_setkey() argument
2384 subkey_size = crypt_subkey_size(cc); in crypt_setkey()
2386 if (crypt_integrity_hmac(cc)) { in crypt_setkey()
2387 if (subkey_size < cc->key_mac_size) in crypt_setkey()
2390 crypt_copy_authenckey(cc->authenc_key, cc->key, in crypt_setkey()
2391 subkey_size - cc->key_mac_size, in crypt_setkey()
2392 cc->key_mac_size); in crypt_setkey()
2395 for (i = 0; i < cc->tfms_count; i++) { in crypt_setkey()
2396 if (crypt_integrity_hmac(cc)) in crypt_setkey()
2397 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
2398 cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
2399 else if (crypt_integrity_aead(cc)) in crypt_setkey()
2400 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
2401 cc->key + (i * subkey_size), in crypt_setkey()
2404 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i], in crypt_setkey()
2405 cc->key + (i * subkey_size), in crypt_setkey()
2411 if (crypt_integrity_hmac(cc)) in crypt_setkey()
2412 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
2427 static int set_key_user(struct crypt_config *cc, struct key *key) in set_key_user() argument
2435 if (cc->key_size != ukp->datalen) in set_key_user()
2438 memcpy(cc->key, ukp->data, cc->key_size); in set_key_user()
2444 static int set_key_encrypted(struct crypt_config *cc, struct key *key) in set_key_encrypted() argument
2452 if (cc->key_size != ekp->decrypted_datalen) in set_key_encrypted()
2455 memcpy(cc->key, ekp->decrypted_data, cc->key_size); in set_key_encrypted()
2461 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2467 int (*set_key)(struct crypt_config *cc, struct key *key); in crypt_set_keyring_key()
2510 ret = set_key(cc, key); in crypt_set_keyring_key()
2522 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2524 ret = crypt_setkey(cc); in crypt_set_keyring_key()
2527 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2528 kfree_sensitive(cc->key_string); in crypt_set_keyring_key()
2529 cc->key_string = new_key_string; in crypt_set_keyring_key()
2561 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2573 static int crypt_set_key(struct crypt_config *cc, char *key) in crypt_set_key() argument
2579 if (!cc->key_size && strcmp(key, "-")) in crypt_set_key()
2584 r = crypt_set_keyring_key(cc, key + 1); in crypt_set_key()
2589 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2592 kfree_sensitive(cc->key_string); in crypt_set_key()
2593 cc->key_string = NULL; in crypt_set_key()
2596 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) in crypt_set_key()
2599 r = crypt_setkey(cc); in crypt_set_key()
2601 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2610 static int crypt_wipe_key(struct crypt_config *cc) in crypt_wipe_key() argument
2614 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_wipe_key()
2615 get_random_bytes(&cc->key, cc->key_size); in crypt_wipe_key()
2618 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { in crypt_wipe_key()
2619 r = cc->iv_gen_ops->wipe(cc); in crypt_wipe_key()
2624 kfree_sensitive(cc->key_string); in crypt_wipe_key()
2625 cc->key_string = NULL; in crypt_wipe_key()
2626 r = crypt_setkey(cc); in crypt_wipe_key()
2627 memset(&cc->key, 0, cc->key_size * sizeof(u8)); in crypt_wipe_key()
2647 struct crypt_config *cc = pool_data; in crypt_page_alloc() local
2655 if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) >= dm_crypt_pages_per_client) && in crypt_page_alloc()
2661 percpu_counter_add(&cc->n_allocated_pages, 1); in crypt_page_alloc()
2668 struct crypt_config *cc = pool_data; in crypt_page_free() local
2671 percpu_counter_sub(&cc->n_allocated_pages, 1); in crypt_page_free()
2676 struct crypt_config *cc = ti->private; in crypt_dtr() local
2680 if (!cc) in crypt_dtr()
2683 if (cc->write_thread) in crypt_dtr()
2684 kthread_stop(cc->write_thread); in crypt_dtr()
2686 if (cc->io_queue) in crypt_dtr()
2687 destroy_workqueue(cc->io_queue); in crypt_dtr()
2688 if (cc->crypt_queue) in crypt_dtr()
2689 destroy_workqueue(cc->crypt_queue); in crypt_dtr()
2691 crypt_free_tfms(cc); in crypt_dtr()
2693 bioset_exit(&cc->bs); in crypt_dtr()
2695 mempool_exit(&cc->page_pool); in crypt_dtr()
2696 mempool_exit(&cc->req_pool); in crypt_dtr()
2697 mempool_exit(&cc->tag_pool); in crypt_dtr()
2699 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0); in crypt_dtr()
2700 percpu_counter_destroy(&cc->n_allocated_pages); in crypt_dtr()
2702 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) in crypt_dtr()
2703 cc->iv_gen_ops->dtr(cc); in crypt_dtr()
2705 if (cc->dev) in crypt_dtr()
2706 dm_put_device(ti, cc->dev); in crypt_dtr()
2708 kfree_sensitive(cc->cipher_string); in crypt_dtr()
2709 kfree_sensitive(cc->key_string); in crypt_dtr()
2710 kfree_sensitive(cc->cipher_auth); in crypt_dtr()
2711 kfree_sensitive(cc->authenc_key); in crypt_dtr()
2713 mutex_destroy(&cc->bio_alloc_lock); in crypt_dtr()
2716 kfree_sensitive(cc); in crypt_dtr()
2727 struct crypt_config *cc = ti->private; in crypt_ctr_ivmode() local
2729 if (crypt_integrity_aead(cc)) in crypt_ctr_ivmode()
2730 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_ivmode()
2732 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_ivmode()
2734 if (cc->iv_size) in crypt_ctr_ivmode()
2736 cc->iv_size = max(cc->iv_size, in crypt_ctr_ivmode()
2745 cc->iv_gen_ops = NULL; in crypt_ctr_ivmode()
2747 cc->iv_gen_ops = &crypt_iv_plain_ops; in crypt_ctr_ivmode()
2749 cc->iv_gen_ops = &crypt_iv_plain64_ops; in crypt_ctr_ivmode()
2751 cc->iv_gen_ops = &crypt_iv_plain64be_ops; in crypt_ctr_ivmode()
2753 cc->iv_gen_ops = &crypt_iv_essiv_ops; in crypt_ctr_ivmode()
2755 cc->iv_gen_ops = &crypt_iv_benbi_ops; in crypt_ctr_ivmode()
2757 cc->iv_gen_ops = &crypt_iv_null_ops; in crypt_ctr_ivmode()
2759 cc->iv_gen_ops = &crypt_iv_eboiv_ops; in crypt_ctr_ivmode()
2761 cc->iv_gen_ops = &crypt_iv_elephant_ops; in crypt_ctr_ivmode()
2762 cc->key_parts = 2; in crypt_ctr_ivmode()
2763 cc->key_extra_size = cc->key_size / 2; in crypt_ctr_ivmode()
2764 if (cc->key_extra_size > ELEPHANT_MAX_KEY_SIZE) in crypt_ctr_ivmode()
2766 set_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags); in crypt_ctr_ivmode()
2768 cc->iv_gen_ops = &crypt_iv_lmk_ops; in crypt_ctr_ivmode()
2775 if (cc->key_size % cc->key_parts) { in crypt_ctr_ivmode()
2776 cc->key_parts++; in crypt_ctr_ivmode()
2777 cc->key_extra_size = cc->key_size / cc->key_parts; in crypt_ctr_ivmode()
2780 cc->iv_gen_ops = &crypt_iv_tcw_ops; in crypt_ctr_ivmode()
2781 cc->key_parts += 2; /* IV + whitening */ in crypt_ctr_ivmode()
2782 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; in crypt_ctr_ivmode()
2784 cc->iv_gen_ops = &crypt_iv_random_ops; in crypt_ctr_ivmode()
2786 cc->integrity_iv_size = cc->iv_size; in crypt_ctr_ivmode()
2800 static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) in crypt_ctr_auth_cipher() argument
2824 cc->key_mac_size = crypto_ahash_digestsize(mac); in crypt_ctr_auth_cipher()
2827 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL); in crypt_ctr_auth_cipher()
2828 if (!cc->authenc_key) in crypt_ctr_auth_cipher()
2837 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_new() local
2841 cc->tfms_count = 1; in crypt_ctr_cipher_new()
2865 if (crypt_integrity_aead(cc)) { in crypt_ctr_cipher_new()
2866 ret = crypt_ctr_auth_cipher(cc, cipher_api); in crypt_ctr_cipher_new()
2874 cc->tfms_count = 64; in crypt_ctr_cipher_new()
2890 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_new()
2893 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_new()
2899 if (crypt_integrity_aead(cc)) in crypt_ctr_cipher_new()
2900 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_cipher_new()
2902 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_cipher_new()
2910 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_old() local
2916 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) { in crypt_ctr_cipher_old()
2930 cc->tfms_count = 1; in crypt_ctr_cipher_old()
2931 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || in crypt_ctr_cipher_old()
2932 !is_power_of_2(cc->tfms_count)) { in crypt_ctr_cipher_old()
2936 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_old()
2978 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_old()
2994 struct crypt_config *cc = ti->private; in crypt_ctr_cipher() local
2998 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); in crypt_ctr_cipher()
2999 if (!cc->cipher_string) { in crypt_ctr_cipher()
3017 ret = crypt_set_key(cc, key); in crypt_ctr_cipher()
3024 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { in crypt_ctr_cipher()
3025 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); in crypt_ctr_cipher()
3033 if (cc->iv_gen_ops && cc->iv_gen_ops->init) { in crypt_ctr_cipher()
3034 ret = cc->iv_gen_ops->init(cc); in crypt_ctr_cipher()
3042 if (cc->key_string) in crypt_ctr_cipher()
3043 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_ctr_cipher()
3050 struct crypt_config *cc = ti->private; in crypt_ctr_optional() local
3079 set_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_ctr_optional()
3082 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_ctr_optional()
3084 set_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); in crypt_ctr_optional()
3086 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_ctr_optional()
3092 cc->on_disk_tag_size = val; in crypt_ctr_optional()
3095 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_ctr_optional()
3101 cc->cipher_auth = kstrdup(sval, GFP_KERNEL); in crypt_ctr_optional()
3102 if (!cc->cipher_auth) in crypt_ctr_optional()
3104 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { in crypt_ctr_optional()
3105 if (cc->sector_size < (1 << SECTOR_SHIFT) || in crypt_ctr_optional()
3106 cc->sector_size > 4096 || in crypt_ctr_optional()
3107 (cc->sector_size & (cc->sector_size - 1))) { in crypt_ctr_optional()
3111 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) { in crypt_ctr_optional()
3115 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT; in crypt_ctr_optional()
3117 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_ctr_optional()
3132 struct crypt_config *cc = ti->private; in crypt_report_zones() local
3133 sector_t sector = cc->start + dm_target_offset(ti, args->next_sector); in crypt_report_zones()
3135 args->start = cc->start; in crypt_report_zones()
3136 return blkdev_report_zones(cc->dev->bdev, sector, nr_zones, in crypt_report_zones()
3148 struct crypt_config *cc; in crypt_ctr() local
3168 cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL); in crypt_ctr()
3169 if (!cc) { in crypt_ctr()
3173 cc->key_size = key_size; in crypt_ctr()
3174 cc->sector_size = (1 << SECTOR_SHIFT); in crypt_ctr()
3175 cc->sector_shift = 0; in crypt_ctr()
3177 ti->private = cc; in crypt_ctr()
3184 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); in crypt_ctr()
3199 if (crypt_integrity_aead(cc)) { in crypt_ctr()
3200 cc->dmreq_start = sizeof(struct aead_request); in crypt_ctr()
3201 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc)); in crypt_ctr()
3202 align_mask = crypto_aead_alignmask(any_tfm_aead(cc)); in crypt_ctr()
3204 cc->dmreq_start = sizeof(struct skcipher_request); in crypt_ctr()
3205 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); in crypt_ctr()
3206 align_mask = crypto_skcipher_alignmask(any_tfm(cc)); in crypt_ctr()
3208 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); in crypt_ctr()
3212 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) in crypt_ctr()
3225 iv_size_padding + cc->iv_size + in crypt_ctr()
3226 cc->iv_size + in crypt_ctr()
3230 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size); in crypt_ctr()
3236 cc->per_bio_data_size = ti->per_io_data_size = in crypt_ctr()
3237 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size, in crypt_ctr()
3240 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc); in crypt_ctr()
3246 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS); in crypt_ctr()
3252 mutex_init(&cc->bio_alloc_lock); in crypt_ctr()
3256 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { in crypt_ctr()
3260 cc->iv_offset = tmpll; in crypt_ctr()
3262 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); in crypt_ctr()
3273 cc->start = tmpll; in crypt_ctr()
3280 if (bdev_is_zoned(cc->dev->bdev)) { in crypt_ctr()
3281 set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_ctr()
3282 set_bit(DM_CRYPT_WRITE_INLINE, &cc->flags); in crypt_ctr()
3285 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) { in crypt_ctr()
3286 ret = crypt_integrity_ctr(cc, ti); in crypt_ctr()
3290 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size; in crypt_ctr()
3291 if (!cc->tag_pool_max_sectors) in crypt_ctr()
3292 cc->tag_pool_max_sectors = 1; in crypt_ctr()
3294 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS, in crypt_ctr()
3295 cc->tag_pool_max_sectors * cc->on_disk_tag_size); in crypt_ctr()
3301 cc->tag_pool_max_sectors <<= cc->sector_shift; in crypt_ctr()
3305 cc->io_queue = alloc_workqueue("kcryptd_io/%s", WQ_MEM_RECLAIM, 1, devname); in crypt_ctr()
3306 if (!cc->io_queue) { in crypt_ctr()
3311 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_ctr()
3312 cc->crypt_queue = alloc_workqueue("kcryptd/%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, in crypt_ctr()
3315 cc->crypt_queue = alloc_workqueue("kcryptd/%s", in crypt_ctr()
3318 if (!cc->crypt_queue) { in crypt_ctr()
3323 spin_lock_init(&cc->write_thread_lock); in crypt_ctr()
3324 cc->write_tree = RB_ROOT; in crypt_ctr()
3326 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname); in crypt_ctr()
3327 if (IS_ERR(cc->write_thread)) { in crypt_ctr()
3328 ret = PTR_ERR(cc->write_thread); in crypt_ctr()
3329 cc->write_thread = NULL; in crypt_ctr()
3333 wake_up_process(cc->write_thread); in crypt_ctr()
3348 struct crypt_config *cc = ti->private; in crypt_map() local
3357 bio_set_dev(bio, cc->dev->bdev); in crypt_map()
3359 bio->bi_iter.bi_sector = cc->start + in crypt_map()
3368 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size)) in crypt_map()
3375 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) in crypt_map()
3378 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) in crypt_map()
3381 io = dm_per_bio_data(bio, cc->per_bio_data_size); in crypt_map()
3382 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); in crypt_map()
3384 if (cc->on_disk_tag_size) { in crypt_map()
3385 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift); in crypt_map()
3390 if (bio_sectors(bio) > cc->tag_pool_max_sectors) in crypt_map()
3391 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors); in crypt_map()
3392 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO); in crypt_map()
3397 if (crypt_integrity_aead(cc)) in crypt_map()
3419 struct crypt_config *cc = ti->private; in crypt_status() local
3429 DMEMIT("%s ", cc->cipher_string); in crypt_status()
3431 if (cc->key_size > 0) { in crypt_status()
3432 if (cc->key_string) in crypt_status()
3433 DMEMIT(":%u:%s", cc->key_size, cc->key_string); in crypt_status()
3435 for (i = 0; i < cc->key_size; i++) { in crypt_status()
3436 DMEMIT("%c%c", hex2asc(cc->key[i] >> 4), in crypt_status()
3437 hex2asc(cc->key[i] & 0xf)); in crypt_status()
3443 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, in crypt_status()
3444 cc->dev->name, (unsigned long long)cc->start); in crypt_status()
3447 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_status()
3448 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_status()
3449 num_feature_args += test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); in crypt_status()
3450 num_feature_args += test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); in crypt_status()
3451 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT); in crypt_status()
3452 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_status()
3453 if (cc->on_disk_tag_size) in crypt_status()
3459 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_status()
3461 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) in crypt_status()
3463 if (test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) in crypt_status()
3465 if (test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) in crypt_status()
3467 if (cc->on_disk_tag_size) in crypt_status()
3468 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth); in crypt_status()
3469 if (cc->sector_size != (1 << SECTOR_SHIFT)) in crypt_status()
3470 DMEMIT(" sector_size:%d", cc->sector_size); in crypt_status()
3471 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_status()
3481 struct crypt_config *cc = ti->private; in crypt_postsuspend() local
3483 set_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_postsuspend()
3488 struct crypt_config *cc = ti->private; in crypt_preresume() local
3490 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { in crypt_preresume()
3500 struct crypt_config *cc = ti->private; in crypt_resume() local
3502 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_resume()
3512 struct crypt_config *cc = ti->private; in crypt_message() local
3519 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { in crypt_message()
3526 if (key_size < 0 || cc->key_size != key_size) { in crypt_message()
3531 ret = crypt_set_key(cc, argv[2]); in crypt_message()
3534 if (cc->iv_gen_ops && cc->iv_gen_ops->init) in crypt_message()
3535 ret = cc->iv_gen_ops->init(cc); in crypt_message()
3537 if (cc->key_string) in crypt_message()
3538 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_message()
3542 return crypt_wipe_key(cc); in crypt_message()
3553 struct crypt_config *cc = ti->private; in crypt_iterate_devices() local
3555 return fn(ti, cc->dev, cc->start, ti->len, data); in crypt_iterate_devices()
3560 struct crypt_config *cc = ti->private; in crypt_io_hints() local
3571 max_t(unsigned, limits->logical_block_size, cc->sector_size); in crypt_io_hints()
3573 max_t(unsigned, limits->physical_block_size, cc->sector_size); in crypt_io_hints()
3574 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size); in crypt_io_hints()