Skip to content

Commit fcd7c26

Browse files
a3fjarkkojs
authored andcommitted
KEYS: trusted: allow use of kernel RNG for key material
The two existing trusted key sources don't make use of the kernel RNG, but instead let the hardware doing the sealing/unsealing also generate the random key material. However, both users and future backends may want to place less trust into the quality of the trust source's random number generator and instead reuse the kernel entropy pool, which can be seeded from multiple entropy sources. Make this possible by adding a new trusted.rng parameter, that will force use of the kernel RNG. In its absence, it's up to the trust source to decide, which random numbers to use, maintaining the existing behavior. Suggested-by: Jarkko Sakkinen <jarkko@kernel.org> Acked-by: Sumit Garg <sumit.garg@linaro.org> Acked-by: Pankaj Gupta <pankaj.gupta@nxp.com> Reviewed-by: David Gstir <david@sigma-star.at> Reviewed-by: Pankaj Gupta <pankaj.gupta@nxp.com> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Tested-by: Pankaj Gupta <pankaj.gupta@nxp.com> Tested-by: Michael Walle <michael@walle.cc> # on ls1028a (non-E and E) Tested-by: John Ernberg <john.ernberg@actia.se> # iMX8QXP Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
1 parent be07858 commit fcd7c26

4 files changed

Lines changed: 57 additions & 10 deletions

File tree

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5963,6 +5963,16 @@
59635963
first trust source as a backend which is initialized
59645964
successfully during iteration.
59655965

5966+
trusted.rng= [KEYS]
5967+
Format: <string>
5968+
The RNG used to generate key material for trusted keys.
5969+
Can be one of:
5970+
- "kernel"
5971+
- the same value as trusted.source: "tpm" or "tee"
5972+
- "default"
5973+
If not specified, "default" is used. In this case,
5974+
the RNG's choice is left to each individual trust source.
5975+
59665976
tsc= Disable clocksource stability checks for TSC.
59675977
Format: <string>
59685978
[x86] reliable: mark tsc clocksource as reliable, this

Documentation/security/keys/trusted-encrypted.rst

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,26 @@ Key Generation
8787
Trusted Keys
8888
------------
8989

90-
New keys are created from random numbers generated in the trust source. They
91-
are encrypted/decrypted using a child key in the storage key hierarchy.
92-
Encryption and decryption of the child key must be protected by a strong
93-
access control policy within the trust source.
90+
New keys are created from random numbers. They are encrypted/decrypted using
91+
a child key in the storage key hierarchy. Encryption and decryption of the
92+
child key must be protected by a strong access control policy within the
93+
trust source. The random number generator in use differs according to the
94+
selected trust source:
9495

95-
* TPM (hardware device) based RNG
96+
* TPM: hardware device based RNG
9697

97-
Strength of random numbers may vary from one device manufacturer to
98-
another.
98+
Keys are generated within the TPM. Strength of random numbers may vary
99+
from one device manufacturer to another.
99100

100-
* TEE (OP-TEE based on Arm TrustZone) based RNG
101+
* TEE: OP-TEE based on Arm TrustZone based RNG
101102

102103
RNG is customizable as per platform needs. It can either be direct output
103104
from platform specific hardware RNG or a software based Fortuna CSPRNG
104105
which can be seeded via multiple entropy sources.
105106

107+
Users may override this by specifying ``trusted.rng=kernel`` on the kernel
108+
command-line to override the used RNG with the kernel's random number pool.
109+
106110
Encrypted Keys
107111
--------------
108112

include/keys/trusted-type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct trusted_key_ops {
6464
/* Unseal a key. */
6565
int (*unseal)(struct trusted_key_payload *p, char *datablob);
6666

67-
/* Get a randomized key. */
67+
/* Optional: Get a randomized key. */
6868
int (*get_random)(unsigned char *key, size_t key_len);
6969

7070
/* Exit key interface. */

security/keys/trusted-keys/trusted_core.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616
#include <linux/key-type.h>
1717
#include <linux/module.h>
1818
#include <linux/parser.h>
19+
#include <linux/random.h>
1920
#include <linux/rcupdate.h>
2021
#include <linux/slab.h>
2122
#include <linux/static_call.h>
2223
#include <linux/string.h>
2324
#include <linux/uaccess.h>
2425

26+
static char *trusted_rng = "default";
27+
module_param_named(rng, trusted_rng, charp, 0);
28+
MODULE_PARM_DESC(rng, "Select trusted key RNG");
29+
2530
static char *trusted_key_source;
2631
module_param_named(source, trusted_key_source, charp, 0);
2732
MODULE_PARM_DESC(source, "Select trusted keys source (tpm or tee)");
@@ -312,8 +317,14 @@ struct key_type key_type_trusted = {
312317
};
313318
EXPORT_SYMBOL_GPL(key_type_trusted);
314319

320+
static int kernel_get_random(unsigned char *key, size_t key_len)
321+
{
322+
return get_random_bytes_wait(key, key_len) ?: key_len;
323+
}
324+
315325
static int __init init_trusted(void)
316326
{
327+
int (*get_random)(unsigned char *key, size_t key_len);
317328
int i, ret = 0;
318329

319330
for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) {
@@ -322,14 +333,36 @@ static int __init init_trusted(void)
322333
strlen(trusted_key_sources[i].name)))
323334
continue;
324335

336+
/*
337+
* We always support trusted.rng="kernel" and "default" as
338+
* well as trusted.rng=$trusted.source if the trust source
339+
* defines its own get_random callback.
340+
*/
341+
get_random = trusted_key_sources[i].ops->get_random;
342+
if (trusted_rng && strcmp(trusted_rng, "default")) {
343+
if (!strcmp(trusted_rng, "kernel")) {
344+
get_random = kernel_get_random;
345+
} else if (strcmp(trusted_rng, trusted_key_sources[i].name) ||
346+
!get_random) {
347+
pr_warn("Unsupported RNG. Supported: kernel");
348+
if (get_random)
349+
pr_cont(", %s", trusted_key_sources[i].name);
350+
pr_cont(", default\n");
351+
return -EINVAL;
352+
}
353+
}
354+
355+
if (!get_random)
356+
get_random = kernel_get_random;
357+
325358
static_call_update(trusted_key_init,
326359
trusted_key_sources[i].ops->init);
327360
static_call_update(trusted_key_seal,
328361
trusted_key_sources[i].ops->seal);
329362
static_call_update(trusted_key_unseal,
330363
trusted_key_sources[i].ops->unseal);
331364
static_call_update(trusted_key_get_random,
332-
trusted_key_sources[i].ops->get_random);
365+
get_random);
333366
static_call_update(trusted_key_exit,
334367
trusted_key_sources[i].ops->exit);
335368
migratable = trusted_key_sources[i].ops->migratable;

0 commit comments

Comments
 (0)