• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "huks_cipher_rsa_test.h"
17 #include "huks_cipher_rsa_test_common.h"
18 
19 #include <gtest/gtest.h>
20 
21 using namespace testing::ext;
22 namespace Unittest::RsaCipher {
23 class HuksCipherRSATest : public testing::Test {
24 public:
25     static void SetUpTestCase(void);
26 
27     static void TearDownTestCase(void);
28 
29     void SetUp();
30 
31     void TearDown();
32 };
33 
SetUpTestCase(void)34 void HuksCipherRSATest::SetUpTestCase(void)
35 {
36 }
37 
TearDownTestCase(void)38 void HuksCipherRSATest::TearDownTestCase(void)
39 {
40 }
41 
SetUp()42 void HuksCipherRSATest::SetUp()
43 {
44 
45 }
46 
TearDown()47 void HuksCipherRSATest::TearDown()
48 {
49 }
50 
51 static struct OH_Huks_Param g_genParams041[] = {
52     {
53         .tag = OH_HUKS_TAG_ALGORITHM,
54         .uint32Param = OH_HUKS_ALG_RSA
55     }, {
56         .tag = OH_HUKS_TAG_PURPOSE,
57         .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT
58     }, {
59         .tag = OH_HUKS_TAG_KEY_SIZE,
60         .uint32Param = OH_HUKS_RSA_KEY_SIZE_4096
61     }, {
62         .tag = OH_HUKS_TAG_PADDING,
63         .uint32Param = OH_HUKS_PADDING_OAEP
64     }, {
65         .tag = OH_HUKS_TAG_DIGEST,
66         .uint32Param = OH_HUKS_DIGEST_SHA384
67     }, {
68         .tag = OH_HUKS_TAG_BLOCK_MODE,
69         .uint32Param = OH_HUKS_MODE_ECB
70     }
71 };
72 static struct OH_Huks_Param g_encryptParams041[] = {
73     {
74         .tag = OH_HUKS_TAG_ALGORITHM,
75         .uint32Param = OH_HUKS_ALG_RSA
76     }, {
77         .tag = OH_HUKS_TAG_PURPOSE,
78         .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT
79     }, {
80         .tag = OH_HUKS_TAG_KEY_SIZE,
81         .uint32Param = OH_HUKS_RSA_KEY_SIZE_4096
82     }, {
83         .tag = OH_HUKS_TAG_PADDING,
84         .uint32Param = OH_HUKS_PADDING_OAEP
85     }, {
86         .tag = OH_HUKS_TAG_DIGEST,
87         .uint32Param = OH_HUKS_DIGEST_SHA384
88     }, {
89         .tag = OH_HUKS_TAG_BLOCK_MODE,
90         .uint32Param = OH_HUKS_MODE_ECB
91     }
92 };
93 static struct OH_Huks_Param g_decryptParams041[] = {
94     {
95         .tag = OH_HUKS_TAG_ALGORITHM,
96         .uint32Param = OH_HUKS_ALG_RSA
97     }, {
98         .tag = OH_HUKS_TAG_PURPOSE,
99         .uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT
100     }, {
101         .tag = OH_HUKS_TAG_KEY_SIZE,
102         .uint32Param = OH_HUKS_RSA_KEY_SIZE_4096
103     }, {
104         .tag = OH_HUKS_TAG_PADDING,
105         .uint32Param = OH_HUKS_PADDING_OAEP
106     }, {
107         .tag = OH_HUKS_TAG_DIGEST,
108         .uint32Param = OH_HUKS_DIGEST_SHA384
109     }, {
110         .tag = OH_HUKS_TAG_BLOCK_MODE,
111         .uint32Param = OH_HUKS_MODE_ECB
112     }
113 };
114 
115 /**
116  * @tc.name: HuksCipherRSATest.Security_HUKS_NAPI_Cipher_RSA_0100
117  * @tc.desc: alg-RSA pur-ENCRYPT-DECRYPT size-4096 pad-OAEP dig-SHA384 mode-ECB.
118  * @tc.type: FUNC
119  */
120 HWTEST_F(HuksCipherRSATest, Security_HUKS_NAPI_Cipher_RSA_0100, TestSize.Level1)
121 {
122     char tmpKeyAlias[] = "HksRSACipherKeyAliasTest041";
123     struct OH_Huks_Blob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
124     struct OH_Huks_Blob inData = { g_inData_32.length(),
125                               (uint8_t *)g_inData_32.c_str() };
126 
127     struct OH_Huks_ParamSet *genParamSet = nullptr;
128     OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams041, sizeof(g_genParams041) / sizeof(OH_Huks_Param));
129     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(gen) failed.";
130 
131     struct OH_Huks_ParamSet *encryptParamSet = nullptr;
132     ret = InitParamSet(&encryptParamSet, g_encryptParams041, sizeof(g_encryptParams041) / sizeof(OH_Huks_Param));
133     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(encrypt) failed.";
134 
135     struct OH_Huks_ParamSet *decryptParamSet = nullptr;
136     ret = InitParamSet(&decryptParamSet, g_decryptParams041, sizeof(g_decryptParams041) / sizeof(OH_Huks_Param));
137     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(decrypt) failed.";
138 
139     ret = HksRsaCipherTestCase(&keyAlias, genParamSet, encryptParamSet, decryptParamSet, &inData);
140     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "this case failed.";
141 
142     OH_Huks_FreeParamSet(&genParamSet);
143     OH_Huks_FreeParamSet(&encryptParamSet);
144     OH_Huks_FreeParamSet(&decryptParamSet);
145 }
146 } // namespace Unittest::RsaCipher