• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 
18 #include "hks_derive_test.h"
19 
20 #include "hks_api.h"
21 #include "hks_param.h"
22 #include "hks_test_api_performance.h"
23 #include "hks_test_common.h"
24 #include "hks_test_log.h"
25 
26 using namespace testing::ext;
27 namespace {
28 class HksDeriveTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31 
32     static void TearDownTestCase(void);
33 
34     void SetUp();
35 
36     void TearDown();
37 };
38 
SetUpTestCase(void)39 void HksDeriveTest::SetUpTestCase(void)
40 {
41 }
42 
TearDownTestCase(void)43 void HksDeriveTest::TearDownTestCase(void)
44 {
45 }
46 
SetUp()47 void HksDeriveTest::SetUp()
48 {
49     EXPECT_EQ(HksInitialize(), 0);
50 }
51 
TearDown()52 void HksDeriveTest::TearDown()
53 {
54 }
55 
56 const int DEFAULT_DERIVE_SIZE = 32;
57 const int DEFAULT_INFO_SIZE = 55;
58 const int DEFAULT_SALT_SIZE = 16;
59 
60 const struct HksTestDeriveParams g_testDeriveParams[] = {
61     /* hkdf-sha256-salt-info */
62     { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
63         {
64             true, /* genKey params */
65             true, HKS_ALG_AES,
66             true, HKS_AES_KEY_SIZE_256,
67             true, HKS_KEY_PURPOSE_DERIVE,
68             true, HKS_DIGEST_SHA256,
69             false, 0,
70             false, 0,
71             false, 0 },
72         { 0 },
73         {
74             true, /* derive params */
75             true, HKS_ALG_HKDF,
76             true, HKS_KEY_PURPOSE_DERIVE,
77             true, HKS_DIGEST_SHA256,
78             false, 0,
79             true, DEFAULT_SALT_SIZE,
80             true, DEFAULT_INFO_SIZE,
81             false, true },
82         {
83             true, DEFAULT_DERIVE_SIZE, true, DEFAULT_DERIVE_SIZE },
84         {
85             false, 0, false, 0 }
86     },
87 
88     /* local: hkdf-sha256-salt-info */
89     { 1, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
90         {
91             true, /* genKey params */
92             true, HKS_ALG_AES,
93             true, HKS_AES_KEY_SIZE_256,
94             true, HKS_KEY_PURPOSE_DERIVE,
95             true, HKS_DIGEST_SHA256,
96             false, 0,
97             false, 0,
98             true, HKS_STORAGE_TEMP },
99         { 0 },
100         {
101             true, /* derive params */
102             true, HKS_ALG_HKDF,
103             true, HKS_KEY_PURPOSE_DERIVE,
104             true, HKS_DIGEST_SHA256,
105             false, 0,
106             true, DEFAULT_SALT_SIZE,
107             true, DEFAULT_INFO_SIZE,
108             true, false },
109         {
110             true, DEFAULT_DERIVE_SIZE, true, DEFAULT_DERIVE_SIZE },
111         {
112             true, DEFAULT_LOCAL_KEY_SIZE, true, DEFAULT_LOCAL_KEY_SIZE }
113     },
114 };
115 
DeriveKey(const struct HksTestDeriveParamSet * deriveParamSetParams,const struct HksBlob * masterKey,struct HksBlob * derivedKey,struct HksBlob ** saltData,struct HksBlob ** infoData)116 static int32_t DeriveKey(const struct HksTestDeriveParamSet *deriveParamSetParams, const struct HksBlob *masterKey,
117     struct HksBlob *derivedKey, struct HksBlob **saltData, struct HksBlob **infoData)
118 {
119     struct HksParamSet *deriveParamSet = NULL;
120     uint32_t saltSize = deriveParamSetParams->saltSize;
121     uint32_t infoSize = deriveParamSetParams->infoSize;
122     if (saltSize != 0) {
123         HKS_TEST_ASSERT(TestConstuctBlob(saltData, true, saltSize, true, saltSize) == 0);
124     }
125     if (infoSize != 0) {
126         HKS_TEST_ASSERT(TestConstuctBlob(infoData, true, infoSize, true, infoSize) == 0);
127     }
128     struct TestDeriveParamSetStructure paramStruct = {
129         &deriveParamSet,
130         deriveParamSetParams->paramSetExist,
131         deriveParamSetParams->setAlg, deriveParamSetParams->alg,
132         deriveParamSetParams->setPurpose, deriveParamSetParams->purpose,
133         deriveParamSetParams->setDigest, deriveParamSetParams->digest,
134         deriveParamSetParams->setIteration, deriveParamSetParams->iteration,
135         deriveParamSetParams->setSalt, *saltData,
136         deriveParamSetParams->setInfo, *infoData,
137         deriveParamSetParams->setIsKeyAlias, deriveParamSetParams->isKeyAlias
138     };
139     int32_t ret = TestConstructDeriveParamSet(&paramStruct);
140     HKS_TEST_ASSERT(ret == 0);
141 
142     ret = HksDeriveKeyRun(deriveParamSet, masterKey, derivedKey, 1);
143     HksFreeParamSet(&deriveParamSet);
144     return ret;
145 }
146 
BaseTestDerive(uint32_t index)147 static int32_t BaseTestDerive(uint32_t index)
148 {
149     /* 1. generate key */
150     struct HksBlob *keyAlias = NULL;
151     int32_t ret;
152     if (g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag &&
153         (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
154         ret = GenerateLocalRandomKey(&keyAlias, &g_testDeriveParams[index].localKeyParams);
155     } else {
156         if (g_testDeriveParams[index].keyAliasParams.blobExist) {
157             ret = HuksGenerateKey(&keyAlias, &g_testDeriveParams[index].keyAliasParams,
158                 &g_testDeriveParams[index].genKeyParamSetParams, NULL);
159         } else {
160             ret = TestConstuctBlob(&keyAlias,
161                 g_testDeriveParams[index].masterKeyParams.blobExist,
162                 g_testDeriveParams[index].masterKeyParams.blobSize,
163                 g_testDeriveParams[index].masterKeyParams.blobDataExist,
164                 g_testDeriveParams[index].masterKeyParams.blobDataSize);
165         }
166     }
167     HKS_TEST_ASSERT(ret == 0);
168 
169     /* 2. derive */
170     struct HksBlob *derivedKey = NULL;
171     ret = TestConstuctBlob(&derivedKey,
172         g_testDeriveParams[index].derivedKeyParams.blobExist,
173         g_testDeriveParams[index].derivedKeyParams.blobSize,
174         g_testDeriveParams[index].derivedKeyParams.blobDataExist,
175         g_testDeriveParams[index].derivedKeyParams.blobDataSize);
176     HKS_TEST_ASSERT(ret == 0);
177 
178     struct HksBlob *saltData = NULL;
179     struct HksBlob *infoData = NULL;
180     ret = DeriveKey(&g_testDeriveParams[index].deriveParamSetParams, keyAlias, derivedKey, &saltData, &infoData);
181     if (ret != g_testDeriveParams[index].expectResult) {
182         HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testDeriveParams[index].testId, ret);
183     }
184     HKS_TEST_ASSERT(ret == g_testDeriveParams[index].expectResult);
185 
186     /* 3. delete key */
187     if (!(g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag &&
188         (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) &&
189         (g_testDeriveParams[index].keyAliasParams.blobExist)) {
190         HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == 0);
191     }
192     TestFreeBlob(&keyAlias);
193     TestFreeBlob(&derivedKey);
194     TestFreeBlob(&saltData);
195     TestFreeBlob(&infoData);
196 
197     return ret;
198 }
199 
200 #ifndef _CUT_AUTHENTICATE_
201 /**
202  * @tc.name: HksDeriveTest.HksDeriveTest001
203  * @tc.desc: The static function will return true;
204  * @tc.type: FUNC
205  */
206 HWTEST_F(HksDeriveTest, HksDeriveTest001, TestSize.Level1)
207 {
208     int32_t ret = BaseTestDerive(0);
209     ASSERT_TRUE(ret == 0);
210 }
211 #endif /* _CUT_AUTHENTICATE_ */
212 
213 /**
214  * @tc.name: HksDeriveTest.HksDeriveTest002
215  * @tc.desc: The static function will return true;
216  * @tc.type: FUNC
217  */
218 HWTEST_F(HksDeriveTest, HksDeriveTest002, TestSize.Level1)
219 {
220     int32_t ret = BaseTestDerive(1);
221     ASSERT_TRUE(ret == 0);
222 }
223 }