1818#include <linux/module.h>
1919#include <linux/crypto.h>
2020#include <linux/xattr.h>
21+ #include <linux/evm.h>
2122#include <keys/encrypted-type.h>
2223#include <crypto/hash.h>
2324#include "evm.h"
@@ -32,6 +33,44 @@ struct crypto_shash *hash_tfm;
3233
3334static DEFINE_MUTEX (mutex );
3435
36+ #define EVM_SET_KEY_BUSY 0
37+
38+ static unsigned long evm_set_key_flags ;
39+
40+ /**
41+ * evm_set_key() - set EVM HMAC key from the kernel
42+ * @key: pointer to a buffer with the key data
43+ * @size: length of the key data
44+ *
45+ * This function allows setting the EVM HMAC key from the kernel
46+ * without using the "encrypted" key subsystem keys. It can be used
47+ * by the crypto HW kernel module which has its own way of managing
48+ * keys.
49+ *
50+ * key length should be between 32 and 128 bytes long
51+ */
52+ int evm_set_key (void * key , size_t keylen )
53+ {
54+ int rc ;
55+
56+ rc = - EBUSY ;
57+ if (test_and_set_bit (EVM_SET_KEY_BUSY , & evm_set_key_flags ))
58+ goto busy ;
59+ rc = - EINVAL ;
60+ if (keylen > MAX_KEY_SIZE )
61+ goto inval ;
62+ memcpy (evmkey , key , keylen );
63+ evm_initialized |= EVM_INIT_HMAC ;
64+ pr_info ("key initialized\n" );
65+ return 0 ;
66+ inval :
67+ clear_bit (EVM_SET_KEY_BUSY , & evm_set_key_flags );
68+ busy :
69+ pr_err ("key initialization failed\n" );
70+ return rc ;
71+ }
72+ EXPORT_SYMBOL_GPL (evm_set_key );
73+
3574static struct shash_desc * init_desc (char type )
3675{
3776 long rc ;
@@ -40,6 +79,10 @@ static struct shash_desc *init_desc(char type)
4079 struct shash_desc * desc ;
4180
4281 if (type == EVM_XATTR_HMAC ) {
82+ if (!(evm_initialized & EVM_INIT_HMAC )) {
83+ pr_err ("HMAC key is not set\n" );
84+ return ERR_PTR (- ENOKEY );
85+ }
4386 tfm = & hmac_tfm ;
4487 algo = evm_hmac ;
4588 } else {
@@ -240,20 +283,17 @@ int evm_init_key(void)
240283{
241284 struct key * evm_key ;
242285 struct encrypted_key_payload * ekp ;
243- int rc = 0 ;
286+ int rc ;
244287
245288 evm_key = request_key (& key_type_encrypted , EVMKEY , NULL );
246289 if (IS_ERR (evm_key ))
247290 return - ENOENT ;
248291
249292 down_read (& evm_key -> sem );
250293 ekp = evm_key -> payload .data [0 ];
251- if (ekp -> decrypted_datalen > MAX_KEY_SIZE ) {
252- rc = - EINVAL ;
253- goto out ;
254- }
255- memcpy (evmkey , ekp -> decrypted_data , ekp -> decrypted_datalen );
256- out :
294+
295+ rc = evm_set_key (ekp -> decrypted_data , ekp -> decrypted_datalen );
296+
257297 /* burn the original key contents */
258298 memset (ekp -> decrypted_data , 0 , ekp -> decrypted_datalen );
259299 up_read (& evm_key -> sem );
0 commit comments