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/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 c8487bce577fdd0ecf14aadbabc78c393329d9e3..d8fb9cd80977ed04716815bfcee20a7d10840e0d 100644 --- a/frameworks/native/src/asym_key.c +++ b/frameworks/native/src/asym_key.c @@ -40,18 +40,8 @@ typedef struct OH_CryptoAsymKeyGenerator { - HcfObjectBase base; - - HcfResult (*generateKeyPair)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, - HcfKeyPair **returnKeyPair); - - HcfResult (*convertKey)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob, - HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair); - - HcfResult (*convertPemKey)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, const char *pubKeyStr, - const char *priKeyStr, HcfKeyPair **returnKeyPair); - - const char *(*getAlgoName)(HcfAsyKeyGenerator *self); + HcfAsyKeyGenerator *base; + HcfKeyDecodingParamsSpec *decSpec; } OH_CryptoAsymKeyGenerator; typedef struct OH_CryptoKeyPair { @@ -120,23 +110,52 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Create(const char *algoName, OH_Cryp if (ctx == NULL) { return CRYPTO_INVALID_PARAMS; } - HcfResult ret = HcfAsyKeyGeneratorCreate(algoName, (HcfAsyKeyGenerator **)ctx); + OH_CryptoAsymKeyGenerator *tmpCtx = HcfMalloc(sizeof(OH_CryptoAsymKeyGenerator), 0); + if (tmpCtx == NULL) { + return CRYPTO_MEMORY_ERROR; + } + HcfResult ret = HcfAsyKeyGeneratorCreate(algoName, &(tmpCtx->base)); + if (ret != HCF_SUCCESS) { + HcfFree(tmpCtx); + return GetOhCryptoErrCode(ret); + } + *ctx = tmpCtx; return GetOhCryptoErrCode(ret); } OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Generate(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoKeyPair **keyCtx) { - if ((ctx == NULL) || (ctx->generateKeyPair == NULL) || (keyCtx == NULL)) { + if ((ctx == NULL) || (ctx->base == NULL) || (ctx->base->generateKeyPair == NULL) || (keyCtx == NULL)) { return CRYPTO_INVALID_PARAMS; } - HcfResult ret = ctx->generateKeyPair((HcfAsyKeyGenerator *)ctx, NULL, (HcfKeyPair **)keyCtx); + HcfResult ret = ctx->base->generateKeyPair((HcfAsyKeyGenerator *)(ctx->base), NULL, (HcfKeyPair **)keyCtx); return GetOhCryptoErrCode(ret); } +OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_SetPassword(OH_CryptoAsymKeyGenerator *ctx, const unsigned char *password, + uint32_t passwordLen) +{ + if ((ctx == NULL) || (password == NULL) || (passwordLen == 0)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfKeyDecodingParamsSpec *decSpec = (HcfKeyDecodingParamsSpec *)HcfMalloc(sizeof(HcfKeyDecodingParamsSpec), 0); + if (decSpec == NULL) { + return CRYPTO_MEMORY_ERROR; + } + decSpec->password = (char *)HcfMalloc(passwordLen + 1, 0); + if (decSpec->password == NULL) { + HcfFree(decSpec); + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(decSpec->password, passwordLen, password, passwordLen); + ctx->decSpec = decSpec; + return CRYPTO_SUCCESS; +} + OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *ctx, Crypto_EncodingType type, Crypto_DataBlob *pubKeyData, Crypto_DataBlob *priKeyData, OH_CryptoKeyPair **keyCtx) { - if ((ctx == NULL) || (pubKeyData == NULL && priKeyData == NULL) || (keyCtx == NULL)) { + if ((ctx == NULL) || (ctx->base == NULL) || (pubKeyData == NULL && priKeyData == NULL) || (keyCtx == NULL)) { return CRYPTO_INVALID_PARAMS; } HcfResult ret = HCF_SUCCESS; @@ -144,13 +163,14 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *c const char *pubKeyStr = (pubKeyData == NULL)? NULL : (const char *)pubKeyData->data; switch (type) { case CRYPTO_PEM: - ret = ctx->convertPemKey == NULL ? HCF_INVALID_PARAMS : - ctx->convertPemKey((HcfAsyKeyGenerator *)ctx, NULL, pubKeyStr, priKeyStr, (HcfKeyPair **)keyCtx); + ret = ctx->base->convertPemKey == NULL ? HCF_INVALID_PARAMS : + ctx->base->convertPemKey((HcfAsyKeyGenerator *)(ctx->base), (HcfParamsSpec *)(ctx->decSpec), pubKeyStr, + priKeyStr, (HcfKeyPair **)keyCtx); break; case CRYPTO_DER: - ret = ctx->convertKey == NULL ? HCF_INVALID_PARAMS : - ctx->convertKey((HcfAsyKeyGenerator *)ctx, NULL, (HcfBlob *)pubKeyData, - (HcfBlob *)priKeyData, (HcfKeyPair **)keyCtx); + ret = ctx->base->convertKey == NULL ? HCF_INVALID_PARAMS : + ctx->base->convertKey((HcfAsyKeyGenerator *)(ctx->base), (HcfParamsSpec *)(ctx->decSpec), + (HcfBlob *)pubKeyData, (HcfBlob *)priKeyData, (HcfKeyPair **)keyCtx); break; default: return CRYPTO_INVALID_PARAMS; @@ -160,18 +180,32 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *c const char *OH_CryptoAsymKeyGenerator_GetAlgoName(OH_CryptoAsymKeyGenerator *ctx) { - if ((ctx == NULL) || (ctx->getAlgoName == NULL)) { + if ((ctx == NULL) || (ctx->base == NULL) || (ctx->base->getAlgoName == NULL)) { return NULL; } - return ctx->getAlgoName((HcfAsyKeyGenerator *)ctx); + return ctx->base->getAlgoName((HcfAsyKeyGenerator *)(ctx->base)); +} + +static void FreeDecParamsSpec(HcfKeyDecodingParamsSpec *decSpec) +{ + if (decSpec == NULL) { + return; + } + HcfFree(decSpec->password); + decSpec->password = NULL; + HcfFree(decSpec); } void OH_CryptoAsymKeyGenerator_Destroy(OH_CryptoAsymKeyGenerator *ctx) { - if ((ctx == NULL) || (ctx->base.destroy == NULL)) { + if (ctx == NULL) { return; } - ctx->base.destroy((HcfObjectBase *)ctx); + HcfObjDestroy(ctx->base); + ctx->base = NULL; + FreeDecParamsSpec(ctx->decSpec); + ctx->decSpec = NULL; + HcfFree(ctx); } void OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *keyCtx) @@ -276,6 +310,7 @@ OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_P } value->data = (uint8_t *)returnInt; value->len = sizeof(int32_t); + ReverseUint8Arr(value->data, value->len); break; case CRYPTO_ECC_FIELD_TYPE_STR: case CRYPTO_ECC_CURVE_NAME_STR: @@ -295,6 +330,7 @@ OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_P } value->data = (uint8_t *)bigIntValue.data; value->len = (size_t)bigIntValue.len; + ReverseUint8Arr(value->data, value->len); break; } return GetOhCryptoErrCode(ret); @@ -315,7 +351,7 @@ OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_Create(OH_CryptoPrivKeyEncoding OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_SetParam(OH_CryptoPrivKeyEncodingParams *ctx, CryptoPrivKeyEncoding_ParamType type, Crypto_DataBlob *value) { - if ((ctx == NULL) || (value == NULL)) { + if ((ctx == NULL) || (value == NULL) || (value->data == NULL) || (value->len == 0)) { return CRYPTO_PARAMETER_CHECK_FAILED; } char *data = (char *)HcfMalloc(value->len + 1, 0); @@ -415,6 +451,7 @@ OH_Crypto_ErrCode OH_CryptoPrivKey_GetParam(OH_CryptoPrivKey *key, CryptoAsymKey } value->data = (uint8_t *)returnInt; value->len = sizeof(int32_t); + ReverseUint8Arr(value->data, value->len); break; case CRYPTO_ECC_FIELD_TYPE_STR: case CRYPTO_ECC_CURVE_NAME_STR: @@ -434,6 +471,7 @@ OH_Crypto_ErrCode OH_CryptoPrivKey_GetParam(OH_CryptoPrivKey *key, CryptoAsymKey } value->data = (uint8_t *)bigIntValue.data; value->len = (size_t)bigIntValue.len; + ReverseUint8Arr(value->data, value->len); break; } return GetOhCryptoErrCodeNew(ret); @@ -470,7 +508,6 @@ typedef struct { } OH_CryptoAsymKeySpecInfoMap; static OH_CryptoAsymKeySpecInfo g_rsaSpecInfo[] = { - {CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, sizeof(HcfRsaCommParamsSpec)}, {CRYPTO_ASYM_KEY_PUBLIC_KEY_SPEC, sizeof(HcfRsaPubKeyParamsSpec)}, {CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, sizeof(HcfRsaKeyPairParamsSpec)}, }; @@ -530,26 +567,34 @@ static OH_Crypto_ErrCode CreateAsymKeySpec(const char *algoName, CryptoAsymKeySp 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; } - HcfAsyKeyGenParams params = { 0 }; - HcfResult ret = ParseAlgNameToParams(algoName, ¶ms); - if (ret != HCF_SUCCESS) { - return GetOhCryptoErrCodeNew(ret); + const OH_CryptoAsymKeySpecInfoMap *infoMap = FindAsymKeySpecInfoMapByAlgoName(algoName); + if (infoMap == NULL) { + return CRYPTO_PARAMETER_CHECK_FAILED; } - OH_CryptoAsymKeySpecInfoMap *infoMap = g_asymKeySpecInfoMap; - for (uint32_t i = 0; i < (sizeof(g_asymKeySpecInfoMap) / sizeof(g_asymKeySpecInfoMap[0])); ++i) { - if (infoMap[i].algo == params.algo) { - for (uint32_t j = 0; j < infoMap[i].specInfoSize; ++j) { - if (infoMap[i].specInfo[j].type == type) { - return CreateAsymKeySpec(algoName, type, infoMap[i].specInfo[j].memSize, spec); - } - } - return CRYPTO_PARAMETER_CHECK_FAILED; + 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; @@ -568,6 +613,7 @@ static OH_Crypto_ErrCode SetDataBlob(uint8_t **dest, uint32_t *destLen, Crypto_D HcfFree(*dest); *dest = tmp; *destLen = value->len; + ReverseUint8Arr(*dest, *destLen); return CRYPTO_SUCCESS; } @@ -582,12 +628,14 @@ static OH_Crypto_ErrCode GetDataBlob(const uint8_t *src, uint32_t srcLen, Crypto } (void)memcpy_s(value->data, srcLen, src, srcLen); value->len = srcLen; + ReverseUint8Arr(value->data, value->len); return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode SetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_P_DATABLOB: return SetDataBlob(&(spec->p.data), &(spec->p.len), value); case CRYPTO_DSA_Q_DATABLOB: @@ -599,9 +647,10 @@ static OH_Crypto_ErrCode SetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKe } } -static OH_Crypto_ErrCode SetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_PK_DATABLOB: return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); default: @@ -609,9 +658,10 @@ static OH_Crypto_ErrCode SetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode SetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); case CRYPTO_DSA_PK_DATABLOB: @@ -626,7 +676,7 @@ static OH_Crypto_ErrCode SetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (SetDsaCommSpec((HcfDsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PUBLIC_KEY_SPEC: return SetDsaPubKeySpec((HcfDsaPubKeyParamsSpec *)spec, type, value); case HCF_KEY_PAIR_SPEC: @@ -636,9 +686,10 @@ static OH_Crypto_ErrCode SetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa } } -static OH_Crypto_ErrCode SetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_N_DATABLOB: return SetDataBlob(&(spec->n.data), &(spec->n.len), value); default: @@ -646,9 +697,10 @@ static OH_Crypto_ErrCode SetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKe } } -static OH_Crypto_ErrCode SetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_E_DATABLOB: return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); default: @@ -656,9 +708,10 @@ static OH_Crypto_ErrCode SetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode SetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_D_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); case CRYPTO_RSA_E_DATABLOB: @@ -673,7 +726,7 @@ static OH_Crypto_ErrCode SetRsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (SetRsaCommSpec((HcfRsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PUBLIC_KEY_SPEC: return SetRsaPubKeySpec((HcfRsaPubKeyParamsSpec *)spec, type, value); case HCF_KEY_PAIR_SPEC: @@ -705,13 +758,15 @@ static OH_Crypto_ErrCode SetEccField(HcfEccCommParamsSpec *spec, Crypto_DataBlob } (void)memcpy_s(field->p.data, value->len, value->data, value->len); field->p.len = value->len; + ReverseUint8Arr(field->p.data, field->p.len); spec->field = (HcfECField *)field; return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode SetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_FP_P_DATABLOB: return SetEccField(spec, value); case CRYPTO_ECC_A_DATABLOB: @@ -728,7 +783,7 @@ static OH_Crypto_ErrCode SetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKe if (value->len != sizeof(spec->h)) { return CRYPTO_PARAMETER_CHECK_FAILED; } - spec->h = *((int *)(value->data)); + spec->h = bigEndianArrToInt32(value->data, value->len); break; default: return CRYPTO_PARAMETER_CHECK_FAILED; @@ -736,9 +791,10 @@ static OH_Crypto_ErrCode SetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKe return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode SetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); default: @@ -746,9 +802,10 @@ static OH_Crypto_ErrCode SetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymK } } -static OH_Crypto_ErrCode SetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_PK_X_DATABLOB: return SetDataBlob(&(spec->pk.x.data), &(spec->pk.x.len), value); case CRYPTO_ECC_PK_Y_DATABLOB: @@ -758,9 +815,10 @@ static OH_Crypto_ErrCode SetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode SetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); case CRYPTO_ECC_PK_X_DATABLOB: @@ -777,7 +835,7 @@ static OH_Crypto_ErrCode SetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (SetEccCommSpec((HcfEccCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return SetEccPriSpec((HcfEccPriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -791,7 +849,7 @@ static OH_Crypto_ErrCode SetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa static OH_Crypto_ErrCode SetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_P_DATABLOB: return SetDataBlob(&(spec->p.data), &(spec->p.len), value); case CRYPTO_DH_G_DATABLOB: @@ -800,7 +858,7 @@ static OH_Crypto_ErrCode SetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ if (value->len != sizeof(spec->length)) { return CRYPTO_PARAMETER_CHECK_FAILED; } - spec->length = *((int *)(value->data)); + spec->length = bigEndianArrToInt(value->data, value->len); break; default: return CRYPTO_PARAMETER_CHECK_FAILED; @@ -810,7 +868,7 @@ static OH_Crypto_ErrCode SetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ static OH_Crypto_ErrCode SetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); default: @@ -818,9 +876,10 @@ static OH_Crypto_ErrCode SetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey } } -static OH_Crypto_ErrCode SetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_PK_DATABLOB: return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); default: @@ -828,9 +887,10 @@ static OH_Crypto_ErrCode SetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsym } } -static OH_Crypto_ErrCode SetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); case CRYPTO_DH_PK_DATABLOB: @@ -845,7 +905,7 @@ static OH_Crypto_ErrCode SetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Par if (SetDhCommSpec((HcfDhCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return SetDhPriSpec((HcfDhPriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -857,9 +917,10 @@ static OH_Crypto_ErrCode SetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Par } } -static OH_Crypto_ErrCode SetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_SK_DATABLOB: case CRYPTO_X25519_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); @@ -868,9 +929,10 @@ static OH_Crypto_ErrCode SetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, C } } -static OH_Crypto_ErrCode SetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_PK_DATABLOB: case CRYPTO_X25519_PK_DATABLOB: return SetDataBlob(&(spec->pk.data), &(spec->pk.len), value); @@ -879,9 +941,10 @@ static OH_Crypto_ErrCode SetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec } } -static OH_Crypto_ErrCode SetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_SK_DATABLOB: case CRYPTO_X25519_SK_DATABLOB: return SetDataBlob(&(spec->sk.data), &(spec->sk.len), value); @@ -893,9 +956,10 @@ static OH_Crypto_ErrCode SetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *sp } } -static OH_Crypto_ErrCode SetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return SetAlg25519PriSpec((HcfAlg25519PriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -920,7 +984,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, Cryp return GetOhCryptoErrCodeNew(ret); } - switch(params.algo) { + switch (params.algo) { case HCF_ALG_DSA: return SetDsaSpec(spec, type, value); case HCF_ALG_RSA: @@ -938,6 +1002,32 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, Cryp } } +static OH_Crypto_ErrCode SetDsaCommonSpec(HcfDsaCommParamsSpec *commonParamsSpec, HcfDsaCommParamsSpec *spec) +{ + spec->p.data = (unsigned char *)HcfMalloc(commonParamsSpec->p.len, 0); + if (spec->p.data == NULL) { + FreeDsaCommParamsSpec(spec); + return CRYPTO_MEMORY_ERROR; + } + spec->q.data = (unsigned char *)HcfMalloc(commonParamsSpec->q.len, 0); + if (spec->q.data == NULL) { + FreeDsaCommParamsSpec(spec); + return CRYPTO_MEMORY_ERROR; + } + spec->g.data = (unsigned char *)HcfMalloc(commonParamsSpec->g.len, 0); + if (spec->g.data == NULL) { + FreeDsaCommParamsSpec(spec); + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(spec->p.data, commonParamsSpec->p.len, commonParamsSpec->p.data, commonParamsSpec->p.len); + (void)memcpy_s(spec->q.data, commonParamsSpec->q.len, commonParamsSpec->q.data, commonParamsSpec->q.len); + (void)memcpy_s(spec->g.data, commonParamsSpec->g.len, commonParamsSpec->g.data, commonParamsSpec->g.len); + spec->p.len = commonParamsSpec->p.len; + spec->q.len = commonParamsSpec->q.len; + spec->g.len = commonParamsSpec->g.len; + return CRYPTO_SUCCESS; +} + static OH_Crypto_ErrCode SetEccCommonSpec(HcfEccCommParamsSpec *commonParamsSpec, HcfEccCommParamsSpec *spec) { HcfEccCommParamsSpec eccCommParamsSpec = {}; @@ -992,7 +1082,9 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec return GetOhCryptoErrCodeNew(ret); } - switch(params.algo) { + switch (params.algo) { + case HCF_ALG_DSA: + return SetDsaCommonSpec((HcfDsaCommParamsSpec *)commonParamsSpec, (HcfDsaCommParamsSpec *)spec); case HCF_ALG_ECC: case HCF_ALG_SM2: return SetEccCommonSpec((HcfEccCommParamsSpec *)commonParamsSpec, (HcfEccCommParamsSpec *)spec); @@ -1003,9 +1095,10 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec } } -static OH_Crypto_ErrCode GetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_P_DATABLOB: return GetDataBlob(spec->p.data, spec->p.len, value); case CRYPTO_DSA_Q_DATABLOB: @@ -1017,9 +1110,10 @@ static OH_Crypto_ErrCode GetDsaCommSpec(HcfDsaCommParamsSpec *spec, CryptoAsymKe } } -static OH_Crypto_ErrCode GetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_PK_DATABLOB: return GetDataBlob(spec->pk.data, spec->pk.len, value); default: @@ -1027,9 +1121,10 @@ static OH_Crypto_ErrCode GetDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode GetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DSA_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); case CRYPTO_DSA_PK_DATABLOB: @@ -1044,7 +1139,7 @@ static OH_Crypto_ErrCode GetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (GetDsaCommSpec((HcfDsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PUBLIC_KEY_SPEC: return GetDsaPubKeySpec((HcfDsaPubKeyParamsSpec *)spec, type, value); case HCF_KEY_PAIR_SPEC: @@ -1054,9 +1149,10 @@ static OH_Crypto_ErrCode GetDsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa } } -static OH_Crypto_ErrCode GetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_N_DATABLOB: return GetDataBlob(spec->n.data, spec->n.len, value); default: @@ -1064,9 +1160,10 @@ static OH_Crypto_ErrCode GetRsaCommSpec(HcfRsaCommParamsSpec *spec, CryptoAsymKe } } -static OH_Crypto_ErrCode GetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_E_DATABLOB: return GetDataBlob(spec->pk.data, spec->pk.len, value); default: @@ -1074,9 +1171,10 @@ static OH_Crypto_ErrCode GetRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode GetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_RSA_D_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); case CRYPTO_RSA_E_DATABLOB: @@ -1091,7 +1189,7 @@ static OH_Crypto_ErrCode GetRsaSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (GetRsaCommSpec((HcfRsaCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PUBLIC_KEY_SPEC: return GetRsaPubKeySpec((HcfRsaPubKeyParamsSpec *)spec, type, value); case HCF_KEY_PAIR_SPEC: @@ -1109,9 +1207,10 @@ static OH_Crypto_ErrCode GetEccField(HcfEccCommParamsSpec *spec, Crypto_DataBlob return GetDataBlob(((HcfECFieldFp *)(spec->field))->p.data, ((HcfECFieldFp *)(spec->field))->p.len, value); } -static OH_Crypto_ErrCode GetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_FP_P_DATABLOB: return GetEccField(spec, value); case CRYPTO_ECC_A_DATABLOB: @@ -1129,8 +1228,8 @@ static OH_Crypto_ErrCode GetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKe if (value->data == NULL) { return CRYPTO_MEMORY_ERROR; } - (void)memcpy_s(value->data, sizeof(spec->h), &(spec->h), sizeof(spec->h)); value->len = sizeof(spec->h); + Int32TobigEndianArr(spec->h, value->data, value->len); break; default: return CRYPTO_PARAMETER_CHECK_FAILED; @@ -1138,9 +1237,10 @@ static OH_Crypto_ErrCode GetEccCommSpec(HcfEccCommParamsSpec *spec, CryptoAsymKe return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode GetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); default: @@ -1148,9 +1248,10 @@ static OH_Crypto_ErrCode GetEccPriSpec(HcfEccPriKeyParamsSpec *spec, CryptoAsymK } } -static OH_Crypto_ErrCode GetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_PK_X_DATABLOB: return GetDataBlob(spec->pk.x.data, spec->pk.x.len, value); case CRYPTO_ECC_PK_Y_DATABLOB: @@ -1160,9 +1261,10 @@ static OH_Crypto_ErrCode GetEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, CryptoAs } } -static OH_Crypto_ErrCode GetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ECC_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); case CRYPTO_ECC_PK_X_DATABLOB: @@ -1179,7 +1281,7 @@ static OH_Crypto_ErrCode GetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa if (GetEccCommSpec((HcfEccCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return GetEccPriSpec((HcfEccPriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -1193,7 +1295,7 @@ static OH_Crypto_ErrCode GetEccSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Pa static OH_Crypto_ErrCode GetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_P_DATABLOB: return GetDataBlob(spec->p.data, spec->p.len, value); case CRYPTO_DH_G_DATABLOB: @@ -1203,8 +1305,8 @@ static OH_Crypto_ErrCode GetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ if (value->data == NULL) { return CRYPTO_MEMORY_ERROR; } - (void)memcpy_s(value->data, sizeof(spec->length), &(spec->length), sizeof(spec->length)); value->len = sizeof(spec->length); + IntTobigEndianArr(spec->length, value->data, value->len); break; default: return CRYPTO_PARAMETER_CHECK_FAILED; @@ -1214,7 +1316,7 @@ static OH_Crypto_ErrCode GetDhCommSpec(HcfDhCommParamsSpec *spec, CryptoAsymKey_ static OH_Crypto_ErrCode GetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); default: @@ -1222,9 +1324,10 @@ static OH_Crypto_ErrCode GetDhPriSpec(HcfDhPriKeyParamsSpec *spec, CryptoAsymKey } } -static OH_Crypto_ErrCode GetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_PK_DATABLOB: return GetDataBlob(spec->pk.data, spec->pk.len, value); default: @@ -1232,9 +1335,10 @@ static OH_Crypto_ErrCode GetDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, CryptoAsym } } -static OH_Crypto_ErrCode GetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_DH_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); case CRYPTO_DH_PK_DATABLOB: @@ -1249,7 +1353,7 @@ static OH_Crypto_ErrCode GetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Par if (GetDhCommSpec((HcfDhCommParamsSpec *)spec, type, value) == CRYPTO_SUCCESS) { return CRYPTO_SUCCESS; } - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return GetDhPriSpec((HcfDhPriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -1261,9 +1365,10 @@ static OH_Crypto_ErrCode GetDhSpec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_Par } } -static OH_Crypto_ErrCode GetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_SK_DATABLOB: case CRYPTO_X25519_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); @@ -1272,9 +1377,10 @@ static OH_Crypto_ErrCode GetAlg25519PriSpec(HcfAlg25519PriKeyParamsSpec *spec, C } } -static OH_Crypto_ErrCode GetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_PK_DATABLOB: case CRYPTO_X25519_PK_DATABLOB: return GetDataBlob(spec->pk.data, spec->pk.len, value); @@ -1283,9 +1389,10 @@ static OH_Crypto_ErrCode GetAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec } } -static OH_Crypto_ErrCode GetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_ED25519_SK_DATABLOB: case CRYPTO_X25519_SK_DATABLOB: return GetDataBlob(spec->sk.data, spec->sk.len, value); @@ -1297,9 +1404,10 @@ static OH_Crypto_ErrCode GetAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *sp } } -static OH_Crypto_ErrCode GetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, Crypto_DataBlob *value) +static OH_Crypto_ErrCode GetAlg25519Spec(OH_CryptoAsymKeySpec *spec, CryptoAsymKey_ParamType type, + Crypto_DataBlob *value) { - switch(spec->specType) { + switch (spec->specType) { case HCF_PRIVATE_KEY_SPEC: return GetAlg25519PriSpec((HcfAlg25519PriKeyParamsSpec *)spec, type, value); case HCF_PUBLIC_KEY_SPEC: @@ -1324,7 +1432,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GetParam(OH_CryptoAsymKeySpec *spec, Cryp return GetOhCryptoErrCodeNew(ret); } - switch(params.algo) { + switch (params.algo) { case HCF_ALG_DSA: return GetDsaSpec(spec, type, value); case HCF_ALG_RSA: diff --git a/frameworks/native/src/crypto_asym_cipher.c b/frameworks/native/src/crypto_asym_cipher.c index fdc2d8b70a0c6e4ec22dca059ee5edf600b4d847..03ecced6b4122177a4a6af7a8585dbe55d3b5850 100644 --- a/frameworks/native/src/crypto_asym_cipher.c +++ b/frameworks/native/src/crypto_asym_cipher.c @@ -77,7 +77,7 @@ OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_Ciph return CRYPTO_PARAMETER_CHECK_FAILED; } HcfResult ret = HCF_SUCCESS; - switch(mode) { + switch (mode) { case CRYPTO_ENCRYPT_MODE: ret = ctx->init((HcfCipher *)ctx, (enum HcfCryptoMode)mode, (HcfKey *)(key->pubKey), NULL); break; diff --git a/frameworks/native/src/crypto_kdf.c b/frameworks/native/src/crypto_kdf.c index 86f95249f2a105a04fcb434e12a99d86f08794eb..96a8bbbc4933871c11467cd650cd11b07eca8a97 100644 --- a/frameworks/native/src/crypto_kdf.c +++ b/frameworks/native/src/crypto_kdf.c @@ -80,14 +80,17 @@ static OH_Crypto_ErrCode SetHkdfParam(HcfHkdfParamsSpec *params, CryptoKdf_Param (void)memcpy_s(data, value->len, value->data, value->len); switch (type) { case CRYPTO_KDF_KEY_DATABLOB: + HcfBlobDataClearAndFree(&(params->key)); params->key.data = data; params->key.len = value->len; break; case CRYPTO_KDF_SALT_DATABLOB: + HcfBlobDataClearAndFree(&(params->salt)); params->salt.data = data; params->salt.len = value->len; break; case CRYPTO_KDF_INFO_DATABLOB: + HcfBlobDataClearAndFree(&(params->info)); params->info.data = data; params->info.len = value->len; break; @@ -107,7 +110,7 @@ static OH_Crypto_ErrCode SetPbkdf2Param(HcfPBKDF2ParamsSpec *params, CryptoKdf_P return CRYPTO_MEMORY_ERROR; } (void)memcpy_s(data, value->len, value->data, value->len); - HcfFree(params->password.data); + HcfBlobDataClearAndFree(&(params->password)); params->password.data = data; params->password.len = value->len; break; @@ -118,7 +121,7 @@ static OH_Crypto_ErrCode SetPbkdf2Param(HcfPBKDF2ParamsSpec *params, CryptoKdf_P return CRYPTO_MEMORY_ERROR; } (void)memcpy_s(data, value->len, value->data, value->len); - HcfFree(params->salt.data); + HcfBlobDataClearAndFree(&(params->salt)); params->salt.data = data; params->salt.len = value->len; break; @@ -136,63 +139,59 @@ static OH_Crypto_ErrCode SetPbkdf2Param(HcfPBKDF2ParamsSpec *params, CryptoKdf_P return CRYPTO_SUCCESS; } +static OH_Crypto_ErrCode SetScryptKeyParam(HcfScryptParamsSpec *params, Crypto_DataBlob *value) +{ + uint8_t *data = (uint8_t *)HcfMalloc(value->len, 0); + if (data == NULL) { + return CRYPTO_MEMORY_ERROR; + } + (void)memcpy_s(data, value->len, value->data, value->len); + 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: { - uint8_t *data = (uint8_t *)HcfMalloc(value->len, 0); - if (data == NULL) { - return CRYPTO_MEMORY_ERROR; - } - (void)memcpy_s(data, value->len, value->data, value->len); - HcfFree(params->passPhrase.data); - params->passPhrase.data = data; - params->passPhrase.len = value->len; - break; - } - case CRYPTO_KDF_SALT_DATABLOB: { - uint8_t *data = (uint8_t *)HcfMalloc(value->len, 0); - if (data == NULL) { - return CRYPTO_MEMORY_ERROR; - } - (void)memcpy_s(data, value->len, value->data, value->len); - HcfFree(params->salt.data); - params->salt.data = data; - params->salt.len = value->len; - break; - } - case CRYPTO_KDF_SCRYPT_N_UINT64: { - if (value->len != sizeof(uint64_t)) { - return CRYPTO_PARAMETER_CHECK_FAILED; - } - params->n = *(uint64_t *)(value->data); - break; - } - case CRYPTO_KDF_SCRYPT_R_UINT64: { - if (value->len != sizeof(uint64_t)) { - return CRYPTO_PARAMETER_CHECK_FAILED; - } - params->r = *(uint64_t *)(value->data); - break; - } - case CRYPTO_KDF_SCRYPT_P_UINT64: { - if (value->len != sizeof(uint64_t)) { - return CRYPTO_PARAMETER_CHECK_FAILED; - } - params->p = *(uint64_t *)(value->data); - break; - } - case CRYPTO_KDF_SCRYPT_MAX_MEM_UINT64: { - if (value->len != sizeof(uint64_t)) { - return CRYPTO_PARAMETER_CHECK_FAILED; - } - params->maxMem = *(uint64_t *)(value->data); - break; - } + case CRYPTO_KDF_KEY_DATABLOB: + return SetScryptKeyParam(params, value); + case CRYPTO_KDF_SALT_DATABLOB: + return SetScryptSaltParam(params, value); + case CRYPTO_KDF_SCRYPT_N_UINT64: + return SetScryptUint64Param(params, value, ¶ms->n); + case CRYPTO_KDF_SCRYPT_R_UINT64: + return SetScryptUint64Param(params, value, ¶ms->r); + case CRYPTO_KDF_SCRYPT_P_UINT64: + return SetScryptUint64Param(params, value, ¶ms->p); + case CRYPTO_KDF_SCRYPT_MAX_MEM_UINT64: + return SetScryptUint64Param(params, value, ¶ms->maxMem); default: return CRYPTO_PARAMETER_CHECK_FAILED; } - return CRYPTO_SUCCESS; } OH_Crypto_ErrCode OH_CryptoKdfParams_SetParam(OH_CryptoKdfParams *params, CryptoKdf_ParamType type, @@ -267,7 +266,7 @@ OH_Crypto_ErrCode OH_CryptoKdf_Create(const char *algoName, OH_CryptoKdf **ctx) return GetOhCryptoErrCodeNew(ret); } -static OH_Crypto_ErrCode HkdfDerive(HcfKdf *ctx, const HcfHkdfParamsSpec *params, int keyLen, HcfBlob *key) +static OH_Crypto_ErrCode HkdfDerive(HcfKdf *ctx, const HcfHkdfParamsSpec *params, uint32_t keyLen, HcfBlob *key) { uint8_t *out = (uint8_t *)HcfMalloc(keyLen, 0); if (out == NULL) { @@ -291,7 +290,7 @@ static OH_Crypto_ErrCode HkdfDerive(HcfKdf *ctx, const HcfHkdfParamsSpec *params return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode Pbkdf2Derive(HcfKdf *ctx, const HcfPBKDF2ParamsSpec *params, int keyLen, HcfBlob *key) +static OH_Crypto_ErrCode Pbkdf2Derive(HcfKdf *ctx, const HcfPBKDF2ParamsSpec *params, uint32_t keyLen, HcfBlob *key) { uint8_t *out = (uint8_t *)HcfMalloc(keyLen, 0); if (out == NULL) { @@ -315,7 +314,7 @@ static OH_Crypto_ErrCode Pbkdf2Derive(HcfKdf *ctx, const HcfPBKDF2ParamsSpec *pa return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode ScryptDerive(HcfKdf *ctx, const HcfScryptParamsSpec *params, int keyLen, HcfBlob *key) +static OH_Crypto_ErrCode ScryptDerive(HcfKdf *ctx, const HcfScryptParamsSpec *params, uint32_t keyLen, HcfBlob *key) { uint8_t *out = (uint8_t *)HcfMalloc(keyLen, 0); if (out == NULL) { @@ -345,16 +344,16 @@ static OH_Crypto_ErrCode ScryptDerive(HcfKdf *ctx, const HcfScryptParamsSpec *pa OH_Crypto_ErrCode OH_CryptoKdf_Derive(OH_CryptoKdf *ctx, const OH_CryptoKdfParams *params, int keyLen, Crypto_DataBlob *key) { - if ((ctx == NULL) || (params == NULL) || (params->algName == NULL) || (key == NULL)) { + if ((ctx == NULL) || (params == NULL) || (params->algName == NULL) || (key == NULL) || (keyLen <= 0)) { return CRYPTO_PARAMETER_CHECK_FAILED; } if (strcmp(params->algName, g_hkdfName) == 0) { - return HkdfDerive((HcfKdf *)ctx, (HcfHkdfParamsSpec *)params, keyLen, (HcfBlob *)key); + return HkdfDerive((HcfKdf *)ctx, (HcfHkdfParamsSpec *)params, (uint32_t)keyLen, (HcfBlob *)key); } else if (strcmp(params->algName, g_pbkdf2Name) == 0) { - return Pbkdf2Derive((HcfKdf *)ctx, (HcfPBKDF2ParamsSpec*)params, keyLen, (HcfBlob *)key); + return Pbkdf2Derive((HcfKdf *)ctx, (HcfPBKDF2ParamsSpec*)params, (uint32_t)keyLen, (HcfBlob *)key); } else if (strcmp(params->algName, g_scryptName) == 0) { - return ScryptDerive((HcfKdf *)ctx, (HcfScryptParamsSpec*)params, keyLen, (HcfBlob *)key); + return ScryptDerive((HcfKdf *)ctx, (HcfScryptParamsSpec*)params, (uint32_t)keyLen, (HcfBlob *)key); } else { return CRYPTO_PARAMETER_CHECK_FAILED; } diff --git a/frameworks/native/src/crypto_key_agreement.c b/frameworks/native/src/crypto_key_agreement.c index ab0c8d130a88c13a4f81e55a6ef7fff1cef779ed..d3d4c023f7aa516967a22c64d85fa5e32bc8693a 100644 --- a/frameworks/native/src/crypto_key_agreement.c +++ b/frameworks/native/src/crypto_key_agreement.c @@ -43,7 +43,8 @@ OH_Crypto_ErrCode OH_CryptoKeyAgreement_GenerateSecret(OH_CryptoKeyAgreement *ct if ((ctx == NULL) || (ctx->generateSecret == NULL) || (privkey == NULL) || (pubkey == NULL) || (secret == NULL)) { return CRYPTO_PARAMETER_CHECK_FAILED; } - HcfResult ret = ctx->generateSecret((HcfKeyAgreement *)ctx, (HcfPriKey *)privkey, (HcfPubKey *)pubkey, (HcfBlob *)secret); + HcfResult ret = ctx->generateSecret((HcfKeyAgreement *)ctx, (HcfPriKey *)privkey, (HcfPubKey *)pubkey, + (HcfBlob *)secret); return GetOhCryptoErrCodeNew(ret); } diff --git a/frameworks/native/src/crypto_mac.c b/frameworks/native/src/crypto_mac.c index 973fcc9dc47f30b465c43e0cad94d92af652c6aa..3d5ceae50a31961843245639c31ed875c5fdac7d 100644 --- a/frameworks/native/src/crypto_mac.c +++ b/frameworks/native/src/crypto_mac.c @@ -30,6 +30,9 @@ typedef struct OH_CryptoMac { HcfMac *macObj; } OH_CryptoMac; +static const char *CMAC_NAME = "CMAC"; +static const char *HMAC_NAME = "HMAC"; + OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx) { if ((algoName == NULL) || (ctx == NULL)) { @@ -40,10 +43,13 @@ OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx) return CRYPTO_MEMORY_ERROR; } HcfMacParamsSpec *paramsSpec = NULL; - if (strcmp(algoName, "CMAC") == 0) { + const char *algName = NULL; + if (strcmp(algoName, CMAC_NAME) == 0) { paramsSpec = (HcfMacParamsSpec *)HcfMalloc(sizeof(HcfCmacParamsSpec), 0); - } else if (strcmp(algoName, "HMAC") == 0) { + algName = CMAC_NAME; + } else if (strcmp(algoName, HMAC_NAME) == 0) { paramsSpec = (HcfMacParamsSpec *)HcfMalloc(sizeof(HcfHmacParamsSpec), 0); + algName = HMAC_NAME; } else { HcfFree(tmpCtx); return CRYPTO_PARAMETER_CHECK_FAILED; @@ -54,22 +60,16 @@ OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx) return CRYPTO_MEMORY_ERROR; } - char *algName = (char *)HcfMalloc(strlen(algoName) + 1, 0); - if (algName == NULL) { - HcfFree(paramsSpec); - HcfFree(tmpCtx); - return CRYPTO_MEMORY_ERROR; - } - (void)memcpy_s(algName, strlen(algoName), algoName, strlen(algoName)); paramsSpec->algName = algName; tmpCtx->paramsSpec = paramsSpec; *ctx = tmpCtx; return CRYPTO_SUCCESS; } -static OH_Crypto_ErrCode SetCmacParam(HcfCmacParamsSpec *paramsSpec, CryptoMac_ParamType type, const Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetCmacParam(HcfCmacParamsSpec *paramsSpec, CryptoMac_ParamType type, + const Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_MAC_CIPHER_NAME_STR: { char *data = (char *)HcfMalloc(value->len + 1, 0); if (data == NULL) { @@ -85,9 +85,10 @@ static OH_Crypto_ErrCode SetCmacParam(HcfCmacParamsSpec *paramsSpec, CryptoMac_P } } -static OH_Crypto_ErrCode SetHmacParam(HcfHmacParamsSpec *paramsSpec, CryptoMac_ParamType type, const Crypto_DataBlob *value) +static OH_Crypto_ErrCode SetHmacParam(HcfHmacParamsSpec *paramsSpec, CryptoMac_ParamType type, + const Crypto_DataBlob *value) { - switch(type) { + switch (type) { case CRYPTO_MAC_DIGEST_NAME_STR: { char *data = (char *)HcfMalloc(value->len + 1, 0); if (data == NULL) { @@ -109,19 +110,15 @@ OH_Crypto_ErrCode OH_CryptoMac_SetParam(OH_CryptoMac *ctx, CryptoMac_ParamType t (value->data == NULL)) { return CRYPTO_PARAMETER_CHECK_FAILED; } + OH_Crypto_ErrCode res = CRYPTO_PARAMETER_CHECK_FAILED; if (strcmp(ctx->paramsSpec->algName, "CMAC") == 0) { - return SetCmacParam((HcfCmacParamsSpec*)(ctx->paramsSpec), type, value); + res = SetCmacParam((HcfCmacParamsSpec*)(ctx->paramsSpec), type, value); } else if (strcmp(ctx->paramsSpec->algName, "HMAC") == 0) { - return SetHmacParam((HcfHmacParamsSpec*)(ctx->paramsSpec), type, value); - } else { - return CRYPTO_PARAMETER_CHECK_FAILED; + res = SetHmacParam((HcfHmacParamsSpec*)(ctx->paramsSpec), type, value); } -} - -OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *key) -{ - if ((ctx == NULL) || (key == NULL)) { - return HCF_INVALID_PARAMS; + + if (res != CRYPTO_SUCCESS) { + return res; } HcfMac *macObj = NULL; @@ -129,23 +126,23 @@ OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *ke if (ret != HCF_SUCCESS) { return GetOhCryptoErrCodeNew(ret); } - if (macObj->init == NULL) { - HcfObjDestroy(macObj); - return CRYPTO_PARAMETER_CHECK_FAILED; - } - ret = macObj->init(macObj, (const HcfSymKey *)key); - if (ret != HCF_SUCCESS) { - HcfObjDestroy(macObj); - return GetOhCryptoErrCodeNew(ret); - } ctx->macObj = macObj; return CRYPTO_SUCCESS; } +OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *key) +{ + if ((ctx == NULL) || (ctx->macObj == NULL) || (ctx->macObj->init == NULL) || (key == NULL)) { + return CRYPTO_PARAMETER_CHECK_FAILED; + } + HcfResult ret = ctx->macObj->init(ctx->macObj, (const HcfSymKey *)key); + return GetOhCryptoErrCodeNew(ret); +} + OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob *in) { if ((ctx == NULL) || (ctx->macObj == NULL) || (ctx->macObj->update == NULL) || (in == NULL)) { - return HCF_INVALID_PARAMS; + return CRYPTO_PARAMETER_CHECK_FAILED; } HcfResult ret = ctx->macObj->update(ctx->macObj, (HcfBlob *)in); return GetOhCryptoErrCodeNew(ret); @@ -181,7 +178,6 @@ static void FreeMacParams(HcfMacParamsSpec *params) HcfFree((void *)(((HcfHmacParamsSpec *)params)->mdName)); ((HcfHmacParamsSpec *)params)->mdName = NULL; } - HcfFree((void *)(params->algName)); params->algName = NULL; HcfFree(params); } diff --git a/frameworks/native/src/crypto_rand.c b/frameworks/native/src/crypto_rand.c index 713e0614ae591f858a7c46046656e3dfcfaae6d9..2f8dd873b07a51636be6ac257a67d63c90ee5146 100644 --- a/frameworks/native/src/crypto_rand.c +++ b/frameworks/native/src/crypto_rand.c @@ -70,8 +70,5 @@ OH_Crypto_ErrCode OH_CryptoRand_SetSeed(OH_CryptoRand *ctx, Crypto_DataBlob *see void OH_CryptoRand_Destroy(OH_CryptoRand *ctx) { - if ((ctx == NULL) || (ctx->base.destroy == NULL)) { - return; - } - ctx->base.destroy((HcfObjectBase *)ctx); + HcfObjDestroy((HcfRand *)ctx); } \ No newline at end of file diff --git a/frameworks/native/src/native_common.c b/frameworks/native/src/native_common.c index 7123ccec1f6e9855c9c757f4ce5cddd805575bbc..4d41e7ae7850270d26b8449e30ad458d2e1c232a 100644 --- a/frameworks/native/src/native_common.c +++ b/frameworks/native/src/native_common.c @@ -45,4 +45,49 @@ OH_Crypto_ErrCode GetOhCryptoErrCodeNew(HcfResult errCode) default: return CRYPTO_OPERTION_ERROR; } +} + +void ReverseUint8Arr(uint8_t *data, size_t len) +{ + for (size_t i = 0; i < len / 2; ++i) { + uint8_t temp = data[i]; + data[i] = data[len - 1 - i]; + data[len - 1 - i] = temp; + } +} + +#define NATIVE_BITS_SIZE 8 + +int32_t bigEndianArrToInt32(const uint8_t *data, size_t len) +{ + int32_t value = 0; + + for (size_t i = 0; i < len; ++i) { + value |= (int32_t)(data[i] << ((sizeof(int32_t) - 1 - i) * NATIVE_BITS_SIZE)); + } + return value; +} + +void Int32TobigEndianArr(int32_t value, uint8_t *data, size_t len) +{ + for (size_t i = 0; i < len; ++i) { + data[i] = (value >> ((sizeof(int32_t) - i - 1) * NATIVE_BITS_SIZE)) & 0xFF; + } +} + +int32_t bigEndianArrToInt(const uint8_t *data, size_t len) +{ + int value = 0; + + for (size_t i = 0; i < len; ++i) { + value |= (int)(data[i] << ((sizeof(int) - 1 - i) * NATIVE_BITS_SIZE)); + } + return value; +} + +void IntTobigEndianArr(int value, uint8_t *data, size_t len) +{ + for (size_t i = 0; i < len; ++i) { + data[i] = (value >> ((sizeof(int) - i - 1) * NATIVE_BITS_SIZE)) & 0xFF; + } } \ No newline at end of file diff --git a/interfaces/kits/native/include/crypto_asym_cipher.h b/interfaces/kits/native/include/crypto_asym_cipher.h index d28e5c92abfefaec3ec4dc6cb038d9056b019466..d6ab307212546b8c8b7bbfa768a63b67b7bc1cd7 100644 --- a/interfaces/kits/native/include/crypto_asym_cipher.h +++ b/interfaces/kits/native/include/crypto_asym_cipher.h @@ -59,7 +59,7 @@ typedef struct OH_CryptoAsymCipher OH_CryptoAsymCipher; * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -74,7 +74,7 @@ OH_Crypto_ErrCode OH_CryptoAsymCipher_Create(const char *algoName, OH_CryptoAsym * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoAsymCipher_Final * @since 20 @@ -90,7 +90,7 @@ OH_Crypto_ErrCode OH_CryptoAsymCipher_Init(OH_CryptoAsymCipher *ctx, Crypto_Ciph * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoAsymCipher_Init * @since 20 @@ -137,7 +137,7 @@ typedef enum { * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -152,7 +152,7 @@ OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_Create(Crypto_DataBlob *sm2Cipherte * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -168,7 +168,7 @@ OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_GetItem(OH_CryptoSm2CiphertextSpec * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -183,7 +183,7 @@ OH_Crypto_ErrCode OH_CryptoSm2CiphertextSpec_SetItem(OH_CryptoSm2CiphertextSpec * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_asym_key.h b/interfaces/kits/native/include/crypto_asym_key.h index 21ec9d8eecb0f197606bb4cda232cc5bdcae994a..4997e5e5589a486c9e68d1808fb409ac4a1cfee7 100644 --- a/interfaces/kits/native/include/crypto_asym_key.h +++ b/interfaces/kits/native/include/crypto_asym_key.h @@ -275,6 +275,25 @@ OH_Crypto_ErrCode OH_CryptoPubKey_Encode(OH_CryptoPubKey *key, Crypto_EncodingTy */ OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value); +/** + * @brief Sets the password to the asymmetric key generator context. + * + * Call this method to set the password if you need to convert encrypted private key data to a key pair using + * {@link OH_CryptoAsymKeyGenerator_Convert} + * + * @param ctx Indicates the asymmetric key generator context. + * @param password Indicates the password. + * @param passwordLen Indicates the password length. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. + * @since 20 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_SetPassword(OH_CryptoAsymKeyGenerator *ctx, const unsigned char *password, + uint32_t passwordLen); + /** * @brief Defines the private key encoding params structure. * @@ -302,7 +321,7 @@ typedef enum { * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -317,7 +336,7 @@ OH_Crypto_ErrCode OH_CryptoPrivKeyEncodingParams_Create(OH_CryptoPrivKeyEncoding * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -344,7 +363,7 @@ void OH_CryptoPrivKeyEncodingParams_Destroy(OH_CryptoPrivKeyEncodingParams *ctx) * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -360,7 +379,7 @@ OH_Crypto_ErrCode OH_CryptoPrivKey_Encode(OH_CryptoPrivKey *key, Crypto_Encoding * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -398,7 +417,7 @@ typedef enum { * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -413,7 +432,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenEcCommonParamsSpec(const char *curveNa * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -428,7 +447,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_GenDhCommonParamsSpec(int pLen, int skLen * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -444,7 +463,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_Create(const char *algoName, CryptoAsymKe * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -459,7 +478,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetParam(OH_CryptoAsymKeySpec *spec, Cryp * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -475,7 +494,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeySpec_SetCommonParamsSpec(OH_CryptoAsymKeySpec * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -505,7 +524,7 @@ typedef struct OH_CryptoAsymKeyGeneratorWithSpec OH_CryptoAsymKeyGeneratorWithSp * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -520,7 +539,7 @@ OH_Crypto_ErrCode OH_CryptoAsymKeyGeneratorWithSpec_Create(OH_CryptoAsymKeySpec * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -552,7 +571,7 @@ typedef struct OH_CryptoEcPoint OH_CryptoEcPoint; * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -567,7 +586,7 @@ OH_Crypto_ErrCode OH_CryptoEcPoint_Create(const char *curveName, Crypto_DataBlob * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -582,7 +601,7 @@ OH_Crypto_ErrCode OH_CryptoEcPoint_GetCoordinate(OH_CryptoEcPoint *point, Crypto * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -597,7 +616,7 @@ OH_Crypto_ErrCode OH_CryptoEcPoint_SetCoordinate(OH_CryptoEcPoint *point, Crypto * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_kdf.h b/interfaces/kits/native/include/crypto_kdf.h index 46dd56b72c7d24df54f33f5dd67eaf8324e553b4..afa02bc37ee2a475ebb4eaa9de02eda31a0312c7 100644 --- a/interfaces/kits/native/include/crypto_kdf.h +++ b/interfaces/kits/native/include/crypto_kdf.h @@ -90,12 +90,12 @@ typedef enum { /** * @brief Creates KDF params. * - * @param algoName Indicates the KDF algorithm name. e.g. "HKDF|SHA384|EXTRACT_AND_EXPAND", "PBKDF2|SHA384". + * @param algoName Indicates the KDF algorithm name. e.g. "HKDF", "PBKDF2", "SCRYPT". * @param params Indicates the KDF params. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -110,7 +110,7 @@ OH_Crypto_ErrCode OH_CryptoKdfParams_Create(const char *algoName, OH_CryptoKdfPa * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -128,12 +128,12 @@ void OH_CryptoKdfParams_Destroy(OH_CryptoKdfParams *params); /** * @brief Creates a KDF context. * - * @param algoName Indicates the KDF algorithm name. + * @param algoName Indicates the KDF algorithm name. e.g. "HKDF|SHA384|EXTRACT_AND_EXPAND", "PBKDF2|SHA384", "SCRYPT". * @param ctx Indicates the KDF context. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -149,7 +149,7 @@ OH_Crypto_ErrCode OH_CryptoKdf_Create(const char *algoName, OH_CryptoKdf **ctx); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_key_agreement.h b/interfaces/kits/native/include/crypto_key_agreement.h index cba6555aaf47d77c3a64383e518ee24e86682448..2b011c409986ddbb4b0786765579d475c68a3ca0 100644 --- a/interfaces/kits/native/include/crypto_key_agreement.h +++ b/interfaces/kits/native/include/crypto_key_agreement.h @@ -58,7 +58,7 @@ typedef struct OH_CryptoKeyAgreement OH_CryptoKeyAgreement; * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -74,7 +74,7 @@ OH_Crypto_ErrCode OH_CryptoKeyAgreement_Create(const char *algoName, OH_CryptoKe * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_mac.h b/interfaces/kits/native/include/crypto_mac.h index 214d07faa79a63d512569d370c67432dcb75765b..24d3c99392e53be742365b56304923349f9ba9a5 100644 --- a/interfaces/kits/native/include/crypto_mac.h +++ b/interfaces/kits/native/include/crypto_mac.h @@ -71,7 +71,7 @@ typedef enum { * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -86,7 +86,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Create(const char *algoName, OH_CryptoMac **ctx); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -100,7 +100,7 @@ OH_Crypto_ErrCode OH_CryptoMac_SetParam(OH_CryptoMac *ctx, CryptoMac_ParamType t * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoMac_Update * @see OH_CryptoMac_Final @@ -116,7 +116,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Init(OH_CryptoMac *ctx, const OH_CryptoSymKey *ke * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoMac_Init * @see OH_CryptoMac_Final @@ -132,7 +132,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Update(OH_CryptoMac *ctx, const Crypto_DataBlob * * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @see OH_CryptoMac_Init * @see OH_CryptoMac_Update @@ -148,7 +148,7 @@ OH_Crypto_ErrCode OH_CryptoMac_Final(OH_CryptoMac *ctx, Crypto_DataBlob *out); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_rand.h b/interfaces/kits/native/include/crypto_rand.h index ff1a469d18883334713e316a4997938768662c39..1e9a4f8e017aaaae3d68c722704b4ef00bfb1acd 100644 --- a/interfaces/kits/native/include/crypto_rand.h +++ b/interfaces/kits/native/include/crypto_rand.h @@ -54,7 +54,7 @@ typedef struct OH_CryptoRand OH_CryptoRand; * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -69,7 +69,7 @@ OH_Crypto_ErrCode OH_CryptoRand_Create(OH_CryptoRand **ctx); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ @@ -92,7 +92,7 @@ const char *OH_CryptoRand_GetAlgoName(OH_CryptoRand *ctx); * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. - * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 1762003 - If parameter check failed. + * {@link OH_Crypto_ErrCode#CRYPTO_PARAMETER_CHECK_FAILED} 17620003 - If parameter check failed. * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto operation failed. * @since 20 */ diff --git a/interfaces/kits/native/include/crypto_signature.h b/interfaces/kits/native/include/crypto_signature.h index 792823b4c3c94e4ed556ecf245115f6e42d19cda..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 */ diff --git a/test/unittest/src/native/native_asym_key_test.cpp b/test/unittest/src/native/native_asym_key_test.cpp index 0a276275de5dd3631f39516d7f324a1e379713e1..9017faed93af1ae51285fd230eb83bad7aa2b8ed 100644 --- a/test/unittest/src/native/native_asym_key_test.cpp +++ b/test/unittest/src/native/native_asym_key_test.cpp @@ -457,25 +457,23 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest006, TestSize.Level0) Crypto_DataBlob pemData = {0}; ret = OH_CryptoPrivKey_Encode(privKey, CRYPTO_PEM, "PKCS8", nullptr, &pemData); EXPECT_EQ(ret, CRYPTO_SUCCESS); - ASSERT_NE(pemData.data, nullptr); - ASSERT_NE(pemData.len, 0); OH_Crypto_FreeDataBlob(&pemData); ret = OH_CryptoPrivKey_Encode(privKey, CRYPTO_PEM, "PKCS8", params, &pemData); EXPECT_EQ(ret, CRYPTO_SUCCESS); - ASSERT_NE(pemData.data, nullptr); - ASSERT_NE(pemData.len, 0); + ret = OH_CryptoAsymKeyGenerator_SetPassword(keyGen, password.data, password.len); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + OH_CryptoKeyPair *keyCtx = nullptr; + 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); - ASSERT_NE(n.data, nullptr); - ASSERT_NE(n.len, 0); ret = OH_CryptoPrivKey_GetParam(privKey, CRYPTO_RSA_D_DATABLOB, &d); EXPECT_EQ(ret, CRYPTO_SUCCESS); - ASSERT_NE(d.data, nullptr); - ASSERT_NE(d.len, 0); OH_Crypto_FreeDataBlob(&pemData); OH_Crypto_FreeDataBlob(&n); @@ -754,41 +752,11 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest012, TestSize.Level0) HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest013, TestSize.Level0) { - OH_CryptoKeyPair *keyCtx = GenerateKeyPair("RSA2048"); - ASSERT_NE(keyCtx, nullptr); - Crypto_DataBlob pubKeyData = {.data = nullptr, .len = 0}; - Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; - Crypto_DataBlob nData = {.data = nullptr, .len = 0}; - OH_Crypto_ErrCode ret = GetRsaKeyParams(keyCtx, &pubKeyData, &privKeyData, &nData); - EXPECT_EQ(ret, CRYPTO_SUCCESS); - OH_CryptoAsymKeySpec *keySpec = nullptr; - ret = OH_CryptoAsymKeySpec_Create("RSA", CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, &keySpec); + OH_Crypto_ErrCode ret = OH_CryptoAsymKeySpec_Create("RSA", CRYPTO_ASYM_KEY_PRIVATE_KEY_SPEC, &keySpec); ASSERT_NE(ret, CRYPTO_SUCCESS); ret = OH_CryptoAsymKeySpec_Create("RSA", CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, &keySpec); - EXPECT_EQ(ret, CRYPTO_SUCCESS); - ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_RSA_E_DATABLOB, &pubKeyData); - EXPECT_NE(ret, CRYPTO_SUCCESS); - ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_RSA_D_DATABLOB, &privKeyData); - EXPECT_NE(ret, CRYPTO_SUCCESS); - ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_RSA_N_DATABLOB, &nData); - EXPECT_EQ(ret, CRYPTO_SUCCESS); - FreeRsaKeyParams(&pubKeyData, &privKeyData, &nData); - ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_RSA_E_DATABLOB, &pubKeyData); - EXPECT_NE(ret, CRYPTO_SUCCESS); - ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_RSA_D_DATABLOB, &privKeyData); - EXPECT_NE(ret, CRYPTO_SUCCESS); - ret = OH_CryptoAsymKeySpec_GetParam(keySpec, CRYPTO_RSA_N_DATABLOB, &nData); - EXPECT_EQ(ret, CRYPTO_SUCCESS); - FreeRsaKeyParams(&pubKeyData, &privKeyData, &nData); - - OH_CryptoKeyPair *keyPair = nullptr; - ret = GenerateKeyPairWithSpec(keySpec, &keyPair); - EXPECT_NE(ret, CRYPTO_SUCCESS); - - OH_CryptoKeyPair_Destroy(keyPair); - OH_CryptoAsymKeySpec_Destroy(keySpec); - OH_CryptoKeyPair_Destroy(keyCtx); + ASSERT_NE(ret, CRYPTO_SUCCESS); } HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest014, TestSize.Level0) @@ -1344,4 +1312,50 @@ HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest027, TestSize.Level0) OH_CryptoEcPoint_Destroy(point); OH_CryptoEcPoint_Destroy(point2); } + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest028, TestSize.Level0) +{ + OH_CryptoKeyPair *keyCtx = GenerateKeyPair("DSA2048"); + ASSERT_NE(keyCtx, nullptr); + Crypto_DataBlob pubKeyData = {.data = nullptr, .len = 0}; + Crypto_DataBlob privKeyData = {.data = nullptr, .len = 0}; + Crypto_DataBlob pData = {.data = nullptr, .len = 0}; + Crypto_DataBlob qData = {.data = nullptr, .len = 0}; + Crypto_DataBlob gData = {.data = nullptr, .len = 0}; + OH_Crypto_ErrCode ret = GetDsaKeyParams(keyCtx, &pubKeyData, &privKeyData, &pData, &qData, &gData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *dsaCommonSpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("DSA", CRYPTO_ASYM_KEY_COMMON_PARAMS_SPEC, &dsaCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(dsaCommonSpec, CRYPTO_DSA_P_DATABLOB, &pData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(dsaCommonSpec, CRYPTO_DSA_Q_DATABLOB, &qData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(dsaCommonSpec, CRYPTO_DSA_G_DATABLOB, &gData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoAsymKeySpec *keySpec = nullptr; + ret = OH_CryptoAsymKeySpec_Create("DSA", CRYPTO_ASYM_KEY_KEY_PAIR_SPEC, &keySpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_PK_DATABLOB, &pubKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeySpec_SetParam(keySpec, CRYPTO_DSA_SK_DATABLOB, &privKeyData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + FreeDsaKeyParams(&pubKeyData, &privKeyData, &pData, &qData, &gData); + ret = OH_CryptoAsymKeySpec_SetCommonParamsSpec(keySpec, dsaCommonSpec); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoKeyPair *keyPair = nullptr; + ret = GenerateKeyPairWithSpec(keySpec, &keyPair); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(keyPair, nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPubKey(keyPair), nullptr); + ASSERT_NE(OH_CryptoKeyPair_GetPrivKey(keyPair), nullptr); + + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeySpec_Destroy(dsaCommonSpec); + OH_CryptoAsymKeySpec_Destroy(keySpec); + OH_CryptoKeyPair_Destroy(keyCtx); +} } \ No newline at end of file