File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,32 @@ List of examples contained within this repository:
7
7
* Cipher encrypt/decrypt using an AES key in cipher block chain (CBC) mode with PKCS7 padding using multiple blocks.
8
8
* Cipher encrypt/decrypt using an AES key in counter (CTR) mode using multiple blocks.
9
9
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
+
10
36
## Prerequisites
11
37
* Install <a href =' https://github.com/ARMmbed/mbed-cli#installing-mbed-cli ' >Mbed CLI</a >
12
38
Original file line number Diff line number Diff line change 19
19
#endif
20
20
21
21
#include " psa/crypto.h"
22
+ #include " entropy.h"
22
23
#include < string.h>
23
24
#include < inttypes.h>
24
25
@@ -331,8 +332,33 @@ static void cipher_examples(void)
331
332
}
332
333
}
333
334
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
+
334
358
int main (void )
335
359
{
360
+ fake_set_initial_nvseed ();
361
+
336
362
ASSERT_STATUS (psa_crypto_init (), PSA_SUCCESS);
337
363
cipher_examples ();
338
364
exit:
You can’t perform that action at this time.
0 commit comments