hc
2024-05-10 cde9070d9970eef1f7ec2360586c802a16230ad8
kernel/security/keys/encrypted-keys/encrypted.c
....@@ -1,15 +1,12 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2010 IBM Corporation
34 * Copyright (C) 2010 Politecnico di Torino, Italy
4
- * TORSEC group -- http://security.polito.it
5
+ * TORSEC group -- https://security.polito.it
56 *
67 * Authors:
78 * Mimi Zohar <zohar@us.ibm.com>
89 * Roberto Sassu <roberto.sassu@polito.it>
9
- *
10
- * This program is free software; you can redistribute it and/or modify
11
- * it under the terms of the GNU General Public License as published by
12
- * the Free Software Foundation, version 2 of the License.
1310 *
1411 * See Documentation/security/keys/trusted-encrypted.rst
1512 */
....@@ -45,6 +42,7 @@
4542 static const char blkcipher_alg[] = "cbc(aes)";
4643 static const char key_format_default[] = "default";
4744 static const char key_format_ecryptfs[] = "ecryptfs";
45
+static const char key_format_enc32[] = "enc32";
4846 static unsigned int ivsize;
4947 static int blksize;
5048
....@@ -54,20 +52,22 @@
5452 #define HASH_SIZE SHA256_DIGEST_SIZE
5553 #define MAX_DATA_SIZE 4096
5654 #define MIN_DATA_SIZE 20
55
+#define KEY_ENC32_PAYLOAD_LEN 32
5756
5857 static struct crypto_shash *hash_tfm;
5958
6059 enum {
61
- Opt_err = -1, Opt_new, Opt_load, Opt_update
60
+ Opt_new, Opt_load, Opt_update, Opt_err
6261 };
6362
6463 enum {
65
- Opt_error = -1, Opt_default, Opt_ecryptfs
64
+ Opt_default, Opt_ecryptfs, Opt_enc32, Opt_error
6665 };
6766
6867 static const match_table_t key_format_tokens = {
6968 {Opt_default, "default"},
7069 {Opt_ecryptfs, "ecryptfs"},
70
+ {Opt_enc32, "enc32"},
7171 {Opt_error, NULL}
7272 };
7373
....@@ -195,6 +195,7 @@
195195 key_format = match_token(p, key_format_tokens, args);
196196 switch (key_format) {
197197 case Opt_ecryptfs:
198
+ case Opt_enc32:
198199 case Opt_default:
199200 *format = p;
200201 *master_desc = strsep(&datablob, " \t");
....@@ -322,27 +323,13 @@
322323 return ukey;
323324 }
324325
325
-static int calc_hash(struct crypto_shash *tfm, u8 *digest,
326
- const u8 *buf, unsigned int buflen)
327
-{
328
- SHASH_DESC_ON_STACK(desc, tfm);
329
- int err;
330
-
331
- desc->tfm = tfm;
332
- desc->flags = 0;
333
-
334
- err = crypto_shash_digest(desc, buf, buflen, digest);
335
- shash_desc_zero(desc);
336
- return err;
337
-}
338
-
339326 static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
340327 const u8 *buf, unsigned int buflen)
341328 {
342329 struct crypto_shash *tfm;
343330 int err;
344331
345
- tfm = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
332
+ tfm = crypto_alloc_shash(hmac_alg, 0, 0);
346333 if (IS_ERR(tfm)) {
347334 pr_err("encrypted_key: can't alloc %s transform: %ld\n",
348335 hmac_alg, PTR_ERR(tfm));
....@@ -351,7 +338,7 @@
351338
352339 err = crypto_shash_setkey(tfm, key, keylen);
353340 if (!err)
354
- err = calc_hash(tfm, digest, buf, buflen);
341
+ err = crypto_shash_tfm_digest(tfm, buf, buflen, digest);
355342 crypto_free_shash(tfm);
356343 return err;
357344 }
....@@ -381,8 +368,9 @@
381368
382369 memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
383370 master_keylen);
384
- ret = calc_hash(hash_tfm, derived_key, derived_buf, derived_buf_len);
385
- kzfree(derived_buf);
371
+ ret = crypto_shash_tfm_digest(hash_tfm, derived_buf, derived_buf_len,
372
+ derived_key);
373
+ kfree_sensitive(derived_buf);
386374 return ret;
387375 }
388376
....@@ -625,15 +613,22 @@
625613 format_len = (!format) ? strlen(key_format_default) : strlen(format);
626614 decrypted_datalen = dlen;
627615 payload_datalen = decrypted_datalen;
628
- if (format && !strcmp(format, key_format_ecryptfs)) {
629
- if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
630
- pr_err("encrypted_key: keylen for the ecryptfs format "
631
- "must be equal to %d bytes\n",
632
- ECRYPTFS_MAX_KEY_BYTES);
633
- return ERR_PTR(-EINVAL);
616
+ if (format) {
617
+ if (!strcmp(format, key_format_ecryptfs)) {
618
+ if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
619
+ pr_err("encrypted_key: keylen for the ecryptfs format must be equal to %d bytes\n",
620
+ ECRYPTFS_MAX_KEY_BYTES);
621
+ return ERR_PTR(-EINVAL);
622
+ }
623
+ decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
624
+ payload_datalen = sizeof(struct ecryptfs_auth_tok);
625
+ } else if (!strcmp(format, key_format_enc32)) {
626
+ if (decrypted_datalen != KEY_ENC32_PAYLOAD_LEN) {
627
+ pr_err("encrypted_key: enc32 key payload incorrect length: %d\n",
628
+ decrypted_datalen);
629
+ return ERR_PTR(-EINVAL);
630
+ }
634631 }
635
- decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
636
- payload_datalen = sizeof(struct ecryptfs_auth_tok);
637632 }
638633
639634 encrypted_datalen = roundup(decrypted_datalen, blksize);
....@@ -817,13 +812,13 @@
817812 ret = encrypted_init(epayload, key->description, format, master_desc,
818813 decrypted_datalen, hex_encoded_iv);
819814 if (ret < 0) {
820
- kzfree(epayload);
815
+ kfree_sensitive(epayload);
821816 goto out;
822817 }
823818
824819 rcu_assign_keypointer(key, epayload);
825820 out:
826
- kzfree(datablob);
821
+ kfree_sensitive(datablob);
827822 return ret;
828823 }
829824
....@@ -832,7 +827,7 @@
832827 struct encrypted_key_payload *epayload;
833828
834829 epayload = container_of(rcu, struct encrypted_key_payload, rcu);
835
- kzfree(epayload);
830
+ kfree_sensitive(epayload);
836831 }
837832
838833 /*
....@@ -890,7 +885,7 @@
890885 rcu_assign_keypointer(key, new_epayload);
891886 call_rcu(&epayload->rcu, encrypted_rcu_free);
892887 out:
893
- kzfree(buf);
888
+ kfree_sensitive(buf);
894889 return ret;
895890 }
896891
....@@ -951,7 +946,7 @@
951946 memzero_explicit(derived_key, sizeof(derived_key));
952947
953948 memcpy(buffer, ascii_buf, asciiblob_len);
954
- kzfree(ascii_buf);
949
+ kfree_sensitive(ascii_buf);
955950
956951 return asciiblob_len;
957952 out:
....@@ -966,7 +961,7 @@
966961 */
967962 static void encrypted_destroy(struct key *key)
968963 {
969
- kzfree(key->payload.data[0]);
964
+ kfree_sensitive(key->payload.data[0]);
970965 }
971966
972967 struct key_type key_type_encrypted = {
....@@ -983,7 +978,7 @@
983978 {
984979 int ret;
985980
986
- hash_tfm = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
981
+ hash_tfm = crypto_alloc_shash(hash_alg, 0, 0);
987982 if (IS_ERR(hash_tfm)) {
988983 pr_err("encrypted_key: can't allocate %s transform: %ld\n",
989984 hash_alg, PTR_ERR(hash_tfm));