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