blob: c048d005aae395f580dd4f60a7441b596bdf5ffc [file] [log] [blame]
Juan Castilloa57a4d52015-04-02 15:44:20 +01001/*
Roberto Vargas52f707f2018-02-12 12:36:17 +00002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Juan Castilloa57a4d52015-04-02 15:44:20 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castilloa57a4d52015-04-02 15:44:20 +01005 */
6
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +01007#include <debug.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +01008
Juan Castillobae6b2a2015-11-05 09:24:53 +00009/* mbed TLS headers */
10#include <mbedtls/memory_buffer_alloc.h>
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010011#include <mbedtls/platform.h>
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010012#include <mbedtls_config.h>
Roberto Vargas52f707f2018-02-12 12:36:17 +000013#include <mbedtls_common.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +010014
15/*
Juan Castillobae6b2a2015-11-05 09:24:53 +000016 * mbed TLS heap
Juan Castilloa57a4d52015-04-02 15:44:20 +010017 */
Qixiang Xuaa05eea2017-08-24 15:26:39 +080018#if (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA) \
19 || (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA)
Qixiang Xu1c2aef12017-08-24 15:12:20 +080020#define MBEDTLS_HEAP_SIZE (13*1024)
David Cunadoc7a1b192017-05-10 16:38:44 +010021#elif (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA)
Soby Mathewcd4a5a92017-06-05 12:18:04 +010022#define MBEDTLS_HEAP_SIZE (7*1024)
Juan Castilloa57a4d52015-04-02 15:44:20 +010023#endif
24static unsigned char heap[MBEDTLS_HEAP_SIZE];
25
26/*
Juan Castillobae6b2a2015-11-05 09:24:53 +000027 * mbed TLS initialization function
Juan Castilloa57a4d52015-04-02 15:44:20 +010028 */
29void mbedtls_init(void)
30{
31 static int ready;
Juan Castilloa57a4d52015-04-02 15:44:20 +010032
33 if (!ready) {
Juan Castillobae6b2a2015-11-05 09:24:53 +000034 /* Initialize the mbed TLS heap */
35 mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE);
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010036
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010037#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010038 /* Use reduced version of snprintf to save space. */
39 mbedtls_platform_set_snprintf(tf_snprintf);
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010040#endif
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010041
Juan Castillobae6b2a2015-11-05 09:24:53 +000042 ready = 1;
Juan Castilloa57a4d52015-04-02 15:44:20 +010043 }
44}