From 6b5c39a83c6f7a75b0d20543dbe9b8b2c432e3a1 Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Tue, 13 May 2025 09:11:08 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- .../kits/native/include/crypto_asym_cipher.h | 14 ++++---- .../kits/native/include/crypto_asym_key.h | 32 +++++++++---------- interfaces/kits/native/include/crypto_kdf.h | 8 ++--- .../native/include/crypto_key_agreement.h | 4 +-- interfaces/kits/native/include/crypto_mac.h | 12 +++---- interfaces/kits/native/include/crypto_rand.h | 6 ++-- .../kits/native/include/crypto_signature.h | 20 ++++++------ 7 files changed, 48 insertions(+), 48 deletions(-) diff --git a/interfaces/kits/native/include/crypto_asym_cipher.h b/interfaces/kits/native/include/crypto_asym_cipher.h index d28e5c9..d6ab307 100644 --- a/interfaces/kits/native/include/crypto_asym_cipher.h +++ b/interfaces/kits/native/include/crypto_asym_cipher.h @@ -59,7 +59,7 @@ typedef struct OH_CryptoAsymCipher OH_CryptoAsymCipher; * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -74,7 +74,7 @@ OH_Crypto_ErrCode OH_CryptoAsymCipher_Create(const char *algoName, OH_CryptoAsym * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoAsymCipher_Final * @since 20 @@ -90,7 +90,7 @@ OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_Ciph * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoAsymCipher_Init * @since 20 @@ -137,7 +137,7 @@ typedef enum { * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -152,7 +152,7 @@ OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_Create(Crypto_DataBlob *sm2Cipherte * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -168,7 +168,7 @@ OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_GetItem(OH_CryptoSm2CiphertextSpec * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -183,7 +183,7 @@ OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_SetItem(OH_CryptoSm2CiphertextSpec * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_asym_key.h b/interfaces/kits/native/include/crypto_asym_key.h index 21ec9d8..7f09d55 100644 --- a/interfaces/kits/native/include/crypto_asym_key.h +++ b/interfaces/kits/native/include/crypto_asym_key.h @@ -302,7 +302,7 @@ typedef enum { * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -317,7 +317,7 @@ OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_Create(OH_CryptoPrivKeyEncoding * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -344,7 +344,7 @@ void OH_CryptoPrivKeyEncodingParams_Destroy(OH_CryptoPrivKeyEncodingParams *ctx) * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -360,7 +360,7 @@ OH_Crypto_ErrCode OH_CryptoPrivKey_Encode(OH_CryptoPrivKey *key, Crypto_Encoding * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -398,7 +398,7 @@ typedef enum { * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -413,7 +413,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenEcCommonParamsSpec(const char *curveNa * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -428,7 +428,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenDhCommonParamsSpec(int pLen, int skLen * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -444,7 +444,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_Create(const char *algoName, CryptoAsymKe * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -459,7 +459,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, Cryp * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -475,7 +475,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -505,7 +505,7 @@ typedef struct OH_CryptoAsymKeyGeneratorWithSpec OH_CryptoAsymKeyGeneratorWithSp * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -520,7 +520,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorWithSpec_Create(OH_CryptoAsymKeySpec * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -552,7 +552,7 @@ typedef struct OH_CryptoEcPoint OH_CryptoEcPoint; * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -567,7 +567,7 @@ OH_Crypto_ErrCode OH_CryptoEcPoint_Create(const char *curveName, Crypto_DataBlob * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -582,7 +582,7 @@ OH_Crypto_ErrCode OH_CryptoEcPoint_GetCoordinate(OH_CryptoEcPoint *point, Crypto * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -597,7 +597,7 @@ OH_Crypto_ErrCode OH_CryptoEcPoint_SetCoordinate(OH_CryptoEcPoint *point, Crypto * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_kdf.h b/interfaces/kits/native/include/crypto_kdf.h index 46dd56b..24ae125 100644 --- a/interfaces/kits/native/include/crypto_kdf.h +++ b/interfaces/kits/native/include/crypto_kdf.h @@ -95,7 +95,7 @@ typedef enum { * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -110,7 +110,7 @@ OH_Crypto_ErrCode OH_CryptoKdfParams_Create(const char *algoName, OH_CryptoKdfPa * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -133,7 +133,7 @@ void OH_CryptoKdfParams_Destroy(OH_CryptoKdfParams *params); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -149,7 +149,7 @@ OH_Crypto_ErrCode OH_CryptoKdf_Create(const char *algoName, OH_CryptoKdf **ctx); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_key_agreement.h b/interfaces/kits/native/include/crypto_key_agreement.h index cba6555..2b011c4 100644 --- a/interfaces/kits/native/include/crypto_key_agreement.h +++ b/interfaces/kits/native/include/crypto_key_agreement.h @@ -58,7 +58,7 @@ typedef struct OH_CryptoKeyAgreement OH_CryptoKeyAgreement; * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -74,7 +74,7 @@ OH_Crypto_ErrCode OH_CryptoKeyAgreement_Create(const char *algoName, OH_CryptoKe * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_mac.h b/interfaces/kits/native/include/crypto_mac.h index 214d07f..24d3c99 100644 --- a/interfaces/kits/native/include/crypto_mac.h +++ b/interfaces/kits/native/include/crypto_mac.h @@ -71,7 +71,7 @@ typedef enum { * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -86,7 +86,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -100,7 +100,7 @@ OH_Crypto_ErrCode OH_CryptoMac_SetParam(OH_CryptoMac *ctx, CryptoMac_ParamType t * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoMac_Update * @see OH_CryptoMac_Final @@ -116,7 +116,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *ke * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoMac_Init * @see OH_CryptoMac_Final @@ -132,7 +132,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob * * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoMac_Init * @see OH_CryptoMac_Update @@ -148,7 +148,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Final(OH_CryptoMac *ctx, Crypto_DataBlob *out); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_rand.h b/interfaces/kits/native/include/crypto_rand.h index ff1a469..1e9a4f8 100644 --- a/interfaces/kits/native/include/crypto_rand.h +++ b/interfaces/kits/native/include/crypto_rand.h @@ -54,7 +54,7 @@ typedef struct OH_CryptoRand OH_CryptoRand; * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -69,7 +69,7 @@ OH_Crypto_ErrCode OH_CryptoRand_Create(OH_CryptoRand **ctx); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -92,7 +92,7 @@ const char *OH_CryptoRand_GetAlgoName(OH_CryptoRand *ctx); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_signature.h b/interfaces/kits/native/include/crypto_signature.h index 792823b..7982b27 100644 --- a/interfaces/kits/native/include/crypto_signature.h +++ b/interfaces/kits/native/include/crypto_signature.h @@ -210,7 +210,7 @@ void OH_CryptoVerify_Destroy(OH_CryptoVerify *ctx); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -224,7 +224,7 @@ OH_Crypto_ErrCode OH_CryptoSign_Create(const char *algoName, OH_CryptoSign **sig * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoSign_Update * @see OH_CryptoSign_Final @@ -240,7 +240,7 @@ OH_Crypto_ErrCode OH_CryptoSign_Init(OH_CryptoSign *ctx, OH_CryptoPrivKey *privK * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoSign_Init * @see OH_CryptoSign_Final @@ -257,7 +257,7 @@ OH_Crypto_ErrCode OH_CryptoSign_Update(OH_CryptoSign *ctx, const Crypto_DataBlob * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoSign_Init * @see OH_CryptoSign_Update @@ -283,7 +283,7 @@ const char *OH_CryptoSign_GetAlgoName(OH_CryptoSign *ctx); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -299,7 +299,7 @@ OH_Crypto_ErrCode OH_CryptoSign_SetParam(OH_CryptoSign *ctx, CryptoSignature_Par * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -329,7 +329,7 @@ typedef struct OH_CryptoEccSignatureSpec OH_CryptoEccSignatureSpec; * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -345,7 +345,7 @@ OH_Crypto_ErrCode OH_CryptoEccSignatureSpec_Create(Crypto_DataBlob *EccSignature * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -361,7 +361,7 @@ OH_Crypto_ErrCode OH_CryptoEccSignatureSpec_GetRAndS(OH_CryptoEccSignatureSpec * * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -376,7 +376,7 @@ OH_Crypto_ErrCode OH_CryptoEccSignatureSpec_SetRAndS(OH_CryptoEccSignatureSpec * * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@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_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ -- Gitee From 74d5416f18f8f0d1bd2776a8b79c74fc1055c608 Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Tue, 13 May 2025 09:13:56 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E9=87=8E=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- common/src/asy_key_params.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/common/src/asy_key_params.c b/common/src/asy_key_params.c index a525620..7b28b25 100644 --- a/common/src/asy_key_params.c +++ b/common/src/asy_key_params.c @@ -187,8 +187,10 @@ void FreeEccCommParamsSpec(HcfEccCommParamsSpec *spec) spec->b.data = NULL; HcfFree(spec->n.data); spec->n.data = NULL; - FreeEcFieldMem(&(spec->field)); - spec->field = NULL; + if (spec->field != NULL) { + FreeEcFieldMem(&(spec->field)); + spec->field = NULL; + } FreeEcPointMem(&(spec->g)); } -- Gitee From 503cb22e7b0fa6bfe0aaf3cad23d367000b5f3c1 Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Tue, 13 May 2025 09:14:27 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=8F=8D=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- frameworks/native/src/crypto_kdf.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/frameworks/native/src/crypto_kdf.c b/frameworks/native/src/crypto_kdf.c index 86f9524..376c27d 100644 --- a/frameworks/native/src/crypto_kdf.c +++ b/frameworks/native/src/crypto_kdf.c @@ -267,7 +267,7 @@ OH_Crypto_ErrCode OH_CryptoKdf_Create(const char *algoName, OH_CryptoKdf **ctx) return GetOhCryptoErrCodeNew(ret); } -static OH_Crypto_ErrCode HkdfDerive(HcfKdf *ctx, const HcfHkdfParamsSpec *params, int keyLen, HcfBlob *key) +static OH_Crypto_ErrCode HkdfDerive(HcfKdf *ctx, const HcfHkdfParamsSpec *params, uint32_t keyLen, HcfBlob *key) { uint8_t *out = (uint8_t *)HcfMalloc(keyLen, 0); if (out == NULL) { @@ -291,7 +291,7 @@ static OH_Crypto_ErrCode HkdfDerive(HcfKdf *ctx, const HcfHkdfParamsSpec *params return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode Pbkdf2Derive(HcfKdf *ctx, const HcfPBKDF2ParamsSpec *params, int keyLen, HcfBlob *key) +static OH_Crypto_ErrCode Pbkdf2Derive(HcfKdf *ctx, const HcfPBKDF2ParamsSpec *params, uint32_t keyLen, HcfBlob *key) { uint8_t *out = (uint8_t *)HcfMalloc(keyLen, 0); if (out == NULL) { @@ -315,7 +315,7 @@ static OH_Crypto_ErrCode Pbkdf2Derive(HcfKdf *ctx, const HcfPBKDF2ParamsSpec *pa return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode ScryptDerive(HcfKdf *ctx, const HcfScryptParamsSpec *params, int keyLen, HcfBlob *key) +static OH_Crypto_ErrCode ScryptDerive(HcfKdf *ctx, const HcfScryptParamsSpec *params, uint32_t keyLen, HcfBlob *key) { uint8_t *out = (uint8_t *)HcfMalloc(keyLen, 0); if (out == NULL) { @@ -345,16 +345,16 @@ static OH_Crypto_ErrCode ScryptDerive(HcfKdf *ctx, const HcfScryptParamsSpec *pa OH_Crypto_ErrCode OH_CryptoKdf_Derive(OH_CryptoKdf *ctx, const OH_CryptoKdfParams *params, int keyLen, Crypto_DataBlob *key) { - if ((ctx == NULL) || (params == NULL) || (params->algName == NULL) || (key == NULL)) { + if ((ctx == NULL) || (params == NULL) || (params->algName == NULL) || (key == NULL) || (keyLen <= 0)) { return CRYPTO_PARAMETER_CHECK_FAILED; } if (strcmp(params->algName, g_hkdfName) == 0) { - return HkdfDerive((HcfKdf *)ctx, (HcfHkdfParamsSpec *)params, keyLen, (HcfBlob *)key); + return HkdfDerive((HcfKdf *)ctx, (HcfHkdfParamsSpec *)params, (uint32_t)keyLen, (HcfBlob *)key); } else if (strcmp(params->algName, g_pbkdf2Name) == 0) { - return Pbkdf2Derive((HcfKdf *)ctx, (HcfPBKDF2ParamsSpec*)params, keyLen, (HcfBlob *)key); + return Pbkdf2Derive((HcfKdf *)ctx, (HcfPBKDF2ParamsSpec*)params, (uint32_t)keyLen, (HcfBlob *)key); } else if (strcmp(params->algName, g_scryptName) == 0) { - return ScryptDerive((HcfKdf *)ctx, (HcfScryptParamsSpec*)params, keyLen, (HcfBlob *)key); + return ScryptDerive((HcfKdf *)ctx, (HcfScryptParamsSpec*)params, (uint32_t)keyLen, (HcfBlob *)key); } else { return CRYPTO_PARAMETER_CHECK_FAILED; } -- Gitee From 6b3df5bf9c5d4d563bd93dbf2c35c624829fb3d9 Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Tue, 13 May 2025 10:45:11 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E6=B8=85=E7=90=86=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- frameworks/native/src/asym_key.c | 125 +++++++++++-------- frameworks/native/src/crypto_asym_cipher.c | 2 +- frameworks/native/src/crypto_kdf.c | 98 +++++++-------- frameworks/native/src/crypto_key_agreement.c | 3 +- frameworks/native/src/crypto_mac.c | 10 +- 5 files changed, 130 insertions(+), 108 deletions(-) diff --git a/frameworks/native/src/asym_key.c b/frameworks/native/src/asym_key.c index c8487bc..8142223 100644 --- a/frameworks/native/src/asym_key.c +++ b/frameworks/native/src/asym_key.c @@ -530,29 +530,36 @@ static OH_Crypto_ErrCode CreateAsymKeySpec(const char *algoName, CryptoAsymKeySp return CRYPTO_SUCCESS; } +static OH_Crypto_ErrCode CreateKeySpecFromInfo(const char *algoName, CryptoAsymKeySpec_Type type, + HcfAsyKeyGenParams *params, OH_CryptoAsymKeySpec **spec) +{ + uint32_t infoSize = sizeof(g_asymKeySpecInfoMap) / sizeof(OH_CryptoAsymKeySpecInfoMap); + for (uint32_t i = 0; i < infoSize; ++i) { + if (g_asymKeySpecInfoMap[i].algo == params->algo) { + for (uint32_t j = 0; j < g_asymKeySpecInfoMap[i].specInfoSize; ++j) { + if (g_asymKeySpecInfoMap[i].specInfo[j].type == type) { + return CreateAsymKeySpec(algoName, type, g_asymKeySpecInfoMap[i].specInfo[j].memSize, spec); + } + } + return CRYPTO_PARAMETER_CHECK_FAILED; + } + } + return CRYPTO_PARAMETER_CHECK_FAILED; +} + OH_Crypto_ErrCode OH_CryptoAsymKeySpec_Create(const char *algoName, CryptoAsymKeySpec_Type type, OH_CryptoAsymKeySpec **spec) { if ((algoName == NULL) || (spec == NULL)) { return CRYPTO_PARAMETER_CHECK_FAILED; } + HcfAsyKeyGenParams params = { 0 }; HcfResult ret = ParseAlgNameToParams(algoName, ¶ms); if (ret != HCF_SUCCESS) { return GetOhCryptoErrCodeNew(ret); } - OH_CryptoAsymKeySpecInfoMap *infoMap = g_asymKeySpecInfoMap; - for (uint32_t i = 0; i < (sizeof(g_asymKeySpecInfoMap) / sizeof(g_asymKeySpecInfoMap[0])); ++i) { - if (infoMap[i].algo == params.algo) { - for (uint32_t j = 0; j < infoMap[i].specInfoSize; ++j) { - if (infoMap[i].specInfo[j].type == type) { - return CreateAsymKeySpec(algoName, type, infoMap[i].specInfo[j].memSize, spec); - } - } - return CRYPTO_PARAMETER_CHECK_FAILED; - } - } - return CRYPTO_PARAMETER_CHECK_FAILED; + return CreateKeySpecFromInfo(algoName, type, ¶ms, spec); } static OH_Crypto_ErrCode SetDataBlob(uint8_t **dest, uint32_t *destLen, Crypto_DataBlob *value) @@ -585,9 +592,10 @@ static OH_Crypto_ErrCode GetDataBlob(const uint8_t *src, uint32_t srcLen, Crypto return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode SetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_P_DATABLOB: return SetDataBlob(&(spec->p.data), &(spec->p.len), value); case CRYPTO_DSA_Q_DATABLOB: @@ -599,9 +607,10 @@ static OH_Crypto_ErrCode SetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKe } } -static OH_Crypto_ErrCode SetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_PK_DATABLOB: return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); default: @@ -609,9 +618,10 @@ static OH_Crypto_ErrCode SetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode SetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); case CRYPTO_DSA_PK_DATABLOB: @@ -626,7 +636,7 @@ static OH_Crypto_ErrCode SetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (SetDsaCommSpec((HcfDsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PUBLIC_KEY_SPEC: return SetDsaPubKeySpec((HcfDsaPubKeyParamsSpec *)spec, type, value); case HCF_KEY_PAIR_SPEC: @@ -636,9 +646,10 @@ static OH_Crypto_ErrCode SetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa } } -static OH_Crypto_ErrCode SetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_N_DATABLOB: return SetDataBlob(&(spec->n.data), &(spec->n.len), value); default: @@ -646,9 +657,10 @@ static OH_Crypto_ErrCode SetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKe } } -static OH_Crypto_ErrCode SetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_E_DATABLOB: return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); default: @@ -656,9 +668,10 @@ static OH_Crypto_ErrCode SetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode SetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_D_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); case CRYPTO_RSA_E_DATABLOB: @@ -673,7 +686,7 @@ static OH_Crypto_ErrCode SetRsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (SetRsaCommSpec((HcfRsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PUBLIC_KEY_SPEC: return SetRsaPubKeySpec((HcfRsaPubKeyParamsSpec *)spec, type, value); case HCF_KEY_PAIR_SPEC: @@ -709,9 +722,10 @@ static OH_Crypto_ErrCode SetEccField(HcfEccCommParamsSpec *spec, Crypto_DataBlob return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode SetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_FP_P_DATABLOB: return SetEccField(spec, value); case CRYPTO_ECC_A_DATABLOB: @@ -736,9 +750,10 @@ static OH_Crypto_ErrCode SetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKe return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode SetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); default: @@ -746,9 +761,10 @@ static OH_Crypto_ErrCode SetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymK } } -static OH_Crypto_ErrCode SetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_PK_X_DATABLOB: return SetDataBlob(&(spec->pk.x.data), &(spec->pk.x.len), value); case CRYPTO_ECC_PK_Y_DATABLOB: @@ -758,9 +774,10 @@ static OH_Crypto_ErrCode SetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode SetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); case CRYPTO_ECC_PK_X_DATABLOB: @@ -777,7 +794,7 @@ static OH_Crypto_ErrCode SetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (SetEccCommSpec((HcfEccCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return SetEccPriSpec((HcfEccPriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -791,7 +808,7 @@ static OH_Crypto_ErrCode SetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa static OH_Crypto_ErrCode SetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_P_DATABLOB: return SetDataBlob(&(spec->p.data), &(spec->p.len), value); case CRYPTO_DH_G_DATABLOB: @@ -810,7 +827,7 @@ static OH_Crypto_ErrCode SetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ static OH_Crypto_ErrCode SetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); default: @@ -818,9 +835,10 @@ static OH_Crypto_ErrCode SetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey } } -static OH_Crypto_ErrCode SetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_PK_DATABLOB: return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); default: @@ -828,9 +846,10 @@ static OH_Crypto_ErrCode SetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsym } } -static OH_Crypto_ErrCode SetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); case CRYPTO_DH_PK_DATABLOB: @@ -845,7 +864,7 @@ static OH_Crypto_ErrCode SetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Par if (SetDhCommSpec((HcfDhCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return SetDhPriSpec((HcfDhPriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -857,9 +876,10 @@ static OH_Crypto_ErrCode SetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Par } } -static OH_Crypto_ErrCode SetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_SK_DATABLOB: case CRYPTO_X25519_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); @@ -868,9 +888,10 @@ static OH_Crypto_ErrCode SetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, C } } -static OH_Crypto_ErrCode SetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_PK_DATABLOB: case CRYPTO_X25519_PK_DATABLOB: return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); @@ -879,9 +900,10 @@ static OH_Crypto_ErrCode SetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec } } -static OH_Crypto_ErrCode SetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_SK_DATABLOB: case CRYPTO_X25519_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); @@ -893,9 +915,10 @@ static OH_Crypto_ErrCode SetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *sp } } -static OH_Crypto_ErrCode SetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return SetAlg25519PriSpec((HcfAlg25519PriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -920,7 +943,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, Cryp return GetOhCryptoErrCodeNew(ret); } - switch(params.algo) { + switch (params.algo) { case HCF_ALG_DSA: return SetDsaSpec(spec, type, value); case HCF_ALG_RSA: diff --git a/frameworks/native/src/crypto_asym_cipher.c b/frameworks/native/src/crypto_asym_cipher.c index fdc2d8b..03ecced 100644 --- a/frameworks/native/src/crypto_asym_cipher.c +++ b/frameworks/native/src/crypto_asym_cipher.c @@ -77,7 +77,7 @@ OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_Ciph return CRYPTO_PARAMETER_CHECK_FAILED; } HcfResult ret = HCF_SUCCESS; - switch(mode) { + switch (mode) { case CRYPTO_ENCRYPT_MODE: ret = ctx->init((HcfCipher *)ctx, (enum HcfCryptoMode)mode, (HcfKey *)(key->pubKey), NULL); break; diff --git a/frameworks/native/src/crypto_kdf.c b/frameworks/native/src/crypto_kdf.c index 376c27d..b95b5b6 100644 --- a/frameworks/native/src/crypto_kdf.c +++ b/frameworks/native/src/crypto_kdf.c @@ -136,63 +136,59 @@ static OH_Crypto_ErrCode SetPbkdf2Param(HcfPBKDF2ParamsSpec *params, CryptoKdf_P return CRYPTO_SUCCESS; } +static OH_Crypto_ErrCode SetScryptKeyParam(HcfScryptParamsSpec *params, Crypto_DataBlob *value) +{ + uint8_t *data = (uint8_t *)HcfMalloc(value->len, 0); + if (data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(data, value->len, value->data, value->len); + HcfFree(params->passPhrase.data); + params->passPhrase.data = data; + params->passPhrase.len = value->len; + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode SetScryptSaltParam(HcfScryptParamsSpec *params, Crypto_DataBlob *value) +{ + uint8_t *data = (uint8_t *)HcfMalloc(value->len, 0); + if (data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(data, value->len, value->data, value->len); + HcfFree(params->salt.data); + params->salt.data = data; + params->salt.len = value->len; + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode SetScryptUint64Param(HcfScryptParamsSpec *params, Crypto_DataBlob *value, uint64_t *target) +{ + if (value->len != sizeof(uint64_t)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + *target = *(uint64_t *)(value->data); + return CRYPTO_SUCCESS; +} + static OH_Crypto_ErrCode SetScryptParam(HcfScryptParamsSpec *params, CryptoKdf_ParamType type, Crypto_DataBlob *value) { switch (type) { - case CRYPTO_KDF_KEY_DATABLOB: { - uint8_t *data = (uint8_t *)HcfMalloc(value->len, 0); - if (data == NULL) { - return CRYPTO_MEMORY_ERROR; - } - (void)memcpy_s(data, value->len, value->data, value->len); - HcfFree(params->passPhrase.data); - params->passPhrase.data = data; - params->passPhrase.len = value->len; - break; - } - case CRYPTO_KDF_SALT_DATABLOB: { - uint8_t *data = (uint8_t *)HcfMalloc(value->len, 0); - if (data == NULL) { - return CRYPTO_MEMORY_ERROR; - } - (void)memcpy_s(data, value->len, value->data, value->len); - HcfFree(params->salt.data); - params->salt.data = data; - params->salt.len = value->len; - break; - } - case CRYPTO_KDF_SCRYPT_N_UINT64: { - if (value->len != sizeof(uint64_t)) { - return CRYPTO_PARAMETER_CHECK_FAILED; - } - params->n = *(uint64_t *)(value->data); - break; - } - case CRYPTO_KDF_SCRYPT_R_UINT64: { - if (value->len != sizeof(uint64_t)) { - return CRYPTO_PARAMETER_CHECK_FAILED; - } - params->r = *(uint64_t *)(value->data); - break; - } - case CRYPTO_KDF_SCRYPT_P_UINT64: { - if (value->len != sizeof(uint64_t)) { - return CRYPTO_PARAMETER_CHECK_FAILED; - } - params->p = *(uint64_t *)(value->data); - break; - } - case CRYPTO_KDF_SCRYPT_MAX_MEM_UINT64: { - if (value->len != sizeof(uint64_t)) { - return CRYPTO_PARAMETER_CHECK_FAILED; - } - params->maxMem = *(uint64_t *)(value->data); - break; - } + case CRYPTO_KDF_KEY_DATABLOB: + return SetScryptKeyParam(params, value); + case CRYPTO_KDF_SALT_DATABLOB: + return SetScryptSaltParam(params, value); + case CRYPTO_KDF_SCRYPT_N_UINT64: + return SetScryptUint64Param(params, value, ¶ms->n); + case CRYPTO_KDF_SCRYPT_R_UINT64: + return SetScryptUint64Param(params, value, ¶ms->r); + case CRYPTO_KDF_SCRYPT_P_UINT64: + return SetScryptUint64Param(params, value, ¶ms->p); + case CRYPTO_KDF_SCRYPT_MAX_MEM_UINT64: + return SetScryptUint64Param(params, value, ¶ms->maxMem); default: return CRYPTO_PARAMETER_CHECK_FAILED; } - return CRYPTO_SUCCESS; } OH_Crypto_ErrCode OH_CryptoKdfParams_SetParam(OH_CryptoKdfParams *params, CryptoKdf_ParamType type, diff --git a/frameworks/native/src/crypto_key_agreement.c b/frameworks/native/src/crypto_key_agreement.c index ab0c8d1..d3d4c02 100644 --- a/frameworks/native/src/crypto_key_agreement.c +++ b/frameworks/native/src/crypto_key_agreement.c @@ -43,7 +43,8 @@ OH_Crypto_ErrCode OH_CryptoKeyAgreement_GenerateSecret(OH_CryptoKeyAgreement *ct if ((ctx == NULL) || (ctx->generateSecret == NULL) || (privkey == NULL) || (pubkey == NULL) || (secret == NULL)) { return CRYPTO_PARAMETER_CHECK_FAILED; } - HcfResult ret = ctx->generateSecret((HcfKeyAgreement *)ctx, (HcfPriKey *)privkey, (HcfPubKey *)pubkey, (HcfBlob *)secret); + HcfResult ret = ctx->generateSecret((HcfKeyAgreement *)ctx, (HcfPriKey *)privkey, (HcfPubKey *)pubkey, + (HcfBlob *)secret); return GetOhCryptoErrCodeNew(ret); } diff --git a/frameworks/native/src/crypto_mac.c b/frameworks/native/src/crypto_mac.c index 973fcc9..314fc01 100644 --- a/frameworks/native/src/crypto_mac.c +++ b/frameworks/native/src/crypto_mac.c @@ -67,9 +67,10 @@ OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx) return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode SetCmacParam(HcfCmacParamsSpec *paramsSpec, CryptoMac_ParamType type, const Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetCmacParam(HcfCmacParamsSpec *paramsSpec, CryptoMac_ParamType type, + const Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_MAC_CIPHER_NAME_STR: { char *data = (char *)HcfMalloc(value->len + 1, 0); if (data == NULL) { @@ -85,9 +86,10 @@ static OH_Crypto_ErrCode SetCmacParam(HcfCmacParamsSpec *paramsSpec, CryptoMac_P } } -static OH_Crypto_ErrCode SetHmacParam(HcfHmacParamsSpec *paramsSpec, CryptoMac_ParamType type, const Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetHmacParam(HcfHmacParamsSpec *paramsSpec, CryptoMac_ParamType type, + const Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_MAC_DIGEST_NAME_STR: { char *data = (char *)HcfMalloc(value->len + 1, 0); if (data == NULL) { -- Gitee From 82791f029abcc41994d15fa86298aff5b605198b Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Tue, 13 May 2025 11:44:35 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8DCAPI=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=A0=81=E6=B2=A1=E6=9C=89=E8=BD=AC=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- frameworks/native/src/crypto_mac.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/native/src/crypto_mac.c b/frameworks/native/src/crypto_mac.c index 314fc01..5181b0c 100644 --- a/frameworks/native/src/crypto_mac.c +++ b/frameworks/native/src/crypto_mac.c @@ -123,7 +123,7 @@ OH_Crypto_ErrCode OH_CryptoMac_SetParam(OH_CryptoMac *ctx, CryptoMac_ParamType t OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *key) { if ((ctx == NULL) || (key == NULL)) { - return HCF_INVALID_PARAMS; + return CRYPTO_PARAMETER_CHECK_FAILED; } HcfMac *macObj = NULL; @@ -147,7 +147,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *ke OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob *in) { if ((ctx == NULL) || (ctx->macObj == NULL) || (ctx->macObj->update == NULL) || (in == NULL)) { - return HCF_INVALID_PARAMS; + return CRYPTO_PARAMETER_CHECK_FAILED; } HcfResult ret = ctx->macObj->update(ctx->macObj, (HcfBlob *)in); return GetOhCryptoErrCodeNew(ret); -- Gitee From 5ff6d112fb8a56788f7acb95d265e8288c06642c Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Tue, 13 May 2025 17:44:27 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E6=96=B0=E5=A2=9EOH=5FCryptoAsymKeyGener?= =?UTF-8?q?ator=5FSetPassword=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- frameworks/native/src/asym_key.c | 84 +++++++++++++------ .../kits/native/include/crypto_asym_key.h | 19 +++++ interfaces/kits/native/include/crypto_kdf.h | 4 +- .../src/native/native_asym_key_test.cpp | 6 ++ 4 files changed, 86 insertions(+), 27 deletions(-) diff --git a/frameworks/native/src/asym_key.c b/frameworks/native/src/asym_key.c index 8142223..ea12ed0 100644 --- a/frameworks/native/src/asym_key.c +++ b/frameworks/native/src/asym_key.c @@ -40,18 +40,8 @@ typedef struct OH_CryptoAsymKeyGenerator { - HcfObjectBase base; - - HcfResult (*generateKeyPair)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, - HcfKeyPair **returnKeyPair); - - HcfResult (*convertKey)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob, - HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair); - - HcfResult (*convertPemKey)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, const char *pubKeyStr, - const char *priKeyStr, HcfKeyPair **returnKeyPair); - - const char *(*getAlgoName)(HcfAsyKeyGenerator *self); + HcfAsyKeyGenerator *base; + HcfKeyDecodingParamsSpec *decSpec; } OH_CryptoAsymKeyGenerator; typedef struct OH_CryptoKeyPair { @@ -120,23 +110,52 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Create(const char *algoName, OH_Cryp if (ctx == NULL) { return CRYPTO_INVALID_PARAMS; } - HcfResult ret = HcfAsyKeyGeneratorCreate(algoName, (HcfAsyKeyGenerator **)ctx); + OH_CryptoAsymKeyGenerator *tmpCtx = HcfMalloc(sizeof(OH_CryptoAsymKeyGenerator), 0); + if (tmpCtx == NULL) { + return CRYPTO_MEMORY_ERROR; + } + HcfResult ret = HcfAsyKeyGeneratorCreate(algoName, &(tmpCtx->base)); + if (ret != HCF_SUCCESS) { + HcfFree(tmpCtx); + return GetOhCryptoErrCode(ret); + } + *ctx = tmpCtx; return GetOhCryptoErrCode(ret); } OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Generate(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoKeyPair **keyCtx) { - if ((ctx == NULL) || (ctx->generateKeyPair == NULL) || (keyCtx == NULL)) { + if ((ctx == NULL) || (ctx->base == NULL) || (ctx->base->generateKeyPair == NULL) || (keyCtx == NULL)) { return CRYPTO_INVALID_PARAMS; } - HcfResult ret = ctx->generateKeyPair((HcfAsyKeyGenerator *)ctx, NULL, (HcfKeyPair **)keyCtx); + HcfResult ret = ctx->base->generateKeyPair((HcfAsyKeyGenerator *)(ctx->base), NULL, (HcfKeyPair **)keyCtx); return GetOhCryptoErrCode(ret); } +OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_SetPassword(OH_CryptoAsymKeyGenerator *ctx, const unsigned char *password, + uint32_t passwordLen) +{ + if ((ctx == NULL) || (password == NULL) || (passwordLen == 0)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfKeyDecodingParamsSpec *decSpec = (HcfKeyDecodingParamsSpec *)HcfMalloc(sizeof(HcfKeyDecodingParamsSpec), 0); + if (decSpec == NULL) { + return CRYPTO_MEMORY_ERROR; + } + decSpec->password = (char *)HcfMalloc(passwordLen + 1, 0); + if (decSpec->password == NULL) { + HcfFree(decSpec); + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(decSpec->password, passwordLen, password, passwordLen); + ctx->decSpec = decSpec; + return CRYPTO_SUCCESS; +} + OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *ctx, Crypto_EncodingType type, Crypto_DataBlob *pubKeyData, Crypto_DataBlob *priKeyData, OH_CryptoKeyPair **keyCtx) { - if ((ctx == NULL) || (pubKeyData == NULL && priKeyData == NULL) || (keyCtx == NULL)) { + if ((ctx == NULL) || (ctx->base == NULL) || (pubKeyData == NULL && priKeyData == NULL) || (keyCtx == NULL)) { return CRYPTO_INVALID_PARAMS; } HcfResult ret = HCF_SUCCESS; @@ -144,13 +163,14 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *c const char *pubKeyStr = (pubKeyData == NULL)? NULL : (const char *)pubKeyData->data; switch (type) { case CRYPTO_PEM: - ret = ctx->convertPemKey == NULL ? HCF_INVALID_PARAMS : - ctx->convertPemKey((HcfAsyKeyGenerator *)ctx, NULL, pubKeyStr, priKeyStr, (HcfKeyPair **)keyCtx); + ret = ctx->base->convertPemKey == NULL ? HCF_INVALID_PARAMS : + ctx->base->convertPemKey((HcfAsyKeyGenerator *)(ctx->base), (HcfParamsSpec *)(ctx->decSpec), pubKeyStr, + priKeyStr, (HcfKeyPair **)keyCtx); break; case CRYPTO_DER: - ret = ctx->convertKey == NULL ? HCF_INVALID_PARAMS : - ctx->convertKey((HcfAsyKeyGenerator *)ctx, NULL, (HcfBlob *)pubKeyData, - (HcfBlob *)priKeyData, (HcfKeyPair **)keyCtx); + ret = ctx->base->convertKey == NULL ? HCF_INVALID_PARAMS : + ctx->base->convertKey((HcfAsyKeyGenerator *)(ctx->base), (HcfParamsSpec *)(ctx->decSpec), + (HcfBlob *)pubKeyData, (HcfBlob *)priKeyData, (HcfKeyPair **)keyCtx); break; default: return CRYPTO_INVALID_PARAMS; @@ -160,18 +180,32 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *c const char *OH_CryptoAsymKeyGenerator_GetAlgoName(OH_CryptoAsymKeyGenerator *ctx) { - if ((ctx == NULL) || (ctx->getAlgoName == NULL)) { + if ((ctx == NULL) || (ctx->base == NULL) || (ctx->base->getAlgoName == NULL)) { return NULL; } - return ctx->getAlgoName((HcfAsyKeyGenerator *)ctx); + return ctx->base->getAlgoName((HcfAsyKeyGenerator *)(ctx->base)); +} + +static void FreeDecParamsSpec(HcfKeyDecodingParamsSpec *decSpec) +{ + if (decSpec == NULL) { + return; + } + HcfFree(decSpec->password); + decSpec->password = NULL; + HcfFree(decSpec); } void OH_CryptoAsymKeyGenerator_Destroy(OH_CryptoAsymKeyGenerator *ctx) { - if ((ctx == NULL) || (ctx->base.destroy == NULL)) { + if (ctx == NULL) { return; } - ctx->base.destroy((HcfObjectBase *)ctx); + HcfObjDestroy(ctx->base); + ctx->base = NULL; + FreeDecParamsSpec(ctx->decSpec); + ctx->decSpec = NULL; + HcfFree(ctx); } void OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *keyCtx) diff --git a/interfaces/kits/native/include/crypto_asym_key.h b/interfaces/kits/native/include/crypto_asym_key.h index 7f09d55..4997e5e 100644 --- a/interfaces/kits/native/include/crypto_asym_key.h +++ b/interfaces/kits/native/include/crypto_asym_key.h @@ -275,6 +275,25 @@ 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 Sets the password to the asymmetric key generator context. + * + * Call this method to set the password if you need to convert encrypted private key data to a key pair using + * {@link OH_CryptoAsymKeyGenerator_Convert} + * + * @param ctx Indicates the asymmetric key generator context. + * @param password Indicates the password. + * @param passwordLen Indicates the password length. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@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_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_SetPassword(OH_CryptoAsymKeyGenerator *ctx, const unsigned char *password, + uint32_t passwordLen); + /** * @brief Defines the private key encoding params structure. * diff --git a/interfaces/kits/native/include/crypto_kdf.h b/interfaces/kits/native/include/crypto_kdf.h index 24ae125..afa02bc 100644 --- a/interfaces/kits/native/include/crypto_kdf.h +++ b/interfaces/kits/native/include/crypto_kdf.h @@ -90,7 +90,7 @@ typedef enum { /** * @brief Creates KDF params. * - * @param algoName Indicates the KDF algorithm name. e.g. "HKDF|SHA384|EXTRACT_AND_EXPAND", "PBKDF2|SHA384". + * @param algoName Indicates the KDF algorithm name. e.g. "HKDF", "PBKDF2", "SCRYPT". * @param params Indicates the KDF params. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. @@ -128,7 +128,7 @@ void OH_CryptoKdfParams_Destroy(OH_CryptoKdfParams *params); /** * @brief Creates a KDF context. * - * @param algoName Indicates the KDF algorithm name. + * @param algoName Indicates the KDF algorithm name. e.g. "HKDF|SHA384|EXTRACT_AND_EXPAND", "PBKDF2|SHA384", "SCRYPT". * @param ctx Indicates the KDF context. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. diff --git a/test/unittest/src/native/native_asym_key_test.cpp b/test/unittest/src/native/native_asym_key_test.cpp index 0a27627..278bc1b 100644 --- a/test/unittest/src/native/native_asym_key_test.cpp +++ b/test/unittest/src/native/native_asym_key_test.cpp @@ -465,6 +465,12 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest006, TestSize.Level0) EXPECT_EQ(ret, CRYPTO_SUCCESS); ASSERT_NE(pemData.data, nullptr); ASSERT_NE(pemData.len, 0); + ret = OH_CryptoAsymKeyGenerator_SetPassword(keyGen, password.data, password.len); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoKeyPair *keyCtx = nullptr; + ret = OH_CryptoAsymKeyGenerator_Convert(keyGen, CRYPTO_PEM, nullptr, &pemData, &keyCtx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoKeyPair_Destroy(keyCtx); Crypto_DataBlob n = {0}; Crypto_DataBlob d = {0}; -- Gitee From 00df82928c3dce28d496ec7adc08c608c58d5855 Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Tue, 13 May 2025 18:01:40 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A7=81=E9=92=A5?= =?UTF-8?q?=E7=BC=96=E7=A0=81=E5=8F=A3=E4=BB=A4=E4=B8=BA=E7=A9=BA=E6=97=B6?= =?UTF-8?q?=E7=BC=96=E7=A0=81=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- frameworks/native/src/asym_key.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/src/asym_key.c b/frameworks/native/src/asym_key.c index ea12ed0..d20117c 100644 --- a/frameworks/native/src/asym_key.c +++ b/frameworks/native/src/asym_key.c @@ -349,7 +349,7 @@ OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_Create(OH_CryptoPrivKeyEncoding OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_SetParam(OH_CryptoPrivKeyEncodingParams *ctx, CryptoPrivKeyEncoding_ParamType type, Crypto_DataBlob *value) { - if ((ctx == NULL) || (value == NULL)) { + if ((ctx == NULL) || (value == NULL) || (value->data == NULL) || (value->len == 0)) { return CRYPTO_PARAMETER_CHECK_FAILED; } char *data = (char *)HcfMalloc(value->len + 1, 0); -- Gitee From d53e500ba0a0f6e3edda0e778c5a1bbe9b1cb83d Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Wed, 14 May 2025 10:23:16 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=AE=BE=E7=BD=AEDsa=E9=9D=9E=E5=AF=B9=E7=A7=B0?= =?UTF-8?q?=E5=AF=86=E9=92=A5=E5=85=AC=E5=85=B1=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- frameworks/native/src/asym_key.c | 29 ++++++- .../src/native/native_asym_key_test.cpp | 80 +++++++++++-------- 2 files changed, 76 insertions(+), 33 deletions(-) diff --git a/frameworks/native/src/asym_key.c b/frameworks/native/src/asym_key.c index d20117c..21eb7cf 100644 --- a/frameworks/native/src/asym_key.c +++ b/frameworks/native/src/asym_key.c @@ -504,7 +504,6 @@ typedef struct { } OH_CryptoAsymKeySpecInfoMap; static OH_CryptoAsymKeySpecInfo g_rsaSpecInfo[] = { - {CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, sizeof(HcfRsaCommParamsSpec)}, {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfRsaPubKeyParamsSpec)}, {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfRsaKeyPairParamsSpec)}, }; @@ -995,6 +994,32 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, Cryp } } +static OH_Crypto_ErrCode SetDsaCommonSpec(HcfDsaCommParamsSpec *commonParamsSpec, HcfDsaCommParamsSpec *spec) +{ + spec->p.data = (unsigned char *)HcfMalloc(commonParamsSpec->p.len, 0); + if (spec->p.data == NULL) { + FreeDsaCommParamsSpec(spec); + return CRYPTO_MEMORY_ERROR; + } + spec->q.data = (unsigned char *)HcfMalloc(commonParamsSpec->q.len, 0); + if (spec->q.data == NULL) { + FreeDsaCommParamsSpec(spec); + return CRYPTO_MEMORY_ERROR; + } + spec->g.data = (unsigned char *)HcfMalloc(commonParamsSpec->g.len, 0); + if (spec->g.data == NULL) { + FreeDsaCommParamsSpec(spec); + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(spec->p.data, commonParamsSpec->p.len, commonParamsSpec->p.data, commonParamsSpec->p.len); + (void)memcpy_s(spec->q.data, commonParamsSpec->q.len, commonParamsSpec->q.data, commonParamsSpec->q.len); + (void)memcpy_s(spec->g.data, commonParamsSpec->g.len, commonParamsSpec->g.data, commonParamsSpec->g.len); + spec->p.len = commonParamsSpec->p.len; + spec->q.len = commonParamsSpec->q.len; + spec->g.len = commonParamsSpec->g.len; + return CRYPTO_SUCCESS; +} + static OH_Crypto_ErrCode SetEccCommonSpec(HcfEccCommParamsSpec *commonParamsSpec, HcfEccCommParamsSpec *spec) { HcfEccCommParamsSpec eccCommParamsSpec = {}; @@ -1050,6 +1075,8 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec } switch(params.algo) { + case HCF_ALG_DSA: + return SetDsaCommonSpec((HcfDsaCommParamsSpec *)commonParamsSpec, (HcfDsaCommParamsSpec *)spec); case HCF_ALG_ECC: case HCF_ALG_SM2: return SetEccCommonSpec((HcfEccCommParamsSpec *)commonParamsSpec, (HcfEccCommParamsSpec *)spec); diff --git a/test/unittest/src/native/native_asym_key_test.cpp b/test/unittest/src/native/native_asym_key_test.cpp index 278bc1b..05a1501 100644 --- a/test/unittest/src/native/native_asym_key_test.cpp +++ b/test/unittest/src/native/native_asym_key_test.cpp @@ -760,41 +760,11 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest012, TestSize.Level0) HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest013, TestSize.Level0) { - OH_CryptoKeyPair *keyCtx = GenerateKeyPair("RSA2048"); - ASSERT_NE(keyCtx, nullptr); - Crypto_DataBlob pubKeyData = {.data = nullptr, .len = 0}; - Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; - Crypto_DataBlob nData = {.data = nullptr, .len = 0}; - OH_Crypto_ErrCode ret = GetRsaKeyParams(keyCtx, &pubKeyData, &privKeyData, &nData); - EXPECT_EQ(ret, CRYPTO_SUCCESS); - OH_CryptoAsymKeySpec *keySpec = nullptr; - ret = OH_CryptoAsymKeySpec_Create("RSA", CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, &keySpec); + OH_Crypto_ErrCode ret = OH_CryptoAsymKeySpec_Create("RSA", CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, &keySpec); ASSERT_NE(ret, CRYPTO_SUCCESS); ret = OH_CryptoAsymKeySpec_Create("RSA", CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, &keySpec); - EXPECT_EQ(ret, CRYPTO_SUCCESS); - ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_RSA_E_DATABLOB, &pubKeyData); - EXPECT_NE(ret, CRYPTO_SUCCESS); - ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_RSA_D_DATABLOB, &privKeyData); - EXPECT_NE(ret, CRYPTO_SUCCESS); - ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_RSA_N_DATABLOB, &nData); - EXPECT_EQ(ret, CRYPTO_SUCCESS); - FreeRsaKeyParams(&pubKeyData, &privKeyData, &nData); - ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_RSA_E_DATABLOB, &pubKeyData); - EXPECT_NE(ret, CRYPTO_SUCCESS); - ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_RSA_D_DATABLOB, &privKeyData); - EXPECT_NE(ret, CRYPTO_SUCCESS); - ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_RSA_N_DATABLOB, &nData); - EXPECT_EQ(ret, CRYPTO_SUCCESS); - FreeRsaKeyParams(&pubKeyData, &privKeyData, &nData); - - OH_CryptoKeyPair *keyPair = nullptr; - ret = GenerateKeyPairWithSpec(keySpec, &keyPair); - EXPECT_NE(ret, CRYPTO_SUCCESS); - - OH_CryptoKeyPair_Destroy(keyPair); - OH_CryptoAsymKeySpec_Destroy(keySpec); - OH_CryptoKeyPair_Destroy(keyCtx); + ASSERT_NE(ret, CRYPTO_SUCCESS); } HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest014, TestSize.Level0) @@ -1350,4 +1320,50 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest027, TestSize.Level0) OH_CryptoEcPoint_Destroy(point); OH_CryptoEcPoint_Destroy(point2); } + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest028, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("DSA2048"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + Crypto_DataBlob pData = {.data = nullptr, .len = 0}; + Crypto_DataBlob qData = {.data = nullptr, .len = 0}; + Crypto_DataBlob gData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetDsaKeyParams(keyCtx, &pubKeyData, &privKeyData, &pData, &qData, &gData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *dsaCommonSpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("DSA", CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, &dsaCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(dsaCommonSpec, CRYPTO_DSA_P_DATABLOB, &pData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(dsaCommonSpec, CRYPTO_DSA_Q_DATABLOB, &qData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(dsaCommonSpec, CRYPTO_DSA_G_DATABLOB, &gData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("DSA", CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDsaKeyParams(&pubKeyData, &privKeyData, &pData, &qData, &gData); + ret = OH_CryptoAsymKeySpec_SetCommonParamsSpec(keySpec, dsaCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = GenerateKeyPairWithSpec(keySpec, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPubKey(keyPair), nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPrivKey(keyPair), nullptr); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(dsaCommonSpec); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} } \ No newline at end of file -- Gitee From ede89f51d1190865cbb505f942368957eee5b581 Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Wed, 14 May 2025 11:28:11 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E6=B8=85=E7=90=86=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- frameworks/native/src/asym_key.c | 135 ++++++++++-------- .../src/native/native_asym_key_test.cpp | 8 -- 2 files changed, 76 insertions(+), 67 deletions(-) diff --git a/frameworks/native/src/asym_key.c b/frameworks/native/src/asym_key.c index 21eb7cf..fda7168 100644 --- a/frameworks/native/src/asym_key.c +++ b/frameworks/native/src/asym_key.c @@ -563,21 +563,19 @@ static OH_Crypto_ErrCode CreateAsymKeySpec(const char *algoName, CryptoAsymKeySp return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode CreateKeySpecFromInfo(const char *algoName, CryptoAsymKeySpec_Type type, - HcfAsyKeyGenParams *params, OH_CryptoAsymKeySpec **spec) -{ - uint32_t infoSize = sizeof(g_asymKeySpecInfoMap) / sizeof(OH_CryptoAsymKeySpecInfoMap); - for (uint32_t i = 0; i < infoSize; ++i) { - if (g_asymKeySpecInfoMap[i].algo == params->algo) { - for (uint32_t j = 0; j < g_asymKeySpecInfoMap[i].specInfoSize; ++j) { - if (g_asymKeySpecInfoMap[i].specInfo[j].type == type) { - return CreateAsymKeySpec(algoName, type, g_asymKeySpecInfoMap[i].specInfo[j].memSize, spec); - } - } - return CRYPTO_PARAMETER_CHECK_FAILED; +static const OH_CryptoAsymKeySpecInfoMap *FindAsymKeySpecInfoMapByAlgoName(const char *algoName) +{ + HcfAsyKeyGenParams params = { 0 }; + HcfResult ret = ParseAlgNameToParams(algoName, ¶ms); + if (ret != HCF_SUCCESS) { + return NULL; + } + for (uint32_t i = 0; i < (sizeof(g_asymKeySpecInfoMap) / sizeof(OH_CryptoAsymKeySpecInfoMap)); ++i) { + if (g_asymKeySpecInfoMap[i].algo == params.algo) { + return &g_asymKeySpecInfoMap[i]; } } - return CRYPTO_PARAMETER_CHECK_FAILED; + return NULL; } OH_Crypto_ErrCode OH_CryptoAsymKeySpec_Create(const char *algoName, CryptoAsymKeySpec_Type type, @@ -586,13 +584,16 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_Create(const char *algoName, CryptoAsymKe if ((algoName == NULL) || (spec == NULL)) { return CRYPTO_PARAMETER_CHECK_FAILED; } - - HcfAsyKeyGenParams params = { 0 }; - HcfResult ret = ParseAlgNameToParams(algoName, ¶ms); - if (ret != HCF_SUCCESS) { - return GetOhCryptoErrCodeNew(ret); + const OH_CryptoAsymKeySpecInfoMap *infoMap = FindAsymKeySpecInfoMapByAlgoName(algoName); + if (infoMap == NULL) { + return CRYPTO_PARAMETER_CHECK_FAILED; } - return CreateKeySpecFromInfo(algoName, type, ¶ms, spec); + for (uint32_t i = 0; i < infoMap->specInfoSize; ++i) { + if (infoMap->specInfo[i].type == type) { + return CreateAsymKeySpec(algoName, type, infoMap->specInfo[i].memSize, spec); + } + } + return CRYPTO_PARAMETER_CHECK_FAILED; } static OH_Crypto_ErrCode SetDataBlob(uint8_t **dest, uint32_t *destLen, Crypto_DataBlob *value) @@ -1074,7 +1075,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec return GetOhCryptoErrCodeNew(ret); } - switch(params.algo) { + switch (params.algo) { case HCF_ALG_DSA: return SetDsaCommonSpec((HcfDsaCommParamsSpec *)commonParamsSpec, (HcfDsaCommParamsSpec *)spec); case HCF_ALG_ECC: @@ -1087,9 +1088,10 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec } } -static OH_Crypto_ErrCode GetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_P_DATABLOB: return GetDataBlob(spec->p.data, spec->p.len, value); case CRYPTO_DSA_Q_DATABLOB: @@ -1101,9 +1103,10 @@ static OH_Crypto_ErrCode GetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKe } } -static OH_Crypto_ErrCode GetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_PK_DATABLOB: return GetDataBlob(spec->pk.data, spec->pk.len, value); default: @@ -1111,9 +1114,10 @@ static OH_Crypto_ErrCode GetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode GetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); case CRYPTO_DSA_PK_DATABLOB: @@ -1128,7 +1132,7 @@ static OH_Crypto_ErrCode GetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (GetDsaCommSpec((HcfDsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PUBLIC_KEY_SPEC: return GetDsaPubKeySpec((HcfDsaPubKeyParamsSpec *)spec, type, value); case HCF_KEY_PAIR_SPEC: @@ -1138,9 +1142,10 @@ static OH_Crypto_ErrCode GetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa } } -static OH_Crypto_ErrCode GetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_N_DATABLOB: return GetDataBlob(spec->n.data, spec->n.len, value); default: @@ -1148,9 +1153,10 @@ static OH_Crypto_ErrCode GetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKe } } -static OH_Crypto_ErrCode GetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_E_DATABLOB: return GetDataBlob(spec->pk.data, spec->pk.len, value); default: @@ -1158,9 +1164,10 @@ static OH_Crypto_ErrCode GetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode GetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_D_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); case CRYPTO_RSA_E_DATABLOB: @@ -1175,7 +1182,7 @@ static OH_Crypto_ErrCode GetRsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (GetRsaCommSpec((HcfRsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PUBLIC_KEY_SPEC: return GetRsaPubKeySpec((HcfRsaPubKeyParamsSpec *)spec, type, value); case HCF_KEY_PAIR_SPEC: @@ -1193,9 +1200,10 @@ static OH_Crypto_ErrCode GetEccField(HcfEccCommParamsSpec *spec, Crypto_DataBlob return GetDataBlob(((HcfECFieldFp *)(spec->field))->p.data, ((HcfECFieldFp *)(spec->field))->p.len, value); } -static OH_Crypto_ErrCode GetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_FP_P_DATABLOB: return GetEccField(spec, value); case CRYPTO_ECC_A_DATABLOB: @@ -1222,9 +1230,10 @@ static OH_Crypto_ErrCode GetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKe return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode GetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); default: @@ -1232,9 +1241,10 @@ static OH_Crypto_ErrCode GetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymK } } -static OH_Crypto_ErrCode GetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_PK_X_DATABLOB: return GetDataBlob(spec->pk.x.data, spec->pk.x.len, value); case CRYPTO_ECC_PK_Y_DATABLOB: @@ -1244,9 +1254,10 @@ static OH_Crypto_ErrCode GetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode GetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); case CRYPTO_ECC_PK_X_DATABLOB: @@ -1263,7 +1274,7 @@ static OH_Crypto_ErrCode GetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (GetEccCommSpec((HcfEccCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return GetEccPriSpec((HcfEccPriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -1277,7 +1288,7 @@ static OH_Crypto_ErrCode GetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa static OH_Crypto_ErrCode GetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_P_DATABLOB: return GetDataBlob(spec->p.data, spec->p.len, value); case CRYPTO_DH_G_DATABLOB: @@ -1298,7 +1309,7 @@ static OH_Crypto_ErrCode GetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ static OH_Crypto_ErrCode GetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); default: @@ -1306,9 +1317,10 @@ static OH_Crypto_ErrCode GetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey } } -static OH_Crypto_ErrCode GetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_PK_DATABLOB: return GetDataBlob(spec->pk.data, spec->pk.len, value); default: @@ -1316,9 +1328,10 @@ static OH_Crypto_ErrCode GetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsym } } -static OH_Crypto_ErrCode GetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); case CRYPTO_DH_PK_DATABLOB: @@ -1333,7 +1346,7 @@ static OH_Crypto_ErrCode GetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Par if (GetDhCommSpec((HcfDhCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return GetDhPriSpec((HcfDhPriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -1345,9 +1358,10 @@ static OH_Crypto_ErrCode GetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Par } } -static OH_Crypto_ErrCode GetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_SK_DATABLOB: case CRYPTO_X25519_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); @@ -1356,9 +1370,10 @@ static OH_Crypto_ErrCode GetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, C } } -static OH_Crypto_ErrCode GetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_PK_DATABLOB: case CRYPTO_X25519_PK_DATABLOB: return GetDataBlob(spec->pk.data, spec->pk.len, value); @@ -1367,9 +1382,10 @@ static OH_Crypto_ErrCode GetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec } } -static OH_Crypto_ErrCode GetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_SK_DATABLOB: case CRYPTO_X25519_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); @@ -1381,9 +1397,10 @@ static OH_Crypto_ErrCode GetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *sp } } -static OH_Crypto_ErrCode GetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return GetAlg25519PriSpec((HcfAlg25519PriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -1408,7 +1425,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec *spec, Cryp return GetOhCryptoErrCodeNew(ret); } - switch(params.algo) { + switch (params.algo) { case HCF_ALG_DSA: return GetDsaSpec(spec, type, value); case HCF_ALG_RSA: diff --git a/test/unittest/src/native/native_asym_key_test.cpp b/test/unittest/src/native/native_asym_key_test.cpp index 05a1501..9017fae 100644 --- a/test/unittest/src/native/native_asym_key_test.cpp +++ b/test/unittest/src/native/native_asym_key_test.cpp @@ -457,14 +457,10 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest006, TestSize.Level0) Crypto_DataBlob pemData = {0}; ret = OH_CryptoPrivKey_Encode(privKey, CRYPTO_PEM, "PKCS8", nullptr, &pemData); EXPECT_EQ(ret, CRYPTO_SUCCESS); - ASSERT_NE(pemData.data, nullptr); - ASSERT_NE(pemData.len, 0); OH_Crypto_FreeDataBlob(&pemData); ret = OH_CryptoPrivKey_Encode(privKey, CRYPTO_PEM, "PKCS8", params, &pemData); EXPECT_EQ(ret, CRYPTO_SUCCESS); - ASSERT_NE(pemData.data, nullptr); - ASSERT_NE(pemData.len, 0); ret = OH_CryptoAsymKeyGenerator_SetPassword(keyGen, password.data, password.len); EXPECT_EQ(ret, CRYPTO_SUCCESS); OH_CryptoKeyPair *keyCtx = nullptr; @@ -476,12 +472,8 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest006, TestSize.Level0) Crypto_DataBlob d = {0}; ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_RSA_N_DATABLOB, &n); EXPECT_EQ(ret, CRYPTO_SUCCESS); - ASSERT_NE(n.data, nullptr); - ASSERT_NE(n.len, 0); ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_RSA_D_DATABLOB, &d); EXPECT_EQ(ret, CRYPTO_SUCCESS); - ASSERT_NE(d.data, nullptr); - ASSERT_NE(d.len, 0); OH_Crypto_FreeDataBlob(&pemData); OH_Crypto_FreeDataBlob(&n); -- Gitee From eeaca4c6263a658f12f3b5303e424111d717e617 Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Thu, 15 May 2025 16:48:54 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- frameworks/native/src/crypto_kdf.c | 11 +++--- frameworks/native/src/crypto_mac.c | 52 +++++++++++++---------------- frameworks/native/src/crypto_rand.c | 5 +-- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/frameworks/native/src/crypto_kdf.c b/frameworks/native/src/crypto_kdf.c index b95b5b6..96a8bbb 100644 --- a/frameworks/native/src/crypto_kdf.c +++ b/frameworks/native/src/crypto_kdf.c @@ -80,14 +80,17 @@ static OH_Crypto_ErrCode SetHkdfParam(HcfHkdfParamsSpec *params, CryptoKdf_Param (void)memcpy_s(data, value->len, value->data, value->len); switch (type) { case CRYPTO_KDF_KEY_DATABLOB: + HcfBlobDataClearAndFree(&(params->key)); params->key.data = data; params->key.len = value->len; break; case CRYPTO_KDF_SALT_DATABLOB: + HcfBlobDataClearAndFree(&(params->salt)); params->salt.data = data; params->salt.len = value->len; break; case CRYPTO_KDF_INFO_DATABLOB: + HcfBlobDataClearAndFree(&(params->info)); params->info.data = data; params->info.len = value->len; break; @@ -107,7 +110,7 @@ static OH_Crypto_ErrCode SetPbkdf2Param(HcfPBKDF2ParamsSpec *params, CryptoKdf_P return CRYPTO_MEMORY_ERROR; } (void)memcpy_s(data, value->len, value->data, value->len); - HcfFree(params->password.data); + HcfBlobDataClearAndFree(&(params->password)); params->password.data = data; params->password.len = value->len; break; @@ -118,7 +121,7 @@ static OH_Crypto_ErrCode SetPbkdf2Param(HcfPBKDF2ParamsSpec *params, CryptoKdf_P return CRYPTO_MEMORY_ERROR; } (void)memcpy_s(data, value->len, value->data, value->len); - HcfFree(params->salt.data); + HcfBlobDataClearAndFree(&(params->salt)); params->salt.data = data; params->salt.len = value->len; break; @@ -143,7 +146,7 @@ static OH_Crypto_ErrCode SetScryptKeyParam(HcfScryptParamsSpec *params, Crypto_D return CRYPTO_MEMORY_ERROR; } (void)memcpy_s(data, value->len, value->data, value->len); - HcfFree(params->passPhrase.data); + HcfBlobDataClearAndFree(&(params->passPhrase)); params->passPhrase.data = data; params->passPhrase.len = value->len; return CRYPTO_SUCCESS; @@ -156,7 +159,7 @@ static OH_Crypto_ErrCode SetScryptSaltParam(HcfScryptParamsSpec *params, Crypto_ return CRYPTO_MEMORY_ERROR; } (void)memcpy_s(data, value->len, value->data, value->len); - HcfFree(params->salt.data); + HcfBlobDataClearAndFree(&(params->salt)); params->salt.data = data; params->salt.len = value->len; return CRYPTO_SUCCESS; diff --git a/frameworks/native/src/crypto_mac.c b/frameworks/native/src/crypto_mac.c index 5181b0c..3d5ceae 100644 --- a/frameworks/native/src/crypto_mac.c +++ b/frameworks/native/src/crypto_mac.c @@ -30,6 +30,9 @@ typedef struct OH_CryptoMac { HcfMac *macObj; } OH_CryptoMac; +static const char *CMAC_NAME = "CMAC"; +static const char *HMAC_NAME = "HMAC"; + OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx) { if ((algoName == NULL) || (ctx == NULL)) { @@ -40,10 +43,13 @@ OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx) return CRYPTO_MEMORY_ERROR; } HcfMacParamsSpec *paramsSpec = NULL; - if (strcmp(algoName, "CMAC") == 0) { + const char *algName = NULL; + if (strcmp(algoName, CMAC_NAME) == 0) { paramsSpec = (HcfMacParamsSpec *)HcfMalloc(sizeof(HcfCmacParamsSpec), 0); - } else if (strcmp(algoName, "HMAC") == 0) { + algName = CMAC_NAME; + } else if (strcmp(algoName, HMAC_NAME) == 0) { paramsSpec = (HcfMacParamsSpec *)HcfMalloc(sizeof(HcfHmacParamsSpec), 0); + algName = HMAC_NAME; } else { HcfFree(tmpCtx); return CRYPTO_PARAMETER_CHECK_FAILED; @@ -54,13 +60,6 @@ OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx) return CRYPTO_MEMORY_ERROR; } - char *algName = (char *)HcfMalloc(strlen(algoName) + 1, 0); - if (algName == NULL) { - HcfFree(paramsSpec); - HcfFree(tmpCtx); - return CRYPTO_MEMORY_ERROR; - } - (void)memcpy_s(algName, strlen(algoName), algoName, strlen(algoName)); paramsSpec->algName = algName; tmpCtx->paramsSpec = paramsSpec; *ctx = tmpCtx; @@ -111,19 +110,15 @@ OH_Crypto_ErrCode OH_CryptoMac_SetParam(OH_CryptoMac *ctx, CryptoMac_ParamType t (value->data == NULL)) { return CRYPTO_PARAMETER_CHECK_FAILED; } + OH_Crypto_ErrCode res = CRYPTO_PARAMETER_CHECK_FAILED; if (strcmp(ctx->paramsSpec->algName, "CMAC") == 0) { - return SetCmacParam((HcfCmacParamsSpec*)(ctx->paramsSpec), type, value); + res = SetCmacParam((HcfCmacParamsSpec*)(ctx->paramsSpec), type, value); } else if (strcmp(ctx->paramsSpec->algName, "HMAC") == 0) { - return SetHmacParam((HcfHmacParamsSpec*)(ctx->paramsSpec), type, value); - } else { - return CRYPTO_PARAMETER_CHECK_FAILED; + res = SetHmacParam((HcfHmacParamsSpec*)(ctx->paramsSpec), type, value); } -} - -OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *key) -{ - if ((ctx == NULL) || (key == NULL)) { - return CRYPTO_PARAMETER_CHECK_FAILED; + + if (res != CRYPTO_SUCCESS) { + return res; } HcfMac *macObj = NULL; @@ -131,19 +126,19 @@ OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *ke if (ret != HCF_SUCCESS) { return GetOhCryptoErrCodeNew(ret); } - if (macObj->init == NULL) { - HcfObjDestroy(macObj); - return CRYPTO_PARAMETER_CHECK_FAILED; - } - ret = macObj->init(macObj, (const HcfSymKey *)key); - if (ret != HCF_SUCCESS) { - HcfObjDestroy(macObj); - return GetOhCryptoErrCodeNew(ret); - } ctx->macObj = macObj; return CRYPTO_SUCCESS; } +OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *key) +{ + if ((ctx == NULL) || (ctx->macObj == NULL) || (ctx->macObj->init == NULL) || (key == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = ctx->macObj->init(ctx->macObj, (const HcfSymKey *)key); + return GetOhCryptoErrCodeNew(ret); +} + OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob *in) { if ((ctx == NULL) || (ctx->macObj == NULL) || (ctx->macObj->update == NULL) || (in == NULL)) { @@ -183,7 +178,6 @@ static void FreeMacParams(HcfMacParamsSpec *params) HcfFree((void *)(((HcfHmacParamsSpec *)params)->mdName)); ((HcfHmacParamsSpec *)params)->mdName = NULL; } - HcfFree((void *)(params->algName)); params->algName = NULL; HcfFree(params); } diff --git a/frameworks/native/src/crypto_rand.c b/frameworks/native/src/crypto_rand.c index 713e061..2f8dd87 100644 --- a/frameworks/native/src/crypto_rand.c +++ b/frameworks/native/src/crypto_rand.c @@ -70,8 +70,5 @@ OH_Crypto_ErrCode OH_CryptoRand_SetSeed(OH_CryptoRand *ctx, Crypto_DataBlob *see void OH_CryptoRand_Destroy(OH_CryptoRand *ctx) { - if ((ctx == NULL) || (ctx->base.destroy == NULL)) { - return; - } - ctx->base.destroy((HcfObjectBase *)ctx); + HcfObjDestroy((HcfRand *)ctx); } \ No newline at end of file -- Gitee From 8339b8ee91d2207aeb32beb7d62b433a7ff3ee3e Mon Sep 17 00:00:00 2001 From: zzh593245631 Date: Fri, 16 May 2025 14:06:15 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E5=A4=A7?= =?UTF-8?q?=E7=AB=AF=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zzh --- frameworks/native/include/native_common.h | 5 +++ frameworks/native/src/asym_key.c | 15 ++++++-- frameworks/native/src/native_common.c | 45 +++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/frameworks/native/include/native_common.h b/frameworks/native/include/native_common.h index 7bfd68b..03c924c 100644 --- a/frameworks/native/include/native_common.h +++ b/frameworks/native/include/native_common.h @@ -26,6 +26,11 @@ extern "C" { OH_Crypto_ErrCode GetOhCryptoErrCode(HcfResult errCode); OH_Crypto_ErrCode GetOhCryptoErrCodeNew(HcfResult errCode); +void ReverseUint8Arr(uint8_t *data, size_t len); +int32_t bigEndianArrToInt32(const uint8_t *data, size_t len); +void Int32TobigEndianArr(int32_t value, uint8_t *data, size_t len); +int32_t bigEndianArrToInt(const uint8_t *data, size_t len); +void IntTobigEndianArr(int value, uint8_t *data, size_t len); #ifdef __cplusplus } #endif diff --git a/frameworks/native/src/asym_key.c b/frameworks/native/src/asym_key.c index fda7168..d8fb9cd 100644 --- a/frameworks/native/src/asym_key.c +++ b/frameworks/native/src/asym_key.c @@ -310,6 +310,7 @@ OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_P } value->data = (uint8_t *)returnInt; value->len = sizeof(int32_t); + ReverseUint8Arr(value->data, value->len); break; case CRYPTO_ECC_FIELD_TYPE_STR: case CRYPTO_ECC_CURVE_NAME_STR: @@ -329,6 +330,7 @@ OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_P } value->data = (uint8_t *)bigIntValue.data; value->len = (size_t)bigIntValue.len; + ReverseUint8Arr(value->data, value->len); break; } return GetOhCryptoErrCode(ret); @@ -449,6 +451,7 @@ OH_Crypto_ErrCode OH_CryptoPrivKey_GetParam(OH_CryptoPrivKey *key, CryptoAsymKey } value->data = (uint8_t *)returnInt; value->len = sizeof(int32_t); + ReverseUint8Arr(value->data, value->len); break; case CRYPTO_ECC_FIELD_TYPE_STR: case CRYPTO_ECC_CURVE_NAME_STR: @@ -468,6 +471,7 @@ OH_Crypto_ErrCode OH_CryptoPrivKey_GetParam(OH_CryptoPrivKey *key, CryptoAsymKey } value->data = (uint8_t *)bigIntValue.data; value->len = (size_t)bigIntValue.len; + ReverseUint8Arr(value->data, value->len); break; } return GetOhCryptoErrCodeNew(ret); @@ -609,6 +613,7 @@ static OH_Crypto_ErrCode SetDataBlob(uint8_t **dest, uint32_t *destLen, Crypto_D HcfFree(*dest); *dest = tmp; *destLen = value->len; + ReverseUint8Arr(*dest, *destLen); return CRYPTO_SUCCESS; } @@ -623,6 +628,7 @@ static OH_Crypto_ErrCode GetDataBlob(const uint8_t *src, uint32_t srcLen, Crypto } (void)memcpy_s(value->data, srcLen, src, srcLen); value->len = srcLen; + ReverseUint8Arr(value->data, value->len); return CRYPTO_SUCCESS; } @@ -752,6 +758,7 @@ static OH_Crypto_ErrCode SetEccField(HcfEccCommParamsSpec *spec, Crypto_DataBlob } (void)memcpy_s(field->p.data, value->len, value->data, value->len); field->p.len = value->len; + ReverseUint8Arr(field->p.data, field->p.len); spec->field = (HcfECField *)field; return CRYPTO_SUCCESS; } @@ -776,7 +783,7 @@ static OH_Crypto_ErrCode SetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKe if (value->len != sizeof(spec->h)) { return CRYPTO_PARAMETER_CHECK_FAILED; } - spec->h = *((int *)(value->data)); + spec->h = bigEndianArrToInt32(value->data, value->len); break; default: return CRYPTO_PARAMETER_CHECK_FAILED; @@ -851,7 +858,7 @@ static OH_Crypto_ErrCode SetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ if (value->len != sizeof(spec->length)) { return CRYPTO_PARAMETER_CHECK_FAILED; } - spec->length = *((int *)(value->data)); + spec->length = bigEndianArrToInt(value->data, value->len); break; default: return CRYPTO_PARAMETER_CHECK_FAILED; @@ -1221,8 +1228,8 @@ static OH_Crypto_ErrCode GetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKe if (value->data == NULL) { return CRYPTO_MEMORY_ERROR; } - (void)memcpy_s(value->data, sizeof(spec->h), &(spec->h), sizeof(spec->h)); value->len = sizeof(spec->h); + Int32TobigEndianArr(spec->h, value->data, value->len); break; default: return CRYPTO_PARAMETER_CHECK_FAILED; @@ -1298,8 +1305,8 @@ static OH_Crypto_ErrCode GetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ if (value->data == NULL) { return CRYPTO_MEMORY_ERROR; } - (void)memcpy_s(value->data, sizeof(spec->length), &(spec->length), sizeof(spec->length)); value->len = sizeof(spec->length); + IntTobigEndianArr(spec->length, value->data, value->len); break; default: return CRYPTO_PARAMETER_CHECK_FAILED; diff --git a/frameworks/native/src/native_common.c b/frameworks/native/src/native_common.c index 7123cce..4d41e7a 100644 --- a/frameworks/native/src/native_common.c +++ b/frameworks/native/src/native_common.c @@ -45,4 +45,49 @@ OH_Crypto_ErrCode GetOhCryptoErrCodeNew(HcfResult errCode) default: return CRYPTO_OPERTION_ERROR; } +} + +void ReverseUint8Arr(uint8_t *data, size_t len) +{ + for (size_t i = 0; i < len / 2; ++i) { + uint8_t temp = data[i]; + data[i] = data[len - 1 - i]; + data[len - 1 - i] = temp; + } +} + +#define NATIVE_BITS_SIZE 8 + +int32_t bigEndianArrToInt32(const uint8_t *data, size_t len) +{ + int32_t value = 0; + + for (size_t i = 0; i < len; ++i) { + value |= (int32_t)(data[i] << ((sizeof(int32_t) - 1 - i) * NATIVE_BITS_SIZE)); + } + return value; +} + +void Int32TobigEndianArr(int32_t value, uint8_t *data, size_t len) +{ + for (size_t i = 0; i < len; ++i) { + data[i] = (value >> ((sizeof(int32_t) - i - 1) * NATIVE_BITS_SIZE)) & 0xFF; + } +} + +int32_t bigEndianArrToInt(const uint8_t *data, size_t len) +{ + int value = 0; + + for (size_t i = 0; i < len; ++i) { + value |= (int)(data[i] << ((sizeof(int) - 1 - i) * NATIVE_BITS_SIZE)); + } + return value; +} + +void IntTobigEndianArr(int value, uint8_t *data, size_t len) +{ + for (size_t i = 0; i < len; ++i) { + data[i] = (value >> ((sizeof(int) - i - 1) * NATIVE_BITS_SIZE)) & 0xFF; + } } \ No newline at end of file -- Gitee