17d37aa17SJuan Castillo /* 23b94189aSRoberto Vargas * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. 37d37aa17SJuan Castillo * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 57d37aa17SJuan Castillo */ 67d37aa17SJuan Castillo 7ab1794f5SAntonio Nino Diaz #include <debug.h> 86c373345SRoberto Vargas #include <stdlib.h> 9*39b6cc66SAntonio Nino Diaz #include <stdio.h> 107d37aa17SJuan Castillo 117d37aa17SJuan Castillo /* mbed TLS headers */ 12649dbf6fSJuan Castillo #include <mbedtls/memory_buffer_alloc.h> 13ab1794f5SAntonio Nino Diaz #include <mbedtls/platform.h> 14c46c18c5SAntonio Nino Diaz #include <mbedtls_config.h> 153b94189aSRoberto Vargas #include <mbedtls_common.h> 167d37aa17SJuan Castillo 177d37aa17SJuan Castillo /* 187d37aa17SJuan Castillo * mbed TLS heap 197d37aa17SJuan Castillo */ 20dcbf3932SQixiang Xu #if (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA) \ 21dcbf3932SQixiang Xu || (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA) 229db9c65aSQixiang Xu #define MBEDTLS_HEAP_SIZE (13*1024) 23b1883510SDavid Cunado #elif (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA) 2438aacad3SSoby Mathew #define MBEDTLS_HEAP_SIZE (7*1024) 257d37aa17SJuan Castillo #endif 267d37aa17SJuan Castillo static unsigned char heap[MBEDTLS_HEAP_SIZE]; 277d37aa17SJuan Castillo 286c373345SRoberto Vargas static void cleanup(void) 296c373345SRoberto Vargas { 306c373345SRoberto Vargas ERROR("EXIT from BL2\n"); 316c373345SRoberto Vargas panic(); 326c373345SRoberto Vargas } 336c373345SRoberto Vargas 347d37aa17SJuan Castillo /* 357d37aa17SJuan Castillo * mbed TLS initialization function 367d37aa17SJuan Castillo */ 377d37aa17SJuan Castillo void mbedtls_init(void) 387d37aa17SJuan Castillo { 397d37aa17SJuan Castillo static int ready; 407d37aa17SJuan Castillo 417d37aa17SJuan Castillo if (!ready) { 426c373345SRoberto Vargas if (atexit(cleanup)) 436c373345SRoberto Vargas panic(); 446c373345SRoberto Vargas 457d37aa17SJuan Castillo /* Initialize the mbed TLS heap */ 46649dbf6fSJuan Castillo mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE); 47ab1794f5SAntonio Nino Diaz 48c46c18c5SAntonio Nino Diaz #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT 49*39b6cc66SAntonio Nino Diaz mbedtls_platform_set_snprintf(snprintf); 50c46c18c5SAntonio Nino Diaz #endif 517d37aa17SJuan Castillo ready = 1; 527d37aa17SJuan Castillo } 537d37aa17SJuan Castillo } 54