blob: 25adf502f40edc2740d6ac0f853783a71365113f [file] [log] [blame]
Soby Mathew294e1cf2022-03-22 16:19:39 +00001/*
2 * Copyright (c) 2022, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6#include <stdint.h>
7#include <string.h>
Javier Almansa Sobrino4165e842022-04-25 17:18:15 +01008
Soby Mathew294e1cf2022-03-22 16:19:39 +00009#include <common/debug.h>
10#include <lib/spinlock.h>
11#include <lib/xlat_tables/xlat_tables_v2.h>
12#include <plat/common/platform.h>
13#include "rmmd_private.h"
14#include <services/rmmd_svc.h>
15
16static spinlock_t lock;
17
18/* For printing Realm attestation token hash */
19#define DIGITS_PER_BYTE 2UL
20#define LENGTH_OF_TERMINATING_ZERO_IN_BYTES 1UL
21#define BYTES_PER_LINE_BASE 4UL
22
23static void print_challenge(uint8_t *hash, size_t hash_size)
24{
25 size_t leftover;
26 /*
27 * bytes_per_line is always a power of two, so it can be used to
28 * construct mask with it when it is necessary to count remainder.
29 *
30 */
31 const size_t bytes_per_line = 1 << BYTES_PER_LINE_BASE;
32 char hash_text[(1 << BYTES_PER_LINE_BASE) * DIGITS_PER_BYTE +
33 LENGTH_OF_TERMINATING_ZERO_IN_BYTES];
34 const char hex_chars[] = {'0', '1', '2', '3', '4', '5', '6', '7',
35 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
36 unsigned int i;
37
38 for (i = 0U; i < hash_size; ++i) {
39 hash_text[(i & (bytes_per_line - 1)) * DIGITS_PER_BYTE] =
40 hex_chars[hash[i] >> 4];
41 hash_text[(i & (bytes_per_line - 1)) * DIGITS_PER_BYTE + 1] =
42 hex_chars[hash[i] & 0x0f];
43 if (((i + 1) & (bytes_per_line - 1)) == 0U) {
44 hash_text[bytes_per_line * DIGITS_PER_BYTE] = '\0';
45 VERBOSE("hash part %u = %s\n",
46 (i >> BYTES_PER_LINE_BASE) + 1, hash_text);
47 }
48 }
49
50 leftover = (size_t)i & (bytes_per_line - 1);
51
52 if (leftover != 0UL) {
53 hash_text[leftover * DIGITS_PER_BYTE] = '\0';
54 VERBOSE("hash part %u = %s\n", (i >> BYTES_PER_LINE_BASE) + 1,
55 hash_text);
56 }
57}
58
59/*
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +010060 * Helper function to validate that the buffer base and length are
61 * within range.
Soby Mathew294e1cf2022-03-22 16:19:39 +000062 */
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +010063static int validate_buffer_params(uint64_t buf_pa, uint64_t buf_len)
Soby Mathew294e1cf2022-03-22 16:19:39 +000064{
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +010065 unsigned long shared_buf_page;
66 uintptr_t shared_buf_base;
Soby Mathew294e1cf2022-03-22 16:19:39 +000067
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +010068 (void)plat_rmmd_get_el3_rmm_shared_mem(&shared_buf_base);
Soby Mathew294e1cf2022-03-22 16:19:39 +000069
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +010070 shared_buf_page = shared_buf_base & ~PAGE_SIZE_MASK;
71
72 /* Validate the buffer pointer */
73 if ((buf_pa & ~PAGE_SIZE_MASK) != shared_buf_page) {
74 ERROR("Buffer PA out of range\n");
75 return E_RMM_BAD_ADDR;
Soby Mathew294e1cf2022-03-22 16:19:39 +000076 }
77
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +010078 /* Validate the size of the shared area */
79 if (((buf_pa + buf_len - 1UL) & ~PAGE_SIZE_MASK) != shared_buf_page) {
80 ERROR("Invalid buffer length\n");
81 return E_RMM_INVAL;
Soby Mathew294e1cf2022-03-22 16:19:39 +000082 }
83
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +010084 return 0; /* No error */
85}
86
87int rmmd_attest_get_platform_token(uint64_t buf_pa, uint64_t *buf_size,
88 uint64_t c_size)
89{
90 int err;
91 uint8_t temp_buf[SHA512_DIGEST_SIZE];
Soby Mathew294e1cf2022-03-22 16:19:39 +000092
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +010093 err = validate_buffer_params(buf_pa, *buf_size);
Soby Mathew294e1cf2022-03-22 16:19:39 +000094 if (err != 0) {
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +010095 return err;
Soby Mathew294e1cf2022-03-22 16:19:39 +000096 }
97
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +010098 if ((c_size != SHA256_DIGEST_SIZE) &&
99 (c_size != SHA384_DIGEST_SIZE) &&
100 (c_size != SHA512_DIGEST_SIZE)) {
101 ERROR("Invalid hash size: %lu\n", c_size);
102 return E_RMM_INVAL;
103 }
104
105 spin_lock(&lock);
Soby Mathew294e1cf2022-03-22 16:19:39 +0000106
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100107 (void)memcpy(temp_buf, (void *)buf_pa, c_size);
108
109 print_challenge((uint8_t *)temp_buf, c_size);
Soby Mathew294e1cf2022-03-22 16:19:39 +0000110
111 /* Get the platform token. */
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100112 err = plat_rmmd_get_cca_attest_token((uintptr_t)buf_pa,
113 buf_size, (uintptr_t)temp_buf, c_size);
Soby Mathew294e1cf2022-03-22 16:19:39 +0000114
115 if (err != 0) {
116 ERROR("Failed to get platform token: %d.\n", err);
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100117 err = E_RMM_UNK;
Soby Mathew294e1cf2022-03-22 16:19:39 +0000118 }
119
Soby Mathew294e1cf2022-03-22 16:19:39 +0000120 spin_unlock(&lock);
121
122 return err;
123}
124
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100125int rmmd_attest_get_signing_key(uint64_t buf_pa, uint64_t *buf_size,
Soby Mathewf05d93a2022-03-22 16:21:19 +0000126 uint64_t ecc_curve)
127{
128 int err;
Soby Mathewf05d93a2022-03-22 16:21:19 +0000129
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100130 err = validate_buffer_params(buf_pa, *buf_size);
131 if (err != 0) {
132 return err;
Soby Mathewf05d93a2022-03-22 16:21:19 +0000133 }
134
135 if (ecc_curve != ATTEST_KEY_CURVE_ECC_SECP384R1) {
136 ERROR("Invalid ECC curve specified\n");
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100137 return E_RMM_INVAL;
Soby Mathewf05d93a2022-03-22 16:21:19 +0000138 }
139
140 spin_lock(&lock);
141
Soby Mathewf05d93a2022-03-22 16:21:19 +0000142 /* Get the Realm attestation key. */
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100143 err = plat_rmmd_get_cca_realm_attest_key((uintptr_t)buf_pa, buf_size,
144 (unsigned int)ecc_curve);
Soby Mathewf05d93a2022-03-22 16:21:19 +0000145 if (err != 0) {
146 ERROR("Failed to get attestation key: %d.\n", err);
Javier Almansa Sobrinodea652e2022-04-13 17:57:35 +0100147 err = E_RMM_UNK;
Soby Mathewf05d93a2022-03-22 16:21:19 +0000148 }
149
Soby Mathewf05d93a2022-03-22 16:21:19 +0000150 spin_unlock(&lock);
151
152 return err;
153}