blob: f0fbc60b6c4b522b11d2d29f45b741df30fdef25 [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/memory_buffer_alloc.h>
12#include <memory_alloc.h>
Mate Toth-Palc69951d2023-03-17 17:30:50 +010013#include <psa/crypto.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
AlexeiFedorov4bbb0e02023-08-22 14:15:21 +010042static struct buffer_alloc_ctx init_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +000043
44int attestation_init(void)
45{
46 int ret;
Mate Toth-Palc69951d2023-03-17 17:30:50 +010047 psa_status_t psa_status;
Soby Mathewb4c6df42022-11-09 11:13:29 +000048
49 /*
50 * Associate the allocated heap for mbedtls with the current CPU.
51 */
AlexeiFedorov44a76a32023-08-29 16:53:26 +010052 ret = buffer_alloc_ctx_assign(&init_ctx);
53 if (ret != 0) {
54 return ret;
55 }
Soby Mathewb4c6df42022-11-09 11:13:29 +000056
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000057 SIMD_FPU_ALLOW(mbedtls_memory_buffer_alloc_init(mem_buf,
58 sizeof(mem_buf)));
Soby Mathewb4c6df42022-11-09 11:13:29 +000059
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000060 SIMD_FPU_ALLOW(ret = attest_rnd_prng_init());
Soby Mathewb4c6df42022-11-09 11:13:29 +000061 if (ret != 0) {
62 return ret;
63 }
64
Mate Toth-Palc69951d2023-03-17 17:30:50 +010065 SIMD_FPU_ALLOW(psa_status = psa_crypto_init());
66 if (psa_status != PSA_SUCCESS) {
67 return -EINVAL;
68 }
69
70 /*
71 * Set the number of max operations per ECC signing iteration to the
72 * configured value.
73 *
74 * This adjusts the length of a single signing loop.
75 */
76 SIMD_FPU_ALLOW(psa_interruptible_set_max_ops(MBEDTLS_ECP_MAX_OPS));
77
Soby Mathewb4c6df42022-11-09 11:13:29 +000078 /* Retrieve the platform key from root world */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000079 SIMD_FPU_ALLOW(ret = attest_init_realm_attestation_key());
Soby Mathewb4c6df42022-11-09 11:13:29 +000080 if (ret != 0) {
81 return ret;
82 }
83
Soby Mathewb4c6df42022-11-09 11:13:29 +000084 /* Retrieve the platform token from root world */
85 ret = attest_setup_platform_token();
86 if (ret != 0) {
87 return ret;
88 }
89
90 buffer_alloc_ctx_unassign();
91
92 attest_initialized = true;
93
94 return 0;
95}
96
97int attestation_heap_ctx_init(unsigned char *buf, size_t buf_size)
98{
99 assert(buf != NULL);
100
101 if (attest_initialized == false) {
102 return -EINVAL;
103 }
104
105 /* Initialise the mbedTLS heap */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000106 mbedtls_memory_buffer_alloc_init(buf, buf_size);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000107
108 return 0;
109}
110
111int attestation_heap_ctx_assign_pe(struct buffer_alloc_ctx *ctx)
112{
113 assert(ctx != NULL);
114
115 if (attest_initialized == false) {
116 return -EINVAL;
117 }
118
119 /*
120 * Associate the buffer_alloc_ctx to this CPU
121 */
AlexeiFedorov44a76a32023-08-29 16:53:26 +0100122 return buffer_alloc_ctx_assign(ctx);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000123}
124
Shruti Gupta9debb132022-12-13 14:38:49 +0000125int attestation_heap_ctx_unassign_pe(void)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000126{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000127 if (attest_initialized == false) {
128 return -EINVAL;
129 }
130
131 buffer_alloc_ctx_unassign();
132 return 0;
133}
134
135inline int attestation_heap_reinit_pe(unsigned char *buf, size_t buf_size)
136{
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000137 mbedtls_memory_buffer_alloc_init(buf, buf_size);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000138
139 return 0;
140}