Skip to content

Add factory entropy injection to the example #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ List of examples contained within this repository:
* Cipher encrypt/decrypt using an AES key in cipher block chain (CBC) mode with PKCS7 padding using multiple blocks.
* Cipher encrypt/decrypt using an AES key in counter (CTR) mode using multiple blocks.

## Factory injection of entropy

This example also contains a fake entropy injection example. Use of this
function (`mbedtls_psa_inject_entropy()`) is demonstrated in this example, but
it is not a function users would ever need to call as part of their
applications. The function is useful for factory tool developers only.

In a production system, and in the absence of other sources of entropy, a
factory tool can inject entropy into the device. After the factory tool
completes manufacturing of a device, that device must contain enough entropy
for the lifetime of the device or be able to produce it with an on-board TRNG.

A factory application wishing to inject entropy should configure Mbed Crypto
using the Mbed TLS configuration system (for the PSA Secure Processing Element,
SPE), such as in the factory application's SPE binary's `mbed_app.json` as
follows:

```javascript
{
"macros": [
"MBEDTLS_ENTROPY_NV_SEED=1",
"MBEDTLS_PLATFORM_NV_SEED_READ_MACRO=mbed_default_seed_read",
"MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO=mbed_default_seed_write"
]
}
```

## Prerequisites
* Install <a href='https://github.com/ARMmbed/mbed-cli#installing-mbed-cli'>Mbed CLI</a>

Expand Down
31 changes: 31 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#endif

#include "psa/crypto.h"
#include "entropy.h"
#include <string.h>
#include <inttypes.h>

Expand Down Expand Up @@ -331,8 +332,38 @@ static void cipher_examples(void)
}
}

static void fake_set_initial_nvseed(void)
{
/* This function, fake_set_initial_nvseed(), is useless on platforms that
* have already been manufactured correctly. This function demonstrates
* what a factory tool may do in order to manufacture a device that does
* not have its own source of entropy. */

/* mbedtls_psa_inject_entropy() is always present, but calls to it will
* always fail unless the PSA Secure Processing Element (SPE) is configured
* with both MBEDTLS_ENTROPY_NV_SEED and MBEDTLS_PSA_HAS_ITS_IO by the
* SPE's Mbed TLS configuration system. */
uint8_t seed[MBEDTLS_ENTROPY_MAX_SEED_SIZE];

/* Calculate a fake seed for injecting. A real factory application would
* inject true entropy for use as the initial NV Seed. */
for (size_t i = 0; i < sizeof(seed); ++i) {
seed[i] = i;
}

int status = mbedtls_psa_inject_entropy(seed, sizeof(seed));
if (status) {
/* The device may already have an NV Seed injected, or another error
* may have happened during injection. */
mbedtls_printf("warning (%d) - this attempt at entropy injection"
" failed\n", status);
}
}

int main(void)
{
fake_set_initial_nvseed();

ASSERT_STATUS(psa_crypto_init(), PSA_SUCCESS);
cipher_examples();
exit:
Expand Down
8 changes: 6 additions & 2 deletions mbed_app.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"target_overrides": {
"*": {
"platform.stdio-convert-newlines": true
"platform.stdio-convert-newlines": true,
"target.extra_labels_add": ["PSA"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently, in addition to PSA label, you must add the MBEDTLS_PSA_CRYPTO_C.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added via new Mbed TLS user configuration file

}
}
},
"macros": [
"MBEDTLS_USER_CONFIG_FILE=\"mbedtls_user_config.h\""
]
}

32 changes: 32 additions & 0 deletions mbedtls_user_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2006-2018, Arm Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of Mbed TLS (https://tls.mbed.org)
*/

/* Enable PSA APIs, which this example depends on. */
#if !defined(MBEDTLS_PSA_CRYPTO_C)
# define MBEDTLS_PSA_CRYPTO_C
#endif

/* Enable the default implementation of the PSA entropy injection API if we are
* building for an SPE. */
#if defined(COMPONENT_PSA_SRV_IMPL) || defined(COMPONENT_PSA_SRV_EMUL)
# define MBEDTLS_ENTROPY_NV_SEED
# define MBEDTLS_PSA_HAS_ITS_IO
# define MBEDTLS_PLATFORM_NV_SEED_READ_MACRO mbed_default_seed_read
# define MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO mbed_default_seed_write
#endif