blob: c9d000d0f201ba8a7b2176b07277428e175a5ef4 [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
Nik Dewallybacae6c2024-07-30 16:58:14 +01008#include <stdint.h>
Karl Zhang3de5ab12021-05-31 11:45:48 +08009#include <cstdlib>
Nik Dewallybacae6c2024-07-30 16:58:14 +010010#include <iostream>
Karl Zhang3de5ab12021-05-31 11:45:48 +080011
12#include "boilerplate.hpp"
13#include "variables.hpp"
14#include "gibberish.hpp"
Karl Zhang3de5ab12021-05-31 11:45:48 +080015#include "string_ops.hpp"
16#include "data_blocks.hpp"
17#include "psa_asset.hpp"
18#include "find_or_create_asset.hpp"
Karl Zhang3de5ab12021-05-31 11:45:48 +080019#include "tf_fuzz.hpp"
20#include "crypto_asset.hpp"
21#include "psa_call.hpp"
22#include "crypto_call.hpp"
Nik Dewallyabac0e52024-08-02 13:42:27 +010023#include "crypto_model.hpp"
Karl Zhang3de5ab12021-05-31 11:45:48 +080024
Nik Dewallyabac0e52024-08-02 13:42:27 +010025using namespace crypto_model;
Karl Zhang3de5ab12021-05-31 11:45:48 +080026
27/**********************************************************************************
28 Methods of class policy_call follow:
29**********************************************************************************/
30
31/* Most of the policy classes, in their fill_in_prep_code() method, need to ensure
32 that, at a minimum, the policy variable (psa_key_attributes_t) exists, so, just
33 to cut down code duplication: */
34void policy_call::policy_fill_in_prep_code (void)
35{
36 vector<variable_info>::iterator policy_variable;
37
38 policy_variable = test_state->find_var (asset_info.get_name());
39 if (policy_variable == test_state->variable.end()) {
40 // No such variable exists, so:
41 test_state->make_var (asset_info.get_name());
42 policy_variable = test_state->find_var (asset_info.get_name());
43 prep_code.assign (test_state->bplate->bplate_string[declare_policy]);
44 find_replace_1st ("$var", asset_info.get_name(), prep_code);
45 }
46}
47
48
49policy_call::policy_call (tf_fuzz_info *test_state, // (constructor)
50 long &call_ser_no,
51 asset_search how_asset_found)
52 : crypto_call(test_state, call_ser_no, how_asset_found)
53{
54 // Note: Key attributes are set in the key_policy_info constructor.
55}
56policy_call::~policy_call (void)
57{
58 // Nothing further to delete.
59 return; // just to have something to pin a breakpoint onto
60}
61
62vector<psa_asset*>::iterator policy_call::resolve_asset (bool create_asset_bool,
63 psa_asset_usage where) {
64 vector<psa_asset*>::iterator found_asset;
65 vector<psa_asset*> *asset_vector;
66 int asset_pick;
67
68 if (random_asset != psa_asset_usage::all) {
69 // != psa_asset_usage::all means to choose some known asset at random:
70 if (random_asset == psa_asset_usage::active) {
71 asset_vector = &(test_state->active_policy_asset);
72 asset_info.how_asset_found = asset_search::found_active;
73 } else if (random_asset == psa_asset_usage::deleted) {
74 asset_vector = &(test_state->deleted_policy_asset);
75 asset_info.how_asset_found = asset_search::found_deleted;
76 } else {
77 // "invalid" assets are not currently used.
78 cerr << "\nError: Tool-internal: Please report error 1102 to " << endl
79 << "TF-Fuzz developers."
80 << endl;
81 exit(1102);
82 }
83 if (asset_vector->size() > 0) {
84 /* Pick an active or deleted asset at random: */
85 asset_pick = rand() % asset_vector->size();
86 found_asset = asset_vector->begin() + asset_pick;
87 /* Copy asset information into template tracker: */
88 asset_info.id_n = (*found_asset)->asset_info.id_n;
89 asset_info.asset_ser_no
90 = (*found_asset)->asset_info.asset_ser_no;
91 } else {
92 if (random_asset == psa_asset_usage::active) {
93 cerr << "\nError: A policy call asks for a "
94 << "randomly chosen active asset, when none " << endl
95 << "is currently defined." << endl;
96 exit(1010);
97 } else if (random_asset == psa_asset_usage::deleted) {
98 cerr << "\nError: A policy call asks for a "
99 << "randomly chosen deleted asset, when none " << endl
100 << "is currently defined." << endl;
101 exit(1011);
102 } // "invalid" assets are not currently used.
103 }
104 } else {
105 // Find the asset by name:
106 asset_info.how_asset_found = test_state->find_or_create_policy_asset (
107 psa_asset_search::name, where,
108 asset_info.get_name(), 0, asset_info.asset_ser_no,
109 create_asset_bool, found_asset );
110 if ( asset_info.how_asset_found == asset_search::unsuccessful
111 || asset_info.how_asset_found == asset_search::something_wrong ) {
112 cerr << "\nError: Tool-internal: Please report error 108 to " << endl
113 << "TF-Fuzz developers."
114 << endl;
115 exit(108);
116 }
117 }
118 return found_asset;
119}
120
121/**********************************************************************************
122 End of methods of class policy_call.
123**********************************************************************************/
124
125
126/**********************************************************************************
127 Methods of class key_call follow:
128**********************************************************************************/
129
130key_call::key_call (tf_fuzz_info *test_state, // (constructor)
131 long &call_ser_no,
132 asset_search how_asset_found)
133 : crypto_call(test_state, call_ser_no, how_asset_found)
134{
135 asset_info.the_asset = nullptr;
136}
137key_call::~key_call (void)
138{
139 // Nothing further to delete.
140 return; // just to have something to pin a breakpoint onto
141}
142
143vector<psa_asset*>::iterator key_call::resolve_asset (bool create_asset_bool,
144 psa_asset_usage where) {
145 vector<psa_asset*>::iterator found_asset;
146 vector<psa_asset*> *asset_vector;
147 int asset_pick;
148
149 if (random_asset != psa_asset_usage::all) {
150 // != psa_asset_usage::all means to choose some known asset at random:
151 if (random_asset == psa_asset_usage::active) {
152 asset_vector = &(test_state->active_key_asset);
153 asset_info.how_asset_found = asset_search::found_active;
154 } else if (random_asset == psa_asset_usage::deleted) {
155 asset_vector = &(test_state->deleted_key_asset);
156 asset_info.how_asset_found = asset_search::found_deleted;
157 } else {
158 // "invalid" assets are not currently used.
159 cerr << "\nError: Tool-internal: Please report error 1103 to " << endl
160 << "TF-Fuzz developers."
161 << endl;
162 exit(1103);
163 }
164 if (asset_vector->size() > 0) {
165 /* Pick an active or deleted asset at random: */
166 asset_pick = rand() % asset_vector->size();
167 found_asset = asset_vector->begin() + asset_pick;
168 /* Copy asset information into template tracker: */
169 asset_info.id_n = (*found_asset)->asset_info.id_n;
170 asset_info.asset_ser_no
171 = (*found_asset)->asset_info.asset_ser_no;
172 } else {
173 if (random_asset == psa_asset_usage::active) {
174 cerr << "\nError: A key call asks for a "
175 << "randomly chosen active asset, when none " << endl
176 << "is currently defined." << endl;
177 exit(1012);
178 } else if (random_asset == psa_asset_usage::deleted) {
179 cerr << "\nError: A key call asks for a "
180 << "randomly chosen deleted asset, when none " << endl
181 << "is currently defined." << endl;
182 exit(1013);
183 } // "invalid" assets are not currently used.
184 }
185 } else {
186 // Find the asset by name:
187 asset_info.how_asset_found = test_state->find_or_create_key_asset (
188 psa_asset_search::name, where,
189 asset_info.get_name(), 0, asset_info.asset_ser_no,
190 create_asset_bool, found_asset );
191 if ( asset_info.how_asset_found == asset_search::unsuccessful
192 || asset_info.how_asset_found == asset_search::something_wrong ) {
193 cerr << "\nError: Tool-internal: Please report error 108 to " << endl
194 << "TF-Fuzz developers."
195 << endl;
196 exit(108);
197 }
198 }
199 return found_asset;
200}
201
202/**********************************************************************************
203 End of methods of class key_call.
204**********************************************************************************/
205
206
207/**********************************************************************************
208 Methods of class init_policy_call follow:
209**********************************************************************************/
210
211init_policy_call::init_policy_call (tf_fuzz_info *test_state, // (constructor)
212 long &call_ser_no,
213 asset_search how_asset_found)
214 : policy_call(test_state, call_ser_no,
215 how_asset_found)
216{
217 // Copy the boilerplate text into local buffers:
218 prep_code.assign ("");
219 call_code.assign (test_state->bplate->bplate_string[init_policy]);
220 call_description = "initialize-policy call";
221}
222init_policy_call::~init_policy_call (void)
223{
224 return; // just to have something to pin a breakpoint onto
225}
226
227bool init_policy_call::copy_call_to_asset (void)
228{
229 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
230}
231
232void init_policy_call::fill_in_prep_code (void)
233{
234 policy_fill_in_prep_code();
235}
236
237void init_policy_call::fill_in_command (void)
238{
239 // (call_code already loaded by constructor)
240 find_replace_all ("$policy", asset_info.get_name(), call_code);
241 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100242 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800243}
244
245/**********************************************************************************
246 End of methods of class init_policy_call.
247**********************************************************************************/
248
249
250/**********************************************************************************
251 Methods of class reset_policy_call follow:
252**********************************************************************************/
253
254reset_policy_call::reset_policy_call (tf_fuzz_info *test_state, // (constructor)
255 long &call_ser_no,
256 asset_search how_asset_found)
257 : policy_call(test_state, call_ser_no,
258 how_asset_found)
259{
260 // Copy the boilerplate text into local buffers:
261 prep_code.assign ("");
262 call_code.assign (test_state->bplate->bplate_string[reset_policy]);
263 call_description = "policy reset call";
264}
265reset_policy_call::~reset_policy_call (void)
266{
267 return; // just to have something to pin a breakpoint onto
268}
269
270bool reset_policy_call::copy_call_to_asset (void)
271{
272 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
273}
274
275void reset_policy_call::fill_in_prep_code (void)
276{
277 policy_fill_in_prep_code();
278}
279
280void reset_policy_call::fill_in_command (void)
281{
282 // (call_code already loaded by constructor)
283 find_replace_all ("$policy", asset_info.get_name(), call_code);
284 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100285 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800286}
287
288/**********************************************************************************
289 End of methods of class reset_policy_call.
290**********************************************************************************/
291
292
293/**********************************************************************************
294 Methods of class add_policy_usage_call follow:
295**********************************************************************************/
296
297add_policy_usage_call::add_policy_usage_call (tf_fuzz_info *test_state, // (constructor)
298 long &call_ser_no,
299 asset_search how_asset_found)
300 : policy_call(test_state, call_ser_no,
301 how_asset_found)
302{
303 // Copy the boilerplate text into local buffers:
304 prep_code.assign ("");
305 call_code.assign (test_state->bplate->bplate_string[add_policy_usage]);
306 call_description = "policy add-usage call";
307}
308add_policy_usage_call::~add_policy_usage_call (void)
309{
310 return; // just to have something to pin a breakpoint onto
311}
312
313bool add_policy_usage_call::copy_call_to_asset (void)
314{
315 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
316}
317
318void add_policy_usage_call::fill_in_prep_code (void)
319{
320 policy_fill_in_prep_code();
321 /* TODO: The variable this creates should have been declared already. Should
322 this instead produce an error if it doesn't exist? */
323}
324
325void add_policy_usage_call::fill_in_command (void)
326{
327 // (call_code already loaded by constructor)
328 find_replace_all ("$policy", asset_info.get_name(), call_code);
329 find_replace_1st ("$flag", policy.usage_string, call_code);
330 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100331 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800332}
333
334/**********************************************************************************
335 End of methods of class add_policy_usage_call.
336**********************************************************************************/
337
338
339/**********************************************************************************
340 Methods of class set_policy_lifetime_call follow:
341**********************************************************************************/
342
343set_policy_lifetime_call::set_policy_lifetime_call (tf_fuzz_info *test_state,
344 long &call_ser_no, // (constructor)
345 asset_search how_asset_found)
346 : policy_call(test_state, call_ser_no,
347 how_asset_found)
348{
349 // Copy the boilerplate text into local buffers:
350 prep_code.assign ("");
351 call_code.assign (test_state->bplate->bplate_string[set_policy_lifetime]);
352 call_description = "policy lifetime-set call";
353}
354set_policy_lifetime_call::~set_policy_lifetime_call (void)
355{
356 return; // just to have something to pin a breakpoint onto
357}
358
359bool set_policy_lifetime_call::copy_call_to_asset (void)
360{
361 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
362}
363
364void set_policy_lifetime_call::fill_in_prep_code (void)
365{
366 policy_fill_in_prep_code();
367}
368
369void set_policy_lifetime_call::fill_in_command (void)
370{
371 // (call_code already loaded by constructor)
372 find_replace_all ("$policy", asset_info.get_name(), call_code);
373 find_replace_1st ("$life",
374 policy.persistent? "PSA_KEY_LIFETIME_PERSISTENT"
375 : "PSA_KEY_LIFETIME_VOLATILE",
376 call_code);
377 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100378 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800379}
380
381/**********************************************************************************
382 End of methods of class set_policy_lifetime_call.
383**********************************************************************************/
384
385
386/**********************************************************************************
387 Methods of class set_policy_size_call follow:
388**********************************************************************************/
389
390set_policy_size_call::set_policy_size_call (tf_fuzz_info *test_state, // (constructor)
391 long &call_ser_no,
392 asset_search how_asset_found)
393 : policy_call(test_state, call_ser_no,
394 how_asset_found)
395{
396 // Copy the boilerplate text into local buffers:
397 prep_code.assign ("");
398 call_code.assign (test_state->bplate->bplate_string[set_policy_size]);
399 call_description = "policy size-set call";
400}
401set_policy_size_call::~set_policy_size_call (void)
402{
403 return; // just to have something to pin a breakpoint onto
404}
405
406bool set_policy_size_call::copy_call_to_asset (void)
407{
408 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
409}
410
411void set_policy_size_call::fill_in_prep_code (void)
412{
413 policy_fill_in_prep_code();
414}
415
416void set_policy_size_call::fill_in_command (void)
417{
418 // (call_code already loaded by constructor)
419 find_replace_all ("$policy", asset_info.get_name(), call_code);
420 find_replace_1st ("$size", to_string (policy.n_bits), call_code);
421 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100422 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800423}
424
425/**********************************************************************************
426 End of methods of class set_policy_size_call.
427**********************************************************************************/
428
429
430/**********************************************************************************
431 Methods of class set_policy_type_call follow:
432**********************************************************************************/
433
434set_policy_type_call::set_policy_type_call (tf_fuzz_info *test_state, // (constructor)
435 long &call_ser_no,
436 asset_search how_asset_found)
437 : policy_call(test_state, call_ser_no,
438 how_asset_found)
439{
440 // Copy the boilerplate text into local buffers:
441 prep_code.assign ("");
442 call_code.assign (test_state->bplate->bplate_string[set_policy_type]);
443 call_description = "policy type-set call";
444}
445set_policy_type_call::~set_policy_type_call (void)
446{
447 return; // just to have something to pin a breakpoint onto
448}
449
450bool set_policy_type_call::copy_call_to_asset (void)
451{
452 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
453}
454
455void set_policy_type_call::fill_in_prep_code (void)
456{
457 policy_fill_in_prep_code();
458}
459
460void set_policy_type_call::fill_in_command (void)
461{
462 // (call_code already loaded by constructor)
463 find_replace_all ("$policy", asset_info.get_name(), call_code);
464 find_replace_1st ("$type", policy.key_type, call_code);
465 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100466 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800467}
468
469/**********************************************************************************
470 End of methods of class set_policy_type_call.
471**********************************************************************************/
472
473
474/**********************************************************************************
475 Methods of class set_policy_algorithm_call follow:
476**********************************************************************************/
477
478set_policy_algorithm_call::set_policy_algorithm_call (tf_fuzz_info *test_state,
479 long &call_ser_no, // (constructor)
480 asset_search how_asset_found)
481 : policy_call(test_state, call_ser_no,
482 how_asset_found)
483{
484 // Copy the boilerplate text into local buffers:
485 prep_code.assign ("");
486 call_code.assign (test_state->bplate->bplate_string[set_policy_algorithm]);
487 call_description = "policy algorithm-set call";
488}
489set_policy_algorithm_call::~set_policy_algorithm_call (void)
490{
491 return; // just to have something to pin a breakpoint onto
492}
493
494bool set_policy_algorithm_call::copy_call_to_asset (void)
495{
496 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
497}
498
499void set_policy_algorithm_call::fill_in_prep_code (void)
500{
501 policy_fill_in_prep_code();
502}
503
504void set_policy_algorithm_call::fill_in_command (void)
505{
506 // (call_code already loaded by constructor)
507 find_replace_all ("$policy", asset_info.get_name(), call_code);
508 find_replace_1st ("$algorithm", policy.key_algorithm, call_code);
509 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100510 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800511}
512
513/**********************************************************************************
514 End of methods of class set_policy_algorithm_call.
515**********************************************************************************/
516
517
518/**********************************************************************************
519 Methods of class set_policy_usage_call follow:
520**********************************************************************************/
521
522set_policy_usage_call::set_policy_usage_call (tf_fuzz_info *test_state, // (constructor)
523 long &call_ser_no,
524 asset_search how_asset_found)
525 : policy_call(test_state, call_ser_no,
526 how_asset_found)
527{
528 // Copy the boilerplate text into local buffers:
529 prep_code.assign ("");
530 call_code.assign (test_state->bplate->bplate_string[set_policy_usage]);
531 call_description = "policy usage-set call";
532}
533set_policy_usage_call::~set_policy_usage_call (void)
534{
535 return; // just to have something to pin a breakpoint onto
536}
537
538bool set_policy_usage_call::copy_call_to_asset (void)
539{
540 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
541}
542
543void set_policy_usage_call::fill_in_prep_code (void)
544{
545 policy_fill_in_prep_code();
546}
547
548void set_policy_usage_call::fill_in_command (void)
549{
550 find_replace_1st ("$policy", asset_info.get_name(), call_code);
551 find_replace_1st ("$usage", "0", call_code);
552 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100553 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800554}
555
556/**********************************************************************************
557 End of methods of class set_policy_usage_call.
558**********************************************************************************/
559
560
561/**********************************************************************************
562 Methods of class get_policy_lifetime_call follow:
563**********************************************************************************/
564
565get_policy_lifetime_call::get_policy_lifetime_call (tf_fuzz_info *test_state,
566 long &call_ser_no, // (constructor)
567 asset_search how_asset_found)
568 : policy_call(test_state, call_ser_no,
569 how_asset_found)
570{
571 // Copy the boilerplate text into local buffers:
572 prep_code.assign ("");
573 call_code.assign (test_state->bplate->bplate_string[get_policy_lifetime]);
574 call_description = "policy lifetime-get call";
575}
576get_policy_lifetime_call::~get_policy_lifetime_call (void)
577{
578 return; // just to have something to pin a breakpoint onto
579}
580
581bool get_policy_lifetime_call::copy_call_to_asset (void)
582{
583 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
584}
585
586void get_policy_lifetime_call::fill_in_prep_code (void)
587{
588 string var_name = asset_info.get_name() + "_life";
589 vector<variable_info>::iterator assign_variable;
590
591 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
592 assign_variable = test_state->find_var (var_name);
593 if (assign_variable == test_state->variable.end()) {
594 // No such variable exists, so:
595 test_state->make_var (var_name);
596 prep_code.append (test_state->bplate->bplate_string[declare_policy_lifetime]);
597 find_replace_all ("$var", var_name, prep_code);
598 }
599}
600
601void get_policy_lifetime_call::fill_in_command (void)
602{
603 // (call_code already loaded by constructor)
604 find_replace_all ("$policy", asset_info.get_name(), call_code);
605 find_replace_1st ("$life", asset_info.get_name() + "_life", call_code);
606 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100607 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800608}
609
610/**********************************************************************************
611 End of methods of class get_policy_lifetime_call.
612**********************************************************************************/
613
614
615/**********************************************************************************
616 Methods of class get_policy_size_call follow:
617**********************************************************************************/
618
619get_policy_size_call::get_policy_size_call (tf_fuzz_info *test_state,
620 long &call_ser_no, // (constructor)
621 asset_search how_asset_found)
622 : policy_call(test_state, call_ser_no,
623 how_asset_found)
624{
625 // Copy the boilerplate text into local buffers:
626 prep_code.assign ("");
627 call_code.assign (test_state->bplate->bplate_string[get_policy_size]);
628 call_description = "policy size-get call";
629}
630get_policy_size_call::~get_policy_size_call (void)
631{
632 return; // just to have something to pin a breakpoint onto
633}
634
635bool get_policy_size_call::copy_call_to_asset (void)
636{
637 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
638}
639
640void get_policy_size_call::fill_in_prep_code (void)
641{
642 string var_name = asset_info.get_name() + "_size";
643 vector<variable_info>::iterator assign_variable;
644
645 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
646 assign_variable = test_state->find_var (var_name);
647 if (assign_variable == test_state->variable.end()) {
648 // No such variable exists, so:
649 test_state->make_var (var_name);
650 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
651 find_replace_all ("$var", var_name, prep_code);
652 find_replace_1st ("$init", to_string(exp_data.data.length()), prep_code);
653 }
654}
655
656void get_policy_size_call::fill_in_command (void)
657{
658 // (call_code already loaded by constructor)
659 find_replace_all ("$policy", asset_info.get_name(), call_code);
660 find_replace_1st ("$size", asset_info.get_name() + "_size", call_code);
661 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100662 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800663}
664
665/**********************************************************************************
666 End of methods of class get_policy_size_call.
667**********************************************************************************/
668
669
670/**********************************************************************************
671 Methods of class get_policy_type_call follow:
672**********************************************************************************/
673
674get_policy_type_call::get_policy_type_call (tf_fuzz_info *test_state,
675 long &call_ser_no, // (constructor)
676 asset_search how_asset_found)
677 : policy_call(test_state, call_ser_no,
678 how_asset_found)
679{
680 // Copy the boilerplate text into local buffers:
681 prep_code.assign ("");
682 call_code.assign (test_state->bplate->bplate_string[get_policy_type]);
683 call_description = "policy type-get call";
684}
685get_policy_type_call::~get_policy_type_call (void)
686{
687 return; // just to have something to pin a breakpoint onto
688}
689
690bool get_policy_type_call::copy_call_to_asset (void)
691{
692 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
693}
694
695void get_policy_type_call::fill_in_prep_code (void)
696{
697 string var_name = asset_info.get_name() + "_type";
698 vector<variable_info>::iterator assign_variable;
699
700 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
701 assign_variable = test_state->find_var (var_name);
702 if (assign_variable == test_state->variable.end()) {
703 // No such variable exists, so:
704 test_state->make_var (var_name);
705 prep_code.append (test_state->bplate->bplate_string[declare_policy_type]);
706 find_replace_all ("$var", var_name, prep_code);
707 }
708}
709
710void get_policy_type_call::fill_in_command (void)
711{
712 // (call_code already loaded by constructor)
713 find_replace_all ("$policy", asset_info.get_name(), call_code);
714 find_replace_1st ("$type", asset_info.get_name() + "_type", call_code);
715 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100716 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800717}
718
719/**********************************************************************************
720 End of methods of class get_policy_type_call.
721**********************************************************************************/
722
723
724/**********************************************************************************
725 Methods of class get_policy_algorithm_call follow:
726**********************************************************************************/
727
728get_policy_algorithm_call::get_policy_algorithm_call (tf_fuzz_info *test_state,
729 long &call_ser_no, // (constructor)
730 asset_search how_asset_found)
731 : policy_call(test_state, call_ser_no,
732 how_asset_found)
733{
734 // Copy the boilerplate text into local buffers:
735 prep_code.assign ("");
736 call_code.assign (test_state->bplate->bplate_string[get_policy_algorithm]);
737 call_description = "policy algorithm-get call";
738}
739get_policy_algorithm_call::~get_policy_algorithm_call (void)
740{
741 return; // just to have something to pin a breakpoint onto
742}
743
744bool get_policy_algorithm_call::copy_call_to_asset (void)
745{
746 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
747}
748
749void get_policy_algorithm_call::fill_in_prep_code (void)
750{
751 string var_name = asset_info.get_name() + "_algo";
752 vector<variable_info>::iterator assign_variable;
753
754 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
755 assign_variable = test_state->find_var (var_name);
756 if (assign_variable == test_state->variable.end()) {
757 // No such variable exists, so:
758 test_state->make_var (var_name);
759 prep_code.append (test_state->bplate->bplate_string[declare_policy_algorithm]);
760 find_replace_all ("$var", var_name, prep_code);
761 }
762}
763
764void get_policy_algorithm_call::fill_in_command (void)
765{
766 // (call_code already loaded by constructor)
767 find_replace_all ("$policy", asset_info.get_name(), call_code);
768 find_replace_1st ("$algorithm", asset_info.get_name() + "_algo", call_code);
769 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100770 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800771}
772
773/**********************************************************************************
774 End of methods of class get_policy_algorithm_call.
775**********************************************************************************/
776
777
778/**********************************************************************************
779 Methods of class get_policy_usage_call follow:
780**********************************************************************************/
781
782get_policy_usage_call::get_policy_usage_call (tf_fuzz_info *test_state,
783 long &call_ser_no, // (constructor)
784 asset_search how_asset_found)
785 : policy_call(test_state, call_ser_no,
786 how_asset_found)
787{
788 // Copy the boilerplate text into local buffers:
789 prep_code.assign ("");
790 call_code.assign (test_state->bplate->bplate_string[get_policy_usage]);
791 call_description = "policy usage-get call";
792}
793get_policy_usage_call::~get_policy_usage_call (void)
794{
795 return; // just to have something to pin a breakpoint onto
796}
797
798bool get_policy_usage_call::copy_call_to_asset (void)
799{
800 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
801}
802
803void get_policy_usage_call::fill_in_prep_code (void)
804{
805 string var_name = asset_info.get_name() + "_usage";
806 vector<variable_info>::iterator assign_variable;
807
808 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
809 assign_variable = test_state->find_var (var_name);
810 if (assign_variable == test_state->variable.end()) {
811 // No such variable exists, so:
812 test_state->make_var (var_name);
813 prep_code.append (test_state->bplate->bplate_string[declare_policy_usage]);
814 find_replace_all ("$var", var_name, prep_code);
815 }
816}
817
818void get_policy_usage_call::fill_in_command (void)
819{
820 // (call_code already loaded by constructor)
821 find_replace_all ("$policy", asset_info.get_name(), call_code);
822 find_replace_1st ("$usage", asset_info.get_name() + "_usage", call_code);
823 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100824 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800825}
826
827/**********************************************************************************
828 End of methods of class get_policy_usage_call.
829**********************************************************************************/
830
831
832/**********************************************************************************
833 Methods of class print_policy_usage_call follow:
834**********************************************************************************/
835
836print_policy_usage_call::print_policy_usage_call (tf_fuzz_info *test_state,
837 long &call_ser_no, // (constructor)
838 asset_search how_asset_found)
839 : policy_call(test_state, call_ser_no,
840 how_asset_found)
841{
842 // Copy the boilerplate text into local buffers:
843 prep_code.assign ("");
844 call_code.assign (test_state->bplate->bplate_string[print_policy_usage]);
845 call_description = "policy usage-print call";
846}
847print_policy_usage_call::~print_policy_usage_call (void)
848{
849 return; // just to have something to pin a breakpoint onto
850}
851
852bool print_policy_usage_call::copy_call_to_asset (void)
853{
854 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
855}
856
857void print_policy_usage_call::fill_in_prep_code (void)
858{
859 string var_name = asset_info.get_name() + "_usage";
860 vector<variable_info>::iterator assign_variable;
861
862 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
863 // Make sure policy-usage variable is defined:
864 assign_variable = test_state->find_var (var_name);
865 if (assign_variable == test_state->variable.end()) {
866 // No such variable exists, so:
867 test_state->make_var (var_name);
868 prep_code.append (test_state->bplate->bplate_string[declare_policy_usage]);
869 find_replace_all ("$var", var_name, prep_code);
870 }
871}
872
873void print_policy_usage_call::fill_in_command (void)
874{
875 string var_name = asset_info.get_name() + "_usage";
876
877 // (call_code already loaded by constructor)
878 find_replace_all ("$policy", asset_info.get_name(), call_code);
879 find_replace_1st ("$usage_string", policy.usage_string, call_code);
880 find_replace_1st ("$usage", var_name, call_code);
881 find_replace_1st ("$print_usage_true_string", policy.print_usage_true_string,
882 call_code);
883 find_replace_1st ("$print_usage_false_string", policy.print_usage_false_string,
884 call_code);
885 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100886 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800887}
888
889/**********************************************************************************
890 End of methods of class print_policy_usage_call.
891**********************************************************************************/
892
893
894/**********************************************************************************
895 Methods of class get_key_policy_call follow:
896**********************************************************************************/
897
898get_key_policy_call::get_key_policy_call (tf_fuzz_info *test_state, // (constructor)
899 long &call_ser_no,
900 asset_search how_asset_found)
901 : policy_call(test_state, call_ser_no,
902 how_asset_found)
903{
904 // Copy the boilerplate text into local buffers:
905 prep_code.assign ("");
906 call_code.assign (test_state->bplate->bplate_string[get_policy]);
907 check_code.assign (test_state->bplate->bplate_string[get_policy_check]);
908 call_description = "policy get call";
909}
910get_key_policy_call::~get_key_policy_call (void)
911{
912 return; // just to have something to pin a breakpoint onto
913}
914
915bool get_key_policy_call::copy_call_to_asset (void)
916{
917 return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
918}
919
920void get_key_policy_call::fill_in_prep_code (void)
921{
922 // No prep code required.
923 return; // just to have something to pin a breakpoint onto
924}
925
926void get_key_policy_call::fill_in_command (void)
927{
928 // (call_code already loaded by constructor)
929 find_replace_all ("key", asset_info.get_name(), call_code);
930 find_replace_all ("$policy", policy.asset_2_name, call_code);
931 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100932 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800933}
934
935/**********************************************************************************
936 End of methods of class get_key_policy_call.
937**********************************************************************************/
938
939
940/**********************************************************************************
941 Methods of class generate_key_call follow:
942**********************************************************************************/
943
944generate_key_call::generate_key_call (tf_fuzz_info *test_state, // (constructor)
945 long &call_ser_no,
946 asset_search how_asset_found)
947 : key_call(test_state, call_ser_no,
948 how_asset_found)
949{
950 // Copy the boilerplate text into local buffers:
951 prep_code.assign ("");
952 call_code.assign (test_state->bplate->bplate_string[generate_key]);
953 check_code.assign (test_state->bplate->bplate_string[generate_key_check]);
954 call_description = "key-generate call";
955}
956generate_key_call::~generate_key_call (void)
957{
958 return; // just to have something to pin a breakpoint onto
959}
960
961bool generate_key_call::copy_call_to_asset (void)
962{
Nik Dewallyabac0e52024-08-02 13:42:27 +0100963 // we create the asset conditionally in simulate
964 return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
965}
966
967bool generate_key_call::simulate() {
968 bool is_policy_valid;
969
970 // NOTE: although algorithm and key-type depend on eachother for validity,
971 // this is checked during key operations not generation
972
973
974 if (policy.key_type == "PSA_KEY_TYPE_RSA_PUBLIC_KEY") {
975 is_policy_valid = false;
976 }
977 else if (policy.key_type == "PSA_KEY_TYPE_PEPPER") {
978 is_policy_valid = false;
979 } else {
980 key_type& kt = get_key_type(policy.key_type);
981 algorithm& alg = get_algorithm(policy.key_algorithm);
982 is_policy_valid = kt.is_valid_key_size(policy.n_bits);
983 }
984
985 psa_asset_usage asset_usage;
986
987 if (!is_policy_valid) {
988 // do not create a key when policy is invalid
989 exp_data.expect_failure();
990 return true;
991 }
992
993 // create an asset, and copy the information we want in it to it.
994 copy_call_to_asset_t<key_asset*> (this, yes_create_asset);
995 switch (asset_info.how_asset_found) {
996 case asset_search::created_new:
997 exp_data.expect_pass();
998
999 break;
1000
1001 default: // asset already exists!
1002 exp_data.expect_failure();
1003 break;
1004 }
1005
1006 return true;
Karl Zhang3de5ab12021-05-31 11:45:48 +08001007}
1008
1009void generate_key_call::fill_in_prep_code (void)
1010{
1011 string var_name = asset_info.get_name();
1012 vector<variable_info>::iterator assign_variable;
1013
1014 // Make sure key variable is defined:
1015 assign_variable = test_state->find_var (var_name);
1016 if (assign_variable == test_state->variable.end()) {
1017 // No such variable exists, so:
1018 test_state->make_var (var_name);
1019 prep_code.append (test_state->bplate->bplate_string[declare_key]);
1020 find_replace_all ("$var", var_name, prep_code);
1021 }
1022}
1023
1024void generate_key_call::fill_in_command (void)
1025{
1026 // (call_code already loaded by constructor)
1027 find_replace_all ("$policy", policy.asset_2_name, call_code);
1028 find_replace_all ("$key", asset_info.get_name(), call_code);
1029 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +01001030 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001031}
1032
1033/**********************************************************************************
1034 End of methods of class generate_key_call.
1035**********************************************************************************/
1036
1037
1038/**********************************************************************************
1039 Methods of class create_key_call follow:
1040**********************************************************************************/
1041
1042create_key_call::create_key_call (tf_fuzz_info *test_state, // (constructor)
1043 long &call_ser_no,
1044 asset_search how_asset_found)
1045 : key_call(test_state, call_ser_no,
1046 how_asset_found)
1047{
1048 // Copy the boilerplate text into local buffers:
1049 prep_code.assign ("");
1050 call_code.assign (test_state->bplate->bplate_string[create_key]);
1051 check_code.assign (test_state->bplate->bplate_string[create_key_check]);
1052 call_description = "key-create call";
1053}
1054create_key_call::~create_key_call (void)
1055{
1056 return; // just to have something to pin a breakpoint onto
1057}
1058
Nik Dewallyabac0e52024-08-02 13:42:27 +01001059
Karl Zhang3de5ab12021-05-31 11:45:48 +08001060bool create_key_call::copy_call_to_asset (void)
1061{
Nik Dewallyabac0e52024-08-02 13:42:27 +01001062 return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001063}
1064
1065void create_key_call::fill_in_prep_code (void)
1066{
1067 string var_name = asset_info.get_name();
1068 vector<variable_info>::iterator assign_variable;
1069 gibberish gib;
1070 char gib_buff[500];
1071 string t_string;
1072
1073 // Key variable:
1074 assign_variable = test_state->find_var (var_name);
1075 if (assign_variable == test_state->variable.end()) {
1076 // No such variable exists, so:
1077 test_state->make_var (var_name);
1078 prep_code.append (test_state->bplate->bplate_string[declare_key]);
1079 find_replace_all ("$var", var_name, prep_code);
1080 }
1081 // Key-data variable:
1082 var_name = asset_info.get_name() + "_set_data";
1083 assign_variable = test_state->find_var (var_name);
1084 if (assign_variable == test_state->variable.end()) {
1085 // No such variable exists, so:
1086 test_state->make_var (var_name);
1087 prep_code.append (test_state->bplate->bplate_string[declare_big_string]);
1088 find_replace_all ("$var", var_name, prep_code);
1089 int rand_data_length = 12 + (rand() % 100);
1090 gib.sentence (gib_buff, gib_buff + rand_data_length - 1);
1091 t_string = gib_buff;
1092 find_replace_all ("$init", t_string, prep_code);
1093 }
1094}
1095
1096void create_key_call::fill_in_command (void)
1097{
1098 // (call_code already loaded by constructor)
1099 find_replace_all ("$policy", policy.asset_2_name, call_code);
1100 find_replace_all ("$data", asset_info.get_name() + "_set_data", call_code);
1101 find_replace_all ("$length", to_string (policy.n_bits), call_code);
1102 find_replace_all ("$key", asset_info.get_name(), call_code);
1103 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +01001104 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001105}
1106
1107/**********************************************************************************
1108 End of methods of class create_key_call.
1109**********************************************************************************/
1110
1111
1112/**********************************************************************************
1113 Methods of class copy_key_call follow:
1114**********************************************************************************/
1115
1116copy_key_call::copy_key_call (tf_fuzz_info *test_state, // (constructor)
1117 long &call_ser_no,
1118 asset_search how_asset_found)
1119 : key_call(test_state, call_ser_no,
1120 how_asset_found)
1121{
1122 // Copy the boilerplate text into local buffers:
1123 prep_code.assign ("");
1124 call_code.assign (test_state->bplate->bplate_string[copy_key]);
1125 check_code.assign (test_state->bplate->bplate_string[copy_key_check]);
1126 call_description = "key-copy call";
1127}
1128copy_key_call::~copy_key_call (void)
1129{
1130 return; // just to have something to pin a breakpoint onto
1131}
1132
1133bool copy_key_call::copy_call_to_asset (void)
1134{
1135 return copy_call_to_asset_t<key_asset*> (this, yes_create_asset);
1136}
1137
1138void copy_key_call::fill_in_prep_code (void)
1139{
1140 string var_name = asset_info.get_name();
1141 vector<variable_info>::iterator assign_variable;
1142
1143 // Make sure key variable is defined:
1144 assign_variable = test_state->find_var (var_name);
1145 if (assign_variable == test_state->variable.end()) {
1146 // No such variable exists, so:
1147 test_state->make_var (var_name);
1148 prep_code.append (test_state->bplate->bplate_string[declare_key]);
1149 find_replace_all ("$var", var_name, prep_code);
1150 }
1151}
1152
1153void copy_key_call::fill_in_command (void)
1154{
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001155 // (call_code already loaded by constructor)
1156 find_replace_all ("$master", policy.asset_3_name, call_code);
1157 find_replace_all ("$policy", policy.asset_2_name, call_code);
1158 find_replace_all ("$copy", asset_info.get_name(), call_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001159
Nik Dewally6663dde2024-08-09 16:12:27 +01001160 // TODO:: move error code modelling code to simulate().
1161
Karl Zhang3de5ab12021-05-31 11:45:48 +08001162 // Calculate the expected results:
1163 asset_search find_result;
1164 vector<psa_asset*>::iterator asset;
1165 long dummy = 0L;
1166 // See if the source key does not exist:
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001167 find_result = test_state->
1168 find_or_create_key_asset (psa_asset_search::name, psa_asset_usage::active,
1169 policy.asset_3_name, (uint64_t) 0, dummy,
1170 dont_create_asset, asset);
Nik Dewally6663dde2024-08-09 16:12:27 +01001171
1172
Karl Zhang3de5ab12021-05-31 11:45:48 +08001173 if (find_result != asset_search::found_active) {
Nik Dewally6663dde2024-08-09 16:12:27 +01001174 exp_data.expect_error_code("PSA_ERROR_INVALID_ARGUMENT");
Karl Zhang3de5ab12021-05-31 11:45:48 +08001175 } else {
1176 // See if the new policy does not exist:
1177 find_result = test_state->
1178 find_or_create_policy_asset (psa_asset_search::name, psa_asset_usage::active,
1179 policy.asset_2_name, (uint64_t) 0, dummy,
1180 dont_create_asset, asset);
1181 if (find_result != asset_search::found_active) {
Nik Dewally6663dde2024-08-09 16:12:27 +01001182 exp_data.expect_error_code("PSA_ERROR_INVALID_ARGUMENT");
1183
Karl Zhang3de5ab12021-05-31 11:45:48 +08001184 } else if (!(*asset)->policy.copyable) {
1185 // See if the source key does not support export:
1186 // TODO: Or wait, it's the original policy for the key, right?
Nik Dewally6663dde2024-08-09 16:12:27 +01001187 exp_data.expect_error_code("PSA_ERROR_NOT_PERMITTED");
Karl Zhang3de5ab12021-05-31 11:45:48 +08001188 }
1189 }
Nik Dewally6663dde2024-08-09 16:12:27 +01001190 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001191}
1192
1193/**********************************************************************************
1194 End of methods of class copy_key_call.
1195**********************************************************************************/
1196
1197
1198/**********************************************************************************
1199 Methods of class read_key_data_call follow:
1200**********************************************************************************/
1201
1202read_key_data_call::read_key_data_call (tf_fuzz_info *test_state, // (constructor)
1203 long &call_ser_no,
1204 asset_search how_asset_found)
1205 : key_call(test_state, call_ser_no,
1206 how_asset_found)
1207{
1208 // Copy the boilerplate text into local buffers:
1209 prep_code.assign ("");
1210 call_code.assign (test_state->bplate->bplate_string[read_key_data]);
1211 check_code.assign (test_state->bplate->bplate_string[read_key_data_check]);
1212 call_description = "key read-data call";
1213}
1214read_key_data_call::~read_key_data_call (void)
1215{
1216 return; // just to have something to pin a breakpoint onto
1217}
1218
1219bool read_key_data_call::copy_call_to_asset (void)
1220{
1221 return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
1222}
1223
1224void read_key_data_call::fill_in_prep_code (void)
1225{
1226 string var_name, length_var_name, actual_length_var_name, var_name_suffix,
1227 length_var_name_suffix, temp_string;
1228 vector<variable_info>::iterator expect_variable;
1229 vector<variable_info>::iterator assign_variable;
1230
1231 if (exp_data.data_var_specified) {
1232 var_name.assign (exp_data.data_var + "_data");
1233 length_var_name.assign (exp_data.data_var + "_length");
1234 /* If actual-data variable doesn't already exist, create variable tracker,
1235 and write declaration for it: */
1236 expect_variable = test_state->find_var (exp_data.data_var);
1237 if (expect_variable == test_state->variable.end()) {
1238 // No such variable exists, so:
1239 test_state->make_var (exp_data.data_var);
1240 expect_variable = test_state->find_var (exp_data.data_var);
1241 prep_code.append (test_state->bplate->bplate_string[declare_big_string]);
1242 find_replace_1st ("$var", var_name, prep_code);
1243 temp_string = (char *) expect_variable->value;
1244 find_replace_1st ("$init", temp_string, prep_code);
1245 // Input data length:
1246 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
1247 find_replace_1st ("$var", length_var_name, prep_code);
1248 find_replace_1st ("$init", to_string(temp_string.length()), prep_code);
1249 // TODO: Is these lengths in bits or bytes?
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001250 // Actual (output) data length:
1251 actual_length_var_name.assign (exp_data.data_var + "_act_length");
1252 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
1253 find_replace_1st ("$var", actual_length_var_name, prep_code);
1254 find_replace_1st ("$init", to_string(temp_string.length()), prep_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001255 }
1256 }
Karl Zhang3de5ab12021-05-31 11:45:48 +08001257 if (assign_data_var_specified) {
1258 var_name.assign (assign_data_var + "_data");
1259 length_var_name.assign (assign_data_var + "_length");
1260 actual_length_var_name.assign (assign_data_var + "_act_length");
1261 /* If actual-data variable doesn't already exist, create variable tracker,
1262 and write declaration for it: */
1263 assign_variable = test_state->find_var (assign_data_var);
1264 if (assign_variable == test_state->variable.end()) {
1265 // No such variable exists, so:
1266 test_state->make_var (assign_data_var);
1267 assign_variable = test_state->find_var (assign_data_var);
1268 prep_code.append (test_state->bplate->bplate_string[declare_big_string]);
1269 find_replace_1st ("$var", var_name, prep_code);
1270 temp_string = (char *) assign_variable->value;
1271 find_replace_1st ("$init", temp_string, prep_code);
1272 // Input data length:
1273 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
1274 find_replace_1st ("$var", length_var_name, prep_code);
1275 find_replace_1st ("$init", to_string(temp_string.length()), prep_code);
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001276 // Actual (output) data length:
1277 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
1278 find_replace_1st ("$var", actual_length_var_name, prep_code);
1279 find_replace_1st ("$init", to_string(temp_string.length()), prep_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001280 }
1281 } else {
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001282 // Single string of two lines declaring string data and its length:
1283 var_name_suffix = "_set_data";
1284 length_var_name_suffix = "_set_length";
1285 if (set_data.n_set_vars > 0) {
1286 var_name_suffix += "_" + to_string(set_data.n_set_vars);
1287 length_var_name_suffix += "_" + to_string(set_data.n_set_vars);
1288 }
1289 var_name.assign (asset_info.get_name() + var_name_suffix);
1290 length_var_name.assign (asset_info.get_name() + length_var_name_suffix);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001291 prep_code.append (test_state->bplate->bplate_string[declare_string]);
1292 find_replace_1st ("$var", var_name, prep_code);
1293 find_replace_1st ("$init", set_data.get(), prep_code);
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001294 temp_string.assign (test_state->bplate->bplate_string[declare_int]);
1295 find_replace_1st ("static int", "static uint32_t", temp_string);
1296 prep_code.append (temp_string);
1297 find_replace_1st ("$var", length_var_name, prep_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001298 find_replace_1st ("$init", to_string(set_data.get().length()), prep_code);
1299 }
Karl Zhang3de5ab12021-05-31 11:45:48 +08001300}
1301
1302void read_key_data_call::fill_in_command (void)
1303{
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001304 string var_name, length_var_name, actual_length_var_name, var_name_suffix,
1305 length_var_name_suffix, temp_string;
Karl Zhang3de5ab12021-05-31 11:45:48 +08001306
1307 // Fill in the PSA command itself:
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001308 if (exp_data.data_var_specified) {
1309 var_name.assign (exp_data.data_var + "_data");
1310 actual_length_var_name.assign (exp_data.data_var + "_act_length");
1311 } else {
1312 actual_length_var_name.assign (assign_data_var + "_act_length");
1313 }
Karl Zhang3de5ab12021-05-31 11:45:48 +08001314 if (assign_data_var_specified) {
1315 var_name.assign (assign_data_var + "_data");
1316 length_var_name.assign (assign_data_var + "_length");
1317 } else {
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001318 var_name_suffix = "_set_data";
1319 if (set_data.n_set_vars > 0) {
1320 var_name_suffix += "_" + to_string(set_data.n_set_vars);
1321 }
Karl Zhang3de5ab12021-05-31 11:45:48 +08001322 var_name.assign (asset_info.get_name() + var_name_suffix);
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001323 length_var_name_suffix = "_set_length";
1324 if (set_data.n_set_vars > 0) {
1325 length_var_name_suffix += "_" + to_string(set_data.n_set_vars);
1326 }
Karl Zhang3de5ab12021-05-31 11:45:48 +08001327 length_var_name.assign (asset_info.get_name() + length_var_name_suffix);
1328 }
1329 find_replace_1st ("$data", var_name, call_code);
1330 find_replace_1st ("$key", asset_info.get_name(), call_code);
1331 string id_string = to_string((long) asset_info.id_n++);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001332 find_replace_1st ("$act_size", actual_length_var_name, call_code);
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001333 find_replace_1st ("$length", length_var_name, call_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001334
Nik Dewally6663dde2024-08-09 16:12:27 +01001335 // TODO: move error checking code to simulate().
1336
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001337 // TODO: check data?
Karl Zhang3de5ab12021-05-31 11:45:48 +08001338
1339 // See if the source key did not exist:
1340 if (!policy.exportable) {
1341 // See if the source key does not support export:
Nik Dewally6663dde2024-08-09 16:12:27 +01001342 exp_data.expect_error_code("PSA_ERROR_NOT_PERMITTED");
Karl Zhang3de5ab12021-05-31 11:45:48 +08001343 }
Nik Dewally6663dde2024-08-09 16:12:27 +01001344 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001345}
1346
1347/**********************************************************************************
1348 End of methods of class read_key_data_call.
1349**********************************************************************************/
1350
1351
1352/**********************************************************************************
1353 Methods of class remove_key_call follow:
1354**********************************************************************************/
1355
1356remove_key_call::remove_key_call (tf_fuzz_info *test_state, // (constructor)
1357 long &call_ser_no,
1358 asset_search how_asset_found)
1359 : key_call(test_state, call_ser_no,
1360 how_asset_found)
1361{
1362 // Copy the boilerplate text into local buffers:
1363 prep_code.assign ("");
1364 call_code.assign (test_state->bplate->bplate_string[remove_key]);
1365 check_code.assign (test_state->bplate->bplate_string[remove_key_check]);
1366 call_description = "key-remove call";
1367}
1368remove_key_call::~remove_key_call (void)
1369{
1370 return; // just to have something to pin a breakpoint onto
1371}
1372
Nik Dewallyabac0e52024-08-02 13:42:27 +01001373bool remove_key_call::simulate () {
1374 if (!exp_data.simulation_needed()) {
1375 return false;
1376 }
1377 switch (asset_info.how_asset_found) {
1378 case asset_search::found_active:
1379 case asset_search::created_new:
1380 exp_data.expect_pass();
1381 return true;
1382
1383 // if the asset never existed, its handle will be null.
1384 // deleting the null handle succeeds
1385 case asset_search::not_found:
1386 exp_data.expect_pass();
1387 return true;
1388
1389 case asset_search::found_deleted:
1390 exp_data.expect_failure();
1391 return true;
1392 default:
1393 exp_data.expect_failure();
1394 return true;
1395 }
1396}
Karl Zhang3de5ab12021-05-31 11:45:48 +08001397bool remove_key_call::copy_call_to_asset (void)
1398{
1399 return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
1400}
1401
1402void remove_key_call::fill_in_prep_code (void)
1403{
1404 // No prep code required.
1405 return; // just to have something to pin a breakpoint onto
1406}
1407
1408void remove_key_call::fill_in_command (void)
1409{
1410 // (call_code already loaded by constructor)
1411 find_replace_all ("$key", asset_info.get_name(), call_code);
1412 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +01001413 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001414}
1415
1416/**********************************************************************************
1417 End of methods of class remove_key_call.
1418**********************************************************************************/