Lines Matching refs:cc
65 struct crypt_config *cc; member
90 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
92 void (*dtr)(struct crypt_config *cc);
93 int (*init)(struct crypt_config *cc);
94 int (*wipe)(struct crypt_config *cc);
95 int (*generator)(struct crypt_config *cc, u8 *iv,
97 int (*post)(struct crypt_config *cc, u8 *iv,
232 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
238 static struct crypto_skcipher *any_tfm(struct crypt_config *cc) in any_tfm() argument
240 return cc->cipher_tfm.tfms[0]; in any_tfm()
243 static struct crypto_aead *any_tfm_aead(struct crypt_config *cc) in any_tfm_aead() argument
245 return cc->cipher_tfm.tfms_aead[0]; in any_tfm_aead()
298 static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain_gen() argument
301 memset(iv, 0, cc->iv_size); in crypt_iv_plain_gen()
307 static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64_gen() argument
310 memset(iv, 0, cc->iv_size); in crypt_iv_plain64_gen()
316 static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_plain64be_gen() argument
319 memset(iv, 0, cc->iv_size); in crypt_iv_plain64be_gen()
321 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector); in crypt_iv_plain64be_gen()
327 static int crypt_iv_essiv_init(struct crypt_config *cc) in crypt_iv_essiv_init() argument
329 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; in crypt_iv_essiv_init()
337 err = crypto_shash_digest(desc, cc->key, cc->key_size, essiv->salt); in crypt_iv_essiv_init()
342 essiv_tfm = cc->iv_private; in crypt_iv_essiv_init()
353 static int crypt_iv_essiv_wipe(struct crypt_config *cc) in crypt_iv_essiv_wipe() argument
355 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; in crypt_iv_essiv_wipe()
362 essiv_tfm = cc->iv_private; in crypt_iv_essiv_wipe()
371 static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc, in alloc_essiv_cipher() argument
380 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC); in alloc_essiv_cipher()
386 if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) { in alloc_essiv_cipher()
403 static void crypt_iv_essiv_dtr(struct crypt_config *cc) in crypt_iv_essiv_dtr() argument
406 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; in crypt_iv_essiv_dtr()
414 essiv_tfm = cc->iv_private; in crypt_iv_essiv_dtr()
419 cc->iv_private = NULL; in crypt_iv_essiv_dtr()
422 static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_essiv_ctr() argument
450 cc->iv_gen_private.essiv.salt = salt; in crypt_iv_essiv_ctr()
451 cc->iv_gen_private.essiv.hash_tfm = hash_tfm; in crypt_iv_essiv_ctr()
453 essiv_tfm = alloc_essiv_cipher(cc, ti, salt, in crypt_iv_essiv_ctr()
456 crypt_iv_essiv_dtr(cc); in crypt_iv_essiv_ctr()
459 cc->iv_private = essiv_tfm; in crypt_iv_essiv_ctr()
470 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_essiv_gen() argument
473 struct crypto_cipher *essiv_tfm = cc->iv_private; in crypt_iv_essiv_gen()
475 memset(iv, 0, cc->iv_size); in crypt_iv_essiv_gen()
482 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_benbi_ctr() argument
485 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc)); in crypt_iv_benbi_ctr()
501 cc->iv_gen_private.benbi.shift = 9 - log; in crypt_iv_benbi_ctr()
506 static void crypt_iv_benbi_dtr(struct crypt_config *cc) in crypt_iv_benbi_dtr() argument
510 static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_benbi_gen() argument
515 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ in crypt_iv_benbi_gen()
517 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); in crypt_iv_benbi_gen()
518 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); in crypt_iv_benbi_gen()
523 static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_null_gen() argument
526 memset(iv, 0, cc->iv_size); in crypt_iv_null_gen()
531 static void crypt_iv_lmk_dtr(struct crypt_config *cc) in crypt_iv_lmk_dtr() argument
533 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_dtr()
543 static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_lmk_ctr() argument
546 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_ctr()
548 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_lmk_ctr()
560 if (cc->key_parts == cc->tfms_count) { in crypt_iv_lmk_ctr()
567 crypt_iv_lmk_dtr(cc); in crypt_iv_lmk_ctr()
575 static int crypt_iv_lmk_init(struct crypt_config *cc) in crypt_iv_lmk_init() argument
577 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_init()
578 int subkey_size = cc->key_size / cc->key_parts; in crypt_iv_lmk_init()
582 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), in crypt_iv_lmk_init()
588 static int crypt_iv_lmk_wipe(struct crypt_config *cc) in crypt_iv_lmk_wipe() argument
590 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_wipe()
598 static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_one() argument
602 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; in crypt_iv_lmk_one()
642 memcpy(iv, &md5state.hash, cc->iv_size); in crypt_iv_lmk_one()
647 static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_gen() argument
655 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_lmk_gen()
657 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset); in crypt_iv_lmk_gen()
660 memset(iv, 0, cc->iv_size); in crypt_iv_lmk_gen()
665 static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, in crypt_iv_lmk_post() argument
675 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_lmk_post()
677 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset); in crypt_iv_lmk_post()
681 crypto_xor(dst + sg->offset, iv, cc->iv_size); in crypt_iv_lmk_post()
687 static void crypt_iv_tcw_dtr(struct crypt_config *cc) in crypt_iv_tcw_dtr() argument
689 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_dtr()
701 static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, in crypt_iv_tcw_ctr() argument
704 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_ctr()
706 if (cc->sector_size != (1 << SECTOR_SHIFT)) { in crypt_iv_tcw_ctr()
711 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { in crypt_iv_tcw_ctr()
722 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); in crypt_iv_tcw_ctr()
725 crypt_iv_tcw_dtr(cc); in crypt_iv_tcw_ctr()
733 static int crypt_iv_tcw_init(struct crypt_config *cc) in crypt_iv_tcw_init() argument
735 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_init()
736 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; in crypt_iv_tcw_init()
738 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); in crypt_iv_tcw_init()
739 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], in crypt_iv_tcw_init()
745 static int crypt_iv_tcw_wipe(struct crypt_config *cc) in crypt_iv_tcw_wipe() argument
747 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_wipe()
749 memset(tcw->iv_seed, 0, cc->iv_size); in crypt_iv_tcw_wipe()
755 static int crypt_iv_tcw_whitening(struct crypt_config *cc, in crypt_iv_tcw_whitening() argument
759 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_whitening()
794 static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_gen() argument
798 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; in crypt_iv_tcw_gen()
805 sg = crypt_get_sg_data(cc, dmreq->sg_in); in crypt_iv_tcw_gen()
807 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset); in crypt_iv_tcw_gen()
813 if (cc->iv_size > 8) in crypt_iv_tcw_gen()
815 cc->iv_size - 8); in crypt_iv_tcw_gen()
820 static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, in crypt_iv_tcw_post() argument
831 sg = crypt_get_sg_data(cc, dmreq->sg_out); in crypt_iv_tcw_post()
833 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset); in crypt_iv_tcw_post()
839 static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv, in crypt_iv_random_gen() argument
843 get_random_bytes(iv, cc->iv_size); in crypt_iv_random_gen()
902 static bool crypt_integrity_aead(struct crypt_config *cc) in crypt_integrity_aead() argument
904 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_integrity_aead()
907 static bool crypt_integrity_hmac(struct crypt_config *cc) in crypt_integrity_hmac() argument
909 return crypt_integrity_aead(cc) && cc->key_mac_size; in crypt_integrity_hmac()
913 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, in crypt_get_sg_data() argument
916 if (unlikely(crypt_integrity_aead(cc))) in crypt_get_sg_data()
928 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size) in dm_crypt_integrity_io_alloc()
935 tag_len = io->cc->on_disk_tag_size * bio_sectors(bio); in dm_crypt_integrity_io_alloc()
938 bip->bip_iter.bi_sector = io->cc->start + io->sector; in dm_crypt_integrity_io_alloc()
948 static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti) in crypt_integrity_ctr() argument
951 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk); in crypt_integrity_ctr()
959 if (bi->tag_size != cc->on_disk_tag_size || in crypt_integrity_ctr()
960 bi->tuple_size != cc->on_disk_tag_size) { in crypt_integrity_ctr()
964 if (1 << bi->interval_exp != cc->sector_size) { in crypt_integrity_ctr()
969 if (crypt_integrity_aead(cc)) { in crypt_integrity_ctr()
970 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size; in crypt_integrity_ctr()
972 cc->integrity_tag_size, cc->integrity_iv_size); in crypt_integrity_ctr()
974 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) { in crypt_integrity_ctr()
978 } else if (cc->integrity_iv_size) in crypt_integrity_ctr()
980 cc->integrity_iv_size); in crypt_integrity_ctr()
982 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) { in crypt_integrity_ctr()
994 static void crypt_convert_init(struct crypt_config *cc, in crypt_convert_init() argument
1005 ctx->cc_sector = sector + cc->iv_offset; in crypt_convert_init()
1009 static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, in dmreq_of_req() argument
1012 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); in dmreq_of_req()
1015 static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq) in req_of_dmreq() argument
1017 return (void *)((char *)dmreq - cc->dmreq_start); in req_of_dmreq()
1020 static u8 *iv_of_dmreq(struct crypt_config *cc, in iv_of_dmreq() argument
1023 if (crypt_integrity_aead(cc)) in iv_of_dmreq()
1025 crypto_aead_alignmask(any_tfm_aead(cc)) + 1); in iv_of_dmreq()
1028 crypto_skcipher_alignmask(any_tfm(cc)) + 1); in iv_of_dmreq()
1031 static u8 *org_iv_of_dmreq(struct crypt_config *cc, in org_iv_of_dmreq() argument
1034 return iv_of_dmreq(cc, dmreq) + cc->iv_size; in org_iv_of_dmreq()
1037 static uint64_t *org_sector_of_dmreq(struct crypt_config *cc, in org_sector_of_dmreq() argument
1040 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size; in org_sector_of_dmreq()
1044 static unsigned int *org_tag_of_dmreq(struct crypt_config *cc, in org_tag_of_dmreq() argument
1047 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + in org_tag_of_dmreq()
1048 cc->iv_size + sizeof(uint64_t); in org_tag_of_dmreq()
1052 static void *tag_from_dmreq(struct crypt_config *cc, in tag_from_dmreq() argument
1058 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) * in tag_from_dmreq()
1059 cc->on_disk_tag_size]; in tag_from_dmreq()
1062 static void *iv_tag_from_dmreq(struct crypt_config *cc, in iv_tag_from_dmreq() argument
1065 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size; in iv_tag_from_dmreq()
1068 static int crypt_convert_block_aead(struct crypt_config *cc, in crypt_convert_block_aead() argument
1080 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size); in crypt_convert_block_aead()
1083 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_aead()
1086 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_aead()
1088 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_aead()
1089 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_aead()
1092 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_aead()
1094 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1095 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_aead()
1097 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1098 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_aead()
1099 tag = tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1100 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_aead()
1109 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1110 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_aead()
1111 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1115 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size); in crypt_convert_block_aead()
1116 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_aead()
1117 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size); in crypt_convert_block_aead()
1119 if (cc->iv_gen_ops) { in crypt_convert_block_aead()
1121 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_aead()
1122 memcpy(org_iv, tag_iv, cc->iv_size); in crypt_convert_block_aead()
1124 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_aead()
1128 if (cc->integrity_iv_size) in crypt_convert_block_aead()
1129 memcpy(tag_iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1132 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_aead()
1135 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size); in crypt_convert_block_aead()
1138 cc->sector_size, iv); in crypt_convert_block_aead()
1140 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size) in crypt_convert_block_aead()
1141 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0, in crypt_convert_block_aead()
1142 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size)); in crypt_convert_block_aead()
1145 cc->sector_size + cc->integrity_tag_size, iv); in crypt_convert_block_aead()
1153 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_aead()
1154 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_aead()
1156 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_aead()
1157 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_aead()
1162 static int crypt_convert_block_skcipher(struct crypt_config *cc, in crypt_convert_block_skcipher() argument
1176 if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) in crypt_convert_block_skcipher()
1179 dmreq = dmreq_of_req(cc, req); in crypt_convert_block_skcipher()
1181 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_convert_block_skcipher()
1182 dmreq->iv_sector >>= cc->sector_shift; in crypt_convert_block_skcipher()
1185 *org_tag_of_dmreq(cc, dmreq) = tag_offset; in crypt_convert_block_skcipher()
1187 iv = iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1188 org_iv = org_iv_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1189 tag_iv = iv_tag_from_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1191 sector = org_sector_of_dmreq(cc, dmreq); in crypt_convert_block_skcipher()
1192 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); in crypt_convert_block_skcipher()
1199 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset); in crypt_convert_block_skcipher()
1202 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset); in crypt_convert_block_skcipher()
1204 if (cc->iv_gen_ops) { in crypt_convert_block_skcipher()
1206 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { in crypt_convert_block_skcipher()
1207 memcpy(org_iv, tag_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1209 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1213 if (cc->integrity_iv_size) in crypt_convert_block_skcipher()
1214 memcpy(tag_iv, org_iv, cc->integrity_iv_size); in crypt_convert_block_skcipher()
1217 memcpy(iv, org_iv, cc->iv_size); in crypt_convert_block_skcipher()
1220 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv); in crypt_convert_block_skcipher()
1227 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) in crypt_convert_block_skcipher()
1228 r = cc->iv_gen_ops->post(cc, org_iv, dmreq); in crypt_convert_block_skcipher()
1230 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); in crypt_convert_block_skcipher()
1231 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); in crypt_convert_block_skcipher()
1239 static void crypt_alloc_req_skcipher(struct crypt_config *cc, in crypt_alloc_req_skcipher() argument
1242 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1); in crypt_alloc_req_skcipher()
1245 ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO); in crypt_alloc_req_skcipher()
1247 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]); in crypt_alloc_req_skcipher()
1255 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req)); in crypt_alloc_req_skcipher()
1258 static void crypt_alloc_req_aead(struct crypt_config *cc, in crypt_alloc_req_aead() argument
1262 ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO); in crypt_alloc_req_aead()
1264 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_req_aead()
1272 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead)); in crypt_alloc_req_aead()
1275 static void crypt_alloc_req(struct crypt_config *cc, in crypt_alloc_req() argument
1278 if (crypt_integrity_aead(cc)) in crypt_alloc_req()
1279 crypt_alloc_req_aead(cc, ctx); in crypt_alloc_req()
1281 crypt_alloc_req_skcipher(cc, ctx); in crypt_alloc_req()
1284 static void crypt_free_req_skcipher(struct crypt_config *cc, in crypt_free_req_skcipher() argument
1287 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_skcipher()
1290 mempool_free(req, &cc->req_pool); in crypt_free_req_skcipher()
1293 static void crypt_free_req_aead(struct crypt_config *cc, in crypt_free_req_aead() argument
1296 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); in crypt_free_req_aead()
1299 mempool_free(req, &cc->req_pool); in crypt_free_req_aead()
1302 static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio) in crypt_free_req() argument
1304 if (crypt_integrity_aead(cc)) in crypt_free_req()
1305 crypt_free_req_aead(cc, req, base_bio); in crypt_free_req()
1307 crypt_free_req_skcipher(cc, req, base_bio); in crypt_free_req()
1313 static blk_status_t crypt_convert(struct crypt_config *cc, in crypt_convert() argument
1317 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT; in crypt_convert()
1324 crypt_alloc_req(cc, ctx); in crypt_convert()
1327 if (crypt_integrity_aead(cc)) in crypt_convert()
1328 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset); in crypt_convert()
1330 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset); in crypt_convert()
1377 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1398 struct crypt_config *cc = io->cc; in crypt_alloc_buffer() local
1407 mutex_lock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1409 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs); in crypt_alloc_buffer()
1418 page = mempool_alloc(&cc->page_pool, gfp_mask); in crypt_alloc_buffer()
1420 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1435 crypt_free_buffer_pages(cc, clone); in crypt_alloc_buffer()
1441 mutex_unlock(&cc->bio_alloc_lock); in crypt_alloc_buffer()
1446 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) in crypt_free_buffer_pages() argument
1453 mempool_free(bv->bv_page, &cc->page_pool); in crypt_free_buffer_pages()
1457 static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, in crypt_io_init() argument
1460 io->cc = cc; in crypt_io_init()
1481 struct crypt_config *cc = io->cc; in crypt_dec_pending() local
1489 crypt_free_req(cc, io->ctx.r.req, base_bio); in crypt_dec_pending()
1492 mempool_free(io->integrity_metadata, &io->cc->tag_pool); in crypt_dec_pending()
1520 struct crypt_config *cc = io->cc; in crypt_endio() local
1528 crypt_free_buffer_pages(cc, clone); in crypt_endio()
1546 struct crypt_config *cc = io->cc; in clone_init() local
1550 bio_set_dev(clone, cc->dev->bdev); in clone_init()
1556 struct crypt_config *cc = io->cc; in kcryptd_io_read() local
1565 clone = bio_clone_fast(io->base_bio, gfp, &cc->bs); in kcryptd_io_read()
1572 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_io_read()
1596 struct crypt_config *cc = io->cc; in kcryptd_queue_read() local
1599 queue_work(cc->io_queue, &io->work); in kcryptd_queue_read()
1613 struct crypt_config *cc = data; in dmcrypt_write() local
1620 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1623 if (!RB_EMPTY_ROOT(&cc->write_tree)) in dmcrypt_write()
1628 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1638 spin_lock_irq(&cc->write_thread_lock); in dmcrypt_write()
1642 write_tree = cc->write_tree; in dmcrypt_write()
1643 cc->write_tree = RB_ROOT; in dmcrypt_write()
1644 spin_unlock_irq(&cc->write_thread_lock); in dmcrypt_write()
1666 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_io_submit() local
1672 crypt_free_buffer_pages(cc, clone); in kcryptd_crypt_write_io_submit()
1681 clone->bi_iter.bi_sector = cc->start + io->sector; in kcryptd_crypt_write_io_submit()
1683 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) { in kcryptd_crypt_write_io_submit()
1688 spin_lock_irqsave(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
1689 if (RB_EMPTY_ROOT(&cc->write_tree)) in kcryptd_crypt_write_io_submit()
1690 wake_up_process(cc->write_thread); in kcryptd_crypt_write_io_submit()
1691 rbp = &cc->write_tree.rb_node; in kcryptd_crypt_write_io_submit()
1702 rb_insert_color(&io->rb_node, &cc->write_tree); in kcryptd_crypt_write_io_submit()
1703 spin_unlock_irqrestore(&cc->write_thread_lock, flags); in kcryptd_crypt_write_io_submit()
1708 struct crypt_config *cc = io->cc; in kcryptd_crypt_write_convert() local
1718 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector); in kcryptd_crypt_write_convert()
1732 r = crypt_convert(cc, &io->ctx); in kcryptd_crypt_write_convert()
1754 struct crypt_config *cc = io->cc; in kcryptd_crypt_read_convert() local
1759 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, in kcryptd_crypt_read_convert()
1762 r = crypt_convert(cc, &io->ctx); in kcryptd_crypt_read_convert()
1778 struct crypt_config *cc = io->cc; in kcryptd_async_done() local
1790 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) in kcryptd_async_done()
1791 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq); in kcryptd_async_done()
1795 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq))); in kcryptd_async_done()
1800 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); in kcryptd_async_done()
1823 struct crypt_config *cc = io->cc; in kcryptd_queue_crypt() local
1826 queue_work(cc->crypt_queue, &io->work); in kcryptd_queue_crypt()
1829 static void crypt_free_tfms_aead(struct crypt_config *cc) in crypt_free_tfms_aead() argument
1831 if (!cc->cipher_tfm.tfms_aead) in crypt_free_tfms_aead()
1834 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_free_tfms_aead()
1835 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]); in crypt_free_tfms_aead()
1836 cc->cipher_tfm.tfms_aead[0] = NULL; in crypt_free_tfms_aead()
1839 kfree(cc->cipher_tfm.tfms_aead); in crypt_free_tfms_aead()
1840 cc->cipher_tfm.tfms_aead = NULL; in crypt_free_tfms_aead()
1843 static void crypt_free_tfms_skcipher(struct crypt_config *cc) in crypt_free_tfms_skcipher() argument
1847 if (!cc->cipher_tfm.tfms) in crypt_free_tfms_skcipher()
1850 for (i = 0; i < cc->tfms_count; i++) in crypt_free_tfms_skcipher()
1851 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_free_tfms_skcipher()
1852 crypto_free_skcipher(cc->cipher_tfm.tfms[i]); in crypt_free_tfms_skcipher()
1853 cc->cipher_tfm.tfms[i] = NULL; in crypt_free_tfms_skcipher()
1856 kfree(cc->cipher_tfm.tfms); in crypt_free_tfms_skcipher()
1857 cc->cipher_tfm.tfms = NULL; in crypt_free_tfms_skcipher()
1860 static void crypt_free_tfms(struct crypt_config *cc) in crypt_free_tfms() argument
1862 if (crypt_integrity_aead(cc)) in crypt_free_tfms()
1863 crypt_free_tfms_aead(cc); in crypt_free_tfms()
1865 crypt_free_tfms_skcipher(cc); in crypt_free_tfms()
1868 static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_skcipher() argument
1873 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count, in crypt_alloc_tfms_skcipher()
1876 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_skcipher()
1879 for (i = 0; i < cc->tfms_count; i++) { in crypt_alloc_tfms_skcipher()
1880 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0); in crypt_alloc_tfms_skcipher()
1881 if (IS_ERR(cc->cipher_tfm.tfms[i])) { in crypt_alloc_tfms_skcipher()
1882 err = PTR_ERR(cc->cipher_tfm.tfms[i]); in crypt_alloc_tfms_skcipher()
1883 crypt_free_tfms(cc); in crypt_alloc_tfms_skcipher()
1891 static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms_aead() argument
1895 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL); in crypt_alloc_tfms_aead()
1896 if (!cc->cipher_tfm.tfms) in crypt_alloc_tfms_aead()
1899 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0); in crypt_alloc_tfms_aead()
1900 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) { in crypt_alloc_tfms_aead()
1901 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]); in crypt_alloc_tfms_aead()
1902 crypt_free_tfms(cc); in crypt_alloc_tfms_aead()
1909 static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) in crypt_alloc_tfms() argument
1911 if (crypt_integrity_aead(cc)) in crypt_alloc_tfms()
1912 return crypt_alloc_tfms_aead(cc, ciphermode); in crypt_alloc_tfms()
1914 return crypt_alloc_tfms_skcipher(cc, ciphermode); in crypt_alloc_tfms()
1917 static unsigned crypt_subkey_size(struct crypt_config *cc) in crypt_subkey_size() argument
1919 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); in crypt_subkey_size()
1922 static unsigned crypt_authenckey_size(struct crypt_config *cc) in crypt_authenckey_size() argument
1924 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param)); in crypt_authenckey_size()
1949 static int crypt_setkey(struct crypt_config *cc) in crypt_setkey() argument
1955 subkey_size = crypt_subkey_size(cc); in crypt_setkey()
1957 if (crypt_integrity_hmac(cc)) { in crypt_setkey()
1958 if (subkey_size < cc->key_mac_size) in crypt_setkey()
1961 crypt_copy_authenckey(cc->authenc_key, cc->key, in crypt_setkey()
1962 subkey_size - cc->key_mac_size, in crypt_setkey()
1963 cc->key_mac_size); in crypt_setkey()
1966 for (i = 0; i < cc->tfms_count; i++) { in crypt_setkey()
1967 if (crypt_integrity_hmac(cc)) in crypt_setkey()
1968 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
1969 cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
1970 else if (crypt_integrity_aead(cc)) in crypt_setkey()
1971 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], in crypt_setkey()
1972 cc->key + (i * subkey_size), in crypt_setkey()
1975 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i], in crypt_setkey()
1976 cc->key + (i * subkey_size), in crypt_setkey()
1982 if (crypt_integrity_hmac(cc)) in crypt_setkey()
1983 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc)); in crypt_setkey()
1998 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2044 if (cc->key_size != ukp->datalen) { in crypt_set_keyring_key()
2051 memcpy(cc->key, ukp->data, cc->key_size); in crypt_set_keyring_key()
2057 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2059 ret = crypt_setkey(cc); in crypt_set_keyring_key()
2062 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_keyring_key()
2063 kzfree(cc->key_string); in crypt_set_keyring_key()
2064 cc->key_string = new_key_string; in crypt_set_keyring_key()
2096 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) in crypt_set_keyring_key() argument
2108 static int crypt_set_key(struct crypt_config *cc, char *key) in crypt_set_key() argument
2114 if (!cc->key_size && strcmp(key, "-")) in crypt_set_key()
2119 r = crypt_set_keyring_key(cc, key + 1); in crypt_set_key()
2124 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2127 kzfree(cc->key_string); in crypt_set_key()
2128 cc->key_string = NULL; in crypt_set_key()
2131 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) in crypt_set_key()
2134 r = crypt_setkey(cc); in crypt_set_key()
2136 set_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_set_key()
2145 static int crypt_wipe_key(struct crypt_config *cc) in crypt_wipe_key() argument
2149 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); in crypt_wipe_key()
2150 get_random_bytes(&cc->key, cc->key_size); in crypt_wipe_key()
2151 kzfree(cc->key_string); in crypt_wipe_key()
2152 cc->key_string = NULL; in crypt_wipe_key()
2153 r = crypt_setkey(cc); in crypt_wipe_key()
2154 memset(&cc->key, 0, cc->key_size * sizeof(u8)); in crypt_wipe_key()
2174 struct crypt_config *cc = pool_data; in crypt_page_alloc() local
2177 if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) && in crypt_page_alloc()
2183 percpu_counter_add(&cc->n_allocated_pages, 1); in crypt_page_alloc()
2190 struct crypt_config *cc = pool_data; in crypt_page_free() local
2193 percpu_counter_sub(&cc->n_allocated_pages, 1); in crypt_page_free()
2198 struct crypt_config *cc = ti->private; in crypt_dtr() local
2202 if (!cc) in crypt_dtr()
2205 if (cc->write_thread) in crypt_dtr()
2206 kthread_stop(cc->write_thread); in crypt_dtr()
2208 if (cc->io_queue) in crypt_dtr()
2209 destroy_workqueue(cc->io_queue); in crypt_dtr()
2210 if (cc->crypt_queue) in crypt_dtr()
2211 destroy_workqueue(cc->crypt_queue); in crypt_dtr()
2213 crypt_free_tfms(cc); in crypt_dtr()
2215 bioset_exit(&cc->bs); in crypt_dtr()
2217 mempool_exit(&cc->page_pool); in crypt_dtr()
2218 mempool_exit(&cc->req_pool); in crypt_dtr()
2219 mempool_exit(&cc->tag_pool); in crypt_dtr()
2221 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0); in crypt_dtr()
2222 percpu_counter_destroy(&cc->n_allocated_pages); in crypt_dtr()
2224 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) in crypt_dtr()
2225 cc->iv_gen_ops->dtr(cc); in crypt_dtr()
2227 if (cc->dev) in crypt_dtr()
2228 dm_put_device(ti, cc->dev); in crypt_dtr()
2230 kzfree(cc->cipher); in crypt_dtr()
2231 kzfree(cc->cipher_string); in crypt_dtr()
2232 kzfree(cc->key_string); in crypt_dtr()
2233 kzfree(cc->cipher_auth); in crypt_dtr()
2234 kzfree(cc->authenc_key); in crypt_dtr()
2236 mutex_destroy(&cc->bio_alloc_lock); in crypt_dtr()
2239 kzfree(cc); in crypt_dtr()
2250 struct crypt_config *cc = ti->private; in crypt_ctr_ivmode() local
2252 if (crypt_integrity_aead(cc)) in crypt_ctr_ivmode()
2253 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_ivmode()
2255 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_ivmode()
2257 if (cc->iv_size) in crypt_ctr_ivmode()
2259 cc->iv_size = max(cc->iv_size, in crypt_ctr_ivmode()
2268 cc->iv_gen_ops = NULL; in crypt_ctr_ivmode()
2270 cc->iv_gen_ops = &crypt_iv_plain_ops; in crypt_ctr_ivmode()
2272 cc->iv_gen_ops = &crypt_iv_plain64_ops; in crypt_ctr_ivmode()
2274 cc->iv_gen_ops = &crypt_iv_plain64be_ops; in crypt_ctr_ivmode()
2276 cc->iv_gen_ops = &crypt_iv_essiv_ops; in crypt_ctr_ivmode()
2278 cc->iv_gen_ops = &crypt_iv_benbi_ops; in crypt_ctr_ivmode()
2280 cc->iv_gen_ops = &crypt_iv_null_ops; in crypt_ctr_ivmode()
2282 cc->iv_gen_ops = &crypt_iv_lmk_ops; in crypt_ctr_ivmode()
2289 if (cc->key_size % cc->key_parts) { in crypt_ctr_ivmode()
2290 cc->key_parts++; in crypt_ctr_ivmode()
2291 cc->key_extra_size = cc->key_size / cc->key_parts; in crypt_ctr_ivmode()
2294 cc->iv_gen_ops = &crypt_iv_tcw_ops; in crypt_ctr_ivmode()
2295 cc->key_parts += 2; /* IV + whitening */ in crypt_ctr_ivmode()
2296 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; in crypt_ctr_ivmode()
2298 cc->iv_gen_ops = &crypt_iv_random_ops; in crypt_ctr_ivmode()
2300 cc->integrity_iv_size = cc->iv_size; in crypt_ctr_ivmode()
2314 static int crypt_ctr_blkdev_cipher(struct crypt_config *cc) in crypt_ctr_blkdev_cipher() argument
2319 if (crypt_integrity_aead(cc)) { in crypt_ctr_blkdev_cipher()
2320 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc))); in crypt_ctr_blkdev_cipher()
2323 if (crypt_integrity_hmac(cc)) { in crypt_ctr_blkdev_cipher()
2330 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc))); in crypt_ctr_blkdev_cipher()
2339 cc->cipher = kstrdup(alg_name, GFP_KERNEL); in crypt_ctr_blkdev_cipher()
2340 return cc->cipher ? 0 : -ENOMEM; in crypt_ctr_blkdev_cipher()
2346 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL); in crypt_ctr_blkdev_cipher()
2347 if (!cc->cipher) in crypt_ctr_blkdev_cipher()
2350 strncpy(cc->cipher, start, end - start); in crypt_ctr_blkdev_cipher()
2360 static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) in crypt_ctr_auth_cipher() argument
2384 cc->key_mac_size = crypto_ahash_digestsize(mac); in crypt_ctr_auth_cipher()
2387 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL); in crypt_ctr_auth_cipher()
2388 if (!cc->authenc_key) in crypt_ctr_auth_cipher()
2397 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_new() local
2401 cc->tfms_count = 1; in crypt_ctr_cipher_new()
2413 cc->tfms_count = 64; in crypt_ctr_cipher_new()
2415 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_new()
2418 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_new()
2425 if (crypt_integrity_aead(cc)) { in crypt_ctr_cipher_new()
2426 ret = crypt_ctr_auth_cipher(cc, cipher_api); in crypt_ctr_cipher_new()
2431 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); in crypt_ctr_cipher_new()
2433 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); in crypt_ctr_cipher_new()
2435 ret = crypt_ctr_blkdev_cipher(cc); in crypt_ctr_cipher_new()
2447 struct crypt_config *cc = ti->private; in crypt_ctr_cipher_old() local
2453 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) { in crypt_ctr_cipher_old()
2467 cc->tfms_count = 1; in crypt_ctr_cipher_old()
2468 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || in crypt_ctr_cipher_old()
2469 !is_power_of_2(cc->tfms_count)) { in crypt_ctr_cipher_old()
2473 cc->key_parts = cc->tfms_count; in crypt_ctr_cipher_old()
2475 cc->cipher = kstrdup(cipher, GFP_KERNEL); in crypt_ctr_cipher_old()
2476 if (!cc->cipher) in crypt_ctr_cipher_old()
2512 ret = crypt_alloc_tfms(cc, cipher_api); in crypt_ctr_cipher_old()
2528 struct crypt_config *cc = ti->private; in crypt_ctr_cipher() local
2532 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); in crypt_ctr_cipher()
2533 if (!cc->cipher_string) { in crypt_ctr_cipher()
2551 ret = crypt_set_key(cc, key); in crypt_ctr_cipher()
2558 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { in crypt_ctr_cipher()
2559 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); in crypt_ctr_cipher()
2567 if (cc->iv_gen_ops && cc->iv_gen_ops->init) { in crypt_ctr_cipher()
2568 ret = cc->iv_gen_ops->init(cc); in crypt_ctr_cipher()
2576 if (cc->key_string) in crypt_ctr_cipher()
2577 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_ctr_cipher()
2584 struct crypt_config *cc = ti->private; in crypt_ctr_optional() local
2613 set_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_ctr_optional()
2616 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_ctr_optional()
2622 cc->on_disk_tag_size = val; in crypt_ctr_optional()
2625 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); in crypt_ctr_optional()
2631 cc->cipher_auth = kstrdup(sval, GFP_KERNEL); in crypt_ctr_optional()
2632 if (!cc->cipher_auth) in crypt_ctr_optional()
2634 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { in crypt_ctr_optional()
2635 if (cc->sector_size < (1 << SECTOR_SHIFT) || in crypt_ctr_optional()
2636 cc->sector_size > 4096 || in crypt_ctr_optional()
2637 (cc->sector_size & (cc->sector_size - 1))) { in crypt_ctr_optional()
2641 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) { in crypt_ctr_optional()
2645 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT; in crypt_ctr_optional()
2647 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_ctr_optional()
2663 struct crypt_config *cc; in crypt_ctr() local
2682 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL); in crypt_ctr()
2683 if (!cc) { in crypt_ctr()
2687 cc->key_size = key_size; in crypt_ctr()
2688 cc->sector_size = (1 << SECTOR_SHIFT); in crypt_ctr()
2689 cc->sector_shift = 0; in crypt_ctr()
2691 ti->private = cc; in crypt_ctr()
2698 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); in crypt_ctr()
2713 if (crypt_integrity_aead(cc)) { in crypt_ctr()
2714 cc->dmreq_start = sizeof(struct aead_request); in crypt_ctr()
2715 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc)); in crypt_ctr()
2716 align_mask = crypto_aead_alignmask(any_tfm_aead(cc)); in crypt_ctr()
2718 cc->dmreq_start = sizeof(struct skcipher_request); in crypt_ctr()
2719 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); in crypt_ctr()
2720 align_mask = crypto_skcipher_alignmask(any_tfm(cc)); in crypt_ctr()
2722 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); in crypt_ctr()
2726 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) in crypt_ctr()
2739 iv_size_padding + cc->iv_size + in crypt_ctr()
2740 cc->iv_size + in crypt_ctr()
2744 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size); in crypt_ctr()
2750 cc->per_bio_data_size = ti->per_io_data_size = in crypt_ctr()
2751 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size, in crypt_ctr()
2754 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc); in crypt_ctr()
2760 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS); in crypt_ctr()
2766 mutex_init(&cc->bio_alloc_lock); in crypt_ctr()
2770 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { in crypt_ctr()
2774 cc->iv_offset = tmpll; in crypt_ctr()
2776 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); in crypt_ctr()
2787 cc->start = tmpll; in crypt_ctr()
2789 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) { in crypt_ctr()
2790 ret = crypt_integrity_ctr(cc, ti); in crypt_ctr()
2794 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size; in crypt_ctr()
2795 if (!cc->tag_pool_max_sectors) in crypt_ctr()
2796 cc->tag_pool_max_sectors = 1; in crypt_ctr()
2798 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS, in crypt_ctr()
2799 cc->tag_pool_max_sectors * cc->on_disk_tag_size); in crypt_ctr()
2805 cc->tag_pool_max_sectors <<= cc->sector_shift; in crypt_ctr()
2809 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1); in crypt_ctr()
2810 if (!cc->io_queue) { in crypt_ctr()
2815 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_ctr()
2816 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1); in crypt_ctr()
2818 cc->crypt_queue = alloc_workqueue("kcryptd", in crypt_ctr()
2821 if (!cc->crypt_queue) { in crypt_ctr()
2826 spin_lock_init(&cc->write_thread_lock); in crypt_ctr()
2827 cc->write_tree = RB_ROOT; in crypt_ctr()
2829 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write"); in crypt_ctr()
2830 if (IS_ERR(cc->write_thread)) { in crypt_ctr()
2831 ret = PTR_ERR(cc->write_thread); in crypt_ctr()
2832 cc->write_thread = NULL; in crypt_ctr()
2836 wake_up_process(cc->write_thread); in crypt_ctr()
2850 struct crypt_config *cc = ti->private; in crypt_map() local
2859 bio_set_dev(bio, cc->dev->bdev); in crypt_map()
2861 bio->bi_iter.bi_sector = cc->start + in crypt_map()
2870 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size)) in crypt_map()
2877 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) in crypt_map()
2880 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) in crypt_map()
2883 io = dm_per_bio_data(bio, cc->per_bio_data_size); in crypt_map()
2884 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); in crypt_map()
2886 if (cc->on_disk_tag_size) { in crypt_map()
2887 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift); in crypt_map()
2892 if (bio_sectors(bio) > cc->tag_pool_max_sectors) in crypt_map()
2893 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors); in crypt_map()
2894 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO); in crypt_map()
2899 if (crypt_integrity_aead(cc)) in crypt_map()
2916 struct crypt_config *cc = ti->private; in crypt_status() local
2926 DMEMIT("%s ", cc->cipher_string); in crypt_status()
2928 if (cc->key_size > 0) { in crypt_status()
2929 if (cc->key_string) in crypt_status()
2930 DMEMIT(":%u:%s", cc->key_size, cc->key_string); in crypt_status()
2932 for (i = 0; i < cc->key_size; i++) in crypt_status()
2933 DMEMIT("%02x", cc->key[i]); in crypt_status()
2937 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, in crypt_status()
2938 cc->dev->name, (unsigned long long)cc->start); in crypt_status()
2941 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); in crypt_status()
2942 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); in crypt_status()
2943 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT); in crypt_status()
2944 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); in crypt_status()
2945 if (cc->on_disk_tag_size) in crypt_status()
2951 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) in crypt_status()
2953 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) in crypt_status()
2955 if (cc->on_disk_tag_size) in crypt_status()
2956 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth); in crypt_status()
2957 if (cc->sector_size != (1 << SECTOR_SHIFT)) in crypt_status()
2958 DMEMIT(" sector_size:%d", cc->sector_size); in crypt_status()
2959 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) in crypt_status()
2969 struct crypt_config *cc = ti->private; in crypt_postsuspend() local
2971 set_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_postsuspend()
2976 struct crypt_config *cc = ti->private; in crypt_preresume() local
2978 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { in crypt_preresume()
2988 struct crypt_config *cc = ti->private; in crypt_resume() local
2990 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); in crypt_resume()
3000 struct crypt_config *cc = ti->private; in crypt_message() local
3007 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { in crypt_message()
3014 if (key_size < 0 || cc->key_size != key_size) { in crypt_message()
3019 ret = crypt_set_key(cc, argv[2]); in crypt_message()
3022 if (cc->iv_gen_ops && cc->iv_gen_ops->init) in crypt_message()
3023 ret = cc->iv_gen_ops->init(cc); in crypt_message()
3025 if (cc->key_string) in crypt_message()
3026 memset(cc->key, 0, cc->key_size * sizeof(u8)); in crypt_message()
3030 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { in crypt_message()
3031 ret = cc->iv_gen_ops->wipe(cc); in crypt_message()
3035 return crypt_wipe_key(cc); in crypt_message()
3047 struct crypt_config *cc = ti->private; in crypt_iterate_devices() local
3049 return fn(ti, cc->dev, cc->start, ti->len, data); in crypt_iterate_devices()
3054 struct crypt_config *cc = ti->private; in crypt_io_hints() local
3065 max_t(unsigned short, limits->logical_block_size, cc->sector_size); in crypt_io_hints()
3067 max_t(unsigned, limits->physical_block_size, cc->sector_size); in crypt_io_hints()
3068 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size); in crypt_io_hints()