blob: 40218e550e5c26cd5edaf8a6ec0a9aaced6dadc1 [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
128 if (policy_info.get_policy_from_key) {
129 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 );
132 }
133 define_call<get_policy_usage_call> (set_data, random_data,
134 fill_in_template, create_call, temLin, rsrc,
135 add_to_end_bool, yes_set_barrier );
136 define_call<get_policy_lifetime_call> (set_data, random_data,
137 fill_in_template, create_call, temLin, rsrc,
138 add_to_end_bool, dont_set_barrier );
139 define_call<get_policy_algorithm_call> (set_data, random_data,
140 fill_in_template, create_call, temLin, rsrc,
141 add_to_end_bool, dont_set_barrier );
142 define_call<get_policy_type_call> (set_data, random_data,
143 fill_in_template, create_call, temLin, rsrc,
144 add_to_end_bool, dont_set_barrier );
145 define_call<get_policy_size_call> (set_data, random_data,
146 fill_in_template, create_call, temLin, rsrc,
147 add_to_end_bool, dont_set_barrier );
148 if (print_data) {
149 /* Printing data, so we need to read and filter usage for various
150 attributes, one by one: */
151 policy_info.print_usage_true_string.assign ("key can be exported.");
152 policy_info.print_usage_false_string.assign ("key cannot be exported.");
153 policy_info.usage_string.assign ("PSA_KEY_USAGE_EXPORT");
154 define_call<print_policy_usage_call> (set_data, random_data,
155 fill_in_template, create_call, temLin, rsrc,
156 add_to_end, dont_set_barrier );
157 policy_info.print_usage_true_string.assign ("key can be copied.");
158 policy_info.print_usage_false_string.assign ("key cannot be copied.");
159 policy_info.usage_string.assign ("PSA_KEY_USAGE_COPY");
160 define_call<print_policy_usage_call> (set_data, random_data,
161 fill_in_template, create_call, temLin, rsrc,
162 add_to_end, dont_set_barrier );
163 policy_info.print_usage_true_string.assign ("key works for encryption.");
164 policy_info.print_usage_false_string.assign ("key is not for encryption.");
165 policy_info.usage_string.assign ("PSA_KEY_USAGE_ENCRYPT");
166 define_call<print_policy_usage_call> (set_data, random_data,
167 fill_in_template, create_call, temLin, rsrc,
168 add_to_end, dont_set_barrier );
169 policy_info.print_usage_true_string.assign ("key works for decyption.");
170 policy_info.print_usage_false_string.assign ("key is not for decyption.");
171 policy_info.usage_string.assign ("PSA_KEY_USAGE_DECRYPT");
172 define_call<print_policy_usage_call> (set_data, random_data,
173 fill_in_template, create_call, temLin, rsrc,
174 add_to_end, dont_set_barrier );
175 policy_info.print_usage_true_string.assign ("key works for signing.");
176 policy_info.print_usage_false_string.assign ("key is not for signing.");
Nik Dewallyc7985db2024-07-10 17:55:08 +0100177 policy_info.usage_string.assign ("PSA_KEY_USAGE_SIGN_HASH");
Karl Zhang3de5ab12021-05-31 11:45:48 +0800178 define_call<print_policy_usage_call> (set_data, random_data,
179 fill_in_template, create_call, temLin, rsrc,
180 add_to_end, dont_set_barrier );
181 policy_info.print_usage_true_string.assign ("key can be used to verify.");
182 policy_info.print_usage_false_string.assign ("key not for verify.");
Nik Dewallyc7985db2024-07-10 17:55:08 +0100183 policy_info.usage_string.assign ("PSA_KEY_USAGE_VERIFY_HASH");
Karl Zhang3de5ab12021-05-31 11:45:48 +0800184 define_call<print_policy_usage_call> (set_data, random_data,
185 fill_in_template, create_call, temLin, rsrc,
186 add_to_end, dont_set_barrier );
187 policy_info.print_usage_true_string.assign ("key can derive other keys.");
188 policy_info.print_usage_false_string.assign ("key cannot derive other keys.");
189 policy_info.usage_string.assign ("PSA_KEY_USAGE_DERIVE");
190 define_call<print_policy_usage_call> (set_data, random_data,
191 fill_in_template, create_call, temLin, rsrc,
192 add_to_end, yes_set_barrier );
193 }
194 }
195 read_policy_template_line (tf_fuzz_info *resources); // (constructor)
196 ~read_policy_template_line (void);
197
198protected:
199 // Data members:
200 // Methods:
201
202private:
203 // Data members:
204 // Methods:
205};
206
207
208class set_key_template_line : public key_template_line
209{
210public:
211 // Data members:
212 // Methods:
213 void setup_call (set_data_info set_info, bool random_data,
214 bool fill_in_template, bool create_call,
215 template_line *temLin, tf_fuzz_info *rsrc) {
216 if (policy_info.copy_key) {
217 // Copying a key:
218 define_call<copy_key_call> (set_info, random_data,
219 fill_in_template, create_call, temLin, rsrc,
220 add_to_end, yes_set_barrier);
221 } else if (set_data.string_specified || set_data.random_data) {
222 // Key data (key material) supplied:
223 define_call<create_key_call> (set_info, random_data,
224 fill_in_template, create_call, temLin, rsrc,
225 add_to_end, yes_set_barrier);
226 } else {
227 // Generate from scratch:
228 define_call<generate_key_call> (set_info, random_data,
229 fill_in_template, create_call, temLin, rsrc,
230 add_to_end, yes_set_barrier);
231 }
232 }
233 set_key_template_line (tf_fuzz_info *resources); // (constructor)
234 ~set_key_template_line (void);
235
236protected:
237 // Data members:
238 // Methods:
239
240private:
241 // Data members:
242 // Methods:
243};
244
245class read_key_template_line : public key_template_line
246{
247public:
248 // Data members:
249 // Methods:
250 void setup_call (set_data_info set_info, bool random_data,
251 bool fill_in_template, bool create_call,
252 template_line *temLin, tf_fuzz_info *rsrc) {
253 define_call<read_key_data_call> (set_data, random_data,
254 fill_in_template, create_call, temLin, rsrc,
255 add_to_end, yes_set_barrier);
256 }
257 read_key_template_line (tf_fuzz_info *resources); // (constructor)
258 ~read_key_template_line (void);
259
260protected:
261 // Data members:
262 // Methods:
263
264private:
265 // Data members:
266 // Methods:
267};
268
269class remove_key_template_line : public key_template_line
270{
271public:
272 // Data members:
273 // Methods:
274 void setup_call (set_data_info set_info, bool random_data,
275 bool fill_in_template, bool create_call,
276 template_line *temLin, tf_fuzz_info *rsrc) {
277 define_call<remove_key_call> (set_data, random_data,
278 fill_in_template, create_call, temLin, rsrc,
279 add_to_end, yes_set_barrier);
280 }
281 remove_key_template_line (tf_fuzz_info *resources); // (constructor)
282 ~remove_key_template_line (void);
283
284protected:
285 // Data members:
286 // Methods:
287
288private:
289 // Data members:
290 // Methods:
291};
292
293#endif // #ifndef CRYPTO_TEMPLATE_LINE_HPP