1 /*
2  * Copyright (c) 2021 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 "hks_test_adapt_for_de.h"
17 #include "hks_test_aes_c.h"
18 
19 #define TEST_AES_32 32
20 
TestAes256ByLocal()21 int32_t TestAes256ByLocal()
22 {
23     HKS_TEST_LOG_I("enter");
24 
25     /* generate aes key 1 */
26     char testKey[TEST_AES_32];
27     struct HksBlob keyBlob;
28 
29     for (int i = 0; i < TEST_AES_32; ++i) {
30         testKey[i] = i + TEST_AES_32;
31     }
32 
33     keyBlob.data = (uint8_t *)testKey;
34     keyBlob.size = sizeof(testKey);
35 
36     /* encrypt by aes key 1 */
37     struct HksParamSet *paramSet = NULL;
38     struct HksParam algParam = {
39         .tag = HKS_TAG_IS_KEY_ALIAS,
40         .uint32Param = 0
41     };
42 
43     int32_t ret = ConstructParamSetEncryptDecryptAes(HKS_MODE_GCM, HKS_PADDING_NONE, true, &paramSet);
44     HKS_TEST_ASSERT(ret == 0);
45 
46     ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1);
47     if (ret != 0) {
48         HKS_TEST_LOG_E("HksAddParam algParam failed!\n");
49         HksFreeParamSet(&paramSet);
50         return ret;
51     }
52 
53     struct HksBlob plainText1 = { strlen(TEST_PLAIN_TEST) + 1, (uint8_t*)TEST_PLAIN_TEST };
54     struct HksBlob cipherText1 = { TEST_AES_256, g_buffer };
55     (void)memset_s(cipherText1.data, cipherText1.size, 0, cipherText1.size);
56     HKS_TEST_ASSERT(HksEncryptForDe(&keyBlob, paramSet, &plainText1, &cipherText1) == 0);
57     g_bufferSize = cipherText1.size;
58 
59     HksFreeParamSet(&paramSet);
60 
61     /* decrypt by aes key 2 */
62     ret = ConstructParamSetEncryptDecryptAes(HKS_MODE_GCM, HKS_PADDING_NONE, false, &paramSet);
63     HKS_TEST_ASSERT(ret == 0);
64     algParam.tag = HKS_TAG_IS_KEY_ALIAS;
65     algParam.uint32Param = 0;
66     ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1);
67     if (ret != 0) {
68         HKS_TEST_LOG_E("HksAddParam algParam failed!\n");
69         HksFreeParamSet(&paramSet);
70         return ret;
71     }
72 
73     struct HksBlob cipherText = { g_bufferSize, g_buffer };
74     uint8_t tmp[TEST_AES_256] = {0};
75     struct HksBlob plainText = { TEST_AES_256, tmp };
76     ret = HksDecryptForDe(&keyBlob, paramSet, &cipherText, &plainText);
77     HKS_TEST_ASSERT(ret == 0);
78     HKS_TEST_ASSERT(plainText1.size == plainText.size);
79     HKS_TEST_ASSERT(memcmp(plainText.data, plainText1.data, plainText.size) == 0);
80     HksFreeParamSet(&paramSet);
81 
82     HKS_TEST_LOG_I("end");
83     return ret;
84 }
85 
86