• 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 "hks_hkdf_derive_test_common.h"
17 
18 #include <gtest/gtest.h>
19 
HksHkdfDeriveTestNormalCase(const struct HksBlob keyAlias,const struct HksParamSet * genParamSet,struct HksParamSet * deriveParamSet,struct HksParamSet * deriveFinalParamsSet)20 int32_t Unittest::HkdfDerive::HksHkdfDeriveTestNormalCase(const struct HksBlob keyAlias,
21     const struct HksParamSet *genParamSet, struct HksParamSet *deriveParamSet, struct HksParamSet *deriveFinalParamsSet)
22 {
23     struct HksBlob inData = {
24         Unittest::HkdfDerive::g_inData.length(),
25         (uint8_t *)Unittest::HkdfDerive::g_inData.c_str()
26     };
27     int32_t ret = HKS_FAILURE;
28 
29     /* 1. Generate Key */
30     // Generate Key
31     ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
32     EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
33 
34     /* 2. Derive Three Stage */
35     // Init
36     uint8_t handleD[sizeof(uint64_t)] = {0};
37     struct HksBlob handleDerive = { sizeof(uint64_t), handleD };
38     ret = HksInit(&keyAlias, deriveParamSet, &handleDerive);
39     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
40     // Update
41     uint8_t tmpOut[Unittest::HkdfDerive::COMMON_SIZE] = {0};
42     struct HksBlob outData = { Unittest::HkdfDerive::COMMON_SIZE, tmpOut };
43     ret = HksUpdate(&handleDerive, deriveParamSet, &inData, &outData);
44     EXPECT_EQ(ret, HKS_SUCCESS) << "Update failed.";
45     // Finish
46     uint8_t outDataD[Unittest::HkdfDerive::COMMON_SIZE] = {0};
47     struct HksBlob outDataDerive = { Unittest::HkdfDerive::COMMON_SIZE, outDataD };
48     ret = HksFinish(&handleDerive, deriveFinalParamsSet, &inData, &outDataDerive);
49     EXPECT_EQ(ret, HKS_SUCCESS) << "Finish failed.";
50 
51     return ret;
52 }
53 
HksHkdfDeriveTestCmpCase(const struct HksBlob keyAlias,const struct HksParamSet * genParamSet,struct HksParamSet * deriveParamSet,struct HksParamSet * deriveFinalParamsSet)54 int32_t Unittest::HkdfDerive::HksHkdfDeriveTestCmpCase(const struct HksBlob keyAlias,
55     const struct HksParamSet *genParamSet, struct HksParamSet *deriveParamSet, struct HksParamSet *deriveFinalParamsSet)
56 {
57     struct HksBlob inData = {
58         Unittest::HkdfDerive::g_inData.length(),
59         (uint8_t *)Unittest::HkdfDerive::g_inData.c_str()
60     };
61     int32_t ret = HKS_FAILURE;
62 
63     /* 1. Generate Key */
64     // Generate Key
65     ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
66     EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
67 
68     /* 2. Derive Three Stage */
69     // Init
70     uint8_t handleD[sizeof(uint64_t)] = {0};
71     struct HksBlob handleDerive = { sizeof(uint64_t), handleD };
72     ret = HksInit(&keyAlias, deriveParamSet, &handleDerive);
73     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
74     // Update
75     uint8_t tmpOut[Unittest::HkdfDerive::COMMON_SIZE] = {0};
76     struct HksBlob outData = { Unittest::HkdfDerive::COMMON_SIZE, tmpOut };
77     ret = HksUpdate(&handleDerive, deriveParamSet, &inData, &outData);
78     EXPECT_EQ(ret, HKS_SUCCESS) << "Update failed.";
79     // Finish
80     uint8_t outDataD[Unittest::HkdfDerive::COMMON_SIZE] = {0};
81     struct HksBlob outDataDerive = { Unittest::HkdfDerive::COMMON_SIZE, outDataD };
82     ret = HksFinish(&handleDerive, deriveFinalParamsSet, &inData, &outDataDerive);
83     EXPECT_EQ(ret, HKS_SUCCESS) << "Finish failed.";
84 
85     uint8_t tmpDerive[Unittest::HkdfDerive::COMMON_SIZE] = {0};
86     struct HksBlob derivedKey = { Unittest::HkdfDerive::COMMON_SIZE, tmpDerive };
87     ret = HksDeriveKey(deriveParamSet, &keyAlias, &derivedKey);
88     EXPECT_EQ(ret, HKS_SUCCESS) << "HksDeriveKey failed.";
89 
90     ret = HksMemCmp(derivedKey.data, outDataDerive.data, outDataDerive.size);
91     EXPECT_EQ(ret, HKS_SUCCESS) << "outDataDerive not equals derivedKey.";
92 
93     return ret;
94 }