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