blob: 92d3d2e194fad4b37e511b896e48aa11150073b4 [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 Dewallyed341b72024-08-20 17:02:30 +01008#include <cstring>
Nik Dewallybacae6c2024-07-30 16:58:14 +01009#include <stdint.h>
Karl Zhang3de5ab12021-05-31 11:45:48 +080010#include <cstdlib>
Nik Dewallybacae6c2024-07-30 16:58:14 +010011#include <iostream>
Karl Zhang3de5ab12021-05-31 11:45:48 +080012
13#include "boilerplate.hpp"
14#include "variables.hpp"
15#include "gibberish.hpp"
Karl Zhang3de5ab12021-05-31 11:45:48 +080016#include "string_ops.hpp"
17#include "data_blocks.hpp"
18#include "psa_asset.hpp"
19#include "find_or_create_asset.hpp"
Karl Zhang3de5ab12021-05-31 11:45:48 +080020#include "tf_fuzz.hpp"
21#include "crypto_asset.hpp"
22#include "psa_call.hpp"
23#include "crypto_call.hpp"
Nik Dewallyabac0e52024-08-02 13:42:27 +010024#include "crypto_model.hpp"
Karl Zhang3de5ab12021-05-31 11:45:48 +080025
Nik Dewallyabac0e52024-08-02 13:42:27 +010026using namespace crypto_model;
Karl Zhang3de5ab12021-05-31 11:45:48 +080027
28/**********************************************************************************
29 Methods of class policy_call follow:
30**********************************************************************************/
31
32/* Most of the policy classes, in their fill_in_prep_code() method, need to ensure
33 that, at a minimum, the policy variable (psa_key_attributes_t) exists, so, just
34 to cut down code duplication: */
35void policy_call::policy_fill_in_prep_code (void)
36{
37 vector<variable_info>::iterator policy_variable;
38
39 policy_variable = test_state->find_var (asset_info.get_name());
40 if (policy_variable == test_state->variable.end()) {
41 // No such variable exists, so:
42 test_state->make_var (asset_info.get_name());
43 policy_variable = test_state->find_var (asset_info.get_name());
44 prep_code.assign (test_state->bplate->bplate_string[declare_policy]);
45 find_replace_1st ("$var", asset_info.get_name(), prep_code);
46 }
47}
48
49
50policy_call::policy_call (tf_fuzz_info *test_state, // (constructor)
51 long &call_ser_no,
52 asset_search how_asset_found)
53 : crypto_call(test_state, call_ser_no, how_asset_found)
54{
55 // Note: Key attributes are set in the key_policy_info constructor.
56}
57policy_call::~policy_call (void)
58{
59 // Nothing further to delete.
60 return; // just to have something to pin a breakpoint onto
61}
62
63vector<psa_asset*>::iterator policy_call::resolve_asset (bool create_asset_bool,
64 psa_asset_usage where) {
65 vector<psa_asset*>::iterator found_asset;
66 vector<psa_asset*> *asset_vector;
67 int asset_pick;
68
69 if (random_asset != psa_asset_usage::all) {
70 // != psa_asset_usage::all means to choose some known asset at random:
71 if (random_asset == psa_asset_usage::active) {
72 asset_vector = &(test_state->active_policy_asset);
73 asset_info.how_asset_found = asset_search::found_active;
74 } else if (random_asset == psa_asset_usage::deleted) {
75 asset_vector = &(test_state->deleted_policy_asset);
76 asset_info.how_asset_found = asset_search::found_deleted;
77 } else {
78 // "invalid" assets are not currently used.
79 cerr << "\nError: Tool-internal: Please report error 1102 to " << endl
80 << "TF-Fuzz developers."
81 << endl;
82 exit(1102);
83 }
84 if (asset_vector->size() > 0) {
85 /* Pick an active or deleted asset at random: */
86 asset_pick = rand() % asset_vector->size();
87 found_asset = asset_vector->begin() + asset_pick;
88 /* Copy asset information into template tracker: */
89 asset_info.id_n = (*found_asset)->asset_info.id_n;
90 asset_info.asset_ser_no
91 = (*found_asset)->asset_info.asset_ser_no;
92 } else {
93 if (random_asset == psa_asset_usage::active) {
94 cerr << "\nError: A policy call asks for a "
95 << "randomly chosen active asset, when none " << endl
96 << "is currently defined." << endl;
97 exit(1010);
98 } else if (random_asset == psa_asset_usage::deleted) {
99 cerr << "\nError: A policy call asks for a "
100 << "randomly chosen deleted asset, when none " << endl
101 << "is currently defined." << endl;
102 exit(1011);
103 } // "invalid" assets are not currently used.
104 }
105 } else {
106 // Find the asset by name:
107 asset_info.how_asset_found = test_state->find_or_create_policy_asset (
108 psa_asset_search::name, where,
109 asset_info.get_name(), 0, asset_info.asset_ser_no,
110 create_asset_bool, found_asset );
111 if ( asset_info.how_asset_found == asset_search::unsuccessful
112 || asset_info.how_asset_found == asset_search::something_wrong ) {
113 cerr << "\nError: Tool-internal: Please report error 108 to " << endl
114 << "TF-Fuzz developers."
115 << endl;
116 exit(108);
117 }
118 }
119 return found_asset;
120}
121
122/**********************************************************************************
123 End of methods of class policy_call.
124**********************************************************************************/
125
126
127/**********************************************************************************
128 Methods of class key_call follow:
129**********************************************************************************/
130
131key_call::key_call (tf_fuzz_info *test_state, // (constructor)
132 long &call_ser_no,
133 asset_search how_asset_found)
134 : crypto_call(test_state, call_ser_no, how_asset_found)
135{
136 asset_info.the_asset = nullptr;
137}
138key_call::~key_call (void)
139{
140 // Nothing further to delete.
141 return; // just to have something to pin a breakpoint onto
142}
143
144vector<psa_asset*>::iterator key_call::resolve_asset (bool create_asset_bool,
145 psa_asset_usage where) {
146 vector<psa_asset*>::iterator found_asset;
147 vector<psa_asset*> *asset_vector;
148 int asset_pick;
149
150 if (random_asset != psa_asset_usage::all) {
151 // != psa_asset_usage::all means to choose some known asset at random:
152 if (random_asset == psa_asset_usage::active) {
153 asset_vector = &(test_state->active_key_asset);
154 asset_info.how_asset_found = asset_search::found_active;
155 } else if (random_asset == psa_asset_usage::deleted) {
156 asset_vector = &(test_state->deleted_key_asset);
157 asset_info.how_asset_found = asset_search::found_deleted;
158 } else {
159 // "invalid" assets are not currently used.
160 cerr << "\nError: Tool-internal: Please report error 1103 to " << endl
161 << "TF-Fuzz developers."
162 << endl;
163 exit(1103);
164 }
165 if (asset_vector->size() > 0) {
166 /* Pick an active or deleted asset at random: */
167 asset_pick = rand() % asset_vector->size();
168 found_asset = asset_vector->begin() + asset_pick;
169 /* Copy asset information into template tracker: */
170 asset_info.id_n = (*found_asset)->asset_info.id_n;
171 asset_info.asset_ser_no
172 = (*found_asset)->asset_info.asset_ser_no;
173 } else {
174 if (random_asset == psa_asset_usage::active) {
175 cerr << "\nError: A key call asks for a "
176 << "randomly chosen active asset, when none " << endl
177 << "is currently defined." << endl;
178 exit(1012);
179 } else if (random_asset == psa_asset_usage::deleted) {
180 cerr << "\nError: A key call asks for a "
181 << "randomly chosen deleted asset, when none " << endl
182 << "is currently defined." << endl;
183 exit(1013);
184 } // "invalid" assets are not currently used.
185 }
186 } else {
187 // Find the asset by name:
188 asset_info.how_asset_found = test_state->find_or_create_key_asset (
189 psa_asset_search::name, where,
190 asset_info.get_name(), 0, asset_info.asset_ser_no,
191 create_asset_bool, found_asset );
192 if ( asset_info.how_asset_found == asset_search::unsuccessful
193 || asset_info.how_asset_found == asset_search::something_wrong ) {
194 cerr << "\nError: Tool-internal: Please report error 108 to " << endl
195 << "TF-Fuzz developers."
196 << endl;
197 exit(108);
198 }
199 }
200 return found_asset;
201}
202
203/**********************************************************************************
204 End of methods of class key_call.
205**********************************************************************************/
206
207
208/**********************************************************************************
209 Methods of class init_policy_call follow:
210**********************************************************************************/
211
212init_policy_call::init_policy_call (tf_fuzz_info *test_state, // (constructor)
213 long &call_ser_no,
214 asset_search how_asset_found)
215 : policy_call(test_state, call_ser_no,
216 how_asset_found)
217{
218 // Copy the boilerplate text into local buffers:
219 prep_code.assign ("");
220 call_code.assign (test_state->bplate->bplate_string[init_policy]);
221 call_description = "initialize-policy call";
222}
223init_policy_call::~init_policy_call (void)
224{
225 return; // just to have something to pin a breakpoint onto
226}
227
228bool init_policy_call::copy_call_to_asset (void)
229{
230 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
231}
232
233void init_policy_call::fill_in_prep_code (void)
234{
235 policy_fill_in_prep_code();
236}
237
238void init_policy_call::fill_in_command (void)
239{
240 // (call_code already loaded by constructor)
241 find_replace_all ("$policy", asset_info.get_name(), call_code);
242 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100243 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800244}
245
246/**********************************************************************************
247 End of methods of class init_policy_call.
248**********************************************************************************/
249
250
251/**********************************************************************************
252 Methods of class reset_policy_call follow:
253**********************************************************************************/
254
255reset_policy_call::reset_policy_call (tf_fuzz_info *test_state, // (constructor)
256 long &call_ser_no,
257 asset_search how_asset_found)
258 : policy_call(test_state, call_ser_no,
259 how_asset_found)
260{
261 // Copy the boilerplate text into local buffers:
262 prep_code.assign ("");
263 call_code.assign (test_state->bplate->bplate_string[reset_policy]);
264 call_description = "policy reset call";
265}
266reset_policy_call::~reset_policy_call (void)
267{
268 return; // just to have something to pin a breakpoint onto
269}
270
271bool reset_policy_call::copy_call_to_asset (void)
272{
273 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
274}
275
276void reset_policy_call::fill_in_prep_code (void)
277{
278 policy_fill_in_prep_code();
279}
280
281void reset_policy_call::fill_in_command (void)
282{
283 // (call_code already loaded by constructor)
284 find_replace_all ("$policy", asset_info.get_name(), call_code);
285 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100286 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800287}
288
289/**********************************************************************************
290 End of methods of class reset_policy_call.
291**********************************************************************************/
292
293
294/**********************************************************************************
295 Methods of class add_policy_usage_call follow:
296**********************************************************************************/
297
298add_policy_usage_call::add_policy_usage_call (tf_fuzz_info *test_state, // (constructor)
299 long &call_ser_no,
300 asset_search how_asset_found)
301 : policy_call(test_state, call_ser_no,
302 how_asset_found)
303{
304 // Copy the boilerplate text into local buffers:
305 prep_code.assign ("");
306 call_code.assign (test_state->bplate->bplate_string[add_policy_usage]);
307 call_description = "policy add-usage call";
308}
309add_policy_usage_call::~add_policy_usage_call (void)
310{
311 return; // just to have something to pin a breakpoint onto
312}
313
314bool add_policy_usage_call::copy_call_to_asset (void)
315{
316 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
317}
318
319void add_policy_usage_call::fill_in_prep_code (void)
320{
321 policy_fill_in_prep_code();
322 /* TODO: The variable this creates should have been declared already. Should
323 this instead produce an error if it doesn't exist? */
324}
325
326void add_policy_usage_call::fill_in_command (void)
327{
328 // (call_code already loaded by constructor)
329 find_replace_all ("$policy", asset_info.get_name(), call_code);
330 find_replace_1st ("$flag", policy.usage_string, call_code);
331 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100332 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800333}
334
335/**********************************************************************************
336 End of methods of class add_policy_usage_call.
337**********************************************************************************/
338
339
340/**********************************************************************************
341 Methods of class set_policy_lifetime_call follow:
342**********************************************************************************/
343
344set_policy_lifetime_call::set_policy_lifetime_call (tf_fuzz_info *test_state,
345 long &call_ser_no, // (constructor)
346 asset_search how_asset_found)
347 : policy_call(test_state, call_ser_no,
348 how_asset_found)
349{
350 // Copy the boilerplate text into local buffers:
351 prep_code.assign ("");
352 call_code.assign (test_state->bplate->bplate_string[set_policy_lifetime]);
353 call_description = "policy lifetime-set call";
354}
355set_policy_lifetime_call::~set_policy_lifetime_call (void)
356{
357 return; // just to have something to pin a breakpoint onto
358}
359
360bool set_policy_lifetime_call::copy_call_to_asset (void)
361{
362 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
363}
364
365void set_policy_lifetime_call::fill_in_prep_code (void)
366{
367 policy_fill_in_prep_code();
368}
369
370void set_policy_lifetime_call::fill_in_command (void)
371{
372 // (call_code already loaded by constructor)
373 find_replace_all ("$policy", asset_info.get_name(), call_code);
374 find_replace_1st ("$life",
375 policy.persistent? "PSA_KEY_LIFETIME_PERSISTENT"
376 : "PSA_KEY_LIFETIME_VOLATILE",
377 call_code);
378 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100379 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800380}
381
382/**********************************************************************************
383 End of methods of class set_policy_lifetime_call.
384**********************************************************************************/
385
386
387/**********************************************************************************
388 Methods of class set_policy_size_call follow:
389**********************************************************************************/
390
391set_policy_size_call::set_policy_size_call (tf_fuzz_info *test_state, // (constructor)
392 long &call_ser_no,
393 asset_search how_asset_found)
394 : policy_call(test_state, call_ser_no,
395 how_asset_found)
396{
397 // Copy the boilerplate text into local buffers:
398 prep_code.assign ("");
399 call_code.assign (test_state->bplate->bplate_string[set_policy_size]);
400 call_description = "policy size-set call";
401}
402set_policy_size_call::~set_policy_size_call (void)
403{
404 return; // just to have something to pin a breakpoint onto
405}
406
407bool set_policy_size_call::copy_call_to_asset (void)
408{
409 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
410}
411
412void set_policy_size_call::fill_in_prep_code (void)
413{
414 policy_fill_in_prep_code();
415}
416
417void set_policy_size_call::fill_in_command (void)
418{
419 // (call_code already loaded by constructor)
420 find_replace_all ("$policy", asset_info.get_name(), call_code);
421 find_replace_1st ("$size", to_string (policy.n_bits), call_code);
422 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100423 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800424}
425
426/**********************************************************************************
427 End of methods of class set_policy_size_call.
428**********************************************************************************/
429
430
431/**********************************************************************************
432 Methods of class set_policy_type_call follow:
433**********************************************************************************/
434
435set_policy_type_call::set_policy_type_call (tf_fuzz_info *test_state, // (constructor)
436 long &call_ser_no,
437 asset_search how_asset_found)
438 : policy_call(test_state, call_ser_no,
439 how_asset_found)
440{
441 // Copy the boilerplate text into local buffers:
442 prep_code.assign ("");
443 call_code.assign (test_state->bplate->bplate_string[set_policy_type]);
444 call_description = "policy type-set call";
445}
446set_policy_type_call::~set_policy_type_call (void)
447{
448 return; // just to have something to pin a breakpoint onto
449}
450
451bool set_policy_type_call::copy_call_to_asset (void)
452{
453 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
454}
455
456void set_policy_type_call::fill_in_prep_code (void)
457{
458 policy_fill_in_prep_code();
459}
460
461void set_policy_type_call::fill_in_command (void)
462{
463 // (call_code already loaded by constructor)
464 find_replace_all ("$policy", asset_info.get_name(), call_code);
465 find_replace_1st ("$type", policy.key_type, call_code);
466 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100467 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800468}
469
470/**********************************************************************************
471 End of methods of class set_policy_type_call.
472**********************************************************************************/
473
474
475/**********************************************************************************
476 Methods of class set_policy_algorithm_call follow:
477**********************************************************************************/
478
479set_policy_algorithm_call::set_policy_algorithm_call (tf_fuzz_info *test_state,
480 long &call_ser_no, // (constructor)
481 asset_search how_asset_found)
482 : policy_call(test_state, call_ser_no,
483 how_asset_found)
484{
485 // Copy the boilerplate text into local buffers:
486 prep_code.assign ("");
487 call_code.assign (test_state->bplate->bplate_string[set_policy_algorithm]);
488 call_description = "policy algorithm-set call";
489}
490set_policy_algorithm_call::~set_policy_algorithm_call (void)
491{
492 return; // just to have something to pin a breakpoint onto
493}
494
495bool set_policy_algorithm_call::copy_call_to_asset (void)
496{
497 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
498}
499
500void set_policy_algorithm_call::fill_in_prep_code (void)
501{
502 policy_fill_in_prep_code();
503}
504
505void set_policy_algorithm_call::fill_in_command (void)
506{
507 // (call_code already loaded by constructor)
508 find_replace_all ("$policy", asset_info.get_name(), call_code);
509 find_replace_1st ("$algorithm", policy.key_algorithm, call_code);
510 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100511 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800512}
513
514/**********************************************************************************
515 End of methods of class set_policy_algorithm_call.
516**********************************************************************************/
517
518
519/**********************************************************************************
520 Methods of class set_policy_usage_call follow:
521**********************************************************************************/
522
523set_policy_usage_call::set_policy_usage_call (tf_fuzz_info *test_state, // (constructor)
524 long &call_ser_no,
525 asset_search how_asset_found)
526 : policy_call(test_state, call_ser_no,
527 how_asset_found)
528{
529 // Copy the boilerplate text into local buffers:
530 prep_code.assign ("");
531 call_code.assign (test_state->bplate->bplate_string[set_policy_usage]);
532 call_description = "policy usage-set call";
533}
534set_policy_usage_call::~set_policy_usage_call (void)
535{
536 return; // just to have something to pin a breakpoint onto
537}
538
539bool set_policy_usage_call::copy_call_to_asset (void)
540{
541 return copy_call_to_asset_t<policy_asset*> (this, yes_create_asset);
542}
543
544void set_policy_usage_call::fill_in_prep_code (void)
545{
546 policy_fill_in_prep_code();
547}
548
549void set_policy_usage_call::fill_in_command (void)
550{
551 find_replace_1st ("$policy", asset_info.get_name(), call_code);
552 find_replace_1st ("$usage", "0", call_code);
553 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100554 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800555}
556
557/**********************************************************************************
558 End of methods of class set_policy_usage_call.
559**********************************************************************************/
560
561
562/**********************************************************************************
563 Methods of class get_policy_lifetime_call follow:
564**********************************************************************************/
565
566get_policy_lifetime_call::get_policy_lifetime_call (tf_fuzz_info *test_state,
567 long &call_ser_no, // (constructor)
568 asset_search how_asset_found)
569 : policy_call(test_state, call_ser_no,
570 how_asset_found)
571{
572 // Copy the boilerplate text into local buffers:
573 prep_code.assign ("");
574 call_code.assign (test_state->bplate->bplate_string[get_policy_lifetime]);
575 call_description = "policy lifetime-get call";
576}
577get_policy_lifetime_call::~get_policy_lifetime_call (void)
578{
579 return; // just to have something to pin a breakpoint onto
580}
581
582bool get_policy_lifetime_call::copy_call_to_asset (void)
583{
584 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
585}
586
587void get_policy_lifetime_call::fill_in_prep_code (void)
588{
589 string var_name = asset_info.get_name() + "_life";
590 vector<variable_info>::iterator assign_variable;
591
592 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
593 assign_variable = test_state->find_var (var_name);
594 if (assign_variable == test_state->variable.end()) {
595 // No such variable exists, so:
596 test_state->make_var (var_name);
597 prep_code.append (test_state->bplate->bplate_string[declare_policy_lifetime]);
598 find_replace_all ("$var", var_name, prep_code);
599 }
600}
601
602void get_policy_lifetime_call::fill_in_command (void)
603{
604 // (call_code already loaded by constructor)
605 find_replace_all ("$policy", asset_info.get_name(), call_code);
606 find_replace_1st ("$life", asset_info.get_name() + "_life", call_code);
607 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100608 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800609}
610
611/**********************************************************************************
612 End of methods of class get_policy_lifetime_call.
613**********************************************************************************/
614
615
616/**********************************************************************************
617 Methods of class get_policy_size_call follow:
618**********************************************************************************/
619
620get_policy_size_call::get_policy_size_call (tf_fuzz_info *test_state,
621 long &call_ser_no, // (constructor)
622 asset_search how_asset_found)
623 : policy_call(test_state, call_ser_no,
624 how_asset_found)
625{
626 // Copy the boilerplate text into local buffers:
627 prep_code.assign ("");
628 call_code.assign (test_state->bplate->bplate_string[get_policy_size]);
629 call_description = "policy size-get call";
630}
631get_policy_size_call::~get_policy_size_call (void)
632{
633 return; // just to have something to pin a breakpoint onto
634}
635
636bool get_policy_size_call::copy_call_to_asset (void)
637{
638 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
639}
640
641void get_policy_size_call::fill_in_prep_code (void)
642{
643 string var_name = asset_info.get_name() + "_size";
644 vector<variable_info>::iterator assign_variable;
645
646 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
647 assign_variable = test_state->find_var (var_name);
648 if (assign_variable == test_state->variable.end()) {
649 // No such variable exists, so:
650 test_state->make_var (var_name);
651 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
652 find_replace_all ("$var", var_name, prep_code);
653 find_replace_1st ("$init", to_string(exp_data.data.length()), prep_code);
654 }
655}
656
657void get_policy_size_call::fill_in_command (void)
658{
659 // (call_code already loaded by constructor)
660 find_replace_all ("$policy", asset_info.get_name(), call_code);
661 find_replace_1st ("$size", asset_info.get_name() + "_size", call_code);
662 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100663 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800664}
665
666/**********************************************************************************
667 End of methods of class get_policy_size_call.
668**********************************************************************************/
669
670
671/**********************************************************************************
672 Methods of class get_policy_type_call follow:
673**********************************************************************************/
674
675get_policy_type_call::get_policy_type_call (tf_fuzz_info *test_state,
676 long &call_ser_no, // (constructor)
677 asset_search how_asset_found)
678 : policy_call(test_state, call_ser_no,
679 how_asset_found)
680{
681 // Copy the boilerplate text into local buffers:
682 prep_code.assign ("");
683 call_code.assign (test_state->bplate->bplate_string[get_policy_type]);
684 call_description = "policy type-get call";
685}
686get_policy_type_call::~get_policy_type_call (void)
687{
688 return; // just to have something to pin a breakpoint onto
689}
690
691bool get_policy_type_call::copy_call_to_asset (void)
692{
693 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
694}
695
696void get_policy_type_call::fill_in_prep_code (void)
697{
698 string var_name = asset_info.get_name() + "_type";
699 vector<variable_info>::iterator assign_variable;
700
701 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
702 assign_variable = test_state->find_var (var_name);
703 if (assign_variable == test_state->variable.end()) {
704 // No such variable exists, so:
705 test_state->make_var (var_name);
706 prep_code.append (test_state->bplate->bplate_string[declare_policy_type]);
707 find_replace_all ("$var", var_name, prep_code);
708 }
709}
710
711void get_policy_type_call::fill_in_command (void)
712{
713 // (call_code already loaded by constructor)
714 find_replace_all ("$policy", asset_info.get_name(), call_code);
715 find_replace_1st ("$type", asset_info.get_name() + "_type", call_code);
716 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100717 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800718}
719
720/**********************************************************************************
721 End of methods of class get_policy_type_call.
722**********************************************************************************/
723
724
725/**********************************************************************************
726 Methods of class get_policy_algorithm_call follow:
727**********************************************************************************/
728
729get_policy_algorithm_call::get_policy_algorithm_call (tf_fuzz_info *test_state,
730 long &call_ser_no, // (constructor)
731 asset_search how_asset_found)
732 : policy_call(test_state, call_ser_no,
733 how_asset_found)
734{
735 // Copy the boilerplate text into local buffers:
736 prep_code.assign ("");
737 call_code.assign (test_state->bplate->bplate_string[get_policy_algorithm]);
738 call_description = "policy algorithm-get call";
739}
740get_policy_algorithm_call::~get_policy_algorithm_call (void)
741{
742 return; // just to have something to pin a breakpoint onto
743}
744
745bool get_policy_algorithm_call::copy_call_to_asset (void)
746{
747 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
748}
749
750void get_policy_algorithm_call::fill_in_prep_code (void)
751{
752 string var_name = asset_info.get_name() + "_algo";
753 vector<variable_info>::iterator assign_variable;
754
755 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
756 assign_variable = test_state->find_var (var_name);
757 if (assign_variable == test_state->variable.end()) {
758 // No such variable exists, so:
759 test_state->make_var (var_name);
760 prep_code.append (test_state->bplate->bplate_string[declare_policy_algorithm]);
761 find_replace_all ("$var", var_name, prep_code);
762 }
763}
764
765void get_policy_algorithm_call::fill_in_command (void)
766{
767 // (call_code already loaded by constructor)
768 find_replace_all ("$policy", asset_info.get_name(), call_code);
769 find_replace_1st ("$algorithm", asset_info.get_name() + "_algo", call_code);
770 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100771 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800772}
773
774/**********************************************************************************
775 End of methods of class get_policy_algorithm_call.
776**********************************************************************************/
777
778
779/**********************************************************************************
780 Methods of class get_policy_usage_call follow:
781**********************************************************************************/
782
783get_policy_usage_call::get_policy_usage_call (tf_fuzz_info *test_state,
784 long &call_ser_no, // (constructor)
785 asset_search how_asset_found)
786 : policy_call(test_state, call_ser_no,
787 how_asset_found)
788{
789 // Copy the boilerplate text into local buffers:
790 prep_code.assign ("");
791 call_code.assign (test_state->bplate->bplate_string[get_policy_usage]);
792 call_description = "policy usage-get call";
793}
794get_policy_usage_call::~get_policy_usage_call (void)
795{
796 return; // just to have something to pin a breakpoint onto
797}
798
799bool get_policy_usage_call::copy_call_to_asset (void)
800{
801 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
802}
803
804void get_policy_usage_call::fill_in_prep_code (void)
805{
806 string var_name = asset_info.get_name() + "_usage";
807 vector<variable_info>::iterator assign_variable;
808
809 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
810 assign_variable = test_state->find_var (var_name);
811 if (assign_variable == test_state->variable.end()) {
812 // No such variable exists, so:
813 test_state->make_var (var_name);
814 prep_code.append (test_state->bplate->bplate_string[declare_policy_usage]);
815 find_replace_all ("$var", var_name, prep_code);
816 }
817}
818
819void get_policy_usage_call::fill_in_command (void)
820{
821 // (call_code already loaded by constructor)
822 find_replace_all ("$policy", asset_info.get_name(), call_code);
823 find_replace_1st ("$usage", asset_info.get_name() + "_usage", call_code);
824 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100825 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800826}
827
828/**********************************************************************************
829 End of methods of class get_policy_usage_call.
830**********************************************************************************/
831
832
833/**********************************************************************************
834 Methods of class print_policy_usage_call follow:
835**********************************************************************************/
836
837print_policy_usage_call::print_policy_usage_call (tf_fuzz_info *test_state,
838 long &call_ser_no, // (constructor)
839 asset_search how_asset_found)
840 : policy_call(test_state, call_ser_no,
841 how_asset_found)
842{
843 // Copy the boilerplate text into local buffers:
844 prep_code.assign ("");
845 call_code.assign (test_state->bplate->bplate_string[print_policy_usage]);
846 call_description = "policy usage-print call";
847}
848print_policy_usage_call::~print_policy_usage_call (void)
849{
850 return; // just to have something to pin a breakpoint onto
851}
852
853bool print_policy_usage_call::copy_call_to_asset (void)
854{
855 return copy_call_to_asset_t<policy_asset*> (this, dont_create_asset);
856}
857
858void print_policy_usage_call::fill_in_prep_code (void)
859{
860 string var_name = asset_info.get_name() + "_usage";
861 vector<variable_info>::iterator assign_variable;
862
863 policy_fill_in_prep_code(); // make sure the policy variable itself is defined
864 // Make sure policy-usage variable is defined:
865 assign_variable = test_state->find_var (var_name);
866 if (assign_variable == test_state->variable.end()) {
867 // No such variable exists, so:
868 test_state->make_var (var_name);
869 prep_code.append (test_state->bplate->bplate_string[declare_policy_usage]);
870 find_replace_all ("$var", var_name, prep_code);
871 }
872}
873
874void print_policy_usage_call::fill_in_command (void)
875{
876 string var_name = asset_info.get_name() + "_usage";
877
878 // (call_code already loaded by constructor)
879 find_replace_all ("$policy", asset_info.get_name(), call_code);
880 find_replace_1st ("$usage_string", policy.usage_string, call_code);
881 find_replace_1st ("$usage", var_name, call_code);
882 find_replace_1st ("$print_usage_true_string", policy.print_usage_true_string,
883 call_code);
884 find_replace_1st ("$print_usage_false_string", policy.print_usage_false_string,
885 call_code);
886 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100887 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800888}
889
890/**********************************************************************************
891 End of methods of class print_policy_usage_call.
892**********************************************************************************/
893
894
895/**********************************************************************************
896 Methods of class get_key_policy_call follow:
897**********************************************************************************/
898
899get_key_policy_call::get_key_policy_call (tf_fuzz_info *test_state, // (constructor)
900 long &call_ser_no,
901 asset_search how_asset_found)
902 : policy_call(test_state, call_ser_no,
903 how_asset_found)
904{
905 // Copy the boilerplate text into local buffers:
906 prep_code.assign ("");
907 call_code.assign (test_state->bplate->bplate_string[get_policy]);
908 check_code.assign (test_state->bplate->bplate_string[get_policy_check]);
909 call_description = "policy get call";
910}
911get_key_policy_call::~get_key_policy_call (void)
912{
913 return; // just to have something to pin a breakpoint onto
914}
915
916bool get_key_policy_call::copy_call_to_asset (void)
917{
918 return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
919}
920
921void get_key_policy_call::fill_in_prep_code (void)
922{
923 // No prep code required.
924 return; // just to have something to pin a breakpoint onto
925}
926
927void get_key_policy_call::fill_in_command (void)
928{
929 // (call_code already loaded by constructor)
930 find_replace_all ("key", asset_info.get_name(), call_code);
931 find_replace_all ("$policy", policy.asset_2_name, call_code);
932 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +0100933 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +0800934}
935
936/**********************************************************************************
937 End of methods of class get_key_policy_call.
938**********************************************************************************/
939
940
941/**********************************************************************************
942 Methods of class generate_key_call follow:
943**********************************************************************************/
944
945generate_key_call::generate_key_call (tf_fuzz_info *test_state, // (constructor)
946 long &call_ser_no,
947 asset_search how_asset_found)
948 : key_call(test_state, call_ser_no,
949 how_asset_found)
950{
951 // Copy the boilerplate text into local buffers:
952 prep_code.assign ("");
953 call_code.assign (test_state->bplate->bplate_string[generate_key]);
954 check_code.assign (test_state->bplate->bplate_string[generate_key_check]);
955 call_description = "key-generate call";
956}
957generate_key_call::~generate_key_call (void)
958{
959 return; // just to have something to pin a breakpoint onto
960}
961
962bool generate_key_call::copy_call_to_asset (void)
963{
Nik Dewallyabac0e52024-08-02 13:42:27 +0100964 // we create the asset conditionally in simulate
965 return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
966}
967
968bool generate_key_call::simulate() {
969 bool is_policy_valid;
970
971 // NOTE: although algorithm and key-type depend on eachother for validity,
972 // this is checked during key operations not generation
973
974
975 if (policy.key_type == "PSA_KEY_TYPE_RSA_PUBLIC_KEY") {
976 is_policy_valid = false;
977 }
978 else if (policy.key_type == "PSA_KEY_TYPE_PEPPER") {
979 is_policy_valid = false;
980 } else {
981 key_type& kt = get_key_type(policy.key_type);
982 algorithm& alg = get_algorithm(policy.key_algorithm);
983 is_policy_valid = kt.is_valid_key_size(policy.n_bits);
984 }
985
986 psa_asset_usage asset_usage;
987
988 if (!is_policy_valid) {
989 // do not create a key when policy is invalid
990 exp_data.expect_failure();
991 return true;
992 }
993
994 // create an asset, and copy the information we want in it to it.
995 copy_call_to_asset_t<key_asset*> (this, yes_create_asset);
996 switch (asset_info.how_asset_found) {
997 case asset_search::created_new:
998 exp_data.expect_pass();
999
1000 break;
1001
1002 default: // asset already exists!
1003 exp_data.expect_failure();
1004 break;
1005 }
1006
1007 return true;
Karl Zhang3de5ab12021-05-31 11:45:48 +08001008}
1009
1010void generate_key_call::fill_in_prep_code (void)
1011{
1012 string var_name = asset_info.get_name();
1013 vector<variable_info>::iterator assign_variable;
1014
1015 // Make sure key variable is defined:
1016 assign_variable = test_state->find_var (var_name);
1017 if (assign_variable == test_state->variable.end()) {
1018 // No such variable exists, so:
1019 test_state->make_var (var_name);
1020 prep_code.append (test_state->bplate->bplate_string[declare_key]);
1021 find_replace_all ("$var", var_name, prep_code);
1022 }
1023}
1024
1025void generate_key_call::fill_in_command (void)
1026{
1027 // (call_code already loaded by constructor)
1028 find_replace_all ("$policy", policy.asset_2_name, call_code);
1029 find_replace_all ("$key", asset_info.get_name(), call_code);
1030 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +01001031 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001032}
1033
1034/**********************************************************************************
1035 End of methods of class generate_key_call.
1036**********************************************************************************/
1037
1038
1039/**********************************************************************************
1040 Methods of class create_key_call follow:
1041**********************************************************************************/
1042
Nik Dewallyed341b72024-08-20 17:02:30 +01001043import_key_call::import_key_call (tf_fuzz_info *test_state, // (constructor)
Karl Zhang3de5ab12021-05-31 11:45:48 +08001044 long &call_ser_no,
1045 asset_search how_asset_found)
1046 : key_call(test_state, call_ser_no,
1047 how_asset_found)
1048{
1049 // Copy the boilerplate text into local buffers:
1050 prep_code.assign ("");
1051 call_code.assign (test_state->bplate->bplate_string[create_key]);
1052 check_code.assign (test_state->bplate->bplate_string[create_key_check]);
1053 call_description = "key-create call";
1054}
Nik Dewallyed341b72024-08-20 17:02:30 +01001055import_key_call::~import_key_call (void)
Karl Zhang3de5ab12021-05-31 11:45:48 +08001056{
1057 return; // just to have something to pin a breakpoint onto
1058}
1059
Nik Dewallyabac0e52024-08-02 13:42:27 +01001060
Nik Dewallyed341b72024-08-20 17:02:30 +01001061bool import_key_call::copy_call_to_asset (void)
Karl Zhang3de5ab12021-05-31 11:45:48 +08001062{
Nik Dewallyabac0e52024-08-02 13:42:27 +01001063 return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001064}
1065
Nik Dewallyed341b72024-08-20 17:02:30 +01001066
1067bool import_key_call::simulate (void) {
1068 bool is_policy_valid;
1069
1070 string data_str = set_data.get();
1071 const char* data_buf = data_str.c_str();
1072 size_t data_size = strlen(data_buf) * 8;
1073
1074 // RSA keys have a very specific binary format that is checked on import.
1075 // a random / user given string almost certainly won't be valid here.
1076 if (policy.key_type == "PSA_KEY_TYPE_RSA_KEY_PAIR") {
1077 is_policy_valid = false;
1078
1079 } else if (policy.key_type == "PSA_KEY_TYPE_PEPPER") {
1080 is_policy_valid = false;
1081
1082 } else {
1083 is_policy_valid=true;
1084 }
1085
1086 // policy.n_bits == 0 means to PSA crypto that we don't care about key size
1087 // (this is good for import)
1088 if (policy.n_bits != 0 && policy.n_bits != data_size) {
1089 is_policy_valid = false;
1090 }
1091
1092 if (!is_policy_valid) {
1093 // do not create a key when policy is invalid
1094 exp_data.expect_failure();
1095 return true;
1096 }
1097
1098 // create an asset, and copy the information we want in it to it.
1099 copy_call_to_asset_t<key_asset*> (this, yes_create_asset);
1100 switch (asset_info.how_asset_found) {
1101 case asset_search::created_new:
1102 exp_data.expect_pass();
1103
1104 break;
1105
1106 default: // asset already exists!
1107 exp_data.expect_failure();
1108 break;
1109 }
1110
1111 return true;
1112};
1113
1114void import_key_call::fill_in_prep_code (void)
Karl Zhang3de5ab12021-05-31 11:45:48 +08001115{
1116 string var_name = asset_info.get_name();
1117 vector<variable_info>::iterator assign_variable;
1118 gibberish gib;
1119 char gib_buff[500];
1120 string t_string;
1121
1122 // Key variable:
1123 assign_variable = test_state->find_var (var_name);
1124 if (assign_variable == test_state->variable.end()) {
1125 // No such variable exists, so:
1126 test_state->make_var (var_name);
1127 prep_code.append (test_state->bplate->bplate_string[declare_key]);
1128 find_replace_all ("$var", var_name, prep_code);
1129 }
Nik Dewallyed341b72024-08-20 17:02:30 +01001130
1131
Karl Zhang3de5ab12021-05-31 11:45:48 +08001132 // Key-data variable:
1133 var_name = asset_info.get_name() + "_set_data";
1134 assign_variable = test_state->find_var (var_name);
1135 if (assign_variable == test_state->variable.end()) {
1136 // No such variable exists, so:
1137 test_state->make_var (var_name);
1138 prep_code.append (test_state->bplate->bplate_string[declare_big_string]);
1139 find_replace_all ("$var", var_name, prep_code);
Nik Dewallyed341b72024-08-20 17:02:30 +01001140 t_string = set_data.get();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001141 find_replace_all ("$init", t_string, prep_code);
1142 }
1143}
1144
Nik Dewallyed341b72024-08-20 17:02:30 +01001145void import_key_call::fill_in_command (void)
Karl Zhang3de5ab12021-05-31 11:45:48 +08001146{
1147 // (call_code already loaded by constructor)
1148 find_replace_all ("$policy", policy.asset_2_name, call_code);
1149 find_replace_all ("$data", asset_info.get_name() + "_set_data", call_code);
Nik Dewallyed341b72024-08-20 17:02:30 +01001150
1151 find_replace_all ("$length", to_string(strlen(set_data.get().c_str())), call_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001152 find_replace_all ("$key", asset_info.get_name(), call_code);
1153 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +01001154 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001155}
1156
1157/**********************************************************************************
1158 End of methods of class create_key_call.
1159**********************************************************************************/
1160
1161
1162/**********************************************************************************
1163 Methods of class copy_key_call follow:
1164**********************************************************************************/
1165
1166copy_key_call::copy_key_call (tf_fuzz_info *test_state, // (constructor)
1167 long &call_ser_no,
1168 asset_search how_asset_found)
1169 : key_call(test_state, call_ser_no,
1170 how_asset_found)
1171{
1172 // Copy the boilerplate text into local buffers:
1173 prep_code.assign ("");
1174 call_code.assign (test_state->bplate->bplate_string[copy_key]);
1175 check_code.assign (test_state->bplate->bplate_string[copy_key_check]);
1176 call_description = "key-copy call";
1177}
1178copy_key_call::~copy_key_call (void)
1179{
1180 return; // just to have something to pin a breakpoint onto
1181}
1182
1183bool copy_key_call::copy_call_to_asset (void)
1184{
1185 return copy_call_to_asset_t<key_asset*> (this, yes_create_asset);
1186}
1187
1188void copy_key_call::fill_in_prep_code (void)
1189{
1190 string var_name = asset_info.get_name();
1191 vector<variable_info>::iterator assign_variable;
1192
1193 // Make sure key variable is defined:
1194 assign_variable = test_state->find_var (var_name);
1195 if (assign_variable == test_state->variable.end()) {
1196 // No such variable exists, so:
1197 test_state->make_var (var_name);
1198 prep_code.append (test_state->bplate->bplate_string[declare_key]);
1199 find_replace_all ("$var", var_name, prep_code);
1200 }
1201}
1202
1203void copy_key_call::fill_in_command (void)
1204{
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001205 // (call_code already loaded by constructor)
1206 find_replace_all ("$master", policy.asset_3_name, call_code);
1207 find_replace_all ("$policy", policy.asset_2_name, call_code);
1208 find_replace_all ("$copy", asset_info.get_name(), call_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001209
Nik Dewally6663dde2024-08-09 16:12:27 +01001210 // TODO:: move error code modelling code to simulate().
1211
Karl Zhang3de5ab12021-05-31 11:45:48 +08001212 // Calculate the expected results:
1213 asset_search find_result;
1214 vector<psa_asset*>::iterator asset;
1215 long dummy = 0L;
1216 // See if the source key does not exist:
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001217 find_result = test_state->
1218 find_or_create_key_asset (psa_asset_search::name, psa_asset_usage::active,
1219 policy.asset_3_name, (uint64_t) 0, dummy,
1220 dont_create_asset, asset);
Nik Dewally6663dde2024-08-09 16:12:27 +01001221
1222
Karl Zhang3de5ab12021-05-31 11:45:48 +08001223 if (find_result != asset_search::found_active) {
Nik Dewally6663dde2024-08-09 16:12:27 +01001224 exp_data.expect_error_code("PSA_ERROR_INVALID_ARGUMENT");
Karl Zhang3de5ab12021-05-31 11:45:48 +08001225 } else {
1226 // See if the new policy does not exist:
1227 find_result = test_state->
1228 find_or_create_policy_asset (psa_asset_search::name, psa_asset_usage::active,
1229 policy.asset_2_name, (uint64_t) 0, dummy,
1230 dont_create_asset, asset);
1231 if (find_result != asset_search::found_active) {
Nik Dewally6663dde2024-08-09 16:12:27 +01001232 exp_data.expect_error_code("PSA_ERROR_INVALID_ARGUMENT");
1233
Karl Zhang3de5ab12021-05-31 11:45:48 +08001234 } else if (!(*asset)->policy.copyable) {
1235 // See if the source key does not support export:
1236 // TODO: Or wait, it's the original policy for the key, right?
Nik Dewally6663dde2024-08-09 16:12:27 +01001237 exp_data.expect_error_code("PSA_ERROR_NOT_PERMITTED");
Karl Zhang3de5ab12021-05-31 11:45:48 +08001238 }
1239 }
Nik Dewally6663dde2024-08-09 16:12:27 +01001240 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001241}
1242
1243/**********************************************************************************
1244 End of methods of class copy_key_call.
1245**********************************************************************************/
1246
1247
1248/**********************************************************************************
1249 Methods of class read_key_data_call follow:
1250**********************************************************************************/
1251
1252read_key_data_call::read_key_data_call (tf_fuzz_info *test_state, // (constructor)
1253 long &call_ser_no,
1254 asset_search how_asset_found)
1255 : key_call(test_state, call_ser_no,
1256 how_asset_found)
1257{
1258 // Copy the boilerplate text into local buffers:
1259 prep_code.assign ("");
1260 call_code.assign (test_state->bplate->bplate_string[read_key_data]);
1261 check_code.assign (test_state->bplate->bplate_string[read_key_data_check]);
1262 call_description = "key read-data call";
1263}
1264read_key_data_call::~read_key_data_call (void)
1265{
1266 return; // just to have something to pin a breakpoint onto
1267}
1268
1269bool read_key_data_call::copy_call_to_asset (void)
1270{
1271 return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
1272}
1273
Nik Dewallyed341b72024-08-20 17:02:30 +01001274bool read_key_data_call::simulate() {
1275
1276 copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
1277 switch (asset_info.how_asset_found) {
1278 case asset_search::found_active:
1279 exp_data.expect_pass();
1280
1281 // Can always export public keys but all other keys need USAGE_EXPORT.
1282 if (!policy.exportable && policy.key_type != "PSA_KEY_TYPE_RSA_PUBLIC_KEY") {
1283 exp_data.expect_error_code("PSA_ERROR_NOT_PERMITTED");
1284 }
1285 break;
1286
1287 case asset_search::found_deleted:
1288 case asset_search::found_invalid:
1289 case asset_search::not_found:
1290 exp_data.expect_error_code("PSA_ERROR_INVALID_HANDLE");
1291 break;
1292
1293 // should never happen in this case
1294 case asset_search::unsuccessful:
1295 case asset_search::created_new:
1296 case asset_search::something_wrong:
1297 exp_data.expect_failure();
1298 break;
1299 }
1300
1301 return true;
1302}
1303
Karl Zhang3de5ab12021-05-31 11:45:48 +08001304void read_key_data_call::fill_in_prep_code (void)
1305{
1306 string var_name, length_var_name, actual_length_var_name, var_name_suffix,
1307 length_var_name_suffix, temp_string;
1308 vector<variable_info>::iterator expect_variable;
1309 vector<variable_info>::iterator assign_variable;
1310
1311 if (exp_data.data_var_specified) {
1312 var_name.assign (exp_data.data_var + "_data");
1313 length_var_name.assign (exp_data.data_var + "_length");
1314 /* If actual-data variable doesn't already exist, create variable tracker,
1315 and write declaration for it: */
1316 expect_variable = test_state->find_var (exp_data.data_var);
1317 if (expect_variable == test_state->variable.end()) {
1318 // No such variable exists, so:
1319 test_state->make_var (exp_data.data_var);
1320 expect_variable = test_state->find_var (exp_data.data_var);
1321 prep_code.append (test_state->bplate->bplate_string[declare_big_string]);
1322 find_replace_1st ("$var", var_name, prep_code);
1323 temp_string = (char *) expect_variable->value;
1324 find_replace_1st ("$init", temp_string, prep_code);
1325 // Input data length:
1326 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
1327 find_replace_1st ("$var", length_var_name, prep_code);
1328 find_replace_1st ("$init", to_string(temp_string.length()), prep_code);
1329 // TODO: Is these lengths in bits or bytes?
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001330 // Actual (output) data length:
1331 actual_length_var_name.assign (exp_data.data_var + "_act_length");
1332 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
1333 find_replace_1st ("$var", actual_length_var_name, prep_code);
1334 find_replace_1st ("$init", to_string(temp_string.length()), prep_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001335 }
1336 }
Karl Zhang3de5ab12021-05-31 11:45:48 +08001337 if (assign_data_var_specified) {
1338 var_name.assign (assign_data_var + "_data");
1339 length_var_name.assign (assign_data_var + "_length");
1340 actual_length_var_name.assign (assign_data_var + "_act_length");
1341 /* If actual-data variable doesn't already exist, create variable tracker,
1342 and write declaration for it: */
1343 assign_variable = test_state->find_var (assign_data_var);
1344 if (assign_variable == test_state->variable.end()) {
1345 // No such variable exists, so:
1346 test_state->make_var (assign_data_var);
1347 assign_variable = test_state->find_var (assign_data_var);
1348 prep_code.append (test_state->bplate->bplate_string[declare_big_string]);
1349 find_replace_1st ("$var", var_name, prep_code);
1350 temp_string = (char *) assign_variable->value;
1351 find_replace_1st ("$init", temp_string, prep_code);
1352 // Input data length:
1353 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
1354 find_replace_1st ("$var", length_var_name, prep_code);
1355 find_replace_1st ("$init", to_string(temp_string.length()), prep_code);
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001356 // Actual (output) data length:
1357 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
1358 find_replace_1st ("$var", actual_length_var_name, prep_code);
1359 find_replace_1st ("$init", to_string(temp_string.length()), prep_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001360 }
1361 } else {
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001362 // Single string of two lines declaring string data and its length:
1363 var_name_suffix = "_set_data";
1364 length_var_name_suffix = "_set_length";
1365 if (set_data.n_set_vars > 0) {
1366 var_name_suffix += "_" + to_string(set_data.n_set_vars);
1367 length_var_name_suffix += "_" + to_string(set_data.n_set_vars);
1368 }
1369 var_name.assign (asset_info.get_name() + var_name_suffix);
1370 length_var_name.assign (asset_info.get_name() + length_var_name_suffix);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001371 prep_code.append (test_state->bplate->bplate_string[declare_string]);
1372 find_replace_1st ("$var", var_name, prep_code);
1373 find_replace_1st ("$init", set_data.get(), prep_code);
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001374 temp_string.assign (test_state->bplate->bplate_string[declare_int]);
1375 find_replace_1st ("static int", "static uint32_t", temp_string);
1376 prep_code.append (temp_string);
1377 find_replace_1st ("$var", length_var_name, prep_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001378 find_replace_1st ("$init", to_string(set_data.get().length()), prep_code);
1379 }
Karl Zhang3de5ab12021-05-31 11:45:48 +08001380}
1381
1382void read_key_data_call::fill_in_command (void)
1383{
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001384 string var_name, length_var_name, actual_length_var_name, var_name_suffix,
1385 length_var_name_suffix, temp_string;
Karl Zhang3de5ab12021-05-31 11:45:48 +08001386
1387 // Fill in the PSA command itself:
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001388 if (exp_data.data_var_specified) {
1389 var_name.assign (exp_data.data_var + "_data");
1390 actual_length_var_name.assign (exp_data.data_var + "_act_length");
1391 } else {
1392 actual_length_var_name.assign (assign_data_var + "_act_length");
1393 }
Karl Zhang3de5ab12021-05-31 11:45:48 +08001394 if (assign_data_var_specified) {
1395 var_name.assign (assign_data_var + "_data");
1396 length_var_name.assign (assign_data_var + "_length");
1397 } else {
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001398 var_name_suffix = "_set_data";
1399 if (set_data.n_set_vars > 0) {
1400 var_name_suffix += "_" + to_string(set_data.n_set_vars);
1401 }
Karl Zhang3de5ab12021-05-31 11:45:48 +08001402 var_name.assign (asset_info.get_name() + var_name_suffix);
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001403 length_var_name_suffix = "_set_length";
1404 if (set_data.n_set_vars > 0) {
1405 length_var_name_suffix += "_" + to_string(set_data.n_set_vars);
1406 }
Karl Zhang3de5ab12021-05-31 11:45:48 +08001407 length_var_name.assign (asset_info.get_name() + length_var_name_suffix);
1408 }
1409 find_replace_1st ("$data", var_name, call_code);
1410 find_replace_1st ("$key", asset_info.get_name(), call_code);
1411 string id_string = to_string((long) asset_info.id_n++);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001412 find_replace_1st ("$act_size", actual_length_var_name, call_code);
Mate Toth-Palffba10e2021-09-22 21:38:03 +02001413 find_replace_1st ("$length", length_var_name, call_code);
Karl Zhang3de5ab12021-05-31 11:45:48 +08001414
Nik Dewally6663dde2024-08-09 16:12:27 +01001415 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001416}
1417
1418/**********************************************************************************
1419 End of methods of class read_key_data_call.
1420**********************************************************************************/
1421
1422
1423/**********************************************************************************
1424 Methods of class remove_key_call follow:
1425**********************************************************************************/
1426
1427remove_key_call::remove_key_call (tf_fuzz_info *test_state, // (constructor)
1428 long &call_ser_no,
1429 asset_search how_asset_found)
1430 : key_call(test_state, call_ser_no,
1431 how_asset_found)
1432{
1433 // Copy the boilerplate text into local buffers:
1434 prep_code.assign ("");
1435 call_code.assign (test_state->bplate->bplate_string[remove_key]);
1436 check_code.assign (test_state->bplate->bplate_string[remove_key_check]);
1437 call_description = "key-remove call";
1438}
1439remove_key_call::~remove_key_call (void)
1440{
1441 return; // just to have something to pin a breakpoint onto
1442}
1443
Nik Dewallyabac0e52024-08-02 13:42:27 +01001444bool remove_key_call::simulate () {
1445 if (!exp_data.simulation_needed()) {
1446 return false;
1447 }
1448 switch (asset_info.how_asset_found) {
1449 case asset_search::found_active:
1450 case asset_search::created_new:
1451 exp_data.expect_pass();
1452 return true;
1453
1454 // if the asset never existed, its handle will be null.
1455 // deleting the null handle succeeds
1456 case asset_search::not_found:
1457 exp_data.expect_pass();
1458 return true;
1459
1460 case asset_search::found_deleted:
1461 exp_data.expect_failure();
1462 return true;
1463 default:
1464 exp_data.expect_failure();
1465 return true;
1466 }
1467}
Karl Zhang3de5ab12021-05-31 11:45:48 +08001468bool remove_key_call::copy_call_to_asset (void)
1469{
1470 return copy_call_to_asset_t<key_asset*> (this, dont_create_asset);
1471}
1472
1473void remove_key_call::fill_in_prep_code (void)
1474{
1475 // No prep code required.
1476 return; // just to have something to pin a breakpoint onto
1477}
1478
1479void remove_key_call::fill_in_command (void)
1480{
1481 // (call_code already loaded by constructor)
1482 find_replace_all ("$key", asset_info.get_name(), call_code);
1483 // Calculate the expected results:
Nik Dewally6663dde2024-08-09 16:12:27 +01001484 fill_in_result_code();
Karl Zhang3de5ab12021-05-31 11:45:48 +08001485}
1486
1487/**********************************************************************************
1488 End of methods of class remove_key_call.
1489**********************************************************************************/