• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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_modify_key_test_c.h"
17 #include "hks_test_adapt_for_de.h"
18 
ConstructDataToBlob(struct HksBlob ** inData,struct HksBlob ** outData,const struct HksTestBlobParams * inTextParams,const struct HksTestBlobParams * outTextParams)19 int32_t ConstructDataToBlob(struct HksBlob **inData, struct HksBlob **outData,
20     const struct HksTestBlobParams *inTextParams, const struct HksTestBlobParams *outTextParams)
21 {
22     int32_t ret = TestConstuctBlob(inData,
23         inTextParams->blobExist,
24         inTextParams->blobSize, inTextParams->blobDataExist,
25         inTextParams->blobDataSize);
26     HKS_TEST_ASSERT(ret == 0);
27 
28     ret = TestConstuctBlob(outData,
29         outTextParams->blobExist,
30         outTextParams->blobSize,
31         outTextParams->blobDataExist, outTextParams->blobDataSize);
32     HKS_TEST_ASSERT(ret == 0);
33     return ret;
34 }
35 
Encrypt(struct CipherEncryptStructure * encryptStruct)36 int32_t Encrypt(struct CipherEncryptStructure *encryptStruct)
37 {
38     int32_t ret;
39     struct HksParamSet *encryptParamSet = NULL;
40 
41     uint32_t ivSize = encryptStruct->cipherParms->ivSize;
42     uint32_t nonceSize = encryptStruct->cipherParms->nonceSize;
43     uint32_t aadSize = encryptStruct->cipherParms->aadSize;
44     if (ivSize != 0) {
45         ret = TestConstuctBlob(encryptStruct->ivData, true, ivSize, true, ivSize);
46         HKS_TEST_ASSERT(ret == 0);
47     }
48     if (nonceSize != 0) {
49         ret = TestConstuctBlob(encryptStruct->nonceData, true, nonceSize, true, nonceSize);
50         HKS_TEST_ASSERT(ret == 0);
51     }
52     if (aadSize != 0) {
53         ret = TestConstuctBlob(encryptStruct->aadData, true, aadSize, true, aadSize);
54         HKS_TEST_ASSERT(ret == 0);
55     }
56     struct AesCipherParamSetStructure enParamStruct = {
57         &encryptParamSet,
58         encryptStruct->cipherParms->paramSetExist,
59         encryptStruct->cipherParms->setAlg, encryptStruct->cipherParms->alg,
60         encryptStruct->cipherParms->setPurpose, encryptStruct->cipherParms->purpose,
61         encryptStruct->cipherParms->setPadding, encryptStruct->cipherParms->padding,
62         encryptStruct->cipherParms->setBlockMode, encryptStruct->cipherParms->mode,
63         encryptStruct->cipherParms->setIv, *(encryptStruct->ivData),
64         encryptStruct->cipherParms->setNonce, *(encryptStruct->nonceData),
65         encryptStruct->cipherParms->setAad, *(encryptStruct->aadData),
66         encryptStruct->cipherParms->setIsKeyAlias, encryptStruct->cipherParms->isKeyAlias
67     };
68     ret = TestConstructAesCipherParamSet(&enParamStruct);
69     HKS_TEST_ASSERT(ret == 0);
70 
71     ret = HksEncryptRun(encryptStruct->keyAlias, encryptParamSet, encryptStruct->plainData, encryptStruct->cipherData,
72         encryptStruct->performTimes);
73     HksFreeParamSet(&encryptParamSet);
74     return ret;
75 }
76 
DecryptCipher(struct CipherDecryptStructure * decryptStruct)77 int32_t DecryptCipher(struct CipherDecryptStructure *decryptStruct)
78 {
79     int32_t ret = TestConstuctBlob(decryptStruct->decryptedData,
80         decryptStruct->cipherParms->decryptedTextParams.blobExist,
81         decryptStruct->cipherParms->decryptedTextParams.blobSize,
82         decryptStruct->cipherParms->decryptedTextParams.blobDataExist,
83         decryptStruct->cipherParms->decryptedTextParams.blobDataSize);
84     HKS_TEST_ASSERT(ret == 0);
85 
86     struct HksParamSet *decryptParamSet = NULL;
87     struct AesCipherParamSetStructure deParamStruct = {
88         &decryptParamSet, decryptStruct->cipherParms->decryptParamSetParams.paramSetExist,
89         decryptStruct->cipherParms->decryptParamSetParams.setAlg,
90         decryptStruct->cipherParms->decryptParamSetParams.alg,
91         decryptStruct->cipherParms->decryptParamSetParams.setPurpose,
92         decryptStruct->cipherParms->decryptParamSetParams.purpose,
93         decryptStruct->cipherParms->decryptParamSetParams.setPadding,
94         decryptStruct->cipherParms->decryptParamSetParams.padding,
95         decryptStruct->cipherParms->decryptParamSetParams.setBlockMode,
96         decryptStruct->cipherParms->decryptParamSetParams.mode,
97         decryptStruct->cipherParms->decryptParamSetParams.setIv,
98         decryptStruct->ivData,
99         decryptStruct->cipherParms->decryptParamSetParams.setNonce, decryptStruct->nonceData,
100         decryptStruct->cipherParms->decryptParamSetParams.setAad, decryptStruct->aadData,
101         decryptStruct->cipherParms->decryptParamSetParams.setIsKeyAlias,
102         decryptStruct->cipherParms->decryptParamSetParams.isKeyAlias
103     };
104     ret = TestConstructAesCipherParamSet(&deParamStruct);
105     HKS_TEST_ASSERT(ret == 0);
106 
107     ret = HksDecryptRun(decryptStruct->keyAlias, decryptParamSet, decryptStruct->cipherData,
108         *(decryptStruct->decryptedData), decryptStruct->performTimes);
109     HksFreeParamSet(&decryptParamSet);
110     return ret;
111 }
112 
GenerateKeyTwo(struct HksBlob * keyAlias,const struct HksTestBlobParams * keyAliasParams,const struct HksTestGenKeyParamsParamSet * genKeyParamSetParams,const struct HksTestGenKeyParamsParamSetOut * genKeyParamSetParamsOut)113 int32_t GenerateKeyTwo(struct HksBlob *keyAlias, const struct HksTestBlobParams *keyAliasParams,
114     const struct HksTestGenKeyParamsParamSet *genKeyParamSetParams,
115     const struct HksTestGenKeyParamsParamSetOut *genKeyParamSetParamsOut)
116 {
117     struct HksParamSet *paramSet = NULL;
118     struct GenerateKeyParamSetStructure paramStruct = {
119         &paramSet,
120         genKeyParamSetParams->paramSetExist,
121         genKeyParamSetParams->setAlg, genKeyParamSetParams->alg,
122         genKeyParamSetParams->setKeySize, genKeyParamSetParams->keySize,
123         genKeyParamSetParams->setPurpose, genKeyParamSetParams->purpose,
124         genKeyParamSetParams->setDigest, genKeyParamSetParams->digest,
125         genKeyParamSetParams->setPadding, genKeyParamSetParams->padding,
126         genKeyParamSetParams->setBlockMode, genKeyParamSetParams->mode,
127         genKeyParamSetParams->setKeyStorageFlag, genKeyParamSetParams->keyStorageFlag
128     };
129     int32_t ret = TestConstructGenerateKeyParamSet(&paramStruct);
130     HKS_TEST_ASSERT(ret == 0);
131 
132     struct HksParamSet *paramSetOut = NULL;
133     if (genKeyParamSetParamsOut != NULL) {
134         ret = TestConstructGenerateKeyParamSetOut(&paramSet,
135             genKeyParamSetParamsOut->paramSetExist, genKeyParamSetParamsOut->paramSetSize);
136         HKS_TEST_ASSERT(ret == 0);
137     }
138 
139     ret = HksGenerateKeyForDe(keyAlias, paramSet, paramSetOut);
140     HKS_TEST_ASSERT(ret == 0);
141 
142     HksFreeParamSet(&paramSet);
143     return ret;
144 }
145