blob: 7f36f6a407c8af0d5027bcee98f2450832624347 [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
42struct buffer_alloc_ctx init_ctx;
43
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 */
52 buffer_alloc_ctx_assign(&init_ctx);
53
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000054 SIMD_FPU_ALLOW(mbedtls_memory_buffer_alloc_init(mem_buf,
55 sizeof(mem_buf)));
Soby Mathewb4c6df42022-11-09 11:13:29 +000056
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000057 SIMD_FPU_ALLOW(ret = attest_rnd_prng_init());
Soby Mathewb4c6df42022-11-09 11:13:29 +000058 if (ret != 0) {
59 return ret;
60 }
61
Mate Toth-Palc69951d2023-03-17 17:30:50 +010062 SIMD_FPU_ALLOW(psa_status = psa_crypto_init());
63 if (psa_status != PSA_SUCCESS) {
64 return -EINVAL;
65 }
66
67 /*
68 * Set the number of max operations per ECC signing iteration to the
69 * configured value.
70 *
71 * This adjusts the length of a single signing loop.
72 */
73 SIMD_FPU_ALLOW(psa_interruptible_set_max_ops(MBEDTLS_ECP_MAX_OPS));
74
Soby Mathewb4c6df42022-11-09 11:13:29 +000075 /* Retrieve the platform key from root world */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000076 SIMD_FPU_ALLOW(ret = attest_init_realm_attestation_key());
Soby Mathewb4c6df42022-11-09 11:13:29 +000077 if (ret != 0) {
78 return ret;
79 }
80
Soby Mathewb4c6df42022-11-09 11:13:29 +000081 /* Retrieve the platform token from root world */
82 ret = attest_setup_platform_token();
83 if (ret != 0) {
84 return ret;
85 }
86
87 buffer_alloc_ctx_unassign();
88
89 attest_initialized = true;
90
91 return 0;
92}
93
94int attestation_heap_ctx_init(unsigned char *buf, size_t buf_size)
95{
96 assert(buf != NULL);
97
98 if (attest_initialized == false) {
99 return -EINVAL;
100 }
101
102 /* Initialise the mbedTLS heap */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000103 mbedtls_memory_buffer_alloc_init(buf, buf_size);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000104
105 return 0;
106}
107
108int attestation_heap_ctx_assign_pe(struct buffer_alloc_ctx *ctx)
109{
110 assert(ctx != NULL);
111
112 if (attest_initialized == false) {
113 return -EINVAL;
114 }
115
116 /*
117 * Associate the buffer_alloc_ctx to this CPU
118 */
119 buffer_alloc_ctx_assign(ctx);
120 return 0;
121}
122
Shruti Gupta9debb132022-12-13 14:38:49 +0000123int attestation_heap_ctx_unassign_pe(void)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000124{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000125 if (attest_initialized == false) {
126 return -EINVAL;
127 }
128
129 buffer_alloc_ctx_unassign();
130 return 0;
131}
132
133inline int attestation_heap_reinit_pe(unsigned char *buf, size_t buf_size)
134{
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000135 mbedtls_memory_buffer_alloc_init(buf, buf_size);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000136
137 return 0;
138}