• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     HcfFree(srcSpec->field);
97     HcfFree(srcSpec);
98 }
99 
100 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest03, TestSize.Level0)
101 {
102     HcfEccCommParamsSpec *srcSpec = nullptr;
103     HcfResult ret = ConstructEcc224CommParamsSpec(&srcSpec);
104     EXPECT_EQ(ret, HCF_SUCCESS);
105 
106     ret = CopyEccCommonSpec(srcSpec, nullptr);
107     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
108     HcfFree(srcSpec->field);
109     HcfFree(srcSpec);
110 }
111 
112 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest04, TestSize.Level0)
113 {
114     HcfEccCommParamsSpec *srcSpec = nullptr;
115     HcfResult ret = ConstructEcc224CommParamsSpec(&srcSpec);
116     EXPECT_EQ(ret, HCF_SUCCESS);
117 
118     ret = CreateEccCommonSpecImpl(srcSpec, nullptr);
119     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
120     HcfFree(srcSpec->field);
121     HcfFree(srcSpec);
122 }
123 
124 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest05, TestSize.Level0)
125 {
126     HcfResult ret = CopyDhCommonSpec(nullptr, nullptr);
127     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
128 }
129 
130 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest06, TestSize.Level0)
131 {
132     HcfResult ret = CreateDhCommonSpecImpl(nullptr, nullptr);
133     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
134 }
135 
136 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest07, TestSize.Level0)
137 {
138     HcfBlob dataBlob = { .data = nullptr, .len = 0 };
139     const char *curveName = nullptr;
140     HcfResult ret = HcfConvertPoint(curveName, &dataBlob, nullptr);
141     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
142 }
143 
144 HWTEST_F(CryptoKeyUtilsTest, CryptoKeyUtilsTest08, TestSize.Level0)
145 {
146     HcfBlob dataBlob = { .data = nullptr, .len = 0 };
147     const char *curveName = nullptr;
148     HcfResult ret = HcfGetEncodedPoint(curveName, nullptr, nullptr, &dataBlob);
149     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
150 }
151 }