blob: 08d7316a2959798c07bf891e46f211a59c05570f [file] [log] [blame]
Karl Zhang3de5ab12021-05-31 11:45:48 +08001/*
Nik Dewallybacae6c2024-07-30 16:58:14 +01002 * Copyright (c) 2019-2024, Arm Limited. All rights reserved.
Karl Zhang3de5ab12021-05-31 11:45:48 +08003 *
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 Dewallybacae6c2024-07-30 16:58:14 +010012#include <iosfwd>
13#include <vector>
Karl Zhang3de5ab12021-05-31 11:45:48 +080014
Nik Dewallybacae6c2024-07-30 16:58:14 +010015#include "data_blocks.hpp"
Karl Zhang3de5ab12021-05-31 11:45:48 +080016
Nik Dewallybacae6c2024-07-30 16:58:14 +010017class psa_asset;
18enum class psa_asset_usage;
19class tf_fuzz_info;
Karl Zhang3de5ab12021-05-31 11:45:48 +080020
21using namespace std;
22
23class psa_call
24{
25public:
Nik Dewally6663dde2024-08-09 16:12:27 +010026 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
86 // TODO: move simulation and error modelling code code into simulate().
87 // once this is done, remove default impl so that simulate is mandatory for
88 // calls.
89 // ..
90 // In particular, need to move code from:
91 // ..
92 // - copy_call_to_asset
93 // ..
94 // - fill_in_command
95 // ..
96 // - fill_in_result_code
97
98 virtual void fill_in_prep_code (void) = 0;
99
100 /// WARNING: Previously, this used to also contain expected value
101 /// modelling code (alongside fill_in_command), and some error code
102 /// modelling may still be left over here. New expected value modelling code
103 /// should be put in simulate() where possible. Doing this gives a much
104 /// nicer split between the simulation step (simulate()), and the code
105 /// generation step (which this method is part of).
106 virtual void fill_in_command (void) = 0;
107
108 void write_out_prep_code (ofstream &test_file);
109 void write_out_command (ofstream &test_file);
110 void write_out_check_code (ofstream &test_file);
111 psa_call (tf_fuzz_info *test_state, long &asset_ser_no,
112 asset_search how_asset_found); // (constructor)
113 ~psa_call (void);
Karl Zhang3de5ab12021-05-31 11:45:48 +0800114
115protected:
Nik Dewally6663dde2024-08-09 16:12:27 +0100116 string prep_code; // declarations and such prior to all of the calls
117 string call_code; // for the call itself
118 string check_code; // for the code to check success of the call
119 static long unique_id_counter; // counts off unique IDs for assets
120
121 /// Fill in expected result checks.
122 ///
123 /// WARNING: Previously, this used to also contain expected value
124 /// modelling code (alongside fill_in_command), and some error code
125 /// modelling may still be left over here. New expected value modelling code
126 /// should be put in simulate() where possible. Doing this gives a much
127 /// nicer split between the simulation step (simulate()), and the code
128 /// generation step (which this method is part of).
129 virtual void fill_in_result_code (void) = 0;
Karl Zhang3de5ab12021-05-31 11:45:48 +0800130
131private:
132 // Data members:
133 // Methods:
134};
135
136
137class sst_call : public psa_call
138{
139public:
140 // Data members: // (low value in hiding these behind setters and getters)
141 // Methods:
142 vector<psa_asset*>::iterator resolve_asset (bool create_asset_bool,
143 psa_asset_usage where);
144 sst_call (tf_fuzz_info *test_state, long &asset_ser_no,
145 asset_search how_asset_found); // (constructor)
146 ~sst_call (void);
147
148protected:
149 // Data members:
Nik Dewally6663dde2024-08-09 16:12:27 +0100150 void fill_in_result_code (void);
Karl Zhang3de5ab12021-05-31 11:45:48 +0800151
152private:
153 // Data members:
154 // Methods:
155};
156
157class crypto_call : public psa_call
158{
159public:
160 // Data members: // (low value in hiding these behind setters and getters)
161 // Methods:
162 bool copy_asset_to_call (void);
163 crypto_call (tf_fuzz_info *test_state, long &asset_ser_no,
164 asset_search how_asset_found); // (constructor)
165 ~crypto_call (void);
166
167protected:
168 // Data members:
169 // Methods:
Nik Dewally6663dde2024-08-09 16:12:27 +0100170 void fill_in_result_code (void);
Karl Zhang3de5ab12021-05-31 11:45:48 +0800171 // for now, the method-overide buck stops here, but that'll probably change
172
173private:
174 // Data members:
175 // Methods:
176};
177
178class security_call : public psa_call
179 /* Strictly speaking, these don't really correspond to PSA calls, so it's a little
180 iffy to subclass them from psa_call. However, the calling patterns work out
181 right. */
182{
183public:
184 // Data members: // (low value in hiding these behind setters and getters)
185 // Methods:
186 vector<psa_asset*>::iterator resolve_asset (bool create_asset_bool,
187 psa_asset_usage where);
188 security_call (tf_fuzz_info *test_state, long &asset_ser_no,
189 asset_search how_asset_found); // (constructor)
190 ~security_call (void);
191
192protected:
193 // Data members:
194 // Methods:
Nik Dewally6663dde2024-08-09 16:12:27 +0100195 void fill_in_result_code (void);
Karl Zhang3de5ab12021-05-31 11:45:48 +0800196 // Should never be invoked, since security calls generate no PSA calls.
197
198private:
199 // Data members:
200 // Methods:
201};
202
203#endif // PSA_CALL_HPP