Skip to content
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

Do Not Review, Do Not Merge: Do Not Review, Do Not Merge: Prototype for single file PSA/Legacy Mbed TLS configs #116

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
ef19839
[nrf noup] add missing items in TF-M interface headers
frkv Apr 26, 2021
5903966
[nrf noup] crypto: Have CRYPTO_HW_ACCELERATOR support nrf_security
SebastianBoe Jan 20, 2022
8f000f6
[nrf toup] secure_fw: Add option to log output on a shared UART insta…
joerchan Oct 6, 2022
d2a1b89
[nrf toup] platform: nordic_nrf: Add support shared UART and using UA…
joerchan Oct 6, 2022
1b67f48
[nrf noup] platform: crypto: Check if for config instead of setting it
joerchan Feb 1, 2023
009179c
[nrf fromtree] platform: Add platform default UART control definition
joerchan Apr 3, 2023
8737c6f
[nrf fromtree] platform: nordic_nrf: Add hardware flow control support
joerchan Apr 3, 2023
ac52dba
[nrf noup] crypto: replace usage of mbedtls_hkdf
vili-nordic Jun 2, 2023
20df030
[nrf noup] Include PSA config file in TF-M PSA interface header
joerchan Jul 3, 2023
bbad10a
[nrf fromtree] crypto: Fix PSA max hash size without configuration he…
joerchan Jun 30, 2023
26ab2aa
[nrf noup] platform: nordic_nrf: Unify code between startup files
SebastianBoe Jul 5, 2023
80d2fb0
[nrf noup] platform: nordic_nrf: Use aliased default IRQ handlers
SebastianBoe Jul 5, 2023
a34be11
[nrf noup] platform: nordic_nrf: Don't hang in IRQs by default
SebastianBoe Jul 6, 2023
837c522
[nrf noup] platform: nordic_nrf: Use the correct SPU_IRQHandler
SebastianBoe Jul 6, 2023
b9ca43b
[nrf noup] platform: nordic_nrf: ifdef out weak fault handlers
SebastianBoe Jul 12, 2023
82287a5
[nrf noup] platform: nordic_nrf: Bugfix in startup file
SebastianBoe Jul 17, 2023
7b6cc5f
[nrf fromtree] Platform: nordic: unify target configuration
SebastianBoe Jul 24, 2023
063f830
[nrf fromtree] platform: nordic_nrf: rewrite SPU functions
SebastianBoe Jul 24, 2023
4791102
[nrf noup] platform: nordic_nrf: Move startup CMake code into common
SebastianBoe Jul 17, 2023
a489e9f
[nrf noup] platform: nrf_nordic: Add temp fix for for secure FPU on 9161
mswarowsky Aug 11, 2023
45db59c
[nrf fromtree] ITS: Add support for encrypted ITS
mswarowsky Mar 16, 2023
2f39a22
[nrf fromtree] platform: nordic_nrf: Move ITS defines to common CMake
SebastianBoe Jul 17, 2023
263f942
[nrf fromtree] nordic_nrf: Set FLIH IRQ tests as default
mswarowsky Sep 5, 2023
e99176e
[nrf fromtree] platform: nordic_nrf: Move STORAGE define to common CMake
SebastianBoe Jul 17, 2023
ea9bc57
[nrf fromtree] platform: nordic_nrf: Move define to common CMake
SebastianBoe Jul 17, 2023
504e700
[nrf fromtree] platform: nordic_nrf: Fix ITS encryption config
mswarowsky Sep 7, 2023
68ce232
[nrf fromtree] Docs: Update ITS encryption graphic
mswarowsky Sep 11, 2023
afd54b6
[nrf fromlist] platform: nordic_nrf: Fix assert in initialization
Vge0rge Sep 21, 2023
6854dd5
[nrf fromtree] platform: nordic_nrf: add builtin atomic macros to glue
nika-nordic Oct 6, 2023
0f553bc
[nrf fromtree] platform: nordic_nrf: Set UART pins using pinctrl method
joerchan Sep 27, 2023
a05533d
[nrf fromtree] platform: nordic_nrf: Remove unused nrf_board.h header
joerchan Oct 4, 2023
a454c04
[nrf fromtree] platform: nordic_nrf: Add missing DEFAULT_UART_CONTROL…
joerchan Oct 4, 2023
8f6d965
[nrf fromtree] platform: lairdconnectivity: Fix build of bl5340 DVK
joerchan Oct 5, 2023
f2a4620
[nrf fromtree] platform: nordic_nrf: Fix nrf9160 non-secure periphera…
joerchan Nov 6, 2023
b61b38d
[nrf fromlist] config: Disable cipher crypto module in small and medi…
joerchan Nov 7, 2023
13330e2
[nrf fromlist] partitions: crypto: Add missing PSA defined algorithms…
joerchan Nov 6, 2023
e80c858
[nrf fromlist] platform: exception_info: Add getter for exception inf…
chrisc11 Oct 8, 2023
dc26a72
[nrf fromlist] platform: Change exception handler to use system regis…
joerchan Nov 13, 2023
985a959
[nrf fromlist] platform: Store callee saved register in exception inf…
joerchan Nov 14, 2023
9739501
[nrf fromlist] platform: nordic_nrf: Store SPU fault information
joerchan Nov 14, 2023
9988cc0
[nrf fromtree] Build: Fixed CRYPTO_TFM_BUILTIN_KEYS_DRIVER option
brianq-ti Aug 22, 2023
29f5e2b
[nrf fromlist] platform: nordic_nrf: unify target configuration header
joerchan Nov 17, 2023
c549292
[nrf fromlist] platform: nordic_nrf: Refactor peripheral SPU configur…
joerchan Nov 17, 2023
4c908f4
[nrf noup] platform: nordic_nrf: Fix NS build with floating point ena…
joerchan Nov 21, 2023
dc589e3
Don't set max ECP curve size
frkv Sep 7, 2023
80c9a70
WIP: removing unused user-config
frkv Nov 2, 2023
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
2 changes: 2 additions & 0 deletions config/check_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ tfm_invalid_config(TEST_PSA_API STREQUAL "PROTECTED_STORAGE" AND NOT TFM_PARTITI
tfm_invalid_config(TEST_PSA_API STREQUAL "STORAGE" AND NOT TFM_PARTITION_INTERNAL_TRUSTED_STORAGE)
tfm_invalid_config(TEST_PSA_API STREQUAL "STORAGE" AND NOT TFM_PARTITION_PROTECTED_STORAGE)

tfm_invalid_config(CONFIG_TFM_LOG_SHARE_UART AND NOT SECURE_UART1)

########################## BL1 #################################################

tfm_invalid_config((BL1 AND PLATFORM_DEFAULT_BL1 AND CONFIG_TFM_BOOT_STORE_MEASUREMENTS) AND NOT TFM_PARTITION_MEASURED_BOOT)
Expand Down
4 changes: 4 additions & 0 deletions config/config_base.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ set(CONFIG_TFM_HALT_ON_CORE_PANIC OFF CACHE BOOL "On fatal e

set(CONFIG_TFM_STACK_WATERMARKS OFF CACHE BOOL "Whether to pre-fill partition stacks with a set value to help determine stack usage")

set(PROJECT_CONFIG_HEADER_FILE "${CMAKE_SOURCE_DIR}/config/config_base.h" CACHE FILEPATH "User defined header file for TF-M config")

set(CONFIG_TFM_LOG_SHARE_UART OFF CACHE BOOL "Allow TF-M and the non-secure application to share the UART instance. TF-M will use it while it is booting, after which the non-secure application will use it until an eventual fatal error is handled and logged by TF-M. Logging from TF-M will therefore otherwise be suppressed")
############################ Platform ##########################################

set(NUM_MAILBOX_QUEUE_SLOT 1 CACHE BOOL "Number of mailbox queue slots")
Expand Down Expand Up @@ -126,6 +129,7 @@ set(PS_ENCRYPTION ON CACHE BOOL "Enable encr
set(PS_CRYPTO_AEAD_ALG PSA_ALG_GCM CACHE STRING "The AEAD algorithm to use for authenticated encryption in Protected Storage")

set(TFM_PARTITION_INTERNAL_TRUSTED_STORAGE OFF CACHE BOOL "Enable Internal Trusted Storage partition")
set(ITS_ENCRYPTION OFF CACHE BOOL "Enable authenticated encryption of ITS files using platform specific APIs")

set(TFM_PARTITION_CRYPTO OFF CACHE BOOL "Enable Crypto partition")
set(CRYPTO_TFM_BUILTIN_KEYS_DRIVER ON CACHE BOOL "Whether to allow crypto service to store builtin keys. Without this, ALL builtin keys must be stored in a platform-specific location")
Expand Down
10 changes: 10 additions & 0 deletions config/config_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@
#define ITS_STACK_SIZE 0x720
#endif

/* The size of the authentication tag used when authentication/encryption of ITS files is enabled */
#ifndef TFM_ITS_AUTH_TAG_LENGTH
#define TFM_ITS_AUTH_TAG_LENGTH 16
#endif

/* The size of the nonce used when ITS file encryption is enabled */
#ifndef TFM_ITS_ENC_NONCE_LENGTH
#define TFM_ITS_ENC_NONCE_LENGTH 12
#endif

/* PS Partition Configs */

/* Create flash FS if it doesn't exist for Protected Storage partition */
Expand Down
2 changes: 1 addition & 1 deletion config/profile/config_profile_medium.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#define CRYPTO_HASH_MODULE_ENABLED 1

/* Enable PSA Crypto Cipher module */
#define CRYPTO_CIPHER_MODULE_ENABLED 1
#define CRYPTO_CIPHER_MODULE_ENABLED 0

/* Enable PSA Crypto asymmetric key signature module */
#define CRYPTO_ASYM_SIGN_MODULE_ENABLED 1
Expand Down
2 changes: 1 addition & 1 deletion config/profile/config_profile_medium_arotless.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#define CRYPTO_HASH_MODULE_ENABLED 1

/* Enable PSA Crypto Cipher module */
#define CRYPTO_CIPHER_MODULE_ENABLED 1
#define CRYPTO_CIPHER_MODULE_ENABLED 0

/* Enable PSA Crypto asymmetric key signature module */
#define CRYPTO_ASYM_SIGN_MODULE_ENABLED 1
Expand Down
2 changes: 1 addition & 1 deletion config/profile/config_profile_small.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#define CRYPTO_HASH_MODULE_ENABLED 1

/* Enable PSA Crypto Cipher module */
#define CRYPTO_CIPHER_MODULE_ENABLED 1
#define CRYPTO_CIPHER_MODULE_ENABLED 0

/* Enable PSA Crypto asymmetric key signature module */
#define CRYPTO_ASYM_SIGN_MODULE_ENABLED 0
Expand Down
4 changes: 4 additions & 0 deletions docs/design_docs/media/tfm_its_encryption.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions docs/design_docs/services/tfm_its_service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ manifest)
- ``tfm_internal_trusted_storage.h`` - TF-M ITS API (with client_id parameter)
- ``tfm_internal_trusted_storage.c`` - TF-M ITS implementation, using the
flash_fs as a backend
- ``its_crypto_interface.h`` - APIs for encrypting ITS assets used by ITS implementation (optional)
- ``its_crypto_interface.c`` - Implementation for ITS encryption (optional)
- ``platform/ext/target/.../tfm_hal_its_encryption.c`` - Platform implementation for ITS encryption HAL APIs (optional)
- ``flash_fs/`` - Filesystem
- ``flash/`` - Flash interface

Expand Down Expand Up @@ -280,6 +283,60 @@ implemented separately, as would some way of isolating static memory belonging
to each partition but not the code. Because of these complications, this option
has not been considered further at this time.


Encryption in ITS
=================

The ITS can optionally be configured to encrypt the internal trusted storage
data.
To support encryption in ITS the target platform must provide an
implementation of the APIs defined in ``platform/include/tfm_hal_its_encryption.h``::

enum tfm_hal_status_t tfm_hal_its_aead_generate_nonce(uint8_t *nonce,
const size_t nonce_size);

enum tfm_hal_status_t tfm_hal_its_aead_encrypt(
struct tfm_hal_its_auth_crypt_ctx *ctx,
const uint8_t *plaintext,
const size_t plaintext_size,
uint8_t *ciphertext,
const size_t ciphertext_size,
uint8_t *tag,
const size_t tag_size);

enum tfm_hal_status_t tfm_hal_its_aead_decrypt(
struct tfm_hal_its_auth_crypt_ctx *ctx,
const uint8_t *ciphertext,
const size_t ciphertext_size,
uint8_t *tag,
const size_t tag_size,
uint8_t *plaintext,
const size_t plaintext_size);


Then encryption can be enabled by setting the build option ``-DITS_ENCRYPTION=ON``.

The figure :numref:`fig-tfm_eits` describes the encryption and decryption
process happening when calling ``tfm_its_set`` and ``tfm_its_get``.

.. figure:: /design_docs/media/tfm_its_encryption.*
:align: center
:name: fig-tfm_eits
:width: 80%

En/Decryption of ITS

By using an AEAD scheme, it is possible to not only encrypt the file data but
also authenticate the file meta data, which include:

- File id
- File size
- File flags

The key used to perform the AEAD operation must be derived from a long-term
key-derivation key and the file id, which is used as a derivation label.
The long-term key-derivation key must be managed by the target platform.

--------------

*Copyright (c) 2019-2022, Arm Limited. All rights reserved.*
11 changes: 11 additions & 0 deletions interface/include/psa/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
#include "crypto_platform.h"
#endif

/* In Mbed TLS, we would query the current config through inclusion of
* of mbedtls/build_info.h, but in TF-M, we don't rely on build_info.h
* hence we just include the current configuration if it has been passed
* through command line. These config defines are required in crypto_sizes.h
* to compute macros that define sizes which depend on algorithms supported
* by the implementation
*/
#if defined(MBEDTLS_PSA_CRYPTO_CONFIG_FILE)
#include MBEDTLS_PSA_CRYPTO_CONFIG_FILE
#endif /* MBEDTLS_PSA_CRYPTO_CONFIG_FILE */

#include <stddef.h>

#ifdef __DOXYGEN_ONLY__
Expand Down
86 changes: 86 additions & 0 deletions interface/include/psa/crypto_extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,92 @@ extern "C" {
#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
PSA_ALG_IS_DSA(alg)

#if defined(MBEDTLS_ECP_C)
#include <mbedtls/ecp.h>

/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
*
* \note This function is provided solely for the convenience of
* Mbed TLS and may be removed at any time without notice.
*
* \param grpid An Mbed TLS elliptic curve identifier
* (`MBEDTLS_ECP_DP_xxx`).
* \param[out] bits On success, the bit size of the curve.
*
* \return The corresponding PSA elliptic curve identifier
* (`PSA_ECC_FAMILY_xxx`).
* \return \c 0 on failure (\p grpid is not recognized).
*/
static inline psa_ecc_family_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
size_t *bits )
{
switch( grpid )
{
case MBEDTLS_ECP_DP_SECP192R1:
*bits = 192;
return( PSA_ECC_FAMILY_SECP_R1 );
case MBEDTLS_ECP_DP_SECP224R1:
*bits = 224;
return( PSA_ECC_FAMILY_SECP_R1 );
case MBEDTLS_ECP_DP_SECP256R1:
*bits = 256;
return( PSA_ECC_FAMILY_SECP_R1 );
case MBEDTLS_ECP_DP_SECP384R1:
*bits = 384;
return( PSA_ECC_FAMILY_SECP_R1 );
case MBEDTLS_ECP_DP_SECP521R1:
*bits = 521;
return( PSA_ECC_FAMILY_SECP_R1 );
case MBEDTLS_ECP_DP_BP256R1:
*bits = 256;
return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
case MBEDTLS_ECP_DP_BP384R1:
*bits = 384;
return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
case MBEDTLS_ECP_DP_BP512R1:
*bits = 512;
return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
case MBEDTLS_ECP_DP_CURVE25519:
*bits = 255;
return( PSA_ECC_FAMILY_MONTGOMERY );
case MBEDTLS_ECP_DP_SECP192K1:
*bits = 192;
return( PSA_ECC_FAMILY_SECP_K1 );
case MBEDTLS_ECP_DP_SECP224K1:
*bits = 224;
return( PSA_ECC_FAMILY_SECP_K1 );
case MBEDTLS_ECP_DP_SECP256K1:
*bits = 256;
return( PSA_ECC_FAMILY_SECP_K1 );
case MBEDTLS_ECP_DP_CURVE448:
*bits = 448;
return( PSA_ECC_FAMILY_MONTGOMERY );
default:
*bits = 0;
return( 0 );
}
}

/** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
*
* \note This function is provided solely for the convenience of
* Mbed TLS and may be removed at any time without notice.
*
* \param curve A PSA elliptic curve identifier
* (`PSA_ECC_FAMILY_xxx`).
* \param byte_length The byte-length of a private key on \p curve.
*
* \return The corresponding Mbed TLS elliptic curve identifier
* (`MBEDTLS_ECP_DP_xxx`).
* \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
* \return #MBEDTLS_ECP_DP_NONE if \p byte_length is not
* correct for \p curve.
*/
mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
size_t byte_length );
#endif /* MBEDTLS_ECP_C */


/**@}*/

#ifdef __cplusplus
Expand Down
14 changes: 9 additions & 5 deletions interface/include/psa/crypto_sizes.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,19 @@
/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
* 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
* HMAC-SHA3-512. */
#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA_384)
#define PSA_HASH_MAX_SIZE 64
#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
#else
#define PSA_HASH_MAX_SIZE 32
#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
#endif
#else
/* Without any PSA configuration we must assume the maximum size possible. */
#define PSA_HASH_MAX_SIZE 64
#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
#endif

/** \def PSA_MAC_MAX_SIZE
*
Expand Down Expand Up @@ -179,11 +185,6 @@
/* The maximum size of an ECC key on this implementation, in bits.
* This is a vendor-specific macro. */
#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
#if defined(MBEDTLS_PSA_CRYPTO_CONFIG_FILE)
#include MBEDTLS_PSA_CRYPTO_CONFIG_FILE
#else
#include "crypto_config.h"
#endif
#if defined(PSA_WANT_ECC_SECP_R1_521)
#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
#elif defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Expand Down Expand Up @@ -214,7 +215,10 @@
#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
#endif
#else /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */
/* Without any PSA configuration we must assume the maximum size possible. */
#ifndef PSA_VENDOR_ECC_MAX_CURVE_BITS
#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
#endif
#endif /* defined(MBEDTLS_PSA_CRYPTO_CONFIG) */

/** This macro returns the maximum supported length of the PSK for the
Expand Down
17 changes: 17 additions & 0 deletions interface/include/psa/crypto_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,23 @@ typedef struct {

/**@}*/

#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
typedef psa_key_id_t mbedtls_svc_key_id_t;

#else /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */
/* Implementation-specific: The Mbed Cryptography library can be built as
* part of a multi-client service that exposes the PSA Cryptograpy API in each
* client and encodes the client identity in the key identifier argument of
* functions such as psa_open_key().
*/
typedef struct
{
psa_key_id_t key_id;
mbedtls_key_owner_id_t owner;
} mbedtls_svc_key_id_t;

#endif /* !MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */

/** \defgroup policy Key policies
* @{
*/
Expand Down
1 change: 1 addition & 0 deletions platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ target_compile_definitions(platform_region_defs
BL1_TRAILER_SIZE=${BL1_TRAILER_SIZE}
$<$<BOOL:${PLATFORM_DEFAULT_BL1}>:PLATFORM_DEFAULT_BL1>
$<$<BOOL:${SECURE_UART1}>:SECURE_UART1>
$<$<BOOL:${CONFIG_TFM_LOG_SHARE_UART}>:CONFIG_TFM_LOG_SHARE_UART>
DAUTH_${DEBUG_AUTHENTICATION}
$<$<BOOL:${MCUBOOT_IMAGE_NUMBER}>:MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}>
$<$<STREQUAL:${MCUBOOT_SIGNATURE_TYPE},RSA>:MCUBOOT_SIGN_RSA>
Expand Down
9 changes: 9 additions & 0 deletions platform/ext/accelerator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

cmake_policy(SET CMP0079 NEW)

# TODO: Verify that this works for both minimal and normal configuration
target_compile_definitions(tfm_config
INTERFACE
CRYPTO_HW_ACCELERATOR
)

# When using nrf_security we don't need these build scripts
return()

if(BL1 AND PLATFORM_DEFAULT_BL1)
add_library(bl1_crypto_hw STATIC)
endif()
Expand Down
Loading