Juan Pablo Conde | 88ffad2 | 2024-10-11 21:22:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2024, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <assert.h> |
| 9 | #include <stdio.h> |
| 10 | |
| 11 | #include <realm_rsi.h> |
| 12 | |
| 13 | #include <arch_features.h> |
| 14 | #include <debug.h> |
| 15 | #include <realm_def.h> |
| 16 | #include <sync.h> |
| 17 | #include <realm_helpers.h> |
| 18 | |
| 19 | #define CHALLENGE_SIZE 8 |
| 20 | |
| 21 | static unsigned char attest_token_buffer[REALM_TOKEN_BUF_SIZE] |
| 22 | __aligned(GRANULE_SIZE); |
| 23 | static uint64_t attest_token_offset; |
| 24 | |
| 25 | bool test_realm_attestation(void) |
| 26 | { |
| 27 | u_register_t rsi_ret; |
| 28 | u_register_t bytes_copied; |
| 29 | u_register_t token_upper_bound; |
| 30 | u_register_t challenge[CHALLENGE_SIZE]; |
| 31 | |
| 32 | for (unsigned int i = 0U; i < CHALLENGE_SIZE; i++) { |
| 33 | challenge[i] = realm_rand64(); |
| 34 | } |
| 35 | |
| 36 | rsi_ret = rsi_attest_token_init(challenge[0], |
| 37 | challenge[1], |
| 38 | challenge[2], |
| 39 | challenge[3], |
| 40 | challenge[4], |
| 41 | challenge[5], |
| 42 | challenge[6], |
| 43 | challenge[7], |
| 44 | &token_upper_bound); |
| 45 | |
| 46 | if (rsi_ret != RSI_SUCCESS) { |
| 47 | realm_printf("RSI_ATTEST_TOKEN_INIT" |
| 48 | " returned with code %lu\n", rsi_ret); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | if (token_upper_bound > REALM_TOKEN_BUF_SIZE) { |
| 53 | realm_printf("Attestation token buffer is not large enough" |
| 54 | " to hold the token.\n"); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | do { |
| 59 | rsi_ret = rsi_attest_token_continue( |
| 60 | (u_register_t)attest_token_buffer, |
| 61 | attest_token_offset, |
| 62 | REALM_TOKEN_BUF_SIZE, |
| 63 | &bytes_copied); |
| 64 | |
| 65 | if ((rsi_ret != RSI_SUCCESS) && (rsi_ret != RSI_INCOMPLETE)) { |
| 66 | realm_printf("RSI_ATTEST_TOKEN_CONTINUE" |
| 67 | " returned with code %lu\n", rsi_ret); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | attest_token_offset += bytes_copied; |
| 72 | |
| 73 | } while (rsi_ret != RSI_SUCCESS); |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | bool test_realm_attestation_fault(void) |
| 79 | { |
| 80 | u_register_t rsi_ret; |
| 81 | u_register_t bytes_copied; |
| 82 | |
| 83 | /* |
| 84 | * This RSI call will fail as RSI_ATTEST_TOKEN_INIT has to be invoked |
| 85 | * before calling RSI_ATTEST_TOKEN_CONTINUE. |
| 86 | */ |
| 87 | rsi_ret = rsi_attest_token_continue( |
| 88 | (u_register_t)attest_token_buffer, |
| 89 | attest_token_offset, |
| 90 | REALM_TOKEN_BUF_SIZE, |
| 91 | &bytes_copied); |
| 92 | |
| 93 | if ((rsi_ret == RSI_SUCCESS) || (rsi_ret == RSI_INCOMPLETE)) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |