1 /*
2 * Copyright (C) 2021 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 "hctest.h"
17
18 #include "hks_bn_exp_mod_test.h"
19 #include "hks_api.h"
20 #include "hks_param.h"
21 #include "hks_test_api_performance.h"
22 #include "hks_test_common.h"
23 #include "hks_test_log.h"
24 #include "hks_type.h"
25 #include "cmsis_os2.h"
26 #include "ohos_types.h"
27
28 #define TEST_TASK_STACK_SIZE 0x2000
29 #define WAIT_TO_TEST_DONE 4
30
31 #include <unistd.h>
32 static osPriority_t g_setPriority;
33
34 /*
35 * @tc.register: register a test suit named "CalcMultiTest"
36 * @param: test subsystem name
37 * @param: c_example module name
38 * @param: CalcMultiTest test suit name
39 */
40 LITE_TEST_SUIT(security, securityData, HksBnExpModTest);
41
ExecHksInitialize(void const * argument)42 static void ExecHksInitialize(void const *argument)
43 {
44 LiteTestPrint("HksInitialize Begin!\n");
45 TEST_ASSERT_TRUE(HksInitialize() == 0);
46 LiteTestPrint("HksInitialize End!\n");
47 osThreadExit();
48 }
49 /**
50 * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
51 * @return: true——setup success
52 */
HksBnExpModTestSetUp()53 static BOOL HksBnExpModTestSetUp()
54 {
55 LiteTestPrint("setup\n");
56 osThreadId_t id;
57 osThreadAttr_t attr;
58 g_setPriority = osPriorityAboveNormal6;
59 attr.name = "test";
60 attr.attr_bits = 0U;
61 attr.cb_mem = NULL;
62 attr.cb_size = 0U;
63 attr.stack_mem = NULL;
64 attr.stack_size = TEST_TASK_STACK_SIZE;
65 attr.priority = g_setPriority;
66 id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
67 sleep(WAIT_TO_TEST_DONE);
68 LiteTestPrint("HksBnExpModTestSetUp End2!\n");
69 return TRUE;
70 }
71
72 /**
73 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
74 * @return: true——teardown success
75 */
HksBnExpModTestTearDown()76 static BOOL HksBnExpModTestTearDown()
77 {
78 LiteTestPrint("tearDown\n");
79 return TRUE;
80 }
81
82
83 #define DEFAULT_X_SIZE 256
84 #define DEFAULT_A_SIZE 256
85 #define DEFAULT_E_SIZE 256
86 #define DEFAULT_N_SIZE 256
87 #define HKS_TEST_2 2
88 #define HKS_TEST_8 8
89
90 static const struct HksTestBnExpModParams g_testBnExpModParams[] = {
91 /* normal case */
92 { 0, HKS_SUCCESS, false,
93 { true, DEFAULT_X_SIZE, true, DEFAULT_X_SIZE },
94 { true, DEFAULT_A_SIZE, true, DEFAULT_A_SIZE },
95 { true, DEFAULT_E_SIZE, true, DEFAULT_E_SIZE },
96 { true, DEFAULT_N_SIZE, true, DEFAULT_N_SIZE }
97 },
98 };
99
TestValue()100 static int32_t TestValue()
101 {
102 HKS_TEST_LOG_I("test value");
103 uint8_t bufX[HKS_TEST_8] = { 0, 0, 0, 0, 0, 0, 0, 0x40 };
104 uint8_t bufA[HKS_TEST_8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
105 uint8_t bufE[HKS_TEST_2] = { 0, 2 };
106 uint8_t bufN[HKS_TEST_8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
107 uint8_t tmpBufX[HKS_TEST_8] = {0};
108 struct HksBlob tmpX = { HKS_TEST_8, tmpBufX };
109 struct HksBlob tmpA = { HKS_TEST_8, bufA };
110 struct HksBlob tmpE = { HKS_TEST_2, bufE };
111 struct HksBlob tmpN = { HKS_TEST_8, bufN };
112 int32_t ret = HksBnExpModRun(&tmpX, &tmpA, &tmpE, &tmpN, 1);
113 for (int i = 0; i < HKS_TEST_8; ++i) {
114 HKS_TEST_LOG_I("%x, %x", tmpBufX[i], bufX[i]);
115 HKS_TEST_ASSERT(tmpBufX[i] == bufX[i]);
116 }
117 return ret;
118 }
119
120 /**
121 * @tc.name: HksBnExpModTest.HksBnExpModTest001
122 * @tc.desc: The static function will return true;
123 * @tc.type: FUNC
124 */
LITE_TEST_CASE(HksBnExpModTest,HksBnExpModTest001,Level1)125 LITE_TEST_CASE(HksBnExpModTest, HksBnExpModTest001, Level1)
126 {
127 int32_t ret;
128 if (g_testBnExpModParams[0].isTestValue) {
129 ret = TestValue();
130 TEST_ASSERT_TRUE(ret == 0);
131 } else {
132 struct HksBlob *x = NULL;
133 struct HksBlob *a = NULL;
134 struct HksBlob *e = NULL;
135 struct HksBlob *n = NULL;
136 ret = TestConstuctBlob(&x, g_testBnExpModParams[0].xParams.blobExist,
137 g_testBnExpModParams[0].xParams.blobSize, g_testBnExpModParams[0].xParams.blobDataExist,
138 g_testBnExpModParams[0].xParams.blobDataSize);
139 TEST_ASSERT_TRUE(ret == 0);
140
141 ret = TestConstructBlobOut(&a, g_testBnExpModParams[0].aParams.blobExist,
142 g_testBnExpModParams[0].aParams.blobSize, g_testBnExpModParams[0].aParams.blobDataExist,
143 g_testBnExpModParams[0].aParams.blobDataSize);
144 TEST_ASSERT_TRUE(ret == 0);
145
146 ret = TestConstuctBlob(&e, g_testBnExpModParams[0].eParams.blobExist,
147 g_testBnExpModParams[0].eParams.blobSize, g_testBnExpModParams[0].eParams.blobDataExist,
148 g_testBnExpModParams[0].eParams.blobDataSize);
149 TEST_ASSERT_TRUE(ret == 0);
150
151 ret = TestConstuctBlob(&n, g_testBnExpModParams[0].nParams.blobExist,
152 g_testBnExpModParams[0].nParams.blobSize, g_testBnExpModParams[0].nParams.blobDataExist,
153 g_testBnExpModParams[0].nParams.blobDataSize);
154 TEST_ASSERT_TRUE(ret == 0);
155 if ((n != NULL) && (n->data != NULL) && (n->size != 0)) {
156 n->data[n->size - 1] = n->data[n->size - 1] | 0x00000001; /* make sure n is odd */
157 }
158
159 ret = HksBnExpModRun(x, a, e, n, 1);
160
161 TEST_ASSERT_TRUE(ret == g_testBnExpModParams[0].expectResult);
162
163 TestFreeBlob(&x);
164 TestFreeBlob(&a);
165 TestFreeBlob(&e);
166 TestFreeBlob(&n);
167 TEST_ASSERT_TRUE(ret == 0);
168 }
169 }
170
171 RUN_TEST_SUITE(HksBnExpModTest);
172