blob: 5dd25fc8fa6fddeabfa7c4ee46b6e90c7e124f22 [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:
Jean-Philippe Bruckerba223852023-07-31 15:26:35 +010024 * +------------------------+-------+-------------------------+
Soby Mathewb4c6df42022-11-09 11:13:29 +000025 * | | MAX | Persisting allocation |
26 * +------------------------+-------+-------------------------+
Jean-Philippe Bruckerba223852023-07-31 15:26:35 +010027 * | Public key computation | 2.9K | 0.1K |
Soby Mathewb4c6df42022-11-09 11:13:29 +000028 * +------------------------+-------+-------------------------+
Jean-Philippe Bruckerba223852023-07-31 15:26:35 +010029 * | one SHA256 HMAC_DRBG | | |
30 * | buffer | 364B | 364B |
31 * | | | |
32 * | PRNG setup for 32 CPUs | 12K | 11.6K |
Soby Mathewb4c6df42022-11-09 11:13:29 +000033 * +------------------------+-------+-------------------------+
34 *
Jean-Philippe Bruckerba223852023-07-31 15:26:35 +010035 * Measured with eg:
Soby Mathewb4c6df42022-11-09 11:13:29 +000036 * src/lib/memory_buffer_alloc.c: mbedtls_memory_buffer_alloc_status()
Jean-Philippe Bruckerba223852023-07-31 15:26:35 +010037 *
38 * Reserve enough space for the temporary PRNG and per-CPU ones (see
39 * attest_rnd_prng_init()), plus a page for other allocations.
Soby Mathewb4c6df42022-11-09 11:13:29 +000040 */
Chuyue Luobcfe4c32023-10-24 13:48:19 +010041#define PRNG_INIT_HEAP_SIZE ((MAX_CPUS + 1UL) * 364UL)
Jean-Philippe Bruckerba223852023-07-31 15:26:35 +010042#define INIT_HEAP_SIZE (PRNG_INIT_HEAP_SIZE + SZ_4K)
Soby Mathewb4c6df42022-11-09 11:13:29 +000043
Jean-Philippe Bruckerba223852023-07-31 15:26:35 +010044static unsigned char mem_buf[INIT_HEAP_SIZE]
Soby Mathewb4c6df42022-11-09 11:13:29 +000045 __aligned(sizeof(unsigned long));
46
47static bool attest_initialized;
48
AlexeiFedorov4bbb0e02023-08-22 14:15:21 +010049static struct buffer_alloc_ctx init_ctx;
Soby Mathewb4c6df42022-11-09 11:13:29 +000050
51int attestation_init(void)
52{
53 int ret;
Mate Toth-Palc69951d2023-03-17 17:30:50 +010054 psa_status_t psa_status;
Soby Mathewb4c6df42022-11-09 11:13:29 +000055
56 /*
57 * Associate the allocated heap for mbedtls with the current CPU.
58 */
AlexeiFedorov44a76a32023-08-29 16:53:26 +010059 ret = buffer_alloc_ctx_assign(&init_ctx);
60 if (ret != 0) {
61 return ret;
62 }
Soby Mathewb4c6df42022-11-09 11:13:29 +000063
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000064 SIMD_FPU_ALLOW(mbedtls_memory_buffer_alloc_init(mem_buf,
65 sizeof(mem_buf)));
Soby Mathewb4c6df42022-11-09 11:13:29 +000066
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000067 SIMD_FPU_ALLOW(ret = attest_rnd_prng_init());
Soby Mathewb4c6df42022-11-09 11:13:29 +000068 if (ret != 0) {
Soby Mathew376ffa42024-10-04 14:01:59 +010069 goto attest_init_fail;
Soby Mathewb4c6df42022-11-09 11:13:29 +000070 }
71
Mate Toth-Palc69951d2023-03-17 17:30:50 +010072 SIMD_FPU_ALLOW(psa_status = psa_crypto_init());
73 if (psa_status != PSA_SUCCESS) {
Soby Mathew376ffa42024-10-04 14:01:59 +010074 ret = -EINVAL;
75 goto attest_init_fail;
Mate Toth-Palc69951d2023-03-17 17:30:50 +010076 }
77
78 /*
79 * Set the number of max operations per ECC signing iteration to the
80 * configured value.
81 *
82 * This adjusts the length of a single signing loop.
83 */
84 SIMD_FPU_ALLOW(psa_interruptible_set_max_ops(MBEDTLS_ECP_MAX_OPS));
85
Soby Mathewb4c6df42022-11-09 11:13:29 +000086 /* Retrieve the platform key from root world */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +000087 SIMD_FPU_ALLOW(ret = attest_init_realm_attestation_key());
Soby Mathewb4c6df42022-11-09 11:13:29 +000088 if (ret != 0) {
Soby Mathew376ffa42024-10-04 14:01:59 +010089 goto attest_init_fail;
Soby Mathewb4c6df42022-11-09 11:13:29 +000090 }
91
Soby Mathewb4c6df42022-11-09 11:13:29 +000092 /* Retrieve the platform token from root world */
93 ret = attest_setup_platform_token();
94 if (ret != 0) {
Soby Mathew376ffa42024-10-04 14:01:59 +010095 goto attest_init_fail;
Soby Mathewb4c6df42022-11-09 11:13:29 +000096 }
97
Soby Mathew9ca57552024-10-03 12:21:36 +010098#if ATTEST_EL3_TOKEN_SIGN
99 /* Initialize the EL3 queue */
100 if (el3_token_sign_queue_init() != 0) {
101 WARN("EL3 queue init failed.\n");
102 ret = -ENOTSUP;
103 goto attest_init_fail;
104 }
105#endif
Soby Mathewb4c6df42022-11-09 11:13:29 +0000106 attest_initialized = true;
107
Soby Mathew376ffa42024-10-04 14:01:59 +0100108attest_init_fail :
109 buffer_alloc_ctx_unassign();
110 return ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000111}
112
113int attestation_heap_ctx_init(unsigned char *buf, size_t buf_size)
114{
115 assert(buf != NULL);
116
117 if (attest_initialized == false) {
Soby Mathew376ffa42024-10-04 14:01:59 +0100118 ERROR("Attestation init failed.\n");
Soby Mathewb4c6df42022-11-09 11:13:29 +0000119 return -EINVAL;
120 }
121
122 /* Initialise the mbedTLS heap */
Arunachalam Ganapathy51119932023-03-23 12:32:49 +0000123 mbedtls_memory_buffer_alloc_init(buf, buf_size);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000124
125 return 0;
126}
127
Soby Mathew376ffa42024-10-04 14:01:59 +0100128void attestation_heap_ctx_assign_pe(struct buffer_alloc_ctx *ctx)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000129{
Soby Mathew376ffa42024-10-04 14:01:59 +0100130 int ret __unused;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000131 assert(ctx != NULL);
132
Soby Mathew376ffa42024-10-04 14:01:59 +0100133 /* Associate the buffer_alloc_ctx to this CPU */
134 ret = buffer_alloc_ctx_assign(ctx);
135 assert(ret == 0);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000136}
137
Soby Mathew376ffa42024-10-04 14:01:59 +0100138void attestation_heap_ctx_unassign_pe(void)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000139{
Soby Mathewb4c6df42022-11-09 11:13:29 +0000140 buffer_alloc_ctx_unassign();
Soby Mathewb4c6df42022-11-09 11:13:29 +0000141}