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 <gtest/gtest.h>
17 #include "securec.h"
18 #include "hal_error.h"
19 #include "clib_error.h"
20 #include "hks_param.h"
21 #include "hc_types.h"
22 #include "hks_type.h"
23 #include "account_module_defines.h"
24 #include "crypto_hash_to_point.h"
25 #include "mbedtls_ec_adapter.h"
26 #include "huks_adapter.h"
27 #include "string_util.h"
28 #include "alg_loader.h"
29 #include "device_auth.h"
30 
31 using namespace std;
32 using namespace testing::ext;
33 
34 namespace {
35 static const int32_t KEY_BYTES_CURVE25519 = 32;
36 static const int32_t SHA_256_LENGTH = 32;
37 static const int32_t DEFAULT_RAND_LEN = 32;
38 static const int32_t EC_LEN = 64;
39 static const int32_t P256_PUBLIC_SIZE = 64;
40 static const int32_t BIGNUM_HEX_LEN = 512;
41 
42 class KeyManagementTest : public testing::Test {
43 public:
44     static void SetUpTestCase();
45     static void TearDownTestCase();
46     void SetUp();
47     void TearDown();
48 };
49 
SetUpTestCase()50 void KeyManagementTest::SetUpTestCase() {}
TearDownTestCase()51 void KeyManagementTest::TearDownTestCase() {}
SetUp()52 void KeyManagementTest::SetUp() {}
TearDown()53 void KeyManagementTest::TearDown() {}
54 
55 HWTEST_F(KeyManagementTest, HashToPointTest001, TestSize.Level0)
56 {
57     uint32_t hashSize = KEY_BYTES_CURVE25519;
58     uint8_t hashData[KEY_BYTES_CURVE25519] = { 0 };
59     uint8_t pointData[KEY_BYTES_CURVE25519] = { 0 };
60     struct HksBlob pointBlob = { hashSize, pointData };
61     struct HksBlob invalidSizeBlob = { 0, hashData };
62     struct HksBlob invalidDataBlob = { hashSize, nullptr };
63     int32_t ret = OpensslHashToPoint(&invalidSizeBlob, &pointBlob);
64     EXPECT_EQ(ret, HKS_ERROR_MALLOC_FAIL);
65     ret = OpensslHashToPoint(&invalidDataBlob, &pointBlob);
66     EXPECT_EQ(ret, HAL_FAILED);
67     HcFree(invalidDataBlob.data);
68 }
69 
70 HWTEST_F(KeyManagementTest, MbedtlsHashToPointTest001, TestSize.Level0)
71 {
72     uint8_t hashData[SHA256_LEN] = { 0 };
73     uint8_t pointData[EC_LEN] = { 0 };
74     Uint8Buff hashBuffer = { hashData, SHA256_LEN };
75     Uint8Buff pointBuffer = { pointData, EC_LEN };
76     Uint8Buff invalidBuffer = { hashData, 0 };
77     Uint8Buff shortPointBuffer = { hashData, EC_LEN - 1 };
78     int32_t ret = MbedtlsHashToPoint(&hashBuffer, &pointBuffer);
79     EXPECT_EQ(ret, HAL_SUCCESS);
80     ret = MbedtlsHashToPoint(nullptr, &pointBuffer);
81     EXPECT_EQ(ret, HAL_ERR_NULL_PTR);
82     ret = MbedtlsHashToPoint(&hashBuffer, nullptr);
83     EXPECT_EQ(ret, HAL_ERR_NULL_PTR);
84     ret = MbedtlsHashToPoint(&invalidBuffer, &pointBuffer);
85     EXPECT_EQ(ret, HAL_ERR_INVALID_LEN);
86     ret = MbedtlsHashToPoint(&hashBuffer, &shortPointBuffer);
87     EXPECT_EQ(ret, HAL_ERR_INVALID_LEN);
88 }
89 
90 HWTEST_F(KeyManagementTest, MbedtlsSharedSecretTest001, TestSize.Level0)
91 {
92     uint8_t keyData[P256_PUBLIC_SIZE] = { 0 };
93     KeyBuff priKeyBuffer = { keyData, P256_PUBLIC_SIZE, false };
94     KeyBuff pubKeyBuffer = { keyData, P256_PUBLIC_SIZE, false };
95     Uint8Buff sharedKeyBuffer = { keyData, P256_PUBLIC_SIZE };
96     int32_t ret = MbedtlsAgreeSharedSecret(&priKeyBuffer, &pubKeyBuffer, &sharedKeyBuffer);
97     EXPECT_EQ(ret, HAL_SUCCESS);
98     KeyBuff keyInvalidBuff01 = { keyData, 0, false };
99     ret = MbedtlsAgreeSharedSecret(&keyInvalidBuff01, &pubKeyBuffer, &sharedKeyBuffer);
100     EXPECT_EQ(ret, HAL_ERR_INVALID_LEN);
101     KeyBuff keyInvalidBuff02 = { keyData, P256_PUBLIC_SIZE - 1, false };
102     ret = MbedtlsAgreeSharedSecret(&priKeyBuffer, &keyInvalidBuff02, &sharedKeyBuffer);
103     EXPECT_EQ(ret, HAL_FAILED);
104 }
105 
106 HWTEST_F(KeyManagementTest, HuksAdapterTest001, TestSize.Level0)
107 {
108     int32_t ret = GetLoaderInstance()->initAlg();
109     EXPECT_EQ(ret, HAL_SUCCESS);
110     uint8_t testMsgData[] = "test_message";
111     uint8_t hashData[SHA_256_LENGTH] = { 0 };
112     Uint8Buff msgBuff = { testMsgData, sizeof(testMsgData) };
113     Uint8Buff hashBuff = { hashData, SHA_256_LENGTH };
114     ret = GetLoaderInstance()->sha256(&msgBuff, &hashBuff);
115     EXPECT_EQ(ret, HAL_SUCCESS);
116 }
117 
118 HWTEST_F(KeyManagementTest, HuksAdapterTest002, TestSize.Level0)
119 {
120     int32_t ret = GetLoaderInstance()->initAlg();
121     EXPECT_EQ(ret, HAL_SUCCESS);
122     uint8_t randData[DEFAULT_RAND_LEN] = { 0 };
123     Uint8Buff randBuff = { randData, DEFAULT_RAND_LEN };
124     ret = GetLoaderInstance()->generateRandom(&randBuff);
125     EXPECT_EQ(ret, HAL_SUCCESS);
126 }
127 
128 HWTEST_F(KeyManagementTest, HuksAdapterTest003, TestSize.Level0)
129 {
130     int32_t ret = GetLoaderInstance()->initAlg();
131     EXPECT_EQ(ret, HAL_SUCCESS);
132     uint8_t keyData[P256_PUBLIC_SIZE] = { 0 };
133     Uint8Buff sharedKey = { keyData, P256_PUBLIC_SIZE };
134     KeyParams privKeyParams = { { keyData, P256_PUBLIC_SIZE, false }, false, DEFAULT_OS_ACCOUNT };
135     KeyBuff pubKeyBuff = { keyData, P256_PUBLIC_SIZE, false };
136     ret = GetLoaderInstance()->agreeSharedSecretWithStorage(&privKeyParams, &pubKeyBuff, P256, P256_PUBLIC_SIZE,
137         &sharedKey);
138     EXPECT_EQ(ret, HAL_ERR_HUKS);
139 }
140 
141 HWTEST_F(KeyManagementTest, HuksAdapterTest004, TestSize.Level0)
142 {
143     int32_t ret = GetLoaderInstance()->initAlg();
144     EXPECT_EQ(ret, HAL_SUCCESS);
145     char bigNumHex[BIGNUM_HEX_LEN + 1] = { 0 };
146     char shortNumHex[BIG_PRIME_LEN_256 + 1] = { 0 };
147     char invalidCharHex[BIGNUM_HEX_LEN + 1] = { 0 };
148     (void)memset_s(bigNumHex, BIGNUM_HEX_LEN, 'a', BIGNUM_HEX_LEN);
149     (void)memset_s(shortNumHex, BIG_PRIME_LEN_256, 'a', BIG_PRIME_LEN_256);
150     (void)memset_s(invalidCharHex, BIGNUM_HEX_LEN, 'z', BIGNUM_HEX_LEN);
151     uint8_t baseData[BIGNUM_HEX_LEN] = { 0 };
152     uint8_t outData[BIG_PRIME_LEN_256] = { 0 };
153     Uint8Buff baseBuff = { baseData, BIGNUM_HEX_LEN };
154     Uint8Buff expBuff = { baseData, BIGNUM_HEX_LEN };
155     Uint8Buff outBuff = { outData, BIG_PRIME_LEN_256 };
156     ret = GetLoaderInstance()->bigNumExpMod(nullptr, nullptr, nullptr, nullptr);
157     EXPECT_EQ(ret, CLIB_ERR_NULL_PTR);
158     ret = GetLoaderInstance()->bigNumExpMod(&baseBuff, &expBuff, shortNumHex, &outBuff);
159     EXPECT_EQ(ret, HAL_FAILED);
160     ret = GetLoaderInstance()->bigNumExpMod(&baseBuff, &expBuff, invalidCharHex, &outBuff);
161     EXPECT_EQ(ret, CLIB_ERR_INVALID_PARAM);
162     uint32_t testLen = strlen(bigNumHex);
163     EXPECT_EQ(testLen, BIGNUM_HEX_LEN);
164     testLen = strlen(invalidCharHex);
165     EXPECT_EQ(testLen, BIGNUM_HEX_LEN);
166     ret = GetLoaderInstance()->bigNumExpMod(&baseBuff, &expBuff, bigNumHex, &outBuff);
167     EXPECT_EQ(ret, HAL_SUCCESS);
168 }
169 }