• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 
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 using namespace testing::ext;
28 namespace {
29 class HksBnExpModTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32 
33     static void TearDownTestCase(void);
34 
35     void SetUp();
36 
37     void TearDown();
38 };
39 
SetUpTestCase(void)40 void HksBnExpModTest::SetUpTestCase(void)
41 {
42 }
43 
TearDownTestCase(void)44 void HksBnExpModTest::TearDownTestCase(void)
45 {
46 }
47 
SetUp()48 void HksBnExpModTest::SetUp()
49 {
50     EXPECT_EQ(HksInitialize(), 0);
51 }
52 
TearDown()53 void HksBnExpModTest::TearDown()
54 {
55 }
56 
57 const int DEFAULT_X_SIZE = 256;
58 const int DEFAULT_A_SIZE = 256;
59 const int DEFAULT_E_SIZE = 256;
60 const int DEFAULT_N_SIZE = 256;
61 const int HKS_TEST_2 = 2;
62 const int HKS_TEST_8 = 8;
63 
64 const struct HksTestBnExpModParams g_testBnExpModParams[] = {
65     /* normal case */
66     { 0, HKS_SUCCESS, false,
67         { true, DEFAULT_X_SIZE, true, DEFAULT_X_SIZE },
68         { true, DEFAULT_A_SIZE, true, DEFAULT_A_SIZE },
69         { true, DEFAULT_E_SIZE, true, DEFAULT_E_SIZE },
70         { true, DEFAULT_N_SIZE, true, DEFAULT_N_SIZE }
71     },
72 };
73 
TestValue()74 static int32_t TestValue()
75 {
76     HKS_TEST_LOG_I("test value");
77     uint8_t bufX[HKS_TEST_8] = { 0, 0, 0, 0, 0, 0, 0, 0x40 };
78     uint8_t bufA[HKS_TEST_8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
79     uint8_t bufE[HKS_TEST_2] = { 0, 2 };
80     uint8_t bufN[HKS_TEST_8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
81     uint8_t tmpBufX[HKS_TEST_8] = {0};
82     struct HksBlob tmpX = { HKS_TEST_8, tmpBufX };
83     struct HksBlob tmpA = { HKS_TEST_8, bufA };
84     struct HksBlob tmpE = { HKS_TEST_2, bufE };
85     struct HksBlob tmpN = { HKS_TEST_8, bufN };
86     int32_t ret = HksBnExpModRun(&tmpX, &tmpA, &tmpE, &tmpN, 1);
87     for (int i = 0; i < HKS_TEST_8; ++i) {
88         HKS_TEST_LOG_I("%x, %x", tmpBufX[i], bufX[i]);
89         HKS_TEST_ASSERT(tmpBufX[i] == bufX[i]);
90     }
91     return ret;
92 }
93 
94 /**
95  * @tc.name: HksBnExpModTest.HksBnExpModTest001
96  * @tc.desc: The static function will return true;
97  * @tc.type: FUNC
98  */
99 HWTEST_F(HksBnExpModTest, HksBnExpModTest001, TestSize.Level1)
100 {
101     int32_t ret;
102     if (g_testBnExpModParams[0].isTestValue) {
103         ret = TestValue();
104         ASSERT_TRUE(ret == 0);
105     } else {
106         struct HksBlob *x = NULL;
107         struct HksBlob *a = NULL;
108         struct HksBlob *e = NULL;
109         struct HksBlob *n = NULL;
110         ret = TestConstuctBlob(&x, g_testBnExpModParams[0].xParams.blobExist,
111             g_testBnExpModParams[0].xParams.blobSize, g_testBnExpModParams[0].xParams.blobDataExist,
112             g_testBnExpModParams[0].xParams.blobDataSize);
113         HKS_TEST_ASSERT(ret == 0);
114 
115         ret = TestConstructBlobOut(&a, g_testBnExpModParams[0].aParams.blobExist,
116             g_testBnExpModParams[0].aParams.blobSize,  g_testBnExpModParams[0].aParams.blobDataExist,
117             g_testBnExpModParams[0].aParams.blobDataSize);
118         HKS_TEST_ASSERT(ret == 0);
119 
120         ret = TestConstuctBlob(&e, g_testBnExpModParams[0].eParams.blobExist,
121             g_testBnExpModParams[0].eParams.blobSize, g_testBnExpModParams[0].eParams.blobDataExist,
122             g_testBnExpModParams[0].eParams.blobDataSize);
123         HKS_TEST_ASSERT(ret == 0);
124 
125         ret = TestConstuctBlob(&n, g_testBnExpModParams[0].nParams.blobExist,
126             g_testBnExpModParams[0].nParams.blobSize, g_testBnExpModParams[0].nParams.blobDataExist,
127             g_testBnExpModParams[0].nParams.blobDataSize);
128         HKS_TEST_ASSERT(ret == 0);
129         if ((n != NULL) && (n->data != NULL) && (n->size != 0)) {
130             n->data[n->size - 1] = n->data[n->size - 1] | 0x00000001; /* make sure n is odd */
131         }
132 
133         ret = HksBnExpModRun(x, a, e, n, 1);
134 
135         HKS_TEST_ASSERT(ret == g_testBnExpModParams[0].expectResult);
136 
137         TestFreeBlob(&x);
138         TestFreeBlob(&a);
139         TestFreeBlob(&e);
140         TestFreeBlob(&n);
141         ASSERT_TRUE(ret == 0);
142     }
143 }
144 }