Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | #include <string.h> |
| 11 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame^] | 12 | #include "tfm_sp_log.h" |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 13 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame^] | 14 | #include "config_crypto.h" |
| 15 | #include "psa/crypto.h" |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 16 | #include "crypto_library.h" |
| 17 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame^] | 18 | /* |
| 19 | * \brief This Mbed TLS include is needed to initialise the memory allocator |
| 20 | * of the library used for internal allocations |
| 21 | */ |
| 22 | #include "mbedtls/memory_buffer_alloc.h" |
| 23 | /* |
| 24 | * \brief This Mbed TLS include is needed to set the mbedtls_printf to the |
| 25 | * function required by the TF-M framework in order to be able to |
| 26 | * print to terminal through mbedtls_printf |
| 27 | */ |
| 28 | #include "mbedtls/platform.h" |
| 29 | /* |
| 30 | * \brief This Mbed TLS include is needed to retrieve version information for |
| 31 | * display |
| 32 | */ |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 33 | #include "mbedtls/build_info.h" |
| 34 | |
| 35 | #ifndef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER |
| 36 | #error "MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER must be selected in Mbed TLS config file" |
| 37 | #endif |
| 38 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame^] | 39 | /** |
| 40 | * \brief Static buffer containing the string describing the mbed TLS version. mbed TLS |
| 41 | * guarantees that the string will never be greater than 18 bytes |
| 42 | */ |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 43 | static char mbedtls_version_full[18]; |
| 44 | |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame^] | 45 | /** |
| 46 | * \brief Static buffer to be used by Mbed Crypto for memory allocations |
| 47 | * |
| 48 | */ |
| 49 | static uint8_t mbedtls_mem_buf[CRYPTO_ENGINE_BUF_SIZE] = {0}; |
| 50 | |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 51 | /*! |
| 52 | * \defgroup tfm_crypto_library Set of functions implementing the abstractions of the underlying cryptographic |
| 53 | * library that implements the PSA Crypto APIs to provide the PSA Crypto core |
| 54 | * functionality to the TF-M Crypto service. Currently it supports only an |
| 55 | * mbed TLS based abstraction. |
| 56 | */ |
| 57 | /*!@{*/ |
| 58 | tfm_crypto_library_key_id_t tfm_crypto_library_key_id_init(int32_t owner, psa_key_id_t key_id) |
| 59 | { |
| 60 | return mbedtls_svc_key_id_make(owner, key_id); |
| 61 | } |
| 62 | |
| 63 | char *tfm_crypto_library_get_info(void) |
| 64 | { |
| 65 | memcpy(mbedtls_version_full, MBEDTLS_VERSION_STRING_FULL, sizeof(MBEDTLS_VERSION_STRING_FULL)); |
| 66 | return mbedtls_version_full; |
| 67 | } |
Antonio de Angelis | edbafb6 | 2022-12-01 13:52:15 +0000 | [diff] [blame^] | 68 | |
| 69 | psa_status_t tfm_crypto_core_library_init(void) |
| 70 | { |
| 71 | /* Initialise the Mbed Crypto memory allocator to use static memory |
| 72 | * allocation from the provided buffer instead of using the heap |
| 73 | */ |
| 74 | mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, |
| 75 | CRYPTO_ENGINE_BUF_SIZE); |
| 76 | |
| 77 | /* mbedtls_printf is used to print messages including error information. */ |
| 78 | #if (TFM_PARTITION_LOG_LEVEL >= TFM_PARTITION_LOG_LEVEL_ERROR) |
| 79 | mbedtls_platform_set_printf(printf); |
| 80 | #endif |
| 81 | |
| 82 | return PSA_SUCCESS; |
| 83 | } |
Antonio de Angelis | 7557e68 | 2022-11-30 15:37:51 +0000 | [diff] [blame] | 84 | /*!@}*/ |