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 #include <hctest.h>
17 #include "hi_watchdog.h"
18 
19 #include "hks_derive_test_c.h"
20 #include "hks_test_adapt_for_de.h"
21 
22 /*
23  * @tc.register: register a test suit named "CalcMultiTest"
24  * @param: test subsystem name
25  * @param: c_example module name
26  * @param: CalcMultiTest test suit name
27  */
28 LITE_TEST_SUIT(husk, huks_lite, HksDeriveTest);
29 
30 /**
31  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
32  * @return: true——setup success
33  */
HksDeriveTestSetUp()34 static BOOL HksDeriveTestSetUp()
35 {
36     LiteTestPrint("setup\n");
37     hi_watchdog_disable();
38     TEST_ASSERT_TRUE(HksInitialize() == 0);
39     return TRUE;
40 }
41 
42 /**
43  * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
44  * @return: true——teardown success
45  */
HksDeriveTestTearDown()46 static BOOL HksDeriveTestTearDown()
47 {
48     LiteTestPrint("tearDown\n");
49     hi_watchdog_enable();
50     return TRUE;
51 }
52 
DeriveKey(const struct HksTestDeriveParamSet * deriveParamSetParams,const struct HksBlob * masterKey,struct HksBlob * derivedKey,struct HksBlob ** saltData,struct HksBlob ** infoData)53 static int32_t DeriveKey(const struct HksTestDeriveParamSet *deriveParamSetParams, const struct HksBlob *masterKey,
54     struct HksBlob *derivedKey, struct HksBlob **saltData, struct HksBlob **infoData)
55 {
56     struct HksParamSet *deriveParamSet = NULL;
57     uint32_t infoSize = deriveParamSetParams->infoSize;
58     uint32_t saltSize = deriveParamSetParams->saltSize;
59     if (saltSize != 0) {
60         HKS_TEST_ASSERT(TestConstuctBlob(saltData, true, saltSize, true, saltSize) == 0);
61     }
62     if (infoSize != 0) {
63         HKS_TEST_ASSERT(TestConstuctBlob(infoData, true, infoSize, true, infoSize) == 0);
64     }
65     struct TestDeriveParamSetStructure paramStruct = {
66         &deriveParamSet, deriveParamSetParams->paramSetExist,
67         deriveParamSetParams->setAlg, deriveParamSetParams->alg,
68         deriveParamSetParams->setPurpose, deriveParamSetParams->purpose,
69         deriveParamSetParams->setDigest, deriveParamSetParams->digest,
70         deriveParamSetParams->setIteration, deriveParamSetParams->iteration,
71         deriveParamSetParams->setSalt, *saltData,
72         deriveParamSetParams->setInfo, *infoData,
73         deriveParamSetParams->setIsKeyAlias,
74         deriveParamSetParams->isKeyAlias
75     };
76     int32_t ret = TestConstructDeriveParamSet(&paramStruct);
77     HKS_TEST_ASSERT(ret == 0);
78 
79     ret = HksDeriveKeyRun(deriveParamSet, masterKey, derivedKey, 1);
80     HksFreeParamSet(&deriveParamSet);
81     return ret;
82 }
83 
BaseTestDerive(uint32_t index)84 static int32_t BaseTestDerive(uint32_t index)
85 {
86     /* 1. generate key */
87     struct HksBlob *keyAlias = NULL;
88     int32_t ret;
89     if (g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag &&
90         (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
91         ret = GenerateLocalRandomKey(&keyAlias,
92             &g_testDeriveParams[index].localKeyParams);
93     } else {
94         if (g_testDeriveParams[index].keyAliasParams.blobExist) {
95             ret = GenerateKey(&keyAlias,
96                 &g_testDeriveParams[index].keyAliasParams,
97                 &g_testDeriveParams[index].genKeyParamSetParams, NULL);
98         } else {
99             ret = TestConstuctBlob(&keyAlias,
100                 g_testDeriveParams[index].masterKeyParams.blobExist,
101                 g_testDeriveParams[index].masterKeyParams.blobSize,
102                 g_testDeriveParams[index].masterKeyParams.blobDataExist,
103                 g_testDeriveParams[index].masterKeyParams.blobDataSize);
104         }
105     }
106     TEST_ASSERT_TRUE(ret == 0);
107 
108     /* 2. derive */
109     struct HksBlob *derivedKey = NULL;
110     ret = TestConstuctBlob(&derivedKey,
111         g_testDeriveParams[index].derivedKeyParams.blobExist,
112         g_testDeriveParams[index].derivedKeyParams.blobSize,
113         g_testDeriveParams[index].derivedKeyParams.blobDataExist,
114         g_testDeriveParams[index].derivedKeyParams.blobDataSize);
115     TEST_ASSERT_TRUE(ret == 0);
116 
117     struct HksBlob *saltData = NULL;
118     struct HksBlob *infoData = NULL;
119     ret = DeriveKey(&g_testDeriveParams[index].deriveParamSetParams, keyAlias, derivedKey, &saltData, &infoData);
120     if (ret != g_testDeriveParams[index].expectResult) {
121         HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testDeriveParams[index].testId, ret);
122     }
123     TEST_ASSERT_TRUE(ret == g_testDeriveParams[index].expectResult);
124 
125     /* 3. delete key */
126     if (!(g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag &&
127         (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) &&
128         (g_testDeriveParams[index].keyAliasParams.blobExist)) {
129         TEST_ASSERT_TRUE(HksDeleteKeyForDe(keyAlias, NULL) == 0);
130     }
131     TestFreeBlob(&keyAlias);
132     TestFreeBlob(&derivedKey);
133     TestFreeBlob(&saltData);
134     TestFreeBlob(&infoData);
135 
136     return ret;
137 }
138 
139 #ifndef _CUT_AUTHENTICATE_
140 /**
141  * @tc.name: HksDeriveTest.HksDeriveTest001
142  * @tc.desc: The static function will return true;
143  * @tc.type: FUNC
144  */
LITE_TEST_CASE(HksDeriveTest,HksDeriveTest001,Level1)145 LITE_TEST_CASE(HksDeriveTest, HksDeriveTest001, Level1)
146 {
147     int32_t ret = BaseTestDerive(0);
148     TEST_ASSERT_TRUE(ret == 0);
149 }
150 #endif /* _CUT_AUTHENTICATE_ */
151 
152 /**
153  * @tc.name: HksDeriveTest.HksDeriveTest002
154  * @tc.desc: The static function will return true;
155  * @tc.type: FUNC
156  */
LITE_TEST_CASE(HksDeriveTest,HksDeriveTest002,Level1)157 LITE_TEST_CASE(HksDeriveTest, HksDeriveTest002, Level1)
158 {
159     int32_t ret = BaseTestDerive(1);
160     TEST_ASSERT_TRUE(ret == 0);
161 }
162 RUN_TEST_SUITE(HksDeriveTest);
163