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 "protocol_common.h" 17 #include "device_auth_defines.h" 18 #include "hc_log.h" 19 #include "hc_types.h" 20 #include "string_util.h" 21 FreeAndCleanKey(Uint8Buff * key)22 void FreeAndCleanKey(Uint8Buff *key) 23 { 24 if (key == NULL || key->val == NULL) { 25 return; 26 } 27 (void)memset_s(key->val, key->length, 0, key->length); 28 HcFree(key->val); 29 key->val = NULL; 30 key->length = 0; 31 } 32 InitSingleParam(Uint8Buff * param,uint32_t len)33 int32_t InitSingleParam(Uint8Buff *param, uint32_t len) 34 { 35 if (param == NULL) { 36 LOGE("Param is null."); 37 return HC_ERR_NULL_PTR; 38 } 39 if (param->val != NULL) { 40 (void)memset_s(param->val, param->length, 0, param->length); 41 HcFree(param->val); 42 } 43 param->length = len; 44 param->val = (uint8_t *)HcMalloc(param->length, 0); 45 if (param->val == NULL) { 46 LOGE("Malloc for param failed."); 47 return HC_ERR_ALLOC_MEMORY; 48 } 49 return HC_SUCCESS; 50 }