• 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_sm4_test_common.h"
17 
18 #include <gtest/gtest.h>
19 using namespace testing::ext;
20 namespace Unittest::Sm4Cipher
21 {
HksSm4CipherTestEncrypt(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * encryptParamSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * cipherText)22 static OH_Huks_Result HksSm4CipherTestEncrypt(const struct OH_Huks_Blob *keyAlias,
23                                               const struct OH_Huks_ParamSet *encryptParamSet,
24                                               const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *cipherText)
25 {
26     uint8_t handleE[sizeof(uint64_t)] = {0};
27     struct OH_Huks_Blob handleEncrypt = {sizeof(uint64_t), handleE};
28     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
29     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
30     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
31         return ret;
32     }
33 
34     ret = TestUpdateLoopFinish(&handleEncrypt, encryptParamSet, inData, cipherText);
35     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateLoopFinish failed.";
36     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
37         return ret;
38     }
39     EXPECT_NE(HksMemCmp(inData->data, cipherText->data, inData->size), (int32_t)OH_HUKS_SUCCESS)
40         << "cipherText equals inData";
41 
42     return ret;
43 }
44 
HksSm4CipherTestDecrypt(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * decryptParamSet,const struct OH_Huks_Blob * cipherText,struct OH_Huks_Blob * plainText,const struct OH_Huks_Blob * inData)45 static OH_Huks_Result HksSm4CipherTestDecrypt(const struct OH_Huks_Blob *keyAlias,
46                                               const struct OH_Huks_ParamSet *decryptParamSet,
47                                               const struct OH_Huks_Blob *cipherText, struct OH_Huks_Blob *plainText,
48                                               const struct OH_Huks_Blob *inData)
49 {
50     uint8_t handleD[sizeof(uint64_t)] = {0};
51     struct OH_Huks_Blob handleDecrypt = {sizeof(uint64_t), handleD};
52     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
53     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
54     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
55         return ret;
56     }
57 
58     ret = TestUpdateLoopFinish(&handleDecrypt, decryptParamSet, cipherText, plainText);
59     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateLoopFinish failed.";
60     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
61         return ret;
62     }
63     EXPECT_EQ(HksMemCmp(inData->data, plainText->data, inData->size), (int32_t)OH_HUKS_SUCCESS)
64         << "plainText not equals inData";
65 
66     return ret;
67 }
HksSm4CipherTestCaseOther(const struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * encryptParamSet,struct OH_Huks_ParamSet * decryptParamSet)68 OH_Huks_Result HksSm4CipherTestCaseOther(const struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *genParamSet,
69                                          struct OH_Huks_ParamSet *encryptParamSet,
70                                          struct OH_Huks_ParamSet *decryptParamSet)
71 {
72     char tmpInData[] = "SM4_ECB_INDATA_1";
73     struct OH_Huks_Blob inData = {g_inData.length(), (uint8_t *)g_inData.c_str()};
74 
75     struct OH_Huks_Param *modeParam = nullptr;
76     OH_Huks_GetParam(genParamSet, OH_HUKS_TAG_BLOCK_MODE, &modeParam);
77     if (modeParam->uint32Param == OH_HUKS_MODE_ECB) {
78         inData.size = strlen(tmpInData);
79         inData.data = (uint8_t *)tmpInData;
80     }
81 
82     /* 1. Generate Key */
83     OH_Huks_Result ret = OH_Huks_GenerateKeyItem(keyAlias, genParamSet, nullptr);
84     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
85         return ret;
86     }
87 
88     /* 2. Encrypt */
89     uint8_t cipher[SM4_COMMON_SIZE] = {0};
90     struct OH_Huks_Blob cipherText = {SM4_COMMON_SIZE, cipher};
91     ret = HksSm4CipherTestEncrypt(keyAlias, encryptParamSet, &inData, &cipherText);
92     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksAesCipherTestEncrypt failed.";
93 
94     /* 3. Decrypt Three Stage */
95     uint8_t plain[SM4_COMMON_SIZE] = {0};
96     struct OH_Huks_Blob plainText = {SM4_COMMON_SIZE, plain};
97     ret = HksSm4CipherTestDecrypt(keyAlias, decryptParamSet, &cipherText, &plainText, &inData);
98     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksAesCipherTestDecrypt failed.";
99 
100     /* 3. Delete Key */
101     EXPECT_EQ(OH_Huks_DeleteKeyItem(keyAlias, genParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
102     return ret;
103 }
104 }  // namespace Unittest::Sm4Cipher
105