Merge branch 'next' into for-linus
[linux-2.6.git] / security / integrity / evm / evm_crypto.c
index 8738def..49a464f 100644 (file)
@@ -26,44 +26,56 @@ static unsigned char evmkey[MAX_KEY_SIZE];
 static int evmkey_len = MAX_KEY_SIZE;
 
 struct crypto_shash *hmac_tfm;
+struct crypto_shash *hash_tfm;
 
 static DEFINE_MUTEX(mutex);
 
-static struct shash_desc *init_desc(void)
+static struct shash_desc *init_desc(char type)
 {
-       int rc;
+       long rc;
+       char *algo;
+       struct crypto_shash **tfm;
        struct shash_desc *desc;
 
-       if (hmac_tfm == NULL) {
+       if (type == EVM_XATTR_HMAC) {
+               tfm = &hmac_tfm;
+               algo = evm_hmac;
+       } else {
+               tfm = &hash_tfm;
+               algo = evm_hash;
+       }
+
+       if (*tfm == NULL) {
                mutex_lock(&mutex);
-               if (hmac_tfm)
+               if (*tfm)
                        goto out;
-               hmac_tfm = crypto_alloc_shash(evm_hmac, 0, CRYPTO_ALG_ASYNC);
-               if (IS_ERR(hmac_tfm)) {
-                       pr_err("Can not allocate %s (reason: %ld)\n",
-                              evm_hmac, PTR_ERR(hmac_tfm));
-                       rc = PTR_ERR(hmac_tfm);
-                       hmac_tfm = NULL;
+               *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
+               if (IS_ERR(*tfm)) {
+                       rc = PTR_ERR(*tfm);
+                       pr_err("Can not allocate %s (reason: %ld)\n", algo, rc);
+                       *tfm = NULL;
                        mutex_unlock(&mutex);
                        return ERR_PTR(rc);
                }
-               rc = crypto_shash_setkey(hmac_tfm, evmkey, evmkey_len);
-               if (rc) {
-                       crypto_free_shash(hmac_tfm);
-                       hmac_tfm = NULL;
-                       mutex_unlock(&mutex);
-                       return ERR_PTR(rc);
+               if (type == EVM_XATTR_HMAC) {
+                       rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
+                       if (rc) {
+                               crypto_free_shash(*tfm);
+                               *tfm = NULL;
+                               mutex_unlock(&mutex);
+                               return ERR_PTR(rc);
+                       }
                }
 out:
                mutex_unlock(&mutex);
        }
 
-       desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac_tfm),
+       desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
                        GFP_KERNEL);
        if (!desc)
                return ERR_PTR(-ENOMEM);
 
-       desc->tfm = hmac_tfm;
+       desc->tfm = *tfm;
        desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
 
        rc = crypto_shash_init(desc);
@@ -108,9 +120,11 @@ static void hmac_add_misc(struct shash_desc *desc, struct inode *inode,
  * the hmac using the requested xattr value. Don't alloc/free memory for
  * each xattr, but attempt to re-use the previously allocated memory.
  */
-int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
-                 const char *req_xattr_value, size_t req_xattr_value_len,
-                 char *digest)
+static int evm_calc_hmac_or_hash(struct dentry *dentry,
+                               const char *req_xattr_name,
+                               const char *req_xattr_value,
+                               size_t req_xattr_value_len,
+                               char type, char *digest)
 {
        struct inode *inode = dentry->d_inode;
        struct shash_desc *desc;
@@ -122,7 +136,7 @@ int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
 
        if (!inode->i_op || !inode->i_op->getxattr)
                return -EOPNOTSUPP;
-       desc = init_desc();
+       desc = init_desc(type);
        if (IS_ERR(desc))
                return PTR_ERR(desc);
 
@@ -156,6 +170,22 @@ out:
        return error;
 }
 
+int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
+                 const char *req_xattr_value, size_t req_xattr_value_len,
+                 char *digest)
+{
+       return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
+                               req_xattr_value_len, EVM_XATTR_HMAC, digest);
+}
+
+int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
+                 const char *req_xattr_value, size_t req_xattr_value_len,
+                 char *digest)
+{
+       return evm_calc_hmac_or_hash(dentry, req_xattr_name, req_xattr_value,
+                               req_xattr_value_len, IMA_XATTR_DIGEST, digest);
+}
+
 /*
  * Calculate the hmac and update security.evm xattr
  *
@@ -186,7 +216,7 @@ int evm_init_hmac(struct inode *inode, const struct xattr *lsm_xattr,
 {
        struct shash_desc *desc;
 
-       desc = init_desc();
+       desc = init_desc(EVM_XATTR_HMAC);
        if (IS_ERR(desc)) {
                printk(KERN_INFO "init_desc failed\n");
                return PTR_ERR(desc);