Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 1 | /* |
Govindraj Raja | 9c7dfb0 | 2023-01-11 18:34:58 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2015-2023, 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 | |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 10 | /* mbed TLS headers */ |
| 11 | #include <mbedtls/memory_buffer_alloc.h> |
Antonio Nino Diaz | 6b90f5e | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 12 | #include <mbedtls/platform.h> |
Govindraj Raja | 9c7dfb0 | 2023-01-11 18:34:58 +0000 | [diff] [blame^] | 13 | #include <mbedtls/version.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | |
| 15 | #include <common/debug.h> |
| 16 | #include <drivers/auth/mbedtls/mbedtls_common.h> |
Govindraj Raja | 9c7dfb0 | 2023-01-11 18:34:58 +0000 | [diff] [blame^] | 17 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 18 | #include <plat/common/platform.h> |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 19 | |
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 20 | static void cleanup(void) |
| 21 | { |
| 22 | ERROR("EXIT from BL2\n"); |
| 23 | panic(); |
| 24 | } |
| 25 | |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 26 | /* |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 27 | * mbed TLS initialization function |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 28 | */ |
| 29 | void mbedtls_init(void) |
| 30 | { |
| 31 | static int ready; |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 32 | void *heap_addr; |
| 33 | size_t heap_size = 0; |
| 34 | int err; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 35 | |
| 36 | if (!ready) { |
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 37 | if (atexit(cleanup)) |
| 38 | panic(); |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 39 | |
| 40 | err = plat_get_mbedtls_heap(&heap_addr, &heap_size); |
| 41 | |
| 42 | /* Ensure heap setup is proper */ |
| 43 | if (err < 0) { |
| 44 | ERROR("Mbed TLS failed to get a heap\n"); |
| 45 | panic(); |
| 46 | } |
| 47 | assert(heap_size >= TF_MBEDTLS_HEAP_SIZE); |
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 48 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 49 | /* Initialize the mbed TLS heap */ |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 50 | mbedtls_memory_buffer_alloc_init(heap_addr, heap_size); |
Antonio Nino Diaz | 6b90f5e | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 51 | |
Antonio Nino Diaz | f1481b2 | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 52 | #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 53 | mbedtls_platform_set_snprintf(snprintf); |
Antonio Nino Diaz | f1481b2 | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 54 | #endif |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 55 | ready = 1; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 56 | } |
| 57 | } |
John Tsichritzis | 69c88d1 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
Ambroise Vincent | d207f56 | 2019-04-10 12:50:27 +0100 | [diff] [blame] | 60 | * The following helper function simply returns the default allocated heap. |
| 61 | * It can be used by platforms for their plat_get_mbedtls_heap() implementation. |
John Tsichritzis | 69c88d1 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 62 | */ |
Ambroise Vincent | d207f56 | 2019-04-10 12:50:27 +0100 | [diff] [blame] | 63 | int get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size) |
John Tsichritzis | 69c88d1 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 64 | { |
| 65 | static unsigned char heap[TF_MBEDTLS_HEAP_SIZE]; |
| 66 | |
| 67 | assert(heap_addr != NULL); |
| 68 | assert(heap_size != NULL); |
| 69 | |
| 70 | *heap_addr = heap; |
| 71 | *heap_size = sizeof(heap); |
| 72 | return 0; |
| 73 | } |