blob: 1e568366d3e4180c8fc11055845c13247397785c [file] [log] [blame]
Karl Zhang3de5ab12021-05-31 11:45:48 +08001/*
Nik Dewally298a8042024-07-23 15:03:13 +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
Nik Dewallybacae6c2024-07-30 16:58:14 +01008#include <stdint.h>
Karl Zhang3de5ab12021-05-31 11:45:48 +08009#include <string>
Nik Dewallybacae6c2024-07-30 16:58:14 +010010#include <vector>
11#include <iosfwd>
12
13class psa_asset;
14
15enum class psa_asset_type;
16class psa_call;
17
18enum class asset_search;
Karl Zhang3de5ab12021-05-31 11:45:48 +080019
20/* These classes "cut down the clutter" by grouping together related data and
21 associated methods (most importantly their constructors) used in template_
22 line, psa_call, psa_asset (etc.). */
23
24#ifndef DATA_BLOCKS_HPP
25#define DATA_BLOCKS_HPP
26
Karl Zhang3de5ab12021-05-31 11:45:48 +080027using namespace std;
28
29
30/**********************************************************************************
31 Class expect_info is all about expected data and expected pass/fail information.
32 The members are therefore broken down with prefixes pf_ (for pass/fail) or
33 data_. Pass/fail, is broadly:
34 * "Pass" == the test passes
35 * "Specified" == some specified failure (e.g., no such asset)
36 * "Nothing" == no expectation
37 Expected data refers to psa-asset data values, generally after reading them.
38 Currently, they are limited to character strings, but that will probably be
39 generalized in the future.
40**********************************************************************************/
41
42class expect_info
43{
44public:
45 // Data members:
46 // Expected-result info:
47 bool pf_nothing; // true to not generate results-check(s)
48 bool pf_pass; // if !expect.pf_nothing, then pass is expected
49 bool pf_fail; // if "expect fail" was specified
Nik Dewallyf0ff5e92024-07-16 10:01:32 +010050 bool pf_specified; // if "expect <ERROR_CODE>" was specified
Karl Zhang3de5ab12021-05-31 11:45:48 +080051 /* if !pf_nothing && !pf_pass, then
52 true == expected result was specified
53 false == tf_fuzz must model expected result, and
54 pf_result_string is the expected result */
55 string pf_result_string;
56 bool data_specified; // (literal expected data specified)
57 string data; // what test template expects data from reading an asset to be
58 int n_exp_vars; // how many check-value variables have been created
59 bool data_var_specified; // check against a variable
60 string data_var; // name of variable containing expected data
61 bool pf_info_incomplete;
62 /* In parsing the template, the expect information comes later than the
63 rest of the call info. This flag tells us to fill in the pass/fail
64 expect info when it comes available. */
65 bool expected_results_saved;
66 /* This indicates whether expected results have or have not already been
67 copied to this call. It's a "one-shot," so to speak, to copy only
68 once when results are known good. Since calls can be inserted into
69 earlier points in the call sequence (not always appended), the call
70 sequence has to be gone over for this process multiple times. */
71 // Methods:
72 expect_info (void); // (default constructor)
73 ~expect_info (void); // (destructor)
74 void set_pf_pass (void);
75 void set_pf_fail (void);
76 void set_pf_nothing (void);
77 void set_pf_error (string error);
78 void copy_expect_to_call (psa_call *the_call);
79
80protected:
81 // Data members:
82 bool data_matches_asset;
83 /* true if template specifies expected data, and that expected data
84 agrees with that in the asset */
85};
86
87
88/**********************************************************************************
89 Class set_data_info addresses PSA-asset data values as affected, directly or
90 indirctly/implicitly, by the template-line content. "Directly," that is, by
91 virtue of the template line stating verbatim what to set data to, or indirectly
92 by virtue of telling TF-Fuzz to create random data for it.
93**********************************************************************************/
94
95class set_data_info
96{
97public:
98 // Data members:
99 bool string_specified;
100 // true if a string of data is specified in template file
101 bool random_data; // true to generate random data for the asset
102 bool file_specified; // true if a file of expected data was specified
103 bool literal_data_not_file;
104 // true to use data strings rather than files as data source
105 int n_set_vars; // how many implicit set variables have been created
106 string file_path; // path to file, if specified
107 string flags_string;
108 // creation flags, nominally for SST but have to be in a vector of base-class
109 uint32_t data_offset; // offset into asset data
110 // Methods:
111 set_data_info (void); // (default constructor)
112 ~set_data_info (void); // (destructor)
113 void set (string set_val);
114 void set_calculated (string set_val);
115 void randomize (void);
116 string get (void);
117 bool set_file (string file_name);
118
119protected:
120 // Data members:
121 string data; // String describing asset data.
122 // Methods:
123 string rand_creation_flags (void);
124};
125
126
127/**********************************************************************************
128 Class asset_name_id_info groups together and acts upon all information related to the
129 human names (as reflected in the code variable names, etc.) for PSA assets.
130**********************************************************************************/
131
132class asset_name_id_info
133{
134public:
135 // Data members (not much value in "hiding" these behind getters)
136 psa_asset *the_asset;
137 psa_asset_type asset_type; // SST vs. key vs. policy (etc.)
138 bool id_n_not_name; // true to create a PSA asset by ID
139 bool name_specified; // true iff template supplied human name
140 bool id_n_specified; // true iff template supplied ID #
141 vector<string> asset_name_vector;
142 vector<int> asset_id_n_vector;
143 long asset_ser_no; // unique ID for psa asset needed to find data string
Mate Toth-Palffba10e2021-09-22 21:38:03 +0200144 /* Note: The original theory is that we can't save away iterators to
145 assets, because STL vectors could get relocated. However,
146 we've switched over to lists, which don't get moved around, so
147 we should be safe now. */
Karl Zhang3de5ab12021-05-31 11:45:48 +0800148 asset_search how_asset_found;
149 uint64_t id_n; // asset ID# (e.g., SST UID).
150 /* Note: This is just a holder to pass ID from template-line to call. The
151 IDs for a given template line are in asset_info.asset_id_n_vector. */
152 // Methods:
153 asset_name_id_info (void); // (default constructor)
154 ~asset_name_id_info (void); // (destructor)
155 void set_name (string set_val);
156 void set_calc_name (string set_val);
157 void set_just_name (string set_val);
158 string get_name (void);
159 void set_id_n (string set_val);
160 void set_id_n (uint64_t set_val);
161 string make_id_n_based_name (uint64_t id_n);
162 // create UID-based asset name
163
164protected:
165 // Data members:
166 string asset_name; // parsed from template, assigned to psa_asset object
167};
168
169
170/**********************************************************************************
171 Class key_policy_info collects together the aspects of a Crypto key attributes
172 ("policies"). These include aspects that can affect TF-Fuzz's test-generation.
173**********************************************************************************/
174
175class key_policy_info
176{
177public:
178 // Data members:
179 // Digested info:
180 bool get_policy_from_key;
181 /* if true, then we must get policy info from a stated key; the asset
182 here is a key that uses the policy, and not the policy itself. */
183 bool implicit_policy;
184 /* if true, then the key was defined with policy specifications, but not
185 a named policy, meaning that we have to create an implicit policy. */
186 bool copy_key; // true to indicate copying one key to another
187 bool exportable; // key data can be exported (viewed - fail exports if not).
188 bool copyable; // can be copied (fail key-copies if not).
189 bool can_encrypt; // OK for encryption (fail other uses).
190 bool can_decrypt; // OK for decryption (fail other uses).
191 bool can_sign; // OK for signing (fail other operations).
Nik Dewallyf0ff5e92024-07-16 10:01:32 +0100192 bool can_verify; // OK for verifying a message signature (fail other uses).
Karl Zhang3de5ab12021-05-31 11:45:48 +0800193 bool derivable; // OK for derive other keys (fail other uses).
194 bool persistent; // must be deleted at the end of test.
195 string usage_string;
196 /* This string is set to a PSA_KEY_USAGE_* value in the template
197 immediately prior to making define_call<add_policy_usage_call>.
198 The copy_template_to_call() therein sets the corresponding string
199 in the call, and that is copied into the code in the fill_in_command()
200 invocation. */
201 string print_usage_true_string;
202 /* For printing out policy usage, this states how to describe the usage
203 if it can be used this way. This is managed similarly with, and used
204 in conjunction with usage_string above. NOTE: THIS ALSO SERVES AS AN
205 INDICATOR WHETHER OR NOT TO PRINT ON A GET-USAGE CALL. "" means not
206 to print. */
207 string print_usage_false_string;
208 /* Also for printing out policy usage, this is how to describe usage if
209 it cannot be used this way. */
210 string key_type; // AES, DES, RSA pair, DS public, etc.
211 string key_algorithm;
212 int n_bits;
213 // for get_key_info call (possibly others) exected key size in bits
214 string handle_str; // the text name of the key's "handle"
215 string key_data; // the key data as best we can know it.
216 string asset_2_name;
217 // if there's a 2nd asset, such as policy on key call, this is its name
218 string asset_3_name; // if there's a 3rd asset, then this is its name
219
220 // Methods:
221 key_policy_info (void); // (default constructor)
222 ~key_policy_info (void); // (destructor)
223
224
225protected:
226 // Data members:
227 bool data_matches_asset;
228 /* true if template specifies expected data, and that expected data
229 agrees with that in the asset */
230};
231
232
233
234#endif // DATA_BLOCKS_HPP