diff --git a/common/src/asy_key_params.c b/common/src/asy_key_params.c index a525620dbc715fde2865ca4f592b652f27442834..7b28b258816fcf5a0beaf19cec928db6f34ea84b 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)); } diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn index 3c6b656461a1acc0b377896a78ec35f796d877f7..c76759e6c6df465d6b4e16083118f75d37e87312 100644 --- a/frameworks/native/BUILD.gn +++ b/frameworks/native/BUILD.gn @@ -39,7 +39,12 @@ ohos_shared_library("ohcrypto") { sources = [ "src/asym_key.c", + "src/crypto_asym_cipher.c", "src/crypto_common.c", + "src/crypto_kdf.c", + "src/crypto_key_agreement.c", + "src/crypto_mac.c", + "src/crypto_rand.c", "src/digest.c", "src/native_common.c", "src/signature.c", diff --git a/frameworks/native/include/native_common.h b/frameworks/native/include/native_common.h index 7bfd68b16acb743b0892990b7f20be1e70b47291..03c924c1ff14168d2f82d6913fe8bd1871e86352 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 9d9732356b83987747c7573cd06ca49d40d683b5..43b7d5df60c00187f32bcfd7d834321f9703b421 100644 --- a/frameworks/native/src/asym_key.c +++ b/frameworks/native/src/asym_key.c @@ -16,30 +16,32 @@ #include "crypto_asym_key.h" #include #include +#include #include "key_pair.h" +#include "detailed_ecc_key_params.h" +#include "detailed_dh_key_params.h" +#include "detailed_rsa_key_params.h" +#include "detailed_dsa_key_params.h" +#include "detailed_alg_25519_key_params.h" +#include "params_parser.h" +#include "ecc_key_util.h" +#include "dh_key_util.h" +#include "key_utils.h" #include "memory.h" #include "pub_key.h" +#include "pri_key.h" #include "result.h" #include "blob.h" #include "object_base.h" #include "native_common.h" +#include "crypto_common.h" #include "big_integer.h" #include "asy_key_generator.h" 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 { @@ -63,28 +65,97 @@ typedef struct OH_CryptoPubKey { HcfResult (*getEncodedDer)(const HcfPubKey *self, const char *format, HcfBlob *returnBlob); } OH_CryptoPubKey; +typedef struct OH_CryptoPrivKey { + HcfKey base; + + HcfResult (*getAsyKeySpecBigInteger)(const HcfPriKey *self, const AsyKeySpecItem item, + HcfBigInteger *returnBigInteger); + + HcfResult (*getAsyKeySpecString)(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString); + + HcfResult (*getAsyKeySpecInt)(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt); + + HcfResult (*getEncodedDer)(const HcfPriKey *self, const char *format, HcfBlob *returnBlob); + + HcfResult (*getEncodedPem)(const HcfPriKey *self, HcfParamsSpec *params, const char *format, char **returnString); + + void (*clearMem)(HcfPriKey *self); +} OH_CryptoPrivKey; + +typedef struct OH_CryptoPrivKeyEncodingParams { + HcfParamsSpec base; + + char *password; + + char *cipher; +} OH_CryptoPrivKeyEncodingParams; + +typedef struct OH_CryptoAsymKeySpec { + char *algName; + HcfAsyKeySpecType specType; +} OH_CryptoAsymKeySpec; + +typedef struct OH_CryptoAsymKeyGeneratorWithSpec { + HcfAsyKeyGeneratorBySpec *generator; + HcfAsyKeySpecType specType; +} OH_CryptoAsymKeyGeneratorWithSpec; + +typedef struct OH_CryptoEcPoint { + HcfPoint pointBase; + char *curveName; +} OH_CryptoEcPoint; + OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Create(const char *algoName, OH_CryptoAsymKeyGenerator **ctx) { 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; @@ -92,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; @@ -108,26 +180,55 @@ 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) { - if ((keyCtx == NULL) || (keyCtx->base.destroy == NULL)) { + if (keyCtx == NULL) { + return; + } + if (keyCtx->base.destroy != NULL) { + keyCtx->base.destroy((HcfObjectBase *)keyCtx); + return; + } + if ((keyCtx->priKey != NULL) && (keyCtx->priKey->base.base.destroy != NULL)) { + HcfObjDestroy(keyCtx->priKey); + keyCtx->priKey = NULL; + HcfFree(keyCtx); + return; + } + if ((keyCtx->pubKey != NULL) && (keyCtx->pubKey->base.base.destroy != NULL)) { + HcfObjDestroy(keyCtx->pubKey); + keyCtx->pubKey = NULL; + HcfFree(keyCtx); return; } - keyCtx->base.destroy((HcfObjectBase *)keyCtx); } OH_CryptoPubKey *OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair *keyCtx) @@ -232,3 +333,1317 @@ OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_P } return GetOhCryptoErrCode(ret); } + +OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_Create(OH_CryptoPrivKeyEncodingParams **ctx) +{ + if (ctx == NULL) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + *ctx = (OH_CryptoPrivKeyEncodingParams *)HcfMalloc(sizeof(OH_CryptoPrivKeyEncodingParams), 0); + if (*ctx == NULL) { + return CRYPTO_MEMORY_ERROR; + } + return CRYPTO_SUCCESS; +} + +OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_SetParam(OH_CryptoPrivKeyEncodingParams *ctx, + CryptoPrivKeyEncoding_ParamType type, Crypto_DataBlob *value) +{ + if ((ctx == NULL) || (value == NULL) || (value->data == NULL) || (value->len == 0)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + char *data = (char *)HcfMalloc(value->len + 1, 0); + if (data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(data, value->len, value->data, value->len); + switch (type) { + case CRYPTO_PRIVATE_KEY_ENCODING_PASSWORD_STR: + HcfFree(ctx->password); + ctx->password = data; + break; + case CRYPTO_PRIVATE_KEY_ENCODING_SYMMETRIC_CIPHER_STR: + HcfFree(ctx->cipher); + ctx->cipher = data; + break; + default: + HcfFree(data); + return CRYPTO_PARAMETER_CHECK_FAILED; + } + return CRYPTO_SUCCESS; +} + +void OH_CryptoPrivKeyEncodingParams_Destroy(OH_CryptoPrivKeyEncodingParams *ctx) +{ + if (ctx == NULL) { + return; + } + HcfFree(ctx->password); + ctx->password = NULL; + HcfFree(ctx->cipher); + ctx->cipher = NULL; + HcfFree(ctx); +} + +OH_Crypto_ErrCode OH_CryptoPrivKey_Encode(OH_CryptoPrivKey *key, Crypto_EncodingType type, + const char *encodingStandard, OH_CryptoPrivKeyEncodingParams *params, Crypto_DataBlob *out) +{ + if ((key == NULL) || (out == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = HCF_SUCCESS; + char *pemStr = NULL; + switch (type) { + case CRYPTO_PEM: + if (key->getEncodedPem == NULL) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + if (params == NULL) { + ret = key->getEncodedPem((HcfPriKey *)key, NULL, encodingStandard, &pemStr); + } else { + ret = key->getEncodedPem((HcfPriKey *)key, (HcfParamsSpec *)params, encodingStandard, &pemStr); + } + + ret = key->getEncodedPem((HcfPriKey *)key, (HcfParamsSpec *)params, encodingStandard, &pemStr); + if (ret != HCF_SUCCESS) { + break; + } + out->data = (uint8_t *)pemStr; + out->len = strlen(pemStr); + break; + case CRYPTO_DER: + if (encodingStandard != NULL) { + ret = key->getEncodedDer == NULL ? HCF_INVALID_PARAMS : + key->getEncodedDer((HcfPriKey *)key, encodingStandard, (HcfBlob *)out); + break; + } else { + ret = key->base.getEncoded == NULL ? HCF_INVALID_PARAMS + : key->base.getEncoded((HcfKey *)key, (HcfBlob *)out); + break; + } + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } + + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoPrivKey_GetParam(OH_CryptoPrivKey *key, CryptoAsymKey_ParamType item, + Crypto_DataBlob *value) +{ + if ((key == NULL) || (value == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = HCF_SUCCESS; + int32_t *returnInt = NULL; + char *returnStr = NULL; + HcfBigInteger bigIntValue = {0}; + switch (item) { + case CRYPTO_DH_L_INT: + case CRYPTO_ECC_H_INT: + case CRYPTO_ECC_FIELD_SIZE_INT: + returnInt = (int32_t *)HcfMalloc(sizeof(int32_t), 0); + if (returnInt == NULL) { + ret = HCF_ERR_MALLOC; + break; + } + ret = key->getAsyKeySpecInt == NULL ? HCF_INVALID_PARAMS : + key->getAsyKeySpecInt((HcfPriKey *)key, (AsyKeySpecItem)item, returnInt); + if (ret != HCF_SUCCESS) { + HcfFree(returnInt); + break; + } + 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: + ret = key->getAsyKeySpecString == NULL ? HCF_INVALID_PARAMS : + key->getAsyKeySpecString((HcfPriKey *)key, (AsyKeySpecItem)item, &returnStr); + if (ret != HCF_SUCCESS) { + break; + } + value->data = (uint8_t *)returnStr; + value->len = strlen(returnStr); + break; + default: + ret = key->getAsyKeySpecBigInteger == NULL ? HCF_INVALID_PARAMS : + key->getAsyKeySpecBigInteger((HcfPriKey *)key, (AsyKeySpecItem)item, &bigIntValue); + if (ret != HCF_SUCCESS) { + break; + } + value->data = (uint8_t *)bigIntValue.data; + value->len = (size_t)bigIntValue.len; + ReverseUint8Arr(value->data, value->len); + break; + } + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenEcCommonParamsSpec(const char *curveName, OH_CryptoAsymKeySpec **spec) +{ + if ((curveName == NULL) || (spec == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + + HcfResult ret = HcfEccKeyUtilCreate(curveName, (HcfEccCommParamsSpec **)spec); + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenDhCommonParamsSpec(int pLen, int skLen, OH_CryptoAsymKeySpec **spec) +{ + if (spec == NULL) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = HcfDhKeyUtilCreate(pLen, skLen, (HcfDhCommParamsSpec **)spec); + return GetOhCryptoErrCodeNew(ret); +} + +typedef struct { + CryptoAsymKeySpec_Type type; + uint32_t memSize; +} OH_CryptoAsymKeySpecInfo; + +typedef struct { + HcfAlgValue algo; + OH_CryptoAsymKeySpecInfo *specInfo; + uint32_t specInfoSize; +} OH_CryptoAsymKeySpecInfoMap; + +static OH_CryptoAsymKeySpecInfo g_rsaSpecInfo[] = { + {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfRsaPubKeyParamsSpec)}, + {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfRsaKeyPairParamsSpec)}, +}; + +static OH_CryptoAsymKeySpecInfo g_dsaSpecInfo[] = { + {CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, sizeof(HcfDsaCommParamsSpec)}, + {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfDsaPubKeyParamsSpec)}, + {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfDsaKeyPairParamsSpec)}, +}; + +static OH_CryptoAsymKeySpecInfo g_eccSpecInfo[] = { + {CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, sizeof(HcfEccCommParamsSpec)}, + {CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, sizeof(HcfEccPriKeyParamsSpec)}, + {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfEccPubKeyParamsSpec)}, + {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfEccKeyPairParamsSpec)}, +}; + +static OH_CryptoAsymKeySpecInfo g_dhSpecInfo[] = { + {CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, sizeof(HcfDhCommParamsSpec)}, + {CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, sizeof(HcfDhPriKeyParamsSpec)}, + {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfDhPubKeyParamsSpec)}, + {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfDhKeyPairParamsSpec)}, +}; + +static OH_CryptoAsymKeySpecInfo g_alg25519SpecInfo[] = { + {CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, sizeof(HcfAlg25519PriKeyParamsSpec)}, + {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfAlg25519PubKeyParamsSpec)}, + {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfAlg25519KeyPairParamsSpec)}, +}; + +static OH_CryptoAsymKeySpecInfoMap g_asymKeySpecInfoMap[] = { + {HCF_ALG_RSA, g_rsaSpecInfo, sizeof(g_rsaSpecInfo) / sizeof(g_rsaSpecInfo[0])}, + {HCF_ALG_DSA, g_dsaSpecInfo, sizeof(g_dsaSpecInfo) / sizeof(g_dsaSpecInfo[0])}, + {HCF_ALG_SM2, g_eccSpecInfo, sizeof(g_eccSpecInfo) / sizeof(g_eccSpecInfo[0])}, + {HCF_ALG_ECC, g_eccSpecInfo, sizeof(g_eccSpecInfo) / sizeof(g_eccSpecInfo[0])}, + {HCF_ALG_DH, g_dhSpecInfo, sizeof(g_dhSpecInfo) / sizeof(g_dhSpecInfo[0])}, + {HCF_ALG_ED25519, g_alg25519SpecInfo, sizeof(g_alg25519SpecInfo) / sizeof(g_alg25519SpecInfo[0])}, + {HCF_ALG_X25519, g_alg25519SpecInfo, sizeof(g_alg25519SpecInfo) / sizeof(g_alg25519SpecInfo[0])}, +}; + +static OH_Crypto_ErrCode CreateAsymKeySpec(const char *algoName, CryptoAsymKeySpec_Type type, uint32_t memSize, + OH_CryptoAsymKeySpec **spec) +{ + OH_CryptoAsymKeySpec *tmpSpec = (OH_CryptoAsymKeySpec *)HcfMalloc(memSize, 0); + if (tmpSpec == NULL) { + return CRYPTO_MEMORY_ERROR; + } + char *algName = (char *)HcfMalloc(strlen(algoName) + 1, 0); + if (algName == NULL) { + HcfFree(tmpSpec); + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(algName, strlen(algoName), algoName, strlen(algoName)); + tmpSpec->specType = (HcfAsyKeySpecType)type; + tmpSpec->algName = algName; + *spec = tmpSpec; + return CRYPTO_SUCCESS; +} + +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 NULL; +} + +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; + } + const OH_CryptoAsymKeySpecInfoMap *infoMap = FindAsymKeySpecInfoMapByAlgoName(algoName); + if (infoMap == NULL) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + 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) +{ + if (value == NULL || value->data == NULL || value->len == 0) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + uint8_t *tmp = (uint8_t *)HcfMalloc(value->len, 0); + if (tmp == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(tmp, value->len, value->data, value->len); + HcfFree(*dest); + *dest = tmp; + *destLen = value->len; + ReverseUint8Arr(*dest, *destLen); + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode GetDataBlob(const uint8_t *src, uint32_t srcLen, Crypto_DataBlob *value) +{ + if (src == NULL || srcLen == 0) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + value->data = (uint8_t *)HcfMalloc(srcLen, 0); + if (value->data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(value->data, srcLen, src, srcLen); + value->len = srcLen; + ReverseUint8Arr(value->data, value->len); + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode SetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DSA_P_DATABLOB: + return SetDataBlob(&(spec->p.data), &(spec->p.len), value); + case CRYPTO_DSA_Q_DATABLOB: + return SetDataBlob(&(spec->q.data), &(spec->q.len), value); + case CRYPTO_DSA_G_DATABLOB: + return SetDataBlob(&(spec->g.data), &(spec->g.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DSA_PK_DATABLOB: + return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DSA_SK_DATABLOB: + return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); + case CRYPTO_DSA_PK_DATABLOB: + return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + if (SetDsaCommSpec((HcfDsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { + return CRYPTO_SUCCESS; + } + switch (spec->specType) { + case HCF_PUBLIC_KEY_SPEC: + return SetDsaPubKeySpec((HcfDsaPubKeyParamsSpec *)spec, type, value); + case HCF_KEY_PAIR_SPEC: + return SetDsaKeyPairSpec((HcfDsaKeyPairParamsSpec *)spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_RSA_N_DATABLOB: + return SetDataBlob(&(spec->n.data), &(spec->n.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_RSA_E_DATABLOB: + return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_RSA_D_DATABLOB: + return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); + case CRYPTO_RSA_E_DATABLOB: + return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetRsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + if (SetRsaCommSpec((HcfRsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { + return CRYPTO_SUCCESS; + } + switch (spec->specType) { + case HCF_PUBLIC_KEY_SPEC: + return SetRsaPubKeySpec((HcfRsaPubKeyParamsSpec *)spec, type, value); + case HCF_KEY_PAIR_SPEC: + return SetRsaKeyPairSpec((HcfRsaKeyPairParamsSpec *)spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetEccField(HcfEccCommParamsSpec *spec, Crypto_DataBlob *value) +{ + HcfECFieldFp *field = (HcfECFieldFp *)HcfMalloc(sizeof(HcfECFieldFp), 0); + if (field == NULL) { + return CRYPTO_MEMORY_ERROR; + } + char *fieldType = "Fp"; + size_t fieldTypeLen = strlen(fieldType); + field->base.fieldType = (char *)HcfMalloc(fieldTypeLen + 1, 0); + if (field->base.fieldType == NULL) { + HcfFree(field); + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(field->base.fieldType, fieldTypeLen, fieldType, fieldTypeLen); + field->p.data = (uint8_t *)HcfMalloc(value->len, 0); + if (field->p.data == NULL) { + HcfFree(field->base.fieldType); + HcfFree(field); + return CRYPTO_MEMORY_ERROR; + } + (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; +} + +static OH_Crypto_ErrCode SetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ECC_FP_P_DATABLOB: + return SetEccField(spec, value); + case CRYPTO_ECC_A_DATABLOB: + return SetDataBlob(&(spec->a.data), &(spec->a.len), value); + case CRYPTO_ECC_B_DATABLOB: + return SetDataBlob(&(spec->b.data), &(spec->b.len), value); + case CRYPTO_ECC_G_X_DATABLOB: + return SetDataBlob(&(spec->g.x.data), &(spec->g.x.len), value); + case CRYPTO_ECC_G_Y_DATABLOB: + return SetDataBlob(&(spec->g.y.data), &(spec->g.y.len), value); + case CRYPTO_ECC_N_DATABLOB: + return SetDataBlob(&(spec->n.data), &(spec->n.len), value); + case CRYPTO_ECC_H_INT: + if (value->len != sizeof(spec->h)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + spec->h = bigEndianArrToInt32(value->data, value->len); + break; + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode SetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ECC_SK_DATABLOB: + return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + 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: + return SetDataBlob(&(spec->pk.y.data), &(spec->pk.y.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ECC_SK_DATABLOB: + return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); + case CRYPTO_ECC_PK_X_DATABLOB: + return SetDataBlob(&(spec->pk.x.data), &(spec->pk.x.len), value); + case CRYPTO_ECC_PK_Y_DATABLOB: + return SetDataBlob(&(spec->pk.y.data), &(spec->pk.y.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + if (SetEccCommSpec((HcfEccCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { + return CRYPTO_SUCCESS; + } + switch (spec->specType) { + case HCF_PRIVATE_KEY_SPEC: + return SetEccPriSpec((HcfEccPriKeyParamsSpec *)spec, type, value); + case HCF_PUBLIC_KEY_SPEC: + return SetEccPubKeySpec((HcfEccPubKeyParamsSpec *)spec, type, value); + case HCF_KEY_PAIR_SPEC: + return SetEccKeyPairSpec((HcfEccKeyPairParamsSpec *)spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DH_P_DATABLOB: + return SetDataBlob(&(spec->p.data), &(spec->p.len), value); + case CRYPTO_DH_G_DATABLOB: + return SetDataBlob(&(spec->g.data), &(spec->g.len), value); + case CRYPTO_DH_L_INT: + if (value->len != sizeof(spec->length)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + spec->length = bigEndianArrToInt(value->data, value->len); + break; + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode SetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DH_SK_DATABLOB: + return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DH_PK_DATABLOB: + return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DH_SK_DATABLOB: + return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); + case CRYPTO_DH_PK_DATABLOB: + return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + if (SetDhCommSpec((HcfDhCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { + return CRYPTO_SUCCESS; + } + switch (spec->specType) { + case HCF_PRIVATE_KEY_SPEC: + return SetDhPriSpec((HcfDhPriKeyParamsSpec *)spec, type, value); + case HCF_PUBLIC_KEY_SPEC: + return SetDhPubKeySpec((HcfDhPubKeyParamsSpec *)spec, type, value); + case HCF_KEY_PAIR_SPEC: + return SetDhKeyPairSpec((HcfDhKeyPairParamsSpec *)spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ED25519_SK_DATABLOB: + case CRYPTO_X25519_SK_DATABLOB: + return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ED25519_PK_DATABLOB: + case CRYPTO_X25519_PK_DATABLOB: + return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ED25519_SK_DATABLOB: + case CRYPTO_X25519_SK_DATABLOB: + return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); + case CRYPTO_ED25519_PK_DATABLOB: + case CRYPTO_X25519_PK_DATABLOB: + return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (spec->specType) { + case HCF_PRIVATE_KEY_SPEC: + return SetAlg25519PriSpec((HcfAlg25519PriKeyParamsSpec *)spec, type, value); + case HCF_PUBLIC_KEY_SPEC: + return SetAlg25519PubKeySpec((HcfAlg25519PubKeyParamsSpec *)spec, type, value); + case HCF_KEY_PAIR_SPEC: + return SetAlg25519KeyPairSpec((HcfAlg25519KeyPairParamsSpec *)spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + if ((spec == NULL) || (value == NULL) || (value->data == NULL) || (value->len == 0)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + + HcfAsyKeyGenParams params = { 0 }; + HcfResult ret = ParseAlgNameToParams(spec->algName, ¶ms); + if (ret != HCF_SUCCESS) { + return GetOhCryptoErrCodeNew(ret); + } + + switch (params.algo) { + case HCF_ALG_DSA: + return SetDsaSpec(spec, type, value); + case HCF_ALG_RSA: + return SetRsaSpec(spec, type, value); + case HCF_ALG_ECC: + case HCF_ALG_SM2: + return SetEccSpec(spec, type, value); + case HCF_ALG_DH: + return SetDhSpec(spec, type, value); + case HCF_ALG_ED25519: + case HCF_ALG_X25519: + return SetAlg25519Spec(spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +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 = {}; + HcfResult ret = CopyEccCommonSpec(commonParamsSpec, &eccCommParamsSpec); + if (ret != HCF_SUCCESS) { + return GetOhCryptoErrCodeNew(ret); + } + spec->field = eccCommParamsSpec.field; + spec->field->fieldType = eccCommParamsSpec.field->fieldType; + ((HcfECFieldFp *)(spec->field))->p.data = ((HcfECFieldFp *)(eccCommParamsSpec.field))->p.data; + ((HcfECFieldFp *)(spec->field))->p.len = ((HcfECFieldFp *)(eccCommParamsSpec.field))->p.len; + spec->a.data = eccCommParamsSpec.a.data; + spec->a.len = eccCommParamsSpec.a.len; + spec->b.data = eccCommParamsSpec.b.data; + spec->b.len = eccCommParamsSpec.b.len; + spec->g.x.data = eccCommParamsSpec.g.x.data; + spec->g.x.len = eccCommParamsSpec.g.x.len; + spec->g.y.data = eccCommParamsSpec.g.y.data; + spec->g.y.len = eccCommParamsSpec.g.y.len; + spec->n.data = eccCommParamsSpec.n.data; + spec->n.len = eccCommParamsSpec.n.len; + spec->h = eccCommParamsSpec.h; + HcfFree(eccCommParamsSpec.base.algName); + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode SetDhCommonSpec(HcfDhCommParamsSpec *commonParamsSpec, HcfDhCommParamsSpec *spec) +{ + HcfDhCommParamsSpec dhCommParamsSpec = {}; + HcfResult ret = CopyDhCommonSpec(commonParamsSpec, &dhCommParamsSpec); + if (ret != HCF_SUCCESS) { + return GetOhCryptoErrCodeNew(ret); + } + spec->p.data = dhCommParamsSpec.p.data; + spec->p.len = dhCommParamsSpec.p.len; + spec->g.data = dhCommParamsSpec.g.data; + spec->g.len = dhCommParamsSpec.g.len; + spec->length = dhCommParamsSpec.length; + HcfFree(dhCommParamsSpec.base.algName); + return CRYPTO_SUCCESS; +} + +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec *spec, + OH_CryptoAsymKeySpec *commonParamsSpec) +{ + if ((spec == NULL) || (commonParamsSpec == NULL) || (commonParamsSpec->specType != HCF_COMMON_PARAMS_SPEC)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfAsyKeyGenParams params = { 0 }; + HcfResult ret = ParseAlgNameToParams(spec->algName, ¶ms); + if (ret != HCF_SUCCESS) { + return GetOhCryptoErrCodeNew(ret); + } + + 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); + case HCF_ALG_DH: + return SetDhCommonSpec((HcfDhCommParamsSpec *)commonParamsSpec, (HcfDhCommParamsSpec *)spec); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DSA_P_DATABLOB: + return GetDataBlob(spec->p.data, spec->p.len, value); + case CRYPTO_DSA_Q_DATABLOB: + return GetDataBlob(spec->q.data, spec->q.len, value); + case CRYPTO_DSA_G_DATABLOB: + return GetDataBlob(spec->g.data, spec->g.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DSA_PK_DATABLOB: + return GetDataBlob(spec->pk.data, spec->pk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DSA_SK_DATABLOB: + return GetDataBlob(spec->sk.data, spec->sk.len, value); + case CRYPTO_DSA_PK_DATABLOB: + return GetDataBlob(spec->pk.data, spec->pk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + if (GetDsaCommSpec((HcfDsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { + return CRYPTO_SUCCESS; + } + switch (spec->specType) { + case HCF_PUBLIC_KEY_SPEC: + return GetDsaPubKeySpec((HcfDsaPubKeyParamsSpec *)spec, type, value); + case HCF_KEY_PAIR_SPEC: + return GetDsaKeyPairSpec((HcfDsaKeyPairParamsSpec *)spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_RSA_N_DATABLOB: + return GetDataBlob(spec->n.data, spec->n.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_RSA_E_DATABLOB: + return GetDataBlob(spec->pk.data, spec->pk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_RSA_D_DATABLOB: + return GetDataBlob(spec->sk.data, spec->sk.len, value); + case CRYPTO_RSA_E_DATABLOB: + return GetDataBlob(spec->pk.data, spec->pk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetRsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + if (GetRsaCommSpec((HcfRsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { + return CRYPTO_SUCCESS; + } + switch (spec->specType) { + case HCF_PUBLIC_KEY_SPEC: + return GetRsaPubKeySpec((HcfRsaPubKeyParamsSpec *)spec, type, value); + case HCF_KEY_PAIR_SPEC: + return GetRsaKeyPairSpec((HcfRsaKeyPairParamsSpec *)spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetEccField(HcfEccCommParamsSpec *spec, Crypto_DataBlob *value) +{ + if ((spec->field == NULL) || (((HcfECFieldFp *)(spec->field))->p.data == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + 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) +{ + switch (type) { + case CRYPTO_ECC_FP_P_DATABLOB: + return GetEccField(spec, value); + case CRYPTO_ECC_A_DATABLOB: + return GetDataBlob(spec->a.data, spec->a.len, value); + case CRYPTO_ECC_B_DATABLOB: + return GetDataBlob(spec->b.data, spec->b.len, value); + case CRYPTO_ECC_G_X_DATABLOB: + return GetDataBlob(spec->g.x.data, spec->g.x.len, value); + case CRYPTO_ECC_G_Y_DATABLOB: + return GetDataBlob(spec->g.y.data, spec->g.y.len, value); + case CRYPTO_ECC_N_DATABLOB: + return GetDataBlob(spec->n.data, spec->n.len, value); + case CRYPTO_ECC_H_INT: + value->data = (uint8_t *)HcfMalloc(sizeof(spec->h), 0); + if (value->data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + value->len = sizeof(spec->h); + Int32TobigEndianArr(spec->h, value->data, value->len); + break; + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode GetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ECC_SK_DATABLOB: + return GetDataBlob(spec->sk.data, spec->sk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + 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: + return GetDataBlob(spec->pk.y.data, spec->pk.y.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ECC_SK_DATABLOB: + return GetDataBlob(spec->sk.data, spec->sk.len, value); + case CRYPTO_ECC_PK_X_DATABLOB: + return GetDataBlob(spec->pk.x.data, spec->pk.x.len, value); + case CRYPTO_ECC_PK_Y_DATABLOB: + return GetDataBlob(spec->pk.y.data, spec->pk.y.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + if (GetEccCommSpec((HcfEccCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { + return CRYPTO_SUCCESS; + } + switch (spec->specType) { + case HCF_PRIVATE_KEY_SPEC: + return GetEccPriSpec((HcfEccPriKeyParamsSpec *)spec, type, value); + case HCF_PUBLIC_KEY_SPEC: + return GetEccPubKeySpec((HcfEccPubKeyParamsSpec *)spec, type, value); + case HCF_KEY_PAIR_SPEC: + return GetEccKeyPairSpec((HcfEccKeyPairParamsSpec *)spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DH_P_DATABLOB: + return GetDataBlob(spec->p.data, spec->p.len, value); + case CRYPTO_DH_G_DATABLOB: + return GetDataBlob(spec->g.data, spec->g.len, value); + case CRYPTO_DH_L_INT: + value->data = (uint8_t *)HcfMalloc(sizeof(spec->length), 0); + if (value->data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + value->len = sizeof(spec->length); + IntTobigEndianArr(spec->length, value->data, value->len); + break; + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode GetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DH_SK_DATABLOB: + return GetDataBlob(spec->sk.data, spec->sk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DH_PK_DATABLOB: + return GetDataBlob(spec->pk.data, spec->pk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_DH_SK_DATABLOB: + return GetDataBlob(spec->sk.data, spec->sk.len, value); + case CRYPTO_DH_PK_DATABLOB: + return GetDataBlob(spec->pk.data, spec->pk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +{ + if (GetDhCommSpec((HcfDhCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { + return CRYPTO_SUCCESS; + } + switch (spec->specType) { + case HCF_PRIVATE_KEY_SPEC: + return GetDhPriSpec((HcfDhPriKeyParamsSpec *)spec, type, value); + case HCF_PUBLIC_KEY_SPEC: + return GetDhPubKeySpec((HcfDhPubKeyParamsSpec *)spec, type, value); + case HCF_KEY_PAIR_SPEC: + return GetDhKeyPairSpec((HcfDhKeyPairParamsSpec *)spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ED25519_SK_DATABLOB: + case CRYPTO_X25519_SK_DATABLOB: + return GetDataBlob(spec->sk.data, spec->sk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ED25519_PK_DATABLOB: + case CRYPTO_X25519_PK_DATABLOB: + return GetDataBlob(spec->pk.data, spec->pk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_ED25519_SK_DATABLOB: + case CRYPTO_X25519_SK_DATABLOB: + return GetDataBlob(spec->sk.data, spec->sk.len, value); + case CRYPTO_ED25519_PK_DATABLOB: + case CRYPTO_X25519_PK_DATABLOB: + return GetDataBlob(spec->pk.data, spec->pk.len, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode GetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + switch (spec->specType) { + case HCF_PRIVATE_KEY_SPEC: + return GetAlg25519PriSpec((HcfAlg25519PriKeyParamsSpec *)spec, type, value); + case HCF_PUBLIC_KEY_SPEC: + return GetAlg25519PubKeySpec((HcfAlg25519PubKeyParamsSpec *)spec, type, value); + case HCF_KEY_PAIR_SPEC: + return GetAlg25519KeyPairSpec((HcfAlg25519KeyPairParamsSpec *)spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) +{ + if ((spec == NULL) || (value == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + + HcfAsyKeyGenParams params = { 0 }; + HcfResult ret = ParseAlgNameToParams(spec->algName, ¶ms); + if (ret != HCF_SUCCESS) { + return GetOhCryptoErrCodeNew(ret); + } + + switch (params.algo) { + case HCF_ALG_DSA: + return GetDsaSpec(spec, type, value); + case HCF_ALG_RSA: + return GetRsaSpec(spec, type, value); + case HCF_ALG_ECC: + case HCF_ALG_SM2: + return GetEccSpec(spec, type, value); + case HCF_ALG_DH: + return GetDhSpec(spec, type, value); + case HCF_ALG_ED25519: + case HCF_ALG_X25519: + return GetAlg25519Spec(spec, type, value); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +void OH_CryptoAsymKeySpec_Destroy(OH_CryptoAsymKeySpec *spec) +{ + if (spec == NULL) { + return; + } + FreeAsyKeySpec((HcfAsyKeyParamsSpec *)spec); +} + +OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorWithSpec_Create(OH_CryptoAsymKeySpec *keySpec, + OH_CryptoAsymKeyGeneratorWithSpec **generator) +{ + if ((keySpec == NULL) || (generator == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + *generator = (OH_CryptoAsymKeyGeneratorWithSpec *)HcfMalloc(sizeof(OH_CryptoAsymKeyGeneratorWithSpec), 0); + if (*generator == NULL) { + return CRYPTO_MEMORY_ERROR; + } + HcfResult ret = HcfAsyKeyGeneratorBySpecCreate((HcfAsyKeyParamsSpec *)keySpec, &((*generator)->generator)); + if (ret != HCF_SUCCESS) { + HcfFree(*generator); + *generator = NULL; + return GetOhCryptoErrCodeNew(ret); + } + (*generator)->specType = keySpec->specType; + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode GenPriKeyPair(HcfAsyKeyGeneratorBySpec *generator, OH_CryptoKeyPair **keyPair) +{ + HcfPriKey *priKey = NULL; + if (generator->generatePriKey == NULL) { + return CRYPTO_NOT_SUPPORTED; + } + HcfResult ret = generator->generatePriKey(generator, &priKey); + if (ret != HCF_SUCCESS) { + return GetOhCryptoErrCodeNew(ret); + } + *keyPair = (OH_CryptoKeyPair *)HcfMalloc(sizeof(OH_CryptoKeyPair), 0); + if (*keyPair == NULL) { + HcfFree(priKey); + return CRYPTO_MEMORY_ERROR; + } + + (*keyPair)->priKey = priKey; + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode GenPubKeyPair(HcfAsyKeyGeneratorBySpec *generator, OH_CryptoKeyPair **keyPair) +{ + HcfPubKey *pubKey = NULL; + if (generator->generatePubKey == NULL) { + return CRYPTO_NOT_SUPPORTED; + } + HcfResult ret = generator->generatePubKey(generator, &pubKey); + if (ret != HCF_SUCCESS) { + return GetOhCryptoErrCodeNew(ret); + } + *keyPair = (OH_CryptoKeyPair *)HcfMalloc(sizeof(OH_CryptoKeyPair), 0); + if (*keyPair == NULL) { + HcfFree(pubKey); + return CRYPTO_MEMORY_ERROR; + } + (*keyPair)->pubKey = pubKey; + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode GenKeyPair(HcfAsyKeyGeneratorBySpec *generator, OH_CryptoKeyPair **keyPair) +{ + if (generator->generateKeyPair == NULL) { + return CRYPTO_NOT_SUPPORTED; + } + HcfResult ret = generator->generateKeyPair(generator, (HcfKeyPair **)keyPair); + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorWithSpec_GenKeyPair(OH_CryptoAsymKeyGeneratorWithSpec *generator, + OH_CryptoKeyPair **keyPair) +{ + if ((generator == NULL) || (generator->generator == NULL) || (keyPair == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + switch (generator->specType) { + case HCF_PRIVATE_KEY_SPEC: + return GenPriKeyPair(generator->generator, keyPair); + case HCF_PUBLIC_KEY_SPEC: + return GenPubKeyPair(generator->generator, keyPair); + case HCF_KEY_PAIR_SPEC: + case HCF_COMMON_PARAMS_SPEC: + return GenKeyPair(generator->generator, keyPair); + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +void OH_CryptoAsymKeyGeneratorWithSpec_Destroy(OH_CryptoAsymKeyGeneratorWithSpec *generator) +{ + if (generator == NULL) { + return; + } + HcfObjDestroy(generator->generator); + generator->generator = NULL; + HcfFree(generator); +} + + +OH_Crypto_ErrCode OH_CryptoEcPoint_Create(const char *curveName, Crypto_DataBlob *ecKeyData, OH_CryptoEcPoint **point) +{ + if ((curveName == NULL) || (point == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + *point = (OH_CryptoEcPoint*)HcfMalloc(sizeof(OH_CryptoEcPoint), 0); + if (*point == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (*point)->curveName = (char *)HcfMalloc(strlen(curveName) + 1, 0); + if ((*point)->curveName == NULL) { + HcfFree(*point); + *point = NULL; + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s((*point)->curveName, strlen(curveName), curveName, strlen(curveName)); + if (ecKeyData == NULL) { + return CRYPTO_SUCCESS; + } + HcfResult ret = HcfConvertPoint(curveName, (HcfBlob *)ecKeyData, &((*point)->pointBase)); + if (ret != HCF_SUCCESS) { + HcfFree(*point); + *point = NULL; + } + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoEcPoint_GetCoordinate(OH_CryptoEcPoint *point, Crypto_DataBlob *x, Crypto_DataBlob *y) +{ + if ((point == NULL) || (x == NULL) || (y == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfPoint dPoint = {}; + HcfResult ret = CopyPoint(&(point->pointBase), &dPoint); + if (ret != HCF_SUCCESS) { + return GetOhCryptoErrCodeNew(ret); + } + x->data = dPoint.x.data; + y->data = dPoint.y.data; + x->len = dPoint.x.len; + y->len = dPoint.y.len; + return CRYPTO_SUCCESS; +} + +OH_Crypto_ErrCode OH_CryptoEcPoint_SetCoordinate(OH_CryptoEcPoint *point, Crypto_DataBlob *x, Crypto_DataBlob *y) +{ + if ((point == NULL) || (x == NULL) || (y == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfPoint sPoint = {}; + sPoint.x.data = x->data; + sPoint.x.len = x->len; + sPoint.y.data = y->data; + sPoint.y.len = y->len; + HcfPoint dPoint = {}; + HcfResult ret = CopyPoint(&sPoint, &dPoint); + if (ret != HCF_SUCCESS) { + return GetOhCryptoErrCodeNew(ret); + } + HcfFree(point->pointBase.x.data); + HcfFree(point->pointBase.y.data); + point->pointBase.x.data = dPoint.x.data; + point->pointBase.x.len = dPoint.x.len; + point->pointBase.y.data = dPoint.y.data; + point->pointBase.y.len = dPoint.y.len; + return CRYPTO_SUCCESS; +} + +OH_Crypto_ErrCode OH_CryptoEcPoint_Encode(OH_CryptoEcPoint *point, const char *format, Crypto_DataBlob *out) +{ + if ((point == NULL) || (format == NULL) || (out == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = HcfGetEncodedPoint(point->curveName, &(point->pointBase), format, (HcfBlob *)out); + return GetOhCryptoErrCodeNew(ret); +} + +void OH_CryptoEcPoint_Destroy(OH_CryptoEcPoint *point) +{ + if (point == NULL) { + return; + } + HcfFree(point->curveName); + point->curveName = NULL; + FreeEcPointMem(&(point->pointBase)); + HcfFree(point); +} \ No newline at end of file diff --git a/frameworks/native/src/crypto_asym_cipher.c b/frameworks/native/src/crypto_asym_cipher.c new file mode 100644 index 0000000000000000000000000000000000000000..03ecced6b4122177a4a6af7a8585dbe55d3b5850 --- /dev/null +++ b/frameworks/native/src/crypto_asym_cipher.c @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "crypto_asym_cipher.h" +#include +#include "result.h" +#include "memory.h" +#include "cipher.h" +#include "sm2_crypto_util.h" +#include "pub_key.h" +#include "pri_key.h" +#include "blob.h" +#include "object_base.h" +#include "native_common.h" +#include "crypto_common.h" + +typedef struct OH_CryptoAsymCipher { + HcfObjectBase base; + + HcfResult (*init)(HcfCipher *self, enum HcfCryptoMode opMode, + HcfKey *key, HcfParamsSpec *params); + + HcfResult (*update)(HcfCipher *self, HcfBlob *input, HcfBlob *output); + + HcfResult (*doFinal)(HcfCipher *self, HcfBlob *input, HcfBlob *output); + + const char *(*getAlgorithm)(HcfCipher *self); + + HcfResult (*setCipherSpecUint8Array)(HcfCipher *self, CipherSpecItem item, HcfBlob blob); + + HcfResult (*getCipherSpecString)(HcfCipher *self, CipherSpecItem item, char **returnString); + + HcfResult (*getCipherSpecUint8Array)(HcfCipher *self, CipherSpecItem item, HcfBlob *returnUint8Array); +} OH_CryptoAsymCipher; + +typedef struct OH_CryptoKeyPair { + HcfObjectBase base; + + HcfPriKey *priKey; + + HcfPubKey *pubKey; +} OH_CryptoKeyPair; + +typedef struct OH_CryptoSm2CiphertextSpec { + HcfBigInteger xCoordinate; + HcfBigInteger yCoordinate; + HcfBlob cipherTextData; + HcfBlob hashData; +} OH_CryptoSm2CiphertextSpec; + +static const char *g_sm2ModeC1C3C2 = "C1C3C2"; + +OH_Crypto_ErrCode OH_CryptoAsymCipher_Create(const char *algoName, OH_CryptoAsymCipher **ctx) +{ + if ((algoName == NULL) || (ctx == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = HcfCipherCreate(algoName, (HcfCipher **)ctx); + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_CipherMode mode, OH_CryptoKeyPair *key) +{ + if ((ctx == NULL) || (ctx->init == NULL) || (key == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = HCF_SUCCESS; + switch (mode) { + case CRYPTO_ENCRYPT_MODE: + ret = ctx->init((HcfCipher *)ctx, (enum HcfCryptoMode)mode, (HcfKey *)(key->pubKey), NULL); + break; + case CRYPTO_DECRYPT_MODE: + ret = ctx->init((HcfCipher *)ctx, (enum HcfCryptoMode)mode, (HcfKey *)(key->priKey), NULL); + break; + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoAsymCipher_Final(OH_CryptoAsymCipher *ctx, const Crypto_DataBlob *in, + Crypto_DataBlob *out) +{ + if ((ctx == NULL) || (ctx->doFinal == NULL) || (out == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = ctx->doFinal((HcfCipher *)ctx, (HcfBlob *)in, (HcfBlob *)out); + return GetOhCryptoErrCodeNew(ret); +} + +void OH_CryptoAsymCipher_Destroy(OH_CryptoAsymCipher *ctx) +{ + if ((ctx == NULL) || (ctx->base.destroy == NULL)) { + return; + } + ctx->base.destroy((HcfObjectBase *)ctx); +} + +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_Create(Crypto_DataBlob *sm2Ciphertext, OH_CryptoSm2CiphertextSpec **spec) +{ + if (spec == NULL) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + if (sm2Ciphertext == NULL) { + *spec = (OH_CryptoSm2CiphertextSpec *)HcfMalloc(sizeof(OH_CryptoSm2CiphertextSpec), 0); + if (*spec == NULL) { + return CRYPTO_MEMORY_ERROR; + } + return CRYPTO_SUCCESS; + } + HcfResult ret = HcfGetCipherTextSpec((HcfBlob *)sm2Ciphertext, g_sm2ModeC1C3C2, (Sm2CipherTextSpec **)spec); + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_GetItem(OH_CryptoSm2CiphertextSpec *spec, + CryptoSm2CiphertextSpec_item item, Crypto_DataBlob *out) +{ + if ((spec == NULL) || (out == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + uint8_t *data = NULL; + size_t len = 0; + switch (item) { + case CRYPTO_SM2_CIPHERTEXT_C1_X: + data = spec->xCoordinate.data; + len = spec->xCoordinate.len; + break; + case CRYPTO_SM2_CIPHERTEXT_C1_Y: + data = spec->yCoordinate.data; + len = spec->yCoordinate.len; + break; + case CRYPTO_SM2_CIPHERTEXT_C2: + data = spec->cipherTextData.data; + len = spec->cipherTextData.len; + break; + case CRYPTO_SM2_CIPHERTEXT_C3: + data = spec->hashData.data; + len = spec->hashData.len; + break; + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } + if ((data == NULL) || (len == 0)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + out->data = (uint8_t *)HcfMalloc(len, 0); + if (out->data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(out->data, len, data, len); + out->len = len; + return CRYPTO_SUCCESS; +} + +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_SetItem(OH_CryptoSm2CiphertextSpec *spec, + CryptoSm2CiphertextSpec_item item, Crypto_DataBlob *in) +{ + if ((spec == NULL) || (in == NULL) || (in->data == NULL) || (in->len == 0)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + uint8_t *data = (uint8_t *)HcfMalloc(in->len, 0); + if (data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(data, in->len, in->data, in->len); + switch (item) { + case CRYPTO_SM2_CIPHERTEXT_C1_X: + HcfFree(spec->xCoordinate.data); + spec->xCoordinate.data = data; + spec->xCoordinate.len = in->len; + break; + case CRYPTO_SM2_CIPHERTEXT_C1_Y: + HcfFree(spec->yCoordinate.data); + spec->yCoordinate.data = data; + spec->yCoordinate.len = in->len; + break; + case CRYPTO_SM2_CIPHERTEXT_C2: + HcfFree(spec->cipherTextData.data); + spec->cipherTextData.data = data; + spec->cipherTextData.len = in->len; + break; + case CRYPTO_SM2_CIPHERTEXT_C3: + HcfFree(spec->hashData.data); + spec->hashData.data = data; + spec->hashData.len = in->len; + break; + default: + HcfFree(data); + return CRYPTO_PARAMETER_CHECK_FAILED; + } + + return CRYPTO_SUCCESS; +} + +OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_Encode(OH_CryptoSm2CiphertextSpec *spec, Crypto_DataBlob *out) +{ + if ((spec == NULL) || (out == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = HcfGenCipherTextBySpec((Sm2CipherTextSpec *)spec, g_sm2ModeC1C3C2, (HcfBlob *)out); + return GetOhCryptoErrCodeNew(ret); +} + +void OH_CryptoSm2CiphertextSpec_Destroy(OH_CryptoSm2CiphertextSpec *spec) +{ + DestroySm2CipherTextSpec((Sm2CipherTextSpec *)spec); +} \ No newline at end of file diff --git a/frameworks/native/src/crypto_kdf.c b/frameworks/native/src/crypto_kdf.c new file mode 100644 index 0000000000000000000000000000000000000000..96a8bbbc4933871c11467cd650cd11b07eca8a97 --- /dev/null +++ b/frameworks/native/src/crypto_kdf.c @@ -0,0 +1,365 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "crypto_kdf.h" +#include +#include +#include +#include "crypto_common.h" +#include "native_common.h" +#include "memory.h" +#include "kdf.h" +#include "kdf_params.h" +#include "detailed_hkdf_params.h" +#include "detailed_pbkdf2_params.h" +#include "detailed_scrypt_params.h" + +typedef struct OH_CryptoKdf { + HcfObjectBase base; + + const char *(*getAlgorithm)(HcfKdf *self); + + HcfResult (*generateSecret)(HcfKdf *self, HcfKdfParamsSpec* paramsSpec); +} OH_CryptoKdf; + + +typedef struct OH_CryptoKdfParams { + const char *algName; +} OH_CryptoKdfParams; + +static const char *g_hkdfName = "HKDF"; +static const char *g_pbkdf2Name = "PBKDF2"; +static const char *g_scryptName = "SCRYPT"; + +OH_Crypto_ErrCode OH_CryptoKdfParams_Create(const char *algoName, OH_CryptoKdfParams **params) +{ + if ((algoName == NULL) || (params == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + + OH_CryptoKdfParams *tmParams = NULL; + const char *algName = NULL; + if (strcmp(algoName, g_hkdfName) == 0) { + tmParams = (OH_CryptoKdfParams *)HcfMalloc(sizeof(HcfHkdfParamsSpec), 0); + algName = g_hkdfName; + } else if (strcmp(algoName, g_pbkdf2Name) == 0) { + tmParams = (OH_CryptoKdfParams *)HcfMalloc(sizeof(HcfPBKDF2ParamsSpec), 0); + algName = g_pbkdf2Name; + } else if (strcmp(algoName, g_scryptName) == 0) { + tmParams = (OH_CryptoKdfParams *)HcfMalloc(sizeof(HcfScryptParamsSpec), 0); + algName = g_scryptName; + } else { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + if (tmParams == NULL) { + return CRYPTO_MEMORY_ERROR; + } + tmParams->algName = algName; + *params = tmParams; + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode SetHkdfParam(HcfHkdfParamsSpec *params, CryptoKdf_ParamType type, 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); + 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; + default: + HcfFree(data); + return CRYPTO_PARAMETER_CHECK_FAILED; + } + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode SetPbkdf2Param(HcfPBKDF2ParamsSpec *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); + HcfBlobDataClearAndFree(&(params->password)); + params->password.data = data; + params->password.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); + HcfBlobDataClearAndFree(&(params->salt)); + params->salt.data = data; + params->salt.len = value->len; + break; + } + case CRYPTO_KDF_ITER_COUNT_INT: { + if (value->len != sizeof(int)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + params->iterations = *(int *)(value->data); + break; + } + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } + 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); + HcfBlobDataClearAndFree(&(params->passPhrase)); + 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); + HcfBlobDataClearAndFree(&(params->salt)); + 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: + 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; + } +} + +OH_Crypto_ErrCode OH_CryptoKdfParams_SetParam(OH_CryptoKdfParams *params, CryptoKdf_ParamType type, + Crypto_DataBlob *value) +{ + if ((params == NULL) || (params->algName == NULL) || (value == NULL) || (value->data == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + if (strcmp(params->algName, g_hkdfName) == 0) { + return SetHkdfParam((HcfHkdfParamsSpec*)params, type, value); + } else if (strcmp(params->algName, g_pbkdf2Name) == 0) { + return SetPbkdf2Param((HcfPBKDF2ParamsSpec*)params, type, value); + } else if (strcmp(params->algName, g_scryptName) == 0) { + return SetScryptParam((HcfScryptParamsSpec*)params, type, value); + } else { + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static void FreeHkdfParamSpec(HcfHkdfParamsSpec *params) +{ + HcfBlobDataClearAndFree(&(params->key)); + HcfBlobDataClearAndFree(&(params->salt)); + HcfBlobDataClearAndFree(&(params->info)); + HcfBlobDataClearAndFree(&(params->output)); +} + +static void FreePbkdf2ParamSpec(HcfPBKDF2ParamsSpec *params) +{ + HcfBlobDataClearAndFree(&(params->password)); + HcfBlobDataClearAndFree(&(params->salt)); + HcfBlobDataClearAndFree(&(params->output)); +} + +static void FreeScryptParamSpec(HcfScryptParamsSpec *params) +{ + HcfBlobDataClearAndFree(&(params->passPhrase)); + HcfBlobDataClearAndFree(&(params->salt)); + HcfBlobDataClearAndFree(&(params->output)); +} + +static void FreeParamSpec(OH_CryptoKdfParams *params) +{ + if (params->algName == NULL) { + return; + } + if (strcmp(params->algName, g_hkdfName) == 0) { + FreeHkdfParamSpec((HcfHkdfParamsSpec*)params); + } else if (strcmp(params->algName, g_pbkdf2Name) == 0) { + FreePbkdf2ParamSpec((HcfPBKDF2ParamsSpec*)params); + } else if (strcmp(params->algName, g_scryptName) == 0) { + FreeScryptParamSpec((HcfScryptParamsSpec*)params); + } +} + +void OH_CryptoKdfParams_Destroy(OH_CryptoKdfParams *params) +{ + if (params == NULL) { + return; + } + FreeParamSpec(params); + params->algName = NULL; + HcfFree(params); +} + +OH_Crypto_ErrCode OH_CryptoKdf_Create(const char *algoName, OH_CryptoKdf **ctx) +{ + if ((algoName == NULL) || (ctx == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = HcfKdfCreate(algoName, (HcfKdf **)ctx); + return GetOhCryptoErrCodeNew(ret); +} + +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) { + return CRYPTO_MEMORY_ERROR; + } + HcfBlob output = {.data = out, .len = keyLen}; + HcfHkdfParamsSpec hkdfParams = { + .base = { .algName = g_hkdfName, }, + .key = params->key, + .salt = params->salt, + .info = params->info, + .output = output, + }; + HcfResult ret = ctx->generateSecret(ctx, &(hkdfParams.base)); + if (ret != HCF_SUCCESS) { + HcfBlobDataClearAndFree(&output); + return GetOhCryptoErrCodeNew(ret); + } + key->data = hkdfParams.output.data; + key->len = hkdfParams.output.len; + return CRYPTO_SUCCESS; +} + +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) { + return CRYPTO_MEMORY_ERROR; + } + HcfBlob output = {.data = out, .len = keyLen}; + HcfPBKDF2ParamsSpec pbkdf2Params = { + .base = { .algName = g_pbkdf2Name, }, + .password = params->password, + .salt = params->salt, + .iterations = params->iterations, + .output = output, + }; + HcfResult ret = ctx->generateSecret(ctx, &(pbkdf2Params.base)); + if (ret != HCF_SUCCESS) { + HcfBlobDataClearAndFree(&output); + return GetOhCryptoErrCodeNew(ret); + } + key->data = pbkdf2Params.output.data; + key->len = pbkdf2Params.output.len; + return CRYPTO_SUCCESS; +} + +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) { + return CRYPTO_MEMORY_ERROR; + } + HcfBlob output = {.data = out, .len = keyLen}; + HcfScryptParamsSpec scryptParams = { + .base = { .algName = g_scryptName, }, + .passPhrase = params->passPhrase, + .salt = params->salt, + .n = params->n, + .p = params->p, + .r = params->r, + .maxMem = params->maxMem, + .output = output, + }; + HcfResult ret = ctx->generateSecret(ctx, &(scryptParams.base)); + if (ret != HCF_SUCCESS) { + HcfBlobDataClearAndFree(&output); + return GetOhCryptoErrCodeNew(ret); + } + key->data = scryptParams.output.data; + key->len = scryptParams.output.len; + return CRYPTO_SUCCESS; +} + +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) || (keyLen <= 0)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + + if (strcmp(params->algName, g_hkdfName) == 0) { + 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, (uint32_t)keyLen, (HcfBlob *)key); + } else if (strcmp(params->algName, g_scryptName) == 0) { + return ScryptDerive((HcfKdf *)ctx, (HcfScryptParamsSpec*)params, (uint32_t)keyLen, (HcfBlob *)key); + } else { + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +void OH_CryptoKdf_Destroy(OH_CryptoKdf *ctx) +{ + HcfObjDestroy((HcfKdf *)ctx); +} \ No newline at end of file diff --git a/frameworks/native/src/crypto_key_agreement.c b/frameworks/native/src/crypto_key_agreement.c new file mode 100644 index 0000000000000000000000000000000000000000..d3d4c023f7aa516967a22c64d85fa5e32bc8693a --- /dev/null +++ b/frameworks/native/src/crypto_key_agreement.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "crypto_key_agreement.h" +#include "native_common.h" +#include "crypto_common.h" +#include "crypto_asym_key.h" +#include "key_agreement.h" + +typedef struct OH_CryptoKeyAgreement { + HcfObjectBase base; + + HcfResult (*generateSecret)(HcfKeyAgreement *self, HcfPriKey *priKey, + HcfPubKey *pubKey, HcfBlob *returnSecret); + + const char *(*getAlgoName)(HcfKeyAgreement *self); +} OH_CryptoKeyAgreement; + +OH_Crypto_ErrCode OH_CryptoKeyAgreement_Create(const char *algoName, OH_CryptoKeyAgreement **ctx) +{ + if (ctx == NULL) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = HcfKeyAgreementCreate(algoName, (HcfKeyAgreement **)ctx); + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoKeyAgreement_GenerateSecret(OH_CryptoKeyAgreement *ctx, OH_CryptoPrivKey *privkey, + OH_CryptoPubKey *pubkey, Crypto_DataBlob *secret) +{ + 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); + return GetOhCryptoErrCodeNew(ret); +} + +void OH_CryptoKeyAgreement_Destroy(OH_CryptoKeyAgreement *ctx) +{ + HcfObjDestroy((HcfKeyAgreement*)ctx); +} \ No newline at end of file diff --git a/frameworks/native/src/crypto_mac.c b/frameworks/native/src/crypto_mac.c new file mode 100644 index 0000000000000000000000000000000000000000..3d5ceae50a31961843245639c31ed875c5fdac7d --- /dev/null +++ b/frameworks/native/src/crypto_mac.c @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "crypto_mac.h" +#include +#include +#include "memory.h" +#include "crypto_common.h" +#include "crypto_sym_key.h" +#include "native_common.h" +#include "mac.h" +#include "mac_params.h" +#include "detailed_cmac_params.h" +#include "detailed_hmac_params.h" + +typedef struct OH_CryptoMac { + HcfMacParamsSpec *paramsSpec; + 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)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + OH_CryptoMac *tmpCtx = (OH_CryptoMac *)HcfMalloc(sizeof(OH_CryptoMac), 0); + if (tmpCtx == NULL) { + return CRYPTO_MEMORY_ERROR; + } + HcfMacParamsSpec *paramsSpec = NULL; + const char *algName = NULL; + if (strcmp(algoName, CMAC_NAME) == 0) { + paramsSpec = (HcfMacParamsSpec *)HcfMalloc(sizeof(HcfCmacParamsSpec), 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; + } + + if (paramsSpec == NULL) { + HcfFree(tmpCtx); + return CRYPTO_MEMORY_ERROR; + } + + paramsSpec->algName = algName; + tmpCtx->paramsSpec = paramsSpec; + *ctx = tmpCtx; + return CRYPTO_SUCCESS; +} + +static OH_Crypto_ErrCode SetCmacParam(HcfCmacParamsSpec *paramsSpec, CryptoMac_ParamType type, + const Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_MAC_CIPHER_NAME_STR: { + char *data = (char *)HcfMalloc(value->len + 1, 0); + if (data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(data, value->len, value->data, value->len); + HcfFree((void *)(paramsSpec->cipherName)); + paramsSpec->cipherName = data; + return CRYPTO_SUCCESS; + } + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +static OH_Crypto_ErrCode SetHmacParam(HcfHmacParamsSpec *paramsSpec, CryptoMac_ParamType type, + const Crypto_DataBlob *value) +{ + switch (type) { + case CRYPTO_MAC_DIGEST_NAME_STR: { + char *data = (char *)HcfMalloc(value->len + 1, 0); + if (data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(data, value->len, value->data, value->len); + HcfFree((void *)(paramsSpec->mdName)); + paramsSpec->mdName = data; + return CRYPTO_SUCCESS; + } + default: + return CRYPTO_PARAMETER_CHECK_FAILED; + } +} + +OH_Crypto_ErrCode OH_CryptoMac_SetParam(OH_CryptoMac *ctx, CryptoMac_ParamType type, const Crypto_DataBlob *value) +{ + if ((ctx == NULL) || (ctx->paramsSpec == NULL) || (ctx->paramsSpec->algName == NULL) || (value == NULL) || + (value->data == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + OH_Crypto_ErrCode res = CRYPTO_PARAMETER_CHECK_FAILED; + if (strcmp(ctx->paramsSpec->algName, "CMAC") == 0) { + res = SetCmacParam((HcfCmacParamsSpec*)(ctx->paramsSpec), type, value); + } else if (strcmp(ctx->paramsSpec->algName, "HMAC") == 0) { + res = SetHmacParam((HcfHmacParamsSpec*)(ctx->paramsSpec), type, value); + } + + if (res != CRYPTO_SUCCESS) { + return res; + } + + HcfMac *macObj = NULL; + HcfResult ret = HcfMacCreate(ctx->paramsSpec, &macObj); + if (ret != HCF_SUCCESS) { + 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)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = ctx->macObj->update(ctx->macObj, (HcfBlob *)in); + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoMac_Final(OH_CryptoMac *ctx, Crypto_DataBlob *out) +{ + if ((ctx == NULL) || (ctx->macObj == NULL) || (ctx->macObj->doFinal == NULL) || (out == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = ctx->macObj->doFinal(ctx->macObj, (HcfBlob *)out); + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoMac_GetLength(OH_CryptoMac *ctx, uint32_t *length) +{ + if ((ctx == NULL) || (ctx->macObj == NULL) || (ctx->macObj->getMacLength == NULL) || (length == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + *length = ctx->macObj->getMacLength(ctx->macObj); + return CRYPTO_SUCCESS; +} + +static void FreeMacParams(HcfMacParamsSpec *params) +{ + if ((params == NULL) || (params->algName == NULL)) { + return; + } + if (strcmp(params->algName, "CMAC") == 0) { + HcfFree((void *)(((HcfCmacParamsSpec *)params)->cipherName)); + ((HcfCmacParamsSpec *)params)->cipherName = NULL; + } else if (strcmp(params->algName, "HMAC") == 0) { + HcfFree((void *)(((HcfHmacParamsSpec *)params)->mdName)); + ((HcfHmacParamsSpec *)params)->mdName = NULL; + } + params->algName = NULL; + HcfFree(params); +} + +void OH_CryptoMac_Destroy(OH_CryptoMac *ctx) +{ + if (ctx == NULL) { + return; + } + FreeMacParams(ctx->paramsSpec); + ctx->paramsSpec = NULL; + HcfObjDestroy(ctx->macObj); + ctx->macObj = NULL; + HcfFree(ctx); +} \ No newline at end of file diff --git a/frameworks/native/src/crypto_rand.c b/frameworks/native/src/crypto_rand.c new file mode 100644 index 0000000000000000000000000000000000000000..2f8dd873b07a51636be6ac257a67d63c90ee5146 --- /dev/null +++ b/frameworks/native/src/crypto_rand.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "crypto_rand.h" +#include +#include "memory.h" +#include "result.h" +#include "blob.h" +#include "object_base.h" +#include "native_common.h" +#include "crypto_common.h" +#include "rand.h" + +typedef struct OH_CryptoRand { + HcfObjectBase base; + + const char *(*getAlgoName)(HcfRand *self); + + HcfResult (*generateRandom)(HcfRand *self, int32_t numBytes, HcfBlob *random); + + HcfResult (*setSeed)(HcfRand *self, HcfBlob *seed); +} OH_CryptoRand; + +OH_Crypto_ErrCode OH_CryptoRand_Create(OH_CryptoRand **ctx) +{ + if (ctx == NULL) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = HcfRandCreate((HcfRand **)ctx); + return GetOhCryptoErrCodeNew(ret); +} + +OH_Crypto_ErrCode OH_CryptoRand_GenerateRandom(OH_CryptoRand *ctx, int len, Crypto_DataBlob *out) +{ + if ((ctx == NULL) || (ctx->generateRandom == NULL) || (out == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = ctx->generateRandom((HcfRand *)ctx, len, (HcfBlob *)out); + return GetOhCryptoErrCodeNew(ret); +} + +const char *OH_CryptoRand_GetAlgoName(OH_CryptoRand *ctx) +{ + if ((ctx == NULL) || (ctx->getAlgoName == NULL)) { + return NULL; + } + return ctx->getAlgoName((HcfRand *)ctx); +} + +OH_Crypto_ErrCode OH_CryptoRand_SetSeed(OH_CryptoRand *ctx, Crypto_DataBlob *seed) +{ + if ((ctx == NULL) || (ctx->setSeed == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = ctx->setSeed((HcfRand *)ctx, (HcfBlob *)seed); + return GetOhCryptoErrCodeNew(ret); +} + +void OH_CryptoRand_Destroy(OH_CryptoRand *ctx) +{ + HcfObjDestroy((HcfRand *)ctx); +} \ No newline at end of file diff --git a/frameworks/native/src/native_common.c b/frameworks/native/src/native_common.c index 979a14a652f62b36e41ab50ba8523f368cd9e1ff..a9579a0018152feb136cbbaccabf26d5698cab24 100644 --- a/frameworks/native/src/native_common.c +++ b/frameworks/native/src/native_common.c @@ -46,3 +46,48 @@ OH_Crypto_ErrCode GetOhCryptoErrCodeNew(HcfResult errCode) 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; + } +} diff --git a/interfaces/kits/native/include/crypto_asym_cipher.h b/interfaces/kits/native/include/crypto_asym_cipher.h new file mode 100644 index 0000000000000000000000000000000000000000..d6ab307212546b8c8b7bbfa768a63b67b7bc1cd7 --- /dev/null +++ b/interfaces/kits/native/include/crypto_asym_cipher.h @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup CryptoAsymCipherApi + * @{ + * + * @brief Describes the asymmetric encryption and decryption algorithm interface provided to applications. + * + * @since 20 + */ + +/** + * @file crypto_asym_cipher.h + * + * @brief Defines the asymmetric cipher APIs. + * + * @library libohcrypto.so + * @kit CryptoArchitectureKit + * @syscap SystemCapability.Security.CryptoFramework + * @since 20 + */ + +#ifndef CRYPTO_ASYM_CIPHER_H +#define CRYPTO_ASYM_CIPHER_H + +#include "crypto_common.h" +#include "crypto_asym_key.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Defines the asymmetric cipher structure. + * + * @since 20 + */ +typedef struct OH_CryptoAsymCipher OH_CryptoAsymCipher; + +/** + * @brief Creates an asymmetric cipher context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name used to generate the asymmetric cipher context. e.g. "RSA|PKCS1", + * "RSA|PKCS1_OAEP|SHA384|MGF1_SHA384", "SM2|SM3". + * @param ctx Indicates the pointer to the asymmetric cipher context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoAsymCipher_Create(const char *algoName, OH_CryptoAsymCipher **ctx); + +/** + * @brief Initializes the asymmetric cipher context with the given crypto mode, key and parameters. + * + * @param ctx Indicates the asymmetric cipher context. + * @param mode Indicates the crypto mode is encryption or decryption. + * @param key Indicates the asymmetric key. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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. + * @see OH_CryptoAsymCipher_Final + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_CipherMode mode, OH_CryptoKeyPair *key); + +/** + * @brief Finalizes the encryption or decryption operation. + * + * @param ctx Indicates the asymmetric cipher context. + * @param in Indicates the input data to be encrypted or decrypted. + * @param out Indicates the result of encryption or decryption. + * @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. + * @see OH_CryptoAsymCipher_Init + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoAsymCipher_Final(OH_CryptoAsymCipher *ctx, const Crypto_DataBlob *in, + Crypto_DataBlob *out); + +/** + * @brief Destroys the asymmetric cipher context. + * + * @param ctx Indicates the asymmetric cipher context. + */ +void OH_CryptoAsymCipher_Destroy(OH_CryptoAsymCipher *ctx); + +/** + * @brief Defines the SM2 ciphertext spec structure. + * + * @since 20 + */ +typedef struct OH_CryptoSm2CiphertextSpec OH_CryptoSm2CiphertextSpec; + +/** + * @brief Defines the SM2 ciphertext spec item type. + * + * @since 20 + */ +typedef enum { + /** Public key x, also known as C1x. */ + CRYPTO_SM2_CIPHERTEXT_C1_X = 0, + /** Public key y, also known as C1y. */ + CRYPTO_SM2_CIPHERTEXT_C1_Y = 1, + /** Hash, also known as C2. */ + CRYPTO_SM2_CIPHERTEXT_C2 = 2, + /** Ciphertext data, also known as C3. */ + CRYPTO_SM2_CIPHERTEXT_C3 = 3, +} CryptoSm2CiphertextSpec_item; + +/** + * @brief Creates a SM2 ciphertext spec. + * + * @param sm2Ciphertext Indicates the SM2 ciphertext in DER format, if sm2Ciphertext param is NULL, + * an empty SM2 ciphertext spec will be created. + * @param spec Indicates the output SM2 ciphertext spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoSm2CiphertextSpec_Create(Crypto_DataBlob *sm2Ciphertext, OH_CryptoSm2CiphertextSpec **spec); + +/** + * @brief Gets the specified item of the SM2 ciphertext. + * + * @param spec Indicates the SM2 ciphertext spec. + * @param item Indicates the SM2 ciphertext spec item. + * @param out Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoSm2CiphertextSpec_GetItem(OH_CryptoSm2CiphertextSpec *spec, + CryptoSm2CiphertextSpec_item item, Crypto_DataBlob *out); + +/** + * @brief Sets the specified item to the SM2 ciphertext spec. + * + * @param spec Indicates the SM2 ciphertext spec. + * @param item Indicates the SM2 ciphertext spec item. + * @param in Indicates the input data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoSm2CiphertextSpec_SetItem(OH_CryptoSm2CiphertextSpec *spec, + CryptoSm2CiphertextSpec_item item, Crypto_DataBlob *in); + +/** + * @brief Encodes the SM2 ciphertext spec to ciphertext in DER format. + * + * @param spec Indicates the SM2 ciphertext spec. + * @param out Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoSm2CiphertextSpec_Encode(OH_CryptoSm2CiphertextSpec *spec, Crypto_DataBlob *out); + +/** + * @brief Destroys the SM2 ciphertext spec. + * + * @param spec Indicates the SM2 ciphertext spec. + * @since 20 + */ +void OH_CryptoSm2CiphertextSpec_Destroy(OH_CryptoSm2CiphertextSpec *spec); + +#ifdef __cplusplus +} +#endif + +#endif /* CRYPTO_ASYM_CIPHER_H */ +/** @} */ diff --git a/interfaces/kits/native/include/crypto_asym_key.h b/interfaces/kits/native/include/crypto_asym_key.h index f5be1dab6cd3f910b452081a334cbd9b01cecb0c..4997e5e5589a486c9e68d1808fb409ac4a1cfee7 100644 --- a/interfaces/kits/native/include/crypto_asym_key.h +++ b/interfaces/kits/native/include/crypto_asym_key.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Huawei Device Co., Ltd. + * Copyright (C) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -28,7 +28,7 @@ * @brief Defines the AsymKey APIs. * * @library libohcrypto.so - * @kit Crypto Architecture Kit + * @kit CryptoArchitectureKit * @syscap SystemCapability.Security.CryptoFramework * @since 12 */ @@ -50,16 +50,16 @@ extern "C" { typedef struct OH_CryptoKeyPair OH_CryptoKeyPair; /** - * @brief Define the public Key structure. + * @brief Define the public key structure. * * @since 12 */ typedef struct OH_CryptoPubKey OH_CryptoPubKey; /** - * @brief Define the private Key structure. + * @brief Defines the private key structure. * - * @since 12 + * @since 20 */ typedef struct OH_CryptoPrivKey OH_CryptoPrivKey; @@ -235,7 +235,7 @@ void OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *keyCtx); OH_CryptoPubKey *OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair *keyCtx); /** - * @brief Get the private key of the key pair. + * @brief Gets the private key of the key pair. * * @param keyCtx Indicates the keyPair context. * @return Return the private key context from the key pair. @@ -248,7 +248,7 @@ OH_CryptoPrivKey *OH_CryptoKeyPair_GetPrivKey(OH_CryptoKeyPair *keyCtx); * * @param key Indicates the public key. * @param type Indicates the pubkey type. - * @param encodingStandard Indicates the encoding standard . + * @param encodingStandard Indicates the encoding standard. * @param out Indicates the encoded result. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. @@ -275,6 +275,361 @@ 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. + * + * @since 20 + */ +typedef struct OH_CryptoPrivKeyEncodingParams OH_CryptoPrivKeyEncodingParams; + +/** + * @brief Defines the private key encoding param type. + * + * @since 20 + */ +typedef enum { + /** Indicates the password string. */ + CRYPTO_PRIVATE_KEY_ENCODING_PASSWORD_STR = 0, + + /** Indicates the symmetric cipher string. */ + CRYPTO_PRIVATE_KEY_ENCODING_SYMMETRIC_CIPHER_STR = 1, +} CryptoPrivKeyEncoding_ParamType; + +/** + * @brief Creates private key encoding params. + * + * @param ctx Indicates the private key encoding params. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoPrivKeyEncodingParams_Create(OH_CryptoPrivKeyEncodingParams **ctx); + +/** + * @brief Sets the private key encoding params. + * + * @param ctx Indicates the private key encoding params. + * @param type Indicates the private key encoding param type. + * @param value Indicates the private key encoding params value. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoPrivKeyEncodingParams_SetParam(OH_CryptoPrivKeyEncodingParams *ctx, + CryptoPrivKeyEncoding_ParamType type, Crypto_DataBlob *value); + +/** + * @brief Destroys the private key encoding params. + * + * @param ctx Indicates the private key encoding params. + * @since 20 + */ +void OH_CryptoPrivKeyEncodingParams_Destroy(OH_CryptoPrivKeyEncodingParams *ctx); + +/** + * @brief Encodes the private key. + * + * @param key Indicates the private key. + * @param type Indicates the private encoding type. + * @param encodingStandard Indicates the encoding standard, such as "PKCS8". + * @param params Indicates the private key encoding params, it can be NULL, and if you want encypt the private key, + * you should set this param. + * @param out Indicates the encoded result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoPrivKey_Encode(OH_CryptoPrivKey *key, Crypto_EncodingType type, + const char *encodingStandard, OH_CryptoPrivKeyEncodingParams *params, Crypto_DataBlob *out); + +/** + * @brief Gets the specified param of the private key. + * + * @param key Indicates the private key. + * @param item Indicates the asymmetric key param type. + * @param value Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoPrivKey_GetParam(OH_CryptoPrivKey *key, CryptoAsymKey_ParamType item, + Crypto_DataBlob *value); + +/** + * @brief Defines the asymmetric key spec structure. + * + * @since 20 + */ +typedef struct OH_CryptoAsymKeySpec OH_CryptoAsymKeySpec; + +/** + * @brief Defines the asymmetric key spec type. + * + * @since 20 + */ +typedef enum { + /** Common parameters spec. */ + CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC = 0, + /** Private key spec. */ + CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC = 1, + /** Public key spec. */ + CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC = 2, + /** Key pair spec. */ + CRYPTO_ASYM_KEY_KEY_PAIR_SPEC = 3, +} CryptoAsymKeySpec_Type; + +/** + * @brief Generates an EC common parameters spec. + * + * @param curveName Indicates the ECC curve name. + * @param spec Indicates the pointer to the EC common parameters spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoAsymKeySpec_GenEcCommonParamsSpec(const char *curveName, OH_CryptoAsymKeySpec **spec); + +/** + * @brief Generates a DH common parameters spec. + * + * @param pLen Indicates the byte length of the prime p. + * @param skLen Indicates the byte length of the private key. + * @param spec Indicates the pointer to the DH common parameters spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoAsymKeySpec_GenDhCommonParamsSpec(int pLen, int skLen, OH_CryptoAsymKeySpec **spec); + +/** + * @brief Creates an asymmetric key spec according to the given algorithm name and spec type. + * + * @param algoName Indicates the algorithm name for generating the spec. Example RSA. + * @param type Indicates the asymmetric key spec type. + * @param spec Indicates the pointer to the asymmetric key spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoAsymKeySpec_Create(const char *algoName, CryptoAsymKeySpec_Type type, + OH_CryptoAsymKeySpec **spec); + +/** + * @brief Sets the specified parameter to the asymmetric key spec. + * + * @param spec Indicates the asymmetric key spec. + * @param type Indicates the asymmetric key param type. + * @param value Indicates the input data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value); + +/** + * @brief Sets the common parameters spec to the asymmetric key spec. + * + * @param spec Indicates the asymmetric key spec. + * @param commonParamsSpec Indicates the common parameters spec. + * @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_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec *spec, + OH_CryptoAsymKeySpec *commonParamsSpec); + +/** + * @brief Gets the specified parameter from the asymmetric key spec. + * + * @param spec Indicates the asymmetric key spec. + * @param type Indicates the asymmetric key param type. + * @param value Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value); + +/** + * @brief Destroys the asymmetric key spec. + * + * @param spec Indicates the asymmetric key spec. + * @since 20 + */ +void OH_CryptoAsymKeySpec_Destroy(OH_CryptoAsymKeySpec *spec); + +/** + * @brief Defines the asymmetric key generator with spec. + * + * @since 20 + */ +typedef struct OH_CryptoAsymKeyGeneratorWithSpec OH_CryptoAsymKeyGeneratorWithSpec; + +/** + * @brief Creates an asymmetric key generator with spec. + * + * @param keySpec Indicates the asymmetric key spec. + * @param generator Indicates the asymmetric key generator with spec. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoAsymKeyGeneratorWithSpec_Create(OH_CryptoAsymKeySpec *keySpec, + OH_CryptoAsymKeyGeneratorWithSpec **generator); + +/** + * @brief Generates a key pair according to the asymmetric key spec. + * + * @param generator Indicates the asymmetric key generator with spec. + * @param keyPair Indicates the pointer to the key pair. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoAsymKeyGeneratorWithSpec_GenKeyPair(OH_CryptoAsymKeyGeneratorWithSpec *generator, + OH_CryptoKeyPair **keyPair); + +/** + * @brief Destroys the asymmetric key generator with spec. + * + * @param generator Indicates the asymmetric key generator with spec. + * @since 20 + */ +void OH_CryptoAsymKeyGeneratorWithSpec_Destroy(OH_CryptoAsymKeyGeneratorWithSpec *generator); + +/** + * @brief Defines the EC point structure. + * + * @since 20 + */ +typedef struct OH_CryptoEcPoint OH_CryptoEcPoint; + +/** + * @brief Creates an EC point. + * + * @param curveName Indicates the curve name. + * @param ecKeyData Indicates the EC point data, supports "04 || x || y", "02 || x" or "03 || x" format. + * If ecKeyData param is NULL, an empty EC point spec will be created. + * @param point Indicates the pointer to the EC point. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoEcPoint_Create(const char *curveName, Crypto_DataBlob *ecKeyData, OH_CryptoEcPoint **point); + +/** + * @brief Gets the x and y coordinate of the EC point. + * + * @param point Indicates the EC point. + * @param x Indicates the x coordinate of the EC point, it can be NULL. + * @param y Indicates the y coordinate of the EC point, it can be NULL. + * @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_CryptoEcPoint_GetCoordinate(OH_CryptoEcPoint *point, Crypto_DataBlob *x, Crypto_DataBlob *y); + +/** + * @brief Sets the x and y coordinate to the EC point. + * + * @param point Indicates the EC point. + * @param x Indicates the x coordinate of the EC point. + * @param y Indicates the y coordinate of the EC point. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoEcPoint_SetCoordinate(OH_CryptoEcPoint *point, Crypto_DataBlob *x, Crypto_DataBlob *y); + +/** + * @brief Encodes the EC point to the specified format. + * + * @param point Indicates the EC point. + * @param format Indicates the encoding format, supports "UNCOMPRESSED" and "COMPRESSED". + * @param out Indicates the encoded ec point data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoEcPoint_Encode(OH_CryptoEcPoint *point, const char *format, Crypto_DataBlob *out); + +/** + * @brief Destroys the EC point. + * + * @param point Indicates the EC point. + * @since 20 + */ +void OH_CryptoEcPoint_Destroy(OH_CryptoEcPoint *point); + #ifdef __cplusplus } #endif diff --git a/interfaces/kits/native/include/crypto_kdf.h b/interfaces/kits/native/include/crypto_kdf.h new file mode 100644 index 0000000000000000000000000000000000000000..afa02bc37ee2a475ebb4eaa9de02eda31a0312c7 --- /dev/null +++ b/interfaces/kits/native/include/crypto_kdf.h @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup CryptoKdfApi + * @{ + * + * @brief Describes the KDF algorithm interface provided to applications. + * + * @since 20 + */ + +/** + * @file crypto_kdf.h + * + * @brief Defines the KDF APIs. + * + * @library libohcrypto.so + * @kit CryptoArchitectureKit + * @syscap SystemCapability.Security.CryptoFramework + * @since 20 + */ + +#ifndef CRYPTO_KDF_H +#define CRYPTO_KDF_H + +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Defines the KDF structure. + * + * @since 20 + */ +typedef struct OH_CryptoKdf OH_CryptoKdf; + +/** + * @brief Defines the KDF params structure. + * + * @since 20 + */ +typedef struct OH_CryptoKdfParams OH_CryptoKdfParams; + +/** + * @brief Defines the KDF param type. + * + * @since 20 + */ +typedef enum { + /** Indicates the key or password for KDF. */ + CRYPTO_KDF_KEY_DATABLOB = 0, + + /** Indicates the salt for KDF. */ + CRYPTO_KDF_SALT_DATABLOB = 1, + + /** Indicates the info for KDF. */ + CRYPTO_KDF_INFO_DATABLOB = 2, + + /** Indicates the iteration count for PBKDF2. */ + CRYPTO_KDF_ITER_COUNT_INT = 3, + + /** Indicates the n for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_N_UINT64 = 4, + + /** Indicates the r for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_R_UINT64 = 5, + + /** Indicates the p for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_P_UINT64 = 6, + + /** Indicates the max memory for SCRYPT KDF. */ + CRYPTO_KDF_SCRYPT_MAX_MEM_UINT64 = 7, +} CryptoKdf_ParamType; + +/** + * @brief Creates KDF params. + * + * @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. + * {@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_CryptoKdfParams_Create(const char *algoName, OH_CryptoKdfParams **params); + +/** + * @brief Sets a parameter to the KDF parameters. + * + * @param params Indicates the KDF parameters. + * @param type Indicates the KDF parameter type. + * @param value Indicates the KDF parameter value. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoKdfParams_SetParam(OH_CryptoKdfParams *params, CryptoKdf_ParamType type, + Crypto_DataBlob *value); + +/** + * @brief Destroys the KDF params. + * + * @param params Indicates the KDF parameters. + * @since 20 + */ +void OH_CryptoKdfParams_Destroy(OH_CryptoKdfParams *params); + +/** + * @brief Creates a KDF context. + * + * @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. + * {@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_CryptoKdf_Create(const char *algoName, OH_CryptoKdf **ctx); + +/** + * @brief Derives a key. + * + * @param ctx The KDF context. + * @param params Indicates the KDF parameters. + * @param keyLen Indicates the key derivation length. + * @param key Indicates the derived key. + * @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_CryptoKdf_Derive(OH_CryptoKdf *ctx, const OH_CryptoKdfParams *params, int keyLen, + Crypto_DataBlob *key); + +/** + * @brief Destroys the KDF context. + * + * @param ctx The KDF context. + * @since 20 + */ +void OH_CryptoKdf_Destroy(OH_CryptoKdf *ctx); + + +#ifdef __cplusplus +} +#endif + +#endif /* CRYPTO_KDF_H */ +/** @} */ diff --git a/interfaces/kits/native/include/crypto_key_agreement.h b/interfaces/kits/native/include/crypto_key_agreement.h new file mode 100644 index 0000000000000000000000000000000000000000..2b011c409986ddbb4b0786765579d475c68a3ca0 --- /dev/null +++ b/interfaces/kits/native/include/crypto_key_agreement.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup CryptoKeyAgreementApi + * @{ + * + * @brief Describes key agreement algorithm interface provided to applications. + * + * @since 20 + */ + +/** + * @file crypto_key_agreement.h + * + * @brief Defines the key agreement APIs. + * + * @library libohcrypto.so + * @kit CryptoArchitectureKit + * @syscap SystemCapability.Security.CryptoFramework + * @since 20 + */ + +#ifndef CRYPTO_KEY_AGREEMENT_H +#define CRYPTO_KEY_AGREEMENT_H + +#include "crypto_common.h" +#include "crypto_asym_key.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Defines the key agreement structure. + * + * @since 20 + */ +typedef struct OH_CryptoKeyAgreement OH_CryptoKeyAgreement; + +/** + * @brief Creates a key agreement context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name used to generate a key agreement context. e.g. "ECC", "X25519". + * @param ctx Indicates the key agreement context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoKeyAgreement_Create(const char *algoName, OH_CryptoKeyAgreement **ctx); + +/** + * @brief Generates a secret value. + * + * @param ctx Indicates the key agreement context. + * @param privkey Indicates the private key. + * @param pubkey Indicates the public key. + * @param secret Indicates the secret value. + * @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_CryptoKeyAgreement_GenerateSecret(OH_CryptoKeyAgreement *ctx, OH_CryptoPrivKey *privkey, + OH_CryptoPubKey *pubkey, Crypto_DataBlob *secret); + +/** + * @brief Destroys the key agreement context. + * + * @param ctx Indicates the key agreement context. + * @since 20 + */ +void OH_CryptoKeyAgreement_Destroy(OH_CryptoKeyAgreement *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* CRYPTO_KEY_AGREEMENT_H */ +/** @} */ diff --git a/interfaces/kits/native/include/crypto_mac.h b/interfaces/kits/native/include/crypto_mac.h new file mode 100644 index 0000000000000000000000000000000000000000..24d3c99392e53be742365b56304923349f9ba9a5 --- /dev/null +++ b/interfaces/kits/native/include/crypto_mac.h @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup CryptoMacApi + * @{ + * + * @brief Describes the MAC algorithm interface provided to applications. + * + * @since 20 + */ + +/** + * @file crypto_mac.h + * + * @brief Defines the MAC algorithm APIs. + * + * @library libohcrypto.so + * @kit CryptoArchitectureKit + * @syscap SystemCapability.Security.CryptoFramework + * @since 20 + */ + +#ifndef CRYPTO_MAC_H +#define CRYPTO_MAC_H + +#include "crypto_common.h" +#include "crypto_sym_key.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Defines the MAC structure. + * + * @since 20 + */ +typedef struct OH_CryptoMac OH_CryptoMac; + +/** + * @brief Defines the MAC algorithm parameter type. + * + * @since 20 + */ +typedef enum { + /** Indicates the algorithm name of the message digest function for HMAC. e.g. "SHA256".*/ + CRYPTO_MAC_DIGEST_NAME_STR = 0, + + /** Indicates the algorithm name of the symmetric cipher function for CMAC. e.g. "AES256".*/ + CRYPTO_MAC_CIPHER_NAME_STR = 1, +} CryptoMac_ParamType; + +/** + * @brief Creates a MAC context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name for generating the MAC context. e.g. "HMAC", "CMAC". + * @param ctx Indicates the pointer to the MAC context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx); + +/** + * @brief Sets the specified parameter to the MAC context. + * + * @param ctx Indicates the MAC context. + * @param type Indicates the MAC parameter type. + * @param value Indicates the parameter value. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoMac_SetParam(OH_CryptoMac *ctx, CryptoMac_ParamType type, const Crypto_DataBlob *value); + +/** + * @brief Initializes the MAC context with a symmetric key. + * + * @param ctx Indicates the MAC context. + * @param key Indicates the symmetric key. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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. + * @see OH_CryptoMac_Update + * @see OH_CryptoMac_Final + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *key); + +/** + * @brief Updates the MAC context with data. + * + * @param ctx Indicates the MAC context. + * @param in Indicates the data to update. + * @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. + * @see OH_CryptoMac_Init + * @see OH_CryptoMac_Final + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob *in); + +/** + * @brief Finalizes the MAC operation. + * + * @param ctx Indicates the MAC context. + * @param out Indicates the MAC result. + * @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. + * @see OH_CryptoMac_Init + * @see OH_CryptoMac_Update + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoMac_Final(OH_CryptoMac *ctx, Crypto_DataBlob *out); + +/** + * @brief Gets the length of the MAC. + * + * @param ctx Indicates the MAC context. + * @param length Indicates the MAC length. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoMac_GetLength(OH_CryptoMac *ctx, uint32_t *length); + +/** + * @brief Destroys the MAC context. + * + * @param ctx Indicates the MAC context. + * @since 20 + */ +void OH_CryptoMac_Destroy(OH_CryptoMac *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* CRYPTO_MAC_H */ +/** @} */ diff --git a/interfaces/kits/native/include/crypto_rand.h b/interfaces/kits/native/include/crypto_rand.h new file mode 100644 index 0000000000000000000000000000000000000000..1e9a4f8e017aaaae3d68c722704b4ef00bfb1acd --- /dev/null +++ b/interfaces/kits/native/include/crypto_rand.h @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup CryptoRandApi + * @{ + * + * @brief Describes the random number generation interface provided to applications. + * + * @since 20 + */ +/** + * @file crypto_rand.h + * + * @brief Defines the random number generator APIs. + * + * @library libohcrypto.so + * @kit CryptoArchitectureKit + * @syscap SystemCapability.Security.CryptoFramework + * @since 20 + */ +#ifndef CRYPTO_RAND_H +#define CRYPTO_RAND_H + +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Defines the random number generator structure. + * + * @since 20 + */ +typedef struct OH_CryptoRand OH_CryptoRand; + +/** + * @brief Creates a random number generator context. + * + * @param ctx Indicates the random number generator context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoRand_Create(OH_CryptoRand **ctx); + +/** + * @brief Generates random numbers. + * + * @param ctx Indicates the random number generator context. + * @param len Indicates the byte length of the random number. + * @param out Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoRand_GenerateRandom(OH_CryptoRand *ctx, int len, Crypto_DataBlob *out); + +/** + * @brief Gets the algorithm name of the random number generator context. + * + * @param ctx Indicates the pointer to the random number generator context. + * @return Return the algorithm name of the random number generator context. + * @since 20 + */ +const char *OH_CryptoRand_GetAlgoName(OH_CryptoRand *ctx); + +/** + * @brief Sets the seed to the random number generator context. + * + * @param ctx Indicates the random number generator context. + * @param seed Indicates the seed. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_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_CryptoRand_SetSeed(OH_CryptoRand *ctx, Crypto_DataBlob *seed); + +/** + * @brief Destroys the random number generator context. + * + * @param ctx Indicates the random number generator context. + * @since 20 + */ +void OH_CryptoRand_Destroy(OH_CryptoRand *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* CRYPTO_RAND_H */ +/** @} */ diff --git a/interfaces/kits/native/include/crypto_signature.h b/interfaces/kits/native/include/crypto_signature.h index 47df37001c8a620c9dbca90412c2fba687ee1bc4..7982b278f2c9dc5dc9d7347cf010e4f9a27d4d40 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 */ @@ -397,4 +397,3 @@ void OH_CryptoEccSignatureSpec_Destroy(OH_CryptoEccSignatureSpec *spec); /** @} */ #endif /* CRYPTO_SIGNATURE_H */ - \ No newline at end of file diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c index 162ef321bcefccf62945cf868780da18842e5cbb..a374d6830f0369bc947938375d028d060f40537a 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c @@ -726,6 +726,11 @@ static HcfResult GetPriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *param } cipher = EVP_CIPHER_fetch(NULL, cipherStr, NULL); passWord = (const char *)spec->password; + if (passWord == NULL) { + LOGE("passWord is NULL."); + OpensslEvpPkeyFree(pkey); + return HCF_ERR_PARAMETER_CHECK_FAILED; + } result = GetPriKeyPem(format, pkey, cipher, passWord, returnString); EVP_CIPHER_free((EVP_CIPHER *)cipher); } else { diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index eee19a469ac50a61cc696e0e604bc3625cbcf345..dc9bfb0e771a23daae63e04980fa6b2a83d61087 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -119,8 +119,13 @@ ohos_unittest("crypto_framework_test") { "src/ecc/crypto_ecc_sign_test.cpp", "src/ecc/crypto_ecc_verify_sub_test.cpp", "src/ecc/crypto_ecc_verify_test.cpp", + "src/native/native_asym_cipher_test.cpp", "src/native/native_asym_key_test.cpp", "src/native/native_digest_test.cpp", + "src/native/native_kdf_test.cpp", + "src/native/native_key_agreement_test.cpp", + "src/native/native_mac_test.cpp", + "src/native/native_rand_test.cpp", "src/native/native_signature_test.cpp", "src/native/native_sym_cipher_test.cpp", "src/native/native_sym_key_test.cpp", diff --git a/test/unittest/src/native/native_asym_cipher_test.cpp b/test/unittest/src/native/native_asym_cipher_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1bced3f1c5c800bf3bf4c007411fb4678c80916 --- /dev/null +++ b/test/unittest/src/native/native_asym_cipher_test.cpp @@ -0,0 +1,313 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "crypto_common.h" +#include "crypto_asym_cipher.h" +#include "log.h" +#include "memory.h" +#include "memory_mock.h" + +using namespace std; +using namespace testing::ext; + +class NativeAsymCipherTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NativeAsymCipherTest::SetUpTestCase() {} + +void NativeAsymCipherTest::TearDownTestCase() {} + +void NativeAsymCipherTest::SetUp() +{ +} + +void NativeAsymCipherTest::TearDown() +{ +} + +HWTEST_F(NativeAsymCipherTest, NativeAsymCipherTest001, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *keyGen = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("RSA3072", &keyGen); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyGen, nullptr); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + OH_CryptoAsymCipher *cipher = nullptr; + ret = OH_CryptoAsymCipher_Create("RSA3072|PKCS1", &cipher); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(cipher, nullptr); + + ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_ENCRYPT_MODE, keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + const char *testData = "Hello, RSA!"; + Crypto_DataBlob in = { + .data = (uint8_t *)testData, + .len = strlen(testData) + }; + + Crypto_DataBlob out = { 0 }; + ret = OH_CryptoAsymCipher_Final(cipher, &in, &out); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_GT(out.len, 0); + + OH_CryptoAsymCipher_Destroy(cipher); + cipher = nullptr; + ret = OH_CryptoAsymCipher_Create("RSA3072|PKCS1", &cipher); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(cipher, nullptr); + + ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_DECRYPT_MODE, keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob decrypted = { 0 }; + ret = OH_CryptoAsymCipher_Final(cipher, &out, &decrypted); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(decrypted.data, nullptr); + ASSERT_EQ(decrypted.len, strlen(testData)); + EXPECT_EQ(memcmp(decrypted.data, testData, decrypted.len), 0); + + OH_Crypto_FreeDataBlob(&out); + OH_Crypto_FreeDataBlob(&decrypted); + OH_CryptoAsymCipher_Destroy(cipher); + OH_CryptoAsymKeyGenerator_Destroy(keyGen); + OH_CryptoKeyPair_Destroy(keyPair); +} + +HWTEST_F(NativeAsymCipherTest, NativeAsymCipherTest002, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *keyGen = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeyGenerator_Create(nullptr, &keyGen); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &keyGen); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoAsymKeyGenerator_Destroy(keyGen); +} + +HWTEST_F(NativeAsymCipherTest, NativeAsymCipherTest003, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *keyGen = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &keyGen); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = OH_CryptoAsymKeyGenerator_Generate(nullptr, &keyPair); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ASSERT_EQ(keyPair, nullptr); + + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + OH_CryptoAsymKeyGenerator_Destroy(keyGen); + OH_CryptoKeyPair_Destroy(keyPair); +} + +HWTEST_F(NativeAsymCipherTest, NativeAsymCipherTest004, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *keyGen = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &keyGen); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymCipher *cipher = nullptr; + ret = OH_CryptoAsymCipher_Create(nullptr, &cipher); + ASSERT_NE(ret, CRYPTO_SUCCESS); + EXPECT_EQ(cipher, nullptr); + + ret = OH_CryptoAsymCipher_Create("SM2|SM3", nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoAsymCipher_Create("SM2|SM3", &cipher); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(cipher, nullptr); + + OH_CryptoAsymCipher_Destroy(cipher); + OH_CryptoAsymKeyGenerator_Destroy(keyGen); + OH_CryptoKeyPair_Destroy(keyPair); +} + +HWTEST_F(NativeAsymCipherTest, NativeAsymCipherTest005, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *keyGen = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &keyGen); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymCipher *cipher = nullptr; + ret = OH_CryptoAsymCipher_Create("SM2|SM3", &cipher); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(cipher, nullptr); + + ret = OH_CryptoAsymCipher_Init(nullptr, CRYPTO_ENCRYPT_MODE, keyPair); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_ENCRYPT_MODE, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoAsymCipher_Init(cipher, (Crypto_CipherMode)2, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_ENCRYPT_MODE, keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + const char *testData = "Hello, SM2!"; + Crypto_DataBlob in = { + .data = (uint8_t *)testData, + .len = strlen(testData) + }; + + Crypto_DataBlob out = { 0 }; + ret = OH_CryptoAsymCipher_Final(nullptr, &in, &out); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoAsymCipher_Final(cipher, &in, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoAsymCipher_Final(cipher, &in, &out); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_GT(out.len, 0); + + OH_Crypto_FreeDataBlob(&out); + OH_CryptoAsymCipher_Destroy(cipher); + OH_CryptoAsymKeyGenerator_Destroy(keyGen); + OH_CryptoKeyPair_Destroy(keyPair); +} + +HWTEST_F(NativeAsymCipherTest, NativeAsymCipherTest006, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *keyGen = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("SM2_256", &keyGen); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymCipher *cipher = nullptr; + ret = OH_CryptoAsymCipher_Create("SM2|SM3", &cipher); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(cipher, nullptr); + + ret = OH_CryptoAsymCipher_Init(cipher, CRYPTO_ENCRYPT_MODE, keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + const char *testData = "Hello, SM2!"; + Crypto_DataBlob in = { + .data = (uint8_t *)testData, + .len = strlen(testData) + }; + + Crypto_DataBlob out = { 0 }; + ret = OH_CryptoAsymCipher_Final(cipher, &in, &out); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_GT(out.len, 0); + + OH_CryptoSm2CiphertextSpec *sm2CipherSpec = nullptr; + ret = OH_CryptoSm2CiphertextSpec_Create(&out, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_Create(&out, &sm2CipherSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(sm2CipherSpec, nullptr); + + Crypto_DataBlob c1x = { 0 }; + Crypto_DataBlob c1y = { 0 }; + Crypto_DataBlob c2 = { 0 }; + Crypto_DataBlob c3 = { 0 }; + ret = OH_CryptoSm2CiphertextSpec_GetItem(nullptr, CRYPTO_SM2_CIPHERTEXT_C1_X, &c1x); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, (CryptoSm2CiphertextSpec_item)4, &c1x); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_X, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_X, &c1x); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_Y, &c1y); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C2, &c2); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_GetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C3, &c3); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoSm2CiphertextSpec_Destroy(sm2CipherSpec); + sm2CipherSpec = nullptr; + ret = OH_CryptoSm2CiphertextSpec_Create(nullptr, &sm2CipherSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(sm2CipherSpec, nullptr); + + ret = OH_CryptoSm2CiphertextSpec_SetItem(nullptr, CRYPTO_SM2_CIPHERTEXT_C1_X, &c1x); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_SetItem(sm2CipherSpec, (CryptoSm2CiphertextSpec_item)4, &c1x); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_X, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoSm2CiphertextSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_X, &c1x); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C1_Y, &c1y); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C2, &c2); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_SetItem(sm2CipherSpec, CRYPTO_SM2_CIPHERTEXT_C3, &c3); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob encoded = { 0 }; + ret = OH_CryptoSm2CiphertextSpec_Encode(nullptr, &encoded); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSm2CiphertextSpec_Encode(sm2CipherSpec, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoSm2CiphertextSpec_Encode(sm2CipherSpec, &encoded); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(encoded.data, nullptr); + ASSERT_GT(encoded.len, 0); + + OH_Crypto_FreeDataBlob(&out); + OH_Crypto_FreeDataBlob(&c1x); + OH_Crypto_FreeDataBlob(&c1y); + OH_Crypto_FreeDataBlob(&c2); + OH_Crypto_FreeDataBlob(&c3); + OH_Crypto_FreeDataBlob(&encoded); + OH_CryptoSm2CiphertextSpec_Destroy(sm2CipherSpec); + OH_CryptoAsymCipher_Destroy(cipher); + OH_CryptoAsymKeyGenerator_Destroy(keyGen); + OH_CryptoKeyPair_Destroy(keyPair); +} \ No newline at end of file diff --git a/test/unittest/src/native/native_asym_key_test.cpp b/test/unittest/src/native/native_asym_key_test.cpp index af72a942ccfb3fbc8c443cc1e193df2b336340e0..9017faed93af1ae51285fd230eb83bad7aa2b8ed 100644 --- a/test/unittest/src/native/native_asym_key_test.cpp +++ b/test/unittest/src/native/native_asym_key_test.cpp @@ -53,6 +53,213 @@ void NativeAsymKeyTest::TearDown() // add destroy here, this will be called when { } +static OH_CryptoKeyPair *GenerateKeyPair(const char *algName) +{ + OH_CryptoAsymKeyGenerator *generator = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create(algName, &generator); + if (ret != CRYPTO_SUCCESS) { + return nullptr; + } + + OH_CryptoKeyPair *keyCtx = nullptr; + ret = OH_CryptoAsymKeyGenerator_Generate(generator, &keyCtx); + if (ret != CRYPTO_SUCCESS) { + OH_CryptoAsymKeyGenerator_Destroy(generator); + return nullptr; + } + + OH_CryptoAsymKeyGenerator_Destroy(generator); + return keyCtx; +} + +static OH_Crypto_ErrCode GenerateKeyPairWithSpec(OH_CryptoAsymKeySpec *keySpec, OH_CryptoKeyPair **keyPair) +{ + OH_CryptoAsymKeyGeneratorWithSpec *generatorSpec = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGeneratorWithSpec_Create(keySpec, &generatorSpec); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + ret = OH_CryptoAsymKeyGeneratorWithSpec_GenKeyPair(generatorSpec, keyPair); + OH_CryptoAsymKeyGeneratorWithSpec_Destroy(generatorSpec); + return ret; +} + +static OH_Crypto_ErrCode GetDsaKeyParams(OH_CryptoKeyPair *keyCtx, Crypto_DataBlob *pubKeyData, + Crypto_DataBlob *privKeyData, Crypto_DataBlob *pData, Crypto_DataBlob *qData, + Crypto_DataBlob *gData) +{ + OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyCtx); + if (pubKey == nullptr) { + return CRYPTO_OPERTION_ERROR; + } + OH_Crypto_ErrCode ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DSA_PK_DATABLOB, pubKeyData); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + + OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyCtx); + if (privKey == nullptr) { + return CRYPTO_OPERTION_ERROR; + } + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_DSA_SK_DATABLOB, privKeyData); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_DSA_P_DATABLOB, pData); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_DSA_Q_DATABLOB, qData); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_DSA_G_DATABLOB, gData); + return ret; +} + +static OH_Crypto_ErrCode GetRsaKeyParams(OH_CryptoKeyPair *keyCtx, Crypto_DataBlob *pubKeyData, + Crypto_DataBlob *privKeyData, Crypto_DataBlob *nData) +{ + OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyCtx); + if (pubKey == nullptr) { + return CRYPTO_OPERTION_ERROR; + } + OH_Crypto_ErrCode ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_RSA_E_DATABLOB, pubKeyData); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + + OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyCtx); + if (privKey == nullptr) { + return CRYPTO_OPERTION_ERROR; + } + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_RSA_D_DATABLOB, privKeyData); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_RSA_N_DATABLOB, nData); + return ret; +} + +static OH_Crypto_ErrCode GetDhKeyParams(OH_CryptoKeyPair *keyCtx, Crypto_DataBlob *pubKeyData, + Crypto_DataBlob *privKeyData) +{ + OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyCtx); + if (pubKey == nullptr) { + return CRYPTO_OPERTION_ERROR; + } + OH_Crypto_ErrCode ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DH_PK_DATABLOB, pubKeyData); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + + OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyCtx); + if (privKey == nullptr) { + return CRYPTO_OPERTION_ERROR; + } + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_DH_SK_DATABLOB, privKeyData); + return ret; +} + +static OH_Crypto_ErrCode GetX25519KeyParams(OH_CryptoKeyPair *keyCtx, Crypto_DataBlob *pubKeyData, + Crypto_DataBlob *privKeyData) +{ + OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyCtx); + if (pubKey == nullptr) { + return CRYPTO_OPERTION_ERROR; + } + OH_Crypto_ErrCode ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_X25519_PK_DATABLOB, pubKeyData); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + + OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyCtx); + if (privKey == nullptr) { + return CRYPTO_OPERTION_ERROR; + } + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_X25519_SK_DATABLOB, privKeyData); + return ret; +} + +static OH_Crypto_ErrCode GetEccKeyParams(OH_CryptoKeyPair *keyCtx, Crypto_DataBlob *pubKeyXData, + Crypto_DataBlob *pubKeyYData, Crypto_DataBlob *privKeyData) +{ + OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyCtx); + if (pubKey == nullptr) { + return CRYPTO_OPERTION_ERROR; + } + OH_Crypto_ErrCode ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_PK_X_DATABLOB, pubKeyXData); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_PK_Y_DATABLOB, pubKeyYData); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + + OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyCtx); + if (privKey == nullptr) { + return CRYPTO_OPERTION_ERROR; + } + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_ECC_SK_DATABLOB, privKeyData); + return ret; +} + +static void FreeDsaKeyParams(Crypto_DataBlob *pubKeyData, Crypto_DataBlob *privKeyData, Crypto_DataBlob *pData, + Crypto_DataBlob *qData, Crypto_DataBlob *gData) +{ + OH_Crypto_FreeDataBlob(pubKeyData); + OH_Crypto_FreeDataBlob(privKeyData); + OH_Crypto_FreeDataBlob(pData); + OH_Crypto_FreeDataBlob(qData); + OH_Crypto_FreeDataBlob(gData); +} + +static void FreeRsaKeyParams(Crypto_DataBlob *pubKeyData, Crypto_DataBlob *privKeyData, Crypto_DataBlob *nData) { + OH_Crypto_FreeDataBlob(pubKeyData); + OH_Crypto_FreeDataBlob(privKeyData); + OH_Crypto_FreeDataBlob(nData); +} + +static void FreeDhKeyParams(Crypto_DataBlob *pubKeyData, Crypto_DataBlob *privKeyData) +{ + OH_Crypto_FreeDataBlob(pubKeyData); + OH_Crypto_FreeDataBlob(privKeyData); +} + +static void FreeX25519KeyParams(Crypto_DataBlob *pubKeyData, Crypto_DataBlob *privKeyData) +{ + OH_Crypto_FreeDataBlob(pubKeyData); + OH_Crypto_FreeDataBlob(privKeyData); +} + +static void FreeEccKeyParams(Crypto_DataBlob *pubKeyXData, Crypto_DataBlob *pubKeyYData, Crypto_DataBlob *privKeyData) +{ + OH_Crypto_FreeDataBlob(pubKeyXData); + OH_Crypto_FreeDataBlob(pubKeyYData); + OH_Crypto_FreeDataBlob(privKeyData); +} + +static void FreeDhCommonParams(Crypto_DataBlob *pData, Crypto_DataBlob *gData, Crypto_DataBlob *lData) +{ + OH_Crypto_FreeDataBlob(pData); + OH_Crypto_FreeDataBlob(gData); + OH_Crypto_FreeDataBlob(lData); +} + +static void FreeEccCommonParams(Crypto_DataBlob *pData, Crypto_DataBlob *aData, Crypto_DataBlob *bData, + Crypto_DataBlob *gxData, Crypto_DataBlob *gyData, Crypto_DataBlob *nData, + Crypto_DataBlob *hData) +{ + OH_Crypto_FreeDataBlob(pData); + OH_Crypto_FreeDataBlob(aData); + OH_Crypto_FreeDataBlob(bData); + OH_Crypto_FreeDataBlob(gxData); + OH_Crypto_FreeDataBlob(gyData); + OH_Crypto_FreeDataBlob(nData); + OH_Crypto_FreeDataBlob(hData); +} + HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest001, TestSize.Level0) { OH_CryptoAsymKeyGenerator *generator = nullptr; @@ -67,7 +274,7 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest001, TestSize.Level0) OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyCtx); - Crypto_DataBlob dataBlob = { .data = nullptr, .len = 0 }; + Crypto_DataBlob dataBlob = {.data = nullptr, .len = 0}; ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DSA_Q_DATABLOB, &dataBlob); EXPECT_EQ(ret, CRYPTO_SUCCESS); ASSERT_NE(dataBlob.data, nullptr); @@ -86,27 +293,28 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest002, TestSize.Level0) OH_CryptoKeyPair *dupKeyPair = nullptr; Crypto_DataBlob pubKeyX509Str = {}; - pubKeyX509Str.data = reinterpret_cast(const_cast(g_testPubkeyX509Str512.c_str())); + pubKeyX509Str.data = reinterpret_cast(const_cast(g_testPubkeyX509Str512.c_str())); pubKeyX509Str.len = strlen(g_testPubkeyX509Str512.c_str()); Crypto_DataBlob pubKeyPkcs1Str = {}; - pubKeyPkcs1Str.data = reinterpret_cast(const_cast(g_testPubkeyPkcs1Str512.c_str())); + pubKeyPkcs1Str.data = reinterpret_cast(const_cast(g_testPubkeyPkcs1Str512.c_str())); pubKeyPkcs1Str.len = strlen(g_testPubkeyPkcs1Str512.c_str()); res = OH_CryptoAsymKeyGenerator_Convert(generator, CRYPTO_PEM, &pubKeyX509Str, nullptr, &dupKeyPair); EXPECT_EQ(res, CRYPTO_SUCCESS); OH_CryptoPubKey *pubkey = OH_CryptoKeyPair_GetPubKey(dupKeyPair); - Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; + Crypto_DataBlob retBlob = {.data = nullptr, .len = 0}; res = OH_CryptoPubKey_Encode(pubkey, CRYPTO_PEM, "PKCS1", &retBlob); EXPECT_EQ(res, CRYPTO_SUCCESS); - int32_t cmpRes = strcmp(reinterpret_cast(retBlob.data), - reinterpret_cast(pubKeyPkcs1Str.data)); + int32_t cmpRes = + strcmp(reinterpret_cast(retBlob.data), reinterpret_cast(pubKeyPkcs1Str.data)); EXPECT_EQ(cmpRes, CRYPTO_SUCCESS); - Crypto_DataBlob retBlobX509 = { .data = nullptr, .len = 0 }; + Crypto_DataBlob retBlobX509 = {.data = nullptr, .len = 0}; res = OH_CryptoPubKey_Encode(pubkey, CRYPTO_PEM, "X509", &retBlobX509); EXPECT_EQ(res, CRYPTO_SUCCESS); - cmpRes = strcmp(reinterpret_cast(retBlobX509.data), reinterpret_cast(pubKeyX509Str.data)); + cmpRes = + strcmp(reinterpret_cast(retBlobX509.data), reinterpret_cast(pubKeyX509Str.data)); EXPECT_EQ(cmpRes, CRYPTO_SUCCESS); OH_Crypto_FreeDataBlob(&retBlob); @@ -125,7 +333,7 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest003, TestSize.Level0) EXPECT_EQ(ret, CRYPTO_SUCCESS); OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyCtx); - Crypto_DataBlob dataBlobE = { .data = nullptr, .len = 0 }; + Crypto_DataBlob dataBlobE = {.data = nullptr, .len = 0}; ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_RSA_E_DATABLOB, &dataBlobE); EXPECT_EQ(ret, CRYPTO_SUCCESS); ASSERT_NE(dataBlobE.data, nullptr); @@ -153,7 +361,7 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest004, TestSize.Level0) OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); ASSERT_EQ(ret, CRYPTO_SUCCESS); - Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; + Crypto_DataBlob retBlob = {.data = nullptr, .len = 0}; ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_PEM, "PKCS1", &retBlob); ASSERT_EQ(ret, CRYPTO_SUCCESS); @@ -163,7 +371,7 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest004, TestSize.Level0) OH_CryptoPubKey *pubKey1 = OH_CryptoKeyPair_GetPubKey(dupKeyPair); ASSERT_EQ(ret, CRYPTO_SUCCESS); - Crypto_DataBlob dataBlob = { .data = nullptr, .len = 0 }; + Crypto_DataBlob dataBlob = {.data = nullptr, .len = 0}; ret = OH_CryptoPubKey_GetParam(pubKey1, CRYPTO_RSA_N_DATABLOB, &dataBlob); ASSERT_EQ(ret, CRYPTO_SUCCESS); @@ -195,7 +403,7 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest005, TestSize.Level0) OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); ASSERT_EQ(ret, CRYPTO_SUCCESS); - Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; + Crypto_DataBlob retBlob = {.data = nullptr, .len = 0}; ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, nullptr, &retBlob); ASSERT_EQ(ret, CRYPTO_SUCCESS); @@ -205,7 +413,7 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest005, TestSize.Level0) OH_CryptoPubKey *pubKey1 = OH_CryptoKeyPair_GetPubKey(dupKeyPair); ASSERT_EQ(ret, CRYPTO_SUCCESS); - Crypto_DataBlob dataBlob = { .data = nullptr, .len = 0 }; + Crypto_DataBlob dataBlob = {.data = nullptr, .len = 0}; ret = OH_CryptoPubKey_GetParam(pubKey1, CRYPTO_ED25519_PK_DATABLOB, &dataBlob); ASSERT_EQ(ret, CRYPTO_SUCCESS); @@ -218,4 +426,936 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest005, TestSize.Level0) OH_CryptoKeyPair_Destroy(keyPair); OH_CryptoKeyPair_Destroy(dupKeyPair); } + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest006, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *keyGen = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("RSA2048", &keyGen); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyGen, nullptr); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyPair); + ASSERT_NE(privKey, nullptr); + + OH_CryptoPrivKeyEncodingParams *params = nullptr; + ret = OH_CryptoPrivKeyEncodingParams_Create(¶ms); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(params, nullptr); + + Crypto_DataBlob password = {(uint8_t *)"1234567890", 10}; + Crypto_DataBlob cipher = {(uint8_t *)"AES-128-CBC", 11}; + ret = OH_CryptoPrivKeyEncodingParams_SetParam(params, CRYPTO_PRIVATE_KEY_ENCODING_PASSWORD_STR, &password); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoPrivKeyEncodingParams_SetParam(params, CRYPTO_PRIVATE_KEY_ENCODING_SYMMETRIC_CIPHER_STR, &cipher); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob pemData = {0}; + ret = OH_CryptoPrivKey_Encode(privKey, CRYPTO_PEM, "PKCS8", nullptr, &pemData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_Crypto_FreeDataBlob(&pemData); + + ret = OH_CryptoPrivKey_Encode(privKey, CRYPTO_PEM, "PKCS8", params, &pemData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + 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}; + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_RSA_N_DATABLOB, &n); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_RSA_D_DATABLOB, &d); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_Crypto_FreeDataBlob(&pemData); + OH_Crypto_FreeDataBlob(&n); + OH_Crypto_FreeDataBlob(&d); + OH_CryptoPrivKeyEncodingParams_Destroy(params); + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeyGenerator_Destroy(keyGen); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest007, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *keyGen = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("ECC224", &keyGen); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyGen, nullptr); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = OH_CryptoAsymKeyGenerator_Generate(keyGen, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyPair); + ASSERT_NE(privKey, nullptr); + + Crypto_DataBlob derData = {0}; + ret = OH_CryptoPrivKey_Encode(nullptr, CRYPTO_DER, "PKCS8", nullptr, &derData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoPrivKey_Encode(privKey, CRYPTO_DER, "PKCS8", nullptr, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoPrivKey_Encode(privKey, CRYPTO_DER, "PKCS8", nullptr, &derData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(derData.data, nullptr); + ASSERT_NE(derData.len, 0); + OH_Crypto_FreeDataBlob(&derData); + + ret = OH_CryptoPrivKey_Encode(privKey, CRYPTO_DER, nullptr, nullptr, &derData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(derData.data, nullptr); + ASSERT_NE(derData.len, 0); + OH_Crypto_FreeDataBlob(&derData); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeyGenerator_Destroy(keyGen); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest008, 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 *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); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_P_DATABLOB, &pData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_Q_DATABLOB, &qData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_G_DATABLOB, &gData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDsaKeyParams(&pubKeyData, &privKeyData, &pData, &qData, &gData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_P_DATABLOB, &pData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_Q_DATABLOB, &qData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_G_DATABLOB, &gData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDsaKeyParams(&pubKeyData, &privKeyData, &pData, &qData, &gData); + + 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(keySpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest009, 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 *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("DSA", CRYPTO_ASYM_KEY_PUBLIC_KEY_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_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_P_DATABLOB, &pData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_Q_DATABLOB, &qData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_G_DATABLOB, &gData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDsaKeyParams(&pubKeyData, &privKeyData, &pData, &qData, &gData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_SK_DATABLOB, &privKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_P_DATABLOB, &pData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_Q_DATABLOB, &qData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_G_DATABLOB, &gData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDsaKeyParams(&pubKeyData, &privKeyData, &pData, &qData, &gData); + + 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_EQ(OH_CryptoKeyPair_GetPrivKey(keyPair), nullptr); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest010, 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 *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("DSA", CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, &keySpec); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_Create("DSA", CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_PK_DATABLOB, &pubKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_SK_DATABLOB, &privKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_P_DATABLOB, &pData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_Q_DATABLOB, &qData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_G_DATABLOB, &gData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDsaKeyParams(&pubKeyData, &privKeyData, &pData, &qData, &gData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_PK_DATABLOB, &pubKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_SK_DATABLOB, &privKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_P_DATABLOB, &pData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_Q_DATABLOB, &qData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DSA_G_DATABLOB, &gData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDsaKeyParams(&pubKeyData, &privKeyData, &pData, &qData, &gData); + + 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(keySpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest011, 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_KEY_PAIR_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_RSA_E_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_RSA_D_DATABLOB, &privKeyData); + EXPECT_EQ(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_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_RSA_D_DATABLOB, &privKeyData); + EXPECT_EQ(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_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(keySpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest012, 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_PUBLIC_KEY_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_RSA_E_DATABLOB, &pubKeyData); + EXPECT_EQ(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_EQ(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_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPubKey(keyPair), nullptr); + EXPECT_EQ(OH_CryptoKeyPair_GetPrivKey(keyPair), nullptr); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest013, TestSize.Level0) +{ + OH_CryptoAsymKeySpec *keySpec = nullptr; + 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); + ASSERT_NE(ret, CRYPTO_SUCCESS); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest014, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("DH_modp2048"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetDhKeyParams(keyCtx, &pubKeyData, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *dhCommonSpec = nullptr; + ret = OH_CryptoAsymKeySpec_GenDhCommonParamsSpec(2048, 1024, &dhCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("DH", CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetCommonParamsSpec(keySpec, dhCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DH_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DH_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDhKeyParams(&pubKeyData, &privKeyData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DH_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DH_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDhKeyParams(&pubKeyData, &privKeyData); + + 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(keySpec); + OH_CryptoAsymKeySpec_Destroy(dhCommonSpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest015, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("DH_modp2048"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetDhKeyParams(keyCtx, &pubKeyData, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *dhCommonSpec = nullptr; + ret = OH_CryptoAsymKeySpec_GenDhCommonParamsSpec(2048, 1024, &dhCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("DH", CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetCommonParamsSpec(keySpec, dhCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DH_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DH_SK_DATABLOB, &privKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + FreeDhKeyParams(&pubKeyData, &privKeyData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DH_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DH_SK_DATABLOB, &privKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + FreeDhKeyParams(&pubKeyData, &privKeyData); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = GenerateKeyPairWithSpec(keySpec, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPubKey(keyPair), nullptr); + EXPECT_EQ(OH_CryptoKeyPair_GetPrivKey(keyPair), nullptr); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoAsymKeySpec_Destroy(dhCommonSpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest016, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("DH_modp2048"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetDhKeyParams(keyCtx, &pubKeyData, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *dhCommonSpec = nullptr; + ret = OH_CryptoAsymKeySpec_GenDhCommonParamsSpec(2048, 1024, &dhCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("DH", CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetCommonParamsSpec(keySpec, dhCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DH_PK_DATABLOB, &pubKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DH_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDhKeyParams(&pubKeyData, &privKeyData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DH_PK_DATABLOB, &pubKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DH_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDhKeyParams(&pubKeyData, &privKeyData); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = GenerateKeyPairWithSpec(keySpec, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + EXPECT_EQ(OH_CryptoKeyPair_GetPubKey(keyPair), nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPrivKey(keyPair), nullptr); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoAsymKeySpec_Destroy(dhCommonSpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest017, TestSize.Level0) +{ + OH_CryptoAsymKeySpec *dhCommonSpec = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeySpec_GenDhCommonParamsSpec(2048, 1024, &dhCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("DH", CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetCommonParamsSpec(keySpec, dhCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob p = {0}; + Crypto_DataBlob g = {0}; + Crypto_DataBlob l = {0}; + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DH_P_DATABLOB, &p); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DH_G_DATABLOB, &g); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_DH_L_INT, &l); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDhCommonParams(&p, &g, &l); + + 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(dhCommonSpec); + OH_CryptoAsymKeySpec_Destroy(keySpec); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest018, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("X25519"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetX25519KeyParams(keyCtx, &pubKeyData, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("X25519", CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_X25519_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_X25519_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeX25519KeyParams(&pubKeyData, &privKeyData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_X25519_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_X25519_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeX25519KeyParams(&pubKeyData, &privKeyData); + + 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(keySpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest019, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("X25519"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetX25519KeyParams(keyCtx, &pubKeyData, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("X25519", CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_X25519_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_X25519_SK_DATABLOB, &privKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + FreeX25519KeyParams(&pubKeyData, &privKeyData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_X25519_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_X25519_SK_DATABLOB, &privKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + FreeX25519KeyParams(&pubKeyData, &privKeyData); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = GenerateKeyPairWithSpec(keySpec, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPubKey(keyPair), nullptr); + EXPECT_EQ(OH_CryptoKeyPair_GetPrivKey(keyPair), nullptr); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest020, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("X25519"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetX25519KeyParams(keyCtx, &pubKeyData, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("X25519", CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_X25519_PK_DATABLOB, &pubKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_X25519_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeX25519KeyParams(&pubKeyData, &privKeyData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_X25519_PK_DATABLOB, &pubKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_X25519_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeX25519KeyParams(&pubKeyData, &privKeyData); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = GenerateKeyPairWithSpec(keySpec, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + EXPECT_EQ(OH_CryptoKeyPair_GetPubKey(keyPair), nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPrivKey(keyPair), nullptr); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest021, TestSize.Level0) +{ + OH_CryptoAsymKeySpec *keySpec = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeySpec_Create("X25519", CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, &keySpec); + EXPECT_NE(ret, CRYPTO_SUCCESS); + OH_CryptoAsymKeySpec_Destroy(keySpec); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest022, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("ECC_BrainPoolP384r1"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyXData = {.data = nullptr, .len = 0}; + Crypto_DataBlob pubKeyYData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetEccKeyParams(keyCtx, &pubKeyXData, &pubKeyYData, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *ecCommonSpec = nullptr; + ret = OH_CryptoAsymKeySpec_GenEcCommonParamsSpec("NID_brainpoolP384r1", &ecCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("ECC", CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetCommonParamsSpec(keySpec, ecCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_PK_X_DATABLOB, &pubKeyXData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_PK_Y_DATABLOB, &pubKeyYData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeEccKeyParams(&pubKeyXData, &pubKeyYData, &privKeyData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_PK_X_DATABLOB, &pubKeyXData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_PK_Y_DATABLOB, &pubKeyYData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeEccKeyParams(&pubKeyXData, &pubKeyYData, &privKeyData); + + 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(keySpec); + OH_CryptoAsymKeySpec_Destroy(ecCommonSpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest023, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("ECC_BrainPoolP384r1"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyXData = {.data = nullptr, .len = 0}; + Crypto_DataBlob pubKeyYData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetEccKeyParams(keyCtx, &pubKeyXData, &pubKeyYData, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *ecCommonSpec = nullptr; + ret = OH_CryptoAsymKeySpec_GenEcCommonParamsSpec("NID_brainpoolP384r1", &ecCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("ECC", CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetCommonParamsSpec(keySpec, ecCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_PK_X_DATABLOB, &pubKeyXData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_PK_Y_DATABLOB, &pubKeyYData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_SK_DATABLOB, &privKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + FreeEccKeyParams(&pubKeyXData, &pubKeyYData, &privKeyData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_PK_X_DATABLOB, &pubKeyXData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_PK_Y_DATABLOB, &pubKeyYData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_SK_DATABLOB, &privKeyData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + FreeEccKeyParams(&pubKeyXData, &pubKeyYData, &privKeyData); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = GenerateKeyPairWithSpec(keySpec, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPubKey(keyPair), nullptr); + EXPECT_EQ(OH_CryptoKeyPair_GetPrivKey(keyPair), nullptr); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoAsymKeySpec_Destroy(ecCommonSpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest024, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("ECC_BrainPoolP384r1"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyXData = {.data = nullptr, .len = 0}; + Crypto_DataBlob pubKeyYData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetEccKeyParams(keyCtx, &pubKeyXData, &pubKeyYData, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *ecCommonSpec = nullptr; + ret = OH_CryptoAsymKeySpec_GenEcCommonParamsSpec("NID_brainpoolP384r1", &ecCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("ECC", CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetCommonParamsSpec(keySpec, ecCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_PK_X_DATABLOB, &pubKeyXData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_PK_Y_DATABLOB, &pubKeyYData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeEccKeyParams(&pubKeyXData, &pubKeyYData, &privKeyData); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_PK_X_DATABLOB, &pubKeyXData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_PK_Y_DATABLOB, &pubKeyYData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeEccKeyParams(&pubKeyXData, &pubKeyYData, &privKeyData); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = GenerateKeyPairWithSpec(keySpec, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + EXPECT_EQ(OH_CryptoKeyPair_GetPubKey(keyPair), nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPrivKey(keyPair), nullptr); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoAsymKeySpec_Destroy(ecCommonSpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest025, TestSize.Level0) +{ + OH_CryptoAsymKeySpec *ecCommonSpec = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeySpec_GenEcCommonParamsSpec("NID_brainpoolP384r1", &ecCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("ECC", CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetCommonParamsSpec(keySpec, ecCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob p = {0}; + Crypto_DataBlob a = {0}; + Crypto_DataBlob b = {0}; + Crypto_DataBlob gx = {0}; + Crypto_DataBlob gy = {0}; + Crypto_DataBlob n = {0}; + Crypto_DataBlob h = {0}; + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_FP_P_DATABLOB, &p); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_A_DATABLOB, &a); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_B_DATABLOB, &b); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_G_X_DATABLOB, &gx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_G_Y_DATABLOB, &gy); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_N_DATABLOB, &n); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_ECC_H_INT, &h); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeEccCommonParams(&p, &a, &b, &gx, &gy, &n, &h); + + 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(keySpec); + OH_CryptoAsymKeySpec_Destroy(ecCommonSpec); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest026, TestSize.Level0) +{ + OH_CryptoAsymKeySpec *ecCommonSpec = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeySpec_GenEcCommonParamsSpec("NID_brainpoolP384r1", &ecCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob p = {0}; + Crypto_DataBlob a = {0}; + Crypto_DataBlob b = {0}; + Crypto_DataBlob gx = {0}; + Crypto_DataBlob gy = {0}; + Crypto_DataBlob n = {0}; + Crypto_DataBlob h = {0}; + ret = OH_CryptoAsymKeySpec_GetParam(ecCommonSpec, CRYPTO_ECC_FP_P_DATABLOB, &p); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(ecCommonSpec, CRYPTO_ECC_A_DATABLOB, &a); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(ecCommonSpec, CRYPTO_ECC_B_DATABLOB, &b); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(ecCommonSpec, CRYPTO_ECC_G_X_DATABLOB, &gx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(ecCommonSpec, CRYPTO_ECC_G_Y_DATABLOB, &gy); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(ecCommonSpec, CRYPTO_ECC_N_DATABLOB, &n); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_GetParam(ecCommonSpec, CRYPTO_ECC_H_INT, &h); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("ECC", CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_FP_P_DATABLOB, &p); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_A_DATABLOB, &a); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_B_DATABLOB, &b); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_G_X_DATABLOB, &gx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_G_Y_DATABLOB, &gy); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_N_DATABLOB, &n); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_ECC_H_INT, &h); + 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); + FreeEccCommonParams(&p, &a, &b, &gx, &gy, &n, &h); + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoAsymKeySpec_Destroy(ecCommonSpec); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest027, TestSize.Level0) +{ + uint8_t prime256v1PointBlobData[] = { + 4, 153, 228, 156, 119, 184, 185, 120, 237, 233, 181, 77, 70, 183, 30, 68, 2, 70, 37, 251, 5, 22, + 199, 84, 87, 222, 65, 103, 8, 26, 255, 137, 206, 80, 159, 163, 46, 22, 104, 156, 169, 14, 149, 199, + 35, 201, 3, 160, 81, 251, 235, 236, 75, 137, 196, 253, 200, 116, 167, 59, 153, 241, 99, 90, 90}; + OH_CryptoEcPoint *point = nullptr; + Crypto_DataBlob prime256v1PointBlob = {prime256v1PointBlobData, sizeof(prime256v1PointBlobData)}; + OH_Crypto_ErrCode ret = OH_CryptoEcPoint_Create("NID_X9_62_prime256v1", &prime256v1PointBlob, &point); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(point, nullptr); + + Crypto_DataBlob ecPubKeyX = {0}; + Crypto_DataBlob ecPubKeyY = {0}; + ret = OH_CryptoEcPoint_GetCoordinate(point, &ecPubKeyX, &ecPubKeyY); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(ecPubKeyX.data, nullptr); + ASSERT_NE(ecPubKeyY.data, nullptr); + ASSERT_NE(ecPubKeyX.len, 0); + ASSERT_NE(ecPubKeyY.len, 0); + + OH_CryptoEcPoint *point2 = nullptr; + ret = OH_CryptoEcPoint_Create("NID_X9_62_prime256v1", nullptr, &point2); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(point2, nullptr); + + ret = OH_CryptoEcPoint_SetCoordinate(point2, &ecPubKeyX, &ecPubKeyY); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob returnPointBlobData = {0}; + ret = OH_CryptoEcPoint_Encode(point2, "UNCOMPRESSED", &returnPointBlobData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(returnPointBlobData.data, nullptr); + ASSERT_NE(returnPointBlobData.len, 0); + EXPECT_EQ(returnPointBlobData.len, prime256v1PointBlob.len); + EXPECT_EQ(memcmp(returnPointBlobData.data, prime256v1PointBlob.data, returnPointBlobData.len), 0); + + OH_Crypto_FreeDataBlob(&ecPubKeyX); + OH_Crypto_FreeDataBlob(&ecPubKeyY); + OH_Crypto_FreeDataBlob(&returnPointBlobData); + 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 diff --git a/test/unittest/src/native/native_kdf_test.cpp b/test/unittest/src/native/native_kdf_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fd57dfee744b66b4186d6b446e8a021c2886af7 --- /dev/null +++ b/test/unittest/src/native/native_kdf_test.cpp @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "crypto_common.h" +#include "crypto_asym_key.h" +#include "crypto_kdf.h" +#include "log.h" +#include "memory.h" +#include "memory_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class NativeKdfest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NativeKdfest::SetUpTestCase() {} +void NativeKdfest::TearDownTestCase() {} + +void NativeKdfest::SetUp() // add init here, this will be called before test. +{ +} + +void NativeKdfest::TearDown() // add destroy here, this will be called when test case done. +{ +} + +static const char *g_keyData = "012345678901234567890123456789"; +static const char *g_infoData = "infostring"; +static const char *g_saltData = "saltstring"; +static const char *g_password = "123456"; + +constexpr uint32_t KEY_NORMAL_LENGTH = 32; + +HWTEST_F(NativeKdfest, NativeKdfest001, TestSize.Level0) +{ + OH_CryptoKdfParams *params = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoKdfParams_Create("HKDF", ¶ms); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + Crypto_DataBlob key = {.data = reinterpret_cast(const_cast(g_keyData)), + .len = strlen(g_keyData)}; + Crypto_DataBlob salt = {.data = reinterpret_cast(const_cast(g_saltData)), + .len = strlen(g_saltData)}; + Crypto_DataBlob info = {.data = reinterpret_cast(const_cast(g_infoData)), + .len = strlen(g_infoData)}; + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_KEY_DATABLOB, &key); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_SALT_DATABLOB, &salt); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_INFO_DATABLOB, &info); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob out = {0}; + OH_CryptoKdf *kdfCtx = nullptr; + ret = OH_CryptoKdf_Create("HKDF|SHA256|EXTRACT_AND_EXPAND", &kdfCtx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdf_Derive(kdfCtx, params, KEY_NORMAL_LENGTH, &out); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_Crypto_FreeDataBlob(&out); + OH_CryptoKdf_Destroy(kdfCtx); + OH_CryptoKdfParams_Destroy(params); +} + +HWTEST_F(NativeKdfest, NativeKdfest002, TestSize.Level0) +{ + OH_CryptoKdfParams *params = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoKdfParams_Create("PBKDF2", ¶ms); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob password = {.data = reinterpret_cast(const_cast(g_password)), + .len = strlen(g_password)}; + Crypto_DataBlob salt = {.data = reinterpret_cast(const_cast(g_saltData)), + .len = strlen(g_saltData)}; + int iterations = 10000; + Crypto_DataBlob iterationsData = {.data = reinterpret_cast(&iterations), .len = sizeof(int)}; + + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_KEY_DATABLOB, &password); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_SALT_DATABLOB, &salt); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_ITER_COUNT_INT, &iterationsData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob out = {0}; + OH_CryptoKdf *kdfCtx = nullptr; + ret = OH_CryptoKdf_Create("PBKDF2|SHA256", &kdfCtx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdf_Derive(kdfCtx, params, KEY_NORMAL_LENGTH, &out); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_Crypto_FreeDataBlob(&out); + OH_CryptoKdf_Destroy(kdfCtx); + OH_CryptoKdfParams_Destroy(params); +} + +HWTEST_F(NativeKdfest, NativeKdfest003, TestSize.Level0) +{ + OH_CryptoKdfParams *params = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoKdfParams_Create("SCRYPT", ¶ms); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob password = {.data = reinterpret_cast(const_cast(g_password)), + .len = strlen(g_password)}; + Crypto_DataBlob salt = {.data = reinterpret_cast(const_cast(g_saltData)), + .len = strlen(g_saltData)}; + + uint64_t n = 1024; + uint64_t p = 16; + uint64_t r = 8; + uint64_t maxMem = 1067008; + Crypto_DataBlob nData = {.data = reinterpret_cast(&n), .len = sizeof(uint64_t)}; + Crypto_DataBlob pData = {.data = reinterpret_cast(&p), .len = sizeof(uint64_t)}; + Crypto_DataBlob rData = {.data = reinterpret_cast(&r), .len = sizeof(uint64_t)}; + Crypto_DataBlob maxMemData = {.data = reinterpret_cast(&maxMem), .len = sizeof(uint64_t)}; + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_KEY_DATABLOB, &password); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_SALT_DATABLOB, &salt); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_SCRYPT_N_UINT64, &nData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_SCRYPT_P_UINT64, &pData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_SCRYPT_R_UINT64, &rData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdfParams_SetParam(params, CRYPTO_KDF_SCRYPT_MAX_MEM_UINT64, &maxMemData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob out = {0}; + OH_CryptoKdf *kdfCtx = nullptr; + ret = OH_CryptoKdf_Create("SCRYPT", &kdfCtx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdf_Derive(kdfCtx, params, KEY_NORMAL_LENGTH, &out); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_Crypto_FreeDataBlob(&out); + OH_CryptoKdf_Destroy(kdfCtx); + OH_CryptoKdfParams_Destroy(params); +} + +HWTEST_F(NativeKdfest, NativeKdfest004, TestSize.Level0) +{ + OH_CryptoKdfParams *params = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoKdfParams_Create("SCRYPT", nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdfParams_Create("XXXX", ¶ms); + EXPECT_NE(ret, CRYPTO_SUCCESS); +} + +HWTEST_F(NativeKdfest, NativeKdfest005, TestSize.Level0) +{ + Crypto_DataBlob out = {0}; + OH_CryptoKdf *kdfCtx = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoKdf_Create("HKDF|SHA256|EXTRACT_AND_EXPAND", &kdfCtx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKdf_Derive(kdfCtx, nullptr, KEY_NORMAL_LENGTH, &out); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + OH_Crypto_FreeDataBlob(&out); + OH_CryptoKdf_Destroy(kdfCtx); +} +} \ No newline at end of file diff --git a/test/unittest/src/native/native_key_agreement_test.cpp b/test/unittest/src/native/native_key_agreement_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c47a5e18f6895e76f6ab7075869c23ff2006edda --- /dev/null +++ b/test/unittest/src/native/native_key_agreement_test.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "crypto_common.h" +#include "crypto_asym_key.h" +#include "crypto_key_agreement.h" +#include "log.h" +#include "memory.h" +#include "memory_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class NativeKeyAgreementTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NativeKeyAgreementTest::SetUpTestCase() {} +void NativeKeyAgreementTest::TearDownTestCase() {} + +void NativeKeyAgreementTest::SetUp() // add init here, this will be called before test. +{ +} + +void NativeKeyAgreementTest::TearDown() // add destroy here, this will be called when test case done. +{ +} + +HWTEST_F(NativeKeyAgreementTest, NativeKeyAgreementTest001, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator* generator = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("X25519", &generator); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoKeyPair *keyPairC = nullptr; + ret = OH_CryptoAsymKeyGenerator_Generate(generator, &keyPairC); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoKeyPair *keyPairS = nullptr; + ret = OH_CryptoAsymKeyGenerator_Generate(generator, &keyPairS); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoPrivKey *privkey = OH_CryptoKeyPair_GetPrivKey(keyPairC); + ASSERT_NE(privkey, nullptr); + OH_CryptoPubKey *pubkey = OH_CryptoKeyPair_GetPubKey(keyPairS); + ASSERT_NE(pubkey, nullptr); + + OH_CryptoKeyAgreement *ctx = nullptr; + ret = OH_CryptoKeyAgreement_Create("X25519", &ctx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob key = {0}; + ret = OH_CryptoKeyAgreement_GenerateSecret(nullptr, privkey, pubkey, &key); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKeyAgreement_GenerateSecret(ctx, nullptr, pubkey, &key); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKeyAgreement_GenerateSecret(ctx, privkey, nullptr, &key); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKeyAgreement_GenerateSecret(ctx, privkey, pubkey, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoKeyAgreement_GenerateSecret(ctx, privkey, pubkey, &key); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_Crypto_FreeDataBlob(&key); + OH_CryptoKeyAgreement_Destroy(ctx); + OH_CryptoKeyPair_Destroy(keyPairC); + OH_CryptoKeyPair_Destroy(keyPairS); + OH_CryptoAsymKeyGenerator_Destroy(generator); +} + +HWTEST_F(NativeKeyAgreementTest, NativeKeyAgreementTest002, TestSize.Level0) +{ + OH_Crypto_ErrCode ret = OH_CryptoKeyAgreement_Create("X25519", nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); +} +} \ No newline at end of file diff --git a/test/unittest/src/native/native_mac_test.cpp b/test/unittest/src/native/native_mac_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..086f554e46f89c92e9bd62a75d7183ac59498622 --- /dev/null +++ b/test/unittest/src/native/native_mac_test.cpp @@ -0,0 +1,208 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "crypto_common.h" +#include "crypto_sym_key.h" +#include "crypto_mac.h" +#include "log.h" +#include "memory.h" +#include "memory_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class NativeMacTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NativeMacTest::SetUpTestCase() {} +void NativeMacTest::TearDownTestCase() {} + +void NativeMacTest::SetUp() // add init here, this will be called before test. +{ +} + +void NativeMacTest::TearDown() // add destroy here, this will be called when test case done. +{ +} + +static OH_CryptoSymKey *GenSymKey(const char *algoName) +{ + OH_CryptoSymKeyGenerator *keyGen = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create(algoName, &keyGen); + if (ret != CRYPTO_SUCCESS) { + return nullptr; + } + OH_CryptoSymKey *keyCtx = nullptr; + ret = OH_CryptoSymKeyGenerator_Generate(keyGen, &keyCtx); + OH_CryptoSymKeyGenerator_Destroy(keyGen); + if (ret != CRYPTO_SUCCESS) { + return nullptr; + } + return keyCtx; +} + +static OH_Crypto_ErrCode CalculateMacTest(OH_CryptoMac *ctx, OH_CryptoSymKey *keyCtx) +{ + OH_Crypto_ErrCode ret = OH_CryptoMac_Init(ctx, keyCtx); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + const char *data = "hello world"; + const char *data2 = "hello openharmony"; + const Crypto_DataBlob in = {.data = reinterpret_cast(const_cast(data)), .len = strlen(data)}; + const Crypto_DataBlob in2 = {.data = reinterpret_cast(const_cast(data2)), .len = strlen(data2)}; + ret = OH_CryptoMac_Update(ctx, &in); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + ret = OH_CryptoMac_Update(ctx, &in2); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + + Crypto_DataBlob out = {0}; + ret = OH_CryptoMac_Final(ctx, &out); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + OH_Crypto_FreeDataBlob(&out); + uint32_t macLen = 0; + ret = OH_CryptoMac_GetLength(ctx, &macLen); + if (ret != CRYPTO_SUCCESS) { + return ret; + } + return CRYPTO_SUCCESS; +} + +HWTEST_F(NativeMacTest, NativeMacTest001, TestSize.Level0) +{ + OH_CryptoSymKey *keyCtx = GenSymKey("HMAC|SM3"); + ASSERT_NE(keyCtx, nullptr); + + OH_CryptoMac *ctx = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoMac_Create("HMAC", &ctx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + const char *digestName = "SM3"; + Crypto_DataBlob digestNameData = {.data = reinterpret_cast(const_cast(digestName)), + .len = strlen(digestName)}; + ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_DIGEST_NAME_STR, &digestNameData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = CalculateMacTest(ctx, keyCtx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoMac_Destroy(ctx); + OH_CryptoSymKey_Destroy(keyCtx); +} + +HWTEST_F(NativeMacTest, NativeMacTest002, TestSize.Level0) +{ + OH_CryptoSymKey *keyCtx = GenSymKey("AES128"); + ASSERT_NE(keyCtx, nullptr); + + OH_CryptoMac *ctx = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoMac_Create("CMAC", &ctx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + const char *cipherName = "AES128"; + Crypto_DataBlob cipherNameData = {.data = reinterpret_cast(const_cast(cipherName)), + .len = strlen(cipherName)}; + ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_CIPHER_NAME_STR, &cipherNameData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = CalculateMacTest(ctx, keyCtx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoMac_Destroy(ctx); + OH_CryptoSymKey_Destroy(keyCtx); +} + +HWTEST_F(NativeMacTest, NativeMacTest003, TestSize.Level0) +{ + OH_CryptoMac *ctx = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoMac_Create(nullptr, &ctx); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoMac_Create("HMAC", nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoMac_Create("XMAC", &ctx); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoMac_Create("HMAC", &ctx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoMac_Destroy(ctx); +} + +HWTEST_F(NativeMacTest, NativeMacTest004, TestSize.Level0) +{ + OH_CryptoMac *ctx = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoMac_Create("HMAC", &ctx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + const char *cipherName = "AES128"; + Crypto_DataBlob cipherNameData = {.data = reinterpret_cast(const_cast(cipherName)), + .len = strlen(cipherName)}; + ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_CIPHER_NAME_STR, &cipherNameData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + const char *digestName = "SM3"; + Crypto_DataBlob digestNameData = {.data = reinterpret_cast(const_cast(digestName)), + .len = strlen(digestName)}; + ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_DIGEST_NAME_STR, &digestNameData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoMac_Destroy(ctx); +} + +HWTEST_F(NativeMacTest, NativeMacTest005, TestSize.Level0) +{ + OH_CryptoMac *ctx = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoMac_Create("CMAC", &ctx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + const char *cipherName = "AES128"; + Crypto_DataBlob cipherNameData = {.data = reinterpret_cast(const_cast(cipherName)), + .len = strlen(cipherName)}; + ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_CIPHER_NAME_STR, &cipherNameData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + const char *digestName = "SM3"; + Crypto_DataBlob digestNameData = {.data = reinterpret_cast(const_cast(digestName)), + .len = strlen(digestName)}; + ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_DIGEST_NAME_STR, &digestNameData); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + OH_CryptoMac_Destroy(ctx); +} + +HWTEST_F(NativeMacTest, NativeMacTest006, TestSize.Level0) +{ + OH_CryptoMac *ctx = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoMac_Create("CMAC", &ctx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoMac_Init(ctx, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoMac_Update(ctx, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoMac_Final(ctx, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoMac_GetLength(ctx, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + + OH_CryptoMac_Destroy(ctx); +} +} \ No newline at end of file diff --git a/test/unittest/src/native/native_rand_test.cpp b/test/unittest/src/native/native_rand_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a56e78a2ac80d374ef493c0be11d18e0249676b --- /dev/null +++ b/test/unittest/src/native/native_rand_test.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "crypto_common.h" +#include "crypto_rand.h" +#include "log.h" +#include "memory.h" +#include "memory_mock.h" + +using namespace std; +using namespace testing::ext; + +class NativeRandTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NativeRandTest::SetUpTestCase() {} + +void NativeRandTest::TearDownTestCase() {} + +void NativeRandTest::SetUp() {} + +void NativeRandTest::TearDown() {} + +HWTEST_F(NativeRandTest, NativeRandTest001, TestSize.Level0) +{ + OH_CryptoRand *rand = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoRand_Create(&rand); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(rand, nullptr); + + uint8_t seedData[12] = {0x25, 0x65, 0x58, 0x89, 0x85, 0x55, 0x66, 0x77, 0x88, 0x99, 0x11, 0x22}; + Crypto_DataBlob seed = { + .data = seedData, + .len = sizeof(seedData) + }; + ret = OH_CryptoRand_SetSeed(rand, &seed); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob out = { 0 }; + ret = OH_CryptoRand_GenerateRandom(rand, 10, &out); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_EQ(out.len, 10); + + const char *algoName = OH_CryptoRand_GetAlgoName(rand); + ASSERT_NE(algoName, nullptr); + EXPECT_GT(strlen(algoName), 0); + + OH_Crypto_FreeDataBlob(&out); + OH_CryptoRand_Destroy(rand); +} + +HWTEST_F(NativeRandTest, NativeRandTest002, TestSize.Level0) +{ + OH_Crypto_ErrCode ret = OH_CryptoRand_Create(nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); +} + +HWTEST_F(NativeRandTest, NativeRandTest003, TestSize.Level0) +{ + OH_CryptoRand *rand = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoRand_Create(&rand); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(rand, nullptr); + + uint8_t seedData[12] = {0x25, 0x65, 0x58, 0x89, 0x85, 0x55, 0x66, 0x77, 0x88, 0x99, 0x11, 0x22}; + Crypto_DataBlob seed = { + .data = seedData, + .len = sizeof(seedData) + }; + ret = OH_CryptoRand_SetSeed(nullptr, &seed); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoRand_SetSeed(rand, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoRand_SetSeed(rand, &seed); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoRand_Destroy(rand); +} + +HWTEST_F(NativeRandTest, NativeRandTest004, TestSize.Level0) +{ + OH_CryptoRand *rand = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoRand_Create(&rand); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(rand, nullptr); + + uint8_t seedData[12] = {0x25, 0x65, 0x58, 0x89, 0x85, 0x55, 0x66, 0x77, 0x88, 0x99, 0x11, 0x22}; + Crypto_DataBlob seed = { + .data = seedData, + .len = sizeof(seedData) + }; + ret = OH_CryptoRand_SetSeed(rand, &seed); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob out = { 0 }; + ret = OH_CryptoRand_GenerateRandom(nullptr, 10, &out); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoRand_GenerateRandom(rand, 10, nullptr); + EXPECT_NE(ret, CRYPTO_SUCCESS); + ret = OH_CryptoRand_GenerateRandom(rand, 10, &out); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_Crypto_FreeDataBlob(&out); + OH_CryptoRand_Destroy(rand); +} + +HWTEST_F(NativeRandTest, NativeRandTest005, TestSize.Level0) +{ + const char *algoName = OH_CryptoRand_GetAlgoName(nullptr); + EXPECT_EQ(algoName, nullptr); +} \ No newline at end of file diff --git a/test/unittest/src/native/native_signature_test.cpp b/test/unittest/src/native/native_signature_test.cpp index ffd79d18ffcd52ac14da4ecba93f92205ab4a858..d7b73a413516a6ae77c5c5d645bdc82c55068fee 100644 --- a/test/unittest/src/native/native_signature_test.cpp +++ b/test/unittest/src/native/native_signature_test.cpp @@ -200,11 +200,11 @@ HWTEST_F(NativeSignatureTest, NativeSignatureTest_Sign002, TestSize.Level0) uint8_t buf[] = {32}; Crypto_DataBlob value = {.data = reinterpret_cast(buf), .len = sizeof(buf)}; res = OH_CryptoSign_SetParam(sign, CRYPTO_PSS_SALT_LEN_INT, &value); - EXPECT_EQ(res, CRYPTO_INVALID_PARAMS); + EXPECT_EQ(res, CRYPTO_PARAMETER_CHECK_FAILED); Crypto_DataBlob outValue = {.data = nullptr, .len = 0}; res = OH_CryptoSign_GetParam(sign, CRYPTO_PSS_SALT_LEN_INT, &outValue); - EXPECT_EQ(res, CRYPTO_INVALID_PARAMS); + EXPECT_EQ(res, CRYPTO_PARAMETER_CHECK_FAILED); OH_CryptoSign_Destroy(sign); }