blob: 64dc1967deb2f77a70b550f60a45b10badc11e86 [file] [log] [blame]
Juan Castillo7d37aa12015-04-02 15:44:20 +01001/*
Roberto Vargas3b941892018-02-12 12:36:17 +00002 * Copyright (c) 2015-2018, 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>
Roberto Vargas6c373342018-05-24 13:34:53 +01008#include <stdlib.h>
Juan Castillo7d37aa12015-04-02 15:44:20 +01009
Juan Castillo649dbf62015-11-05 09:24:53 +000010/* mbed TLS headers */
11#include <mbedtls/memory_buffer_alloc.h>
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010012#include <mbedtls/platform.h>
Antonio Nino Diazc46c18c2017-06-06 10:54:39 +010013#include <mbedtls_config.h>
Roberto Vargas3b941892018-02-12 12:36:17 +000014#include <mbedtls_common.h>
Juan Castillo7d37aa12015-04-02 15:44:20 +010015
16/*
Juan Castillo649dbf62015-11-05 09:24:53 +000017 * mbed TLS heap
Juan Castillo7d37aa12015-04-02 15:44:20 +010018 */
Qixiang Xudcbf3932017-08-24 15:26:39 +080019#if (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA) \
20 || (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA)
Qixiang Xu9db9c652017-08-24 15:12:20 +080021#define MBEDTLS_HEAP_SIZE (13*1024)
David Cunadob1883512017-05-10 16:38:44 +010022#elif (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA)
Soby Mathew38aacad2017-06-05 12:18:04 +010023#define MBEDTLS_HEAP_SIZE (7*1024)
Juan Castillo7d37aa12015-04-02 15:44:20 +010024#endif
25static unsigned char heap[MBEDTLS_HEAP_SIZE];
26
Roberto Vargas6c373342018-05-24 13:34:53 +010027static void cleanup(void)
28{
29 ERROR("EXIT from BL2\n");
30 panic();
31}
32
Juan Castillo7d37aa12015-04-02 15:44:20 +010033/*
Juan Castillo649dbf62015-11-05 09:24:53 +000034 * mbed TLS initialization function
Juan Castillo7d37aa12015-04-02 15:44:20 +010035 */
36void mbedtls_init(void)
37{
38 static int ready;
Juan Castillo7d37aa12015-04-02 15:44:20 +010039
40 if (!ready) {
Roberto Vargas6c373342018-05-24 13:34:53 +010041 if (atexit(cleanup))
42 panic();
43
Juan Castillo649dbf62015-11-05 09:24:53 +000044 /* Initialize the mbed TLS heap */
45 mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE);
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010046
Antonio Nino Diazc46c18c2017-06-06 10:54:39 +010047#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010048 /* Use reduced version of snprintf to save space. */
49 mbedtls_platform_set_snprintf(tf_snprintf);
Antonio Nino Diazc46c18c2017-06-06 10:54:39 +010050#endif
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010051
Juan Castillo649dbf62015-11-05 09:24:53 +000052 ready = 1;
Juan Castillo7d37aa12015-04-02 15:44:20 +010053 }
54}