From 22c9531849ea86a3207549ac6696eb12ba7f409c Mon Sep 17 00:00:00 2001 From: lcc Date: Mon, 1 Jul 2024 10:56:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=97=E6=B3=95=E5=BA=93=E5=A2=9E=E5=8A=A0Pu?= =?UTF-8?q?blic=20C=20Api?= 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 | 14 ++ frameworks/native/src/crypto_common.c | 14 ++ frameworks/native/src/digest.c | 61 ++++++++ frameworks/native/src/signature.c | 14 ++ frameworks/native/src/sym_cipher.c | 14 ++ frameworks/native/src/sym_key.c | 14 ++ interfaces/kits/native/include/asym_key.h | 19 +++ .../kits/native/include/crypto_common.h | 65 ++++++++ interfaces/kits/native/include/digest.h | 142 ++++++++++++++++++ interfaces/kits/native/include/signature.h | 19 +++ interfaces/kits/native/include/sym_cipher.h | 19 +++ interfaces/kits/native/include/sym_key.h | 19 +++ 14 files changed, 469 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..c7ff8f3 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -21,6 +21,7 @@ group("crypto_framework_component") { "frameworks:crypto_framework_lib", "frameworks/cj:cj_cryptoframework_ffi", "frameworks/js/napi/crypto:cryptoframework_napi", + "frameworks/native:ohcrypto", "plugin:crypto_openssl_plugin_lib", ] } diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn new file mode 100644 index 0000000..7dfa79e --- /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 = [ + "../../interfaces/kits/native/include" + ] + + sources = [ + "src/crypto_common.c", + "src/digest.c", + ] + + deps = [ + "${framework_path}: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..13e52f9 --- /dev/null +++ b/frameworks/native/src/asym_key.c @@ -0,0 +1,14 @@ +/* + * 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. + */ \ 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..13e52f9 --- /dev/null +++ b/frameworks/native/src/crypto_common.c @@ -0,0 +1,14 @@ +/* + * 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. + */ \ 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..13e52f9 --- /dev/null +++ b/frameworks/native/src/signature.c @@ -0,0 +1,14 @@ +/* + * 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. + */ \ 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..13e52f9 --- /dev/null +++ b/frameworks/native/src/sym_cipher.c @@ -0,0 +1,14 @@ +/* + * 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. + */ \ 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..13e52f9 --- /dev/null +++ b/frameworks/native/src/sym_key.c @@ -0,0 +1,14 @@ +/* + * 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. + */ \ No newline at end of file diff --git a/interfaces/kits/native/include/asym_key.h b/interfaces/kits/native/include/asym_key.h new file mode 100644 index 0000000..d3ad941 --- /dev/null +++ b/interfaces/kits/native/include/asym_key.h @@ -0,0 +1,19 @@ +/* + * 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 + +#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..c7f33e8 --- /dev/null +++ b/interfaces/kits/native/include/crypto_common.h @@ -0,0 +1,65 @@ +/* + * 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; + +#ifdef __cplusplus +} +#endif + +#endif /* CRYPTO_COMMON_H */ \ 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..674716c --- /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} 401 - 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} 401 - 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} 401 - 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} 401 - 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} 401 - 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} 401 - 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..7c01533 --- /dev/null +++ b/interfaces/kits/native/include/signature.h @@ -0,0 +1,19 @@ +/* + * 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 + +#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..e12130f --- /dev/null +++ b/interfaces/kits/native/include/sym_cipher.h @@ -0,0 +1,19 @@ +/* + * 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 + +#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..d0f6ca3 --- /dev/null +++ b/interfaces/kits/native/include/sym_key.h @@ -0,0 +1,19 @@ +/* + * 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 + +#endif \ No newline at end of file -- Gitee