• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "huks_signverify_ecc_test_common.h"
17 
18 #include <gtest/gtest.h>
19 
20 using namespace testing::ext;
21 namespace Unittest::EccSifnVerify
22 {
HksTestSignVerify(struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData,bool isSign)23 OH_Huks_Result HksTestSignVerify(struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *paramSet,
24                                  const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData, bool isSign)
25 {
26     uint8_t tmpHandle[sizeof(uint64_t)] = {0};
27     struct OH_Huks_Blob handle = {sizeof(uint64_t), tmpHandle};
28     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, paramSet, &handle, nullptr);
29     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
30     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
31         return ret;
32     }
33 
34     struct OH_Huks_Param *tmpParam = NULL;
35     ret = OH_Huks_GetParam(paramSet, OH_HUKS_TAG_PURPOSE, &tmpParam);
36     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
37         return ret;
38     }
39 
40     ret = TestUpdateFinish(&handle, paramSet, tmpParam->uint32Param, inData, outData);
41     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateFinish failed.";
42     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
43         return ret;
44     }
45     return ret;
46 }
47 
HksEccSignVerifyTestNormalCase(struct OH_Huks_Blob keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * signParamSet,struct OH_Huks_ParamSet * verifyParamSet)48 OH_Huks_Result HksEccSignVerifyTestNormalCase(struct OH_Huks_Blob keyAlias, struct OH_Huks_ParamSet *genParamSet,
49                                               struct OH_Huks_ParamSet *signParamSet,
50                                               struct OH_Huks_ParamSet *verifyParamSet)
51 {
52     struct OH_Huks_Blob inData = {g_inData.length(), (uint8_t *)g_inData.c_str()};
53 
54     /* 1. Generate Key */
55     // Generate Key
56     OH_Huks_Result ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
57     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
58 
59     /* 2. Sign Three Stage */
60     uint8_t outDataS[ECC_COMMON_SIZE] = {0};
61     struct OH_Huks_Blob outDataSign = {ECC_COMMON_SIZE, outDataS};
62     ret = HksTestSignVerify(&keyAlias, signParamSet, &inData, &outDataSign, true);
63     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Sign failed.";
64 
65     /* 3. Export Public Key */
66     uint8_t pubKey[OH_HUKS_ECC_KEY_SIZE_521] = {0};
67     struct OH_Huks_Blob publicKey = {OH_HUKS_ECC_KEY_SIZE_521, pubKey};
68     ret = OH_Huks_ExportPublicKeyItem(&keyAlias, genParamSet, &publicKey);
69     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportPublicKey failed.";
70 
71     /* 4. Import Key */
72     char newKey[] = "ECC_Sign_Verify_Import_KeyAlias";
73     struct OH_Huks_Blob newKeyAlias = {.size = strlen(newKey), .data = (uint8_t *)newKey};
74     ret = OH_Huks_ImportKeyItem(&newKeyAlias, verifyParamSet, &publicKey);
75     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ImportKey failed";
76 
77     /* 5. Verify Three Stage */
78     ret = HksTestSignVerify(&newKeyAlias, verifyParamSet, &inData, &outDataSign, false);
79     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Verify failed.";
80 
81     /* 6. Delete New Key */
82     ret = OH_Huks_DeleteKeyItem(&newKeyAlias, verifyParamSet);
83     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete ImportKey failed.";
84 
85     return ret;
86 }
87 }  // namespace Unittest::EccSifnVerify