Skip to content

Commit 441daf7

Browse files
authored
Merge pull request #6 from Patater/factory-entropy-example
Add factory entropy injection to the example
2 parents b5a4e60 + 276fde4 commit 441daf7

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed

README.md

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

9+
## Factory injection of entropy
10+
11+
This example also contains a fake entropy injection example. Use of this
12+
function (`mbedtls_psa_inject_entropy()`) is demonstrated in this example, but
13+
it is not a function users would ever need to call as part of their
14+
applications. The function is useful for factory tool developers only.
15+
16+
In a production system, and in the absence of other sources of entropy, a
17+
factory tool can inject entropy into the device. After the factory tool
18+
completes manufacturing of a device, that device must contain enough entropy
19+
for the lifetime of the device or be able to produce it with an on-board TRNG.
20+
21+
A factory application wishing to inject entropy should configure Mbed Crypto
22+
using the Mbed TLS configuration system (for the PSA Secure Processing Element,
23+
SPE), such as in the factory application's SPE binary's `mbed_app.json` as
24+
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+
936
## Prerequisites
1037
* Install <a href='https://github.com/ARMmbed/mbed-cli#installing-mbed-cli'>Mbed CLI</a>
1138

main.cpp

Lines changed: 31 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,38 @@ static void cipher_examples(void)
331332
}
332333
}
333334

335+
static void fake_set_initial_nvseed(void)
336+
{
337+
/* This function, fake_set_initial_nvseed(), is useless on platforms that
338+
* have already been manufactured correctly. This function demonstrates
339+
* what a factory tool may do in order to manufacture a device that does
340+
* not have its own source of entropy. */
341+
342+
/* mbedtls_psa_inject_entropy() is always present, but calls to it will
343+
* always fail unless the PSA Secure Processing Element (SPE) is configured
344+
* with both MBEDTLS_ENTROPY_NV_SEED and MBEDTLS_PSA_HAS_ITS_IO by the
345+
* SPE's Mbed TLS configuration system. */
346+
uint8_t seed[MBEDTLS_ENTROPY_MAX_SEED_SIZE];
347+
348+
/* Calculate a fake seed for injecting. A real factory application would
349+
* inject true entropy for use as the initial NV Seed. */
350+
for (size_t i = 0; i < sizeof(seed); ++i) {
351+
seed[i] = i;
352+
}
353+
354+
int status = mbedtls_psa_inject_entropy(seed, sizeof(seed));
355+
if (status) {
356+
/* The device may already have an NV Seed injected, or another error
357+
* may have happened during injection. */
358+
mbedtls_printf("warning (%d) - this attempt at entropy injection"
359+
" failed\n", status);
360+
}
361+
}
362+
334363
int main(void)
335364
{
365+
fake_set_initial_nvseed();
366+
336367
ASSERT_STATUS(psa_crypto_init(), PSA_SUCCESS);
337368
cipher_examples();
338369
exit:

mbed_app.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
22
"target_overrides": {
33
"*": {
4-
"platform.stdio-convert-newlines": true
4+
"platform.stdio-convert-newlines": true,
5+
"target.extra_labels_add": ["PSA"]
56
}
6-
}
7+
},
8+
"macros": [
9+
"MBEDTLS_USER_CONFIG_FILE=\"mbedtls_user_config.h\""
10+
]
711
}
812

mbedtls_user_config.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2006-2018, Arm Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* This file is part of Mbed TLS (https://tls.mbed.org)
18+
*/
19+
20+
/* Enable PSA APIs, which this example depends on. */
21+
#if !defined(MBEDTLS_PSA_CRYPTO_C)
22+
# define MBEDTLS_PSA_CRYPTO_C
23+
#endif
24+
25+
/* Enable the default implementation of the PSA entropy injection API if we are
26+
* building for an SPE. */
27+
#if defined(COMPONENT_PSA_SRV_IMPL) || defined(COMPONENT_PSA_SRV_EMUL)
28+
# define MBEDTLS_ENTROPY_NV_SEED
29+
# define MBEDTLS_PSA_HAS_ITS_IO
30+
# define MBEDTLS_PLATFORM_NV_SEED_READ_MACRO mbed_default_seed_read
31+
# define MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO mbed_default_seed_write
32+
#endif

0 commit comments

Comments
 (0)