]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - security/keys/encrypted-keys/encrypted.c
Merge branch 'ufs-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[karo-tx-linux.git] / security / keys / encrypted-keys / encrypted.c
index 0f7b95de3b5f1762f7caa84d319c282e06300ebe..bb6324d1ccec32f6dde05f520d8d2ed2e089c785 100644 (file)
@@ -30,6 +30,7 @@
 #include <linux/scatterlist.h>
 #include <linux/ctype.h>
 #include <crypto/aes.h>
+#include <crypto/algapi.h>
 #include <crypto/hash.h>
 #include <crypto/sha.h>
 #include <crypto/skcipher.h>
@@ -54,13 +55,7 @@ static int blksize;
 #define MAX_DATA_SIZE 4096
 #define MIN_DATA_SIZE  20
 
-struct sdesc {
-       struct shash_desc shash;
-       char ctx[];
-};
-
-static struct crypto_shash *hashalg;
-static struct crypto_shash *hmacalg;
+static struct crypto_shash *hash_tfm;
 
 enum {
        Opt_err = -1, Opt_new, Opt_load, Opt_update
@@ -320,53 +315,38 @@ error:
        return ukey;
 }
 
-static struct sdesc *alloc_sdesc(struct crypto_shash *alg)
-{
-       struct sdesc *sdesc;
-       int size;
-
-       size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
-       sdesc = kmalloc(size, GFP_KERNEL);
-       if (!sdesc)
-               return ERR_PTR(-ENOMEM);
-       sdesc->shash.tfm = alg;
-       sdesc->shash.flags = 0x0;
-       return sdesc;
-}
-
-static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
+static int calc_hash(struct crypto_shash *tfm, u8 *digest,
                     const u8 *buf, unsigned int buflen)
 {
-       struct sdesc *sdesc;
-       int ret;
+       SHASH_DESC_ON_STACK(desc, tfm);
+       int err;
 
-       sdesc = alloc_sdesc(hmacalg);
-       if (IS_ERR(sdesc)) {
-               pr_info("encrypted_key: can't alloc %s\n", hmac_alg);
-               return PTR_ERR(sdesc);
-       }
+       desc->tfm = tfm;
+       desc->flags = 0;
 
-       ret = crypto_shash_setkey(hmacalg, key, keylen);
-       if (!ret)
-               ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
-       kfree(sdesc);
-       return ret;
+       err = crypto_shash_digest(desc, buf, buflen, digest);
+       shash_desc_zero(desc);
+       return err;
 }
 
-static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen)
+static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
+                    const u8 *buf, unsigned int buflen)
 {
-       struct sdesc *sdesc;
-       int ret;
+       struct crypto_shash *tfm;
+       int err;
 
-       sdesc = alloc_sdesc(hashalg);
-       if (IS_ERR(sdesc)) {
-               pr_info("encrypted_key: can't alloc %s\n", hash_alg);
-               return PTR_ERR(sdesc);
+       tfm = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
+       if (IS_ERR(tfm)) {
+               pr_err("encrypted_key: can't alloc %s transform: %ld\n",
+                      hmac_alg, PTR_ERR(tfm));
+               return PTR_ERR(tfm);
        }
 
-       ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
-       kfree(sdesc);
-       return ret;
+       err = crypto_shash_setkey(tfm, key, keylen);
+       if (!err)
+               err = calc_hash(tfm, digest, buf, buflen);
+       crypto_free_shash(tfm);
+       return err;
 }
 
 enum derived_key_type { ENC_KEY, AUTH_KEY };
@@ -394,8 +374,8 @@ static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
 
        memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
               master_keylen);
-       ret = calc_hash(derived_key, derived_buf, derived_buf_len);
-       kfree(derived_buf);
+       ret = calc_hash(hash_tfm, derived_key, derived_buf, derived_buf_len);
+       kzfree(derived_buf);
        return ret;
 }
 
@@ -527,6 +507,7 @@ static int datablob_hmac_append(struct encrypted_key_payload *epayload,
        if (!ret)
                dump_hmac(NULL, digest, HASH_SIZE);
 out:
+       memzero_explicit(derived_key, sizeof(derived_key));
        return ret;
 }
 
@@ -555,8 +536,8 @@ static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
        ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
        if (ret < 0)
                goto out;
-       ret = memcmp(digest, epayload->format + epayload->datablob_len,
-                    sizeof digest);
+       ret = crypto_memneq(digest, epayload->format + epayload->datablob_len,
+                           sizeof(digest));
        if (ret) {
                ret = -EINVAL;
                dump_hmac("datablob",
@@ -565,6 +546,7 @@ static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
                dump_hmac("calc", digest, HASH_SIZE);
        }
 out:
+       memzero_explicit(derived_key, sizeof(derived_key));
        return ret;
 }
 
@@ -721,6 +703,7 @@ static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
 out:
        up_read(&mkey->sem);
        key_put(mkey);
+       memzero_explicit(derived_key, sizeof(derived_key));
        return ret;
 }
 
@@ -827,13 +810,13 @@ static int encrypted_instantiate(struct key *key,
        ret = encrypted_init(epayload, key->description, format, master_desc,
                             decrypted_datalen, hex_encoded_iv);
        if (ret < 0) {
-               kfree(epayload);
+               kzfree(epayload);
                goto out;
        }
 
        rcu_assign_keypointer(key, epayload);
 out:
-       kfree(datablob);
+       kzfree(datablob);
        return ret;
 }
 
@@ -842,8 +825,7 @@ static void encrypted_rcu_free(struct rcu_head *rcu)
        struct encrypted_key_payload *epayload;
 
        epayload = container_of(rcu, struct encrypted_key_payload, rcu);
-       memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
-       kfree(epayload);
+       kzfree(epayload);
 }
 
 /*
@@ -901,7 +883,7 @@ static int encrypted_update(struct key *key, struct key_preparsed_payload *prep)
        rcu_assign_keypointer(key, new_epayload);
        call_rcu(&epayload->rcu, encrypted_rcu_free);
 out:
-       kfree(buf);
+       kzfree(buf);
        return ret;
 }
 
@@ -959,33 +941,26 @@ static long encrypted_read(const struct key *key, char __user *buffer,
 
        up_read(&mkey->sem);
        key_put(mkey);
+       memzero_explicit(derived_key, sizeof(derived_key));
 
        if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
                ret = -EFAULT;
-       kfree(ascii_buf);
+       kzfree(ascii_buf);
 
        return asciiblob_len;
 out:
        up_read(&mkey->sem);
        key_put(mkey);
+       memzero_explicit(derived_key, sizeof(derived_key));
        return ret;
 }
 
 /*
- * encrypted_destroy - before freeing the key, clear the decrypted data
- *
- * Before freeing the key, clear the memory containing the decrypted
- * key data.
+ * encrypted_destroy - clear and free the key's payload
  */
 static void encrypted_destroy(struct key *key)
 {
-       struct encrypted_key_payload *epayload = key->payload.data[0];
-
-       if (!epayload)
-               return;
-
-       memzero_explicit(epayload->decrypted_data, epayload->decrypted_datalen);
-       kfree(key->payload.data[0]);
+       kzfree(key->payload.data[0]);
 }
 
 struct key_type key_type_encrypted = {
@@ -998,47 +973,17 @@ struct key_type key_type_encrypted = {
 };
 EXPORT_SYMBOL_GPL(key_type_encrypted);
 
-static void encrypted_shash_release(void)
-{
-       if (hashalg)
-               crypto_free_shash(hashalg);
-       if (hmacalg)
-               crypto_free_shash(hmacalg);
-}
-
-static int __init encrypted_shash_alloc(void)
+static int __init init_encrypted(void)
 {
        int ret;
 
-       hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
-       if (IS_ERR(hmacalg)) {
-               pr_info("encrypted_key: could not allocate crypto %s\n",
-                       hmac_alg);
-               return PTR_ERR(hmacalg);
+       hash_tfm = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
+       if (IS_ERR(hash_tfm)) {
+               pr_err("encrypted_key: can't allocate %s transform: %ld\n",
+                      hash_alg, PTR_ERR(hash_tfm));
+               return PTR_ERR(hash_tfm);
        }
 
-       hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
-       if (IS_ERR(hashalg)) {
-               pr_info("encrypted_key: could not allocate crypto %s\n",
-                       hash_alg);
-               ret = PTR_ERR(hashalg);
-               goto hashalg_fail;
-       }
-
-       return 0;
-
-hashalg_fail:
-       crypto_free_shash(hmacalg);
-       return ret;
-}
-
-static int __init init_encrypted(void)
-{
-       int ret;
-
-       ret = encrypted_shash_alloc();
-       if (ret < 0)
-               return ret;
        ret = aes_get_sizes();
        if (ret < 0)
                goto out;
@@ -1047,14 +992,14 @@ static int __init init_encrypted(void)
                goto out;
        return 0;
 out:
-       encrypted_shash_release();
+       crypto_free_shash(hash_tfm);
        return ret;
 
 }
 
 static void __exit cleanup_encrypted(void)
 {
-       encrypted_shash_release();
+       crypto_free_shash(hash_tfm);
        unregister_key_type(&key_type_encrypted);
 }