Skip to content

Commit 9b8d24a

Browse files
Eric Biggersjarkkojs
authored andcommitted
KEYS: encrypted: Use SHA-256 library instead of crypto_shash
Instead of the "sha256" crypto_shash, just use sha256(). Similarly, instead of the "hmac(sha256)" crypto_shash, just use hmac_sha256_usingrawkey(). This is simpler and faster. Signed-off-by: Eric Biggers <ebiggers@kernel.org> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Reviewed-by: Mimi Zohar <zohar@linux.ibm.com> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
1 parent 720a485 commit 9b8d24a

2 files changed

Lines changed: 11 additions & 55 deletions

File tree

security/keys/Kconfig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ config ENCRYPTED_KEYS
8787
tristate "ENCRYPTED KEYS"
8888
depends on KEYS
8989
select CRYPTO
90-
select CRYPTO_HMAC
9190
select CRYPTO_AES
9291
select CRYPTO_CBC
93-
select CRYPTO_SHA256
92+
select CRYPTO_LIB_SHA256
9493
select CRYPTO_RNG
9594
help
9695
This option provides support for create/encrypting/decrypting keys

security/keys/encrypted-keys/encrypted.c

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <linux/scatterlist.h>
2828
#include <linux/ctype.h>
2929
#include <crypto/aes.h>
30-
#include <crypto/hash.h>
3130
#include <crypto/sha2.h>
3231
#include <crypto/skcipher.h>
3332
#include <crypto/utils.h>
@@ -37,8 +36,6 @@
3736

3837
static const char KEY_TRUSTED_PREFIX[] = "trusted:";
3938
static const char KEY_USER_PREFIX[] = "user:";
40-
static const char hash_alg[] = "sha256";
41-
static const char hmac_alg[] = "hmac(sha256)";
4239
static const char blkcipher_alg[] = "cbc(aes)";
4340
static const char key_format_default[] = "default";
4441
static const char key_format_ecryptfs[] = "ecryptfs";
@@ -54,8 +51,6 @@ static int blksize;
5451
#define MIN_DATA_SIZE 20
5552
#define KEY_ENC32_PAYLOAD_LEN 32
5653

57-
static struct crypto_shash *hash_tfm;
58-
5954
enum {
6055
Opt_new, Opt_load, Opt_update, Opt_err
6156
};
@@ -329,26 +324,6 @@ static struct key *request_user_key(const char *master_desc, const u8 **master_k
329324
return ukey;
330325
}
331326

332-
static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
333-
const u8 *buf, unsigned int buflen)
334-
{
335-
struct crypto_shash *tfm;
336-
int err;
337-
338-
tfm = crypto_alloc_shash(hmac_alg, 0, 0);
339-
if (IS_ERR(tfm)) {
340-
pr_err("encrypted_key: can't alloc %s transform: %ld\n",
341-
hmac_alg, PTR_ERR(tfm));
342-
return PTR_ERR(tfm);
343-
}
344-
345-
err = crypto_shash_setkey(tfm, key, keylen);
346-
if (!err)
347-
err = crypto_shash_tfm_digest(tfm, buf, buflen, digest);
348-
crypto_free_shash(tfm);
349-
return err;
350-
}
351-
352327
enum derived_key_type { ENC_KEY, AUTH_KEY };
353328

354329
/* Derive authentication/encryption key from trusted key */
@@ -357,7 +332,6 @@ static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
357332
{
358333
u8 *derived_buf;
359334
unsigned int derived_buf_len;
360-
int ret;
361335

362336
derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
363337
if (derived_buf_len < HASH_SIZE)
@@ -374,10 +348,9 @@ static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
374348

375349
memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
376350
master_keylen);
377-
ret = crypto_shash_tfm_digest(hash_tfm, derived_buf, derived_buf_len,
378-
derived_key);
351+
sha256(derived_buf, derived_buf_len, derived_key);
379352
kfree_sensitive(derived_buf);
380-
return ret;
353+
return 0;
381354
}
382355

383356
static struct skcipher_request *init_skcipher_req(const u8 *key,
@@ -503,10 +476,10 @@ static int datablob_hmac_append(struct encrypted_key_payload *epayload,
503476
goto out;
504477

505478
digest = epayload->format + epayload->datablob_len;
506-
ret = calc_hmac(digest, derived_key, sizeof derived_key,
507-
epayload->format, epayload->datablob_len);
508-
if (!ret)
509-
dump_hmac(NULL, digest, HASH_SIZE);
479+
hmac_sha256_usingrawkey(derived_key, sizeof(derived_key),
480+
epayload->format, epayload->datablob_len,
481+
digest);
482+
dump_hmac(NULL, digest, HASH_SIZE);
510483
out:
511484
memzero_explicit(derived_key, sizeof(derived_key));
512485
return ret;
@@ -534,9 +507,8 @@ static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
534507
} else
535508
p = epayload->format;
536509

537-
ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
538-
if (ret < 0)
539-
goto out;
510+
hmac_sha256_usingrawkey(derived_key, sizeof(derived_key), p, len,
511+
digest);
540512
ret = crypto_memneq(digest, epayload->format + epayload->datablob_len,
541513
sizeof(digest));
542514
if (ret) {
@@ -1011,29 +983,14 @@ static int __init init_encrypted(void)
1011983
{
1012984
int ret;
1013985

1014-
hash_tfm = crypto_alloc_shash(hash_alg, 0, 0);
1015-
if (IS_ERR(hash_tfm)) {
1016-
pr_err("encrypted_key: can't allocate %s transform: %ld\n",
1017-
hash_alg, PTR_ERR(hash_tfm));
1018-
return PTR_ERR(hash_tfm);
1019-
}
1020-
1021986
ret = aes_get_sizes();
1022987
if (ret < 0)
1023-
goto out;
1024-
ret = register_key_type(&key_type_encrypted);
1025-
if (ret < 0)
1026-
goto out;
1027-
return 0;
1028-
out:
1029-
crypto_free_shash(hash_tfm);
1030-
return ret;
1031-
988+
return ret;
989+
return register_key_type(&key_type_encrypted);
1032990
}
1033991

1034992
static void __exit cleanup_encrypted(void)
1035993
{
1036-
crypto_free_shash(hash_tfm);
1037994
unregister_key_type(&key_type_encrypted);
1038995
}
1039996

0 commit comments

Comments
 (0)