• 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_aes_test_common.h"
17 
18 #include <gtest/gtest.h>
19 
20 using namespace testing::ext;
21 namespace Unittest::AesCipher
22 {
HksAesCipherTestEncrypt(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * encryptParamSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * cipherText)23     OH_Huks_Result HksAesCipherTestEncrypt(
24         const struct OH_Huks_Blob *keyAlias,
25         const struct OH_Huks_ParamSet *encryptParamSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *cipherText)
26     {
27         uint8_t handleE[sizeof(uint64_t)] = {0};
28         struct OH_Huks_Blob handleEncrypt = {sizeof(uint64_t), handleE};
29         OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
30         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
31         if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
32         {
33             return ret;
34         }
35 
36         ret = TestUpdateLoopFinish(&handleEncrypt, encryptParamSet, inData, cipherText);
37         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateLoopFinish failed.";
38         if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
39         {
40             return ret;
41         }
42         EXPECT_NE(HksMemCmp(inData->data, cipherText->data, inData->size), (int32_t)OH_HUKS_SUCCESS) << "cipherText equals inData";
43 
44         return ret;
45     }
46 
HksAesCipherTestDecrypt(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)47     OH_Huks_Result HksAesCipherTestDecrypt(
48         const struct OH_Huks_Blob *keyAlias,
49         const struct OH_Huks_ParamSet *decryptParamSet, const struct OH_Huks_Blob *cipherText, struct OH_Huks_Blob *plainText,
50         const struct OH_Huks_Blob *inData)
51     {
52         uint8_t handleD[sizeof(uint64_t)] = {0};
53         struct OH_Huks_Blob handleDecrypt = {sizeof(uint64_t), handleD};
54         OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
55         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
56         if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
57         {
58             return ret;
59         }
60 
61         ret = TestUpdateLoopFinish(&handleDecrypt, decryptParamSet, cipherText, plainText);
62         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateLoopFinish failed.";
63         if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
64         {
65             return ret;
66         }
67         EXPECT_EQ(HksMemCmp(inData->data, plainText->data, inData->size), (int32_t)OH_HUKS_SUCCESS) << "plainText not equals inData";
68 
69         return ret;
70     }
71 
HksAesCipherTestCaseOther(const struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * encryptParamSet,struct OH_Huks_ParamSet * decryptParamSet)72     OH_Huks_Result HksAesCipherTestCaseOther(
73         const struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *genParamSet,
74         struct OH_Huks_ParamSet *encryptParamSet, struct OH_Huks_ParamSet *decryptParamSet)
75     {
76         char tmpInData[] = "AES_ECB_INDATA_1";
77         struct OH_Huks_Blob inData = {
78             g_inData.length(),
79             (uint8_t *)g_inData.c_str()};
80 
81         struct OH_Huks_Param *modeParam = nullptr;
82         OH_Huks_Result ret = OH_Huks_GetParam(genParamSet, OH_HUKS_TAG_BLOCK_MODE, &modeParam);
83         if (modeParam->uint32Param == OH_HUKS_MODE_ECB)
84         {
85             inData.size = strlen(tmpInData);
86             inData.data = (uint8_t *)tmpInData;
87         }
88 
89         /* 1. Generate Key */
90         ret = OH_Huks_GenerateKeyItem(keyAlias, genParamSet, nullptr);
91         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
92         if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
93         {
94             return ret;
95         }
96 
97         /* 2. Encrypt */
98         uint8_t cipher[AES_COMMON_SIZE] = {0};
99         struct OH_Huks_Blob cipherText = {AES_COMMON_SIZE, cipher};
100         ret = HksAesCipherTestEncrypt(keyAlias, encryptParamSet, &inData, &cipherText);
101         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksAesCipherTestEncrypt failed.";
102 
103         /* 3. Decrypt Three Stage */
104         uint8_t plain[AES_COMMON_SIZE] = {0};
105         struct OH_Huks_Blob plainText = {AES_COMMON_SIZE, plain};
106         ret = HksAesCipherTestDecrypt(keyAlias, decryptParamSet, &cipherText, &plainText, &inData);
107         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksAesCipherTestDecrypt failed.";
108 
109         /* 3. Delete Key */
110         EXPECT_EQ(OH_Huks_DeleteKeyItem(keyAlias, genParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
111         return ret;
112     }
113 
HksAesCipherTestCaseGcm2(const struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * encryptParamSet,struct OH_Huks_ParamSet * decryptParamSet,struct OH_Huks_ParamSet * decrypt1ParamSet)114     OH_Huks_Result HksAesCipherTestCaseGcm2(
115         const struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *genParamSet,
116         struct OH_Huks_ParamSet *encryptParamSet, struct OH_Huks_ParamSet *decryptParamSet, struct OH_Huks_ParamSet *decrypt1ParamSet)
117     {
118         struct OH_Huks_Blob inData = {
119             g_inData.length(),
120             (uint8_t *)g_inData.c_str()};
121 
122         /* 1. Generate Key */
123         OH_Huks_Result ret = OH_Huks_GenerateKeyItem(keyAlias, genParamSet, nullptr);
124         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
125         if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
126         {
127             return ret;
128         }
129 
130         /* 2. Encrypt Three Stage */
131         uint8_t cipher[AES_COMMON_SIZE] = {0};
132         struct OH_Huks_Blob cipherText = {AES_COMMON_SIZE, cipher};
133         ret = HksAesCipherTestEncrypt(keyAlias, encryptParamSet, &inData, &cipherText);
134         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksAesCipherTestEncrypt failed.";
135 
136         cipherText.size -= AEAD_SIZE;
137 
138         uint32_t i = 0;
139         for (i = 0; i < decryptParamSet->paramsCnt; i++)
140         {
141             if (decryptParamSet->params[i].tag == OH_HUKS_TAG_KEY_FLAG)
142             {
143                 uint8_t *tempPtr = cipherText.data;
144                 (void)memcpy_s(decryptParamSet->params[i].blob.data, AEAD_SIZE,
145                                tempPtr + cipherText.size, AEAD_SIZE);
146                 break;
147             }
148         }
149 
150         /* 3. Decrypt Three Stage */
151         // Init
152         uint8_t handleD[sizeof(uint64_t)] = {0};
153         struct OH_Huks_Blob handleDecrypt = {sizeof(uint64_t), handleD};
154         ret = OH_Huks_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
155         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
156 
157         // Update & Finish
158         uint8_t plain[AES_COMMON_SIZE] = {0};
159         struct OH_Huks_Blob plainText = {AES_COMMON_SIZE, plain};
160         ret = TestUpdateLoopFinish(&handleDecrypt, decryptParamSet, &cipherText, &plainText);
161         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateLoopFinish failed.";
162         EXPECT_EQ(HksMemCmp(inData.data, plainText.data, inData.size), (int32_t)OH_HUKS_SUCCESS) << "plainText not equals inData";
163 
164         /* 3. Delete Key */
165         EXPECT_EQ(OH_Huks_DeleteKeyItem(keyAlias, genParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
166         return ret;
167     }
168 }