Commit da31a078 authored by Milan Broz's avatar Milan Broz Committed by Mike Snitzer

dm crypt: properly handle extra key string in initialization

Some encryption modes use extra keys (e.g. loopAES has IV seed) which
are not used in block cipher initialization but are part of key string
in table constructor.

This patch adds an additional field which describes the length of the
extra key(s) and substracts it before real key encryption setting.

The key_size always includes the size, in bytes, of the key provided
in mapping table.

The key_parts describes how many parts (usually keys) are contained in
the whole key buffer.  And key_extra_size contains size in bytes of
additional keys part (this number of bytes must be subtracted because it
is processed by the IV generator).

| K1 | K2 | .... | K64 |      Kiv       |
|----------- key_size ----------------- |
|                      |-key_extra_size-|
|     [64 keys]        |  [1 key]       | => key_parts = 65

Example where key string contains main key K, whitening key
Kw and IV seed Kiv:

|     K       |   Kiv   |       Kw      |
|--------------- key_size --------------|
|             |-----key_extra_size------|
|  [1 key]    | [1 key] |     [1 key]   | => key_parts = 3

Because key_extra_size is calculated during IV mode setting, key
initialization is moved after this step.

For now, this change has no effect to supported modes (thanks to ilog2
rounding) but it is required by the following patch.

Also, fix a sparse warning in crypt_iv_lmk_one().
Signed-off-by: default avatarMilan Broz <gmazyland@gmail.com>
Signed-off-by: default avatarMike Snitzer <snitzer@redhat.com>
parent 2c2263c9
...@@ -171,7 +171,8 @@ struct crypt_config { ...@@ -171,7 +171,8 @@ struct crypt_config {
unsigned long flags; unsigned long flags;
unsigned int key_size; unsigned int key_size;
unsigned int key_parts; unsigned int key_parts; /* independent parts in key buffer */
unsigned int key_extra_size; /* additional keys length */
u8 key[0]; u8 key[0];
}; };
...@@ -530,7 +531,7 @@ static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, ...@@ -530,7 +531,7 @@ static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
char ctx[crypto_shash_descsize(lmk->hash_tfm)]; char ctx[crypto_shash_descsize(lmk->hash_tfm)];
} sdesc; } sdesc;
struct md5_state md5state; struct md5_state md5state;
u32 buf[4]; __le32 buf[4];
int i, r; int i, r;
sdesc.desc.tfm = lmk->hash_tfm; sdesc.desc.tfm = lmk->hash_tfm;
...@@ -1274,9 +1275,12 @@ static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) ...@@ -1274,9 +1275,12 @@ static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
static int crypt_setkey_allcpus(struct crypt_config *cc) static int crypt_setkey_allcpus(struct crypt_config *cc)
{ {
unsigned subkey_size = cc->key_size >> ilog2(cc->tfms_count); unsigned subkey_size;
int err = 0, i, r; int err = 0, i, r;
/* Ignore extra keys (which are used for IV etc) */
subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
for (i = 0; i < cc->tfms_count; i++) { for (i = 0; i < cc->tfms_count; i++) {
r = crypto_ablkcipher_setkey(cc->tfms[i], r = crypto_ablkcipher_setkey(cc->tfms[i],
cc->key + (i * subkey_size), cc->key + (i * subkey_size),
...@@ -1409,6 +1413,7 @@ static int crypt_ctr_cipher(struct dm_target *ti, ...@@ -1409,6 +1413,7 @@ static int crypt_ctr_cipher(struct dm_target *ti,
return -EINVAL; return -EINVAL;
} }
cc->key_parts = cc->tfms_count; cc->key_parts = cc->tfms_count;
cc->key_extra_size = 0;
cc->cipher = kstrdup(cipher, GFP_KERNEL); cc->cipher = kstrdup(cipher, GFP_KERNEL);
if (!cc->cipher) if (!cc->cipher)
...@@ -1460,13 +1465,6 @@ static int crypt_ctr_cipher(struct dm_target *ti, ...@@ -1460,13 +1465,6 @@ static int crypt_ctr_cipher(struct dm_target *ti,
goto bad; goto bad;
} }
/* Initialize and set key */
ret = crypt_set_key(cc, key);
if (ret < 0) {
ti->error = "Error decoding and setting key";
goto bad;
}
/* Initialize IV */ /* Initialize IV */
cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc)); cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
if (cc->iv_size) if (cc->iv_size)
...@@ -1497,14 +1495,23 @@ static int crypt_ctr_cipher(struct dm_target *ti, ...@@ -1497,14 +1495,23 @@ static int crypt_ctr_cipher(struct dm_target *ti,
* to length of provided multi-key string. * to length of provided multi-key string.
* If present (version 3), last key is used as IV seed. * If present (version 3), last key is used as IV seed.
*/ */
if (cc->key_size % cc->key_parts) if (cc->key_size % cc->key_parts) {
cc->key_parts++; cc->key_parts++;
cc->key_extra_size = cc->key_size / cc->key_parts;
}
} else { } else {
ret = -EINVAL; ret = -EINVAL;
ti->error = "Invalid IV mode"; ti->error = "Invalid IV mode";
goto bad; goto bad;
} }
/* Initialize and set key */
ret = crypt_set_key(cc, key);
if (ret < 0) {
ti->error = "Error decoding and setting key";
goto bad;
}
/* Allocate IV */ /* Allocate IV */
if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment