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..6641c700175d680c319631a07ec2d9d8cbde13a3 --- /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..649b2ddb5400351d9622ad0d1626c65f7fbd718e --- /dev/null +++ b/interfaces/kits/native/include/crypto_asym_cipher.h @@ -0,0 +1,204 @@ +/* + * 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 Describes the asymmetric encryption and decryption algorithm interface provided to 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 Defines the asymmetric cipher structure. + * + * @since 17 + */ +typedef struct OH_CryptoAsymCipher OH_CryptoAsymCipher; + +/** + * @brief Creates an 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 Inits the asymmetric cipher context with the given crypto mode, key and parameters. + * + * @param ctx Indicates the asymmetric cipher context. + * @param mode 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. + * @see OH_CryptoAsymCipher_Finalize + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_CipherMode mode, OH_CryptoKeyPair *key); + +/** + * @brief Finalizes the encryption or decryption 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. + * @see OH_CryptoAsymCipher_Init + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoAsymCipher_Finalize(OH_CryptoAsymCipher *ctx, const Crypto_DataBlob *in, + Crypto_DataBlob *out); + +/** + * @brief Destroys the asymmetric cipher context. + * + * @param ctx Indicates the asymmetric cipher context. + */ +void OH_CryptoAsymCipher_Destroy(OH_CryptoAsymCipher *ctx); + +/** + * @brief Defines the SM2 ciphertext spec structure. + * + * @since 17 + */ +typedef struct OH_CryptoSm2CiphertextSpec OH_CryptoSm2CiphertextSpec; + +/** + * @brief Defines 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 Creates a SM2 ciphertext spec. + * + * @param sm2Ciphertext Indicates the SM2 ciphertext in DER format, if sm2Ciphertext param is NULL, + * 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 *sm2Ciphertext, OH_CryptoSm2CiphertextSpec **spec); + +/** + * @brief Gets 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 Sets 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 Encodes the SM2 ciphertext spec to ciphertext in DER 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 Destroys the SM2 ciphertext spec. + * + * @param spec Indicates the SM2 ciphertext spec. + * @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..a91c163ca192e6f2c9d7702f0b6bfb95c6962cef 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,343 @@ 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 Defines the private key encoding params structure. + * + * @since 17 + */ +typedef struct OH_CryptoPrivKeyEncodingParams OH_CryptoPrivKeyEncodingParams; + +/** + * @brief Defines the private key encoding param type. + * + * @since 17 + */ +typedef enum { + /** Indicates the password string. */ + CRYPTO_PRIVATE_KEY_ENCODING_PASSWORD_STR = 0, + + /** Indicates the symmetric cipher string. */ + CRYPTO_PRIVATE_KEY_ENCODING_SYMMETRIC_CIPHER_STR = 1, +} CryptoPrivKeyEncoding_ParamType; + +/** + * @brief Creates 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 Sets the private key encoding params. + * + * @param ctx Indicates the private key encoding params. + * @param type Indicates the private key encoding param type. + * @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_SetParam(OH_CryptoPrivKeyEncodingParams *ctx, + CryptoPrivKeyEncoding_ParamType type, Crypto_DataBlob *value); + +/** + * @brief Destroys the private key encoding params. + * + * @param ctx Indicates the private key encoding params. + * @since 17 + */ +void OH_CryptoPrivKeyEncodingParams_Destroy(OH_CryptoPrivKeyEncodingParams *ctx); + +/** + * @brief Encodes 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 Gets 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 Generates an EC common parameters spec. + * + * @param curveName Indicates the ECC curve name. + * @param spec Indicates the pointer to the EC 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_GenEcCommonParamsSpec(const char *curveName, OH_CryptoAsymKeySpec **spec); + +/** + * @brief Generates a 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 Creates 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 Sets 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 Sets 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 Gets 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 Destroys the asymmetric key spec. + * + * @param spec Indicates the asymmetric key spec. + * @since 17 + */ +void OH_CryptoAsymKeySpec_Destroy(OH_CryptoAsymKeySpec *spec); + +/** + * @brief Defines the asymmetric key generator with spec. + * + * @since 17 + */ +typedef struct OH_CryptoAsymKeyGeneratorWithSpec OH_CryptoAsymKeyGeneratorWithSpec; + +/** + * @brief Creates 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 Generates 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 Destroys the asymmetric key generator with spec. + * + * @param generator Indicates the asymmetric key generator with spec. + * @since 17 + */ +void OH_CryptoAsymKeyGeneratorWithSpec_Destroy(OH_CryptoAsymKeyGeneratorWithSpec *generator); + +/** + * @brief Defines the EC point structure. + * + * @since 17 + */ +typedef struct OH_CryptoEcPoint OH_CryptoEcPoint; + +/** + * @brief Creates an EC point. + * + * @param x Indicates the x coordinate of the EC point. + * @param y Indicates the y coordinate of the EC point. + * @param point Indicates the pointer to the EC point. + * @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_CryptoEcPoint_Create(Crypto_DataBlob *x, Crypto_DataBlob *y, OH_CryptoEcPoint **point); + +/** + * @brief Converts the EC point from the specified format to the specified format. + * + * @param curveName Indicates the curve name. + * @param ecKeyData Indicates the EC point data, support "04 || x || y", "02 || x" or "03 || x" format. + * @param point Indicates the pointer to the EC point. + * @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_CryptoEcPoint_Convert(const char *curveName, Crypto_DataBlob *ecKeyData, OH_CryptoEcPoint **point); + +/** + * @brief Gets the x coordinate of the EC point. + * + * @param point Indicates the EC point. + * @param x Indicates the x coordinate of the EC point. + * @param y Indicates the y coordinate of the EC point. + * @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_CryptoEcPoint_GetXAndY(OH_CryptoEcPoint *point, Crypto_DataBlob *x, Crypto_DataBlob *y); + +/** + * @brief Encodes the EC point to the specified format. + * + * @param point Indicates the EC point. + * @param curveName Indicates the curve name. + * @param format Indicates the format, supports "UNCOMPRESSED" and "COMPRESSED". + * @param out Indicates the encoded ec point 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_CryptoEcPoint_Encode(OH_CryptoEcPoint *point, const char *curveName, const char *format, + Crypto_DataBlob *out); + +/** + * @brief Destroys the EC point. + * + * @param point Indicates the EC point. + * @since 17 + */ +void OH_CryptoEcPoint_Destroy(OH_CryptoEcPoint *point); + #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..61b1d4419a049b00036d67904660348a370195a3 --- /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 Describes the KDF algorithm interface provided to 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 Defines the KDF structure. + * + * @since 17 + */ +typedef struct OH_CryptoKdf OH_CryptoKdf; + +/** + * @brief Defines the KDF params structure. + * + * @since 17 + */ +typedef struct OH_CryptoKdfParams OH_CryptoKdfParams; + +/** + * @brief Defines the KDF param type. + * + * @since 17 + */ +typedef enum { + /** Indicates the key or password for KDF. */ + CRYPTO_KDF_KEY_DATABLOB = 0, + + /** Indicates the salt for KDF. */ + CRYPTO_KDF_SALT_DATABLOB = 1, + + /** Indicates the info for KDF. */ + CRYPTO_KDF_INFO_DATABLOB = 2, + + /** Indicates the iteration count for KDF. */ + CRYPTO_KDF_ITER_COUNT_INT = 3, + + /** Indicates the n for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_N_UINT64 = 4, + + /** Indicates the r for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_R_UINT64 = 5, + + /** Indicates the p for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_P_UINT64 = 6, + + /** Indicates the maxMem for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_MAXMEM_UINT64 = 7, +} CryptoKdf_ParamType; + +/** + * @brief Creates 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 Sets 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 Destroys the KDF params. + * + * @param params Indicates the KDF parameters. + * @since 17 + */ +void OH_CryptoKdfParams_Destroy(OH_CryptoKdfParams *params); + +/** + * @brief Creates 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 Derives a key. + * + * @param ctx The KDF context. + * @param params Indicates the KDF parameters. + * @param keyLen Indicates the derived key length. + * @param out Indicates 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 *params, int keyLen, + Crypto_DataBlob *out); + +/** + * @brief Destroys 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..38fb1844c58c19bab7915d9b8df7744b9d01fb4e --- /dev/null +++ b/interfaces/kits/native/include/crypto_key_agreement.h @@ -0,0 +1,98 @@ +/* + * 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 CryptoKeyAgreementApi + * @{ + * + * @brief Describes key agreement algorithm interface provided to applications. + * + * @since 17 + */ + +/** + * @file crypto_key_agreement.h + * + * @brief Defines the key agreement 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 Defines the key agreement structure. + * + * @since 17 + */ +typedef struct OH_CryptoKeyAgreement OH_CryptoKeyAgreement; + +/** + * @brief Creates a key agreement context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name used to generate a key agreement context. + * Example AES128|GCM|PKCS7. + * @param ctx Indicates 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 Generates a secret value. + * + * @param ctx Indicates the key agreement context. + * @param privkey Indicates the private key. + * @param pubkey Indicates the public key. + * @param out Indicates the secret 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_CryptoKeyAgreement_GenerateSecret(OH_CryptoKeyAgreement *ctx, OH_CryptoPrivKey *privkey, + OH_CryptoPubKey *pubkey, Crypto_DataBlob *out); + +/** + * @brief Destroys the key agreement context. + * + * @param ctx Indicates the key agreement context. + * @since 17 + */ +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..a50645df6479e474a13b2e81d57d66978377f658 --- /dev/null +++ b/interfaces/kits/native/include/crypto_mac.h @@ -0,0 +1,170 @@ +/* + * 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 Describes the MAC algorithm interface provided to applications. + * + * @since 17 + */ + +/** + * @file crypto_mac.h + * + * @brief Defines the MAC algorithm 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 Defines the MAC algorithm parameter type. + * + * @since 17 + */ +typedef enum { + /** Indicates the algorithm name of the message digest function.*/ + CRYPTO_MAC_DIGEST_NAME_STR = 0, + + /** Indicates the algorithm name of the symmetric cipher function.*/ + CRYPTO_MAC_CIPHER_NAME_STR = 1, +} CryptoMac_ParamType; + +/** + * @brief Defines the MAC structure. + * + * @since 17 + */ +typedef struct OH_CryptoMac OH_CryptoMac; + +/** + * @brief Creates 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 Sets 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 Initializes 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. + * @see OH_CryptoMac_Update + * @see OH_CryptoMac_Finalize + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *key); + +/** + * @brief Updates the MAC context with data. + * + * @param ctx Indicates the MAC context. + * @param input Indicates the data to update. + * @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_Init + * @see OH_CryptoMac_Finalize + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob *input); + +/** + * @brief Finalizes the MAC computation. + * + * @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_Init + * @see OH_CryptoMac_Update + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoMac_Finalize(OH_CryptoMac *ctx, Crypto_DataBlob *out); + +/** + * @brief Gets the length of the MAC. + * + * @param ctx Indicates the MAC context. + * @param length Indicates the MAC length. + * @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_GetLength(OH_CryptoMac *ctx, uint32_t *length); + +/** + * @brief Destroys the MAC context. + * + * @param ctx Indicates 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..7f45e64f32b6095a6a77b114a87bf2399afebf8d --- /dev/null +++ b/interfaces/kits/native/include/crypto_rand.h @@ -0,0 +1,114 @@ +/* + * 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 Describes the random number generation interface provided to 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 +#define CRYPTO_RAND_H + +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Defines the random number generator structure. + * + * @since 17 + */ +typedef struct OH_CryptoRand OH_CryptoRand; + +/** + * @brief Creates a random number generator context. + * + * @param ctx Indicates 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 Generates random numbers. + * + * @param ctx Indicates the random number generator context. + * @param len Indicates the byte 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 Gets 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 Sets the seed to the random number generator context. + * + * @param ctx Indicates the random number generator context. + * @param seed Indicates 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 Destroys the random number generator context. + * + * @param ctx Indicates 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..82f3eb79f52ab3d8d801d1cbbd7531912dd6e9fb 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,189 @@ OH_Crypto_ErrCode OH_CryptoVerify_GetParam(OH_CryptoVerify *ctx, CryptoSignature */ void OH_CryptoVerify_Destroy(OH_CryptoVerify *ctx); +/** + * @brief Creates a sign context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name for generating the sign context. Example RSA3072|PKCS1|SHA384. + * @param sign Indicates 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 Updates the data to be signed. + * + * @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 Finalizes 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_Finalize(OH_CryptoSign *ctx, const Crypto_DataBlob *data, Crypto_DataBlob *out); + +/** + * @brief Gets the algorithm name of the sign context. + * + * @param ctx Indicates the sign context. + * @return Return signature algorithm name. + * @since 17 + */ +const char *OH_CryptoSign_GetAlgoName(OH_CryptoSign *ctx); + +/** + * @brief Sets 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 17 + */ +OH_Crypto_ErrCode OH_CryptoSign_SetParam(OH_CryptoSign *ctx, CryptoSignature_ParamType type, + const Crypto_DataBlob *value); + +/** + * @brief Gets 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 17 + */ +OH_Crypto_ErrCode OH_CryptoSign_GetParam(OH_CryptoSign *ctx, CryptoSignature_ParamType type, Crypto_DataBlob *value); + +/** + * @brief Destroys the sign context. + * + * @param ctx Indicates the sign context. + * @since 17 + */ +void OH_CryptoSign_Destroy(OH_CryptoSign *ctx); + + +/** + * @brief Defines the EC signature data spec. + * + * @since 17 + */ +typedef struct OH_CryptoEcSignatureDataSpec OH_CryptoEcSignatureDataSpec; + +/** + * @brief Creates the EC signature data spec, also support SM2 signature. + * + * @param ecSignatureData Indicates the EC signature data in DER format, if ecSignatureData parameter is NULL, + * an empty EC signature data spec will be created. + * @param spec Indicates the output EC 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_CryptoEcSignatureDataSpec_Create(Crypto_DataBlob *ecSignatureData, + OH_CryptoEcSignatureDataSpec **spec); + +/** + * @brief Gets the r and s value from the EC signature data spec. + * + * @param spec Indicates the EC 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_CryptoEcSignatureDataSpec_GetRAndS(OH_CryptoEcSignatureDataSpec *spec, Crypto_DataBlob *r, + Crypto_DataBlob *s); + +/** + * @brief Sets the r and s value to the EC signature data spec. + * + * @param spec Indicates the EC 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_CryptoEcSignatureDataSpec_SetRAndS(OH_CryptoEcSignatureDataSpec *spec, Crypto_DataBlob *r, + Crypto_DataBlob *s); + +/** + * @brief Encodes the EC signature data spec to signature data in DER format. + * + * @param spec Indicates the EC 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_CryptoEcSignatureDataSpec_Encode(OH_CryptoEcSignatureDataSpec *spec, Crypto_DataBlob *out); + +/** + * @brief Destroys the EC signature data spec. + * + * @param spec Indicates the EC signature data spec. + * @since 17 + */ +void OH_CryptoEcSignatureDataSpec_Destroy(OH_CryptoEcSignatureDataSpec *spec); + #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..df78305616a5bb81ab85fceb5e95fa943ec2b952 --- /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); + uint16_t macLen; + ret = OH_CryptoMac_GetLength(ctx, &macLen); + + 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_CryptoEcSignatureDataSpec *spec = NULL; + ret = OH_CryptoEcSignatureDataSpec_Create(&out, &spec); + Crypto_DataBlob r = {0}; + Crypto_DataBlob s = {0}; + ret = OH_CryptoEcSignatureDataSpec_GetRAndS(spec, &r, &s); + OH_CryptoEcSignatureDataSpec_Destroy(spec); + spec = NULL; + + // 由R和S生成ASN1格式的签名数据 + ret = OH_CryptoEcSignatureDataSpec_Create(NULL, &spec); + ret = OH_CryptoEcSignatureDataSpec_SetRAndS(spec, &r, &s); + Crypto_DataBlob sig = {0}; + ret = OH_CryptoEcSignatureDataSpec_Encode(spec, &sig); + OH_CryptoEcSignatureDataSpec_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); +}