blob: 9f465a05f3b8635046c091e3442bcc53559298bf [file] [log] [blame]
Thaddeus Gonzalez-Serna799225f2025-06-02 09:42:49 -05001/*
2 * Copyright (c) 2025, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6#include <uuid.h>
7#include <io_storage.h>
8#include <platform.h>
9#include <platform_def.h>
10#include <psci.h>
11#include <smccc.h>
12#include <status.h>
13#include <tftf_lib.h>
14#include <uuid_utils.h>
15#include "neg_scenario_test_infra.h"
16
17#define CRYPTO_SUPPORT 1
18
19static fip_toc_entry_t *
20find_fiptoc_entry_t(const int fip_base, const uuid_t *uuid)
21{
22 fip_toc_entry_t *current_file =
23 (fip_toc_entry_t *) (fip_base + sizeof(fip_toc_header_t));
24
25 while (!is_uuid_null(&(current_file->uuid))) {
26 if (uuid_equal(&(current_file->uuid), uuid)){
27 return current_file;
28 }
29
30 current_file += 1;
31 };
32
33 return NULL;
34}
35
36test_result_t test_invalid_rotpk(void)
37{
38 smc_args args = { SMC_PSCI_SYSTEM_RESET };
39 smc_ret_values ret = (smc_ret_values){0};
40 const uuid_t trusted_cert = UUID_TRUSTED_KEY_CERT;
41
42 uintptr_t handle;
43 fip_toc_entry_t * cert;
44 size_t exp_len, len;
45 int address, rc;
46 void * paramOut = NULL;
47
48 if(tftf_is_rebooted() ){
49 /* ROTPK is tampered with and upon reboot tfa should not reach this point */
50 return TEST_RESULT_FAIL;
51 }
52
53 /* Locate Trusted Key certificate memory address by using UUID */
54 cert = find_fiptoc_entry_t(PLAT_ARM_FIP_BASE, &trusted_cert);
55 if (cert == NULL){
56 return TEST_RESULT_FAIL;
57 }
58
59 address = (uintptr_t)cert->offset_address;
60 exp_len = cert->size;
61 if (exp_len == 0U){
62 return TEST_RESULT_FAIL;
63 }
64
65 /* Runtime-sized buffer on stack */
66 uint8_t cert_buffer[exp_len];
67
68 /* Open NVM and Read certicate */
69 plat_get_nvm_handle(&handle);
70 if(handle < 0) {
71 return TEST_RESULT_FAIL;
72 }
73
74 rc = io_seek(handle, IO_SEEK_SET, address);
75 if (rc < 0){
76 return TEST_RESULT_FAIL;
77 }
78
79 rc = io_read(handle, (uintptr_t) &cert_buffer, exp_len, &len);
80 if (rc < 0 || len != exp_len){
81 return TEST_RESULT_FAIL;
82 }
83
84 /* Parse certifacte to retrieve public key */
85 rc = get_pubKey_from_cert(&cert_buffer, len, &paramOut);
86 if ( rc != 0){
87 return TEST_RESULT_FAIL;
88 }
89
90 /*
91 * Corrupt part of the certificate in storage.
92 * Simple overwrite: just clobber the first 32 bytes so parsing/verification fails.
93 */
94 {
95 uint8_t junk[32] = {0};
96
97 rc = io_seek(handle, IO_SEEK_SET, address);
98 if (rc < 0){
99 return TEST_RESULT_FAIL;
100 }
101
102 rc = io_write(handle, (uintptr_t)junk, sizeof(junk), &len);
103 if (rc < 0 || len != sizeof(junk)){
104 return TEST_RESULT_FAIL;
105 }
106 }
107
108 /* Reboot */
109 tftf_notify_reboot();
110 ret = tftf_smc(&args);
111
112 /* The PSCI SYSTEM_RESET call is not supposed to return */
113 tftf_testcase_printf("System didn't reboot properly (%d)\n",
114 (unsigned int)ret.ret0);
115
116 /* If this point is reached, reboot failed to trigger*/
117 return TEST_RESULT_FAIL;
118}