1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HKS_TEST_COMMON_H 17 #define HKS_TEST_COMMON_H 18 19 #include "hks_type.h" 20 #include "securec.h" 21 22 #define GOTO_ERROR_IF_FAIL(ret, err) \ 23 if ((ret) != 0) { \ 24 goto err; \ 25 } 26 27 #define GOTO_ERROR_IF_SUCCESS(ret, err) \ 28 if ((ret) == 0) { \ 29 goto err; \ 30 } 31 32 #define HKS_SLICE_MAX_LEN 64 * 1024 33 34 #define DEFAULT_TEST_CASE_ID 0xFFFFFFFF 35 #define DEFAULT_EXECUTION_TIMES 0xFFFFFFFF 36 #define DEFAULT_PERFORM_TIMES 0xFFFFFFFF 37 38 #define MAX_ARGS_LEN 20 39 #define DEFAULT_KEY_ALIAS_SIZE 64 40 #define DEFAULT_SLICE_SIZE (2 * 128 + 1) 41 42 #define DEFAULT_PARAM_SET_OUT_SIZE 4096 43 #define HKS_MAX_RANDOM_LEN 1024 44 #define MAX_OUT_BLOB_SIZE (5 * 1024 * 1024) 45 46 #define TEST_MAX_FILE_NAME_LEN 512 47 48 #define DEFAULT_LOCAL_KEY_SIZE 256 49 50 /* inputparams: struct HksBlob *blob */ 51 struct HksTestBlobParams { 52 bool blobExist; /* blob null or not */ 53 uint32_t blobSize; /* blob->size value */ 54 bool blobDataExist; /* blob->data null or not */ 55 uint32_t blobDataSize; /* blob->data malloc size */ 56 }; 57 58 /* inputparams: struct HksParamSet *paramSet */ 59 struct HksTestGenKeyParamsParamSet { 60 bool paramSetExist; /* paramSet null or not */ 61 bool setAlg; 62 uint32_t alg; 63 bool setKeySize; 64 uint32_t keySize; 65 bool setPurpose; 66 uint32_t purpose; 67 bool setDigest; 68 uint32_t digest; 69 bool setPadding; 70 uint32_t padding; 71 bool setBlockMode; 72 uint32_t mode; 73 bool setKeyStorageFlag; 74 uint32_t keyStorageFlag; 75 }; 76 77 struct HksTestGenKeyParamsParamSetOut { 78 bool paramSetExist; /* paramSet null or not */ 79 uint32_t paramSetSize; 80 }; 81 82 enum HksTestCipherType { 83 HKS_TEST_CIPHER_TYPE_AES, 84 HKS_TEST_CIPHER_TYPE_RSA, 85 }; 86 87 struct TestRsaCipherParamSet { 88 struct HksParamSet **outParamSet; 89 bool paramSetExist; 90 bool setAlg; 91 uint32_t alg; 92 bool setPurpose; 93 uint32_t purpose; 94 bool setDigest; 95 uint32_t digest; 96 bool setPadding; 97 uint32_t padding; 98 }; 99 100 struct HksTestCipherParamsParamSet { 101 enum HksTestCipherType cipherType; 102 bool paramSetExist; /* paramSet null or not */ 103 bool setAlg; 104 uint32_t alg; 105 bool setPurpose; 106 uint32_t purpose; 107 bool setDigest; 108 uint32_t digest; 109 bool setPadding; 110 uint32_t padding; 111 bool setBlockMode; 112 uint32_t mode; 113 bool setIv; 114 uint32_t ivSize; 115 bool setNonce; 116 uint32_t nonceSize; 117 bool setAad; 118 uint32_t aadSize; 119 bool setIsKeyAlias; 120 bool isKeyAlias; 121 }; 122 123 struct HksTestMacParamSet { 124 bool paramSetExist; /* paramSet null or not */ 125 bool setPurpose; 126 uint32_t purpose; 127 bool setDigest; 128 uint32_t digest; 129 }; 130 131 struct HksTestDeriveParamSet { 132 bool paramSetExist; /* paramSet null or not */ 133 bool setAlg; 134 uint32_t alg; 135 bool setPurpose; 136 uint32_t purpose; 137 bool setDigest; 138 uint32_t digest; 139 bool setIteration; 140 uint32_t iteration; 141 bool setSalt; 142 uint32_t saltSize; 143 bool setInfo; 144 uint32_t infoSize; 145 bool setIsKeyAlias; 146 bool isKeyAlias; 147 }; 148 149 struct HksTestAgreeParamSet { 150 bool paramSetExist; /* paramSet null or not */ 151 bool setAlg; 152 uint32_t alg; 153 bool setKeySize; 154 uint32_t keySize; 155 bool setIsKeyAlias; 156 bool isKeyAlias; 157 }; 158 159 enum HksTestMacType { 160 HKS_TEST_MAC_TYPE_REE, 161 HKS_TEST_MAC_TYPE_TEE, 162 }; 163 164 struct HksTestHashParamsParamSet { 165 bool paramSetExist; /* paramSet null or not */ 166 bool setDigest; 167 uint32_t digest; 168 }; 169 170 struct HksTestParamSetOut { 171 bool paramSetExist; 172 uint32_t paramSetSize; 173 uint32_t realParamSetSize; 174 }; 175 176 struct HksTestGenKeyParams { 177 uint32_t testId; 178 int32_t expectResult; 179 struct HksTestBlobParams keyAliasParams; 180 struct HksTestGenKeyParamsParamSet paramSetParams; 181 struct HksTestGenKeyParamsParamSetOut paramSetParamsOut; 182 }; 183 184 struct HksTestCipherParams { 185 uint32_t testId; 186 int32_t expectResult; 187 struct HksTestBlobParams keyAliasParams; 188 struct HksTestGenKeyParamsParamSet genKeyParamSetParams; 189 struct HksTestGenKeyParamsParamSetOut genKeyParamSetParamsOut; 190 struct HksTestCipherParamsParamSet encryptParamSetParams; 191 struct HksTestCipherParamsParamSet decryptParamSetParams; 192 struct HksTestBlobParams plainTextParams; 193 struct HksTestBlobParams cipherTextParams; 194 struct HksTestBlobParams decryptedTextParams; 195 struct HksTestBlobParams localKeyParams; 196 }; 197 198 struct HksTestEncryptParams { 199 uint32_t testId; 200 int32_t expectResult; 201 struct HksTestBlobParams keyAliasParams; 202 struct HksTestGenKeyParamsParamSet genKeyParamSetParams; 203 struct HksTestBlobParams encryptAliasParams; 204 struct HksTestCipherParamsParamSet encryptParamSetParams; 205 struct HksTestBlobParams inDataParams; 206 struct HksTestBlobParams outDataParams; 207 struct HksTestBlobParams localKeyParams; 208 }; 209 210 struct HksTestDecryptParams { 211 uint32_t testId; 212 int32_t expectResult; 213 struct HksTestBlobParams keyAliasParams; 214 struct HksTestGenKeyParamsParamSet genKeyParamSetParams; 215 struct HksTestBlobParams decryptAliasParams; 216 struct HksTestCipherParamsParamSet decryptParamSetParams; 217 struct HksTestBlobParams inDataParams; 218 struct HksTestBlobParams outDataParams; 219 struct HksTestBlobParams localKeyParams; 220 }; 221 222 struct HksTestMacParams { 223 uint32_t testId; 224 int32_t expectResult; 225 enum HksTestMacType macType; 226 struct HksTestBlobParams keyAliasParams; 227 struct HksTestGenKeyParamsParamSet genKeyParamSetParams; 228 struct HksTestBlobParams keyParams; 229 struct HksTestMacParamSet macParamSetParams; 230 struct HksTestBlobParams srcDataParams; 231 struct HksTestBlobParams macParams; 232 }; 233 234 struct HksTestDeriveParams { 235 uint32_t testId; 236 int32_t expectResult; 237 struct HksTestBlobParams keyAliasParams; 238 struct HksTestGenKeyParamsParamSet genKeyParamSetParams; 239 struct HksTestBlobParams masterKeyParams; 240 struct HksTestDeriveParamSet deriveParamSetParams; 241 struct HksTestBlobParams derivedKeyParams; 242 struct HksTestBlobParams localKeyParams; 243 }; 244 245 struct HksTestKeyExistParams { 246 uint32_t testId; 247 int32_t expectResult; 248 bool isGenKey; 249 struct HksTestBlobParams keyAliasParams; 250 }; 251 252 struct HksTestGetKeyParamSetParams { 253 uint32_t testId; 254 int32_t expectResult; 255 bool isGenKey; 256 struct HksTestBlobParams keyAliasParams; 257 struct HksTestParamSetOut paramSetOutParams; 258 }; 259 260 struct HksTestGenRandomParams { 261 uint32_t testId; 262 int32_t expectResult; 263 struct HksTestBlobParams randomParams; 264 }; 265 266 struct HksTestHashParams { 267 uint32_t testId; 268 int32_t expectResult; 269 struct HksTestHashParamsParamSet paramSetParams; 270 struct HksTestBlobParams srcDataParams; 271 struct HksTestBlobParams hashParams; 272 }; 273 274 struct HksTestBnExpModParams { 275 uint32_t testId; 276 int32_t expectResult; 277 bool isTestValue; 278 struct HksTestBlobParams xParams; 279 struct HksTestBlobParams aParams; 280 struct HksTestBlobParams eParams; 281 struct HksTestBlobParams nParams; 282 }; 283 284 struct HksTestAgreeParams { 285 uint32_t testId; 286 int32_t expectResult; 287 struct HksTestBlobParams keyAlias1Params; 288 struct HksTestBlobParams keyAlias2Params; 289 struct HksTestGenKeyParamsParamSet genKeyParamSetParams; 290 struct HksTestBlobParams privateKeyParams; 291 struct HksTestAgreeParamSet agreeParamSetParams; 292 struct HksTestBlobParams peerPublicParams; 293 struct HksTestBlobParams agreedKeyParams; 294 struct HksTestBlobParams localPrivateKeyParams; 295 struct HksTestBlobParams localPublicKeyParams; 296 }; 297 298 struct TestAgreeParamSetStructure { 299 struct HksParamSet **outParamSet; 300 bool paramSetExist; 301 bool setAlg; 302 uint32_t alg; 303 bool setKeySize; 304 uint32_t keySize; 305 bool setIsKeyAlias; 306 bool isKeyAlias; 307 }; 308 309 struct GenerateKeyParamSetStructure { 310 struct HksParamSet **outParamSet; 311 bool paramSetExist; 312 bool setAlg; 313 uint32_t alg; 314 bool setKeySize; 315 uint32_t keySize; 316 bool setPurpose; 317 uint32_t purpose; 318 bool setDigest; 319 uint32_t digest; 320 bool setPadding; 321 uint32_t padding; 322 bool setBlockMode; 323 uint32_t mode; 324 bool setKeyStorageFlag; 325 uint32_t keyStorageFlag; 326 }; 327 328 struct AesCipherParamSetStructure { 329 struct HksParamSet **outParamSet; 330 bool paramSetExist; 331 bool setAlg; 332 uint32_t alg; 333 bool setPurpose; 334 uint32_t purpose; 335 bool setPadding; 336 uint32_t padding; 337 bool setBlockMode; 338 uint32_t mode; 339 bool setIv; 340 const struct HksBlob *ivBlob; 341 bool setNonce; 342 const struct HksBlob *nonceBlob; 343 bool setAad; 344 const struct HksBlob *aadBlob; 345 bool setIsKeyAlias; 346 bool isKeyAlias; 347 }; 348 349 struct TestMacParamSetStructure { 350 struct HksParamSet **outParamSet; 351 bool paramSetExist; 352 bool setPurpose; 353 uint32_t purpose; 354 bool setDigest; 355 uint32_t digest; 356 bool setKeyAlias; 357 bool isKeyAlias; 358 }; 359 360 struct TestDeriveParamSetStructure { 361 struct HksParamSet **outParamSet; 362 bool paramSetExist; 363 bool setAlg; 364 uint32_t alg; 365 bool setPurpose; 366 uint32_t purpose; 367 bool setDigest; 368 uint32_t digest; 369 bool setIteration; 370 uint32_t iteration; 371 bool setSalt; 372 const struct HksBlob *saltBlob; 373 bool setInfo; 374 const struct HksBlob *infoBlob; 375 bool setIsKeyAlias; 376 bool isKeyAlias; 377 }; 378 379 #ifdef __cplusplus 380 extern "C" { 381 #endif 382 383 void TestFreeBlob(struct HksBlob **key); 384 385 int32_t TestConstuctBlob(struct HksBlob **blob, 386 bool blobExist, 387 uint32_t blobSize, 388 bool blobDataExist, 389 uint32_t realBlobDataSize); 390 391 int32_t TestConstructGenerateKeyParamSetOut( 392 struct HksParamSet **outParamSet, 393 bool paramSetExist, uint32_t paramSetSize); 394 395 int32_t TestConstructGenerateKeyParamSet(struct GenerateKeyParamSetStructure *paramStruct); 396 397 int32_t TestConstructRsaCipherParamSet(struct TestRsaCipherParamSet *paramStruct); 398 399 int32_t TestConstructAesCipherParamSet(struct AesCipherParamSetStructure *paramStruct); 400 401 int32_t TestConstructMacParamSet(struct TestMacParamSetStructure *paramStruct); 402 403 int32_t TestConstructAgreeParamSet(struct TestAgreeParamSetStructure *paramStruct); 404 405 int32_t GenerateLocalX25519Key(struct HksBlob **privateKey, struct HksBlob **publicKey, 406 const struct HksTestBlobParams *localPrivateKeyParams, const struct HksTestBlobParams *localPublicKeyParams); 407 408 int32_t TestConstructDeriveParamSet(struct TestDeriveParamSetStructure *paramStruct); 409 410 int32_t TestConstructHashParamSet( 411 struct HksParamSet **outParamSet, 412 bool paramSetExist, 413 bool setDigest, uint32_t digest); 414 415 int32_t HuksGenerateKey(struct HksBlob **keyAlias, const struct HksTestBlobParams *keyAliasParams, 416 const struct HksTestGenKeyParamsParamSet *genKeyParamSetParams, 417 const struct HksTestGenKeyParamsParamSetOut *genKeyParamSetParamsOut); 418 419 int32_t GenerateLocalRandomKey(struct HksBlob **keyAlias, const struct HksTestBlobParams *localKeyParams); 420 421 int32_t TestConstructBlobOut(struct HksBlob **blob, 422 bool blobExist, 423 uint32_t blobSize, 424 bool blobDataExist, 425 uint32_t realBlobDataSize); 426 427 int32_t TestGenDefaultKeyAndGetAlias(struct HksBlob **keyAlias); 428 429 #ifdef __cplusplus 430 } 431 #endif 432 433 #endif /* HKS_TEST_COMMON_H */ 434