xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_common.c (revision 7d37aa171158422b5ee7ee6c3cdad58f6aa066b4)
1*7d37aa17SJuan Castillo /*
2*7d37aa17SJuan Castillo  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3*7d37aa17SJuan Castillo  *
4*7d37aa17SJuan Castillo  * Redistribution and use in source and binary forms, with or without
5*7d37aa17SJuan Castillo  * modification, are permitted provided that the following conditions are met:
6*7d37aa17SJuan Castillo  *
7*7d37aa17SJuan Castillo  * Redistributions of source code must retain the above copyright notice, this
8*7d37aa17SJuan Castillo  * list of conditions and the following disclaimer.
9*7d37aa17SJuan Castillo  *
10*7d37aa17SJuan Castillo  * Redistributions in binary form must reproduce the above copyright notice,
11*7d37aa17SJuan Castillo  * this list of conditions and the following disclaimer in the documentation
12*7d37aa17SJuan Castillo  * and/or other materials provided with the distribution.
13*7d37aa17SJuan Castillo  *
14*7d37aa17SJuan Castillo  * Neither the name of ARM nor the names of its contributors may be used
15*7d37aa17SJuan Castillo  * to endorse or promote products derived from this software without specific
16*7d37aa17SJuan Castillo  * prior written permission.
17*7d37aa17SJuan Castillo  *
18*7d37aa17SJuan Castillo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*7d37aa17SJuan Castillo  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*7d37aa17SJuan Castillo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*7d37aa17SJuan Castillo  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*7d37aa17SJuan Castillo  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*7d37aa17SJuan Castillo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*7d37aa17SJuan Castillo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*7d37aa17SJuan Castillo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*7d37aa17SJuan Castillo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*7d37aa17SJuan Castillo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*7d37aa17SJuan Castillo  * POSSIBILITY OF SUCH DAMAGE.
29*7d37aa17SJuan Castillo  */
30*7d37aa17SJuan Castillo 
31*7d37aa17SJuan Castillo #include <assert.h>
32*7d37aa17SJuan Castillo 
33*7d37aa17SJuan Castillo /* mbedTLS headers */
34*7d37aa17SJuan Castillo #include <polarssl/memory_buffer_alloc.h>
35*7d37aa17SJuan Castillo 
36*7d37aa17SJuan Castillo /*
37*7d37aa17SJuan Castillo  * mbedTLS heap
38*7d37aa17SJuan Castillo  */
39*7d37aa17SJuan Castillo #if (MBEDTLS_KEY_ALG_ID == MBEDTLS_ECDSA)
40*7d37aa17SJuan Castillo #define MBEDTLS_HEAP_SIZE		(14*1024)
41*7d37aa17SJuan Castillo #elif (MBEDTLS_KEY_ALG_ID == MBEDTLS_RSA)
42*7d37aa17SJuan Castillo #define MBEDTLS_HEAP_SIZE		(8*1024)
43*7d37aa17SJuan Castillo #endif
44*7d37aa17SJuan Castillo static unsigned char heap[MBEDTLS_HEAP_SIZE];
45*7d37aa17SJuan Castillo 
46*7d37aa17SJuan Castillo /*
47*7d37aa17SJuan Castillo  * mbedTLS initialization function
48*7d37aa17SJuan Castillo  *
49*7d37aa17SJuan Castillo  * Return: 0 = success, Otherwise = error
50*7d37aa17SJuan Castillo  */
51*7d37aa17SJuan Castillo void mbedtls_init(void)
52*7d37aa17SJuan Castillo {
53*7d37aa17SJuan Castillo 	static int ready;
54*7d37aa17SJuan Castillo 	int rc;
55*7d37aa17SJuan Castillo 
56*7d37aa17SJuan Castillo 	if (!ready) {
57*7d37aa17SJuan Castillo 		/* Initialize the mbedTLS heap */
58*7d37aa17SJuan Castillo 		rc = memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE);
59*7d37aa17SJuan Castillo 		if (rc == 0) {
60*7d37aa17SJuan Castillo 			ready = 1;
61*7d37aa17SJuan Castillo 		} else {
62*7d37aa17SJuan Castillo 			assert(0);
63*7d37aa17SJuan Castillo 		}
64*7d37aa17SJuan Castillo 	}
65*7d37aa17SJuan Castillo }
66