1/*
2 * Copyright (c) 2023 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 <gtest/hwext/gtest-multithread.h>
18#include <vector>
19#include <unistd.h>
20#include <securec.h>
21
22#include "v1_0/ihuks.h"
23#include "v1_0/ihuks_types.h"
24#include "huks_sa_type.h"
25#include "huks_hdi_test_util.h"
26
27using namespace testing;
28using namespace testing::ext;
29using namespace testing::mt;
30namespace Unittest::HuksHdiTest {
31static struct IHuks *g_huksHdiProxy = nullptr;
32
33const static uint8_t g_importKeyAlias[] = {
34    0x74, 0x65, 0x73, 0x74, 0x5F, 0x69, 0x6D, 0x70, 0x6F, 0x72, 0x74, 0x5F, 0x6B, 0x65, 0x79, 0x00
35};
36
37static uint8_t g_importParamSet[] = {
38    0x8C, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
39    0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
40    0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
41    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
42    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
43    0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x27, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
44    0x04, 0x00, 0x00, 0x00, 0x38, 0x3E, 0x86, 0xF7, 0x2E, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
45    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x75, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
46    0xFB, 0x60, 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47};
48
49const static uint8_t g_importKey[] = {
50    0x17, 0xD6, 0x23, 0xCB, 0xA7, 0x05, 0x60, 0x22, 0xC1, 0x35, 0xCD, 0x3F, 0x30, 0x2D, 0xF6, 0x31,
51};
52
53const static uint8_t g_importOutKey[] = {
54    0x1C, 0x01, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
55    0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
56    0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
57    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
58    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
59    0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x27, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
60    0x04, 0x00, 0x00, 0x00, 0x08, 0xA1, 0x88, 0xF7, 0x2E, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
61    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x75, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
62    0xFB, 0x60, 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x17, 0x27, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
63    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x27, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
64    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x27, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
65    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF, 0x03, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
66    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x27, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
67    0x40, 0x00, 0x00, 0x00, 0x0C, 0xA1, 0x88, 0xF7, 0x00, 0x00, 0x00, 0x00, 0x58, 0x14, 0xBA, 0x89,
68    0x77, 0xBB, 0x5B, 0x03, 0x70, 0x70, 0x7A, 0x17, 0x88, 0x5F, 0x69, 0xBF, 0x6A, 0xCD, 0x50, 0xCC,
69    0xE0, 0xA2, 0x8F, 0x7C, 0x0F, 0x80, 0x74, 0x8F, 0x30, 0xA5, 0x1B, 0x9C, 0x74, 0x7F, 0x87, 0x5F,
70    0x8F, 0x07, 0x5B, 0xEF, 0x83, 0x3A, 0x40, 0x21, 0x10, 0x00, 0x00, 0x00, 0x6D, 0xE9, 0x0C, 0x45,
71    0x1A, 0x10, 0xA5, 0x63, 0xF2, 0xEA, 0x05, 0x98, 0x70, 0x86, 0x4A, 0xD5,
72};
73
74const static uint8_t g_generateKeyAlias[] = {
75    0x48, 0x6B, 0x73, 0x41, 0x45, 0x53, 0x43, 0x69, 0x70, 0x68, 0x65, 0x72, 0x4B, 0x65, 0x79, 0x41,
76    0x6C, 0x69, 0x61, 0x73, 0x54, 0x65, 0x73, 0x74, 0x30, 0x30, 0x31,
77};
78
79static uint8_t g_generateParamSet[] = {
80    0x8C, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
81    0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
82    0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
83    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
84    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
85    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x27, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
86    0x04, 0x00, 0x00, 0x00, 0x28, 0x3E, 0x86, 0xF7, 0x2E, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
87    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x75, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
88    0xFB, 0x60, 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89};
90
91const static uint8_t g_generateOutKey[] = {
92    0x1C, 0x01, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
93    0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
94    0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
95    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
96    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
97    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x27, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
98    0x04, 0x00, 0x00, 0x00, 0xE8, 0xA0, 0x88, 0xF7, 0x2E, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
99    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x75, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
100    0xFB, 0x60, 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x17, 0x27, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
101    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x27, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
102    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x27, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
103    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF, 0x03, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
104    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x27, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
105    0x40, 0x00, 0x00, 0x00, 0xEC, 0xA0, 0x88, 0xF7, 0x00, 0x00, 0x00, 0x00, 0x4D, 0x07, 0xB4, 0x87,
106    0xF1, 0x94, 0x9A, 0x2A, 0x8A, 0xD4, 0xFE, 0x57, 0xF0, 0x5E, 0xE6, 0x96, 0x50, 0x7C, 0x20, 0x7E,
107    0xC2, 0x5D, 0x42, 0x6E, 0x68, 0x2C, 0x9D, 0xC5, 0x79, 0x0E, 0x16, 0x72, 0x45, 0x2F, 0x1A, 0xA8,
108    0x2D, 0x18, 0x25, 0x9C, 0x86, 0xA3, 0x4C, 0x57, 0x10, 0x00, 0x00, 0x00, 0xE7, 0xF8, 0x6F, 0x32,
109    0x11, 0xEC, 0xF8, 0x66, 0x05, 0xD0, 0x1F, 0x38, 0xAD, 0xE1, 0xF3, 0x04,
110};
111
112static uint8_t g_initParamSet[] = {
113    0xBC, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
114    0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
115    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
116    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
117    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
118    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
119    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
120    0x10, 0x00, 0x00, 0x00, 0x58, 0x3E, 0x86, 0xF7, 0x11, 0x27, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
121    0x04, 0x00, 0x00, 0x00, 0x68, 0x3E, 0x86, 0xF7, 0x2E, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
122    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x75, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
123    0xFB, 0x60, 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125};
126
127const static uint8_t g_initKey[] = {
128    0x1C, 0x01, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
129    0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
130    0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
131    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
132    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
133    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x27, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
134    0x04, 0x00, 0x00, 0x00, 0xE8, 0xA0, 0x88, 0xF7, 0x2E, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
135    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x75, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
136    0xFB, 0x60, 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x17, 0x27, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
137    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x27, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
138    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x27, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
139    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF, 0x03, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
140    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x27, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00,
141    0x40, 0x00, 0x00, 0x00, 0xEC, 0xA0, 0x88, 0xF7, 0x00, 0x00, 0x00, 0x00, 0x4D, 0x07, 0xB4, 0x87,
142    0xF1, 0x94, 0x9A, 0x2A, 0x8A, 0xD4, 0xFE, 0x57, 0xF0, 0x5E, 0xE6, 0x96, 0x50, 0x7C, 0x20, 0x7E,
143    0xC2, 0x5D, 0x42, 0x6E, 0x68, 0x2C, 0x9D, 0xC5, 0x79, 0x0E, 0x16, 0x72, 0x45, 0x2F, 0x1A, 0xA8,
144    0x2D, 0x18, 0x25, 0x9C, 0x86, 0xA3, 0x4C, 0x57, 0x10, 0x00, 0x00, 0x00, 0xE7, 0xF8, 0x6F, 0x32,
145    0x11, 0xEC, 0xF8, 0x66, 0x05, 0xD0, 0x1F, 0x38, 0xAD, 0xE1, 0xF3, 0x04,
146};
147
148class HuksHdiApiTest : public testing::Test {
149public:
150    static void SetUpTestCase(void);
151
152    static void TearDownTestCase(void);
153
154    void SetUp();
155
156    void TearDown();
157};
158
159void HuksHdiApiTest::SetUpTestCase(void)
160{
161    g_huksHdiProxy = IHuksGetInstance("hdi_service", true);
162    int32_t ret = g_huksHdiProxy->ModuleInit(g_huksHdiProxy);
163    HUKS_TEST_LOG_I("ModuleInit = %d", ret);
164}
165
166void HuksHdiApiTest::TearDownTestCase(void)
167{
168    if (g_huksHdiProxy != nullptr) {
169        IHuksReleaseInstance("hdi_service", g_huksHdiProxy, true);
170        g_huksHdiProxy = nullptr;
171    }
172}
173
174void HuksHdiApiTest::SetUp()
175{
176}
177
178void HuksHdiApiTest::TearDown()
179{
180}
181
182#define HKS_TAG_TYPE_MASK (0xF << 28)
183#define HKS_TAG_TYPE_BYTES (5 << 28)
184
185static uint32_t GetTagType(uint32_t tag)
186{
187    return (tag & (uint32_t)HKS_TAG_TYPE_MASK);
188}
189
190static inline bool IsAdditionOverflow(uint32_t a, uint32_t b)
191{
192    return (UINT32_MAX - a) < b;
193}
194
195static int32_t HuksFreshParamSet(struct HksParamSet *paramSet, bool isCopy)
196{
197    uint32_t size = paramSet->paramSetSize;
198    uint32_t offset = sizeof(struct HksParamSet) + sizeof(struct HksParam) * paramSet->paramsCnt;
199
200    for (uint32_t i = 0; i < paramSet->paramsCnt; i++) {
201        if (offset > size) {
202            return HUKS_FAILURE;
203        }
204        if (GetTagType(paramSet->params[i].tag) == HKS_TAG_TYPE_BYTES) {
205            if (IsAdditionOverflow(offset, paramSet->params[i].blob.size)) {
206                return HUKS_FAILURE;
207            }
208
209            if (isCopy && (memcpy_s((uint8_t *)paramSet + offset, size - offset,
210                paramSet->params[i].blob.data, paramSet->params[i].blob.size) != EOK)) {
211                return HUKS_FAILURE;
212            }
213            paramSet->params[i].blob.data = (uint8_t *)paramSet + offset;
214            offset += paramSet->params[i].blob.size;
215        }
216    }
217
218    if (paramSet->paramSetSize != offset) {
219        return HUKS_FAILURE;
220    }
221    return HUKS_SUCCESS;
222}
223
224/**
225 * @tc.name: HuksHdiApiTest.HdiFuncPointerTest001
226 * @tc.desc: Test hdi func pointer whether nullptr;
227 * @tc.require:issueI77AT9
228 * @tc.type: FUNC
229 */
230HWTEST_F(HuksHdiApiTest, HdiFuncPointerTest001, TestSize.Level0)
231{
232    ASSERT_NE(g_huksHdiProxy, nullptr);
233    ASSERT_NE(g_huksHdiProxy->ModuleInit, nullptr);
234    ASSERT_NE(g_huksHdiProxy->ModuleDestroy, nullptr);
235    ASSERT_NE(g_huksHdiProxy->GenerateKey, nullptr);
236    ASSERT_NE(g_huksHdiProxy->ImportKey, nullptr);
237    ASSERT_NE(g_huksHdiProxy->ImportWrappedKey, nullptr);
238    ASSERT_NE(g_huksHdiProxy->ExportPublicKey, nullptr);
239    ASSERT_NE(g_huksHdiProxy->Init, nullptr);
240    ASSERT_NE(g_huksHdiProxy->Update, nullptr);
241    ASSERT_NE(g_huksHdiProxy->Finish, nullptr);
242    ASSERT_NE(g_huksHdiProxy->Abort, nullptr);
243    ASSERT_NE(g_huksHdiProxy->CheckKeyValidity, nullptr);
244    ASSERT_NE(g_huksHdiProxy->AttestKey, nullptr);
245    ASSERT_NE(g_huksHdiProxy->GenerateRandom, nullptr);
246    ASSERT_NE(g_huksHdiProxy->Sign, nullptr);
247    ASSERT_NE(g_huksHdiProxy->Verify, nullptr);
248    ASSERT_NE(g_huksHdiProxy->Encrypt, nullptr);
249    ASSERT_NE(g_huksHdiProxy->Decrypt, nullptr);
250    ASSERT_NE(g_huksHdiProxy->AgreeKey, nullptr);
251    ASSERT_NE(g_huksHdiProxy->DeriveKey, nullptr);
252    ASSERT_NE(g_huksHdiProxy->Mac, nullptr);
253    ASSERT_NE(g_huksHdiProxy->UpgradeKey, nullptr);
254    ASSERT_NE(g_huksHdiProxy->GetVersion, nullptr);
255}
256
257/**
258 * @tc.name: HuksHdiApiTest.ApiPassthroughTest001
259 * @tc.desc: Test Generate key with current software huks driver;
260 * @tc.require:issueI77AT9
261 * @tc.type: FUNC
262 */
263HWTEST_F(HuksHdiApiTest, ApiPassthroughTest001, TestSize.Level0)
264{
265    ASSERT_NE(g_huksHdiProxy, nullptr);
266    ASSERT_NE(g_huksHdiProxy->GenerateKey, nullptr);
267    struct HuksBlob keyAlias = {
268        .data = (uint8_t *)g_generateKeyAlias,
269        .dataLen = sizeof(g_generateKeyAlias)
270    };
271    uint8_t keyBuff[1] = {0};
272    struct HuksBlob key = {
273        .data = keyBuff,
274        .dataLen = sizeof(keyBuff)
275    };
276    int ret = HuksFreshParamSet((struct HksParamSet *)g_generateParamSet, false);
277    ASSERT_EQ(ret, HUKS_SUCCESS);
278    struct HuksParamSet paramSet = {
279        .data = (uint8_t *)g_generateParamSet,
280        .dataLen = sizeof(g_generateParamSet)
281    };
282    uint8_t outKeyBuffer[512];
283    struct HuksBlob outKey = {
284        .data = outKeyBuffer,
285        .dataLen = sizeof(outKeyBuffer)
286    };
287    ret = g_huksHdiProxy->GenerateKey(g_huksHdiProxy, &keyAlias, &paramSet, &key, &outKey);
288    ASSERT_EQ(ret, HUKS_SUCCESS);
289    ASSERT_EQ(outKey.dataLen, sizeof(g_generateOutKey));
290}
291
292/**
293 * @tc.name: HuksHdiApiTest.ApiPassthroughTest002
294 * @tc.desc: Test Import key with current software huks driver(hardcoded root key);
295 * @tc.require:issueI77AT9
296 * @tc.type: FUNC
297 */
298HWTEST_F(HuksHdiApiTest, ApiPassthroughTest002, TestSize.Level0)
299{
300    ASSERT_NE(g_huksHdiProxy, nullptr);
301    ASSERT_NE(g_huksHdiProxy->ImportKey, nullptr);
302    struct HuksBlob keyAlias = {
303        .data = (uint8_t *)g_importKeyAlias,
304        .dataLen = sizeof(g_importKeyAlias)
305    };
306    struct HuksBlob key = {
307        .data = (uint8_t *)g_importKey,
308        .dataLen = sizeof(g_importKey)
309    };
310
311    int ret = HuksFreshParamSet((struct HksParamSet *)g_importParamSet, false);
312    ASSERT_EQ(ret, HUKS_SUCCESS);
313    struct HuksParamSet paramSet = {
314        .data = (uint8_t *)g_importParamSet,
315        .dataLen = sizeof(g_importParamSet)
316    };
317    uint8_t outKeyBuffer[512];
318    struct HuksBlob outKey = {
319        .data = outKeyBuffer,
320        .dataLen = sizeof(outKeyBuffer)
321    };
322    ret = g_huksHdiProxy->ImportKey(g_huksHdiProxy, &keyAlias, &key, &paramSet, &outKey);
323    ASSERT_EQ(ret, HUKS_SUCCESS);
324    ASSERT_EQ(outKey.dataLen, sizeof(g_importOutKey));
325}
326
327/**
328 * @tc.name: HuksHdiApiTest.ApiPassthroughTest003
329 * @tc.desc: Test init key with current software huks driver(hardcoded root key);
330 * @tc.require:issueI77AT9
331 * @tc.type: FUNC
332 */
333HWTEST_F(HuksHdiApiTest, ApiPassthroughTest003, TestSize.Level0)
334{
335    ASSERT_NE(g_huksHdiProxy, nullptr);
336    ASSERT_NE(g_huksHdiProxy->Init, nullptr);
337    struct HuksBlob key = {
338        .data = (uint8_t *)g_initKey,
339        .dataLen = sizeof(g_initKey)
340    };
341    int ret = HuksFreshParamSet((struct HksParamSet *)g_initParamSet, false);
342    ASSERT_EQ(ret, HUKS_SUCCESS);
343    struct HuksParamSet paramSet = {
344        .data = (uint8_t *)g_initParamSet,
345        .dataLen = sizeof(g_initParamSet)
346    };
347    uint8_t outHandleBuffer[12];
348    struct HuksBlob outHandle = {
349        .data = outHandleBuffer,
350        .dataLen = sizeof(outHandleBuffer)
351    };
352    uint8_t outHandleToken[32];
353    struct HuksBlob outToken = {
354        .data = outHandleToken,
355        .dataLen = sizeof(outHandleToken)
356    };
357    ret = g_huksHdiProxy->Init(g_huksHdiProxy, &key, &paramSet, &outHandle, &outToken);
358    ASSERT_EQ(ret, HUKS_SUCCESS);
359    ASSERT_EQ(outHandle.dataLen, 8);
360}
361
362/**
363 * @tc.name: HuksHdiApiTest.MultiThreadTest001
364 * @tc.desc: Test init key with current software huks driver(hardcoded root key) in multi thread scenario;
365 * @tc.require:issueI77AT9
366 * @tc.type: FUNC
367 */
368HWMTEST_F(HuksHdiApiTest, MultiThreadTest001, TestSize.Level0, 10)
369{
370    std::thread::id thisId = std::this_thread::get_id();
371    std::ostringstream oss;
372    oss << thisId;
373    std::string thisIdString = oss.str();
374    long int thread_id = atol(thisIdString.c_str());
375    HUKS_TEST_LOG_I("running thread id:%ld start\n", thread_id);
376
377    ASSERT_NE(g_huksHdiProxy, nullptr);
378    ASSERT_NE(g_huksHdiProxy->Init, nullptr);
379    struct HuksBlob key = {
380        .data = (uint8_t *)g_initKey,
381        .dataLen = sizeof(g_initKey)
382    };
383    struct HuksParamSet paramSet = {
384        .data = (uint8_t *)g_initParamSet,
385        .dataLen = sizeof(g_initParamSet)
386    };
387    uint8_t outHandleBuffer[12];
388    struct HuksBlob outHandle = {
389        .data = outHandleBuffer,
390        .dataLen = sizeof(outHandleBuffer)
391    };
392    uint8_t outHandleToken[32];
393    struct HuksBlob outToken = {
394        .data = outHandleToken,
395        .dataLen = sizeof(outHandleToken)
396    };
397    int ret = g_huksHdiProxy->Init(g_huksHdiProxy, &key, &paramSet, &outHandle, &outToken);
398    ASSERT_EQ(ret, HUKS_SUCCESS);
399    ASSERT_EQ(outHandle.dataLen, 8);
400    HUKS_TEST_LOG_I("running thread id:%ld end\n", thread_id);
401}
402}