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