提交 #1
This commit is contained in:
269
base/security/crypto_framework/common/src/asy_key_params.c
Normal file
269
base/security/crypto_framework/common/src/asy_key_params.c
Normal file
@@ -0,0 +1,269 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "asy_key_params.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <securec.h>
|
||||
|
||||
#include "big_integer.h"
|
||||
#include "detailed_dsa_key_params.h"
|
||||
#include "detailed_ecc_key_params.h"
|
||||
#include "detailed_rsa_key_params.h"
|
||||
#include "memory.h"
|
||||
#include "log.h"
|
||||
|
||||
#define ALG_NAME_DSA "DSA"
|
||||
#define ALG_NAME_ECC "ECC"
|
||||
#define ALG_NAME_RSA "RSA"
|
||||
|
||||
void FreeDsaCommParamsSpec(HcfDsaCommParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL) {
|
||||
return;
|
||||
}
|
||||
HcfFree(spec->base.algName);
|
||||
spec->base.algName = NULL;
|
||||
HcfFree(spec->p.data);
|
||||
spec->p.data = NULL;
|
||||
HcfFree(spec->q.data);
|
||||
spec->q.data = NULL;
|
||||
HcfFree(spec->g.data);
|
||||
spec->g.data = NULL;
|
||||
}
|
||||
|
||||
static void DestroyDsaCommParamsSpec(HcfDsaCommParamsSpec *spec)
|
||||
{
|
||||
FreeDsaCommParamsSpec(spec);
|
||||
HcfFree(spec);
|
||||
}
|
||||
|
||||
void DestroyDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL) {
|
||||
return;
|
||||
}
|
||||
FreeDsaCommParamsSpec(&(spec->base));
|
||||
HcfFree(spec->pk.data);
|
||||
spec->pk.data = NULL;
|
||||
HcfFree(spec);
|
||||
}
|
||||
|
||||
void DestroyDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL) {
|
||||
return;
|
||||
}
|
||||
FreeDsaCommParamsSpec(&(spec->base));
|
||||
HcfFree(spec->pk.data);
|
||||
spec->pk.data = NULL;
|
||||
(void)memset_s(spec->sk.data, spec->sk.len, 0, spec->sk.len);
|
||||
HcfFree(spec->sk.data);
|
||||
spec->sk.data = NULL;
|
||||
HcfFree(spec);
|
||||
}
|
||||
|
||||
static void FreeEcFieldMem(HcfECField **field)
|
||||
{
|
||||
HcfFree((*field)->fieldType);
|
||||
(*field)->fieldType = NULL;
|
||||
HcfFree(((HcfECFieldFp *)(*field))->p.data);
|
||||
((HcfECFieldFp *)(*field))->p.data = NULL;
|
||||
HcfFree(*field);
|
||||
*field = NULL;
|
||||
}
|
||||
|
||||
static void FreeEcPointMem(HcfPoint *point)
|
||||
{
|
||||
HcfFree(point->x.data);
|
||||
point->x.data = NULL;
|
||||
HcfFree(point->y.data);
|
||||
point->y.data = NULL;
|
||||
}
|
||||
|
||||
void FreeEccCommParamsSpec(HcfEccCommParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL) {
|
||||
return;
|
||||
}
|
||||
HcfFree(spec->base.algName);
|
||||
spec->base.algName = NULL;
|
||||
HcfFree(spec->a.data);
|
||||
spec->a.data = NULL;
|
||||
HcfFree(spec->b.data);
|
||||
spec->b.data = NULL;
|
||||
HcfFree(spec->n.data);
|
||||
spec->n.data = NULL;
|
||||
FreeEcFieldMem(&(spec->field));
|
||||
spec->field = NULL;
|
||||
FreeEcPointMem(&(spec->g));
|
||||
}
|
||||
|
||||
static void DestroyEccCommParamsSpec(HcfEccCommParamsSpec *spec)
|
||||
{
|
||||
FreeEccCommParamsSpec(spec);
|
||||
HcfFree(spec);
|
||||
}
|
||||
|
||||
void DestroyEccPubKeySpec(HcfEccPubKeyParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL) {
|
||||
return;
|
||||
}
|
||||
FreeEccCommParamsSpec(&(spec->base));
|
||||
FreeEcPointMem(&(spec->pk));
|
||||
HcfFree(spec);
|
||||
}
|
||||
|
||||
void DestroyEccPriKeySpec(HcfEccPriKeyParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL) {
|
||||
return;
|
||||
}
|
||||
FreeEccCommParamsSpec(&(spec->base));
|
||||
(void)memset_s(spec->sk.data, spec->sk.len, 0, spec->sk.len);
|
||||
HcfFree(spec->sk.data);
|
||||
spec->sk.data = NULL;
|
||||
HcfFree(spec);
|
||||
}
|
||||
|
||||
void DestroyEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL) {
|
||||
return;
|
||||
}
|
||||
FreeEccCommParamsSpec(&(spec->base));
|
||||
FreeEcPointMem(&(spec->pk));
|
||||
(void)memset_s(spec->sk.data, spec->sk.len, 0, spec->sk.len);
|
||||
HcfFree(spec->sk.data);
|
||||
spec->sk.data = NULL;
|
||||
HcfFree(spec);
|
||||
}
|
||||
|
||||
void FreeRsaCommParamsSpec(HcfRsaCommParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL) {
|
||||
return;
|
||||
}
|
||||
HcfFree(spec->base.algName);
|
||||
spec->base.algName = NULL;
|
||||
HcfFree(spec->n.data);
|
||||
spec->n.data = NULL;
|
||||
}
|
||||
|
||||
static void DestroyRsaCommParamsSpec(HcfRsaCommParamsSpec *spec)
|
||||
{
|
||||
FreeRsaCommParamsSpec(spec);
|
||||
HcfFree(spec);
|
||||
}
|
||||
|
||||
void DestroyRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL) {
|
||||
return;
|
||||
}
|
||||
FreeRsaCommParamsSpec(&(spec->base));
|
||||
HcfFree(spec->pk.data);
|
||||
spec->pk.data = NULL;
|
||||
HcfFree(spec);
|
||||
}
|
||||
|
||||
void DestroyRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL) {
|
||||
return;
|
||||
}
|
||||
FreeRsaCommParamsSpec(&(spec->base));
|
||||
HcfFree(spec->pk.data);
|
||||
spec->pk.data = NULL;
|
||||
(void)memset_s(spec->sk.data, spec->sk.len, 0, spec->sk.len);
|
||||
HcfFree(spec->sk.data);
|
||||
spec->sk.data = NULL;
|
||||
HcfFree(spec);
|
||||
}
|
||||
|
||||
static void DestroyDsaParamsSpec(HcfAsyKeyParamsSpec *spec)
|
||||
{
|
||||
switch (spec->specType) {
|
||||
case HCF_COMMON_PARAMS_SPEC:
|
||||
DestroyDsaCommParamsSpec((HcfDsaCommParamsSpec *)spec);
|
||||
break;
|
||||
case HCF_PUBLIC_KEY_SPEC:
|
||||
DestroyDsaPubKeySpec((HcfDsaPubKeyParamsSpec *)spec);
|
||||
break;
|
||||
case HCF_KEY_PAIR_SPEC:
|
||||
DestroyDsaKeyPairSpec((HcfDsaKeyPairParamsSpec *)spec);
|
||||
break;
|
||||
default:
|
||||
LOGE("No matching DSA key params spec type.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void DestroyEccParamsSpec(HcfAsyKeyParamsSpec *spec)
|
||||
{
|
||||
switch (spec->specType) {
|
||||
case HCF_COMMON_PARAMS_SPEC:
|
||||
DestroyEccCommParamsSpec((HcfEccCommParamsSpec *)spec);
|
||||
break;
|
||||
case HCF_PRIVATE_KEY_SPEC:
|
||||
DestroyEccPriKeySpec((HcfEccPriKeyParamsSpec *)spec);
|
||||
break;
|
||||
case HCF_PUBLIC_KEY_SPEC:
|
||||
DestroyEccPubKeySpec((HcfEccPubKeyParamsSpec *)spec);
|
||||
break;
|
||||
case HCF_KEY_PAIR_SPEC:
|
||||
DestroyEccKeyPairSpec((HcfEccKeyPairParamsSpec *)spec);
|
||||
break;
|
||||
default:
|
||||
LOGE("No matching ECC key params spec type.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void DestroyRsaParamsSpec(HcfAsyKeyParamsSpec *spec)
|
||||
{
|
||||
switch (spec->specType) {
|
||||
case HCF_COMMON_PARAMS_SPEC:
|
||||
DestroyRsaCommParamsSpec((HcfRsaCommParamsSpec *)spec);
|
||||
break;
|
||||
case HCF_PUBLIC_KEY_SPEC:
|
||||
DestroyRsaPubKeySpec((HcfRsaPubKeyParamsSpec *)spec);
|
||||
break;
|
||||
case HCF_KEY_PAIR_SPEC:
|
||||
DestroyRsaKeyPairSpec((HcfRsaKeyPairParamsSpec *)spec);
|
||||
break;
|
||||
default:
|
||||
LOGE("No matching RSA key params spec type.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void FreeAsyKeySpec(HcfAsyKeyParamsSpec *spec)
|
||||
{
|
||||
if (spec == NULL || spec->algName == NULL) {
|
||||
return;
|
||||
}
|
||||
if (strcmp(spec->algName, ALG_NAME_DSA) == 0) {
|
||||
return DestroyDsaParamsSpec(spec);
|
||||
} else if (strcmp(spec->algName, ALG_NAME_ECC) == 0) {
|
||||
return DestroyEccParamsSpec(spec);
|
||||
} else if (strcmp(spec->algName, ALG_NAME_RSA) == 0) {
|
||||
return DestroyRsaParamsSpec(spec);
|
||||
} else {
|
||||
LOGE("No matching key params spec alg name.");
|
||||
}
|
||||
}
|
||||
42
base/security/crypto_framework/common/src/blob.c
Normal file
42
base/security/crypto_framework/common/src/blob.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) 2022-2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "blob.h"
|
||||
|
||||
#include <securec.h>
|
||||
#include "memory.h"
|
||||
#include "log.h"
|
||||
|
||||
void HcfBlobDataFree(HcfBlob *blob)
|
||||
{
|
||||
if ((blob == NULL) || (blob->data == NULL)) {
|
||||
return;
|
||||
}
|
||||
HcfFree(blob->data);
|
||||
blob->data = NULL;
|
||||
blob->len = 0;
|
||||
}
|
||||
|
||||
void HcfBlobDataClearAndFree(HcfBlob *blob)
|
||||
{
|
||||
if ((blob == NULL) || (blob->data == NULL)) {
|
||||
LOGD("The input blob is null, no need to free.");
|
||||
return;
|
||||
}
|
||||
(void)memset_s(blob->data, blob->len, 0, blob->len);
|
||||
HcfFree(blob->data);
|
||||
blob->data = NULL;
|
||||
blob->len = 0;
|
||||
}
|
||||
183
base/security/crypto_framework/common/src/hcf_parcel.c
Normal file
183
base/security/crypto_framework/common/src/hcf_parcel.c
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "hcf_parcel.h"
|
||||
#include "securec.h"
|
||||
#include "memory.h"
|
||||
|
||||
const int PARCEL_DEFAULT_INCREASE_STEP = 16;
|
||||
const uint32_t PARCEL_UINT_MAX = 0xffffffffU;
|
||||
const int HALF_LEN = 2;
|
||||
|
||||
HcParcel CreateParcel(uint32_t size, uint32_t allocUnit)
|
||||
{
|
||||
HcParcel parcel;
|
||||
(void)memset_s(&parcel, sizeof(parcel), 0, sizeof(parcel));
|
||||
parcel.allocUnit = allocUnit;
|
||||
if (parcel.allocUnit == 0) {
|
||||
parcel.allocUnit = PARCEL_DEFAULT_INCREASE_STEP;
|
||||
}
|
||||
if (size > 0) {
|
||||
parcel.data = (char *)HcfMalloc(size, 0);
|
||||
if (parcel.data != NULL) {
|
||||
parcel.length = size;
|
||||
}
|
||||
}
|
||||
return parcel;
|
||||
}
|
||||
|
||||
void DeleteParcel(HcParcel *parcel)
|
||||
{
|
||||
if (parcel == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (parcel->data != NULL) {
|
||||
HcfFree(parcel->data);
|
||||
parcel->data = 0;
|
||||
}
|
||||
parcel->length = 0;
|
||||
parcel->beginPos = 0;
|
||||
parcel->endPos = 0;
|
||||
}
|
||||
|
||||
uint32_t GetParcelDataSize(const HcParcel *parcel)
|
||||
{
|
||||
if (parcel == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if (parcel->endPos >= parcel->beginPos) {
|
||||
return parcel->endPos - parcel->beginPos;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *GetParcelData(const HcParcel *parcel)
|
||||
{
|
||||
if (parcel == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return parcel->data + parcel->beginPos;
|
||||
}
|
||||
|
||||
static bool ParcelRealloc(HcParcel *parcel, uint32_t size)
|
||||
{
|
||||
if (parcel->length >= size) {
|
||||
return false;
|
||||
}
|
||||
char *newData = (char *)HcfMalloc(size, 0);
|
||||
if (newData == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (memcpy_s(newData, size, parcel->data, parcel->length) != EOK) {
|
||||
HcfFree(newData);
|
||||
return false;
|
||||
}
|
||||
HcfFree(parcel->data);
|
||||
parcel->data = newData;
|
||||
parcel->length = size;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ParcelIncrease(HcParcel *parcel, uint32_t size)
|
||||
{
|
||||
if (parcel == NULL || size == 0) {
|
||||
return false;
|
||||
}
|
||||
if (parcel->data == NULL) {
|
||||
if (parcel->length != 0) {
|
||||
return false;
|
||||
}
|
||||
*parcel = CreateParcel(size, parcel->allocUnit);
|
||||
if (parcel->data == NULL) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return ParcelRealloc(parcel, size);
|
||||
}
|
||||
}
|
||||
|
||||
static void ParcelRecycle(HcParcel *parcel)
|
||||
{
|
||||
if (parcel == NULL) {
|
||||
return;
|
||||
}
|
||||
if (parcel->data == NULL || parcel->beginPos < parcel->allocUnit) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t contentSize = parcel->endPos - parcel->beginPos;
|
||||
if (contentSize > 0) {
|
||||
if (memmove_s(parcel->data, parcel->endPos - parcel->beginPos,
|
||||
parcel->data + parcel->beginPos, parcel->endPos - parcel->beginPos) != EOK) {
|
||||
}
|
||||
}
|
||||
parcel->beginPos = 0;
|
||||
parcel->endPos = contentSize;
|
||||
}
|
||||
|
||||
static uint32_t GetParcelIncreaseSize(HcParcel *parcel, uint32_t newSize)
|
||||
{
|
||||
if (parcel == NULL || parcel->allocUnit == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (newSize % parcel->allocUnit) {
|
||||
return (newSize / parcel->allocUnit + 1) * parcel->allocUnit;
|
||||
} else {
|
||||
return (newSize / parcel->allocUnit) * parcel->allocUnit;
|
||||
}
|
||||
}
|
||||
|
||||
bool ParcelWrite(HcParcel *parcel, const void *src, uint32_t dataSize)
|
||||
{
|
||||
errno_t rc;
|
||||
if (parcel == NULL || src == NULL || dataSize == 0) {
|
||||
return false;
|
||||
}
|
||||
if (parcel->endPos > PARCEL_UINT_MAX - dataSize) {
|
||||
return false;
|
||||
}
|
||||
if (parcel->endPos + dataSize > parcel->length) {
|
||||
ParcelRecycle(parcel);
|
||||
if (parcel->endPos + dataSize > parcel->length) {
|
||||
uint32_t newSize = GetParcelIncreaseSize(parcel, parcel->endPos + dataSize);
|
||||
if (!ParcelIncrease(parcel, newSize)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
rc = memmove_s(parcel->data + parcel->endPos, dataSize, src, dataSize);
|
||||
if (rc != EOK) {
|
||||
return false;
|
||||
}
|
||||
parcel->endPos += dataSize;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ParcelWriteInt8(HcParcel *parcel, char src)
|
||||
{
|
||||
return ParcelWrite(parcel, &src, sizeof(src));
|
||||
}
|
||||
|
||||
bool ParcelPopBack(HcParcel *parcel, uint32_t size)
|
||||
{
|
||||
if (parcel != NULL && size > 0 && GetParcelDataSize(parcel) >= size) {
|
||||
parcel->endPos -= size;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
233
base/security/crypto_framework/common/src/hcf_string.c
Normal file
233
base/security/crypto_framework/common/src/hcf_string.c
Normal file
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "hcf_string.h"
|
||||
|
||||
const uint32_t STRING_ALLOC_SIZE = 10;
|
||||
const uint32_t STRING_END_CHAR_LENGTH = 1;
|
||||
const char STRING_END_CHAR = '\0';
|
||||
#define MAX_INT 0x7FFFFFFF
|
||||
#define MAX_UINT 0xFFFFFFFF
|
||||
|
||||
/*
|
||||
* Append string pointer
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: string pointer.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringAppendPointer(HcString *self, const char *str)
|
||||
{
|
||||
if (self != NULL && str != NULL) {
|
||||
// remove '\0'
|
||||
ParcelPopBack(&self->parcel, STRING_END_CHAR_LENGTH);
|
||||
// append string (include '\0')
|
||||
return ParcelWrite(&self->parcel, (void *)str, strlen(str) + 1);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Assign a value to the HcString
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: assign value of string pointer.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringSetPointer(HcString *self, const char *str)
|
||||
{
|
||||
if (self != NULL) {
|
||||
DeleteParcel(&self->parcel);
|
||||
return StringAppendPointer(self, str);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Assign a value to the HcString with fixed length
|
||||
* Notice: It will add '\0' automatically.
|
||||
* @param self: self pointer.
|
||||
* @param str: assign value of string pointer.
|
||||
* @param len: the length of string.
|
||||
* @return true (ok), false (error)
|
||||
*/
|
||||
bool StringSetPointerWithLength(HcString* self, const char *str, uint32_t len)
|
||||
{
|
||||
if (self == NULL || str == NULL) {
|
||||
return false;
|
||||
}
|
||||
uint32_t strLen = strlen(str);
|
||||
if (strLen < len) {
|
||||
return false;
|
||||
}
|
||||
DeleteParcel(&self->parcel);
|
||||
if (len > 0) {
|
||||
if (false == ParcelWrite(&self->parcel, str, len)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return ParcelWriteInt8(&self->parcel, (uint32_t)STRING_END_CHAR);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the string pointer data
|
||||
* @param self: self pointer.
|
||||
* @return the pointer data of the string
|
||||
*/
|
||||
const char *StringGet(const HcString *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return GetParcelData(&self->parcel);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the length of the string
|
||||
* @param self: self pointer.
|
||||
* @return the length of the string
|
||||
*/
|
||||
uint32_t StringLength(const HcString *self)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return 0;
|
||||
} else {
|
||||
uint32_t length = GetParcelDataSize(&self->parcel);
|
||||
if (length > 0) {
|
||||
return length - STRING_END_CHAR_LENGTH;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a char from string
|
||||
* @param self: self pointer.
|
||||
* @param c: the char you want find
|
||||
* @param begin: the position find from
|
||||
* @return the position of the char
|
||||
*/
|
||||
int StringFind(const HcString *self, char c, uint32_t begin)
|
||||
{
|
||||
if (self == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// because the return value is int
|
||||
// so the string length cannot bigger than MAX_INT
|
||||
uint32_t strLen = StringLength(self);
|
||||
if (strLen >= MAX_INT) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* curChar = StringGet(self);
|
||||
while (begin < strLen) {
|
||||
if (*(curChar + begin) == c) {
|
||||
return begin;
|
||||
}
|
||||
++begin;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get sub string from a string.
|
||||
* @param self: self pointer.
|
||||
* @param begin: the begin position of the sub string.
|
||||
* @param len: the length of the sub string.
|
||||
* @param dst: the string pointer which saved the sub string content.
|
||||
* @return the operation result.
|
||||
*/
|
||||
bool StringSubString(const HcString *self, uint32_t begin, uint32_t len, HcString* dst)
|
||||
{
|
||||
if (self == NULL || dst == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (MAX_UINT - len <= begin) {
|
||||
return false;
|
||||
}
|
||||
const char* beingPointer = StringGet(self) + begin;
|
||||
return StringSetPointerWithLength(dst, beingPointer, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare the string with another string.
|
||||
* @param self: self pointer.
|
||||
* @param dst: the pointer of another string.
|
||||
* @return the compare result.
|
||||
* -1: self is smaller than dst
|
||||
* 0: self is equal with dst
|
||||
* 1: self is bigger than dst
|
||||
*/
|
||||
int StringCompare(const HcString *self, const char* dst)
|
||||
{
|
||||
if (self == NULL || dst == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* src = StringGet(self);
|
||||
if (src == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
if ((*src) > (*dst)) {
|
||||
return 1;
|
||||
} else if ((*src) < (*dst)) {
|
||||
return -1;
|
||||
} else {
|
||||
if ((*src) == '\0') {
|
||||
return 0;
|
||||
}
|
||||
++src;
|
||||
++dst;
|
||||
}
|
||||
} while (1);
|
||||
// should never be here
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a string.
|
||||
* Notice: You should delete_string when you don't need the string anymore.
|
||||
* @return return the created string.
|
||||
*/
|
||||
HcString CreateString(void)
|
||||
{
|
||||
HcString str;
|
||||
str.parcel = CreateParcel(0, STRING_ALLOC_SIZE);
|
||||
ParcelWriteInt8(&str.parcel, STRING_END_CHAR);
|
||||
return str;
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a string. In fact it will not destroy the string,
|
||||
* but only free the allocate memory of the string and reset the member's value
|
||||
* of the string.
|
||||
* You can continue to use the string if you want.
|
||||
* Notice: You should delete the string when you don't need it any more to avoid memory leak.
|
||||
* @param str: The string you want to delete.
|
||||
*/
|
||||
void DeleteString(HcString *str)
|
||||
{
|
||||
if (str != NULL) {
|
||||
DeleteParcel(&str->parcel);
|
||||
}
|
||||
}
|
||||
59
base/security/crypto_framework/common/src/log.c
Normal file
59
base/security/crypto_framework/common/src/log.c
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include "config.h"
|
||||
#include "securec.h"
|
||||
|
||||
static void HcfOutPrint(const char *buf, HcfLogLevel level)
|
||||
{
|
||||
switch (level) {
|
||||
case HCF_LOG_LEVEL_DEBUG:
|
||||
HCF_LOG_DEBUG(buf);
|
||||
break;
|
||||
case HCF_LOG_LEVEL_INFO:
|
||||
HCF_LOG_INFO(buf);
|
||||
break;
|
||||
case HCF_LOG_LEVEL_WARN:
|
||||
HCF_LOG_WARN(buf);
|
||||
break;
|
||||
case HCF_LOG_LEVEL_ERROR:
|
||||
HCF_LOG_ERROR(buf);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void HcfLogPrint(HcfLogLevel level, const char *funName, const char *fmt, ...)
|
||||
{
|
||||
int32_t ulPos = 0;
|
||||
char outStr[LOG_PRINT_MAX_LEN] = {0};
|
||||
int32_t ret = sprintf_s(outStr, sizeof(outStr), "%s: ", funName);
|
||||
if (ret < 0) {
|
||||
return;
|
||||
}
|
||||
ulPos = strlen(outStr);
|
||||
va_list arg;
|
||||
va_start(arg, fmt);
|
||||
ret = vsprintf_s(&outStr[ulPos], sizeof(outStr) - ulPos, fmt, arg);
|
||||
va_end(arg);
|
||||
if (ret < 0) {
|
||||
return;
|
||||
}
|
||||
HcfOutPrint(outStr, level);
|
||||
}
|
||||
39
base/security/crypto_framework/common/src/memory.c
Normal file
39
base/security/crypto_framework/common/src/memory.c
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "memory.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "securec.h"
|
||||
|
||||
void *HcfMalloc(uint32_t size, char val)
|
||||
{
|
||||
if (size == 0) {
|
||||
LOGE("malloc size is invalid");
|
||||
return NULL;
|
||||
}
|
||||
void *addr = malloc(size);
|
||||
if (addr != NULL) {
|
||||
(void)memset_s(addr, size, val, size);
|
||||
}
|
||||
return addr;
|
||||
}
|
||||
|
||||
void HcfFree(void *addr)
|
||||
{
|
||||
if (addr != NULL) {
|
||||
free(addr);
|
||||
}
|
||||
}
|
||||
25
base/security/crypto_framework/common/src/object_base.c
Normal file
25
base/security/crypto_framework/common/src/object_base.c
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "object_base.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void HcfObjDestroy(void *obj)
|
||||
{
|
||||
if (obj != NULL) {
|
||||
((HcfObjectBase *)obj)->destroy((HcfObjectBase *)obj);
|
||||
}
|
||||
}
|
||||
183
base/security/crypto_framework/common/src/params_parser.c
Normal file
183
base/security/crypto_framework/common/src/params_parser.c
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* Copyright (C) 2022-2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "params_parser.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include "hcf_string.h"
|
||||
#include "log.h"
|
||||
|
||||
static const HcfParaConfig PARAM_CONFIG[] = {
|
||||
{"ECC224", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_224},
|
||||
{"ECC256", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_256},
|
||||
{"ECC384", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_384},
|
||||
{"ECC521", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_521},
|
||||
|
||||
{"AES128", HCF_ALG_KEY_TYPE, HCF_ALG_AES_128},
|
||||
{"AES192", HCF_ALG_KEY_TYPE, HCF_ALG_AES_192},
|
||||
{"AES256", HCF_ALG_KEY_TYPE, HCF_ALG_AES_256},
|
||||
{"SM4_128", HCF_ALG_KEY_TYPE, HCF_ALG_SM4_128},
|
||||
{"3DES192", HCF_ALG_KEY_TYPE, HCF_ALG_3DES_192},
|
||||
|
||||
{"ECB", HCF_ALG_MODE, HCF_ALG_MODE_ECB},
|
||||
{"CBC", HCF_ALG_MODE, HCF_ALG_MODE_CBC},
|
||||
{"CTR", HCF_ALG_MODE, HCF_ALG_MODE_CTR},
|
||||
{"OFB", HCF_ALG_MODE, HCF_ALG_MODE_OFB},
|
||||
{"CFB", HCF_ALG_MODE, HCF_ALG_MODE_CFB},
|
||||
{"CFB1", HCF_ALG_MODE, HCF_ALG_MODE_CFB1},
|
||||
{"CFB8", HCF_ALG_MODE, HCF_ALG_MODE_CFB8},
|
||||
{"CFB64", HCF_ALG_MODE, HCF_ALG_MODE_CFB64},
|
||||
{"CFB128", HCF_ALG_MODE, HCF_ALG_MODE_CFB128},
|
||||
{"CCM", HCF_ALG_MODE, HCF_ALG_MODE_CCM},
|
||||
{"GCM", HCF_ALG_MODE, HCF_ALG_MODE_GCM},
|
||||
|
||||
{"NoPadding", HCF_ALG_PADDING_TYPE, HCF_ALG_NOPADDING},
|
||||
{"PKCS5", HCF_ALG_PADDING_TYPE, HCF_ALG_PADDING_PKCS5},
|
||||
{"PKCS7", HCF_ALG_PADDING_TYPE, HCF_ALG_PADDING_PKCS7},
|
||||
|
||||
{"RSA512", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_512},
|
||||
{"RSA768", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_768},
|
||||
{"RSA1024", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_1024},
|
||||
{"RSA2048", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_2048},
|
||||
{"RSA3072", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_3072},
|
||||
{"RSA4096", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_4096},
|
||||
{"RSA8192", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_8192},
|
||||
|
||||
{"PKCS1", HCF_ALG_PADDING_TYPE, HCF_OPENSSL_RSA_PKCS1_PADDING},
|
||||
{"PKCS1_OAEP", HCF_ALG_PADDING_TYPE, HCF_OPENSSL_RSA_PKCS1_OAEP_PADDING},
|
||||
{"PSS", HCF_ALG_PADDING_TYPE, HCF_OPENSSL_RSA_PSS_PADDING},
|
||||
|
||||
{"NoHash", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_NONE},
|
||||
{"MD5", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_MD5},
|
||||
{"SM3", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SM3},
|
||||
{"SHA1", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SHA1},
|
||||
{"SHA224", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SHA224},
|
||||
{"SHA256", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SHA256},
|
||||
{"SHA384", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SHA384},
|
||||
{"SHA512", HCF_ALG_DIGEST, HCF_OPENSSL_DIGEST_SHA512},
|
||||
|
||||
{"MGF1_MD5", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_MD5},
|
||||
{"MGF1_SHA1", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_SHA1},
|
||||
{"MGF1_SHA224", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_SHA224},
|
||||
{"MGF1_SHA256", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_SHA256},
|
||||
{"MGF1_SHA384", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_SHA384},
|
||||
{"MGF1_SHA512", HCF_ALG_MGF1_DIGEST, HCF_OPENSSL_DIGEST_SHA512},
|
||||
|
||||
{"PRIMES_2", HCF_ALG_PRIMES, HCF_OPENSSL_PRIMES_2},
|
||||
{"PRIMES_3", HCF_ALG_PRIMES, HCF_OPENSSL_PRIMES_3},
|
||||
{"PRIMES_4", HCF_ALG_PRIMES, HCF_OPENSSL_PRIMES_4},
|
||||
{"PRIMES_5", HCF_ALG_PRIMES, HCF_OPENSSL_PRIMES_5},
|
||||
|
||||
{"DSA1024", HCF_ALG_KEY_TYPE, HCF_ALG_DSA_1024},
|
||||
{"DSA2048", HCF_ALG_KEY_TYPE, HCF_ALG_DSA_2048},
|
||||
{"DSA3072", HCF_ALG_KEY_TYPE, HCF_ALG_DSA_3072},
|
||||
|
||||
{"SM2_256", HCF_ALG_KEY_TYPE, HCF_ALG_SM2_256},
|
||||
|
||||
{"RSA", HCF_ALG_TYPE, HCF_ALG_RSA_DEFAULT},
|
||||
{"DSA", HCF_ALG_TYPE, HCF_ALG_DSA_DEFAULT},
|
||||
{"ECC", HCF_ALG_TYPE, HCF_ALG_ECC_DEFAULT},
|
||||
{"SM2", HCF_ALG_TYPE, HCF_ALG_SM2_DEFAULT},
|
||||
{"AES", HCF_ALG_TYPE, HCF_ALG_AES_DEFAULT},
|
||||
{"SM4", HCF_ALG_TYPE, HCF_ALG_SM4_DEFAULT},
|
||||
{"3DES", HCF_ALG_TYPE, HCF_ALG_3DES_DEFAULT},
|
||||
|
||||
{"C1C3C2", HCF_ALG_TEXT_FORMAT, HCF_ALG_TEXT_FORMAT_C1C3C2},
|
||||
{"C1C2C3", HCF_ALG_TEXT_FORMAT, HCF_ALG_TEXT_FORMAT_C1C2C3},
|
||||
};
|
||||
|
||||
static const HcfAlgMap ALG_MAP[] = {
|
||||
{"DSA", HCF_ALG_DSA},
|
||||
{"RSA", HCF_ALG_RSA},
|
||||
{"ECC", HCF_ALG_ECC},
|
||||
{"SM2", HCF_ALG_SM2},
|
||||
};
|
||||
|
||||
static const HcfParaConfig *FindConfig(const HcString* tag)
|
||||
{
|
||||
if (tag == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < sizeof(PARAM_CONFIG) / sizeof(HcfParaConfig); ++i) {
|
||||
if (StringCompare(tag, PARAM_CONFIG[i].tag) == 0) {
|
||||
return &PARAM_CONFIG[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HcfResult ParseAndSetParameter(const char *paramsStr, void *params, SetParameterFunc setFunc)
|
||||
{
|
||||
if (paramsStr == NULL || setFunc == NULL) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcString str = CreateString();
|
||||
HcString subStr = CreateString();
|
||||
if (!StringSetPointer(&str, paramsStr)) {
|
||||
DeleteString(&subStr);
|
||||
DeleteString(&str);
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
HcfResult ret = HCF_INVALID_PARAMS;
|
||||
uint32_t pos = 0;
|
||||
do {
|
||||
int findPos = StringFind(&str, '|', pos);
|
||||
if (findPos >= 0) {
|
||||
if (!StringSubString(&str, pos, findPos - pos, &subStr)) {
|
||||
LOGE("StringSubString failed!");
|
||||
break;
|
||||
}
|
||||
ret = (*setFunc)(FindConfig(&subStr), params);
|
||||
if (ret != HCF_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
pos = findPos + 1;
|
||||
} else {
|
||||
uint32_t strLen = StringLength(&str);
|
||||
if (strLen < pos) {
|
||||
break;
|
||||
}
|
||||
if (!StringSubString(&str, pos, strLen - pos, &subStr)) {
|
||||
LOGE("get last string failed!");
|
||||
break;
|
||||
}
|
||||
ret = (*setFunc)(FindConfig(&subStr), params);
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
DeleteString(&subStr);
|
||||
DeleteString(&str);
|
||||
return ret;
|
||||
}
|
||||
|
||||
HcfResult ParseAlgNameToParams(const char *algNameStr, HcfAsyKeyGenParams *params)
|
||||
{
|
||||
if (algNameStr == NULL || params == NULL) {
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
for (uint32_t i = 0; i < sizeof(ALG_MAP) / sizeof(HcfAlgMap); ++i) {
|
||||
if (strcmp(algNameStr, ALG_MAP[i].algNameStr) == 0) {
|
||||
params->algo = ALG_MAP[i].algValue;
|
||||
params->bits = 0;
|
||||
return HCF_SUCCESS;
|
||||
}
|
||||
}
|
||||
LOGE("Not support algorithm name: %s", algNameStr);
|
||||
return HCF_INVALID_PARAMS;
|
||||
}
|
||||
51
base/security/crypto_framework/common/src/utils.c
Normal file
51
base/security/crypto_framework/common/src/utils.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include <string.h>
|
||||
#include "log.h"
|
||||
|
||||
bool IsStrValid(const char *str, uint32_t maxLen)
|
||||
{
|
||||
if (str == NULL) {
|
||||
LOGE("input string is NULL ptr");
|
||||
return false;
|
||||
}
|
||||
// One byte must be reserved for the terminator.
|
||||
if (strnlen(str, maxLen) >= maxLen) {
|
||||
LOGE("input string is beyond max length");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsBlobValid(const HcfBlob *blob)
|
||||
{
|
||||
return ((blob != NULL) && (blob->data != NULL) && (blob->len > 0));
|
||||
}
|
||||
|
||||
bool IsClassMatch(const HcfObjectBase *obj, const char *className)
|
||||
{
|
||||
if ((obj == NULL) || (obj->getClass() == NULL) || (className == NULL)) {
|
||||
return false;
|
||||
}
|
||||
if (strcmp(obj->getClass(), className) == 0) {
|
||||
return true;
|
||||
} else {
|
||||
LOGE("class is not match. expect class: %s, input class: %s", className, obj->getClass());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user