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