xref: /rk3399_ARM-atf/services/std_svc/rmmd/rmmd_attest.c (revision dc65ae46439f4d1be06e3a016fe76319d7a62954)
10f9159b7SSoby Mathew /*
20f9159b7SSoby Mathew  * Copyright (c) 2022, Arm Limited. All rights reserved.
30f9159b7SSoby Mathew  *
40f9159b7SSoby Mathew  * SPDX-License-Identifier: BSD-3-Clause
50f9159b7SSoby Mathew  */
60f9159b7SSoby Mathew #include <stdint.h>
70f9159b7SSoby Mathew #include <string.h>
80f9159b7SSoby Mathew #include <common/debug.h>
90f9159b7SSoby Mathew #include <lib/spinlock.h>
100f9159b7SSoby Mathew #include <lib/xlat_tables/xlat_tables_v2.h>
110f9159b7SSoby Mathew #include <plat/common/platform.h>
120f9159b7SSoby Mathew #include "rmmd_private.h"
130f9159b7SSoby Mathew #include <services/rmmd_svc.h>
140f9159b7SSoby Mathew 
150f9159b7SSoby Mathew static spinlock_t lock;
160f9159b7SSoby Mathew 
170f9159b7SSoby Mathew /* For printing Realm attestation token hash */
180f9159b7SSoby Mathew #define DIGITS_PER_BYTE				2UL
190f9159b7SSoby Mathew #define LENGTH_OF_TERMINATING_ZERO_IN_BYTES	1UL
200f9159b7SSoby Mathew #define BYTES_PER_LINE_BASE			4UL
210f9159b7SSoby Mathew 
220f9159b7SSoby Mathew static void print_challenge(uint8_t *hash, size_t hash_size)
230f9159b7SSoby Mathew {
240f9159b7SSoby Mathew 	size_t leftover;
250f9159b7SSoby Mathew 	/*
260f9159b7SSoby Mathew 	 * bytes_per_line is always a power of two, so it can be used to
270f9159b7SSoby Mathew 	 * construct mask with it when it is necessary to count remainder.
280f9159b7SSoby Mathew 	 *
290f9159b7SSoby Mathew 	 */
300f9159b7SSoby Mathew 	const size_t bytes_per_line = 1 << BYTES_PER_LINE_BASE;
310f9159b7SSoby Mathew 	char hash_text[(1 << BYTES_PER_LINE_BASE) * DIGITS_PER_BYTE +
320f9159b7SSoby Mathew 		LENGTH_OF_TERMINATING_ZERO_IN_BYTES];
330f9159b7SSoby Mathew 	const char hex_chars[] = {'0', '1', '2', '3', '4', '5', '6', '7',
340f9159b7SSoby Mathew 				  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
350f9159b7SSoby Mathew 	unsigned int i;
360f9159b7SSoby Mathew 
370f9159b7SSoby Mathew 	for (i = 0U; i < hash_size; ++i) {
380f9159b7SSoby Mathew 		hash_text[(i & (bytes_per_line - 1)) * DIGITS_PER_BYTE] =
390f9159b7SSoby Mathew 			hex_chars[hash[i] >> 4];
400f9159b7SSoby Mathew 		hash_text[(i & (bytes_per_line - 1)) * DIGITS_PER_BYTE + 1] =
410f9159b7SSoby Mathew 			hex_chars[hash[i] & 0x0f];
420f9159b7SSoby Mathew 		if (((i + 1) & (bytes_per_line - 1)) == 0U) {
430f9159b7SSoby Mathew 			hash_text[bytes_per_line * DIGITS_PER_BYTE] = '\0';
440f9159b7SSoby Mathew 			VERBOSE("hash part %u = %s\n",
450f9159b7SSoby Mathew 				(i >> BYTES_PER_LINE_BASE) + 1, hash_text);
460f9159b7SSoby Mathew 		}
470f9159b7SSoby Mathew 	}
480f9159b7SSoby Mathew 
490f9159b7SSoby Mathew 	leftover = (size_t)i & (bytes_per_line - 1);
500f9159b7SSoby Mathew 
510f9159b7SSoby Mathew 	if (leftover != 0UL) {
520f9159b7SSoby Mathew 		hash_text[leftover * DIGITS_PER_BYTE] = '\0';
530f9159b7SSoby Mathew 		VERBOSE("hash part %u = %s\n", (i >> BYTES_PER_LINE_BASE) + 1,
540f9159b7SSoby Mathew 			hash_text);
550f9159b7SSoby Mathew 	}
560f9159b7SSoby Mathew }
570f9159b7SSoby Mathew 
580f9159b7SSoby Mathew /*
59*dc65ae46SJavier Almansa Sobrino  * Helper function to validate that the buffer base and length are
60*dc65ae46SJavier Almansa Sobrino  * within range.
610f9159b7SSoby Mathew  */
62*dc65ae46SJavier Almansa Sobrino static int validate_buffer_params(uint64_t buf_pa, uint64_t buf_len)
630f9159b7SSoby Mathew {
64*dc65ae46SJavier Almansa Sobrino 	unsigned long shared_buf_page;
65*dc65ae46SJavier Almansa Sobrino 	uintptr_t shared_buf_base;
660f9159b7SSoby Mathew 
67*dc65ae46SJavier Almansa Sobrino 	(void)plat_rmmd_get_el3_rmm_shared_mem(&shared_buf_base);
680f9159b7SSoby Mathew 
69*dc65ae46SJavier Almansa Sobrino 	shared_buf_page = shared_buf_base & ~PAGE_SIZE_MASK;
70*dc65ae46SJavier Almansa Sobrino 
71*dc65ae46SJavier Almansa Sobrino 	/* Validate the buffer pointer */
72*dc65ae46SJavier Almansa Sobrino 	if ((buf_pa & ~PAGE_SIZE_MASK) != shared_buf_page) {
73*dc65ae46SJavier Almansa Sobrino 		ERROR("Buffer PA out of range\n");
74*dc65ae46SJavier Almansa Sobrino 		return E_RMM_BAD_ADDR;
750f9159b7SSoby Mathew 	}
760f9159b7SSoby Mathew 
77*dc65ae46SJavier Almansa Sobrino 	/* Validate the size of the shared area */
78*dc65ae46SJavier Almansa Sobrino 	if (((buf_pa + buf_len - 1UL) & ~PAGE_SIZE_MASK) != shared_buf_page) {
79*dc65ae46SJavier Almansa Sobrino 		ERROR("Invalid buffer length\n");
80*dc65ae46SJavier Almansa Sobrino 		return E_RMM_INVAL;
81*dc65ae46SJavier Almansa Sobrino 	}
82*dc65ae46SJavier Almansa Sobrino 
83*dc65ae46SJavier Almansa Sobrino 	return 0; /* No error */
84*dc65ae46SJavier Almansa Sobrino }
85*dc65ae46SJavier Almansa Sobrino 
86*dc65ae46SJavier Almansa Sobrino int rmmd_attest_get_platform_token(uint64_t buf_pa, uint64_t *buf_size,
87*dc65ae46SJavier Almansa Sobrino 				   uint64_t c_size)
88*dc65ae46SJavier Almansa Sobrino {
89*dc65ae46SJavier Almansa Sobrino 	int err;
90*dc65ae46SJavier Almansa Sobrino 	uint8_t temp_buf[SHA512_DIGEST_SIZE];
91*dc65ae46SJavier Almansa Sobrino 
92*dc65ae46SJavier Almansa Sobrino 	err = validate_buffer_params(buf_pa, *buf_size);
93*dc65ae46SJavier Almansa Sobrino 	if (err != 0) {
94*dc65ae46SJavier Almansa Sobrino 		return err;
95*dc65ae46SJavier Almansa Sobrino 	}
96*dc65ae46SJavier Almansa Sobrino 
97*dc65ae46SJavier Almansa Sobrino 	if ((c_size != SHA256_DIGEST_SIZE) &&
98*dc65ae46SJavier Almansa Sobrino 	    (c_size != SHA384_DIGEST_SIZE) &&
99*dc65ae46SJavier Almansa Sobrino 	    (c_size != SHA512_DIGEST_SIZE)) {
100*dc65ae46SJavier Almansa Sobrino 		ERROR("Invalid hash size: %lu\n", c_size);
101*dc65ae46SJavier Almansa Sobrino 		return E_RMM_INVAL;
1020f9159b7SSoby Mathew 	}
1030f9159b7SSoby Mathew 
1040f9159b7SSoby Mathew 	spin_lock(&lock);
1050f9159b7SSoby Mathew 
106*dc65ae46SJavier Almansa Sobrino 	(void)memcpy(temp_buf, (void *)buf_pa, c_size);
1070f9159b7SSoby Mathew 
108*dc65ae46SJavier Almansa Sobrino 	print_challenge((uint8_t *)temp_buf, c_size);
1090f9159b7SSoby Mathew 
1100f9159b7SSoby Mathew 	/* Get the platform token. */
111*dc65ae46SJavier Almansa Sobrino 	err = plat_rmmd_get_cca_attest_token((uintptr_t)buf_pa,
112*dc65ae46SJavier Almansa Sobrino 		buf_size, (uintptr_t)temp_buf, c_size);
1130f9159b7SSoby Mathew 
1140f9159b7SSoby Mathew 	if (err != 0) {
1150f9159b7SSoby Mathew 		ERROR("Failed to get platform token: %d.\n", err);
116*dc65ae46SJavier Almansa Sobrino 		err = E_RMM_UNK;
1170f9159b7SSoby Mathew 	}
1180f9159b7SSoby Mathew 
1190f9159b7SSoby Mathew 	spin_unlock(&lock);
1200f9159b7SSoby Mathew 
1210f9159b7SSoby Mathew 	return err;
1220f9159b7SSoby Mathew }
1230f9159b7SSoby Mathew 
124*dc65ae46SJavier Almansa Sobrino int rmmd_attest_get_signing_key(uint64_t buf_pa, uint64_t *buf_size,
125a0435105SSoby Mathew 				uint64_t ecc_curve)
126a0435105SSoby Mathew {
127a0435105SSoby Mathew 	int err;
128a0435105SSoby Mathew 
129*dc65ae46SJavier Almansa Sobrino 	err = validate_buffer_params(buf_pa, *buf_size);
130*dc65ae46SJavier Almansa Sobrino 	if (err != 0) {
131*dc65ae46SJavier Almansa Sobrino 		return err;
132a0435105SSoby Mathew 	}
133a0435105SSoby Mathew 
134a0435105SSoby Mathew 	if (ecc_curve != ATTEST_KEY_CURVE_ECC_SECP384R1) {
135a0435105SSoby Mathew 		ERROR("Invalid ECC curve specified\n");
136*dc65ae46SJavier Almansa Sobrino 		return E_RMM_INVAL;
137a0435105SSoby Mathew 	}
138a0435105SSoby Mathew 
139a0435105SSoby Mathew 	spin_lock(&lock);
140a0435105SSoby Mathew 
141a0435105SSoby Mathew 	/* Get the Realm attestation key. */
142*dc65ae46SJavier Almansa Sobrino 	err = plat_rmmd_get_cca_realm_attest_key((uintptr_t)buf_pa, buf_size,
143*dc65ae46SJavier Almansa Sobrino 						 (unsigned int)ecc_curve);
144a0435105SSoby Mathew 	if (err != 0) {
145a0435105SSoby Mathew 		ERROR("Failed to get attestation key: %d.\n", err);
146*dc65ae46SJavier Almansa Sobrino 		err =  E_RMM_UNK;
147a0435105SSoby Mathew 	}
148a0435105SSoby Mathew 
149a0435105SSoby Mathew 	spin_unlock(&lock);
150a0435105SSoby Mathew 
151a0435105SSoby Mathew 	return err;
152a0435105SSoby Mathew }
153