Juan Castillo | 7d37aa1 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 1 | /* |
John Tsichritzis | 17e1335 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved. |
Juan Castillo | 7d37aa1 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 3 | * |
dp-arm | 82cb2c1 | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | 7d37aa1 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
John Tsichritzis | 6d01a46 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
Juan Castillo | 649dbf6 | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 10 | /* mbed TLS headers */ |
| 11 | #include <mbedtls/memory_buffer_alloc.h> |
Antonio Nino Diaz | ab1794f | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 12 | #include <mbedtls/platform.h> |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | |
| 14 | #include <common/debug.h> |
| 15 | #include <drivers/auth/mbedtls/mbedtls_common.h> |
Lucian Paul-Trifu | 3519afe | 2022-03-08 15:02:31 +0000 | [diff] [blame^] | 16 | #include MBEDTLS_CONFIG_FILE |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 17 | #include <plat/common/platform.h> |
Juan Castillo | 7d37aa1 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 18 | |
Roberto Vargas | 6c37334 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 19 | static void cleanup(void) |
| 20 | { |
| 21 | ERROR("EXIT from BL2\n"); |
| 22 | panic(); |
| 23 | } |
| 24 | |
Juan Castillo | 7d37aa1 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 25 | /* |
Juan Castillo | 649dbf6 | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 26 | * mbed TLS initialization function |
Juan Castillo | 7d37aa1 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 27 | */ |
| 28 | void mbedtls_init(void) |
| 29 | { |
| 30 | static int ready; |
John Tsichritzis | 6d01a46 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 31 | void *heap_addr; |
| 32 | size_t heap_size = 0; |
| 33 | int err; |
Juan Castillo | 7d37aa1 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 34 | |
| 35 | if (!ready) { |
Roberto Vargas | 6c37334 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 36 | if (atexit(cleanup)) |
| 37 | panic(); |
| 38 | |
Lucian Paul-Trifu | 3519afe | 2022-03-08 15:02:31 +0000 | [diff] [blame^] | 39 | #if DRTM_SUPPORT && defined(IMAGE_BL31) |
| 40 | /* |
| 41 | * XXX-LPT: Short-circuit the mbedtls heap linkage for DRTM. |
| 42 | * The heap linkage should ideally be integrated with the other sub- |
| 43 | * systems that require it (e.g. trusted board boot). |
| 44 | */ |
| 45 | err = get_mbedtls_heap_helper(&heap_addr, &heap_size); |
| 46 | #else |
John Tsichritzis | 6d01a46 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 47 | err = plat_get_mbedtls_heap(&heap_addr, &heap_size); |
Lucian Paul-Trifu | 3519afe | 2022-03-08 15:02:31 +0000 | [diff] [blame^] | 48 | #endif |
John Tsichritzis | 6d01a46 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 49 | |
| 50 | /* Ensure heap setup is proper */ |
| 51 | if (err < 0) { |
| 52 | ERROR("Mbed TLS failed to get a heap\n"); |
| 53 | panic(); |
| 54 | } |
| 55 | assert(heap_size >= TF_MBEDTLS_HEAP_SIZE); |
| 56 | |
Juan Castillo | 649dbf6 | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 57 | /* Initialize the mbed TLS heap */ |
John Tsichritzis | 6d01a46 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 58 | mbedtls_memory_buffer_alloc_init(heap_addr, heap_size); |
Antonio Nino Diaz | ab1794f | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 59 | |
Antonio Nino Diaz | c46c18c | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 60 | #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT |
Antonio Nino Diaz | 39b6cc6 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 61 | mbedtls_platform_set_snprintf(snprintf); |
Antonio Nino Diaz | c46c18c | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 62 | #endif |
Juan Castillo | 649dbf6 | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 63 | ready = 1; |
Juan Castillo | 7d37aa1 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
John Tsichritzis | 17e1335 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
Ambroise Vincent | 2374ab1 | 2019-04-10 12:50:27 +0100 | [diff] [blame] | 68 | * The following helper function simply returns the default allocated heap. |
| 69 | * It can be used by platforms for their plat_get_mbedtls_heap() implementation. |
John Tsichritzis | 17e1335 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 70 | */ |
Ambroise Vincent | 2374ab1 | 2019-04-10 12:50:27 +0100 | [diff] [blame] | 71 | int get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size) |
John Tsichritzis | 17e1335 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 72 | { |
| 73 | static unsigned char heap[TF_MBEDTLS_HEAP_SIZE]; |
| 74 | |
| 75 | assert(heap_addr != NULL); |
| 76 | assert(heap_size != NULL); |
| 77 | |
| 78 | *heap_addr = heap; |
| 79 | *heap_size = sizeof(heap); |
| 80 | return 0; |
| 81 | } |