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> 8*6c373345SRoberto Vargas #include <stdlib.h> 97d37aa17SJuan Castillo 107d37aa17SJuan Castillo /* mbed TLS headers */ 11649dbf6fSJuan Castillo #include <mbedtls/memory_buffer_alloc.h> 12ab1794f5SAntonio Nino Diaz #include <mbedtls/platform.h> 13c46c18c5SAntonio Nino Diaz #include <mbedtls_config.h> 143b94189aSRoberto Vargas #include <mbedtls_common.h> 157d37aa17SJuan Castillo 167d37aa17SJuan Castillo /* 177d37aa17SJuan Castillo * mbed TLS heap 187d37aa17SJuan Castillo */ 19dcbf3932SQixiang Xu #if (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA) \ 20dcbf3932SQixiang Xu || (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA) 219db9c65aSQixiang Xu #define MBEDTLS_HEAP_SIZE (13*1024) 22b1883510SDavid Cunado #elif (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA) 2338aacad3SSoby Mathew #define MBEDTLS_HEAP_SIZE (7*1024) 247d37aa17SJuan Castillo #endif 257d37aa17SJuan Castillo static unsigned char heap[MBEDTLS_HEAP_SIZE]; 267d37aa17SJuan Castillo 27*6c373345SRoberto Vargas static void cleanup(void) 28*6c373345SRoberto Vargas { 29*6c373345SRoberto Vargas ERROR("EXIT from BL2\n"); 30*6c373345SRoberto Vargas panic(); 31*6c373345SRoberto Vargas } 32*6c373345SRoberto Vargas 337d37aa17SJuan Castillo /* 347d37aa17SJuan Castillo * mbed TLS initialization function 357d37aa17SJuan Castillo */ 367d37aa17SJuan Castillo void mbedtls_init(void) 377d37aa17SJuan Castillo { 387d37aa17SJuan Castillo static int ready; 397d37aa17SJuan Castillo 407d37aa17SJuan Castillo if (!ready) { 41*6c373345SRoberto Vargas if (atexit(cleanup)) 42*6c373345SRoberto Vargas panic(); 43*6c373345SRoberto Vargas 447d37aa17SJuan Castillo /* Initialize the mbed TLS heap */ 45649dbf6fSJuan Castillo mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE); 46ab1794f5SAntonio Nino Diaz 47c46c18c5SAntonio Nino Diaz #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT 48ab1794f5SAntonio Nino Diaz /* Use reduced version of snprintf to save space. */ 49ab1794f5SAntonio Nino Diaz mbedtls_platform_set_snprintf(tf_snprintf); 50c46c18c5SAntonio Nino Diaz #endif 51ab1794f5SAntonio Nino Diaz 527d37aa17SJuan Castillo ready = 1; 537d37aa17SJuan Castillo } 547d37aa17SJuan Castillo } 55