diff --git a/CryptoArchitectureKit/crypto_asym_key.h b/CryptoArchitectureKit/crypto_asym_key.h index 63a76d39d29443c884ca14ec4de1f5aaf701be35..b2bdd1b94f4a1d9b60ce913d51da9cde9dded6ae 100644 --- a/CryptoArchitectureKit/crypto_asym_key.h +++ b/CryptoArchitectureKit/crypto_asym_key.h @@ -56,6 +56,13 @@ typedef struct OH_CryptoKeyPair OH_CryptoKeyPair; */ typedef struct OH_CryptoPubKey OH_CryptoPubKey; +/** + * @brief Define the private Key structure. + * + * @since 20 + */ +typedef struct OH_CryptoPrivKey OH_CryptoPrivKey; + /** * @brief Define the asymmetric key parameter types. * @@ -227,6 +234,15 @@ void OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *keyCtx); */ OH_CryptoPubKey *OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair *keyCtx); +/** + * @brief Get the private key of the key pair. + * + * @param keyCtx Indicates the keyPair context. + * @return Return the private key context from the key pair. + * @since 20 + */ +OH_CryptoPrivKey *OH_CryptoKeyPair_GetPriKey(OH_CryptoKeyPair *keyCtx); + /** * @brief Encode the public key. * diff --git a/CryptoArchitectureKit/crypto_signature.h b/CryptoArchitectureKit/crypto_signature.h index 321b3f83311d14013dfd841a65c9c4cba8b6458a..c16d649319ee1d61d1b55aaf6691e0994e6d8f8f 100644 --- a/CryptoArchitectureKit/crypto_signature.h +++ b/CryptoArchitectureKit/crypto_signature.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Huawei Device Co., Ltd. + * Copyright (C) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -71,6 +71,13 @@ typedef enum { */ typedef struct OH_CryptoVerify OH_CryptoVerify; +/** + * @brief Defines the sign structure. + * + * @since 20 + */ +typedef struct OH_CryptoSign OH_CryptoSign; + /** * @brief Create a verify context according to the given algorithm name. * @@ -159,8 +166,8 @@ const char *OH_CryptoVerify_GetAlgoName(OH_CryptoVerify *ctx); * @brief Set the specified parameter to the verify context. * * @param ctx Indicates the verify context. - * @param type Indicates the verify signature_paramType. - * @param value Indicates the verify result. + * @param type Indicates the verify parameter type. + * @param value Indicates the input data. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. @@ -175,8 +182,8 @@ OH_Crypto_ErrCode OH_CryptoVerify_SetParam(OH_CryptoVerify *ctx, CryptoSignature * @brief Get the specified parameter from the verify context. * * @param ctx Indicates the verify context. - * @param type Indicates the verify signature_paramType. - * @param value Indicates the verify result. + * @param type Indicates the verify parameter type. + * @param value Indicates the output data. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. @@ -195,9 +202,199 @@ OH_Crypto_ErrCode OH_CryptoVerify_GetParam(OH_CryptoVerify *ctx, CryptoSignature */ void OH_CryptoVerify_Destroy(OH_CryptoVerify *ctx); +/** + * @brief Creates a sign context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name for generating the sign context. e.g. "RSA|PKCS1|SHA384", "ECC|SHA384". + * @param sign Indicates the sign context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoSign_Create(const char *algoName, OH_CryptoSign **sign); + +/** + * @brief Initializes the sign context. + * + * @param ctx Indicates the sign context. + * @param privKey Indicates the private key. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @see OH_CryptoSign_Update + * @see OH_CryptoSign_Final + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoSign_Init(OH_CryptoSign *ctx, OH_CryptoPrivKey *privKey); + +/** + * @brief Updates the data to be signed. + * + * @param ctx Indicates the sign context. + * @param in Indicates the data to be signed. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @see OH_CryptoSign_Init + * @see OH_CryptoSign_Final + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoSign_Update(OH_CryptoSign *ctx, const Crypto_DataBlob *in); + +/** + * @brief Finalizes the sign operation. + * + * @param ctx Indicates the sign context. + * @param in Indicates the data to be signed, if OH_CryptoSign_Update has been called, this parameter can be NULL. + * @param out Indicates the sign result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @see OH_CryptoSign_Init + * @see OH_CryptoSign_Update + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoSign_Final(OH_CryptoSign *ctx, const Crypto_DataBlob *in, Crypto_DataBlob *out); + +/** + * @brief Gets the algorithm name of the sign context. + * + * @param ctx Indicates the sign context. + * @return Return signature algorithm name. + * @since 20 + */ +const char *OH_CryptoSign_GetAlgoName(OH_CryptoSign *ctx); + +/** + * @brief Sets the specified parameter to the sign context. + * + * @param ctx Indicates the sign context. + * @param type Indicates the signature parameter type. + * @param value Indicates the input data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoSign_SetParam(OH_CryptoSign *ctx, CryptoSignature_ParamType type, + const Crypto_DataBlob *value); + +/** + * @brief Gets the specified parameter from the sign context. + * + * @param ctx Indicates the sign context. + * @param type Indicates the signature parameter type. + * @param value Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoSign_GetParam(OH_CryptoSign *ctx, CryptoSignature_ParamType type, Crypto_DataBlob *value); + +/** + * @brief Destroys the sign context. + * + * @param ctx Indicates the sign context. + * @since 20 + */ +void OH_CryptoSign_Destroy(OH_CryptoSign *ctx); + +/** + * @brief Defines the ECC signature spec. + * + * @since 20 + */ +typedef struct OH_CryptoEccSignatureSpec OH_CryptoEccSignatureSpec; + +/** + * @brief Creates the ECC signature spec, also support SM2 signature. + * + * @param EccSignature Indicates the ECC signature in DER format, if EccSignature parameter is NULL, + * an empty ECC signature spec will be created. + * @param spec Indicates the output ECC signature spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoEccSignatureSpec_Create(Crypto_DataBlob *EccSignature, + OH_CryptoEccSignatureSpec **spec); + +/** + * @brief Gets the r and s value from the ECC signature spec. + * + * @param spec Indicates the ECC signature spec. + * @param r Indicates the output r value. + * @param s Indicates the output s value. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoEccSignatureSpec_GetRAndS(OH_CryptoEccSignatureSpec *spec, Crypto_DataBlob *r, + Crypto_DataBlob *s); + +/** + * @brief Sets the r and s value to the ECC signature spec. + * + * @param spec Indicates the ECC signature spec. + * @param r Indicates the input r value. + * @param s Indicates the input s value. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoEccSignatureSpec_SetRAndS(OH_CryptoEccSignatureSpec *spec, Crypto_DataBlob *r, + Crypto_DataBlob *s); + +/** + * @brief Encodes the ECC signature spec to signature data in DER format. + * + * @param spec Indicates the ECC signature spec. + * @param out Indicates the output data blob. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoEccSignatureSpec_Encode(OH_CryptoEccSignatureSpec *spec, Crypto_DataBlob *out); + +/** + * @brief Destroys the ECC signature spec. + * + * @param spec Indicates the ECC signature spec. + * @since 20 + */ +void OH_CryptoEccSignatureSpec_Destroy(OH_CryptoEccSignatureSpec *spec); + + #ifdef __cplusplus } #endif /** @} */ #endif /* CRYPTO_SIGNATURE_H */ + \ No newline at end of file