1 /*
2 * Copyright (c) 2021-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 <iostream>
18
19 #include "file_ex.h"
20 #include "hks_ability.h"
21 #include "hks_config.h"
22 #include "hks_crypto_hal.h"
23 #include "hks_crypto_hal_common.h"
24 #include "hks_mem.h"
25
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace Security {
29 namespace Huks {
30 namespace UnitTest {
31 class HksCryptoHalDsaKey : public HksCryptoHalCommon, public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 };
38
39 const uint32_t DSA_SIZE_512 = 512;
SetUpTestCase(void)40 void HksCryptoHalDsaKey::SetUpTestCase(void)
41 {
42 }
43
TearDownTestCase(void)44 void HksCryptoHalDsaKey::TearDownTestCase(void)
45 {
46 }
47
SetUp()48 void HksCryptoHalDsaKey::SetUp()
49 {
50 EXPECT_EQ(HksCryptoAbilityInit(), 0);
51 }
52
TearDown()53 void HksCryptoHalDsaKey::TearDown()
54 {
55 }
56
57 /**
58 * @tc.number : HksCryptoHalDsaKey_001
59 * @tc.name : HksCryptoHalDsaKey_001
60 * @tc.desc : Using HksCryptoHalGenerateKey Generate DSA-256bit key.
61 */
62 HWTEST_F(HksCryptoHalDsaKey, HksCryptoHalDsaKey_001, Function | SmallTest | Level0)
63 {
64 int32_t ret;
65
66 HksKeySpec spec = {
67 .algType = HKS_ALG_DSA,
68 .keyLen = DSA_SIZE_512,
69 .algParam = nullptr,
70 };
71
72 HksBlob key = { .size = 0, .data = nullptr };
73
74 ret = HksCryptoHalGenerateKey(&spec, &key);
75 #if defined(HKS_SUPPORT_DSA_C) && defined(HKS_SUPPORT_DSA_GENERATE_KEY)
76 #if defined(_USE_OPENSSL_)
77 ASSERT_EQ(HKS_SUCCESS, ret);
78 ASSERT_NE((uint32_t)0, key.size);
79 ASSERT_NE(nullptr, key.data);
80 HKS_FREE(key.data);
81 #endif
82 #if defined(_USE_MBEDTLS_)
83 ASSERT_EQ(HKS_ERROR_NOT_SUPPORTED, ret);
84 HKS_FREE(key.data);
85 #endif
86 #else
87 ASSERT_EQ(HKS_ERROR_NOT_SUPPORTED, ret);
88 #endif
89 }
90
91 /**
92 * @tc.number : HksCryptoHalDsaKey_002
93 * @tc.name : HksCryptoHalDsaKey_002
94 * @tc.desc : Generate key and export public key with DSA.
95 */
96 HWTEST_F(HksCryptoHalDsaKey, HksCryptoHalDsaKey_002, Function | SmallTest | Level0)
97 {
98 int32_t ret;
99
100 HksKeySpec spec = {
101 .algType = HKS_ALG_DSA,
102 .keyLen = DSA_SIZE_512,
103 };
104
105 HksBlob key = { .size = 0, .data = NULL };
106
107 ret = HksCryptoHalGenerateKey(&spec, &key);
108 #ifdef _USE_OPENSSL_
109 ASSERT_EQ(ret, HKS_SUCCESS);
110 #endif
111 #ifdef _USE_MBEDTLS_
112 ASSERT_EQ(ret, HKS_ERROR_NOT_SUPPORTED);
113 return;
114 #endif
115
116 KeyMaterialDsa *keyMaterial = (KeyMaterialDsa *)key.data;
117 ASSERT_NE(keyMaterial, nullptr);
118
119 uint32_t keyOutLen =
120 sizeof(KeyMaterialDsa) + keyMaterial->ySize + keyMaterial->pSize + keyMaterial->qSize + keyMaterial->gSize;
121 HksBlob keyOut = { .size = keyOutLen, .data = (uint8_t *)HksMalloc(keyOutLen) };
122 ASSERT_NE(keyOut.data, nullptr);
123
124 ret = HksCryptoHalGetPubKey(&key, &keyOut);
125 #ifdef _USE_OPENSSL_
126 ASSERT_EQ(ret, HKS_SUCCESS);
127 #endif
128 #ifdef _USE_MBEDTLS_
129 ASSERT_EQ(ret, HKS_ERROR_NOT_SUPPORTED);
130 #endif
131 HKS_FREE_BLOB(key);
132 HKS_FREE_BLOB(keyOut);
133 }
134 } // namespace UnitTest
135 } // namespace Huks
136 } // namespace Security
137 } // namespace OHOS