blob: e9aa409b5f81c13878f93dded92697f877870e33 [file] [log] [blame]
Juan Castillo7d37aa12015-04-02 15:44:20 +01001/*
dp-arm66b4c162017-03-07 10:08:42 +00002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Juan Castillo7d37aa12015-04-02 15:44:20 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo7d37aa12015-04-02 15:44:20 +01005 */
6
Antonio Nino Diazab1794f2017-05-19 11:37:22 +01007#include <debug.h>
Juan Castillo7d37aa12015-04-02 15:44:20 +01008
Juan Castillo649dbf62015-11-05 09:24:53 +00009/* mbed TLS headers */
10#include <mbedtls/memory_buffer_alloc.h>
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010011#include <mbedtls/platform.h>
Antonio Nino Diazc46c18c2017-06-06 10:54:39 +010012#include <mbedtls_config.h>
Juan Castillo7d37aa12015-04-02 15:44:20 +010013
14/*
Juan Castillo649dbf62015-11-05 09:24:53 +000015 * mbed TLS heap
Juan Castillo7d37aa12015-04-02 15:44:20 +010016 */
David Cunadob1883512017-05-10 16:38:44 +010017#if (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA)
Juan Castillo7d37aa12015-04-02 15:44:20 +010018#define MBEDTLS_HEAP_SIZE (14*1024)
David Cunadob1883512017-05-10 16:38:44 +010019#elif (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA)
Soby Mathew38aacad2017-06-05 12:18:04 +010020#define MBEDTLS_HEAP_SIZE (7*1024)
Juan Castillo7d37aa12015-04-02 15:44:20 +010021#endif
22static unsigned char heap[MBEDTLS_HEAP_SIZE];
23
24/*
Juan Castillo649dbf62015-11-05 09:24:53 +000025 * mbed TLS initialization function
Juan Castillo7d37aa12015-04-02 15:44:20 +010026 */
27void mbedtls_init(void)
28{
29 static int ready;
Juan Castillo7d37aa12015-04-02 15:44:20 +010030
31 if (!ready) {
Juan Castillo649dbf62015-11-05 09:24:53 +000032 /* Initialize the mbed TLS heap */
33 mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE);
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010034
Antonio Nino Diazc46c18c2017-06-06 10:54:39 +010035#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010036 /* Use reduced version of snprintf to save space. */
37 mbedtls_platform_set_snprintf(tf_snprintf);
Antonio Nino Diazc46c18c2017-06-06 10:54:39 +010038#endif
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010039
Juan Castillo649dbf62015-11-05 09:24:53 +000040 ready = 1;
Juan Castillo7d37aa12015-04-02 15:44:20 +010041 }
42}