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 | * |
| 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 | |
| 37 | static unsigned char mem_buf[INIT_HEAP_PAGES * SZ_4K] |
| 38 | __aligned(sizeof(unsigned long)); |
| 39 | |
| 40 | static bool attest_initialized; |
| 41 | |
| 42 | struct buffer_alloc_ctx init_ctx; |
| 43 | |
| 44 | int attestation_init(void) |
| 45 | { |
| 46 | int ret; |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame^] | 47 | psa_status_t psa_status; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Associate the allocated heap for mbedtls with the current CPU. |
| 51 | */ |
| 52 | buffer_alloc_ctx_assign(&init_ctx); |
| 53 | |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 54 | SIMD_FPU_ALLOW(mbedtls_memory_buffer_alloc_init(mem_buf, |
| 55 | sizeof(mem_buf))); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 56 | |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 57 | SIMD_FPU_ALLOW(ret = attest_rnd_prng_init()); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 58 | if (ret != 0) { |
| 59 | return ret; |
| 60 | } |
| 61 | |
Mate Toth-Pal | c69951d | 2023-03-17 17:30:50 +0100 | [diff] [blame^] | 62 | 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 Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 75 | /* Retrieve the platform key from root world */ |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 76 | SIMD_FPU_ALLOW(ret = attest_init_realm_attestation_key()); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 77 | if (ret != 0) { |
| 78 | return ret; |
| 79 | } |
| 80 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 81 | /* 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 | |
| 94 | int 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 Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 103 | mbedtls_memory_buffer_alloc_init(buf, buf_size); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int 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 Gupta | 9debb13 | 2022-12-13 14:38:49 +0000 | [diff] [blame] | 123 | int attestation_heap_ctx_unassign_pe(void) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 124 | { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 125 | if (attest_initialized == false) { |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | buffer_alloc_ctx_unassign(); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | inline int attestation_heap_reinit_pe(unsigned char *buf, size_t buf_size) |
| 134 | { |
Arunachalam Ganapathy | 5111993 | 2023-03-23 12:32:49 +0000 | [diff] [blame] | 135 | mbedtls_memory_buffer_alloc_init(buf, buf_size); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |