1 /*
2  * Copyright (C) 2024 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 "key_utils.h"
17 #include "ecc_key_util.h"
18 
19 #include <gtest/gtest.h>
20 #include "blob.h"
21 #include "memory.h"
22 #include "memory_mock.h"
23 #include "openssl_common.h"
24 #include "detailed_ecc_key_params.h"
25 #include "ecc_common.h"
26 #include "ecc_openssl_common_param_spec.h"
27 
28 using namespace std;
29 using namespace testing::ext;
30 
31 namespace {
32 class CryptoKeyUtilsTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp();
37     void TearDown();
38 };
39 
SetUpTestCase()40 void CryptoKeyUtilsTest::SetUpTestCase() {}
TearDownTestCase()41 void CryptoKeyUtilsTest::TearDownTestCase() {}
SetUp()42 void CryptoKeyUtilsTest::SetUp() {}
TearDown()43 void CryptoKeyUtilsTest::TearDown() {}
44 
45 static const bool IS_BIG_ENDIAN = IsBigEndian();
46 
ConstructEcc224CommParamsSpec(HcfEccCommParamsSpec ** spec)47 static HcfResult ConstructEcc224CommParamsSpec(HcfEccCommParamsSpec **spec)
48 {
49     HcfEccCommParamsSpec *eccCommSpec =
50         reinterpret_cast<HcfEccCommParamsSpec *>(HcfMalloc(sizeof(HcfEccCommParamsSpec), 0));
51     if (eccCommSpec == nullptr) {
52         return HCF_ERR_MALLOC;
53     }
54     HcfECFieldFp *tmpField = reinterpret_cast<HcfECFieldFp *>(HcfMalloc(sizeof(HcfECFieldFp), 0));
55     if (tmpField == nullptr) {
56         HcfFree(eccCommSpec);
57         return HCF_ERR_MALLOC;
58     }
59 
60     eccCommSpec->base.algName = const_cast<char *>(g_eccAlgName.c_str());
61     eccCommSpec->base.specType = HCF_COMMON_PARAMS_SPEC;
62     eccCommSpec->field = reinterpret_cast<HcfECField *>(tmpField);
63     eccCommSpec->field->fieldType = const_cast<char *>(g_eccFieldType.c_str());
64     ((HcfECFieldFp *)(eccCommSpec->field))->p.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigP : g_ecc224CorrectLittleP);
65     ((HcfECFieldFp *)(eccCommSpec->field))->p.len = NID_secp224r1_len;
66     eccCommSpec->a.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigA : g_ecc224CorrectLittleA);
67     eccCommSpec->a.len = NID_secp224r1_len;
68     eccCommSpec->b.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigB : g_ecc224CorrectLittleB);
69     eccCommSpec->b.len = NID_secp224r1_len;
70     eccCommSpec->g.x.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigGX : g_ecc224CorrectLittleGX);
71     eccCommSpec->g.x.len = NID_secp224r1_len;
72     eccCommSpec->g.y.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigGY : g_ecc224CorrectLittleGY);
73     eccCommSpec->g.y.len = NID_secp224r1_len;
74     eccCommSpec->n.data = (IS_BIG_ENDIAN ? g_ecc224CorrectBigN : g_ecc224CorrectLittleN);
75     eccCommSpec->n.len = NID_secp224r1_len;
76     eccCommSpec->h = g_ecc224CorrectH;
77 
78     *spec = eccCommSpec;
79     return HCF_SUCCESS;
80 }
81 
82 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest01, TestSize.Level0)
83 {
84     HcfResult ret = CopyAsyKeyParamsSpec(nullptr, nullptr);
85     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
86 }
87 
88 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest02, TestSize.Level0)
89 {
90     HcfEccCommParamsSpec *srcSpec = nullptr;
91     HcfResult ret = ConstructEcc224CommParamsSpec(&srcSpec);
92     EXPECT_EQ(ret, HCF_SUCCESS);
93 
94     ret = CopyPoint(&(srcSpec->g), nullptr);
95     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
96 }
97 
98 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest03, TestSize.Level0)
99 {
100     HcfEccCommParamsSpec *srcSpec = nullptr;
101     HcfResult ret = ConstructEcc224CommParamsSpec(&srcSpec);
102     EXPECT_EQ(ret, HCF_SUCCESS);
103 
104     ret = CopyEccCommonSpec(srcSpec, nullptr);
105     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
106 }
107 
108 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest04, TestSize.Level0)
109 {
110     HcfEccCommParamsSpec *srcSpec = nullptr;
111     HcfResult ret = ConstructEcc224CommParamsSpec(&srcSpec);
112     EXPECT_EQ(ret, HCF_SUCCESS);
113 
114     ret = CreateEccCommonSpecImpl(srcSpec, nullptr);
115     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
116 }
117 
118 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest05, TestSize.Level0)
119 {
120     HcfResult ret = CopyDhCommonSpec(nullptr, nullptr);
121     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
122 }
123 
124 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest06, TestSize.Level0)
125 {
126     HcfResult ret = CreateDhCommonSpecImpl(nullptr, nullptr);
127     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
128 }
129 
130 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest07, TestSize.Level0)
131 {
132     HcfBlob dataBlob = { .data = nullptr, .len = 0 };
133     const char *curveName = nullptr;
134     HcfResult ret = HcfConvertPoint(curveName, &dataBlob, nullptr);
135     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
136 }
137 
138 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest08, TestSize.Level0)
139 {
140     HcfBlob dataBlob = { .data = nullptr, .len = 0 };
141     const char *curveName = nullptr;
142     HcfResult ret = HcfGetEncodedPoint(curveName, nullptr, nullptr, &dataBlob);
143     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
144 }
145 }