From 9362f64880fd41fc006b9ced3487857fd1eb8087 Mon Sep 17 00:00:00 2001 From: lcc Date: Wed, 26 Jun 2024 11:42:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BE=9BPublic=20CAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lcc --- BUILD.gn | 1 + frameworks/native/BUILD.gn | 54 +++++ frameworks/native/src/asym_key.c | 144 ++++++++++++ frameworks/native/src/crypto_common.c | 23 ++ frameworks/native/src/digest.c | 61 +++++ frameworks/native/src/signature.c | 112 +++++++++ frameworks/native/src/sym_cipher.c | 107 +++++++++ frameworks/native/src/sym_key.c | 81 +++++++ interfaces/kits/native/include/asym_key.h | 216 ++++++++++++++++++ .../kits/native/include/crypto_common.h | 71 ++++++ interfaces/kits/native/include/digest.h | 142 ++++++++++++ interfaces/kits/native/include/signature.h | 185 +++++++++++++++ interfaces/kits/native/include/sym_cipher.h | 181 +++++++++++++++ interfaces/kits/native/include/sym_key.h | 152 ++++++++++++ 14 files changed, 1530 insertions(+) create mode 100644 frameworks/native/BUILD.gn create mode 100644 frameworks/native/src/asym_key.c create mode 100644 frameworks/native/src/crypto_common.c create mode 100644 frameworks/native/src/digest.c create mode 100644 frameworks/native/src/signature.c create mode 100644 frameworks/native/src/sym_cipher.c create mode 100644 frameworks/native/src/sym_key.c create mode 100644 interfaces/kits/native/include/asym_key.h create mode 100644 interfaces/kits/native/include/crypto_common.h create mode 100644 interfaces/kits/native/include/digest.h create mode 100644 interfaces/kits/native/include/signature.h create mode 100644 interfaces/kits/native/include/sym_cipher.h create mode 100644 interfaces/kits/native/include/sym_key.h diff --git a/BUILD.gn b/BUILD.gn index dcbe0a5..3c72acb 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -22,6 +22,7 @@ group("crypto_framework_component") { "frameworks/cj:cj_cryptoframework_ffi", "frameworks/js/napi/crypto:cryptoframework_napi", "plugin:crypto_openssl_plugin_lib", + "frameworks/native:ohcrypto", ] } } diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn new file mode 100644 index 0000000..d1afb6b --- /dev/null +++ b/frameworks/native/BUILD.gn @@ -0,0 +1,54 @@ +# Copyright (C) 2024 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. + +import("//base/security/crypto_framework/common/common.gni") +import("//base/security/crypto_framework/frameworks/frameworks.gni") +import("//build/ohos.gni") + +ohos_shared_library("ohcrypto") { + + if (os_level == "standard") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + } + + cflags = [ + "-DHILOG_ENABLE", + "-fPIC", + "-Wall", + ] + + include_dirs = [ + "//base/security/crypto_framework/interfaces/kits/native/include" + ] + + sources = [ + "//base/security/crypto_framework/frameworks/native/src/crypto_common.c", + "//base/security/crypto_framework/frameworks/native/src/digest.c", + ] + + deps = [ + "//base/security/crypto_framework/frameworks:crypto_framework_lib", + ] + + external_deps = [ + "hilog:libhilog", + ] + + subsystem_name = "security" + innerapi_tags = [ "ndk" ] + part_name = "crypto_framework" +} diff --git a/frameworks/native/src/asym_key.c b/frameworks/native/src/asym_key.c new file mode 100644 index 0000000..ed7b45d --- /dev/null +++ b/frameworks/native/src/asym_key.c @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2024 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 "asym_key.h" +#include "key_pair.h" +#include "pub_key.h" +#include "result.h" +#include "asy_key_generator.h" + + +typedef struct OH_CryptoAsymKeyGenerator { + HcfObjectBase base; + + HcfResult (*generateKeyPair)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, + HcfKeyPair **returnKeyPair); + + HcfResult (*convertKey)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob, + HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair); + + HcfResult (*convertPemKey)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, const char *pubKeyStr, + const char *priKeyStr, HcfKeyPair **returnKeyPair); + + const char *(*getAlgoName)(HcfAsyKeyGenerator *self); +}; + +typedef struct OH_CryptoKeyPair { + HcfObjectBase base; + + HcfPriKey *priKey; + + HcfPubKey *pubKey; +}; + +typedef struct OH_CryptoPubKey { + HcfKey base; + + HcfResult (*getAsyKeySpecBigInteger)(const HcfPubKey *self, const AsyKeySpecItem item, + HcfBigInteger *returnBigInteger); + + HcfResult (*getAsyKeySpecString)(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString); + + HcfResult (*getAsyKeySpecInt)(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt); + + HcfResult (*getEncodedDer)(const HcfPubKey *self, const char *format, HcfBlob *returnBlob); +}; + +Crypto_Result OH_CryptoAsymKeyGenerator_Create(const char *algoName, OH_CryptoAsymKeyGenerator **ctx) +{ + return (Crypto_Result)HcfAsyKeyGeneratorCreate(algoName, (HcfAsyKeyGenerator **)ctx); +} + +Crypto_Result OH_CryptoAsymKeyGenerator_Generatey(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoKeyPair **keyPair) +{ + return (Crypto_Result)ctx->generateKeyPair((HcfAsyKeyGenerator *)ctx, NULL, (HcfKeyPair **)keyPair); +} + +Crypto_Result OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *ctx, Crypto_Encoding_type type, + Crypto_DataBlob *pubKeyData, Crypto_DataBlob *priKeyData, OH_CryptoKeyPair **key) +{ + return (Crypto_Result)ctx->convertKey((HcfAsyKeyGenerator *)ctx, NULL, (HcfBlob *)pubKeyData, (HcfBlob *)priKeyData, (HcfKeyPair **)keyPair); +} + +const char *OH_CryptoAsymKeyGenerator_GetAlgoName(OH_CryptoAsymKeyGenerator *ctx) +{ + return ctx->getAlgoName((HcfAsyKeyGenerator *)ctx); +} + +void OH_CryptoAsymKeyGenerator_Destroy(OH_CryptoAsymKeyGenerator *ctx) +{ + return ctx->base.destroy((HcfAsyKeyGenerator *)ctx); +} + +void OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *key) +{ + return key->base.destroy((HcfKeyPair *)key); +} + +const char *OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair *key) +{ + return key->pubKey((HcfKeyPair *)key); +} + +Crypto_Result OH_CryptoPubKey_Encode(OH_CryptoPubKey *key, Crypto_Encoding_type type, const char *encodingStandard, Crypto_DataBlob *out) +{ + Crypto_Result ret = CRYPTO_SUCCESS; + switch (type) { + case CRYPTO_PEM: + char pemStr[] = {}; + ret = (Crypto_Result)key->base.getEncodedPem((HcfKey *)key, encodingStandard, &pemStr); + out->data = (uint8 *)pemStr; + out->len = sizeof(pemStr); + break; + case CRYPTO_DER: + if (encodingStandard == "X509") { + ret = (Crypto_Result)key->getEncodedDer((HcfKey *)key, encodingStandard, (HcfBlob *)out); + } else if (encodingStandard == "PKCS1") { + ret = (Crypto_Result)key->base.getEncoded((HcfKey *)key, (HcfBlob *)out); + } + break; + default: + break; + } + + return ret; +} + +Crypto_Result OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value) +{ + Crypto_Result ret = CRYPTO_SUCCESS; + switch (item) + { + case CRYPTO_ECC_H_INT: + case CRYPTO_ECC_FIELD_SIZE_INT: + ret = (Crypto_Result)key->getAsyKeySpecInt((HcfPubKey *)key, item, (HcfBigInteger *)value); + break; + + case CRYPTO_ECC_FIELD_TYPE_STR: + case CRYPTO_ECC_CURVE_NAME_STR: + char *returnStr = NULL; + ret = (Crypto_Result)key->getAsyKeySpecString((HcfPubKey *)key, item, &returnStr); + value->data = (uint8 *)returnStr; + value->len = sizeof(returnStr); + break; + case CRYPTODH_L_NUM: + ret = (Crypto_Result)key->getAsyKeySpecBigInteger((HcfPubKey *)key, item, (HcfBigInteger *)value); + break; + default: + ret = (Crypto_Result)key->getAsyKeySpecBigInteger((HcfPubKey *)key, item, (HcfBigInteger *)value); + break; + } + return ret; +} \ No newline at end of file diff --git a/frameworks/native/src/crypto_common.c b/frameworks/native/src/crypto_common.c new file mode 100644 index 0000000..026d279 --- /dev/null +++ b/frameworks/native/src/crypto_common.c @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2024 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 "crypto_common.h" +#include "blob.h" + + +void OH_Crypto_DataBlobFree(Crypto_DataBlob *dataBlob) +{ + return HcfBlobDataClearAndFree((HcfBlob *)dataBlob); +} \ No newline at end of file diff --git a/frameworks/native/src/digest.c b/frameworks/native/src/digest.c new file mode 100644 index 0000000..14314f4 --- /dev/null +++ b/frameworks/native/src/digest.c @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2024 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 "digest.h" +#include "result.h" +#include "md.h" +#include "crypto_common.h" + +struct OH_CryptoDigest { + HcfObjectBase base; + + HcfResult (*update)(HcfMd *self, HcfBlob *input); + + HcfResult (*doFinal)(HcfMd *self, HcfBlob *output); + + uint32_t (*getMdLength)(HcfMd *self); + + const char *(*getAlgoName)(HcfMd *self); +}; + +Crypto_Result OH_CryptoDigest_Create(const char *algoName, OH_CryptoDigest **md) +{ + return (Crypto_Result)HcfMdCreate(algoName, (HcfMd **)md); +} + +Crypto_Result OH_CryptoDigest_Update(OH_CryptoDigest *ctx, Crypto_DataBlob *in) +{ + return (Crypto_Result)ctx->update((HcfMd *)ctx, (HcfBlob *)in); +} + +Crypto_Result OH_CryptoDigest_Final(OH_CryptoDigest *ctx, Crypto_DataBlob *out) +{ + return (Crypto_Result)ctx->doFinal((HcfMd *)ctx, (HcfBlob *)out); +} + +uint32_t OH_CryptoDigest_GetLength(OH_CryptoDigest *ctx) +{ + return ctx->getMdLength((HcfMd *)ctx); +} + +const char *OH_CryptoDigest_GetAlgoName(OH_CryptoDigest *ctx) +{ + return ctx->getAlgoName((HcfMd *)ctx); +} + +void OH_DigestCrypto_Destroy(OH_CryptoDigest *ctx) +{ + return ctx->base.destroy((HcfObjectBase *)ctx); +} \ No newline at end of file diff --git a/frameworks/native/src/signature.c b/frameworks/native/src/signature.c new file mode 100644 index 0000000..0077d8b --- /dev/null +++ b/frameworks/native/src/signature.c @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2024 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 "signature.h" +#include "crypto_common.h" +#include "result.h" + +struct OH_CryptoVerify { + HcfObjectBase base; + + HcfResult (*init)(HcfVerify *self, HcfParamsSpec *params, HcfPubKey *publicKey); + + HcfResult (*update)(HcfVerify *self, HcfBlob *data); + + bool (*verify)(HcfVerify *self, HcfBlob *data, HcfBlob *signatureData); + + HcfResult (*recover)(HcfVerify *self, HcfBlob *signatureData, HcfBlob *rawSignatureData); + + const char *(*getAlgoName)(HcfVerify *self); + + HcfResult (*setVerifySpecInt)(HcfVerify *self, SignSpecItem item, int32_t saltLen); + + HcfResult (*getVerifySpecString)(HcfVerify *self, SignSpecItem item, char **returnString); + + HcfResult (*getVerifySpecInt)(HcfVerify *self, SignSpecItem item, int32_t *returnInt); + + HcfResult (*setVerifySpecUint8Array)(HcfVerify *self, SignSpecItem item, HcfBlob blob); +}; + +Crypto_Result OH_CryptoVerify_Create(const char *algoName, OH_CryptoVerify **verify) +{ + return (Crypto_Result)HcfVerifyCreate(algoName, (HcfVerify **)verify); +} + +Crypto_Result OH_CryptoVerify_Init(OH_CryptoVerify *verify, OH_CryptoPubKey *pubKey) +{ + return (Crypto_Result)verify->init((HcfVerify *)verify, NULL, (HcfPubKey *)pubKey); +} + +Crypto_Result OH_CryptoVerify_Update(OH_CryptoVerify *verify, Crypto_DataBlob *in) +{ + return (Crypto_Result)verify->update((HcfVerify *)verify, (HcfBlob *)in); +} + +bool OH_CryptoVerify_Final(OH_CryptoVerify *verify, Crypto_DataBlob *in, Crypto_DataBlob *signData) +{ + return (Crypto_Result)verify->verify((HcfVerify *)verify, (HcfBlob *)in, (HcfBlob *)signData); +} + +Crypto_Result OH_CryptoVerify_Recover(OH_CryptoVerify *verify, Crypto_DataBlob *signData, Crypto_DataBlob *rawSignData) +{ + return (Crypto_Result)verify->recover((HcfVerify *)verify, (HcfBlob *)signData, (HcfBlob *)rawSignData); +} + +const char *OH_CryptoVerify_GetAlgoName(OH_CryptoVerify *verify) +{ + return verify->getAlgoName((HcfVerify *)verify); +} + +Crypto_Result OH_CryptoVerify_SetParam(OH_CryptoVerify *verify, CryptoSignature_ParamType type, Crypto_DataBlob *value) +{ + return (Crypto_Result)verify->setVerifySpecInt((HcfVerify *)verify, (SignSpecItem)type, (HcfBlob *)value); +} + + +Crypto_Result OH_CryptoVerify_GetParam(OH_CryptoVerify *verify, CryptoSignature_ParamType type, Crypto_DataBlob *value) +{ + ret = verify->getSignSpecInt((HcfVerify *)verify, (SignSpecItem)type, (HcfBlob *)value) + Crypto_Result ret = CRYPTO_INVALID_PARAMS; + switch (type) { + case CRYPTO_PSS_SALT_LEN_INT: + case CRYPTO_PSS_TRAILER_FIELD_INT: + int32_t *returnInt = 0; + ret = verify->getSignSpecInt((HcfVerify *)verify, (SignSpecItem)type, returnInt); + + break; + case CRYPTO_SM2_USER_ID_DATABLOB: + // only support mgf1 + ret = verify->setVerifySpecUint8Array((HcfVerify *)verify, (SignSpecItem)type, (HcfBlob *)value); + break; + case CRYPTO_PSS_MD_NAME_STR: + case CRYPTO_PSS_MGF_NAME_STR: + case CRYPTO_PSS_MGF1_NAME_STR: + char *returnStr = NULL; + ret = verify->getVerifySpecString((HcfVerify *)verify, (SignSpecItem)type, &returnStr); + value->data = (uint8_t *)returnStr; + value->len = strlen(returnStr); + break; + default: + LOGE("Invalid input cipher spec"); + return CRYPTO_INVALID_PARAMS; + } + return ret; +} + + +void OH_CryptoVerify_Destroy(OH_CryptoVerify *verify) +{ + return verify->base.destroy((HcfVerify *)verify); +} \ No newline at end of file diff --git a/frameworks/native/src/sym_cipher.c b/frameworks/native/src/sym_cipher.c new file mode 100644 index 0000000..7d5e30b --- /dev/null +++ b/frameworks/native/src/sym_cipher.c @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2024 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 "sym_cipher.h" +#include "crypto_common.h" + +struct HcfCipher { + HcfObjectBase base; + + HcfResult (*init)(HcfCipher *self, enum HcfCryptoMode opMode, + HcfKey *key, HcfParamsSpec *params); + + HcfResult (*update)(HcfCipher *self, HcfBlob *input, HcfBlob *output); + + HcfResult (*doFinal)(HcfCipher *self, HcfBlob *input, HcfBlob *output); + + const char *(*getAlgorithm)(HcfCipher *self); + + HcfResult (*setCipherSpecUint8Array)(HcfCipher *self, CipherSpecItem item, HcfBlob blob); + + HcfResult (*getCipherSpecString)(HcfCipher *self, CipherSpecItem item, char **returnString); + + HcfResult (*getCipherSpecUint8Array)(HcfCipher *self, CipherSpecItem item, HcfBlob *returnUint8Array); +}; + +struct OH_CryptoSymCipherParams { + HcfParamsSpec base; + HcfBlob iv; + HcfBlob aad; + HcfBlob tag; +}; + +Crypto_Result OH_CryptoSymCipherParams_Create(OH_CryptoSymCipherParams **params) +{ + *params = (OH_CryptoSymCipherParams *)malloc(sizeof(OH_CryptoSymCipherParams)); + return CRYPTO_SUCCESS; +} + +Crypto_Result OH_CryptoSymCipherParams_SetParam(OH_CryptoSymCipherParams *params, CryptoSymCipher_ParamsType paramsType, Crypto_DataBlob *value) +{ + if ((params == NULL) || (value == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + switch (paramsType) { + case CRYPTO_IV_DATABLOB: + params.iv = value; + break; + case CRYPTO_AAD_DATABLOB: + params.aad = value; + break; + case CRYPTO_TAG_DATABLOB: + params.tag = value; + break; + default: + break; + } + return CRYPTO_SUCCESS; +} + +void OH_CryptoSymCipherParams_Destroy(OH_CryptoSymCipherParams *params) +{ + if (params != NULL) { + free(params); + } +} + +Crypto_Result OH_CryptoSymCipher_Create(const char *algoName, OH_CryptoSymCipher **ctx) +{ + return (Crypto_Result)HcfCipherCreate(algoName, (HcfCipher **)ctx ); +} + +Crypto_Result OH_CryptoSymCipher_Init(OH_CryptoSymCipher *ctx, OH_Crypto_CryptoMode mod, OH_CryptoSymKey *key, OH_CryptoSymCipherParams *params) +{ + return (Crypto_Result)ctx->init((HcfCipher *)ctx, (HcfCryptoMode)mod, (HcfKey *)key, (HcfParamsSpec *)params); +} + +Crypto_Result OH_CryptoSymCipher_Update(OH_CryptoSymCipher *ctx, Crypto_DataBlob *in, Crypto_DataBlob *out) +{ + return (Crypto_Result)ctx->update((HcfCipher *)ctx, (HcfBlob* )in, (HcfBlob *)out); +} + +Crypto_Result OH_CryptoSymCipher_Final(OH_CryptoSymCipher *ctx, Crypto_DataBlob *out) +{ + return (Crypto_Result)ctx->doFinal((HcfCipher *)ctx, (HcfBlob* )out); +} + +const char *OH_CryptoSymCipher_GetAlgoName(OH_CryptoSymKey *ctx) +{ + return ctx->getAlgorithm((HcfCipher *)ctx), +} + +void OH_CryptoSymCipher_Destroy(OH_CryptoSymKey *ctx) +{ + return ctx->base->destroy((HcfCipher *)ctx); +} \ No newline at end of file diff --git a/frameworks/native/src/sym_key.c b/frameworks/native/src/sym_key.c new file mode 100644 index 0000000..2db5c76 --- /dev/null +++ b/frameworks/native/src/sym_key.c @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2024 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 "sym_key.h" +#include "crypto_common.h" +#include "sym_key_generator.h" +#include "key/sym_key.h" +#include "result.h" + + +struct OH_CryptoSymKeyGenerator { + HcfObjectBase base; + + /** Generate symmetric key object */ + HcfResult (*generateSymKey)(HcfSymKeyGenerator *self, HcfSymKey **symKey); + + /** Convert byte data to symmetric key object */ + HcfResult (*convertSymKey)(HcfSymKeyGenerator *self, const HcfBlob *key, HcfSymKey **symKey); + + /** Get the algorithm name of the current these key generator objects */ + const char *(*getAlgoName)(HcfSymKeyGenerator *self); +}; + +struct OH_CryptoSymKey { + HcfKey key; + + void (*clearMem)(HcfSymKey *self); +}; + +Crypto_Result OH_CryptoSymKeyGenerator_Create(const char *algoName, OH_CryptoSymKeyGenerator **ctx) +{ + return (Crypto_Result)HcfSymKeyGeneratorCreate(algoName, (HcfSymKeyGenerator **)ctx); +} + +Crypto_Result OH_CryptoSymKeyGenerator_Generate(OH_CryptoSymKeyGenerator *ctx, OH_CryptoSymKey **keyCtx) +{ + return (Crypto_Result)ctx->generateSymKey((HcfSymKeyGenerator *)ctx, (HcfSymKey **)key); +} + +Crypto_Result OH_CryptoSymKeyGenerator_Convert(OH_CryptoSymKeyGenerator *ctx, const Crypto_DataBlob *keyData, OH_CryptoSymKey **key) +{ + return (Crypto_Result)ctx->convertSymKey((HcfSymKeyGenerator *)ctx, (HcfBlob *)keyData, (HcfSymKey **)key); +} + +const char *OH_CryptoSymKeyGenerator_GetAlgoName(OH_CryptoSymKeyGenerator *ctx) +{ + return ctx->getAlgoName((HcfSymKeyGenerator *)ctx); +} + +void OH_CryptoSymKeyGenerator_Destroy(OH_CryptoSymKeyGenerator *ctx) +{ + return ctx->base.destroy((HcfSymKeyGenerator *)ctx); +} + +const char *OH_CryptoSymKey_GetAlgoName(OH_CryptoSymKey *key); +{ + return key->key.getAlgorithm((HcfKey *)key->key); +} + +Crypto_Result OH_CryptoSymKey_GetKeyData(OH_CryptoSymKey *key, Crypto_DataBlob *out) +{ + return key->key.getEncoded((HcfKey *)key->key, (HcfBlob *)out); +} + +void OH_CryptoSymKey_Destroy(OH_CryptoSymKey *key) +{ + return key->key.base.destroy((HcfKey *)key->key); +} diff --git a/interfaces/kits/native/include/asym_key.h b/interfaces/kits/native/include/asym_key.h new file mode 100644 index 0000000..ce62eeb --- /dev/null +++ b/interfaces/kits/native/include/asym_key.h @@ -0,0 +1,216 @@ +/* + * Copyright (C) 2024 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. + */ + +#ifndef ASYM_KEY_H +#define ASYM_KEY_H + +/** + * @addtogroup CryptoAsymKeyApi + * @{ + * + * @brief Describe the features provided by the OpenHarmony asymmetric key related interface for applications. + * + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + * @version 1.0 + */ + +/** + * @file asym_key.h + * + * @brief Defines the AsymKey APIs. + * + * @kit Crypto Architecture Kit + * @since 12 + * @version 1.0 + */ + +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct OH_CryptoKeyPair OH_CryptoKeyPair; +typedef struct OH_CryptoPubKey OH_CryptoPubKey; + +typedef enum { + CRYPTO_DSA_P_DATABLOB = 101, + CRYPTO_DSA_Q_DATABLOB = 102, + CRYPTO_DSA_G_DATABLOB = 103, + CRYPTO_DSA_SK_DATABLOB = 104, + CRYPTO_DSA_PK_DATABLOB = 105, + + CRYPTO_ECC_FP_P_DATABLOB = 201, + CRYPTO_ECC_A_DATABLOB = 202, + CRYPTO_ECC_B_DATABLOB = 203, + CRYPTO_ECC_G_X_DATABLOB = 204, + CRYPTO_ECC_G_Y_DATABLOB = 205, + CRYPTO_ECC_N_DATABLOB = 206, + CRYPTO_ECC_H_INT = 207, // warning: ECC_H_NUM in JS + CRYPTO_ECC_SK_DATABLOB = 208, + CRYPTO_ECC_PK_X_DATABLOB = 209, + CRYPTO_ECC_PK_Y_DATABLOB = 210, + CRYPTO_ECC_FIELD_TYPE_STR = 211, + CRYPTO_ECC_FIELD_SIZE_INT = 212, // warning: ECC_FIELD_SIZE_NUM in JS + CRYPTO_ECC_CURVE_NAME_STR = 213, + + CRYPTO_RSA_N_DATABLOB = 301, + CRYPTO_RSA_D_DATABLOB = 302, + CRYPTO_RSA_E_DATABLOB = 303, + + CRYPTODH_P_DATABLOB = 401, + CRYPTODH_G_DATABLOB = 402, + CRYPTODH_L_NUM = 403, + CRYPTODH_SK_DATABLOB = 404, + CRYPTODH_PK_DATABLOB = 405, + + CRYPTOED25519_SK_DATABLOB = 501, + CRYPTOED25519_PK_DATABLOB = 502, + CRYPTOX25519_SK_DATABLOB = 601, + CRYPTOX25519_PK_DATABLOB = 602, +} CryptoAsymKey_ParamType; + +typedef enum { + CRYPTO_PEM = 0, + CRYPTO_DER = 1, +} Crypto_Encoding_type; + +typedef struct OH_CryptoAsymKeyGenerator OH_CryptoAsymKeyGenerator; + +/** + * @brief Create the asymmetric key generator instance according to the given algorithm name. + * + * @param algoName Indicates the algorithm name for generating the generator. + * @param ctx Indicates the pointer to asymmetric key generator instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoAsymKeyGenerator_Create(const char *algoName, OH_CryptoAsymKeyGenerator **ctx); + +/** + * @brief Randomly generate asymmetric keys. + * + * @param ctx Indicates the asymmetric key generator instance. + * @param keyCtx Indicates the pointer to the asyKey instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoAsymKeyGenerator_Generatey(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoSymKey **keyCtx); + +/** + * @brief Generate asymmetric keys for specified data. + * + * @param ctx Indicates the asymmetric key generator instance. + * @param type Indicates encryption encoding type. + * @param pubKeyData Indicates public key data. + * @param priKeyData Indicates private key data. + * @param keyPair Indicates the pointer to the keyPair instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *ctx, Crypto_Encoding_type type, Crypto_DataBlob *pubKeyData, Crypto_DataBlob *priKeyData, OH_CryptoKeyPair **key); + +/** + * @brief Get AsymKey AlgoName. + * + * @param ctx Indicates the asymmetric key generator instance. + * @return Returns the asymmetric key algorithm name. + * @since 12 + * @version 1.0 + */ +const char *OH_CryptoAsymKeyGenerator_GetAlgoName(OH_CryptoAsymKeyGenerator *ctx); + +/** + * @brief Destroy the AsymKey generater. + * + * @param ctx Indicates the asymmetric key generator instance. + * @since 12 + * @version 1.0 + */ +void OH_CryptoAsymKeyGenerator_Destroy(OH_CryptoAsymKeyGenerator *ctx); + +/** + * @brief Destroy the KeyPair generater. + * + * @param key Indicates the keyPair instance. + * @since 12 + * @version 1.0 + */ +void OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *key); + +/** + * @brief Get pubkey from keypair. + * + * @param key Indicates the keyPair instance. + * @return Return the public key obtained from the key pair. + * @since 12 + * @version 1.0 + */ +const char *OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair *key); + +/** + * @brief The public key outputs the result according to the specified encoding specification. + * + * @param key pubkey. + * @param type pubkey type. + * @param encodingStandard encoding standard . + * @param out Encoded result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoPubKey_Encode(OH_CryptoPubKey *key, Crypto_Encoding_type type, const char *encodingStandard, Crypto_DataBlob *out); + +/** + * @brief Get param from the pubKey instance. + * + * @param key pubkey. + * @param item Asym Key Param Type. + * @param value Specify the value of Param Type output. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/interfaces/kits/native/include/crypto_common.h b/interfaces/kits/native/include/crypto_common.h new file mode 100644 index 0000000..2cb6aca --- /dev/null +++ b/interfaces/kits/native/include/crypto_common.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2024 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. + */ + +#ifndef CRYPTO_COMMON_H +#define CRYPTO_COMMON_H + +/** + * @addtogroup CryptoCommonApi + * @{ + * + * @brief Describe OpenHarmony common interfaces Provide for applications. + * + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + * @version 1.0 + */ + +/** + * @file crypto_common.h + * + * @brief Defines the CryptoCommon APIs. + * + * @kit Crypto Architecture Kit + * @since 12 + * @version 1.0 + */ + +#include +#include + +typedef struct Crypto_DataBlob { + uint8_t *data; + size_t len; +} Crypto_DataBlob; + +typedef enum { + CRYPTO_SUCCESS = 0, + CRYPTO_INVALID_PARAMS = -10001, + CRYPTO_NOT_SUPPORT = -10002, + CRYPTO_ERR_MALLOC = -20001, + CRYPTO_CRYPTO_OPERTION = -30001, +} Crypto_Result; + +typedef enum { + CRYPTO_ENCRYPT_MODE = 0, + CRYPTO_DECRYPT_MODE = 1, +} Crypto_CipherMode; + +/** + * @brief Create the AsymKey generater. + * + * @param dataBlob DataBlob to be free. + * @since 12 + * @version 1.0 + */ +void OH_Crypto_DataBlobFree(Crypto_DataBlob *dataBlob); + + +#endif \ No newline at end of file diff --git a/interfaces/kits/native/include/digest.h b/interfaces/kits/native/include/digest.h new file mode 100644 index 0000000..24cda5c --- /dev/null +++ b/interfaces/kits/native/include/digest.h @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2024 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. + */ + +#ifndef DIGEST_H +#define DIGEST_H + +/** + * @addtogroup CryptoDigestApi + * @{ + * + * @brief Describe OpenHarmony encryption features, including key generation, + * encryption and decryption, signature verification, and digest interfaces + * Provide for applications. + * + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + * @version 1.0 + */ + +/** + * @file digest.h + * + * @brief Defines the Digest APIs. + * + * @kit Crypto Architecture Kit + * @since 12 + * @version 1.0 + */ + +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct OH_CryptoDigest OH_CryptoDigest; + +/** + * @brief Create the Digest generater. + * + * @param algoName Indicates the algorithm name for generating the generator. + * @param md Indicates the pointer to the md instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoDigest_Create(const char *algoName, OH_CryptoDigest **md); + +/** + * @brief Update md with DataBlob. + * + * @param md Indicates the pointer to the md instance. + * @param in Indicates the DataBlob. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoDigest_Update(OH_CryptoDigest *ctx, Crypto_DataBlob *in); + +/** + * @brief Update md with DataBlob. + * + * @param md Indicates the pointer to the md instance. + * @param out Return the result as DataBlob. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoDigest_Final(OH_CryptoDigest *ctx, Crypto_DataBlob *out); + +/** + * @brief Get digest length. + * + * @param md Indicates the pointer to the md instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +uint32_t OH_CryptoDigest_GetLength(OH_CryptoDigest *ctx); + +/** + * @brief Get digest algoName. + * + * @param md Indicates the pointer to the md instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +const char *OH_CryptoDigest_GetAlgoName(OH_CryptoDigest *ctx); + +/** + * @brief Destroy digest pointer. + * + * @param md Indicates the pointer to the md instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +void OH_DigestCrypto_Destroy(OH_CryptoDigest *ctx); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* DIGEST_H */ \ No newline at end of file diff --git a/interfaces/kits/native/include/signature.h b/interfaces/kits/native/include/signature.h new file mode 100644 index 0000000..ecdedcb --- /dev/null +++ b/interfaces/kits/native/include/signature.h @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2024 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. + */ + +#ifndef SIGNATURE_H +#define SIGNATURE_H + +/** + * @addtogroup CryptoSignatureApi + * @{ + * + * @brief Describe OpenHarmony signature verification interfaces Provide for applications. + * + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + * @version 1.0 + */ + +/** + * @file signature.h + * + * @brief Defines the Signature APIs. + * + * @kit Crypto Architecture Kit + * @since 12 + * @version 1.0 + */ + +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + CRYPTO_PSS_MD_NAME_STR = 100, + CRYPTO_PSS_MGF_NAME_STR = 101, + CRYPTO_PSS_MGF1_NAME_STR = 102, + CRYPTO_PSS_SALT_LEN_INT = 103, + CRYPTO_PSS_TRAILER_FIELD_INT = 104, + CRYPTO_SM2_USER_ID_DATABLOB = 105, +} CryptoSignature_ParamType; + +typedef struct OH_CryptoVerify OH_CryptoVerify; + +/** + * @brief Create the Verify generater. + * + * @param algoName Indicates the algorithm name for generating the generator. + * @param verify Indicates the pointer to the verify instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoVerify_Create(const char *algoName, OH_CryptoVerify **verify); + +/** + * @brief Init verify with given pubKey. + * + * @param verify Indicates the verify instance. + * @param pubKey indicates the pubKey + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoVerify_Init(OH_CryptoVerify *verify, OH_CryptoPubKey *pubKey); + +/** + * @brief Update verify with DataBlob. + * + * @param verify Indicates the verify instance. + * @param in Indicates the dataBlob. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoVerify_Update(OH_CryptoVerify *verify, Crypto_DataBlob *in); + +/** + * @brief Output the result of verify calculation. + * + * @param verify Indicates the verify instance. + * @param in Indicates the dataBlob. + * @param out Indicates the result of verify. + * @return Return result use bool value. + * @since 12 + * @version 1.0 + */ +bool OH_CryptoVerify_Final(OH_CryptoVerify *verify, Crypto_DataBlob *in, Crypto_DataBlob *signData); + +/** + * @brief Used to recover signed data. + * + * @param verify Indicates the verify instance. + * @param signData the signature data. + * @param rawSignData the rawSign data + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoVerify_Recover(OH_CryptoVerify *verify, Crypto_DataBlob *signData, Crypto_DataBlob *rawSignData); + +/** + * @brief Get the verify algoName. + * + * @param verify Indicates the verify instance. + * @return Return verify algorithm name. + * @since 12 + * @version 1.0 + */ +const char *OH_CryptoVerify_GetAlgoName(OH_CryptoVerify *verify); + +/** + * @brief Set the specified parameter to the verify object. + * + * @param verify Indicates the verify instance. + * @param type Indicates the verify signature_paramType. + * @param value Indicates the verify result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoVerify_SetParam(OH_CryptoVerify *verify, CryptoSignature_ParamType type, Crypto_DataBlob *value); + +/** + * @brief Get the specified parameter from the verify object. + * + * @param verify Indicates the verify instance. + * @param type Indicates the verify signature_paramType. + * @param value Indicates the verify result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoVerify_GetParam(OH_CryptoVerify *verify, CryptoSignature_ParamType type, Crypto_DataBlob *value); + +/** + * @brief Destroy the verify generater. + * + * @param verify Indicates the verify instance. + * @since 12 + * @version 1.0 + */ +void OH_CryptoVerify_Destroy(OH_CryptoVerify *verify); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/interfaces/kits/native/include/sym_cipher.h b/interfaces/kits/native/include/sym_cipher.h new file mode 100644 index 0000000..51a3d7b --- /dev/null +++ b/interfaces/kits/native/include/sym_cipher.h @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2024 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. + */ + +#ifndef SYM_CIPHER_H +#define SYM_CIPHER_H + +/** + * @addtogroup CryptoSymCipherApi + * @{ + * + * @brief Describe the functions provided by the OpenHarmony symmetric key encryption and decryption interface for applications. + * + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + * @version 1.0 + */ + +/** + * @file sym_cipher.h + * + * @brief Defines the SymCipher APIs. + * + * @kit Crypto Architecture Kit + * @since 12 + * @version 1.0 + */ + +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + CRYPTO_IV_DATABLOB = 100, + CRYPTO_AAD_DATABLOB = 101, + CRYPTO_TAG_DATABLOB = 102, +} CryptoSymCipher_ParamsType; + +typedef struct OH_CryptoSymCipher OH_CryptoSymCipher; +typedef struct OH_CryptoSymCipherParams OH_CryptoSymCipherParams; + +/** + * @brief Create the cipherParams generater. + * + * @param params Indicates the pointer to the cipherParams instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoSymCipherParams_Create(OH_CryptoSymCipherParams **params); + +/** + * @brief Set parameters for the cipher object. + * + * @param params Indicates the parameters instance. + * @param paramsType Set Cipher parameters. + * @param value Indicates the setParam result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoSymCipherParams_SetParam(OH_CryptoSymCipherParams *params, CryptoSymCipher_ParamsType paramsType, Crypto_DataBlob *value); + +/** + * @brief Destroy the cipherParams generater. + * + * @param params Indicates the parameters instance. + * @since 12 + * @version 1.0 + */ +void OH_CryptoSymCipherParams_Destroy(OH_CryptoSymCipherParams *params); + +/** + * @brief Create the SymCipher generater. + * + * @param algoName Indicates the algorithm name for generating the generator. + * @param ctx Indicates the pointer to the SymCipher instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoSymCipher_Create(const char *algoName, OH_CryptoSymCipher **ctx); + +/** + * @brief Init symCipher with params. + * + * @param ctx Indicates the symCipher instance. + * @param mod Indicates whether the mode is encrypted or decrypted. + * @param key Indicates symmetric key. + * @param params Indicates symCipherParams instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoSymCipher_Init(OH_CryptoSymCipher *ctx, OH_Crypto_CryptoMode mod, OH_CryptoSymKey *key, OH_CryptoSymCipherParams *params); + +/** + * @brief Update symCipher with dataBlob. + * + * @param ctx Indicates the symCipher instance. + * @param in Indicates the input dataBlob. + * @param out Indicates the output dataBlob. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoSymCipher_Update(OH_CryptoSymCipher *ctx, Crypto_DataBlob *in, Crypto_DataBlob *out); + +/** + * @brief Output symCipher result with dataBlob + * + * @param ctx Indicates the symCipher instance. + * @param out Indicates the output dataBlob. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoSymCipher_Final(OH_CryptoSymCipher *ctx, Crypto_DataBlob *out); + +/** + * @brief Get the symCipher algoName. + * + * @param ctx Indicates the symKey instance. + * @return Return symCipher algorithm name. + * @since 12 + * @version 1.0 + */ +const char *OH_CryptoSymCipher_GetAlgoName(OH_CryptoSymKey *ctx); + +/** + * @brief Destroy the symCipher generater. + * + * @param ctx Indicates the symKey instance. + * @since 12 + * @version 1.0 + */ +void OH_CryptoSymCipher_Destroy(OH_CryptoSymKey *ctx); + + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/interfaces/kits/native/include/sym_key.h b/interfaces/kits/native/include/sym_key.h new file mode 100644 index 0000000..c147b6a --- /dev/null +++ b/interfaces/kits/native/include/sym_key.h @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2024 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. + */ + +#ifndef SYM_KEY_H +#define SYM_KEY_H + +#include "crypto_common.h" + +/** + * @addtogroup CryptoSymKeyApi + * @{ + * + * @brief Describe OpenHarmony symmetric key related features interfaces Provide for applications. + * + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + * @version 1.0 + */ + +/** + * @file sym_key.h + * + * @brief Defines the SymKey APIs. + * + * @kit Crypto Architecture Kit + * @since 12 + * @version 1.0 + */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct OH_CryptoSymKey OH_CryptoSymKey; +typedef struct OH_CryptoSymKeyGenerator OH_CryptoSymKeyGenerator; + +/** + * @brief Create the SymKey generater. + * + * @param algoName Indicates the algorithm name for generating the generator. + * @param ctx Indicates the pointer to the SymKey generater instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoSymKeyGenerator_Create(const char *algoName, OH_CryptoSymKeyGenerator **ctx); + +/** + * @brief Randomly generate a symmetric key object. + * + * @param ctx Indicates the SymKey generater instance. + * @param keyCtx Indicates the pointer to the SymKey instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoSymKeyGenerator_Generate(OH_CryptoSymKeyGenerator *ctx, OH_CryptoSymKey **keyCtx); + +/** + * @brief Generation symmetric keys for specified data. + * + * @param ctx Indicates the SymKey generater instance. + * @param keyData Indicates the data to generate the Symkey. + * @param key Indicates the pointer to the SymKey instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoSymKeyGenerator_Convert(OH_CryptoSymKeyGenerator *ctx, const Crypto_DataBlob *keyData, OH_CryptoSymKey **key); + +/** + * @brief Get symmetric keys algorithm name from generater instance. + * + * @param ctx Indicates the SymKey generater instance. + * @return Return symmetric keys algorithm name. + * @since 12 + * @version 1.0 + */ +const char *OH_CryptoSymKeyGenerator_GetAlgoName(OH_CryptoSymKeyGenerator *ctx); + +/** + * @brief Destroy symmetric keys generater. + * + * @param ctx Indicates the SymKey generater instance. + * @since 12 + * @version 1.0 + */ +void OH_CryptoSymKeyGenerator_Destroy(OH_CryptoSymKeyGenerator *ctx); + +/** + * @brief Get symmetric keys algorithm name from key. + * + * @param key Indicates the SymKey instance. + * @return Return algorithm name. + * @since 12 + * @version 1.0 + */ +const char *OH_CryptoSymKey_GetAlgoName(OH_CryptoSymKey *key); + +/** + * @brief Create the AsymKey generater. + * + * @param key Indicates the SymKey instance. + * @param out Indicate to obtain the result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. + * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. + * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 1763001 - If crypto opertion failed. + * @since 12 + * @version 1.0 + */ +Crypto_Result OH_CryptoSymKey_GetKeyData(OH_CryptoSymKey *key, Crypto_DataBlob *out); + +/** + * @brief Destroy symmetric keys. + * + * @param key Indicates the SymKey instance. + * @since 12 + * @version 1.0 + */ +void OH_CryptoSymKey_Destroy(OH_CryptoSymKey *key); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file -- Gitee