Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* boilerplate.*pp contains class(es) to read and store customizable code |
| 9 | fragments used in realizing the pieces of a test. It's critical that |
| 10 | these be customizable in external library files, so that TF-Fuzz can |
| 11 | exercise TF-A as well as TF-M. Each TF-x has its own library of text |
| 12 | pieces, which are read into these data structures upon program initial- |
| 13 | ization. */ |
| 14 | |
| 15 | #ifndef BOILERPLATE_HPP |
| 16 | #define BOILERPLATE_HPP |
| 17 | |
| 18 | #include <iostream> |
| 19 | #include <fstream> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | // Constant mnemonics for entries in array of boilerplate texts: |
| 27 | const int |
| 28 | // Stuff around the test itself: |
| 29 | preamble_A = 0, // setup stuff before PSA calls begin |
| 30 | /* Strings to be substituted from template: |
| 31 | $purpose: The purpose of the test */ |
| 32 | hashing_code = 1, // code to perform a simple hash of asset data |
| 33 | /* Strings to be substituted from template: (none) */ |
| 34 | preamble_B = 2, // setup stuff before PSA calls begin |
| 35 | /* Strings to be substituted from template: |
| 36 | $purpose: The purpose of the test */ |
| 37 | preamble_C = 3, // setup stuff before PSA calls begin |
| 38 | /* Strings to be substituted from template: |
| 39 | $purpose: The purpose of the test */ |
| 40 | declare_int = 4, // declaration for an int |
| 41 | /* Strings to be substituted from template: |
| 42 | $var: The integer thing to declare |
| 43 | $init: Its initialization */ |
| 44 | declare_string = 5, // declaration for a string |
| 45 | /* Strings to be substituted from template: |
| 46 | $var: The string variable to declare |
| 47 | $init: Its initialization */ |
| 48 | declare_big_string = 6, // declaration for a string, sized large |
| 49 | /* Strings to be substituted from template: |
| 50 | $var: The string variable to declare |
| 51 | $init: Its initialization */ |
| 52 | declare_size_t = 7, // declaration for a size_t |
| 53 | /* Strings to be substituted from template: |
| 54 | $var: The integer thing to declare |
| 55 | $init: Its initialization */ |
| 56 | declare_policy = 8, // declaration for a policy (key attributes) |
| 57 | /* Strings to be substituted from template: |
| 58 | $var: The variable to declare */ |
| 59 | declare_policy_algorithm = 9, // declaration for a |
| 60 | /* Strings to be substituted from template: |
| 61 | $var: The variable to declare */ |
| 62 | declare_policy_lifetime = 10, // declaration for a |
| 63 | /* Strings to be substituted from template: |
| 64 | $var: The variable to declare */ |
| 65 | declare_policy_type = 11, // declaration for a |
| 66 | /* Strings to be substituted from template: |
| 67 | $var: The variable to declare */ |
| 68 | declare_policy_usage = 12, // declaration for a |
| 69 | /* Strings to be substituted from template: |
| 70 | $var: The variable to declare */ |
| 71 | declare_key = 13, // declaration for a |
| 72 | /* Strings to be substituted from template: |
| 73 | $var: The variable to declare */ |
| 74 | declare_generic = 14, // declaration for a some other type |
| 75 | /* Strings to be substituted from template: |
| 76 | $type: The type to declare the variable to |
| 77 | $var: The string variable to declare |
| 78 | $init: Its initialization */ |
| 79 | test_log = 15, // print a message to the test log |
| 80 | /* Strings to be substituted from template: |
| 81 | $message: What to print. */ |
| 82 | teardown_sst = 16, // call to delete SST resources after test completes |
| 83 | /* Strings to be substituted from template: |
| 84 | $uid: (Exactly that) */ |
| 85 | teardown_sst_check = 17, // boilerplate code to check success of previous; |
| 86 | /* No strings to be substituted from template */ |
| 87 | teardown_key = 18, // call to delete crypto keys after test completes |
| 88 | /* Strings to be substituted from template: |
| 89 | $handle: The handle to the key */ |
| 90 | teardown_key_check = 19, // boilerplate code to check success of previous; |
| 91 | /* No strings to be substituted from template */ |
| 92 | closeout = 20, // final code to close out the test. |
| 93 | // No strings to substitute. |
| 94 | sst_pass_string = 21, // passing SST expected result |
| 95 | sst_fail_removed = 22, // SST expected result from having deleted the asset |
| 96 | // SST calls: |
| 97 | set_sst_call = 23, // boilerplate for setting up an SST asset; |
| 98 | /* Strings to be substituted from template: |
| 99 | $op: (for comment) either "Creating" or "Resetting" |
| 100 | $description: its "human name" if given, or "UID=..." if not |
| 101 | $data_source: either first ~10 chars of data or file path |
| 102 | $uid: (Exactly that) |
| 103 | $length: Data length |
| 104 | $data: Pointer to data (C string) |
| 105 | $flags: SST-asset creation flags */ |
| 106 | set_sst_check = 24, // boilerplate code to check success of previous; |
| 107 | /* Strings to be substituted from template: |
| 108 | $expect: Expected return status string */ |
| 109 | get_sst_call = 25, // boilerplate for retrieving data from an SST asset; |
| 110 | /* Strings to be substituted from template: |
| 111 | $uid: (Exactly that) |
| 112 | $offset: Start positon in the "file" TO DO: ADD OFFSET |
| 113 | $length: Data length |
| 114 | $act_data: Pointer to actual data (C string) */ |
| 115 | get_sst_check = 26, // boilerplate code to check call result only; |
| 116 | /* Strings to be substituted from template: |
| 117 | $expect: Expected return status string */ |
| 118 | get_sst_check_all = 27, // boilerplate code to check call result and data; |
| 119 | /* Strings to be substituted from template: |
| 120 | $expect: Expected return status |
| 121 | $exp_data: Expected read data |
| 122 | $act_data: Actual read data |
| 123 | $length: Data length */ |
| 124 | get_sst_hash = 28, // boilerplate code to invoke hasher; |
| 125 | /* Strings to be substituted from template: |
| 126 | $act_data_var: Actual read data, to be hashed |
| 127 | $hash_var: Hash-result variable */ |
| 128 | remove_sst = 29, // boilerplate for removing an SST asset; |
| 129 | /* Strings to be substituted from template: |
| 130 | $uid: (Exactly that) */ |
| 131 | remove_sst_check = 30, // boilerplate code to check success of previous; |
| 132 | /* Strings to be substituted from template: |
| 133 | $expect: Expected return status */ |
| 134 | // Crypto-key-policy calls: |
| 135 | init_policy = 31, // initialize a policy (key attributes); |
| 136 | /* Strings to be substituted from template: |
| 137 | $policy: The name of the policy */ |
| 138 | reset_policy = 32, // reset a policy (key attributes); |
| 139 | /* Strings to be substituted from template: |
| 140 | $policy: The name of the policy */ |
| 141 | add_policy_usage = 33, // add a usage flag to a policy; |
| 142 | /* Strings to be substituted from template: |
| 143 | $policy: The name of the policy |
| 144 | $flag: usage-flag name */ |
| 145 | set_policy_lifetime = 34, |
| 146 | /* Strings to be substituted from template: |
| 147 | $policy: The name of the policy |
| 148 | $life: Lifetime -- volatile or persistent */ |
| 149 | set_policy_size = 35, |
| 150 | /* Strings to be substituted from template: |
| 151 | $policy: The name of the policy |
| 152 | $size: The key size, in bits */ |
| 153 | set_policy_type = 36, |
| 154 | /* Strings to be substituted from template: |
| 155 | $policy: The name of the policy |
| 156 | $type: The key type -- RSA pair, AES, etc. */ |
| 157 | set_policy_algorithm = 37, |
| 158 | /* Strings to be substituted from template: |
| 159 | $policy: The name of the policy |
| 160 | $algorithm: The algorithm -- AEAD, sign, hash mask, etc. */ |
| 161 | set_policy_usage = 38, |
| 162 | /* Strings to be substituted from template: |
| 163 | $policy: The name of the policy |
| 164 | $usage: The usage -- encrypt, decrypt, export, copy, etc. */ |
| 165 | get_policy_lifetime = 39, |
| 166 | /* Strings to be substituted from template: |
| 167 | $policy: The name of the policy |
| 168 | $life: Lifetime -- volatile or persistent */ |
| 169 | get_policy_lifetime_print = 40, |
| 170 | /* Strings to be substituted from template: |
| 171 | $policy: The name of the policy |
| 172 | $life: Lifetime -- volatile or persistent */ |
| 173 | get_policy_size = 41, |
| 174 | /* Strings to be substituted from template: |
| 175 | $policy: The name of the policy |
| 176 | $size: The key size, in bits */ |
| 177 | get_policy_type = 42, |
| 178 | /* Strings to be substituted from template: |
| 179 | $policy: The name of the policy |
| 180 | $type: The key type -- RSA pair, AES, etc. */ |
| 181 | get_policy_type_print = 43, |
| 182 | /* Strings to be substituted from template: |
| 183 | $policy: The name of the policy |
| 184 | $type: The key type -- RSA pair, AES, etc. */ |
| 185 | get_policy_algorithm = 44, |
| 186 | /* Strings to be substituted from template: |
| 187 | $policy: The name of the policy |
| 188 | $algorithm: The algorithm -- AEAD, sign, hash mask, etc. */ |
| 189 | get_policy_algorithm_print = 45, |
| 190 | /* Strings to be substituted from template: |
| 191 | $policy: The name of the policy |
| 192 | $algorithm: The algorithm -- AEAD, sign, hash mask, etc. */ |
| 193 | get_policy_usage = 46, |
| 194 | /* Strings to be substituted from template: |
| 195 | $policy: The name of the policy |
| 196 | $usage: The usage -- encrypt, decrypt, export, copy, etc. */ |
| 197 | print_policy_usage = 47, |
| 198 | /* Strings to be substituted from template: |
| 199 | $policy: The name of the policy |
| 200 | $usage: The usage -- encrypt, decrypt, export, copy, etc. */ |
| 201 | get_policy = 48, // read the policy from a key; |
| 202 | /* Strings to be substituted from template: |
| 203 | $key: The key whose policy we want to read |
| 204 | $policy: The policy variable to read into */ |
| 205 | get_policy_check = 49, // success check fpr previous; |
| 206 | /* Strings to be substituted from template: |
| 207 | $expect: Expected return status */ |
| 208 | // Crypto-key-related calls: |
| 209 | generate_key = 50, // generate key from scratch; |
| 210 | /* Strings to be substituted from template: |
| 211 | $key: The key to generate |
| 212 | $policy: The name of the policy */ |
| 213 | generate_key_check = 51, // success check for previous; |
| 214 | /* Strings to be substituted from template: |
| 215 | $expect: Expected return status */ |
| 216 | create_key = 52, // Create key from policy and key data; |
| 217 | /* Strings to be substituted from template: |
| 218 | $policy: The name of the policy |
| 219 | $data: The key data |
| 220 | $length: The key-data length -- sizeof() */ |
| 221 | create_key_check = 53, // success check for previous; |
| 222 | /* Strings to be substituted from template: |
| 223 | $expect: Expected return status */ |
| 224 | copy_key = 54, // Copy key data from one key to another; different policy; |
| 225 | /* Strings to be substituted from template: |
| 226 | $master: The source key-handle variable |
| 227 | $policy: The name of the policy |
| 228 | $copy: The copy key-handle variable */ |
| 229 | copy_key_check = 55, // success check for previous; |
| 230 | /* Strings to be substituted from template: |
| 231 | $expect: Expected return status */ |
| 232 | compare_data = 56, // compare data blocks, such as key data |
| 233 | /* Strings to be substituted from template: |
| 234 | $exp_data: Expected read data |
| 235 | $act_data: Actual read data |
| 236 | $length: Data length */ |
| 237 | read_key_data = 57, // read key data; |
| 238 | /* Strings to be substituted from template: |
| 239 | $key: The key to read |
| 240 | $data: Where to put the data |
| 241 | $length: Length of the data buffer |
| 242 | $act_size: The actual size of the key data retrieved */ |
| 243 | read_key_data_check = 58, // success check for previous; |
| 244 | /* Strings to be substituted from template: |
| 245 | $expect: Expected return status */ |
| 246 | remove_key = 59, // boilerplate for creating a key; |
| 247 | /* Strings to be substituted from template: |
| 248 | $handle: The handle to the key */ |
| 249 | remove_key_check = 60, // boilerplate code to check success of previous; |
| 250 | /* Strings to be substituted from template: |
| 251 | $expect: Expected return status */ |
| 252 | // Just an anchor at the end, for sizing the string array (if needed): |
| 253 | n_boilerplate_texts = 61; |
| 254 | |
| 255 | |
| 256 | class boilerplate |
| 257 | { |
| 258 | public: // (More pain than it's worth to create getters and setters) |
| 259 | // Data members: |
| 260 | vector<string> bplate_string; |
| 261 | string cpp_string; // used to stuff bplate_string |
| 262 | // Methods: |
| 263 | void stuff_boilerplate_strings (void); |
| 264 | boilerplate (void); |
| 265 | }; |
| 266 | |
| 267 | |
| 268 | #endif // #ifndef BOILERPLATE_HPP |