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