blob: d37f2dba8014789bf5699c46c8bfe0fd9c7d1113 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
6#include <assert.h>
7#include <attestation.h>
8#include <attestation_priv.h>
9#include <debug.h>
10#include <errno.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000011#include <mbedtls/ecp.h>
12#include <mbedtls/memory_buffer_alloc.h>
13#include <memory_alloc.h>
Arunachalam Ganapathyf6491212023-02-23 16:04:34 +000014#include <simd.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000015#include <sizes.h>
16
17/*
18 * Memory buffer for the allocator during key initialization.
19 *
20 * Used to compute the public key and setup a PRNG object per CPU. PRNGs are
21 * needed for key blinding during EC signing.
22 *
23 * Memory requirements:
24 * +------------------------+-------+ ------------------------+
25 * | | MAX | Persisting allocation |
26 * +------------------------+-------+-------------------------+
27 * | Public key computation | 2.4K | 0.4K |
28 * +------------------------+-------+-------------------------+
29 * | PRNG setup | 6.1K | 3.7K |
30 * +------------------------+-------+-------------------------+
31 *
32 * Measured with:
33 * src/lib/memory_buffer_alloc.c: mbedtls_memory_buffer_alloc_status()
34 */
35#define INIT_HEAP_PAGES 3
36
37static unsigned char mem_buf[INIT_HEAP_PAGES * SZ_4K]
38 __aligned(sizeof(unsigned long));
39
40static bool attest_initialized;
41
42struct buffer_alloc_ctx init_ctx;
43
44int attestation_init(void)
45{
46 int ret;
47
48 /*
49 * Associate the allocated heap for mbedtls with the current CPU.
50 */
51 buffer_alloc_ctx_assign(&init_ctx);
52
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000053 SIMD_FPU_ALLOW(mbedtls_memory_buffer_alloc_init(mem_buf,
54 sizeof(mem_buf)));
Soby Mathewb4c6df42022-11-09 11:13:29 +000055
56 /*
57 * Set the number of max operations per ECC signing iteration
58 * Check for effective minimum values for
59 * - ext/mbedtls/include/mbedtls/ecp.h:493
60 *
61 * This adjusts the length of a single signing loop.
62 */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000063 SIMD_FPU_ALLOW(mbedtls_ecp_set_max_ops(MBEDTLS_ECP_MAX_OPS));
Soby Mathewb4c6df42022-11-09 11:13:29 +000064
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000065 SIMD_FPU_ALLOW(ret = attest_rnd_prng_init());
Soby Mathewb4c6df42022-11-09 11:13:29 +000066 if (ret != 0) {
67 return ret;
68 }
69
70 /* Retrieve the platform key from root world */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000071 SIMD_FPU_ALLOW(ret = attest_init_realm_attestation_key());
Soby Mathewb4c6df42022-11-09 11:13:29 +000072 if (ret != 0) {
73 return ret;
74 }
75
Soby Mathewb4c6df42022-11-09 11:13:29 +000076 /* Retrieve the platform token from root world */
77 ret = attest_setup_platform_token();
78 if (ret != 0) {
79 return ret;
80 }
81
82 buffer_alloc_ctx_unassign();
83
84 attest_initialized = true;
85
86 return 0;
87}
88
89int attestation_heap_ctx_init(unsigned char *buf, size_t buf_size)
90{
91 assert(buf != NULL);
92
93 if (attest_initialized == false) {
94 return -EINVAL;
95 }
96
97 /* Initialise the mbedTLS heap */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000098 mbedtls_memory_buffer_alloc_init(buf, buf_size);
Soby Mathewb4c6df42022-11-09 11:13:29 +000099
100 return 0;
101}
102
103int attestation_heap_ctx_assign_pe(struct buffer_alloc_ctx *ctx)
104{
105 assert(ctx != NULL);
106
107 if (attest_initialized == false) {
108 return -EINVAL;
109 }
110
111 /*
112 * Associate the buffer_alloc_ctx to this CPU
113 */
114 buffer_alloc_ctx_assign(ctx);
115 return 0;
116}
117
Shruti Gupta9debb132022-12-13 14:38:49 +0000118int attestation_heap_ctx_unassign_pe(void)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000119{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000120 if (attest_initialized == false) {
121 return -EINVAL;
122 }
123
124 buffer_alloc_ctx_unassign();
125 return 0;
126}
127
128inline int attestation_heap_reinit_pe(unsigned char *buf, size_t buf_size)
129{
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000130 mbedtls_memory_buffer_alloc_init(buf, buf_size);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000131
132 return 0;
133}