blob: 75be0fe0580f4f3d6ee8517b1d60aa647cce87a4 [file] [log] [blame]
Karl Zhang3de5ab12021-05-31 11:45:48 +08001/*
2 * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef CRYPTO_TEMPLATE_LINE_HPP
9#define CRYPTO_TEMPLATE_LINE_HPP
10
11#include <cstdint>
12
13/* This project's header files #including other project headers quickly becomes
14 unrealistically complicated. The only solution is for each .cpp to include
15 the headers it needs.
16#include "psa_asset.hpp"
17//class psa_asset; // just need a forward reference
18#include "template_line.hpp"
19#include "psa_call.hpp"
20*/
21using namespace std;
22
23
24class set_policy_template_line : public policy_template_line
25{
26public:
27 // Data members:
28 // Methods:
29 void setup_call (set_data_info set_info, bool random_data,
30 bool fill_in_template, bool create_call,
31 template_line *temLin, tf_fuzz_info *rsrc) {
32 /* If the name of the primary asset is known, then add calls at
33 random, otherwise append to end. If not, then we do not know
34 what asset-name barrier to search for. */
35 bool add_to_end_bool = (random_asset != psa_asset_usage::all);
36
37 // Add the calls of interest:
38 define_call<init_policy_call> (set_data, random_data,
39 fill_in_template, create_call, temLin, rsrc,
40 add_random_after_barrier, yes_set_barrier );
41 define_call<reset_policy_call> (set_data, random_data,
42 fill_in_template, create_call, temLin, rsrc,
43 add_random_after_barrier, yes_set_barrier );
44 policy_info.usage_string.assign ("0"); // clear out all usages
45 define_call<set_policy_usage_call> (set_data, random_data,
46 fill_in_template, create_call, temLin, rsrc,
47 add_random_after_barrier, yes_set_barrier );
48 if (policy_info.exportable) {
49 policy_info.usage_string.assign ("PSA_KEY_USAGE_EXPORT");
50 define_call<add_policy_usage_call> (set_data, random_data,
51 fill_in_template, create_call, temLin, rsrc,
52 add_to_end_bool, dont_set_barrier );
53 }
54 if (policy_info.copyable) {
55 policy_info.usage_string.assign ("PSA_KEY_USAGE_COPY");
56 define_call<add_policy_usage_call> (set_data, random_data,
57 fill_in_template, create_call, temLin, rsrc,
58 add_to_end_bool, dont_set_barrier );
59 }
60 if (policy_info.can_encrypt) {
61 policy_info.usage_string.assign ("PSA_KEY_USAGE_ENCRYPT");
62 define_call<add_policy_usage_call> (set_data, random_data,
63 fill_in_template, create_call, temLin, rsrc,
64 add_to_end_bool, dont_set_barrier );
65 }
66 if (policy_info.can_decrypt) {
67 policy_info.usage_string.assign ("PSA_KEY_USAGE_DECRYPT");
68 define_call<add_policy_usage_call> (set_data, random_data,
69 fill_in_template, create_call, temLin, rsrc,
70 add_to_end_bool, dont_set_barrier );
71 }
72 if (policy_info.can_sign) {
Nik Dewallyc7985db2024-07-10 17:55:08 +010073 policy_info.usage_string.assign ("PSA_KEY_USAGE_SIGN_HASH");
Karl Zhang3de5ab12021-05-31 11:45:48 +080074 define_call<add_policy_usage_call> (set_data, random_data,
75 fill_in_template, create_call, temLin, rsrc,
76 add_to_end_bool, dont_set_barrier );
77 }
78 if (policy_info.can_verify) {
Nik Dewallyc7985db2024-07-10 17:55:08 +010079 policy_info.usage_string.assign ("PSA_KEY_USAGE_VERIFY_HASH");
Karl Zhang3de5ab12021-05-31 11:45:48 +080080 define_call<add_policy_usage_call> (set_data, random_data,
81 fill_in_template, create_call, temLin, rsrc,
82 add_to_end_bool, dont_set_barrier );
83 }
84 if (policy_info.derivable) {
85 policy_info.usage_string.assign ("PSA_KEY_USAGE_DERIVE");
86 define_call<add_policy_usage_call> (set_data, random_data,
87 fill_in_template, create_call, temLin, rsrc,
88 add_to_end_bool, dont_set_barrier );
89 }
90 define_call<set_policy_lifetime_call> (set_data, random_data,
91 fill_in_template, create_call, temLin, rsrc,
92 add_to_end_bool, dont_set_barrier );
93 define_call<set_policy_algorithm_call> (set_data, random_data,
94 fill_in_template, create_call, temLin, rsrc,
95 add_to_end_bool, dont_set_barrier );
96 define_call<set_policy_type_call> (set_data, random_data,
97 fill_in_template, create_call, temLin, rsrc,
98 add_to_end, yes_set_barrier );
99 define_call<set_policy_size_call> (set_data, random_data,
100 fill_in_template, create_call, temLin, rsrc,
101 add_to_end, yes_set_barrier );
102 }
103 set_policy_template_line (tf_fuzz_info *resources); // (constructor)
104 ~set_policy_template_line (void);
105
106protected:
107 // Data members:
108 // Methods:
109
110private:
111 // Data members:
112 // Methods:
113};
114
115
116class read_policy_template_line : public policy_template_line
117{
118public:
119 // Data members:
120 // Methods:
121 void setup_call (set_data_info set_info, bool random_data,
122 bool fill_in_template, bool create_call,
123 template_line *temLin, tf_fuzz_info *rsrc) {
124 /* If the name of the primary asset is known, then add calls at
125 random, otherwise append to end. If not, then we do not know
126 what asset-name barrier to search for. */
127 bool add_to_end_bool = (random_asset != psa_asset_usage::all);
128
129 if (policy_info.get_policy_from_key) {
130 define_call<get_key_policy_call> (set_data, random_data,
131 fill_in_template, create_call, temLin, rsrc,
132 add_to_end_bool, yes_set_barrier );
133 }
134 define_call<get_policy_usage_call> (set_data, random_data,
135 fill_in_template, create_call, temLin, rsrc,
136 add_to_end_bool, yes_set_barrier );
137 define_call<get_policy_lifetime_call> (set_data, random_data,
138 fill_in_template, create_call, temLin, rsrc,
139 add_to_end_bool, dont_set_barrier );
140 define_call<get_policy_algorithm_call> (set_data, random_data,
141 fill_in_template, create_call, temLin, rsrc,
142 add_to_end_bool, dont_set_barrier );
143 define_call<get_policy_type_call> (set_data, random_data,
144 fill_in_template, create_call, temLin, rsrc,
145 add_to_end_bool, dont_set_barrier );
146 define_call<get_policy_size_call> (set_data, random_data,
147 fill_in_template, create_call, temLin, rsrc,
148 add_to_end_bool, dont_set_barrier );
149 if (print_data) {
150 /* Printing data, so we need to read and filter usage for various
151 attributes, one by one: */
152 policy_info.print_usage_true_string.assign ("key can be exported.");
153 policy_info.print_usage_false_string.assign ("key cannot be exported.");
154 policy_info.usage_string.assign ("PSA_KEY_USAGE_EXPORT");
155 define_call<print_policy_usage_call> (set_data, random_data,
156 fill_in_template, create_call, temLin, rsrc,
157 add_to_end, dont_set_barrier );
158 policy_info.print_usage_true_string.assign ("key can be copied.");
159 policy_info.print_usage_false_string.assign ("key cannot be copied.");
160 policy_info.usage_string.assign ("PSA_KEY_USAGE_COPY");
161 define_call<print_policy_usage_call> (set_data, random_data,
162 fill_in_template, create_call, temLin, rsrc,
163 add_to_end, dont_set_barrier );
164 policy_info.print_usage_true_string.assign ("key works for encryption.");
165 policy_info.print_usage_false_string.assign ("key is not for encryption.");
166 policy_info.usage_string.assign ("PSA_KEY_USAGE_ENCRYPT");
167 define_call<print_policy_usage_call> (set_data, random_data,
168 fill_in_template, create_call, temLin, rsrc,
169 add_to_end, dont_set_barrier );
170 policy_info.print_usage_true_string.assign ("key works for decyption.");
171 policy_info.print_usage_false_string.assign ("key is not for decyption.");
172 policy_info.usage_string.assign ("PSA_KEY_USAGE_DECRYPT");
173 define_call<print_policy_usage_call> (set_data, random_data,
174 fill_in_template, create_call, temLin, rsrc,
175 add_to_end, dont_set_barrier );
176 policy_info.print_usage_true_string.assign ("key works for signing.");
177 policy_info.print_usage_false_string.assign ("key is not for signing.");
Nik Dewallyc7985db2024-07-10 17:55:08 +0100178 policy_info.usage_string.assign ("PSA_KEY_USAGE_SIGN_HASH");
Karl Zhang3de5ab12021-05-31 11:45:48 +0800179 define_call<print_policy_usage_call> (set_data, random_data,
180 fill_in_template, create_call, temLin, rsrc,
181 add_to_end, dont_set_barrier );
182 policy_info.print_usage_true_string.assign ("key can be used to verify.");
183 policy_info.print_usage_false_string.assign ("key not for verify.");
Nik Dewallyc7985db2024-07-10 17:55:08 +0100184 policy_info.usage_string.assign ("PSA_KEY_USAGE_VERIFY_HASH");
Karl Zhang3de5ab12021-05-31 11:45:48 +0800185 define_call<print_policy_usage_call> (set_data, random_data,
186 fill_in_template, create_call, temLin, rsrc,
187 add_to_end, dont_set_barrier );
188 policy_info.print_usage_true_string.assign ("key can derive other keys.");
189 policy_info.print_usage_false_string.assign ("key cannot derive other keys.");
190 policy_info.usage_string.assign ("PSA_KEY_USAGE_DERIVE");
191 define_call<print_policy_usage_call> (set_data, random_data,
192 fill_in_template, create_call, temLin, rsrc,
193 add_to_end, yes_set_barrier );
194 }
195 }
196 read_policy_template_line (tf_fuzz_info *resources); // (constructor)
197 ~read_policy_template_line (void);
198
199protected:
200 // Data members:
201 // Methods:
202
203private:
204 // Data members:
205 // Methods:
206};
207
208
209class set_key_template_line : public key_template_line
210{
211public:
212 // Data members:
213 // Methods:
214 void setup_call (set_data_info set_info, bool random_data,
215 bool fill_in_template, bool create_call,
216 template_line *temLin, tf_fuzz_info *rsrc) {
217 if (policy_info.copy_key) {
218 // Copying a key:
219 define_call<copy_key_call> (set_info, random_data,
220 fill_in_template, create_call, temLin, rsrc,
221 add_to_end, yes_set_barrier);
222 } else if (set_data.string_specified || set_data.random_data) {
223 // Key data (key material) supplied:
224 define_call<create_key_call> (set_info, random_data,
225 fill_in_template, create_call, temLin, rsrc,
226 add_to_end, yes_set_barrier);
227 } else {
228 // Generate from scratch:
229 define_call<generate_key_call> (set_info, random_data,
230 fill_in_template, create_call, temLin, rsrc,
231 add_to_end, yes_set_barrier);
232 }
233 }
234 set_key_template_line (tf_fuzz_info *resources); // (constructor)
235 ~set_key_template_line (void);
236
237protected:
238 // Data members:
239 // Methods:
240
241private:
242 // Data members:
243 // Methods:
244};
245
246class read_key_template_line : public key_template_line
247{
248public:
249 // Data members:
250 // Methods:
251 void setup_call (set_data_info set_info, bool random_data,
252 bool fill_in_template, bool create_call,
253 template_line *temLin, tf_fuzz_info *rsrc) {
254 define_call<read_key_data_call> (set_data, random_data,
255 fill_in_template, create_call, temLin, rsrc,
256 add_to_end, yes_set_barrier);
257 }
258 read_key_template_line (tf_fuzz_info *resources); // (constructor)
259 ~read_key_template_line (void);
260
261protected:
262 // Data members:
263 // Methods:
264
265private:
266 // Data members:
267 // Methods:
268};
269
270class remove_key_template_line : public key_template_line
271{
272public:
273 // Data members:
274 // Methods:
275 void setup_call (set_data_info set_info, bool random_data,
276 bool fill_in_template, bool create_call,
277 template_line *temLin, tf_fuzz_info *rsrc) {
278 define_call<remove_key_call> (set_data, random_data,
279 fill_in_template, create_call, temLin, rsrc,
280 add_to_end, yes_set_barrier);
281 }
282 remove_key_template_line (tf_fuzz_info *resources); // (constructor)
283 ~remove_key_template_line (void);
284
285protected:
286 // Data members:
287 // Methods:
288
289private:
290 // Data members:
291 // Methods:
292};
293
294#endif // #ifndef CRYPTO_TEMPLATE_LINE_HPP