Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 1 | /* |
Nik Dewally | bacae6c | 2024-07-30 16:58:14 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2024, Arm Limited. All rights reserved. |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef PSA_CALL_HPP |
| 9 | #define PSA_CALL_HPP |
| 10 | |
| 11 | #include <string> |
Nik Dewally | bacae6c | 2024-07-30 16:58:14 +0100 | [diff] [blame] | 12 | #include <iosfwd> |
| 13 | #include <vector> |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 14 | |
Nik Dewally | bacae6c | 2024-07-30 16:58:14 +0100 | [diff] [blame] | 15 | #include "data_blocks.hpp" |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 16 | |
Nik Dewally | bacae6c | 2024-07-30 16:58:14 +0100 | [diff] [blame] | 17 | class psa_asset; |
| 18 | enum class psa_asset_usage; |
| 19 | class tf_fuzz_info; |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 20 | |
| 21 | using namespace std; |
| 22 | |
| 23 | class psa_call |
| 24 | { |
| 25 | public: |
Nik Dewally | 6663dde | 2024-08-09 16:12:27 +0100 | [diff] [blame] | 26 | string call_description; // description of the call, just for tracing |
| 27 | expect_info exp_data; // everything about expected results |
| 28 | set_data_info set_data; // everything about setting PSA-asset-data values |
| 29 | asset_name_id_info asset_info; // everything about the asset(s) for this line |
| 30 | key_policy_info policy; // (specific to crypto, but have to put this here) |
| 31 | string asset_2_name; // if there's a 2nd asset, then this is its name |
| 32 | string asset_3_name; // if there's a 3rd asset, then this is its name |
| 33 | psa_asset_usage random_asset; |
| 34 | /* if asked to use some random asset from active or deleted, this says |
| 35 | which. psa_asset_usage::all if not using this feature. */ |
| 36 | bool assign_data_var_specified; // asset data to/from named variable |
| 37 | string assign_data_var; // name of variable to dump (assign) data into |
| 38 | // Expected-result info: |
| 39 | bool print_data; // true to print asset data to test log |
| 40 | bool hash_data; // true to hash data for later comparison |
| 41 | string id_string; // not all PSA calls involve an ID, but a diverse set do |
| 42 | long call_ser_no; // unique serial# for this psa_call (see note in tf_fuzz.hpp) |
| 43 | tf_fuzz_info *test_state; // the big blob with pointers to everything going on |
| 44 | string barrier; |
| 45 | /* "barrier" is used for template-line operations that resolve a series of |
| 46 | PSA calls. In particular, with respect to the fact that TF-Fuzz strives |
| 47 | to randomize these multiple calls where possible, meaning interspersing |
| 48 | them among other, earlier commands. However, for example, calls to set |
| 49 | the aspects of a policy can't be pushed too far back, such as in among |
| 50 | calls setting that same policy for a previous operation! "barrier" is |
| 51 | either "", in which case this call does not care whether you place calls |
| 52 | before it, or it contains the name of an asset that, calls related to |
| 53 | which must be placed *after* this call. */ |
| 54 | string target_barrier; |
| 55 | /* asset to tell the psa_call objects to set and search barrier to when |
| 56 | re-ordering PSA calls. For key policies, this is not necessarily the |
| 57 | nominal asset of that call. For a policy call, it is that policy asset, |
| 58 | so that later re-settings of the same policy don't pollute the current |
| 59 | setting of that policy. However, for key sets and reads, it is not the |
| 60 | key asset, but its policy. */ |
| 61 | |
| 62 | virtual vector<psa_asset*>::iterator resolve_asset (bool create_asset_bool, |
| 63 | psa_asset_usage where) = 0; |
| 64 | |
| 65 | /// Updates asset based on call information. |
| 66 | /// |
| 67 | /// WARNING: previously, this used to be the place to do call simulation |
| 68 | /// logic such as modifiying assets. Code that does simulation or in any |
| 69 | /// way mutates the state should now instead go in simulate(). |
| 70 | virtual bool copy_call_to_asset (void) = 0; |
| 71 | |
| 72 | /// Updates call based on asset information. |
| 73 | virtual bool copy_asset_to_call (void) = 0; |
| 74 | |
| 75 | /// Simulates the effect of the call, returning true if a change has been |
| 76 | /// made. |
| 77 | /// |
| 78 | /// This is called before asset simulatio takes place. For more details on |
| 79 | /// control flow, see simulate_calls(). |
| 80 | /// |
| 81 | /// If no return code for the call was given in the template, this should be |
| 82 | /// updated here. However, if a return code is already present, it should |
| 83 | /// never be overwritten. |
| 84 | virtual bool simulate (void); |
| 85 | |
Nik Dewally | abac0e5 | 2024-08-02 13:42:27 +0100 | [diff] [blame^] | 86 | // Update policy information in the call based on the policy |
| 87 | // asset specified in policy.get_policy_from_policy. If this is unset, |
| 88 | // the existing values are used as-is. |
| 89 | // |
| 90 | // This enables the simulation time setting of the policy. |
| 91 | // |
| 92 | // See `key_policy_info.get_policy_from_policy`. |
| 93 | void copy_policy_to_call(void); |
| 94 | |
Nik Dewally | 6663dde | 2024-08-09 16:12:27 +0100 | [diff] [blame] | 95 | // TODO: move simulation and error modelling code code into simulate(). |
| 96 | // once this is done, remove default impl so that simulate is mandatory for |
| 97 | // calls. |
| 98 | // .. |
| 99 | // In particular, need to move code from: |
| 100 | // .. |
| 101 | // - copy_call_to_asset |
| 102 | // .. |
| 103 | // - fill_in_command |
| 104 | // .. |
| 105 | // - fill_in_result_code |
| 106 | |
| 107 | virtual void fill_in_prep_code (void) = 0; |
| 108 | |
| 109 | /// WARNING: Previously, this used to also contain expected value |
| 110 | /// modelling code (alongside fill_in_command), and some error code |
| 111 | /// modelling may still be left over here. New expected value modelling code |
| 112 | /// should be put in simulate() where possible. Doing this gives a much |
| 113 | /// nicer split between the simulation step (simulate()), and the code |
| 114 | /// generation step (which this method is part of). |
| 115 | virtual void fill_in_command (void) = 0; |
| 116 | |
| 117 | void write_out_prep_code (ofstream &test_file); |
| 118 | void write_out_command (ofstream &test_file); |
| 119 | void write_out_check_code (ofstream &test_file); |
| 120 | psa_call (tf_fuzz_info *test_state, long &asset_ser_no, |
| 121 | asset_search how_asset_found); // (constructor) |
| 122 | ~psa_call (void); |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 123 | |
| 124 | protected: |
Nik Dewally | 6663dde | 2024-08-09 16:12:27 +0100 | [diff] [blame] | 125 | string prep_code; // declarations and such prior to all of the calls |
| 126 | string call_code; // for the call itself |
| 127 | string check_code; // for the code to check success of the call |
| 128 | static long unique_id_counter; // counts off unique IDs for assets |
| 129 | |
| 130 | /// Fill in expected result checks. |
| 131 | /// |
| 132 | /// WARNING: Previously, this used to also contain expected value |
| 133 | /// modelling code (alongside fill_in_command), and some error code |
| 134 | /// modelling may still be left over here. New expected value modelling code |
| 135 | /// should be put in simulate() where possible. Doing this gives a much |
| 136 | /// nicer split between the simulation step (simulate()), and the code |
| 137 | /// generation step (which this method is part of). |
| 138 | virtual void fill_in_result_code (void) = 0; |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 139 | |
| 140 | private: |
| 141 | // Data members: |
| 142 | // Methods: |
| 143 | }; |
| 144 | |
| 145 | |
| 146 | class sst_call : public psa_call |
| 147 | { |
| 148 | public: |
| 149 | // Data members: // (low value in hiding these behind setters and getters) |
| 150 | // Methods: |
| 151 | vector<psa_asset*>::iterator resolve_asset (bool create_asset_bool, |
| 152 | psa_asset_usage where); |
| 153 | sst_call (tf_fuzz_info *test_state, long &asset_ser_no, |
| 154 | asset_search how_asset_found); // (constructor) |
| 155 | ~sst_call (void); |
| 156 | |
| 157 | protected: |
| 158 | // Data members: |
Nik Dewally | 6663dde | 2024-08-09 16:12:27 +0100 | [diff] [blame] | 159 | void fill_in_result_code (void); |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 160 | |
| 161 | private: |
| 162 | // Data members: |
| 163 | // Methods: |
| 164 | }; |
| 165 | |
| 166 | class crypto_call : public psa_call |
| 167 | { |
| 168 | public: |
| 169 | // Data members: // (low value in hiding these behind setters and getters) |
| 170 | // Methods: |
Nik Dewally | abac0e5 | 2024-08-02 13:42:27 +0100 | [diff] [blame^] | 171 | bool copy_asset_to_call (void) override; |
| 172 | virtual bool simulate() override; |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 173 | crypto_call (tf_fuzz_info *test_state, long &asset_ser_no, |
| 174 | asset_search how_asset_found); // (constructor) |
| 175 | ~crypto_call (void); |
| 176 | |
| 177 | protected: |
| 178 | // Data members: |
| 179 | // Methods: |
Nik Dewally | abac0e5 | 2024-08-02 13:42:27 +0100 | [diff] [blame^] | 180 | void fill_in_result_code (void) override; |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 181 | // for now, the method-overide buck stops here, but that'll probably change |
Nik Dewally | abac0e5 | 2024-08-02 13:42:27 +0100 | [diff] [blame^] | 182 | bool simulate_ret_code(void); |
| 183 | |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 184 | |
| 185 | private: |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | class security_call : public psa_call |
| 189 | /* Strictly speaking, these don't really correspond to PSA calls, so it's a little |
| 190 | iffy to subclass them from psa_call. However, the calling patterns work out |
| 191 | right. */ |
| 192 | { |
| 193 | public: |
| 194 | // Data members: // (low value in hiding these behind setters and getters) |
| 195 | // Methods: |
| 196 | vector<psa_asset*>::iterator resolve_asset (bool create_asset_bool, |
| 197 | psa_asset_usage where); |
| 198 | security_call (tf_fuzz_info *test_state, long &asset_ser_no, |
| 199 | asset_search how_asset_found); // (constructor) |
| 200 | ~security_call (void); |
| 201 | |
| 202 | protected: |
| 203 | // Data members: |
| 204 | // Methods: |
Nik Dewally | 6663dde | 2024-08-09 16:12:27 +0100 | [diff] [blame] | 205 | void fill_in_result_code (void); |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 206 | // Should never be invoked, since security calls generate no PSA calls. |
| 207 | |
| 208 | private: |
| 209 | // Data members: |
| 210 | // Methods: |
| 211 | }; |
| 212 | |
| 213 | #endif // PSA_CALL_HPP |