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