xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_common.c (revision 2374ab1799bedae1acc17fde0205d272f8111836)
17d37aa17SJuan Castillo /*
217e1335cSJohn Tsichritzis  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
37d37aa17SJuan Castillo  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
57d37aa17SJuan Castillo  */
67d37aa17SJuan Castillo 
76d01a463SJohn Tsichritzis #include <assert.h>
809d40e0eSAntonio Nino Diaz #include <stddef.h>
909d40e0eSAntonio Nino Diaz 
107d37aa17SJuan Castillo /* mbed TLS headers */
11649dbf6fSJuan Castillo #include <mbedtls/memory_buffer_alloc.h>
12ab1794f5SAntonio Nino Diaz #include <mbedtls/platform.h>
1309d40e0eSAntonio Nino Diaz 
1409d40e0eSAntonio Nino Diaz #include <common/debug.h>
1509d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_common.h>
1609d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_config.h>
1709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
187d37aa17SJuan Castillo 
196c373345SRoberto Vargas static void cleanup(void)
206c373345SRoberto Vargas {
216c373345SRoberto Vargas 	ERROR("EXIT from BL2\n");
226c373345SRoberto Vargas 	panic();
236c373345SRoberto Vargas }
246c373345SRoberto Vargas 
257d37aa17SJuan Castillo /*
267d37aa17SJuan Castillo  * mbed TLS initialization function
277d37aa17SJuan Castillo  */
287d37aa17SJuan Castillo void mbedtls_init(void)
297d37aa17SJuan Castillo {
307d37aa17SJuan Castillo 	static int ready;
316d01a463SJohn Tsichritzis 	void *heap_addr;
326d01a463SJohn Tsichritzis 	size_t heap_size = 0;
336d01a463SJohn Tsichritzis 	int err;
347d37aa17SJuan Castillo 
357d37aa17SJuan Castillo 	if (!ready) {
366c373345SRoberto Vargas 		if (atexit(cleanup))
376c373345SRoberto Vargas 			panic();
386c373345SRoberto Vargas 
396d01a463SJohn Tsichritzis 		err = plat_get_mbedtls_heap(&heap_addr, &heap_size);
406d01a463SJohn Tsichritzis 
416d01a463SJohn Tsichritzis 		/* Ensure heap setup is proper */
426d01a463SJohn Tsichritzis 		if (err < 0) {
436d01a463SJohn Tsichritzis 			ERROR("Mbed TLS failed to get a heap\n");
446d01a463SJohn Tsichritzis 			panic();
456d01a463SJohn Tsichritzis 		}
466d01a463SJohn Tsichritzis 		assert(heap_size >= TF_MBEDTLS_HEAP_SIZE);
476d01a463SJohn Tsichritzis 
487d37aa17SJuan Castillo 		/* Initialize the mbed TLS heap */
496d01a463SJohn Tsichritzis 		mbedtls_memory_buffer_alloc_init(heap_addr, heap_size);
50ab1794f5SAntonio Nino Diaz 
51c46c18c5SAntonio Nino Diaz #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
5239b6cc66SAntonio Nino Diaz 		mbedtls_platform_set_snprintf(snprintf);
53c46c18c5SAntonio Nino Diaz #endif
547d37aa17SJuan Castillo 		ready = 1;
557d37aa17SJuan Castillo 	}
567d37aa17SJuan Castillo }
5717e1335cSJohn Tsichritzis 
5817e1335cSJohn Tsichritzis /*
59*2374ab17SAmbroise Vincent  * The following helper function simply returns the default allocated heap.
60*2374ab17SAmbroise Vincent  * It can be used by platforms for their plat_get_mbedtls_heap() implementation.
6117e1335cSJohn Tsichritzis  */
62*2374ab17SAmbroise Vincent int get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size)
6317e1335cSJohn Tsichritzis {
6417e1335cSJohn Tsichritzis 	static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
6517e1335cSJohn Tsichritzis 
6617e1335cSJohn Tsichritzis 	assert(heap_addr != NULL);
6717e1335cSJohn Tsichritzis 	assert(heap_size != NULL);
6817e1335cSJohn Tsichritzis 
6917e1335cSJohn Tsichritzis 	*heap_addr = heap;
7017e1335cSJohn Tsichritzis 	*heap_size = sizeof(heap);
7117e1335cSJohn Tsichritzis 	return 0;
7217e1335cSJohn Tsichritzis }
73