二、dm-crypt分析
dm-crypt是dm构架中用于块设备加密的模块。dm-crypt通过dm虚拟一个块设备,并在bio转发的时候将数据加密后存储来实现块设备的加密,而这些对于应用层是透明的。dm-crypt的target_type定义如下:
- static struct target_type crypt_target = {
- .name = "crypt",
- .version = {1, 7, 0},
- .module = THIS_MODULE,
- .ctr = crypt_ctr,
- .dtr = crypt_dtr,
- .map = crypt_map,
- .status = crypt_status,
- .postsuspend = crypt_postsuspend,
- .preresume = crypt_preresume,
- .resume = crypt_resume,
- .message = crypt_message,
- .merge = crypt_merge,
- .iterate_devices = crypt_iterate_devices,
- };
这里重点分析ctr和map函数。ctr决定了设备的创建过程、也决定了与密码算法的关联过程;map决定了bio转发,也决定了对密码算法调用的步骤。设备创建和bio转发在前文中已经讲过。这里重点分析与密码算法的关联。
2.1 创建密码算法实例
crypt_ctr函数的代码很长,我贴在这里,一般情况下就没必要展开了。
- static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
- {
- struct crypt_config *cc;
- struct crypto_ablkcipher *tfm;
- char *tmp;
- char *cipher;
- char *chainmode;
- char *ivmode;
- char *ivopts;
- unsigned int key_size;
- unsigned long long tmpll;
- if (argc != 5) {
- ti->error = "Not enough arguments";
- return -EINVAL;
- }
- tmp = argv[0];
- cipher = strsep(&tmp, "-");
- chainmode = strsep(&tmp, "-");
- ivopts = strsep(&tmp, "-");
- ivmode = strsep(&ivopts, ":");
- if (tmp)
- DMWARN("Unexpected additional cipher options");
- key_size = strlen(argv[1]) >> 1;
- cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
- if (cc == NULL) {
- ti->error =
- "Cannot allocate transparent encryption context";
- return -ENOMEM;
- }
- /* Compatibility mode for old dm-crypt cipher strings */
- if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
- chainmode = "cbc";
- ivmode = "plain";
- }
- if (strcmp(chainmode, "ecb") && !ivmode) {
- ti->error = "This chaining mode requires an IV mechanism";
- goto bad_cipher;
- }
- if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)",
- chainmode, cipher) >= CRYPTO_MAX_ALG_NAME) {
- ti->error = "Chain mode + cipher name is too long";
- goto bad_cipher;
- }
- tfm = crypto_alloc_ablkcipher(cc->cipher, 0, 0);
- if (IS_ERR(tfm)) {
- ti->error = "Error allocating crypto tfm";
- goto bad_cipher;
- }
- strcpy(cc->cipher, cipher);
- strcpy(cc->chainmode, chainmode);
- cc->tfm = tfm;
- if (crypt_set_key(cc, argv[1]) < 0) {
- ti->error = "Error decoding and setting key";
- goto bad_ivmode;
- }
- /*
- * Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi".
- * See comments at iv code
- */
- if (ivmode == NULL)
- cc->iv_gen_ops = NULL;
- else if (strcmp(ivmode, "plain") == 0)
- cc->iv_gen_ops = &crypt_iv_plain_ops;
- else if (strcmp(ivmode, "plain64") == 0)
- cc->iv_gen_ops = &crypt_iv_plain64_ops;
- else if (strcmp(ivmode, "essiv") == 0)
- cc->iv_gen_ops = &crypt_iv_essiv_ops;
- else if (strcmp(ivmode, "benbi") == 0)
- cc->iv_gen_ops = &crypt_iv_benbi_ops;
- else if (strcmp(ivmode, "null") == 0)
- cc->iv_gen_ops = &crypt_iv_null_ops;
- else {
- ti->error = "Invalid IV mode";
- goto bad_ivmode;
- }
- if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
- cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
- goto bad_ivmode;
- if (cc->iv_gen_ops && cc->iv_gen_ops->init &&
- cc->iv_gen_ops->init(cc) < 0) {
- ti->error = "Error initialising IV";
- goto bad_slab_pool;
- }
- cc->iv_size = crypto_ablkcipher_ivsize(tfm);
- if (cc->iv_size)
- /* at least a 64 bit sector number should fit in our buffer */
- cc->iv_size = max(cc->iv_size,
- (unsigned int)(sizeof(u64) / sizeof(u8)));
- else {
- if (cc->iv_gen_ops) {
- DMWARN("Selected cipher does not support IVs");
- if (cc->iv_gen_ops->dtr)
- cc->iv_gen_ops->dtr(cc);
- cc->iv_gen_ops = NULL;
- }
- }
- cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
- if (!cc->io_pool) {
- ti->error = "Cannot allocate crypt io mempool";
- goto bad_slab_pool;
- }
- cc->dmreq_start = sizeof(struct ablkcipher_request);
- cc->dmreq_start += crypto_ablkcipher_reqsize(tfm);
- cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
- cc->dmreq_start += crypto_ablkcipher_alignmask(tfm) &
- ~(crypto_tfm_ctx_alignment() - 1);
- cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
- sizeof(struct dm_crypt_request) + cc->iv_size);
- if (!cc->req_pool) {
- ti->error = "Cannot allocate crypt request mempool";
- goto bad_req_pool;
- }
- cc->req = NULL;
- cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
- if (!cc->page_pool) {
- ti->error = "Cannot allocate page mempool";
- goto bad_page_pool;
- }
- cc->bs = bioset_create(MIN_IOS, 0);
- if (!cc->bs) {
- ti->error = "Cannot allocate crypt bioset";
- goto bad_bs;
- }
- if (sscanf(argv[2], "%llu", &tmpll) != 1) {
- ti->error = "Invalid iv_offset sector";
- goto bad_device;
- }
- cc->iv_offset = tmpll;
- if (sscanf(argv[4], "%llu", &tmpll) != 1) {
- ti->error = "Invalid device sector";
- goto bad_device;
- }
- cc->start = tmpll;
- if (dm_get_device(ti, argv[3], cc->start, ti->len,
- dm_table_get_mode(ti->table), &cc->dev)) {
- ti->error = "Device lookup failed";
- goto bad_device;
- }
- if (ivmode && cc->iv_gen_ops) {
- if (ivopts)
- *(ivopts - 1) = ':';
- cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
- if (!cc->iv_mode) {
- ti->error = "Error kmallocing iv_mode string";
- goto bad_ivmode_string;
- }
- strcpy(cc->iv_mode, ivmode);
- } else
- cc->iv_mode = NULL;
- cc->io_queue = create_singlethread_workqueue("kcryptd_io");
- if (!cc->io_queue) {
- ti->error = "Couldn't create kcryptd io queue";
- goto bad_io_queue;
- }
- cc->crypt_queue = create_singlethread_workqueue("kcryptd");
- if (!cc->crypt_queue) {
- ti->error = "Couldn't create kcryptd queue";
- goto bad_crypt_queue;
- }
- ti->num_flush_requests = 1;
- ti->private = cc;
- return 0;
- bad_crypt_queue:
- destroy_workqueue(cc->io_queue);
- bad_io_queue:
- kfree(cc->iv_mode);
- bad_ivmode_string:
- dm_put_device(ti, cc->dev);
- bad_device:
- bioset_free(cc->bs);
- bad_bs:
- mempool_destroy(cc->page_pool);
- bad_page_pool:
- mempool_destroy(cc->req_pool);
- bad_req_pool:
- mempool_destroy(cc->io_pool);
- bad_slab_pool:
- if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
- cc->iv_gen_ops->dtr(cc);
- bad_ivmode:
- crypto_free_ablkcipher(tfm);
- bad_cipher:
- /* Must zero key material before freeing */
- kzfree(cc);
- return -EINVAL;
- }
crypt_ctr的参数格式是<cipher> <key> <iv_offset> <dev_path> <start>,这些参数在ctr中被一一解析并存放到crypt_config结构中。
<cipher>的格式是cipher-chainmode-ivopts:ivmode。cipher就是算法注册时的cra_name;chainmode就是前面所说的ecb/cbc之类。chainmode默认是cbc,如果chainmode不是ecb,则必须指定ivmode。ivmode有5种:plain、plain64、essiv、benbi和null,分别对应不同的iv生成算法,而ivopts是传给这几种ivmode的ctr的参数,其中null、benbi、plain和plain64没有使用,而essiv将ivopts作为在系统中注册的哈希算法名,由该哈希算法生成iv。
<start>是加密的起始块,start之前不由dm-crypt管理。
<iv_offset>是为了保存iv到磁盘上而预留位置(单位:sector)。因此dm-crypt设备上偏移为sector的bio对应与原始磁盘上sector+<iv_offset>+<start>偏移的块。但是对于dm-crypt内部来讲,偏移为sector+<iv_offset>,也就是说dm-crypt内部将iv所占据的那些块给隐藏了。但是我没有在代码中发现dm-crypt使用了<start>到<iv_offset>之间的空间。
