Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 1 | /* |
Juan Pablo Conde | 9b94a42 | 2024-07-10 14:33:42 -0500 | [diff] [blame] | 2 | * Copyright (c) 2022-2024, Arm Limited. All rights reserved. |
Raghu Krishnamurthy | c11b60e | 2024-06-03 19:02:29 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2024, NVIDIA Corporation. All rights reserved. |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | */ |
Juan Pablo Conde | 9b94a42 | 2024-07-10 14:33:42 -0500 | [diff] [blame] | 7 | #include <errno.h> |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | #include <string.h> |
Javier Almansa Sobrino | 4165e84 | 2022-04-25 17:18:15 +0100 | [diff] [blame] | 10 | |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 11 | #include <common/debug.h> |
| 12 | #include <lib/spinlock.h> |
| 13 | #include <lib/xlat_tables/xlat_tables_v2.h> |
| 14 | #include <plat/common/platform.h> |
| 15 | #include "rmmd_private.h" |
Raghu Krishnamurthy | c11b60e | 2024-06-03 19:02:29 -0700 | [diff] [blame^] | 16 | #include <services/rmm_el3_token_sign.h> |
| 17 | #include <smccc_helpers.h> |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 18 | |
| 19 | static spinlock_t lock; |
| 20 | |
| 21 | /* For printing Realm attestation token hash */ |
| 22 | #define DIGITS_PER_BYTE 2UL |
| 23 | #define LENGTH_OF_TERMINATING_ZERO_IN_BYTES 1UL |
| 24 | #define BYTES_PER_LINE_BASE 4UL |
| 25 | |
| 26 | static void print_challenge(uint8_t *hash, size_t hash_size) |
| 27 | { |
| 28 | size_t leftover; |
| 29 | /* |
| 30 | * bytes_per_line is always a power of two, so it can be used to |
| 31 | * construct mask with it when it is necessary to count remainder. |
| 32 | * |
| 33 | */ |
| 34 | const size_t bytes_per_line = 1 << BYTES_PER_LINE_BASE; |
| 35 | char hash_text[(1 << BYTES_PER_LINE_BASE) * DIGITS_PER_BYTE + |
| 36 | LENGTH_OF_TERMINATING_ZERO_IN_BYTES]; |
| 37 | const char hex_chars[] = {'0', '1', '2', '3', '4', '5', '6', '7', |
| 38 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| 39 | unsigned int i; |
| 40 | |
| 41 | for (i = 0U; i < hash_size; ++i) { |
| 42 | hash_text[(i & (bytes_per_line - 1)) * DIGITS_PER_BYTE] = |
| 43 | hex_chars[hash[i] >> 4]; |
| 44 | hash_text[(i & (bytes_per_line - 1)) * DIGITS_PER_BYTE + 1] = |
| 45 | hex_chars[hash[i] & 0x0f]; |
| 46 | if (((i + 1) & (bytes_per_line - 1)) == 0U) { |
| 47 | hash_text[bytes_per_line * DIGITS_PER_BYTE] = '\0'; |
| 48 | VERBOSE("hash part %u = %s\n", |
| 49 | (i >> BYTES_PER_LINE_BASE) + 1, hash_text); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | leftover = (size_t)i & (bytes_per_line - 1); |
| 54 | |
| 55 | if (leftover != 0UL) { |
| 56 | hash_text[leftover * DIGITS_PER_BYTE] = '\0'; |
| 57 | VERBOSE("hash part %u = %s\n", (i >> BYTES_PER_LINE_BASE) + 1, |
| 58 | hash_text); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /* |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 63 | * Helper function to validate that the buffer base and length are |
| 64 | * within range. |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 65 | */ |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 66 | static int validate_buffer_params(uint64_t buf_pa, uint64_t buf_len) |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 67 | { |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 68 | unsigned long shared_buf_page; |
| 69 | uintptr_t shared_buf_base; |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 70 | |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 71 | (void)plat_rmmd_get_el3_rmm_shared_mem(&shared_buf_base); |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 72 | |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 73 | shared_buf_page = shared_buf_base & ~PAGE_SIZE_MASK; |
| 74 | |
| 75 | /* Validate the buffer pointer */ |
| 76 | if ((buf_pa & ~PAGE_SIZE_MASK) != shared_buf_page) { |
| 77 | ERROR("Buffer PA out of range\n"); |
| 78 | return E_RMM_BAD_ADDR; |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 81 | /* Validate the size of the shared area */ |
| 82 | if (((buf_pa + buf_len - 1UL) & ~PAGE_SIZE_MASK) != shared_buf_page) { |
| 83 | ERROR("Invalid buffer length\n"); |
| 84 | return E_RMM_INVAL; |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 87 | return 0; /* No error */ |
| 88 | } |
| 89 | |
| 90 | int rmmd_attest_get_platform_token(uint64_t buf_pa, uint64_t *buf_size, |
Juan Pablo Conde | 9b94a42 | 2024-07-10 14:33:42 -0500 | [diff] [blame] | 91 | uint64_t c_size, |
| 92 | uint64_t *remaining_len) |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 93 | { |
| 94 | int err; |
| 95 | uint8_t temp_buf[SHA512_DIGEST_SIZE]; |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 96 | |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 97 | err = validate_buffer_params(buf_pa, *buf_size); |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 98 | if (err != 0) { |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 99 | return err; |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 102 | if ((c_size != SHA256_DIGEST_SIZE) && |
| 103 | (c_size != SHA384_DIGEST_SIZE) && |
| 104 | (c_size != SHA512_DIGEST_SIZE)) { |
| 105 | ERROR("Invalid hash size: %lu\n", c_size); |
| 106 | return E_RMM_INVAL; |
| 107 | } |
| 108 | |
| 109 | spin_lock(&lock); |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 110 | |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 111 | (void)memcpy(temp_buf, (void *)buf_pa, c_size); |
| 112 | |
| 113 | print_challenge((uint8_t *)temp_buf, c_size); |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 114 | |
| 115 | /* Get the platform token. */ |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 116 | err = plat_rmmd_get_cca_attest_token((uintptr_t)buf_pa, |
Juan Pablo Conde | 9b94a42 | 2024-07-10 14:33:42 -0500 | [diff] [blame] | 117 | buf_size, (uintptr_t)temp_buf, c_size, remaining_len); |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 118 | |
Juan Pablo Conde | 9b94a42 | 2024-07-10 14:33:42 -0500 | [diff] [blame] | 119 | switch (err) { |
| 120 | case 0: |
| 121 | err = E_RMM_OK; |
| 122 | break; |
| 123 | case -EAGAIN: |
| 124 | err = E_RMM_AGAIN; |
| 125 | break; |
| 126 | case -EINVAL: |
| 127 | err = E_RMM_INVAL; |
| 128 | break; |
| 129 | default: |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 130 | ERROR("Failed to get platform token: %d.\n", err); |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 131 | err = E_RMM_UNK; |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Soby Mathew | 294e1cf | 2022-03-22 16:19:39 +0000 | [diff] [blame] | 134 | spin_unlock(&lock); |
| 135 | |
| 136 | return err; |
| 137 | } |
| 138 | |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 139 | int rmmd_attest_get_signing_key(uint64_t buf_pa, uint64_t *buf_size, |
Soby Mathew | f05d93a | 2022-03-22 16:21:19 +0000 | [diff] [blame] | 140 | uint64_t ecc_curve) |
| 141 | { |
| 142 | int err; |
Soby Mathew | f05d93a | 2022-03-22 16:21:19 +0000 | [diff] [blame] | 143 | |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 144 | err = validate_buffer_params(buf_pa, *buf_size); |
| 145 | if (err != 0) { |
| 146 | return err; |
Soby Mathew | f05d93a | 2022-03-22 16:21:19 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if (ecc_curve != ATTEST_KEY_CURVE_ECC_SECP384R1) { |
| 150 | ERROR("Invalid ECC curve specified\n"); |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 151 | return E_RMM_INVAL; |
Soby Mathew | f05d93a | 2022-03-22 16:21:19 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | spin_lock(&lock); |
| 155 | |
Soby Mathew | f05d93a | 2022-03-22 16:21:19 +0000 | [diff] [blame] | 156 | /* Get the Realm attestation key. */ |
Javier Almansa Sobrino | dea652e | 2022-04-13 17:57:35 +0100 | [diff] [blame] | 157 | err = plat_rmmd_get_cca_realm_attest_key((uintptr_t)buf_pa, buf_size, |
| 158 | (unsigned int)ecc_curve); |
Soby Mathew | f05d93a | 2022-03-22 16:21:19 +0000 | [diff] [blame] | 159 | if (err != 0) { |
| 160 | ERROR("Failed to get attestation key: %d.\n", err); |
Raghu Krishnamurthy | c11b60e | 2024-06-03 19:02:29 -0700 | [diff] [blame^] | 161 | err = E_RMM_UNK; |
Soby Mathew | f05d93a | 2022-03-22 16:21:19 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Soby Mathew | f05d93a | 2022-03-22 16:21:19 +0000 | [diff] [blame] | 164 | spin_unlock(&lock); |
| 165 | |
| 166 | return err; |
| 167 | } |
Raghu Krishnamurthy | c11b60e | 2024-06-03 19:02:29 -0700 | [diff] [blame^] | 168 | |
| 169 | static int rmmd_el3_token_sign_push_req(uint64_t buf_pa, uint64_t buf_size) |
| 170 | { |
| 171 | int err; |
| 172 | |
| 173 | err = validate_buffer_params(buf_pa, buf_size); |
| 174 | if (err != 0) { |
| 175 | return err; |
| 176 | } |
| 177 | |
| 178 | if (buf_size < sizeof(struct el3_token_sign_request)) { |
| 179 | return E_RMM_INVAL; |
| 180 | } |
| 181 | |
| 182 | spin_lock(&lock); |
| 183 | |
| 184 | /* Call platform port to handle attestation toekn signing request. */ |
| 185 | err = plat_rmmd_el3_token_sign_push_req((struct el3_token_sign_request *)buf_pa); |
| 186 | |
| 187 | spin_unlock(&lock); |
| 188 | |
| 189 | return err; |
| 190 | } |
| 191 | |
| 192 | static int rmmd_el3_token_sign_pull_resp(uint64_t buf_pa, uint64_t buf_size) |
| 193 | { |
| 194 | int err; |
| 195 | |
| 196 | err = validate_buffer_params(buf_pa, buf_size); |
| 197 | if (err != 0) { |
| 198 | return err; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | if (buf_size < sizeof(struct el3_token_sign_response)) { |
| 203 | return E_RMM_INVAL; |
| 204 | } |
| 205 | |
| 206 | spin_lock(&lock); |
| 207 | |
| 208 | /* Pull attestation signing response from HES. */ |
| 209 | err = plat_rmmd_el3_token_sign_pull_resp( |
| 210 | (struct el3_token_sign_response *)buf_pa); |
| 211 | |
| 212 | spin_unlock(&lock); |
| 213 | |
| 214 | return err; |
| 215 | } |
| 216 | |
| 217 | static int rmmd_attest_get_attest_pub_key(uint64_t buf_pa, uint64_t *buf_size, |
| 218 | uint64_t ecc_curve) |
| 219 | { |
| 220 | int err; |
| 221 | |
| 222 | err = validate_buffer_params(buf_pa, *buf_size); |
| 223 | if (err != 0) { |
| 224 | return err; |
| 225 | } |
| 226 | |
| 227 | if (ecc_curve != ATTEST_KEY_CURVE_ECC_SECP384R1) { |
| 228 | ERROR("Invalid ECC curve specified\n"); |
| 229 | return E_RMM_INVAL; |
| 230 | } |
| 231 | |
| 232 | spin_lock(&lock); |
| 233 | |
| 234 | /* Get the Realm attestation public key from platform port. */ |
| 235 | err = plat_rmmd_el3_token_sign_get_rak_pub( |
| 236 | (uintptr_t)buf_pa, buf_size, (unsigned int)ecc_curve); |
| 237 | |
| 238 | spin_unlock(&lock); |
| 239 | if (err != 0) { |
| 240 | ERROR("Failed to get attestation public key from HES: %d.\n", |
| 241 | err); |
| 242 | err = E_RMM_UNK; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | return err; |
| 247 | } |
| 248 | |
| 249 | uint64_t rmmd_el3_token_sign(void *handle, uint64_t opcode, uint64_t x2, |
| 250 | uint64_t x3, uint64_t x4) |
| 251 | { |
| 252 | int ret; |
| 253 | |
| 254 | switch (opcode) { |
| 255 | case RMM_EL3_TOKEN_SIGN_PUSH_REQ_OP: |
| 256 | ret = rmmd_el3_token_sign_push_req(x2, x3); |
| 257 | SMC_RET1(handle, ret); |
| 258 | case RMM_EL3_TOKEN_SIGN_PULL_RESP_OP: |
| 259 | ret = rmmd_el3_token_sign_pull_resp(x2, x3); |
| 260 | SMC_RET1(handle, ret); |
| 261 | case RMM_EL3_TOKEN_SIGN_GET_RAK_PUB_OP: |
| 262 | ret = rmmd_attest_get_attest_pub_key(x2, &x3, x4); |
| 263 | SMC_RET2(handle, ret, x3); |
| 264 | default: |
| 265 | SMC_RET1(handle, SMC_UNK); |
| 266 | } |
| 267 | } |