blob: 86e866ace1e36d93a38da3db906ca1647d2016d1 [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 CRYPTO_TEMPLATE_LINE_HPP
9#define CRYPTO_TEMPLATE_LINE_HPP
10
11#include <cstdint>
Nik Dewallybacae6c2024-07-30 16:58:14 +010012#include <string>
13#include <iosfwd>
Karl Zhang3de5ab12021-05-31 11:45:48 +080014
Nik Dewallybacae6c2024-07-30 16:58:14 +010015#include "crypto_call.hpp"
16#include "data_blocks.hpp"
17#include "find_or_create_asset.hpp"
Karl Zhang3de5ab12021-05-31 11:45:48 +080018#include "template_line.hpp"
Nik Dewallybacae6c2024-07-30 16:58:14 +010019#include "tf_fuzz.hpp"
Karl Zhang3de5ab12021-05-31 11:45:48 +080020
Nik Dewallybacae6c2024-07-30 16:58:14 +010021using namespace std;
Karl Zhang3de5ab12021-05-31 11:45:48 +080022
23class set_policy_template_line : public policy_template_line
24{
25public:
26 // Data members:
27 // Methods:
28 void setup_call (set_data_info set_info, bool random_data,
29 bool fill_in_template, bool create_call,
30 template_line *temLin, tf_fuzz_info *rsrc) {
31 /* If the name of the primary asset is known, then add calls at
32 random, otherwise append to end. If not, then we do not know
33 what asset-name barrier to search for. */
34 bool add_to_end_bool = (random_asset != psa_asset_usage::all);
35
36 // Add the calls of interest:
37 define_call<init_policy_call> (set_data, random_data,
38 fill_in_template, create_call, temLin, rsrc,
39 add_random_after_barrier, yes_set_barrier );
40 define_call<reset_policy_call> (set_data, random_data,
41 fill_in_template, create_call, temLin, rsrc,
42 add_random_after_barrier, yes_set_barrier );
43 policy_info.usage_string.assign ("0"); // clear out all usages
44 define_call<set_policy_usage_call> (set_data, random_data,
45 fill_in_template, create_call, temLin, rsrc,
46 add_random_after_barrier, yes_set_barrier );
47 if (policy_info.exportable) {
48 policy_info.usage_string.assign ("PSA_KEY_USAGE_EXPORT");
49 define_call<add_policy_usage_call> (set_data, random_data,
50 fill_in_template, create_call, temLin, rsrc,
51 add_to_end_bool, dont_set_barrier );
52 }
53 if (policy_info.copyable) {
54 policy_info.usage_string.assign ("PSA_KEY_USAGE_COPY");
55 define_call<add_policy_usage_call> (set_data, random_data,
56 fill_in_template, create_call, temLin, rsrc,
57 add_to_end_bool, dont_set_barrier );
58 }
59 if (policy_info.can_encrypt) {
60 policy_info.usage_string.assign ("PSA_KEY_USAGE_ENCRYPT");
61 define_call<add_policy_usage_call> (set_data, random_data,
62 fill_in_template, create_call, temLin, rsrc,
63 add_to_end_bool, dont_set_barrier );
64 }
65 if (policy_info.can_decrypt) {
66 policy_info.usage_string.assign ("PSA_KEY_USAGE_DECRYPT");
67 define_call<add_policy_usage_call> (set_data, random_data,
68 fill_in_template, create_call, temLin, rsrc,
69 add_to_end_bool, dont_set_barrier );
70 }
71 if (policy_info.can_sign) {
Nik Dewallyc7985db2024-07-10 17:55:08 +010072 policy_info.usage_string.assign ("PSA_KEY_USAGE_SIGN_HASH");
Karl Zhang3de5ab12021-05-31 11:45:48 +080073 define_call<add_policy_usage_call> (set_data, random_data,
74 fill_in_template, create_call, temLin, rsrc,
75 add_to_end_bool, dont_set_barrier );
76 }
77 if (policy_info.can_verify) {
Nik Dewallyc7985db2024-07-10 17:55:08 +010078 policy_info.usage_string.assign ("PSA_KEY_USAGE_VERIFY_HASH");
Karl Zhang3de5ab12021-05-31 11:45:48 +080079 define_call<add_policy_usage_call> (set_data, random_data,
80 fill_in_template, create_call, temLin, rsrc,
81 add_to_end_bool, dont_set_barrier );
82 }
83 if (policy_info.derivable) {
84 policy_info.usage_string.assign ("PSA_KEY_USAGE_DERIVE");
85 define_call<add_policy_usage_call> (set_data, random_data,
86 fill_in_template, create_call, temLin, rsrc,
87 add_to_end_bool, dont_set_barrier );
88 }
89 define_call<set_policy_lifetime_call> (set_data, random_data,
90 fill_in_template, create_call, temLin, rsrc,
91 add_to_end_bool, dont_set_barrier );
92 define_call<set_policy_algorithm_call> (set_data, random_data,
93 fill_in_template, create_call, temLin, rsrc,
94 add_to_end_bool, dont_set_barrier );
95 define_call<set_policy_type_call> (set_data, random_data,
96 fill_in_template, create_call, temLin, rsrc,
97 add_to_end, yes_set_barrier );
98 define_call<set_policy_size_call> (set_data, random_data,
99 fill_in_template, create_call, temLin, rsrc,
100 add_to_end, yes_set_barrier );
101 }
102 set_policy_template_line (tf_fuzz_info *resources); // (constructor)
103 ~set_policy_template_line (void);
104
105protected:
106 // Data members:
107 // Methods:
108
109private:
110 // Data members:
111 // Methods:
112};
113
114
115class read_policy_template_line : public policy_template_line
116{
117public:
118 // Data members:
119 // Methods:
120 void setup_call (set_data_info set_info, bool random_data,
121 bool fill_in_template, bool create_call,
122 template_line *temLin, tf_fuzz_info *rsrc) {
123 /* If the name of the primary asset is known, then add calls at
124 random, otherwise append to end. If not, then we do not know
125 what asset-name barrier to search for. */
126 bool add_to_end_bool = (random_asset != psa_asset_usage::all);
127
Nik Dewallyed341b72024-08-20 17:02:30 +0100128 if (policy_info.generate_get_policy_from_key_call) {
Karl Zhang3de5ab12021-05-31 11:45:48 +0800129 define_call<get_key_policy_call> (set_data, random_data,
130 fill_in_template, create_call, temLin, rsrc,
131 add_to_end_bool, yes_set_barrier );
Nik Dewallyed341b72024-08-20 17:02:30 +0100132
Karl Zhang3de5ab12021-05-31 11:45:48 +0800133 }
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:
Nik Dewallyed341b72024-08-20 17:02:30 +0100224 define_call<import_key_call> (set_info, random_data,
Karl Zhang3de5ab12021-05-31 11:45:48 +0800225 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) {
Nik Dewallyed341b72024-08-20 17:02:30 +0100254
255 temLin->policy_info.get_policy_info_from=temLin->asset_info.get_name();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800256 define_call<read_key_data_call> (set_data, random_data,
257 fill_in_template, create_call, temLin, rsrc,
258 add_to_end, yes_set_barrier);
259 }
260 read_key_template_line (tf_fuzz_info *resources); // (constructor)
261 ~read_key_template_line (void);
262
263protected:
264 // Data members:
265 // Methods:
266
267private:
268 // Data members:
269 // Methods:
270};
271
272class remove_key_template_line : public key_template_line
273{
274public:
275 // Data members:
276 // Methods:
277 void setup_call (set_data_info set_info, bool random_data,
278 bool fill_in_template, bool create_call,
279 template_line *temLin, tf_fuzz_info *rsrc) {
280 define_call<remove_key_call> (set_data, random_data,
281 fill_in_template, create_call, temLin, rsrc,
282 add_to_end, yes_set_barrier);
283 }
284 remove_key_template_line (tf_fuzz_info *resources); // (constructor)
285 ~remove_key_template_line (void);
286
287protected:
288 // Data members:
289 // Methods:
290
291private:
292 // Data members:
293 // Methods:
294};
295
296#endif // #ifndef CRYPTO_TEMPLATE_LINE_HPP