• 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 "aes_openssl_common.h"
17 
18 #include "log.h"
19 #include "memory.h"
20 #include "result.h"
21 
GetIv(HcfParamsSpec * params)22 const unsigned char *GetIv(HcfParamsSpec *params)
23 {
24     if (params == NULL) {
25         return NULL;
26     }
27     HcfIvParamsSpec *spec = (HcfIvParamsSpec *)params;
28     uint8_t *iv = spec->iv.data;
29     return (const unsigned char *)iv;
30 }
31 
GetCcmTagLen(HcfParamsSpec * params)32 int32_t GetCcmTagLen(HcfParamsSpec *params)
33 {
34     if (params == NULL) {
35         return 0;
36     }
37     HcfCcmParamsSpec *spec = (HcfCcmParamsSpec *)params;
38     size_t tagLen = spec->tag.len;
39     return (int)tagLen;
40 }
41 
GetCcmTag(HcfParamsSpec * params)42 void *GetCcmTag(HcfParamsSpec *params)
43 {
44     if (params == NULL) {
45         return NULL;
46     }
47     HcfCcmParamsSpec *spec = (HcfCcmParamsSpec *)params;
48     uint8_t *tag = spec->tag.data;
49     return (void *)tag;
50 }
51 
FreeCipherData(CipherData ** data)52 void FreeCipherData(CipherData **data)
53 {
54     if (data == NULL || *data == NULL) {
55         return;
56     }
57     if ((*data)->ctx != NULL) {
58         EVP_CIPHER_CTX_free((*data)->ctx);
59         (*data)->ctx = NULL;
60     }
61     if ((*data)->aad != NULL) {
62         HcfFree((*data)->aad);
63         (*data)->aad = NULL;
64     }
65     if ((*data)->iv != NULL) {
66         HcfFree((*data)->iv);
67         (*data)->iv = NULL;
68     }
69     if ((*data)->tag != NULL) {
70         HcfFree((*data)->tag);
71         (*data)->tag = NULL;
72     }
73     HcfFree(*data);
74     *data = NULL;
75 }
76 
FreeRedundantOutput(HcfBlob * blob)77 void FreeRedundantOutput(HcfBlob *blob)
78 {
79     if (blob == NULL) {
80         return;
81     }
82     // when decrypt result is empty plaintext, out blob data maybe not null (malloc by hcf before decryption)
83     if ((blob->len == 0) && (blob->data != NULL)) {
84         HcfFree(blob->data);
85         blob->data = NULL;
86     }
87 }