Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 1 | /* |
Roberto Vargas | 52f707f | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 5 | */ |
6 | |||||
Antonio Nino Diaz | 6b90f5e | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 7 | #include <debug.h> |
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 8 | #include <stdlib.h> |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 9 | #include <stdio.h> |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 10 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 11 | /* mbed TLS headers */ |
12 | #include <mbedtls/memory_buffer_alloc.h> | ||||
Antonio Nino Diaz | 6b90f5e | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 13 | #include <mbedtls/platform.h> |
Antonio Nino Diaz | f1481b2 | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 14 | #include <mbedtls_config.h> |
Roberto Vargas | 52f707f | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 15 | #include <mbedtls_common.h> |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 16 | |
17 | /* | ||||
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 18 | * mbed TLS heap |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 19 | */ |
Qixiang Xu | aa05eea | 2017-08-24 15:26:39 +0800 | [diff] [blame] | 20 | #if (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA) \ |
21 | || (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA) | ||||
Qixiang Xu | 1c2aef1 | 2017-08-24 15:12:20 +0800 | [diff] [blame] | 22 | #define MBEDTLS_HEAP_SIZE (13*1024) |
David Cunado | c7a1b19 | 2017-05-10 16:38:44 +0100 | [diff] [blame] | 23 | #elif (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA) |
Soby Mathew | cd4a5a9 | 2017-06-05 12:18:04 +0100 | [diff] [blame] | 24 | #define MBEDTLS_HEAP_SIZE (7*1024) |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 25 | #endif |
26 | static unsigned char heap[MBEDTLS_HEAP_SIZE]; | ||||
27 | |||||
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 28 | static void cleanup(void) |
29 | { | ||||
30 | ERROR("EXIT from BL2\n"); | ||||
31 | panic(); | ||||
32 | } | ||||
33 | |||||
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 34 | /* |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 35 | * mbed TLS initialization function |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 36 | */ |
37 | void mbedtls_init(void) | ||||
38 | { | ||||
39 | static int ready; | ||||
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 40 | |
41 | if (!ready) { | ||||
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 42 | if (atexit(cleanup)) |
43 | panic(); | ||||
44 | |||||
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 45 | /* Initialize the mbed TLS heap */ |
46 | mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE); | ||||
Antonio Nino Diaz | 6b90f5e | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 47 | |
Antonio Nino Diaz | f1481b2 | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 48 | #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 49 | mbedtls_platform_set_snprintf(snprintf); |
Antonio Nino Diaz | f1481b2 | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 50 | #endif |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 51 | ready = 1; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 52 | } |
53 | } |