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