Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 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 Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 11 | #include <mbedtls/memory_buffer_alloc.h> |
| 12 | #include <memory_alloc.h> |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 13 | #include <psa/crypto.h> |
Arunachalam Ganapathy | f649121 | 2023-02-23 16:04:34 +0000 | [diff] [blame] | 14 | #include <simd.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 15 | #include <sizes.h> |
| 16 | |
| 17 | /* |
| 18 | * Memory buffer for the allocator during key initialization. |
| 19 | * |
Rustam Ismayilov | 54296b8 | 2024-10-08 18:39:26 +0200 | [diff] [blame] | 20 | * Used to compute the public key and set up a PRNG object per CPU. PRNGs are |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 21 | * needed for key blinding during EC signing. |
| 22 | * |
| 23 | * Memory requirements: |
Jean-Philippe Brucker | ba22385 | 2023-07-31 15:26:35 +0100 | [diff] [blame] | 24 | * +------------------------+-------+-------------------------+ |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 25 | * | | MAX | Persisting allocation | |
| 26 | * +------------------------+-------+-------------------------+ |
Jean-Philippe Brucker | ba22385 | 2023-07-31 15:26:35 +0100 | [diff] [blame] | 27 | * | Public key computation | 2.9K | 0.1K | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 28 | * +------------------------+-------+-------------------------+ |
Jean-Philippe Brucker | ba22385 | 2023-07-31 15:26:35 +0100 | [diff] [blame] | 29 | * | one SHA256 HMAC_DRBG | | | |
| 30 | * | buffer | 364B | 364B | |
| 31 | * | | | | |
| 32 | * | PRNG setup for 32 CPUs | 12K | 11.6K | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 33 | * +------------------------+-------+-------------------------+ |
| 34 | * |
Jean-Philippe Brucker | ba22385 | 2023-07-31 15:26:35 +0100 | [diff] [blame] | 35 | * Measured with eg: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 36 | * src/lib/memory_buffer_alloc.c: mbedtls_memory_buffer_alloc_status() |
Jean-Philippe Brucker | ba22385 | 2023-07-31 15:26:35 +0100 | [diff] [blame] | 37 | * |
| 38 | * Reserve enough space for the temporary PRNG and per-CPU ones (see |
Mathieu Poirier | d8e1f30 | 2024-06-03 10:50:13 -0600 | [diff] [blame] | 39 | * attest_rnd_prng_init()), plus more space for other allocations. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 40 | */ |
Chuyue Luo | bcfe4c3 | 2023-10-24 13:48:19 +0100 | [diff] [blame] | 41 | #define PRNG_INIT_HEAP_SIZE ((MAX_CPUS + 1UL) * 364UL) |
Mathieu Poirier | d8e1f30 | 2024-06-03 10:50:13 -0600 | [diff] [blame] | 42 | #define MISC_PER_CPU (SZ_4K / 16U) |
| 43 | #define INIT_HEAP_SIZE (PRNG_INIT_HEAP_SIZE + (MISC_PER_CPU * MAX_CPUS)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 44 | |
Jean-Philippe Brucker | ba22385 | 2023-07-31 15:26:35 +0100 | [diff] [blame] | 45 | static unsigned char mem_buf[INIT_HEAP_SIZE] |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 46 | __aligned(sizeof(unsigned long)); |
| 47 | |
| 48 | static bool attest_initialized; |
| 49 | |
AlexeiFedorov | 4bbb0e0 | 2023-08-22 14:15:21 +0100 | [diff] [blame] | 50 | static struct buffer_alloc_ctx init_ctx; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 51 | |
| 52 | int attestation_init(void) |
| 53 | { |
| 54 | int ret; |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 55 | psa_status_t psa_status; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 56 | |
Rustam Ismayilov | 54296b8 | 2024-10-08 18:39:26 +0200 | [diff] [blame] | 57 | /* Enable Data Independent Timing feature */ |
| 58 | write_dit(DIT_BIT); |
| 59 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 60 | /* |
| 61 | * Associate the allocated heap for mbedtls with the current CPU. |
| 62 | */ |
AlexeiFedorov | 44a76a3 | 2023-08-29 16:53:26 +0100 | [diff] [blame] | 63 | ret = buffer_alloc_ctx_assign(&init_ctx); |
| 64 | if (ret != 0) { |
| 65 | return ret; |
| 66 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 67 | |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 68 | SIMD_FPU_ALLOW(mbedtls_memory_buffer_alloc_init(mem_buf, |
| 69 | sizeof(mem_buf))); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 70 | |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 71 | SIMD_FPU_ALLOW(ret = attest_rnd_prng_init()); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 72 | if (ret != 0) { |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 73 | goto attest_init_fail; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 76 | SIMD_FPU_ALLOW(psa_status = psa_crypto_init()); |
| 77 | if (psa_status != PSA_SUCCESS) { |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 78 | ret = -EINVAL; |
| 79 | goto attest_init_fail; |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Set the number of max operations per ECC signing iteration to the |
| 84 | * configured value. |
| 85 | * |
| 86 | * This adjusts the length of a single signing loop. |
| 87 | */ |
| 88 | SIMD_FPU_ALLOW(psa_interruptible_set_max_ops(MBEDTLS_ECP_MAX_OPS)); |
| 89 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 90 | /* Retrieve the platform key from root world */ |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 91 | SIMD_FPU_ALLOW(ret = attest_init_realm_attestation_key()); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 92 | if (ret != 0) { |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 93 | goto attest_init_fail; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 96 | /* Retrieve the platform token from root world */ |
| 97 | ret = attest_setup_platform_token(); |
| 98 | if (ret != 0) { |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 99 | goto attest_init_fail; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Soby Mathew | 9ca5755 | 2024-10-03 12:21:36 +0100 | [diff] [blame] | 102 | #if ATTEST_EL3_TOKEN_SIGN |
| 103 | /* Initialize the EL3 queue */ |
| 104 | if (el3_token_sign_queue_init() != 0) { |
| 105 | WARN("EL3 queue init failed.\n"); |
| 106 | ret = -ENOTSUP; |
| 107 | goto attest_init_fail; |
| 108 | } |
| 109 | #endif |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 110 | attest_initialized = true; |
| 111 | |
Rustam Ismayilov | 54296b8 | 2024-10-08 18:39:26 +0200 | [diff] [blame] | 112 | /* Disable Data Independent Timing feature */ |
| 113 | write_dit(0x0); |
| 114 | |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 115 | attest_init_fail : |
| 116 | buffer_alloc_ctx_unassign(); |
| 117 | return ret; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | int attestation_heap_ctx_init(unsigned char *buf, size_t buf_size) |
| 121 | { |
| 122 | assert(buf != NULL); |
| 123 | |
| 124 | if (attest_initialized == false) { |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 125 | ERROR("Attestation init failed.\n"); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | /* Initialise the mbedTLS heap */ |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 130 | mbedtls_memory_buffer_alloc_init(buf, buf_size); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 135 | void attestation_heap_ctx_assign_pe(struct buffer_alloc_ctx *ctx) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 136 | { |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 137 | int ret __unused; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 138 | assert(ctx != NULL); |
| 139 | |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 140 | /* Associate the buffer_alloc_ctx to this CPU */ |
| 141 | ret = buffer_alloc_ctx_assign(ctx); |
| 142 | assert(ret == 0); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Soby Mathew | 376ffa4 | 2024-10-04 14:01:59 +0100 | [diff] [blame] | 145 | void attestation_heap_ctx_unassign_pe(void) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 146 | { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 147 | buffer_alloc_ctx_unassign(); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 148 | } |