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_pbkdf_test_common.h"
17
18 #include <gtest/gtest.h>
19
20 using namespace testing::ext;
21 namespace Unittest::Pbkdf2Derive
22 {
HksPbkdf2DeriveTestNormalCase(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 HksPbkdf2DeriveTestNormalCase(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
HksPbkdf2DeriveTestCmpCase(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 HksPbkdf2DeriveTestCmpCase(const struct OH_Huks_Blob keyAlias,
56 const struct OH_Huks_ParamSet *genParamSet,
57 struct OH_Huks_ParamSet *deriveParamSet,
58 struct OH_Huks_ParamSet *deriveFinalParamsSet)
59 {
60 struct OH_Huks_Blob inData = {g_inData.length(), (uint8_t *)g_inData.c_str()};
61
62 /* 1. Generate Key */
63 // Generate Key
64 OH_Huks_Result ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
65 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
66
67 /* 2. Derive Three Stage */
68 // Init
69 uint8_t handleD[sizeof(uint64_t)] = {0};
70 struct OH_Huks_Blob handleDerive = {sizeof(uint64_t), handleD};
71 ret = OH_Huks_InitSession(&keyAlias, deriveParamSet, &handleDerive, nullptr);
72 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
73 // Update
74 uint8_t tmpOut[COMMON_SIZE] = {0};
75 struct OH_Huks_Blob outData = {COMMON_SIZE, tmpOut};
76 ret = OH_Huks_UpdateSession(&handleDerive, deriveParamSet, &inData, &outData);
77 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Update failed.";
78 // Finish
79 uint8_t outDataD[COMMON_SIZE] = {0};
80 struct OH_Huks_Blob outDataDerive = {COMMON_SIZE, outDataD};
81 ret = OH_Huks_FinishSession(&handleDerive, deriveFinalParamsSet, &inData, &outDataDerive);
82 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Finish failed.";
83
84 return ret;
85 }
86 } // namespace Unittest::Pbkdf2Derive