• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace testing::mt;
30 namespace Unittest::HuksHdiTest {
31 static struct IHuks *g_huksHdiProxy = nullptr;
32 
33 const static uint8_t g_importKeyAlias[] = {
34     0x74, 0x65, 0x73, 0x74, 0x5F, 0x69, 0x6D, 0x70, 0x6F, 0x72, 0x74, 0x5F, 0x6B, 0x65, 0x79, 0x00
35 };
36 
37 static 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 
49 const static uint8_t g_importKey[] = {
50     0x17, 0xD6, 0x23, 0xCB, 0xA7, 0x05, 0x60, 0x22, 0xC1, 0x35, 0xCD, 0x3F, 0x30, 0x2D, 0xF6, 0x31,
51 };
52 
53 const 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 
74 const 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 
79 static 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 
91 const 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 
112 static 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 
127 const 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 
148 class HuksHdiApiTest : public testing::Test {
149 public:
150     static void SetUpTestCase(void);
151 
152     static void TearDownTestCase(void);
153 
154     void SetUp();
155 
156     void TearDown();
157 };
158 
SetUpTestCase(void)159 void 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 
TearDownTestCase(void)166 void HuksHdiApiTest::TearDownTestCase(void)
167 {
168     if (g_huksHdiProxy != nullptr) {
169         IHuksReleaseInstance("hdi_service", g_huksHdiProxy, true);
170         g_huksHdiProxy = nullptr;
171     }
172 }
173 
SetUp()174 void HuksHdiApiTest::SetUp()
175 {
176 }
177 
TearDown()178 void HuksHdiApiTest::TearDown()
179 {
180 }
181 
182 #define HKS_TAG_TYPE_MASK (0xF << 28)
183 #define HKS_TAG_TYPE_BYTES (5 << 28)
184 
GetTagType(uint32_t tag)185 static uint32_t GetTagType(uint32_t tag)
186 {
187     return (tag & (uint32_t)HKS_TAG_TYPE_MASK);
188 }
189 
IsAdditionOverflow(uint32_t a,uint32_t b)190 static inline bool IsAdditionOverflow(uint32_t a, uint32_t b)
191 {
192     return (UINT32_MAX - a) < b;
193 }
194 
HuksFreshParamSet(struct HksParamSet * paramSet,bool isCopy)195 static 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  */
230 HWTEST_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->ExportChipsetPlatformPublicKey, nullptr);
255     ASSERT_NE(g_huksHdiProxy->GetVersion, nullptr);
256 }
257 
258 /**
259  * @tc.name: HuksHdiApiTest.ApiPassthroughTest001
260  * @tc.desc: Test Generate key with current software huks driver;
261  * @tc.require:issueI77AT9
262  * @tc.type: FUNC
263  */
264 HWTEST_F(HuksHdiApiTest, ApiPassthroughTest001, TestSize.Level0)
265 {
266     ASSERT_NE(g_huksHdiProxy, nullptr);
267     ASSERT_NE(g_huksHdiProxy->GenerateKey, nullptr);
268     struct HuksBlob keyAlias = {
269         .data = (uint8_t *)g_generateKeyAlias,
270         .dataLen = sizeof(g_generateKeyAlias)
271     };
272     uint8_t keyBuff[1] = {0};
273     struct HuksBlob key = {
274         .data = keyBuff,
275         .dataLen = sizeof(keyBuff)
276     };
277     int ret = HuksFreshParamSet((struct HksParamSet *)g_generateParamSet, false);
278     ASSERT_EQ(ret, HUKS_SUCCESS);
279     struct HuksParamSet paramSet = {
280         .data = (uint8_t *)g_generateParamSet,
281         .dataLen = sizeof(g_generateParamSet)
282     };
283     uint8_t outKeyBuffer[512];
284     struct HuksBlob outKey = {
285         .data = outKeyBuffer,
286         .dataLen = sizeof(outKeyBuffer)
287     };
288     ret = g_huksHdiProxy->GenerateKey(g_huksHdiProxy, &keyAlias, &paramSet, &key, &outKey);
289     ASSERT_EQ(ret, HUKS_SUCCESS);
290     ASSERT_EQ(outKey.dataLen, sizeof(g_generateOutKey));
291 }
292 
293 /**
294  * @tc.name: HuksHdiApiTest.ApiPassthroughTest002
295  * @tc.desc: Test Import key with current software huks driver(hardcoded root key);
296  * @tc.require:issueI77AT9
297  * @tc.type: FUNC
298  */
299 HWTEST_F(HuksHdiApiTest, ApiPassthroughTest002, TestSize.Level0)
300 {
301     ASSERT_NE(g_huksHdiProxy, nullptr);
302     ASSERT_NE(g_huksHdiProxy->ImportKey, nullptr);
303     struct HuksBlob keyAlias = {
304         .data = (uint8_t *)g_importKeyAlias,
305         .dataLen = sizeof(g_importKeyAlias)
306     };
307     struct HuksBlob key = {
308         .data = (uint8_t *)g_importKey,
309         .dataLen = sizeof(g_importKey)
310     };
311 
312     int ret = HuksFreshParamSet((struct HksParamSet *)g_importParamSet, false);
313     ASSERT_EQ(ret, HUKS_SUCCESS);
314     struct HuksParamSet paramSet = {
315         .data = (uint8_t *)g_importParamSet,
316         .dataLen = sizeof(g_importParamSet)
317     };
318     uint8_t outKeyBuffer[512];
319     struct HuksBlob outKey = {
320         .data = outKeyBuffer,
321         .dataLen = sizeof(outKeyBuffer)
322     };
323     ret = g_huksHdiProxy->ImportKey(g_huksHdiProxy, &keyAlias, &key, &paramSet, &outKey);
324     ASSERT_EQ(ret, HUKS_SUCCESS);
325     ASSERT_EQ(outKey.dataLen, sizeof(g_importOutKey));
326 }
327 
328 /**
329  * @tc.name: HuksHdiApiTest.ApiPassthroughTest003
330  * @tc.desc: Test init key with current software huks driver(hardcoded root key);
331  * @tc.require:issueI77AT9
332  * @tc.type: FUNC
333  */
334 HWTEST_F(HuksHdiApiTest, ApiPassthroughTest003, TestSize.Level0)
335 {
336     ASSERT_NE(g_huksHdiProxy, nullptr);
337     ASSERT_NE(g_huksHdiProxy->Init, nullptr);
338     struct HuksBlob key = {
339         .data = (uint8_t *)g_initKey,
340         .dataLen = sizeof(g_initKey)
341     };
342     int ret = HuksFreshParamSet((struct HksParamSet *)g_initParamSet, false);
343     ASSERT_EQ(ret, HUKS_SUCCESS);
344     struct HuksParamSet paramSet = {
345         .data = (uint8_t *)g_initParamSet,
346         .dataLen = sizeof(g_initParamSet)
347     };
348     uint8_t outHandleBuffer[12];
349     struct HuksBlob outHandle = {
350         .data = outHandleBuffer,
351         .dataLen = sizeof(outHandleBuffer)
352     };
353     uint8_t outHandleToken[32];
354     struct HuksBlob outToken = {
355         .data = outHandleToken,
356         .dataLen = sizeof(outHandleToken)
357     };
358     ret = g_huksHdiProxy->Init(g_huksHdiProxy, &key, &paramSet, &outHandle, &outToken);
359     ASSERT_EQ(ret, HUKS_SUCCESS);
360     ASSERT_EQ(outHandle.dataLen, 8);
361 }
362 
363 /**
364  * @tc.name: HuksHdiApiTest.MultiThreadTest001
365  * @tc.desc: Test init key with current software huks driver(hardcoded root key) in multi thread scenario;
366  * @tc.require:issueI77AT9
367  * @tc.type: FUNC
368  */
369 HWMTEST_F(HuksHdiApiTest, MultiThreadTest001, TestSize.Level0, 10)
370 {
371     std::thread::id thisId = std::this_thread::get_id();
372     std::ostringstream oss;
373     oss << thisId;
374     std::string thisIdString = oss.str();
375     long int thread_id = atol(thisIdString.c_str());
376     HUKS_TEST_LOG_I("running thread id:%ld start\n", thread_id);
377 
378     ASSERT_NE(g_huksHdiProxy, nullptr);
379     ASSERT_NE(g_huksHdiProxy->Init, nullptr);
380     struct HuksBlob key = {
381         .data = (uint8_t *)g_initKey,
382         .dataLen = sizeof(g_initKey)
383     };
384     struct HuksParamSet paramSet = {
385         .data = (uint8_t *)g_initParamSet,
386         .dataLen = sizeof(g_initParamSet)
387     };
388     uint8_t outHandleBuffer[12];
389     struct HuksBlob outHandle = {
390         .data = outHandleBuffer,
391         .dataLen = sizeof(outHandleBuffer)
392     };
393     uint8_t outHandleToken[32];
394     struct HuksBlob outToken = {
395         .data = outHandleToken,
396         .dataLen = sizeof(outHandleToken)
397     };
398     int ret = g_huksHdiProxy->Init(g_huksHdiProxy, &key, &paramSet, &outHandle, &outToken);
399     ASSERT_EQ(ret, HUKS_SUCCESS);
400     ASSERT_EQ(outHandle.dataLen, 8);
401     HUKS_TEST_LOG_I("running thread id:%ld end\n", thread_id);
402 }
403 }