117fd14ceSopenharmony_ci/*
217fd14ceSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
317fd14ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
417fd14ceSopenharmony_ci * you may not use this file except in compliance with the License.
517fd14ceSopenharmony_ci * You may obtain a copy of the License at
617fd14ceSopenharmony_ci *
717fd14ceSopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
817fd14ceSopenharmony_ci *
917fd14ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1017fd14ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1117fd14ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1217fd14ceSopenharmony_ci * See the License for the specific language governing permissions and
1317fd14ceSopenharmony_ci * limitations under the License.
1417fd14ceSopenharmony_ci */
1517fd14ceSopenharmony_ci
1617fd14ceSopenharmony_ci#include "protocol_common.h"
1717fd14ceSopenharmony_ci#include "device_auth_defines.h"
1817fd14ceSopenharmony_ci#include "hc_log.h"
1917fd14ceSopenharmony_ci#include "hc_types.h"
2017fd14ceSopenharmony_ci#include "string_util.h"
2117fd14ceSopenharmony_ci
2217fd14ceSopenharmony_civoid FreeAndCleanKey(Uint8Buff *key)
2317fd14ceSopenharmony_ci{
2417fd14ceSopenharmony_ci    if (key == NULL || key->val == NULL) {
2517fd14ceSopenharmony_ci        return;
2617fd14ceSopenharmony_ci    }
2717fd14ceSopenharmony_ci    (void)memset_s(key->val, key->length, 0, key->length);
2817fd14ceSopenharmony_ci    HcFree(key->val);
2917fd14ceSopenharmony_ci    key->val = NULL;
3017fd14ceSopenharmony_ci    key->length = 0;
3117fd14ceSopenharmony_ci}
3217fd14ceSopenharmony_ci
3317fd14ceSopenharmony_ciint32_t InitSingleParam(Uint8Buff *param, uint32_t len)
3417fd14ceSopenharmony_ci{
3517fd14ceSopenharmony_ci    if (param == NULL) {
3617fd14ceSopenharmony_ci        LOGE("Param is null.");
3717fd14ceSopenharmony_ci        return HC_ERR_NULL_PTR;
3817fd14ceSopenharmony_ci    }
3917fd14ceSopenharmony_ci    if (param->val != NULL) {
4017fd14ceSopenharmony_ci        (void)memset_s(param->val, param->length, 0, param->length);
4117fd14ceSopenharmony_ci        HcFree(param->val);
4217fd14ceSopenharmony_ci    }
4317fd14ceSopenharmony_ci    param->length = len;
4417fd14ceSopenharmony_ci    param->val = (uint8_t *)HcMalloc(param->length, 0);
4517fd14ceSopenharmony_ci    if (param->val == NULL) {
4617fd14ceSopenharmony_ci        LOGE("Malloc for param failed.");
4717fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
4817fd14ceSopenharmony_ci    }
4917fd14ceSopenharmony_ci    return HC_SUCCESS;
5017fd14ceSopenharmony_ci}