blob: 4db66d7d5feb909cc0af46abdf5215094f6289a5 [file] [log] [blame]
Antonio de Angelis7557e682022-11-30 15:37:51 +00001/*
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 Angelisedbafb62022-12-01 13:52:15 +000012#include "tfm_sp_log.h"
Antonio de Angelis7557e682022-11-30 15:37:51 +000013
Antonio de Angelisedbafb62022-12-01 13:52:15 +000014#include "config_crypto.h"
15#include "psa/crypto.h"
Antonio de Angelis7557e682022-11-30 15:37:51 +000016#include "crypto_library.h"
17
Antonio de Angelisedbafb62022-12-01 13:52:15 +000018/*
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 Angelis7557e682022-11-30 15:37:51 +000033#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 Angelisedbafb62022-12-01 13:52:15 +000039/**
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 Angelis7557e682022-11-30 15:37:51 +000043static char mbedtls_version_full[18];
44
Antonio de Angelisedbafb62022-12-01 13:52:15 +000045/**
46 * \brief Static buffer to be used by Mbed Crypto for memory allocations
47 *
48 */
49static uint8_t mbedtls_mem_buf[CRYPTO_ENGINE_BUF_SIZE] = {0};
50
Antonio de Angelis7557e682022-11-30 15:37:51 +000051/*!
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/*!@{*/
58tfm_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
63char *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 Angelisedbafb62022-12-01 13:52:15 +000068
69psa_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 Angelis7557e682022-11-30 15:37:51 +000084/*!@}*/