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 {
HksHkdfDeriveTestNormalCase(const struct OH_Huks_Blob keyAlias,const struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * deriveParamSet,struct OH_Huks_ParamSet * deriveFinalParamsSet)22 OH_Huks_Result HksHkdfDeriveTestNormalCase(const struct OH_Huks_Blob keyAlias,
23 const struct OH_Huks_ParamSet *genParamSet, struct OH_Huks_ParamSet *deriveParamSet, struct OH_Huks_ParamSet *deriveFinalParamsSet)
24 {
25 struct OH_Huks_Blob inData = {
26 g_inData.length(),
27 (uint8_t *)g_inData.c_str()
28 };
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,
56 const struct OH_Huks_ParamSet *genParamSet, struct OH_Huks_ParamSet *deriveParamSet, struct OH_Huks_ParamSet *deriveFinalParamsSet)
57 {
58 struct OH_Huks_Blob inData = {
59 g_inData.length(),
60 (uint8_t *)g_inData.c_str()
61 };
62
63 /* 1. Generate Key */
64 // Generate Key
65 OH_Huks_Result ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
66 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
67
68 /* 2. Derive Three Stage */
69 // Init
70 uint8_t handleD[sizeof(uint64_t)] = {0};
71 struct OH_Huks_Blob handleDerive = { sizeof(uint64_t), handleD };
72 ret = OH_Huks_InitSession(&keyAlias, deriveParamSet, &handleDerive, nullptr);
73 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
74 // Update
75 uint8_t tmpOut[COMMON_SIZE] = {0};
76 struct OH_Huks_Blob outData = { COMMON_SIZE, tmpOut };
77 ret = OH_Huks_UpdateSession(&handleDerive, deriveParamSet, &inData, &outData);
78 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Update failed.";
79 // Finish
80 uint8_t outDataD[COMMON_SIZE] = {0};
81 struct OH_Huks_Blob outDataDerive = { COMMON_SIZE, outDataD };
82 ret = OH_Huks_FinishSession(&handleDerive, deriveFinalParamsSet, &inData, &outDataDerive);
83 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Finish failed.";
84
85 return ret;
86 }
87 }
88