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