blob: 205c2432afd404cf55e2a5de0a68371f00864192 [file] [log] [blame]
Juan Castilloa57a4d52015-04-02 15:44:20 +01001/*
dp-arm52b1fe52017-03-07 10:08:42 +00002 * Copyright (c) 2015-2017, 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>
Juan Castilloa57a4d52015-04-02 15:44:20 +010013
14/*
Juan Castillobae6b2a2015-11-05 09:24:53 +000015 * mbed TLS heap
Juan Castilloa57a4d52015-04-02 15:44:20 +010016 */
Qixiang Xuaa05eea2017-08-24 15:26:39 +080017#if (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA) \
18 || (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA)
Qixiang Xu1c2aef12017-08-24 15:12:20 +080019#define MBEDTLS_HEAP_SIZE (13*1024)
David Cunadoc7a1b192017-05-10 16:38:44 +010020#elif (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA)
Soby Mathewcd4a5a92017-06-05 12:18:04 +010021#define MBEDTLS_HEAP_SIZE (7*1024)
Juan Castilloa57a4d52015-04-02 15:44:20 +010022#endif
23static unsigned char heap[MBEDTLS_HEAP_SIZE];
24
25/*
Juan Castillobae6b2a2015-11-05 09:24:53 +000026 * mbed TLS initialization function
Juan Castilloa57a4d52015-04-02 15:44:20 +010027 */
28void mbedtls_init(void)
29{
30 static int ready;
Juan Castilloa57a4d52015-04-02 15:44:20 +010031
32 if (!ready) {
Juan Castillobae6b2a2015-11-05 09:24:53 +000033 /* Initialize the mbed TLS heap */
34 mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE);
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010035
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010036#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010037 /* Use reduced version of snprintf to save space. */
38 mbedtls_platform_set_snprintf(tf_snprintf);
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010039#endif
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010040
Juan Castillobae6b2a2015-11-05 09:24:53 +000041 ready = 1;
Juan Castilloa57a4d52015-04-02 15:44:20 +010042 }
43}