From 30ebf7b20fa00d3831354d34769b4b7c1a42fdbe Mon Sep 17 00:00:00 2001 From: lanming Date: Tue, 18 Feb 2025 21:51:33 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0CAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lanming --- frameworks/native/src/asym_key.c | 32 +++ .../kits/native/include/crypto_asym_cipher.h | 197 ++++++++++++++++++ .../kits/native/include/crypto_asym_key.h | 174 +++++++++++++++- interfaces/kits/native/include/crypto_kdf.h | 170 +++++++++++++++ .../native/include/crypto_key_agreement.h | 96 +++++++++ interfaces/kits/native/include/crypto_mac.h | 164 +++++++++++++++ interfaces/kits/native/include/crypto_rand.h | 112 ++++++++++ .../kits/native/include/crypto_signature.h | 122 ++++++++++- .../kits/native/include/crypto_sym_cipher.h | 4 +- 9 files changed, 1062 insertions(+), 9 deletions(-) create mode 100644 interfaces/kits/native/include/crypto_asym_cipher.h create mode 100644 interfaces/kits/native/include/crypto_kdf.h create mode 100644 interfaces/kits/native/include/crypto_key_agreement.h create mode 100644 interfaces/kits/native/include/crypto_mac.h create mode 100644 interfaces/kits/native/include/crypto_rand.h diff --git a/frameworks/native/src/asym_key.c b/frameworks/native/src/asym_key.c index 5007aa7..4f329e4 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_asym_cipher.h b/interfaces/kits/native/include/crypto_asym_cipher.h new file mode 100644 index 0000000..8db3888 --- /dev/null +++ b/interfaces/kits/native/include/crypto_asym_cipher.h @@ -0,0 +1,197 @@ +/* + * 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 param type. + * + * @since 17 + */ +typedef struct OH_CryptoAsymCipher OH_CryptoAsymCipher; + +/** + * @brief Create a asymmetric key cipher context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name used to generate the asymmetric key cipher context. + * Example AES128|GCM|PKCS7. + * @param ctx Indicates the pointer to the asymmetric key 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 12 + */ +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 key cipher context. + * @param mod Indicates the crypto mode is encryption or decryption. + * @param key Indicates the asymmetric key. + * @param params Indicates the algorithm parameters such as IV. + * @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 key 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 key cipher context. + * + * @param ctx Indicates the asymmetric key cipher context. + */ +void OH_CryptoAsymCipher_Destroy(OH_CryptoAsymCipher *ctx); + +/** + * @brief Define the asymmetric cipher param type. + * + * @since 17 + */ +typedef struct OH_CryptoSm2Cipher OH_CryptoSm2Cipher; + +/** + * @brief Define the asymmetric cipher format type. + * + * @since 17 + */ +typedef enum { + /** SM2 cipher format C1C3C2 */ + CRYPTO_SM2_CIPHER_C1C3C2_FORMAT = 0, + /** SM2 cipher format ASN1 */ + CRYPTO_SM2_CIPHER_ASN1_FORMAT = 1, +} CryptoSm2CipherFormat; + +/** + * @brief Define the sm2 cipher component type. + * + * @since 17 + */ +typedef enum { + /** SM2 cipher public key x*/ + CRYPTO_SM2_CIPHER_C1_X = 0, + /** SM2 cipher public key y*/ + CRYPTO_SM2_CIPHER_C1_Y = 1, + /** SM2 cipher hash */ + CRYPTO_SM2_CIPHER_C2 = 2, + /** SM2 ciphertext data */ + CRYPTO_SM2_CIPHER_C3 = 3, +} CryptoSm2CipherComponent; + +/** + * @brief Create the asymmetric cipher context. + * + * @param in Indicates the input data. + * @param format Indicates the format of the asymmetric cipher. + * @param sm2Cipher Indicates the output 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_CryptoSm2Cipher_Create(const Crypto_DataBlob *in, CryptoSm2CipherFormat format, OH_CryptoSm2Cipher **sm2Cipher); + +/** + * @brief Get the specified param of the asymmetric cipher. + * + * @param sm2Cipher Indicates the asymmetric cipher context. + * @param item Indicates the asymmetric cipher 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_CryptoSm2Cipher_GetComponent(OH_CryptoSm2Cipher *sm2Cipher, CryptoSm2CipherComponent item, Crypto_DataBlob *value); + +/** + * @brief Encode the asymmetric cipher. + * + * @param sm2Cipher Indicates the asymmetric cipher context. + * @param format Indicates the format of the asymmetric cipher. + * @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_CryptoSm2Cipher_Encode(OH_CryptoSm2Cipher *sm2Cipher, CryptoSm2CipherFormat format, Crypto_DataBlob *out); + +/** + * @brief Destroy the sm2 cipher context. + * + * @param sm2Cipher Indicates the sm2 cipher context. + * @since 17 + */ +void OH_CryptoSm2Cipher_Destroy(OH_CryptoSm2Cipher *sm2Cipher); + +#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 ea16609..a05dfc1 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,160 @@ 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 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 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 12 + */ +OH_Crypto_ErrCode OH_CryptoPrivKey_Encode(OH_CryptoPrivKey *key, Crypto_EncodingType type, + const char *encodingStandard, 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 12 + */ +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. */ + CRYPTO_COMMON_PARAMS_SPEC = 0, + /** Private key. */ + CRYPTO_PRIVATE_KEY_SPEC = 1, + /** Public key. */ + CRYPTO_PUBLIC_KEY_SPEC = 2, + /** Key pair. */ + CRYPTO_KEY_PAIR_SPEC = 3, +} CryptoAsymKeySpec_Type; + +/** + * @brief Create an asymmetric key spec according to the given algorithm name. + * + * @param algoName Indicates the algorithm name for generating the spec. Example RSA1024|PRIMES_2. + * @param type Indicates the asymmetric key spec type. + * @param ctx Indicates the pointer to asymmetric key spec 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_CryptoAsymKeySpec_Create(const char *algoName, CryptoAsymKeySpec_Type type, + OH_CryptoAsymKeySpec **ctx); + +/** + * @brief Set the specified parameter to the asymmetric key spec. + * + * @param keySpec Indicates the asymmetric key spec context. + * @param type Indicates the asymmetric key 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_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *keySpec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value); + +/** + * @brief Get the specified parameter from the asymmetric key spec. + * + * @param keySpec Indicates the asymmetric key spec context. + * @param type Indicates the asymmetric key 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_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec *keySpec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value); + +/** + * @brief Destroy the asymmetric key spec. + * + * @param keySpec Indicates the asymmetric key spec context. + * @since 17 + */ +void OH_CryptoAsymKeySpec_Destroy(OH_CryptoAsymKeySpec *keySpec); + +/** + * @brief Define the asymmetric key generator by spec structure. + * + * @since 17 + */ +typedef struct OH_CryptoAsymKeyGeneratorBySpec OH_CryptoAsymKeyGeneratorBySpec; + +/** + * @brief Create an asymmetric key generator by spec. + * + * @param generator Indicates the pointer to asymmetric key generator by spec 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_CryptoAsymKeyGeneratorBySpec_Create(OH_CryptoAsymKeySpec *keySpec, OH_CryptoAsymKeyGeneratorBySpec **generator); + +/** + * @brief Generate a key pair according to the asymmetric key spec. + * + * @param generator Indicates the asymmetric key generator by spec context. + * @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_CryptoAsymKeyGeneratorBySpec_Generate(OH_CryptoAsymKeyGeneratorBySpec *generator, + OH_CryptoKeyPair **keyPair); + +/** + * @brief Destroy the asymmetric key generator by spec. + * + * @param generator Indicates the asymmetric key generator by spec context. + * @since 17 + */ +void OH_CryptoAsymKeyGeneratorBySpec_Destroy(OH_CryptoAsymKeyGeneratorBySpec *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 0000000..48aa9fe --- /dev/null +++ b/interfaces/kits/native/include/crypto_kdf.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 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 param 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_ParamsType; + +/** + * @brief Create a KDF params context. + * + * @param kdfAlgoName Indicates the KDF algorithm name. + * @param params Indicates the KDF params 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_CryptoKdfParams_Create(const char *kdfAlgoName, OH_CryptoKdfParams **params); + +/** + * @brief Set a parameter to the KDF params context. + * + * @param params Indicates the parameters context. + * @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_ParamsType type, + Crypto_DataBlob *value); + +/** + * @brief Destroy the KDF params context. + * + * @param params Indicates the parameters context. + * @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 [in] The KDF instance. + * @param kdfParams [in] The KDF parameters. + * @param out [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 CryptoKdf_ParamsType *kdfParams, int keyLen, + Crypto_DataBlob *out); + +/** + * @brief Destroy a KDF instance. + * + * @param ctx [in] The KDF instance. + * @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 0000000..8c929a8 --- /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_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 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 0000000..4b3d66b --- /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_DIGEST_NAME_STR = 0, + + /** Indicates the algorithm name of the symmetric cipher function.*/ + CRYPTO_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 output 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 *output); + +/** + * @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 0000000..b389e42 --- /dev/null +++ b/interfaces/kits/native/include/crypto_rand.h @@ -0,0 +1,112 @@ +/* + * 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 value Indicates the pointer to the random number. + * @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, Crypto_DataBlob *value); + +/** + * @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 eb77a1b..b2cec9a 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,111 @@ 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); + #ifdef __cplusplus } #endif diff --git a/interfaces/kits/native/include/crypto_sym_cipher.h b/interfaces/kits/native/include/crypto_sym_cipher.h index 3e4245f..0dee8d4 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. -- Gitee From 6f5fd9eec4c04538b8dfc3d4ea52fdf2c64a1566 Mon Sep 17 00:00:00 2001 From: lanming Date: Thu, 20 Feb 2025 14:33:42 +0800 Subject: [PATCH 02/11] 12 --- interfaces/kits/native/include/crypto_asym_cipher.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/interfaces/kits/native/include/crypto_asym_cipher.h b/interfaces/kits/native/include/crypto_asym_cipher.h index 8db3888..cc26ccc 100644 --- a/interfaces/kits/native/include/crypto_asym_cipher.h +++ b/interfaces/kits/native/include/crypto_asym_cipher.h @@ -54,7 +54,7 @@ typedef struct OH_CryptoAsymCipher OH_CryptoAsymCipher; * @brief Create a asymmetric key cipher context according to the given algorithm name. * * @param algoName Indicates the algorithm name used to generate the asymmetric key cipher context. - * Example AES128|GCM|PKCS7. + * Example RSA2048|PKCS1. * @param ctx Indicates the pointer to the asymmetric key 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. @@ -71,7 +71,6 @@ OH_Crypto_ErrCode OH_CryptoAsymCipher_Create(const char *algoName, OH_CryptoAsym * @param ctx Indicates the asymmetric key cipher context. * @param mod Indicates the crypto mode is encryption or decryption. * @param key Indicates the asymmetric key. - * @param params Indicates the algorithm parameters such as IV. * @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. -- Gitee From d0712c91d89015b82def9f746e259d134f03ab34 Mon Sep 17 00:00:00 2001 From: lanming Date: Thu, 20 Feb 2025 19:17:20 +0800 Subject: [PATCH 03/11] example --- .../native/include/crypto_architecture_kit.h | 56 +++++ interfaces/kits/native/include/crypto_kdf.h | 1 + interfaces/kits/native/include/crypto_rand.h | 5 +- interfaces/kits/native/include/example.c | 196 ++++++++++++++++++ 4 files changed, 256 insertions(+), 2 deletions(-) create mode 100644 interfaces/kits/native/include/crypto_architecture_kit.h create mode 100644 interfaces/kits/native/include/example.c 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 0000000..22ee233 --- /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_kdf.h b/interfaces/kits/native/include/crypto_kdf.h index 48aa9fe..b6319b0 100644 --- a/interfaces/kits/native/include/crypto_kdf.h +++ b/interfaces/kits/native/include/crypto_kdf.h @@ -142,6 +142,7 @@ OH_Crypto_ErrCode OH_CryptoKdf_Create(const char *algoName, OH_CryptoKdf **ctx); * * @param ctx [in] The KDF instance. * @param kdfParams [in] The KDF parameters. + * @param keyLen [in] The derived key length. * @param out [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. diff --git a/interfaces/kits/native/include/crypto_rand.h b/interfaces/kits/native/include/crypto_rand.h index b389e42..f2dde56 100644 --- a/interfaces/kits/native/include/crypto_rand.h +++ b/interfaces/kits/native/include/crypto_rand.h @@ -63,7 +63,8 @@ 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 value Indicates the pointer to the random number. + * @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. @@ -71,7 +72,7 @@ OH_Crypto_ErrCode OH_CryptoRand_Create(OH_CryptoRand **ctx); * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoRand_GenerateRandom(OH_CryptoRand *ctx, Crypto_DataBlob *value); +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. diff --git a/interfaces/kits/native/include/example.c b/interfaces/kits/native/include/example.c new file mode 100644 index 0000000..8ab7ffb --- /dev/null +++ b/interfaces/kits/native/include/example.c @@ -0,0 +1,196 @@ +#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); +} + +// 随机数 +#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); + ret = OH_CryptoSign_Init(ctx, keyPair); + 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); +} + +// 密钥派生 +#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); +} + -- Gitee From c6a6a17aa2e95a5ece66f172ecf3461f39e47493 Mon Sep 17 00:00:00 2001 From: lanming Date: Thu, 20 Feb 2025 19:57:44 +0800 Subject: [PATCH 04/11] c1 --- interfaces/kits/native/include/example.c | 47 ++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/interfaces/kits/native/include/example.c b/interfaces/kits/native/include/example.c index 8ab7ffb..2d20d3a 100644 --- a/interfaces/kits/native/include/example.c +++ b/interfaces/kits/native/include/example.c @@ -37,6 +37,53 @@ int asym_cipher_test() 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 + Crypto_DataBlob out2 = {0}; + OH_CryptoSm2Cipher *sm2Cipher = NULL; + ret = OH_CryptoSm2Cipher_Create(&out, CRYPTO_SM2_CIPHER_ASN1_FORMAT, &sm2Cipher); + OH_Crypto_FreeDataBlob(&out); + + Crypto_DataBlob c1x = {0}; + Crypto_DataBlob c1y = {0}; + Crypto_DataBlob c2 = {0}; + Crypto_DataBlob c3 = {0}; + ret = OH_CryptoSm2Cipher_GetComponent(sm2Cipher, CRYPTO_SM2_CIPHER_C1_X, &c1x); + ret = OH_CryptoSm2Cipher_GetComponent(sm2Cipher, CRYPTO_SM2_CIPHER_C1_Y, &c1y); + ret = OH_CryptoSm2Cipher_GetComponent(sm2Cipher, CRYPTO_SM2_CIPHER_C2, &c2); + ret = OH_CryptoSm2Cipher_GetComponent(sm2Cipher, CRYPTO_SM2_CIPHER_C3, &c3); + OH_CryptoSm2Cipher_Destroy(sm2Cipher); + + OH_Crypto_FreeDataBlob(&c1x); + OH_Crypto_FreeDataBlob(&c1y); + OH_Crypto_FreeDataBlob(&c2); + OH_Crypto_FreeDataBlob(&c3); +} + // 随机数 #include "crypto_common.h" #include "crypto_rand.h" -- Gitee From 0d7f4023acec3f499ef3f6ec27f2c4b26b47b6b1 Mon Sep 17 00:00:00 2001 From: lanming Date: Thu, 20 Feb 2025 21:30:47 +0800 Subject: [PATCH 05/11] paramSpec --- .../kits/native/include/crypto_asym_key.h | 62 ++++++++++++++++--- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/interfaces/kits/native/include/crypto_asym_key.h b/interfaces/kits/native/include/crypto_asym_key.h index a05dfc1..600bc07 100644 --- a/interfaces/kits/native/include/crypto_asym_key.h +++ b/interfaces/kits/native/include/crypto_asym_key.h @@ -308,14 +308,14 @@ OH_Crypto_ErrCode OH_CryptoPrivKey_Encode(OH_CryptoPrivKey *key, Crypto_Encoding OH_Crypto_ErrCode OH_CryptoPrivKey_GetParam(OH_CryptoPrivKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value); /** - * @brief Define the asymmetric key spec structure. + * @brief Define the asymmetric key params spec structure. * * @since 17 */ -typedef struct OH_CryptoAsymKeySpec OH_CryptoAsymKeySpec; +typedef struct OH_CryptoAsymKeyParamsSpec OH_CryptoAsymKeyParamsSpec; /** - * @brief Define the asymmetric key spec type. + * @brief Define the asymmetric key params spec type. * * @since 17 */ @@ -328,12 +328,41 @@ typedef enum { CRYPTO_PUBLIC_KEY_SPEC = 2, /** Key pair. */ CRYPTO_KEY_PAIR_SPEC = 3, -} CryptoAsymKeySpec_Type; +} CryptoAsymKeyParamsSpec_Type; + +/** + * @brief Generate ECC parameters. + * + * @param curveName Indicates the curve name. + * @param ctx Indicates the pointer to the ECC parameters. + * @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_CryptoAsymKeyParamsSpec_GenEccParams(const char *curveName, OH_CryptoAsymKeyParamsSpec **ctx); + +/** + * @brief Generate DH parameters. + * + * @param pLen Indicates the length of the prime number. + * @param skLen Indicates the length of the secret key. + * @param ctx Indicates the pointer to the DH parameters. + * @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_CryptoAsymKeyParamsSpec_GenDhParams(int pLen, int skLen, OH_CryptoAsymKeyParamsSpec **ctx); /** * @brief Create an asymmetric key spec according to the given algorithm name. * - * @param algoName Indicates the algorithm name for generating the spec. Example RSA1024|PRIMES_2. + * @param algoName Indicates the algorithm name for generating the spec. Example RSA. * @param type Indicates the asymmetric key spec type. * @param ctx Indicates the pointer to asymmetric key spec context. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. @@ -343,8 +372,8 @@ typedef enum { * {@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 **ctx); +OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_Create(const char *algoName, CryptoAsymKeyParamsSpec_Type type, + OH_CryptoAsymKeyParamsSpec **ctx); /** * @brief Set the specified parameter to the asymmetric key spec. @@ -359,8 +388,21 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_Create(const char *algoName, CryptoAsymKe * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *keySpec, CryptoAsymKey_ParamType type, +OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_SetParam(OH_CryptoAsymKeyParamsSpec *keySpec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value); +/** + * @brief Set the common parameters to the asymmetric key spec. + * + * @param keySpec Indicates the asymmetric key spec context. + * @param commonParams Indicates the common parameters. + * @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_CryptoAsymKeyParamsSpec_SetCommonParams(OH_CryptoAsymKeyParamsSpec *keySpec, OH_CryptoAsymKeyParamsSpec *commonParams); /** * @brief Get the specified parameter from the asymmetric key spec. @@ -375,7 +417,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *keySpec, C * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec *keySpec, CryptoAsymKey_ParamType type, +OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_GetParam(OH_CryptoAsymKeyParamsSpec *keySpec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value); /** @@ -384,7 +426,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec *keySpec, C * @param keySpec Indicates the asymmetric key spec context. * @since 17 */ -void OH_CryptoAsymKeySpec_Destroy(OH_CryptoAsymKeySpec *keySpec); +void OH_CryptoAsymKeyParamsSpec_Destroy(OH_CryptoAsymKeyParamsSpec *keySpec); /** * @brief Define the asymmetric key generator by spec structure. -- Gitee From 81a8d6d1ee1fbae72379357ca49d4aed5755be0a Mon Sep 17 00:00:00 2001 From: lanming Date: Thu, 20 Feb 2025 21:40:51 +0800 Subject: [PATCH 06/11] paramSpec --- interfaces/kits/native/include/crypto_asym_key.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/native/include/crypto_asym_key.h b/interfaces/kits/native/include/crypto_asym_key.h index 600bc07..18cf519 100644 --- a/interfaces/kits/native/include/crypto_asym_key.h +++ b/interfaces/kits/native/include/crypto_asym_key.h @@ -446,7 +446,7 @@ typedef struct OH_CryptoAsymKeyGeneratorBySpec OH_CryptoAsymKeyGeneratorBySpec; * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorBySpec_Create(OH_CryptoAsymKeySpec *keySpec, OH_CryptoAsymKeyGeneratorBySpec **generator); +OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorBySpec_Create(OH_CryptoAsymKeyParamsSpec *keySpec, OH_CryptoAsymKeyGeneratorBySpec **generator); /** * @brief Generate a key pair according to the asymmetric key spec. -- Gitee From d9b6f2ac20a3208b8edba7a2d789e1614bf1874c Mon Sep 17 00:00:00 2001 From: lanming Date: Tue, 25 Feb 2025 16:26:10 +0800 Subject: [PATCH 07/11] sm2_cipher Signed-off-by: lanming --- .../kits/native/include/crypto_asym_cipher.h | 71 ++++++++++--------- .../kits/native/include/crypto_signature.h | 3 + 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/interfaces/kits/native/include/crypto_asym_cipher.h b/interfaces/kits/native/include/crypto_asym_cipher.h index cc26ccc..ea744df 100644 --- a/interfaces/kits/native/include/crypto_asym_cipher.h +++ b/interfaces/kits/native/include/crypto_asym_cipher.h @@ -101,46 +101,33 @@ OH_Crypto_ErrCode OH_CryptoAsymCipher_Final(OH_CryptoAsymCipher *ctx, const Cryp void OH_CryptoAsymCipher_Destroy(OH_CryptoAsymCipher *ctx); /** - * @brief Define the asymmetric cipher param type. + * @brief Define the sm2 cipher spec. * * @since 17 */ -typedef struct OH_CryptoSm2Cipher OH_CryptoSm2Cipher; +typedef struct OH_CryptoSm2CipherSpec OH_CryptoSm2CipherSpec; /** - * @brief Define the asymmetric cipher format type. + * @brief Define the sm2 cipher spec item type. * * @since 17 */ typedef enum { - /** SM2 cipher format C1C3C2 */ - CRYPTO_SM2_CIPHER_C1C3C2_FORMAT = 0, - /** SM2 cipher format ASN1 */ - CRYPTO_SM2_CIPHER_ASN1_FORMAT = 1, -} CryptoSm2CipherFormat; - -/** - * @brief Define the sm2 cipher component type. - * - * @since 17 - */ -typedef enum { - /** SM2 cipher public key x*/ + /** Public key x, also known as C1x. */ CRYPTO_SM2_CIPHER_C1_X = 0, - /** SM2 cipher public key y*/ + /** Public key y, also known as C1y. */ CRYPTO_SM2_CIPHER_C1_Y = 1, - /** SM2 cipher hash */ + /** Hash, also known as C2. */ CRYPTO_SM2_CIPHER_C2 = 2, - /** SM2 ciphertext data */ + /** Ciphertext data, also known as C3. */ CRYPTO_SM2_CIPHER_C3 = 3, -} CryptoSm2CipherComponent; +} CryptoSm2CipherSpecItem; /** - * @brief Create the asymmetric cipher context. + * @brief Create a sm2 cipher spec. * - * @param in Indicates the input data. - * @param format Indicates the format of the asymmetric cipher. - * @param sm2Cipher Indicates the output asymmetric cipher context. + * @param in Indicates the sm2 ciphertext in ASN1 format, if in is NULL, then an empty sm2 cipher spec will be created. + * @param sm2CipherSpec Indicates the output sm2 cipher 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. @@ -148,13 +135,13 @@ typedef enum { * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoSm2Cipher_Create(const Crypto_DataBlob *in, CryptoSm2CipherFormat format, OH_CryptoSm2Cipher **sm2Cipher); +OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_Create(Crypto_DataBlob *in, OH_CryptoSm2CipherSpec **sm2CipherSpec); /** * @brief Get the specified param of the asymmetric cipher. * - * @param sm2Cipher Indicates the asymmetric cipher context. - * @param item Indicates the asymmetric cipher param type. + * @param sm2CipherSpec Indicates the sm2 cipher spec context. + * @param item Indicates the sm2 cipher spec item 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. @@ -163,13 +150,27 @@ OH_Crypto_ErrCode OH_CryptoSm2Cipher_Create(const Crypto_DataBlob *in, CryptoSm2 * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoSm2Cipher_GetComponent(OH_CryptoSm2Cipher *sm2Cipher, CryptoSm2CipherComponent item, Crypto_DataBlob *value); +OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_GetItem(OH_CryptoSm2CipherSpec *sm2CipherSpec, CryptoSm2CipherSpecItem item, Crypto_DataBlob *value); + +/** + * @brief Set the specified param of the asymmetric cipher. + * + * @param sm2CipherSpec Indicates the sm2 cipher spec context. + * @param item Indicates the sm2 cipher spec item 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_CryptoSm2CipherSpec_SetItem(OH_CryptoSm2CipherSpec *sm2CipherSpec, CryptoSm2CipherSpecItem item, Crypto_DataBlob *value); /** - * @brief Encode the asymmetric cipher. + * @brief Encode the sm2 cipher spec to cipher text in ASN1 format. * - * @param sm2Cipher Indicates the asymmetric cipher context. - * @param format Indicates the format of the asymmetric cipher. + * @param sm2CipherSpec Indicates the sm2 cipher spec context. * @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. @@ -178,15 +179,15 @@ OH_Crypto_ErrCode OH_CryptoSm2Cipher_GetComponent(OH_CryptoSm2Cipher *sm2Cipher, * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoSm2Cipher_Encode(OH_CryptoSm2Cipher *sm2Cipher, CryptoSm2CipherFormat format, Crypto_DataBlob *out); +OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_Encode(OH_CryptoSm2CipherSpec *sm2CipherSpec, Crypto_DataBlob *out); /** - * @brief Destroy the sm2 cipher context. + * @brief Destroy the sm2 cipher spec. * - * @param sm2Cipher Indicates the sm2 cipher context. + * @param sm2CipherSpec Indicates the sm2 cipher spec context. * @since 17 */ -void OH_CryptoSm2Cipher_Destroy(OH_CryptoSm2Cipher *sm2Cipher); +void OH_CryptoSm2CipherSpec_Destroy(OH_CryptoSm2CipherSpec *sm2CipherSpec); #ifdef __cplusplus } diff --git a/interfaces/kits/native/include/crypto_signature.h b/interfaces/kits/native/include/crypto_signature.h index b2cec9a..df68183 100644 --- a/interfaces/kits/native/include/crypto_signature.h +++ b/interfaces/kits/native/include/crypto_signature.h @@ -307,6 +307,9 @@ OH_Crypto_ErrCode OH_CryptoSign_GetParam(OH_CryptoSign *ctx, CryptoSignature_Par */ void OH_CryptoSign_Destroy(OH_CryptoSign *ctx); + +OH_Crypto_ErrCode OH_CryptoSignature_ConvertSignatureDataFormat(const Crypto_DataBlob *in, Crypto_DataBlob *out); + #ifdef __cplusplus } #endif -- Gitee From c4163a48ffcf4611026db73063b042b9f0e5b269 Mon Sep 17 00:00:00 2001 From: lanming Date: Tue, 25 Feb 2025 16:33:16 +0800 Subject: [PATCH 08/11] sm2_cipher example Signed-off-by: lanming --- interfaces/kits/native/include/example.c | 27 +++++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/interfaces/kits/native/include/example.c b/interfaces/kits/native/include/example.c index 2d20d3a..8260207 100644 --- a/interfaces/kits/native/include/example.c +++ b/interfaces/kits/native/include/example.c @@ -63,25 +63,36 @@ int asym_cipher_test() cipher = NULL; // 3. 获取C1、C2和C3 - Crypto_DataBlob out2 = {0}; - OH_CryptoSm2Cipher *sm2Cipher = NULL; - ret = OH_CryptoSm2Cipher_Create(&out, CRYPTO_SM2_CIPHER_ASN1_FORMAT, &sm2Cipher); + OH_CryptoSm2CipherSpec *sm2CipherSpec = NULL; + ret = OH_CryptoSm2CipherSpec_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_CryptoSm2Cipher_GetComponent(sm2Cipher, CRYPTO_SM2_CIPHER_C1_X, &c1x); - ret = OH_CryptoSm2Cipher_GetComponent(sm2Cipher, CRYPTO_SM2_CIPHER_C1_Y, &c1y); - ret = OH_CryptoSm2Cipher_GetComponent(sm2Cipher, CRYPTO_SM2_CIPHER_C2, &c2); - ret = OH_CryptoSm2Cipher_GetComponent(sm2Cipher, CRYPTO_SM2_CIPHER_C3, &c3); - OH_CryptoSm2Cipher_Destroy(sm2Cipher); + ret = OH_CryptoSm2CipherSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C1_X, &c1x); + ret = OH_CryptoSm2CipherSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C1_Y, &c1y); + ret = OH_CryptoSm2CipherSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C2, &c2); + ret = OH_CryptoSm2CipherSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C3, &c3); + OH_CryptoSm2CipherSpec_Destroy(sm2CipherSpec); + sm2CipherSpec = NULL; + + // 4. 创建ASN1格式的sm2密文 + ret = OH_CryptoSm2CipherSpec_Create(NULL, &sm2CipherSpec); + ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C1_X, &c1x); + ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C1_Y, &c1y); + ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C2, &c2); + ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_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); } // 随机数 -- Gitee From d15dfb822237a239815af45bb62e064921bf5420 Mon Sep 17 00:00:00 2001 From: lanming Date: Tue, 25 Feb 2025 17:00:16 +0800 Subject: [PATCH 09/11] sm2 signature data example Signed-off-by: lanming --- .../kits/native/include/crypto_asym_cipher.h | 8 +- .../kits/native/include/crypto_signature.h | 73 ++++++++++++++++++- interfaces/kits/native/include/example.c | 54 +++++++++++++- 3 files changed, 129 insertions(+), 6 deletions(-) diff --git a/interfaces/kits/native/include/crypto_asym_cipher.h b/interfaces/kits/native/include/crypto_asym_cipher.h index ea744df..8107d6b 100644 --- a/interfaces/kits/native/include/crypto_asym_cipher.h +++ b/interfaces/kits/native/include/crypto_asym_cipher.h @@ -142,7 +142,7 @@ OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_Create(Crypto_DataBlob *in, OH_CryptoSm * * @param sm2CipherSpec Indicates the sm2 cipher spec context. * @param item Indicates the sm2 cipher spec item type. - * @param value Indicates the output 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. @@ -150,14 +150,14 @@ OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_Create(Crypto_DataBlob *in, OH_CryptoSm * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_GetItem(OH_CryptoSm2CipherSpec *sm2CipherSpec, CryptoSm2CipherSpecItem item, Crypto_DataBlob *value); +OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_GetItem(OH_CryptoSm2CipherSpec *sm2CipherSpec, CryptoSm2CipherSpecItem item, Crypto_DataBlob *out); /** * @brief Set the specified param of the asymmetric cipher. * * @param sm2CipherSpec Indicates the sm2 cipher spec context. * @param item Indicates the sm2 cipher spec item type. - * @param value Indicates the input data. + * @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. @@ -165,7 +165,7 @@ OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_GetItem(OH_CryptoSm2CipherSpec *sm2Ciph * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_SetItem(OH_CryptoSm2CipherSpec *sm2CipherSpec, CryptoSm2CipherSpecItem item, Crypto_DataBlob *value); +OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_SetItem(OH_CryptoSm2CipherSpec *sm2CipherSpec, CryptoSm2CipherSpecItem item, Crypto_DataBlob *in); /** * @brief Encode the sm2 cipher spec to cipher text in ASN1 format. diff --git a/interfaces/kits/native/include/crypto_signature.h b/interfaces/kits/native/include/crypto_signature.h index df68183..b4fd18f 100644 --- a/interfaces/kits/native/include/crypto_signature.h +++ b/interfaces/kits/native/include/crypto_signature.h @@ -308,7 +308,78 @@ OH_Crypto_ErrCode OH_CryptoSign_GetParam(OH_CryptoSign *ctx, CryptoSignature_Par void OH_CryptoSign_Destroy(OH_CryptoSign *ctx); -OH_Crypto_ErrCode OH_CryptoSignature_ConvertSignatureDataFormat(const Crypto_DataBlob *in, Crypto_DataBlob *out); +/** + * @brief Define the ECC or SM2 signature data spec. + * + * @since 17 + */ +typedef struct OH_CryptoEccSignatureDataSpec OH_CryptoEccSignatureDataSpec; + +/** + * @brief Create the ECC or SM2 signature data spec. + * + * @param in Indicates the input data. + * @param eccSignatureDataSpec Indicates the output ECC or SM2 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 or SM2 signature data spec. + * + * @param eccSignatureDataSpec Indicates the ECC or SM2 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 or SM2 signature data spec. + * + * @param eccSignatureDataSpec Indicates the ECC or SM2 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 or SM2 signature data spec to signature data in ASN1 format. + * + * @param eccSignatureDataSpec Indicates the ECC or SM2 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 or SM2 signature data spec. + * + * @param eccSignatureDataSpec Indicates the ECC or SM2 signature data spec. + * @since 17 + */ +OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_Destroy(OH_CryptoEccSignatureDataSpec *eccSignatureDataSpec); #ifdef __cplusplus } diff --git a/interfaces/kits/native/include/example.c b/interfaces/kits/native/include/example.c index 8260207..3aaaa0c 100644 --- a/interfaces/kits/native/include/example.c +++ b/interfaces/kits/native/include/example.c @@ -199,7 +199,9 @@ int signature_test() // 2. 签名 OH_CryptoSign *ctx = NULL; ret = OH_CryptoSign_Create("RSA3072|PKCS1|SHA384", &ctx); - ret = OH_CryptoSign_Init(ctx, keyPair); + 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"), @@ -212,6 +214,56 @@ int signature_test() 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" -- Gitee From 63f63a8f365bba338f68f7f56b53f3a39fac455f Mon Sep 17 00:00:00 2001 From: lanming Date: Tue, 25 Feb 2025 17:24:59 +0800 Subject: [PATCH 10/11] sm2 signature data example Signed-off-by: lanming --- .../kits/native/include/crypto_signature.h | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/interfaces/kits/native/include/crypto_signature.h b/interfaces/kits/native/include/crypto_signature.h index b4fd18f..982fbbc 100644 --- a/interfaces/kits/native/include/crypto_signature.h +++ b/interfaces/kits/native/include/crypto_signature.h @@ -309,17 +309,17 @@ void OH_CryptoSign_Destroy(OH_CryptoSign *ctx); /** - * @brief Define the ECC or SM2 signature data spec. + * @brief Define the ECC signature data spec. * * @since 17 */ typedef struct OH_CryptoEccSignatureDataSpec OH_CryptoEccSignatureDataSpec; /** - * @brief Create the ECC or SM2 signature data spec. + * @brief Create the ECC signature data spec, alse support SM2 signature. * - * @param in Indicates the input data. - * @param eccSignatureDataSpec Indicates the output ECC or SM2 signature data spec. + * @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. @@ -330,11 +330,11 @@ typedef struct OH_CryptoEccSignatureDataSpec OH_CryptoEccSignatureDataSpec; OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_Create(Crypto_DataBlob *in, OH_CryptoEccSignatureDataSpec **eccSignatureDataSpec); /** - * @brief Get the r and s value from the ECC or SM2 signature data spec. + * @brief Get the r and s value from the ECC signature data spec. * - * @param eccSignatureDataSpec Indicates the ECC or SM2 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. + * @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. @@ -345,9 +345,9 @@ OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_Create(Crypto_DataBlob *in, OH_C 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 or SM2 signature data spec. + * @brief Set the r and s value to the ECC signature data spec. * - * @param eccSignatureDataSpec Indicates the ECC or SM2 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. @@ -360,9 +360,9 @@ OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_GetRAndS(OH_CryptoEccSignatureDa OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_SetRAndS(OH_CryptoEccSignatureDataSpec *eccSignatureDataSpec, Crypto_DataBlob *r, Crypto_DataBlob *s); /** - * @brief Encode the ECC or SM2 signature data spec to signature data in ASN1 format. + * @brief Encode the ECC signature data spec to signature data in ASN1 format. * - * @param eccSignatureDataSpec Indicates the ECC or SM2 signature data spec. + * @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. @@ -374,9 +374,9 @@ OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_SetRAndS(OH_CryptoEccSignatureDa OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_Encode(OH_CryptoEccSignatureDataSpec *eccSignatureDataSpec, Crypto_DataBlob *out); /** - * @brief Destroy the ECC or SM2 signature data spec. + * @brief Destroy the ECC signature data spec. * - * @param eccSignatureDataSpec Indicates the ECC or SM2 signature data spec. + * @param eccSignatureDataSpec Indicates the ECC signature data spec. * @since 17 */ OH_Crypto_ErrCode OH_CryptoEccSignatureDataSpec_Destroy(OH_CryptoEccSignatureDataSpec *eccSignatureDataSpec); -- Gitee From 4733f36710f1514683b7e9b059ed28fe7fdc66f2 Mon Sep 17 00:00:00 2001 From: lanming Date: Wed, 26 Feb 2025 17:38:38 +0800 Subject: [PATCH 11/11] review 1 Signed-off-by: lanming --- .../kits/native/include/crypto_asym_cipher.h | 73 ++++---- .../kits/native/include/crypto_asym_key.h | 172 ++++++++++++------ interfaces/kits/native/include/crypto_kdf.h | 36 ++-- .../native/include/crypto_key_agreement.h | 4 +- interfaces/kits/native/include/crypto_mac.h | 8 +- interfaces/kits/native/include/example.c | 20 +- 6 files changed, 190 insertions(+), 123 deletions(-) diff --git a/interfaces/kits/native/include/crypto_asym_cipher.h b/interfaces/kits/native/include/crypto_asym_cipher.h index 8107d6b..2dd6585 100644 --- a/interfaces/kits/native/include/crypto_asym_cipher.h +++ b/interfaces/kits/native/include/crypto_asym_cipher.h @@ -44,31 +44,30 @@ extern "C" { #endif /** - * @brief Define the asymmetric cipher param type. + * @brief Define the asymmetric cipher structure. * * @since 17 */ typedef struct OH_CryptoAsymCipher OH_CryptoAsymCipher; /** - * @brief Create a asymmetric key cipher context according to the given algorithm name. + * @brief Create a asymmetric cipher context according to the given algorithm name. * - * @param algoName Indicates the algorithm name used to generate the asymmetric key cipher context. - * Example RSA2048|PKCS1. - * @param ctx Indicates the pointer to the asymmetric key cipher context. + * @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 12 + * @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 key cipher context. + * @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. @@ -82,7 +81,7 @@ OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_Ciph /** * @brief Final the crypto operation. * - * @param ctx Indicates the asymmetric key cipher context. + * @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. @@ -94,40 +93,40 @@ OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_Ciph OH_Crypto_ErrCode OH_CryptoAsymCipher_Final(OH_CryptoAsymCipher *ctx, const Crypto_DataBlob *in, Crypto_DataBlob *out); /** - * @brief Destroy the asymmetric key cipher context. + * @brief Destroy the asymmetric cipher context. * - * @param ctx Indicates the asymmetric key cipher context. + * @param ctx Indicates the asymmetric cipher context. */ void OH_CryptoAsymCipher_Destroy(OH_CryptoAsymCipher *ctx); /** - * @brief Define the sm2 cipher spec. + * @brief Define the sm2 ciphertext spec structure. * * @since 17 */ -typedef struct OH_CryptoSm2CipherSpec OH_CryptoSm2CipherSpec; +typedef struct OH_CryptoSm2CiphertextSpec OH_CryptoSm2CiphertextSpec; /** - * @brief Define the sm2 cipher spec item type. + * @brief Define the sm2 ciphertext spec item type. * * @since 17 */ typedef enum { /** Public key x, also known as C1x. */ - CRYPTO_SM2_CIPHER_C1_X = 0, + CRYPTO_SM2_CIPHERTEXT_C1_X = 0, /** Public key y, also known as C1y. */ - CRYPTO_SM2_CIPHER_C1_Y = 1, + CRYPTO_SM2_CIPHERTEXT_C1_Y = 1, /** Hash, also known as C2. */ - CRYPTO_SM2_CIPHER_C2 = 2, + CRYPTO_SM2_CIPHERTEXT_C2 = 2, /** Ciphertext data, also known as C3. */ - CRYPTO_SM2_CIPHER_C3 = 3, -} CryptoSm2CipherSpecItem; + CRYPTO_SM2_CIPHERTEXT_C3 = 3, +} CryptoSm2CiphertextSpec_item; /** - * @brief Create a sm2 cipher spec. + * @brief Create a sm2 ciphertext spec. * - * @param in Indicates the sm2 ciphertext in ASN1 format, if in is NULL, then an empty sm2 cipher spec will be created. - * @param sm2CipherSpec Indicates the output sm2 cipher 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. @@ -135,13 +134,13 @@ typedef enum { * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_Create(Crypto_DataBlob *in, OH_CryptoSm2CipherSpec **sm2CipherSpec); +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_Create(Crypto_DataBlob *in, OH_CryptoSm2CiphertextSpec **spec); /** - * @brief Get the specified param of the asymmetric cipher. + * @brief Get the specified item of the sm2 ciphertext. * - * @param sm2CipherSpec Indicates the sm2 cipher spec context. - * @param item Indicates the sm2 cipher spec item type. + * @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. @@ -150,13 +149,14 @@ OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_Create(Crypto_DataBlob *in, OH_CryptoSm * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_GetItem(OH_CryptoSm2CipherSpec *sm2CipherSpec, CryptoSm2CipherSpecItem item, Crypto_DataBlob *out); +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_GetItem(OH_CryptoSm2CiphertextSpec *spec, + CryptoSm2CiphertextSpec_item item, Crypto_DataBlob *out); /** - * @brief Set the specified param of the asymmetric cipher. + * @brief Set the specified item to the sm2 ciphertext spec. * - * @param sm2CipherSpec Indicates the sm2 cipher spec context. - * @param item Indicates the sm2 cipher spec item type. + * @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. @@ -165,12 +165,13 @@ OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_GetItem(OH_CryptoSm2CipherSpec *sm2Ciph * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_SetItem(OH_CryptoSm2CipherSpec *sm2CipherSpec, CryptoSm2CipherSpecItem item, Crypto_DataBlob *in); +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_SetItem(OH_CryptoSm2CiphertextSpec *spec, + CryptoSm2CiphertextSpec_item item, Crypto_DataBlob *in); /** - * @brief Encode the sm2 cipher spec to cipher text in ASN1 format. + * @brief Encode the sm2 ciphertext spec to ciphertext in ASN1 format. * - * @param sm2CipherSpec Indicates the sm2 cipher spec context. + * @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. @@ -179,15 +180,15 @@ OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_SetItem(OH_CryptoSm2CipherSpec *sm2Ciph * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoSm2CipherSpec_Encode(OH_CryptoSm2CipherSpec *sm2CipherSpec, Crypto_DataBlob *out); +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_Encode(OH_CryptoSm2CiphertextSpec *spec, Crypto_DataBlob *out); /** - * @brief Destroy the sm2 cipher spec. + * @brief Destroy the sm2 ciphertext spec. * - * @param sm2CipherSpec Indicates the sm2 cipher spec context. + * @param spec Indicates the sm2 ciphertext spec context. * @since 17 */ -void OH_CryptoSm2CipherSpec_Destroy(OH_CryptoSm2CipherSpec *sm2CipherSpec); +void OH_CryptoSm2CiphertextSpec_Destroy(OH_CryptoSm2CiphertextSpec *spec); #ifdef __cplusplus } diff --git a/interfaces/kits/native/include/crypto_asym_key.h b/interfaces/kits/native/include/crypto_asym_key.h index 18cf519..c303eab 100644 --- a/interfaces/kits/native/include/crypto_asym_key.h +++ b/interfaces/kits/native/include/crypto_asym_key.h @@ -275,22 +275,84 @@ 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 12 + * @since 17 */ OH_Crypto_ErrCode OH_CryptoPrivKey_Encode(OH_CryptoPrivKey *key, Crypto_EncodingType type, - const char *encodingStandard, Crypto_DataBlob *out); + const char *encodingStandard, OH_CryptoPrivKeyEncodingParams *params, Crypto_DataBlob *out); /** * @brief Get the specified param of the private key. @@ -303,38 +365,38 @@ OH_Crypto_ErrCode OH_CryptoPrivKey_Encode(OH_CryptoPrivKey *key, Crypto_Encoding * {@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 + * @since 17 */ OH_Crypto_ErrCode OH_CryptoPrivKey_GetParam(OH_CryptoPrivKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value); /** - * @brief Define the asymmetric key params spec structure. + * @brief Define the asymmetric key spec structure. * * @since 17 */ -typedef struct OH_CryptoAsymKeyParamsSpec OH_CryptoAsymKeyParamsSpec; +typedef struct OH_CryptoAsymKeySpec OH_CryptoAsymKeySpec; /** - * @brief Define the asymmetric key params spec type. + * @brief Define the asymmetric key spec type. * * @since 17 */ typedef enum { - /** Common parameters. */ - CRYPTO_COMMON_PARAMS_SPEC = 0, - /** Private key. */ - CRYPTO_PRIVATE_KEY_SPEC = 1, - /** Public key. */ - CRYPTO_PUBLIC_KEY_SPEC = 2, - /** Key pair. */ - CRYPTO_KEY_PAIR_SPEC = 3, -} CryptoAsymKeyParamsSpec_Type; - -/** - * @brief Generate ECC parameters. - * - * @param curveName Indicates the curve name. - * @param ctx Indicates the pointer to the ECC parameters. + /** 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. @@ -342,14 +404,14 @@ typedef enum { * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_GenEccParams(const char *curveName, OH_CryptoAsymKeyParamsSpec **ctx); +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenEccCommonParamsSpec(const char *curveName, OH_CryptoAsymKeySpec **spec); /** - * @brief Generate DH parameters. + * @brief Generate DH common parameters spec. * - * @param pLen Indicates the length of the prime number. - * @param skLen Indicates the length of the secret key. - * @param ctx Indicates the pointer to the DH parameters. + * @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. @@ -357,14 +419,14 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_GenEccParams(const char *curveName, * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_GenDhParams(int pLen, int skLen, OH_CryptoAsymKeyParamsSpec **ctx); +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. + * @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 ctx Indicates the pointer to asymmetric key spec context. + * @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. @@ -372,14 +434,14 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_GenDhParams(int pLen, int skLen, OH * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_Create(const char *algoName, CryptoAsymKeyParamsSpec_Type type, - OH_CryptoAsymKeyParamsSpec **ctx); +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 keySpec Indicates the asymmetric key spec context. - * @param type Indicates the asymmetric key parameter type. + * @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. @@ -388,13 +450,14 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_Create(const char *algoName, Crypto * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_SetParam(OH_CryptoAsymKeyParamsSpec *keySpec, CryptoAsymKey_ParamType type, +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value); + /** - * @brief Set the common parameters to the asymmetric key spec. + * @brief Set the common parameters spec to the asymmetric key spec. * - * @param keySpec Indicates the asymmetric key spec context. - * @param commonParams Indicates the common parameters. + * @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. @@ -402,13 +465,14 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_SetParam(OH_CryptoAsymKeyParamsSpec * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_SetCommonParams(OH_CryptoAsymKeyParamsSpec *keySpec, OH_CryptoAsymKeyParamsSpec *commonParams); +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec *spec, + OH_CryptoAsymKeySpec *commonParamsSpec); /** * @brief Get the specified parameter from the asymmetric key spec. * - * @param keySpec Indicates the asymmetric key spec context. - * @param type Indicates the asymmetric key parameter type. + * @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. @@ -417,28 +481,29 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_SetCommonParams(OH_CryptoAsymKeyPar * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeyParamsSpec_GetParam(OH_CryptoAsymKeyParamsSpec *keySpec, CryptoAsymKey_ParamType type, +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value); /** * @brief Destroy the asymmetric key spec. * - * @param keySpec Indicates the asymmetric key spec context. + * @param spec Indicates the asymmetric key spec. * @since 17 */ -void OH_CryptoAsymKeyParamsSpec_Destroy(OH_CryptoAsymKeyParamsSpec *keySpec); +void OH_CryptoAsymKeySpec_Destroy(OH_CryptoAsymKeySpec *spec); /** - * @brief Define the asymmetric key generator by spec structure. + * @brief Define the asymmetric key generator with spec. * * @since 17 */ -typedef struct OH_CryptoAsymKeyGeneratorBySpec OH_CryptoAsymKeyGeneratorBySpec; +typedef struct OH_CryptoAsymKeyGeneratorWithSpec OH_CryptoAsymKeyGeneratorWithSpec; /** - * @brief Create an asymmetric key generator by spec. + * @brief Create an asymmetric key generator with spec. * - * @param generator Indicates the pointer to asymmetric key generator by spec context. + * @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. @@ -446,12 +511,13 @@ typedef struct OH_CryptoAsymKeyGeneratorBySpec OH_CryptoAsymKeyGeneratorBySpec; * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorBySpec_Create(OH_CryptoAsymKeyParamsSpec *keySpec, OH_CryptoAsymKeyGeneratorBySpec **generator); +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 by spec context. + * @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. @@ -460,16 +526,16 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorBySpec_Create(OH_CryptoAsymKeyParamsS * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorBySpec_Generate(OH_CryptoAsymKeyGeneratorBySpec *generator, +OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorWithSpec_GenKeyPair(OH_CryptoAsymKeyGeneratorWithSpec *generator, OH_CryptoKeyPair **keyPair); /** - * @brief Destroy the asymmetric key generator by spec. + * @brief Destroy the asymmetric key generator with spec. * - * @param generator Indicates the asymmetric key generator by spec context. + * @param generator Indicates the asymmetric key generator with spec. * @since 17 */ -void OH_CryptoAsymKeyGeneratorBySpec_Destroy(OH_CryptoAsymKeyGeneratorBySpec *generator); +void OH_CryptoAsymKeyGeneratorWithSpec_Destroy(OH_CryptoAsymKeyGeneratorWithSpec *generator); #ifdef __cplusplus } diff --git a/interfaces/kits/native/include/crypto_kdf.h b/interfaces/kits/native/include/crypto_kdf.h index b6319b0..d4719a7 100644 --- a/interfaces/kits/native/include/crypto_kdf.h +++ b/interfaces/kits/native/include/crypto_kdf.h @@ -50,7 +50,7 @@ extern "C" { typedef struct OH_CryptoKdf OH_CryptoKdf; /** - * @brief Define the KDF param structure. + * @brief Define the KDF params structure. * * @since 17 */ @@ -85,25 +85,25 @@ typedef enum { /** Indicates the maxMem for SCRYPT KDF. */ CRYPTO_KDF_SCRYPT_MAXMEM_UINT64 = 106, -} CryptoKdf_ParamsType; +} CryptoKdf_ParamType; /** - * @brief Create a KDF params context. + * @brief Create a KDF params. * - * @param kdfAlgoName Indicates the KDF algorithm name. - * @param params Indicates the KDF params context. + * @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 *kdfAlgoName, OH_CryptoKdfParams **params); +OH_Crypto_ErrCode OH_CryptoKdfParams_Create(const char *algoName, OH_CryptoKdfParams **params); /** - * @brief Set a parameter to the KDF params context. + * @brief Set a parameter to the KDF parameters. * - * @param params Indicates the parameters context. + * @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. @@ -113,13 +113,13 @@ OH_Crypto_ErrCode OH_CryptoKdfParams_Create(const char *kdfAlgoName, OH_CryptoKd * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoKdfParams_SetParam(OH_CryptoKdfParams *params, CryptoKdf_ParamsType type, +OH_Crypto_ErrCode OH_CryptoKdfParams_SetParam(OH_CryptoKdfParams *params, CryptoKdf_ParamType type, Crypto_DataBlob *value); /** - * @brief Destroy the KDF params context. + * @brief Destroy the KDF params. * - * @param params Indicates the parameters context. + * @param params Indicates the parameters. * @since 17 */ void OH_CryptoKdfParams_Destroy(OH_CryptoKdfParams *params); @@ -140,10 +140,10 @@ OH_Crypto_ErrCode OH_CryptoKdf_Create(const char *algoName, OH_CryptoKdf **ctx); /** * @brief Derive a key. * - * @param ctx [in] The KDF instance. - * @param kdfParams [in] The KDF parameters. - * @param keyLen [in] The derived key length. - * @param out [out] The derived 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. @@ -151,13 +151,13 @@ OH_Crypto_ErrCode OH_CryptoKdf_Create(const char *algoName, OH_CryptoKdf **ctx); * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 17 */ -OH_Crypto_ErrCode OH_CryptoKdf_Derive(OH_CryptoKdf *ctx, const CryptoKdf_ParamsType *kdfParams, int keyLen, +OH_Crypto_ErrCode OH_CryptoKdf_Derive(OH_CryptoKdf *ctx, const OH_CryptoKdfParams *kdfParams, int keyLen, Crypto_DataBlob *out); /** - * @brief Destroy a KDF instance. + * @brief Destroy the KDF. * - * @param ctx [in] The KDF instance. + * @param ctx The KDF. * @since 17 */ void OH_CryptoKdf_Destroy(OH_CryptoKdf *ctx); diff --git a/interfaces/kits/native/include/crypto_key_agreement.h b/interfaces/kits/native/include/crypto_key_agreement.h index 8c929a8..9738e0d 100644 --- a/interfaces/kits/native/include/crypto_key_agreement.h +++ b/interfaces/kits/native/include/crypto_key_agreement.h @@ -33,8 +33,8 @@ * @since 17 */ -#ifndef CRYPTO_ASYM_CIPHER_H -#define CRYPTO_ASYM_CIPHER_H +#ifndef CRYPTO_KEY_AGREEMENT_H +#define CRYPTO_KEY_AGREEMENT_H #include "crypto_common.h" #include "crypto_asym_key.h" diff --git a/interfaces/kits/native/include/crypto_mac.h b/interfaces/kits/native/include/crypto_mac.h index 4b3d66b..45a7106 100644 --- a/interfaces/kits/native/include/crypto_mac.h +++ b/interfaces/kits/native/include/crypto_mac.h @@ -49,10 +49,10 @@ extern "C" { */ typedef enum { /** Indicates the algorithm name of the message digest function.*/ - CRYPTO_DIGEST_NAME_STR = 0, + CRYPTO_MAC_DIDEST_NAME_STR = 0, /** Indicates the algorithm name of the symmetric cipher function.*/ - CRYPTO_CIPHER_NAME_STR = 1, + CRYPTO_MAC_CIPHER_NAME_STR = 1, } CryptoMac_ParamType; /** @@ -124,7 +124,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob * * @brief Finalize mac with dataBlob. * * @param ctx Indicates the mac context. - * @param output Indicates the result as dataBlob. + * @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. @@ -133,7 +133,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob * * @see OH_CryptoMac_Update * @since 17 */ -OH_Crypto_ErrCode OH_CryptoMac_Final(OH_CryptoMac *ctx, Crypto_DataBlob *output); +OH_Crypto_ErrCode OH_CryptoMac_Final(OH_CryptoMac *ctx, Crypto_DataBlob *out); /** * @brief Get the mac length of the mac context. diff --git a/interfaces/kits/native/include/example.c b/interfaces/kits/native/include/example.c index 3aaaa0c..895a641 100644 --- a/interfaces/kits/native/include/example.c +++ b/interfaces/kits/native/include/example.c @@ -63,27 +63,27 @@ int asym_cipher_test() cipher = NULL; // 3. 获取C1、C2和C3 - OH_CryptoSm2CipherSpec *sm2CipherSpec = NULL; - ret = OH_CryptoSm2CipherSpec_Create(&out, &sm2CipherSpec); + 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_CryptoSm2CipherSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C1_X, &c1x); - ret = OH_CryptoSm2CipherSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C1_Y, &c1y); - ret = OH_CryptoSm2CipherSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C2, &c2); - ret = OH_CryptoSm2CipherSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C3, &c3); + 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_CIPHER_C1_X, &c1x); - ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C1_Y, &c1y); - ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C2, &c2); - ret = OH_CryptoSm2CipherSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHER_C3, &c3); + 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); -- Gitee