aboutsummaryrefslogtreecommitdiff
path: root/tools/tf_fuzz/template/template_line.cpp
blob: 759948f276db8ef16754868f628ba8e622e886cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

/* Objects typed to subclasses of the these classes are constructed and filled in by
   the parser as it parses test-template lines.  There is not necessarily a one-to-one
   correspondence between the template lines and either the PSA commands generated nor
   PSA assets they manipulate.  PSA assets (which persist through the test) and PSA
   commands are therefore tracked in separate objects, but referenced here. */

#include <vector>
#include <algorithm>  // for STL find()

#include "class_forwards.hpp"

#include "data_blocks.hpp"
#include "boilerplate.hpp"
#include "randomization.hpp"
#include "gibberish.hpp"
#include "compute.hpp"
#include "psa_asset.hpp"
#include "find_or_create_asset.hpp"
#include "template_line.hpp"
#include "tf_fuzz.hpp"
#include "crypto_asset.hpp"
#include "psa_call.hpp"
#include "crypto_call.hpp"
#include "sst_asset.hpp"
#include "crypto_asset.hpp"



/**********************************************************************************
   Methods of class template_line follow:
**********************************************************************************/

// Constructor, passing in the tf_fuzz object for reference to all objects:
template_line::template_line (tf_fuzz_info *resources) : test_state(resources)
{
    set_data.file_path.assign ("");
    assign_data_var_specified = false;
    flags_string.assign ("");
    random_asset = psa_asset_usage::all;
        // if not deleting a random asset of a certain type, then search all as usual
    set_data.file_specified = false;
    print_data = hash_data = false;
    is_remove = false;  // will correct this in the remove case
}

/**********************************************************************************
   Class template_line methods regarding setting and getting asset-names follow:
**********************************************************************************/

// Default destructor:
template_line::~template_line (void)
{
    // Destruct the vectors of asset names/IDs:
    asset_info.asset_name_vector.erase (asset_info.asset_name_vector.begin(),
                                        asset_info.asset_name_vector.end());
    asset_info.asset_id_n_vector.erase (asset_info.asset_id_n_vector.begin(),
                                        asset_info.asset_id_n_vector.end());
}

// (Default constructor not used)

/**********************************************************************************
   End of methods of class template_line.
**********************************************************************************/


/**********************************************************************************
   Methods of class sst_template_line follow:
**********************************************************************************/

// (Currently, no need to specialize copy_template_to_call() for individual SST ops.)
bool sst_template_line::copy_template_to_call (void)
{
    for (auto call : test_state->calls) {
        if (call->call_ser_no == call_ser_no) {
            // Copy asset info to call object for creation code:
            call->asset_info = asset_info;
            call->set_data = set_data;
            call->set_data.string_specified =   set_data.string_specified
                                             || set_data.random_data;
                // not really right, but more convenient to combine these two cases
            call->assign_data_var_specified = assign_data_var_specified;
            call->assign_data_var = assign_data_var;
            call->random_asset = random_asset;
            call->flags_string = flags_string;
            call->exp_data = expect;
            call->exp_data.pf_info_incomplete = true;
            call->id_string = asset_name;  // data = expected
            call->print_data = print_data;
            call->hash_data = hash_data;
            return true;
        }
    }
    return false;  // somehow didn't find it the call.
}

sst_template_line::sst_template_line (tf_fuzz_info *resources)
                                          : template_line (resources)
{
    asset_info.asset_type = psa_asset_type::sst;
}

// Default destructor:
sst_template_line::~sst_template_line (void)
{
    // No real clean-up needed.
    return;  // just to have something to pin a breakpoint onto
}

// (Default constructor not used)



/**********************************************************************************
   End of methods of class sst_template_line.
**********************************************************************************/


/**********************************************************************************
   Methods of class key_template_line follow:
**********************************************************************************/

key_template_line::key_template_line (tf_fuzz_info *resources)
                                          : template_line (resources)
{
    // Note:  Similar random initialization for asset and template
    // Randomize handle:
    // TODO:  Key handles appear to be a lot more complex a question than the below
    asset_info.asset_type = psa_asset_type::key;
    string wrong_data;  // holder for random data to be overwritten
    gibberish *gib = new gibberish;
    handle_str = gib->word (false, const_cast<char*>(wrong_data.c_str()),
                            const_cast<char*>(  wrong_data.c_str())
                                              + set_data.get().length()-1);
    // Randomize key type:
    key_type = rand_key_type();
    // Randomize lifetime:
    lifetime_str = ((rand() % 2) == 1)?
                       "PSA_KEY_LIFETIME_VOLATILE" : "PSA_KEY_LIFETIME_PERSISTENT";
    // Choose a random expected key size in bits:
    expected_n_bits = to_string(rand()%10000);
}

// Create ID-based name:
string key_template_line::make_id_based_name (uint64_t id_n, string &name)
{
    string result = "Key_ID_";
    result.append(to_string(id_n));
    return result;
}

// Default destructor:
key_template_line::~key_template_line (void)
{
    // No real clean-up needed.
    return;  // just to have something to pin a breakpoint onto
}

// (Default constructor not used)



/**********************************************************************************
   End of methods of class key_template_line.
**********************************************************************************/


/**********************************************************************************
   Methods of class policy_template_line follow:
**********************************************************************************/

policy_template_line::policy_template_line (tf_fuzz_info *resources)
            : template_line (resources)
{
    asset_info.asset_type = psa_asset_type::policy;
    // Randomize key-policy usage and algorithm:
    policy_usage = rand_key_usage();
    policy_algorithm = rand_key_algorithm();
}

// Create ID-based name:
string policy_template_line::make_id_based_name (uint64_t id_n, string &name)
{
    string result = "Policy_ID_";
    result.append(to_string(id_n));
    //
    return result;
}

// Default destructor:
policy_template_line::~policy_template_line (void)
{
    // No real clean-up needed.
    return;  // just to have something to pin a breakpoint onto
}

// (Default constructor not used)



/**********************************************************************************
   End of methods of class policy_template_line.
**********************************************************************************/


/**********************************************************************************
   Methods of class security_template_line follow:
**********************************************************************************/

security_template_line::security_template_line (tf_fuzz_info *resources)
            : template_line (resources)
{
}

// Default destructor:
security_template_line::~security_template_line (void)
{
    // No real clean-up needed.
    return;  // just to have something to pin a breakpoint onto
}

// (Default constructor not used)


/**********************************************************************************
   End of methods of class security_template_line.
**********************************************************************************/