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