Skip to content

Commit a61de2e

Browse files
committed
Demonstrate entropy injection
Make the example demonstrate entropy injection by adding a new function, called before psa_crypto_init(), that attempts to inject some fake entropy into the system. Print a warning message if injecting entropy fails.
1 parent 2f78c5b commit a61de2e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ List of examples contained within this repository:
77
* Cipher encrypt/decrypt using an AES key in cipher block chain (CBC) mode with PKCS7 padding using multiple blocks.
88
* Cipher encrypt/decrypt using an AES key in counter (CTR) mode using multiple blocks.
99

10+
## Factory injection of entropy
11+
12+
This example also contains a fake entropy injection example. Use of this
13+
function (`mbedtls_psa_inject_entropy()`) is demonstrated in this example, but
14+
it is not a function users would ever need to call as part of their
15+
applications. The function is useful for factory tool developers only.
16+
17+
In a production system, and in the absence of other sources of entropy, a
18+
factory tool can inject entropy into the device. After the factory tool
19+
completes manufacturing of a device, that device must contain enough entropy
20+
for the lifetime of the device or be able to produce it with an on-board TRNG.
21+
22+
A factory application wishing to inject entropy should configure Mbed Crypto
23+
using the Mbed TLS configuration system, such as in the factory application's
24+
`mbed_app.json` as follows:
25+
26+
```javascript
27+
{
28+
"macros": [
29+
"MBEDTLS_ENTROPY_NV_SEED=1",
30+
"MBEDTLS_PLATFORM_NV_SEED_READ_MACRO=mbed_default_seed_read",
31+
"MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO=mbed_default_seed_write"
32+
]
33+
}
34+
```
35+
1036
## Prerequisites
1137
* Install <a href='https://github.com/ARMmbed/mbed-cli#installing-mbed-cli'>Mbed CLI</a>
1238

main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#endif
2020

2121
#include "psa/crypto.h"
22+
#include "entropy.h"
2223
#include <string.h>
2324
#include <inttypes.h>
2425

@@ -331,8 +332,33 @@ static void cipher_examples(void)
331332
}
332333
}
333334

335+
static void fake_set_initial_nvseed(void)
336+
{
337+
/* mbedtls_psa_inject_entropy() depends on both MBEDTLS_ENTROPY_NV_SEED and
338+
* MBEDTLS_PSA_HAS_ITS_IO being enabled by the Mbed TLS configuration
339+
* system. */
340+
#if defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO)
341+
uint8_t seed[MBEDTLS_ENTROPY_MAX_SEED_SIZE];
342+
343+
/* Calculate a fake seed for injecting. A real factory application would
344+
* inject true entropy for use as the initial NV Seed. */
345+
for (size_t i = 0; i < sizeof(seed); ++i) {
346+
seed[i] = i;
347+
}
348+
349+
int status = mbedtls_psa_inject_entropy(seed, sizeof(seed));
350+
if (status) {
351+
/* The device may already have an NV Seed injected, or another error
352+
* may have happened during injection. */
353+
mbedtls_printf("warning - this attempt at entropy injection failed\n");
354+
}
355+
#endif
356+
}
357+
334358
int main(void)
335359
{
360+
fake_set_initial_nvseed();
361+
336362
ASSERT_STATUS(psa_crypto_init(), PSA_SUCCESS);
337363
cipher_examples();
338364
exit:

0 commit comments

Comments
 (0)