diff --git a/frameworks/native/src/asym_key.c b/frameworks/native/src/asym_key.c index 5007aa7400aa794cdbfb5510b23a896316320874..4f329e400819a0c5f845446cf5db1cb5b279b5e3 100644 --- a/frameworks/native/src/asym_key.c +++ b/frameworks/native/src/asym_key.c @@ -14,6 +14,11 @@ */ #include "crypto_asym_key.h" +#include "detailed_ecc_key_params.h" +#include "detailed_dh_key_params.h" +#include "detailed_rsa_key_params.h" +#include "detailed_dsa_key_params.h" +#include "detailed_alg_25519_key_params.h" #include #include #include "key_pair.h" @@ -224,3 +229,30 @@ OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_P } return GetOhCryptoErrCode(ret); } + +struct OH_CryptoAsymKeySpec { + union KeySpec + { + HcfEccCommParamsSpec *eccCommSpec; + HcfEccPubKeyParamsSpec *eccPubKeySpec; + HcfEccPriKeyParamsSpec *eccPriKeySpec; + HcfEccKeyPairParamsSpec *eccKeyPairSpec; + + HcfDhCommParamsSpec *dhCommSpec; + HcfDhPubKeyParamsSpec *dhPubKeySpec; + HcfDhPriKeyParamsSpec *dhPriKeySpec; + HcfDhKeyPairParamsSpec *dhKeyPairSpec; + + HcfRsaCommParamsSpec *rsaCommSpec; + HcfRsaPubKeyParamsSpec *rsaPubKeySpec; + HcfRsaKeyPairParamsSpec *rsaKeyPairSpec; + + HcfDsaCommParamsSpec *dsaCommSpec; + HcfDsaPubKeyParamsSpec *dsaPubKeySpec; + HcfDsaKeyPairParamsSpec *dsaKeyPairSpec; + + HcfAlg25519PubKeyParamsSpec *alg25519CommSpec; + HcfAlg25519PriKeyParamsSpec *alg25519PrivCommSpec; + HcfAlg25519KeyPairParamsSpec *ed25519CommSpec; + }; +}; diff --git a/interfaces/kits/native/include/crypto_architecture_kit.h b/interfaces/kits/native/include/crypto_architecture_kit.h new file mode 100644 index 0000000000000000000000000000000000000000..22ee233c5a8743395e50eb046995aa2673146b1c --- /dev/null +++ b/interfaces/kits/native/include/crypto_architecture_kit.h @@ -0,0 +1,56 @@ +/* + * 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. + */ + +/** + * @addtogroup CryptoArchitectureKit + * @{ + * + * @brief Provides an entry to the crypto header files for you to reference. + * + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + */ + +/** + * @file crypto_architecture_kit.h + * + * @brief Provides an entry to the crypto header files for you to reference. + * + * @library libohcrypto.so + * @kit CryptoArchitectureKit + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + */ + +#ifndef CRYPTO_ARCHITECTURE_KIT_H +#define CRYPTO_ARCHITECTURE_KIT_H + +#include "crypto_common.h" +#include "crypto_asym_key.h" +#include "crypto_asym_cipher.h" +#include "crypto_digest.h" +#include "crypto_signature.h" +#include "crypto_kdf.h" +#include "crypto_rand.h" +#include "crypto_mac.h" +#include "crypto_key_agreement.h" +#include "crypto_sym_cipher.h" +#include "crypto_sym_key.h" + + +/** @} */ + + +#endif /* CRYPTO_ARCHITECTURE_KIT_H*/ \ No newline at end of file diff --git a/interfaces/kits/native/include/crypto_asym_cipher.h b/interfaces/kits/native/include/crypto_asym_cipher.h new file mode 100644 index 0000000000000000000000000000000000000000..2dd6585b96497ce669f011acd19fb387c5b8c380 --- /dev/null +++ b/interfaces/kits/native/include/crypto_asym_cipher.h @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2025 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. + */ + +/** + * @addtogroup CryptoAsymCipherApi + * @{ + * + * @brief Describe openHarmony asymmetric cipher interfaces provide for applications. + * + * @since 17 + */ + +/** + * @file crypto_asym_cipher.h + * + * @brief Defines the asymmetric cipher APIs. + * + * @library libohcrypto.so + * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework + * @since 17 + */ + +#ifndef CRYPTO_ASYM_CIPHER_H +#define CRYPTO_ASYM_CIPHER_H + +#include "crypto_common.h" +#include "crypto_asym_key.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Define the asymmetric cipher structure. + * + * @since 17 + */ +typedef struct OH_CryptoAsymCipher OH_CryptoAsymCipher; + +/** + * @brief Create a asymmetric cipher context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name used to generate the asymmetric cipher context. Example RSA2048|PKCS1. + * @param ctx Indicates the pointer to the asymmetric cipher context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymCipher_Create(const char *algoName, OH_CryptoAsymCipher **ctx); + +/** + * @brief Init the crypto operation with the given crypto mode, key and parameters. + * + * @param ctx Indicates the asymmetric cipher context. + * @param mod Indicates the crypto mode is encryption or decryption. + * @param key Indicates the asymmetric key. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + */ +OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_CipherMode mod, OH_CryptoKeyPair *key); + +/** + * @brief Final the crypto operation. + * + * @param ctx Indicates the asymmetric cipher context. + * @param in Indicates the input data. + * @param out Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + */ +OH_Crypto_ErrCode OH_CryptoAsymCipher_Final(OH_CryptoAsymCipher *ctx, const Crypto_DataBlob *in, Crypto_DataBlob *out); + +/** + * @brief Destroy the asymmetric cipher context. + * + * @param ctx Indicates the asymmetric cipher context. + */ +void OH_CryptoAsymCipher_Destroy(OH_CryptoAsymCipher *ctx); + +/** + * @brief Define the sm2 ciphertext spec structure. + * + * @since 17 + */ +typedef struct OH_CryptoSm2CiphertextSpec OH_CryptoSm2CiphertextSpec; + +/** + * @brief Define the sm2 ciphertext spec item type. + * + * @since 17 + */ +typedef enum { + /** Public key x, also known as C1x. */ + CRYPTO_SM2_CIPHERTEXT_C1_X = 0, + /** Public key y, also known as C1y. */ + CRYPTO_SM2_CIPHERTEXT_C1_Y = 1, + /** Hash, also known as C2. */ + CRYPTO_SM2_CIPHERTEXT_C2 = 2, + /** Ciphertext data, also known as C3. */ + CRYPTO_SM2_CIPHERTEXT_C3 = 3, +} CryptoSm2CiphertextSpec_item; + +/** + * @brief Create a sm2 ciphertext spec. + * + * @param in Indicates the sm2 ciphertext in ASN1 format, if in is NULL, then an empty sm2 ciphertext spec will be created. + * @param spec Indicates the output sm2 ciphertext spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_Create(Crypto_DataBlob *in, OH_CryptoSm2CiphertextSpec **spec); + +/** + * @brief Get the specified item of the sm2 ciphertext. + * + * @param spec Indicates the sm2 ciphertext spec. + * @param item Indicates the sm2 ciphertext spec item. + * @param out Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_GetItem(OH_CryptoSm2CiphertextSpec *spec, + CryptoSm2CiphertextSpec_item item, Crypto_DataBlob *out); + +/** + * @brief Set the specified item to the sm2 ciphertext spec. + * + * @param spec Indicates the sm2 ciphertext spec. + * @param item Indicates the sm2 ciphertext spec item. + * @param in Indicates the input data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_SetItem(OH_CryptoSm2CiphertextSpec *spec, + CryptoSm2CiphertextSpec_item item, Crypto_DataBlob *in); + +/** + * @brief Encode the sm2 ciphertext spec to ciphertext in ASN1 format. + * + * @param spec Indicates the sm2 ciphertext spec. + * @param out Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_Encode(OH_CryptoSm2CiphertextSpec *spec, Crypto_DataBlob *out); + +/** + * @brief Destroy the sm2 ciphertext spec. + * + * @param spec Indicates the sm2 ciphertext spec context. + * @since 17 + */ +void OH_CryptoSm2CiphertextSpec_Destroy(OH_CryptoSm2CiphertextSpec *spec); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif \ No newline at end of file diff --git a/interfaces/kits/native/include/crypto_asym_key.h b/interfaces/kits/native/include/crypto_asym_key.h index ea16609c6b9b43007eb8b6abf5d4f96cf34b55d3..c303eab6ec93b65ff569ac9a791ab759f9a6fb45 100644 --- a/interfaces/kits/native/include/crypto_asym_key.h +++ b/interfaces/kits/native/include/crypto_asym_key.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Huawei Device Co., Ltd. + * Copyright (C) 2024-2025 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 @@ -56,6 +56,13 @@ typedef struct OH_CryptoKeyPair OH_CryptoKeyPair; */ typedef struct OH_CryptoPubKey OH_CryptoPubKey; +/** + * @brief Define the private Key structure. + * + * @since 17 + */ +typedef struct OH_CryptoPrivKey OH_CryptoPrivKey; + /** * @brief Define the asymmetric key parameter types. * @@ -227,12 +234,21 @@ void OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *keyCtx); */ OH_CryptoPubKey *OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair *keyCtx); +/** + * @brief Get the private key of the key pair. + * + * @param keyCtx Indicates the keyPair context. + * @return Return the private key context from the key pair. + * @since 17 + */ +OH_CryptoPrivKey *OH_CryptoKeyPair_GetPrivKey(OH_CryptoKeyPair *keyCtx); + /** * @brief Encode the public key. * * @param key Indicates the public key. * @param type Indicates the pubkey type. - * @param encodingStandard Indicates the encoding standard . + * @param encodingStandard Indicates the encoding standard. * @param out Indicates the encoded result. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. @@ -259,6 +275,268 @@ OH_Crypto_ErrCode OH_CryptoPubKey_Encode(OH_CryptoPubKey *key, Crypto_EncodingTy */ OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value); +// 私钥加密 +/** + * @brief Define the private key encoding params structure. + * + * @since 17 + */ +typedef struct OH_CryptoPrivKeyEncodingParams OH_CryptoPrivKeyEncodingParams; + +/** + * @brief Define the private key encoding param type. + * + * @since 17 + */ +typedef enum { + /** Indicates the password string. */ + CRYPTO_PRIVKEY_ENCODING_PASSWORD_STR = 0, + + /** Indicates the symmetric key data blob. */ + CRYPTO_PRIVKEY_ENCODING_SYMETRIC_KEY_DATABLOB = 1, +} CryptoPrivKeyEncoding_Paramtype; + +/** + * @brief Create the private key encoding params. + * + * @param ctx Indicates the private key encoding params. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_Create(OH_CryptoPrivKeyEncodingParams **ctx); + +/** + * @brief Set the private key encoding params. + * + * @param ctx Indicates the private key encoding params. + * @param type Indicates the private key encoding param type. + * @param params Indicates the private key encoding params, it can be NULL, and if you want encypt the private key, + * you should set this param. + * @param value Indicates the private key encoding params value. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_Set(OH_CryptoPrivKeyEncodingParams *ctx, + CryptoPrivKeyEncoding_Paramtype type, Crypto_DataBlob *value); + +/** + * @brief Destroy the private key encoding params. + * + * @param ctx Indicates the private key encoding params. + * @since 17 + */ +void OH_CryptoPrivKeyEncodingParams_Destroy(OH_CryptoPrivKeyEncodingParams *ctx); + +/** + * @brief Encode the private key. + * + * @param key Indicates the private key. + * @param type Indicates the private encoding type. + * @param encodingStandard Indicates the encoding standard, such as "PKCS8". + * @param params Indicates the private key encoding params, it can be NULL, and if you want encypt the private key, + * you should set this param. + * @param out Indicates the encoded result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoPrivKey_Encode(OH_CryptoPrivKey *key, Crypto_EncodingType type, + const char *encodingStandard, OH_CryptoPrivKeyEncodingParams *params, Crypto_DataBlob *out); + +/** + * @brief Get the specified param of the private key. + * + * @param key Indicates the private key. + * @param item Indicates the asymmetric key param type. + * @param value Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoPrivKey_GetParam(OH_CryptoPrivKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value); + +/** + * @brief Define the asymmetric key spec structure. + * + * @since 17 + */ +typedef struct OH_CryptoAsymKeySpec OH_CryptoAsymKeySpec; + +/** + * @brief Define the asymmetric key spec type. + * + * @since 17 + */ +typedef enum { + /** Common parameters spec. */ + CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC = 0, + /** Private key spec. */ + CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC = 1, + /** Public key spec. */ + CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC = 2, + /** Key pair spec. */ + CRYPTO_ASYM_KEY_KEY_PAIR_SPEC = 3, +} CryptoAsymKeySpec_Type; + +/** + * @brief Generate ECC common parameters spec. + * + * @param curveName Indicates the ECC curve name. + * @param spec Indicates the pointer to the ECC common parameters spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenEccCommonParamsSpec(const char *curveName, OH_CryptoAsymKeySpec **spec); + +/** + * @brief Generate DH common parameters spec. + * + * @param pLen Indicates the byte length of the prime p. + * @param skLen Indicates the byte length of the private key. + * @param spec Indicates the pointer to the DH common parameters spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenDhCommonParamsSpec(int pLen, int skLen, OH_CryptoAsymKeySpec **spec); + +/** + * @brief Create an asymmetric key spec according to the given algorithm name and spec type. + * + * @param algoName Indicates the algorithm name for generating the spec. Example RSA. + * @param type Indicates the asymmetric key spec type. + * @param spec Indicates the pointer to the asymmetric key spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_Create(const char *algoName, CryptoAsymKeySpec_Type type, + OH_CryptoAsymKeySpec **spec); + +/** + * @brief Set the specified parameter to the asymmetric key spec. + * + * @param spec Indicates the asymmetric key spec. + * @param type Indicates the asymmetric key param type. + * @param value Indicates the input data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value); + +/** + * @brief Set the common parameters spec to the asymmetric key spec. + * + * @param spec Indicates the asymmetric key spec. + * @param commonSpec Indicates the common parameters spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec *spec, + OH_CryptoAsymKeySpec *commonParamsSpec); + +/** + * @brief Get the specified parameter from the asymmetric key spec. + * + * @param spec Indicates the asymmetric key spec. + * @param type Indicates the asymmetric key param type. + * @param value Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value); + +/** + * @brief Destroy the asymmetric key spec. + * + * @param spec Indicates the asymmetric key spec. + * @since 17 + */ +void OH_CryptoAsymKeySpec_Destroy(OH_CryptoAsymKeySpec *spec); + +/** + * @brief Define the asymmetric key generator with spec. + * + * @since 17 + */ +typedef struct OH_CryptoAsymKeyGeneratorWithSpec OH_CryptoAsymKeyGeneratorWithSpec; + +/** + * @brief Create an asymmetric key generator with spec. + * + * @param keySpec Indicates the asymmetric key spec. + * @param generator Indicates the asymmetric key generator with spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorWithSpec_Create(OH_CryptoAsymKeySpec *keySpec, + OH_CryptoAsymKeyGeneratorWithSpec **generator); + +/** + * @brief Generate a key pair according to the asymmetric key spec. + * + * @param generator Indicates the asymmetric key generator with spec. + * @param keyPair Indicates the pointer to the key pair. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorWithSpec_GenKeyPair(OH_CryptoAsymKeyGeneratorWithSpec *generator, + OH_CryptoKeyPair **keyPair); + +/** + * @brief Destroy the asymmetric key generator with spec. + * + * @param generator Indicates the asymmetric key generator with spec. + * @since 17 + */ +void OH_CryptoAsymKeyGeneratorWithSpec_Destroy(OH_CryptoAsymKeyGeneratorWithSpec *generator); + #ifdef __cplusplus } #endif diff --git a/interfaces/kits/native/include/crypto_kdf.h b/interfaces/kits/native/include/crypto_kdf.h new file mode 100644 index 0000000000000000000000000000000000000000..d4719a7cf6a20749ecce54f491368ff1260ec200 --- /dev/null +++ b/interfaces/kits/native/include/crypto_kdf.h @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2025 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. + */ + +/** + * @addtogroup CryptoKdfApi + * @{ + * + * @brief Describe openHarmony KDF interfaces provide for applications. + * + * @since 17 + */ + +/** + * @file crypto_kdf.h + * + * @brief Defines the KDF APIs. + * + * @library libohcrypto.so + * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework + * @since 17 + */ + +#ifndef CRYPTO_KDF_H +#define CRYPTO_KDF_H + +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Define the KDF structure. + * + * @since 17 + */ +typedef struct OH_CryptoKdf OH_CryptoKdf; + +/** + * @brief Define the KDF params structure. + * + * @since 17 + */ +typedef struct OH_CryptoKdfParams OH_CryptoKdfParams; + +/** + * @brief Define the KDF param type. + * + * @since 17 + */ +typedef enum { + /** Indicates the key or password for KDF. */ + CRYPTO_KDF_KEY_DATABLOB = 100, + + /** Indicates the salt for KDF. */ + CRYPTO_KDF_SALT_DATABLOB = 101, + + /** Indicates the info for KDF. */ + CRYPTO_KDF_INFO_DATABLOB = 102, + + /** Indicates the iteration count for KDF. */ + CRYPTO_KDF_ITER_COUNT_INT = 103, + + /** Indicates the n for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_N_UINT64 = 103, + + /** Indicates the r for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_R_UINT64 = 104, + + /** Indicates the p for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_P_UINT64 = 105, + + /** Indicates the maxMem for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_MAXMEM_UINT64 = 106, +} CryptoKdf_ParamType; + +/** + * @brief Create a KDF params. + * + * @param algoName Indicates the KDF algorithm name. + * @param params Indicates the KDF params. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoKdfParams_Create(const char *algoName, OH_CryptoKdfParams **params); + +/** + * @brief Set a parameter to the KDF parameters. + * + * @param params Indicates the KDF parameters. + * @param type Indicates the KDF parameter type. + * @param value Indicates the KDF parameter value. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoKdfParams_SetParam(OH_CryptoKdfParams *params, CryptoKdf_ParamType type, + Crypto_DataBlob *value); + +/** + * @brief Destroy the KDF params. + * + * @param params Indicates the parameters. + * @since 17 + */ +void OH_CryptoKdfParams_Destroy(OH_CryptoKdfParams *params); + +/** + * @brief Create a KDF context. + * + * @param algoName Indicates the KDF algorithm name. + * @param ctx Indicates the KDF context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoKdf_Create(const char *algoName, OH_CryptoKdf **ctx); + +/** + * @brief Derive a key. + * + * @param ctx The KDF context. + * @param kdfParams The KDF parameters. + * @param keyLen The derived key length. + * @param out The derived key. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoKdf_Derive(OH_CryptoKdf *ctx, const OH_CryptoKdfParams *kdfParams, int keyLen, + Crypto_DataBlob *out); + +/** + * @brief Destroy the KDF. + * + * @param ctx The KDF. + * @since 17 + */ +void OH_CryptoKdf_Destroy(OH_CryptoKdf *ctx); + + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* CRYPTO_KDF_H */ \ No newline at end of file diff --git a/interfaces/kits/native/include/crypto_key_agreement.h b/interfaces/kits/native/include/crypto_key_agreement.h new file mode 100644 index 0000000000000000000000000000000000000000..9738e0dc618d30067a186ca40c6af66f47d719bd --- /dev/null +++ b/interfaces/kits/native/include/crypto_key_agreement.h @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2025 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. + */ + +/** + * @addtogroup CryptoAsymCipherApi + * @{ + * + * @brief Describe openHarmony asymmetric cipher interfaces provide for applications. + * + * @since 17 + */ + +/** + * @file crypto_asym_cipher.h + * + * @brief Defines the asymmetric cipher APIs. + * + * @library libohcrypto.so + * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework + * @since 17 + */ + +#ifndef CRYPTO_KEY_AGREEMENT_H +#define CRYPTO_KEY_AGREEMENT_H + +#include "crypto_common.h" +#include "crypto_asym_key.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Define the key agreement param type. + * + * @since 17 + */ +typedef struct OH_CryptoKeyAgreement OH_CryptoKeyAgreement; + +/** + * @brief Create a key agreement context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name used to generate the key agreement context. + * Example AES128|GCM|PKCS7. + * @param ctx Indicates the pointer to the key agreement context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoKeyAgreement_Create(const char *algoName, OH_CryptoKeyAgreement **ctx); + +/** + * @brief Derive the key agreement result. + * + * @param ctx Indicates the key agreement context. + * @param privkey Indicates the private key. + * @param pubkey Indicates the public key. + * @param out Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + */ +OH_Crypto_ErrCode OH_CryptoKeyAgreement_Derive(OH_CryptoKeyAgreement *ctx, OH_CryptoPrivKey *privkey, + OH_CryptoPubKey *pubkey, Crypto_DataBlob *out); + +/** + * @brief Destroy the key agreement context. + * + * @param ctx Indicates the key agreement context. + */ +void OH_CryptoKeyAgreement_Destroy(OH_CryptoKeyAgreement *ctx); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif \ No newline at end of file diff --git a/interfaces/kits/native/include/crypto_mac.h b/interfaces/kits/native/include/crypto_mac.h new file mode 100644 index 0000000000000000000000000000000000000000..45a71061de353669a4975d5cece4c44cfabd631f --- /dev/null +++ b/interfaces/kits/native/include/crypto_mac.h @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2025 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. + */ + +/** + * @addtogroup CryptoMacApi + * @{ + * + * @brief Describe openHarmony mac interfaces provide for applications. + * + * @since 17 + */ +/** + * @file crypto_mac.h + * + * @brief Defines the mac APIs. + * + * @library libohcrypto.so + * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework + * @since 17 + */ + +#ifndef CRYPTO_MAC_H +#define CRYPTO_MAC_H + +#include "crypto_common.h" +#include "crypto_sym_key.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Define the mac param type. + * + * @since 17 + */ +typedef enum { + /** Indicates the algorithm name of the message digest function.*/ + CRYPTO_MAC_DIDEST_NAME_STR = 0, + + /** Indicates the algorithm name of the symmetric cipher function.*/ + CRYPTO_MAC_CIPHER_NAME_STR = 1, +} CryptoMac_ParamType; + +/** + * @brief Define the mac structure. + * + * @since 17 + */ +typedef struct OH_CryptoMac OH_CryptoMac; + +/** + * @brief Create a mac context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name for generating the mac context. + * @param ctx Indicates the pointer to the mac context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx); + +/** + * @brief Set the specified parameter to the mac context. + * + * @param ctx Indicates the mac context. + * @param type Indicates the mac parameter type. + * @param value Indicates the parameter value. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoMac_SetParam(OH_CryptoMac *ctx, CryptoMac_ParamType type, const Crypto_DataBlob *value); + +/** + * @brief Initialize the mac context with a symmetric key. + * + * @param ctx Indicates the mac context. + * @param key Indicates the symmetric key. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *key); + +/** + * @brief Update mac with dataBlob. + * + * @param ctx Indicates the mac context. + * @param input 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 parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @see OH_CryptoMac_Final + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob *input); + +/** + * @brief Finalize mac with dataBlob. + * + * @param ctx Indicates the mac context. + * @param out Indicates 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 parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @see OH_CryptoMac_Update + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoMac_Final(OH_CryptoMac *ctx, Crypto_DataBlob *out); + +/** + * @brief Get the mac length of the mac context. + * + * @param ctx Indicates the mac context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +uint32_t OH_CryptoMac_GetLength(OH_CryptoMac *ctx); + +/** + * @brief Destroy the mac context. + * + * @param ctx Indicates the pointer to the mac context. + * @since 17 + */ +void OH_CryptoMac_Destroy(OH_CryptoMac *ctx); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* CRYPTO_MAC_H */ \ No newline at end of file diff --git a/interfaces/kits/native/include/crypto_rand.h b/interfaces/kits/native/include/crypto_rand.h new file mode 100644 index 0000000000000000000000000000000000000000..f2dde56ac96bf98933b5cd0894c918284bcca012 --- /dev/null +++ b/interfaces/kits/native/include/crypto_rand.h @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2025 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. + */ + +/** + * @addtogroup CryptoRandApi + * @{ + * + * @brief Describe the functions provided by the openHarmony random number generator interface for applications. + * + * @since 17 + */ +/** + * @file crypto_rand.h + * + * @brief Defines the random number generator APIs. + * + * @library libohcrypto.so + * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework + * @since 17 + */ +#ifndef CRYPTO_RAND_H + +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Define the random number generator structure. + * + * @since 17 + */ +typedef struct OH_CryptoRand OH_CryptoRand; + +/** + * @brief Create a random number generator. + * + * @param ctx Indicates the pointer to the random number generator context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoRand_Create(OH_CryptoRand **ctx); + +/** + * @brief Generate a random number. + * + * @param ctx Indicates the pointer to the random number generator context. + * @param len Indicates the length of the random number. + * @param out Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoRand_GenerateRandom(OH_CryptoRand *ctx, int len, Crypto_DataBlob *out); + +/** + * @brief Get the algorithm name of the random number generator context. + * + * @param ctx Indicates the pointer to the random number generator context. + * @return Return the algorithm name of the random number generator context. + * @since 17 + */ +const char *OH_CryptoRand_GetAlgoName(OH_CryptoRand *ctx); + +/** + * @brief Set the seed of the random number generator. + * + * @param ctx Indicates the pointer to the random number generator context. + * @param seed Indicates the pointer to the seed. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoRand_SetSeed(OH_CryptoRand *ctx, Crypto_DataBlob *seed); + +/** + * @brief Destroy the random number generator context. + * + * @param ctx Indicates the pointer to the random number generator context. + * @since 17 + */ +void OH_CryptoRand_Destroy(OH_CryptoRand *ctx); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* CRYPTO_RAND_H */ \ No newline at end of file diff --git a/interfaces/kits/native/include/crypto_signature.h b/interfaces/kits/native/include/crypto_signature.h index eb77a1bf24a0420022c639d9e48682a2fe1f31c6..982fbbc956e7ab76c5c9f29be805796165bd6808 100644 --- a/interfaces/kits/native/include/crypto_signature.h +++ b/interfaces/kits/native/include/crypto_signature.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Huawei Device Co., Ltd. + * Copyright (C) 2024-2025 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 @@ -71,6 +71,13 @@ typedef enum { */ typedef struct OH_CryptoVerify OH_CryptoVerify; +/** + * @brief Define the signature structure. + * + * @since 17 + */ +typedef struct OH_CryptoSign OH_CryptoSign; + /** * @brief Create a verify context according to the given algorithm name. * @@ -159,8 +166,8 @@ const char *OH_CryptoVerify_GetAlgoName(OH_CryptoVerify *ctx); * @brief Set the specified parameter to the verify context. * * @param ctx Indicates the verify context. - * @param type Indicates the verify signature_paramType. - * @param value Indicates the verify result. + * @param type Indicates the verify parameter type. + * @param value Indicates the input data. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. @@ -175,8 +182,8 @@ OH_Crypto_ErrCode OH_CryptoVerify_SetParam(OH_CryptoVerify *ctx, CryptoSignature * @brief Get the specified parameter from the verify context. * * @param ctx Indicates the verify context. - * @param type Indicates the verify signature_paramType. - * @param value Indicates the verify result. + * @param type Indicates the verify parameter type. + * @param value Indicates the output data. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. @@ -195,6 +202,185 @@ OH_Crypto_ErrCode OH_CryptoVerify_GetParam(OH_CryptoVerify *ctx, CryptoSignature */ void OH_CryptoVerify_Destroy(OH_CryptoVerify *ctx); +/** + * @brief Create a sign context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name for generating the sign context. Example RSA1024|PKCS1|SHA256. + * @param sign Indicates the pointer to the sign context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoSign_Create(const char *algoName, OH_CryptoSign **sign); + +/** + * @brief Initialize the sign context. + * + * @param ctx Indicates the sign context. + * @param privKey Indicates the private key. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoSign_Init(OH_CryptoSign *ctx, OH_CryptoPrivKey *privKey); + +/** + * @brief Update sign data. + * + * @param ctx Indicates the sign context. + * @param data Indicates the data to be signed. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoSign_Update(OH_CryptoSign *ctx, const Crypto_DataBlob *data); + +/** + * @brief Finish the sign operation. + * + * @param ctx Indicates the sign context. + * @param data Indicates the data to be signed. + * @param out Indicates the sign result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoSign_Final(OH_CryptoSign *ctx, const Crypto_DataBlob *data, Crypto_DataBlob *out); + +/** + * @brief Get the algorithm name of the sign context. + * + * @param ctx Indicates the sign context. + * @return Return signature algorithm name. + * @since 12 + */ +const char *OH_CryptoSign_GetAlgoName(OH_CryptoSign *ctx); + +/** + * @brief Set the specified parameter to the sign context. + * + * @param ctx Indicates the sign context. + * @param type Indicates the signature parameter type. + * @param value Indicates the input data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSign_SetParam(OH_CryptoSign *ctx, CryptoSignature_ParamType type, + const Crypto_DataBlob *value); + +/** + * @brief Get the specified parameter from the sign context. + * + * @param ctx Indicates the sign context. + * @param type Indicates the signature parameter type. + * @param value Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSign_GetParam(OH_CryptoSign *ctx, CryptoSignature_ParamType type, Crypto_DataBlob *value); + +/** + * @brief Destroy the sign context. + * + * @param ctx Indicates the sign context. + * @since 17 + */ +void OH_CryptoSign_Destroy(OH_CryptoSign *ctx); + + +/** + * @brief Define the ECC signature data spec. + * + * @since 17 + */ +typedef struct OH_CryptoEccSignatureDataSpec OH_CryptoEccSignatureDataSpec; + +/** + * @brief Create the ECC signature data spec, alse support SM2 signature. + * + * @param in Indicates the ECC signature data in ASN1 format, if in is NULL, then an empty ECC signature data spec will be created. + * @param eccSignatureDataSpec Indicates the output ECC signature data spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_Create(Crypto_DataBlob *in, OH_CryptoEccSignatureDataSpec **eccSignatureDataSpec); + +/** + * @brief Get the r and s value from the ECC signature data spec. + * + * @param eccSignatureDataSpec Indicates the ECC signature data spec. + * @param r Indicates the output r value. + * @param s Indicates the output s value. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_GetRAndS(OH_CryptoEccSignatureDataSpec *eccSignatureDataSpec, Crypto_DataBlob *r, Crypto_DataBlob *s); + +/** + * @brief Set the r and s value to the ECC signature data spec. + * + * @param eccSignatureDataSpec Indicates the ECC signature data spec. + * @param r Indicates the input r value. + * @param s Indicates the input s value. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_SetRAndS(OH_CryptoEccSignatureDataSpec *eccSignatureDataSpec, Crypto_DataBlob *r, Crypto_DataBlob *s); + +/** + * @brief Encode the ECC signature data spec to signature data in ASN1 format. + * + * @param eccSignatureDataSpec Indicates the ECC signature data spec. + * @param out Indicates the output data blob. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_Encode(OH_CryptoEccSignatureDataSpec *eccSignatureDataSpec, Crypto_DataBlob *out); + +/** + * @brief Destroy the ECC signature data spec. + * + * @param eccSignatureDataSpec Indicates the ECC signature data spec. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_Destroy(OH_CryptoEccSignatureDataSpec *eccSignatureDataSpec); + #ifdef __cplusplus } #endif diff --git a/interfaces/kits/native/include/crypto_sym_cipher.h b/interfaces/kits/native/include/crypto_sym_cipher.h index 3e4245fef0110e7ff3980d04e11ed573e949dd5d..0dee8d4dc7f55c0c155b906178f4b22f1c6173c3 100644 --- a/interfaces/kits/native/include/crypto_sym_cipher.h +++ b/interfaces/kits/native/include/crypto_sym_cipher.h @@ -89,8 +89,8 @@ OH_Crypto_ErrCode OH_CryptoSymCipherParams_Create(OH_CryptoSymCipherParams **par * @brief Set a parameter to the cipher params context. * * @param params Indicates the parameters context. - * @param paramsType Set cipher parameters. - * @param value Indicates the setParam result. + * @param paramsType Indicates the cipher parameter type. + * @param value Indicates the input data. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. diff --git a/interfaces/kits/native/include/example.c b/interfaces/kits/native/include/example.c new file mode 100644 index 0000000000000000000000000000000000000000..895a64199721276ed72418b75778eb43971d167c --- /dev/null +++ b/interfaces/kits/native/include/example.c @@ -0,0 +1,306 @@ +#include "crypto_architecture_kit.h" + + +// 非对称加密 +#include "crypto_common.h" +#include "crypto_asym_cipher.h" +int asym_cipher_test() +{ + // 1. 生成密钥对 + OH_CryptoAsymKeyGenerator *keyGen = NULL; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("RSA3072", &keyGen); + OH_CryptoKeyPair *KeyPair = NULL; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &KeyPair); + + // 2. 加密 + OH_CryptoAsymCipher *cipher = NULL; + ret = OH_CryptoAsymCipher_Create("RSA3072|PKCS1", &cipher); + ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_ENCRYPT_MODE, KeyPair); + const Crypto_DataBlob in = { + .data = "hello world", + .len = strlen("hello world"), + }; + + Crypto_DataBlob out = {0}; + ret = OH_CryptoAsymCipher_Final(cipher, &in, &out); + OH_CryptoAsymCipher_Destroy(cipher); + cipher = NULL; + + // 3. 解密 + Crypto_DataBlob out2 = {0}; + ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_DECRYPT_MODE, KeyPair); + ret = OH_CryptoAsymCipher_Final(cipher, &out, &out2); + OH_CryptoAsymCipher_Destroy(cipher); + cipher = NULL; + + OH_Crypto_FreeDataBlob(&out); + OH_Crypto_FreeDataBlob(&out2); +} + +// 获取sm2密文C1、C2和C3 +#include "crypto_common.h" +#include "crypto_asym_cipher.h" +int asym_cipher_test() +{ + // 1. 生成密钥对 + OH_CryptoAsymKeyGenerator *keyGen = NULL; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("SM2_128", &keyGen); + OH_CryptoKeyPair *KeyPair = NULL; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &KeyPair); + + // 2. 加密 + OH_CryptoAsymCipher *cipher = NULL; + ret = OH_CryptoAsymCipher_Create("SM2_128", &cipher); + ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_ENCRYPT_MODE, KeyPair); + const Crypto_DataBlob in = { + .data = "hello world", + .len = strlen("hello world"), + }; + + Crypto_DataBlob out = {0}; + ret = OH_CryptoAsymCipher_Final(cipher, &in, &out); + OH_CryptoAsymCipher_Destroy(cipher); + cipher = NULL; + + // 3. 获取C1、C2和C3 + OH_CryptoSm2CiphertextSpec *sm2CipherSpec = NULL; + ret = OH_CryptoSm2CiphertextSpec_Create(&out, &sm2CipherSpec); + OH_Crypto_FreeDataBlob(&out); + + Crypto_DataBlob c1x = {0}; + Crypto_DataBlob c1y = {0}; + Crypto_DataBlob c2 = {0}; + Crypto_DataBlob c3 = {0}; + ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_X, &c1x); + ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_Y, &c1y); + ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C2, &c2); + ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C3, &c3); + OH_CryptoSm2CipherSpec_Destroy(sm2CipherSpec); + sm2CipherSpec = NULL; + + // 4. 创建ASN1格式的sm2密文 + ret = OH_CryptoSm2CipherSpec_Create(NULL, &sm2CipherSpec); + ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_X, &c1x); + ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_Y, &c1y); + ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C2, &c2); + ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C3, &c3); + ret = OH_CryptoSm2CipherSpec_Encode(sm2CipherSpec, &out); + + OH_Crypto_FreeDataBlob(&c1x); + OH_Crypto_FreeDataBlob(&c1y); + OH_Crypto_FreeDataBlob(&c2); + OH_Crypto_FreeDataBlob(&c3); + OH_CryptoSm2CipherSpec_Destroy(sm2CipherSpec); + sm2CipherSpec = NULL; + OH_Crypto_FreeDataBlob(&out); +} + +// 随机数 +#include "crypto_common.h" +#include "crypto_rand.h" +int rand_test() +{ + OH_CryptoRand *rand = NULL; + OH_Crypto_ErrCode ret = OH_CryptoRand_Create(&rand); + uint8_t seedData[12] = {0x25, 0x65, 0x58, 0x89, 0x85, 0x55, 0x66, 0x77, 0x88, 0x99, 0x11, 0x22}; + Crypto_DataBlob seed = { + .data = seedData, + .len = sizeof(seedData), + }; + ret = OH_CryptoRand_SetSeed(rand, &seed); + Crypto_DataBlob out = {0}; + ret = OH_CryptoRand_GenerateRandom(rand, 10, &out); + printf("rand algname = %s", OH_CryptoRand_GetAlgoName(rand)); + OH_CryptoRand_Destroy(rand); +} + +// 消息认证码 +#include "crypto_common.h" +#include "crypto_mac.h" +int mac_test() +{ + // 1. 生成密钥 + OH_CryptoSymKeyGenerator *keyGen = NULL; + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("HMAC", &keyGen); + uint8_t deyData[14] = {0x56, 0x33, 0x2, 0xfa, 0x33, 0x54, 0x85, 0x67, 0x11, 0x22, 0x33, 0x44, 0x55, 0x88}; + Crypto_DataBlob keyDataBlob = { + .data = deyData, + .len = sizeof(deyData), + }; + + Crypto_DataBlob symKey = {0}; + OH_CryptoSymKey *keyCtx = NULL; + ret = OH_CryptoSymKeyGenerator_Convert(keyGen, &keyDataBlob, keyCtx); + + // 2. 计算MAC + OH_CryptoMac *ctx = NULL; + ret = OH_CryptoMac_Create("HMAC", &ctx); + ret = OH_CryptoMac_Init(ctx, &keyCtx); + const Crypto_DataBlob in = { + .data = "hello world", + .len = strlen("hello world"), + }; + + const Crypto_DataBlob in2 = { + .data = "hello openharmony", + .len = strlen("hello openharmony"), + }; + + ret = OH_CryptoMac_Update(ctx, &in); + ret = OH_CryptoMac_Update(ctx, &in2); + + Crypto_DataBlob out = {0}; + ret = OH_CryptoMac_Final(ctx, &out); + printf("rand algname = %u", OH_CryptoMac_GetLength(ctx)); + + OH_CryptoMac_Destroy(ctx); + OH_Crypto_FreeDataBlob(&out); +} + +// 密钥协商 +#include "crypto_common.h" +#include "crypto_key_agreement.h" +int mac_test() +{ + // 1. 生成密钥 + OH_CryptoSymKeyGenerator *keyGen = NULL; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("X25519", &keyGen); + OH_CryptoKeyPair *keyPairC = NULL; + OH_CryptoKeyPair *keyPairS = NULL; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPairC); + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPairS); + OH_CryptoAsymCipher_Destroy(keyGen); + + // 2. 协商密钥 + OH_CryptoKeyAgreement *ctx = NULL; + ret = OH_CryptoKeyAgreement_Create("X25519", &ctx); + + OH_CryptoPrivKey *privkey = OH_CryptoKeyPair_GetPrivKey(keyPairC); + OH_CryptoPubKey *pubkey = OH_CryptoKeyPair_GetPubKey(keyPairS); + Crypto_DataBlob key = {0}; + ret = OH_CryptoKeyAgreement_Derive(ctx, privkey, pubkey, &key); + + OH_CryptoKeyAgreement_Destroy(ctx); + OH_Crypto_FreeDataBlob(&key); +} + +// 签名 +#include "crypto_common.h" +#include "crypto_signature.h" +int signature_test() +{ + // 1. 生成密钥 + OH_CryptoAsymKeyGenerator *keyGen = NULL; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("RSA3072", &keyGen); + OH_CryptoKeyPair *keyPair = NULL; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); + OH_CryptoAsymKeyGenerator_Destroy(keyGen); + + // 2. 签名 + OH_CryptoSign *ctx = NULL; + ret = OH_CryptoSign_Create("RSA3072|PKCS1|SHA384", &ctx); + OH_CryptoPrivKey *privKey = NULL; + privKey = OH_CryptoKeyPair_GetPrivKey(keyPair); + ret = OH_CryptoSign_Init(ctx, privKey); + const Crypto_DataBlob in = { + .data = "hello world", + .len = strlen("hello world"), + }; + Crypto_DataBlob out = {0}; + ret = OH_CryptoSign_Update(ctx, &in); + ret = OH_CryptoSign_Final(ctx, NULL, &out); + printf("rand algname = %u", OH_CryptoSign_GetAlgoName(ctx)); + OH_CryptoSign_Destroy(ctx); + OH_Crypto_FreeDataBlob(&out); +} + +// SM2签名数据格式转换 +#include "crypto_common.h" +#include "crypto_signature.h" +int sm_signature_test() +{ + // 1. 生成密钥 + OH_CryptoAsymKeyGenerator *keyGen = NULL; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &keyGen); + OH_CryptoKeyPair *keyPair = NULL; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); + OH_CryptoAsymKeyGenerator_Destroy(keyGen); + + // 2. 签名 + OH_CryptoSign *ctx = NULL; + ret = OH_CryptoSign_Create("SM2_256|SM3", &ctx); + OH_CryptoPrivKey *privKey = NULL; + privKey = OH_CryptoKeyPair_GetPrivKey(keyPair); + ret = OH_CryptoSign_Init(ctx, privKey); + const Crypto_DataBlob in = { + .data = "hello world", + .len = strlen("hello world"), + }; + Crypto_DataBlob out = {0}; + ret = OH_CryptoSign_Update(ctx, &in); + ret = OH_CryptoSign_Final(ctx, NULL, &out); + printf("rand algname = %u", OH_CryptoSign_GetAlgoName(ctx)); + OH_CryptoSign_Destroy(ctx); + + // 获取R和S + OH_CryptoEccSignatureDataSpec *spec = NULL; + ret = OH_CryptoEccSignatureDataSpec_Create(&out, &spec); + Crypto_DataBlob r = {0}; + Crypto_DataBlob s = {0}; + ret = OH_CryptoEccSignatureDataSpec_GetRAndS(spec, &r, &s); + OH_CryptoEccSignatureDataSpec_Destroy(spec); + spec = NULL; + + // 由R和S生成ASN1格式的签名数据 + ret = OH_CryptoEccSignatureDataSpec_Create(NULL, &spec); + ret = OH_CryptoEccSignatureDataSpec_SetRAndS(spec, &r, &s); + Crypto_DataBlob sig = {0}; + ret = OH_CryptoEccSignatureDataSpec_Encode(spec, &sig); + OH_CryptoEccSignatureDataSpec_Destroy(spec); + spec = NULL; + OH_Crypto_FreeDataBlob(&out); + OH_Crypto_FreeDataBlob(&r); + OH_Crypto_FreeDataBlob(&s); + OH_Crypto_FreeDataBlob(&sig); +} + +// 密钥派生 +#include "crypto_common.h" +#include "crypto_kdf.h" +int kdf_test() +{ + // 1. 生成密钥 + OH_CryptoKdfParams *params = NULL; + OH_Crypto_ErrCode ret = OH_CryptoKdfParams_Create("HKDF", ¶ms); + + uint8_t deyData[14] = {0x56, 0x33, 0x2, 0xfa, 0x33, 0x54, 0x85, 0x67, 0x11, 0x22, 0x33, 0x44, 0x55, 0x88}; + Crypto_DataBlob keyDataBlob = { + .data = deyData, + .len = sizeof(deyData), + }; + + uint8_t saltData[4] = {0x85, 0xac, 0x2d, 0x05}; + Crypto_DataBlob saltDataBlob = { + .data = saltData, + .len = sizeof(saltData), + }; + + Crypto_DataBlob infoDataBlob = { + .data = "hello openharmony", + .len = strlen("hello openharmony"), + }; + + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_KEY_DATABLOB, &keyDataBlob); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_SALT_DATABLOB, &saltDataBlob); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_INFO_DATABLOB, &infoDataBlob); + + Crypto_DataBlob out = {0}; + + OH_CryptoKdf *kdfCtx = NULL; + ret = OH_CryptoKdf_Create("HKDF|SHA256|EXTRACT_AND_EXPAND", &kdfCtx); + ret = OH_CryptoKdf_Derive(kdfCtx, params, 32, &out); + OH_CryptoKdf_Destroy(kdfCtx); + OH_CryptoKdfParams_Destroy(params); + OH_Crypto_FreeDataBlob(&out); +} +