|
|
@@ -26,34 +26,48 @@ 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 struct shash_desc *init_desc(void)
|
|
|
+static struct shash_desc *init_desc(const char type)
|
|
|
{
|
|
|
int rc;
|
|
|
+ char *algo;
|
|
|
+ struct crypto_shash **tfm;
|
|
|
struct shash_desc *desc;
|
|
|
|
|
|
- if (hmac_tfm == NULL) {
|
|
|
- hmac_tfm = crypto_alloc_shash(evm_hmac, 0, CRYPTO_ALG_ASYNC);
|
|
|
- if (IS_ERR(hmac_tfm)) {
|
|
|
+ if (type == EVM_XATTR_HMAC) {
|
|
|
+ tfm = &hmac_tfm;
|
|
|
+ algo = evm_hmac;
|
|
|
+ } else {
|
|
|
+ tfm = &hash_tfm;
|
|
|
+ algo = evm_hash;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (*tfm == NULL) {
|
|
|
+ *tfm = crypto_alloc_shash(algo, 0, CRYPTO_ALG_ASYNC);
|
|
|
+ if (IS_ERR(*tfm)) {
|
|
|
pr_err("Can not allocate %s (reason: %ld)\n",
|
|
|
- evm_hmac, PTR_ERR(hmac_tfm));
|
|
|
- rc = PTR_ERR(hmac_tfm);
|
|
|
- hmac_tfm = NULL;
|
|
|
+ algo, PTR_ERR(*tfm));
|
|
|
+ rc = PTR_ERR(*tfm);
|
|
|
+ *tfm = NULL;
|
|
|
return ERR_PTR(rc);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- 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_setkey(hmac_tfm, evmkey, evmkey_len);
|
|
|
- if (rc)
|
|
|
- goto out;
|
|
|
+ if (type == EVM_XATTR_HMAC) {
|
|
|
+ rc = crypto_shash_setkey(*tfm, evmkey, evmkey_len);
|
|
|
+ if (rc)
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
rc = crypto_shash_init(desc);
|
|
|
out:
|
|
|
if (rc) {
|
|
|
@@ -97,9 +111,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;
|
|
|
@@ -111,7 +127,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);
|
|
|
|
|
|
@@ -145,6 +161,22 @@ int evm_calc_hmac(struct dentry *dentry, const char *req_xattr_name,
|
|
|
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
|
|
|
*
|
|
|
@@ -175,7 +207,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);
|