blob: 7095fde33f191feb871cdc195f2b61a37bd96e02 [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>
Antonio Nino Diaz00086e32018-08-16 16:46:06 +01009#include <stdio.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +010010
Juan Castillobae6b2a2015-11-05 09:24:53 +000011/* mbed TLS headers */
12#include <mbedtls/memory_buffer_alloc.h>
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010013#include <mbedtls/platform.h>
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010014#include <mbedtls_config.h>
Roberto Vargas52f707f2018-02-12 12:36:17 +000015#include <mbedtls_common.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +010016
17/*
Juan Castillobae6b2a2015-11-05 09:24:53 +000018 * mbed TLS heap
Juan Castilloa57a4d52015-04-02 15:44:20 +010019 */
Qixiang Xuaa05eea2017-08-24 15:26:39 +080020#if (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA) \
21 || (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA)
Qixiang Xu1c2aef12017-08-24 15:12:20 +080022#define MBEDTLS_HEAP_SIZE (13*1024)
David Cunadoc7a1b192017-05-10 16:38:44 +010023#elif (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA)
Soby Mathewcd4a5a92017-06-05 12:18:04 +010024#define MBEDTLS_HEAP_SIZE (7*1024)
Juan Castilloa57a4d52015-04-02 15:44:20 +010025#endif
26static unsigned char heap[MBEDTLS_HEAP_SIZE];
27
Roberto Vargas64d4de02018-05-24 13:34:53 +010028static void cleanup(void)
29{
30 ERROR("EXIT from BL2\n");
31 panic();
32}
33
Juan Castilloa57a4d52015-04-02 15:44:20 +010034/*
Juan Castillobae6b2a2015-11-05 09:24:53 +000035 * mbed TLS initialization function
Juan Castilloa57a4d52015-04-02 15:44:20 +010036 */
37void mbedtls_init(void)
38{
39 static int ready;
Juan Castilloa57a4d52015-04-02 15:44:20 +010040
41 if (!ready) {
Roberto Vargas64d4de02018-05-24 13:34:53 +010042 if (atexit(cleanup))
43 panic();
44
Juan Castillobae6b2a2015-11-05 09:24:53 +000045 /* Initialize the mbed TLS heap */
46 mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE);
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010047
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010048#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010049 mbedtls_platform_set_snprintf(snprintf);
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010050#endif
Juan Castillobae6b2a2015-11-05 09:24:53 +000051 ready = 1;
Juan Castilloa57a4d52015-04-02 15:44:20 +010052 }
53}