xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_common.c (revision 39b6cc66d670be41d6b51b644beb675f386a4240)
1 /*
2  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <debug.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 
11 /* mbed TLS headers */
12 #include <mbedtls/memory_buffer_alloc.h>
13 #include <mbedtls/platform.h>
14 #include <mbedtls_config.h>
15 #include <mbedtls_common.h>
16 
17 /*
18  * mbed TLS heap
19  */
20 #if (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA) \
21 	|| (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA)
22 #define MBEDTLS_HEAP_SIZE		(13*1024)
23 #elif (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA)
24 #define MBEDTLS_HEAP_SIZE		(7*1024)
25 #endif
26 static unsigned char heap[MBEDTLS_HEAP_SIZE];
27 
28 static void cleanup(void)
29 {
30 	ERROR("EXIT from BL2\n");
31 	panic();
32 }
33 
34 /*
35  * mbed TLS initialization function
36  */
37 void mbedtls_init(void)
38 {
39 	static int ready;
40 
41 	if (!ready) {
42 		if (atexit(cleanup))
43 			panic();
44 
45 		/* Initialize the mbed TLS heap */
46 		mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE);
47 
48 #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
49 		mbedtls_platform_set_snprintf(snprintf);
50 #endif
51 		ready = 1;
52 	}
53 }
54