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_derive_hkdf_test_common.h"
17
18 #include <gtest/gtest.h>
19
20 using namespace testing::ext;
21 namespace Unittest::HkdfDerive
22 {
HksHkdfDeriveTestNormalCase(const struct OH_Huks_Blob keyAlias,const struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * deriveParamSet,struct OH_Huks_ParamSet * deriveFinalParamsSet)23 OH_Huks_Result HksHkdfDeriveTestNormalCase(const struct OH_Huks_Blob keyAlias,
24 const struct OH_Huks_ParamSet *genParamSet,
25 struct OH_Huks_ParamSet *deriveParamSet,
26 struct OH_Huks_ParamSet *deriveFinalParamsSet)
27 {
28 struct OH_Huks_Blob inData = {g_inData.length(), (uint8_t *)g_inData.c_str()};
29
30 /* 1. Generate Key */
31 // Generate Key
32 OH_Huks_Result ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
33 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
34
35 /* 2. Derive Three Stage */
36 // Init
37 uint8_t handleD[sizeof(uint64_t)] = {0};
38 struct OH_Huks_Blob handleDerive = {sizeof(uint64_t), handleD};
39 ret = OH_Huks_InitSession(&keyAlias, deriveParamSet, &handleDerive, nullptr);
40 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
41 // Update
42 uint8_t tmpOut[COMMON_SIZE] = {0};
43 struct OH_Huks_Blob outData = {COMMON_SIZE, tmpOut};
44 ret = OH_Huks_UpdateSession(&handleDerive, deriveParamSet, &inData, &outData);
45 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Update failed.";
46 // Finish
47 uint8_t outDataD[COMMON_SIZE] = {0};
48 struct OH_Huks_Blob outDataDerive = {COMMON_SIZE, outDataD};
49 ret = OH_Huks_FinishSession(&handleDerive, deriveFinalParamsSet, &inData, &outDataDerive);
50 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Finish failed.";
51
52 return ret;
53 }
54
HksHkdfDeriveTestCmpCase(const struct OH_Huks_Blob keyAlias,const struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * deriveParamSet,struct OH_Huks_ParamSet * deriveFinalParamsSet)55 OH_Huks_Result HksHkdfDeriveTestCmpCase(const struct OH_Huks_Blob keyAlias, const struct OH_Huks_ParamSet *genParamSet,
56 struct OH_Huks_ParamSet *deriveParamSet,
57 struct OH_Huks_ParamSet *deriveFinalParamsSet)
58 {
59 struct OH_Huks_Blob inData = {g_inData.length(), (uint8_t *)g_inData.c_str()};
60
61 /* 1. Generate Key */
62 // Generate Key
63 OH_Huks_Result ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
64 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
65
66 /* 2. Derive Three Stage */
67 // Init
68 uint8_t handleD[sizeof(uint64_t)] = {0};
69 struct OH_Huks_Blob handleDerive = {sizeof(uint64_t), handleD};
70 ret = OH_Huks_InitSession(&keyAlias, deriveParamSet, &handleDerive, nullptr);
71 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
72 // Update
73 uint8_t tmpOut[COMMON_SIZE] = {0};
74 struct OH_Huks_Blob outData = {COMMON_SIZE, tmpOut};
75 ret = OH_Huks_UpdateSession(&handleDerive, deriveParamSet, &inData, &outData);
76 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Update failed.";
77 // Finish
78 uint8_t outDataD[COMMON_SIZE] = {0};
79 struct OH_Huks_Blob outDataDerive = {COMMON_SIZE, outDataD};
80 ret = OH_Huks_FinishSession(&handleDerive, deriveFinalParamsSet, &inData, &outDataDerive);
81 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Finish failed.";
82
83 return ret;
84 }
85 } // namespace Unittest::HkdfDerive
86