From fbec9132529c74a022674f268652fb9d0b9d2bd1 Mon Sep 17 00:00:00 2001 From: wangyongzhong2 Date: Fri, 8 Dec 2023 22:48:51 +0800 Subject: [PATCH] =?UTF-8?q?add=20Ed25519=E3=80=81x25519=E3=80=81DH?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyongzhong2 --- BUILD.gn | 1 + bundle.json | 2 + common/inc/params_parser.h | 22 + common/src/asy_key_params.c | 199 +- common/src/params_parser.c | 25 +- frameworks/crypto_operation/key_agreement.c | 28 +- frameworks/crypto_operation/signature.c | 6 + frameworks/frameworks.gni | 1 + frameworks/js/napi/crypto/BUILD.gn | 1 + .../inc/napi_crypto_framework_defines.h | 3 + .../js/napi/crypto/inc/napi_dh_key_util.h | 39 + frameworks/js/napi/crypto/inc/napi_utils.h | 4 +- .../js/napi/crypto/src/napi_dh_key_util.cpp | 98 + frameworks/js/napi/crypto/src/napi_init.cpp | 11 + frameworks/js/napi/crypto/src/napi_utils.cpp | 494 ++++- frameworks/key/asy_key_generator.c | 402 +++- frameworks/key/dh_key_util.c | 52 + frameworks/key/key_utils.c | 45 +- frameworks/spi/dh_key_util_spi.h | 29 + .../detailed_alg_25519_key_params.h | 54 + .../detailed_dh_key_params.h | 63 + interfaces/innerkits/key/asy_key_generator.h | 14 + interfaces/innerkits/key/dh_key_util.h | 33 + interfaces/innerkits/key/key.h | 11 + interfaces/innerkits/key/key_utils.h | 5 + .../common/inc/dh_openssl_common.h | 29 + .../common/inc/ecc_openssl_common.h | 557 +---- .../inc/ecc_openssl_common_param_spec.h | 553 +++++ .../common/inc/openssl_adapter.h | 46 +- .../openssl_plugin/common/inc/openssl_class.h | 43 + .../common/inc/openssl_common.h | 3 + .../common/src/dh_openssl_common.c | 139 ++ .../common/src/ecc_openssl_common.c | 511 +++++ .../common/src/openssl_adapter.c | 199 ++ .../common/src/openssl_common.c | 55 +- .../key_agreement/inc/dh_openssl.h | 33 + .../key_agreement/inc/x25519_openssl.h | 33 + .../key_agreement/src/dh_openssl.c | 102 + .../key_agreement/src/ecdh_openssl.c | 50 +- .../key_agreement/src/x25519_openssl.c | 101 + .../signature/inc/ed25519_openssl.h | 34 + .../signature/src/ed25519_openssl.c | 358 ++++ .../inc/alg_25519_asy_key_generator_openssl.h | 35 + .../inc/dh_asy_key_generator_openssl.h | 32 + .../dh_common_param_spec_generator_openssl.h | 32 + .../src/alg_25519_asy_key_generator_openssl.c | 983 +++++++++ .../src/dh_asy_key_generator_openssl.c | 1114 ++++++++++ .../dh_common_param_spec_generator_openssl.c | 253 +++ .../src/dsa_asy_key_generator_openssl.c | 6 +- .../src/ecc_asy_key_generator_openssl.c | 674 +----- .../ecc_common_param_spec_generator_openssl.c | 56 +- .../src/sm2_asy_key_generator_openssl.c | 610 +----- plugin/plugin.gni | 12 +- .../hcfsigncreate_fuzzer.cpp | 32 + .../hcfverifycreate_fuzzer.cpp | 50 + .../asykeygenerator_fuzzer.cpp | 726 ++++++- test/fuzztest/key/dhkeyutil_fuzzer/BUILD.gn | 56 + .../fuzztest/key/dhkeyutil_fuzzer/corpus/init | 14 + .../key/dhkeyutil_fuzzer/dhkeyutil_fuzzer.cpp | 63 + .../key/dhkeyutil_fuzzer/dhkeyutil_fuzzer.h | 21 + .../fuzztest/key/dhkeyutil_fuzzer/project.xml | 25 + test/unittest/BUILD.gn | 10 + .../src/crypto_asy_key_generator_cov_test.cpp | 1 + ...rypto_brainpool_asy_key_generator_test.cpp | 12 +- .../crypto_brainpool_key_agreement_test.cpp | 19 +- .../crypto_brainpool_no_length_sign_test.cpp | 1 + ...crypto_brainpool_no_length_verify_test.cpp | 62 +- ...ypto_dh_asy_key_generator_by_spec_test.cpp | 1815 ++++++++++++++++ .../src/crypto_dh_asy_key_generator_test.cpp | 1876 +++++++++++++++++ .../src/crypto_dh_key_agreement_test.cpp | 500 +++++ ...pto_ecc_asy_key_generator_by_spec_test.cpp | 1 + .../crypto_ecc_key_agreement_by_spec_test.cpp | 1 + .../unittest/src/crypto_ecc_key_util_test.cpp | 39 +- .../src/crypto_ecc_no_length_sign_test.cpp | 1 + .../src/crypto_ecc_no_length_verify_test.cpp | 1 + ...ed25519_asy_key_generator_by_spec_test.cpp | 936 ++++++++ .../crypto_ed25519_asy_key_generator_test.cpp | 1466 +++++++++++++ .../unittest/src/crypto_ed25519_sign_test.cpp | 626 ++++++ .../src/crypto_ed25519_verify_test.cpp | 590 ++++++ ...pto_sm2_asy_key_generator_by_spec_test.cpp | 272 ++- test/unittest/src/crypto_sm2_sign_test.cpp | 33 +- test/unittest/src/crypto_sm2_verify_test.cpp | 38 +- ..._x25519_asy_key_generator_by_spec_test.cpp | 834 ++++++++ .../crypto_x25519_asy_key_generator_test.cpp | 741 +++++++ .../src/crypto_x25519_key_agreement_test.cpp | 441 ++++ test/unittest/src/openssl_adapter_mock.c | 415 +++- 86 files changed, 18128 insertions(+), 1885 deletions(-) create mode 100644 frameworks/js/napi/crypto/inc/napi_dh_key_util.h create mode 100644 frameworks/js/napi/crypto/src/napi_dh_key_util.cpp create mode 100644 frameworks/key/dh_key_util.c create mode 100644 frameworks/spi/dh_key_util_spi.h create mode 100644 interfaces/innerkits/algorithm_parameter/detailed_alg_25519_key_params.h create mode 100644 interfaces/innerkits/algorithm_parameter/detailed_dh_key_params.h create mode 100644 interfaces/innerkits/key/dh_key_util.h create mode 100644 plugin/openssl_plugin/common/inc/dh_openssl_common.h create mode 100644 plugin/openssl_plugin/common/inc/ecc_openssl_common_param_spec.h create mode 100644 plugin/openssl_plugin/common/src/dh_openssl_common.c create mode 100644 plugin/openssl_plugin/common/src/ecc_openssl_common.c create mode 100644 plugin/openssl_plugin/crypto_operation/key_agreement/inc/dh_openssl.h create mode 100644 plugin/openssl_plugin/crypto_operation/key_agreement/inc/x25519_openssl.h create mode 100644 plugin/openssl_plugin/crypto_operation/key_agreement/src/dh_openssl.c create mode 100644 plugin/openssl_plugin/crypto_operation/key_agreement/src/x25519_openssl.c create mode 100644 plugin/openssl_plugin/crypto_operation/signature/inc/ed25519_openssl.h create mode 100644 plugin/openssl_plugin/crypto_operation/signature/src/ed25519_openssl.c create mode 100644 plugin/openssl_plugin/key/asy_key_generator/inc/alg_25519_asy_key_generator_openssl.h create mode 100644 plugin/openssl_plugin/key/asy_key_generator/inc/dh_asy_key_generator_openssl.h create mode 100644 plugin/openssl_plugin/key/asy_key_generator/inc/dh_common_param_spec_generator_openssl.h create mode 100644 plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c create mode 100644 plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c create mode 100644 plugin/openssl_plugin/key/asy_key_generator/src/dh_common_param_spec_generator_openssl.c create mode 100644 test/fuzztest/key/dhkeyutil_fuzzer/BUILD.gn create mode 100644 test/fuzztest/key/dhkeyutil_fuzzer/corpus/init create mode 100644 test/fuzztest/key/dhkeyutil_fuzzer/dhkeyutil_fuzzer.cpp create mode 100644 test/fuzztest/key/dhkeyutil_fuzzer/dhkeyutil_fuzzer.h create mode 100644 test/fuzztest/key/dhkeyutil_fuzzer/project.xml create mode 100644 test/unittest/src/crypto_dh_asy_key_generator_by_spec_test.cpp create mode 100644 test/unittest/src/crypto_dh_asy_key_generator_test.cpp create mode 100644 test/unittest/src/crypto_dh_key_agreement_test.cpp create mode 100644 test/unittest/src/crypto_ed25519_asy_key_generator_by_spec_test.cpp create mode 100644 test/unittest/src/crypto_ed25519_asy_key_generator_test.cpp create mode 100644 test/unittest/src/crypto_ed25519_sign_test.cpp create mode 100644 test/unittest/src/crypto_ed25519_verify_test.cpp create mode 100644 test/unittest/src/crypto_x25519_asy_key_generator_by_spec_test.cpp create mode 100644 test/unittest/src/crypto_x25519_asy_key_generator_test.cpp create mode 100644 test/unittest/src/crypto_x25519_key_agreement_test.cpp diff --git a/BUILD.gn b/BUILD.gn index 8234646..5b63a96 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -40,6 +40,7 @@ group("crypto_framework_fuzztest") { "test/fuzztest/crypto_operation/hcfsigncreate_fuzzer:fuzztest", "test/fuzztest/crypto_operation/hcfverifycreate_fuzzer:fuzztest", "test/fuzztest/key/asykeygenerator_fuzzer:fuzztest", + "test/fuzztest/key/dhkeyutil_fuzzer:fuzztest", "test/fuzztest/key/ecckeyutil_fuzzer:fuzztest", "test/fuzztest/key/symkeygenerator_fuzzer:fuzztest", "test/fuzztest/rand/hcfrandcreate_fuzzer:fuzztest", diff --git a/bundle.json b/bundle.json index f96df49..42d0a42 100644 --- a/bundle.json +++ b/bundle.json @@ -44,8 +44,10 @@ "algorithm_parameter/algorithm_parameter.h", "algorithm_parameter/asy_key_params.h", "algorithm_parameter/detailed_ccm_params.h", + "algorithm_parameter/detailed_dh_key_params.h", "algorithm_parameter/detailed_dsa_key_params.h", "algorithm_parameter/detailed_ecc_key_params.h", + "algorithm_parameter/detailed_alg_25519_key_params.h", "algorithm_parameter/detailed_gcm_params.h", "algorithm_parameter/detailed_iv_params.h", "algorithm_parameter/detailed_pbkdf2_params.h", diff --git a/common/inc/params_parser.h b/common/inc/params_parser.h index 9a984a4..8fd6422 100644 --- a/common/inc/params_parser.h +++ b/common/inc/params_parser.h @@ -41,6 +41,9 @@ typedef enum { HCF_ALG_HMAC, HCF_ALG_PKBDF2, HCF_ALG_ECC_BRAINPOOL, + HCF_ALG_ED25519, + HCF_ALG_X25519, + HCF_ALG_DH, } HcfAlgValue; typedef enum { @@ -120,6 +123,8 @@ typedef enum { HCF_ALG_3DES_DEFAULT, HCF_ALG_HMAC_DEFAULT, HCF_ALG_ECC_BRAINPOOL_DEFAULT, + HCF_ALG_X25519_DEFAULT, + HCF_ALG_DH_DEFAULT, // key derivation function, PBKDF2 HCF_ALG_PBKDF2_DEFAULT, @@ -143,6 +148,23 @@ typedef enum { HCF_ALG_ECC_BP384T1, HCF_ALG_ECC_BP512R1, HCF_ALG_ECC_BP512T1, + + // ed25519 + HCF_ALG_ED25519_256, + HCF_ALG_X25519_256, + + // DH keysize + HCF_OPENSSL_DH_MODP_1536, + HCF_OPENSSL_DH_MODP_2048, + HCF_OPENSSL_DH_MODP_3072, + HCF_OPENSSL_DH_MODP_4096, + HCF_OPENSSL_DH_MODP_6144, + HCF_OPENSSL_DH_MODP_8192, + HCF_OPENSSL_DH_FFDHE_2048, + HCF_OPENSSL_DH_FFDHE_3072, + HCF_OPENSSL_DH_FFDHE_4096, + HCF_OPENSSL_DH_FFDHE_6144, + HCF_OPENSSL_DH_FFDHE_8192, } HcfAlgParaValue; typedef struct { diff --git a/common/src/asy_key_params.c b/common/src/asy_key_params.c index 65b8252..17f562c 100644 --- a/common/src/asy_key_params.c +++ b/common/src/asy_key_params.c @@ -21,8 +21,10 @@ #include "params_parser.h" #include "big_integer.h" #include "detailed_dsa_key_params.h" +#include "detailed_dh_key_params.h" #include "detailed_ecc_key_params.h" #include "detailed_rsa_key_params.h" +#include "detailed_alg_25519_key_params.h" #include "memory.h" #include "log.h" @@ -30,6 +32,17 @@ #define ALG_NAME_ECC "ECC" #define ALG_NAME_SM2 "SM2" #define ALG_NAME_RSA "RSA" +#define ALG_NAME_DH "DH" +#define ALG_NAME_ED25519 "Ed25519" +#define ALG_NAME_X25519 "X25519" + +typedef void (*HcfFreeParamsAsyKeySpec)(HcfAsyKeyParamsSpec *); + +typedef struct { + char *algo; + + HcfFreeParamsAsyKeySpec createFreeFunc; +} HcfFreeAsyKeySpec; void FreeDsaCommParamsSpec(HcfDsaCommParamsSpec *spec) { @@ -46,6 +59,19 @@ void FreeDsaCommParamsSpec(HcfDsaCommParamsSpec *spec) spec->g.data = NULL; } +void FreeDhCommParamsSpec(HcfDhCommParamsSpec *spec) +{ + if (spec == NULL) { + return; + } + HcfFree(spec->base.algName); + spec->base.algName = NULL; + HcfFree(spec->p.data); + spec->p.data = NULL; + HcfFree(spec->g.data); + spec->g.data = NULL; +} + static void DestroyDsaCommParamsSpec(HcfDsaCommParamsSpec *spec) { FreeDsaCommParamsSpec(spec); @@ -77,6 +103,49 @@ void DestroyDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec) HcfFree(spec); } +static void DestroyDhCommParamsSpec(HcfDhCommParamsSpec *spec) +{ + FreeDhCommParamsSpec(spec); + HcfFree(spec); +} + +void DestroyDhPubKeySpec(HcfDhPubKeyParamsSpec *spec) +{ + if (spec == NULL) { + return; + } + FreeDhCommParamsSpec(&(spec->base)); + HcfFree(spec->pk.data); + spec->pk.data = NULL; + HcfFree(spec); +} + +void DestroyDhPriKeySpec(HcfDhPriKeyParamsSpec *spec) +{ + if (spec == NULL) { + return; + } + FreeDhCommParamsSpec(&(spec->base)); + (void)memset_s(spec->sk.data, spec->sk.len, 0, spec->sk.len); + HcfFree(spec->sk.data); + spec->sk.data = NULL; + HcfFree(spec); +} + +void DestroyDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec) +{ + if (spec == NULL) { + return; + } + FreeDhCommParamsSpec(&(spec->base)); + HcfFree(spec->pk.data); + spec->pk.data = NULL; + (void)memset_s(spec->sk.data, spec->sk.len, 0, spec->sk.len); + HcfFree(spec->sk.data); + spec->sk.data = NULL; + HcfFree(spec); +} + static void FreeEcFieldMem(HcfECField **field) { HcfFree((*field)->fieldType); @@ -214,6 +283,27 @@ static void DestroyDsaParamsSpec(HcfAsyKeyParamsSpec *spec) } } +static void DestroyDhParamsSpec(HcfAsyKeyParamsSpec *spec) +{ + switch (spec->specType) { + case HCF_COMMON_PARAMS_SPEC: + DestroyDhCommParamsSpec((HcfDhCommParamsSpec *)spec); + break; + case HCF_PUBLIC_KEY_SPEC: + DestroyDhPubKeySpec((HcfDhPubKeyParamsSpec *)spec); + break; + case HCF_PRIVATE_KEY_SPEC: + DestroyDhPriKeySpec((HcfDhPriKeyParamsSpec *)spec); + break; + case HCF_KEY_PAIR_SPEC: + DestroyDhKeyPairSpec((HcfDhKeyPairParamsSpec *)spec); + break; + default: + LOGE("No matching DH key params spec type."); + break; + } +} + static void DestroyEccParamsSpec(HcfAsyKeyParamsSpec *spec) { switch (spec->specType) { @@ -253,18 +343,111 @@ static void DestroyRsaParamsSpec(HcfAsyKeyParamsSpec *spec) } } +void DestroyAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec) +{ + if (spec == NULL) { + return; + } + if (spec->pk.data != NULL) { + (void)memset_s(spec->pk.data, spec->pk.len, 0, spec->pk.len); + HcfFree(spec->pk.data); + spec->pk.data = NULL; + } + if (spec->base.algName != NULL) { + HcfFree(spec->base.algName); + spec->base.algName = NULL; + } + HcfFree(spec); +} + +void DestroyAlg25519PriKeySpec(HcfAlg25519PriKeyParamsSpec *spec) +{ + if (spec == NULL) { + return; + } + if (spec->sk.data != NULL) { + (void)memset_s(spec->sk.data, spec->sk.len, 0, spec->sk.len); + HcfFree(spec->sk.data); + spec->sk.data = NULL; + } + if (spec->base.algName != NULL) { + HcfFree(spec->base.algName); + spec->base.algName = NULL; + } + HcfFree(spec); +} + +void DestroyAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec) +{ + if (spec == NULL) { + return; + } + if (spec->pk.data != NULL) { + (void)memset_s(spec->pk.data, spec->pk.len, 0, spec->pk.len); + HcfFree(spec->pk.data); + spec->pk.data = NULL; + } + if (spec->sk.data != NULL) { + (void)memset_s(spec->sk.data, spec->sk.len, 0, spec->sk.len); + HcfFree(spec->sk.data); + spec->sk.data = NULL; + } + if (spec->base.algName != NULL) { + HcfFree(spec->base.algName); + spec->base.algName = NULL; + } + HcfFree(spec); +} + +static void DestroyAlg25519ParamsSpec(HcfAsyKeyParamsSpec *spec) +{ + switch (spec->specType) { + case HCF_PUBLIC_KEY_SPEC: + DestroyAlg25519PubKeySpec((HcfAlg25519PubKeyParamsSpec *)spec); + break; + case HCF_PRIVATE_KEY_SPEC: + DestroyAlg25519PriKeySpec((HcfAlg25519PriKeyParamsSpec *)spec); + break; + case HCF_KEY_PAIR_SPEC: + DestroyAlg25519KeyPairSpec((HcfAlg25519KeyPairParamsSpec *)spec); + break; + default: + LOGE("No matching alg25519 key params spec type."); + break; + } +} + +static HcfFreeAsyKeySpec ASY_KEY_FREE_ABILITY[] = { + { ALG_NAME_DSA, DestroyDsaParamsSpec }, + { ALG_NAME_ECC, DestroyEccParamsSpec }, + { ALG_NAME_SM2, DestroyEccParamsSpec }, + { ALG_NAME_RSA, DestroyRsaParamsSpec }, + { ALG_NAME_X25519, DestroyAlg25519ParamsSpec }, + { ALG_NAME_ED25519, DestroyAlg25519ParamsSpec }, + { ALG_NAME_DH, DestroyDhParamsSpec } +}; + +static HcfFreeParamsAsyKeySpec FindAsyKeySpecFreeAbility(HcfAsyKeyParamsSpec *spec) +{ + for (uint32_t i = 0; i < sizeof(ASY_KEY_FREE_ABILITY) / sizeof(ASY_KEY_FREE_ABILITY[0]); i++) { + if (strcmp(spec->algName, ASY_KEY_FREE_ABILITY[i].algo) == 0) { + return ASY_KEY_FREE_ABILITY[i].createFreeFunc; + } + } + LOGE("No matching key params spec alg name! [Algo]: %s", spec->algName); + return NULL; +} + void FreeAsyKeySpec(HcfAsyKeyParamsSpec *spec) { if (spec == NULL || spec->algName == NULL) { return; } - if (strcmp(spec->algName, ALG_NAME_DSA) == 0) { - return DestroyDsaParamsSpec(spec); - } else if (strcmp(spec->algName, ALG_NAME_ECC) == 0 || strcmp(spec->algName, ALG_NAME_SM2) == 0) { - return DestroyEccParamsSpec(spec); - } else if (strcmp(spec->algName, ALG_NAME_RSA) == 0) { - return DestroyRsaParamsSpec(spec); + HcfFreeParamsAsyKeySpec createFreeFunc = FindAsyKeySpecFreeAbility(spec); + if (createFreeFunc != NULL) { + return createFreeFunc(spec); } else { - LOGE("No matching key params spec alg name."); + LOGE("create freeFunc failed."); } -} \ No newline at end of file +} + diff --git a/common/src/params_parser.c b/common/src/params_parser.c index efda7ae..ab7f8fe 100644 --- a/common/src/params_parser.c +++ b/common/src/params_parser.c @@ -57,6 +57,19 @@ static const HcfParaConfig PARAM_CONFIG[] = { {"RSA4096", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_4096}, {"RSA8192", HCF_ALG_KEY_TYPE, HCF_OPENSSL_RSA_8192}, + {"DH_modp1536", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_MODP_1536}, + {"DH_modp2048", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_MODP_2048}, + {"DH_modp3072", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_MODP_3072}, + {"DH_modp4096", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_MODP_4096}, + {"DH_modp6144", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_MODP_6144}, + {"DH_modp8192", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_MODP_8192}, + + {"DH_ffdhe2048", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_FFDHE_2048}, + {"DH_ffdhe3072", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_FFDHE_3072}, + {"DH_ffdhe4096", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_FFDHE_4096}, + {"DH_ffdhe6144", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_FFDHE_6144}, + {"DH_ffdhe8192", HCF_ALG_KEY_TYPE, HCF_OPENSSL_DH_FFDHE_8192}, + {"PKCS1", HCF_ALG_PADDING_TYPE, HCF_OPENSSL_RSA_PKCS1_PADDING}, {"PKCS1_OAEP", HCF_ALG_PADDING_TYPE, HCF_OPENSSL_RSA_PKCS1_OAEP_PADDING}, {"PSS", HCF_ALG_PADDING_TYPE, HCF_OPENSSL_RSA_PSS_PADDING}, @@ -98,6 +111,8 @@ static const HcfParaConfig PARAM_CONFIG[] = { {"HMAC", HCF_ALG_TYPE, HCF_ALG_HMAC_DEFAULT}, {"PBKDF2", HCF_ALG_TYPE, HCF_ALG_PBKDF2_DEFAULT}, {"ECC_BP", HCF_ALG_TYPE, HCF_ALG_ECC_BRAINPOOL_DEFAULT}, + {"X25519_BP", HCF_ALG_TYPE, HCF_ALG_X25519_DEFAULT}, + {"DH", HCF_ALG_TYPE, HCF_ALG_DH_DEFAULT}, {"C1C3C2", HCF_ALG_TEXT_FORMAT, HCF_ALG_TEXT_FORMAT_C1C3C2}, {"C1C2C3", HCF_ALG_TEXT_FORMAT, HCF_ALG_TEXT_FORMAT_C1C2C3}, @@ -115,14 +130,20 @@ static const HcfParaConfig PARAM_CONFIG[] = { {"ECC_BrainPoolP384r1", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_BP384R1}, {"ECC_BrainPoolP384t1", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_BP384T1}, {"ECC_BrainPoolP512r1", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_BP512R1}, - {"ECC_BrainPoolP512t1", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_BP512T1} + {"ECC_BrainPoolP512t1", HCF_ALG_KEY_TYPE, HCF_ALG_ECC_BP512T1}, + + {"Ed25519", HCF_ALG_KEY_TYPE, HCF_ALG_ED25519_256}, + {"X25519", HCF_ALG_KEY_TYPE, HCF_ALG_X25519_256} }; static const HcfAlgMap ALG_MAP[] = { {"DSA", HCF_ALG_DSA}, {"RSA", HCF_ALG_RSA}, {"ECC", HCF_ALG_ECC}, - {"SM2", HCF_ALG_SM2} + {"SM2", HCF_ALG_SM2}, + {"Ed25519", HCF_ALG_ED25519}, + {"X25519", HCF_ALG_X25519}, + {"DH", HCF_ALG_DH} }; static const HcfCurveMap CURVE_MAP[] = { diff --git a/frameworks/crypto_operation/key_agreement.c b/frameworks/crypto_operation/key_agreement.c index db9405c..6787894 100644 --- a/frameworks/crypto_operation/key_agreement.c +++ b/frameworks/crypto_operation/key_agreement.c @@ -19,7 +19,9 @@ #include "key_agreement_spi.h" #include "config.h" +#include "dh_openssl.h" #include "ecdh_openssl.h" +#include "x25519_openssl.h" #include "log.h" #include "memory.h" #include "params_parser.h" @@ -42,7 +44,9 @@ typedef struct { } HcfKeyAgreementGenAbility; static const HcfKeyAgreementGenAbility KEY_AGREEMENT_GEN_ABILITY_SET[] = { - { HCF_ALG_ECC, HcfKeyAgreementSpiEcdhCreate } + { HCF_ALG_ECC, HcfKeyAgreementSpiEcdhCreate }, + { HCF_ALG_X25519, HcfKeyAgreementSpiX25519Create }, + { HCF_ALG_DH, HcfKeyAgreementSpiDhCreate } }; static HcfKeyAgreementSpiCreateFunc FindAbility(HcfKeyAgreementParams *params) @@ -79,6 +83,22 @@ static void SetKeyType(HcfAlgParaValue value, HcfKeyAgreementParams *paramsObj) case HCF_ALG_ECC_BP512T1: paramsObj->algo = HCF_ALG_ECC; break; + case HCF_ALG_X25519_256: + paramsObj->algo = HCF_ALG_X25519; + break; + case HCF_OPENSSL_DH_MODP_1536: + case HCF_OPENSSL_DH_MODP_2048: + case HCF_OPENSSL_DH_MODP_3072: + case HCF_OPENSSL_DH_MODP_4096: + case HCF_OPENSSL_DH_MODP_6144: + case HCF_OPENSSL_DH_MODP_8192: + case HCF_OPENSSL_DH_FFDHE_2048: + case HCF_OPENSSL_DH_FFDHE_3072: + case HCF_OPENSSL_DH_FFDHE_4096: + case HCF_OPENSSL_DH_FFDHE_6144: + case HCF_OPENSSL_DH_FFDHE_8192: + paramsObj->algo = HCF_ALG_DH; + break; default: LOGE("Invalid algo %u.", value); break; @@ -91,6 +111,12 @@ static void SetKeyTypeDefault(HcfAlgParaValue value, HcfKeyAgreementParams *par case HCF_ALG_ECC_DEFAULT: paramsObj->algo = HCF_ALG_ECC; break; + case HCF_ALG_X25519_DEFAULT: + paramsObj->algo = HCF_ALG_X25519; + break; + case HCF_ALG_DH_DEFAULT: + paramsObj->algo = HCF_ALG_DH; + break; default: LOGE("Invalid algo %u.", value); break; diff --git a/frameworks/crypto_operation/signature.c b/frameworks/crypto_operation/signature.c index cb3f1e5..2629401 100644 --- a/frameworks/crypto_operation/signature.c +++ b/frameworks/crypto_operation/signature.c @@ -26,6 +26,7 @@ #include "signature_spi.h" #include "signature_rsa_openssl.h" #include "sm2_openssl.h" +#include "ed25519_openssl.h" #include "utils.h" typedef HcfResult (*HcfSignSpiCreateFunc)(HcfSignatureParams *, HcfSignSpi **); @@ -65,6 +66,7 @@ static const HcfSignGenAbility SIGN_GEN_ABILITY_SET[] = { { HCF_ALG_DSA, HcfSignSpiDsaCreate }, { HCF_ALG_SM2, HcfSignSpiSm2Create }, { HCF_ALG_ECC_BRAINPOOL, HcfSignSpiEcdsaCreate }, + { HCF_ALG_ED25519, HcfSignSpiEd25519Create }, }; static const HcfVerifyGenAbility VERIFY_GEN_ABILITY_SET[] = { @@ -73,6 +75,7 @@ static const HcfVerifyGenAbility VERIFY_GEN_ABILITY_SET[] = { { HCF_ALG_DSA, HcfVerifySpiDsaCreate }, { HCF_ALG_SM2, HcfVerifySpiSm2Create }, { HCF_ALG_ECC_BRAINPOOL, HcfVerifySpiEcdsaCreate }, + { HCF_ALG_ED25519, HcfVerifySpiEd25519Create }, }; static HcfSignSpiCreateFunc FindSignAbility(HcfSignatureParams *params) @@ -163,6 +166,9 @@ static void SetKeyType(HcfAlgParaValue value, HcfSignatureParams *paramsObj) case HCF_ALG_SM2_256: paramsObj->algo = HCF_ALG_SM2; break; + case HCF_ALG_ED25519_256: + paramsObj->algo = HCF_ALG_ED25519; + break; default: LOGE("there is not matched algorithm."); break; diff --git a/frameworks/frameworks.gni b/frameworks/frameworks.gni index ad41b93..625e96c 100644 --- a/frameworks/frameworks.gni +++ b/frameworks/frameworks.gni @@ -43,6 +43,7 @@ framework_key_agreement_files = framework_key_files = [ "${framework_path}/key/asy_key_generator.c", + "${framework_path}/key/dh_key_util.c", "${framework_path}/key/ecc_key_util.c", "${framework_path}/key/key_utils.c", "${framework_path}/key/sym_key_generator.c", diff --git a/frameworks/js/napi/crypto/BUILD.gn b/frameworks/js/napi/crypto/BUILD.gn index a2ee368..ac45bbf 100644 --- a/frameworks/js/napi/crypto/BUILD.gn +++ b/frameworks/js/napi/crypto/BUILD.gn @@ -46,6 +46,7 @@ ohos_shared_library("cryptoframework_napi") { "src/napi_asy_key_generator.cpp", "src/napi_asy_key_spec_generator.cpp", "src/napi_cipher.cpp", + "src/napi_dh_key_util.cpp", "src/napi_ecc_key_util.cpp", "src/napi_init.cpp", "src/napi_kdf.cpp", diff --git a/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h b/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h index 151a57e..bf20906 100644 --- a/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h +++ b/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h @@ -62,6 +62,9 @@ const std::string CCM_PARAMS_SPEC = "CcmParamsSpec"; const std::string DSA_ASY_KEY_SPEC = "DSA"; const std::string ECC_ASY_KEY_SPEC = "ECC"; const std::string RSA_ASY_KEY_SPEC = "RSA"; +const std::string X25519_ASY_KEY_SPEC = "X25519"; +const std::string ED25519_ASY_KEY_SPEC = "Ed25519"; +const std::string DH_ASY_KEY_SPEC = "DH"; const std::string SM2_ASY_KEY_SPEC = "SM2"; const std::string TAG_SPEC_TYPE = "specType"; const std::string DSA_COMM_ASY_KEY_SPEC = "DsaCommParamsSpec"; diff --git a/frameworks/js/napi/crypto/inc/napi_dh_key_util.h b/frameworks/js/napi/crypto/inc/napi_dh_key_util.h new file mode 100644 index 0000000..5a0fdd9 --- /dev/null +++ b/frameworks/js/napi/crypto/inc/napi_dh_key_util.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_NAPI_DH_KEY_UTIL_H +#define HCF_NAPI_DH_KEY_UTIL_H + +#include +#include "dh_key_util.h" +#include "log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace CryptoFramework { +class NapiDHKeyUtil { +public: + explicit NapiDHKeyUtil(); + ~NapiDHKeyUtil(); + + static napi_value JsGenDHCommonParamsSpec(napi_env env, napi_callback_info info); + static napi_value DHKeyUtilConstructor(napi_env env, napi_callback_info info); + static napi_value GenDHCommonParamSpec(napi_env env); + static void DefineNapiDHKeyUtilJSClass(napi_env env, napi_value exports); +}; +} // namespace CryptoFramework +} // namespace OHOS +#endif diff --git a/frameworks/js/napi/crypto/inc/napi_utils.h b/frameworks/js/napi/crypto/inc/napi_utils.h index 0b68f65..30c5632 100644 --- a/frameworks/js/napi/crypto/inc/napi_utils.h +++ b/frameworks/js/napi/crypto/inc/napi_utils.h @@ -24,7 +24,9 @@ #include "blob.h" #include "big_integer.h" #include "cipher.h" +#include "detailed_dh_key_params.h" #include "detailed_ecc_key_params.h" +#include "detailed_alg_25519_key_params.h" #include "napi/native_api.h" #include "napi/native_node_api.h" #include "signature.h" @@ -60,7 +62,7 @@ bool GetAsyKeySpecFromNapiValue(napi_env env, napi_value arg, HcfAsyKeyParamsSpe bool BuildSetNamedProperty(napi_env env, HcfBigInteger *number, const char *name, napi_value *intence); napi_value ConvertBigIntToNapiValue(napi_env env, HcfBigInteger *blob); napi_value ConvertEccCommParamsSpecToNapiValue(napi_env env, HcfEccCommParamsSpec *blob); - +napi_value ConvertDhCommParamsSpecToNapiValue(napi_env env, HcfDhCommParamsSpec *blob); bool GetStringFromJSParams(napi_env env, napi_value arg, std::string &returnStr); bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt); diff --git a/frameworks/js/napi/crypto/src/napi_dh_key_util.cpp b/frameworks/js/napi/crypto/src/napi_dh_key_util.cpp new file mode 100644 index 0000000..7174c1d --- /dev/null +++ b/frameworks/js/napi/crypto/src/napi_dh_key_util.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi_dh_key_util.h" +#include "securec.h" +#include "detailed_ecc_key_params.h" +#include "log.h" + +#include "napi_crypto_framework_defines.h" +#include "napi_utils.h" +#include "napi_key_pair.h" +#include "napi_pri_key.h" +#include "napi_pub_key.h" + +namespace OHOS { +namespace CryptoFramework { +NapiDHKeyUtil::NapiDHKeyUtil() {} + +NapiDHKeyUtil::~NapiDHKeyUtil() {} + +napi_value NapiDHKeyUtil::JsGenDHCommonParamsSpec(napi_env env, napi_callback_info info) +{ + size_t expectedArgc = PARAMS_NUM_TWO; + size_t argc = ARGS_SIZE_TWO; + napi_value argv[ARGS_SIZE_TWO] = { nullptr }; + napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + + if ((argc != expectedArgc) && (argc != (expectedArgc - 1))) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid.")); + LOGE("The input args num is invalid."); + return nullptr; + } + + int32_t pLen = 0; + if (!GetInt32FromJSParams(env, argv[0], pLen)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get pLen.")); + LOGE("failed to get pLen."); + return NapiGetNull(env); + } + + int32_t skLen = 0; + if (argc == expectedArgc) { + if (!GetInt32FromJSParams(env, argv[1], skLen)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get skLen.")); + LOGE("failed to get skLen."); + return NapiGetNull(env); + } + } + HcfDhCommParamsSpec *dhCommParamsSpec = nullptr; + if (HcfDhKeyUtilCreate(pLen, skLen, &dhCommParamsSpec) != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create c generator fail.")); + LOGE("create c generator fail."); + return NapiGetNull(env); + } + + napi_value instance = ConvertDhCommParamsSpecToNapiValue(env, dhCommParamsSpec); + FreeDhCommParamsSpec(dhCommParamsSpec); + return instance; +} + +napi_value NapiDHKeyUtil::DHKeyUtilConstructor(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t argc = ARGS_SIZE_ONE; + napi_value argv[ARGS_SIZE_ONE] = { nullptr }; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + return thisVar; +} + +napi_value NapiDHKeyUtil::GenDHCommonParamSpec(napi_env env) +{ + napi_value cons = nullptr; + napi_property_descriptor clzDes[] = { + DECLARE_NAPI_STATIC_FUNCTION("genDHCommonParamsSpec", NapiDHKeyUtil::JsGenDHCommonParamsSpec), + }; + NAPI_CALL(env, napi_define_class(env, "DHKeyUtil", NAPI_AUTO_LENGTH, NapiDHKeyUtil::DHKeyUtilConstructor, + nullptr, sizeof(clzDes) / sizeof(clzDes[0]), clzDes, &cons)); + return cons; +} + +void NapiDHKeyUtil::DefineNapiDHKeyUtilJSClass(napi_env env, napi_value exports) +{ + napi_set_named_property(env, exports, "DHKeyUtil", NapiDHKeyUtil::GenDHCommonParamSpec(env)); +} +} // CryptoFramework +} // OHOS diff --git a/frameworks/js/napi/crypto/src/napi_init.cpp b/frameworks/js/napi/crypto/src/napi_init.cpp index e61f4c1..8c554a3 100644 --- a/frameworks/js/napi/crypto/src/napi_init.cpp +++ b/frameworks/js/napi/crypto/src/napi_init.cpp @@ -20,6 +20,7 @@ #include "napi_asy_key_spec_generator.h" #include "napi_sym_key_generator.h" #include "napi_cipher.h" +#include "napi_dh_key_util.h" #include "napi_ecc_key_util.h" #include "napi_key_pair.h" #include "napi_pri_key.h" @@ -110,6 +111,15 @@ static napi_value CreateAsyKeySpecItemCode(napi_env env) AddUint32Property(env, code, "RSA_N_BN", RSA_N_BN); AddUint32Property(env, code, "RSA_SK_BN", RSA_SK_BN); AddUint32Property(env, code, "RSA_PK_BN", RSA_PK_BN); + AddUint32Property(env, code, "DH_P_BN", DH_P_BN); + AddUint32Property(env, code, "DH_G_BN", DH_G_BN); + AddUint32Property(env, code, "DH_L_NUM", DH_L_NUM); + AddUint32Property(env, code, "DH_PK_BN", DH_PK_BN); + AddUint32Property(env, code, "DH_SK_BN", DH_SK_BN); + AddUint32Property(env, code, "ED25519_SK_BN", ED25519_SK_BN); + AddUint32Property(env, code, "ED25519_PK_BN", ED25519_PK_BN); + AddUint32Property(env, code, "X25519_SK_BN", X25519_SK_BN); + AddUint32Property(env, code, "X25519_PK_BN", X25519_PK_BN); return code; } @@ -220,6 +230,7 @@ static napi_value ModuleExport(napi_env env, napi_value exports) NapiCipher::DefineCipherJSClass(env, exports); NapiKdf::DefineKdfJSClass(env, exports); NapiECCKeyUtil::DefineNapiECCKeyUtilJSClass(env, exports); + NapiDHKeyUtil::DefineNapiDHKeyUtilJSClass(env, exports); LOGI("module init end."); return exports; } diff --git a/frameworks/js/napi/crypto/src/napi_utils.cpp b/frameworks/js/napi/crypto/src/napi_utils.cpp index d440b67..026447d 100644 --- a/frameworks/js/napi/crypto/src/napi_utils.cpp +++ b/frameworks/js/napi/crypto/src/napi_utils.cpp @@ -25,6 +25,8 @@ #include "detailed_dsa_key_params.h" #include "detailed_ecc_key_params.h" #include "detailed_rsa_key_params.h" +#include "detailed_alg_25519_key_params.h" +#include "detailed_dh_key_params.h" #include "utils.h" namespace OHOS { @@ -60,7 +62,16 @@ static const AsyKeySpecItemRelation ASY_KEY_SPEC_RELATION_SET[] = { { RSA_N_BN, SPEC_ITEM_TYPE_BIG_INT }, { RSA_SK_BN, SPEC_ITEM_TYPE_BIG_INT }, - { RSA_PK_BN, SPEC_ITEM_TYPE_BIG_INT } + { RSA_PK_BN, SPEC_ITEM_TYPE_BIG_INT }, + { DH_P_BN, SPEC_ITEM_TYPE_BIG_INT }, + { DH_G_BN, SPEC_ITEM_TYPE_BIG_INT }, + { DH_L_NUM, SPEC_ITEM_TYPE_NUM }, + { DH_PK_BN, SPEC_ITEM_TYPE_BIG_INT }, + { DH_SK_BN, SPEC_ITEM_TYPE_BIG_INT }, + { ED25519_SK_BN, SPEC_ITEM_TYPE_BIG_INT }, + { ED25519_PK_BN, SPEC_ITEM_TYPE_BIG_INT }, + { X25519_SK_BN, SPEC_ITEM_TYPE_BIG_INT }, + { X25519_PK_BN, SPEC_ITEM_TYPE_BIG_INT }, }; int32_t GetAsyKeySpecType(AsyKeySpecItem targetItemType) @@ -567,7 +578,7 @@ static bool GetDsaCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParams { HcfDsaCommParamsSpec *spec = reinterpret_cast(HcfMalloc(sizeof(HcfDsaCommParamsSpec), 0)); if (spec == nullptr) { - LOGE("malloc falied!"); + LOGE("malloc failed!"); return false; } if (!InitDsaCommonAsyKeySpec(env, arg, spec)) { @@ -584,7 +595,7 @@ static bool GetDsaPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec * HcfDsaPubKeyParamsSpec *spec = reinterpret_cast( HcfMalloc(sizeof(HcfDsaPubKeyParamsSpec), 0)); if (spec == nullptr) { - LOGE("malloc falied!"); + LOGE("malloc failed!"); return false; } @@ -616,7 +627,7 @@ static bool GetDsaKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParam HcfDsaKeyPairParamsSpec *spec = reinterpret_cast( HcfMalloc(sizeof(HcfDsaKeyPairParamsSpec), 0)); if (spec == nullptr) { - LOGE("malloc falied!"); + LOGE("malloc failed!"); return false; } @@ -770,7 +781,7 @@ static bool InitEccDetailAsyKeySpec(napi_env env, napi_value arg, HcfEccCommPara return true; } -static bool InitEccCommonAsyKeySpec(napi_env env, napi_value arg, HcfEccCommParamsSpec *spec, const string &algName) +static bool InitEccCommonAsyKeySpec(napi_env env, napi_value arg, HcfEccCommParamsSpec *spec, const string algName) { size_t algNameLen = ECC_ASY_KEY_SPEC.length(); spec->base.algName = static_cast(HcfMalloc(algNameLen + 1, 0)); @@ -819,7 +830,7 @@ static bool GetEccCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParams { HcfEccCommParamsSpec *spec = reinterpret_cast(HcfMalloc(sizeof(HcfEccCommParamsSpec), 0)); if (spec == nullptr) { - LOGE("malloc falied!"); + LOGE("malloc failed!"); return false; } if (!InitEccCommonAsyKeySpec(env, arg, spec, algName)) { @@ -836,7 +847,7 @@ static bool GetEccPriKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec * HcfEccPriKeyParamsSpec *spec = reinterpret_cast(HcfMalloc(sizeof(HcfEccPriKeyParamsSpec), 0)); if (spec == nullptr) { - LOGE("malloc falied!"); + LOGE("malloc failed!"); return false; } @@ -870,7 +881,7 @@ static bool GetEccPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec * HcfEccPubKeyParamsSpec *spec = reinterpret_cast(HcfMalloc(sizeof(HcfEccPubKeyParamsSpec), 0)); if (spec == nullptr) { - LOGE("malloc falied!"); + LOGE("malloc failed!"); return false; } @@ -903,7 +914,7 @@ static bool GetEccKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParam HcfEccKeyPairParamsSpec *spec = reinterpret_cast(HcfMalloc(sizeof(HcfEccKeyPairParamsSpec), 0)); if (spec == nullptr) { - LOGE("malloc falied!"); + LOGE("malloc failed!"); return false; } @@ -1000,7 +1011,7 @@ static bool GetRsaPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec * HcfRsaPubKeyParamsSpec *spec = reinterpret_cast(HcfMalloc(sizeof(HcfRsaPubKeyParamsSpec), 0)); if (spec == nullptr) { - LOGE("malloc falied!"); + LOGE("malloc failed!"); return false; } @@ -1032,7 +1043,7 @@ static bool GetRsaKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParam HcfRsaKeyPairParamsSpec *spec = reinterpret_cast(HcfMalloc(sizeof(HcfRsaKeyPairParamsSpec), 0)); if (spec == nullptr) { - LOGE("malloc falied!"); + LOGE("malloc failed!"); return false; } @@ -1097,6 +1108,326 @@ static bool GetRsaAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec * } } +static bool InitAlg25519CommonAsyKeySpec(HcfAsyKeyParamsSpec *spec, string algName) +{ + size_t algNameLen = algName.length(); + spec->algName = static_cast(HcfMalloc(algNameLen + 1, 0)); + if (spec->algName == nullptr) { + LOGE("malloc alg25519 algName failed!"); + return false; + } + (void)memcpy_s(spec->algName, algNameLen + 1, algName.c_str(), algNameLen); + return true; +} + +static bool GetAlg25519PriKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, string algName) +{ + HcfAlg25519PriKeyParamsSpec *spec = + reinterpret_cast(HcfMalloc(sizeof(HcfAlg25519PriKeyParamsSpec), 0)); + if (spec == nullptr) { + LOGE("malloc failed!"); + return false; + } + if (!InitAlg25519CommonAsyKeySpec(reinterpret_cast(spec), algName)) { + LOGE("InitRsaCommonAsyKeySpec failed!"); + DestroyAlg25519PriKeySpec(spec); + return false; + } + spec->base.specType = HCF_PRIVATE_KEY_SPEC; + + napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk"); + bool ret = GetBigIntFromNapiValue(env, sk, &spec->sk); + if (!ret) { + // get big int fail, sk is null + DestroyAlg25519PriKeySpec(spec); + return false; + } + *asyKeySpec = reinterpret_cast(spec); + return true; +} + +static bool GetAlg25519PubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, string algName) +{ + HcfAlg25519PubKeyParamsSpec *spec = + reinterpret_cast(HcfMalloc(sizeof(HcfAlg25519PubKeyParamsSpec), 0)); + if (spec == nullptr) { + LOGE("malloc failed!"); + return false; + } + if (!InitAlg25519CommonAsyKeySpec(reinterpret_cast(spec), algName)) { + LOGE("InitRsaCommonAsyKeySpec failed!"); + DestroyAlg25519PubKeySpec(spec); + return false; + } + spec->base.specType = HCF_PUBLIC_KEY_SPEC; + + napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk"); + bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk); + if (!ret) { + DestroyAlg25519PubKeySpec(spec); + return false; + } + *asyKeySpec = reinterpret_cast(spec); + return true; +} + +static bool GetAlg25519KeyPairAsyKeySpec(napi_env env, napi_value arg, + HcfAsyKeyParamsSpec **asyKeySpec, string algName) +{ + HcfAlg25519KeyPairParamsSpec *spec = + reinterpret_cast(HcfMalloc(sizeof(HcfAlg25519KeyPairParamsSpec), 0)); + if (spec == nullptr) { + LOGE("malloc failed!"); + return false; + } + if (!InitAlg25519CommonAsyKeySpec(reinterpret_cast(spec), algName)) { + LOGE("InitRsaCommonAsyKeySpec failed!"); + HcfFree(spec); + return false; + } + spec->base.specType = HCF_KEY_PAIR_SPEC; + + // get big int fail, sk is null + napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk"); + bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk); + if (!ret) { + DestroyAlg25519KeyPairSpec(spec); + return false; + } + napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk"); + ret = GetBigIntFromNapiValue(env, sk, &spec->sk); + if (!ret) { + DestroyAlg25519KeyPairSpec(spec); + return false; + } + *asyKeySpec = reinterpret_cast(spec); + return true; +} + +static bool GetAlg25519AsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, string algName) +{ + napi_value data = nullptr; + napi_valuetype valueType = napi_undefined; + + napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data); + napi_typeof(env, data, &valueType); + if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) { + LOGE("failed to get valid algo name!"); + return false; + } + HcfAsyKeySpecType asyKeySpecType; + status = napi_get_value_uint32(env, data, reinterpret_cast(&asyKeySpecType)); + if (status != napi_ok) { + LOGE("failed to get valid asyKeySpecType!"); + return false; + } + if (asyKeySpecType == HCF_PRIVATE_KEY_SPEC) { + return GetAlg25519PriKeySpec(env, arg, asyKeySpec, algName); + } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) { + return GetAlg25519PubKeySpec(env, arg, asyKeySpec, algName); + } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) { + return GetAlg25519KeyPairAsyKeySpec(env, arg, asyKeySpec, algName); + } else { + LOGE("keySpec not support!"); + return false; + } +} + +static bool InitDhCommonAsyKeySpec(napi_env env, napi_value arg, HcfDhCommParamsSpec *spec) +{ + size_t algNameLen = DH_ASY_KEY_SPEC.length(); + spec->base.algName = static_cast(HcfMalloc(algNameLen + 1, 0)); + if (spec->base.algName == nullptr) { + LOGE("malloc DH algName failed!"); + return false; + } + (void)memcpy_s(spec->base.algName, algNameLen+ 1, DH_ASY_KEY_SPEC.c_str(), algNameLen); + spec->base.specType = HCF_COMMON_PARAMS_SPEC; + + napi_value length = nullptr; + napi_valuetype valueType = napi_undefined; + napi_status status = napi_get_named_property(env, arg, "l", &length); + napi_typeof(env, length, &valueType); + if ((status != napi_ok) || (length == nullptr) || (valueType == napi_undefined)) { + LOGE("failed to get valid l!"); + HcfFree(spec->base.algName); + spec->base.algName = nullptr; + return false; + } + if (!GetInt32FromJSParams(env, length, spec->length)) { + LOGE("get dh asyKeySpec length failed!"); + HcfFree(spec->base.algName); + spec->base.algName = nullptr; + return false; + } + napi_value p = GetDetailAsyKeySpecValue(env, arg, "p"); + napi_value g = GetDetailAsyKeySpecValue(env, arg, "g"); + bool ret = GetBigIntFromNapiValue(env, p, &spec->p); + if (!ret) { + HcfFree(spec->base.algName); + spec->base.algName = nullptr; + return false; + } + ret = GetBigIntFromNapiValue(env, g, &spec->g); + if (!ret) { + FreeDhCommParamsSpec(spec); + return false; + } + return true; +} + +static bool GetDhCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec) +{ + HcfDhCommParamsSpec *spec = reinterpret_cast(HcfMalloc(sizeof(HcfDhCommParamsSpec), 0)); + if (spec == nullptr) { + LOGE("malloc failed!"); + return false; + } + if (!InitDhCommonAsyKeySpec(env, arg, spec)) { + LOGE("InitDhCommonAsyKeySpec failed!"); + HcfFree(spec); + return false; + } + *asyKeySpec = reinterpret_cast(spec); + return true; +} + +static bool GetDhPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec) +{ + HcfDhPubKeyParamsSpec *spec = reinterpret_cast( + HcfMalloc(sizeof(HcfDhPubKeyParamsSpec), 0)); + if (spec == nullptr) { + LOGE("malloc failed!"); + return false; + } + + napi_value commSpecValue = GetCommSpecNapiValue(env, arg); + if (commSpecValue == nullptr) { + LOGE("Get comm spec napi value failed."); + HcfFree(spec); + return false; + } + if (!InitDhCommonAsyKeySpec(env, commSpecValue, reinterpret_cast(spec))) { + LOGE("InitDhCommonAsyKeySpec failed."); + HcfFree(spec); + return false; + } + spec->base.base.specType = HCF_PUBLIC_KEY_SPEC; + + napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk"); + bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk); + if (!ret) { + DestroyDhPubKeySpec(spec); + return false; + } + *asyKeySpec = reinterpret_cast(spec); + return true; +} + +static bool GetDhPriKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec) +{ + HcfDhPriKeyParamsSpec *spec = reinterpret_cast( + HcfMalloc(sizeof(HcfDhPriKeyParamsSpec), 0)); + if (spec == nullptr) { + LOGE("malloc failed!"); + return false; + } + + napi_value commSpecValue = GetCommSpecNapiValue(env, arg); + if (commSpecValue == nullptr) { + LOGE("Get comm spec napi value failed."); + HcfFree(spec); + return false; + } + if (!InitDhCommonAsyKeySpec(env, commSpecValue, reinterpret_cast(spec))) { + LOGE("InitDhCommonAsyKeySpec failed."); + HcfFree(spec); + return false; + } + spec->base.base.specType = HCF_PRIVATE_KEY_SPEC; + + napi_value pk = GetDetailAsyKeySpecValue(env, arg, "sk"); + bool ret = GetBigIntFromNapiValue(env, pk, &spec->sk); + if (!ret) { + DestroyDhPriKeySpec(spec); + return false; + } + *asyKeySpec = reinterpret_cast(spec); + return true; +} + +static bool GetDhKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec) +{ + HcfDhKeyPairParamsSpec *spec = reinterpret_cast( + HcfMalloc(sizeof(HcfDhKeyPairParamsSpec), 0)); + if (spec == nullptr) { + LOGE("malloc failed!"); + return false; + } + + napi_value commSpecValue = GetCommSpecNapiValue(env, arg); + if (commSpecValue == nullptr) { + LOGE("Get comm spec napi value failed."); + HcfFree(spec); + return false; + } + if (!InitDhCommonAsyKeySpec(env, commSpecValue, reinterpret_cast(spec))) { + LOGE("InitDhCommonAsyKeySpec failed!"); + HcfFree(spec); + return false; + } + spec->base.base.specType = HCF_KEY_PAIR_SPEC; + + napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk"); + bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk); + if (!ret) { + FreeDhCommParamsSpec(reinterpret_cast(spec)); + HcfFree(spec); + return false; + } + napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk"); + ret = GetBigIntFromNapiValue(env, sk, &spec->sk); + if (!ret) { + FreeDhCommParamsSpec(reinterpret_cast(spec)); + HcfFree(spec->pk.data); + HcfFree(spec); + return false; + } + *asyKeySpec = reinterpret_cast(spec); + return true; +} + +static bool GetDh25519AsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec) +{ + napi_value data = nullptr; + napi_valuetype valueType = napi_undefined; + + napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data); + napi_typeof(env, data, &valueType); + if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) { + LOGE("failed to get valid algo name!"); + return false; + } + HcfAsyKeySpecType asyKeySpecType; + status = napi_get_value_uint32(env, data, reinterpret_cast(&asyKeySpecType)); + if (status != napi_ok) { + LOGE("failed to get valid asyKeySpecType!"); + return false; + } + if (asyKeySpecType == HCF_PRIVATE_KEY_SPEC) { + return GetDhPriKeySpec(env, arg, asyKeySpec); + } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) { + return GetDhPubKeySpec(env, arg, asyKeySpec); + } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) { + return GetDhKeyPairAsyKeySpec(env, arg, asyKeySpec); + } else if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) { + return GetDhCommonAsyKeySpec(env, arg, asyKeySpec); + } else { + LOGE("keySpec not support!"); + return false; + } +} + bool GetAsyKeySpecFromNapiValue(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec) { napi_value data = nullptr; @@ -1125,6 +1456,10 @@ bool GetAsyKeySpecFromNapiValue(napi_env env, napi_value arg, HcfAsyKeyParamsSpe return GetEccAsyKeySpec(env, arg, asyKeySpec, algName); } else if (algName.compare(RSA_ASY_KEY_SPEC) == 0) { return GetRsaAsyKeySpec(env, arg, asyKeySpec); + } else if (algName.compare(ED25519_ASY_KEY_SPEC) == 0 || algName.compare(X25519_ASY_KEY_SPEC) == 0) { + return GetAlg25519AsyKeySpec(env, arg, asyKeySpec, algName); + } else if (algName.compare(DH_ASY_KEY_SPEC) == 0) { + return GetDh25519AsyKeySpec(env, arg, asyKeySpec); } else { LOGE("AlgName not support! [AlgName]: %s", algName.c_str()); return false; @@ -1510,10 +1845,10 @@ static napi_value ConvertEccCommonParamPointToNapiValue(napi_env env, HcfEccComm return point; } -bool BuildSetNamedProperty(napi_env env, HcfBigInteger *number, const char *name, napi_value *intence) +bool BuildSetNamedProperty(napi_env env, HcfBigInteger *number, const char *name, napi_value *instance) { napi_value value = ConvertBigIntToNapiValue(env, number); - napi_status status = napi_set_named_property(env, *intence, name, value); + napi_status status = napi_set_named_property(env, *instance, name, value); if (status != napi_ok) { LOGE("create value failed!"); return false; @@ -1522,17 +1857,17 @@ bool BuildSetNamedProperty(napi_env env, HcfBigInteger *number, const char *name } static bool BuildIntancePartertoNapiValueSon(napi_env env, napi_status status, HcfEccCommParamsSpec *blob, - napi_value *intence) + napi_value *instance) { - if (!BuildSetNamedProperty(env, &(blob->a), "a", intence)) { + if (!BuildSetNamedProperty(env, &(blob->a), "a", instance)) { LOGE("build setNamedProperty a failed!"); return false; } - if (!BuildSetNamedProperty(env, &(blob->b), "b", intence)) { + if (!BuildSetNamedProperty(env, &(blob->b), "b", instance)) { LOGE("build setNamedProperty b failed!"); return false; } - if (!BuildSetNamedProperty(env, &(blob->n), "n", intence)) { + if (!BuildSetNamedProperty(env, &(blob->n), "n", instance)) { LOGE("build setNamedProperty n failed!"); return false; } @@ -1542,7 +1877,7 @@ static bool BuildIntancePartertoNapiValueSon(napi_env env, napi_status status, H LOGE("create h uint32 failed!"); return false; } - status = napi_set_named_property(env, *intence, "h", h); + status = napi_set_named_property(env, *instance, "h", h); if (status != napi_ok) { LOGE("create h uint32 failed!"); return false; @@ -1550,7 +1885,7 @@ static bool BuildIntancePartertoNapiValueSon(napi_env env, napi_status status, H return true; } -static bool BuildIntenceParterToNapiValue(napi_env env, HcfEccCommParamsSpec *blob, napi_value *intence) +static bool BuildInstanceParterToNapiValue(napi_env env, HcfEccCommParamsSpec *blob, napi_value *instance) { napi_value algName; size_t algNameLength = HcfStrlen(blob->base.algName); @@ -1569,17 +1904,17 @@ static bool BuildIntenceParterToNapiValue(napi_env env, HcfEccCommParamsSpec *bl LOGE("create uint32 failed!"); return false; } - status = napi_set_named_property(env, *intence, "algName", algName); + status = napi_set_named_property(env, *instance, "algName", algName); if (status != napi_ok) { LOGE("create set algName failed!"); return false; } - status = napi_set_named_property(env, *intence, "specType", specType); + status = napi_set_named_property(env, *instance, "specType", specType); if (status != napi_ok) { LOGE("create set specType failed!"); return false; } - if (!BuildIntancePartertoNapiValueSon(env, status, blob, intence)) { + if (!BuildIntancePartertoNapiValueSon(env, status, blob, instance)) { LOGE("create intance parter napi value failed!"); return false; } @@ -1593,8 +1928,8 @@ napi_value ConvertEccCommParamsSpecToNapiValue(napi_env env, HcfEccCommParamsSpe LOGE("Invalid blob!"); return NapiGetNull(env); } - napi_value intence; - napi_status status = napi_create_object(env, &intence); + napi_value instance; + napi_status status = napi_create_object(env, &instance); if (status != napi_ok) { napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create object failed!")); LOGE("create object failed!"); @@ -1612,24 +1947,127 @@ napi_value ConvertEccCommParamsSpecToNapiValue(napi_env env, HcfEccCommParamsSpe LOGE("Covert commonParam fieldFp failed!"); return NapiGetNull(env); } - if (!BuildIntenceParterToNapiValue(env, blob, &intence)) { + if (!BuildInstanceParterToNapiValue(env, blob, &instance)) { napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build object failed!")); LOGE("Build object failed!"); return NapiGetNull(env); } - status = napi_set_named_property(env, intence, "field", field); + status = napi_set_named_property(env, instance, "field", field); if (status != napi_ok) { napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set fieldFp failed!")); LOGE("set fieldFp failed!"); return NapiGetNull(env); } - status = napi_set_named_property(env, intence, "g", point); + status = napi_set_named_property(env, instance, "g", point); if (status != napi_ok) { napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set g failed!")); LOGE("set g failed!"); return NapiGetNull(env); } - return intence; + return instance; +} + +static bool BuildDhInstanceToNapiValueSub(napi_env env, HcfDhCommParamsSpec *blob, napi_value *instance) +{ + if (!BuildSetNamedProperty(env, &(blob->p), "p", instance)) { + LOGE("build setNamedProperty a failed!"); + return false; + } + if (!BuildSetNamedProperty(env, &(blob->g), "g", instance)) { + LOGE("build setNamedProperty b failed!"); + return false; + } + napi_value length; + napi_status status = napi_create_int32(env, blob->length, &length); + if (status != napi_ok) { + LOGE("create length number failed!"); + return false; + } + status = napi_set_named_property(env, *instance, "l", length); + if (status != napi_ok) { + LOGE("create length number failed!"); + return false; + } + return true; +} + +static bool BuildDhInstanceToNapiValue(napi_env env, HcfDhCommParamsSpec *blob, napi_value *instance) +{ + napi_value algName; + size_t algNameLength = HcfStrlen(blob->base.algName); + if (!algNameLength) { + LOGE("algName is empty!"); + return false; + } + napi_status status = napi_create_string_utf8(env, blob->base.algName, algNameLength, &algName); + if (status != napi_ok) { + LOGE("create algName failed!"); + return false; + } + napi_value specType; + status = napi_create_uint32(env, blob->base.specType, &specType); + if (status != napi_ok) { + LOGE("create uint32 failed!"); + return false; + } + status = napi_set_named_property(env, *instance, "algName", algName); + if (status != napi_ok) { + LOGE("create set algName failed!"); + return false; + } + status = napi_set_named_property(env, *instance, "specType", specType); + if (status != napi_ok) { + LOGE("create set specType failed!"); + return false; + } + if (!BuildDhInstanceToNapiValueSub(env, blob, instance)) { + LOGE("create intance parter napi value failed!"); + return false; + } + return true; +} + +static bool CheckDhCommonParamSpec(napi_env env, HcfDhCommParamsSpec *blob) +{ + if (blob == nullptr) { + LOGE("Invalid blob!"); + return false; + } + if (blob->base.algName == nullptr) { + LOGE("Invalid blob algName!"); + return false; + } + if (blob->p.data == nullptr || blob->p.len == 0) { + LOGE("Invalid blob a!"); + return false; + } + if (blob->g.data == nullptr || blob->g.len == 0) { + LOGE("Invalid blob point x!"); + return false; + } + return true; +} + +napi_value ConvertDhCommParamsSpecToNapiValue(napi_env env, HcfDhCommParamsSpec *blob) +{ + if (!CheckDhCommonParamSpec(env, blob)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!")); + LOGE("Invalid blob!"); + return NapiGetNull(env); + } + napi_value instance; + napi_status status = napi_create_object(env, &instance); + if (status != napi_ok) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create object failed!")); + LOGE("create object failed!"); + return NapiGetNull(env); + } + if (!BuildDhInstanceToNapiValue(env, blob, &instance)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build object failed!")); + LOGE("Build object failed!"); + return NapiGetNull(env); + } + return instance; } } // namespace CryptoFramework } // namespace OHOS diff --git a/frameworks/key/asy_key_generator.c b/frameworks/key/asy_key_generator.c index 4fe71c5..b57ca99 100644 --- a/frameworks/key/asy_key_generator.c +++ b/frameworks/key/asy_key_generator.c @@ -19,10 +19,14 @@ #include "asy_key_generator_spi.h" #include "config.h" +#include "detailed_alg_25519_key_params.h" +#include "detailed_dh_key_params.h" #include "detailed_dsa_key_params.h" #include "detailed_rsa_key_params.h" #include "detailed_ecc_key_params.h" +#include "dh_asy_key_generator_openssl.h" #include "dsa_asy_key_generator_openssl.h" +#include "alg_25519_asy_key_generator_openssl.h" #include "ecc_asy_key_generator_openssl.h" #include "key_utils.h" #include "params_parser.h" @@ -36,6 +40,9 @@ #define ALG_NAME_ECC "ECC" #define ALG_NAME_SM2 "SM2" #define ALG_NAME_RSA "RSA" +#define ALG_NAME_DH "DH" +#define ALG_NAME_X25519 "X25519" +#define ALG_NAME_ED25519 "Ed25519" #define ASY_KEY_GENERATOR_CLASS "HcfAsyKeyGenerator" #define ASY_KEY_GENERATOR_BY_SPEC_CLASS "HcfAsyKeyGeneratorBySpec" @@ -67,7 +74,10 @@ static const HcfAsyKeyGenAbility ASY_KEY_GEN_ABILITY_SET[] = { { HCF_ALG_RSA, HcfAsyKeyGeneratorSpiRsaCreate }, { HCF_ALG_ECC, HcfAsyKeyGeneratorSpiEccCreate }, { HCF_ALG_DSA, HcfAsyKeyGeneratorSpiDsaCreate }, - { HCF_ALG_SM2, HcfAsyKeyGeneratorSpiSm2Create } + { HCF_ALG_SM2, HcfAsyKeyGeneratorSpiSm2Create }, + { HCF_ALG_ED25519, HcfAsyKeyGeneratorSpiEd25519Create }, + { HCF_ALG_X25519, HcfAsyKeyGeneratorSpiX25519Create }, + { HCF_ALG_DH, HcfAsyKeyGeneratorSpiDhCreate } }; typedef struct { @@ -106,7 +116,20 @@ static const KeyTypeAlg KEY_TYPE_MAP[] = { { HCF_ALG_ECC_BP384R1, HCF_ALG_ECC_BP384R1, HCF_ALG_ECC }, { HCF_ALG_ECC_BP384T1, HCF_ALG_ECC_BP384T1, HCF_ALG_ECC }, { HCF_ALG_ECC_BP512R1, HCF_ALG_ECC_BP512R1, HCF_ALG_ECC }, - { HCF_ALG_ECC_BP512T1, HCF_ALG_ECC_BP512T1, HCF_ALG_ECC } + { HCF_ALG_ECC_BP512T1, HCF_ALG_ECC_BP512T1, HCF_ALG_ECC }, + { HCF_ALG_ED25519_256, HCF_ALG_ED25519_256, HCF_ALG_ED25519 }, + { HCF_ALG_X25519_256, HCF_ALG_X25519_256, HCF_ALG_X25519 }, + { HCF_OPENSSL_DH_MODP_1536, HCF_DH_MODP_SIZE_1536, HCF_ALG_DH }, + { HCF_OPENSSL_DH_MODP_2048, HCF_DH_MODP_SIZE_2048, HCF_ALG_DH }, + { HCF_OPENSSL_DH_MODP_3072, HCF_DH_MODP_SIZE_3072, HCF_ALG_DH }, + { HCF_OPENSSL_DH_MODP_4096, HCF_DH_MODP_SIZE_4096, HCF_ALG_DH }, + { HCF_OPENSSL_DH_MODP_6144, HCF_DH_MODP_SIZE_6144, HCF_ALG_DH }, + { HCF_OPENSSL_DH_MODP_8192, HCF_DH_MODP_SIZE_8192, HCF_ALG_DH }, + { HCF_OPENSSL_DH_FFDHE_2048, HCF_DH_FFDHE_SIZE_2048, HCF_ALG_DH }, + { HCF_OPENSSL_DH_FFDHE_3072, HCF_DH_FFDHE_SIZE_3072, HCF_ALG_DH }, + { HCF_OPENSSL_DH_FFDHE_4096, HCF_DH_FFDHE_SIZE_4096, HCF_ALG_DH }, + { HCF_OPENSSL_DH_FFDHE_6144, HCF_DH_FFDHE_SIZE_6144, HCF_ALG_DH }, + { HCF_OPENSSL_DH_FFDHE_8192, HCF_DH_FFDHE_SIZE_8192, HCF_ALG_DH } }; static bool IsDsaCommParamsSpecValid(HcfDsaCommParamsSpec *paramsSpec) { @@ -173,6 +196,82 @@ static bool IsDsaParamsSpecValid(const HcfAsyKeyParamsSpec *paramsSpec) return ret; } +static bool IsDhCommParamsSpecValid(HcfDhCommParamsSpec *paramsSpec) +{ + if ((paramsSpec->p.data == NULL) || (paramsSpec->p.len == 0)) { + LOGE("BigInteger p is invalid"); + return false; + } + if ((paramsSpec->g.data == NULL) || (paramsSpec->g.len == 0)) { + LOGE("BigInteger g is invalid"); + return false; + } + return true; +} + +static bool IsDhPriKeySpecValid(HcfDhPriKeyParamsSpec *paramsSpec) +{ + if (!IsDhCommParamsSpecValid(&(paramsSpec->base))) { + return false; + } + if ((paramsSpec->sk.data == NULL) || (paramsSpec->sk.len == 0)) { + LOGE("BigInteger sk is invalid"); + return false; + } + return true; +} + +static bool IsDhPubKeySpecValid(HcfDhPubKeyParamsSpec *paramsSpec) +{ + if (!IsDhCommParamsSpecValid(&(paramsSpec->base))) { + return false; + } + if ((paramsSpec->pk.data == NULL) || (paramsSpec->pk.len == 0)) { + LOGE("BigInteger pk is invalid"); + return false; + } + return true; +} + +static bool IsDhKeyPairSpecValid(HcfDhKeyPairParamsSpec *paramsSpec) +{ + if (!IsDhCommParamsSpecValid(&(paramsSpec->base))) { + return false; + } + if ((paramsSpec->pk.data == NULL) || (paramsSpec->pk.len == 0)) { + LOGE("BigInteger pk is invalid"); + return false; + } + if ((paramsSpec->sk.data == NULL) || (paramsSpec->sk.len == 0)) { + LOGE("BigInteger sk is invalid"); + return false; + } + return true; +} + +static bool IsDhParamsSpecValid(const HcfAsyKeyParamsSpec *paramsSpec) +{ + bool ret = false; + switch (paramsSpec->specType) { + case HCF_COMMON_PARAMS_SPEC: + ret = IsDhCommParamsSpecValid((HcfDhCommParamsSpec *)paramsSpec); + break; + case HCF_PRIVATE_KEY_SPEC: + ret = IsDhPriKeySpecValid((HcfDhPriKeyParamsSpec *)paramsSpec); + break; + case HCF_PUBLIC_KEY_SPEC: + ret = IsDhPubKeySpecValid((HcfDhPubKeyParamsSpec *)paramsSpec); + break; + case HCF_KEY_PAIR_SPEC: + ret = IsDhKeyPairSpecValid((HcfDhKeyPairParamsSpec *)paramsSpec); + break; + default: + LOGE("SpecType not support! [SpecType]: %d", paramsSpec->specType); + break; + } + return ret; +} + static bool IsEccCommParamsSpecValid(HcfEccCommParamsSpec *paramsSpec) { if ((paramsSpec->a.data == NULL) || (paramsSpec->a.len == 0)) { @@ -273,6 +372,57 @@ static bool IsEccParamsSpecValid(const HcfAsyKeyParamsSpec *paramsSpec) return ret; } +static bool IsAlg25519PriKeySpecValid(HcfAlg25519PriKeyParamsSpec *paramsSpec) +{ + if ((paramsSpec->sk.data == NULL) || (paramsSpec->sk.len == 0)) { + LOGE("Uint8Array sk is invalid"); + return false; + } + return true; +} + +static bool IsAlg25519PubKeySpecValid(HcfAlg25519PubKeyParamsSpec *paramsSpec) +{ + if ((paramsSpec->pk.data == NULL) || (paramsSpec->pk.len == 0)) { + LOGE("Uint8Array pk is invalid"); + return false; + } + return true; +} + +static bool IsAlg25519KeyPairSpecValid(HcfAlg25519KeyPairParamsSpec *paramsSpec) +{ + if ((paramsSpec->pk.data == NULL) || (paramsSpec->pk.len == 0)) { + LOGE("Uint8Array pk is invalid"); + return false; + } + if ((paramsSpec->sk.data == NULL) || (paramsSpec->sk.len == 0)) { + LOGE("Uint8Array sk is invalid"); + return false; + } + return true; +} + +static bool IsAlg25519ParamsSpecValid(const HcfAsyKeyParamsSpec *paramsSpec) +{ + bool ret = false; + switch (paramsSpec->specType) { + case HCF_PRIVATE_KEY_SPEC: + ret = IsAlg25519PriKeySpecValid((HcfAlg25519PriKeyParamsSpec *)paramsSpec); + break; + case HCF_PUBLIC_KEY_SPEC: + ret = IsAlg25519PubKeySpecValid((HcfAlg25519PubKeyParamsSpec *)paramsSpec); + break; + case HCF_KEY_PAIR_SPEC: + ret = IsAlg25519KeyPairSpecValid((HcfAlg25519KeyPairParamsSpec *)paramsSpec); + break; + default: + LOGE("SpecType not support! [SpecType]: %d", paramsSpec->specType); + break; + } + return ret; +} + static bool IsRsaCommParamsSpecValid(HcfRsaCommParamsSpec *paramsSpec) { if ((paramsSpec->n.data == NULL) || (paramsSpec->n.len == 0)) { @@ -342,6 +492,11 @@ static bool IsParamsSpecValid(const HcfAsyKeyParamsSpec *paramsSpec) return IsEccParamsSpecValid(paramsSpec); } else if (strcmp(paramsSpec->algName, ALG_NAME_RSA) == 0) { return IsRsaParamsSpecValid(paramsSpec); + } else if (strcmp(paramsSpec->algName, ALG_NAME_X25519) == 0 || + strcmp(paramsSpec->algName, ALG_NAME_ED25519) == 0) { + return IsAlg25519ParamsSpecValid(paramsSpec); + } else if (strcmp(paramsSpec->algName, ALG_NAME_DH) == 0) { + return IsDhParamsSpecValid(paramsSpec); } else { LOGE("AlgName not support! [AlgName]: %s", paramsSpec->algName); return false; @@ -553,6 +708,119 @@ static HcfResult CreateDsaParamsSpecImpl(const HcfAsyKeyParamsSpec *paramsSpec, return ret; } +static HcfResult CreateDhPubKeySpecImpl(const HcfDhPubKeyParamsSpec *srcSpec, HcfDhPubKeyParamsSpec **destSpec) +{ + HcfDhPubKeyParamsSpec *spec = (HcfDhPubKeyParamsSpec *)HcfMalloc(sizeof(HcfDhPubKeyParamsSpec), 0); + if (spec == NULL) { + LOGE("Failed to allocate dest spec memory"); + return HCF_ERR_MALLOC; + } + if (CopyDhCommonSpec(&(srcSpec->base), &(spec->base)) != HCF_SUCCESS) { + HcfFree(spec); + return HCF_INVALID_PARAMS; + } + spec->pk.data = (unsigned char *)HcfMalloc(srcSpec->pk.len, 0); + if (spec->pk.data == NULL) { + LOGE("Failed to allocate public key memory"); + FreeDhCommParamsSpec(&(spec->base)); + DestroyDhPubKeySpec(spec); + return HCF_ERR_MALLOC; + } + (void)memcpy_s(spec->pk.data, srcSpec->pk.len, srcSpec->pk.data, srcSpec->pk.len); + spec->pk.len = srcSpec->pk.len; + + *destSpec = spec; + return HCF_SUCCESS; +} + +static HcfResult CreateDhPriKeySpecImpl(const HcfDhPriKeyParamsSpec *srcSpec, HcfDhPriKeyParamsSpec **destSpec) +{ + HcfDhPriKeyParamsSpec *spec = (HcfDhPriKeyParamsSpec *)HcfMalloc(sizeof(HcfDhPriKeyParamsSpec), 0); + if (spec == NULL) { + LOGE("Failed to allocate dest spec memory"); + return HCF_ERR_MALLOC; + } + if (CopyDhCommonSpec(&(srcSpec->base), &(spec->base)) != HCF_SUCCESS) { + HcfFree(spec); + return HCF_INVALID_PARAMS; + } + spec->sk.data = (unsigned char *)HcfMalloc(srcSpec->sk.len, 0); + if (spec->sk.data == NULL) { + LOGE("Failed to allocate private key memory"); + FreeDhCommParamsSpec(&(spec->base)); + HcfFree(spec); + return HCF_ERR_MALLOC; + } + (void)memcpy_s(spec->sk.data, srcSpec->sk.len, srcSpec->sk.data, srcSpec->sk.len); + spec->sk.len = srcSpec->sk.len; + + *destSpec = spec; + return HCF_SUCCESS; +} + +static HcfResult CreateDhKeyPairSpecImpl(const HcfDhKeyPairParamsSpec *srcSpec, HcfDhKeyPairParamsSpec **destSpec) +{ + HcfDhKeyPairParamsSpec *spec = (HcfDhKeyPairParamsSpec *)HcfMalloc(sizeof(HcfDhKeyPairParamsSpec), 0); + if (spec == NULL) { + LOGE("Failed to allocate dest spec memory"); + return HCF_ERR_MALLOC; + } + if (CopyDhCommonSpec(&(srcSpec->base), &(spec->base)) != HCF_SUCCESS) { + HcfFree(spec); + return HCF_INVALID_PARAMS; + } + spec->pk.data = (unsigned char *)HcfMalloc(srcSpec->pk.len, 0); + if (spec->pk.data == NULL) { + LOGE("Failed to allocate public key memory"); + FreeDhCommParamsSpec(&(spec->base)); + HcfFree(spec); + return HCF_ERR_MALLOC; + } + spec->sk.data = (unsigned char *)HcfMalloc(srcSpec->sk.len, 0); + if (spec->sk.data == NULL) { + LOGE("Failed to allocate private key memory"); + FreeDhCommParamsSpec(&(spec->base)); + HcfFree(spec->pk.data); + HcfFree(spec); + return HCF_ERR_MALLOC; + } + (void)memcpy_s(spec->pk.data, srcSpec->pk.len, srcSpec->pk.data, srcSpec->pk.len); + (void)memcpy_s(spec->sk.data, srcSpec->sk.len, srcSpec->sk.data, srcSpec->sk.len); + spec->pk.len = srcSpec->pk.len; + spec->sk.len = srcSpec->sk.len; + + *destSpec = spec; + return HCF_SUCCESS; +} + +static HcfResult CreateDhParamsSpecImpl(const HcfAsyKeyParamsSpec *paramsSpec, HcfAsyKeyParamsSpec **impl) +{ + HcfResult ret = HCF_SUCCESS; + HcfDhCommParamsSpec *spec = NULL; + switch (paramsSpec->specType) { + case HCF_COMMON_PARAMS_SPEC: + ret = CreateDhCommonSpecImpl((HcfDhCommParamsSpec *)paramsSpec, &spec); + break; + case HCF_PUBLIC_KEY_SPEC: + ret = CreateDhPubKeySpecImpl((HcfDhPubKeyParamsSpec *)paramsSpec, (HcfDhPubKeyParamsSpec **)&spec); + break; + case HCF_PRIVATE_KEY_SPEC: + ret = CreateDhPriKeySpecImpl((HcfDhPriKeyParamsSpec *)paramsSpec, (HcfDhPriKeyParamsSpec **)&spec); + break; + case HCF_KEY_PAIR_SPEC: + ret = CreateDhKeyPairSpecImpl((HcfDhKeyPairParamsSpec *)paramsSpec, (HcfDhKeyPairParamsSpec **)&spec); + break; + default: + LOGE("Invalid spec type [%d]", paramsSpec->specType); + ret = HCF_INVALID_PARAMS; + break; + } + if (ret == HCF_SUCCESS) { + *impl = (HcfAsyKeyParamsSpec *)spec; + } + return ret; +} + static HcfResult CreateEccPubKeySpecImpl(const HcfEccPubKeyParamsSpec *srcSpec, HcfEccPubKeyParamsSpec **destSpec) { HcfEccPubKeyParamsSpec *tmpSpec = (HcfEccPubKeyParamsSpec *)HcfMalloc(sizeof(HcfEccPubKeyParamsSpec), 0); @@ -763,6 +1031,129 @@ static HcfResult CreateRsaParamsSpecImpl(const HcfAsyKeyParamsSpec *paramsSpec, return ret; } +static HcfResult CreateAlg25519PubKeySpecImpl(const HcfAlg25519PubKeyParamsSpec *srcSpec, + HcfAlg25519PubKeyParamsSpec **destSpec) +{ + HcfAlg25519PubKeyParamsSpec *tmpSpec = + (HcfAlg25519PubKeyParamsSpec *)HcfMalloc(sizeof(HcfAlg25519PubKeyParamsSpec), 0); + if (tmpSpec == NULL) { + LOGE("Failed to allocate dest spec memory"); + return HCF_ERR_MALLOC; + } + if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(tmpSpec->base)) != HCF_SUCCESS) { + DestroyAlg25519PubKeySpec(tmpSpec); + LOGE("Copy alg25519 commonSpec memory"); + return HCF_INVALID_PARAMS; + } + tmpSpec->pk.data = (unsigned char *)HcfMalloc(srcSpec->pk.len, 0); + if (tmpSpec->pk.data == NULL) { + LOGE("Failed to allocate private key memory"); + DestroyAlg25519PubKeySpec(tmpSpec); + return HCF_ERR_MALLOC; + } + (void)memcpy_s(tmpSpec->pk.data, srcSpec->pk.len, srcSpec->pk.data, srcSpec->pk.len); + tmpSpec->pk.len = srcSpec->pk.len; + + *destSpec = tmpSpec; + return HCF_SUCCESS; +} + +static HcfResult CreateAlg25519PriKeySpecImpl(const HcfAlg25519PriKeyParamsSpec *srcSpec, + HcfAlg25519PriKeyParamsSpec **destSpec) +{ + HcfAlg25519PriKeyParamsSpec *tmpSpec = + (HcfAlg25519PriKeyParamsSpec *)HcfMalloc(sizeof(HcfAlg25519PriKeyParamsSpec), 0); + if (tmpSpec == NULL) { + LOGE("Failed to allocate dest spec memory"); + return HCF_ERR_MALLOC; + } + if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(tmpSpec->base)) != HCF_SUCCESS) { + DestroyAlg25519PriKeySpec(tmpSpec); + LOGE("Copy alg25519 commonSpec memory"); + return HCF_INVALID_PARAMS; + } + tmpSpec->sk.data = (unsigned char *)HcfMalloc(srcSpec->sk.len, 0); + if (tmpSpec->sk.data == NULL) { + LOGE("Failed to allocate private key memory"); + DestroyAlg25519PriKeySpec(tmpSpec); + return HCF_ERR_MALLOC; + } + (void)memcpy_s(tmpSpec->sk.data, srcSpec->sk.len, srcSpec->sk.data, srcSpec->sk.len); + tmpSpec->sk.len = srcSpec->sk.len; + + *destSpec = tmpSpec; + return HCF_SUCCESS; +} + +static HcfResult CreateAlg25519KeyPairSpecImpl(const HcfAlg25519KeyPairParamsSpec *srcSpec, + HcfAlg25519KeyPairParamsSpec **destSpec) +{ + HcfAlg25519KeyPairParamsSpec *tmpSpec = + (HcfAlg25519KeyPairParamsSpec *)HcfMalloc(sizeof(HcfAlg25519KeyPairParamsSpec), 0); + if (tmpSpec == NULL) { + LOGE("Failed to allocate dest spec memory"); + return HCF_ERR_MALLOC; + } + if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(tmpSpec->base)) != HCF_SUCCESS) { + DestroyAlg25519KeyPairSpec(tmpSpec); + LOGE("Copy alg25519 commonSpec memory"); + return HCF_INVALID_PARAMS; + } + tmpSpec->pk.data = (unsigned char *)HcfMalloc(srcSpec->pk.len, 0); + if (tmpSpec->pk.data == NULL) { + LOGE("Failed to allocate private key memory"); + DestroyAlg25519KeyPairSpec(tmpSpec); + return HCF_ERR_MALLOC; + } + + tmpSpec->sk.data = (unsigned char *)HcfMalloc(srcSpec->sk.len, 0); + if (tmpSpec->sk.data == NULL) { + LOGE("Failed to allocate private key memory"); + DestroyAlg25519KeyPairSpec(tmpSpec); + return HCF_ERR_MALLOC; + } + (void)memcpy_s(tmpSpec->pk.data, srcSpec->pk.len, srcSpec->pk.data, srcSpec->pk.len); + tmpSpec->pk.len = srcSpec->pk.len; + (void)memcpy_s(tmpSpec->sk.data, srcSpec->sk.len, srcSpec->sk.data, srcSpec->sk.len); + tmpSpec->sk.len = srcSpec->sk.len; + + *destSpec = tmpSpec; + return HCF_SUCCESS; +} + +static HcfResult CreateAlg25519ParamsSpecImpl(const HcfAsyKeyParamsSpec *paramsSpec, HcfAsyKeyParamsSpec **impl) +{ + HcfResult ret = HCF_SUCCESS; + HcfAlg25519PubKeyParamsSpec *pubKeySpec = NULL; + HcfAlg25519PriKeyParamsSpec *priKeySpec = NULL; + HcfAlg25519KeyPairParamsSpec *keyPairSpec = NULL; + switch (paramsSpec->specType) { + case HCF_PUBLIC_KEY_SPEC: + ret = CreateAlg25519PubKeySpecImpl((HcfAlg25519PubKeyParamsSpec *)paramsSpec, &pubKeySpec); + if (ret == HCF_SUCCESS) { + *impl = (HcfAsyKeyParamsSpec *)pubKeySpec; + } + break; + case HCF_PRIVATE_KEY_SPEC: + ret = CreateAlg25519PriKeySpecImpl((HcfAlg25519PriKeyParamsSpec *)paramsSpec, &priKeySpec); + if (ret == HCF_SUCCESS) { + *impl = (HcfAsyKeyParamsSpec *)priKeySpec; + } + break; + case HCF_KEY_PAIR_SPEC: + ret = CreateAlg25519KeyPairSpecImpl((HcfAlg25519KeyPairParamsSpec *)paramsSpec, &keyPairSpec); + if (ret == HCF_SUCCESS) { + *impl = (HcfAsyKeyParamsSpec *)keyPairSpec; + } + break; + default: + LOGE("SpecType not support! [SpecType]: %d", paramsSpec->specType); + ret = HCF_INVALID_PARAMS; + break; + } + return ret; +} + static HcfResult CreateAsyKeyParamsSpecImpl(const HcfAsyKeyParamsSpec *paramsSpec, HcfAlgValue alg, HcfAsyKeyParamsSpec **impl) { @@ -778,6 +1169,13 @@ static HcfResult CreateAsyKeyParamsSpecImpl(const HcfAsyKeyParamsSpec *paramsSpe case HCF_ALG_RSA: ret = CreateRsaParamsSpecImpl(paramsSpec, impl); break; + case HCF_ALG_ED25519: + case HCF_ALG_X25519: + ret = CreateAlg25519ParamsSpecImpl(paramsSpec, impl); + break; + case HCF_ALG_DH: + ret = CreateDhParamsSpecImpl(paramsSpec, impl); + break; default: ret = HCF_INVALID_PARAMS; break; diff --git a/frameworks/key/dh_key_util.c b/frameworks/key/dh_key_util.c new file mode 100644 index 0000000..898e72c --- /dev/null +++ b/frameworks/key/dh_key_util.c @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dh_key_util.h" +#include +#include "dh_key_util_spi.h" +#include "config.h" +#include "dh_common_param_spec_generator_openssl.h" +#include "key_utils.h" +#include "params_parser.h" +#include "log.h" +#include "memory.h" +#include "utils.h" + +HcfResult HcfDhKeyUtilCreate(uint32_t pLen, int32_t skLen, HcfDhCommParamsSpec **returnCommonParamSpec) +{ + if ((skLen < 0) || (returnCommonParamSpec == NULL)) { + LOGE("Failed to parser parmas!"); + return HCF_INVALID_PARAMS; + } + + if (skLen > pLen) { + LOGE("skLen is greater than pLen!"); + return HCF_INVALID_PARAMS; + } + + HcfDhCommParamsSpecSpi *spiInstance = NULL; + HcfResult ret = HcfDhCommonParamSpecCreate(pLen, skLen, &spiInstance); + if (ret != HCF_SUCCESS) { + LOGE("Failed to create spi object!"); + return HCF_ERR_MALLOC; + } + ret = CreateDhCommonSpecImpl(&(spiInstance->paramsSpec), returnCommonParamSpec); + if (ret != HCF_SUCCESS) { + LOGE("Failed to create spi object!"); + } + FreeDhCommParamsSpec(&(spiInstance->paramsSpec)); + HcfFree(spiInstance); + return ret; +} \ No newline at end of file diff --git a/frameworks/key/key_utils.c b/frameworks/key/key_utils.c index 024a6b1..4f3e43c 100644 --- a/frameworks/key/key_utils.c +++ b/frameworks/key/key_utils.c @@ -175,4 +175,47 @@ HcfResult CreateEccCommonSpecImpl(const HcfEccCommParamsSpec *srcSpec, HcfEccCom } *destSpec = tmpSpec; return HCF_SUCCESS; -} \ No newline at end of file +} + +HcfResult CopyDhCommonSpec(const HcfDhCommParamsSpec *srcSpec, HcfDhCommParamsSpec *destSpec) +{ + if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(destSpec->base)) != HCF_SUCCESS) { + return HCF_INVALID_PARAMS; + } + destSpec->p.data = (unsigned char *)HcfMalloc(srcSpec->p.len, 0); + if (destSpec->p.data == NULL) { + LOGE("Failed to allocate p data memory"); + FreeDhCommParamsSpec(destSpec); + return HCF_ERR_MALLOC; + } + + destSpec->g.data = (unsigned char *)HcfMalloc(srcSpec->g.len, 0); + if (destSpec->g.data == NULL) { + LOGE("Failed to allocate g data memory"); + FreeDhCommParamsSpec(destSpec); + return HCF_ERR_MALLOC; + } + destSpec->length = srcSpec->length; + (void)memcpy_s(destSpec->p.data, srcSpec->p.len, srcSpec->p.data, srcSpec->p.len); + (void)memcpy_s(destSpec->g.data, srcSpec->g.len, srcSpec->g.data, srcSpec->g.len); + destSpec->p.len = srcSpec->p.len; + destSpec->g.len = srcSpec->g.len; + return HCF_SUCCESS; +} + +HcfResult CreateDhCommonSpecImpl(const HcfDhCommParamsSpec *srcSpec, HcfDhCommParamsSpec **destSpec) +{ + HcfDhCommParamsSpec *spec = (HcfDhCommParamsSpec *)HcfMalloc(sizeof(HcfDhCommParamsSpec), 0); + if (spec == NULL) { + LOGE("Failed to allocate dest spec memory"); + return HCF_ERR_MALLOC; + } + + if (CopyDhCommonSpec(srcSpec, spec) != HCF_SUCCESS) { + HcfFree(spec); + return HCF_INVALID_PARAMS; + } + + *destSpec = spec; + return HCF_SUCCESS; +} diff --git a/frameworks/spi/dh_key_util_spi.h b/frameworks/spi/dh_key_util_spi.h new file mode 100644 index 0000000..4fa3784 --- /dev/null +++ b/frameworks/spi/dh_key_util_spi.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_DH_KEY_UTIL_SPI_H +#define HCF_DH_KEY_UTIL_SPI_H + +#include +#include "result.h" +#include "detailed_dh_key_params.h" + +typedef struct HcfDhCommParamsSpecSpi HcfDhCommParamsSpecSpi; + +struct HcfDhCommParamsSpecSpi { + HcfDhCommParamsSpec paramsSpec; +}; + +#endif diff --git a/interfaces/innerkits/algorithm_parameter/detailed_alg_25519_key_params.h b/interfaces/innerkits/algorithm_parameter/detailed_alg_25519_key_params.h new file mode 100644 index 0000000..ddaeb02 --- /dev/null +++ b/interfaces/innerkits/algorithm_parameter/detailed_alg_25519_key_params.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_DETAILED_ALG_25519_KEY_PARAMS_H +#define HCF_DETAILED_ALG_25519_KEY_PARAMS_H + +#include +#include + +#include "asy_key_params.h" +#include "big_integer.h" +#include "blob.h" + +typedef struct HcfAlg25519PubKeyParamsSpec { + HcfAsyKeyParamsSpec base; + HcfBigInteger pk; +} HcfAlg25519PubKeyParamsSpec; + +typedef struct HcfAlg25519PriKeyParamsSpec { + HcfAsyKeyParamsSpec base; + HcfBigInteger sk; +} HcfAlg25519PriKeyParamsSpec; + +typedef struct HcfAlg25519KeyPairParamsSpec { + HcfAsyKeyParamsSpec base; + HcfBigInteger sk; + HcfBigInteger pk; +} HcfAlg25519KeyPairParamsSpec; +#ifdef __cplusplus +extern "C" { +#endif + +void DestroyAlg25519PriKeySpec(HcfAlg25519PriKeyParamsSpec *spec); + +void DestroyAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec); + +void DestroyAlg25519KeyPairSpec(HcfAlg25519KeyPairParamsSpec *spec); + +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/interfaces/innerkits/algorithm_parameter/detailed_dh_key_params.h b/interfaces/innerkits/algorithm_parameter/detailed_dh_key_params.h new file mode 100644 index 0000000..8ad0156 --- /dev/null +++ b/interfaces/innerkits/algorithm_parameter/detailed_dh_key_params.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_DETAILED_DH_KEY_PARAMS_H +#define HCF_DETAILED_DH_KEY_PARAMS_H + +#include +#include + +#include "asy_key_params.h" +#include "big_integer.h" + +typedef struct HcfDhCommParamsSpec { + HcfAsyKeyParamsSpec base; + HcfBigInteger p; + HcfBigInteger g; + int length; +} HcfDhCommParamsSpec; + +typedef struct HcfDhPubKeyParamsSpec { + HcfDhCommParamsSpec base; + HcfBigInteger pk; +} HcfDhPubKeyParamsSpec; + +typedef struct HcfDhPriKeyParamsSpec { + HcfDhCommParamsSpec base; + HcfBigInteger sk; +} HcfDhPriKeyParamsSpec; + +typedef struct HcfDhKeyPairParamsSpec { + HcfDhCommParamsSpec base; + HcfBigInteger sk; + HcfBigInteger pk; +} HcfDhKeyPairParamsSpec; +#ifdef __cplusplus +extern "C" { +#endif + +void FreeDhCommParamsSpec(HcfDhCommParamsSpec *spec); + +void DestroyDhPubKeySpec(HcfDhPubKeyParamsSpec *spec); + +void DestroyDhPriKeySpec(HcfDhPriKeyParamsSpec *spec); + +void DestroyDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec); + + +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/interfaces/innerkits/key/asy_key_generator.h b/interfaces/innerkits/key/asy_key_generator.h index 198ebcd..3e35f91 100644 --- a/interfaces/innerkits/key/asy_key_generator.h +++ b/interfaces/innerkits/key/asy_key_generator.h @@ -32,6 +32,20 @@ enum HcfRsaKeySize { HCF_RSA_KEY_SIZE_8192 = 8192, }; +enum HcfDhNamedGroupId { + HCF_DH_MODP_SIZE_1536 = 0, + HCF_DH_MODP_SIZE_2048, + HCF_DH_MODP_SIZE_3072, + HCF_DH_MODP_SIZE_4096, + HCF_DH_MODP_SIZE_6144, + HCF_DH_MODP_SIZE_8192, + HCF_DH_FFDHE_SIZE_2048, + HCF_DH_FFDHE_SIZE_3072, + HCF_DH_FFDHE_SIZE_4096, + HCF_DH_FFDHE_SIZE_6144, + HCF_DH_FFDHE_SIZE_8192, +}; + enum HcfDsaKeySize { HCF_DSA_KEY_SIZE_1024 = 1024, HCF_DSA_KEY_SIZE_2048 = 2048, diff --git a/interfaces/innerkits/key/dh_key_util.h b/interfaces/innerkits/key/dh_key_util.h new file mode 100644 index 0000000..573cc30 --- /dev/null +++ b/interfaces/innerkits/key/dh_key_util.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_DH_KEY_UTIL_H +#define HCF_DH_KEY_UTIL_H + +#include +#include "result.h" +#include "detailed_dh_key_params.h" + +#ifdef __cplusplus +extern "C" { +#endif + +HcfResult HcfDhKeyUtilCreate(uint32_t pLen, int32_t skLen, HcfDhCommParamsSpec **returnCommonParamSpec); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/interfaces/innerkits/key/key.h b/interfaces/innerkits/key/key.h index 7e08c3a..548e8c6 100644 --- a/interfaces/innerkits/key/key.h +++ b/interfaces/innerkits/key/key.h @@ -44,6 +44,17 @@ typedef enum { RSA_N_BN = 301, RSA_SK_BN = 302, RSA_PK_BN = 303, + + DH_P_BN = 401, + DH_G_BN = 402, + DH_L_NUM = 403, + DH_SK_BN = 404, + DH_PK_BN = 405, + + ED25519_SK_BN = 501, + ED25519_PK_BN = 502, + X25519_SK_BN = 601, + X25519_PK_BN = 602, } AsyKeySpecItem; typedef struct HcfKey HcfKey; diff --git a/interfaces/innerkits/key/key_utils.h b/interfaces/innerkits/key/key_utils.h index f327329..0e8f0eb 100644 --- a/interfaces/innerkits/key/key_utils.h +++ b/interfaces/innerkits/key/key_utils.h @@ -18,6 +18,7 @@ #include #include "result.h" +#include "detailed_dh_key_params.h" #include "detailed_ecc_key_params.h" #ifdef __cplusplus @@ -34,6 +35,10 @@ HcfResult CopyEccCommonSpec(const HcfEccCommParamsSpec *srcSpec, HcfEccCommParam HcfResult CreateEccCommonSpecImpl(const HcfEccCommParamsSpec *srcSpec, HcfEccCommParamsSpec **destSpec); +HcfResult CopyDhCommonSpec(const HcfDhCommParamsSpec *srcSpec, HcfDhCommParamsSpec *destSpec); + +HcfResult CreateDhCommonSpecImpl(const HcfDhCommParamsSpec *srcSpec, HcfDhCommParamsSpec **destSpec); + #ifdef __cplusplus } #endif diff --git a/plugin/openssl_plugin/common/inc/dh_openssl_common.h b/plugin/openssl_plugin/common/inc/dh_openssl_common.h new file mode 100644 index 0000000..3839440 --- /dev/null +++ b/plugin/openssl_plugin/common/inc/dh_openssl_common.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_DH_OPENSSL_COMMON_H +#define HCF_DH_OPENSSL_COMMON_H + +#include +#include + +#include "result.h" +#include "utils.h" + +EVP_PKEY *NewEvpPkeyByDh(DH *dh, bool withDuplicate); +char *GetNidNameByDhId(int32_t pLen); +char *GetNidNameByDhPLen(int32_t pLen); + +#endif diff --git a/plugin/openssl_plugin/common/inc/ecc_openssl_common.h b/plugin/openssl_plugin/common/inc/ecc_openssl_common.h index 964508c..a9a73d8 100644 --- a/plugin/openssl_plugin/common/inc/ecc_openssl_common.h +++ b/plugin/openssl_plugin/common/inc/ecc_openssl_common.h @@ -16,538 +16,27 @@ #ifndef HCF_ECC_OPENSSL_COMMON_H #define HCF_ECC_OPENSSL_COMMON_H -#include "utils.h" - -static const int32_t NID_secp224r1_len = 28; -static const int32_t NID_X9_62_prime256v1_len = 32; -static const int32_t NID_secp384r1_len = 48; -static const int32_t NID_secp521r1_len = 66; -static const int32_t NID_brainpoolP160r1_len = 20; -static const int32_t NID_brainpoolP160t1_len = 20; -static const int32_t NID_brainpoolP192r1_len = 24; -static const int32_t NID_brainpoolP192t1_len = 24; -static const int32_t NID_brainpoolP224r1_len = 28; -static const int32_t NID_brainpoolP224t1_len = 28; -static const int32_t NID_brainpoolP256r1_len = 32; -static const int32_t NID_brainpoolP256t1_len = 32; -static const int32_t NID_brainpoolP320r1_len = 40; -static const int32_t NID_brainpoolP320t1_len = 40; -static const int32_t NID_brainpoolP384r1_len = 48; -static const int32_t NID_brainpoolP384t1_len = 48; -static const int32_t NID_brainpoolP512r1_len = 64; -static const int32_t NID_brainpoolP512t1_len = 64; - -static unsigned char g_ecc224CorrectBigP[] = { - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x01 -}; - -static unsigned char g_ecc224CorrectBigB[] = { - 0xB4, 0x05, 0x0A, 0x85, 0x0C, 0x04, 0xB3, 0xAB, 0xF5, 0x41, 0x32, 0x56, - 0x50, 0x44, 0xB0, 0xB7, 0xD7, 0xBF, 0xD8, 0xBA, 0x27, 0x0B, 0x39, 0x43, - 0x23, 0x55, 0xFF, 0xB4 -}; - -static unsigned char g_ecc224CorrectBigGX[] = { - 0xB7, 0x0E, 0x0C, 0xBD, 0x6B, 0xB4, 0xBF, 0x7F, 0x32, 0x13, 0x90, 0xB9, - 0x4A, 0x03, 0xC1, 0xD3, 0x56, 0xC2, 0x11, 0x22, 0x34, 0x32, 0x80, 0xD6, - 0x11, 0x5C, 0x1D, 0x21 -}; - -static unsigned char g_ecc224CorrectBigGY[] = { - 0xbd, 0x37, 0x63, 0x88, 0xb5, 0xf7, 0x23, 0xfb, 0x4c, 0x22, 0xdf, 0xe6, - 0xcd, 0x43, 0x75, 0xa0, 0x5a, 0x07, 0x47, 0x64, 0x44, 0xd5, 0x81, 0x99, - 0x85, 0x00, 0x7e, 0x34 -}; - -// ECC256 -static unsigned char g_ecc256CorrectBigP[] = { - 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF -}; - -static unsigned char g_ecc256CorrectBigB[] = { - 0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7, 0xB3, 0xEB, 0xBD, 0x55, - 0x76, 0x98, 0x86, 0xBC, 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6, - 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B -}; - -static unsigned char g_ecc256CorrectBigGX[] = { - 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8, 0xBC, 0xE6, 0xE5, - 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0, - 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96 -}; - -static unsigned char g_ecc256CorrectBigGY[] = { - 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B, 0x8E, 0xE7, 0xEB, 0x4A, - 0x7C, 0x0F, 0x9E, 0x16, 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE, - 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5 -}; - -// ECC384 -static unsigned char g_ecc384CorrectBigP[] = { - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF -}; - -static unsigned char g_ecc384CorrectBigB[] = { - 0xB3, 0x31, 0x2F, 0xA7, 0xE2, 0x3E, 0xE7, 0xE4, 0x98, 0x8E, 0x05, 0x6B, - 0xE3, 0xF8, 0x2D, 0x19, 0x18, 0x1D, 0x9C, 0x6E, 0xFE, 0x81, 0x41, 0x12, - 0x03, 0x14, 0x08, 0x8F, 0x50, 0x13, 0x87, 0x5A, 0xC6, 0x56, 0x39, 0x8D, - 0x8A, 0x2E, 0xD1, 0x9D, 0x2A, 0x85, 0xC8, 0xED, 0xD3, 0xEC, 0x2A, 0xEF -}; - -static unsigned char g_ecc384CorrectBigGX[] = { - 0xAA, 0x87, 0xCA, 0x22, 0xBE, 0x8B, 0x05, 0x37, 0x8E, 0xB1, 0xC7, 0x1E, - 0xF3, 0x20, 0xAD, 0x74, 0x6E, 0x1D, 0x3B, 0x62, 0x8B, 0xA7, 0x9B, 0x98, - 0x59, 0xF7, 0x41, 0xE0, 0x82, 0x54, 0x2A, 0x38, 0x55, 0x02, 0xF2, 0x5D, - 0xBF, 0x55, 0x29, 0x6C, 0x3A, 0x54, 0x5E, 0x38, 0x72, 0x76, 0x0A, 0xB7 -}; - -static unsigned char g_ecc384CorrectBigGY[] = { - 0x36, 0x17, 0xDE, 0x4A, 0x96, 0x26, 0x2C, 0x6F, 0x5D, 0x9E, 0x98, 0xBF, - 0x92, 0x92, 0xDC, 0x29, 0xF8, 0xF4, 0x1D, 0xBD, 0x28, 0x9A, 0x14, 0x7C, - 0xE9, 0xDA, 0x31, 0x13, 0xB5, 0xF0, 0xB8, 0xC0, 0x0A, 0x60, 0xB1, 0xCE, - 0x1D, 0x7E, 0x81, 0x9D, 0x7A, 0x43, 0x1D, 0x7C, 0x90, 0xEA, 0x0E, 0x5F, -}; - -// ECC521 -static unsigned char g_ecc521CorrectBigP[] = { - 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF -}; - -static unsigned char g_ecc521CorrectBigB[] = { - 0x00, 0x51, 0x95, 0x3E, 0xB9, 0x61, 0x8E, 0x1C, 0x9A, 0x1F, 0x92, 0x9A, - 0x21, 0xA0, 0xB6, 0x85, 0x40, 0xEE, 0xA2, 0xDA, 0x72, 0x5B, 0x99, 0xB3, - 0x15, 0xF3, 0xB8, 0xB4, 0x89, 0x91, 0x8E, 0xF1, 0x09, 0xE1, 0x56, 0x19, - 0x39, 0x51, 0xEC, 0x7E, 0x93, 0x7B, 0x16, 0x52, 0xC0, 0xBD, 0x3B, 0xB1, - 0xBF, 0x07, 0x35, 0x73, 0xDF, 0x88, 0x3D, 0x2C, 0x34, 0xF1, 0xEF, 0x45, - 0x1F, 0xD4, 0x6B, 0x50, 0x3F, 0x00 -}; - -static unsigned char g_ecc521CorrectBigGX[] = { - 0x00, 0xC6, 0x85, 0x8E, 0x06, 0xB7, 0x04, 0x04, 0xE9, 0xCD, 0x9E, 0x3E, - 0xCB, 0x66, 0x23, 0x95, 0xB4, 0x42, 0x9C, 0x64, 0x81, 0x39, 0x05, 0x3F, - 0xB5, 0x21, 0xF8, 0x28, 0xAF, 0x60, 0x6B, 0x4D, 0x3D, 0xBA, 0xA1, 0x4B, - 0x5E, 0x77, 0xEF, 0xE7, 0x59, 0x28, 0xFE, 0x1D, 0xC1, 0x27, 0xA2, 0xFF, - 0xA8, 0xDE, 0x33, 0x48, 0xB3, 0xC1, 0x85, 0x6A, 0x42, 0x9B, 0xF9, 0x7E, - 0x7E, 0x31, 0xC2, 0xE5, 0xBD, 0x66 -}; - -static unsigned char g_ecc521CorrectBigGY[] = { - 0x01, 0x18, 0x39, 0x29, 0x6A, 0x78, 0x9A, 0x3B, 0xC0, 0x04, 0x5C, 0x8A, - 0x5F, 0xB4, 0x2C, 0x7D, 0x1B, 0xD9, 0x98, 0xF5, 0x44, 0x49, 0x57, 0x9B, - 0x44, 0x68, 0x17, 0xAF, 0xBD, 0x17, 0x27, 0x3E, 0x66, 0x2C, 0x97, 0xEE, - 0x72, 0x99, 0x5E, 0xF4, 0x26, 0x40, 0xC5, 0x50, 0xB9, 0x01, 0x3F, 0xAD, - 0x07, 0x61, 0x35, 0x3C, 0x70, 0x86, 0xA2, 0x72, 0xC2, 0x40, 0x88, 0xBE, - 0x94, 0x76, 0x9F, 0xD1, 0x66, 0x50 -}; - -// SM2_256 -static unsigned char g_sm256CorrectBigP[] = { - 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff -}; - -static unsigned char g_sm256CorrectBigB[] = { - 0x28, 0xe9, 0xfa, 0x9e, 0x9d, 0x9f, 0x5e, 0x34, 0x4d, 0x5a, 0x9e, 0x4b, - 0xcf, 0x65, 0x09, 0xa7, 0xf3, 0x97, 0x89, 0xf5, 0x15, 0xab, 0x8f, 0x92, - 0xdd, 0xbc, 0xbd, 0x41, 0x4d, 0x94, 0x0e, 0x93, -}; - -static unsigned char g_sm256CorrectBigGX[] = { - 0x32, 0xc4, 0xae, 0x2c, 0x1f, 0x19, 0x81, 0x19, 0x5f, 0x99, 0x04, 0x46, - 0x6a, 0x39, 0xc9, 0x94, 0x8f, 0xe3, 0x0b, 0xbf, 0xf2, 0x66, 0x0b, 0xe1, - 0x71, 0x5a, 0x45, 0x89, 0x33, 0x4c, 0x74, 0xc7 -}; - -static unsigned char g_sm256CorrectBigGY[] = { - 0xbc, 0x37, 0x36, 0xa2, 0xf4, 0xf6, 0x77, 0x9c, 0x59, 0xbd, 0xce, 0xe3, - 0x6b, 0x69, 0x21, 0x53, 0xd0, 0xa9, 0x87, 0x7c, 0xc6, 0x2a, 0x47, 0x40, - 0x02, 0xdf, 0x32, 0xe5, 0x21, 0x39, 0xf0, 0xa0 -}; - -// BrainPool160r1 -static unsigned char g_bp160r1CorrectBigP[] = { - 0xE9, 0x5E, 0x4A, 0x5F, 0x73, 0x70, 0x59, 0xDC, 0x60, 0xDF, 0xC7, 0xAD, - 0x95, 0xB3, 0xD8, 0x13, 0x95, 0x15, 0x62, 0x0F -}; - -static unsigned char g_bp160r1CorrectBigB[] = { - 0x1E, 0x58, 0x9A, 0x85, 0x95, 0x42, 0x34, 0x12, 0x13, 0x4F, 0xAA, 0x2D, - 0xBD, 0xEC, 0x95, 0xC8, 0xD8, 0x67, 0x5E, 0x58 -}; - -static unsigned char g_bp160r1CorrectBigGX[] = { - 0xBE, 0xD5, 0xAF, 0x16, 0xEA, 0x3F, 0x6A, 0x4F, 0x62, 0x93, 0x8C, 0x46, - 0x31, 0xEB, 0x5A, 0xF7, 0xBD, 0xBC, 0xDB, 0xC3 -}; - -static unsigned char g_bp160r1CorrectBigGY[] = { - 0x16, 0x67, 0xCB, 0x47, 0x7A, 0x1A, 0x8E, 0xC3, 0x38, 0xF9, 0x47, 0x41, - 0x66, 0x9C, 0x97, 0x63, 0x16, 0xDA, 0x63, 0x21 -}; - -// BrainPool160t1 -static unsigned char g_bp160t1CorrectBigP[] = { - 0xE9, 0x5E, 0x4A, 0x5F, 0x73, 0x70, 0x59, 0xDC, 0x60, 0xDF, 0xC7, 0xAD, - 0x95, 0xB3, 0xD8, 0x13, 0x95, 0x15, 0x62, 0x0F -}; - -static unsigned char g_bp160t1CorrectBigB[] = { - 0x7A, 0x55, 0x6B, 0x6D, 0xAE, 0x53, 0x5B, 0x7B, 0x51, 0xED, 0x2C, 0x4D, - 0x7D, 0xAA, 0x7A, 0x0B, 0x5C, 0x55, 0xF3, 0x80 -}; - -static unsigned char g_bp160t1CorrectBigGX[] = { - 0xB1, 0x99, 0xB1, 0x3B, 0x9B, 0x34, 0xEF, 0xC1, 0x39, 0x7E, 0x64, 0xBA, - 0xEB, 0x05, 0xAC, 0xC2, 0x65, 0xFF, 0x23, 0x78 -}; - -static unsigned char g_bp160t1CorrectBigGY[] = { - 0xAD, 0xD6, 0x71, 0x8B, 0x7C, 0x7C, 0x19, 0x61, 0xF0, 0x99, 0x1B, 0x84, - 0x24, 0x43, 0x77, 0x21, 0x52, 0xC9, 0xE0, 0xAD -}; - -// BrainPool192r1 -static unsigned char g_bp192r1CorrectBigP[] = { - 0xC3, 0x02, 0xF4, 0x1D, 0x93, 0x2A, 0x36, 0xCD, 0xA7, 0xA3, 0x46, 0x30, - 0x93, 0xD1, 0x8D, 0xB7, 0x8F, 0xCE, 0x47, 0x6D, 0xE1, 0xA8, 0x62, 0x97 -}; - -static unsigned char g_bp192r1CorrectBigB[] = { - 0x46, 0x9A, 0x28, 0xEF, 0x7C, 0x28, 0xCC, 0xA3, 0xDC, 0x72, 0x1D, 0x04, - 0x4F, 0x44, 0x96, 0xBC, 0xCA, 0x7E, 0xF4, 0x14, 0x6F, 0xBF, 0x25, 0xC9 -}; - -static unsigned char g_bp192r1CorrectBigGX[] = { - 0xC0, 0xA0, 0x64, 0x7E, 0xAA, 0xB6, 0xA4, 0x87, 0x53, 0xB0, 0x33, 0xC5, - 0x6C, 0xB0, 0xF0, 0x90, 0x0A, 0x2F, 0x5C, 0x48, 0x53, 0x37, 0x5F, 0xD6 -}; - -static unsigned char g_bp192r1CorrectBigGY[] = { - 0x14, 0xB6, 0x90, 0x86, 0x6A, 0xBD, 0x5B, 0xB8, 0x8B, 0x5F, 0x48, 0x28, - 0xC1, 0x49, 0x00, 0x02, 0xE6, 0x77, 0x3F, 0xA2, 0xFA, 0x29, 0x9B, 0x8F -}; - -// BrainPool192t1 -static unsigned char g_bp192t1CorrectBigP[] = { - 0xC3, 0x02, 0xF4, 0x1D, 0x93, 0x2A, 0x36, 0xCD, 0xA7, 0xA3, 0x46, 0x30, - 0x93, 0xD1, 0x8D, 0xB7, 0x8F, 0xCE, 0x47, 0x6D, 0xE1, 0xA8, 0x62, 0x97 -}; - -static unsigned char g_bp192t1CorrectBigB[] = { - 0x13, 0xD5, 0x6F, 0xFA, 0xEC, 0x78, 0x68, 0x1E, 0x68, 0xF9, 0xDE, 0xB4, - 0x3B, 0x35, 0xBE, 0xC2, 0xFB, 0x68, 0x54, 0x2E, 0x27, 0x89, 0x7B, 0x79 -}; - -static unsigned char g_bp192t1CorrectBigGX[] = { - 0x3A, 0xE9, 0xE5, 0x8C, 0x82, 0xF6, 0x3C, 0x30, 0x28, 0x2E, 0x1F, 0xE7, - 0xBB, 0xF4, 0x3F, 0xA7, 0x2C, 0x44, 0x6A, 0xF6, 0xF4, 0x61, 0x81, 0x29 -}; - -static unsigned char g_bp192t1CorrectBigGY[] = { - 0x09, 0x7E, 0x2C, 0x56, 0x67, 0xC2, 0x22, 0x3A, 0x90, 0x2A, 0xB5, 0xCA, - 0x44, 0x9D, 0x00, 0x84, 0xB7, 0xE5, 0xB3, 0xDE, 0x7C, 0xCC, 0x01, 0xC9 -}; - -// BrainPool224r1 -static unsigned char g_bp224r1CorrectBigP[] = { - 0xD7, 0xC1, 0x34, 0xAA, 0x26, 0x43, 0x66, 0x86, 0x2A, 0x18, 0x30, 0x25, - 0x75, 0xD1, 0xD7, 0x87, 0xB0, 0x9F, 0x07, 0x57, 0x97, 0xDA, 0x89, 0xF5, - 0x7E, 0xC8, 0xC0, 0xFF -}; - -static unsigned char g_bp224r1CorrectBigB[] = { - 0x25, 0x80, 0xF6, 0x3C, 0xCF, 0xE4, 0x41, 0x38, 0x87, 0x07, 0x13, 0xB1, - 0xA9, 0x23, 0x69, 0xE3, 0x3E, 0x21, 0x35, 0xD2, 0x66, 0xDB, 0xB3, 0x72, - 0x38, 0x6C, 0x40, 0x0B -}; - -static unsigned char g_bp224r1CorrectBigGX[] = { - 0x0D, 0x90, 0x29, 0xAD, 0x2C, 0x7E, 0x5C, 0xF4, 0x34, 0x08, 0x23, 0xB2, - 0xA8, 0x7D, 0xC6, 0x8C, 0x9E, 0x4C, 0xE3, 0x17, 0x4C, 0x1E, 0x6E, 0xFD, - 0xEE, 0x12, 0xC0, 0x7D -}; - -static unsigned char g_bp224r1CorrectBigGY[] = { - 0x58, 0xAA, 0x56, 0xF7, 0x72, 0xC0, 0x72, 0x6F, 0x24, 0xC6, 0xB8, 0x9E, - 0x4E, 0xCD, 0xAC, 0x24, 0x35, 0x4B, 0x9E, 0x99, 0xCA, 0xA3, 0xF6, 0xD3, - 0x76, 0x14, 0x02, 0xCD -}; - -// BrainPool224t1 -static unsigned char g_bp224t1CorrectBigP[] = { - 0xD7, 0xC1, 0x34, 0xAA, 0x26, 0x43, 0x66, 0x86, 0x2A, 0x18, 0x30, 0x25, - 0x75, 0xD1, 0xD7, 0x87, 0xB0, 0x9F, 0x07, 0x57, 0x97, 0xDA, 0x89, 0xF5, - 0x7E, 0xC8, 0xC0, 0xFF -}; - -static unsigned char g_bp224t1CorrectBigB[] = { - 0x4B, 0x33, 0x7D, 0x93, 0x41, 0x04, 0xCD, 0x7B, 0xEF, 0x27, 0x1B, 0xF6, - 0x0C, 0xED, 0x1E, 0xD2, 0x0D, 0xA1, 0x4C, 0x08, 0xB3, 0xBB, 0x64, 0xF1, - 0x8A, 0x60, 0x88, 0x8D -}; - -static unsigned char g_bp224t1CorrectBigGX[] = { - 0x6A, 0xB1, 0xE3, 0x44, 0xCE, 0x25, 0xFF, 0x38, 0x96, 0x42, 0x4E, 0x7F, - 0xFE, 0x14, 0x76, 0x2E, 0xCB, 0x49, 0xF8, 0x92, 0x8A, 0xC0, 0xC7, 0x60, - 0x29, 0xB4, 0xD5, 0x80 -}; - -static unsigned char g_bp224t1CorrectBigGY[] = { - 0x03, 0x74, 0xE9, 0xF5, 0x14, 0x3E, 0x56, 0x8C, 0xD2, 0x3F, 0x3F, 0x4D, - 0x7C, 0x0D, 0x4B, 0x1E, 0x41, 0xC8, 0xCC, 0x0D, 0x1C, 0x6A, 0xBD, 0x5F, - 0x1A, 0x46, 0xDB, 0x4C -}; - -// BrainPool256r1 -static unsigned char g_bp256r1CorrectBigP[] = { - 0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC, 0x3E, 0x66, 0x0A, 0x90, - 0x9D, 0x83, 0x8D, 0x72, 0x6E, 0x3B, 0xF6, 0x23, 0xD5, 0x26, 0x20, 0x28, - 0x20, 0x13, 0x48, 0x1D, 0x1F, 0x6E, 0x53, 0x77 -}; - -static unsigned char g_bp256r1CorrectBigB[] = { - 0x26, 0xDC, 0x5C, 0x6C, 0xE9, 0x4A, 0x4B, 0x44, 0xF3, 0x30, 0xB5, 0xD9, - 0xBB, 0xD7, 0x7C, 0xBF, 0x95, 0x84, 0x16, 0x29, 0x5C, 0xF7, 0xE1, 0xCE, - 0x6B, 0xCC, 0xDC, 0x18, 0xFF, 0x8C, 0x07, 0xB6 -}; - -static unsigned char g_bp256r1CorrectBigGX[] = { - 0x8B, 0xD2, 0xAE, 0xB9, 0xCB, 0x7E, 0x57, 0xCB, 0x2C, 0x4B, 0x48, 0x2F, - 0xFC, 0x81, 0xB7, 0xAF, 0xB9, 0xDE, 0x27, 0xE1, 0xE3, 0xBD, 0x23, 0xC2, - 0x3A, 0x44, 0x53, 0xBD, 0x9A, 0xCE, 0x32, 0x62 -}; - -static unsigned char g_bp256r1CorrectBigGY[] = { - 0x54, 0x7E, 0xF8, 0x35, 0xC3, 0xDA, 0xC4, 0xFD, 0x97, 0xF8, 0x46, 0x1A, - 0x14, 0x61, 0x1D, 0xC9, 0xC2, 0x77, 0x45, 0x13, 0x2D, 0xED, 0x8E, 0x54, - 0x5C, 0x1D, 0x54, 0xC7, 0x2F, 0x04, 0x69, 0x97 -}; - -// BrainPool256t1 -static unsigned char g_bp256t1CorrectBigP[] = { - 0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC, 0x3E, 0x66, 0x0A, 0x90, - 0x9D, 0x83, 0x8D, 0x72, 0x6E, 0x3B, 0xF6, 0x23, 0xD5, 0x26, 0x20, 0x28, - 0x20, 0x13, 0x48, 0x1D, 0x1F, 0x6E, 0x53, 0x77 -}; - -static unsigned char g_bp256t1CorrectBigB[] = { - 0x66, 0x2C, 0x61, 0xC4, 0x30, 0xD8, 0x4E, 0xA4, 0xFE, 0x66, 0xA7, 0x73, - 0x3D, 0x0B, 0x76, 0xB7, 0xBF, 0x93, 0xEB, 0xC4, 0xAF, 0x2F, 0x49, 0x25, - 0x6A, 0xE5, 0x81, 0x01, 0xFE, 0xE9, 0x2B, 0x04 -}; - -static unsigned char g_bp256t1CorrectBigGX[] = { - 0xA3, 0xE8, 0xEB, 0x3C, 0xC1, 0xCF, 0xE7, 0xB7, 0x73, 0x22, 0x13, 0xB2, - 0x3A, 0x65, 0x61, 0x49, 0xAF, 0xA1, 0x42, 0xC4, 0x7A, 0xAF, 0xBC, 0x2B, - 0x79, 0xA1, 0x91, 0x56, 0x2E, 0x13, 0x05, 0xF4 -}; - -static unsigned char g_bp256t1CorrectBigGY[] = { - 0x2D, 0x99, 0x6C, 0x82, 0x34, 0x39, 0xC5, 0x6D, 0x7F, 0x7B, 0x22, 0xE1, - 0x46, 0x44, 0x41, 0x7E, 0x69, 0xBC, 0xB6, 0xDE, 0x39, 0xD0, 0x27, 0x00, - 0x1D, 0xAB, 0xE8, 0xF3, 0x5B, 0x25, 0xC9, 0xBE -}; - -// BrainPool320r1 -static unsigned char g_bp320r1CorrectBigP[] = { - 0xD3, 0x5E, 0x47, 0x20, 0x36, 0xBC, 0x4F, 0xB7, 0xE1, 0x3C, 0x78, 0x5E, - 0xD2, 0x01, 0xE0, 0x65, 0xF9, 0x8F, 0xCF, 0xA6, 0xF6, 0xF4, 0x0D, 0xEF, - 0x4F, 0x92, 0xB9, 0xEC, 0x78, 0x93, 0xEC, 0x28, 0xFC, 0xD4, 0x12, 0xB1, - 0xF1, 0xB3, 0x2E, 0x27 -}; - -static unsigned char g_bp320r1CorrectBigB[] = { - 0x52, 0x08, 0x83, 0x94, 0x9D, 0xFD, 0xBC, 0x42, 0xD3, 0xAD, 0x19, 0x86, - 0x40, 0x68, 0x8A, 0x6F, 0xE1, 0x3F, 0x41, 0x34, 0x95, 0x54, 0xB4, 0x9A, - 0xCC, 0x31, 0xDC, 0xCD, 0x88, 0x45, 0x39, 0x81, 0x6F, 0x5E, 0xB4, 0xAC, - 0x8F, 0xB1, 0xF1, 0xA6 -}; - -static unsigned char g_bp320r1CorrectBigGX[] = { - 0x43, 0xBD, 0x7E, 0x9A, 0xFB, 0x53, 0xD8, 0xB8, 0x52, 0x89, 0xBC, 0xC4, - 0x8E, 0xE5, 0xBF, 0xE6, 0xF2, 0x01, 0x37, 0xD1, 0x0A, 0x08, 0x7E, 0xB6, - 0xE7, 0x87, 0x1E, 0x2A, 0x10, 0xA5, 0x99, 0xC7, 0x10, 0xAF, 0x8D, 0x0D, - 0x39, 0xE2, 0x06, 0x11 -}; - -static unsigned char g_bp320r1CorrectBigGY[] = { - 0x14, 0xFD, 0xD0, 0x55, 0x45, 0xEC, 0x1C, 0xC8, 0xAB, 0x40, 0x93, 0x24, - 0x7F, 0x77, 0x27, 0x5E, 0x07, 0x43, 0xFF, 0xED, 0x11, 0x71, 0x82, 0xEA, - 0xA9, 0xC7, 0x78, 0x77, 0xAA, 0xAC, 0x6A, 0xC7, 0xD3, 0x52, 0x45, 0xD1, - 0x69, 0x2E, 0x8E, 0xE1 -}; - -// BrainPool320t1 -static unsigned char g_bp320t1CorrectBigP[] = { - 0xD3, 0x5E, 0x47, 0x20, 0x36, 0xBC, 0x4F, 0xB7, 0xE1, 0x3C, 0x78, 0x5E, - 0xD2, 0x01, 0xE0, 0x65, 0xF9, 0x8F, 0xCF, 0xA6, 0xF6, 0xF4, 0x0D, 0xEF, - 0x4F, 0x92, 0xB9, 0xEC, 0x78, 0x93, 0xEC, 0x28, 0xFC, 0xD4, 0x12, 0xB1, - 0xF1, 0xB3, 0x2E, 0x27 -}; - -static unsigned char g_bp320t1CorrectBigB[] = { - 0xA7, 0xF5, 0x61, 0xE0, 0x38, 0xEB, 0x1E, 0xD5, 0x60, 0xB3, 0xD1, 0x47, - 0xDB, 0x78, 0x20, 0x13, 0x06, 0x4C, 0x19, 0xF2, 0x7E, 0xD2, 0x7C, 0x67, - 0x80, 0xAA, 0xF7, 0x7F, 0xB8, 0xA5, 0x47, 0xCE, 0xB5, 0xB4, 0xFE, 0xF4, - 0x22, 0x34, 0x03, 0x53 -}; - -static unsigned char g_bp320t1CorrectBigGX[] = { - 0x92, 0x5B, 0xE9, 0xFB, 0x01, 0xAF, 0xC6, 0xFB, 0x4D, 0x3E, 0x7D, 0x49, - 0x90, 0x01, 0x0F, 0x81, 0x34, 0x08, 0xAB, 0x10, 0x6C, 0x4F, 0x09, 0xCB, - 0x7E, 0xE0, 0x78, 0x68, 0xCC, 0x13, 0x6F, 0xFF, 0x33, 0x57, 0xF6, 0x24, - 0xA2, 0x1B, 0xED, 0x52 -}; - -static unsigned char g_bp320t1CorrectBigGY[] = { - 0x63, 0xBA, 0x3A, 0x7A, 0x27, 0x48, 0x3E, 0xBF, 0x66, 0x71, 0xDB, 0xEF, - 0x7A, 0xBB, 0x30, 0xEB, 0xEE, 0x08, 0x4E, 0x58, 0xA0, 0xB0, 0x77, 0xAD, - 0x42, 0xA5, 0xA0, 0x98, 0x9D, 0x1E, 0xE7, 0x1B, 0x1B, 0x9B, 0xC0, 0x45, - 0x5F, 0xB0, 0xD2, 0xC3 -}; - -// BrainPool384r1 -static unsigned char g_bp384r1CorrectBigP[] = { - 0x8C, 0xB9, 0x1E, 0x82, 0xA3, 0x38, 0x6D, 0x28, 0x0F, 0x5D, 0x6F, 0x7E, - 0x50, 0xE6, 0x41, 0xDF, 0x15, 0x2F, 0x71, 0x09, 0xED, 0x54, 0x56, 0xB4, - 0x12, 0xB1, 0xDA, 0x19, 0x7F, 0xB7, 0x11, 0x23, 0xAC, 0xD3, 0xA7, 0x29, - 0x90, 0x1D, 0x1A, 0x71, 0x87, 0x47, 0x00, 0x13, 0x31, 0x07, 0xEC, 0x53 -}; - -static unsigned char g_bp384r1CorrectBigB[] = { - 0x04, 0xA8, 0xC7, 0xDD, 0x22, 0xCE, 0x28, 0x26, 0x8B, 0x39, 0xB5, 0x54, - 0x16, 0xF0, 0x44, 0x7C, 0x2F, 0xB7, 0x7D, 0xE1, 0x07, 0xDC, 0xD2, 0xA6, - 0x2E, 0x88, 0x0E, 0xA5, 0x3E, 0xEB, 0x62, 0xD5, 0x7C, 0xB4, 0x39, 0x02, - 0x95, 0xDB, 0xC9, 0x94, 0x3A, 0xB7, 0x86, 0x96, 0xFA, 0x50, 0x4C, 0x11 -}; - -static unsigned char g_bp384r1CorrectBigGX[] = { - 0x1D, 0x1C, 0x64, 0xF0, 0x68, 0xCF, 0x45, 0xFF, 0xA2, 0xA6, 0x3A, 0x81, - 0xB7, 0xC1, 0x3F, 0x6B, 0x88, 0x47, 0xA3, 0xE7, 0x7E, 0xF1, 0x4F, 0xE3, - 0xDB, 0x7F, 0xCA, 0xFE, 0x0C, 0xBD, 0x10, 0xE8, 0xE8, 0x26, 0xE0, 0x34, - 0x36, 0xD6, 0x46, 0xAA, 0xEF, 0x87, 0xB2, 0xE2, 0x47, 0xD4, 0xAF, 0x1E -}; - -static unsigned char g_bp384r1CorrectBigGY[] = { - 0x8A, 0xBE, 0x1D, 0x75, 0x20, 0xF9, 0xC2, 0xA4, 0x5C, 0xB1, 0xEB, 0x8E, - 0x95, 0xCF, 0xD5, 0x52, 0x62, 0xB7, 0x0B, 0x29, 0xFE, 0xEC, 0x58, 0x64, - 0xE1, 0x9C, 0x05, 0x4F, 0xF9, 0x91, 0x29, 0x28, 0x0E, 0x46, 0x46, 0x21, - 0x77, 0x91, 0x81, 0x11, 0x42, 0x82, 0x03, 0x41, 0x26, 0x3C, 0x53, 0x15 -}; - -// BrainPool384t1 -static unsigned char g_bp384t1CorrectBigP[] = { - 0x8C, 0xB9, 0x1E, 0x82, 0xA3, 0x38, 0x6D, 0x28, 0x0F, 0x5D, 0x6F, 0x7E, - 0x50, 0xE6, 0x41, 0xDF, 0x15, 0x2F, 0x71, 0x09, 0xED, 0x54, 0x56, 0xB4, - 0x12, 0xB1, 0xDA, 0x19, 0x7F, 0xB7, 0x11, 0x23, 0xAC, 0xD3, 0xA7, 0x29, - 0x90, 0x1D, 0x1A, 0x71, 0x87, 0x47, 0x00, 0x13, 0x31, 0x07, 0xEC, 0x53 -}; - -static unsigned char g_bp384t1CorrectBigB[] = { - 0x7F, 0x51, 0x9E, 0xAD, 0xA7, 0xBD, 0xA8, 0x1B, 0xD8, 0x26, 0xDB, 0xA6, - 0x47, 0x91, 0x0F, 0x8C, 0x4B, 0x93, 0x46, 0xED, 0x8C, 0xCD, 0xC6, 0x4E, - 0x4B, 0x1A, 0xBD, 0x11, 0x75, 0x6D, 0xCE, 0x1D, 0x20, 0x74, 0xAA, 0x26, - 0x3B, 0x88, 0x80, 0x5C, 0xED, 0x70, 0x35, 0x5A, 0x33, 0xB4, 0x71, 0xEE -}; - -static unsigned char g_bp384t1CorrectBigGX[] = { - 0x18, 0xDE, 0x98, 0xB0, 0x2D, 0xB9, 0xA3, 0x06, 0xF2, 0xAF, 0xCD, 0x72, - 0x35, 0xF7, 0x2A, 0x81, 0x9B, 0x80, 0xAB, 0x12, 0xEB, 0xD6, 0x53, 0x17, - 0x24, 0x76, 0xFE, 0xCD, 0x46, 0x2A, 0xAB, 0xFF, 0xC4, 0xFF, 0x19, 0x1B, - 0x94, 0x6A, 0x5F, 0x54, 0xD8, 0xD0, 0xAA, 0x2F, 0x41, 0x88, 0x08, 0xCC -}; - -static unsigned char g_bp384t1CorrectBigGY[] = { - 0x25, 0xAB, 0x05, 0x69, 0x62, 0xD3, 0x06, 0x51, 0xA1, 0x14, 0xAF, 0xD2, - 0x75, 0x5A, 0xD3, 0x36, 0x74, 0x7F, 0x93, 0x47, 0x5B, 0x7A, 0x1F, 0xCA, - 0x3B, 0x88, 0xF2, 0xB6, 0xA2, 0x08, 0xCC, 0xFE, 0x46, 0x94, 0x08, 0x58, - 0x4D, 0xC2, 0xB2, 0x91, 0x26, 0x75, 0xBF, 0x5B, 0x9E, 0x58, 0x29, 0x28 -}; - -// BrainPool512r1 -static unsigned char g_bp512r1CorrectBigP[] = { - 0xAA, 0xDD, 0x9D, 0xB8, 0xDB, 0xE9, 0xC4, 0x8B, 0x3F, 0xD4, 0xE6, 0xAE, - 0x33, 0xC9, 0xFC, 0x07, 0xCB, 0x30, 0x8D, 0xB3, 0xB3, 0xC9, 0xD2, 0x0E, - 0xD6, 0x63, 0x9C, 0xCA, 0x70, 0x33, 0x08, 0x71, 0x7D, 0x4D, 0x9B, 0x00, - 0x9B, 0xC6, 0x68, 0x42, 0xAE, 0xCD, 0xA1, 0x2A, 0xE6, 0xA3, 0x80, 0xE6, - 0x28, 0x81, 0xFF, 0x2F, 0x2D, 0x82, 0xC6, 0x85, 0x28, 0xAA, 0x60, 0x56, - 0x58, 0x3A, 0x48, 0xF3 -}; - -static unsigned char g_bp512r1CorrectBigB[] = { - 0x3D, 0xF9, 0x16, 0x10, 0xA8, 0x34, 0x41, 0xCA, 0xEA, 0x98, 0x63, 0xBC, - 0x2D, 0xED, 0x5D, 0x5A, 0xA8, 0x25, 0x3A, 0xA1, 0x0A, 0x2E, 0xF1, 0xC9, - 0x8B, 0x9A, 0xC8, 0xB5, 0x7F, 0x11, 0x17, 0xA7, 0x2B, 0xF2, 0xC7, 0xB9, - 0xE7, 0xC1, 0xAC, 0x4D, 0x77, 0xFC, 0x94, 0xCA, 0xDC, 0x08, 0x3E, 0x67, - 0x98, 0x40, 0x50, 0xB7, 0x5E, 0xBA, 0xE5, 0xDD, 0x28, 0x09, 0xBD, 0x63, - 0x80, 0x16, 0xF7, 0x23 -}; - -static unsigned char g_bp512r1CorrectBigGX[] = { - 0x81, 0xAE, 0xE4, 0xBD, 0xD8, 0x2E, 0xD9, 0x64, 0x5A, 0x21, 0x32, 0x2E, - 0x9C, 0x4C, 0x6A, 0x93, 0x85, 0xED, 0x9F, 0x70, 0xB5, 0xD9, 0x16, 0xC1, - 0xB4, 0x3B, 0x62, 0xEE, 0xF4, 0xD0, 0x09, 0x8E, 0xFF, 0x3B, 0x1F, 0x78, - 0xE2, 0xD0, 0xD4, 0x8D, 0x50, 0xD1, 0x68, 0x7B, 0x93, 0xB9, 0x7D, 0x5F, - 0x7C, 0x6D, 0x50, 0x47, 0x40, 0x6A, 0x5E, 0x68, 0x8B, 0x35, 0x22, 0x09, - 0xBC, 0xB9, 0xF8, 0x22 -}; - -static unsigned char g_bp512r1CorrectBigGY[] = { - 0x7D, 0xDE, 0x38, 0x5D, 0x56, 0x63, 0x32, 0xEC, 0xC0, 0xEA, 0xBF, 0xA9, - 0xCF, 0x78, 0x22, 0xFD, 0xF2, 0x09, 0xF7, 0x00, 0x24, 0xA5, 0x7B, 0x1A, - 0xA0, 0x00, 0xC5, 0x5B, 0x88, 0x1F, 0x81, 0x11, 0xB2, 0xDC, 0xDE, 0x49, - 0x4A, 0x5F, 0x48, 0x5E, 0x5B, 0xCA, 0x4B, 0xD8, 0x8A, 0x27, 0x63, 0xAE, - 0xD1, 0xCA, 0x2B, 0x2F, 0xA8, 0xF0, 0x54, 0x06, 0x78, 0xCD, 0x1E, 0x0F, - 0x3A, 0xD8, 0x08, 0x92 -}; - -// BrainPool512t1 -static unsigned char g_bp512t1CorrectBigP[] = { - 0xAA, 0xDD, 0x9D, 0xB8, 0xDB, 0xE9, 0xC4, 0x8B, 0x3F, 0xD4, 0xE6, 0xAE, - 0x33, 0xC9, 0xFC, 0x07, 0xCB, 0x30, 0x8D, 0xB3, 0xB3, 0xC9, 0xD2, 0x0E, - 0xD6, 0x63, 0x9C, 0xCA, 0x70, 0x33, 0x08, 0x71, 0x7D, 0x4D, 0x9B, 0x00, - 0x9B, 0xC6, 0x68, 0x42, 0xAE, 0xCD, 0xA1, 0x2A, 0xE6, 0xA3, 0x80, 0xE6, - 0x28, 0x81, 0xFF, 0x2F, 0x2D, 0x82, 0xC6, 0x85, 0x28, 0xAA, 0x60, 0x56, - 0x58, 0x3A, 0x48, 0xF3 -}; - -static unsigned char g_bp512t1CorrectBigB[] = { - 0x7C, 0xBB, 0xBC, 0xF9, 0x44, 0x1C, 0xFA, 0xB7, 0x6E, 0x18, 0x90, 0xE4, - 0x68, 0x84, 0xEA, 0xE3, 0x21, 0xF7, 0x0C, 0x0B, 0xCB, 0x49, 0x81, 0x52, - 0x78, 0x97, 0x50, 0x4B, 0xEC, 0x3E, 0x36, 0xA6, 0x2B, 0xCD, 0xFA, 0x23, - 0x04, 0x97, 0x65, 0x40, 0xF6, 0x45, 0x00, 0x85, 0xF2, 0xDA, 0xE1, 0x45, - 0xC2, 0x25, 0x53, 0xB4, 0x65, 0x76, 0x36, 0x89, 0x18, 0x0E, 0xA2, 0x57, - 0x18, 0x67, 0x42, 0x3E -}; - -static unsigned char g_bp512t1CorrectBigGX[] = { - 0x64, 0x0E, 0xCE, 0x5C, 0x12, 0x78, 0x87, 0x17, 0xB9, 0xC1, 0xBA, 0x06, - 0xCB, 0xC2, 0xA6, 0xFE, 0xBA, 0x85, 0x84, 0x24, 0x58, 0xC5, 0x6D, 0xDE, - 0x9D, 0xB1, 0x75, 0x8D, 0x39, 0xC0, 0x31, 0x3D, 0x82, 0xBA, 0x51, 0x73, - 0x5C, 0xDB, 0x3E, 0xA4, 0x99, 0xAA, 0x77, 0xA7, 0xD6, 0x94, 0x3A, 0x64, - 0xF7, 0xA3, 0xF2, 0x5F, 0xE2, 0x6F, 0x06, 0xB5, 0x1B, 0xAA, 0x26, 0x96, - 0xFA, 0x90, 0x35, 0xDA -}; - -static unsigned char g_bp512t1CorrectBigGY[] = { - 0x5B, 0x53, 0x4B, 0xD5, 0x95, 0xF5, 0xAF, 0x0F, 0xA2, 0xC8, 0x92, 0x37, - 0x6C, 0x84, 0xAC, 0xE1, 0xBB, 0x4E, 0x30, 0x19, 0xB7, 0x16, 0x34, 0xC0, - 0x11, 0x31, 0x15, 0x9C, 0xAE, 0x03, 0xCE, 0xE9, 0xD9, 0x93, 0x21, 0x84, - 0xBE, 0xEF, 0x21, 0x6B, 0xD7, 0x1D, 0xF2, 0xDA, 0xDF, 0x86, 0xA6, 0x27, - 0x30, 0x6E, 0xCF, 0xF9, 0x6D, 0xBB, 0x8B, 0xAC, 0xE1, 0x98, 0xB6, 0x1E, - 0x00, 0xF8, 0xB3, 0x32 -}; +#include "detailed_ecc_key_params.h" +#include "openssl_class.h" +#include "openssl_common.h" + +HcfResult NewEcKeyPair(int32_t curveId, EC_KEY **returnEcKey); +void FreeCurveBigNum(BIGNUM *pStd, BIGNUM *bStd, BIGNUM *xStd, BIGNUM *yStd); +HcfResult NewGroupFromCurveGFp(const HcfEccCommParamsSpec *ecParams, EC_GROUP **ecGroup, BN_CTX *ctx); +HcfResult SetEcPointToGroup(const HcfEccCommParamsSpec *ecParams, EC_GROUP *group, BN_CTX *ctx); +HcfResult GenerateEcGroupWithParamsSpec(const HcfEccCommParamsSpec *ecParams, EC_GROUP **ecGroup); +HcfResult InitEcKeyByPubKey(const HcfPoint *pubKey, EC_KEY *ecKey); +HcfResult InitEcKeyByPriKey(const HcfBigInteger *priKey, EC_KEY *ecKey); +HcfResult SetEcPubKeyFromPriKey(const HcfBigInteger *priKey, EC_KEY *ecKey); +HcfResult SetEcKey(const HcfPoint *pubKey, const HcfBigInteger *priKey, EC_KEY *ecKey); +HcfResult GetCurveGFp(const EC_GROUP *group, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger); +HcfResult GetGenerator(const EC_GROUP *group, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger); +HcfResult GetOrder(const EC_GROUP *group, HcfBigInteger *returnBigInteger); +HcfResult GetCofactor(const EC_GROUP *group, int *returnCofactor); +HcfResult GetFieldSize(const EC_GROUP *group, int32_t *fieldSize); +HcfResult GetFieldType(const HcfKey *self, const bool isPrivate, char **returnString); +HcfResult GetPubKeyXOrY(const EC_GROUP *group, const EC_POINT *point, const AsyKeySpecItem item, + HcfBigInteger *returnBigInteger); +HcfResult GetPkSkBigInteger(const HcfKey *self, bool isPrivate, + const AsyKeySpecItem item, HcfBigInteger *returnBigInteger); #endif diff --git a/plugin/openssl_plugin/common/inc/ecc_openssl_common_param_spec.h b/plugin/openssl_plugin/common/inc/ecc_openssl_common_param_spec.h new file mode 100644 index 0000000..6a9b217 --- /dev/null +++ b/plugin/openssl_plugin/common/inc/ecc_openssl_common_param_spec.h @@ -0,0 +1,553 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_ECC_OPENSSL_COMMON_PARAM_SPEC_H +#define HCF_ECC_OPENSSL_COMMON_PARAM_SPEC_H + +#include "utils.h" + +static const int32_t NID_secp224r1_len = 28; +static const int32_t NID_X9_62_prime256v1_len = 32; +static const int32_t NID_secp384r1_len = 48; +static const int32_t NID_secp521r1_len = 66; +static const int32_t NID_brainpoolP160r1_len = 20; +static const int32_t NID_brainpoolP160t1_len = 20; +static const int32_t NID_brainpoolP192r1_len = 24; +static const int32_t NID_brainpoolP192t1_len = 24; +static const int32_t NID_brainpoolP224r1_len = 28; +static const int32_t NID_brainpoolP224t1_len = 28; +static const int32_t NID_brainpoolP256r1_len = 32; +static const int32_t NID_brainpoolP256t1_len = 32; +static const int32_t NID_brainpoolP320r1_len = 40; +static const int32_t NID_brainpoolP320t1_len = 40; +static const int32_t NID_brainpoolP384r1_len = 48; +static const int32_t NID_brainpoolP384t1_len = 48; +static const int32_t NID_brainpoolP512r1_len = 64; +static const int32_t NID_brainpoolP512t1_len = 64; + +static unsigned char g_ecc224CorrectBigP[] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01 +}; + +static unsigned char g_ecc224CorrectBigB[] = { + 0xB4, 0x05, 0x0A, 0x85, 0x0C, 0x04, 0xB3, 0xAB, 0xF5, 0x41, 0x32, 0x56, + 0x50, 0x44, 0xB0, 0xB7, 0xD7, 0xBF, 0xD8, 0xBA, 0x27, 0x0B, 0x39, 0x43, + 0x23, 0x55, 0xFF, 0xB4 +}; + +static unsigned char g_ecc224CorrectBigGX[] = { + 0xB7, 0x0E, 0x0C, 0xBD, 0x6B, 0xB4, 0xBF, 0x7F, 0x32, 0x13, 0x90, 0xB9, + 0x4A, 0x03, 0xC1, 0xD3, 0x56, 0xC2, 0x11, 0x22, 0x34, 0x32, 0x80, 0xD6, + 0x11, 0x5C, 0x1D, 0x21 +}; + +static unsigned char g_ecc224CorrectBigGY[] = { + 0xbd, 0x37, 0x63, 0x88, 0xb5, 0xf7, 0x23, 0xfb, 0x4c, 0x22, 0xdf, 0xe6, + 0xcd, 0x43, 0x75, 0xa0, 0x5a, 0x07, 0x47, 0x64, 0x44, 0xd5, 0x81, 0x99, + 0x85, 0x00, 0x7e, 0x34 +}; + +// ECC256 +static unsigned char g_ecc256CorrectBigP[] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF +}; + +static unsigned char g_ecc256CorrectBigB[] = { + 0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7, 0xB3, 0xEB, 0xBD, 0x55, + 0x76, 0x98, 0x86, 0xBC, 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6, + 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B +}; + +static unsigned char g_ecc256CorrectBigGX[] = { + 0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47, 0xF8, 0xBC, 0xE6, 0xE5, + 0x63, 0xA4, 0x40, 0xF2, 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0, + 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96 +}; + +static unsigned char g_ecc256CorrectBigGY[] = { + 0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B, 0x8E, 0xE7, 0xEB, 0x4A, + 0x7C, 0x0F, 0x9E, 0x16, 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE, + 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5 +}; + +// ECC384 +static unsigned char g_ecc384CorrectBigP[] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF +}; + +static unsigned char g_ecc384CorrectBigB[] = { + 0xB3, 0x31, 0x2F, 0xA7, 0xE2, 0x3E, 0xE7, 0xE4, 0x98, 0x8E, 0x05, 0x6B, + 0xE3, 0xF8, 0x2D, 0x19, 0x18, 0x1D, 0x9C, 0x6E, 0xFE, 0x81, 0x41, 0x12, + 0x03, 0x14, 0x08, 0x8F, 0x50, 0x13, 0x87, 0x5A, 0xC6, 0x56, 0x39, 0x8D, + 0x8A, 0x2E, 0xD1, 0x9D, 0x2A, 0x85, 0xC8, 0xED, 0xD3, 0xEC, 0x2A, 0xEF +}; + +static unsigned char g_ecc384CorrectBigGX[] = { + 0xAA, 0x87, 0xCA, 0x22, 0xBE, 0x8B, 0x05, 0x37, 0x8E, 0xB1, 0xC7, 0x1E, + 0xF3, 0x20, 0xAD, 0x74, 0x6E, 0x1D, 0x3B, 0x62, 0x8B, 0xA7, 0x9B, 0x98, + 0x59, 0xF7, 0x41, 0xE0, 0x82, 0x54, 0x2A, 0x38, 0x55, 0x02, 0xF2, 0x5D, + 0xBF, 0x55, 0x29, 0x6C, 0x3A, 0x54, 0x5E, 0x38, 0x72, 0x76, 0x0A, 0xB7 +}; + +static unsigned char g_ecc384CorrectBigGY[] = { + 0x36, 0x17, 0xDE, 0x4A, 0x96, 0x26, 0x2C, 0x6F, 0x5D, 0x9E, 0x98, 0xBF, + 0x92, 0x92, 0xDC, 0x29, 0xF8, 0xF4, 0x1D, 0xBD, 0x28, 0x9A, 0x14, 0x7C, + 0xE9, 0xDA, 0x31, 0x13, 0xB5, 0xF0, 0xB8, 0xC0, 0x0A, 0x60, 0xB1, 0xCE, + 0x1D, 0x7E, 0x81, 0x9D, 0x7A, 0x43, 0x1D, 0x7C, 0x90, 0xEA, 0x0E, 0x5F, +}; + +// ECC521 +static unsigned char g_ecc521CorrectBigP[] = { + 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF +}; + +static unsigned char g_ecc521CorrectBigB[] = { + 0x00, 0x51, 0x95, 0x3E, 0xB9, 0x61, 0x8E, 0x1C, 0x9A, 0x1F, 0x92, 0x9A, + 0x21, 0xA0, 0xB6, 0x85, 0x40, 0xEE, 0xA2, 0xDA, 0x72, 0x5B, 0x99, 0xB3, + 0x15, 0xF3, 0xB8, 0xB4, 0x89, 0x91, 0x8E, 0xF1, 0x09, 0xE1, 0x56, 0x19, + 0x39, 0x51, 0xEC, 0x7E, 0x93, 0x7B, 0x16, 0x52, 0xC0, 0xBD, 0x3B, 0xB1, + 0xBF, 0x07, 0x35, 0x73, 0xDF, 0x88, 0x3D, 0x2C, 0x34, 0xF1, 0xEF, 0x45, + 0x1F, 0xD4, 0x6B, 0x50, 0x3F, 0x00 +}; + +static unsigned char g_ecc521CorrectBigGX[] = { + 0x00, 0xC6, 0x85, 0x8E, 0x06, 0xB7, 0x04, 0x04, 0xE9, 0xCD, 0x9E, 0x3E, + 0xCB, 0x66, 0x23, 0x95, 0xB4, 0x42, 0x9C, 0x64, 0x81, 0x39, 0x05, 0x3F, + 0xB5, 0x21, 0xF8, 0x28, 0xAF, 0x60, 0x6B, 0x4D, 0x3D, 0xBA, 0xA1, 0x4B, + 0x5E, 0x77, 0xEF, 0xE7, 0x59, 0x28, 0xFE, 0x1D, 0xC1, 0x27, 0xA2, 0xFF, + 0xA8, 0xDE, 0x33, 0x48, 0xB3, 0xC1, 0x85, 0x6A, 0x42, 0x9B, 0xF9, 0x7E, + 0x7E, 0x31, 0xC2, 0xE5, 0xBD, 0x66 +}; + +static unsigned char g_ecc521CorrectBigGY[] = { + 0x01, 0x18, 0x39, 0x29, 0x6A, 0x78, 0x9A, 0x3B, 0xC0, 0x04, 0x5C, 0x8A, + 0x5F, 0xB4, 0x2C, 0x7D, 0x1B, 0xD9, 0x98, 0xF5, 0x44, 0x49, 0x57, 0x9B, + 0x44, 0x68, 0x17, 0xAF, 0xBD, 0x17, 0x27, 0x3E, 0x66, 0x2C, 0x97, 0xEE, + 0x72, 0x99, 0x5E, 0xF4, 0x26, 0x40, 0xC5, 0x50, 0xB9, 0x01, 0x3F, 0xAD, + 0x07, 0x61, 0x35, 0x3C, 0x70, 0x86, 0xA2, 0x72, 0xC2, 0x40, 0x88, 0xBE, + 0x94, 0x76, 0x9F, 0xD1, 0x66, 0x50 +}; + +// SM2_256 +static unsigned char g_sm256CorrectBigP[] = { + 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff +}; + +static unsigned char g_sm256CorrectBigB[] = { + 0x28, 0xe9, 0xfa, 0x9e, 0x9d, 0x9f, 0x5e, 0x34, 0x4d, 0x5a, 0x9e, 0x4b, + 0xcf, 0x65, 0x09, 0xa7, 0xf3, 0x97, 0x89, 0xf5, 0x15, 0xab, 0x8f, 0x92, + 0xdd, 0xbc, 0xbd, 0x41, 0x4d, 0x94, 0x0e, 0x93, +}; + +static unsigned char g_sm256CorrectBigGX[] = { + 0x32, 0xc4, 0xae, 0x2c, 0x1f, 0x19, 0x81, 0x19, 0x5f, 0x99, 0x04, 0x46, + 0x6a, 0x39, 0xc9, 0x94, 0x8f, 0xe3, 0x0b, 0xbf, 0xf2, 0x66, 0x0b, 0xe1, + 0x71, 0x5a, 0x45, 0x89, 0x33, 0x4c, 0x74, 0xc7 +}; + +static unsigned char g_sm256CorrectBigGY[] = { + 0xbc, 0x37, 0x36, 0xa2, 0xf4, 0xf6, 0x77, 0x9c, 0x59, 0xbd, 0xce, 0xe3, + 0x6b, 0x69, 0x21, 0x53, 0xd0, 0xa9, 0x87, 0x7c, 0xc6, 0x2a, 0x47, 0x40, + 0x02, 0xdf, 0x32, 0xe5, 0x21, 0x39, 0xf0, 0xa0 +}; + +// BrainPool160r1 +static unsigned char g_bp160r1CorrectBigP[] = { + 0xE9, 0x5E, 0x4A, 0x5F, 0x73, 0x70, 0x59, 0xDC, 0x60, 0xDF, 0xC7, 0xAD, + 0x95, 0xB3, 0xD8, 0x13, 0x95, 0x15, 0x62, 0x0F +}; + +static unsigned char g_bp160r1CorrectBigB[] = { + 0x1E, 0x58, 0x9A, 0x85, 0x95, 0x42, 0x34, 0x12, 0x13, 0x4F, 0xAA, 0x2D, + 0xBD, 0xEC, 0x95, 0xC8, 0xD8, 0x67, 0x5E, 0x58 +}; + +static unsigned char g_bp160r1CorrectBigGX[] = { + 0xBE, 0xD5, 0xAF, 0x16, 0xEA, 0x3F, 0x6A, 0x4F, 0x62, 0x93, 0x8C, 0x46, + 0x31, 0xEB, 0x5A, 0xF7, 0xBD, 0xBC, 0xDB, 0xC3 +}; + +static unsigned char g_bp160r1CorrectBigGY[] = { + 0x16, 0x67, 0xCB, 0x47, 0x7A, 0x1A, 0x8E, 0xC3, 0x38, 0xF9, 0x47, 0x41, + 0x66, 0x9C, 0x97, 0x63, 0x16, 0xDA, 0x63, 0x21 +}; + +// BrainPool160t1 +static unsigned char g_bp160t1CorrectBigP[] = { + 0xE9, 0x5E, 0x4A, 0x5F, 0x73, 0x70, 0x59, 0xDC, 0x60, 0xDF, 0xC7, 0xAD, + 0x95, 0xB3, 0xD8, 0x13, 0x95, 0x15, 0x62, 0x0F +}; + +static unsigned char g_bp160t1CorrectBigB[] = { + 0x7A, 0x55, 0x6B, 0x6D, 0xAE, 0x53, 0x5B, 0x7B, 0x51, 0xED, 0x2C, 0x4D, + 0x7D, 0xAA, 0x7A, 0x0B, 0x5C, 0x55, 0xF3, 0x80 +}; + +static unsigned char g_bp160t1CorrectBigGX[] = { + 0xB1, 0x99, 0xB1, 0x3B, 0x9B, 0x34, 0xEF, 0xC1, 0x39, 0x7E, 0x64, 0xBA, + 0xEB, 0x05, 0xAC, 0xC2, 0x65, 0xFF, 0x23, 0x78 +}; + +static unsigned char g_bp160t1CorrectBigGY[] = { + 0xAD, 0xD6, 0x71, 0x8B, 0x7C, 0x7C, 0x19, 0x61, 0xF0, 0x99, 0x1B, 0x84, + 0x24, 0x43, 0x77, 0x21, 0x52, 0xC9, 0xE0, 0xAD +}; + +// BrainPool192r1 +static unsigned char g_bp192r1CorrectBigP[] = { + 0xC3, 0x02, 0xF4, 0x1D, 0x93, 0x2A, 0x36, 0xCD, 0xA7, 0xA3, 0x46, 0x30, + 0x93, 0xD1, 0x8D, 0xB7, 0x8F, 0xCE, 0x47, 0x6D, 0xE1, 0xA8, 0x62, 0x97 +}; + +static unsigned char g_bp192r1CorrectBigB[] = { + 0x46, 0x9A, 0x28, 0xEF, 0x7C, 0x28, 0xCC, 0xA3, 0xDC, 0x72, 0x1D, 0x04, + 0x4F, 0x44, 0x96, 0xBC, 0xCA, 0x7E, 0xF4, 0x14, 0x6F, 0xBF, 0x25, 0xC9 +}; + +static unsigned char g_bp192r1CorrectBigGX[] = { + 0xC0, 0xA0, 0x64, 0x7E, 0xAA, 0xB6, 0xA4, 0x87, 0x53, 0xB0, 0x33, 0xC5, + 0x6C, 0xB0, 0xF0, 0x90, 0x0A, 0x2F, 0x5C, 0x48, 0x53, 0x37, 0x5F, 0xD6 +}; + +static unsigned char g_bp192r1CorrectBigGY[] = { + 0x14, 0xB6, 0x90, 0x86, 0x6A, 0xBD, 0x5B, 0xB8, 0x8B, 0x5F, 0x48, 0x28, + 0xC1, 0x49, 0x00, 0x02, 0xE6, 0x77, 0x3F, 0xA2, 0xFA, 0x29, 0x9B, 0x8F +}; + +// BrainPool192t1 +static unsigned char g_bp192t1CorrectBigP[] = { + 0xC3, 0x02, 0xF4, 0x1D, 0x93, 0x2A, 0x36, 0xCD, 0xA7, 0xA3, 0x46, 0x30, + 0x93, 0xD1, 0x8D, 0xB7, 0x8F, 0xCE, 0x47, 0x6D, 0xE1, 0xA8, 0x62, 0x97 +}; + +static unsigned char g_bp192t1CorrectBigB[] = { + 0x13, 0xD5, 0x6F, 0xFA, 0xEC, 0x78, 0x68, 0x1E, 0x68, 0xF9, 0xDE, 0xB4, + 0x3B, 0x35, 0xBE, 0xC2, 0xFB, 0x68, 0x54, 0x2E, 0x27, 0x89, 0x7B, 0x79 +}; + +static unsigned char g_bp192t1CorrectBigGX[] = { + 0x3A, 0xE9, 0xE5, 0x8C, 0x82, 0xF6, 0x3C, 0x30, 0x28, 0x2E, 0x1F, 0xE7, + 0xBB, 0xF4, 0x3F, 0xA7, 0x2C, 0x44, 0x6A, 0xF6, 0xF4, 0x61, 0x81, 0x29 +}; + +static unsigned char g_bp192t1CorrectBigGY[] = { + 0x09, 0x7E, 0x2C, 0x56, 0x67, 0xC2, 0x22, 0x3A, 0x90, 0x2A, 0xB5, 0xCA, + 0x44, 0x9D, 0x00, 0x84, 0xB7, 0xE5, 0xB3, 0xDE, 0x7C, 0xCC, 0x01, 0xC9 +}; + +// BrainPool224r1 +static unsigned char g_bp224r1CorrectBigP[] = { + 0xD7, 0xC1, 0x34, 0xAA, 0x26, 0x43, 0x66, 0x86, 0x2A, 0x18, 0x30, 0x25, + 0x75, 0xD1, 0xD7, 0x87, 0xB0, 0x9F, 0x07, 0x57, 0x97, 0xDA, 0x89, 0xF5, + 0x7E, 0xC8, 0xC0, 0xFF +}; + +static unsigned char g_bp224r1CorrectBigB[] = { + 0x25, 0x80, 0xF6, 0x3C, 0xCF, 0xE4, 0x41, 0x38, 0x87, 0x07, 0x13, 0xB1, + 0xA9, 0x23, 0x69, 0xE3, 0x3E, 0x21, 0x35, 0xD2, 0x66, 0xDB, 0xB3, 0x72, + 0x38, 0x6C, 0x40, 0x0B +}; + +static unsigned char g_bp224r1CorrectBigGX[] = { + 0x0D, 0x90, 0x29, 0xAD, 0x2C, 0x7E, 0x5C, 0xF4, 0x34, 0x08, 0x23, 0xB2, + 0xA8, 0x7D, 0xC6, 0x8C, 0x9E, 0x4C, 0xE3, 0x17, 0x4C, 0x1E, 0x6E, 0xFD, + 0xEE, 0x12, 0xC0, 0x7D +}; + +static unsigned char g_bp224r1CorrectBigGY[] = { + 0x58, 0xAA, 0x56, 0xF7, 0x72, 0xC0, 0x72, 0x6F, 0x24, 0xC6, 0xB8, 0x9E, + 0x4E, 0xCD, 0xAC, 0x24, 0x35, 0x4B, 0x9E, 0x99, 0xCA, 0xA3, 0xF6, 0xD3, + 0x76, 0x14, 0x02, 0xCD +}; + +// BrainPool224t1 +static unsigned char g_bp224t1CorrectBigP[] = { + 0xD7, 0xC1, 0x34, 0xAA, 0x26, 0x43, 0x66, 0x86, 0x2A, 0x18, 0x30, 0x25, + 0x75, 0xD1, 0xD7, 0x87, 0xB0, 0x9F, 0x07, 0x57, 0x97, 0xDA, 0x89, 0xF5, + 0x7E, 0xC8, 0xC0, 0xFF +}; + +static unsigned char g_bp224t1CorrectBigB[] = { + 0x4B, 0x33, 0x7D, 0x93, 0x41, 0x04, 0xCD, 0x7B, 0xEF, 0x27, 0x1B, 0xF6, + 0x0C, 0xED, 0x1E, 0xD2, 0x0D, 0xA1, 0x4C, 0x08, 0xB3, 0xBB, 0x64, 0xF1, + 0x8A, 0x60, 0x88, 0x8D +}; + +static unsigned char g_bp224t1CorrectBigGX[] = { + 0x6A, 0xB1, 0xE3, 0x44, 0xCE, 0x25, 0xFF, 0x38, 0x96, 0x42, 0x4E, 0x7F, + 0xFE, 0x14, 0x76, 0x2E, 0xCB, 0x49, 0xF8, 0x92, 0x8A, 0xC0, 0xC7, 0x60, + 0x29, 0xB4, 0xD5, 0x80 +}; + +static unsigned char g_bp224t1CorrectBigGY[] = { + 0x03, 0x74, 0xE9, 0xF5, 0x14, 0x3E, 0x56, 0x8C, 0xD2, 0x3F, 0x3F, 0x4D, + 0x7C, 0x0D, 0x4B, 0x1E, 0x41, 0xC8, 0xCC, 0x0D, 0x1C, 0x6A, 0xBD, 0x5F, + 0x1A, 0x46, 0xDB, 0x4C +}; + +// BrainPool256r1 +static unsigned char g_bp256r1CorrectBigP[] = { + 0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC, 0x3E, 0x66, 0x0A, 0x90, + 0x9D, 0x83, 0x8D, 0x72, 0x6E, 0x3B, 0xF6, 0x23, 0xD5, 0x26, 0x20, 0x28, + 0x20, 0x13, 0x48, 0x1D, 0x1F, 0x6E, 0x53, 0x77 +}; + +static unsigned char g_bp256r1CorrectBigB[] = { + 0x26, 0xDC, 0x5C, 0x6C, 0xE9, 0x4A, 0x4B, 0x44, 0xF3, 0x30, 0xB5, 0xD9, + 0xBB, 0xD7, 0x7C, 0xBF, 0x95, 0x84, 0x16, 0x29, 0x5C, 0xF7, 0xE1, 0xCE, + 0x6B, 0xCC, 0xDC, 0x18, 0xFF, 0x8C, 0x07, 0xB6 +}; + +static unsigned char g_bp256r1CorrectBigGX[] = { + 0x8B, 0xD2, 0xAE, 0xB9, 0xCB, 0x7E, 0x57, 0xCB, 0x2C, 0x4B, 0x48, 0x2F, + 0xFC, 0x81, 0xB7, 0xAF, 0xB9, 0xDE, 0x27, 0xE1, 0xE3, 0xBD, 0x23, 0xC2, + 0x3A, 0x44, 0x53, 0xBD, 0x9A, 0xCE, 0x32, 0x62 +}; + +static unsigned char g_bp256r1CorrectBigGY[] = { + 0x54, 0x7E, 0xF8, 0x35, 0xC3, 0xDA, 0xC4, 0xFD, 0x97, 0xF8, 0x46, 0x1A, + 0x14, 0x61, 0x1D, 0xC9, 0xC2, 0x77, 0x45, 0x13, 0x2D, 0xED, 0x8E, 0x54, + 0x5C, 0x1D, 0x54, 0xC7, 0x2F, 0x04, 0x69, 0x97 +}; + +// BrainPool256t1 +static unsigned char g_bp256t1CorrectBigP[] = { + 0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC, 0x3E, 0x66, 0x0A, 0x90, + 0x9D, 0x83, 0x8D, 0x72, 0x6E, 0x3B, 0xF6, 0x23, 0xD5, 0x26, 0x20, 0x28, + 0x20, 0x13, 0x48, 0x1D, 0x1F, 0x6E, 0x53, 0x77 +}; + +static unsigned char g_bp256t1CorrectBigB[] = { + 0x66, 0x2C, 0x61, 0xC4, 0x30, 0xD8, 0x4E, 0xA4, 0xFE, 0x66, 0xA7, 0x73, + 0x3D, 0x0B, 0x76, 0xB7, 0xBF, 0x93, 0xEB, 0xC4, 0xAF, 0x2F, 0x49, 0x25, + 0x6A, 0xE5, 0x81, 0x01, 0xFE, 0xE9, 0x2B, 0x04 +}; + +static unsigned char g_bp256t1CorrectBigGX[] = { + 0xA3, 0xE8, 0xEB, 0x3C, 0xC1, 0xCF, 0xE7, 0xB7, 0x73, 0x22, 0x13, 0xB2, + 0x3A, 0x65, 0x61, 0x49, 0xAF, 0xA1, 0x42, 0xC4, 0x7A, 0xAF, 0xBC, 0x2B, + 0x79, 0xA1, 0x91, 0x56, 0x2E, 0x13, 0x05, 0xF4 +}; + +static unsigned char g_bp256t1CorrectBigGY[] = { + 0x2D, 0x99, 0x6C, 0x82, 0x34, 0x39, 0xC5, 0x6D, 0x7F, 0x7B, 0x22, 0xE1, + 0x46, 0x44, 0x41, 0x7E, 0x69, 0xBC, 0xB6, 0xDE, 0x39, 0xD0, 0x27, 0x00, + 0x1D, 0xAB, 0xE8, 0xF3, 0x5B, 0x25, 0xC9, 0xBE +}; + +// BrainPool320r1 +static unsigned char g_bp320r1CorrectBigP[] = { + 0xD3, 0x5E, 0x47, 0x20, 0x36, 0xBC, 0x4F, 0xB7, 0xE1, 0x3C, 0x78, 0x5E, + 0xD2, 0x01, 0xE0, 0x65, 0xF9, 0x8F, 0xCF, 0xA6, 0xF6, 0xF4, 0x0D, 0xEF, + 0x4F, 0x92, 0xB9, 0xEC, 0x78, 0x93, 0xEC, 0x28, 0xFC, 0xD4, 0x12, 0xB1, + 0xF1, 0xB3, 0x2E, 0x27 +}; + +static unsigned char g_bp320r1CorrectBigB[] = { + 0x52, 0x08, 0x83, 0x94, 0x9D, 0xFD, 0xBC, 0x42, 0xD3, 0xAD, 0x19, 0x86, + 0x40, 0x68, 0x8A, 0x6F, 0xE1, 0x3F, 0x41, 0x34, 0x95, 0x54, 0xB4, 0x9A, + 0xCC, 0x31, 0xDC, 0xCD, 0x88, 0x45, 0x39, 0x81, 0x6F, 0x5E, 0xB4, 0xAC, + 0x8F, 0xB1, 0xF1, 0xA6 +}; + +static unsigned char g_bp320r1CorrectBigGX[] = { + 0x43, 0xBD, 0x7E, 0x9A, 0xFB, 0x53, 0xD8, 0xB8, 0x52, 0x89, 0xBC, 0xC4, + 0x8E, 0xE5, 0xBF, 0xE6, 0xF2, 0x01, 0x37, 0xD1, 0x0A, 0x08, 0x7E, 0xB6, + 0xE7, 0x87, 0x1E, 0x2A, 0x10, 0xA5, 0x99, 0xC7, 0x10, 0xAF, 0x8D, 0x0D, + 0x39, 0xE2, 0x06, 0x11 +}; + +static unsigned char g_bp320r1CorrectBigGY[] = { + 0x14, 0xFD, 0xD0, 0x55, 0x45, 0xEC, 0x1C, 0xC8, 0xAB, 0x40, 0x93, 0x24, + 0x7F, 0x77, 0x27, 0x5E, 0x07, 0x43, 0xFF, 0xED, 0x11, 0x71, 0x82, 0xEA, + 0xA9, 0xC7, 0x78, 0x77, 0xAA, 0xAC, 0x6A, 0xC7, 0xD3, 0x52, 0x45, 0xD1, + 0x69, 0x2E, 0x8E, 0xE1 +}; + +// BrainPool320t1 +static unsigned char g_bp320t1CorrectBigP[] = { + 0xD3, 0x5E, 0x47, 0x20, 0x36, 0xBC, 0x4F, 0xB7, 0xE1, 0x3C, 0x78, 0x5E, + 0xD2, 0x01, 0xE0, 0x65, 0xF9, 0x8F, 0xCF, 0xA6, 0xF6, 0xF4, 0x0D, 0xEF, + 0x4F, 0x92, 0xB9, 0xEC, 0x78, 0x93, 0xEC, 0x28, 0xFC, 0xD4, 0x12, 0xB1, + 0xF1, 0xB3, 0x2E, 0x27 +}; + +static unsigned char g_bp320t1CorrectBigB[] = { + 0xA7, 0xF5, 0x61, 0xE0, 0x38, 0xEB, 0x1E, 0xD5, 0x60, 0xB3, 0xD1, 0x47, + 0xDB, 0x78, 0x20, 0x13, 0x06, 0x4C, 0x19, 0xF2, 0x7E, 0xD2, 0x7C, 0x67, + 0x80, 0xAA, 0xF7, 0x7F, 0xB8, 0xA5, 0x47, 0xCE, 0xB5, 0xB4, 0xFE, 0xF4, + 0x22, 0x34, 0x03, 0x53 +}; + +static unsigned char g_bp320t1CorrectBigGX[] = { + 0x92, 0x5B, 0xE9, 0xFB, 0x01, 0xAF, 0xC6, 0xFB, 0x4D, 0x3E, 0x7D, 0x49, + 0x90, 0x01, 0x0F, 0x81, 0x34, 0x08, 0xAB, 0x10, 0x6C, 0x4F, 0x09, 0xCB, + 0x7E, 0xE0, 0x78, 0x68, 0xCC, 0x13, 0x6F, 0xFF, 0x33, 0x57, 0xF6, 0x24, + 0xA2, 0x1B, 0xED, 0x52 +}; + +static unsigned char g_bp320t1CorrectBigGY[] = { + 0x63, 0xBA, 0x3A, 0x7A, 0x27, 0x48, 0x3E, 0xBF, 0x66, 0x71, 0xDB, 0xEF, + 0x7A, 0xBB, 0x30, 0xEB, 0xEE, 0x08, 0x4E, 0x58, 0xA0, 0xB0, 0x77, 0xAD, + 0x42, 0xA5, 0xA0, 0x98, 0x9D, 0x1E, 0xE7, 0x1B, 0x1B, 0x9B, 0xC0, 0x45, + 0x5F, 0xB0, 0xD2, 0xC3 +}; + +// BrainPool384r1 +static unsigned char g_bp384r1CorrectBigP[] = { + 0x8C, 0xB9, 0x1E, 0x82, 0xA3, 0x38, 0x6D, 0x28, 0x0F, 0x5D, 0x6F, 0x7E, + 0x50, 0xE6, 0x41, 0xDF, 0x15, 0x2F, 0x71, 0x09, 0xED, 0x54, 0x56, 0xB4, + 0x12, 0xB1, 0xDA, 0x19, 0x7F, 0xB7, 0x11, 0x23, 0xAC, 0xD3, 0xA7, 0x29, + 0x90, 0x1D, 0x1A, 0x71, 0x87, 0x47, 0x00, 0x13, 0x31, 0x07, 0xEC, 0x53 +}; + +static unsigned char g_bp384r1CorrectBigB[] = { + 0x04, 0xA8, 0xC7, 0xDD, 0x22, 0xCE, 0x28, 0x26, 0x8B, 0x39, 0xB5, 0x54, + 0x16, 0xF0, 0x44, 0x7C, 0x2F, 0xB7, 0x7D, 0xE1, 0x07, 0xDC, 0xD2, 0xA6, + 0x2E, 0x88, 0x0E, 0xA5, 0x3E, 0xEB, 0x62, 0xD5, 0x7C, 0xB4, 0x39, 0x02, + 0x95, 0xDB, 0xC9, 0x94, 0x3A, 0xB7, 0x86, 0x96, 0xFA, 0x50, 0x4C, 0x11 +}; + +static unsigned char g_bp384r1CorrectBigGX[] = { + 0x1D, 0x1C, 0x64, 0xF0, 0x68, 0xCF, 0x45, 0xFF, 0xA2, 0xA6, 0x3A, 0x81, + 0xB7, 0xC1, 0x3F, 0x6B, 0x88, 0x47, 0xA3, 0xE7, 0x7E, 0xF1, 0x4F, 0xE3, + 0xDB, 0x7F, 0xCA, 0xFE, 0x0C, 0xBD, 0x10, 0xE8, 0xE8, 0x26, 0xE0, 0x34, + 0x36, 0xD6, 0x46, 0xAA, 0xEF, 0x87, 0xB2, 0xE2, 0x47, 0xD4, 0xAF, 0x1E +}; + +static unsigned char g_bp384r1CorrectBigGY[] = { + 0x8A, 0xBE, 0x1D, 0x75, 0x20, 0xF9, 0xC2, 0xA4, 0x5C, 0xB1, 0xEB, 0x8E, + 0x95, 0xCF, 0xD5, 0x52, 0x62, 0xB7, 0x0B, 0x29, 0xFE, 0xEC, 0x58, 0x64, + 0xE1, 0x9C, 0x05, 0x4F, 0xF9, 0x91, 0x29, 0x28, 0x0E, 0x46, 0x46, 0x21, + 0x77, 0x91, 0x81, 0x11, 0x42, 0x82, 0x03, 0x41, 0x26, 0x3C, 0x53, 0x15 +}; + +// BrainPool384t1 +static unsigned char g_bp384t1CorrectBigP[] = { + 0x8C, 0xB9, 0x1E, 0x82, 0xA3, 0x38, 0x6D, 0x28, 0x0F, 0x5D, 0x6F, 0x7E, + 0x50, 0xE6, 0x41, 0xDF, 0x15, 0x2F, 0x71, 0x09, 0xED, 0x54, 0x56, 0xB4, + 0x12, 0xB1, 0xDA, 0x19, 0x7F, 0xB7, 0x11, 0x23, 0xAC, 0xD3, 0xA7, 0x29, + 0x90, 0x1D, 0x1A, 0x71, 0x87, 0x47, 0x00, 0x13, 0x31, 0x07, 0xEC, 0x53 +}; + +static unsigned char g_bp384t1CorrectBigB[] = { + 0x7F, 0x51, 0x9E, 0xAD, 0xA7, 0xBD, 0xA8, 0x1B, 0xD8, 0x26, 0xDB, 0xA6, + 0x47, 0x91, 0x0F, 0x8C, 0x4B, 0x93, 0x46, 0xED, 0x8C, 0xCD, 0xC6, 0x4E, + 0x4B, 0x1A, 0xBD, 0x11, 0x75, 0x6D, 0xCE, 0x1D, 0x20, 0x74, 0xAA, 0x26, + 0x3B, 0x88, 0x80, 0x5C, 0xED, 0x70, 0x35, 0x5A, 0x33, 0xB4, 0x71, 0xEE +}; + +static unsigned char g_bp384t1CorrectBigGX[] = { + 0x18, 0xDE, 0x98, 0xB0, 0x2D, 0xB9, 0xA3, 0x06, 0xF2, 0xAF, 0xCD, 0x72, + 0x35, 0xF7, 0x2A, 0x81, 0x9B, 0x80, 0xAB, 0x12, 0xEB, 0xD6, 0x53, 0x17, + 0x24, 0x76, 0xFE, 0xCD, 0x46, 0x2A, 0xAB, 0xFF, 0xC4, 0xFF, 0x19, 0x1B, + 0x94, 0x6A, 0x5F, 0x54, 0xD8, 0xD0, 0xAA, 0x2F, 0x41, 0x88, 0x08, 0xCC +}; + +static unsigned char g_bp384t1CorrectBigGY[] = { + 0x25, 0xAB, 0x05, 0x69, 0x62, 0xD3, 0x06, 0x51, 0xA1, 0x14, 0xAF, 0xD2, + 0x75, 0x5A, 0xD3, 0x36, 0x74, 0x7F, 0x93, 0x47, 0x5B, 0x7A, 0x1F, 0xCA, + 0x3B, 0x88, 0xF2, 0xB6, 0xA2, 0x08, 0xCC, 0xFE, 0x46, 0x94, 0x08, 0x58, + 0x4D, 0xC2, 0xB2, 0x91, 0x26, 0x75, 0xBF, 0x5B, 0x9E, 0x58, 0x29, 0x28 +}; + +// BrainPool512r1 +static unsigned char g_bp512r1CorrectBigP[] = { + 0xAA, 0xDD, 0x9D, 0xB8, 0xDB, 0xE9, 0xC4, 0x8B, 0x3F, 0xD4, 0xE6, 0xAE, + 0x33, 0xC9, 0xFC, 0x07, 0xCB, 0x30, 0x8D, 0xB3, 0xB3, 0xC9, 0xD2, 0x0E, + 0xD6, 0x63, 0x9C, 0xCA, 0x70, 0x33, 0x08, 0x71, 0x7D, 0x4D, 0x9B, 0x00, + 0x9B, 0xC6, 0x68, 0x42, 0xAE, 0xCD, 0xA1, 0x2A, 0xE6, 0xA3, 0x80, 0xE6, + 0x28, 0x81, 0xFF, 0x2F, 0x2D, 0x82, 0xC6, 0x85, 0x28, 0xAA, 0x60, 0x56, + 0x58, 0x3A, 0x48, 0xF3 +}; + +static unsigned char g_bp512r1CorrectBigB[] = { + 0x3D, 0xF9, 0x16, 0x10, 0xA8, 0x34, 0x41, 0xCA, 0xEA, 0x98, 0x63, 0xBC, + 0x2D, 0xED, 0x5D, 0x5A, 0xA8, 0x25, 0x3A, 0xA1, 0x0A, 0x2E, 0xF1, 0xC9, + 0x8B, 0x9A, 0xC8, 0xB5, 0x7F, 0x11, 0x17, 0xA7, 0x2B, 0xF2, 0xC7, 0xB9, + 0xE7, 0xC1, 0xAC, 0x4D, 0x77, 0xFC, 0x94, 0xCA, 0xDC, 0x08, 0x3E, 0x67, + 0x98, 0x40, 0x50, 0xB7, 0x5E, 0xBA, 0xE5, 0xDD, 0x28, 0x09, 0xBD, 0x63, + 0x80, 0x16, 0xF7, 0x23 +}; + +static unsigned char g_bp512r1CorrectBigGX[] = { + 0x81, 0xAE, 0xE4, 0xBD, 0xD8, 0x2E, 0xD9, 0x64, 0x5A, 0x21, 0x32, 0x2E, + 0x9C, 0x4C, 0x6A, 0x93, 0x85, 0xED, 0x9F, 0x70, 0xB5, 0xD9, 0x16, 0xC1, + 0xB4, 0x3B, 0x62, 0xEE, 0xF4, 0xD0, 0x09, 0x8E, 0xFF, 0x3B, 0x1F, 0x78, + 0xE2, 0xD0, 0xD4, 0x8D, 0x50, 0xD1, 0x68, 0x7B, 0x93, 0xB9, 0x7D, 0x5F, + 0x7C, 0x6D, 0x50, 0x47, 0x40, 0x6A, 0x5E, 0x68, 0x8B, 0x35, 0x22, 0x09, + 0xBC, 0xB9, 0xF8, 0x22 +}; + +static unsigned char g_bp512r1CorrectBigGY[] = { + 0x7D, 0xDE, 0x38, 0x5D, 0x56, 0x63, 0x32, 0xEC, 0xC0, 0xEA, 0xBF, 0xA9, + 0xCF, 0x78, 0x22, 0xFD, 0xF2, 0x09, 0xF7, 0x00, 0x24, 0xA5, 0x7B, 0x1A, + 0xA0, 0x00, 0xC5, 0x5B, 0x88, 0x1F, 0x81, 0x11, 0xB2, 0xDC, 0xDE, 0x49, + 0x4A, 0x5F, 0x48, 0x5E, 0x5B, 0xCA, 0x4B, 0xD8, 0x8A, 0x27, 0x63, 0xAE, + 0xD1, 0xCA, 0x2B, 0x2F, 0xA8, 0xF0, 0x54, 0x06, 0x78, 0xCD, 0x1E, 0x0F, + 0x3A, 0xD8, 0x08, 0x92 +}; + +// BrainPool512t1 +static unsigned char g_bp512t1CorrectBigP[] = { + 0xAA, 0xDD, 0x9D, 0xB8, 0xDB, 0xE9, 0xC4, 0x8B, 0x3F, 0xD4, 0xE6, 0xAE, + 0x33, 0xC9, 0xFC, 0x07, 0xCB, 0x30, 0x8D, 0xB3, 0xB3, 0xC9, 0xD2, 0x0E, + 0xD6, 0x63, 0x9C, 0xCA, 0x70, 0x33, 0x08, 0x71, 0x7D, 0x4D, 0x9B, 0x00, + 0x9B, 0xC6, 0x68, 0x42, 0xAE, 0xCD, 0xA1, 0x2A, 0xE6, 0xA3, 0x80, 0xE6, + 0x28, 0x81, 0xFF, 0x2F, 0x2D, 0x82, 0xC6, 0x85, 0x28, 0xAA, 0x60, 0x56, + 0x58, 0x3A, 0x48, 0xF3 +}; + +static unsigned char g_bp512t1CorrectBigB[] = { + 0x7C, 0xBB, 0xBC, 0xF9, 0x44, 0x1C, 0xFA, 0xB7, 0x6E, 0x18, 0x90, 0xE4, + 0x68, 0x84, 0xEA, 0xE3, 0x21, 0xF7, 0x0C, 0x0B, 0xCB, 0x49, 0x81, 0x52, + 0x78, 0x97, 0x50, 0x4B, 0xEC, 0x3E, 0x36, 0xA6, 0x2B, 0xCD, 0xFA, 0x23, + 0x04, 0x97, 0x65, 0x40, 0xF6, 0x45, 0x00, 0x85, 0xF2, 0xDA, 0xE1, 0x45, + 0xC2, 0x25, 0x53, 0xB4, 0x65, 0x76, 0x36, 0x89, 0x18, 0x0E, 0xA2, 0x57, + 0x18, 0x67, 0x42, 0x3E +}; + +static unsigned char g_bp512t1CorrectBigGX[] = { + 0x64, 0x0E, 0xCE, 0x5C, 0x12, 0x78, 0x87, 0x17, 0xB9, 0xC1, 0xBA, 0x06, + 0xCB, 0xC2, 0xA6, 0xFE, 0xBA, 0x85, 0x84, 0x24, 0x58, 0xC5, 0x6D, 0xDE, + 0x9D, 0xB1, 0x75, 0x8D, 0x39, 0xC0, 0x31, 0x3D, 0x82, 0xBA, 0x51, 0x73, + 0x5C, 0xDB, 0x3E, 0xA4, 0x99, 0xAA, 0x77, 0xA7, 0xD6, 0x94, 0x3A, 0x64, + 0xF7, 0xA3, 0xF2, 0x5F, 0xE2, 0x6F, 0x06, 0xB5, 0x1B, 0xAA, 0x26, 0x96, + 0xFA, 0x90, 0x35, 0xDA +}; + +static unsigned char g_bp512t1CorrectBigGY[] = { + 0x5B, 0x53, 0x4B, 0xD5, 0x95, 0xF5, 0xAF, 0x0F, 0xA2, 0xC8, 0x92, 0x37, + 0x6C, 0x84, 0xAC, 0xE1, 0xBB, 0x4E, 0x30, 0x19, 0xB7, 0x16, 0x34, 0xC0, + 0x11, 0x31, 0x15, 0x9C, 0xAE, 0x03, 0xCE, 0xE9, 0xD9, 0x93, 0x21, 0x84, + 0xBE, 0xEF, 0x21, 0x6B, 0xD7, 0x1D, 0xF2, 0xDA, 0xDF, 0x86, 0xA6, 0x27, + 0x30, 0x6E, 0xCF, 0xF9, 0x6D, 0xBB, 0x8B, 0xAC, 0xE1, 0x98, 0xB6, 0x1E, + 0x00, 0xF8, 0xB3, 0x32 +}; +#endif diff --git a/plugin/openssl_plugin/common/inc/openssl_adapter.h b/plugin/openssl_plugin/common/inc/openssl_adapter.h index 2c9768c..feeeb58 100644 --- a/plugin/openssl_plugin/common/inc/openssl_adapter.h +++ b/plugin/openssl_plugin/common/inc/openssl_adapter.h @@ -24,7 +24,9 @@ #include #include #include +#include #include +#include #ifdef __cplusplus extern "C" { @@ -96,9 +98,13 @@ EVP_PKEY_CTX *Openssl_EVP_MD_CTX_get_pkey_ctx(EVP_MD_CTX *ctx); int Openssl_EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey); int Openssl_EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t count); int Openssl_EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen); +int Openssl_EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, + const unsigned char *tbs, size_t tbslen); int Openssl_EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey); int Openssl_EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t count); int Openssl_EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, size_t siglen); +int Openssl_EVP_DigestVerify(EVP_MD_CTX *ctx, unsigned char *sig, size_t siglen, + const unsigned char *tbs, size_t tbslen); int Openssl_EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx); int Openssl_EVP_PKEY_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen); @@ -107,9 +113,14 @@ int Openssl_EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t size_t tbslen); EVP_PKEY *Openssl_EVP_PKEY_new(void); +EVP_PKEY *Openssl_EVP_PKEY_new_raw_public_key(int type, ENGINE *e, const unsigned char *pub, size_t len); +EVP_PKEY *Openssl_EVP_PKEY_new_raw_private_key(int type, ENGINE *e, const unsigned char *pub, size_t len); +int Openssl_EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, size_t *len); +int Openssl_EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, size_t *len); int Openssl_EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key); void Openssl_EVP_PKEY_free(EVP_PKEY *pkey); - +EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, + EVP_PKEY *pkey, const char *propquery); EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e); int Openssl_EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx); int Openssl_EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer); @@ -125,9 +136,17 @@ int Openssl_EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx); int Openssl_EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx); EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_id(int id, ENGINE *e); +int Openssl_EVP_PKEY_base_id(EVP_PKEY *pkey); +EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx, const char *name, const char *propquery); +OSSL_PARAM Openssl_OSSL_PARAM_construct_utf8_string(const char *key, char *buf, size_t bsize); +OSSL_PARAM Openssl_OSSL_PARAM_construct_end(void); +OSSL_PARAM Openssl_OSSL_PARAM_construct_uint(const char *key, unsigned int *buf); +OSSL_PARAM Openssl_OSSL_PARAM_construct_int(const char *key, int *buf); +int Openssl_EVP_PKEY_generate(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); int Openssl_EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int id_len); int Openssl_EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx); int Openssl_EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits); +int Openssl_EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params); int Openssl_EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); int Openssl_EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx); int Openssl_EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); @@ -149,6 +168,12 @@ DSA *Openssl_d2i_DSAPrivateKey(DSA **dsa, const unsigned char **ppin, long lengt int Openssl_i2d_DSA_PUBKEY(DSA *dsa, unsigned char **ppout); int Openssl_i2d_DSAPrivateKey(DSA *dsa, unsigned char **ppout); +int Openssl_EVP_PKEY_check(EVP_PKEY_CTX *ctx); +EVP_PKEY *Openssl_EVP_PKEY_dup(EVP_PKEY *a); +EVP_PKEY *Openssl_d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length); +EVP_PKEY *Openssl_d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp, long length); +int Openssl_i2d_PUBKEY(EVP_PKEY *pkey, unsigned char **ppout); +int Openssl_i2d_PrivateKey(EVP_PKEY *pkey, unsigned char **ppout); RSA *Openssl_RSA_new(void); void Openssl_RSA_free(RSA *rsa); int Openssl_RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes, @@ -270,6 +295,25 @@ EC_GROUP *Openssl_EC_GROUP_new_by_curve_name(int nid); int OPENSSL_EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr); +DH *Openssl_DH_new(void); +int Openssl_DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh); +void Openssl_DH_free(DH *dh); +int Openssl_DH_generate_key(DH *dh); +const BIGNUM *Openssl_DH_get0_p(const DH *dh); +const BIGNUM *Openssl_DH_get0_q(const DH *dh); +const BIGNUM *Openssl_DH_get0_g(const DH *dh); +long Openssl_DH_get_length(const DH *dh); +int Openssl_DH_set_length(DH *dh, long length); +const BIGNUM *Openssl_DH_get0_pub_key(const DH *dh); +const BIGNUM *Openssl_DH_get0_priv_key(const DH *dh); +int Openssl_EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key); +int Openssl_EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key); +struct dh_st *Openssl_EVP_PKEY_get1_DH(EVP_PKEY *pkey); +int Openssl_EVP_PKEY_CTX_set_dh_paramgen_prime_len(EVP_PKEY_CTX *ctx, int pbits); +int Openssl_DH_up_ref(DH *r); +int Openssl_DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g); +int Openssl_DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key); + #ifdef __cplusplus } #endif diff --git a/plugin/openssl_plugin/common/inc/openssl_class.h b/plugin/openssl_plugin/common/inc/openssl_class.h index 4144b72..8110602 100644 --- a/plugin/openssl_plugin/common/inc/openssl_class.h +++ b/plugin/openssl_plugin/common/inc/openssl_class.h @@ -95,6 +95,26 @@ typedef struct { } HcfOpensslDsaKeyPair; #define OPENSSL_DSA_KEYPAIR_CLASS "OPENSSL.DSA.KEY_PAIR" +typedef struct { + HcfPubKey base; + + EVP_PKEY *pkey; +} HcfOpensslAlg25519PubKey; +#define OPENSSL_ALG25519_PUBKEY_CLASS "OPENSSL.ALG25519.PUB_KEY" + +typedef struct { + HcfPriKey base; + + EVP_PKEY *pkey; +} HcfOpensslAlg25519PriKey; +#define OPENSSL_ALG25519_PRIKEY_CLASS "OPENSSL.ALG25519.PRI_KEY" + +typedef struct { + HcfKeyPair base; +} HcfOpensslAlg25519KeyPair; +#define OPENSSL_ALG25519_KEYPAIR_CLASS "OPENSSL.ALG25519.KEY_PAIR" + + typedef struct { HcfPubKey base; @@ -122,6 +142,29 @@ typedef struct { } HcfOpensslSm2KeyPair; #define HCF_OPENSSL_SM2_KEY_PAIR_CLASS "OPENSSL.SM2.KEY_PAIR" +typedef struct { + HcfPubKey base; + + uint32_t bits; + + DH *pk; +} HcfOpensslDhPubKey; +#define OPENSSL_DH_PUBKEY_CLASS "OPENSSL.DH.PUB_KEY" + +typedef struct { + HcfPriKey base; + + uint32_t bits; + + DH *sk; +} HcfOpensslDhPriKey; +#define OPENSSL_DH_PRIKEY_CLASS "OPENSSL.DH.PRI_KEY" + +typedef struct { + HcfKeyPair base; +} HcfOpensslDhKeyPair; +#define OPENSSL_DH_KEYPAIR_CLASS "OPENSSL.DH.KEY_PAIR" + #define OPENSSL_RSA_CIPHER_CLASS "OPENSSL.RSA.CIPHER" #define OPENSSL_3DES_CIPHER_CLASS "OPENSSL.3DES.CIPHER" #define OPENSSL_AES_CIPHER_CLASS "OPENSSL.AES.CIPHER" diff --git a/plugin/openssl_plugin/common/inc/openssl_common.h b/plugin/openssl_plugin/common/inc/openssl_common.h index 5417fe0..78c5665 100644 --- a/plugin/openssl_plugin/common/inc/openssl_common.h +++ b/plugin/openssl_plugin/common/inc/openssl_common.h @@ -26,6 +26,7 @@ #include "big_integer.h" #include "params_parser.h" #include "result.h" +#include "utils.h" #define HCF_OPENSSL_SUCCESS 1 /* openssl return 1: success */ #define HCF_BITS_PER_BYTE 8 @@ -62,6 +63,8 @@ HcfResult GetRsaSpecStringMGF(char **returnString); HcfResult GetSm2SpecStringSm3(char **returnString); +HcfResult KeyDerive(EVP_PKEY *priKey, EVP_PKEY *pubKey, HcfBlob *returnSecret); + #ifdef __cplusplus } #endif diff --git a/plugin/openssl_plugin/common/src/dh_openssl_common.c b/plugin/openssl_plugin/common/src/dh_openssl_common.c new file mode 100644 index 0000000..7bb5871 --- /dev/null +++ b/plugin/openssl_plugin/common/src/dh_openssl_common.c @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "dh_openssl_common.h" +#include + +#include "securec.h" + +#include "detailed_dh_key_params.h" +#include "log.h" +#include "memory.h" +#include "openssl_adapter.h" +#include "openssl_class.h" +#include "openssl_common.h" + +enum HcfDhNamedGroupId { + HCF_DH_MODP_SIZE_1536 = 0, + HCF_DH_MODP_SIZE_2048, + HCF_DH_MODP_SIZE_3072, + HCF_DH_MODP_SIZE_4096, + HCF_DH_MODP_SIZE_6144, + HCF_DH_MODP_SIZE_8192, + HCF_DH_FFDHE_SIZE_2048, + HCF_DH_FFDHE_SIZE_3072, + HCF_DH_FFDHE_SIZE_4096, + HCF_DH_FFDHE_SIZE_6144, + HCF_DH_FFDHE_SIZE_8192 +}; + +enum HcfDhPLenSize { + HCF_DH_PLEN_2048 = 2048, + HCF_DH_PLEN_3072 = 3072, + HCF_DH_PLEN_4096 = 4096, + HCF_DH_PLEN_6144 = 6144, + HCF_DH_PLEN_8192 = 8192 +}; + +typedef struct { + enum HcfDhNamedGroupId dhId; + char *nidName; +} NidNameByType; + +static const NidNameByType NID_NAME_BY_TYPE_MAP[] = { + { HCF_DH_MODP_SIZE_1536, "modp_1536" }, + { HCF_DH_MODP_SIZE_2048, "modp_2048" }, + { HCF_DH_MODP_SIZE_3072, "modp_3072" }, + { HCF_DH_MODP_SIZE_4096, "modp_4096" }, + { HCF_DH_MODP_SIZE_6144, "modp_6144" }, + { HCF_DH_MODP_SIZE_8192, "modp_8192" }, + { HCF_DH_FFDHE_SIZE_2048, "ffdhe2048" }, + { HCF_DH_FFDHE_SIZE_3072, "ffdhe3072" }, + { HCF_DH_FFDHE_SIZE_4096, "ffdhe4096" }, + { HCF_DH_FFDHE_SIZE_6144, "ffdhe6144" }, + { HCF_DH_FFDHE_SIZE_8192, "ffdhe8192" } +}; + +typedef struct { + enum HcfDhPLenSize pLen; + char *nidName; +} NidNameByPLen; + +static const NidNameByPLen NID_NAME_PLEN_MAP[] = { + { HCF_DH_PLEN_2048, "ffdhe2048" }, + { HCF_DH_PLEN_3072, "ffdhe3072" }, + { HCF_DH_PLEN_4096, "ffdhe4096" }, + { HCF_DH_PLEN_6144, "ffdhe6144" }, + { HCF_DH_PLEN_8192, "ffdhe8192" } +}; + +EVP_PKEY *NewEvpPkeyByDh(DH *dh, bool withDuplicate) +{ + if (dh == NULL) { + LOGE("DH is NULL"); + return NULL; + } + EVP_PKEY *pKey = Openssl_EVP_PKEY_new(); + if (pKey == NULL) { + LOGE("EVP_PKEY_new fail"); + HcfPrintOpensslError(); + return NULL; + } + if (withDuplicate) { + if (Openssl_EVP_PKEY_set1_DH(pKey, dh) != HCF_OPENSSL_SUCCESS) { + LOGE("EVP_PKEY_set1_DH fail"); + HcfPrintOpensslError(); + Openssl_EVP_PKEY_free(pKey); + return NULL; + } + } else { + if (Openssl_EVP_PKEY_assign_DH(pKey, dh) != HCF_OPENSSL_SUCCESS) { + LOGE("EVP_PKEY_assign_DH fail"); + HcfPrintOpensslError(); + Openssl_EVP_PKEY_free(pKey); + return NULL; + } + } + return pKey; +} + +char *GetNidNameByDhId(int32_t pLen) +{ + if (pLen < 0) { + LOGE("Invalid pLen"); + return NULL; + } + for (uint32_t i = 0; i < sizeof(NID_NAME_BY_TYPE_MAP) / sizeof(NID_NAME_BY_TYPE_MAP[0]); i++) { + if (NID_NAME_BY_TYPE_MAP[i].dhId == pLen) { + return NID_NAME_BY_TYPE_MAP[i].nidName; + } + } + LOGE("Invalid prime len:%d", pLen); + return NULL; +} + +char *GetNidNameByDhPLen(int32_t pLen) +{ + if (pLen < 0) { + LOGE("Invalid pLen"); + return NULL; + } + for (uint32_t i = 0; i < sizeof(NID_NAME_PLEN_MAP) / sizeof(NID_NAME_PLEN_MAP[0]); i++) { + if (NID_NAME_PLEN_MAP[i].pLen == pLen) { + return NID_NAME_PLEN_MAP[i].nidName; + } + } + LOGE("Invalid prime len:%d", pLen); + return NULL; +} diff --git a/plugin/openssl_plugin/common/src/ecc_openssl_common.c b/plugin/openssl_plugin/common/src/ecc_openssl_common.c new file mode 100644 index 0000000..69db8dd --- /dev/null +++ b/plugin/openssl_plugin/common/src/ecc_openssl_common.c @@ -0,0 +1,511 @@ +/* + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ecc_openssl_common.h" + +#include "securec.h" + +#include "log.h" +#include "memory.h" +#include "openssl_adapter.h" +#include "utils.h" + +HcfResult NewEcKeyPair(int32_t curveId, EC_KEY **returnEcKey) +{ + EC_KEY *ecKey = Openssl_EC_KEY_new_by_curve_name(curveId); + if (ecKey == NULL) { + LOGE("new ec key failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (Openssl_EC_KEY_generate_key(ecKey) <= 0) { + LOGE("generate ec key failed."); + Openssl_EC_KEY_free(ecKey); + return HCF_ERR_CRYPTO_OPERATION; + } + if (Openssl_EC_KEY_check_key(ecKey) <= 0) { + LOGE("check key fail."); + Openssl_EC_KEY_free(ecKey); + return HCF_ERR_CRYPTO_OPERATION; + } + *returnEcKey = ecKey; + return HCF_SUCCESS; +} + +void FreeCurveBigNum(BIGNUM *pStd, BIGNUM *bStd, BIGNUM *xStd, BIGNUM *yStd) +{ + Openssl_BN_free(pStd); + Openssl_BN_free(bStd); + Openssl_BN_free(xStd); + Openssl_BN_free(yStd); +} + +HcfResult NewGroupFromCurveGFp(const HcfEccCommParamsSpec *ecParams, EC_GROUP **ecGroup, BN_CTX *ctx) +{ + HcfResult ret = HCF_SUCCESS; + HcfECFieldFp *field = (HcfECFieldFp *)(ecParams->field); + BIGNUM *p = NULL; + BIGNUM *a = NULL; + BIGNUM *b = NULL; + EC_GROUP *group = NULL; + do { + if (BigIntegerToBigNum(&(field->p), &p) != HCF_SUCCESS || + BigIntegerToBigNum(&(ecParams->a), &a) != HCF_SUCCESS || + BigIntegerToBigNum(&(ecParams->b), &b) != HCF_SUCCESS) { + LOGE("BigInteger to BigNum failed"); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + group = Openssl_EC_GROUP_new_curve_GFp(p, a, b, ctx); + if (group == NULL) { + LOGE("Alloc group memory failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + } while (0); + Openssl_BN_free(p); + Openssl_BN_free(a); + Openssl_BN_free(b); + + if (ret == HCF_SUCCESS) { + *ecGroup = group; + return ret; + } + Openssl_EC_GROUP_free(group); + return ret; +} + +HcfResult SetEcPointToGroup(const HcfEccCommParamsSpec *ecParams, EC_GROUP *group, BN_CTX *ctx) +{ + HcfResult ret = HCF_SUCCESS; + BIGNUM *x = NULL; + BIGNUM *y = NULL; + BIGNUM *order = NULL; + EC_POINT *generator = NULL; + BIGNUM *cofactor = Openssl_BN_new(); + if (cofactor == NULL) { + LOGE("Alloc cofactor memory failed."); + return HCF_ERR_MALLOC; + } + do { + if (BigIntegerToBigNum(&(ecParams->g.x), &x) != HCF_SUCCESS || + BigIntegerToBigNum(&(ecParams->g.y), &y) != HCF_SUCCESS || + BigIntegerToBigNum(&(ecParams->n), &order) != HCF_SUCCESS || + !Openssl_BN_set_word(cofactor, (uint32_t)ecParams->h)) { + LOGE("BigInteger to BigNum failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + generator = Openssl_EC_POINT_new(group); + if (generator == NULL) { + LOGE("Alloc group memory failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (!Openssl_EC_POINT_set_affine_coordinates_GFp(group, generator, x, y, ctx)) { + LOGE("Openssl_EC_POINT_set_affine_coordinates_GFp failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + HcfPrintOpensslError(); + break; + } + + if (!Openssl_EC_GROUP_set_generator(group, generator, order, cofactor)) { + LOGE("Openssl_EC_GROUP_set_generator failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + HcfPrintOpensslError(); + break; + } + } while (0); + Openssl_BN_free(x); + Openssl_BN_free(y); + Openssl_BN_free(order); + Openssl_BN_free(cofactor); + Openssl_EC_POINT_free(generator); + return ret; +} + +HcfResult GenerateEcGroupWithParamsSpec(const HcfEccCommParamsSpec *ecParams, EC_GROUP **ecGroup) +{ + if (ecParams == NULL || ecGroup == NULL) { + LOGE("Invalid input parameters."); + return HCF_INVALID_PARAMS; + } + EC_GROUP *group = NULL; + BN_CTX *ctx = Openssl_BN_CTX_new(); + if (ctx == NULL) { + LOGE("Alloc ctx memory failed."); + return HCF_ERR_MALLOC; + } + HcfResult ret = NewGroupFromCurveGFp(ecParams, &group, ctx); + if (ret != HCF_SUCCESS) { + LOGE("New Ec group fail"); + Openssl_BN_CTX_free(ctx); + return ret; + } + ret = SetEcPointToGroup(ecParams, group, ctx); + if (ret != HCF_SUCCESS) { + Openssl_BN_CTX_free(ctx); + Openssl_EC_GROUP_free(group); + LOGE("Set Ec point fail"); + return ret; + } + *ecGroup = group; + return ret; +} + +HcfResult InitEcKeyByPubKey(const HcfPoint *pubKey, EC_KEY *ecKey) +{ + const EC_GROUP *group = Openssl_EC_KEY_get0_group(ecKey); + if (group == NULL) { + LOGE("Not find group from ecKey."); + return HCF_ERR_CRYPTO_OPERATION; + } + EC_POINT *point = Openssl_EC_POINT_new(group); + if (point == NULL) { + LOGE("New ec point failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + BIGNUM *pkX = NULL; + BIGNUM *pkY = NULL; + if (BigIntegerToBigNum(&(pubKey->x), &pkX) != HCF_SUCCESS || + BigIntegerToBigNum(&(pubKey->y), &pkY) != HCF_SUCCESS) { + LOGE("BigInteger to BigNum failed."); + Openssl_EC_POINT_free(point); + Openssl_BN_free(pkX); + Openssl_BN_free(pkY); + return HCF_ERR_CRYPTO_OPERATION; + } + + // only support fp point. + // can use EC_POINT_set_affine_coordinates() set x and y by group, deep copy. + int32_t ret = (int32_t)Openssl_EC_POINT_set_affine_coordinates_GFp(group, point, pkX, pkY, NULL); + Openssl_BN_free(pkX); + Openssl_BN_free(pkY); + + if (ret != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl_EC_POINT_set_affine_coordinates_GFp failed."); + Openssl_EC_POINT_free(point); + return HCF_ERR_CRYPTO_OPERATION; + } + ret = Openssl_EC_KEY_set_public_key(ecKey, point); + if (ret != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl_EC_KEY_set_public_key failed."); + Openssl_EC_POINT_free(point); + return HCF_ERR_CRYPTO_OPERATION; + } + Openssl_EC_POINT_free(point); + return HCF_SUCCESS; +} + +HcfResult InitEcKeyByPriKey(const HcfBigInteger *priKey, EC_KEY *ecKey) +{ + BIGNUM *sk = NULL; + if (BigIntegerToBigNum(priKey, &sk) != HCF_SUCCESS) { + LOGE("BigInteger to BigNum failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + int32_t ret = (int32_t)Openssl_EC_KEY_set_private_key(ecKey, sk); + if (ret != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl_EC_KEY_set_private_key failed."); + Openssl_BN_free(sk); + return HCF_ERR_CRYPTO_OPERATION; + } + Openssl_BN_free(sk); + return HCF_SUCCESS; +} + +HcfResult SetEcPubKeyFromPriKey(const HcfBigInteger *priKey, EC_KEY *ecKey) +{ + const EC_GROUP *group = Openssl_EC_KEY_get0_group(ecKey); + if (group == NULL) { + LOGE("Not find group from ecKey."); + return HCF_ERR_CRYPTO_OPERATION; + } + BIGNUM *sk = NULL; + if (BigIntegerToBigNum(priKey, &sk) != HCF_SUCCESS) { + LOGE("BigInteger to BigNum failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult ret = HCF_SUCCESS; + EC_POINT *point = Openssl_EC_POINT_new(group); + do { + if (point == NULL) { + LOGE("Openssl_EC_POINT_new failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (!Openssl_EC_POINT_mul(group, point, sk, NULL, NULL, NULL)) { + LOGE("Openssl_EC_POINT_new or Openssl_EC_POINT_mul failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (!Openssl_EC_KEY_set_public_key(ecKey, point)) { + LOGE("Openssl_EC_KEY_set_public_key failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + } + } while (0); + Openssl_EC_POINT_free(point); + Openssl_BN_free(sk); + return HCF_SUCCESS; +} + +HcfResult SetEcKey(const HcfPoint *pubKey, const HcfBigInteger *priKey, EC_KEY *ecKey) +{ + HcfResult ret = HCF_SUCCESS; + if (pubKey != NULL) { + ret = InitEcKeyByPubKey(pubKey, ecKey); + if (ret != HCF_SUCCESS) { + LOGE("InitEcKeyByPubKey failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + } + if (priKey != NULL) { + ret = InitEcKeyByPriKey(priKey, ecKey); + if (ret != HCF_SUCCESS) { + LOGE("InitEcKeyByPriKey failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (pubKey == NULL) { + ret = SetEcPubKeyFromPriKey(priKey, ecKey); + if (ret != HCF_SUCCESS) { + LOGE("SetEcPubKeyFromPriKey failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + } + } + return ret; +} + +HcfResult GetCurveGFp(const EC_GROUP *group, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) +{ + BIGNUM *p = Openssl_BN_new(); + BIGNUM *a = Openssl_BN_new(); + BIGNUM *b = Openssl_BN_new(); + if (p == NULL || a == NULL || b == NULL) { + LOGE("new BN failed."); + Openssl_BN_free(p); + Openssl_BN_free(a); + Openssl_BN_free(b); + return HCF_ERR_CRYPTO_OPERATION; + } + + if (Openssl_EC_GROUP_get_curve_GFp(group, p, a, b, NULL) != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl_EC_GROUP_get_curve_GFp failed."); + Openssl_BN_free(p); + Openssl_BN_free(a); + Openssl_BN_free(b); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfResult ret = HCF_INVALID_PARAMS; + switch (item) { + case ECC_FP_P_BN: + ret = BigNumToBigInteger(p, returnBigInteger); + break; + case ECC_A_BN: + ret = BigNumToBigInteger(a, returnBigInteger); + break; + case ECC_B_BN: + ret = BigNumToBigInteger(b, returnBigInteger); + break; + default: + LOGE("Invalid ecc key big number spec!"); + break; + } + Openssl_BN_free(p); + Openssl_BN_free(a); + Openssl_BN_free(b); + return ret; +} + +HcfResult GetGenerator(const EC_GROUP *group, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) +{ + const EC_POINT *generator = Openssl_EC_GROUP_get0_generator(group); + if (generator == NULL) { + LOGE("Openssl_EC_GROUP_get0_generator failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + + BIGNUM *gX = Openssl_BN_new(); + BIGNUM *gY = Openssl_BN_new(); + if (gX == NULL || gY == NULL) { + LOGE("new BN failed."); + Openssl_BN_free(gX); + Openssl_BN_free(gY); + return HCF_ERR_CRYPTO_OPERATION; + } + + if (Openssl_EC_POINT_get_affine_coordinates_GFp(group, generator, gX, gY, NULL) != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); + Openssl_BN_free(gX); + Openssl_BN_free(gY); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfResult ret = HCF_INVALID_PARAMS; + switch (item) { + case ECC_G_X_BN: + ret = BigNumToBigInteger(gX, returnBigInteger); + break; + case ECC_G_Y_BN: + ret = BigNumToBigInteger(gY, returnBigInteger); + break; + default: + LOGE("Invalid ecc key big number spec!"); + break; + } + Openssl_BN_free(gX); + Openssl_BN_free(gY); + return ret; +} + +HcfResult GetOrder(const EC_GROUP *group, HcfBigInteger *returnBigInteger) +{ + BIGNUM *order = Openssl_BN_new(); + if (order == NULL) { + LOGE("new BN failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + + if (Openssl_EC_GROUP_get_order(group, order, NULL) != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); + Openssl_BN_free(order); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfResult ret = BigNumToBigInteger(order, returnBigInteger); + Openssl_BN_free(order); + return ret; +} + +HcfResult GetCofactor(const EC_GROUP *group, int *returnCofactor) +{ + BIGNUM *cofactor = Openssl_BN_new(); + if (cofactor == NULL) { + LOGE("new BN failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + + if (Openssl_EC_GROUP_get_cofactor(group, cofactor, NULL) != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); + Openssl_BN_free(cofactor); + return HCF_ERR_CRYPTO_OPERATION; + } + + *returnCofactor = (int)(Openssl_BN_get_word(cofactor)); + // cofactor should not be zero. + if (*returnCofactor == 0) { + LOGE("Openssl_BN_get_word failed."); + Openssl_BN_free(cofactor); + return HCF_ERR_CRYPTO_OPERATION; + } + Openssl_BN_free(cofactor); + return HCF_SUCCESS; +} + +HcfResult GetFieldSize(const EC_GROUP *group, int32_t *fieldSize) +{ + *fieldSize = Openssl_EC_GROUP_get_degree(group); + if (*fieldSize == 0) { + LOGE("Openssl_EC_GROUP_get_degree failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + return HCF_SUCCESS; +} + +HcfResult GetFieldType(const HcfKey *self, const bool isPrivate, char **returnString) +{ + char *fieldType = NULL; + if (isPrivate) { + fieldType = ((HcfOpensslEccPriKey *)self)->fieldType; + } else { + fieldType = ((HcfOpensslEccPubKey *)self)->fieldType; + } + + if (fieldType == NULL) { + LOGE("No fieldType in EccPubKey struct."); + return HCF_INVALID_PARAMS; + } + + size_t len = HcfStrlen(fieldType); + if (!len) { + LOGE("fieldType is empty!"); + return HCF_INVALID_PARAMS; + } + *returnString = (char *)HcfMalloc(len + 1, 0); + if (*returnString == NULL) { + LOGE("Alloc returnString memory failed."); + return HCF_ERR_MALLOC; + } + (void)memcpy_s(*returnString, len, fieldType, len); + + return HCF_SUCCESS; +} + +HcfResult GetPubKeyXOrY(const EC_GROUP *group, const EC_POINT *point, const AsyKeySpecItem item, + HcfBigInteger *returnBigInteger) +{ + BIGNUM *pkX = Openssl_BN_new(); + BIGNUM *pkY = Openssl_BN_new(); + if (pkX == NULL || pkY == NULL) { + LOGE("new BN failed."); + Openssl_BN_free(pkX); + Openssl_BN_free(pkY); + return HCF_ERR_CRYPTO_OPERATION; + } + + if (Openssl_EC_POINT_get_affine_coordinates_GFp(group, point, pkX, pkY, NULL) != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); + Openssl_BN_free(pkX); + Openssl_BN_free(pkY); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfResult ret = HCF_INVALID_PARAMS; + switch (item) { + case ECC_PK_X_BN: + ret = BigNumToBigInteger(pkX, returnBigInteger); + break; + case ECC_PK_Y_BN: + ret = BigNumToBigInteger(pkY, returnBigInteger); + break; + default: + LOGE("Invalid ecc key big number spec!"); + break; + } + Openssl_BN_free(pkX); + Openssl_BN_free(pkY); + return ret; +} + +HcfResult GetPkSkBigInteger(const HcfKey *self, bool isPrivate, + const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) +{ + HcfResult ret = HCF_INVALID_PARAMS; + if (item == ECC_SK_BN) { + if (!isPrivate) { + LOGE("ecc pub key has no private key spec item"); + return ret; + } + ret = BigNumToBigInteger(Openssl_EC_KEY_get0_private_key(((HcfOpensslEccPriKey *)self)->ecKey), + returnBigInteger); + } else { + if (isPrivate) { + LOGE("ecc pri key cannot get pub key spec item"); + return ret; + } + ret = GetPubKeyXOrY(Openssl_EC_KEY_get0_group(((HcfOpensslEccPubKey *)self)->ecKey), + Openssl_EC_KEY_get0_public_key(((HcfOpensslEccPubKey *)self)->ecKey), item, returnBigInteger); + } + return ret; +} diff --git a/plugin/openssl_plugin/common/src/openssl_adapter.c b/plugin/openssl_plugin/common/src/openssl_adapter.c index 35446e7..b48c03e 100644 --- a/plugin/openssl_plugin/common/src/openssl_adapter.c +++ b/plugin/openssl_plugin/common/src/openssl_adapter.c @@ -336,6 +336,11 @@ int Openssl_EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t * return EVP_DigestSignFinal(ctx, sigret, siglen); } +int Openssl_EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen) +{ + return EVP_DigestSign(ctx, sig, siglen, tbs, tbslen); +} + int Openssl_EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) { return EVP_DigestVerifyInit(ctx, pctx, type, e, pkey); @@ -351,6 +356,12 @@ int Openssl_EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, siz return EVP_DigestVerifyFinal(ctx, sig, siglen); } +int Openssl_EVP_DigestVerify(EVP_MD_CTX *ctx, unsigned char *sig, size_t siglen, + const unsigned char *tbs, size_t tbslen) +{ + return EVP_DigestVerify(ctx, sig, siglen, tbs, tbslen); +} + int Openssl_EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) { return EVP_PKEY_sign_init(ctx); @@ -373,11 +384,37 @@ int Openssl_EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t return EVP_PKEY_verify(ctx, sig, siglen, tbs, tbslen); } +EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, + EVP_PKEY *pkey, const char *propquery) +{ + return EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propquery); +} + EVP_PKEY *Openssl_EVP_PKEY_new(void) { return EVP_PKEY_new(); } +EVP_PKEY *Openssl_EVP_PKEY_new_raw_public_key(int type, ENGINE *e, const unsigned char *pub, size_t len) +{ + return EVP_PKEY_new_raw_public_key(type, e, pub, len); +} + +EVP_PKEY *Openssl_EVP_PKEY_new_raw_private_key(int type, ENGINE *e, const unsigned char *pub, size_t len) +{ + return EVP_PKEY_new_raw_private_key(type, e, pub, len); +} + +int Openssl_EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, size_t *len) +{ + return EVP_PKEY_get_raw_public_key(pkey, pub, len); +} + +int Openssl_EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, size_t *len) +{ + return EVP_PKEY_get_raw_private_key(pkey, priv, len); +} + int Openssl_EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) { return EVP_PKEY_assign_EC_KEY(pkey, key); @@ -444,6 +481,41 @@ EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_id(int id, ENGINE *e) return EVP_PKEY_CTX_new_id(id, e); } +int Openssl_EVP_PKEY_base_id(EVP_PKEY *pkey) +{ + return EVP_PKEY_base_id(pkey); +} + +EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx, const char *name, const char *propquery) +{ + return EVP_PKEY_CTX_new_from_name(libctx, name, propquery); +} + +OSSL_PARAM Openssl_OSSL_PARAM_construct_utf8_string(const char *key, char *buf, size_t bsize) +{ + return OSSL_PARAM_construct_utf8_string(key, buf, bsize); +} + +OSSL_PARAM Openssl_OSSL_PARAM_construct_end(void) +{ + return OSSL_PARAM_construct_end(); +} + +int Openssl_EVP_PKEY_generate(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) +{ + return EVP_PKEY_generate(ctx, ppkey); +} + +OSSL_PARAM Openssl_OSSL_PARAM_construct_uint(const char *key, unsigned int *buf) +{ + return OSSL_PARAM_construct_uint(key, buf); +} + +OSSL_PARAM Openssl_OSSL_PARAM_construct_int(const char *key, int *buf) +{ + return OSSL_PARAM_construct_int(key, buf); +} + int Openssl_EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int id_len) { return EVP_PKEY_CTX_set1_id(ctx, id, id_len); @@ -459,6 +531,11 @@ int Openssl_EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits) return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits); } +int Openssl_EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params) +{ + return EVP_PKEY_CTX_set_params(ctx, params); +} + int Openssl_EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) { return EVP_PKEY_paramgen(ctx, ppkey); @@ -561,6 +638,36 @@ int Openssl_i2d_DSAPrivateKey(DSA *dsa, unsigned char **ppout) return i2d_DSAPrivateKey(dsa, ppout); } +int Openssl_EVP_PKEY_check(EVP_PKEY_CTX *ctx) +{ + return EVP_PKEY_check(ctx); +} + +EVP_PKEY *Openssl_EVP_PKEY_dup(EVP_PKEY *a) +{ + return EVP_PKEY_dup(a); +} + +EVP_PKEY *Openssl_d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length) +{ + return d2i_PUBKEY(a, pp, length); +} + +EVP_PKEY *Openssl_d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp, long length) +{ + return d2i_PrivateKey(type, a, pp, length); +} + +int Openssl_i2d_PUBKEY(EVP_PKEY *pkey, unsigned char **ppout) +{ + return i2d_PUBKEY(pkey, ppout); +} + +int Openssl_i2d_PrivateKey(EVP_PKEY *pkey, unsigned char **ppout) +{ + return i2d_PrivateKey(pkey, ppout); +} + RSA *Openssl_RSA_new(void) { return RSA_new(); @@ -1093,4 +1200,96 @@ EC_GROUP *Openssl_EC_GROUP_new_by_curve_name(int nid) int OPENSSL_EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) { return EVP_CIPHER_CTX_ctrl(ctx, type, arg, ptr); +} + +DH *Openssl_DH_new(void) +{ + return DH_new(); +} + +int Openssl_DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh) +{ + return DH_compute_key_padded(key, pub_key, dh); +} + +void Openssl_DH_free(DH *dh) +{ + if (dh != NULL) { + return DH_free(dh); + } +} + +int Openssl_DH_generate_key(DH *dh) +{ + return DH_generate_key(dh); +} + +const BIGNUM *Openssl_DH_get0_p(const DH *dh) +{ + return DH_get0_p(dh); +} + +const BIGNUM *Openssl_DH_get0_q(const DH *dh) +{ + return DH_get0_q(dh); +} + +const BIGNUM *Openssl_DH_get0_g(const DH *dh) +{ + return DH_get0_g(dh); +} + +long Openssl_DH_get_length(const DH *dh) +{ + return DH_get_length(dh); +} + +int Openssl_DH_set_length(DH *dh, long length) +{ + return DH_set_length(dh, length); +} + +const BIGNUM *Openssl_DH_get0_pub_key(const DH *dh) +{ + return DH_get0_pub_key(dh); +} + +const BIGNUM *Openssl_DH_get0_priv_key(const DH *dh) +{ + return DH_get0_priv_key(dh); +} + +int Openssl_EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) +{ + return EVP_PKEY_set1_DH(pkey, key); +} + +DH *Openssl_EVP_PKEY_get1_DH(EVP_PKEY *pkey) +{ + return EVP_PKEY_get1_DH(pkey); +} + +int Openssl_EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key) +{ + return EVP_PKEY_assign_DH(pkey, key); +} + +int Openssl_EVP_PKEY_CTX_set_dh_paramgen_prime_len(EVP_PKEY_CTX *ctx, int pbits) +{ + return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, pbits); +} + +int Openssl_DH_up_ref(DH *r) +{ + return DH_up_ref(r); +} + +int Openssl_DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) +{ + return DH_set0_pqg(dh, p, q, g); +} + +int Openssl_DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) +{ + return DH_set0_key(dh, pub_key, priv_key); } \ No newline at end of file diff --git a/plugin/openssl_plugin/common/src/openssl_common.c b/plugin/openssl_plugin/common/src/openssl_common.c index 3c94269..ccb72ef 100644 --- a/plugin/openssl_plugin/common/src/openssl_common.c +++ b/plugin/openssl_plugin/common/src/openssl_common.c @@ -136,7 +136,7 @@ HcfResult GetCurveNameByCurveId(int32_t curveId, char **curveName) return HCF_SUCCESS; } } - LOGE("invalid key size:%d", curveId); + LOGE("Invalid curve id:%d", curveId); return HCF_INVALID_PARAMS; } @@ -167,7 +167,7 @@ HcfResult GetAlgNameByBits(int32_t keyLen, char **algName) return HCF_SUCCESS; } } - LOGE("invalid key size:%d", keyLen); + LOGE("Invalid key size:%d", keyLen); return HCF_INVALID_PARAMS; } @@ -445,3 +445,54 @@ HcfResult BigNumToBigInteger(const BIGNUM *src, HcfBigInteger *dest) } return HCF_SUCCESS; } + +HcfResult KeyDerive(EVP_PKEY *priKey, EVP_PKEY *pubKey, HcfBlob *returnSecret) +{ + EVP_PKEY_CTX *ctx = Openssl_EVP_PKEY_CTX_new(priKey, NULL); + if (ctx == NULL) { + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult ret = HCF_ERR_CRYPTO_OPERATION; + do { + if (Openssl_EVP_PKEY_derive_init(ctx) != HCF_OPENSSL_SUCCESS) { + LOGE("Evp key derive init failed!"); + HcfPrintOpensslError(); + break; + } + if (Openssl_EVP_PKEY_derive_set_peer(ctx, pubKey) != HCF_OPENSSL_SUCCESS) { + LOGE("Evp key derive set peer failed!"); + HcfPrintOpensslError(); + break; + } + size_t maxLen; + if (Openssl_EVP_PKEY_derive(ctx, NULL, &maxLen) != HCF_OPENSSL_SUCCESS) { + LOGE("Evp key derive failed!"); + HcfPrintOpensslError(); + break; + } + uint8_t *secretData = (uint8_t *)HcfMalloc(maxLen, 0); + if (secretData == NULL) { + LOGE("Failed to allocate secretData memory!"); + ret = HCF_ERR_MALLOC; + break; + } + size_t actualLen = maxLen; + if (Openssl_EVP_PKEY_derive(ctx, secretData, &actualLen) != HCF_OPENSSL_SUCCESS) { + LOGE("Evp key derive failed!"); + HcfPrintOpensslError(); + HcfFree(secretData); + break; + } + if (actualLen > maxLen) { + LOGE("signature data too long."); + HcfFree(secretData); + break; + } + returnSecret->data = secretData; + returnSecret->len = actualLen; + ret = HCF_SUCCESS; + } while (0); + Openssl_EVP_PKEY_CTX_free(ctx); + return ret; +} diff --git a/plugin/openssl_plugin/crypto_operation/key_agreement/inc/dh_openssl.h b/plugin/openssl_plugin/crypto_operation/key_agreement/inc/dh_openssl.h new file mode 100644 index 0000000..bfa5adb --- /dev/null +++ b/plugin/openssl_plugin/crypto_operation/key_agreement/inc/dh_openssl.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_DH_OPENSSL_H +#define HCF_DH_OPENSSL_H + +#include "key_agreement_spi.h" +#include "params_parser.h" +#include "result.h" + +#ifdef __cplusplus +extern "C" { +#endif + +HcfResult HcfKeyAgreementSpiDhCreate(HcfKeyAgreementParams *params, HcfKeyAgreementSpi **returnObj); + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/plugin/openssl_plugin/crypto_operation/key_agreement/inc/x25519_openssl.h b/plugin/openssl_plugin/crypto_operation/key_agreement/inc/x25519_openssl.h new file mode 100644 index 0000000..b94fe44 --- /dev/null +++ b/plugin/openssl_plugin/crypto_operation/key_agreement/inc/x25519_openssl.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_X25519_OPENSSL_H +#define HCF_X25519_OPENSSL_H + +#include "key_agreement_spi.h" +#include "params_parser.h" +#include "result.h" + +#ifdef __cplusplus +extern "C" { +#endif + +HcfResult HcfKeyAgreementSpiX25519Create(HcfKeyAgreementParams *params, HcfKeyAgreementSpi **returnObj); + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/plugin/openssl_plugin/crypto_operation/key_agreement/src/dh_openssl.c b/plugin/openssl_plugin/crypto_operation/key_agreement/src/dh_openssl.c new file mode 100644 index 0000000..6f55055 --- /dev/null +++ b/plugin/openssl_plugin/crypto_operation/key_agreement/src/dh_openssl.c @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dh_openssl.h" +#include "dh_openssl_common.h" + +#include +#include + +#include "algorithm_parameter.h" +#include "openssl_adapter.h" +#include "openssl_class.h" +#include "openssl_common.h" +#include "log.h" +#include "memory.h" +#include "utils.h" + +typedef struct { + HcfKeyAgreementSpi base; +} HcfKeyAgreementSpiDhOpensslImpl; + +static const char *GetDhClass(void) +{ + return "HcfKeyAgreement.HcfKeyAgreementSpiDhOpensslImpl"; +} + +static void DestroyDh(HcfObjectBase *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return; + } + if (!IsClassMatch(self, GetDhClass())) { + LOGE("Invalid class of self."); + return; + } + HcfFree(self); +} + +static HcfResult EngineGenerateSecret(HcfKeyAgreementSpi *self, HcfPriKey *priKey, + HcfPubKey *pubKey, HcfBlob *returnSecret) +{ + if ((self == NULL) || (priKey == NULL) || (pubKey == NULL) || (returnSecret == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if ((!IsClassMatch((HcfObjectBase *)self, GetDhClass())) || + (!IsClassMatch((HcfObjectBase *)priKey, OPENSSL_DH_PRIKEY_CLASS)) || + (!IsClassMatch((HcfObjectBase *)pubKey, OPENSSL_DH_PUBKEY_CLASS))) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + EVP_PKEY *pubPKey = NewEvpPkeyByDh(((HcfOpensslDhPubKey *)pubKey)->pk, true); + if (pubPKey == NULL) { + LOGE("Failed to get public pkey."); + return HCF_ERR_CRYPTO_OPERATION; + } + EVP_PKEY *priPKey = NewEvpPkeyByDh(((HcfOpensslDhPriKey *)priKey)->sk, true); + if (priPKey == NULL) { + LOGE("Failed to get private pkey."); + Openssl_EVP_PKEY_free(pubPKey); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult res = KeyDerive(priPKey, pubPKey, returnSecret); + Openssl_EVP_PKEY_free(priPKey); + Openssl_EVP_PKEY_free(pubPKey); + return res; +} + +HcfResult HcfKeyAgreementSpiDhCreate(HcfKeyAgreementParams *params, HcfKeyAgreementSpi **returnObj) +{ + if ((params == NULL) || (returnObj == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + + HcfKeyAgreementSpiDhOpensslImpl *returnImpl = (HcfKeyAgreementSpiDhOpensslImpl *)HcfMalloc( + sizeof(HcfKeyAgreementSpiDhOpensslImpl), 0); + if (returnImpl == NULL) { + LOGE("Failed to allocate returnImpl memroy!"); + return HCF_ERR_MALLOC; + } + returnImpl->base.base.getClass = GetDhClass; + returnImpl->base.base.destroy = DestroyDh; + returnImpl->base.engineGenerateSecret = EngineGenerateSecret; + + *returnObj = (HcfKeyAgreementSpi *)returnImpl; + return HCF_SUCCESS; +} + diff --git a/plugin/openssl_plugin/crypto_operation/key_agreement/src/ecdh_openssl.c b/plugin/openssl_plugin/crypto_operation/key_agreement/src/ecdh_openssl.c index ba8af2d..442918f 100644 --- a/plugin/openssl_plugin/crypto_operation/key_agreement/src/ecdh_openssl.c +++ b/plugin/openssl_plugin/crypto_operation/key_agreement/src/ecdh_openssl.c @@ -71,54 +71,6 @@ static EVP_PKEY *NewPKeyByEccPriKey(HcfOpensslEccPriKey *privateKey) return res; } -static HcfResult EcdhDerive(EVP_PKEY *priPKey, EVP_PKEY *pubPKey, HcfBlob *returnSecret) -{ - EVP_PKEY_CTX *ctx = Openssl_EVP_PKEY_CTX_new(priPKey, NULL); - if (ctx == NULL) { - HcfPrintOpensslError(); - return HCF_ERR_CRYPTO_OPERATION; - } - if (Openssl_EVP_PKEY_derive_init(ctx) != HCF_OPENSSL_SUCCESS) { - HcfPrintOpensslError(); - Openssl_EVP_PKEY_CTX_free(ctx); - return HCF_ERR_CRYPTO_OPERATION; - } - if (Openssl_EVP_PKEY_derive_set_peer(ctx, pubPKey) != HCF_OPENSSL_SUCCESS) { - HcfPrintOpensslError(); - Openssl_EVP_PKEY_CTX_free(ctx); - return HCF_ERR_CRYPTO_OPERATION; - } - size_t maxLen; - if (Openssl_EVP_PKEY_derive(ctx, NULL, &maxLen) != HCF_OPENSSL_SUCCESS) { - HcfPrintOpensslError(); - Openssl_EVP_PKEY_CTX_free(ctx); - return HCF_ERR_CRYPTO_OPERATION; - } - uint8_t *secretData = (uint8_t *)HcfMalloc(maxLen, 0); - if (secretData == NULL) { - LOGE("Failed to allocate secretData memory!"); - Openssl_EVP_PKEY_CTX_free(ctx); - return HCF_ERR_MALLOC; - } - size_t actualLen = maxLen; - if (Openssl_EVP_PKEY_derive(ctx, secretData, &actualLen) != HCF_OPENSSL_SUCCESS) { - HcfPrintOpensslError(); - Openssl_EVP_PKEY_CTX_free(ctx); - HcfFree(secretData); - return HCF_ERR_CRYPTO_OPERATION; - } - Openssl_EVP_PKEY_CTX_free(ctx); - if (actualLen > maxLen) { - LOGE("signature data too long."); - HcfFree(secretData); - return HCF_ERR_CRYPTO_OPERATION; - } - - returnSecret->data = secretData; - returnSecret->len = actualLen; - return HCF_SUCCESS; -} - static const char *GetEcdhClass(void) { return "HcfKeyAgreement.HcfKeyAgreementSpiEcdhOpensslImpl"; @@ -160,7 +112,7 @@ static HcfResult EngineGenerateSecret(HcfKeyAgreementSpi *self, HcfPriKey *priKe return HCF_ERR_CRYPTO_OPERATION; } - HcfResult res = EcdhDerive(priPKey, pubPKey, returnSecret); + HcfResult res = KeyDerive(priPKey, pubPKey, returnSecret); Openssl_EVP_PKEY_free(priPKey); Openssl_EVP_PKEY_free(pubPKey); return res; diff --git a/plugin/openssl_plugin/crypto_operation/key_agreement/src/x25519_openssl.c b/plugin/openssl_plugin/crypto_operation/key_agreement/src/x25519_openssl.c new file mode 100644 index 0000000..e1fba82 --- /dev/null +++ b/plugin/openssl_plugin/crypto_operation/key_agreement/src/x25519_openssl.c @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "x25519_openssl.h" + +#include +#include + +#include "algorithm_parameter.h" +#include "openssl_adapter.h" +#include "openssl_class.h" +#include "openssl_common.h" +#include "log.h" +#include "memory.h" +#include "utils.h" + +typedef struct { + HcfKeyAgreementSpi base; +} HcfKeyAgreementSpiX25519OpensslImpl; + +static const char *GetX25519Class(void) +{ + return "HcfKeyAgreement.HcfKeyAgreementSpiX25519OpensslImpl"; +} + +static void DestroyX25519(HcfObjectBase *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return; + } + if (!IsClassMatch(self, GetX25519Class())) { + LOGE("Invalid class of self."); + return; + } + HcfFree(self); +} + +static HcfResult EngineGenerateSecret(HcfKeyAgreementSpi *self, HcfPriKey *priKey, + HcfPubKey *pubKey, HcfBlob *returnSecret) +{ + if ((self == NULL) || (priKey == NULL) || (pubKey == NULL) || (returnSecret == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if ((!IsClassMatch((HcfObjectBase *)self, GetX25519Class())) || + (!IsClassMatch((HcfObjectBase *)priKey, OPENSSL_ALG25519_PRIKEY_CLASS)) || + (!IsClassMatch((HcfObjectBase *)pubKey, OPENSSL_ALG25519_PUBKEY_CLASS))) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + EVP_PKEY *pubPKey = Openssl_EVP_PKEY_dup(((HcfOpensslAlg25519PubKey *)pubKey)->pkey); + if (pubPKey == NULL) { + LOGE("Failed to dup public pkey."); + return HCF_ERR_CRYPTO_OPERATION; + } + EVP_PKEY *priPKey = Openssl_EVP_PKEY_dup(((HcfOpensslAlg25519PriKey *)priKey)->pkey); + if (priPKey == NULL) { + LOGE("Failed to dup private pkey."); + Openssl_EVP_PKEY_free(pubPKey); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult res = KeyDerive(priPKey, pubPKey, returnSecret); + Openssl_EVP_PKEY_free(priPKey); + Openssl_EVP_PKEY_free(pubPKey); + return res; +} + +HcfResult HcfKeyAgreementSpiX25519Create(HcfKeyAgreementParams *params, HcfKeyAgreementSpi **returnObj) +{ + if ((params == NULL) || (returnObj == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + + HcfKeyAgreementSpiX25519OpensslImpl *returnImpl = (HcfKeyAgreementSpiX25519OpensslImpl *)HcfMalloc( + sizeof(HcfKeyAgreementSpiX25519OpensslImpl), 0); + if (returnImpl == NULL) { + LOGE("Failed to allocate returnImpl memroy!"); + return HCF_ERR_MALLOC; + } + returnImpl->base.base.getClass = GetX25519Class; + returnImpl->base.base.destroy = DestroyX25519; + returnImpl->base.engineGenerateSecret = EngineGenerateSecret; + + *returnObj = (HcfKeyAgreementSpi *)returnImpl; + return HCF_SUCCESS; +} + diff --git a/plugin/openssl_plugin/crypto_operation/signature/inc/ed25519_openssl.h b/plugin/openssl_plugin/crypto_operation/signature/inc/ed25519_openssl.h new file mode 100644 index 0000000..0085962 --- /dev/null +++ b/plugin/openssl_plugin/crypto_operation/signature/inc/ed25519_openssl.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_ED25519_OPENSSL_H +#define HCF_ED25519_OPENSSL_H + +#include "signature_spi.h" +#include "params_parser.h" +#include "result.h" + +#ifdef __cplusplus +extern "C" { +#endif + +HcfResult HcfSignSpiEd25519Create(HcfSignatureParams *params, HcfSignSpi **returnObj); +HcfResult HcfVerifySpiEd25519Create(HcfSignatureParams *params, HcfVerifySpi **returnObj); + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/plugin/openssl_plugin/crypto_operation/signature/src/ed25519_openssl.c b/plugin/openssl_plugin/crypto_operation/signature/src/ed25519_openssl.c new file mode 100644 index 0000000..95f99f0 --- /dev/null +++ b/plugin/openssl_plugin/crypto_operation/signature/src/ed25519_openssl.c @@ -0,0 +1,358 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ed25519_openssl.h" + +#include +#include + +#include "securec.h" + +#include "algorithm_parameter.h" +#include "openssl_adapter.h" +#include "openssl_class.h" +#include "openssl_common.h" +#include "log.h" +#include "memory.h" +#include "utils.h" + +#define OPENSSL_ED25519_SIGN_CLASS "OPENSSL.ED25519.SIGN" +#define OPENSSL_ED25519_VERIFY_CLASS "OPENSSL.ED25519.VERIFY" + +typedef struct { + HcfSignSpi base; + + EVP_MD_CTX *mdCtx; + + CryptoStatus status; +} HcfSignSpiEd25519OpensslImpl; + +typedef struct { + HcfVerifySpi base; + + EVP_MD_CTX *mdCtx; + + CryptoStatus status; +} HcfVerifySpiEd25519OpensslImpl; + +static const char *GetEd25519SignClass(void) +{ + return OPENSSL_ED25519_SIGN_CLASS; +} + +static const char *GetEd25519VerifyClass(void) +{ + return OPENSSL_ED25519_VERIFY_CLASS; +} + +static void DestroyEd25519Sign(HcfObjectBase *self) +{ + if (self == NULL || !IsClassMatch(self, self->getClass())) { + return; + } + HcfSignSpiEd25519OpensslImpl *impl = (HcfSignSpiEd25519OpensslImpl *)self; + if (impl->mdCtx != NULL) { + Openssl_EVP_MD_CTX_free(impl->mdCtx); + impl->mdCtx = NULL; + } + HcfFree(impl); +} + +static void DestroyEd25519Verify(HcfObjectBase *self) +{ + if (self == NULL || !IsClassMatch(self, self->getClass())) { + return; + } + HcfVerifySpiEd25519OpensslImpl *impl = (HcfVerifySpiEd25519OpensslImpl *)self; + if (impl->mdCtx != NULL) { + Openssl_EVP_MD_CTX_free(impl->mdCtx); + impl->mdCtx = NULL; + } + HcfFree(impl); +} + +static HcfResult EngineSignInit(HcfSignSpi *self, HcfParamsSpec *params, HcfPriKey *privateKey) +{ + (void)params; + if ((self == NULL) || (privateKey == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if ((!IsClassMatch((HcfObjectBase *)self, self->base.getClass())) || + (!IsClassMatch((HcfObjectBase *)privateKey, OPENSSL_ALG25519_PRIKEY_CLASS))) { + return HCF_INVALID_PARAMS; + } + + HcfSignSpiEd25519OpensslImpl *impl = (HcfSignSpiEd25519OpensslImpl *)self; + if (impl->status != UNINITIALIZED) { + LOGE("Repeated initialization is not allowed."); + return HCF_INVALID_PARAMS; + } + EVP_PKEY *pKey = Openssl_EVP_PKEY_dup(((HcfOpensslAlg25519PriKey *)privateKey)->pkey); + if (pKey == NULL) { + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + if (Openssl_EVP_DigestSignInit(impl->mdCtx, NULL, NULL, NULL, pKey) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + Openssl_EVP_PKEY_free(pKey); + return HCF_ERR_CRYPTO_OPERATION; + } + Openssl_EVP_PKEY_free(pKey); + impl->status = INITIALIZED; + return HCF_SUCCESS; +} + +static HcfResult EngineSignUpdate(HcfSignSpi *self, HcfBlob *data) +{ + (void)self; + (void)data; + return HCF_INVALID_PARAMS; +} + +static HcfResult EngineSignDoFinal(HcfSignSpi *self, HcfBlob *data, HcfBlob *returnSignatureData) +{ + if ((self == NULL) || (returnSignatureData == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, self->base.getClass())) { + return HCF_INVALID_PARAMS; + } + if (!IsBlobValid(data)) { + LOGE("Invalid sign data."); + return HCF_INVALID_PARAMS; + } + HcfSignSpiEd25519OpensslImpl *impl = (HcfSignSpiEd25519OpensslImpl *)self; + if (impl->status != INITIALIZED) { + LOGE("The message has not been initialized."); + return HCF_INVALID_PARAMS; + } + size_t siglen; + if (Openssl_EVP_DigestSign(impl->mdCtx, NULL, &siglen, data->data, data->len) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + uint8_t *signatureData = (uint8_t *)HcfMalloc(siglen, 0); + if (signatureData == NULL) { + LOGE("Failed to allocate signatureData memory!"); + return HCF_ERR_MALLOC; + } + if (Openssl_EVP_DigestSign(impl->mdCtx, signatureData, &siglen, data->data, data->len) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + HcfFree(signatureData); + return HCF_ERR_CRYPTO_OPERATION; + } + returnSignatureData->data = signatureData; + returnSignatureData->len = (uint32_t)siglen; + return HCF_SUCCESS; +} + +static HcfResult EngineVerifyInit(HcfVerifySpi *self, HcfParamsSpec *params, HcfPubKey *publicKey) +{ + (void)params; + if ((self == NULL) || (publicKey == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if ((!IsClassMatch((HcfObjectBase *)self, self->base.getClass())) || + (!IsClassMatch((HcfObjectBase *)publicKey, OPENSSL_ALG25519_PUBKEY_CLASS))) { + return HCF_INVALID_PARAMS; + } + + HcfVerifySpiEd25519OpensslImpl *impl = (HcfVerifySpiEd25519OpensslImpl *)self; + if (impl->status != UNINITIALIZED) { + LOGE("Repeated initialization is not allowed."); + return HCF_INVALID_PARAMS; + } + EVP_PKEY *pKey = Openssl_EVP_PKEY_dup(((HcfOpensslAlg25519PubKey *)publicKey)->pkey); + if (pKey == NULL) { + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + if (Openssl_EVP_DigestVerifyInit(impl->mdCtx, NULL, NULL, NULL, pKey) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + Openssl_EVP_PKEY_free(pKey); + return HCF_ERR_CRYPTO_OPERATION; + } + Openssl_EVP_PKEY_free(pKey); + impl->status = INITIALIZED; + return HCF_SUCCESS; +} + +static HcfResult EngineVerifyUpdate(HcfVerifySpi *self, HcfBlob *data) +{ + (void)self; + (void)data; + return HCF_INVALID_PARAMS; +} + +static bool EngineVerifyDoFinal(HcfVerifySpi *self, HcfBlob *data, HcfBlob *signatureData) +{ + if ((self == NULL) || (!IsBlobValid(signatureData))) { + LOGE("Invalid input parameter."); + return false; + } + if (!IsClassMatch((HcfObjectBase *)self, self->base.getClass())) { + return false; + } + if (!IsBlobValid(data)) { + LOGE("Invalid verify data."); + return false; + } + HcfVerifySpiEd25519OpensslImpl *impl = (HcfVerifySpiEd25519OpensslImpl *)self; + if (impl->status != INITIALIZED) { + LOGE("The message has not been initialized."); + return false; + } + if (Openssl_EVP_DigestVerify(impl->mdCtx, signatureData->data, signatureData->len, + data->data, data->len) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + return false; + } + return true; +} + +static HcfResult EngineGetSignSpecString(HcfSignSpi *self, SignSpecItem item, char **returnString) +{ + (void)self; + (void)item; + (void)returnString; + return HCF_NOT_SUPPORT; +} + +static HcfResult EngineSetSignSpecUint8Array(HcfSignSpi *self, SignSpecItem item, HcfBlob userId) +{ + (void)self; + (void)item; + (void)userId; + return HCF_NOT_SUPPORT; +} + +static HcfResult EngineGetSignSpecInt(HcfSignSpi *self, SignSpecItem item, int32_t *returnInt) +{ + (void)self; + (void)item; + (void)returnInt; + return HCF_NOT_SUPPORT; +} + +static HcfResult EngineSetSignSpecInt(HcfSignSpi *self, SignSpecItem item, int32_t saltLen) +{ + (void)self; + (void)item; + (void)saltLen; + return HCF_NOT_SUPPORT; +} + +static HcfResult EngineGetVerifySpecString(HcfVerifySpi *self, SignSpecItem item, char **returnString) +{ + (void)self; + (void)item; + (void)returnString; + return HCF_NOT_SUPPORT; +} + +static HcfResult EngineSetVerifySpecUint8Array(HcfVerifySpi *self, SignSpecItem item, HcfBlob userId) +{ + (void)self; + (void)item; + (void)userId; + return HCF_NOT_SUPPORT; +} + +static HcfResult EngineGetVerifySpecInt(HcfVerifySpi *self, SignSpecItem item, int32_t *returnInt) +{ + (void)self; + (void)item; + (void)returnInt; + return HCF_NOT_SUPPORT; +} + +static HcfResult EngineSetVerifySpecInt(HcfVerifySpi *self, SignSpecItem item, int32_t saltLen) +{ + (void)self; + (void)item; + (void)saltLen; + return HCF_NOT_SUPPORT; +} + +HcfResult HcfSignSpiEd25519Create(HcfSignatureParams *params, HcfSignSpi **returnObj) +{ + if ((params == NULL) || (returnObj == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + + HcfSignSpiEd25519OpensslImpl *returnImpl = (HcfSignSpiEd25519OpensslImpl *)HcfMalloc( + sizeof(HcfSignSpiEd25519OpensslImpl), 0); + if (returnImpl == NULL) { + LOGE("Failed to allocate returnImpl memroy!"); + return HCF_ERR_MALLOC; + } + returnImpl->base.base.getClass = GetEd25519SignClass; + returnImpl->base.base.destroy = DestroyEd25519Sign; + returnImpl->base.engineInit = EngineSignInit; + returnImpl->base.engineUpdate = EngineSignUpdate; + returnImpl->base.engineSign = EngineSignDoFinal; + returnImpl->base.engineGetSignSpecString = EngineGetSignSpecString; + returnImpl->base.engineSetSignSpecUint8Array = EngineSetSignSpecUint8Array; + returnImpl->base.engineGetSignSpecInt = EngineGetSignSpecInt; + returnImpl->base.engineSetSignSpecInt = EngineSetSignSpecInt; + returnImpl->status = UNINITIALIZED; + returnImpl->mdCtx = Openssl_EVP_MD_CTX_new(); + if (returnImpl->mdCtx == NULL) { + LOGE("Failed to allocate mdCtx memory!"); + HcfFree(returnImpl); + return HCF_ERR_MALLOC; + } + + *returnObj = (HcfSignSpi *)returnImpl; + return HCF_SUCCESS; +} + +HcfResult HcfVerifySpiEd25519Create(HcfSignatureParams *params, HcfVerifySpi **returnObj) +{ + if ((params == NULL) || (returnObj == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + + HcfVerifySpiEd25519OpensslImpl *returnImpl = (HcfVerifySpiEd25519OpensslImpl *)HcfMalloc( + sizeof(HcfVerifySpiEd25519OpensslImpl), 0); + if (returnImpl == NULL) { + LOGE("Failed to allocate returnImpl memroy!"); + return HCF_ERR_MALLOC; + } + returnImpl->base.base.getClass = GetEd25519VerifyClass; + returnImpl->base.base.destroy = DestroyEd25519Verify; + returnImpl->base.engineInit = EngineVerifyInit; + returnImpl->base.engineUpdate = EngineVerifyUpdate; + returnImpl->base.engineVerify = EngineVerifyDoFinal; + returnImpl->base.engineGetVerifySpecString = EngineGetVerifySpecString; + returnImpl->base.engineSetVerifySpecUint8Array = EngineSetVerifySpecUint8Array; + returnImpl->base.engineGetVerifySpecInt = EngineGetVerifySpecInt; + returnImpl->base.engineSetVerifySpecInt = EngineSetVerifySpecInt; + returnImpl->status = UNINITIALIZED; + returnImpl->mdCtx = Openssl_EVP_MD_CTX_new(); + if (returnImpl->mdCtx == NULL) { + LOGE("Failed to allocate mdCtx memory!"); + HcfFree(returnImpl); + return HCF_ERR_MALLOC; + } + + *returnObj = (HcfVerifySpi *)returnImpl; + return HCF_SUCCESS; +} diff --git a/plugin/openssl_plugin/key/asy_key_generator/inc/alg_25519_asy_key_generator_openssl.h b/plugin/openssl_plugin/key/asy_key_generator/inc/alg_25519_asy_key_generator_openssl.h new file mode 100644 index 0000000..b996227 --- /dev/null +++ b/plugin/openssl_plugin/key/asy_key_generator/inc/alg_25519_asy_key_generator_openssl.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_ALG_25519_ASY_KEY_GENERATOR_OPENSSL_H +#define HCF_ALG_25519_ASY_KEY_GENERATOR_OPENSSL_H + +#include "asy_key_generator_spi.h" +#include "params_parser.h" +#include "result.h" + +#ifdef __cplusplus +extern "C" { +#endif + +HcfResult HcfAsyKeyGeneratorSpiEd25519Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnObj); + +HcfResult HcfAsyKeyGeneratorSpiX25519Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnObj); + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/plugin/openssl_plugin/key/asy_key_generator/inc/dh_asy_key_generator_openssl.h b/plugin/openssl_plugin/key/asy_key_generator/inc/dh_asy_key_generator_openssl.h new file mode 100644 index 0000000..dd1d938 --- /dev/null +++ b/plugin/openssl_plugin/key/asy_key_generator/inc/dh_asy_key_generator_openssl.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_DH_ASY_KEY_GENERATOR_OPENSSL_H +#define HCF_DH_ASY_KEY_GENERATOR_OPENSSL_H + +#include "asy_key_generator_spi.h" +#include "params_parser.h" +#include "result.h" + +#ifdef __cplusplus +extern "C" { +#endif + +HcfResult HcfAsyKeyGeneratorSpiDhCreate(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **generator); + +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/plugin/openssl_plugin/key/asy_key_generator/inc/dh_common_param_spec_generator_openssl.h b/plugin/openssl_plugin/key/asy_key_generator/inc/dh_common_param_spec_generator_openssl.h new file mode 100644 index 0000000..3bbd300 --- /dev/null +++ b/plugin/openssl_plugin/key/asy_key_generator/inc/dh_common_param_spec_generator_openssl.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_DH_COMMON_PARAM_SPEC_GENERATOR_OPENSSL_H +#define HCF_DH_COMMON_PARAM_SPEC_GENERATOR_OPENSSL_H + +#include "dh_key_util_spi.h" +#include "params_parser.h" +#include "result.h" + +#ifdef __cplusplus +extern "C" { +#endif + +HcfResult HcfDhCommonParamSpecCreate(uint32_t pLen, int32_t skLen, HcfDhCommParamsSpecSpi **returnCommonParamSpec); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c new file mode 100644 index 0000000..8a80c20 --- /dev/null +++ b/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c @@ -0,0 +1,983 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "alg_25519_asy_key_generator_openssl.h" + +#include "securec.h" + +#include +#include + +#include "detailed_alg_25519_key_params.h" +#include "log.h" +#include "memory.h" +#include "openssl_adapter.h" +#include "openssl_class.h" +#include "openssl_common.h" +#include "utils.h" + +#define OPENSSL_ED25519_GENERATOR_CLASS "OPENSSL.ED25519.KEYGENERATOR" +#define OPENSSL_X25519_GENERATOR_CLASS "OPENSSL.X25519.KEYGENERATOR" +#define OPENSSL_ALG_25519_PUBKEY_FORMAT "X.509" +#define OPENSSL_ALG_25519_PRIKEY_FORMAT "PKCS#8" +#define ALGORITHM_NAME_ALG25519 "Alg25519" +#define ALGORITHM_NAME_ED25519 "Ed25519" +#define ALGORITHM_NAME_X25519 "X25519" + +typedef struct { + HcfAsyKeyGeneratorSpi base; +} HcfAsyKeyGeneratorSpiAlg25519OpensslImpl; + +static const char *GetEd25519KeyGeneratorSpiClass(void) +{ + return OPENSSL_ED25519_GENERATOR_CLASS; +} + +static const char *GetX25519KeyGeneratorSpiClass(void) +{ + return OPENSSL_X25519_GENERATOR_CLASS; +} + +static const char *GetAlg25519KeyPairClass(void) +{ + return OPENSSL_ALG25519_KEYPAIR_CLASS; +} + +static const char *GetAlg25519PubKeyClass(void) +{ + return OPENSSL_ALG25519_PUBKEY_CLASS; +} + +static const char *GetAlg25519PriKeyClass(void) +{ + return OPENSSL_ALG25519_PRIKEY_CLASS; +} + +static void DestroyAlg25519KeyGeneratorSpiImpl(HcfObjectBase *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return; + } + if (!IsClassMatch(self, GetEd25519KeyGeneratorSpiClass()) && + !IsClassMatch(self, GetX25519KeyGeneratorSpiClass())) { + LOGE("Invalid class of self."); + return; + } + HcfFree(self); +} + +static void DestroyAlg25519PubKey(HcfObjectBase *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return; + } + if (!IsClassMatch(self, GetAlg25519PubKeyClass())) { + LOGE("Invalid class of self."); + return; + } + HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self; + Openssl_EVP_PKEY_free(impl->pkey); + impl->pkey = NULL; + HcfFree(impl); +} + +static void DestroyAlg25519PriKey(HcfObjectBase *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return; + } + if (!IsClassMatch(self, GetAlg25519PriKeyClass())) { + LOGE("Invalid class of self."); + return; + } + HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self; + Openssl_EVP_PKEY_free(impl->pkey); + impl->pkey = NULL; + HcfFree(impl); +} + +static void DestroyAlg25519KeyPair(HcfObjectBase *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return; + } + if (!IsClassMatch(self, GetAlg25519KeyPairClass())) { + LOGE("Invalid class of self."); + return; + } + HcfOpensslAlg25519KeyPair *impl = (HcfOpensslAlg25519KeyPair *)self; + DestroyAlg25519PubKey((HcfObjectBase *)impl->base.pubKey); + impl->base.pubKey = NULL; + DestroyAlg25519PriKey((HcfObjectBase *)impl->base.priKey); + impl->base.priKey = NULL; + HcfFree(self); +} + +static const char *GetAlg25519PubKeyAlgorithm(HcfKey *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return NULL; + } + if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) { + LOGE("Invalid class of self."); + return NULL; + } + return ALGORITHM_NAME_ALG25519; +} + +static const char *GetAlg25519PriKeyAlgorithm(HcfKey *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return NULL; + } + if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) { + LOGE("Invalid class of self."); + return NULL; + } + return ALGORITHM_NAME_ALG25519; +} + +static HcfResult GetAlg25519PubKeyEncoded(HcfKey *self, HcfBlob *returnBlob) +{ + if ((self == NULL) || (returnBlob == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self; + if (impl->pkey == NULL) { + LOGE("pkey is NULL."); + return HCF_INVALID_PARAMS; + } + unsigned char *returnData = NULL; + int len = Openssl_i2d_PUBKEY(impl->pkey, &returnData); + if (len <= 0) { + LOGE("Call i2d_PUBKEY failed"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + returnBlob->data = returnData; + returnBlob->len = len; + return HCF_SUCCESS; +} + +static HcfResult GetAlg25519PriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) +{ + if ((self == NULL) || (returnBlob == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self; + if (impl->pkey == NULL) { + LOGE("pkey is NULL."); + return HCF_INVALID_PARAMS; + } + unsigned char *returnData = NULL; + int len = Openssl_i2d_PrivateKey(impl->pkey, &returnData); + if (len <= 0) { + LOGE("Call i2d_PrivateKey failed"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + returnBlob->data = returnData; + returnBlob->len = len; + return HCF_SUCCESS; +} + +static const char *GetAlg25519PubKeyFormat(HcfKey *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return NULL; + } + if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) { + LOGE("Invalid class of self."); + return NULL; + } + return OPENSSL_ALG_25519_PUBKEY_FORMAT; +} + +static const char *GetAlg25519PriKeyFormat(HcfKey *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return NULL; + } + if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) { + LOGE("Invalid class of self."); + return NULL; + } + return OPENSSL_ALG_25519_PRIKEY_FORMAT; +} + +static HcfResult GetAlg25519PubKey(EVP_PKEY *pubKey, HcfBigInteger *returnBigInteger) +{ + size_t len = 0; + if (!Openssl_EVP_PKEY_get_raw_public_key(pubKey, NULL, &len)) { + LOGE("Get len failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + returnBigInteger->data = (unsigned char *)HcfMalloc(len, 0); + if (returnBigInteger->data == NULL) { + LOGE("Failed to allocate returnBigInteger memory."); + return HCF_ERR_MALLOC; + } + if (!Openssl_EVP_PKEY_get_raw_public_key(pubKey, returnBigInteger->data, &len)) { + LOGE("Get data failed."); + HcfFree(returnBigInteger->data); + returnBigInteger->data = NULL; + return HCF_ERR_CRYPTO_OPERATION; + } + returnBigInteger->len = len; + return HCF_SUCCESS; +} + +static HcfResult CheckEvpKeyTypeFromAlg25519PubKey(EVP_PKEY *alg25519Pk, const AsyKeySpecItem item) +{ + int type = Openssl_EVP_PKEY_base_id(alg25519Pk); + if (type != EVP_PKEY_ED25519 && type != EVP_PKEY_X25519) { + LOGE("Invalid pkey type."); + return HCF_INVALID_PARAMS; + } + if ((type == EVP_PKEY_ED25519 && item != ED25519_PK_BN) || + (type == EVP_PKEY_X25519 && item != X25519_PK_BN)) { + LOGE("Invalid AsyKeySpecItem."); + return HCF_INVALID_PARAMS; + } + return HCF_SUCCESS; +} + +static HcfResult CheckEvpKeyTypeFromAlg25519PriKey(EVP_PKEY *alg25519Sk, const AsyKeySpecItem item) +{ + int type = Openssl_EVP_PKEY_base_id(alg25519Sk); + if (type != EVP_PKEY_ED25519 && type != EVP_PKEY_X25519) { + LOGE("Invalid pkey type."); + return HCF_INVALID_PARAMS; + } + if ((type == EVP_PKEY_ED25519 && item != ED25519_SK_BN) || + (type == EVP_PKEY_X25519 && item != X25519_SK_BN)) { + LOGE("Invalid AsyKeySpecItem."); + return HCF_INVALID_PARAMS; + } + return HCF_SUCCESS; +} + +static HcfResult GetBigIntegerSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item, + HcfBigInteger *returnBigInteger) +{ + if (self == NULL || returnBigInteger == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + HcfResult ret = HCF_INVALID_PARAMS; + HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self; + EVP_PKEY *alg25519Pk = impl->pkey; + if (alg25519Pk == NULL) { + LOGE("pKey is null."); + return HCF_INVALID_PARAMS; + } + if (CheckEvpKeyTypeFromAlg25519PubKey(alg25519Pk, item) != HCF_SUCCESS) { + LOGE("Check pKey type failed."); + return HCF_INVALID_PARAMS; + } + if (item == ED25519_PK_BN || item == X25519_PK_BN) { + ret = GetAlg25519PubKey(alg25519Pk, returnBigInteger); + } else { + LOGE("Input item is invalid"); + } + return ret; +} + +static HcfResult GetAlg25519PriKey(EVP_PKEY *priKey, HcfBigInteger *returnBigInteger) +{ + size_t len = 0; + if (!Openssl_EVP_PKEY_get_raw_private_key(priKey, NULL, &len)) { + LOGE("Get len failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + returnBigInteger->data = (unsigned char *)HcfMalloc(len, 0); + if (returnBigInteger->data == NULL) { + LOGE("Failed to allocate returnBigInteger memory."); + return HCF_ERR_MALLOC; + } + if (!Openssl_EVP_PKEY_get_raw_private_key(priKey, returnBigInteger->data, &len)) { + LOGE("Get data failed."); + HcfFree(returnBigInteger->data); + returnBigInteger->data = NULL; + return HCF_ERR_CRYPTO_OPERATION; + } + returnBigInteger->len = len; + return HCF_SUCCESS; +} + +static HcfResult GetBigIntegerSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item, + HcfBigInteger *returnBigInteger) +{ + if (self == NULL || returnBigInteger == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + HcfResult ret = HCF_INVALID_PARAMS; + HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self; + EVP_PKEY *alg25519Sk = impl->pkey; + if (alg25519Sk == NULL) { + LOGE("pKey is null."); + return HCF_INVALID_PARAMS; + } + if (CheckEvpKeyTypeFromAlg25519PriKey(alg25519Sk, item) != HCF_SUCCESS) { + LOGE("Check pKey type failed."); + return HCF_INVALID_PARAMS; + } + if (item == ED25519_SK_BN || item == X25519_SK_BN) { + ret = GetAlg25519PriKey(alg25519Sk, returnBigInteger); + } else { + LOGE("Input item is invalid"); + } + return ret; +} + +static HcfResult GetIntSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt) +{ + (void)self; + (void)returnInt; + return HCF_NOT_SUPPORT; +} + +static HcfResult GetIntSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt) +{ + (void)self; + (void)returnInt; + return HCF_NOT_SUPPORT; +} + +static HcfResult GetStrSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString) +{ + (void)self; + (void)returnString; + return HCF_NOT_SUPPORT; +} + +static HcfResult GetStrSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString) +{ + (void)self; + (void)returnString; + return HCF_NOT_SUPPORT; +} + +static void ClearAlg25519PriKeyMem(HcfPriKey *self) +{ + if (self == NULL) { + LOGE("Invalid params."); + return; + } + if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) { + LOGE("Invalid class of self."); + return; + } + HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self; + Openssl_EVP_PKEY_free(impl->pkey); + impl->pkey = NULL; +} + +static HcfResult GenerateAlg25519EvpKey(int type, EVP_PKEY **ppkey) +{ + EVP_PKEY_CTX *paramsCtx = NULL; + HcfResult ret = HCF_SUCCESS; + do { + paramsCtx = Openssl_EVP_PKEY_CTX_new_id(type, NULL); + if (paramsCtx == NULL) { + LOGE("Create params ctx failed."); + ret = HCF_ERR_MALLOC; + break; + } + if (Openssl_EVP_PKEY_keygen_init(paramsCtx) != HCF_OPENSSL_SUCCESS) { + LOGE("Key ctx generate init failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_keygen(paramsCtx, ppkey) != HCF_OPENSSL_SUCCESS) { + LOGE("Generate pkey failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + } while (0); + if (paramsCtx != NULL) { + Openssl_EVP_PKEY_CTX_free(paramsCtx); + } + return ret; +} + +static void FillOpensslAlg25519PubKeyFunc(HcfOpensslAlg25519PubKey *pk) +{ + pk->base.base.base.destroy = DestroyAlg25519PubKey; + pk->base.base.base.getClass = GetAlg25519PubKeyClass; + pk->base.base.getAlgorithm = GetAlg25519PubKeyAlgorithm; + pk->base.base.getEncoded = GetAlg25519PubKeyEncoded; + pk->base.base.getFormat = GetAlg25519PubKeyFormat; + pk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromAlg25519PubKey; + pk->base.getAsyKeySpecInt = GetIntSpecFromAlg25519PubKey; + pk->base.getAsyKeySpecString = GetStrSpecFromAlg25519PubKey; +} + +static void FillOpensslAlg25519PriKeyFunc(HcfOpensslAlg25519PriKey *sk) +{ + sk->base.base.base.destroy = DestroyAlg25519PriKey; + sk->base.base.base.getClass = GetAlg25519PriKeyClass; + sk->base.base.getAlgorithm = GetAlg25519PriKeyAlgorithm; + sk->base.base.getEncoded = GetAlg25519PriKeyEncoded; + sk->base.base.getFormat = GetAlg25519PriKeyFormat; + sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromAlg25519PriKey; + sk->base.getAsyKeySpecInt = GetIntSpecFromAlg25519PriKey; + sk->base.getAsyKeySpecString = GetStrSpecFromAlg25519PriKey; + sk->base.clearMem = ClearAlg25519PriKeyMem; +} + +static HcfResult CreateAlg25519PubKey(EVP_PKEY *pkey, HcfOpensslAlg25519PubKey **returnPubKey) +{ + HcfOpensslAlg25519PubKey *alg25519PubKey = + (HcfOpensslAlg25519PubKey *)HcfMalloc(sizeof(HcfOpensslAlg25519PubKey), 0); + if (alg25519PubKey == NULL) { + LOGE("Failed to allocate alg25519 public key memory."); + return HCF_ERR_MALLOC; + } + FillOpensslAlg25519PubKeyFunc(alg25519PubKey); + alg25519PubKey->pkey = pkey; + *returnPubKey = alg25519PubKey; + return HCF_SUCCESS; +} + +static HcfResult CreateAlg25519PriKey(EVP_PKEY *pkey, HcfOpensslAlg25519PriKey **returnPriKey) +{ + HcfOpensslAlg25519PriKey *alg25519PriKey = + (HcfOpensslAlg25519PriKey *)HcfMalloc(sizeof(HcfOpensslAlg25519PriKey), 0); + if (alg25519PriKey == NULL) { + LOGE("Failed to allocate alg25519 private key memory."); + return HCF_ERR_MALLOC; + } + FillOpensslAlg25519PriKeyFunc(alg25519PriKey); + alg25519PriKey->pkey = pkey; + *returnPriKey = alg25519PriKey; + return HCF_SUCCESS; +} + +static HcfResult CreateAlg25519KeyPair(const HcfOpensslAlg25519PubKey *pubKey, + const HcfOpensslAlg25519PriKey *priKey, HcfKeyPair **returnKeyPair) +{ + HcfOpensslAlg25519KeyPair *keyPair = + (HcfOpensslAlg25519KeyPair *)HcfMalloc(sizeof(HcfOpensslAlg25519KeyPair), 0); + if (keyPair == NULL) { + LOGE("Failed to allocate keyPair memory."); + return HCF_ERR_MALLOC; + } + keyPair->base.base.getClass = GetAlg25519KeyPairClass; + keyPair->base.base.destroy = DestroyAlg25519KeyPair; + keyPair->base.pubKey = (HcfPubKey *)pubKey; + keyPair->base.priKey = (HcfPriKey *)priKey; + + *returnKeyPair = (HcfKeyPair *)keyPair; + return HCF_SUCCESS; +} + +static HcfResult GeneratePubKeyByPkey(EVP_PKEY *pkey, HcfOpensslAlg25519PubKey **returnPubKey) +{ + EVP_PKEY *evpPkey = Openssl_EVP_PKEY_dup(pkey); + if (evpPkey == NULL) { + LOGE("pkey dup failed"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult ret = CreateAlg25519PubKey(evpPkey, returnPubKey); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 public key failed"); + Openssl_EVP_PKEY_free(evpPkey); + } + return ret; +} + +static HcfResult GeneratePriKeyByPkey(EVP_PKEY *pkey, HcfOpensslAlg25519PriKey **returnPriKey) +{ + EVP_PKEY *evpPkey = Openssl_EVP_PKEY_dup(pkey); + if (evpPkey == NULL) { + LOGE("pkey dup failed"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult ret = CreateAlg25519PriKey(evpPkey, returnPriKey); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 private key failed"); + Openssl_EVP_PKEY_free(evpPkey); + } + return ret; +} + +static HcfResult GenerateAlg25519PubAndPriKey(int type, HcfOpensslAlg25519PubKey **returnPubKey, + HcfOpensslAlg25519PriKey **returnPriKey) +{ + EVP_PKEY *pkey = NULL; + HcfResult ret = GenerateAlg25519EvpKey(type, &pkey); + if (ret != HCF_SUCCESS) { + LOGE("Generate alg25519 EVP_PKEY failed."); + return ret; + } + + ret = GeneratePubKeyByPkey(pkey, returnPubKey); + if (ret != HCF_SUCCESS) { + LOGE("Generate pubkey fail."); + Openssl_EVP_PKEY_free(pkey); + return ret; + } + + ret = GeneratePriKeyByPkey(pkey, returnPriKey); + if (ret != HCF_SUCCESS) { + LOGE("Generate prikey fail."); + HcfObjDestroy(*returnPubKey); + *returnPubKey = NULL; + Openssl_EVP_PKEY_free(pkey); + return HCF_ERR_CRYPTO_OPERATION; + } + + Openssl_EVP_PKEY_free(pkey); + return ret; +} + +static HcfResult ConvertAlg25519PubKey(const HcfBlob *pubKeyBlob, HcfOpensslAlg25519PubKey **returnPubKey) +{ + const unsigned char *tmpData = (const unsigned char *)(pubKeyBlob->data); + EVP_PKEY *pkey = Openssl_d2i_PUBKEY(NULL, &tmpData, pubKeyBlob->len); + if (pkey == NULL) { + LOGE("D2i pubkey fail."); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult ret = CreateAlg25519PubKey(pkey, returnPubKey); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 public key failed"); + Openssl_EVP_PKEY_free(pkey); + } + return ret; +} + +static HcfResult ConvertAlg25519PriKey(int type, const HcfBlob *priKeyBlob, + HcfOpensslAlg25519PriKey **returnPriKey) +{ + const unsigned char *tmpData = (const unsigned char *)(priKeyBlob->data); + EVP_PKEY *pkey = Openssl_d2i_PrivateKey(type, NULL, &tmpData, priKeyBlob->len); + if (pkey == NULL) { + LOGE("D2i privateKey fail."); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult ret = CreateAlg25519PriKey(pkey, returnPriKey); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 private key failed"); + Openssl_EVP_PKEY_free(pkey); + } + return ret; +} + +static HcfResult ConvertAlg25519PubAndPriKey(int type, const HcfBlob *pubKeyBlob, const HcfBlob *priKeyBlob, + HcfOpensslAlg25519PubKey **returnPubKey, HcfOpensslAlg25519PriKey **returnPriKey) +{ + if (pubKeyBlob != NULL) { + if (ConvertAlg25519PubKey(pubKeyBlob, returnPubKey) != HCF_SUCCESS) { + LOGE("Convert alg25519 public key failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + } + if (priKeyBlob != NULL) { + if (ConvertAlg25519PriKey(type, priKeyBlob, returnPriKey) != HCF_SUCCESS) { + LOGE("Convert alg25519 private key failed."); + HcfObjDestroy(*returnPubKey); + *returnPubKey = NULL; + return HCF_ERR_CRYPTO_OPERATION; + } + } + return HCF_SUCCESS; +} + +static HcfResult CheckClassMatch(HcfAsyKeyGeneratorSpi *self, int *type) +{ + if (IsClassMatch((HcfObjectBase *)self, GetEd25519KeyGeneratorSpiClass())) { + *type = EVP_PKEY_ED25519; + } else if (IsClassMatch((HcfObjectBase *)self, GetX25519KeyGeneratorSpiClass())) { + *type = EVP_PKEY_X25519; + } else { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + return HCF_SUCCESS; +} + +static HcfResult EngineGenerateAlg25519KeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnKeyPair) +{ + if (self == NULL || returnKeyPair == NULL) { + LOGE("Invalid params."); + return HCF_INVALID_PARAMS; + } + int type = 0; + if (CheckClassMatch(self, &type) != HCF_SUCCESS) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + + HcfOpensslAlg25519PubKey *pubKey = NULL; + HcfOpensslAlg25519PriKey *priKey = NULL; + HcfResult ret = GenerateAlg25519PubAndPriKey(type, &pubKey, &priKey); + if (ret != HCF_SUCCESS) { + LOGE("Generate alg25519 pk and sk by openssl failed."); + return ret; + } + + ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 keyPair failed."); + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + return ret; + } + return HCF_SUCCESS; +} + +static HcfResult EngineConvertAlg25519Key(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob, + HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair) +{ + if ((self == NULL) || (returnKeyPair == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + int type = 0; + if (CheckClassMatch(self, &type) != HCF_SUCCESS) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + bool pubKeyValid = IsBlobValid(pubKeyBlob); + bool priKeyValid = IsBlobValid(priKeyBlob); + if ((!pubKeyValid) && (!priKeyValid)) { + LOGE("The private key and public key cannot both be NULL."); + return HCF_INVALID_PARAMS; + } + + HcfOpensslAlg25519PubKey *pubKey = NULL; + HcfOpensslAlg25519PriKey *priKey = NULL; + HcfBlob *inputPk = pubKeyValid ? pubKeyBlob : NULL; + HcfBlob *inputSk = priKeyValid ? priKeyBlob : NULL; + HcfResult ret = ConvertAlg25519PubAndPriKey(type, inputPk, inputSk, &pubKey, &priKey); + if (ret != HCF_SUCCESS) { + LOGE("Convert alg25519 keyPair failed."); + return ret; + } + ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 keyPair failed."); + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + } + return ret; +} + +static HcfResult CreateOpensslAlg25519PubKey(const HcfBigInteger *pk, const char *algName, + EVP_PKEY **returnAlg25519) +{ + EVP_PKEY *pubkey = NULL; + if (strcmp(algName, ALGORITHM_NAME_ED25519) == 0) { + pubkey = Openssl_EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, pk->data, pk->len); + } else if (strcmp(algName, ALGORITHM_NAME_X25519) == 0) { + pubkey = Openssl_EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL, pk->data, pk->len); + } else { + LOGE("Invalid algName! [Algo]: %s", algName); + return HCF_INVALID_PARAMS; + } + // 设置公钥数据 + if (pubkey == NULL) { + LOGE("Set alg25519 pubKey failed."); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + *returnAlg25519 = pubkey; + return HCF_SUCCESS; +} + +static HcfResult CreateOpensslAlg25519PriKey(const HcfBigInteger *sk, const char *algName, + EVP_PKEY **returnAlg25519) +{ + EVP_PKEY *privkey = NULL; + if (strcmp(algName, ALGORITHM_NAME_ED25519) == 0) { + privkey = Openssl_EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL, sk->data, sk->len); + } else if (strcmp(algName, ALGORITHM_NAME_X25519) == 0) { + privkey = Openssl_EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, sk->data, sk->len); + } else { + LOGE("Invalid algName! [Algo]: %s", algName); + return HCF_INVALID_PARAMS; + } + // 设置私钥数据 + if (privkey == NULL) { + LOGE("set alg25519 priKey failed."); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + *returnAlg25519 = privkey; + return HCF_SUCCESS; +} + +static HcfResult CreateAlg25519PubKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec, + const char *algName, HcfOpensslAlg25519PubKey **returnPubKey) +{ + EVP_PKEY *alg25519 = NULL; + if (CreateOpensslAlg25519PubKey(&(paramsSpec->pk), algName, &alg25519) != HCF_SUCCESS) { + LOGE("Create openssl alg25519 pubKey failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (CreateAlg25519PubKey(alg25519, returnPubKey) != HCF_SUCCESS) { + LOGE("Create alg25519 pubKey failed."); + Openssl_EVP_PKEY_free(alg25519); + return HCF_ERR_MALLOC; + } + return HCF_SUCCESS; +} + +static HcfResult CreateAlg25519PriKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec, + const char *algName, HcfOpensslAlg25519PriKey **returnPriKey) +{ + EVP_PKEY *alg25519 = NULL; + if (CreateOpensslAlg25519PriKey(&(paramsSpec->sk), algName, &alg25519) != HCF_SUCCESS) { + LOGE("Create openssl alg25519 priKey failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (CreateAlg25519PriKey(alg25519, returnPriKey) != HCF_SUCCESS) { + LOGE("Create alg25519 priKey failed."); + Openssl_EVP_PKEY_free(alg25519); + return HCF_ERR_MALLOC; + } + return HCF_SUCCESS; +} + +static HcfResult CreateAlg25519KeyPairByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec, + const char *algName, HcfKeyPair **returnKeyPair) +{ + HcfOpensslAlg25519PubKey *pubKey = NULL; + HcfResult ret = CreateAlg25519PubKeyByKeyPairSpec(paramsSpec, algName, &pubKey); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 pubKey failed."); + return ret; + } + + HcfOpensslAlg25519PriKey *priKey = NULL; + ret = CreateAlg25519PriKeyByKeyPairSpec(paramsSpec, algName, &priKey); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 priKey failed."); + HcfObjDestroy(pubKey); + return ret; + } + ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 keyPair failed."); + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + return ret; + } + return HCF_SUCCESS; +} + +static HcfResult CreateAlg25519PubKeyByPubKeySpec(const HcfAlg25519PubKeyParamsSpec *paramsSpec, + const char *algName, HcfOpensslAlg25519PubKey **returnPubKey) +{ + EVP_PKEY *alg25519 = NULL; + if (CreateOpensslAlg25519PubKey(&(paramsSpec->pk), algName, &alg25519) != HCF_SUCCESS) { + LOGE("Create openssl alg25519 pubKey failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (CreateAlg25519PubKey(alg25519, returnPubKey) != HCF_SUCCESS) { + LOGE("Create alg25519 pubKey failed."); + Openssl_EVP_PKEY_free(alg25519); + return HCF_ERR_MALLOC; + } + return HCF_SUCCESS; +} + +static HcfResult CreateAlg25519PriKeyByPriKeySpec(const HcfAlg25519PriKeyParamsSpec *paramsSpec, + const char *algName, HcfOpensslAlg25519PriKey **returnPriKey) +{ + EVP_PKEY *alg25519 = NULL; + if (CreateOpensslAlg25519PriKey(&(paramsSpec->sk), algName, &alg25519) != HCF_SUCCESS) { + LOGE("Create openssl alg25519 priKey failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (CreateAlg25519PriKey(alg25519, returnPriKey) != HCF_SUCCESS) { + LOGE("Create alg25519 priKey failed."); + Openssl_EVP_PKEY_free(alg25519); + return HCF_ERR_MALLOC; + } + return HCF_SUCCESS; +} + +static HcfResult EngineGenerateAlg25519PubKeyBySpec(const HcfAsyKeyGeneratorSpi *self, + const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey) +{ + if ((self == NULL) || (paramsSpec == NULL) || (returnPubKey == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + + if (!IsClassMatch((HcfObjectBase *)self, GetEd25519KeyGeneratorSpiClass()) && + !IsClassMatch((HcfObjectBase *)self, GetX25519KeyGeneratorSpiClass())) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + + if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) && + (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) || + (paramsSpec->specType != HCF_PUBLIC_KEY_SPEC)) { + LOGE("Invalid params spec."); + return HCF_INVALID_PARAMS; + } + HcfOpensslAlg25519PubKey *alg25519Pk = NULL; + HcfResult ret = CreateAlg25519PubKeyByPubKeySpec((const HcfAlg25519PubKeyParamsSpec *)paramsSpec, + paramsSpec->algName, &alg25519Pk); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 public key by spec failed."); + } else { + *returnPubKey = (HcfPubKey *)alg25519Pk; + } + return ret; +} + +static HcfResult EngineGenerateAlg25519PriKeyBySpec(const HcfAsyKeyGeneratorSpi *self, + const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey) +{ + if ((self == NULL) || (paramsSpec == NULL) || (returnPriKey == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + + if (!IsClassMatch((HcfObjectBase *)self, GetEd25519KeyGeneratorSpiClass()) && + !IsClassMatch((HcfObjectBase *)self, GetX25519KeyGeneratorSpiClass())) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + + if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) && + (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) || + (paramsSpec->specType != HCF_PRIVATE_KEY_SPEC)) { + LOGE("Invalid params spec."); + return HCF_INVALID_PARAMS; + } + HcfOpensslAlg25519PriKey *alg25519Sk = NULL; + HcfResult ret = CreateAlg25519PriKeyByPriKeySpec((const HcfAlg25519PriKeyParamsSpec *)paramsSpec, + paramsSpec->algName, &alg25519Sk); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 private key by spec failed."); + } else { + *returnPriKey = (HcfPriKey *)alg25519Sk; + } + return ret; +} + +static HcfResult EngineGenerateAlg25519KeyPairBySpec(const HcfAsyKeyGeneratorSpi *self, + const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair) +{ + if ((self == NULL) || (paramsSpec == NULL) || (returnKeyPair == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + + if (!IsClassMatch((HcfObjectBase *)self, GetEd25519KeyGeneratorSpiClass()) && + !IsClassMatch((HcfObjectBase *)self, GetX25519KeyGeneratorSpiClass())) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + + if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) && + (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) || + (paramsSpec->specType != HCF_KEY_PAIR_SPEC)) { + LOGE("Invalid params spec."); + return HCF_INVALID_PARAMS; + } + HcfResult ret = CreateAlg25519KeyPairByKeyPairSpec((const HcfAlg25519KeyPairParamsSpec *)paramsSpec, + paramsSpec->algName, returnKeyPair); + if (ret != HCF_SUCCESS) { + LOGE("Create alg25519 key pair by spec failed."); + } + return ret; +} + +HcfResult HcfAsyKeyGeneratorSpiEd25519Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnSpi) +{ + if (params == NULL || returnSpi == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *impl = (HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *)HcfMalloc( + sizeof(HcfAsyKeyGeneratorSpiAlg25519OpensslImpl), 0); + if (impl == NULL) { + LOGE("Failed to allocate generator impl memroy."); + return HCF_ERR_MALLOC; + } + impl->base.base.getClass = GetEd25519KeyGeneratorSpiClass; + impl->base.base.destroy = DestroyAlg25519KeyGeneratorSpiImpl; + impl->base.engineGenerateKeyPair = EngineGenerateAlg25519KeyPair; + impl->base.engineConvertKey = EngineConvertAlg25519Key; + impl->base.engineGenerateKeyPairBySpec = EngineGenerateAlg25519KeyPairBySpec; + impl->base.engineGeneratePubKeyBySpec = EngineGenerateAlg25519PubKeyBySpec; + impl->base.engineGeneratePriKeyBySpec = EngineGenerateAlg25519PriKeyBySpec; + + *returnSpi = (HcfAsyKeyGeneratorSpi *)impl; + return HCF_SUCCESS; +} + +HcfResult HcfAsyKeyGeneratorSpiX25519Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnSpi) +{ + if (params == NULL || returnSpi == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *impl = (HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *)HcfMalloc( + sizeof(HcfAsyKeyGeneratorSpiAlg25519OpensslImpl), 0); + if (impl == NULL) { + LOGE("Failed to allocate generator impl memroy."); + return HCF_ERR_MALLOC; + } + impl->base.base.getClass = GetX25519KeyGeneratorSpiClass; + impl->base.base.destroy = DestroyAlg25519KeyGeneratorSpiImpl; + impl->base.engineGenerateKeyPair = EngineGenerateAlg25519KeyPair; + impl->base.engineConvertKey = EngineConvertAlg25519Key; + impl->base.engineGenerateKeyPairBySpec = EngineGenerateAlg25519KeyPairBySpec; + impl->base.engineGeneratePubKeyBySpec = EngineGenerateAlg25519PubKeyBySpec; + impl->base.engineGeneratePriKeyBySpec = EngineGenerateAlg25519PriKeyBySpec; + + *returnSpi = (HcfAsyKeyGeneratorSpi *)impl; + return HCF_SUCCESS; +} + diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c new file mode 100644 index 0000000..0f93a2b --- /dev/null +++ b/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c @@ -0,0 +1,1114 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dh_asy_key_generator_openssl.h" +#include + +#include "dh_openssl_common.h" +#include "detailed_dh_key_params.h" +#include "log.h" +#include "memory.h" +#include "openssl_adapter.h" +#include "openssl_class.h" +#include "openssl_common.h" + +#define OPENSSL_DH_GENERATOR_CLASS "OPENSSL.DH.KEYGENERATOR" +#define OPENSSL_DH_PUBKEY_FORMAT "X.509" +#define OPENSSL_DH_PRIKEY_FORMAT "PKCS#8" +#define ALGORITHM_NAME_DH "DH" +#define PARAMS_NUM_TWO 2 +#define BIT8 8 + +static bool isBySpec = true; +typedef struct { + HcfAsyKeyGeneratorSpi base; + + int32_t pBits; +} HcfAsyKeyGeneratorSpiDhOpensslImpl; + +static void FreeCtx(EVP_PKEY_CTX *paramsCtx, EVP_PKEY *paramsPkey, EVP_PKEY_CTX *pkeyCtx) +{ + if (paramsCtx != NULL) { + Openssl_EVP_PKEY_CTX_free(paramsCtx); + } + if (paramsPkey != NULL) { + Openssl_EVP_PKEY_free(paramsPkey); + } + if (pkeyCtx != NULL) { + Openssl_EVP_PKEY_CTX_free(pkeyCtx); + } +} + +static void FreeCommSpecBn(BIGNUM *p, BIGNUM *g) +{ + if (p != NULL) { + Openssl_BN_free(p); + p = NULL; + } + if (g != NULL) { + Openssl_BN_free(g); + g = NULL; + } +} + +static const char *GetDhKeyGeneratorSpiClass(void) +{ + return OPENSSL_DH_GENERATOR_CLASS; +} + +static const char *GetDhKeyPairClass(void) +{ + return OPENSSL_DH_KEYPAIR_CLASS; +} + +static const char *GetDhPubKeyClass(void) +{ + return OPENSSL_DH_PUBKEY_CLASS; +} + +static const char *GetDhPriKeyClass(void) +{ + return OPENSSL_DH_PRIKEY_CLASS; +} + +static void DestroyDhKeyGeneratorSpiImpl(HcfObjectBase *self) +{ + if (self == NULL) { + return; + } + if (!IsClassMatch(self, GetDhKeyGeneratorSpiClass())) { + return; + } + HcfFree(self); +} + +static void DestroyDhPubKey(HcfObjectBase *self) +{ + if (self == NULL) { + return; + } + if (!IsClassMatch(self, GetDhPubKeyClass())) { + return; + } + HcfOpensslDhPubKey *impl = (HcfOpensslDhPubKey *)self; + Openssl_DH_free(impl->pk); + impl->pk = NULL; + HcfFree(impl); +} + +static void DestroyDhPriKey(HcfObjectBase *self) +{ + if (self == NULL) { + return; + } + if (!IsClassMatch(self, GetDhPriKeyClass())) { + return; + } + HcfOpensslDhPriKey *impl = (HcfOpensslDhPriKey *)self; + Openssl_DH_free(impl->sk); + impl->sk = NULL; + HcfFree(impl); +} + +static void DestroyDhKeyPair(HcfObjectBase *self) +{ + if (self == NULL) { + return; + } + if (!IsClassMatch(self, GetDhKeyPairClass())) { + return; + } + HcfOpensslDhKeyPair *impl = (HcfOpensslDhKeyPair *)self; + DestroyDhPubKey((HcfObjectBase *)impl->base.pubKey); + impl->base.pubKey = NULL; + DestroyDhPriKey((HcfObjectBase *)impl->base.priKey); + impl->base.priKey = NULL; + HcfFree(self); +} + +static const char *GetDhPubKeyAlgorithm(HcfKey *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return NULL; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhPubKeyClass())) { + return NULL; + } + return ALGORITHM_NAME_DH; +} + +static const char *GetDhPriKeyAlgorithm(HcfKey *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return NULL; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) { + return NULL; + } + return ALGORITHM_NAME_DH; +} + +static HcfResult GetDhPubKeyEncoded(HcfKey *self, HcfBlob *returnBlob) +{ + if ((self == NULL) || (returnBlob == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhPubKeyClass())) { + return HCF_INVALID_PARAMS; + } + HcfOpensslDhPubKey *impl = (HcfOpensslDhPubKey *)self; + unsigned char *returnData = NULL; + EVP_PKEY *pKey = NewEvpPkeyByDh(impl->pk, true); + if (pKey == NULL) { + LOGE("new pKey by dh fail."); + return HCF_ERR_CRYPTO_OPERATION; + } + int len = Openssl_i2d_PUBKEY(pKey, &returnData); + if (len <= 0) { + LOGE("Call i2d_PUBKEY failed"); + HcfPrintOpensslError(); + Openssl_EVP_PKEY_free(pKey); + return HCF_ERR_CRYPTO_OPERATION; + } + returnBlob->data = returnData; + returnBlob->len = len; + Openssl_EVP_PKEY_free(pKey); + return HCF_SUCCESS; +} + +static HcfResult GetDhPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) +{ + if ((self == NULL) || (returnBlob == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) { + return HCF_INVALID_PARAMS; + } + HcfOpensslDhPriKey *impl = (HcfOpensslDhPriKey *)self; + unsigned char *returnData = NULL; + EVP_PKEY *pKey = NewEvpPkeyByDh(impl->sk, true); + if (pKey == NULL) { + LOGE("new pKey by dh fail."); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + int len = Openssl_i2d_PrivateKey(pKey, &returnData); + if (len <= 0) { + LOGE("Call i2d_PrivateKey failed."); + HcfPrintOpensslError(); + Openssl_EVP_PKEY_free(pKey); + return HCF_ERR_CRYPTO_OPERATION; + } + returnBlob->data = returnData; + returnBlob->len = len; + Openssl_EVP_PKEY_free(pKey); + return HCF_SUCCESS; +} + +static const char *GetDhPubKeyFormat(HcfKey *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return NULL; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhPubKeyClass())) { + return NULL; + } + return OPENSSL_DH_PUBKEY_FORMAT; +} + +static const char *GetDhPriKeyFormat(HcfKey *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return NULL; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) { + return NULL; + } + return OPENSSL_DH_PRIKEY_FORMAT; +} + +static HcfResult GetBigIntegerSpec(const HcfPubKey *pubSelf, const HcfPriKey *priSelf, const AsyKeySpecItem item, + HcfBigInteger *returnBigInteger) +{ + DH *dh = NULL; + if (pubSelf != NULL) { + if (item == DH_SK_BN) { + LOGE("Invalid item."); + return HCF_INVALID_PARAMS; + } + HcfOpensslDhPubKey *impl = (HcfOpensslDhPubKey *)pubSelf; + dh = impl->pk; + } else { + if (item == DH_PK_BN) { + LOGE("Invalid item."); + return HCF_INVALID_PARAMS; + } + HcfOpensslDhPriKey *impl = (HcfOpensslDhPriKey *)priSelf; + dh = impl->sk; + } + if (dh == NULL) { + LOGE("Dh is null."); + return HCF_INVALID_PARAMS; + } + HcfResult ret = HCF_SUCCESS; + switch (item) { + case DH_P_BN: + ret = BigNumToBigInteger(Openssl_DH_get0_p(dh), returnBigInteger); + break; + case DH_G_BN: + ret = BigNumToBigInteger(Openssl_DH_get0_g(dh), returnBigInteger); + break; + case DH_PK_BN: + ret = BigNumToBigInteger(Openssl_DH_get0_pub_key(dh), returnBigInteger); + break; + case DH_SK_BN: + ret = BigNumToBigInteger(Openssl_DH_get0_priv_key(dh), returnBigInteger); + break; + default: + LOGE("Input item [%d] is invalid", item); + ret = HCF_INVALID_PARAMS; + break; + } + return ret; +} + +static HcfResult GetBigIntegerSpecFromDhPubKey(const HcfPubKey *self, const AsyKeySpecItem item, + HcfBigInteger *returnBigInteger) +{ + if (self == NULL || returnBigInteger == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhPubKeyClass())) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + HcfResult ret = GetBigIntegerSpec(self, NULL, item, returnBigInteger); + if (ret != HCF_SUCCESS) { + LOGE("Get big integer failed."); + } + return ret; +} + +static HcfResult GetBigIntegerSpecFromDhPriKey(const HcfPriKey *self, const AsyKeySpecItem item, + HcfBigInteger *returnBigInteger) +{ + if (self == NULL || returnBigInteger == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + HcfResult ret = HCF_SUCCESS; + ret = GetBigIntegerSpec(NULL, self, item, returnBigInteger); + if (ret != HCF_SUCCESS) { + LOGE("Get big integer failed."); + } + return ret; +} + +static HcfResult GetIntSpecFromDhPubKey(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt) +{ + (void)self; + (void)returnInt; + return HCF_NOT_SUPPORT; +} + +static HcfResult GetIntSpecFromDhPriKey(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt) +{ + if (self == NULL || returnInt == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) { + LOGE("Invalid class of self."); + return HCF_INVALID_PARAMS; + } + if (item != DH_L_NUM) { + return HCF_INVALID_PARAMS; + } + if (!isBySpec) { + *returnInt = 0; + return HCF_SUCCESS; + } + HcfOpensslDhPriKey *impl = (HcfOpensslDhPriKey *)self; + DH *dh = impl->sk; + if (dh == NULL) { + LOGE("Dh is null."); + return HCF_INVALID_PARAMS; + } + const BIGNUM *sk = Openssl_DH_get0_priv_key(dh); + if (sk == NULL) { + LOGE("Get private key fail."); + return HCF_ERR_CRYPTO_OPERATION; + } + *returnInt = Openssl_BN_num_bits(sk); + if (*returnInt <= 0) { + LOGE("Get private key bytes fail."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (*returnInt % BIT8 != 0) { + *returnInt = (*returnInt / BIT8 + 1) * BIT8; + } + return HCF_SUCCESS; +} + +static HcfResult GetStrSpecFromDhPubKey(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString) +{ + (void)self; + (void)returnString; + return HCF_NOT_SUPPORT; +} + +static HcfResult GetStrSpecFromDhPriKey(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString) +{ + (void)self; + (void)returnString; + return HCF_NOT_SUPPORT; +} + +static void ClearDhPriKeyMem(HcfPriKey *self) +{ + if (self == NULL) { + return; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) { + return; + } + HcfOpensslDhPriKey *impl = (HcfOpensslDhPriKey *)self; + Openssl_DH_free(impl->sk); + impl->sk = NULL; +} + +static EVP_PKEY *ConstructDhOsslParamsAndGenPkey(int32_t dhId, EVP_PKEY_CTX *paramsCtx) +{ + EVP_PKEY *paramsPkey = NULL; + OSSL_PARAM params[PARAMS_NUM_TWO]; + char *nidName = GetNidNameByDhId(dhId); + if (nidName == NULL) { + LOGE("Get nid name failed."); + return NULL; + } + params[0] = Openssl_OSSL_PARAM_construct_utf8_string("group", nidName, 0); + params[1] = Openssl_OSSL_PARAM_construct_end(); + if (Openssl_EVP_PKEY_keygen_init(paramsCtx) != HCF_OPENSSL_SUCCESS) { + LOGE("ParamsCtx generate init failed."); + return NULL; + } + if (Openssl_EVP_PKEY_CTX_set_params(paramsCtx, params) != HCF_OPENSSL_SUCCESS) { + LOGE("ParamsCtx set failed."); + return NULL; + } + if (Openssl_EVP_PKEY_generate(paramsCtx, ¶msPkey) != HCF_OPENSSL_SUCCESS) { + LOGE("Create generate failed."); + return NULL; + } + return paramsPkey; +} + +static HcfResult GenerateDhEvpKey(int32_t dhId, EVP_PKEY **ppkey) +{ + HcfResult ret = HCF_SUCCESS; + EVP_PKEY *paramsPkey = NULL; + EVP_PKEY_CTX *pkeyCtx = NULL; + EVP_PKEY_CTX *paramsCtx = NULL; + + do { + paramsCtx = Openssl_EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); + if (paramsCtx == NULL) { + LOGE("New paramsCtx from name failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + paramsPkey = ConstructDhOsslParamsAndGenPkey(dhId, paramsCtx); + if (paramsPkey == NULL) { + LOGE("Construct dh params and generate pkey failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + pkeyCtx = Openssl_EVP_PKEY_CTX_new(paramsPkey, NULL); + if (pkeyCtx == NULL) { + LOGE("Create pkey ctx failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_keygen_init(pkeyCtx) != HCF_OPENSSL_SUCCESS) { + LOGE("Key ctx generate init failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_keygen(pkeyCtx, ppkey) != HCF_OPENSSL_SUCCESS) { + LOGE("Generate pkey failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_check(pkeyCtx) != HCF_OPENSSL_SUCCESS) { + LOGE("Check pkey fail."); + Openssl_EVP_PKEY_free(*ppkey); + *ppkey = NULL; + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + } while (0); + FreeCtx(paramsCtx, paramsPkey, pkeyCtx); + return ret; +} + +static void FillOpensslDhPubKeyFunc(HcfOpensslDhPubKey *pk) +{ + pk->base.base.base.destroy = DestroyDhPubKey; + pk->base.base.base.getClass = GetDhPubKeyClass; + pk->base.base.getAlgorithm = GetDhPubKeyAlgorithm; + pk->base.base.getEncoded = GetDhPubKeyEncoded; + pk->base.base.getFormat = GetDhPubKeyFormat; + pk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromDhPubKey; + pk->base.getAsyKeySpecInt = GetIntSpecFromDhPubKey; + pk->base.getAsyKeySpecString = GetStrSpecFromDhPubKey; +} + +static void FillOpensslDhPriKeyFunc(HcfOpensslDhPriKey *sk) +{ + sk->base.base.base.destroy = DestroyDhPriKey; + sk->base.base.base.getClass = GetDhPriKeyClass; + sk->base.base.getAlgorithm = GetDhPriKeyAlgorithm; + sk->base.base.getEncoded = GetDhPriKeyEncoded; + sk->base.base.getFormat = GetDhPriKeyFormat; + sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromDhPriKey; + sk->base.getAsyKeySpecInt = GetIntSpecFromDhPriKey; + sk->base.getAsyKeySpecString = GetStrSpecFromDhPriKey; + sk->base.clearMem = ClearDhPriKeyMem; +} + +static HcfResult CreateDhPubKey(DH *pk, HcfOpensslDhPubKey **returnPubKey) +{ + HcfOpensslDhPubKey *dhPubKey = (HcfOpensslDhPubKey *)HcfMalloc(sizeof(HcfOpensslDhPubKey), 0); + if (dhPubKey == NULL) { + LOGE("Failed to allocate DH public key memory."); + return HCF_ERR_MALLOC; + } + FillOpensslDhPubKeyFunc(dhPubKey); + dhPubKey->pk = pk; + + *returnPubKey = dhPubKey; + return HCF_SUCCESS; +} + +static HcfResult CreateDhPriKey(DH *sk, HcfOpensslDhPriKey **returnPriKey) +{ + HcfOpensslDhPriKey *dhPriKey = (HcfOpensslDhPriKey *)HcfMalloc(sizeof(HcfOpensslDhPriKey), 0); + if (dhPriKey == NULL) { + LOGE("Failed to allocate Dh private key memory."); + return HCF_ERR_MALLOC; + } + FillOpensslDhPriKeyFunc(dhPriKey); + dhPriKey->sk = sk; + + *returnPriKey = dhPriKey; + return HCF_SUCCESS; +} + +static HcfResult CreateDhKeyPair(const HcfOpensslDhPubKey *pubKey, const HcfOpensslDhPriKey *priKey, + HcfKeyPair **returnKeyPair) +{ + HcfOpensslDhKeyPair *keyPair = (HcfOpensslDhKeyPair *)HcfMalloc(sizeof(HcfOpensslDhKeyPair), 0); + if (keyPair == NULL) { + LOGE("Failed to allocate keyPair memory."); + return HCF_ERR_MALLOC; + } + keyPair->base.base.getClass = GetDhKeyPairClass; + keyPair->base.base.destroy = DestroyDhKeyPair; + keyPair->base.pubKey = (HcfPubKey *)pubKey; + keyPair->base.priKey = (HcfPriKey *)priKey; + + *returnKeyPair = (HcfKeyPair *)keyPair; + return HCF_SUCCESS; +} + +static HcfResult GeneratePubKeyByPkey(EVP_PKEY *pkey, HcfOpensslDhPubKey **returnPubKey) +{ + DH *pk = Openssl_EVP_PKEY_get1_DH(pkey); + if (pk == NULL) { + LOGE("Get dh public key from pkey failed"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult ret = CreateDhPubKey(pk, returnPubKey); + if (ret != HCF_SUCCESS) { + LOGE("Create DH public key failed"); + Openssl_DH_free(pk); + } + return ret; +} + +static HcfResult GeneratePriKeyByPkey(EVP_PKEY *pkey, HcfOpensslDhPriKey **returnPriKey) +{ + DH *sk = Openssl_EVP_PKEY_get1_DH(pkey); + if (sk == NULL) { + LOGE("Get DH private key from pkey failed"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult ret = CreateDhPriKey(sk, returnPriKey); + if (ret != HCF_SUCCESS) { + LOGE("Create DH private key failed"); + Openssl_DH_free(sk); + } + return ret; +} + +static HcfResult GenerateDhPubAndPriKey(int32_t dhId, HcfOpensslDhPubKey **returnPubKey, + HcfOpensslDhPriKey **returnPriKey) +{ + EVP_PKEY *pkey = NULL; + HcfResult ret = GenerateDhEvpKey(dhId, &pkey); + if (ret != HCF_SUCCESS) { + LOGE("Generate DH EVP_PKEY failed."); + return ret; + } + + ret = GeneratePubKeyByPkey(pkey, returnPubKey); + if (ret != HCF_SUCCESS) { + Openssl_EVP_PKEY_free(pkey); + LOGE("Generate public key failed."); + return ret; + } + + ret = GeneratePriKeyByPkey(pkey, returnPriKey); + if (ret != HCF_SUCCESS) { + HcfObjDestroy(*returnPubKey); + *returnPubKey = NULL; + Openssl_EVP_PKEY_free(pkey); + LOGE("Generate private key failed."); + return ret; + } + + Openssl_EVP_PKEY_free(pkey); + return ret; +} + +static HcfResult ConvertCommSpec2Bn(const HcfDhCommParamsSpec *paramsSpec, BIGNUM **p, BIGNUM **g) +{ + if (BigIntegerToBigNum(&(paramsSpec->p), p) != HCF_SUCCESS) { + LOGE("Get openssl BN p failed"); + return HCF_ERR_CRYPTO_OPERATION; + } + if (BigIntegerToBigNum(&(paramsSpec->g), g) != HCF_SUCCESS) { + LOGE("Get openssl BN g failed"); + Openssl_BN_free(*p); + *p = NULL; + return HCF_ERR_CRYPTO_OPERATION; + } + return HCF_SUCCESS; +} + +static HcfResult CreateOpensslDhKey(const HcfDhCommParamsSpec *paramsSpec, BIGNUM *pk, BIGNUM *sk, DH **returnDh) +{ + BIGNUM *p = NULL; + BIGNUM *g = NULL; + if (ConvertCommSpec2Bn(paramsSpec, &p, &g)!= HCF_SUCCESS) { + LOGE("Get openssl BN p q failed"); + return HCF_ERR_CRYPTO_OPERATION; + } + DH *dh = Openssl_DH_new(); + if (dh == NULL) { + FreeCommSpecBn(p, g); + LOGE("Openssl dh new failed"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + if (Openssl_DH_set0_pqg(dh, p, NULL, g) != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl dh set pqg failed"); + HcfPrintOpensslError(); + FreeCommSpecBn(p, g); + Openssl_DH_free(dh); + return HCF_ERR_CRYPTO_OPERATION; + } + if (sk == NULL && paramsSpec->length >= 0) { + if (Openssl_DH_set_length(dh, paramsSpec->length) != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl dh set length failed"); + HcfPrintOpensslError(); + Openssl_DH_free(dh); + return HCF_ERR_CRYPTO_OPERATION; + } + } + if ((pk == NULL) && (sk == NULL)) { + *returnDh = dh; + return HCF_SUCCESS; + } + if (Openssl_DH_set0_key(dh, pk, sk) != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl DH set key failed"); + HcfPrintOpensslError(); + Openssl_DH_free(dh); + return HCF_ERR_CRYPTO_OPERATION; + } + *returnDh = dh; + return HCF_SUCCESS; +} + +static HcfResult GenerateOpensslDhKeyByCommSpec(const HcfDhCommParamsSpec *paramsSpec, DH **returnDh) +{ + if (CreateOpensslDhKey(paramsSpec, NULL, NULL, returnDh) != HCF_SUCCESS) { + LOGE("Create openssl dh key failed"); + return HCF_ERR_CRYPTO_OPERATION; + } + + if (Openssl_DH_generate_key(*returnDh) != HCF_OPENSSL_SUCCESS) { + LOGE("Openssl DH generate key failed"); + HcfPrintOpensslError(); + Openssl_DH_free(*returnDh); + *returnDh = NULL; + return HCF_ERR_CRYPTO_OPERATION; + } + return HCF_SUCCESS; +} + +static HcfResult GenerateOpensslDhKeyByPubKeySpec(const HcfDhPubKeyParamsSpec *paramsSpec, DH **returnDh) +{ + BIGNUM *pubKey = NULL; + if (BigIntegerToBigNum(&(paramsSpec->pk), &pubKey) != HCF_SUCCESS) { + LOGE("Get openssl BN pk failed"); + return HCF_ERR_CRYPTO_OPERATION; + } + + if (CreateOpensslDhKey(&(paramsSpec->base), pubKey, NULL, returnDh) != HCF_SUCCESS) { + Openssl_BN_free(pubKey); + return HCF_ERR_CRYPTO_OPERATION; + } + return HCF_SUCCESS; +} + +static HcfResult GenerateOpensslDhKeyByPriKeySpec(const HcfDhPriKeyParamsSpec *paramsSpec, DH **returnDh) +{ + BIGNUM *priKey = NULL; + if (BigIntegerToBigNum(&(paramsSpec->sk), &priKey) != HCF_SUCCESS) { + LOGE("Get openssl BN pk failed"); + return HCF_ERR_CRYPTO_OPERATION; + } + + if (CreateOpensslDhKey(&(paramsSpec->base), NULL, priKey, returnDh) != HCF_SUCCESS) { + Openssl_BN_free(priKey); + return HCF_ERR_CRYPTO_OPERATION; + } + return HCF_SUCCESS; +} +static HcfResult GenerateOpensslDhKeyByKeyPairSpec(const HcfDhKeyPairParamsSpec *paramsSpec, DH **returnDh) +{ + BIGNUM *pubKey = NULL; + BIGNUM *priKey = NULL; + if (BigIntegerToBigNum(&(paramsSpec->pk), &pubKey) != HCF_SUCCESS) { + LOGE("Get openssl BN pk failed"); + return HCF_ERR_CRYPTO_OPERATION; + } + if (BigIntegerToBigNum(&(paramsSpec->sk), &priKey) != HCF_SUCCESS) { + LOGE("Get openssl BN sk failed"); + Openssl_BN_free(pubKey); + return HCF_ERR_CRYPTO_OPERATION; + } + if (CreateOpensslDhKey(&(paramsSpec->base), pubKey, priKey, returnDh) != HCF_SUCCESS) { + Openssl_BN_free(pubKey); + Openssl_BN_free(priKey); + return HCF_ERR_CRYPTO_OPERATION; + } + return HCF_SUCCESS; +} + +static HcfResult CreateDhKeyPairByCommSpec(const HcfDhCommParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair) +{ + DH *dh = NULL; + if (GenerateOpensslDhKeyByCommSpec(paramsSpec, &dh) != HCF_SUCCESS) { + LOGE("Generate openssl dh key by commSpec failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfOpensslDhPubKey *pubKey = NULL; + if (CreateDhPubKey(dh, &pubKey) != HCF_SUCCESS) { + LOGE("Create dh pubKey failed."); + Openssl_DH_free(dh); + return HCF_ERR_MALLOC; + } + + if (Openssl_DH_up_ref(dh) != HCF_OPENSSL_SUCCESS) { + LOGE("Dup DH failed."); + HcfPrintOpensslError(); + HcfObjDestroy(pubKey); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfOpensslDhPriKey *priKey = NULL; + if (CreateDhPriKey(dh, &priKey) != HCF_SUCCESS) { + LOGE("Create dh priKey failed."); + Openssl_DH_free(dh); + HcfObjDestroy(pubKey); + return HCF_ERR_MALLOC; + } + + if (CreateDhKeyPair(pubKey, priKey, returnKeyPair) != HCF_SUCCESS) { + LOGE("Create dh keyPair failed."); + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + return HCF_ERR_MALLOC; + } + return HCF_SUCCESS; +} + +static HcfResult CreateDhPubKeyByKeyPairSpec(const HcfDhKeyPairParamsSpec *paramsSpec, + HcfOpensslDhPubKey **returnPubKey) +{ + DH *dh = NULL; + if (GenerateOpensslDhKeyByKeyPairSpec(paramsSpec, &dh) != HCF_SUCCESS) { + LOGE("Generate openssl dh key by keyPairSpec failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (CreateDhPubKey(dh, returnPubKey) != HCF_SUCCESS) { + LOGE("Create dh pubKey failed."); + Openssl_DH_free(dh); + return HCF_ERR_MALLOC; + } + return HCF_SUCCESS; +} + +static HcfResult CreateDhPriKeyByKeyPairSpec(const HcfDhKeyPairParamsSpec *paramsSpec, + HcfOpensslDhPriKey **returnPriKey) +{ + DH *dh = NULL; + if (GenerateOpensslDhKeyByKeyPairSpec(paramsSpec, &dh) != HCF_SUCCESS) { + LOGE("Generate openssl dh key by keyPairSpec failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (CreateDhPriKey(dh, returnPriKey) != HCF_SUCCESS) { + LOGE("Create dh priKey failed."); + Openssl_DH_free(dh); + return HCF_ERR_MALLOC; + } + return HCF_SUCCESS; +} + +static HcfResult CreateDhKeyPairByKeyPairSpec(const HcfDhKeyPairParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair) +{ + HcfOpensslDhPubKey *pubKey = NULL; + HcfResult ret = CreateDhPubKeyByKeyPairSpec(paramsSpec, &pubKey); + if (ret != HCF_SUCCESS) { + LOGE("Create dh pubKey by keyPairSpec failed."); + return ret; + } + + HcfOpensslDhPriKey *priKey = NULL; + ret = CreateDhPriKeyByKeyPairSpec(paramsSpec, &priKey); + if (ret != HCF_SUCCESS) { + LOGE("Create dh priKey by keyPairSpec failed."); + HcfObjDestroy(pubKey); + return ret; + } + ret = CreateDhKeyPair(pubKey, priKey, returnKeyPair); + if (ret != HCF_SUCCESS) { + LOGE("Create dh keyPair failed."); + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + return ret; + } + return HCF_SUCCESS; +} + +static HcfResult CreateDhKeyPairBySpec(const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair) +{ + if (paramsSpec->specType == HCF_COMMON_PARAMS_SPEC) { + return CreateDhKeyPairByCommSpec((const HcfDhCommParamsSpec *)paramsSpec, returnKeyPair); + } else { + return CreateDhKeyPairByKeyPairSpec((const HcfDhKeyPairParamsSpec*)paramsSpec, returnKeyPair); + } +} + +static HcfResult CreateDhPubKeyBySpec(const HcfDhPubKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey) +{ + DH *dh = NULL; + if (GenerateOpensslDhKeyByPubKeySpec(paramsSpec, &dh) != HCF_SUCCESS) { + LOGE("Generate openssl dh key by pubKeySpec failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfOpensslDhPubKey *pubKey = NULL; + if (CreateDhPubKey(dh, &pubKey) != HCF_SUCCESS) { + LOGE("Create dh pubKey failed."); + Openssl_DH_free(dh); + return HCF_ERR_MALLOC; + } + *returnPubKey = (HcfPubKey *)pubKey; + return HCF_SUCCESS; +} + +static HcfResult CreateDhPriKeyBySpec(const HcfDhPriKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey) +{ + DH *dh = NULL; + if (GenerateOpensslDhKeyByPriKeySpec(paramsSpec, &dh) != HCF_SUCCESS) { + LOGE("Generate openssl dh key by priKeySpec failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfOpensslDhPriKey *priKey = NULL; + if (CreateDhPriKey(dh, &priKey) != HCF_SUCCESS) { + LOGE("Create dh priKey failed."); + Openssl_DH_free(dh); + return HCF_ERR_MALLOC; + } + *returnPriKey = (HcfPriKey *)priKey; + return HCF_SUCCESS; +} + +static HcfResult ConvertDhPubKey(const HcfBlob *pubKeyBlob, HcfOpensslDhPubKey **returnPubKey) +{ + const unsigned char *temp = (const unsigned char *)pubKeyBlob->data; + EVP_PKEY *pKey = Openssl_d2i_PUBKEY(NULL, &temp, pubKeyBlob->len); + if (pKey == NULL) { + LOGE("d2i_PrivateKey or PUBKEY fail."); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + DH *dh = Openssl_EVP_PKEY_get1_DH(pKey); + if (dh == NULL) { + LOGE("EVP_PKEY_get1_DH fail"); + HcfPrintOpensslError(); + Openssl_EVP_PKEY_free(pKey); + return HCF_ERR_CRYPTO_OPERATION; + } + Openssl_EVP_PKEY_free(pKey); + HcfResult ret = CreateDhPubKey(dh, returnPubKey); + if (ret != HCF_SUCCESS) { + LOGE("Create dh public key failed"); + Openssl_DH_free(dh); + } + return ret; +} +static HcfResult ConvertDhPriKey(const HcfBlob *priKeyBlob, HcfOpensslDhPriKey **returnPriKey) +{ + const unsigned char *temp = (const unsigned char *)priKeyBlob->data; + EVP_PKEY *pKey = Openssl_d2i_PrivateKey(EVP_PKEY_DH, NULL, &temp, priKeyBlob->len); + if (pKey == NULL) { + LOGE("d2i_PrivateKey or PUBKEY fail."); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + DH *dh = Openssl_EVP_PKEY_get1_DH(pKey); + if (dh == NULL) { + LOGE("EVP_PKEY_get1_DH fail"); + HcfPrintOpensslError(); + Openssl_EVP_PKEY_free(pKey); + return HCF_ERR_CRYPTO_OPERATION; + } + Openssl_EVP_PKEY_free(pKey); + HcfResult ret = CreateDhPriKey(dh, returnPriKey); + if (ret != HCF_SUCCESS) { + LOGE("Create DH private key failed"); + Openssl_DH_free(dh); + } + return ret; +} + +static HcfResult ConvertDhPubAndPriKey(const HcfBlob *pubKeyBlob, const HcfBlob *priKeyBlob, + HcfOpensslDhPubKey **returnPubKey, HcfOpensslDhPriKey **returnPriKey) +{ + if (pubKeyBlob != NULL) { + if (ConvertDhPubKey(pubKeyBlob, returnPubKey) != HCF_SUCCESS) { + LOGE("Convert DH public key failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + } + if (priKeyBlob != NULL) { + if (ConvertDhPriKey(priKeyBlob, returnPriKey) != HCF_SUCCESS) { + LOGE("Convert DH private key failed."); + HcfObjDestroy(*returnPubKey); + *returnPubKey = NULL; + return HCF_ERR_CRYPTO_OPERATION; + } + } + return HCF_SUCCESS; +} + +static HcfResult EngineGenerateDhKeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnKeyPair) +{ + if (self == NULL || returnKeyPair == NULL) { + LOGE("Invalid params."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) { + LOGE("Class not match."); + return HCF_INVALID_PARAMS; + } + HcfAsyKeyGeneratorSpiDhOpensslImpl *impl = (HcfAsyKeyGeneratorSpiDhOpensslImpl *)self; + + HcfOpensslDhPubKey *pubKey = NULL; + HcfOpensslDhPriKey *priKey = NULL; + HcfResult ret = GenerateDhPubAndPriKey(impl->pBits, &pubKey, &priKey); + if (ret != HCF_SUCCESS) { + LOGE("Generate DH pk and sk by openssl failed."); + return ret; + } + + ret = CreateDhKeyPair(pubKey, priKey, returnKeyPair); + if (ret != HCF_SUCCESS) { + LOGE("Create dh keyPair failed."); + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + return ret; + } + isBySpec = false; + return HCF_SUCCESS; +} + +static HcfResult EngineConvertDhKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob, + HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair) +{ + (void)params; + if ((self == NULL) || (returnKeyPair == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) { + LOGE("Class not match."); + return HCF_INVALID_PARAMS; + } + bool pubKeyValid = IsBlobValid(pubKeyBlob); + bool priKeyValid = IsBlobValid(priKeyBlob); + if ((!pubKeyValid) && (!priKeyValid)) { + LOGE("The private key and public key cannot both be NULL."); + return HCF_INVALID_PARAMS; + } + + HcfOpensslDhPubKey *pubKey = NULL; + HcfOpensslDhPriKey *priKey = NULL; + HcfBlob *inputPk = pubKeyValid ? pubKeyBlob : NULL; + HcfBlob *inputSk = priKeyValid ? priKeyBlob : NULL; + HcfResult ret = ConvertDhPubAndPriKey(inputPk, inputSk, &pubKey, &priKey); + if (ret != HCF_SUCCESS) { + LOGE("Convert dh pubKey and priKey failed."); + return ret; + } + ret = CreateDhKeyPair(pubKey, priKey, returnKeyPair); + if (ret != HCF_SUCCESS) { + LOGE("Create dh keyPair failed."); + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + } + return ret; +} + +static HcfResult EngineGenerateDhKeyPairBySpec(const HcfAsyKeyGeneratorSpi *self, + const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair) +{ + if ((self == NULL) || (paramsSpec == NULL) || (returnKeyPair == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + + if (!IsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) { + LOGE("Class not match."); + return HCF_INVALID_PARAMS; + } + + if ((strcmp(paramsSpec->algName, ALGORITHM_NAME_DH) != 0) || + ((paramsSpec->specType != HCF_COMMON_PARAMS_SPEC) && (paramsSpec->specType != HCF_KEY_PAIR_SPEC))) { + LOGE("Invalid params spec."); + return HCF_INVALID_PARAMS; + } + HcfResult ret = CreateDhKeyPairBySpec(paramsSpec, returnKeyPair); + if (ret != HCF_SUCCESS) { + LOGE("Create DH key pair by spec failed."); + } + isBySpec = true; + return ret; +} + +static HcfResult EngineGenerateDhPubKeyBySpec(const HcfAsyKeyGeneratorSpi *self, + const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey) +{ + if ((self == NULL) || (paramsSpec == NULL) || (returnPubKey == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + + if (!IsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) { + LOGE("Class not match."); + return HCF_INVALID_PARAMS; + } + + if ((strcmp(paramsSpec->algName, ALGORITHM_NAME_DH) != 0) || + ((paramsSpec->specType != HCF_PUBLIC_KEY_SPEC) && (paramsSpec->specType != HCF_KEY_PAIR_SPEC))) { + LOGE("Invalid params spec."); + return HCF_INVALID_PARAMS; + } + + HcfResult ret = CreateDhPubKeyBySpec((const HcfDhPubKeyParamsSpec *)paramsSpec, returnPubKey); + if (ret != HCF_SUCCESS) { + LOGE("Create DH public key by spec failed."); + } + isBySpec = true; + return ret; +} + +static HcfResult EngineGenerateDhPriKeyBySpec(const HcfAsyKeyGeneratorSpi *self, + const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey) +{ + if ((self == NULL) || (paramsSpec == NULL) || (returnPriKey == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) { + LOGE("Class not match."); + return HCF_INVALID_PARAMS; + } + if ((strcmp(paramsSpec->algName, ALGORITHM_NAME_DH) != 0) || + ((paramsSpec->specType != HCF_PRIVATE_KEY_SPEC) && (paramsSpec->specType != HCF_KEY_PAIR_SPEC))) { + LOGE("Invalid params spec."); + return HCF_INVALID_PARAMS; + } + + HcfResult ret = CreateDhPriKeyBySpec((const HcfDhPriKeyParamsSpec *)paramsSpec, returnPriKey); + if (ret != HCF_SUCCESS) { + LOGE("Create DH private key by spec failed."); + } + isBySpec = true; + return ret; +} + +HcfResult HcfAsyKeyGeneratorSpiDhCreate(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnSpi) +{ + if (params == NULL || returnSpi == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + HcfAsyKeyGeneratorSpiDhOpensslImpl *impl = (HcfAsyKeyGeneratorSpiDhOpensslImpl *)HcfMalloc( + sizeof(HcfAsyKeyGeneratorSpiDhOpensslImpl), 0); + if (impl == NULL) { + LOGE("Failed to allocate generator impl memroy."); + return HCF_ERR_MALLOC; + } + impl->pBits = params->bits; + impl->base.base.getClass = GetDhKeyGeneratorSpiClass; + impl->base.base.destroy = DestroyDhKeyGeneratorSpiImpl; + impl->base.engineGenerateKeyPair = EngineGenerateDhKeyPair; + impl->base.engineConvertKey = EngineConvertDhKey; + impl->base.engineGenerateKeyPairBySpec = EngineGenerateDhKeyPairBySpec; + impl->base.engineGeneratePubKeyBySpec = EngineGenerateDhPubKeyBySpec; + impl->base.engineGeneratePriKeyBySpec = EngineGenerateDhPriKeyBySpec; + + *returnSpi = (HcfAsyKeyGeneratorSpi *)impl; + return HCF_SUCCESS; +} \ No newline at end of file diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/dh_common_param_spec_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/dh_common_param_spec_generator_openssl.c new file mode 100644 index 0000000..0ba99bd --- /dev/null +++ b/plugin/openssl_plugin/key/asy_key_generator/src/dh_common_param_spec_generator_openssl.c @@ -0,0 +1,253 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dh_common_param_spec_generator_openssl.h" +#include "dh_openssl_common.h" +#include "securec.h" + +#include "log.h" +#include "memory.h" +#include "openssl_adapter.h" +#include "openssl_class.h" +#include "openssl_common.h" +#include "utils.h" + +#define PARAMS_NUM_TWO 2 +#define PARAMS_NUM_THREE 3 + +static void FreeCtx(EVP_PKEY_CTX *paramsCtx, EVP_PKEY *paramsPkey, EVP_PKEY_CTX *pkeyCtx) +{ + if (paramsCtx != NULL) { + Openssl_EVP_PKEY_CTX_free(paramsCtx); + } + if (paramsPkey != NULL) { + Openssl_EVP_PKEY_free(paramsPkey); + } + if (pkeyCtx != NULL) { + Openssl_EVP_PKEY_CTX_free(pkeyCtx); + } +} + +static HcfResult GenDhParamsPkey(uint32_t pLen, EVP_PKEY **paramsPkey) +{ + EVP_PKEY_CTX *paramsCtx = Openssl_EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL); + if (paramsCtx == NULL) { + HcfPrintOpensslError(); + LOGE("Create params ctx failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfResult ret = HCF_SUCCESS; + do { + if (Openssl_EVP_PKEY_paramgen_init(paramsCtx) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + LOGE("Params ctx generate init failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_CTX_set_dh_paramgen_prime_len(paramsCtx, pLen) <= 0) { + HcfPrintOpensslError(); + LOGE("Set length of bits to params ctx failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_paramgen(paramsCtx, paramsPkey) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + LOGE("Generate params pkey failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + } while (0); + Openssl_EVP_PKEY_CTX_free(paramsCtx); + return ret; +} + +static HcfResult GenerateDhUnknownGroupEvpKey(int32_t pLen, int32_t skLen, EVP_PKEY **ppkey) +{ + EVP_PKEY *paramsPkey = NULL; + EVP_PKEY_CTX *pkeyCtx = NULL; + OSSL_PARAM params[PARAMS_NUM_TWO]; + if (skLen != 0) { + params[0] = Openssl_OSSL_PARAM_construct_int("priv_len", &skLen); + params[1] = Openssl_OSSL_PARAM_construct_end(); + } else { + params[0] = Openssl_OSSL_PARAM_construct_end(); + } + HcfResult ret = HCF_SUCCESS; + do { + if (GenDhParamsPkey(pLen, ¶msPkey) != HCF_SUCCESS) { + LOGE("Generate params pkey failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + pkeyCtx = Openssl_EVP_PKEY_CTX_new_from_pkey(NULL, paramsPkey, NULL); + if (pkeyCtx == NULL) { + HcfPrintOpensslError(); + LOGE("Create pkey ctx failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_keygen_init(pkeyCtx) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + LOGE("Key ctx generate init failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_CTX_set_params(pkeyCtx, params) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + LOGE("Set params failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_keygen(pkeyCtx, ppkey) != HCF_OPENSSL_SUCCESS) { + HcfPrintOpensslError(); + LOGE("Generate pkey failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + } while (0); + FreeCtx(NULL, paramsPkey, pkeyCtx); + return ret; +} + +static HcfResult GenerateDhKnownGroupEvpKey(int32_t skLen, char *nidName, EVP_PKEY **ppkey) +{ + HcfResult ret = HCF_SUCCESS; + EVP_PKEY *paramsPkey = NULL; + EVP_PKEY_CTX *pkeyCtx = NULL; + EVP_PKEY_CTX *paramsCtx = NULL; + OSSL_PARAM params[PARAMS_NUM_THREE]; + + params[0] = Openssl_OSSL_PARAM_construct_utf8_string("group", nidName, 0); + if (skLen != 0) { + params[1] = Openssl_OSSL_PARAM_construct_int("priv_len", &skLen); + params[PARAMS_NUM_TWO] = Openssl_OSSL_PARAM_construct_end(); + } else { + params[1] = Openssl_OSSL_PARAM_construct_end(); + } + do { + paramsCtx = Openssl_EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); + if (paramsCtx == NULL) { + LOGE("New paramsCtx from name failed."); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_keygen_init(paramsCtx) != HCF_OPENSSL_SUCCESS) { + LOGE("ParamsCtx generate init failed."); + HcfPrintOpensslError(); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_CTX_set_params(paramsCtx, params) != HCF_OPENSSL_SUCCESS) { + LOGE("ParamsCtx set failed."); + HcfPrintOpensslError(); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (Openssl_EVP_PKEY_generate(paramsCtx, ppkey) != HCF_OPENSSL_SUCCESS) { + LOGE("Create generate failed."); + HcfPrintOpensslError(); + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + } while (0); + FreeCtx(paramsCtx, paramsPkey, pkeyCtx); + return ret; +} + +static HcfResult BuildCommonParam(EVP_PKEY *dhKey, HcfDhCommParamsSpecSpi *returnCommonParamSpec) +{ + DH *sk = Openssl_EVP_PKEY_get1_DH(dhKey); + if (sk == NULL) { + LOGE("Get dh private key from pkey failed"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + if (BigNumToBigInteger(Openssl_DH_get0_p(sk), &(returnCommonParamSpec->paramsSpec.p)) != HCF_SUCCESS) { + LOGE("BuildCommonParamPrime failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (BigNumToBigInteger(Openssl_DH_get0_g(sk), &(returnCommonParamSpec->paramsSpec.g)) != HCF_SUCCESS) { + LOGE("BuildCommonParamGenerator failed."); + HcfFree(returnCommonParamSpec->paramsSpec.p.data); + return HCF_ERR_CRYPTO_OPERATION; + } + return HCF_SUCCESS; +} + +HcfResult SetAlgName(const char *algName, char **returnAlgName) +{ + size_t srcAlgNameLen = HcfStrlen(algName); + if (!srcAlgNameLen) { + LOGE("algName is empty!"); + return HCF_INVALID_PARAMS; + } + *returnAlgName = (char *)HcfMalloc(srcAlgNameLen + 1, 0); + if (*returnAlgName == NULL) { + LOGE("algName malloc failed."); + return HCF_ERR_MALLOC; + } + if (memcpy_s(*returnAlgName, srcAlgNameLen, algName, srcAlgNameLen) != EOK) { + LOGE("memcpy algName failed."); + HcfFree(*returnAlgName); + return HCF_ERR_CRYPTO_OPERATION; + } + return HCF_SUCCESS; +} + +HcfResult HcfDhCommonParamSpecCreate(uint32_t pLen, int32_t skLen, HcfDhCommParamsSpecSpi **returnCommonParamSpec) +{ + if (returnCommonParamSpec == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + EVP_PKEY *dhKey = NULL; + char *nidName = GetNidNameByDhPLen(pLen); + if (nidName == NULL) { + if (GenerateDhUnknownGroupEvpKey(pLen, skLen, &dhKey) != HCF_SUCCESS) { + LOGE("generate dh unknown group evpKey failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + } else { + if (GenerateDhKnownGroupEvpKey(skLen, nidName, &dhKey) != HCF_SUCCESS) { + LOGE("generate dh known group evpKey failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + } + HcfDhCommParamsSpecSpi *object = (HcfDhCommParamsSpecSpi*)HcfMalloc(sizeof(HcfDhCommParamsSpecSpi), 0); + if (object == NULL) { + LOGE("build dh common params object failed."); + Openssl_EVP_PKEY_free(dhKey); + return HCF_ERR_MALLOC; + } + const char *algName = "DH"; + object->paramsSpec.base.specType = HCF_COMMON_PARAMS_SPEC; + object->paramsSpec.length = skLen; + if (SetAlgName(algName, &(object->paramsSpec.base.algName)) != HCF_SUCCESS) { + LOGE("get algName parameter failed."); + HcfFree(object); + Openssl_EVP_PKEY_free(dhKey); + return HCF_INVALID_PARAMS; + } + if (BuildCommonParam(dhKey, object)!= HCF_SUCCESS) { + LOGE("create keyPair failed."); + HcfFree(object->paramsSpec.base.algName); + HcfFree(object); + Openssl_EVP_PKEY_free(dhKey); + return HCF_ERR_CRYPTO_OPERATION; + } + *returnCommonParamSpec = object; + Openssl_EVP_PKEY_free(dhKey); + return HCF_SUCCESS; +} diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c index 0c82e32..d66d712 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c @@ -885,7 +885,7 @@ static HcfResult EngineGenerateDsaKeyPairBySpec(const HcfAsyKeyGeneratorSpi *sel } HcfResult ret = CreateDsaKeyPairBySpec(paramsSpec, returnKeyPair); if (ret != HCF_SUCCESS) { - LOGE("Create DSA key pair by spec falied."); + LOGE("Create DSA key pair by spec failed."); } return ret; } @@ -910,7 +910,7 @@ static HcfResult EngineGenerateDsaPubKeyBySpec(const HcfAsyKeyGeneratorSpi *self HcfResult ret = CreateDsaPubKeyByPubKeySpec((const HcfDsaPubKeyParamsSpec *)paramsSpec, returnPubKey); if (ret != HCF_SUCCESS) { - LOGE("Create DSA public key by spec falied."); + LOGE("Create DSA public key by spec failed."); } return ret; } @@ -933,7 +933,7 @@ static HcfResult EngineGenerateDsaPriKeyBySpec(const HcfAsyKeyGeneratorSpi *self HcfOpensslDsaPriKey *dsaSk = NULL; HcfResult ret = CreateDsaPriKeyByKeyPairSpec((const HcfDsaKeyPairParamsSpec *)paramsSpec, &dsaSk); if (ret != HCF_SUCCESS) { - LOGE("Create DSA private key by spec falied."); + LOGE("Create DSA private key by spec failed."); } else { *returnPriKey = (HcfPriKey *)dsaSk; } diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c index 78c4000..9c2c4e4 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c @@ -17,13 +17,11 @@ #include "securec.h" -#include "detailed_ecc_key_params.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "log.h" #include "memory.h" #include "openssl_adapter.h" -#include "openssl_class.h" -#include "openssl_common.h" #include "utils.h" #define OPENSSL_ECC_KEY_GENERATOR_CLASS "OPENSSL.ECC.KEY_GENERATOR_CLASS" @@ -54,35 +52,6 @@ typedef struct { int32_t curveId; } HcfAsyKeyGeneratorSpiOpensslEccImpl; -static HcfResult NewEcKeyPair(int32_t curveId, EC_KEY **returnEcKey) -{ - EC_KEY *ecKey = Openssl_EC_KEY_new_by_curve_name(curveId); - if (ecKey == NULL) { - LOGE("new ec key failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - if (Openssl_EC_KEY_generate_key(ecKey) <= 0) { - LOGE("generate ec key failed."); - Openssl_EC_KEY_free(ecKey); - return HCF_ERR_CRYPTO_OPERATION; - } - if (Openssl_EC_KEY_check_key(ecKey) <= 0) { - LOGE("check key fail."); - Openssl_EC_KEY_free(ecKey); - return HCF_ERR_CRYPTO_OPERATION; - } - *returnEcKey = ecKey; - return HCF_SUCCESS; -} - -static void FreeCurveBigNum(BIGNUM *pStd, BIGNUM *bStd, BIGNUM *xStd, BIGNUM *yStd) -{ - Openssl_BN_free(pStd); - Openssl_BN_free(bStd); - Openssl_BN_free(xStd); - Openssl_BN_free(yStd); -} - static HcfResult CheckEc224CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { BIGNUM *pStd = NULL, *bStd = NULL, *xStd = NULL, *yStd = NULL; @@ -656,119 +625,6 @@ static HcfResult CheckParamsSpecToGetCurveId(const HcfEccCommParamsSpec *ecParam return res; } -static HcfResult NewGroupFromCurveGFp(const HcfEccCommParamsSpec *ecParams, EC_GROUP **ecGroup, BN_CTX *ctx) -{ - HcfResult res = HCF_SUCCESS; - HcfECFieldFp *field = (HcfECFieldFp *)(ecParams->field); - BIGNUM *p = NULL, *a = NULL, *b = NULL; - EC_GROUP *group = NULL; - do { - if (BigIntegerToBigNum(&(field->p), &p) != HCF_SUCCESS || - BigIntegerToBigNum(&(ecParams->a), &a) != HCF_SUCCESS || - BigIntegerToBigNum(&(ecParams->b), &b) != HCF_SUCCESS) { - LOGE("BigInteger to BigNum failed"); - res = HCF_ERR_CRYPTO_OPERATION; - break; - } - group = Openssl_EC_GROUP_new_curve_GFp(p, a, b, ctx); - if (group == NULL) { - LOGE("Alloc group memory failed."); - res = HCF_ERR_CRYPTO_OPERATION; - break; - } - } while (0); - Openssl_BN_free(p); - Openssl_BN_free(a); - Openssl_BN_free(b); - - if (res == HCF_SUCCESS) { - *ecGroup = group; - return res; - } - Openssl_EC_GROUP_free(group); - return res; -} - -static HcfResult SetEcPointToGroup(const HcfEccCommParamsSpec *ecParams, EC_GROUP *group, BN_CTX *ctx) -{ - HcfResult res = HCF_SUCCESS; - BIGNUM *x = NULL, *y = NULL; - BIGNUM *order = NULL, *cofactor = NULL; - EC_POINT *generator = NULL; - cofactor = Openssl_BN_new(); - if (cofactor == NULL) { - LOGE("Alloc cofactor memory failed."); - return HCF_ERR_MALLOC; - } - do { - if (BigIntegerToBigNum(&(ecParams->g.x), &x) != HCF_SUCCESS || - BigIntegerToBigNum(&(ecParams->g.y), &y) != HCF_SUCCESS || - BigIntegerToBigNum(&(ecParams->n), &order) != HCF_SUCCESS || - !Openssl_BN_set_word(cofactor, (uint32_t)ecParams->h)) { - LOGE("BigInteger to BigNum failed."); - res = HCF_ERR_CRYPTO_OPERATION; - break; - } - generator = Openssl_EC_POINT_new(group); - if (generator == NULL) { - LOGE("Alloc group memory failed."); - res = HCF_ERR_CRYPTO_OPERATION; - break; - } - if (!Openssl_EC_POINT_set_affine_coordinates_GFp(group, generator, x, y, ctx)) { - LOGE("Openssl_EC_POINT_set_affine_coordinates_GFp failed."); - res = HCF_ERR_CRYPTO_OPERATION; - HcfPrintOpensslError(); - break; - } - - if (!Openssl_EC_GROUP_set_generator(group, generator, order, cofactor)) { - LOGE("Openssl_EC_GROUP_set_generator failed."); - res = HCF_ERR_CRYPTO_OPERATION; - HcfPrintOpensslError(); - break; - } - } while (0); - Openssl_BN_free(x); - Openssl_BN_free(y); - Openssl_BN_free(order); - Openssl_BN_free(cofactor); - Openssl_EC_POINT_free(generator); - return res; -} - -static HcfResult GenerateEcGroupWithParamsSpec(const HcfEccCommParamsSpec *ecParams, EC_GROUP **ecGroup) -{ - if (ecGroup == NULL) { - LOGE("Invalid input parameters."); - return HCF_INVALID_PARAMS; - } - HcfResult res = HCF_SUCCESS; - EC_GROUP *group = NULL; - BN_CTX *ctx = NULL; - ctx = Openssl_BN_CTX_new(); - if (ctx == NULL) { - LOGE("Alloc ctx memory failed."); - res = HCF_ERR_MALLOC; - return res; - } - res = NewGroupFromCurveGFp(ecParams, &group, ctx); - if (res != HCF_SUCCESS) { - LOGE("New Ec group fail"); - Openssl_BN_CTX_free(ctx); - return res; - } - res = SetEcPointToGroup(ecParams, group, ctx); - if (res != HCF_SUCCESS) { - Openssl_BN_CTX_free(ctx); - Openssl_EC_GROUP_free(group); - LOGE("Set Ec point fail"); - return res; - } - *ecGroup = group; - return res; -} - static HcfResult GenerateEcKeyWithParamsSpec(const HcfEccCommParamsSpec *ecParams, EC_KEY **returnKey) { if (ecParams == NULL || returnKey == NULL) { @@ -777,8 +633,8 @@ static HcfResult GenerateEcKeyWithParamsSpec(const HcfEccCommParamsSpec *ecParam } EC_KEY *ecKey = NULL; int32_t curveId = 0; - HcfResult res = CheckParamsSpecToGetCurveId(ecParams, &curveId); - if (res == HCF_SUCCESS && curveId != 0) { + HcfResult ret = CheckParamsSpecToGetCurveId(ecParams, &curveId); + if (ret == HCF_SUCCESS && curveId != 0) { ecKey = Openssl_EC_KEY_new_by_curve_name(curveId); LOGD("generate EC_KEY by curve name"); if (ecKey == NULL) { @@ -787,10 +643,10 @@ static HcfResult GenerateEcKeyWithParamsSpec(const HcfEccCommParamsSpec *ecParam } } else { EC_GROUP *group = NULL; - res = GenerateEcGroupWithParamsSpec(ecParams, &group); - if (res != HCF_SUCCESS) { + ret = GenerateEcGroupWithParamsSpec(ecParams, &group); + if (ret != HCF_SUCCESS) { LOGE("GenerateEcGroupWithParamsSpec failed."); - return res; + return ret; } ecKey = Openssl_EC_KEY_new(); if (ecKey == NULL) { @@ -812,137 +668,17 @@ static HcfResult GenerateEcKeyWithParamsSpec(const HcfEccCommParamsSpec *ecParam return HCF_SUCCESS; } -static HcfResult InitEcKeyByPubKey(const HcfPoint *pubKey, EC_KEY *ecKey) -{ - const EC_GROUP *group = Openssl_EC_KEY_get0_group(ecKey); - if (group == NULL) { - LOGE("Not find group from ecKey."); - return HCF_ERR_CRYPTO_OPERATION; - } - EC_POINT *point = Openssl_EC_POINT_new(group); - if (point == NULL) { - LOGE("New ec point failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - BIGNUM *pkX = NULL, *pkY = NULL; - if (BigIntegerToBigNum(&(pubKey->x), &pkX) != HCF_SUCCESS || - BigIntegerToBigNum(&(pubKey->y), &pkY) != HCF_SUCCESS) { - LOGE("BigInteger to BigNum failed."); - Openssl_EC_POINT_free(point); - Openssl_BN_free(pkX); - Openssl_BN_free(pkY); - return HCF_ERR_CRYPTO_OPERATION; - } - - // only support fp point. - // can use EC_POINT_set_affine_coordinates() set x and y by group, deep copy. - int res = Openssl_EC_POINT_set_affine_coordinates_GFp(group, point, pkX, pkY, NULL); - Openssl_BN_free(pkX); - Openssl_BN_free(pkY); - - if (res != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_POINT_set_affine_coordinates_GFp failed."); - Openssl_EC_POINT_free(point); - return HCF_ERR_CRYPTO_OPERATION; - } - res = Openssl_EC_KEY_set_public_key(ecKey, point); - if (res != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_KEY_set_public_key failed."); - Openssl_EC_POINT_free(point); - return HCF_ERR_CRYPTO_OPERATION; - } - Openssl_EC_POINT_free(point); - return HCF_SUCCESS; -} - -static HcfResult InitEcKeyByPriKey(const HcfBigInteger *priKey, EC_KEY *ecKey) -{ - BIGNUM *sk = NULL; - if (BigIntegerToBigNum(priKey, &sk) != HCF_SUCCESS) { - LOGE("BigInteger to BigNum failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - int32_t res = (int32_t)Openssl_EC_KEY_set_private_key(ecKey, sk); - if (res != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_KEY_set_private_key failed."); - Openssl_BN_free(sk); - return HCF_ERR_CRYPTO_OPERATION; - } - Openssl_BN_free(sk); - return HCF_SUCCESS; -} - -static HcfResult SetEcPubKeyFromPriKey(const HcfBigInteger *priKey, EC_KEY *ecKey) -{ - const EC_GROUP *group = Openssl_EC_KEY_get0_group(ecKey); - if (group == NULL) { - LOGE("Not find group from ecKey."); - return HCF_ERR_CRYPTO_OPERATION; - } - BIGNUM *sk = NULL; - if (BigIntegerToBigNum(priKey, &sk) != HCF_SUCCESS) { - LOGE("BigInteger to BigNum failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - EC_POINT *point = Openssl_EC_POINT_new(group); - if (point == NULL) { - LOGE("Openssl_EC_POINT_new failed."); - Openssl_BN_free(sk); - return HCF_ERR_CRYPTO_OPERATION; - } - if (!Openssl_EC_POINT_mul(group, point, sk, NULL, NULL, NULL)) { - LOGE("Openssl_EC_POINT_new or Openssl_EC_POINT_mul failed."); - Openssl_EC_POINT_free(point); - Openssl_BN_free(sk); - return HCF_ERR_CRYPTO_OPERATION; - } - int32_t res = (int32_t)Openssl_EC_KEY_set_public_key(ecKey, point); - if (res != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_KEY_set_public_key failed."); - Openssl_EC_POINT_free(point); - Openssl_BN_free(sk); - return HCF_ERR_CRYPTO_OPERATION; - } - Openssl_EC_POINT_free(point); - Openssl_BN_free(sk); - return HCF_SUCCESS; -} - -static HcfResult SetEcKey(const HcfPoint *pubKey, const HcfBigInteger *priKey, EC_KEY *ecKey) -{ - HcfResult res = HCF_SUCCESS; - if (pubKey != NULL) { - res = InitEcKeyByPubKey(pubKey, ecKey); - if (res != HCF_SUCCESS) { - LOGE("InitEcKeyByPubKey failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - } - if (priKey != NULL) { - res = InitEcKeyByPriKey(priKey, ecKey); - if (res != HCF_SUCCESS) { - LOGE("InitEcKeyByPriKey failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - if (pubKey == NULL) { - res = SetEcPubKeyFromPriKey(priKey, ecKey); - if (res != HCF_SUCCESS) { - LOGE("SetEcPubKeyFromPriKey failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - } - } - return res; -} - static HcfResult NewEcKeyPairWithCommSpec(const HcfEccCommParamsSpec *ecParams, EC_KEY **returnEckey) { - LOGD("start gen EC key by comm spec"); + if (ecParams == NULL || returnEckey == NULL) { + LOGE("Invalid input parameters."); + return HCF_INVALID_PARAMS; + } EC_KEY *ecKey = NULL; - HcfResult res = GenerateEcKeyWithParamsSpec(ecParams, &ecKey); - if (res != HCF_SUCCESS) { + HcfResult ret = GenerateEcKeyWithParamsSpec(ecParams, &ecKey); + if (ret != HCF_SUCCESS) { LOGE("generate EC key fails"); - return res; + return ret; } if (Openssl_EC_KEY_generate_key(ecKey) != HCF_OPENSSL_SUCCESS) { LOGE("Openssl_EC_KEY_generate_key failed."); @@ -955,21 +691,24 @@ static HcfResult NewEcKeyPairWithCommSpec(const HcfEccCommParamsSpec *ecParams, Openssl_EC_KEY_free(ecKey); return HCF_ERR_CRYPTO_OPERATION; } - LOGD("Gen EC_key and check success"); *returnEckey = ecKey; - return res; + return ret; } static HcfResult NewEcPubKeyWithPubSpec(const HcfEccPubKeyParamsSpec *ecParams, EC_KEY **returnEcKey) { - EC_KEY *ecKey = NULL; - HcfResult res = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey); - if (res != HCF_SUCCESS) { - LOGE("generate EC key fails"); - return res; + if (ecParams == NULL || returnEcKey == NULL) { + LOGE("Invalid input parameters."); + return HCF_INVALID_PARAMS; } - res = SetEcKey(&(ecParams->pk), NULL, ecKey); - if (res != HCF_SUCCESS) { + EC_KEY *ecKey = NULL; + HcfResult ret = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey); + if (ret != HCF_SUCCESS) { + LOGE("generate EC key fails"); + return ret; + } + ret = SetEcKey(&(ecParams->pk), NULL, ecKey); + if (ret != HCF_SUCCESS) { LOGE("Set pub ecKey failed."); Openssl_EC_KEY_free(ecKey); return HCF_ERR_CRYPTO_OPERATION; @@ -981,19 +720,23 @@ static HcfResult NewEcPubKeyWithPubSpec(const HcfEccPubKeyParamsSpec *ecParams, return HCF_ERR_CRYPTO_OPERATION; } *returnEcKey = ecKey; - return res; + return ret; } static HcfResult NewEcPriKeyWithPriSpec(const HcfEccPriKeyParamsSpec *ecParams, EC_KEY **returnEcKey) { - EC_KEY *ecKey = NULL; - HcfResult res = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey); - if (res != HCF_SUCCESS) { - LOGE("generate EC key fails"); - return res; + if (ecParams == NULL || returnEcKey == NULL) { + LOGE("Invalid input parameters."); + return HCF_INVALID_PARAMS; } - res = SetEcKey(NULL, &(ecParams->sk), ecKey); - if (res != HCF_SUCCESS) { + EC_KEY *ecKey = NULL; + HcfResult ret = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey); + if (ret != HCF_SUCCESS) { + LOGE("generate EC key fails"); + return ret; + } + ret = SetEcKey(NULL, &(ecParams->sk), ecKey); + if (ret != HCF_SUCCESS) { LOGE("Set pri ecKey failed."); Openssl_EC_KEY_free(ecKey); return HCF_ERR_CRYPTO_OPERATION; @@ -1005,24 +748,28 @@ static HcfResult NewEcPriKeyWithPriSpec(const HcfEccPriKeyParamsSpec *ecParams, return HCF_ERR_CRYPTO_OPERATION; } *returnEcKey = ecKey; - return res; + return ret; } static HcfResult NewEcKeyWithKeyPairSpec(const HcfEccKeyPairParamsSpec *ecParams, EC_KEY **returnEcKey, bool needPrivate) { + if (ecParams == NULL || returnEcKey == NULL) { + LOGE("Invalid input parameters."); + return HCF_INVALID_PARAMS; + } EC_KEY *ecKey = NULL; - HcfResult res = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey); - if (res != HCF_SUCCESS) { + HcfResult ret = GenerateEcKeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey); + if (ret != HCF_SUCCESS) { LOGE("generate EC key fails"); - return res; + return ret; } if (needPrivate) { - res = SetEcKey(&(ecParams->pk), &(ecParams->sk), ecKey); + ret = SetEcKey(&(ecParams->pk), &(ecParams->sk), ecKey); } else { - res = SetEcKey(&(ecParams->pk), NULL, ecKey); + ret = SetEcKey(&(ecParams->pk), NULL, ecKey); } - if (res != HCF_SUCCESS) { + if (ret != HCF_SUCCESS) { LOGE("SetEcKey failed."); Openssl_EC_KEY_free(ecKey); return HCF_ERR_CRYPTO_OPERATION; @@ -1034,7 +781,58 @@ static HcfResult NewEcKeyWithKeyPairSpec(const HcfEccKeyPairParamsSpec *ecParams return HCF_ERR_CRYPTO_OPERATION; } *returnEcKey = ecKey; - return res; + return ret; +} + +static HcfResult GenKeyPairEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) +{ + HcfResult ret = HCF_INVALID_PARAMS; + switch (params->specType) { + case HCF_COMMON_PARAMS_SPEC: + ret = NewEcKeyPairWithCommSpec((HcfEccCommParamsSpec *)params, ecKey); + break; + case HCF_KEY_PAIR_SPEC: + ret = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true); + break; + default: + LOGE("Invaild input spec to gen key pair."); + break; + } + return ret; +} + +static HcfResult GenPubKeyEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) +{ + HcfResult ret = HCF_INVALID_PARAMS; + switch (params->specType) { + case HCF_PUBLIC_KEY_SPEC: + ret = NewEcPubKeyWithPubSpec((HcfEccPubKeyParamsSpec *)params, ecKey); + break; + case HCF_KEY_PAIR_SPEC: + ret = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, false); + break; + default: + LOGE("Invaild input spec to gen pub key"); + break; + } + return ret; +} + +static HcfResult GenPriKeyEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) +{ + HcfResult ret = HCF_INVALID_PARAMS; + switch (params->specType) { + case HCF_PRIVATE_KEY_SPEC: + ret = NewEcPriKeyWithPriSpec((HcfEccPriKeyParamsSpec *)params, ecKey); + break; + case HCF_KEY_PAIR_SPEC: + ret = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true); + break; + default: + LOGE("Invaild input spec to gen pri key"); + break; + } + return ret; } static const char *GetEccKeyPairGeneratorClass(void) @@ -1248,205 +1046,6 @@ static void EccPriKeyClearMem(HcfPriKey *self) impl->ecKey = NULL; } -static HcfResult GetCurveGFp(const EC_GROUP *group, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) -{ - BIGNUM *p = Openssl_BN_new(); - BIGNUM *a = Openssl_BN_new(); - BIGNUM *b = Openssl_BN_new(); - if (p == NULL || a == NULL || b == NULL) { - LOGE("new BN failed."); - Openssl_BN_free(p); - Openssl_BN_free(a); - Openssl_BN_free(b); - return HCF_ERR_CRYPTO_OPERATION; - } - - if (Openssl_EC_GROUP_get_curve_GFp(group, p, a, b, NULL) != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_GROUP_get_curve_GFp failed."); - Openssl_BN_free(p); - Openssl_BN_free(a); - Openssl_BN_free(b); - return HCF_ERR_CRYPTO_OPERATION; - } - - HcfResult res = HCF_INVALID_PARAMS; - switch (item) { - case ECC_FP_P_BN: - res = BigNumToBigInteger(p, returnBigInteger); - break; - case ECC_A_BN: - res = BigNumToBigInteger(a, returnBigInteger); - break; - case ECC_B_BN: - res = BigNumToBigInteger(b, returnBigInteger); - break; - default: - break; - } - Openssl_BN_free(p); - Openssl_BN_free(a); - Openssl_BN_free(b); - return res; -} - -static HcfResult GetGenerator(const EC_GROUP *group, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) -{ - const EC_POINT *generator = Openssl_EC_GROUP_get0_generator(group); - if (generator == NULL) { - LOGE("Openssl_EC_GROUP_get0_generator failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - - BIGNUM *gX = Openssl_BN_new(); - BIGNUM *gY = Openssl_BN_new(); - if (gX == NULL || gY == NULL) { - LOGE("new BN failed."); - Openssl_BN_free(gX); - Openssl_BN_free(gY); - return HCF_ERR_CRYPTO_OPERATION; - } - - if (Openssl_EC_POINT_get_affine_coordinates_GFp(group, generator, gX, gY, NULL) != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); - Openssl_BN_free(gX); - Openssl_BN_free(gY); - return HCF_ERR_CRYPTO_OPERATION; - } - - HcfResult res = HCF_INVALID_PARAMS; - switch (item) { - case ECC_G_X_BN: - res = BigNumToBigInteger(gX, returnBigInteger); - break; - case ECC_G_Y_BN: - res = BigNumToBigInteger(gY, returnBigInteger); - break; - default: - break; - } - Openssl_BN_free(gX); - Openssl_BN_free(gY); - return res; -} - -static HcfResult GetOrder(const EC_GROUP *group, HcfBigInteger *returnBigInteger) -{ - BIGNUM *order = Openssl_BN_new(); - if (order == NULL) { - LOGE("new BN failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - - if (Openssl_EC_GROUP_get_order(group, order, NULL) != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); - Openssl_BN_free(order); - return HCF_ERR_CRYPTO_OPERATION; - } - - HcfResult res = BigNumToBigInteger(order, returnBigInteger); - Openssl_BN_free(order); - return res; -} - -static HcfResult GetCofactor(const EC_GROUP *group, int *returnCofactor) -{ - BIGNUM *cofactor = Openssl_BN_new(); - if (cofactor == NULL) { - LOGE("new BN failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - - if (Openssl_EC_GROUP_get_cofactor(group, cofactor, NULL) != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); - Openssl_BN_free(cofactor); - return HCF_ERR_CRYPTO_OPERATION; - } - - *returnCofactor = (int)(Openssl_BN_get_word(cofactor)); - // cofactor should not be zero. - if (*returnCofactor == 0) { - LOGE("Openssl_BN_get_word failed."); - Openssl_BN_free(cofactor); - return HCF_ERR_CRYPTO_OPERATION; - } - Openssl_BN_free(cofactor); - return HCF_SUCCESS; -} - -static HcfResult GetFieldSize(const EC_GROUP *group, int32_t *fieldSize) -{ - *fieldSize = Openssl_EC_GROUP_get_degree(group); - if (*fieldSize == 0) { - LOGE("Openssl_EC_GROUP_get_degree failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - return HCF_SUCCESS; -} - -static HcfResult GetPubKeyXOrY(const EC_GROUP *group, const EC_POINT *point, const AsyKeySpecItem item, - HcfBigInteger *returnBigInteger) -{ - BIGNUM *pkX = Openssl_BN_new(); - BIGNUM *pkY = Openssl_BN_new(); - if (pkX == NULL || pkY == NULL) { - LOGE("new BN failed."); - Openssl_BN_free(pkX); - Openssl_BN_free(pkY); - return HCF_ERR_CRYPTO_OPERATION; - } - - if (Openssl_EC_POINT_get_affine_coordinates_GFp(group, point, pkX, pkY, NULL) != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); - Openssl_BN_free(pkX); - Openssl_BN_free(pkY); - return HCF_ERR_CRYPTO_OPERATION; - } - - HcfResult res = HCF_INVALID_PARAMS; - switch (item) { - case ECC_PK_X_BN: - res = BigNumToBigInteger(pkX, returnBigInteger); - break; - case ECC_PK_Y_BN: - res = BigNumToBigInteger(pkY, returnBigInteger); - break; - default: - break; - } - Openssl_BN_free(pkX); - Openssl_BN_free(pkY); - return res; -} - -static HcfResult GetFieldType(const HcfKey *self, const bool isPrivate, char **returnString) -{ - char *fieldType = NULL; - if (isPrivate) { - fieldType = ((HcfOpensslEccPriKey *)self)->fieldType; - } else { - fieldType = ((HcfOpensslEccPubKey *)self)->fieldType; - } - - if (fieldType == NULL) { - LOGE("No fieldType in EccPubKey struct."); - return HCF_INVALID_PARAMS; - } - - size_t len = HcfStrlen(fieldType); - if (!len) { - LOGE("fieldType is empty!"); - return HCF_INVALID_PARAMS; - } - *returnString = (char *)HcfMalloc(len + 1, 0); - if (*returnString == NULL) { - LOGE("Alloc returnString memory failed."); - return HCF_ERR_MALLOC; - } - (void)memcpy_s(*returnString, len, fieldType, len); - - return HCF_SUCCESS; -} - static HcfResult GetCurveName(const HcfKey *self, const bool isPriavte, char **returnString) { int32_t curveId = 0; @@ -1498,28 +1097,6 @@ static HcfResult CheckEcKeySelf(const HcfKey *self, bool *isPrivate) } } -static HcfResult GetPkSkBigInteger(const HcfKey *self, bool isPrivate, - const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) -{ - HcfResult res = HCF_INVALID_PARAMS; - if (item == ECC_SK_BN) { - if (!isPrivate) { - LOGE("ecc pub key has no private key spec item"); - return res; - } - res = BigNumToBigInteger(Openssl_EC_KEY_get0_private_key(((HcfOpensslEccPriKey *)self)->ecKey), - returnBigInteger); - } else { - if (isPrivate) { - LOGE("ecc pri key cannot get pub key spec item"); - return res; - } - res = GetPubKeyXOrY(Openssl_EC_KEY_get0_group(((HcfOpensslEccPubKey *)self)->ecKey), - Openssl_EC_KEY_get0_public_key(((HcfOpensslEccPubKey *)self)->ecKey), item, returnBigInteger); - } - return res; -} - static HcfResult GetEcKeySpecBigInteger(const HcfKey *self, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) { @@ -1935,57 +1512,6 @@ static HcfResult EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair * return HCF_SUCCESS; } -static HcfResult GenKeyPairEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) -{ - HcfResult res = HCF_INVALID_PARAMS; - switch (params->specType) { - case HCF_COMMON_PARAMS_SPEC: - res = NewEcKeyPairWithCommSpec((HcfEccCommParamsSpec *)params, ecKey); - break; - case HCF_KEY_PAIR_SPEC: - res = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true); - break; - default: - LOGE("Invaild input spec to gen key pair."); - break; - } - return res; -} - -static HcfResult GenPubKeyEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) -{ - HcfResult res = HCF_INVALID_PARAMS; - switch (params->specType) { - case HCF_PUBLIC_KEY_SPEC: - res = NewEcPubKeyWithPubSpec((HcfEccPubKeyParamsSpec *)params, ecKey); - break; - case HCF_KEY_PAIR_SPEC: - res = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, false); - break; - default: - LOGE("Invaild input spec to gen pub key"); - break; - } - return res; -} - -static HcfResult GenPriKeyEcKeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) -{ - HcfResult res = HCF_INVALID_PARAMS; - switch (params->specType) { - case HCF_PRIVATE_KEY_SPEC: - res = NewEcPriKeyWithPriSpec((HcfEccPriKeyParamsSpec *)params, ecKey); - break; - case HCF_KEY_PAIR_SPEC: - res = NewEcKeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true); - break; - default: - LOGE("Invaild input spec to gen pri key"); - break; - } - return res; -} - static HcfResult EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params, HcfKeyPair **returnKeyPair) { diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/ecc_common_param_spec_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/ecc_common_param_spec_generator_openssl.c index fddc1a7..38c147e 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/ecc_common_param_spec_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/ecc_common_param_spec_generator_openssl.c @@ -16,7 +16,7 @@ #include "ecc_common_param_spec_generator_openssl.h" #include "securec.h" -#include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "log.h" #include "memory.h" #include "openssl_adapter.h" @@ -66,17 +66,35 @@ static HcfResult GetCofactor(const EC_GROUP *group, int32_t *returnCofactor) return HCF_SUCCESS; } -static HcfResult BuildCommonParamPart(const EC_GROUP *ecGroup, HcfEccCommParamsSpecSpi *returnCommonParamSpec) +static EC_POINT *BuildEcPoint(const EC_GROUP *ecGroup) { EC_POINT *point = Openssl_EC_POINT_new(ecGroup); if (point == NULL) { LOGE("new ec point failed."); - return HCF_ERR_MALLOC; + return NULL; } - if (!Openssl_EC_POINT_copy(point, Openssl_EC_GROUP_get0_generator(ecGroup))) { + const EC_POINT *tmpPoint = Openssl_EC_GROUP_get0_generator(ecGroup); + if (tmpPoint == NULL) { + LOGE("get ec generator failed."); + Openssl_EC_POINT_free(point); + return NULL; + } + if (!Openssl_EC_POINT_copy(point, tmpPoint)) { LOGE("ec point copy failed."); Openssl_EC_POINT_free(point); - return HCF_ERR_CRYPTO_OPERATION; + return NULL; + } + + return point; +} + +static HcfResult BuildCommonParamPart(const EC_GROUP *ecGroup, HcfEccCommParamsSpecSpi *returnCommonParamSpec) +{ + EC_POINT *point = NULL; + point = BuildEcPoint(ecGroup); + if (point == NULL) { + LOGE("build ec point failed."); + return HCF_ERR_MALLOC; } BIGNUM *x = Openssl_BN_new(); if (x == NULL) { @@ -92,7 +110,6 @@ static HcfResult BuildCommonParamPart(const EC_GROUP *ecGroup, HcfEccCommParamsS return HCF_ERR_MALLOC; } HcfResult ret = HCF_SUCCESS; - do { if (!Openssl_EC_POINT_get_affine_coordinates_GFp(ecGroup, point, x, y, NULL)) { LOGE("EC_POINT_get_affine_coordinates_GFp failed."); @@ -110,7 +127,6 @@ static HcfResult BuildCommonParamPart(const EC_GROUP *ecGroup, HcfEccCommParamsS break; } } while (0); - Openssl_BN_free(x); Openssl_BN_free(y); Openssl_EC_POINT_free(point); @@ -232,6 +248,24 @@ static HcfEccCommParamsSpecSpi *BuildEccCommonParamObject(void) return spi; } +static void FreeEccCommParamObject(HcfEccCommParamsSpecSpi *spec) +{ + if (spec == NULL) { + LOGE("Invalid input parameter."); + return; + } + HcfFree(spec->paramsSpec.base.algName); + spec->paramsSpec.base.algName = NULL; + if (spec->paramsSpec.field != NULL) { + HcfFree(spec->paramsSpec.field->fieldType); + spec->paramsSpec.field->fieldType = NULL; + HcfFree(spec->paramsSpec.field); + spec->paramsSpec.field = NULL; + } + HcfFree(spec); + spec = NULL; +} + HcfResult HcfECCCommonParamSpecCreate(HcfAsyKeyGenParams *params, HcfEccCommParamsSpecSpi **returnCommonParamSpec) { if ((params == NULL) || (returnCommonParamSpec == NULL)) { @@ -259,16 +293,16 @@ HcfResult HcfECCCommonParamSpecCreate(HcfAsyKeyGenParams *params, HcfEccCommPara object->paramsSpec.base.specType = HCF_COMMON_PARAMS_SPEC; if (GetAlgNameByBits(params->bits, &(object->paramsSpec.base.algName)) != HCF_SUCCESS) { LOGE("get algName parameter failed."); - Openssl_EC_GROUP_free(ecGroup); - HcfFree(object); + FreeEccCommParamObject(object); object = NULL; + Openssl_EC_GROUP_free(ecGroup); return HCF_INVALID_PARAMS; } if (BuildCommonParam(ecGroup, object)!= HCF_SUCCESS) { LOGE("create keyPair failed."); - Openssl_EC_GROUP_free(ecGroup); - HcfFree(object); + FreeEccCommParamObject(object); object = NULL; + Openssl_EC_GROUP_free(ecGroup); return HCF_ERR_CRYPTO_OPERATION; } *returnCommonParamSpec = object; diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c index 6809cea..673054c 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c @@ -15,13 +15,11 @@ #include "sm2_asy_key_generator_openssl.h" #include "securec.h" -#include "openssl_adapter.h" -#include "openssl_class.h" -#include "openssl_common.h" -#include "detailed_ecc_key_params.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "log.h" #include "memory.h" +#include "openssl_adapter.h" #include "utils.h" #define OPENSSL_SM2_256_BITS 256 @@ -37,35 +35,6 @@ typedef struct { int32_t curveId; } HcfAsyKeyGeneratorSpiOpensslSm2Impl; -static HcfResult NewEcKeyPair(int32_t curveId, EC_KEY **returnEcKey) -{ - EC_KEY *ecKey = Openssl_EC_KEY_new_by_curve_name(curveId); - if (ecKey == NULL) { - LOGE("new ec key failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - if (Openssl_EC_KEY_generate_key(ecKey) <= 0) { - LOGE("generate ec key failed."); - Openssl_EC_KEY_free(ecKey); - return HCF_ERR_CRYPTO_OPERATION; - } - if (Openssl_EC_KEY_check_key(ecKey) <= 0) { - LOGE("check key fail."); - Openssl_EC_KEY_free(ecKey); - return HCF_ERR_CRYPTO_OPERATION; - } - *returnEcKey = ecKey; - return HCF_SUCCESS; -} - -static void FreeCurveBigNum(BIGNUM *pStd, BIGNUM *bStd, BIGNUM *xStd, BIGNUM *yStd) -{ - Openssl_BN_free(pStd); - Openssl_BN_free(bStd); - Openssl_BN_free(xStd); - Openssl_BN_free(yStd); -} - static HcfResult CheckSm256CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y) { BIGNUM *pStd = NULL, *bStd = NULL, *xStd = NULL, *yStd = NULL; @@ -116,116 +85,6 @@ static HcfResult CheckParamsSpecToGetCurveId(const HcfEccCommParamsSpec *ecParam return ret; } -static HcfResult NewGroupFromCurveGFp(const HcfEccCommParamsSpec *ecParams, EC_GROUP **ecGroup, BN_CTX *ctx) -{ - HcfResult ret = HCF_SUCCESS; - HcfECFieldFp *field = (HcfECFieldFp *)(ecParams->field); - BIGNUM *p = NULL, *a = NULL, *b = NULL; - EC_GROUP *group = NULL; - do { - if (BigIntegerToBigNum(&(field->p), &p) != HCF_SUCCESS || - BigIntegerToBigNum(&(ecParams->a), &a) != HCF_SUCCESS || - BigIntegerToBigNum(&(ecParams->b), &b) != HCF_SUCCESS) { - LOGE("BigInteger to BigNum failed"); - ret = HCF_ERR_CRYPTO_OPERATION; - break; - } - group = Openssl_EC_GROUP_new_curve_GFp(p, a, b, ctx); - if (group == NULL) { - LOGE("Alloc group memory failed."); - ret = HCF_ERR_CRYPTO_OPERATION; - break; - } - } while (0); - Openssl_BN_free(p); - Openssl_BN_free(a); - Openssl_BN_free(b); - - if (ret != HCF_SUCCESS) { - Openssl_EC_GROUP_free(group); - return ret; - } - *ecGroup = group; - return ret; -} - -static HcfResult SetSm2PointToGroup(const HcfEccCommParamsSpec *ecParams, EC_GROUP *group, BN_CTX *ctx) -{ - HcfResult ret = HCF_SUCCESS; - BIGNUM *x = NULL, *y = NULL; - BIGNUM *order = NULL; - EC_POINT *generator = NULL; - BIGNUM *cofactor = Openssl_BN_new(); - if (cofactor == NULL) { - LOGE("Alloc cofactor memory failed."); - return HCF_ERR_MALLOC; - } - do { - if (BigIntegerToBigNum(&(ecParams->g.x), &x) != HCF_SUCCESS || - BigIntegerToBigNum(&(ecParams->g.y), &y) != HCF_SUCCESS || - BigIntegerToBigNum(&(ecParams->n), &order) != HCF_SUCCESS || - !Openssl_BN_set_word(cofactor, (uint32_t)ecParams->h)) { - LOGE("BigInteger to BigNum failed."); - ret = HCF_ERR_CRYPTO_OPERATION; - break; - } - generator = Openssl_EC_POINT_new(group); - if (generator == NULL) { - LOGE("Alloc group memory failed."); - ret = HCF_ERR_CRYPTO_OPERATION; - break; - } - if (!Openssl_EC_POINT_set_affine_coordinates_GFp(group, generator, x, y, ctx)) { - LOGE("Openssl_EC_POINT_set_affine_coordinates_GFp failed."); - ret = HCF_ERR_CRYPTO_OPERATION; - HcfPrintOpensslError(); - break; - } - - if (!Openssl_EC_GROUP_set_generator(group, generator, order, cofactor)) { - LOGE("Openssl_EC_GROUP_set_generator failed."); - ret = HCF_ERR_CRYPTO_OPERATION; - HcfPrintOpensslError(); - break; - } - } while (0); - Openssl_BN_free(x); - Openssl_BN_free(y); - Openssl_BN_free(order); - Openssl_BN_free(cofactor); - Openssl_EC_POINT_free(generator); - return ret; -} - -static HcfResult GenerateSm2GroupWithParamsSpec(const HcfEccCommParamsSpec *ecParams, EC_GROUP **ecGroup) -{ - if (ecParams == NULL || ecGroup == NULL) { - LOGE("Invalid input parameters."); - return HCF_INVALID_PARAMS; - } - EC_GROUP *group = NULL; - BN_CTX *ctx = Openssl_BN_CTX_new(); - if (ctx == NULL) { - LOGE("Alloc ctx memory failed."); - return HCF_ERR_MALLOC; - } - HcfResult ret = NewGroupFromCurveGFp(ecParams, &group, ctx); - if (ret != HCF_SUCCESS) { - LOGE("New Ec group fail"); - Openssl_BN_CTX_free(ctx); - return ret; - } - ret = SetSm2PointToGroup(ecParams, group, ctx); - if (ret != HCF_SUCCESS) { - Openssl_BN_CTX_free(ctx); - Openssl_EC_GROUP_free(group); - LOGE("Set Ec point fail"); - return ret; - } - *ecGroup = group; - return ret; -} - static HcfResult GenerateSm2KeyWithParamsSpec(const HcfEccCommParamsSpec *ecParams, EC_KEY **returnKey) { if (ecParams == NULL || returnKey == NULL) { @@ -244,9 +103,9 @@ static HcfResult GenerateSm2KeyWithParamsSpec(const HcfEccCommParamsSpec *ecPara } } else { EC_GROUP *group = NULL; - ret = GenerateSm2GroupWithParamsSpec(ecParams, &group); + ret = GenerateEcGroupWithParamsSpec(ecParams, &group); if (ret != HCF_SUCCESS) { - LOGE("GenerateSm2GroupWithParamsSpec failed."); + LOGE("GenerateEcGroupWithParamsSpec failed."); return ret; } ecKey = Openssl_EC_KEY_new(); @@ -269,130 +128,6 @@ static HcfResult GenerateSm2KeyWithParamsSpec(const HcfEccCommParamsSpec *ecPara return HCF_SUCCESS; } -static HcfResult InitEcKeyByPubKey(const HcfPoint *pubKey, EC_KEY *ecKey) -{ - const EC_GROUP *group = Openssl_EC_KEY_get0_group(ecKey); - if (group == NULL) { - LOGE("Not find group from ecKey."); - return HCF_ERR_CRYPTO_OPERATION; - } - EC_POINT *point = Openssl_EC_POINT_new(group); - if (point == NULL) { - LOGE("New ec point failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - BIGNUM *pkX = NULL, *pkY = NULL; - if (BigIntegerToBigNum(&(pubKey->x), &pkX) != HCF_SUCCESS || - BigIntegerToBigNum(&(pubKey->y), &pkY) != HCF_SUCCESS) { - LOGE("BigInteger to BigNum failed."); - Openssl_EC_POINT_free(point); - Openssl_BN_free(pkX); - Openssl_BN_free(pkY); - return HCF_ERR_CRYPTO_OPERATION; - } - - // only support fp point. - // can use EC_POINT_set_affine_coordinates() set x and y by group, deep copy. - int32_t ret = (int32_t)Openssl_EC_POINT_set_affine_coordinates_GFp(group, point, pkX, pkY, NULL); - Openssl_BN_free(pkX); - Openssl_BN_free(pkY); - - if (ret != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_POINT_set_affine_coordinates_GFp failed."); - Openssl_EC_POINT_free(point); - return HCF_ERR_CRYPTO_OPERATION; - } - ret = Openssl_EC_KEY_set_public_key(ecKey, point); - if (ret != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_KEY_set_public_key failed."); - Openssl_EC_POINT_free(point); - return HCF_ERR_CRYPTO_OPERATION; - } - Openssl_EC_POINT_free(point); - return HCF_SUCCESS; -} - -static HcfResult InitEcKeyByPriKey(const HcfBigInteger *priKey, EC_KEY *ecKey) -{ - BIGNUM *sk = NULL; - if (BigIntegerToBigNum(priKey, &sk) != HCF_SUCCESS) { - LOGE("BigInteger to BigNum failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - int32_t ret = (int32_t)Openssl_EC_KEY_set_private_key(ecKey, sk); - if (ret != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_KEY_set_private_key failed."); - Openssl_BN_free(sk); - return HCF_ERR_CRYPTO_OPERATION; - } - Openssl_BN_free(sk); - return HCF_SUCCESS; -} - -static HcfResult SetEcPubKeyFromPriKey(const HcfBigInteger *priKey, EC_KEY *ecKey) -{ - const EC_GROUP *group = Openssl_EC_KEY_get0_group(ecKey); - if (group == NULL) { - LOGE("Not find group from ecKey."); - return HCF_ERR_CRYPTO_OPERATION; - } - BIGNUM *sk = NULL; - if (BigIntegerToBigNum(priKey, &sk) != HCF_SUCCESS) { - LOGE("BigInteger to BigNum failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - HcfResult ret = HCF_SUCCESS; - EC_POINT *point = Openssl_EC_POINT_new(group); - - do { - if (point == NULL) { - LOGE("Openssl_EC_POINT_new failed."); - ret = HCF_ERR_CRYPTO_OPERATION; - break; - } - if (!Openssl_EC_POINT_mul(group, point, sk, NULL, NULL, NULL)) { - LOGE("Openssl_EC_POINT_new or Openssl_EC_POINT_mul failed."); - ret = HCF_ERR_CRYPTO_OPERATION; - break; - } - if (!Openssl_EC_KEY_set_public_key(ecKey, point)) { - LOGE("Openssl_EC_KEY_set_public_key failed."); - ret = HCF_ERR_CRYPTO_OPERATION; - } - } while (0); - - Openssl_EC_POINT_free(point); - Openssl_BN_free(sk); - return ret; -} - -static HcfResult SetEcKey(const HcfPoint *pubKey, const HcfBigInteger *priKey, EC_KEY *ecKey) -{ - HcfResult ret = HCF_SUCCESS; - if (pubKey != NULL) { - ret = InitEcKeyByPubKey(pubKey, ecKey); - if (ret != HCF_SUCCESS) { - LOGE("InitEcKeyByPubKey failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - } - if (priKey != NULL) { - ret = InitEcKeyByPriKey(priKey, ecKey); - if (ret != HCF_SUCCESS) { - LOGE("InitEcKeyByPriKey failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - if (pubKey == NULL) { - ret = SetEcPubKeyFromPriKey(priKey, ecKey); - if (ret != HCF_SUCCESS) { - LOGE("SetEcPubKeyFromPriKey failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - } - } - return ret; -} - static HcfResult NewSm2KeyPairWithCommSpec(const HcfEccCommParamsSpec *ecParams, EC_KEY **returnEckey) { if (ecParams == NULL || returnEckey == NULL) { @@ -509,6 +244,57 @@ static HcfResult NewSm2KeyWithKeyPairSpec(const HcfEccKeyPairParamsSpec *ecParam return ret; } +static HcfResult GenKeyPairSm2KeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) +{ + HcfResult ret = HCF_INVALID_PARAMS; + switch (params->specType) { + case HCF_COMMON_PARAMS_SPEC: + ret = NewSm2KeyPairWithCommSpec((HcfEccCommParamsSpec *)params, ecKey); + break; + case HCF_KEY_PAIR_SPEC: + ret = NewSm2KeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true); + break; + default: + LOGE("Invaild input spec to gen key pair."); + break; + } + return ret; +} + +static HcfResult GenPubKeySm2KeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) +{ + HcfResult ret = HCF_INVALID_PARAMS; + switch (params->specType) { + case HCF_PUBLIC_KEY_SPEC: + ret = NewSm2PubKeyWithPubSpec((HcfEccPubKeyParamsSpec *)params, ecKey); + break; + case HCF_KEY_PAIR_SPEC: + ret = NewSm2KeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, false); + break; + default: + LOGE("Invaild input spec to gen pub key"); + break; + } + return ret; +} + +static HcfResult GenPriKeySm2KeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) +{ + HcfResult ret = HCF_INVALID_PARAMS; + switch (params->specType) { + case HCF_PRIVATE_KEY_SPEC: + ret = NewSm2PriKeyWithPriSpec((HcfEccPriKeyParamsSpec *)params, ecKey); + break; + case HCF_KEY_PAIR_SPEC: + ret = NewSm2KeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true); + break; + default: + LOGE("Invaild input spec to gen pri key"); + break; + } + return ret; +} + static const char *GetSm2KeyPairGeneratorClass(void) { return OPENSSL_SM2_KEY_GENERATOR_CLASS; @@ -722,211 +508,6 @@ static void Sm2PriKeyClearMem(HcfPriKey *self) impl->ecKey = NULL; } -static HcfResult GetCurveGFp(const EC_GROUP *group, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) -{ - BIGNUM *p = Openssl_BN_new(); - BIGNUM *a = Openssl_BN_new(); - BIGNUM *b = Openssl_BN_new(); - if (p == NULL || a == NULL || b == NULL) { - LOGE("new BN failed."); - Openssl_BN_free(p); - Openssl_BN_free(a); - Openssl_BN_free(b); - return HCF_ERR_CRYPTO_OPERATION; - } - - if (Openssl_EC_GROUP_get_curve_GFp(group, p, a, b, NULL) != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_GROUP_get_curve_GFp failed."); - Openssl_BN_free(p); - Openssl_BN_free(a); - Openssl_BN_free(b); - return HCF_ERR_CRYPTO_OPERATION; - } - - HcfResult ret = HCF_INVALID_PARAMS; - switch (item) { - case ECC_FP_P_BN: - ret = BigNumToBigInteger(p, returnBigInteger); - break; - case ECC_A_BN: - ret = BigNumToBigInteger(a, returnBigInteger); - break; - case ECC_B_BN: - ret = BigNumToBigInteger(b, returnBigInteger); - break; - default: - LOGE("Invalid ecc key big number spec!"); - break; - } - Openssl_BN_free(p); - Openssl_BN_free(a); - Openssl_BN_free(b); - return ret; -} - -static HcfResult GetGenerator(const EC_GROUP *group, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) -{ - const EC_POINT *generator = Openssl_EC_GROUP_get0_generator(group); - if (generator == NULL) { - LOGE("Openssl_EC_GROUP_get0_generator failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - - BIGNUM *gX = Openssl_BN_new(); - BIGNUM *gY = Openssl_BN_new(); - if (gX == NULL || gY == NULL) { - LOGE("new BN failed."); - Openssl_BN_free(gX); - Openssl_BN_free(gY); - return HCF_ERR_CRYPTO_OPERATION; - } - - if (Openssl_EC_POINT_get_affine_coordinates_GFp(group, generator, gX, gY, NULL) != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); - Openssl_BN_free(gX); - Openssl_BN_free(gY); - return HCF_ERR_CRYPTO_OPERATION; - } - - HcfResult ret = HCF_INVALID_PARAMS; - switch (item) { - case ECC_G_X_BN: - ret = BigNumToBigInteger(gX, returnBigInteger); - break; - case ECC_G_Y_BN: - ret = BigNumToBigInteger(gY, returnBigInteger); - break; - default: - LOGE("Invalid ecc key big number spec!"); - break; - } - Openssl_BN_free(gX); - Openssl_BN_free(gY); - return ret; -} - -static HcfResult GetOrder(const EC_GROUP *group, HcfBigInteger *returnBigInteger) -{ - BIGNUM *order = Openssl_BN_new(); - if (order == NULL) { - LOGE("new BN failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - - if (Openssl_EC_GROUP_get_order(group, order, NULL) != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); - Openssl_BN_free(order); - return HCF_ERR_CRYPTO_OPERATION; - } - - HcfResult ret = BigNumToBigInteger(order, returnBigInteger); - Openssl_BN_free(order); - return ret; -} - -static HcfResult GetCofactor(const EC_GROUP *group, int *returnCofactor) -{ - BIGNUM *cofactor = Openssl_BN_new(); - if (cofactor == NULL) { - LOGE("new BN failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - - if (Openssl_EC_GROUP_get_cofactor(group, cofactor, NULL) != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); - Openssl_BN_free(cofactor); - return HCF_ERR_CRYPTO_OPERATION; - } - - *returnCofactor = (int)(Openssl_BN_get_word(cofactor)); - // cofactor should not be zero. - if (*returnCofactor == 0) { - LOGE("Openssl_BN_get_word failed."); - Openssl_BN_free(cofactor); - return HCF_ERR_CRYPTO_OPERATION; - } - Openssl_BN_free(cofactor); - return HCF_SUCCESS; -} - -static HcfResult GetFieldSize(const EC_GROUP *group, int32_t *fieldSize) -{ - *fieldSize = Openssl_EC_GROUP_get_degree(group); - if (*fieldSize == 0) { - LOGE("Openssl_EC_GROUP_get_degree failed."); - return HCF_ERR_CRYPTO_OPERATION; - } - return HCF_SUCCESS; -} - -static HcfResult GetPubKeyXOrY(const EC_GROUP *group, const EC_POINT *point, const AsyKeySpecItem item, - HcfBigInteger *returnBigInteger) -{ - BIGNUM *pkX = Openssl_BN_new(); - BIGNUM *pkY = Openssl_BN_new(); - if (pkX == NULL || pkY == NULL) { - LOGE("new BN failed."); - Openssl_BN_free(pkX); - Openssl_BN_free(pkY); - return HCF_ERR_CRYPTO_OPERATION; - } - - if (Openssl_EC_POINT_get_affine_coordinates_GFp(group, point, pkX, pkY, NULL) != HCF_OPENSSL_SUCCESS) { - LOGE("Openssl_EC_POINT_get_affine_coordinates_GFp failed."); - Openssl_BN_free(pkX); - Openssl_BN_free(pkY); - return HCF_ERR_CRYPTO_OPERATION; - } - - HcfResult ret = HCF_INVALID_PARAMS; - switch (item) { - case ECC_PK_X_BN: - ret = BigNumToBigInteger(pkX, returnBigInteger); - break; - case ECC_PK_Y_BN: - ret = BigNumToBigInteger(pkY, returnBigInteger); - break; - default: - LOGE("Invalid ecc key big number spec!"); - break; - } - Openssl_BN_free(pkX); - Openssl_BN_free(pkY); - return ret; -} - -static HcfResult GetFieldType(const HcfKey *self, bool isPrivate, char **returnString) -{ - char *fieldType = NULL; - if (isPrivate) { - fieldType = ((HcfOpensslSm2PriKey *)self)->fieldType; - } else { - fieldType = ((HcfOpensslSm2PubKey *)self)->fieldType; - } - - if (fieldType == NULL) { - LOGE("No fieldType in Sm2PubKey struct."); - return HCF_INVALID_PARAMS; - } - - size_t len = HcfStrlen(fieldType); - if (!len) { - LOGE("fieldType is empty!"); - return HCF_INVALID_PARAMS; - } - *returnString = (char *)HcfMalloc(len + 1, 0); - if (*returnString == NULL) { - LOGE("Alloc returnString memory failed."); - return HCF_ERR_MALLOC; - } - if (memcpy_s(*returnString, len, fieldType, len) != EOK) { - LOGE("memcpy returnString failed."); - HcfFree(*returnString); - return HCF_ERR_MALLOC; - } - return HCF_SUCCESS; -} - static HcfResult GetCurveName(const HcfKey *self, bool isPriavte, char **returnString) { int32_t curveId = 0; @@ -978,28 +559,6 @@ static HcfResult CheckSm2KeySelf(const HcfKey *self, bool *isPrivate) } } -static HcfResult GetPkSkBigInteger(const HcfKey *self, bool isPrivate, - const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) -{ - HcfResult ret = HCF_INVALID_PARAMS; - if (item == ECC_SK_BN) { - if (!isPrivate) { - LOGE("ecc pub key has no private key spec item"); - return ret; - } - ret = BigNumToBigInteger(Openssl_EC_KEY_get0_private_key(((HcfOpensslEccPriKey *)self)->ecKey), - returnBigInteger); - } else { - if (isPrivate) { - LOGE("ecc pri key cannot get pub key spec item"); - return ret; - } - ret = GetPubKeyXOrY(Openssl_EC_KEY_get0_group(((HcfOpensslEccPubKey *)self)->ecKey), - Openssl_EC_KEY_get0_public_key(((HcfOpensslEccPubKey *)self)->ecKey), item, returnBigInteger); - } - return ret; -} - static HcfResult GetSm2KeySpecBigInteger(const HcfKey *self, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) { @@ -1015,9 +574,9 @@ static HcfResult GetSm2KeySpecBigInteger(const HcfKey *self, const AsyKeySpecIte } const EC_GROUP *group = NULL; if (isPrivate) { - group = Openssl_EC_KEY_get0_group(((HcfOpensslEccPriKey *)self)->ecKey); + group = Openssl_EC_KEY_get0_group(((HcfOpensslSm2PriKey *)self)->ecKey); } else { - group = Openssl_EC_KEY_get0_group(((HcfOpensslEccPubKey *)self)->ecKey); + group = Openssl_EC_KEY_get0_group(((HcfOpensslSm2PubKey *)self)->ecKey); } if (group == NULL) { LOGE("get group failed"); @@ -1091,9 +650,9 @@ static HcfResult GetSm2KeySpecInt(const HcfKey *self, const AsyKeySpecItem item, } const EC_GROUP *group = NULL; if (isPrivate) { - group = Openssl_EC_KEY_get0_group(((HcfOpensslEccPriKey *)self)->ecKey); + group = Openssl_EC_KEY_get0_group(((HcfOpensslSm2PriKey *)self)->ecKey); } else { - group = Openssl_EC_KEY_get0_group(((HcfOpensslEccPubKey *)self)->ecKey); + group = Openssl_EC_KEY_get0_group(((HcfOpensslSm2PubKey *)self)->ecKey); } if (group == NULL) { LOGE("get group failed"); @@ -1414,57 +973,6 @@ static HcfResult EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair * return ret; } -static HcfResult GenKeyPairSm2KeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) -{ - HcfResult ret = HCF_INVALID_PARAMS; - switch (params->specType) { - case HCF_COMMON_PARAMS_SPEC: - ret = NewSm2KeyPairWithCommSpec((HcfEccCommParamsSpec *)params, ecKey); - break; - case HCF_KEY_PAIR_SPEC: - ret = NewSm2KeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true); - break; - default: - LOGE("Invaild input spec to gen key pair."); - break; - } - return ret; -} - -static HcfResult GenPubKeySm2KeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) -{ - HcfResult ret = HCF_INVALID_PARAMS; - switch (params->specType) { - case HCF_PUBLIC_KEY_SPEC: - ret = NewSm2PubKeyWithPubSpec((HcfEccPubKeyParamsSpec *)params, ecKey); - break; - case HCF_KEY_PAIR_SPEC: - ret = NewSm2KeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, false); - break; - default: - LOGE("Invaild input spec to gen pub key"); - break; - } - return ret; -} - -static HcfResult GenPriKeySm2KeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey) -{ - HcfResult ret = HCF_INVALID_PARAMS; - switch (params->specType) { - case HCF_PRIVATE_KEY_SPEC: - ret = NewSm2PriKeyWithPriSpec((HcfEccPriKeyParamsSpec *)params, ecKey); - break; - case HCF_KEY_PAIR_SPEC: - ret = NewSm2KeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true); - break; - default: - LOGE("Invaild input spec to gen pri key"); - break; - } - return ret; -} - static HcfResult EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params, HcfKeyPair **returnKeyPair) { diff --git a/plugin/plugin.gni b/plugin/plugin.gni index 30b3eb0..4ed4b8d 100644 --- a/plugin/plugin.gni +++ b/plugin/plugin.gni @@ -36,23 +36,33 @@ plugin_signature_files = [ "${plugin_path}/openssl_plugin/crypto_operation/signature/src/ecdsa_openssl.c", "${plugin_path}/openssl_plugin/crypto_operation/signature/src/signature_rsa_openssl.c", "${plugin_path}/openssl_plugin/crypto_operation/signature/src/sm2_openssl.c", + "${plugin_path}/openssl_plugin/crypto_operation/signature/src/ed25519_openssl.c", ] plugin_common_files = [ "${plugin_path}/openssl_plugin/common/src/openssl_adapter.c", "${plugin_path}/openssl_plugin/common/src/openssl_common.c", + "${plugin_path}/openssl_plugin/common/src/dh_openssl_common.c", + "${plugin_path}/openssl_plugin/common/src/ecc_openssl_common.c", "${plugin_path}/openssl_plugin/common/src/rsa_openssl_common.c", ] plugin_asy_key_generator_files = [ "${plugin_path}/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c", "${plugin_path}/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c", + "${plugin_path}/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c", + "${plugin_path}/openssl_plugin/key/asy_key_generator/src/dh_common_param_spec_generator_openssl.c", "${plugin_path}/openssl_plugin/key/asy_key_generator/src/ecc_common_param_spec_generator_openssl.c", "${plugin_path}/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c", "${plugin_path}/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c", + "${plugin_path}/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c", ] -plugin_key_agreement_files = [ "${plugin_path}/openssl_plugin/crypto_operation/key_agreement/src/ecdh_openssl.c" ] +plugin_key_agreement_files = [ + "${plugin_path}/openssl_plugin/crypto_operation/key_agreement/src/dh_openssl.c", + "${plugin_path}/openssl_plugin/crypto_operation/key_agreement/src/ecdh_openssl.c", + "${plugin_path}/openssl_plugin/crypto_operation/key_agreement/src/x25519_openssl.c", +] plugin_sym_key_files = [ "${plugin_path}/openssl_plugin/key/sym_key_generator/src/sym_key_openssl.c", diff --git a/test/fuzztest/crypto_operation/hcfsigncreate_fuzzer/hcfsigncreate_fuzzer.cpp b/test/fuzztest/crypto_operation/hcfsigncreate_fuzzer/hcfsigncreate_fuzzer.cpp index d1d2bdd..eef4997 100755 --- a/test/fuzztest/crypto_operation/hcfsigncreate_fuzzer/hcfsigncreate_fuzzer.cpp +++ b/test/fuzztest/crypto_operation/hcfsigncreate_fuzzer/hcfsigncreate_fuzzer.cpp @@ -121,11 +121,43 @@ namespace OHOS { HcfObjDestroy(sign); } + static void TestSignEd25519(void) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + if (res != HCF_SUCCESS) { + return; + } + + HcfKeyPair *ed25519KeyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &ed25519KeyPair); + HcfObjDestroy(generator); + if (res != HCF_SUCCESS) { + return; + } + + HcfSign *sign = nullptr; + res = HcfSignCreate("Ed25519", &sign); + if (res != HCF_SUCCESS) { + HcfObjDestroy(ed25519KeyPair); + return; + } + static HcfBlob mockInput = { + .data = reinterpret_cast(g_mockMessage), + .len = INPUT_MSG_LEN + }; + (void)sign->init(sign, nullptr, ed25519KeyPair->priKey); + (void)sign->update(sign, &mockInput); + HcfObjDestroy(ed25519KeyPair); + HcfObjDestroy(sign); + } + bool HcfSignCreateFuzzTest(const uint8_t* data, size_t size) { TestSign(); TestSignSm2(); TestSignBrainpool(); + TestSignEd25519(); HcfSign *sign = nullptr; std::string algoName(reinterpret_cast(data), size); HcfResult res = HcfSignCreate(algoName.c_str(), &sign); diff --git a/test/fuzztest/crypto_operation/hcfverifycreate_fuzzer/hcfverifycreate_fuzzer.cpp b/test/fuzztest/crypto_operation/hcfverifycreate_fuzzer/hcfverifycreate_fuzzer.cpp index f537b64..9652e98 100755 --- a/test/fuzztest/crypto_operation/hcfverifycreate_fuzzer/hcfverifycreate_fuzzer.cpp +++ b/test/fuzztest/crypto_operation/hcfverifycreate_fuzzer/hcfverifycreate_fuzzer.cpp @@ -175,11 +175,61 @@ namespace OHOS { HcfObjDestroy(verify); } + static void TestVerifyEd25519(void) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + if (res != HCF_SUCCESS) { + return; + } + + HcfKeyPair *ed25519KeyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &ed25519KeyPair); + HcfObjDestroy(generator); + if (res != HCF_SUCCESS) { + return; + } + + HcfSign *sign = nullptr; + res = HcfSignCreate("Ed25519", &sign); + if (res != HCF_SUCCESS) { + HcfObjDestroy(ed25519KeyPair); + return; + } + static HcfBlob mockInput = { + .data = reinterpret_cast(g_mockMessage), + .len = INPUT_MSG_LEN + }; + (void)sign->init(sign, nullptr, ed25519KeyPair->priKey); + (void)sign->update(sign, &mockInput); + + HcfVerify *verify = nullptr; + res = HcfVerifyCreate("Ed25519", &verify); + if (res != HCF_SUCCESS) { + HcfObjDestroy(ed25519KeyPair); + HcfObjDestroy(sign); + return; + } + HcfBlob out = { + .data = nullptr, + .len = 0 + }; + (void)sign->sign(sign, nullptr, &out); + (void)verify->init(verify, nullptr, ed25519KeyPair->pubKey); + (void)verify->update(verify, &mockInput); + (void)verify->verify(verify, nullptr, &out); + HcfObjDestroy(ed25519KeyPair); + HcfObjDestroy(sign); + HcfBlobDataFree(&out); + HcfObjDestroy(verify); + } + bool HcfVerifyCreateFuzzTest(const uint8_t* data, size_t size) { TestVerify(); TestVerifySm2(); TestVerifyBrainpool(); + TestVerifyEd25519(); HcfVerify *verify = nullptr; std::string algoName(reinterpret_cast(data), size); HcfResult res = HcfVerifyCreate(algoName.c_str(), &verify); diff --git a/test/fuzztest/key/asykeygenerator_fuzzer/asykeygenerator_fuzzer.cpp b/test/fuzztest/key/asykeygenerator_fuzzer/asykeygenerator_fuzzer.cpp index dc638ed..29081c2 100644 --- a/test/fuzztest/key/asykeygenerator_fuzzer/asykeygenerator_fuzzer.cpp +++ b/test/fuzztest/key/asykeygenerator_fuzzer/asykeygenerator_fuzzer.cpp @@ -21,12 +21,16 @@ #include "asy_key_generator.h" #include "blob.h" +#include "detailed_alg_25519_key_params.h" +#include "detailed_dh_key_params.h" #include "detailed_dsa_key_params.h" #include "detailed_ecc_key_params.h" #include "detailed_rsa_key_params.h" +#include "ecc_openssl_common_param_spec.h" #include "ecc_openssl_common.h" #include "ecc_common.h" #include "ecc_key_util.h" +#include "dh_key_util.h" #include "key_utils.h" #include "result.h" @@ -35,12 +39,26 @@ using namespace std; namespace { HcfEccCommParamsSpec *g_sm2256CommSpec = nullptr; HcfEccCommParamsSpec *g_brainpoolP160r1CommSpec = nullptr; +HcfDhCommParamsSpec *g_dhCommSpec = nullptr; +static string g_ed25519AlgoName = "Ed25519"; +static string g_x25519AlgoName = "X25519"; } namespace OHOS { + constexpr uint32_t PLEN_BITS = 3072; + constexpr int32_t SKLEN_BITS = 256; HcfEccPubKeyParamsSpec g_ecc256PubKeySpec; HcfEccPriKeyParamsSpec g_ecc256PriKeySpec; HcfEccKeyPairParamsSpec g_ecc256KeyPairSpec; + HcfAlg25519KeyPairParamsSpec g_ed25519KeyPairSpec; + HcfAlg25519PriKeyParamsSpec g_ed25519PriKeySpec; + HcfAlg25519PubKeyParamsSpec g_ed25519PubKeySpec; + HcfAlg25519KeyPairParamsSpec g_x25519KeyPairSpec; + HcfAlg25519PriKeyParamsSpec g_x25519PriKeySpec; + HcfAlg25519PubKeyParamsSpec g_x25519PubKeySpec; + HcfDhPubKeyParamsSpec g_dhPubKeySpec; + HcfDhPriKeyParamsSpec g_dhPriKeySpec; + HcfDhKeyPairParamsSpec g_dhKeyPairSpec; enum class GenerateType { FUZZ_COMMON = 0, @@ -55,6 +73,12 @@ namespace OHOS { static const int SM2256_PRI_KEY_LEN = 51; static const int BRAINPOOLP160R1_PUB_KEY_LEN = 68; static const int BRAINPOOLP160R1_PRI_KEY_LEN = 40; + static const int ED25519_PUB_KEY_LEN = 44; + static const int ED25519_PRI_KEY_LEN = 48; + static const int X25519_PUB_KEY_LEN = 44; + static const int X25519_PRI_KEY_LEN = 48; + static const int DH_PUB_KEY_LEN = 553; + static const int DH_PRI_KEY_LEN = 323; static uint8_t g_mockEcc224PubKey[ECC224_PUB_KEY_LEN] = { 48, 78, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 5, 43, 129, 4, 0, 33, 3, 58, 0, 4, 252, 171, 11, 115, 79, 252, 109, 120, 46, 97, 131, 145, 207, 141, 146, 235, 133, 37, 218, 180, 8, 149, 47, 244, 137, 238, 207, 95, 153, 65, 250, 32, 77, 184, 249, 181, @@ -83,6 +107,61 @@ namespace OHOS { 46, 76, 111, 184, 30, 42, 223, 86, 187, 131, 127, 41, 28, 223, 93, 134, 160, 11, 6, 9, 43, 36, 3, 3, 2, 8, 1, 1, 1 }; + static uint8_t g_mockEd25519PubKey[ED25519_PUB_KEY_LEN] = { 48, 42, 48, 5, 6, 3, 43, 101, 112, 3, 33, 0, 101, 94, + 172, 9, 171, 197, 147, 204, 102, 87, 132, 67, 59, 108, 68, 121, 150, 93, 83, 26, 173, 99, 63, 125, 86, 91, 77, + 207, 147, 216, 158, 5 }; + + static uint8_t g_mockEd25519PriKey[ED25519_PRI_KEY_LEN] = { 48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, + 32, 31, 229, 164, 209, 117, 143, 227, 85, 227, 67, 214, 165, 40, 220, 217, 105, 123, 246, 71, 104, 129, 79, 19, + 173, 36, 32, 69, 83, 25, 136, 92, 25 }; + + static uint8_t g_mockX25519PubKey[X25519_PUB_KEY_LEN] = { 48, 42, 48, 5, 6, 3, 43, 101, 110, 3, 33, 0, 173, 38, 49, + 140, 12, 119, 139, 84, 170, 234, 223, 247, 240, 167, 79, 192, 41, 114, 211, 76, 38, 151, 123, 141, 209, 44, 31, + 97, 16, 137, 236, 5 }; + + static uint8_t g_mockX25519PriKey[X25519_PRI_KEY_LEN] = { 48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 110, 4, 34, 4, 32, + 96, 70, 225, 130, 145, 57, 68, 247, 129, 6, 13, 185, 167, 100, 237, 166, 63, 125, 219, 75, 59, 27, 123, 100, 68, + 136, 52, 93, 100, 253, 222, 80 }; + + static uint8_t g_mockDhPubKey[DH_PUB_KEY_LEN] = { 48, 130, 2, 37, 48, 130, 1, 23, 6, 9, 42, 134, 72, 134, 247, 13, + 1, 3, 1, 48, 130, 1, 8, 2, 130, 1, 1, 0, 255, 255, 255, 255, 255, 255, 255, 255, 201, 15, 218, 162, 33, 104, + 194, 52, 196, 198, 98, 139, 128, 220, 28, 209, 41, 2, 78, 8, 138, 103, 204, 116, 2, 11, 190, 166, 59, 19, 155, + 34, 81, 74, 8, 121, 142, 52, 4, 221, 239, 149, 25, 179, 205, 58, 67, 27, 48, 43, 10, 109, 242, 95, 20, 55, 79, + 225, 53, 109, 109, 81, 194, 69, 228, 133, 181, 118, 98, 94, 126, 198, 244, 76, 66, 233, 166, 55, 237, 107, 11, + 255, 92, 182, 244, 6, 183, 237, 238, 56, 107, 251, 90, 137, 159, 165, 174, 159, 36, 17, 124, 75, 31, 230, 73, + 40, 102, 81, 236, 228, 91, 61, 194, 0, 124, 184, 161, 99, 191, 5, 152, 218, 72, 54, 28, 85, 211, 154, 105, 22, + 63, 168, 253, 36, 207, 95, 131, 101, 93, 35, 220, 163, 173, 150, 28, 98, 243, 86, 32, 133, 82, 187, 158, 213, + 41, 7, 112, 150, 150, 109, 103, 12, 53, 78, 74, 188, 152, 4, 241, 116, 108, 8, 202, 24, 33, 124, 50, 144, 94, + 70, 46, 54, 206, 59, 227, 158, 119, 44, 24, 14, 134, 3, 155, 39, 131, 162, 236, 7, 162, 143, 181, 197, 93, 240, + 111, 76, 82, 201, 222, 43, 203, 246, 149, 88, 23, 24, 57, 149, 73, 124, 234, 149, 106, 229, 21, 210, 38, 24, + 152, 250, 5, 16, 21, 114, 142, 90, 138, 172, 170, 104, 255, 255, 255, 255, 255, 255, 255, 255, 2, 1, 2, 3, 130, + 1, 6, 0, 2, 130, 1, 1, 0, 228, 194, 161, 19, 145, 70, 104, 142, 66, 200, 1, 158, 107, 23, 93, 212, 19, 223, + 145, 196, 11, 179, 169, 69, 136, 163, 136, 142, 122, 230, 238, 249, 102, 227, 49, 92, 64, 255, 8, 185, 238, 5, + 97, 253, 174, 161, 140, 70, 40, 159, 105, 249, 76, 206, 35, 97, 16, 138, 185, 172, 90, 77, 248, 8, 242, 31, + 212, 84, 224, 226, 60, 71, 162, 47, 158, 148, 251, 118, 206, 151, 80, 23, 158, 241, 181, 139, 129, 240, 26, + 150, 180, 237, 252, 73, 84, 173, 63, 215, 130, 6, 124, 97, 118, 165, 133, 66, 235, 97, 143, 148, 105, 86, 174, + 71, 254, 169, 22, 172, 116, 130, 198, 237, 131, 230, 113, 12, 228, 21, 138, 128, 168, 40, 207, 205, 190, 160, + 114, 156, 90, 210, 114, 54, 42, 191, 167, 99, 100, 138, 145, 120, 165, 62, 162, 238, 62, 76, 162, 90, 97, 245, + 30, 55, 157, 139, 36, 118, 121, 242, 214, 79, 0, 27, 36, 4, 243, 62, 107, 34, 222, 110, 252, 24, 202, 3, 216, + 160, 83, 228, 254, 253, 87, 198, 235, 234, 210, 80, 124, 218, 188, 82, 116, 144, 70, 40, 231, 124, 172, 59, + 154, 6, 87, 22, 9, 198, 113, 142, 39, 64, 137, 34, 100, 195, 55, 75, 204, 185, 1, 222, 27, 245, 213, 22, 222, + 83, 0, 222, 8, 194, 21, 85, 90, 32, 236, 205, 86, 38, 70, 57, 171, 248, 168, 52, 85, 46, 1, 149 }; + + static uint8_t g_mockDhPriKey[DH_PRI_KEY_LEN] = { 48, 130, 1, 63, 2, 1, 0, 48, 130, 1, 23, 6, 9, 42, 134, 72, 134, + 247, 13, 1, 3, 1, 48, 130, 1, 8, 2, 130, 1, 1, 0, 255, 255, 255, 255, 255, 255, 255, 255, 201, 15, 218, 162, + 33, 104, 194, 52, 196, 198, 98, 139, 128, 220, 28, 209, 41, 2, 78, 8, 138, 103, 204, 116, 2, 11, 190, 166, 59, + 19, 155, 34, 81, 74, 8, 121, 142, 52, 4, 221, 239, 149, 25, 179, 205, 58, 67, 27, 48, 43, 10, 109, 242, 95, 20, + 55, 79, 225, 53, 109, 109, 81, 194, 69, 228, 133, 181, 118, 98, 94, 126, 198, 244, 76, 66, 233, 166, 55, 237, + 107, 11, 255, 92, 182, 244, 6, 183, 237, 238, 56, 107, 251, 90, 137, 159, 165, 174, 159, 36, 17, 124, 75, 31, + 230, 73, 40, 102, 81, 236, 228, 91, 61, 194, 0, 124, 184, 161, 99, 191, 5, 152, 218, 72, 54, 28, 85, 211, 154, + 105, 22, 63, 168, 253, 36, 207, 95, 131, 101, 93, 35, 220, 163, 173, 150, 28, 98, 243, 86, 32, 133, 82, 187, + 158, 213, 41, 7, 112, 150, 150, 109, 103, 12, 53, 78, 74, 188, 152, 4, 241, 116, 108, 8, 202, 24, 33, 124, 50, + 144, 94, 70, 46, 54, 206, 59, 227, 158, 119, 44, 24, 14, 134, 3, 155, 39, 131, 162, 236, 7, 162, 143, 181, 197, + 93, 240, 111, 76, 82, 201, 222, 43, 203, 246, 149, 88, 23, 24, 57, 149, 73, 124, 234, 149, 106, 229, 21, 210, + 38, 24, 152, 250, 5, 16, 21, 114, 142, 90, 138, 172, 170, 104, 255, 255, 255, 255, 255, 255, 255, 255, 2, 1, 2, + 4, 31, 2, 29, 0, 237, 124, 61, 162, 122, 242, 226, 132, 236, 155, 58, 14, 154, 128, 233, 85, 121, 59, 252, 255, + 157, 145, 75, 251, 236, 154, 85, 203 }; + constexpr uint32_t DSA2048_PRI_SIZE = 20; constexpr uint32_t DSA2048_PUB_SIZE = 256; constexpr uint32_t DSA2048_P_SIZE = 256; @@ -572,25 +651,14 @@ namespace OHOS { return HCF_SUCCESS; } - static HcfResult ConstructSm2256PubKeyParamsSpec(const std::string &algoName, - HcfEccCommParamsSpec *eccCommParamsSpec, HcfAsyKeyParamsSpec **spec) + static HcfResult ConstructSm2256PubKeyParams(HcfKeyPair *keyPair, HcfEccCommParamsSpec *eccCommParamsSpec, + HcfAsyKeyParamsSpec **spec) { - HcfAsyKeyGenerator *generator = nullptr; - HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); - if (res != HCF_SUCCESS) { - return res; - } - HcfKeyPair *keyPair = nullptr; - res = generator->generateKeyPair(generator, nullptr, &keyPair); - if (res != HCF_SUCCESS) { - HcfObjDestroy(generator); - return res; - } HcfEccPubKeyParamsSpec *eccPubKeySpec = &g_ecc256PubKeySpec; HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; eccPubKeySpec->base.base.algName = eccCommParamsSpec->base.algName; - eccPubKeySpec->base.base.specType = HCF_KEY_PAIR_SPEC; + eccPubKeySpec->base.base.specType = HCF_PUBLIC_KEY_SPEC; eccPubKeySpec->base.field = eccCommParamsSpec->field; eccPubKeySpec->base.field->fieldType = eccCommParamsSpec->field->fieldType; ((HcfECFieldFp *)(eccPubKeySpec->base.field))->p.data = ((HcfECFieldFp *)(eccCommParamsSpec->field))->p.data; @@ -606,16 +674,48 @@ namespace OHOS { eccPubKeySpec->base.n.data = eccCommParamsSpec->n.data; eccPubKeySpec->base.n.len = eccCommParamsSpec->n.len; eccPubKeySpec->base.h = eccCommParamsSpec->h; - res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ECC_PK_X_BN, &retBigInt); + HcfResult res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ECC_PK_X_BN, &retBigInt); + if (res != HCF_SUCCESS) { + return res; + } eccPubKeySpec->pk.x.data = retBigInt.data; eccPubKeySpec->pk.x.len = retBigInt.len; res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ECC_PK_Y_BN, &retBigInt); + if (res != HCF_SUCCESS) { + return res; + } eccPubKeySpec->pk.y.data =retBigInt.data; eccPubKeySpec->pk.y.len = retBigInt.len; *spec = reinterpret_cast(eccPubKeySpec); + return HCF_SUCCESS; + } + + static HcfResult ConstructSm2256PubKeyParamsSpec(const std::string &algoName, + HcfEccCommParamsSpec *eccCommParamsSpec, HcfAsyKeyParamsSpec **spec) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + + res = ConstructSm2256PubKeyParams(keyPair, eccCommParamsSpec, spec); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } HcfObjDestroy(generator); + HcfObjDestroy(keyPair); return HCF_SUCCESS; } @@ -627,17 +727,19 @@ namespace OHOS { if (res != HCF_SUCCESS) { return res; } + HcfKeyPair *keyPair = nullptr; res = generator->generateKeyPair(generator, nullptr, &keyPair); if (res != HCF_SUCCESS) { HcfObjDestroy(generator); return res; } + HcfEccPriKeyParamsSpec *eccPriKeySpec = &g_ecc256PriKeySpec; HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; eccPriKeySpec->base.base.algName = eccCommParamsSpec->base.algName; - eccPriKeySpec->base.base.specType = HCF_KEY_PAIR_SPEC; + eccPriKeySpec->base.base.specType = HCF_PRIVATE_KEY_SPEC; eccPriKeySpec->base.field = eccCommParamsSpec->field; eccPriKeySpec->base.field->fieldType = eccCommParamsSpec->field->fieldType; ((HcfECFieldFp *)(eccPriKeySpec->base.field))->p.data = ((HcfECFieldFp *)(eccCommParamsSpec->field))->p.data; @@ -654,28 +756,23 @@ namespace OHOS { eccPriKeySpec->base.n.len = eccCommParamsSpec->n.len; eccPriKeySpec->base.h = eccCommParamsSpec->h; res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ECC_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_ERR_CRYPTO_OPERATION; + } eccPriKeySpec->sk.data = retBigInt.data; eccPriKeySpec->sk.len = retBigInt.len; *spec = reinterpret_cast(eccPriKeySpec); HcfObjDestroy(generator); + HcfObjDestroy(keyPair); return HCF_SUCCESS; } - static HcfResult ConstructSm2256KeyPairParamsSpec(const std::string &algoName, - HcfEccCommParamsSpec *eccCommParamsSpec, HcfAsyKeyParamsSpec **spec) + static HcfResult ConstructSm2256KeyPairParams(HcfKeyPair *keyPair, HcfEccCommParamsSpec *eccCommParamsSpec, + HcfAsyKeyParamsSpec **spec) { - HcfAsyKeyGenerator *generator = nullptr; - HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); - if (res != HCF_SUCCESS) { - return res; - } - HcfKeyPair *keyPair = nullptr; - res = generator->generateKeyPair(generator, nullptr, &keyPair); - if (res != HCF_SUCCESS) { - HcfObjDestroy(generator); - return res; - } HcfEccKeyPairParamsSpec *eccKeyPairSpec = &g_ecc256KeyPairSpec; HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; @@ -696,27 +793,349 @@ namespace OHOS { eccKeyPairSpec->base.n.data = eccCommParamsSpec->n.data; eccKeyPairSpec->base.n.len = eccCommParamsSpec->n.len; eccKeyPairSpec->base.h = eccCommParamsSpec->h; - res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ECC_PK_X_BN, &retBigInt); + HcfResult res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ECC_PK_X_BN, &retBigInt); + if (res != HCF_SUCCESS) { + return res; + } eccKeyPairSpec->pk.x.data = retBigInt.data; eccKeyPairSpec->pk.x.len = retBigInt.len; res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ECC_PK_Y_BN, &retBigInt); + if (res != HCF_SUCCESS) { + return res; + } eccKeyPairSpec->pk.y.data =retBigInt.data; eccKeyPairSpec->pk.y.len = retBigInt.len; res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ECC_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + return res; + } eccKeyPairSpec->sk.data = retBigInt.data; eccKeyPairSpec->sk.len = retBigInt.len; *spec = reinterpret_cast(eccKeyPairSpec); + return HCF_SUCCESS; + } + static HcfResult ConstructSm2256KeyPairParamsSpec(const std::string &algoName, + HcfEccCommParamsSpec *eccCommParamsSpec, HcfAsyKeyParamsSpec **spec) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + + res = ConstructSm2256KeyPairParams(keyPair, eccCommParamsSpec, spec); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; + } + + static HcfResult ConstructAlg25519KeyPairParamsSpec(const string &algoName, bool choose, + HcfAsyKeyParamsSpec **spec) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfAlg25519KeyPairParamsSpec *alg25519KeyPairSpec = nullptr; + if (choose) { + alg25519KeyPairSpec = &g_ed25519KeyPairSpec; + alg25519KeyPairSpec->base.algName = g_ed25519AlgoName.data(); + } else { + alg25519KeyPairSpec = &g_x25519KeyPairSpec; + alg25519KeyPairSpec->base.algName = g_x25519AlgoName.data(); + } + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + alg25519KeyPairSpec->base.specType = HCF_KEY_PAIR_SPEC; + if (choose) { + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ED25519_PK_BN, &retBigInt); + } else { + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, X25519_PK_BN, &retBigInt); + } + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_ERR_CRYPTO_OPERATION; + } + alg25519KeyPairSpec->pk.data = retBigInt.data; + alg25519KeyPairSpec->pk.len = retBigInt.len; + + if (choose) { + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ED25519_SK_BN, &retBigInt); + } else { + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, X25519_SK_BN, &retBigInt); + } + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_ERR_CRYPTO_OPERATION; + } + alg25519KeyPairSpec->sk.data = retBigInt.data; + alg25519KeyPairSpec->sk.len = retBigInt.len; + + *spec = reinterpret_cast(alg25519KeyPairSpec); + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; + } + + static HcfResult ConstructAlg25519PubKeyParamsSpec(const string &algoName, bool choose, + HcfAsyKeyParamsSpec **spec) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfAlg25519PubKeyParamsSpec *alg25519PubKeySpec = nullptr; + if (choose) { + alg25519PubKeySpec = &g_ed25519PubKeySpec; + alg25519PubKeySpec->base.algName = g_ed25519AlgoName.data(); + } else { + alg25519PubKeySpec = &g_x25519PubKeySpec; + alg25519PubKeySpec->base.algName = g_x25519AlgoName.data(); + } + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + alg25519PubKeySpec->base.specType = HCF_PUBLIC_KEY_SPEC; + if (choose) { + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ED25519_PK_BN, &retBigInt); + } else { + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, X25519_PK_BN, &retBigInt); + } + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_ERR_CRYPTO_OPERATION; + } + alg25519PubKeySpec->pk.data = retBigInt.data; + alg25519PubKeySpec->pk.len = retBigInt.len; + + *spec = reinterpret_cast(alg25519PubKeySpec); + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; + } + + static HcfResult ConstructAlg25519PriKeyParamsSpec(const string &algoName, bool choose, + HcfAsyKeyParamsSpec **spec) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return HCF_ERR_CRYPTO_OPERATION; + } + HcfAlg25519PriKeyParamsSpec *alg25519PriKeySpec = nullptr; + if (choose) { + alg25519PriKeySpec = &g_ed25519PriKeySpec; + alg25519PriKeySpec->base.algName = g_ed25519AlgoName.data(); + } else { + alg25519PriKeySpec = &g_x25519PriKeySpec; + alg25519PriKeySpec->base.algName = g_x25519AlgoName.data(); + } + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + alg25519PriKeySpec->base.specType = HCF_PRIVATE_KEY_SPEC; + if (choose) { + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ED25519_SK_BN, &retBigInt); + } else { + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, X25519_SK_BN, &retBigInt); + } + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_ERR_CRYPTO_OPERATION; + } + alg25519PriKeySpec->sk.data = retBigInt.data; + alg25519PriKeySpec->sk.len = retBigInt.len; + + *spec = reinterpret_cast(alg25519PriKeySpec); + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; + } + + static HcfResult ConstructDhCommParamsSpec(uint32_t pLen, int32_t skLen, HcfDhCommParamsSpec **spec) + { + HcfDhCommParamsSpec *dhCommSpec = nullptr; + + HcfDhKeyUtilCreate(pLen, skLen, &dhCommSpec); + *spec = dhCommSpec; + return HCF_SUCCESS; + } + + static HcfResult ConstructDhPubKeyParamsSpec(const std::string &algoName, + HcfDhCommParamsSpec *dhCommParamsSpec, HcfAsyKeyParamsSpec **spec) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfDhPubKeyParamsSpec *dhPubKeySpec = &g_dhPubKeySpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + + dhPubKeySpec->base.base.algName = dhCommParamsSpec->base.algName; + dhPubKeySpec->base.base.specType = HCF_PUBLIC_KEY_SPEC; + dhPubKeySpec->base.p.data = dhCommParamsSpec->p.data; + dhPubKeySpec->base.p.len = dhCommParamsSpec->p.len; + dhPubKeySpec->base.g.data = dhCommParamsSpec->g.data; + dhPubKeySpec->base.g.len = dhCommParamsSpec->g.len; + dhPubKeySpec->base.length = dhCommParamsSpec->length; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_ERR_CRYPTO_OPERATION; + } + dhPubKeySpec->pk.data = retBigInt.data; + dhPubKeySpec->pk.len = retBigInt.len; + + *spec = reinterpret_cast(dhPubKeySpec); + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; + } + + static HcfResult ConstructDhPriKeyParamsSpec(const std::string &algoName, + HcfDhCommParamsSpec *dhCommParamsSpec, HcfAsyKeyParamsSpec **spec) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return HCF_ERR_CRYPTO_OPERATION; + } + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfDhPriKeyParamsSpec *dhPriKeySpec = &g_dhPriKeySpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + + dhPriKeySpec->base.base.algName = dhCommParamsSpec->base.algName; + dhPriKeySpec->base.base.specType = HCF_PRIVATE_KEY_SPEC; + dhPriKeySpec->base.p.data = dhCommParamsSpec->p.data; + dhPriKeySpec->base.p.len = dhCommParamsSpec->p.len; + dhPriKeySpec->base.g.data = dhCommParamsSpec->g.data; + dhPriKeySpec->base.g.len = dhCommParamsSpec->g.len; + dhPriKeySpec->base.length = dhCommParamsSpec->length; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, DH_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_ERR_CRYPTO_OPERATION; + } + dhPriKeySpec->sk.data = retBigInt.data; + dhPriKeySpec->sk.len = retBigInt.len; + + *spec = reinterpret_cast(dhPriKeySpec); + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; + } + + static HcfResult ConstructDhKeyPairParamsSpec(const std::string &algoName, + HcfDhCommParamsSpec *dhCommParamsSpec, HcfAsyKeyParamsSpec **spec) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return HCF_ERR_CRYPTO_OPERATION; + } + + HcfDhKeyPairParamsSpec *dhKeyPairSpec = &g_dhKeyPairSpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + + dhKeyPairSpec->base.base.algName = dhCommParamsSpec->base.algName; + dhKeyPairSpec->base.base.specType = HCF_KEY_PAIR_SPEC; + dhKeyPairSpec->base.p.data = dhCommParamsSpec->p.data; + dhKeyPairSpec->base.p.len = dhCommParamsSpec->p.len; + dhKeyPairSpec->base.g.data = dhCommParamsSpec->g.data; + dhKeyPairSpec->base.g.len = dhCommParamsSpec->g.len; + dhKeyPairSpec->base.length = dhCommParamsSpec->length; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_ERR_CRYPTO_OPERATION; + } + dhKeyPairSpec->pk.data = retBigInt.data; + dhKeyPairSpec->pk.len = retBigInt.len; + + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, DH_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_ERR_CRYPTO_OPERATION; + } + dhKeyPairSpec->sk.data = retBigInt.data; + dhKeyPairSpec->sk.len = retBigInt.len; + + *spec = reinterpret_cast(dhKeyPairSpec); + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); return HCF_SUCCESS; } static void TestEccKey(void) { HcfAsyKeyGenerator *generator = nullptr; - int32_t res = HcfAsyKeyGeneratorCreate("ECC224", &generator); + HcfResult res = HcfAsyKeyGeneratorCreate("ECC224", &generator); if (res != HCF_SUCCESS) { return; } @@ -810,7 +1229,7 @@ namespace OHOS { static void TestSm2Key(void) { HcfAsyKeyGenerator *generator = nullptr; - int32_t res = HcfAsyKeyGeneratorCreate("SM2_256", &generator); + HcfResult res = HcfAsyKeyGeneratorCreate("SM2_256", &generator); if (res != HCF_SUCCESS) { return; } @@ -840,7 +1259,7 @@ namespace OHOS { static void TestBrainpoolKey(void) { HcfAsyKeyGenerator *generator = nullptr; - int32_t res = HcfAsyKeyGeneratorCreate("ECC_BrainPoolP160r1", &generator); + HcfResult res = HcfAsyKeyGeneratorCreate("ECC_BrainPoolP160r1", &generator); if (res != HCF_SUCCESS) { return; } @@ -868,6 +1287,99 @@ namespace OHOS { HcfObjDestroy(convertKeyPair); } + static void TestEd25519Key(void) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + if (res != HCF_SUCCESS) { + return; + } + (void)generator->getAlgoName(generator); + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return; + } + HcfKeyPair *convertKeyPair = nullptr; + static HcfBlob mockEd25519PubKeyBlob = { + .data = g_mockEd25519PubKey, + .len = ED25519_PUB_KEY_LEN + }; + + static HcfBlob mockEd25519PriKeyBlob = { + .data = g_mockEd25519PriKey, + .len = ED25519_PRI_KEY_LEN + }; + (void)generator->convertKey(generator, nullptr, &mockEd25519PubKeyBlob, &mockEd25519PriKeyBlob, + &convertKeyPair); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + HcfObjDestroy(convertKeyPair); + } + + static void TestX25519Key(void) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + if (res != HCF_SUCCESS) { + return; + } + (void)generator->getAlgoName(generator); + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return; + } + HcfKeyPair *convertKeyPair = nullptr; + static HcfBlob mockX25519PubKeyBlob = { + .data = g_mockX25519PubKey, + .len = X25519_PUB_KEY_LEN + }; + + static HcfBlob mockX25519PriKeyBlob = { + .data = g_mockX25519PriKey, + .len = X25519_PRI_KEY_LEN + }; + (void)generator->convertKey(generator, nullptr, &mockX25519PubKeyBlob, &mockX25519PriKeyBlob, + &convertKeyPair); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + HcfObjDestroy(convertKeyPair); + } + + static void TestDhKey(void) + { + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp2048", &generator); + if (res != HCF_SUCCESS) { + return; + } + (void)generator->getAlgoName(generator); + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return; + } + HcfKeyPair *convertKeyPair = nullptr; + static HcfBlob mockDhPubKeyBlob = { + .data = g_mockDhPubKey, + .len = DH_PUB_KEY_LEN + }; + + static HcfBlob mockX25519PriKeyBlob = { + .data = g_mockDhPriKey, + .len = DH_PRI_KEY_LEN + }; + (void)generator->convertKey(generator, nullptr, &mockDhPubKeyBlob, &mockX25519PriKeyBlob, + &convertKeyPair); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + HcfObjDestroy(convertKeyPair); + } + static void GenEccKeyBySpec(GenerateType type) { HcfAsyKeyParamsSpec *paramSpec = nullptr; @@ -875,7 +1387,7 @@ namespace OHOS { HcfKeyPair *keyPair = nullptr; HcfPriKey *priKey = nullptr; HcfPubKey *pubKey = nullptr; - int32_t res = HCF_SUCCESS; + HcfResult res = HCF_SUCCESS; switch (type) { case GenerateType::FUZZ_COMMON: res = ConstructEcc224CommParamsSpec(¶mSpec); @@ -928,7 +1440,7 @@ namespace OHOS { HcfKeyPair *keyPair = nullptr; HcfPriKey *priKey = nullptr; HcfPubKey *pubKey = nullptr; - int32_t res = HCF_SUCCESS; + HcfResult res = HCF_SUCCESS; switch (type) { case GenerateType::FUZZ_COMMON: GenerateRsa2048CorrectCommonKeySpec(dataN, &rsaCommSpec); @@ -973,7 +1485,7 @@ namespace OHOS { HcfKeyPair *keyPair = nullptr; HcfPriKey *priKey = nullptr; HcfPubKey *pubKey = nullptr; - int32_t res = HCF_SUCCESS; + HcfResult res = HCF_SUCCESS; switch (type) { case GenerateType::FUZZ_COMMON: res = HcfAsyKeyGeneratorBySpecCreate(reinterpret_cast(&dsaCommonSpec), @@ -1017,7 +1529,7 @@ namespace OHOS { HcfKeyPair *keyPair = nullptr; HcfPriKey *priKey = nullptr; HcfPubKey *pubKey = nullptr; - int32_t res = HCF_SUCCESS; + HcfResult res = HCF_SUCCESS; switch (type) { case GenerateType::FUZZ_PRIKEY: res = ConstructSm2256PriKeyParamsSpec(algoName, eccCommParamsSpec, ¶mSpec); @@ -1041,10 +1553,11 @@ namespace OHOS { (void)generator->generateKeyPair(generator, &keyPair); (void)generator->generatePriKey(generator, &priKey); (void)generator->generatePubKey(generator, &pubKey); - HcfObjDestroy(generator); - HcfObjDestroy(keyPair); - HcfObjDestroy(priKey); + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); } static void GenSm2KeyCommonBySpec(HcfEccCommParamsSpec *eccCommSpec) @@ -1053,7 +1566,7 @@ namespace OHOS { HcfKeyPair *keyPair = nullptr; HcfPriKey *priKey = nullptr; HcfPubKey *pubKey = nullptr; - int32_t res = HCF_SUCCESS; + HcfResult res = HCF_SUCCESS; res = HcfAsyKeyGeneratorBySpecCreate(reinterpret_cast(eccCommSpec), &generator); if (res != HCF_SUCCESS) { return; @@ -1089,6 +1602,131 @@ namespace OHOS { g_brainpoolP160r1CommSpec = nullptr; } + static void GenAlg25519KeyBySpec(GenerateType type, const std::string &algoName, + bool choose) + { + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfAsyKeyGeneratorBySpec *generator = nullptr; + HcfKeyPair *keyPair = nullptr; + HcfPriKey *priKey = nullptr; + HcfPubKey *pubKey = nullptr; + HcfResult res = HCF_SUCCESS; + switch (type) { + case GenerateType::FUZZ_PRIKEY: + res = ConstructAlg25519PriKeyParamsSpec(algoName, choose, ¶mSpec); + break; + case GenerateType::FUZZ_PUBKEY: + res = ConstructAlg25519PubKeyParamsSpec(algoName, choose, ¶mSpec); + break; + case GenerateType::FUZZ_KEYPAIR: + res = ConstructAlg25519KeyPairParamsSpec(algoName, choose, ¶mSpec); + break; + default: + return; + } + if (res != HCF_SUCCESS) { + return; + } + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generator); + if (res != HCF_SUCCESS) { + HcfObjDestroy(paramSpec); + return; + } + (void)generator->generateKeyPair(generator, &keyPair); + (void)generator->generatePriKey(generator, &priKey); + (void)generator->generatePubKey(generator, &pubKey); + + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + } + + static void TestEd25519KeyBySpec(void) + { + GenAlg25519KeyBySpec(GenerateType::FUZZ_PRIKEY, "Ed25519", true); + GenAlg25519KeyBySpec(GenerateType::FUZZ_PUBKEY, "Ed25519", true); + GenAlg25519KeyBySpec(GenerateType::FUZZ_KEYPAIR, "Ed25519", true); + } + + static void TestX25519KeyBySpec(void) + { + GenAlg25519KeyBySpec(GenerateType::FUZZ_PRIKEY, "X25519", false); + GenAlg25519KeyBySpec(GenerateType::FUZZ_PUBKEY, "X25519", false); + GenAlg25519KeyBySpec(GenerateType::FUZZ_KEYPAIR, "X25519", false); + } + + static void GenDhKeyBySpec(GenerateType type, const std::string &algoName, + HcfDhCommParamsSpec *dhCommParamsSpec) + { + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfAsyKeyGeneratorBySpec *generator = nullptr; + HcfKeyPair *keyPair = nullptr; + HcfPriKey *priKey = nullptr; + HcfPubKey *pubKey = nullptr; + HcfResult res = HCF_SUCCESS; + switch (type) { + case GenerateType::FUZZ_PRIKEY: + res = ConstructDhPriKeyParamsSpec(algoName, dhCommParamsSpec, ¶mSpec); + break; + case GenerateType::FUZZ_PUBKEY: + res = ConstructDhPubKeyParamsSpec(algoName, dhCommParamsSpec, ¶mSpec); + break; + case GenerateType::FUZZ_KEYPAIR: + res = ConstructDhKeyPairParamsSpec(algoName, dhCommParamsSpec, ¶mSpec); + break; + default: + return; + } + if (res != HCF_SUCCESS) { + return; + } + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generator); + if (res != HCF_SUCCESS) { + HcfObjDestroy(paramSpec); + return; + } + (void)generator->generateKeyPair(generator, &keyPair); + (void)generator->generatePriKey(generator, &priKey); + (void)generator->generatePubKey(generator, &pubKey); + + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + } + + static void GenDhKeyCommonBySpec(HcfDhCommParamsSpec *dhCommSpec) + { + HcfAsyKeyGeneratorBySpec *generator = nullptr; + HcfKeyPair *keyPair = nullptr; + HcfPriKey *priKey = nullptr; + HcfPubKey *pubKey = nullptr; + HcfResult res = HCF_SUCCESS; + res = HcfAsyKeyGeneratorBySpecCreate(reinterpret_cast(dhCommSpec), &generator); + if (res != HCF_SUCCESS) { + return; + } + (void)generator->generateKeyPair(generator, &keyPair); + (void)generator->generatePriKey(generator, &priKey); + (void)generator->generatePubKey(generator, &pubKey); + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + HcfObjDestroy(priKey); + HcfObjDestroy(pubKey); + } + + static void TestDhKeyBySpec(void) + { + ConstructDhCommParamsSpec(PLEN_BITS, SKLEN_BITS, &g_dhCommSpec); + GenDhKeyCommonBySpec(g_dhCommSpec); + GenDhKeyBySpec(GenerateType::FUZZ_PRIKEY, "DH_ffdhe3072", g_dhCommSpec); + GenDhKeyBySpec(GenerateType::FUZZ_PUBKEY, "DH_ffdhe3072", g_dhCommSpec); + GenDhKeyBySpec(GenerateType::FUZZ_KEYPAIR, "DH_ffdhe3072", g_dhCommSpec); + FreeDhCommParamsSpec(g_dhCommSpec); + g_dhCommSpec = nullptr; + } + bool AsyKeyGeneratorFuzzTest(const uint8_t* data, size_t size) { if (g_testFlag) { @@ -1097,11 +1735,17 @@ namespace OHOS { TestDsaKey(); TestSm2Key(); TestBrainpoolKey(); + TestEd25519Key(); + TestX25519Key(); + TestDhKey(); TestEccKeyBySpec(); TestRsaKeyBySpec(); TestDsaKeyBySpec(); TestSm2KeyBySpec(); TestBrainpoolKeyBySpec(); + TestEd25519KeyBySpec(); + TestX25519KeyBySpec(); + TestDhKeyBySpec(); g_testFlag = false; } HcfAsyKeyGenerator *generator = nullptr; diff --git a/test/fuzztest/key/dhkeyutil_fuzzer/BUILD.gn b/test/fuzztest/key/dhkeyutil_fuzzer/BUILD.gn new file mode 100644 index 0000000..58007ca --- /dev/null +++ b/test/fuzztest/key/dhkeyutil_fuzzer/BUILD.gn @@ -0,0 +1,56 @@ +# Copyright (C) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/security/crypto_framework/frameworks/frameworks.gni") + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +module_output_path = "crypto_framework/crypto_framework" + +##############################fuzztest########################################## +ohos_fuzztest("DhKeyUtilFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "../dhkeyutil_fuzzer" + include_dirs = framework_inc_path + include_dirs += [ "//base/security/crypto_framework/test/unittest/include/" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "dhkeyutil_fuzzer.cpp" ] + deps = [ + "//third_party/bounds_checking_function:libsec_shared", + "//third_party/openssl:libcrypto_shared", + ] + + external_deps = [ + "c_utils:utils", + "crypto_framework:crypto_framework_lib", + "hilog:libhilog", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":DhKeyUtilFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/key/dhkeyutil_fuzzer/corpus/init b/test/fuzztest/key/dhkeyutil_fuzzer/corpus/init new file mode 100644 index 0000000..23e26fe --- /dev/null +++ b/test/fuzztest/key/dhkeyutil_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (C) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/key/dhkeyutil_fuzzer/dhkeyutil_fuzzer.cpp b/test/fuzztest/key/dhkeyutil_fuzzer/dhkeyutil_fuzzer.cpp new file mode 100644 index 0000000..a832563 --- /dev/null +++ b/test/fuzztest/key/dhkeyutil_fuzzer/dhkeyutil_fuzzer.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dhkeyutil_fuzzer.h" + +#include +#include +#include + +#include "dh_key_util.h" +#include "blob.h" +#include "detailed_dh_key_params.h" +#include "result.h" + +using namespace std; + +namespace OHOS { + static bool g_testFlag = true; + static void TestDhKey(void) + { + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + int32_t res = HcfDhKeyUtilCreate(3072, 512, &returnCommonParamSpec); + if (res != HCF_SUCCESS) { + return; + } + FreeDhCommParamsSpec(returnCommonParamSpec); + } + + bool DhKeyUtilFuzzTest(const uint32_t* pLen, size_t size) + { + if (g_testFlag) { + TestDhKey(); + g_testFlag = false; + } + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(*pLen, 0, &returnCommonParamSpec); + if (res != HCF_SUCCESS) { + return false; + } + FreeDhCommParamsSpec(returnCommonParamSpec); + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint32_t* pLen, size_t size) +{ + /* Run your code on data */ + OHOS::DhKeyUtilFuzzTest(pLen, size); + return 0; +} diff --git a/test/fuzztest/key/dhkeyutil_fuzzer/dhkeyutil_fuzzer.h b/test/fuzztest/key/dhkeyutil_fuzzer/dhkeyutil_fuzzer.h new file mode 100644 index 0000000..bd06ec7 --- /dev/null +++ b/test/fuzztest/key/dhkeyutil_fuzzer/dhkeyutil_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DH_KEY_UTIL_FUZZER_H +#define DH_KEY_UTIL_FUZZER_H + +#define FUZZ_PROJECT_NAME "dhkeyutil_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/key/dhkeyutil_fuzzer/project.xml b/test/fuzztest/key/dhkeyutil_fuzzer/project.xml new file mode 100644 index 0000000..e8173d6 --- /dev/null +++ b/test/fuzztest/key/dhkeyutil_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 54a7013..bdc0139 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -42,6 +42,9 @@ ohos_unittest("crypto_framework_test") { "src/crypto_brainpool_no_length_sign_test.cpp", "src/crypto_brainpool_no_length_verify_test.cpp", "src/crypto_common_cov_test.cpp", + "src/crypto_dh_asy_key_generator_by_spec_test.cpp", + "src/crypto_dh_asy_key_generator_test.cpp", + "src/crypto_dh_key_agreement_test.cpp", "src/crypto_dsa_asy_key_generator_by_spec_test.cpp", "src/crypto_dsa_asy_key_generator_test.cpp", "src/crypto_dsa_exception_test.cpp", @@ -56,6 +59,10 @@ ohos_unittest("crypto_framework_test") { "src/crypto_ecc_no_length_verify_test.cpp", "src/crypto_ecc_sign_test.cpp", "src/crypto_ecc_verify_test.cpp", + "src/crypto_ed25519_asy_key_generator_by_spec_test.cpp", + "src/crypto_ed25519_asy_key_generator_test.cpp", + "src/crypto_ed25519_sign_test.cpp", + "src/crypto_ed25519_verify_test.cpp", "src/crypto_mac_test.cpp", "src/crypto_md_sm3_test.cpp", "src/crypto_md_test.cpp", @@ -76,6 +83,9 @@ ohos_unittest("crypto_framework_test") { "src/crypto_sm3_mac_test.cpp", "src/crypto_sm4_cipher_test.cpp", "src/crypto_sm4_generator_test.cpp", + "src/crypto_x25519_asy_key_generator_by_spec_test.cpp", + "src/crypto_x25519_asy_key_generator_test.cpp", + "src/crypto_x25519_key_agreement_test.cpp", ] sources += framework_files + plugin_files diff --git a/test/unittest/src/crypto_asy_key_generator_cov_test.cpp b/test/unittest/src/crypto_asy_key_generator_cov_test.cpp index d87c898..01d309d 100644 --- a/test/unittest/src/crypto_asy_key_generator_cov_test.cpp +++ b/test/unittest/src/crypto_asy_key_generator_cov_test.cpp @@ -22,6 +22,7 @@ #include "detailed_dsa_key_params.h" #include "detailed_ecc_key_params.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "ecc_common.h" #include "memory.h" #include "openssl_class.h" diff --git a/test/unittest/src/crypto_brainpool_asy_key_generator_test.cpp b/test/unittest/src/crypto_brainpool_asy_key_generator_test.cpp index 5859e65..cb83f73 100644 --- a/test/unittest/src/crypto_brainpool_asy_key_generator_test.cpp +++ b/test/unittest/src/crypto_brainpool_asy_key_generator_test.cpp @@ -63,20 +63,20 @@ HcfObjectBase g_obj = { .destroy = nullptr }; -static void ECC_BrainPool160r1KeyBlob(HcfBlob * priblob, HcfBlob *pubblob) +void CryptoBrainPoolAsyKeyGeneratorTest::SetUpTestCase() { HcfAsyKeyGenerator *generator = nullptr; int32_t res = HcfAsyKeyGeneratorCreate("ECC_BrainPoolP160r1", &generator); HcfKeyPair *keyPair = nullptr; res = generator->generateKeyPair(generator, nullptr, &keyPair); + EXPECT_EQ(res, HCF_SUCCESS); res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &g_mockECC_BrainPool160r1PriKeyBlob); + EXPECT_EQ(res, HCF_SUCCESS); res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &g_mockECC_BrainPool160r1PubKeyBlob); -} - -void CryptoBrainPoolAsyKeyGeneratorTest::SetUpTestCase() -{ - ECC_BrainPool160r1KeyBlob(&g_mockECC_BrainPool160r1PriKeyBlob, &g_mockECC_BrainPool160r1PubKeyBlob); + EXPECT_EQ(res, HCF_SUCCESS); + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); } HWTEST_F(CryptoBrainPoolAsyKeyGeneratorTest, CryptoBrainPoolAsyKeyGeneratorTest001, TestSize.Level0) diff --git a/test/unittest/src/crypto_brainpool_key_agreement_test.cpp b/test/unittest/src/crypto_brainpool_key_agreement_test.cpp index 472dedf..ef1335c 100644 --- a/test/unittest/src/crypto_brainpool_key_agreement_test.cpp +++ b/test/unittest/src/crypto_brainpool_key_agreement_test.cpp @@ -59,6 +59,9 @@ static HcfResult ConstructEccBrainPool160r1KeyPairCommParamsSpec(const string &a HcfEccCommParamsSpec *eccCommSpec = nullptr; HcfEccKeyUtilCreate(algoName.c_str(), &eccCommSpec); + if (eccCommSpec == nullptr) { + return HCF_INVALID_PARAMS; + } *spec = eccCommSpec; return HCF_SUCCESS; @@ -67,14 +70,18 @@ static HcfResult ConstructEccBrainPool160r1KeyPairCommParamsSpec(const string &a static HcfResult Constructbrainpool160r1KeyPairParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) { HcfAsyKeyGenerator *generator = nullptr; - int32_t res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); - + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } HcfKeyPair *keyPair = nullptr; res = generator->generateKeyPair(generator, nullptr, &keyPair); - + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } HcfEccKeyPairParamsSpec *eccKeyPairSpec = &g_brainpool160r1KeyPairSpec; HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; - eccKeyPairSpec->base.base.algName = g_eccCommSpec->base.algName; eccKeyPairSpec->base.base.specType = HCF_KEY_PAIR_SPEC; eccKeyPairSpec->base.field = g_eccCommSpec->field; @@ -95,17 +102,15 @@ static HcfResult Constructbrainpool160r1KeyPairParamsSpec(const string &algoName res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ECC_PK_X_BN, &retBigInt); eccKeyPairSpec->pk.x.data = retBigInt.data; eccKeyPairSpec->pk.x.len = retBigInt.len; - res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ECC_PK_Y_BN, &retBigInt); eccKeyPairSpec->pk.y.data =retBigInt.data; eccKeyPairSpec->pk.y.len = retBigInt.len; - res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ECC_SK_BN, &retBigInt); eccKeyPairSpec->sk.data = retBigInt.data; eccKeyPairSpec->sk.len = retBigInt.len; - *spec = (HcfAsyKeyParamsSpec *)eccKeyPairSpec; HcfObjDestroy(generator); + HcfObjDestroy(keyPair); return HCF_SUCCESS; } diff --git a/test/unittest/src/crypto_brainpool_no_length_sign_test.cpp b/test/unittest/src/crypto_brainpool_no_length_sign_test.cpp index 19d7113..e8ca223 100644 --- a/test/unittest/src/crypto_brainpool_no_length_sign_test.cpp +++ b/test/unittest/src/crypto_brainpool_no_length_sign_test.cpp @@ -22,6 +22,7 @@ #include "blob.h" #include "detailed_ecc_key_params.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "ecc_common.h" #include "ecdsa_openssl.h" #include "memory.h" diff --git a/test/unittest/src/crypto_brainpool_no_length_verify_test.cpp b/test/unittest/src/crypto_brainpool_no_length_verify_test.cpp index 420708e..ff5f370 100644 --- a/test/unittest/src/crypto_brainpool_no_length_verify_test.cpp +++ b/test/unittest/src/crypto_brainpool_no_length_verify_test.cpp @@ -22,6 +22,7 @@ #include "blob.h" #include "detailed_ecc_key_params.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "ecc_common.h" #include "ecdsa_openssl.h" #include "memory.h" @@ -45,7 +46,7 @@ public: }; static string g_brainpool160r1AlgName = "ECC_BrainPoolP160r1"; -static string g_brainpool160r1curveName = "NID_brainpoolP160r1"; +static string g_brainpool160r1CurveName = "NID_brainpoolP160r1"; HcfEccCommParamsSpec *g_eccCommSpec = nullptr; @@ -74,6 +75,9 @@ static HcfResult ConstructEccBrainPool160r1KeyPairCommParamsSpec(const string &a { HcfEccCommParamsSpec *eccCommSpec = nullptr; HcfEccKeyUtilCreate(algoName.c_str(), &eccCommSpec); + if (eccCommSpec == nullptr) { + return HCF_INVALID_PARAMS; + } *spec = eccCommSpec; return HCF_SUCCESS; } @@ -82,11 +86,16 @@ static HcfResult Constructbrainpool160r1KeyPairParamsSpec(const string &algoName HcfAsyKeyParamsSpec **spec) { HcfAsyKeyGenerator *generator = nullptr; - int32_t res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); - + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } HcfKeyPair *keyPair = nullptr; res = generator->generateKeyPair(generator, nullptr, &keyPair); - + if (res != HCF_SUCCESS) { + HcfObjDestroy(keyPair); + return res; + } HcfEccKeyPairParamsSpec *eccKeyPairSpec = &g_brainpool160r1KeyPairSpec; HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; @@ -121,6 +130,7 @@ static HcfResult Constructbrainpool160r1KeyPairParamsSpec(const string &algoName *spec = (HcfAsyKeyParamsSpec *)eccKeyPairSpec; HcfObjDestroy(generator); + HcfObjDestroy(keyPair); return HCF_SUCCESS; } @@ -284,7 +294,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest009 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -313,7 +323,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest010 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -342,7 +352,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest011 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -371,7 +381,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest012 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -404,7 +414,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest013 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -434,7 +444,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest014 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -464,7 +474,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest015 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -497,7 +507,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest016 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -530,7 +540,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest017 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -563,7 +573,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest018 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -596,7 +606,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest019 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -632,7 +642,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest020 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -669,7 +679,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest021 ASSERT_NE(sign, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -726,7 +736,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest022 ASSERT_NE(sign, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -775,7 +785,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest023 ASSERT_NE(sign, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -832,7 +842,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest024 ASSERT_NE(sign, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -889,7 +899,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest025 ASSERT_NE(sign, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -943,7 +953,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest026 ASSERT_NE(sign, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -1015,7 +1025,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest028 ASSERT_NE(spiObj, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -1045,7 +1055,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest029 ASSERT_NE(spiObj, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -1235,7 +1245,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest037 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); @@ -1302,7 +1312,7 @@ HWTEST_F(CryptoBrainPoolNoLengthVerifyTest, CryptoBrainPoolNoLengthVerifyTest038 ASSERT_NE(verify, nullptr); HcfAsyKeyParamsSpec *paramSpec = nullptr; - res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1curveName, ¶mSpec); + res = Constructbrainpool160r1KeyPairParamsSpec(g_brainpool160r1AlgName, g_brainpool160r1CurveName, ¶mSpec); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(paramSpec, nullptr); diff --git a/test/unittest/src/crypto_dh_asy_key_generator_by_spec_test.cpp b/test/unittest/src/crypto_dh_asy_key_generator_by_spec_test.cpp new file mode 100644 index 0000000..661b73e --- /dev/null +++ b/test/unittest/src/crypto_dh_asy_key_generator_by_spec_test.cpp @@ -0,0 +1,1815 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "asy_key_generator.h" +#include "blob.h" +#include "dh_key_util.h" +#include "ecdsa_openssl.h" +#include "memory.h" +#include "securec.h" +#include "openssl_common.h" +#include "asy_key_params.h" +#include "params_parser.h" +#include "dh_asy_key_generator_openssl.h" +#include "detailed_dh_key_params.h" +#include "alg_25519_asy_key_generator_openssl.h" +#include "detailed_alg_25519_key_params.h" +#include "dh_common_param_spec_generator_openssl.h" +#include "memory_mock.h" +#include "openssl_adapter_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +constexpr int SKLEN_DH128 = 128; +constexpr int SKLEN_DH223 = 223; +constexpr int SKLEN_DH255 = 255; +constexpr int SKLEN_DH303 = 303; +constexpr int SKLEN_DH351 = 351; +constexpr int SKLEN_DH399 = 399; +constexpr int SKLEN_DH1024 = 1024; +constexpr int SKLEN_EQZERO = 0; +constexpr int PLEN_DH511 = 511; +constexpr int PLEN_DH512 = 512; +constexpr int PLEN_DH1536 = 1536; +constexpr int PLEN_DH2048 = 2048; +constexpr int PLEN_DH3072 = 3072; +constexpr int PLEN_DH4096 = 4096; +constexpr int PLEN_DH6144 = 6144; +constexpr int PLEN_DH8192 = 8192; +constexpr int PLEN_DH10001 = 10001; +constexpr int PLEN_LTZERO = -1; +constexpr int PLEN_LTSK = 20; + +class CryptoDHAsyKeyGeneratorBySpecTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +static string g_dh1536AlgoName = "DH_modp1536"; +static string g_dh2048AlgoName = "DH_modp2048"; +static string g_dh3072AlgoName = "DH_modp3072"; +static string g_dh4096AlgoName = "DH_modp4096"; +static string g_dh6144AlgoName = "DH_modp6144"; +static string g_dh8192AlgoName = "DH_modp8192"; +static string g_ed25519AlgoName = "Ed25519"; + +static string g_dhAlgoName = "DH"; +static string g_dhpubkeyformatName = "X.509"; +static string g_dhprikeyformatName = "PKCS#8"; + +HcfDhCommParamsSpec *g_dh1536CommSpec = nullptr; +HcfDhCommParamsSpec *g_dh2048CommSpec = nullptr; +HcfDhCommParamsSpec *g_dh3072CommSpec = nullptr; +HcfDhCommParamsSpec *g_dh4096CommSpec = nullptr; +HcfDhCommParamsSpec *g_dh6144CommSpec = nullptr; +HcfDhCommParamsSpec *g_dh8192CommSpec = nullptr; + +HcfDhKeyPairParamsSpec g_dhKeyPairSpec; +HcfDhPriKeyParamsSpec g_dhPriKeySpec; +HcfDhPubKeyParamsSpec g_dhPubKeySpec; + +HcfAlg25519KeyPairParamsSpec g_ed25519KeyPairSpec; +HcfAlg25519PriKeyParamsSpec g_ed25519PriKeySpec; +HcfAlg25519PubKeyParamsSpec g_ed25519PubKeySpec; + +void CryptoDHAsyKeyGeneratorBySpecTest::SetUp() {} +void CryptoDHAsyKeyGeneratorBySpecTest::TearDown() {} + +static const char *GetMockClass(void) +{ + return "HcfEcc"; +} +HcfObjectBase g_obj = { + .getClass = GetMockClass, + .destroy = nullptr +}; + +static HcfResult ConstructDHKeyCommParamsSpec(int32_t pLen, int32_t skLen, HcfDhCommParamsSpec **spec) +{ + HcfDhCommParamsSpec *dhCommSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(pLen, skLen, &dhCommSpec); + if (res != HCF_SUCCESS) { + return HCF_INVALID_PARAMS; + } + *spec = dhCommSpec; + return HCF_SUCCESS; +} + +static HcfResult ConstructDHKeyPairParamsCommonSpec(const string &algoName, HcfDhCommParamsSpec *comSpec, + HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + HcfDhKeyPairParamsSpec *dhKeyPairSpec = &g_dhKeyPairSpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + dhKeyPairSpec->base.base.algName = comSpec->base.algName; + dhKeyPairSpec->base.base.specType = HCF_COMMON_PARAMS_SPEC; + dhKeyPairSpec->base.g = comSpec->g; + dhKeyPairSpec->base.p = comSpec->p; + dhKeyPairSpec->base.length = comSpec->length; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + dhKeyPairSpec->pk.data = retBigInt.data; + dhKeyPairSpec->pk.len = retBigInt.len; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, DH_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + dhKeyPairSpec->sk.data = retBigInt.data; + dhKeyPairSpec->sk.len = retBigInt.len; + *spec = (HcfAsyKeyParamsSpec *)dhKeyPairSpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ConstructDHKeyPairParamsSpec(const string &algoName, HcfDhCommParamsSpec *comSpec, + HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + HcfDhKeyPairParamsSpec *dhKeyPairSpec = &g_dhKeyPairSpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + dhKeyPairSpec->base.base.algName = comSpec->base.algName; + dhKeyPairSpec->base.base.specType = HCF_KEY_PAIR_SPEC; + dhKeyPairSpec->base.g = comSpec->g; + dhKeyPairSpec->base.p = comSpec->p; + dhKeyPairSpec->base.length = comSpec->length; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + dhKeyPairSpec->pk.data = retBigInt.data; + dhKeyPairSpec->pk.len = retBigInt.len; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, DH_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + dhKeyPairSpec->sk.data = retBigInt.data; + dhKeyPairSpec->sk.len = retBigInt.len; + *spec = (HcfAsyKeyParamsSpec *)dhKeyPairSpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ConstructDHPubKeyParamsSpec(const string &algoName, HcfDhCommParamsSpec *comSpec, + HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + HcfDhPubKeyParamsSpec *dhPubKeySpec = &g_dhPubKeySpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + dhPubKeySpec->base.base.algName = comSpec->base.algName; + dhPubKeySpec->base.base.specType = HCF_PUBLIC_KEY_SPEC; + dhPubKeySpec->base.g = comSpec->g; + dhPubKeySpec->base.length = comSpec->length; + dhPubKeySpec->base.p = comSpec->p; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + dhPubKeySpec->pk.data = retBigInt.data; + dhPubKeySpec->pk.len = retBigInt.len; + + *spec = (HcfAsyKeyParamsSpec *)dhPubKeySpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ConstructDHPriKeyParamsSpec(const string &algoName, HcfDhCommParamsSpec *comSpec, + HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + HcfDhPriKeyParamsSpec *dhPriKeySpec = &g_dhPriKeySpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + dhPriKeySpec->base.base.algName = comSpec->base.algName; + dhPriKeySpec->base.base.specType = HCF_PRIVATE_KEY_SPEC; + dhPriKeySpec->base.g = comSpec->g; + dhPriKeySpec->base.length = comSpec->length; + dhPriKeySpec->base.p = comSpec->p; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, DH_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + dhPriKeySpec->sk.data = retBigInt.data; + dhPriKeySpec->sk.len = retBigInt.len; + + *spec = (HcfAsyKeyParamsSpec *)dhPriKeySpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ConstructEd25519KeyPairParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + HcfAlg25519KeyPairParamsSpec *ed25519KeyPairSpec = &g_ed25519KeyPairSpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + ed25519KeyPairSpec->base.algName = g_ed25519AlgoName.data(); + ed25519KeyPairSpec->base.specType = HCF_KEY_PAIR_SPEC; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ED25519_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + ed25519KeyPairSpec->pk.data = retBigInt.data; + ed25519KeyPairSpec->pk.len = retBigInt.len; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ED25519_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + ed25519KeyPairSpec->sk.data = retBigInt.data; + ed25519KeyPairSpec->sk.len = retBigInt.len; + + *spec = (HcfAsyKeyParamsSpec *)ed25519KeyPairSpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ConstructEd25519PubKeyParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + + HcfAlg25519PubKeyParamsSpec *ed25519PubKeySpec = &g_ed25519PubKeySpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + ed25519PubKeySpec->base.algName = g_ed25519AlgoName.data(); + ed25519PubKeySpec->base.specType = HCF_PUBLIC_KEY_SPEC; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ED25519_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + ed25519PubKeySpec->pk.data = retBigInt.data; + ed25519PubKeySpec->pk.len = retBigInt.len; + + *spec = (HcfAsyKeyParamsSpec *)ed25519PubKeySpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ConstructEd25519PriKeyParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return HCF_INVALID_PARAMS; + } + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return HCF_INVALID_PARAMS; + } + + HcfAlg25519PriKeyParamsSpec *ed25519PriKeySpec = &g_ed25519PriKeySpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + + ed25519PriKeySpec->base.algName = g_ed25519AlgoName.data(); + ed25519PriKeySpec->base.specType = HCF_PRIVATE_KEY_SPEC; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ED25519_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + ed25519PriKeySpec->sk.data = retBigInt.data; + ed25519PriKeySpec->sk.len = retBigInt.len; + + *spec = (HcfAsyKeyParamsSpec *)ed25519PriKeySpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + + return HCF_SUCCESS; +} + +void CryptoDHAsyKeyGeneratorBySpecTest::SetUpTestCase() +{ + HcfResult res = ConstructDHKeyCommParamsSpec(PLEN_DH1536, SKLEN_DH1024, &g_dh1536CommSpec); + ASSERT_EQ(res, HCF_SUCCESS); + res = ConstructDHKeyCommParamsSpec(PLEN_DH2048, SKLEN_DH1024, &g_dh2048CommSpec); + ASSERT_EQ(res, HCF_SUCCESS); + res = ConstructDHKeyCommParamsSpec(PLEN_DH3072, SKLEN_DH1024, &g_dh3072CommSpec); + ASSERT_EQ(res, HCF_SUCCESS); + res = ConstructDHKeyCommParamsSpec(PLEN_DH4096, SKLEN_DH1024, &g_dh4096CommSpec); + ASSERT_EQ(res, HCF_SUCCESS); + res = ConstructDHKeyCommParamsSpec(PLEN_DH6144, SKLEN_DH1024, &g_dh6144CommSpec); + ASSERT_EQ(res, HCF_SUCCESS); + res = ConstructDHKeyCommParamsSpec(PLEN_DH8192, SKLEN_DH1024, &g_dh8192CommSpec); + ASSERT_EQ(res, HCF_SUCCESS); +} + +void CryptoDHAsyKeyGeneratorBySpecTest::TearDownTestCase() +{ + FreeDhCommParamsSpec(g_dh1536CommSpec); + FreeDhCommParamsSpec(g_dh2048CommSpec); + FreeDhCommParamsSpec(g_dh3072CommSpec); + FreeDhCommParamsSpec(g_dh4096CommSpec); + FreeDhCommParamsSpec(g_dh6144CommSpec); + FreeDhCommParamsSpec(g_dh8192CommSpec); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_1, TestSize.Level0) +{ + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH2048, SKLEN_DH1024, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + + FreeDhCommParamsSpec(returnCommonParamSpec); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_2, TestSize.Level0) +{ + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH3072, SKLEN_DH1024, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + + FreeDhCommParamsSpec(returnCommonParamSpec); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_3, TestSize.Level0) +{ + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH4096, SKLEN_DH1024, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + + FreeDhCommParamsSpec(returnCommonParamSpec); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_4, TestSize.Level0) +{ + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH6144, SKLEN_DH1024, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + + FreeDhCommParamsSpec(returnCommonParamSpec); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_5, TestSize.Level0) +{ + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH8192, SKLEN_DH1024, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + + FreeDhCommParamsSpec(returnCommonParamSpec); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_6, TestSize.Level0) +{ + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH8192, SKLEN_EQZERO, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + + FreeDhCommParamsSpec(returnCommonParamSpec); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_1, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_2, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh2048AlgoName, g_dh2048CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_3, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh3072AlgoName, g_dh3072CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_4, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh4096AlgoName, g_dh4096CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_5, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh6144AlgoName, g_dh6144CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_6, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh8192AlgoName, g_dh8192CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest003, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + const char *className = returnObj->base.getClass(); + ASSERT_NE(className, NULL); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest004, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + returnObj->base.destroy(&(returnObj->base)); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest005, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + const char * algName = returnObj->getAlgName(returnObj); + ASSERT_EQ(algName, g_dhAlgoName); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest006, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest007, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest008, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(&(keyPair->base)); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest009, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->pubKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest010, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&(keyPair->pubKey->base.base)); + keyPair->pubKey = nullptr; + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest011, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(&(keyPair->pubKey->base)); + ASSERT_EQ(algorithmName, g_dhAlgoName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->pubKey->base.getFormat(&(keyPair->pubKey->base)); + ASSERT_EQ(formatName, g_dhpubkeyformatName); + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest012, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->priKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest013, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&(keyPair->priKey->base.base)); + keyPair->priKey = nullptr; + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest014, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(&(keyPair->priKey->base)); + ASSERT_EQ(algorithmName, g_dhAlgoName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->priKey->base.getFormat(&(keyPair->priKey->base)); + ASSERT_EQ(formatName, g_dhprikeyformatName); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest015, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(keyPair->priKey); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_ERR_CRYPTO_OPERATION); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest016, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHPubKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnObj->generatePubKey(returnObj, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = pubKey->getAsyKeySpecBigInteger(pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + res = pubKey->getAsyKeySpecBigInteger(pubKey, DH_G_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + res = pubKey->getAsyKeySpecBigInteger(pubKey, DH_PK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + int32_t returnInt = 0; + res = pubKey->getAsyKeySpecInt(pubKey, DH_L_NUM, &returnInt); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + ASSERT_EQ(returnInt, 0); + + HcfObjDestroy(pubKey); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest017, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHPriKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnObj->generatePriKey(returnObj, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = priKey->getAsyKeySpecBigInteger(priKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + res = priKey->getAsyKeySpecBigInteger(priKey, DH_G_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + res = priKey->getAsyKeySpecBigInteger(priKey, DH_SK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + int32_t returnInt = 0; + res = priKey->getAsyKeySpecInt(priKey, DH_L_NUM, &returnInt); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnInt, 0); + + HcfObjDestroy(priKey); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest018, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_1536, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfObjDestroy(returnSpi); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest019, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_1536, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnSpi->engineGenerateKeyPairBySpec(returnSpi, paramSpec, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest020, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_1536, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructDHPubKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnSpi->engineGeneratePubKeyBySpec(returnSpi, paramSpec, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(pubKey); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest021, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_1536, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructDHPriKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnSpi->engineGeneratePriKeyBySpec(returnSpi, paramSpec, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(priKey); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest022, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + const char *algName1 = returnObj->getAlgName(nullptr); + ASSERT_NE(algName1, g_dhAlgoName.data()); + + const char *algName2 = returnObj->getAlgName((HcfAsyKeyGeneratorBySpec *)&g_obj); + ASSERT_NE(algName2, g_dhAlgoName.data()); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest023, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(nullptr, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + res = returnObj->generateKeyPair((HcfAsyKeyGeneratorBySpec *)&g_obj, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest024, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(nullptr); + ASSERT_EQ(algorithmName, NULL); + + algorithmName = keyPair->pubKey->base.getAlgorithm((HcfKey *)&g_obj); + ASSERT_EQ(algorithmName, NULL); + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest025, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(nullptr, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->pubKey->base.getEncoded((HcfKey *)&g_obj, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest026, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *formatName = nullptr; + formatName = keyPair->pubKey->base.getFormat(nullptr); + ASSERT_EQ(formatName, nullptr); + + formatName = keyPair->pubKey->base.getFormat((HcfKey *)&g_obj); + ASSERT_EQ(formatName, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest027, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(nullptr); + ASSERT_EQ(algorithmName, NULL); + + algorithmName = keyPair->priKey->base.getAlgorithm((HcfKey *)&g_obj); + ASSERT_EQ(algorithmName, NULL); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest028, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(nullptr, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->priKey->base.getEncoded((HcfKey *)&g_obj, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest029, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *formatName = nullptr; + formatName = keyPair->priKey->base.getFormat(nullptr); + ASSERT_EQ(formatName, nullptr); + + formatName = keyPair->priKey->base.getFormat((HcfKey *)&g_obj); + ASSERT_EQ(formatName, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest030, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHPubKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnObj->generatePubKey(returnObj, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = pubKey->getAsyKeySpecBigInteger(pubKey, DH_PK_BN, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + res = pubKey->getAsyKeySpecBigInteger(pubKey, X25519_PK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnBigInteger.data, nullptr); + ASSERT_EQ(returnBigInteger.len, 0); + + res = pubKey->getAsyKeySpecBigInteger((HcfPubKey *)&g_obj, DH_PK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnBigInteger.data, nullptr); + ASSERT_EQ(returnBigInteger.len, 0); + + res = pubKey->getAsyKeySpecInt(pubKey, DH_L_NUM, nullptr); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + + int32_t returnInt = 0; + res = pubKey->getAsyKeySpecInt(pubKey, X25519_PK_BN, &returnInt); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + ASSERT_EQ(returnInt, 0); + + res = pubKey->getAsyKeySpecInt((HcfPubKey *)&g_obj, DH_L_NUM, &returnInt); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + ASSERT_EQ(returnInt, 0); + + char *returnString = nullptr; + res = pubKey->getAsyKeySpecString(pubKey, ED25519_SK_BN, &returnString); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + ASSERT_EQ(returnString, nullptr); + + res = pubKey->getAsyKeySpecString(pubKey, ECC_CURVE_NAME_STR, &returnString); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + ASSERT_EQ(returnString, nullptr); + + HcfObjDestroy(pubKey); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest031, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHPriKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnObj->generatePriKey(returnObj, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = priKey->getAsyKeySpecBigInteger(priKey, DH_PK_BN, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + res = priKey->getAsyKeySpecBigInteger(priKey, X25519_PK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnBigInteger.data, nullptr); + ASSERT_EQ(returnBigInteger.len, 0); + + res = priKey->getAsyKeySpecBigInteger((HcfPriKey *)&g_obj, DH_PK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnBigInteger.data, nullptr); + ASSERT_EQ(returnBigInteger.len, 0); + + res = priKey->getAsyKeySpecInt(priKey, DH_L_NUM, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + int32_t returnInt = 0; + res = priKey->getAsyKeySpecInt(priKey, X25519_PK_BN, &returnInt); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnInt, 0); + + res = priKey->getAsyKeySpecInt((HcfPriKey *)&g_obj, DH_L_NUM, &returnInt); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnInt, 0); + + char *returnString = nullptr; + res = priKey->getAsyKeySpecString(priKey, ED25519_SK_BN, &returnString); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + ASSERT_EQ(returnString, nullptr); + + res = priKey->getAsyKeySpecString(priKey, ECC_CURVE_NAME_STR, &returnString); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + ASSERT_EQ(returnString, nullptr); + + HcfObjDestroy(priKey); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest032, TestSize.Level0) +{ + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_LTZERO, SKLEN_DH1024, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_ERR_MALLOC); + + res = HcfDhKeyUtilCreate(PLEN_LTSK, SKLEN_DH1024, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest033, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHPubKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnObj->generatePriKey(returnObj, &priKey); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(priKey, nullptr); + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest034, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHPriKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnObj->generatePubKey(returnObj, &pubKey); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(pubKey, nullptr); + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest035, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_1536, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructDHKeyPairParamsCommonSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnSpi->engineGenerateKeyPairBySpec(returnSpi, paramSpec, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest036, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_1536, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + returnSpi->base.destroy(nullptr); + returnSpi->base.destroy(&g_obj); + HcfObjDestroy(returnSpi); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest037, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_1536, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructDHKeyPairParamsCommonSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + res = returnSpi->engineGenerateKeyPairBySpec(returnSpi, paramSpec, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + HcfKeyPair *keyPair = nullptr; + res = returnSpi->engineGenerateKeyPairBySpec((HcfAsyKeyGeneratorSpi *)&g_obj, paramSpec, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + paramSpec = nullptr; + res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + keyPair = nullptr; + res = returnSpi->engineGenerateKeyPairBySpec(returnSpi, paramSpec, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest038, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_2048, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructDHPubKeyParamsSpec(g_dh2048AlgoName, g_dh2048CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + res = returnSpi->engineGeneratePubKeyBySpec(returnSpi, paramSpec, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + HcfPubKey *pubKey = nullptr; + res = returnSpi->engineGeneratePubKeyBySpec((HcfAsyKeyGeneratorSpi *)&g_obj, paramSpec, &pubKey); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(pubKey, nullptr); + + paramSpec = nullptr; + res = ConstructEd25519PubKeyParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + pubKey = nullptr; + res = returnSpi->engineGeneratePubKeyBySpec(returnSpi, paramSpec, &pubKey); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(pubKey, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(pubKey); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest039, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_3072, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructDHPriKeyParamsSpec(g_dh3072AlgoName, g_dh3072CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + res = returnSpi->engineGeneratePriKeyBySpec(returnSpi, paramSpec, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + HcfPriKey *priKey = nullptr; + res = returnSpi->engineGeneratePriKeyBySpec((HcfAsyKeyGeneratorSpi *)&g_obj, paramSpec, &priKey); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(priKey, nullptr); + + paramSpec = nullptr; + res = ConstructEd25519PriKeyParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + priKey = nullptr; + res = returnSpi->engineGeneratePriKeyBySpec(returnSpi, paramSpec, &priKey); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(priKey, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(priKey); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest040, TestSize.Level0) +{ + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(nullptr, &returnSpi); + + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnSpi, nullptr); + + HcfObjDestroy(returnSpi); +} + +static void OpensslMockTestFunc(uint32_t mallocCount, HcfAsyKeyParamsSpec *paramSpec) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + HcfResult res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + if (res != HCF_SUCCESS) { + continue; + } + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + if (res != HCF_SUCCESS) { + continue; + } + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); + } +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest041, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHKeyPairParamsCommonSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + StartRecordOpensslCallNum(); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc(mallocCount, paramSpec); + + EndRecordOpensslCallNum(); +} + +static void OpensslMockTestFunc1(uint32_t mallocCount, HcfDhCommParamsSpec *returnCommonParamSpec) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH4096, SKLEN_DH1024, &returnCommonParamSpec); + if (res != HCF_SUCCESS) { + continue; + } + FreeDhCommParamsSpec(returnCommonParamSpec); + } +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest042, TestSize.Level0) +{ + StartRecordOpensslCallNum(); + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH4096, SKLEN_DH1024, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + + FreeDhCommParamsSpec(returnCommonParamSpec); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc1(mallocCount, returnCommonParamSpec); + + EndRecordOpensslCallNum(); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest043, TestSize.Level0) +{ + HcfResult res = HcfDhCommonParamSpecCreate(PLEN_DH4096, SKLEN_DH1024, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); +} + +static void OpensslMockTestFunc2(uint32_t mallocCount) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH512, SKLEN_DH128, &returnCommonParamSpec); + if (res != HCF_SUCCESS) { + continue; + } + FreeDhCommParamsSpec(returnCommonParamSpec); + } +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest044, TestSize.Level0) +{ + StartRecordOpensslCallNum(); + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH512, SKLEN_DH128, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + + FreeDhCommParamsSpec(returnCommonParamSpec); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc2(mallocCount); + + EndRecordOpensslCallNum(); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest045, TestSize.Level0) +{ + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH2048, SKLEN_DH223, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_ERR_MALLOC); + + res = HcfDhKeyUtilCreate(PLEN_DH3072, SKLEN_DH255, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_ERR_MALLOC); + + res = HcfDhKeyUtilCreate(PLEN_DH4096, SKLEN_DH303, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_ERR_MALLOC); + + res = HcfDhKeyUtilCreate(PLEN_DH6144, SKLEN_DH351, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_ERR_MALLOC); + + res = HcfDhKeyUtilCreate(PLEN_DH8192, SKLEN_DH399, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_ERR_MALLOC); + + FreeDhCommParamsSpec(returnCommonParamSpec); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest046, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructDHPriKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnObj->generatePriKey(returnObj, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = priKey->base.getEncoded(&(priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + HcfAsyKeyGenerator *generator = nullptr; + res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, nullptr, &blob, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(returnObj); + HcfObjDestroy(priKey); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest047, TestSize.Level0) +{ + HcfDhCommParamsSpec *returnCommonParamSpec = nullptr; + HcfResult res = HcfDhKeyUtilCreate(PLEN_DH10001, SKLEN_EQZERO, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_ERR_MALLOC); + + res = HcfDhKeyUtilCreate(PLEN_DH511, SKLEN_EQZERO, &returnCommonParamSpec); + ASSERT_EQ(res, HCF_ERR_MALLOC); + + FreeDhCommParamsSpec(returnCommonParamSpec); +} +} \ No newline at end of file diff --git a/test/unittest/src/crypto_dh_asy_key_generator_test.cpp b/test/unittest/src/crypto_dh_asy_key_generator_test.cpp new file mode 100644 index 0000000..72a91f4 --- /dev/null +++ b/test/unittest/src/crypto_dh_asy_key_generator_test.cpp @@ -0,0 +1,1876 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "asy_key_generator.h" +#include "dh_asy_key_generator_openssl.h" +#include "blob.h" +#include "detailed_ecc_key_params.h" +#include "ecc_key_util.h" +#include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" +#include "ecc_common.h" +#include "ecdsa_openssl.h" +#include "memory.h" +#include "securec.h" +#include "openssl_common.h" +#include "asy_key_params.h" +#include "params_parser.h" +#include "memory_mock.h" +#include "openssl_adapter_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoDHAsyKeyGeneratorTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void CryptoDHAsyKeyGeneratorTest::TearDownTestCase() {} +void CryptoDHAsyKeyGeneratorTest::SetUp() {} +void CryptoDHAsyKeyGeneratorTest::TearDown() {} + +static string g_dh1536AlgoName = "DH_modp1536"; +static string g_dhAlgoName = "DH"; +static string g_dhpubkeyformatName = "X.509"; +static string g_dhprikeyformatName = "PKCS#8"; +constexpr int BIT8 = 8; +constexpr int BIT4 = 4; +constexpr int BIT2 = 2; + +static const char *GetMockClass(void) +{ + return "HcfEcc"; +} +HcfObjectBase g_obj = { + .getClass = GetMockClass, + .destroy = nullptr +}; + +HcfBlob g_mockDH1536PriKeyBlob = { + .data = nullptr, + .len = 0 +}; + +HcfBlob g_mockDH1536PubKeyBlob = { + .data = nullptr, + .len = 0 +}; + +HcfBlob g_mockECC_BrainPool160r1PriKeyBlob = { + .data = nullptr, + .len = 0 +}; + +HcfBlob g_mockECC_BrainPool160r1PubKeyBlob = { + .data = nullptr, + .len = 0 +}; + +string g_modp_1536_p = + "ffffffffffffffffca237327f1746c084abc9804670c354e7096966d9ed52907" + "208552bb1c62f356dca3ad9683655d23fd24cf5f69163fa81c55d39a98da4836" + "a163bf05c2007cb8ece45b3d492866517c4b1fe6ae9f24115a899fa5ee386bfb" + "f406b7ed0bff5cb6a637ed6bf44c42e9625e7ec6e485b5766d51c2454fe1356d" + "f25f1437302b0a6dcd3a431bef9519b38e3404dd514a08793b139b22020bbea6" + "8a67cc7429024e0880dc1cd1c4c6628b2168c234c90fdaa2ffffffffffffffff"; + +string g_modp_2048_p = + "ffffffffffffffff8aacaa6815728e5a98fa051015d22618ea956ae53995497c" + "95581718de2bcbf66f4c52c9b5c55df0ec07a28f9b2783a2180e8603e39e772c" + "2e36ce3b32905e46ca18217cf1746c084abc9804670c354e7096966d9ed52907" + "208552bb1c62f356dca3ad9683655d23fd24cf5f69163fa81c55d39a98da4836" + "a163bf05c2007cb8ece45b3d492866517c4b1fe6ae9f24115a899fa5ee386bfb" + "f406b7ed0bff5cb6a637ed6bf44c42e9625e7ec6e485b5766d51c2454fe1356d" + "f25f1437302b0a6dcd3a431bef9519b38e3404dd514a08793b139b22020bbea6" + "8a67cc7429024e0880dc1cd1c4c6628b2168c234c90fdaa2ffffffffffffffff"; + +string g_modp_3072_p = + "ffffffffffffffffa93ad2ca4b82d120e0fd108e43db5bfc74e5ab3108e24fa0" + "bad946e2770988c07a615d6cbbe11757177b200c521f2b183ec86a64d8760273" + "d98a0864f12ffa061ad2ee6bcee3d2264a25619d1e8c94e0db0933d7abf5ae8c" + "a6e1e4c7b3970f855d060c7d8aea715758dbef0aecfb8504df1cba64a85521ab" + "04507a33ad33170d8aaac42d15728e5a98fa051015d22618ea956ae53995497c" + "95581718de2bcbf66f4c52c9b5c55df0ec07a28f9b2783a2180e8603e39e772c" + "2e36ce3b32905e46ca18217cf1746c084abc9804670c354e7096966d9ed52907" + "208552bb1c62f356dca3ad9683655d23fd24cf5f69163fa81c55d39a98da4836" + "a163bf05c2007cb8ece45b3d492866517c4b1fe6ae9f24115a899fa5ee386bfb" + "f406b7ed0bff5cb6a637ed6bf44c42e9625e7ec6e485b5766d51c2454fe1356d" + "f25f1437302b0a6dcd3a431bef9519b38e3404dd514a08793b139b22020bbea6" + "8a67cc7429024e0880dc1cd1c4c6628b2168c234c90fdaa2ffffffffffffffff"; + +string g_modp_4096_p = + "ffffffffffffffff340631994df435c990a6c08f86ffb7dc8d8fddc193b4ea98" + "d5b05aa9d00691272170481cb81bdd76cee2d7af1f612970515be7ed233ba186" + "a090c3a299b2964f4e6bc05d287c59471fbecaa62e8efc1404de8ef9dbbbc2db" + "2ad44ce82583e9cab6150bda1a9468346af4e23c99c32718bdba5b2688719a10" + "a787e6d71a723c12a92108014b82d120e0fd108e43db5bfc74e5ab3108e24fa0" + "bad946e2770988c07a615d6cbbe11757177b200c521f2b183ec86a64d8760273" + "d98a0864f12ffa061ad2ee6bcee3d2264a25619d1e8c94e0db0933d7abf5ae8c" + "a6e1e4c7b3970f855d060c7d8aea715758dbef0aecfb8504df1cba64a85521ab" + "04507a33ad33170d8aaac42d15728e5a98fa051015d22618ea956ae53995497c" + "95581718de2bcbf66f4c52c9b5c55df0ec07a28f9b2783a2180e8603e39e772c" + "2e36ce3b32905e46ca18217cf1746c084abc9804670c354e7096966d9ed52907" + "208552bb1c62f356dca3ad9683655d23fd24cf5f69163fa81c55d39a98da4836" + "a163bf05c2007cb8ece45b3d492866517c4b1fe6ae9f24115a899fa5ee386bfb" + "f406b7ed0bff5cb6a637ed6bf44c42e9625e7ec6e485b5766d51c2454fe1356d" + "f25f1437302b0a6dcd3a431bef9519b38e3404dd514a08793b139b22020bbea6" + "8a67cc7429024e0880dc1cd1c4c6628b2168c234c90fdaa2ffffffffffffffff"; + +string g_modp_6144_p = + "ffffffffffffffff6dcc4024e694f91e0b7474d612bf2d5b3f4860ee043e8f66" + "6e3c0468387fe8d72ef29632da56c9eca313d55ceb19ccb18a1fbff0f550aa3d" + "b7c5da7606a1d58bf29be328a79715ee0f8037e014cc5ed2bf48e1d8cc8f6d7e" + "2b4154aa4bd407b2ff585ac50f1d45b736cc88be23a97a7ebec7e8f359e7c97f" + "900b1c9eb5a8403146980c82d55e702f6e74fef6f482d7ced1721d03f032ea15" + "c64b92ec5983ca01378cd2bf6fb8f4012bd7af4233205151e6cc254bdb7f1447" + "ced4bb1b44ce6cbacf9b14edda3edbeb865a8918179727b09027d831b06a53ed" + "413001aee5db382fad9e530ef8ff94063dba37bdc9751e76602646dec1d4dcb2" + "d27c702636c3fab4340284924df435c990a6c08f86ffb7dc8d8fddc193b4ea98" + "d5b05aa9d00691272170481cb81bdd76cee2d7af1f612970515be7ed233ba186" + "a090c3a299b2964f4e6bc05d287c59471fbecaa62e8efc1404de8ef9dbbbc2db" + "2ad44ce82583e9cab6150bda1a9468346af4e23c99c32718bdba5b2688719a10" + "a787e6d71a723c12a92108014b82d120e0fd108e43db5bfc74e5ab3108e24fa0" + "bad946e2770988c07a615d6cbbe11757177b200c521f2b183ec86a64d8760273" + "d98a0864f12ffa061ad2ee6bcee3d2264a25619d1e8c94e0db0933d7abf5ae8c" + "a6e1e4c7b3970f855d060c7d8aea715758dbef0aecfb8504df1cba64a85521ab" + "04507a33ad33170d8aaac42d15728e5a98fa051015d22618ea956ae53995497c" + "95581718de2bcbf66f4c52c9b5c55df0ec07a28f9b2783a2180e8603e39e772c" + "2e36ce3b32905e46ca18217cf1746c084abc9804670c354e7096966d9ed52907" + "208552bb1c62f356dca3ad9683655d23fd24cf5f69163fa81c55d39a98da4836" + "a163bf05c2007cb8ece45b3d492866517c4b1fe6ae9f24115a899fa5ee386bfb" + "f406b7ed0bff5cb6a637ed6bf44c42e9625e7ec6e485b5766d51c2454fe1356d" + "f25f1437302b0a6dcd3a431bef9519b38e3404dd514a08793b139b22020bbea6" + "8a67cc7429024e0880dc1cd1c4c6628b2168c234c90fdaa2ffffffffffffffff"; + +string g_modp_8192_p = + "ffffffffffffffff98edd3df60c980dd80b96e71c81f56e8765694df9e3050e2" + "5677e9aa9558e447fc026e47c9190da6d5ee382b889a002e481c6cd74009438b" + "eb879f92359046f41ecfa268faf36bc37ee74d73b1d510bd5ded7ea1f9ab4819" + "0846851d64f31cc5a0255dc14597e89974ab6a36df310ee03f44f82d6d2a13f8" + "b3a278a6062b3cf5ed5bdd3a79683303a2c087e8fa9d4b7f2f8385dd4bcbc886" + "6cea306b3473fc641a23f0c713eb57a8a4037c0722222e04fc848ad9e3fdb8be" + "e39d652d238f16cb2bf1c9783423b4745ae4f5683aab639c6ba424662576f693" + "8afc47ed741fa7bf8d9dd3003bc832b673b931bad8bec4d0a932df8c38777cb6" + "12fee5e474a3926f6dbe1159e694f91e0b7474d612bf2d5b3f4860ee043e8f66" + "6e3c0468387fe8d72ef29632da56c9eca313d55ceb19ccb18a1fbff0f550aa3d" + "b7c5da7606a1d58bf29be328a79715ee0f8037e014cc5ed2bf48e1d8cc8f6d7e" + "2b4154aa4bd407b2ff585ac50f1d45b736cc88be23a97a7ebec7e8f359e7c97f" + "900b1c9eb5a8403146980c82d55e702f6e74fef6f482d7ced1721d03f032ea15" + "c64b92ec5983ca01378cd2bf6fb8f4012bd7af4233205151e6cc254bdb7f1447" + "ced4bb1b44ce6cbacf9b14edda3edbeb865a8918179727b09027d831b06a53ed" + "413001aee5db382fad9e530ef8ff94063dba37bdc9751e76602646dec1d4dcb2" + "d27c702636c3fab4340284924df435c990a6c08f86ffb7dc8d8fddc193b4ea98" + "d5b05aa9d00691272170481cb81bdd76cee2d7af1f612970515be7ed233ba186" + "a090c3a299b2964f4e6bc05d287c59471fbecaa62e8efc1404de8ef9dbbbc2db" + "2ad44ce82583e9cab6150bda1a9468346af4e23c99c32718bdba5b2688719a10" + "a787e6d71a723c12a92108014b82d120e0fd108e43db5bfc74e5ab3108e24fa0" + "bad946e2770988c07a615d6cbbe11757177b200c521f2b183ec86a64d8760273" + "d98a0864f12ffa061ad2ee6bcee3d2264a25619d1e8c94e0db0933d7abf5ae8c" + "a6e1e4c7b3970f855d060c7d8aea715758dbef0aecfb8504df1cba64a85521ab" + "04507a33ad33170d8aaac42d15728e5a98fa051015d22618ea956ae53995497c" + "95581718de2bcbf66f4c52c9b5c55df0ec07a28f9b2783a2180e8603e39e772c" + "2e36ce3b32905e46ca18217cf1746c084abc9804670c354e7096966d9ed52907" + "208552bb1c62f356dca3ad9683655d23fd24cf5f69163fa81c55d39a98da4836" + "a163bf05c2007cb8ece45b3d492866517c4b1fe6ae9f24115a899fa5ee386bfb" + "f406b7ed0bff5cb6a637ed6bf44c42e9625e7ec6e485b5766d51c2454fe1356d" + "f25f1437302b0a6dcd3a431bef9519b38e3404dd514a08793b139b22020bbea6" + "8a67cc7429024e0880dc1cd1c4c6628b2168c234c90fdaa2ffffffffffffffff"; + +string g_ffdhe_2048_p = + "ffffffffffffffff61285c97886b4238c1b2effac6f34a267d1683b2c58ef183" + "2ec220053bb5fcbc4c6fad73c3fe3b1beef281838e4f1232e98583ff9172fe9c" + "28342f61c03404cdcdf7e2ec9e02fce1ee0a6d700b07a7c86372bb19ae56ede7" + "de394df41d4f42a360d7f468b96adab7b2c8e3fbd108a94bb324fb61bc0ab182" + "483a797a30acca4f36ade7351df158a1f3efe872e2a689dae0e68b77984f0c70" + "7f57c935b557135e3ded1af3856365555f066ed02433f51fd5fd6561d3df1ed5" + "aec4617af681b202630c75d87d2fe363249b3ef9cc939dce146433fba9e13641" + "ce2d3695d8b9c583273d3cf1afdc5620a2bb4a9aadf85458ffffffffffffffff"; + +string g_ffdhe_3072_p = + "ffffffffffffffff66c62e3725e41d2b3fd59d7c3c1b20eefa53ddef0abcd06b" + "d5c4484e1dbf9a429b0deadaabc5219722363a0de86d2bc59c9df69e5cae82ab" + "71f54bff64f2e21ee2d74dd3f4fd4452bc437944b4130c9385139270aefe1309" + "c186d91c598cb0fa91f7f7ee7ad91d26d6e6c90761b46fc9f99c0238bc34f4de" + "6519035bde355b3b611fcfdc886b4238c1b2effac6f34a267d1683b2c58ef183" + "2ec220053bb5fcbc4c6fad73c3fe3b1beef281838e4f1232e98583ff9172fe9c" + "28342f61c03404cdcdf7e2ec9e02fce1ee0a6d700b07a7c86372bb19ae56ede7" + "de394df41d4f42a360d7f468b96adab7b2c8e3fbd108a94bb324fb61bc0ab182" + "483a797a30acca4f36ade7351df158a1f3efe872e2a689dae0e68b77984f0c70" + "7f57c935b557135e3ded1af3856365555f066ed02433f51fd5fd6561d3df1ed5" + "aec4617af681b202630c75d87d2fe363249b3ef9cc939dce146433fba9e13641" + "ce2d3695d8b9c583273d3cf1afdc5620a2bb4a9aadf85458ffffffffffffffff"; + +string g_ffdhe_4096_p = + "ffffffffffffffff5e655f6ac68a007ef44182e14db5a8517f88a46b8ec9b55a" + "cec97dcf0a8291cdf98d0acc2a4ecea97140003c1a1db93d33cb8b7a092999a3" + "71ad00386dc778f9918130c4a907600a2d9e6832ed6a1e01efb4318a7135c886" + "7e31cc7a87f55ba5550340047763cf1dd69f6d18ac7d5f42e58857b67930e9e4" + "164df4fb6e6f52c3669e1ef125e41d2b3fd59d7c3c1b20eefa53ddef0abcd06b" + "d5c4484e1dbf9a429b0deadaabc5219722363a0de86d2bc59c9df69e5cae82ab" + "71f54bff64f2e21ee2d74dd3f4fd4452bc437944b4130c9385139270aefe1309" + "c186d91c598cb0fa91f7f7ee7ad91d26d6e6c90761b46fc9f99c0238bc34f4de" + "6519035bde355b3b611fcfdc886b4238c1b2effac6f34a267d1683b2c58ef183" + "2ec220053bb5fcbc4c6fad73c3fe3b1beef281838e4f1232e98583ff9172fe9c" + "28342f61c03404cdcdf7e2ec9e02fce1ee0a6d700b07a7c86372bb19ae56ede7" + "de394df41d4f42a360d7f468b96adab7b2c8e3fbd108a94bb324fb61bc0ab182" + "483a797a30acca4f36ade7351df158a1f3efe872e2a689dae0e68b77984f0c70" + "7f57c935b557135e3ded1af3856365555f066ed02433f51fd5fd6561d3df1ed5" + "aec4617af681b202630c75d87d2fe363249b3ef9cc939dce146433fba9e13641" + "ce2d3695d8b9c583273d3cf1afdc5620a2bb4a9aadf85458ffffffffffffffff"; + +string g_ffdhe_6144_p = + "ffffffffffffffffd0e40e65a40e329c7938dad4a41d570dd43161c162a69526" + "9adb1e693fdd4a8edc6b80d65b3b71f9c6272b04ec9d1810cacef4038ccf2dd5" + "c95b9117e49f5235b854338a505dc82d1562a84662292c316ae77f5ed72b0374" + "462d538cf9c9091b47a67cbe0ae8db5822611682b3a739c12a281bf6eeaac023" + "77caf99294c6651e94b2bbc1763e4e4b0077d9b4587e38da183023c37fb29f8c" + "f9e3a26e0abec1ff350511e3a00ef092db6340d8b855322ea9a96910a52471f7" + "4cfdb477388147fb4e46041f9b1f5c3efccfec71cdad06574c701c3ab38e8c33" + "b1c0fd4c917bdd649b7624c83bb45432caf53ea623ba444238532a3a4e677d2c" + "45036c7a0bfd64b65e0dd902c68a007ef44182e14db5a8517f88a46b8ec9b55a" + "cec97dcf0a8291cdf98d0acc2a4ecea97140003c1a1db93d33cb8b7a092999a3" + "71ad00386dc778f9918130c4a907600a2d9e6832ed6a1e01efb4318a7135c886" + "7e31cc7a87f55ba5550340047763cf1dd69f6d18ac7d5f42e58857b67930e9e4" + "164df4fb6e6f52c3669e1ef125e41d2b3fd59d7c3c1b20eefa53ddef0abcd06b" + "d5c4484e1dbf9a429b0deadaabc5219722363a0de86d2bc59c9df69e5cae82ab" + "71f54bff64f2e21ee2d74dd3f4fd4452bc437944b4130c9385139270aefe1309" + "c186d91c598cb0fa91f7f7ee7ad91d26d6e6c90761b46fc9f99c0238bc34f4de" + "6519035bde355b3b611fcfdc886b4238c1b2effac6f34a267d1683b2c58ef183" + "2ec220053bb5fcbc4c6fad73c3fe3b1beef281838e4f1232e98583ff9172fe9c" + "28342f61c03404cdcdf7e2ec9e02fce1ee0a6d700b07a7c86372bb19ae56ede7" + "de394df41d4f42a360d7f468b96adab7b2c8e3fbd108a94bb324fb61bc0ab182" + "483a797a30acca4f36ade7351df158a1f3efe872e2a689dae0e68b77984f0c70" + "7f57c935b557135e3ded1af3856365555f066ed02433f51fd5fd6561d3df1ed5" + "aec4617af681b202630c75d87d2fe363249b3ef9cc939dce146433fba9e13641" + "ce2d3695d8b9c583273d3cf1afdc5620a2bb4a9aadf85458ffffffffffffffff"; + +string g_ffdhe_8192_p = + "ffffffffffffffffc5c6424cd68c8bb7838ff88c011e2a94a9f4614e0822e506" + "f7a8443d97d11d4930677f0da6bbfde5c1fe86fe2f741ef85d71a87efafabe1c" + "fbe58a30ded2fbab72b0a66eb6855dfeba8a4fe81efc8ce03f2fa45783f81d4a" + "a577e231a1fe307588d9c0a0d5b80194ad9a95f9624816cd50c1217b99e9e316" + "0e423cfc51aa691e3826e52c1c217e6c09703fee51a8a9316a460e74bb709987" + "9c86b022541fc68c46fd825159160cc035c35f5c2846c0ba8b75828254504ac7" + "d2af05e429388839c01bd702cb2c0f1c7c932665555b2f74a3ab882986b63142" + "f64b10ef0b8cc3bdedd1cc5e687feb69c9509d43fdb23fced951ae641e425a31" + "f600c83836ad004ccff46aaaa40e329c7938dad4a41d570dd43161c162a69526" + "9adb1e693fdd4a8edc6b80d65b3b71f9c6272b04ec9d1810cacef4038ccf2dd5" + "c95b9117e49f5235b854338a505dc82d1562a84662292c316ae77f5ed72b0374" + "462d538cf9c9091b47a67cbe0ae8db5822611682b3a739c12a281bf6eeaac023" + "77caf99294c6651e94b2bbc1763e4e4b0077d9b4587e38da183023c37fb29f8c" + "f9e3a26e0abec1ff350511e3a00ef092db6340d8b855322ea9a96910a52471f7" + "4cfdb477388147fb4e46041f9b1f5c3efccfec71cdad06574c701c3ab38e8c33" + "b1c0fd4c917bdd649b7624c83bb45432caf53ea623ba444238532a3a4e677d2c" + "45036c7a0bfd64b65e0dd902c68a007ef44182e14db5a8517f88a46b8ec9b55a" + "cec97dcf0a8291cdf98d0acc2a4ecea97140003c1a1db93d33cb8b7a092999a3" + "71ad00386dc778f9918130c4a907600a2d9e6832ed6a1e01efb4318a7135c886" + "7e31cc7a87f55ba5550340047763cf1dd69f6d18ac7d5f42e58857b67930e9e4" + "164df4fb6e6f52c3669e1ef125e41d2b3fd59d7c3c1b20eefa53ddef0abcd06b" + "d5c4484e1dbf9a429b0deadaabc5219722363a0de86d2bc59c9df69e5cae82ab" + "71f54bff64f2e21ee2d74dd3f4fd4452bc437944b4130c9385139270aefe1309" + "c186d91c598cb0fa91f7f7ee7ad91d26d6e6c90761b46fc9f99c0238bc34f4de" + "6519035bde355b3b611fcfdc886b4238c1b2effac6f34a267d1683b2c58ef183" + "2ec220053bb5fcbc4c6fad73c3fe3b1beef281838e4f1232e98583ff9172fe9c" + "28342f61c03404cdcdf7e2ec9e02fce1ee0a6d700b07a7c86372bb19ae56ede7" + "de394df41d4f42a360d7f468b96adab7b2c8e3fbd108a94bb324fb61bc0ab182" + "483a797a30acca4f36ade7351df158a1f3efe872e2a689dae0e68b77984f0c70" + "7f57c935b557135e3ded1af3856365555f066ed02433f51fd5fd6561d3df1ed5" + "aec4617af681b202630c75d87d2fe363249b3ef9cc939dce146433fba9e13641" + "ce2d3695d8b9c583273d3cf1afdc5620a2bb4a9aadf85458ffffffffffffffff"; + +static HcfResult DH1536KeyBlob(HcfBlob * priblob, HcfBlob *pubblob) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &g_mockDH1536PriKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &g_mockDH1536PubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ECC_BrainPool160r1KeyBlob(HcfBlob * priblob, HcfBlob *pubblob) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("ECC_BrainPoolP160r1", &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &g_mockECC_BrainPool160r1PriKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &g_mockECC_BrainPool160r1PubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +void CryptoDHAsyKeyGeneratorTest::SetUpTestCase() +{ + HcfResult res = DH1536KeyBlob(&g_mockDH1536PriKeyBlob, &g_mockDH1536PubKeyBlob); + ASSERT_EQ(res, HCF_SUCCESS); + res = ECC_BrainPool160r1KeyBlob(&g_mockECC_BrainPool160r1PriKeyBlob, &g_mockECC_BrainPool160r1PubKeyBlob); + ASSERT_EQ(res, HCF_SUCCESS); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_1, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_2, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp2048", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_3, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp3072", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_4, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp4096", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_5, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp6144", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_6, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp8192", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_7, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_ffdhe2048", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_8, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_ffdhe3072", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_9, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_ffdhe4096", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_10, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_ffdhe6144", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest001_11, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_ffdhe8192", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest002, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + const char *className = generator->base.getClass(); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest003, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + generator->base.destroy((HcfObjectBase *)generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest004, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + const char *algoName = generator->getAlgoName(generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_EQ(algoName, g_dh1536AlgoName); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest005, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest006, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest007, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(&(keyPair->base)); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest008, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->pubKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest009, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&(keyPair->pubKey->base.base)); + keyPair->pubKey = nullptr; + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest010, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(&(keyPair->pubKey->base)); + ASSERT_EQ(algorithmName, g_dhAlgoName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->pubKey->base.getFormat(&(keyPair->pubKey->base)); + ASSERT_EQ(formatName, g_dhpubkeyformatName); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest011, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->priKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest012, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&(keyPair->priKey->base.base)); + keyPair->priKey = nullptr; + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest013, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(&(keyPair->priKey->base)); + ASSERT_EQ(algorithmName, g_dhAlgoName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->priKey->base.getFormat(&(keyPair->priKey->base)); + ASSERT_EQ(formatName, g_dhprikeyformatName); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest014, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(keyPair->priKey); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_ERR_CRYPTO_OPERATION); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest015, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + res = generator->convertKey(generator, nullptr, nullptr, &g_mockDH1536PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, nullptr, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest016, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest017, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(&(keyPair->base)); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest018, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->pubKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest019, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&(keyPair->pubKey->base.base)); + keyPair->pubKey = nullptr; + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest020, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(&(keyPair->pubKey->base)); + ASSERT_EQ(algorithmName, g_dhAlgoName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->pubKey->base.getFormat(&(keyPair->pubKey->base)); + ASSERT_EQ(formatName, g_dhpubkeyformatName); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest021, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->priKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest022, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&(keyPair->priKey->base.base)); + keyPair->priKey = nullptr; + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest023, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(&(keyPair->priKey->base)); + ASSERT_EQ(algorithmName, g_dhAlgoName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->priKey->base.getFormat(&(keyPair->priKey->base)); + ASSERT_EQ(formatName, g_dhprikeyformatName); + + int32_t returnInt = 0; + res = keyPair->priKey->getAsyKeySpecInt(keyPair->priKey, DH_L_NUM, &returnInt); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_EQ(returnInt, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest024, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(keyPair->priKey); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_ERR_CRYPTO_OPERATION); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +static void MemoryMallocTestFunc(uint32_t mallocCount) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetRecordMallocNum(); + SetMockMallocIndex(i); + HcfAsyKeyGenerator *tmpGenerator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &tmpGenerator); + if (res != HCF_SUCCESS) { + continue; + } + HcfKeyPair *tmpKeyPair = nullptr; + res = tmpGenerator->generateKeyPair(tmpGenerator, nullptr, &tmpKeyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPubKeyBlob = { + .data = nullptr, + .len = 0 + }; + res = tmpKeyPair->pubKey->base.getEncoded(&(tmpKeyPair->pubKey->base), &tmpPubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPriKeyBlob = { + .data = nullptr, + .len = 0 + }; + res = tmpKeyPair->priKey->base.getEncoded(&(tmpKeyPair->priKey->base), &tmpPriKeyBlob); + if (res != HCF_SUCCESS) { + free(tmpPubKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfKeyPair *tmpOutKeyPair = nullptr; + res = tmpGenerator->convertKey(tmpGenerator, nullptr, &tmpPubKeyBlob, &tmpPriKeyBlob, &tmpOutKeyPair); + free(tmpPubKeyBlob.data); + free(tmpPriKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + if (res == HCF_SUCCESS) { + HcfObjDestroy(tmpOutKeyPair); + } + } +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest025, TestSize.Level0) +{ + StartRecordMallocNum(); + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob pubKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &pubKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKeyBlob.data, nullptr); + ASSERT_NE(pubKeyBlob.len, 0); + + HcfBlob priKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &priKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKeyBlob.data, nullptr); + ASSERT_NE(priKeyBlob.len, 0); + + HcfKeyPair *outKeyPair = nullptr; + res = generator->convertKey(generator, nullptr, &pubKeyBlob, &priKeyBlob, &outKeyPair); + + free(pubKeyBlob.data); + free(priKeyBlob.data); + HcfObjDestroy(outKeyPair); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + + uint32_t mallocCount = GetMallocNum(); + MemoryMallocTestFunc(mallocCount); + + EndRecordMallocNum(); +} + +static void OpensslMockTestFunc(uint32_t mallocCount) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + HcfAsyKeyGenerator *tmpGenerator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &tmpGenerator); + if (res != HCF_SUCCESS) { + continue; + } + HcfKeyPair *tmpKeyPair = nullptr; + res = tmpGenerator->generateKeyPair(tmpGenerator, nullptr, &tmpKeyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPubKeyBlob = { .data = nullptr, .len = 0 }; + res = tmpKeyPair->pubKey->base.getEncoded(&(tmpKeyPair->pubKey->base), &tmpPubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPriKeyBlob = { .data = nullptr, .len = 0 }; + res = tmpKeyPair->priKey->base.getEncoded(&(tmpKeyPair->priKey->base), &tmpPriKeyBlob); + if (res != HCF_SUCCESS) { + free(tmpPubKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfKeyPair *tmpOutKeyPair = nullptr; + res = tmpGenerator->convertKey(tmpGenerator, nullptr, &tmpPubKeyBlob, &tmpPriKeyBlob, &tmpOutKeyPair); + free(tmpPubKeyBlob.data); + free(tmpPriKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + if (res == HCF_SUCCESS) { + HcfObjDestroy(tmpOutKeyPair); + } + } +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest026, TestSize.Level0) +{ + StartRecordOpensslCallNum(); + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob pubKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &pubKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKeyBlob.data, nullptr); + ASSERT_NE(pubKeyBlob.len, 0); + + HcfBlob priKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &priKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKeyBlob.data, nullptr); + ASSERT_NE(priKeyBlob.len, 0); + + HcfKeyPair *outKeyPair = nullptr; + res = generator->convertKey(generator, nullptr, &pubKeyBlob, &priKeyBlob, &outKeyPair); + + free(pubKeyBlob.data); + free(priKeyBlob.data); + HcfObjDestroy(outKeyPair); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc(mallocCount); + + EndRecordOpensslCallNum(); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest027, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + const char *algoName1 = generator->getAlgoName(nullptr); + ASSERT_EQ(algoName1, NULL); + + const char *algoName2 = generator->getAlgoName((HcfAsyKeyGenerator *)&g_obj); + ASSERT_EQ(algoName2, NULL); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest028, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + generator->base.destroy(nullptr); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest029, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + generator->base.destroy(&g_obj); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest030, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(nullptr, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, NULL); + + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + res = generator->convertKey((HcfAsyKeyGenerator *)&g_obj, nullptr, &g_mockDH1536PubKeyBlob, + &g_mockDH1536PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, NULL); + + res = generator->convertKey(generator, nullptr, nullptr, nullptr, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, NULL); + + res = generator->convertKey(generator, nullptr, &g_mockECC_BrainPool160r1PubKeyBlob, + &g_mockECC_BrainPool160r1PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_ERR_CRYPTO_OPERATION); + ASSERT_EQ(keyPair, NULL); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest031, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(nullptr); + ASSERT_EQ(algorithmName, NULL); + + algorithmName = keyPair->pubKey->base.getAlgorithm((HcfKey *)&g_obj); + ASSERT_EQ(algorithmName, NULL); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest032, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(nullptr, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->pubKey->base.getEncoded((HcfKey *)&g_obj, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest033, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *formatName = nullptr; + formatName = keyPair->pubKey->base.getFormat(nullptr); + ASSERT_EQ(formatName, nullptr); + + formatName = keyPair->pubKey->base.getFormat((HcfKey *)&g_obj); + ASSERT_EQ(formatName, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest034, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(nullptr); + ASSERT_EQ(algorithmName, NULL); + + algorithmName = keyPair->priKey->base.getAlgorithm((HcfKey *)&g_obj); + ASSERT_EQ(algorithmName, NULL); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest035, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(nullptr, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->priKey->base.getEncoded((HcfKey *)&g_obj, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest036, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *formatName = nullptr; + formatName = keyPair->priKey->base.getFormat(nullptr); + ASSERT_EQ(formatName, nullptr); + + formatName = keyPair->priKey->base.getFormat((HcfKey *)&g_obj); + ASSERT_EQ(formatName, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest037, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(nullptr); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest038, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem((HcfPriKey *)&g_obj); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest039, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(nullptr); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest040, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&g_obj); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest041, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(nullptr); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest042, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&g_obj); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest043, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_1536, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnSpi->engineConvertKey((HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, &g_mockDH1536PubKeyBlob, + &g_mockDH1536PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest044, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_1536, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnSpi->engineGenerateKeyPair((HcfAsyKeyGeneratorSpi *)&g_obj, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest045, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_DH, + .bits = HCF_OPENSSL_DH_MODP_1536, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + res = returnSpi->engineGenerateKeyPair(returnSpi, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + HcfObjDestroy(returnSpi); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest046, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(nullptr); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest047, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockDH1536PubKeyBlob, &g_mockDH1536PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(&g_obj); +} + +static char *ByteToHexString(unsigned char *byteArray, int byteArrayLen) +{ + char *hexString = static_cast(malloc(byteArrayLen * BIT2 + 1)); + if (hexString == nullptr) { + return nullptr; + } + int i; + for (i = 0; i < byteArrayLen; i++) { + if (sprintf_s(hexString + i * BIT2, (byteArrayLen - i) * BIT2 + 1, "%02x", byteArray[i]) < 0) { + HcfFree(hexString); + return nullptr; + } + } + hexString[byteArrayLen * BIT2] = '\0'; + + char *reversedString = static_cast(malloc(byteArrayLen * BIT2 + 1)); + if (reversedString == nullptr) { + HcfFree(hexString); + return nullptr; + } + int j = 0; + for (i = 0; i < byteArrayLen * BIT2; i += BIT8) { + char group[BIT8 + 1]; + (void)memcpy_s(group, BIT8, hexString + i, BIT8); + group[BIT8] = '\0'; + for (int k = 0; k < BIT4; k++) { + char temp = group[BIT2 * k]; + group[BIT2 * k] = group[BIT2 * k + 1]; + group[BIT2 * k + 1] = temp; + } + (void)memcpy_s(reversedString + j, BIT8, group, BIT8); + j += BIT8; + } + reversedString[j] = '\0'; + + for (i = 0; i < byteArrayLen * BIT2; i += BIT8) { + int start = i; + int end = i + BIT8 - 1; + if (end >= byteArrayLen * BIT2) { + end = byteArrayLen * BIT2 - 1; + } + for (j = start; j < end; j++, end--) { + char temp = reversedString[j]; + reversedString[j] = reversedString[end]; + reversedString[end] = temp; + } + } + HcfFree(hexString); + return reversedString; +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest048, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_modp_1536_p.data()); + EXPECT_EQ(flag, 0); + + HcfFree(hexString); + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest049, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp2048", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_modp_2048_p.data()); + EXPECT_EQ(flag, 0); + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest050, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp3072", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_modp_3072_p.data()); + EXPECT_EQ(flag, 0); + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest051, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp4096", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_modp_4096_p.data()); + EXPECT_EQ(flag, 0); + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest052, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp6144", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_modp_6144_p.data()); + EXPECT_EQ(flag, 0); + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest053, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_modp8192", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_modp_8192_p.data()); + EXPECT_EQ(flag, 0); + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest054, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_ffdhe2048", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_ffdhe_2048_p.data()); + EXPECT_EQ(flag, 0); + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest055, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_ffdhe3072", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_ffdhe_3072_p.data()); + EXPECT_EQ(flag, 0); + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest056, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_ffdhe4096", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_ffdhe_4096_p.data()); + EXPECT_EQ(flag, 0); + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest057, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_ffdhe6144", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_ffdhe_6144_p.data()); + EXPECT_EQ(flag, 0); + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoDHAsyKeyGeneratorTest, CryptoDHAsyKeyGeneratorTest058, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("DH_ffdhe8192", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + + char *hexString = ByteToHexString(returnBigInteger.data, returnBigInteger.len); + ASSERT_NE(hexString, nullptr); + int32_t flag = strcmp(hexString, g_ffdhe_8192_p.data()); + EXPECT_EQ(flag, 0); + + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} +} diff --git a/test/unittest/src/crypto_dh_key_agreement_test.cpp b/test/unittest/src/crypto_dh_key_agreement_test.cpp new file mode 100644 index 0000000..836899d --- /dev/null +++ b/test/unittest/src/crypto_dh_key_agreement_test.cpp @@ -0,0 +1,500 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "securec.h" + +#include "asy_key_generator.h" +#include "alg_25519_asy_key_generator_openssl.h" +#include "detailed_alg_25519_key_params.h" +#include "key_agreement.h" +#include "ecc_key_util.h" +#include "params_parser.h" +#include "dh_asy_key_generator_openssl.h" +#include "detailed_dh_key_params.h" +#include "dh_key_util.h" +#include "dh_openssl.h" +#include "memory_mock.h" +#include "openssl_adapter_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoDHKeyAgreementTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + static HcfKeyPair *dh1536KeyPair_; + static HcfKeyPair *dh3072KeyPair1_; + static HcfKeyPair *dh3072KeyPair2_; +}; + +HcfKeyPair *CryptoDHKeyAgreementTest::dh1536KeyPair_ = nullptr; +HcfKeyPair *CryptoDHKeyAgreementTest::dh3072KeyPair1_ = nullptr; +HcfKeyPair *CryptoDHKeyAgreementTest::dh3072KeyPair2_ = nullptr; + +static string g_dh1536AlgoName = "DH_modp1536"; +static string g_dh3072AlgoName = "DH_modp3072"; + +void CryptoDHKeyAgreementTest::SetUp() {} +void CryptoDHKeyAgreementTest::TearDown() {} + +static const char *GetMockClass(void) +{ + return "HcfSymKeyGenerator"; +} + +static HcfObjectBase g_obj = { + .getClass = GetMockClass, + .destroy = nullptr +}; + +void CryptoDHKeyAgreementTest::SetUpTestCase() +{ + HcfAsyKeyGenerator *generator = nullptr; + int32_t res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + dh1536KeyPair_ = keyPair; + HcfObjDestroy(generator); + + HcfAsyKeyGenerator *generator1 = nullptr; + res = HcfAsyKeyGeneratorCreate("DH_modp3072", &generator1); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator1, nullptr); + + HcfKeyPair *keyPair1 = nullptr; + res = generator1->generateKeyPair(generator1, nullptr, &keyPair1); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair1, nullptr); + + dh3072KeyPair1_ = keyPair1; + HcfObjDestroy(generator1); + + HcfAsyKeyGenerator *generator2 = nullptr; + res = HcfAsyKeyGeneratorCreate("DH_modp3072", &generator2); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator2, nullptr); + + HcfKeyPair *keyPair2 = nullptr; + res = generator2->generateKeyPair(generator2, nullptr, &keyPair2); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair2, nullptr); + + dh3072KeyPair2_ = keyPair2; + HcfObjDestroy(generator2); +} + +void CryptoDHKeyAgreementTest::TearDownTestCase() +{ + HcfObjDestroy(dh1536KeyPair_); + HcfObjDestroy(dh3072KeyPair1_); + HcfObjDestroy(dh3072KeyPair2_); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_1, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_modp1536", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_2, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_modp2048", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_3, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_modp3072", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_4, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_modp4096", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_5, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_modp6144", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_6, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_modp8192", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_7, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_ffdhe2048", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_8, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_ffdhe3072", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_9, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_ffdhe4096", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_10, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_ffdhe6144", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest001_11, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_ffdhe8192", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest002, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_dh1536AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + const char *className = keyAgreement->base.getClass(); + ASSERT_NE(className, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest003, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_dh1536AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + keyAgreement->base.destroy((HcfObjectBase *)keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest004, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_dh1536AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + const char *algName = keyAgreement->getAlgoName(keyAgreement); + ASSERT_EQ(algName, g_dh1536AlgoName); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = keyAgreement->generateSecret(keyAgreement, dh1536KeyPair_->priKey, dh1536KeyPair_->pubKey, &out); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + free(out.data); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest005, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("DH_9999", &keyAgreement); + + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest006, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_dh1536AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + keyAgreement->base.destroy(nullptr); + keyAgreement->base.destroy(&g_obj); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest007, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_dh1536AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + const char *algName1 = keyAgreement->getAlgoName(nullptr); + ASSERT_EQ(algName1, nullptr); + + const char *algName2 = keyAgreement->getAlgoName((HcfKeyAgreement *)(&g_obj)); + ASSERT_EQ(algName2, nullptr); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest008, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_dh1536AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = keyAgreement->generateSecret(keyAgreement, dh1536KeyPair_->priKey, nullptr, &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(out.data, nullptr); + ASSERT_EQ(out.len, (const unsigned int)0); + + res = keyAgreement->generateSecret(keyAgreement, nullptr, dh1536KeyPair_->pubKey, &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(out.data, nullptr); + ASSERT_EQ(out.len, (const unsigned int)0); + + res = keyAgreement->generateSecret((HcfKeyAgreement *)&g_obj, dh1536KeyPair_->priKey, dh1536KeyPair_->pubKey, &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(out.data, nullptr); + ASSERT_EQ(out.len, (const unsigned int)0); + + free(out.data); + HcfObjDestroy(keyAgreement); +} + +HcfKeyAgreementParams params = { + .algo = HCF_ALG_DH, +}; + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest009, TestSize.Level0) +{ + HcfResult res = HcfKeyAgreementSpiDhCreate(¶ms, nullptr); + + ASSERT_EQ(res, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest010, TestSize.Level0) +{ + HcfKeyAgreementSpi *spiObj = nullptr; + HcfResult res = HcfKeyAgreementSpiDhCreate(¶ms, &spiObj); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(spiObj, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = spiObj->engineGenerateSecret((HcfKeyAgreementSpi *)&g_obj, dh1536KeyPair_->priKey, dh1536KeyPair_->pubKey, + &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + res = spiObj->engineGenerateSecret(spiObj, (HcfPriKey *)&g_obj, dh1536KeyPair_->pubKey, &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + res = spiObj->engineGenerateSecret(spiObj, dh1536KeyPair_->priKey, (HcfPubKey *)&g_obj, &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + HcfObjDestroy(spiObj); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest011, TestSize.Level0) +{ + HcfKeyAgreementSpi *spiObj = nullptr; + HcfResult res = HcfKeyAgreementSpiDhCreate(¶ms, &spiObj); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(spiObj, nullptr); + + spiObj->base.destroy(nullptr); + + HcfObjDestroy(spiObj); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest012, TestSize.Level0) +{ + HcfKeyAgreementSpi *spiObj = nullptr; + HcfResult res = HcfKeyAgreementSpiDhCreate(¶ms, &spiObj); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(spiObj, nullptr); + + spiObj->base.destroy(&g_obj); + + HcfObjDestroy(spiObj); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest013, TestSize.Level0) +{ + StartRecordMallocNum(); + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_dh1536AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = keyAgreement->generateSecret(keyAgreement, dh1536KeyPair_->priKey, dh1536KeyPair_->pubKey, &out); + + ASSERT_EQ(res, HCF_SUCCESS); + + HcfObjDestroy(keyAgreement); + + uint32_t mallocCount = GetMallocNum(); + + for (uint32_t i = 0; i < mallocCount; i++) { + ResetRecordMallocNum(); + SetMockMallocIndex(i); + keyAgreement = nullptr; + res = HcfKeyAgreementCreate(g_dh1536AlgoName.c_str(), &keyAgreement); + + if (res != HCF_SUCCESS) { + continue; + } + + out = { + .data = nullptr, + .len = 0 + }; + res = keyAgreement->generateSecret(keyAgreement, dh1536KeyPair_->priKey, dh1536KeyPair_->pubKey, &out); + + if (res != HCF_SUCCESS) { + HcfObjDestroy(keyAgreement); + continue; + } + + HcfObjDestroy(keyAgreement); + free(out.data); + } + EndRecordMallocNum(); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest014, TestSize.Level0) +{ + StartRecordOpensslCallNum(); + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_dh1536AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = keyAgreement->generateSecret(keyAgreement, dh1536KeyPair_->priKey, dh1536KeyPair_->pubKey, &out); + + ASSERT_EQ(res, HCF_SUCCESS); + + HcfObjDestroy(keyAgreement); + + uint32_t mallocCount = GetOpensslCallNum(); + + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + keyAgreement = nullptr; + res = HcfKeyAgreementCreate(g_dh1536AlgoName.c_str(), &keyAgreement); + + if (res != HCF_SUCCESS) { + continue; + } + + out = { + .data = nullptr, + .len = 0 + }; + res = keyAgreement->generateSecret(keyAgreement, dh1536KeyPair_->priKey, dh1536KeyPair_->pubKey, &out); + + if (res != HCF_SUCCESS) { + HcfObjDestroy(keyAgreement); + continue; + } + + HcfObjDestroy(keyAgreement); + free(out.data); + } + EndRecordOpensslCallNum(); +} + +HWTEST_F(CryptoDHKeyAgreementTest, CryptoDHKeyAgreementTest015, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_dh3072AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = keyAgreement->generateSecret(keyAgreement, dh3072KeyPair2_->priKey, dh3072KeyPair1_->pubKey, &out); + + ASSERT_EQ(res, HCF_SUCCESS); + + HcfObjDestroy(keyAgreement); + free(out.data); +} +} diff --git a/test/unittest/src/crypto_ecc_asy_key_generator_by_spec_test.cpp b/test/unittest/src/crypto_ecc_asy_key_generator_by_spec_test.cpp index 704fef8..516e559 100644 --- a/test/unittest/src/crypto_ecc_asy_key_generator_by_spec_test.cpp +++ b/test/unittest/src/crypto_ecc_asy_key_generator_by_spec_test.cpp @@ -21,6 +21,7 @@ #include "detailed_ecc_key_params.h" #include "ecc_asy_key_generator_openssl.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "ecc_common.h" #include "blob.h" #include "memory_mock.h" diff --git a/test/unittest/src/crypto_ecc_key_agreement_by_spec_test.cpp b/test/unittest/src/crypto_ecc_key_agreement_by_spec_test.cpp index 425426c..6eb5b82 100644 --- a/test/unittest/src/crypto_ecc_key_agreement_by_spec_test.cpp +++ b/test/unittest/src/crypto_ecc_key_agreement_by_spec_test.cpp @@ -19,6 +19,7 @@ #include "asy_key_generator.h" #include "detailed_ecc_key_params.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "ecc_common.h" #include "ecdh_openssl.h" #include "key_agreement.h" diff --git a/test/unittest/src/crypto_ecc_key_util_test.cpp b/test/unittest/src/crypto_ecc_key_util_test.cpp index 9bd5673..0c0f6d7 100644 --- a/test/unittest/src/crypto_ecc_key_util_test.cpp +++ b/test/unittest/src/crypto_ecc_key_util_test.cpp @@ -21,6 +21,7 @@ #include "detailed_ecc_key_params.h" #include "ecc_key_util.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "ecc_common.h" #include "ecdsa_openssl.h" #include "memory.h" @@ -29,7 +30,6 @@ #include "openssl_adapter_mock.h" #include "openssl_common.h" #include "asy_key_params.h" -#include "log.h" #include "params_parser.h" #include "ecc_common_param_spec_generator_openssl.h" @@ -1944,4 +1944,41 @@ HWTEST_F(CryptoEccKeyUtilTest, CryptoEccKeyUtilTest050, TestSize.Level0) HcfObjDestroy(returnCommonParamSpec); } + +HWTEST_F(CryptoEccKeyUtilTest, CryptoEccKeyUtilTest051, TestSize.Level0) +{ + int32_t res = HcfEccKeyUtilCreate("NID_brainpoolP160r1", nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); +} + +static void OpensslMockTestFunc(uint32_t mallocCount, HcfEccCommParamsSpec *returnCommonParamSpec) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + + int32_t res = HcfEccKeyUtilCreate("NID_brainpoolP160r1", &returnCommonParamSpec); + if (res != HCF_SUCCESS) { + continue; + } + + FreeEccCommParamsSpec(returnCommonParamSpec); + } +} + +HWTEST_F(CryptoEccKeyUtilTest, CryptoEccKeyUtilTest052, TestSize.Level0) +{ + StartRecordOpensslCallNum(); + HcfEccCommParamsSpec *returnCommonParamSpec = NULL; + int32_t res = HcfEccKeyUtilCreate("NID_brainpoolP160r1", &returnCommonParamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnCommonParamSpec, nullptr); + + FreeEccCommParamsSpec(returnCommonParamSpec); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc(mallocCount, returnCommonParamSpec); + + EndRecordOpensslCallNum(); +} } \ No newline at end of file diff --git a/test/unittest/src/crypto_ecc_no_length_sign_test.cpp b/test/unittest/src/crypto_ecc_no_length_sign_test.cpp index ee230bd..e133790 100644 --- a/test/unittest/src/crypto_ecc_no_length_sign_test.cpp +++ b/test/unittest/src/crypto_ecc_no_length_sign_test.cpp @@ -20,6 +20,7 @@ #include "blob.h" #include "detailed_ecc_key_params.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "ecc_common.h" #include "ecdsa_openssl.h" #include "memory.h" diff --git a/test/unittest/src/crypto_ecc_no_length_verify_test.cpp b/test/unittest/src/crypto_ecc_no_length_verify_test.cpp index ade0b49..ed93887 100644 --- a/test/unittest/src/crypto_ecc_no_length_verify_test.cpp +++ b/test/unittest/src/crypto_ecc_no_length_verify_test.cpp @@ -20,6 +20,7 @@ #include "blob.h" #include "detailed_ecc_key_params.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "ecc_common.h" #include "ecdsa_openssl.h" #include "memory.h" diff --git a/test/unittest/src/crypto_ed25519_asy_key_generator_by_spec_test.cpp b/test/unittest/src/crypto_ed25519_asy_key_generator_by_spec_test.cpp new file mode 100644 index 0000000..b095821 --- /dev/null +++ b/test/unittest/src/crypto_ed25519_asy_key_generator_by_spec_test.cpp @@ -0,0 +1,936 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "asy_key_generator.h" +#include "blob.h" +#include "ecdsa_openssl.h" +#include "memory.h" +#include "securec.h" +#include "openssl_common.h" +#include "asy_key_params.h" +#include "params_parser.h" +#include "alg_25519_asy_key_generator_openssl.h" +#include "detailed_alg_25519_key_params.h" +#include "memory_mock.h" +#include "openssl_adapter_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoEd25519AsyKeyGeneratorBySpecTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +static string g_ed25519AlgoName = "Ed25519"; +static string g_pubkeyformatName = "X.509"; +static string g_prikeyformatName = "PKCS#8"; +static string g_algorithmName = "Alg25519"; + +HcfAlg25519KeyPairParamsSpec g_ed25519KeyPairSpec; +HcfAlg25519PriKeyParamsSpec g_ed25519PriKeySpec; +HcfAlg25519PubKeyParamsSpec g_ed25519PubKeySpec; + +void CryptoEd25519AsyKeyGeneratorBySpecTest::SetUp() {} +void CryptoEd25519AsyKeyGeneratorBySpecTest::TearDown() {} +void CryptoEd25519AsyKeyGeneratorBySpecTest::SetUpTestCase() {} +void CryptoEd25519AsyKeyGeneratorBySpecTest::TearDownTestCase() {} + +static const char *g_mockMessage = "hello world"; +static HcfBlob g_mockInput = { + .data = (uint8_t *)g_mockMessage, + .len = 12 +}; + +static const char *GetMockClass(void) +{ + return "ed25519generator"; +} +HcfObjectBase g_obj = { + .getClass = GetMockClass, + .destroy = nullptr +}; + +static HcfResult ConstructEd25519KeyPairParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + HcfAlg25519KeyPairParamsSpec *ed25519KeyPairSpec = &g_ed25519KeyPairSpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + ed25519KeyPairSpec->base.algName = g_ed25519AlgoName.data(); + ed25519KeyPairSpec->base.specType = HCF_KEY_PAIR_SPEC; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ED25519_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + ed25519KeyPairSpec->pk.data = retBigInt.data; + ed25519KeyPairSpec->pk.len = retBigInt.len; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ED25519_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + ed25519KeyPairSpec->sk.data = retBigInt.data; + ed25519KeyPairSpec->sk.len = retBigInt.len; + *spec = (HcfAsyKeyParamsSpec *)ed25519KeyPairSpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ConstructEd25519PubKeyParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + return res; + } + HcfAlg25519PubKeyParamsSpec *ed25519PubKeySpec = &g_ed25519PubKeySpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + ed25519PubKeySpec->base.algName = g_ed25519AlgoName.data(); + ed25519PubKeySpec->base.specType = HCF_PUBLIC_KEY_SPEC; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ED25519_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + ed25519PubKeySpec->pk.data = retBigInt.data; + ed25519PubKeySpec->pk.len = retBigInt.len; + *spec = (HcfAsyKeyParamsSpec *)ed25519PubKeySpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ConstructEd25519PriKeyParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + + HcfAlg25519PriKeyParamsSpec *ed25519PriKeySpec = &g_ed25519PriKeySpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + + ed25519PriKeySpec->base.algName = g_ed25519AlgoName.data(); + ed25519PriKeySpec->base.specType = HCF_PRIVATE_KEY_SPEC; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ED25519_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + ed25519PriKeySpec->sk.data = retBigInt.data; + ed25519PriKeySpec->sk.len = retBigInt.len; + + *spec = (HcfAsyKeyParamsSpec *)ed25519PriKeySpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest001_1, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest001_2, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519PubKeyParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest001_3, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519PriKeyParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest002, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + const char *className = returnObj->base.getClass(); + ASSERT_NE(className, NULL); + ASSERT_NE(returnObj, nullptr); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest003, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + returnObj->base.destroy(&g_obj); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest004, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + const char *algoName = returnObj->getAlgName(returnObj); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_EQ(algoName, g_ed25519AlgoName); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest005, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest006, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest007, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(&(keyPair->base)); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest008, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->pubKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest009, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&(keyPair->pubKey->base.base)); + keyPair->pubKey = nullptr; + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest010, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(&(keyPair->pubKey->base)); + ASSERT_EQ(algorithmName, g_algorithmName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->pubKey->base.getFormat(&(keyPair->pubKey->base)); + ASSERT_EQ(formatName, g_pubkeyformatName); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest011, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->priKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest012, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&(keyPair->priKey->base.base)); + keyPair->priKey = nullptr; + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest013, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(&(keyPair->priKey->base)); + ASSERT_EQ(algorithmName, g_algorithmName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->priKey->base.getFormat(&(keyPair->priKey->base)); + ASSERT_EQ(formatName, g_prikeyformatName); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest014, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(keyPair->priKey); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest015, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *pubparamSpec = nullptr; + HcfResult res = ConstructEd25519PubKeyParamsSpec(g_ed25519AlgoName, &pubparamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubparamSpec, nullptr); + + HcfAsyKeyParamsSpec *priparamSpec = nullptr; + res = ConstructEd25519PriKeyParamsSpec(g_ed25519AlgoName, &priparamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priparamSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnpriObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(priparamSpec, &returnpriObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnpriObj, nullptr); + + HcfAsyKeyGeneratorBySpec *returnpubObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(pubparamSpec, &returnpubObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnpubObj, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnpubObj->generatePubKey(returnpubObj, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnpriObj->generatePriKey(returnpriObj, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = pubKey->getAsyKeySpecBigInteger(pubKey, ED25519_PK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + res = priKey->getAsyKeySpecBigInteger(priKey, ED25519_SK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + HcfObjDestroy(returnpubObj); + HcfObjDestroy(returnpriObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest016, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519KeyPairParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfSign *sign = nullptr; + res = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(res, HCF_SUCCESS); + res = sign->init(sign, nullptr, keyPair->priKey); + ASSERT_EQ(res, HCF_SUCCESS); + HcfBlob out = { .data = nullptr, .len = 0 }; + res = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, 0); + + HcfVerify *verify = nullptr; + res = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(res, HCF_SUCCESS); + res = verify->init(verify, nullptr, keyPair->pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + + bool flag = verify->verify(verify, &g_mockInput, &out); + ASSERT_EQ(flag, true); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest017, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_ED25519, + .bits = HCF_ALG_ED25519_256, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiEd25519Create(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + HcfObjDestroy(returnSpi); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest018, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_ED25519, + .bits = HCF_ALG_ED25519_256, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiEd25519Create(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramsSpec = nullptr; + res = ConstructEd25519KeyPairParamsSpec("Ed25519", ¶msSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnSpi->engineGenerateKeyPairBySpec(returnSpi, paramsSpec, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest019, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_ED25519, + .bits = HCF_ALG_ED25519_256, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiEd25519Create(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramsSpec = nullptr; + res = ConstructEd25519PubKeyParamsSpec("Ed25519", ¶msSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnSpi->engineGeneratePubKeyBySpec(returnSpi, paramsSpec, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(pubKey); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest020, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_ED25519, + .bits = HCF_ALG_ED25519_256, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiEd25519Create(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramsSpec = nullptr; + res = ConstructEd25519PriKeyParamsSpec("Ed25519", ¶msSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnSpi->engineGeneratePriKeyBySpec(returnSpi, paramsSpec, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(priKey); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest021, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519PubKeyParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnObj->generatePriKey(returnObj, &priKey); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(priKey, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + HcfObjDestroy(returnObj); + HcfObjDestroy(priKey); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest022, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519PriKeyParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnObj->generatePubKey(returnObj, &pubKey); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(pubKey, nullptr); + + HcfObjDestroy(returnObj); + HcfObjDestroy(keyPair); + HcfObjDestroy(pubKey); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest023, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator; + HcfResult res = HcfAsyKeyGeneratorCreate(g_ed25519AlgoName.c_str(), &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob1 = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob1); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob1.data, nullptr); + ASSERT_NE(blob1.len, 0); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructEd25519PubKeyParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnObj->generatePubKey(returnObj, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfBlob blob2 = { .data = nullptr, .len = 0 }; + res = pubKey->base.getEncoded(&(pubKey->base), &blob2); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob2.data, nullptr); + ASSERT_NE(blob2.len, 0); + + ASSERT_EQ(*(blob1.data), *(blob2.data)); + ASSERT_EQ(blob1.len, blob2.len); + + HcfObjDestroy(returnObj); + HcfObjDestroy(pubKey); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest024, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator; + HcfResult res = HcfAsyKeyGeneratorCreate(g_ed25519AlgoName.c_str(), &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob1 = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob1); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob1.data, nullptr); + ASSERT_NE(blob1.len, 0); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructEd25519PriKeyParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnObj->generatePriKey(returnObj, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfBlob blob2 = { .data = nullptr, .len = 0 }; + res = priKey->base.getEncoded(&(priKey->base), &blob2); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob2.data, nullptr); + ASSERT_NE(blob2.len, 0); + + ASSERT_EQ(*(blob1.data), *(blob2.data)); + ASSERT_EQ(blob1.len, blob2.len); + + HcfObjDestroy(returnObj); + HcfObjDestroy(priKey); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest025, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519PriKeyParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnObj->generatePriKey(returnObj, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = priKey->base.getEncoded(&(priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + HcfAsyKeyGenerator *generator; + res = HcfAsyKeyGeneratorCreate(g_ed25519AlgoName.c_str(), &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, nullptr, &blob, &keyPair); + + HcfObjDestroy(returnObj); + HcfObjDestroy(priKey); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorBySpecTest, CryptoEd25519AsyKeyGeneratorBySpecTest026, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructEd25519PubKeyParamsSpec(g_ed25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnObj->generatePubKey(returnObj, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = pubKey->base.getEncoded(&(pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + HcfAsyKeyGenerator *generator; + res = HcfAsyKeyGeneratorCreate(g_ed25519AlgoName.c_str(), &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &blob, nullptr, &keyPair); + + HcfObjDestroy(returnObj); + HcfObjDestroy(pubKey); + HcfObjDestroy(keyPair); +} +} \ No newline at end of file diff --git a/test/unittest/src/crypto_ed25519_asy_key_generator_test.cpp b/test/unittest/src/crypto_ed25519_asy_key_generator_test.cpp new file mode 100644 index 0000000..38a129c --- /dev/null +++ b/test/unittest/src/crypto_ed25519_asy_key_generator_test.cpp @@ -0,0 +1,1466 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "securec.h" + +#include "asy_key_generator.h" +#include "blob.h" +#include "params_parser.h" +#include "key_pair.h" +#include "object_base.h" +#include "signature.h" +#include "alg_25519_asy_key_generator_openssl.h" +#include "detailed_alg_25519_key_params.h" +#include "memory_mock.h" +#include "openssl_adapter_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoEd25519AsyKeyGeneratorTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void CryptoEd25519AsyKeyGeneratorTest::TearDownTestCase() {} +void CryptoEd25519AsyKeyGeneratorTest::SetUp() {} +void CryptoEd25519AsyKeyGeneratorTest::TearDown() {} +static string g_ed25519AlgoName = "Ed25519"; +static string g_pubkeyformatName = "X.509"; +static string g_prikeyformatName = "PKCS#8"; +static string g_algorithmName = "Alg25519"; + +HcfBlob g_mockEd25519PriKeyBlob = { + .data = nullptr, + .len = 0 +}; + +HcfBlob g_mockEd25519PubKeyBlob = { + .data = nullptr, + .len = 0 +}; + +HcfBlob g_mockECC_BrainPool160r1PriKeyBlob = { + .data = nullptr, + .len = 0 +}; + +HcfBlob g_mockECC_BrainPool160r1PubKeyBlob = { + .data = nullptr, + .len = 0 +}; + +static const char *GetMockClass(void) +{ + return "ed25519generator"; +} +HcfObjectBase g_obj = { + .getClass = GetMockClass, + .destroy = nullptr +}; + +static const char *g_mockMessage = "hello world"; +static HcfBlob g_mockInput = { + .data = (uint8_t *)g_mockMessage, + .len = 12 +}; + +static HcfResult Ed25519KeyBlob(HcfBlob * priblob, HcfBlob *pubblob) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + if (res != HCF_SUCCESS) { + return HCF_INVALID_PARAMS; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &g_mockEd25519PriKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &g_mockEd25519PubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ECC_BrainPool160r1KeyBlob(HcfBlob * priblob, HcfBlob *pubblob) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("ECC_BrainPoolP160r1", &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &g_mockECC_BrainPool160r1PriKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &g_mockECC_BrainPool160r1PubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +void CryptoEd25519AsyKeyGeneratorTest::SetUpTestCase() +{ + HcfResult res = Ed25519KeyBlob(&g_mockEd25519PriKeyBlob, &g_mockEd25519PubKeyBlob); + ASSERT_EQ(res, HCF_SUCCESS); + res = ECC_BrainPool160r1KeyBlob(&g_mockECC_BrainPool160r1PriKeyBlob, &g_mockECC_BrainPool160r1PubKeyBlob); + ASSERT_EQ(res, HCF_SUCCESS); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest001, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest002, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + + const char *className = generator->base.getClass(); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest003, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + generator->base.destroy((HcfObjectBase *)generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest004, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + + const char *algoName = generator->getAlgoName(generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_EQ(algoName, g_ed25519AlgoName); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest005, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest006, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest007, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(&(keyPair->base)); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest008, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->pubKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest009, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&(keyPair->pubKey->base.base)); + keyPair->pubKey = nullptr; + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest010, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(&(keyPair->pubKey->base)); + ASSERT_EQ(algorithmName, g_algorithmName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->pubKey->base.getFormat(&(keyPair->pubKey->base)); + ASSERT_EQ(formatName, g_pubkeyformatName); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ED25519_PK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest011, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->priKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest012, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&(keyPair->priKey->base.base)); + keyPair->priKey = nullptr; + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest013, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(&(keyPair->priKey->base)); + ASSERT_EQ(algorithmName, g_algorithmName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->priKey->base.getFormat(&(keyPair->priKey->base)); + ASSERT_EQ(formatName, g_prikeyformatName); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ED25519_SK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest014, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(keyPair->priKey); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest015, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + res = generator->convertKey(generator, nullptr, nullptr, &g_mockEd25519PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, nullptr, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest016, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest017, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(&(keyPair->base)); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest018, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->pubKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest019, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&(keyPair->pubKey->base.base)); + keyPair->pubKey = nullptr; + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest020, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(&(keyPair->pubKey->base)); + ASSERT_EQ(algorithmName, g_algorithmName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->pubKey->base.getFormat(&(keyPair->pubKey->base)); + ASSERT_EQ(formatName, g_pubkeyformatName); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ED25519_PK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest021, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->priKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest022, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&(keyPair->priKey->base.base)); + keyPair->priKey = nullptr; + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest023, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(&(keyPair->priKey->base)); + ASSERT_EQ(algorithmName, g_algorithmName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->priKey->base.getFormat(&(keyPair->priKey->base)); + ASSERT_EQ(formatName, g_prikeyformatName); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ED25519_SK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest024, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(keyPair->priKey); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +static void MemoryMallocTestFunc(uint32_t mallocCount) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetRecordMallocNum(); + SetMockMallocIndex(i); + HcfAsyKeyGenerator *tmpGenerator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &tmpGenerator); + if (res != HCF_SUCCESS) { + continue; + } + HcfKeyPair *tmpKeyPair = nullptr; + res = tmpGenerator->generateKeyPair(tmpGenerator, nullptr, &tmpKeyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPubKeyBlob = { + .data = nullptr, + .len = 0 + }; + res = tmpKeyPair->pubKey->base.getEncoded(&(tmpKeyPair->pubKey->base), &tmpPubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPriKeyBlob = { + .data = nullptr, + .len = 0 + }; + res = tmpKeyPair->priKey->base.getEncoded(&(tmpKeyPair->priKey->base), &tmpPriKeyBlob); + if (res != HCF_SUCCESS) { + free(tmpPubKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfKeyPair *tmpOutKeyPair = nullptr; + res = tmpGenerator->convertKey(tmpGenerator, nullptr, &tmpPubKeyBlob, &tmpPriKeyBlob, &tmpOutKeyPair); + free(tmpPubKeyBlob.data); + free(tmpPriKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + if (res == HCF_SUCCESS) { + HcfObjDestroy(tmpOutKeyPair); + } + } +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest025, TestSize.Level0) +{ + StartRecordMallocNum(); + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob pubKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &pubKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKeyBlob.data, nullptr); + ASSERT_NE(pubKeyBlob.len, 0); + + HcfBlob priKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &priKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKeyBlob.data, nullptr); + ASSERT_NE(priKeyBlob.len, 0); + + HcfKeyPair *outKeyPair = nullptr; + res = generator->convertKey(generator, nullptr, &pubKeyBlob, &priKeyBlob, &outKeyPair); + + free(pubKeyBlob.data); + free(priKeyBlob.data); + HcfObjDestroy(outKeyPair); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + + uint32_t mallocCount = GetMallocNum(); + MemoryMallocTestFunc(mallocCount); + + EndRecordMallocNum(); +} + +static void OpensslMockTestFunc(uint32_t mallocCount) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + HcfAsyKeyGenerator *tmpGenerator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &tmpGenerator); + if (res != HCF_SUCCESS) { + continue; + } + HcfKeyPair *tmpKeyPair = nullptr; + res = tmpGenerator->generateKeyPair(tmpGenerator, nullptr, &tmpKeyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPubKeyBlob = { + .data = nullptr, + .len = 0 + }; + res = tmpKeyPair->pubKey->base.getEncoded(&(tmpKeyPair->pubKey->base), &tmpPubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPriKeyBlob = { + .data = nullptr, + .len = 0 + }; + res = tmpKeyPair->priKey->base.getEncoded(&(tmpKeyPair->priKey->base), &tmpPriKeyBlob); + if (res != HCF_SUCCESS) { + free(tmpPubKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfKeyPair *tmpOutKeyPair = nullptr; + res = tmpGenerator->convertKey(tmpGenerator, nullptr, &tmpPubKeyBlob, &tmpPriKeyBlob, &tmpOutKeyPair); + free(tmpPubKeyBlob.data); + free(tmpPriKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + if (res == HCF_SUCCESS) { + HcfObjDestroy(tmpOutKeyPair); + } + } +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest026, TestSize.Level0) +{ + StartRecordOpensslCallNum(); + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob pubKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &pubKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKeyBlob.data, nullptr); + ASSERT_NE(pubKeyBlob.len, 0); + + HcfBlob priKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &priKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKeyBlob.data, nullptr); + ASSERT_NE(priKeyBlob.len, 0); + + HcfKeyPair *outKeyPair = nullptr; + res = generator->convertKey(generator, nullptr, &pubKeyBlob, &priKeyBlob, &outKeyPair); + + free(pubKeyBlob.data); + free(priKeyBlob.data); + HcfObjDestroy(outKeyPair); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc(mallocCount); + + EndRecordOpensslCallNum(); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest0027, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + + HcfResult res = HcfAsyKeyGeneratorCreate("ED25519", &generator); + + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(generator, NULL); + + res = HcfAsyKeyGeneratorCreate(nullptr, &generator); + + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(generator, NULL); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest028, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + const char *algoName = nullptr; + algoName = generator->getAlgoName(nullptr); + ASSERT_EQ(algoName, NULL); + + algoName = nullptr; + algoName = generator->getAlgoName((HcfAsyKeyGenerator *)&g_obj); + ASSERT_EQ(algoName, NULL); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest029, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + generator->base.destroy(nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest030, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + generator->base.destroy(&g_obj); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest031, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(nullptr, nullptr, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + res = generator->generateKeyPair((HcfAsyKeyGenerator *)&g_obj, nullptr, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + res = generator->generateKeyPair(generator, nullptr, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest032, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(nullptr); + ASSERT_EQ(algorithmName, NULL); + + algorithmName = keyPair->pubKey->base.getAlgorithm((HcfKey *)&g_obj); + ASSERT_EQ(algorithmName, NULL); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest033, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(nullptr, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->pubKey->base.getEncoded((HcfKey *)&g_obj, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest034, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *formatName = nullptr; + formatName = keyPair->pubKey->base.getFormat(nullptr); + ASSERT_EQ(formatName, nullptr); + + formatName = keyPair->pubKey->base.getFormat((HcfKey *)&g_obj); + ASSERT_EQ(formatName, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest035, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, ED25519_SK_BN, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnBigInteger.data, nullptr); + ASSERT_EQ(returnBigInteger.len, 0); + + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DSA_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnBigInteger.data, nullptr); + ASSERT_EQ(returnBigInteger.len, 0); + + res = keyPair->pubKey->getAsyKeySpecInt(keyPair->pubKey, ED25519_PK_BN, nullptr); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + + res = keyPair->pubKey->getAsyKeySpecString(keyPair->pubKey, ED25519_PK_BN, nullptr); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest036, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(nullptr); + ASSERT_EQ(algorithmName, NULL); + + algorithmName = keyPair->priKey->base.getAlgorithm((HcfKey *)&g_obj); + ASSERT_EQ(algorithmName, NULL); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest037, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(nullptr, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->priKey->base.getEncoded((HcfKey *)&g_obj, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest038, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *formatName = nullptr; + formatName = keyPair->priKey->base.getFormat(nullptr); + ASSERT_EQ(formatName, nullptr); + + formatName = keyPair->priKey->base.getFormat((HcfKey *)&g_obj); + ASSERT_EQ(formatName, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest039, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ED25519_PK_BN, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnBigInteger.data, nullptr); + ASSERT_EQ(returnBigInteger.len, 0); + + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, DSA_P_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(returnBigInteger.data, nullptr); + ASSERT_EQ(returnBigInteger.len, 0); + + res = keyPair->priKey->getAsyKeySpecInt(keyPair->priKey, ED25519_SK_BN, nullptr); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + + res = keyPair->priKey->getAsyKeySpecString(keyPair->priKey, ED25519_SK_BN, nullptr); + ASSERT_EQ(res, HCF_NOT_SUPPORT); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest040, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(nullptr, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, NULL); + + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + res = generator->convertKey((HcfAsyKeyGenerator *)&g_obj, nullptr, &g_mockEd25519PubKeyBlob, + &g_mockEd25519PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, NULL); + + res = generator->convertKey(generator, nullptr, nullptr, nullptr, &keyPair); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyPair, NULL); + + res = generator->convertKey(generator, nullptr, &g_mockECC_BrainPool160r1PubKeyBlob, + &g_mockECC_BrainPool160r1PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_ERR_CRYPTO_OPERATION); + ASSERT_EQ(keyPair, NULL); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest041, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(nullptr); + ASSERT_EQ(algorithmName, NULL); + + algorithmName = keyPair->pubKey->base.getAlgorithm((HcfKey *)&g_obj); + ASSERT_EQ(algorithmName, NULL); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest042, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(nullptr, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->pubKey->base.getEncoded((HcfKey *)&g_obj, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest043, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *formatName = nullptr; + formatName = keyPair->pubKey->base.getFormat(nullptr); + ASSERT_EQ(formatName, nullptr); + + formatName = keyPair->pubKey->base.getFormat((HcfKey *)&g_obj); + ASSERT_EQ(formatName, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest044, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(nullptr); + ASSERT_EQ(algorithmName, NULL); + + algorithmName = keyPair->priKey->base.getAlgorithm((HcfKey *)&g_obj); + ASSERT_EQ(algorithmName, NULL); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest045, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(nullptr, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + res = keyPair->priKey->base.getEncoded((HcfKey *)&g_obj, &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest046, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *formatName = nullptr; + formatName = keyPair->priKey->base.getFormat(nullptr); + ASSERT_EQ(formatName, nullptr); + + formatName = keyPair->priKey->base.getFormat((HcfKey *)&g_obj); + ASSERT_EQ(formatName, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest047, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(nullptr); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest048, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockEd25519PubKeyBlob, &g_mockEd25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem((HcfPriKey *)&g_obj); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest049, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest050, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&g_obj); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest051, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest052, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&g_obj); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoEd25519AsyKeyGeneratorTest, CryptoEd25519AsyKeyGeneratorTest053, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(g_ed25519AlgoName.c_str(), &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfSign *sign = nullptr; + res = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(res, HCF_SUCCESS); + res = sign->init(sign, nullptr, keyPair->priKey); + ASSERT_EQ(res, HCF_SUCCESS); + HcfBlob out = { .data = nullptr, .len = 0 }; + res = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, 0); + + HcfVerify *verify = nullptr; + res = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(res, HCF_SUCCESS); + res = verify->init(verify, nullptr, keyPair->pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + + bool flag = verify->verify(verify, &g_mockInput, &out); + ASSERT_EQ(flag, true); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} +} \ No newline at end of file diff --git a/test/unittest/src/crypto_ed25519_sign_test.cpp b/test/unittest/src/crypto_ed25519_sign_test.cpp new file mode 100644 index 0000000..ba40abd --- /dev/null +++ b/test/unittest/src/crypto_ed25519_sign_test.cpp @@ -0,0 +1,626 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "asy_key_generator.h" +#include "blob.h" +#include "ecdsa_openssl.h" +#include "memory.h" +#include "securec.h" +#include "openssl_common.h" +#include "asy_key_params.h" +#include "params_parser.h" +#include "ed25519_openssl.h" +#include "detailed_alg_25519_key_params.h" +#include "alg_25519_asy_key_generator_openssl.h" +#include "memory_mock.h" +#include "openssl_adapter_mock.h" + + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoEd25519SignTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + static HcfKeyPair *ed25519KeyPair_; + static HcfKeyPair *x25519KeyPair_; +}; + +HcfKeyPair *CryptoEd25519SignTest::ed25519KeyPair_ = nullptr; +HcfKeyPair *CryptoEd25519SignTest::x25519KeyPair_ = nullptr; + +static string g_ed25519AlgoName = "Ed25519"; +static string g_x25519AlgoName = "X25519"; + +void CryptoEd25519SignTest::SetUp() {} +void CryptoEd25519SignTest::TearDown() {} + +static const char *g_mockMessage = "hello world"; +static HcfBlob g_mockInput = { + .data = (uint8_t *)g_mockMessage, + .len = 12 +}; + +static const char *g_mock64Message = "ABCDABCDACBDABCDABCDABCDACBDABCDABCDABCDACBDABCDABCDABCDACBDABCD"; +static HcfBlob g_mock64Input = { + .data = (uint8_t *)g_mock64Message, + .len = 65 +}; + +static const char *GetMockClass(void) +{ + return "Ed25519Sign"; +} +HcfObjectBase g_obj = { + .getClass = GetMockClass, + .destroy = nullptr +}; + +void CryptoEd25519SignTest::SetUpTestCase() +{ + HcfAsyKeyGenerator *generator = nullptr; + int32_t ret = HcfAsyKeyGeneratorCreate(g_ed25519AlgoName.c_str(), &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + ret = generator->generateKeyPair(generator, nullptr, &keyPair); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + ed25519KeyPair_ = keyPair; + + ret = HcfAsyKeyGeneratorCreate(g_x25519AlgoName.c_str(), &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + ret = generator->generateKeyPair(generator, nullptr, &keyPair); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + x25519KeyPair_ = keyPair; + + HcfObjDestroy(generator); +} + +void CryptoEd25519SignTest::TearDownTestCase() +{ + HcfObjDestroy(ed25519KeyPair_); + HcfObjDestroy(x25519KeyPair_); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest001, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest002, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + const char *className = sign->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest003, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + sign->base.destroy((HcfObjectBase *)sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest004, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + const char *algName = sign->getAlgoName(sign); + ASSERT_EQ(algName, g_ed25519AlgoName); + + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest005, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest006, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + free(out.data); + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest007, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + HcfVerify *verify = nullptr; + ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + bool flag = verify->verify(verify, &g_mockInput, &out); + ASSERT_EQ(flag, true); + + free(out.data); + HcfObjDestroy(sign); + HcfObjDestroy(verify); +} + +static void MemoryMallocTestFunc(uint32_t mallocCount, HcfBlob *input) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetRecordMallocNum(); + SetMockMallocIndex(i); + HcfAsyKeyGenerator *generator = nullptr; + HcfResult ret = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + if (ret != HCF_SUCCESS) { + continue; + } + HcfKeyPair *keyPair = nullptr; + ret = generator->generateKeyPair(generator, nullptr, &keyPair); + HcfObjDestroy(generator); + if (ret != HCF_SUCCESS) { + continue; + } + HcfSign *sign = nullptr; + ret = HcfSignCreate("Ed25519", &sign); + if (ret != HCF_SUCCESS) { + HcfObjDestroy(keyPair); + continue; + } + ret = sign->init(sign, nullptr, keyPair->priKey); + if (ret != HCF_SUCCESS) { + HcfObjDestroy(sign); + HcfObjDestroy(keyPair); + continue; + } + ret = sign->update(sign, input); + if (ret != HCF_SUCCESS) { + HcfObjDestroy(sign); + HcfObjDestroy(keyPair); + continue; + } + HcfBlob out = { + .data = nullptr, + .len = 0 + }; + ret = sign->sign(sign, nullptr, &out); + HcfObjDestroy(sign); + HcfObjDestroy(keyPair); + if (ret == HCF_SUCCESS) { + free(out.data); + } + } +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest008, TestSize.Level0) +{ + StartRecordMallocNum(); + HcfAsyKeyGenerator *generator = nullptr; + HcfResult ret = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + ret = generator->generateKeyPair(generator, nullptr, &keyPair); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(generator); + + HcfSign *sign = nullptr; + ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + HcfParamsSpec params; + ret = sign->init(sign, ¶ms, keyPair->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + const char *message = "hello world"; + HcfBlob input = { + .data = (uint8_t *)message, + .len = 12 + }; + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + free(out.data); + HcfObjDestroy(sign); + HcfObjDestroy(keyPair); + + uint32_t mallocCount = GetMallocNum(); + MemoryMallocTestFunc(mallocCount, &input); + + EndRecordMallocNum(); +} + +static void OpensslMockTestFunc(uint32_t mallocCount, HcfBlob *input) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + HcfAsyKeyGenerator *generator = nullptr; + HcfResult ret = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + if (ret != HCF_SUCCESS) { + continue; + } + HcfKeyPair *keyPair = nullptr; + ret = generator->generateKeyPair(generator, nullptr, &keyPair); + HcfObjDestroy(generator); + if (ret != HCF_SUCCESS) { + continue; + } + HcfSign *sign = nullptr; + ret = HcfSignCreate("Ed25519", &sign); + if (ret != HCF_SUCCESS) { + HcfObjDestroy(keyPair); + continue; + } + ret = sign->init(sign, nullptr, keyPair->priKey); + if (ret != HCF_SUCCESS) { + HcfObjDestroy(sign); + HcfObjDestroy(keyPair); + continue; + } + HcfBlob out = { + .data = nullptr, + .len = 0 + }; + ret = sign->sign(sign, input, &out); + HcfObjDestroy(sign); + HcfObjDestroy(keyPair); + if (ret == HCF_SUCCESS) { + free(out.data); + } + } +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest009, TestSize.Level0) +{ + StartRecordOpensslCallNum(); + HcfAsyKeyGenerator *generator = nullptr; + HcfResult ret = HcfAsyKeyGeneratorCreate("Ed25519", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + ret = generator->generateKeyPair(generator, nullptr, &keyPair); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(generator); + + HcfSign *sign = nullptr; + ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + HcfParamsSpec params; + ret = sign->init(sign, ¶ms, keyPair->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + const char *message = "hello world"; + HcfBlob input = { + .data = (uint8_t *)message, + .len = 12 + }; + ret = sign->update(sign, &input); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign(sign, &input, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + free(out.data); + HcfObjDestroy(sign); + HcfObjDestroy(keyPair); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc(mallocCount, &input); + + EndRecordOpensslCallNum(); +} + +HcfSignatureParams g_params = { + .algo = HCF_ALG_ED25519, +}; + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest010, TestSize.Level0) +{ + HcfSignSpi *sign = nullptr; + HcfResult ret = HcfSignSpiEd25519Create(nullptr, &sign); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + ASSERT_EQ(sign, nullptr); + ret = HcfSignSpiEd25519Create(&g_params, &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest011, TestSize.Level0) +{ + HcfSignSpi *sign = nullptr; + HcfResult ret = HcfSignSpiEd25519Create(&g_params, &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + sign->base.destroy(nullptr); + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest012, TestSize.Level0) +{ + HcfSignSpi *sign = nullptr; + HcfResult ret = HcfSignSpiEd25519Create(&g_params, &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->engineInit(sign, nullptr, nullptr); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest013, TestSize.Level0) +{ + HcfSignSpi *sign = nullptr; + HcfResult ret = HcfSignSpiEd25519Create(&g_params, &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->engineInit(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->engineSign(sign, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest014, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + int32_t returnInt = 1; + ret = sign->setSignSpecInt(sign, SM2_USER_ID_UINT8ARR, returnInt); + ASSERT_EQ(ret, HCF_NOT_SUPPORT); + ret = sign->getSignSpecInt(sign, SM2_USER_ID_UINT8ARR, &returnInt); + ASSERT_EQ(ret, HCF_NOT_SUPPORT); + HcfBlob returnBlob = { .data = nullptr, .len = 0}; + ret = sign->setSignSpecUint8Array(sign, SM2_USER_ID_UINT8ARR, returnBlob); + ASSERT_EQ(ret, HCF_NOT_SUPPORT); + char *itemName = nullptr; + ret = sign->getSignSpecString(sign, SM2_USER_ID_UINT8ARR, &itemName); + ASSERT_EQ(ret, HCF_NOT_SUPPORT); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest015, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + sign->base.destroy(nullptr); + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest016, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + sign->base.destroy(&g_obj); + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest017, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + const char *algoName = sign->getAlgoName(nullptr); + ASSERT_EQ(algoName, NULL); + + algoName = sign->getAlgoName((HcfSign *)&g_obj); + ASSERT_EQ(algoName, NULL); + + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest018, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(nullptr, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest019, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init((HcfSign *)&g_obj, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest020, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, x25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION); + + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest021, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_NE(ret, HCF_SUCCESS); + + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest022, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->sign(sign, &g_mockInput, nullptr); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign((HcfSign*)&g_obj, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + + ret = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest023, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ret = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + HcfObjDestroy(sign); +} + +HWTEST_F(CryptoEd25519SignTest, CryptoEd25519SignTest024, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign(sign, &g_mock64Input, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + HcfObjDestroy(sign); +} +} \ No newline at end of file diff --git a/test/unittest/src/crypto_ed25519_verify_test.cpp b/test/unittest/src/crypto_ed25519_verify_test.cpp new file mode 100644 index 0000000..6a18476 --- /dev/null +++ b/test/unittest/src/crypto_ed25519_verify_test.cpp @@ -0,0 +1,590 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "asy_key_generator.h" +#include "blob.h" +#include "ecdsa_openssl.h" +#include "memory.h" +#include "securec.h" +#include "openssl_common.h" +#include "asy_key_params.h" +#include "params_parser.h" +#include "detailed_alg_25519_key_params.h" +#include "alg_25519_asy_key_generator_openssl.h" +#include "ed25519_openssl.h" +#include "memory_mock.h" +#include "openssl_adapter_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoEd25519VerifyTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + static HcfKeyPair *ed25519KeyPair_; + static HcfKeyPair *x25519KeyPair_; +}; + +HcfKeyPair *CryptoEd25519VerifyTest::ed25519KeyPair_ = nullptr; +HcfKeyPair *CryptoEd25519VerifyTest::x25519KeyPair_ = nullptr; + +static string g_ed25519AlgoName = "Ed25519"; +static string g_x25519AlgoName = "X25519"; + +void CryptoEd25519VerifyTest::SetUp() {} +void CryptoEd25519VerifyTest::TearDown() {} + +static const char *g_mockMessage = "hello world"; +static HcfBlob g_mockInput = { + .data = (uint8_t *)g_mockMessage, + .len = 12 +}; + +static const char *GetMockClass(void) +{ + return "Ed25519Verifytest"; +} +HcfObjectBase g_obj = { + .getClass = GetMockClass, + .destroy = nullptr +}; + +static const char *g_mock64Message = "ABCDABCDACBDABCDABCDABCDACBDABCDABCDABCDACBDABCDABCDABCDACBDABCD"; +static HcfBlob g_mock64Input = { + .data = (uint8_t *)g_mock64Message, + .len = 65 +}; + +void CryptoEd25519VerifyTest::SetUpTestCase() +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult ret = HcfAsyKeyGeneratorCreate(g_ed25519AlgoName.c_str(), &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + ret = generator->generateKeyPair(generator, nullptr, &keyPair); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + ed25519KeyPair_ = keyPair; + + ret = HcfAsyKeyGeneratorCreate(g_x25519AlgoName.c_str(), &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + ret = generator->generateKeyPair(generator, nullptr, &keyPair); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + x25519KeyPair_ = keyPair; + + HcfObjDestroy(generator); +} + +void CryptoEd25519VerifyTest::TearDownTestCase() +{ + HcfObjDestroy(ed25519KeyPair_); + HcfObjDestroy(x25519KeyPair_); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest001, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest002, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + const char *className = verify->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest003, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + verify->base.destroy((HcfObjectBase *)verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest004, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + const char *algName = verify->getAlgoName(verify); + ASSERT_EQ(algName, g_ed25519AlgoName); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest005, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest006, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + HcfVerify *verify = nullptr; + ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + bool flag = verify->verify(verify, &g_mockInput, &out); + ASSERT_EQ(flag, true); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + free(out.data); + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest007, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + HcfVerify *verify = nullptr; + ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + bool flag = verify->verify(verify, &g_mockInput, &out); + ASSERT_EQ(flag, true); + + free(out.data); + HcfObjDestroy(sign); + HcfObjDestroy(verify); +} + +static bool GetSignTestData(HcfBlob *out) +{ + HcfSign *sign = nullptr; + int32_t res = HcfSignCreate("Ed25519", &sign); + if (res != HCF_SUCCESS) { + return false; + } + res = sign->init(sign, nullptr, CryptoEd25519VerifyTest::ed25519KeyPair_->priKey); + if (res != HCF_SUCCESS) { + HcfObjDestroy(sign); + return false; + } + res = sign->sign(sign, &g_mockInput, out); + HcfObjDestroy(sign); + return res == HCF_SUCCESS; +} + +static void OpensslMockTestFunc(uint32_t mallocCount, HcfBlob *out) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + HcfVerify *verify = nullptr; + int32_t res = HcfVerifyCreate("Ed25519", &verify); + if (res != HCF_SUCCESS) { + continue; + } + res = verify->init(verify, nullptr, CryptoEd25519VerifyTest::ed25519KeyPair_->pubKey); + if (res != HCF_SUCCESS) { + HcfObjDestroy(verify); + continue; + } + res = verify->verify(verify, &g_mockInput, out); + if (res != HCF_SUCCESS) { + HcfObjDestroy(verify); + continue; + } + HcfObjDestroy(verify); + } +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest008, TestSize.Level0) +{ + HcfBlob out = { .data = nullptr, .len = 0 }; + ASSERT_EQ(GetSignTestData(&out), true); + StartRecordOpensslCallNum(); + + HcfVerify *verify = nullptr; + int32_t res = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + res = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + bool flag = verify->verify(verify, &g_mockInput, &out); + ASSERT_EQ(flag, true); + HcfObjDestroy(verify); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc(mallocCount, &out); + EndRecordOpensslCallNum(); +} + +HcfSignatureParams g_params = { + .algo = HCF_ALG_ED25519, +}; + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest009, TestSize.Level0) +{ + HcfVerifySpi *verify = nullptr; + HcfResult ret = HcfVerifySpiEd25519Create(nullptr, &verify); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + ASSERT_EQ(verify, nullptr); + ret = HcfVerifySpiEd25519Create(&g_params, &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest010, TestSize.Level0) +{ + HcfVerifySpi *verify = nullptr; + HcfResult ret = HcfVerifySpiEd25519Create(&g_params, &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + verify->base.destroy(nullptr); + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest011, TestSize.Level0) +{ + HcfVerifySpi *verify = nullptr; + HcfResult ret = HcfVerifySpiEd25519Create(&g_params, &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->engineInit(verify, nullptr, nullptr); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest012, TestSize.Level0) +{ + HcfVerifySpi *verify = nullptr; + int32_t ret = HcfVerifySpiEd25519Create(&g_params, &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->engineVerify(verify, &g_mockInput, &g_mockInput); + ASSERT_EQ(ret, false); + ret = verify->engineVerify(verify, nullptr, &g_mockInput); + ASSERT_EQ(ret, false); + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest013, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + int32_t returnInt = 1; + ret = verify->setVerifySpecInt(verify, SM2_USER_ID_UINT8ARR, returnInt); + ASSERT_EQ(ret, HCF_NOT_SUPPORT); + ret = verify->getVerifySpecInt(verify, SM2_USER_ID_UINT8ARR, &returnInt); + ASSERT_EQ(ret, HCF_NOT_SUPPORT); + HcfBlob returnBlob = { .data = nullptr, .len = 0}; + ret = verify->setVerifySpecUint8Array(verify, SM2_USER_ID_UINT8ARR, returnBlob); + ASSERT_EQ(ret, HCF_NOT_SUPPORT); + char *itemName = nullptr; + ret = verify->getVerifySpecString(verify, SM2_USER_ID_UINT8ARR, &itemName); + ASSERT_EQ(ret, HCF_NOT_SUPPORT); + ret = verify->update(verify, &g_mockInput); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest014, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + verify->base.destroy(nullptr); + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest015, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + verify->base.destroy(&g_obj); + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest016, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + const char *algoName = verify->getAlgoName(nullptr); + ASSERT_EQ(algoName, NULL); + + algoName = verify->getAlgoName((HcfVerify *)&g_obj); + ASSERT_EQ(algoName, NULL); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest017, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(nullptr, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest018, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init((HcfVerify *)&g_obj, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_INVALID_PARAMS); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest019, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, x25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest020, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_NE(ret, HCF_SUCCESS); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest021, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + bool flag = verify->verify(verify, &g_mockInput, &out); + ASSERT_EQ(flag, false); + + free(out.data); + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest022, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_SUCCESS); + HcfBlob out = { .data = nullptr, .len = 0 }; + bool flag = verify->verify(verify, nullptr, &out); + ASSERT_EQ(flag, false); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest023, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_SUCCESS); + bool flag = verify->verify(verify, &g_mockInput, nullptr); + ASSERT_EQ(flag, false); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest024, TestSize.Level0) +{ + HcfVerify *verify = nullptr; + HcfResult ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_SUCCESS); + bool flag = verify->verify(verify, &g_mockInput, &g_mockInput); + ASSERT_EQ(flag, false); + + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest025, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign(sign, &g_mockInput, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + HcfVerify *verify = nullptr; + ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + bool flag = verify->verify(verify, &g_mockInput, &out); + flag = verify->verify(verify, &g_mockInput, &out); + ASSERT_EQ(flag, true); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + free(out.data); + HcfObjDestroy(verify); +} + +HWTEST_F(CryptoEd25519VerifyTest, CryptoEd25519VerifyTest026, TestSize.Level0) +{ + HcfSign *sign = nullptr; + HcfResult ret = HcfSignCreate("Ed25519", &sign); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(sign, nullptr); + + ret = sign->init(sign, nullptr, ed25519KeyPair_->priKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + HcfBlob out = { .data = nullptr, .len = 0 }; + ret = sign->sign(sign, &g_mock64Input, &out); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + HcfVerify *verify = nullptr; + ret = HcfVerifyCreate("Ed25519", &verify); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(verify, nullptr); + + ret = verify->init(verify, nullptr, ed25519KeyPair_->pubKey); + ASSERT_EQ(ret, HCF_SUCCESS); + + bool flag = verify->verify(verify, &g_mock64Input, &out); + ASSERT_EQ(flag, true); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + free(out.data); + HcfObjDestroy(verify); +} +} \ No newline at end of file diff --git a/test/unittest/src/crypto_sm2_asy_key_generator_by_spec_test.cpp b/test/unittest/src/crypto_sm2_asy_key_generator_by_spec_test.cpp index 1f02f77..931fa6f 100644 --- a/test/unittest/src/crypto_sm2_asy_key_generator_by_spec_test.cpp +++ b/test/unittest/src/crypto_sm2_asy_key_generator_by_spec_test.cpp @@ -21,6 +21,7 @@ #include "sm2_asy_key_generator_openssl.h" #include "detailed_ecc_key_params.h" #include "ecc_openssl_common.h" +#include "ecc_openssl_common_param_spec.h" #include "ecc_common.h" #include "ecc_key_util.h" #include "key_utils.h" @@ -2864,10 +2865,10 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecTest, CryptoSm2AsyKeyGeneratorBySpecTest0 ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(pubKey, nullptr); - AsyKeySpecItem item = ECC_CURVE_NAME_STR; - - res = pubKey->getAsyKeySpecString(pubKey, item, nullptr); - + res = pubKey->getAsyKeySpecString(pubKey, ECC_CURVE_NAME_STR, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + char *retStr = nullptr; + res = pubKey->getAsyKeySpecString(pubKey, ECC_FIELD_SIZE_INT, &retStr); ASSERT_EQ(res, HCF_INVALID_PARAMS); HcfObjDestroy(pubKey); @@ -2894,9 +2895,13 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecTest, CryptoSm2AsyKeyGeneratorBySpecTest0 AsyKeySpecItem item = ECC_FIELD_TYPE_STR; res = keyPair->priKey->getAsyKeySpecString(keyPair->priKey, item, &retStr); - ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(retStr, nullptr); + retStr = nullptr; + res = keyPair->pubKey->getAsyKeySpecString(keyPair->pubKey, item, &retStr); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(retStr, nullptr); + free(retStr); @@ -2948,8 +2953,11 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecTest, CryptoSm2AsyKeyGeneratorBySpecTest1 int retInt = 0; AsyKeySpecItem item = ECC_FIELD_SIZE_INT; - res = keyPair->pubKey->getAsyKeySpecInt(keyPair->pubKey, item, &retInt); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(retInt, 0); + retInt = 0; + res = keyPair->priKey->getAsyKeySpecInt(keyPair->priKey, item, &retInt); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(retInt, 0); @@ -2974,12 +2982,16 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecTest, CryptoSm2AsyKeyGeneratorBySpecTest1 ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(keyPair, nullptr); - AsyKeySpecItem item = ECC_FIELD_SIZE_INT; - - res = keyPair->priKey->getAsyKeySpecInt(keyPair->priKey, item, nullptr); - + res = keyPair->priKey->getAsyKeySpecInt(keyPair->priKey, ECC_FIELD_SIZE_INT, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + int retInt = 0; + res = keyPair->priKey->getAsyKeySpecInt(keyPair->priKey, ECC_FIELD_TYPE_STR, &retInt); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, ECC_SK_BN, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, DSA_SK_BN, &retBigInt); ASSERT_EQ(res, HCF_INVALID_PARAMS); - HcfObjDestroy(keyPair); HcfObjDestroy(generator); } @@ -3094,4 +3106,242 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecTest, CryptoSm2AsyKeyGeneratorBySpecTest1 HcfObjDestroy(generator); HcfObjDestroy(generatorSpec); } + +static void OpensslMockTestFunc(uint32_t mallocCount, HcfAsyKeyParamsSpec *paramSpec) +{ + for (int i = 0; i < mallocCount - THREE; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + + HcfAsyKeyGeneratorBySpec *generatorBySpec = nullptr; + int32_t res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generatorBySpec); + if (res != HCF_SUCCESS) { + continue; + } + HcfKeyPair *keyPair = nullptr; + res = generatorBySpec->generateKeyPair(generatorBySpec, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generatorBySpec); + continue; + } + HcfBlob pubKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &pubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generatorBySpec); + continue; + } + HcfBlob priKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &priKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generatorBySpec); + continue; + } + free(pubKeyBlob.data); + free(priKeyBlob.data); + HcfObjDestroy(keyPair); + HcfObjDestroy(generatorBySpec); + } +} + +HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecTest, CryptoSm2AsyKeyGeneratorBySpecTest103, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + int32_t res = ConstructSm2256KeyPairParamsSpec(g_sm2AlgName, ¶mSpec); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + StartRecordOpensslCallNum(); + HcfAsyKeyGeneratorBySpec *generatorBySpec = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generatorBySpec); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generatorBySpec, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = generatorBySpec->generateKeyPair(generatorBySpec, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob pubKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &pubKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKeyBlob.data, nullptr); + ASSERT_NE(pubKeyBlob.len, 0); + + HcfBlob priKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &priKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKeyBlob.data, nullptr); + ASSERT_NE(priKeyBlob.len, 0); + + free(pubKeyBlob.data); + free(priKeyBlob.data); + HcfObjDestroy(keyPair); + HcfObjDestroy(generatorBySpec); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc(mallocCount, paramSpec); + + EndRecordOpensslCallNum(); +} + +static void OpensslMockTestFunc1(uint32_t mallocCount, HcfAsyKeyParamsSpec *paramSpec) +{ + for (int i = 0; i < mallocCount - 1; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + + HcfAsyKeyGeneratorBySpec *generatorBySpec = nullptr; + int32_t res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generatorBySpec); + if (res != HCF_SUCCESS) { + continue; + } + HcfKeyPair *keyPair = nullptr; + res = generatorBySpec->generateKeyPair(generatorBySpec, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generatorBySpec); + continue; + } + HcfObjDestroy(keyPair); + HcfObjDestroy(generatorBySpec); + } +} + +HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecTest, CryptoSm2AsyKeyGeneratorBySpecTest104, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + int32_t res = ConstructSm2256CommParamsSpec(¶mSpec); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + StartRecordOpensslCallNum(); + HcfAsyKeyGeneratorBySpec *generatorBySpec = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generatorBySpec); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generatorBySpec, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = generatorBySpec->generateKeyPair(generatorBySpec, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generatorBySpec); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc1(mallocCount, paramSpec); + + EndRecordOpensslCallNum(); +} + +static void OpensslMockTestFunc2(uint32_t mallocCount, HcfAsyKeyParamsSpec *paramSpec) +{ + for (int i = 0; i < mallocCount - FIVE; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + + HcfAsyKeyGeneratorBySpec *generatorBySpec = nullptr; + int32_t res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generatorBySpec); + if (res != HCF_SUCCESS) { + continue; + } + HcfPriKey *priKey = nullptr; + res = generatorBySpec->generatePriKey(generatorBySpec, &priKey); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generatorBySpec); + continue; + } + HcfObjDestroy(priKey); + HcfObjDestroy(generatorBySpec); + } +} + +HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecTest, CryptoSm2AsyKeyGeneratorBySpecTest105, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + int32_t res = ConstructSm2256PriKeyParamsSpec(g_sm2AlgName, ¶mSpec); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + StartRecordOpensslCallNum(); + HcfAsyKeyGeneratorBySpec *generatorBySpec = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generatorBySpec); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generatorBySpec, nullptr); + + HcfPriKey *priKey = nullptr; + res = generatorBySpec->generatePriKey(generatorBySpec, &priKey); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfObjDestroy(priKey); + HcfObjDestroy(generatorBySpec); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc2(mallocCount, paramSpec); + + EndRecordOpensslCallNum(); +} + +static void OpensslMockTestFunc3(uint32_t mallocCount, HcfAsyKeyParamsSpec *paramSpec) +{ + for (int i = 0; i < mallocCount - 1; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + + HcfAsyKeyGeneratorBySpec *generatorBySpec = nullptr; + int32_t res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generatorBySpec); + if (res != HCF_SUCCESS) { + continue; + } + HcfPubKey *pubKey = nullptr; + res = generatorBySpec->generatePubKey(generatorBySpec, &pubKey); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generatorBySpec); + continue; + } + HcfObjDestroy(pubKey); + HcfObjDestroy(generatorBySpec); + } +} + +HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecTest, CryptoSm2AsyKeyGeneratorBySpecTest106, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + int32_t res = ConstructSm2256PubKeyParamsSpec(g_sm2AlgName, ¶mSpec); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + StartRecordOpensslCallNum(); + HcfAsyKeyGeneratorBySpec *generatorBySpec = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generatorBySpec); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generatorBySpec, nullptr); + + HcfPubKey *pubKey = nullptr; + res = generatorBySpec->generatePubKey(generatorBySpec, &pubKey); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfObjDestroy(pubKey); + HcfObjDestroy(generatorBySpec); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc3(mallocCount, paramSpec); + + EndRecordOpensslCallNum(); +} } diff --git a/test/unittest/src/crypto_sm2_sign_test.cpp b/test/unittest/src/crypto_sm2_sign_test.cpp index 4683aae..b8348b0 100644 --- a/test/unittest/src/crypto_sm2_sign_test.cpp +++ b/test/unittest/src/crypto_sm2_sign_test.cpp @@ -975,6 +975,13 @@ static void OpensslMockTestFunc(uint32_t mallocCount, HcfBlob *input) HcfObjDestroy(keyPair); continue; } + uint8_t pSourceData[] = "1234567812345678\0"; + HcfBlob pSource = {.data = (uint8_t *)pSourceData, .len = strlen((char *)pSourceData)}; + res = sign->setSignSpecUint8Array(sign, SM2_USER_ID_UINT8ARR, pSource); + if (res != HCF_SUCCESS) { + HcfObjDestroy(sign); + continue; + } res = sign->update(sign, input); if (res != HCF_SUCCESS) { HcfObjDestroy(sign); @@ -1018,6 +1025,10 @@ HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest043, TestSize.Level0) res = sign->init(sign, ¶ms, keyPair->priKey); ASSERT_EQ(res, HCF_SUCCESS); + uint8_t pSourceData[] = "1234567812345678\0"; + HcfBlob pSource = {.data = (uint8_t *)pSourceData, .len = strlen((char *)pSourceData)}; + res = sign->setSignSpecUint8Array(sign, SM2_USER_ID_UINT8ARR, pSource); + ASSERT_EQ(res, HCF_SUCCESS); const char *message = "hello world"; HcfBlob input = { .data = (uint8_t *)message, @@ -1316,8 +1327,28 @@ HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest058, TestSize.Level0) HcfObjDestroy(sign); } -// sign设置userid参数,进行签名,verify不设置参数进行验签 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest059, TestSize.Level0) +{ + HcfSignatureParams params = { + .algo = HCF_ALG_SM2, + .md = HCF_OPENSSL_DIGEST_SM3, + }; + HcfSignSpi *spiObj = nullptr; + int32_t res = HcfSignSpiSm2Create(¶ms, &spiObj); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(spiObj, nullptr); + + uint8_t pSourceData[] = "1234567812345678\0"; + HcfBlob pSource = {.data = (uint8_t *)pSourceData, .len = strlen((char *)pSourceData)}; + res = spiObj->engineSetSignSpecUint8Array(nullptr, SM2_USER_ID_UINT8ARR, pSource); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + HcfObjDestroy(spiObj); +} + +// sign设置userid参数,进行签名,verify不设置参数进行验签 +HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest060, TestSize.Level0) { HcfSign *sign = nullptr; int32_t res = HcfSignCreate("SM2|SM3", &sign); diff --git a/test/unittest/src/crypto_sm2_verify_test.cpp b/test/unittest/src/crypto_sm2_verify_test.cpp index 30d847a..8dc4362 100644 --- a/test/unittest/src/crypto_sm2_verify_test.cpp +++ b/test/unittest/src/crypto_sm2_verify_test.cpp @@ -1000,6 +1000,13 @@ static void OpensslMockTestFunc(uint32_t mallocCount, HcfBlob *out) HcfObjDestroy(verify); continue; } + uint8_t pSourceData[] = "1234567812345678\0"; + HcfBlob pSource = {.data = (uint8_t *)pSourceData, .len = strlen((char *)pSourceData)}; + res = verify->setVerifySpecUint8Array(verify, SM2_USER_ID_UINT8ARR, pSource); + if (res != HCF_SUCCESS) { + HcfObjDestroy(verify); + continue; + } res = verify->update(verify, &g_mockInput); if (res != HCF_SUCCESS) { HcfObjDestroy(verify); @@ -1018,20 +1025,18 @@ HWTEST_F(CryptoSm2VerifyTest, CryptoSm2VerifyTest042, TestSize.Level0) HcfVerify *verify = nullptr; int32_t res = HcfVerifyCreate("SM2|SM3", &verify); - ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(verify, nullptr); - res = verify->init(verify, nullptr, g_sm2256KeyPair_->pubKey); - ASSERT_EQ(res, HCF_SUCCESS); - + uint8_t pSourceData[] = "1234567812345678\0"; + HcfBlob pSource = {.data = (uint8_t *)pSourceData, .len = strlen((char *)pSourceData)}; + res = verify->setVerifySpecUint8Array(verify, SM2_USER_ID_UINT8ARR, pSource); + ASSERT_EQ(res, HCF_SUCCESS); res = verify->update(verify, &g_mockInput); - ASSERT_EQ(res, HCF_SUCCESS); bool flag = verify->verify(verify, &g_mockInput, &out); - ASSERT_EQ(flag, true); HcfObjDestroy(verify); @@ -1318,8 +1323,27 @@ HWTEST_F(CryptoSm2VerifyTest, CryptoSm2VerifyTest057, TestSize.Level0) HcfObjDestroy(verify); } -// Test verify signData from third-Party HWTEST_F(CryptoSm2VerifyTest, CryptoSm2VerifyTest058, TestSize.Level0) +{ + HcfSignatureParams params = { + .algo = HCF_ALG_SM2, + .md = HCF_OPENSSL_DIGEST_SM3, + }; + HcfVerifySpi *spiObj = nullptr; + int32_t res = HcfVerifySpiSm2Create(¶ms, &spiObj); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(spiObj, nullptr); + uint8_t pSourceData[] = "1234567812345678\0"; + HcfBlob pSource = {.data = (uint8_t *)pSourceData, .len = strlen((char *)pSourceData)}; + res = spiObj->engineSetVerifySpecUint8Array(nullptr, SM2_USER_ID_UINT8ARR, pSource); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + HcfObjDestroy(spiObj); +} + +// Test verify signData from third-Party +HWTEST_F(CryptoSm2VerifyTest, CryptoSm2VerifyTest059, TestSize.Level0) { uint8_t pk[] = { 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, diff --git a/test/unittest/src/crypto_x25519_asy_key_generator_by_spec_test.cpp b/test/unittest/src/crypto_x25519_asy_key_generator_by_spec_test.cpp new file mode 100644 index 0000000..4d13372 --- /dev/null +++ b/test/unittest/src/crypto_x25519_asy_key_generator_by_spec_test.cpp @@ -0,0 +1,834 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "securec.h" + +#include "asy_key_generator.h" +#include "asy_key_generator_spi.h" +#include "blob.h" +#include "signature.h" +#include "memory.h" +#include "openssl_class.h" +#include "openssl_common.h" +#include "asy_key_params.h" +#include "key_utils.h" +#include "key_pair.h" +#include "object_base.h" +#include "alg_25519_asy_key_generator_openssl.h" +#include "detailed_alg_25519_key_params.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoX25519AsyKeyGeneratorBySpecTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +static string g_x25519AlgoName = "X25519"; +static string g_pubkeyformatName = "X.509"; +static string g_prikeyformatName = "PKCS#8"; +static string g_algorithmName = "Alg25519"; + +HcfAlg25519KeyPairParamsSpec g_x25519KeyPairSpec; +HcfAlg25519PriKeyParamsSpec g_x25519PriKeySpec; +HcfAlg25519PubKeyParamsSpec g_x25519PubKeySpec; + +void CryptoX25519AsyKeyGeneratorBySpecTest::SetUp() {} +void CryptoX25519AsyKeyGeneratorBySpecTest::TearDown() {} +void CryptoX25519AsyKeyGeneratorBySpecTest::SetUpTestCase() {} +void CryptoX25519AsyKeyGeneratorBySpecTest::TearDownTestCase() {} + +static HcfResult ConstructX25519KeyPairParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + + HcfAlg25519KeyPairParamsSpec *x25519KeyPairSpec = &g_x25519KeyPairSpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + + x25519KeyPairSpec->base.algName = g_x25519AlgoName.data(); + x25519KeyPairSpec->base.specType = HCF_KEY_PAIR_SPEC; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, X25519_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + x25519KeyPairSpec->pk.data = retBigInt.data; + x25519KeyPairSpec->pk.len = retBigInt.len; + + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, X25519_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + x25519KeyPairSpec->sk.data = retBigInt.data; + x25519KeyPairSpec->sk.len = retBigInt.len; + + *spec = (HcfAsyKeyParamsSpec *)x25519KeyPairSpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ConstructX25519PubKeyParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return HCF_INVALID_PARAMS; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return HCF_INVALID_PARAMS; + } + + HcfAlg25519PubKeyParamsSpec *x25519PubKeySpec = &g_x25519PubKeySpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + x25519PubKeySpec->base.algName = g_x25519AlgoName.data(); + x25519PubKeySpec->base.specType = HCF_PUBLIC_KEY_SPEC; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, X25519_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + x25519PubKeySpec->pk.data = retBigInt.data; + x25519PubKeySpec->pk.len = retBigInt.len; + + *spec = (HcfAsyKeyParamsSpec *)x25519PubKeySpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +static HcfResult ConstructX25519PriKeyParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + + HcfAlg25519PriKeyParamsSpec *x25519PriKeySpec = &g_x25519PriKeySpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + + x25519PriKeySpec->base.algName = g_x25519AlgoName.data(); + x25519PriKeySpec->base.specType = HCF_PRIVATE_KEY_SPEC; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, X25519_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + x25519PriKeySpec->sk.data = retBigInt.data; + x25519PriKeySpec->sk.len = retBigInt.len; + + *spec = (HcfAsyKeyParamsSpec *)x25519PriKeySpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + + return HCF_SUCCESS; +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest001_1, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest001_2, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519PubKeyParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest001_3, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519PriKeyParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest002, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + const char *className = returnObj->base.getClass(); + ASSERT_NE(className, NULL); + ASSERT_NE(returnObj, nullptr); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest003, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + returnObj->base.destroy((HcfObjectBase *)returnObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest004, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + const char *algoName = returnObj->getAlgName(returnObj); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_EQ(algoName, g_x25519AlgoName); + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest005, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest006, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest007, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(&(keyPair->base)); + + ASSERT_NE(returnObj, nullptr); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest008, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->pubKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest009, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&(keyPair->pubKey->base.base)); + keyPair->pubKey = nullptr; + + ASSERT_NE(returnObj, nullptr); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest010, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(&(keyPair->pubKey->base)); + ASSERT_EQ(algorithmName, g_algorithmName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->pubKey->base.getFormat(&(keyPair->pubKey->base)); + ASSERT_EQ(formatName, g_pubkeyformatName); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest011, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->priKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest012, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&(keyPair->priKey->base.base)); + keyPair->priKey = nullptr; + + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest013, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(&(keyPair->priKey->base)); + ASSERT_EQ(algorithmName, g_algorithmName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->priKey->base.getFormat(&(keyPair->priKey->base)); + ASSERT_EQ(formatName, g_prikeyformatName); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest014, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnObj->generateKeyPair(returnObj, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(keyPair->priKey); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(returnObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest015, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *pubparamSpec = nullptr; + HcfResult res = ConstructX25519PubKeyParamsSpec(g_x25519AlgoName, &pubparamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubparamSpec, nullptr); + + HcfAsyKeyParamsSpec *priparamSpec = nullptr; + res = ConstructX25519PriKeyParamsSpec(g_x25519AlgoName, &priparamSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priparamSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnpubObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(pubparamSpec, &returnpubObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnpubObj, nullptr); + + HcfAsyKeyGeneratorBySpec *returnpriObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(priparamSpec, &returnpriObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnpriObj, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnpubObj->generatePubKey(returnpubObj, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnpriObj->generatePriKey(returnpriObj, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 }; + res = pubKey->getAsyKeySpecBigInteger(pubKey, X25519_PK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + res = priKey->getAsyKeySpecBigInteger(priKey, X25519_SK_BN, &returnBigInteger); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnBigInteger.data, nullptr); + ASSERT_NE(returnBigInteger.len, 0); + + HcfObjDestroy(pubKey); + HcfObjDestroy(priKey); + HcfObjDestroy(returnpubObj); + HcfObjDestroy(returnpriObj); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest016, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_X25519, + .bits = HCF_ALG_X25519_256, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiX25519Create(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + HcfObjDestroy(returnSpi); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest017, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_X25519, + .bits = HCF_ALG_X25519_256, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiX25519Create(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramsSpec = nullptr; + res = ConstructX25519KeyPairParamsSpec("X25519", ¶msSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = returnSpi->engineGenerateKeyPairBySpec(returnSpi, paramsSpec, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest018, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_X25519, + .bits = HCF_ALG_X25519_256, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiX25519Create(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructX25519PubKeyParamsSpec("X25519", ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnSpi->engineGeneratePubKeyBySpec(returnSpi, paramSpec, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(pubKey); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest019, TestSize.Level0) +{ + HcfAsyKeyGenParams params = { + .algo = HCF_ALG_X25519, + .bits = HCF_ALG_X25519_256, + .primes = HCF_OPENSSL_PRIMES_2, + }; + + HcfAsyKeyGeneratorSpi *returnSpi = nullptr; + HcfResult res = HcfAsyKeyGeneratorSpiX25519Create(¶ms, &returnSpi); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructX25519PriKeyParamsSpec("X25519", ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnSpi, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnSpi->engineGeneratePriKeyBySpec(returnSpi, paramSpec, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfObjDestroy(returnSpi); + HcfObjDestroy(priKey); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest020, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob1 = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob1); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob1.data, nullptr); + ASSERT_NE(blob1.len, 0); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructX25519PubKeyParamsSpec("X25519", ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnObj->generatePubKey(returnObj, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfBlob blob2 = { .data = nullptr, .len = 0 }; + res = pubKey->base.getEncoded(&(pubKey->base), &blob2); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob2.data, nullptr); + ASSERT_NE(blob2.len, 0); + + ASSERT_EQ(*(blob1.data), *(blob2.data)); + ASSERT_EQ(blob1.len, blob2.len); + + HcfObjDestroy(returnObj); + HcfObjDestroy(pubKey); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest021, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob blob1 = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob1); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob1.data, nullptr); + ASSERT_NE(blob1.len, 0); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructX25519PriKeyParamsSpec("X25519", ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnObj->generatePriKey(returnObj, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfBlob blob2 = { .data = nullptr, .len = 0 }; + res = priKey->base.getEncoded(&(priKey->base), &blob2); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob2.data, nullptr); + ASSERT_NE(blob2.len, 0); + + ASSERT_EQ(*(blob1.data), *(blob2.data)); + ASSERT_EQ(blob1.len, blob2.len); + + HcfObjDestroy(returnObj); + HcfObjDestroy(priKey); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest022, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519PriKeyParamsSpec("X25519", ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPriKey *priKey = nullptr; + res = returnObj->generatePriKey(returnObj, &priKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKey, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = priKey->base.getEncoded(&(priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + HcfAsyKeyGenerator *generator; + res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, nullptr, &blob, &keyPair); + + HcfObjDestroy(returnObj); + HcfObjDestroy(priKey); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorBySpecTest, CryptoX25519AsyKeyGeneratorBySpecTest023, TestSize.Level0) +{ + HcfAsyKeyParamsSpec *paramSpec = nullptr; + HcfResult res = ConstructX25519PubKeyParamsSpec("X25519", ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *returnObj = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(returnObj, nullptr); + + HcfPubKey *pubKey = nullptr; + res = returnObj->generatePubKey(returnObj, &pubKey); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKey, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = pubKey->base.getEncoded(&(pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + HcfAsyKeyGenerator *generator; + res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &blob, nullptr, &keyPair); + + HcfObjDestroy(returnObj); + HcfObjDestroy(pubKey); + HcfObjDestroy(keyPair); +} +} \ No newline at end of file diff --git a/test/unittest/src/crypto_x25519_asy_key_generator_test.cpp b/test/unittest/src/crypto_x25519_asy_key_generator_test.cpp new file mode 100644 index 0000000..b178da5 --- /dev/null +++ b/test/unittest/src/crypto_x25519_asy_key_generator_test.cpp @@ -0,0 +1,741 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "securec.h" + +#include "asy_key_generator.h" +#include "ecc_asy_key_generator_openssl.h" +#include "blob.h" +#include "params_parser.h" +#include "key_utils.h" +#include "key_pair.h" +#include "object_base.h" +#include "memory_mock.h" +#include "openssl_adapter_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoX25519AsyKeyGeneratorTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void CryptoX25519AsyKeyGeneratorTest::TearDownTestCase() {} +void CryptoX25519AsyKeyGeneratorTest::SetUp() {} +void CryptoX25519AsyKeyGeneratorTest::TearDown() {} + +static string g_x25519AlgoName = "X25519"; +static string g_pubkeyformatName = "X.509"; +static string g_prikeyformatName = "PKCS#8"; +static string g_algorithmName = "Alg25519"; + +HcfBlob g_mockX25519PriKeyBlob = { + .data = nullptr, + .len = 0 +}; + +HcfBlob g_mockX25519PubKeyBlob = { + .data = nullptr, + .len = 0 +}; + +static HcfResult X25519KeyBlob(HcfBlob * priblob, HcfBlob *pubblob) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + if (res != HCF_SUCCESS) { + return res; + } + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + return res; + } + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &g_mockX25519PriKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &g_mockX25519PubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +void CryptoX25519AsyKeyGeneratorTest::SetUpTestCase() +{ + HcfResult res = X25519KeyBlob(&g_mockX25519PriKeyBlob, &g_mockX25519PubKeyBlob); + ASSERT_EQ(res, HCF_SUCCESS); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest001, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest002, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + + const char *className = generator->base.getClass(); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest003, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + generator->base.destroy((HcfObjectBase *)generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest004, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + + const char *algoName = generator->getAlgoName(generator); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_EQ(algoName, g_x25519AlgoName); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest005, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest006, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest007, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(&(keyPair->base)); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest008, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->pubKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest009, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&(keyPair->pubKey->base.base)); + keyPair->pubKey = nullptr; + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest010, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(&(keyPair->pubKey->base)); + ASSERT_EQ(algorithmName, g_algorithmName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->pubKey->base.getFormat(&(keyPair->pubKey->base)); + ASSERT_EQ(formatName, g_pubkeyformatName); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest011, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->priKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest012, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&(keyPair->priKey->base.base)); + keyPair->priKey = nullptr; + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest013, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(&(keyPair->priKey->base)); + ASSERT_EQ(algorithmName, g_algorithmName); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->priKey->base.getFormat(&(keyPair->priKey->base)); + ASSERT_EQ(formatName, g_prikeyformatName); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest014, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(keyPair->priKey); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest015, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, &g_mockX25519PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + res = generator->convertKey(generator, nullptr, nullptr, &g_mockX25519PriKeyBlob, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, nullptr, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest016, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, &g_mockX25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest017, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, &g_mockX25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->base.destroy(&(keyPair->base)); + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest018, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, &g_mockX25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->pubKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest019, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, &g_mockX25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->pubKey->base.base.destroy(&(keyPair->pubKey->base.base)); + keyPair->pubKey = nullptr; + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest020, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, &g_mockX25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->pubKey->base.getAlgorithm(&(keyPair->pubKey->base)); + ASSERT_NE(algorithmName, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->pubKey->base.getFormat(&(keyPair->pubKey->base)); + ASSERT_NE(formatName, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest021, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, &g_mockX25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *className = keyPair->priKey->base.base.getClass(); + ASSERT_NE(className, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest022, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, &g_mockX25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->base.base.destroy(&(keyPair->priKey->base.base)); + keyPair->priKey = nullptr; + + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest023, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, &g_mockX25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + const char *algorithmName = keyPair->priKey->base.getAlgorithm(&(keyPair->priKey->base)); + ASSERT_NE(algorithmName, nullptr); + + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(blob.data, nullptr); + ASSERT_NE(blob.len, 0); + + const char *formatName = keyPair->priKey->base.getFormat(&(keyPair->priKey->base)); + ASSERT_NE(formatName, nullptr); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest024, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfKeyPair *keyPair = nullptr; + res = generator->convertKey(generator, nullptr, &g_mockX25519PubKeyBlob, &g_mockX25519PriKeyBlob, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + keyPair->priKey->clearMem(keyPair->priKey); + HcfBlob blob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(blob.data, nullptr); + ASSERT_EQ(blob.len, 0); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + +static void MemoryMallocTestFunc(uint32_t mallocCount) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetRecordMallocNum(); + SetMockMallocIndex(i); + HcfAsyKeyGenerator *tmpGenerator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &tmpGenerator); + if (res != HCF_SUCCESS) { + continue; + } + HcfKeyPair *tmpKeyPair = nullptr; + res = tmpGenerator->generateKeyPair(tmpGenerator, nullptr, &tmpKeyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPubKeyBlob = { + .data = nullptr, + .len = 0 + }; + res = tmpKeyPair->pubKey->base.getEncoded(&(tmpKeyPair->pubKey->base), &tmpPubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPriKeyBlob = { + .data = nullptr, + .len = 0 + }; + res = tmpKeyPair->priKey->base.getEncoded(&(tmpKeyPair->priKey->base), &tmpPriKeyBlob); + if (res != HCF_SUCCESS) { + free(tmpPubKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfKeyPair *tmpOutKeyPair = nullptr; + res = tmpGenerator->convertKey(tmpGenerator, nullptr, &tmpPubKeyBlob, &tmpPriKeyBlob, &tmpOutKeyPair); + free(tmpPubKeyBlob.data); + free(tmpPriKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + if (res == HCF_SUCCESS) { + HcfObjDestroy(tmpOutKeyPair); + } + } +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest025, TestSize.Level0) +{ + StartRecordMallocNum(); + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob pubKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &pubKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKeyBlob.data, nullptr); + ASSERT_NE(pubKeyBlob.len, 0); + + HcfBlob priKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &priKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKeyBlob.data, nullptr); + ASSERT_NE(priKeyBlob.len, 0); + + HcfKeyPair *outKeyPair = nullptr; + res = generator->convertKey(generator, nullptr, &pubKeyBlob, &priKeyBlob, &outKeyPair); + + free(pubKeyBlob.data); + free(priKeyBlob.data); + HcfObjDestroy(outKeyPair); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + + uint32_t mallocCount = GetMallocNum(); + MemoryMallocTestFunc(mallocCount); + + EndRecordMallocNum(); +} + +static void OpensslMockTestFunc(uint32_t mallocCount) +{ + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + HcfAsyKeyGenerator *tmpGenerator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &tmpGenerator); + if (res != HCF_SUCCESS) { + continue; + } + HcfKeyPair *tmpKeyPair = nullptr; + res = tmpGenerator->generateKeyPair(tmpGenerator, nullptr, &tmpKeyPair); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPubKeyBlob = { + .data = nullptr, + .len = 0 + }; + res = tmpKeyPair->pubKey->base.getEncoded(&(tmpKeyPair->pubKey->base), &tmpPubKeyBlob); + if (res != HCF_SUCCESS) { + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfBlob tmpPriKeyBlob = { + .data = nullptr, + .len = 0 + }; + res = tmpKeyPair->priKey->base.getEncoded(&(tmpKeyPair->priKey->base), &tmpPriKeyBlob); + if (res != HCF_SUCCESS) { + free(tmpPubKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + continue; + } + HcfKeyPair *tmpOutKeyPair = nullptr; + res = tmpGenerator->convertKey(tmpGenerator, nullptr, &tmpPubKeyBlob, &tmpPriKeyBlob, &tmpOutKeyPair); + free(tmpPubKeyBlob.data); + free(tmpPriKeyBlob.data); + HcfObjDestroy(tmpKeyPair); + HcfObjDestroy(tmpGenerator); + if (res == HCF_SUCCESS) { + HcfObjDestroy(tmpOutKeyPair); + } + } +} + +HWTEST_F(CryptoX25519AsyKeyGeneratorTest, CryptoX25519AsyKeyGeneratorTest026, TestSize.Level0) +{ + StartRecordOpensslCallNum(); + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate("X25519", &generator); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob pubKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &pubKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKeyBlob.data, nullptr); + ASSERT_NE(pubKeyBlob.len, 0); + + HcfBlob priKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &priKeyBlob); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKeyBlob.data, nullptr); + ASSERT_NE(priKeyBlob.len, 0); + + HcfKeyPair *outKeyPair = nullptr; + res = generator->convertKey(generator, nullptr, &pubKeyBlob, &priKeyBlob, &outKeyPair); + + free(pubKeyBlob.data); + free(priKeyBlob.data); + HcfObjDestroy(outKeyPair); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + + uint32_t mallocCount = GetOpensslCallNum(); + OpensslMockTestFunc(mallocCount); + + EndRecordOpensslCallNum(); +} +} \ No newline at end of file diff --git a/test/unittest/src/crypto_x25519_key_agreement_test.cpp b/test/unittest/src/crypto_x25519_key_agreement_test.cpp new file mode 100644 index 0000000..8b20f7c --- /dev/null +++ b/test/unittest/src/crypto_x25519_key_agreement_test.cpp @@ -0,0 +1,441 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "securec.h" + +#include "asy_key_generator.h" +#include "alg_25519_asy_key_generator_openssl.h" +#include "detailed_alg_25519_key_params.h" +#include "key_agreement.h" +#include "params_parser.h" +#include "x25519_openssl.h" +#include "memory_mock.h" +#include "openssl_adapter_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoX25519KeyAgreementTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + + static HcfKeyPair *ed25519KeyPair_; + static HcfKeyPair *x25519KeyPair_; +}; + +HcfKeyPair *CryptoX25519KeyAgreementTest::ed25519KeyPair_ = nullptr; +HcfKeyPair *CryptoX25519KeyAgreementTest::x25519KeyPair_ = nullptr; + +static string g_ed25519AlgoName = "Ed25519"; +static string g_x25519AlgoName = "X25519"; + +HcfAlg25519KeyPairParamsSpec g_x25519KeyPairSpec; + +void CryptoX25519KeyAgreementTest::SetUp() {} +void CryptoX25519KeyAgreementTest::TearDown() {} + +static const char *GetMockClass(void) +{ + return "HcfSymKeyGenerator"; +} + +static HcfObjectBase g_obj = { + .getClass = GetMockClass, + .destroy = nullptr +}; + +static HcfResult ConstructX25519KeyPairParamsSpec(const string &algoName, HcfAsyKeyParamsSpec **spec) +{ + HcfAsyKeyGenerator *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return res; + } + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + if (res != HCF_SUCCESS) { + return res; + } + HcfAlg25519KeyPairParamsSpec *x25519KeyPairSpec = &g_x25519KeyPairSpec; + HcfBigInteger retBigInt = { .data = nullptr, .len = 0 }; + x25519KeyPairSpec->base.algName = const_cast(g_x25519AlgoName.c_str()); + x25519KeyPairSpec->base.specType = HCF_KEY_PAIR_SPEC; + res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, X25519_PK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + x25519KeyPairSpec->pk.data = retBigInt.data; + x25519KeyPairSpec->pk.len = retBigInt.len; + res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, X25519_SK_BN, &retBigInt); + if (res != HCF_SUCCESS) { + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return res; + } + x25519KeyPairSpec->sk.data = retBigInt.data; + x25519KeyPairSpec->sk.len = retBigInt.len; + + *spec = (HcfAsyKeyParamsSpec *)x25519KeyPairSpec; + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); + return HCF_SUCCESS; +} + +void CryptoX25519KeyAgreementTest::SetUpTestCase() +{ + HcfAsyKeyGenerator *generator = nullptr; + int32_t ret = HcfAsyKeyGeneratorCreate(g_ed25519AlgoName.c_str(), &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + ret = generator->generateKeyPair(generator, nullptr, &keyPair); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + ed25519KeyPair_ = keyPair; + + ret = HcfAsyKeyGeneratorCreate(g_x25519AlgoName.c_str(), &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + ret = generator->generateKeyPair(generator, nullptr, &keyPair); + ASSERT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + x25519KeyPair_ = keyPair; + + HcfObjDestroy(generator); +} + +void CryptoX25519KeyAgreementTest::TearDownTestCase() +{ + HcfObjDestroy(ed25519KeyPair_); + HcfObjDestroy(x25519KeyPair_); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest001, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("X25519", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest002, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("X25519", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + const char *className = keyAgreement->base.getClass(); + ASSERT_NE(className, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest003, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("X25519", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + keyAgreement->base.destroy((HcfObjectBase *)keyAgreement); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest004, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("X25519", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + const char *algName = keyAgreement->getAlgoName(keyAgreement); + ASSERT_EQ(algName, g_x25519AlgoName); + + HcfAsyKeyParamsSpec *paramSpec = nullptr; + res = ConstructX25519KeyPairParamsSpec(g_x25519AlgoName, ¶mSpec); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(paramSpec, nullptr); + + HcfAsyKeyGeneratorBySpec *generator = nullptr; + res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &generator); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(generator, nullptr); + + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, &keyPair); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = keyAgreement->generateSecret(keyAgreement, keyPair->priKey, keyPair->pubKey, &out); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(out.data, nullptr); + ASSERT_NE(out.len, (const unsigned int)0); + + free(out.data); + HcfObjDestroy(keyAgreement); + HcfObjDestroy(generator); + HcfObjDestroy(keyPair); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest005, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("x25519", &keyAgreement); + + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(keyAgreement, nullptr); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest006, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("X25519", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + keyAgreement->base.destroy(nullptr); + keyAgreement->base.destroy(&g_obj); + + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest007, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("X25519", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + const char *algName = nullptr; + algName = keyAgreement->getAlgoName(nullptr); + ASSERT_EQ(algName, nullptr); + + algName = keyAgreement->getAlgoName((HcfKeyAgreement *)(&g_obj)); + ASSERT_EQ(algName, nullptr); + + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest008, TestSize.Level0) +{ + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate("X25519", &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = keyAgreement->generateSecret(keyAgreement, x25519KeyPair_->priKey, nullptr, &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(out.data, nullptr); + ASSERT_EQ(out.len, (const unsigned int)0); + + res = keyAgreement->generateSecret(keyAgreement, nullptr, x25519KeyPair_->pubKey, &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(out.data, nullptr); + ASSERT_EQ(out.len, (const unsigned int)0); + + res = keyAgreement->generateSecret(keyAgreement, ed25519KeyPair_->priKey, ed25519KeyPair_->pubKey, &out); + ASSERT_EQ(res, HCF_ERR_CRYPTO_OPERATION); + ASSERT_EQ(out.data, nullptr); + ASSERT_EQ(out.len, (const unsigned int)0); + + res = keyAgreement->generateSecret((HcfKeyAgreement *)&g_obj, ed25519KeyPair_->priKey, + ed25519KeyPair_->pubKey, &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(out.data, nullptr); + ASSERT_EQ(out.len, (const unsigned int)0); + + free(out.data); + HcfObjDestroy(keyAgreement); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest009, TestSize.Level0) +{ + HcfKeyAgreementParams params = { + .algo = HCF_ALG_X25519, + }; + + HcfResult res = HcfKeyAgreementSpiX25519Create(¶ms, nullptr); + + ASSERT_EQ(res, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest010, TestSize.Level0) +{ + HcfKeyAgreementParams params = { + .algo = HCF_ALG_X25519, + }; + + HcfKeyAgreementSpi *spiObj = nullptr; + HcfResult res = HcfKeyAgreementSpiX25519Create(¶ms, &spiObj); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(spiObj, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = spiObj->engineGenerateSecret((HcfKeyAgreementSpi *)&g_obj, x25519KeyPair_->priKey, x25519KeyPair_->pubKey, + &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + res = spiObj->engineGenerateSecret(spiObj, (HcfPriKey *)&g_obj, x25519KeyPair_->pubKey, &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + res = spiObj->engineGenerateSecret(spiObj, x25519KeyPair_->priKey, (HcfPubKey *)&g_obj, &out); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + + HcfObjDestroy(spiObj); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest011, TestSize.Level0) +{ + HcfKeyAgreementParams params = { + .algo = HCF_ALG_X25519, + }; + + HcfKeyAgreementSpi *spiObj = nullptr; + HcfResult res = HcfKeyAgreementSpiX25519Create(¶ms, &spiObj); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(spiObj, nullptr); + + spiObj->base.destroy(nullptr); + + HcfObjDestroy(spiObj); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest012, TestSize.Level0) +{ + HcfKeyAgreementParams params = { + .algo = HCF_ALG_X25519, + }; + + HcfKeyAgreementSpi *spiObj = nullptr; + HcfResult res = HcfKeyAgreementSpiX25519Create(¶ms, &spiObj); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(spiObj, nullptr); + + spiObj->base.destroy(&g_obj); + + HcfObjDestroy(spiObj); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest013, TestSize.Level0) +{ + StartRecordMallocNum(); + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_x25519AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = keyAgreement->generateSecret(keyAgreement, x25519KeyPair_->priKey, x25519KeyPair_->pubKey, &out); + + ASSERT_EQ(res, HCF_SUCCESS); + + HcfObjDestroy(keyAgreement); + + uint32_t mallocCount = GetMallocNum(); + + for (uint32_t i = 0; i < mallocCount; i++) { + ResetRecordMallocNum(); + SetMockMallocIndex(i); + keyAgreement = nullptr; + res = HcfKeyAgreementCreate(g_x25519AlgoName.c_str(), &keyAgreement); + + if (res != HCF_SUCCESS) { + continue; + } + + out = { + .data = nullptr, + .len = 0 + }; + res = keyAgreement->generateSecret(keyAgreement, x25519KeyPair_->priKey, x25519KeyPair_->pubKey, &out); + + if (res != HCF_SUCCESS) { + HcfObjDestroy(keyAgreement); + continue; + } + + HcfObjDestroy(keyAgreement); + free(out.data); + } + EndRecordMallocNum(); +} + +HWTEST_F(CryptoX25519KeyAgreementTest, CryptoX25519KeyAgreementTest014, TestSize.Level0) +{ + StartRecordOpensslCallNum(); + HcfKeyAgreement *keyAgreement = nullptr; + HcfResult res = HcfKeyAgreementCreate(g_x25519AlgoName.c_str(), &keyAgreement); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyAgreement, nullptr); + + HcfBlob out = { .data = nullptr, .len = 0 }; + res = keyAgreement->generateSecret(keyAgreement, x25519KeyPair_->priKey, x25519KeyPair_->pubKey, &out); + + ASSERT_EQ(res, HCF_SUCCESS); + + HcfObjDestroy(keyAgreement); + + uint32_t mallocCount = GetOpensslCallNum(); + + for (uint32_t i = 0; i < mallocCount; i++) { + ResetOpensslCallNum(); + SetOpensslCallMockIndex(i); + keyAgreement = nullptr; + res = HcfKeyAgreementCreate(g_x25519AlgoName.c_str(), &keyAgreement); + + if (res != HCF_SUCCESS) { + continue; + } + + out = { + .data = nullptr, + .len = 0 + }; + res = keyAgreement->generateSecret(keyAgreement, x25519KeyPair_->priKey, x25519KeyPair_->pubKey, &out); + + if (res != HCF_SUCCESS) { + HcfObjDestroy(keyAgreement); + continue; + } + + HcfObjDestroy(keyAgreement); + free(out.data); + } + EndRecordOpensslCallNum(); +} +} \ No newline at end of file diff --git a/test/unittest/src/openssl_adapter_mock.c b/test/unittest/src/openssl_adapter_mock.c index 53afa6d..7ac1063 100644 --- a/test/unittest/src/openssl_adapter_mock.c +++ b/test/unittest/src/openssl_adapter_mock.c @@ -23,6 +23,7 @@ static uint32_t g_mockIndex = __INT32_MAX__; static uint32_t g_callNum = 0; static bool g_isRecordCallNum = false; static bool g_isNeedSpecialMock = false; +static int g_double = 2; static bool Is_Need_Mock(void) { @@ -80,12 +81,16 @@ void Openssl_BN_clear(BIGNUM *a) void Openssl_BN_clear_free(BIGNUM *a) { - BN_clear_free(a); + if (a != NULL) { + BN_clear_free(a); + } } void Openssl_BN_free(BIGNUM *a) { - BN_free(a); + if (a != NULL) { + BN_free(a); + } } BIGNUM *Openssl_BN_new(void) @@ -138,7 +143,9 @@ BN_CTX *Openssl_BN_CTX_new(void) void Openssl_BN_CTX_free(BN_CTX *ctx) { - BN_CTX_free(ctx); + if (ctx != NULL) { + BN_CTX_free(ctx); + } } int Openssl_BN_num_bytes(const BIGNUM *a) @@ -271,7 +278,9 @@ EC_GROUP *Openssl_EC_GROUP_dup(const EC_GROUP *a) void Openssl_EC_GROUP_free(EC_GROUP *group) { - EC_GROUP_free(group); + if (group != NULL) { + EC_GROUP_free(group); + } } EC_KEY *Openssl_EC_KEY_new(void) @@ -363,6 +372,14 @@ EC_POINT *Openssl_EC_POINT_new(const EC_GROUP *group) return EC_POINT_new(group); } +int Openssl_EC_POINT_copy(EC_POINT *dst, const EC_POINT *src) +{ + if (Is_Need_Mock()) { + return 0; + } + return EC_POINT_copy(dst, src); +} + int Openssl_EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx) { @@ -450,12 +467,16 @@ void Openssl_EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags) void Openssl_EC_KEY_free(EC_KEY *key) { - EC_KEY_free(key); + if (key != NULL) { + EC_KEY_free(key); + } } void Openssl_EC_POINT_free(EC_POINT *point) { - EC_POINT_free(point); + if (point != NULL) { + EC_POINT_free(point); + } } EVP_MD_CTX *Openssl_EVP_MD_CTX_new(void) @@ -466,9 +487,19 @@ EVP_MD_CTX *Openssl_EVP_MD_CTX_new(void) return EVP_MD_CTX_new(); } +EVP_PKEY_CTX *Openssl_EVP_MD_CTX_get_pkey_ctx(EVP_MD_CTX *ctx) +{ + if (Is_Need_Mock()) { + return NULL; + } + return EVP_MD_CTX_get_pkey_ctx(ctx); +} + void Openssl_EVP_MD_CTX_free(EVP_MD_CTX *ctx) { - EVP_MD_CTX_free(ctx); + if (ctx != NULL) { + EVP_MD_CTX_free(ctx); + } } int Openssl_EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) @@ -511,6 +542,30 @@ int Openssl_EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, size_t * return EVP_DigestSignFinal(ctx, sigret, siglen); } +int Openssl_EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen) +{ + if (sig != NULL && g_isNeedSpecialMock) { + g_callNum++; + } + if (Is_Need_Mock()) { + if (sig == NULL) { + return -1; + } + if (g_isNeedSpecialMock) { + int res = EVP_DigestSign(ctx, sig, siglen, tbs, tbslen); + *siglen = *siglen * 2; + g_isNeedSpecialMock = false; + return res; + } + g_isNeedSpecialMock = true; + return -1; + } + if (sig != NULL) { + g_callNum++; + } + return EVP_DigestSign(ctx, sig, siglen, tbs, tbslen); +} + int Openssl_EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) { if (Is_Need_Mock()) { @@ -535,6 +590,15 @@ int Openssl_EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, siz return EVP_DigestVerifyFinal(ctx, sig, siglen); } +int Openssl_EVP_DigestVerify(EVP_MD_CTX *ctx, unsigned char *sig, size_t siglen, + const unsigned char *tbs, size_t tbslen) +{ + if (Is_Need_Mock()) { + return -1; + } + return EVP_DigestVerify(ctx, sig, siglen, tbs, tbslen); +} + int Openssl_EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) { if (Is_Need_Mock()) { @@ -569,6 +633,15 @@ int Openssl_EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t return EVP_PKEY_verify(ctx, sig, siglen, tbs, tbslen); } +EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, + EVP_PKEY *pkey, const char *propquery) +{ + if (Is_Need_Mock()) { + return NULL; + } + return EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propquery); +} + EVP_PKEY *Openssl_EVP_PKEY_new(void) { if (Is_Need_Mock()) { @@ -577,6 +650,38 @@ EVP_PKEY *Openssl_EVP_PKEY_new(void) return EVP_PKEY_new(); } +EVP_PKEY *Openssl_EVP_PKEY_new_raw_public_key(int type, ENGINE *e, const unsigned char *pub, size_t len) +{ + if (Is_Need_Mock()) { + return NULL; + } + return EVP_PKEY_new_raw_public_key(type, e, pub, len); +} + +EVP_PKEY *Openssl_EVP_PKEY_new_raw_private_key(int type, ENGINE *e, const unsigned char *pub, size_t len) +{ + if (Is_Need_Mock()) { + return NULL; + } + return EVP_PKEY_new_raw_private_key(type, e, pub, len); +} + +int Openssl_EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, size_t *len) +{ + if (Is_Need_Mock()) { + return -1; + } + return EVP_PKEY_get_raw_public_key(pkey, pub, len); +} + +int Openssl_EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, size_t *len) +{ + if (Is_Need_Mock()) { + return -1; + } + return EVP_PKEY_get_raw_private_key(pkey, priv, len); +} + int Openssl_EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) { if (Is_Need_Mock()) { @@ -587,7 +692,9 @@ int Openssl_EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) void Openssl_EVP_PKEY_free(EVP_PKEY *pkey) { - EVP_PKEY_free(pkey); + if (pkey != NULL) { + EVP_PKEY_free(pkey); + } } EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) @@ -606,6 +713,30 @@ int Openssl_EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) return EVP_PKEY_derive_init(ctx); } +int Openssl_EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int id_len) +{ + if (id != NULL && g_isNeedSpecialMock) { + g_callNum++; + } + if (Is_Need_Mock()) { + if (id == NULL) { + return -1; + } + if (g_isNeedSpecialMock) { + int res = EVP_PKEY_CTX_set1_id(ctx, id, id_len); + id_len = id_len * g_double; + g_isNeedSpecialMock = false; + return res; + } + g_isNeedSpecialMock = true; + return -1; + } + if (id != NULL) { + g_callNum++; + } + return EVP_PKEY_CTX_set1_id(ctx, id, id_len); +} + int Openssl_EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) { if (Is_Need_Mock()) { @@ -662,7 +793,9 @@ int Openssl_EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) void Openssl_EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) { - EVP_PKEY_CTX_free(ctx); + if (ctx != NULL) { + EVP_PKEY_CTX_free(ctx); + } } EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_id(int id, ENGINE *e) @@ -673,6 +806,47 @@ EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_id(int id, ENGINE *e) return EVP_PKEY_CTX_new_id(id, e); } +int Openssl_EVP_PKEY_base_id(EVP_PKEY *pkey) +{ + return EVP_PKEY_base_id(pkey); +} + +EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx, const char *name, const char *propquery) +{ + if (Is_Need_Mock()) { + return NULL; + } + return EVP_PKEY_CTX_new_from_name(libctx, name, propquery); +} + +OSSL_PARAM Openssl_OSSL_PARAM_construct_utf8_string(const char *key, char *buf, size_t bsize) +{ + return OSSL_PARAM_construct_utf8_string(key, buf, bsize); +} + +OSSL_PARAM Openssl_OSSL_PARAM_construct_end(void) +{ + return OSSL_PARAM_construct_end(); +} + +int Openssl_EVP_PKEY_generate(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) +{ + if (Is_Need_Mock()) { + return -1; + } + return EVP_PKEY_generate(ctx, ppkey); +} + +OSSL_PARAM Openssl_OSSL_PARAM_construct_uint(const char *key, unsigned int *buf) +{ + return OSSL_PARAM_construct_uint(key, buf); +} + +OSSL_PARAM Openssl_OSSL_PARAM_construct_int(const char *key, int *buf) +{ + return OSSL_PARAM_construct_int(key, buf); +} + int Openssl_EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx) { if (Is_Need_Mock()) { @@ -689,6 +863,14 @@ int Openssl_EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits) return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits); } +int Openssl_EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params) +{ + if (Is_Need_Mock()) { + return -1; + } + return EVP_PKEY_CTX_set_params(ctx, params); +} + int Openssl_EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) { if (Is_Need_Mock()) { @@ -739,7 +921,9 @@ DSA *Openssl_DSA_new(void) void Openssl_DSA_free(DSA *dsa) { - DSA_free(dsa); + if (dsa != NULL) { + DSA_free(dsa); + } } int Openssl_DSA_up_ref(DSA *dsa) @@ -846,6 +1030,54 @@ int Openssl_i2d_DSAPrivateKey(DSA *dsa, unsigned char **ppout) return i2d_DSAPrivateKey(dsa, ppout); } +int Openssl_EVP_PKEY_check(EVP_PKEY_CTX *ctx) +{ + if (Is_Need_Mock()) { + return -1; + } + return EVP_PKEY_check(ctx); +} + +EVP_PKEY *Openssl_EVP_PKEY_dup(EVP_PKEY *a) +{ + if (Is_Need_Mock()) { + return NULL; + } + return EVP_PKEY_dup(a); +} + +EVP_PKEY *Openssl_d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length) +{ + if (Is_Need_Mock()) { + return NULL; + } + return d2i_PUBKEY(a, pp, length); +} + +EVP_PKEY *Openssl_d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp, long length) +{ + if (Is_Need_Mock()) { + return NULL; + } + return d2i_PrivateKey(type, a, pp, length); +} + +int Openssl_i2d_PUBKEY(EVP_PKEY *pkey, unsigned char **ppout) +{ + if (Is_Need_Mock()) { + return -1; + } + return i2d_PUBKEY(pkey, ppout); +} + +int Openssl_i2d_PrivateKey(EVP_PKEY *pkey, unsigned char **ppout) +{ + if (Is_Need_Mock()) { + return -1; + } + return i2d_PrivateKey(pkey, ppout); +} + RSA *Openssl_RSA_new(void) { return RSA_new(); @@ -853,7 +1085,9 @@ RSA *Openssl_RSA_new(void) void Openssl_RSA_free(RSA *rsa) { - RSA_free(rsa); + if (rsa != NULL) { + RSA_free(rsa); + } } int Openssl_RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes, @@ -1000,7 +1234,9 @@ int Openssl_BIO_read(BIO *b, void *data, int dlen) void Openssl_BIO_free_all(BIO *a) { - return BIO_free_all(a); + if (a != NULL) { + return BIO_free_all(a); + } } int Openssl_RAND_priv_bytes(unsigned char *buf, int num) @@ -1114,7 +1350,9 @@ size_t Openssl_HMAC_size(const HMAC_CTX *ctx) void Openssl_HMAC_CTX_free(HMAC_CTX *ctx) { - HMAC_CTX_free(ctx); + if (ctx != NULL) { + HMAC_CTX_free(ctx); + } } HMAC_CTX *Openssl_HMAC_CTX_new(void) @@ -1127,7 +1365,9 @@ HMAC_CTX *Openssl_HMAC_CTX_new(void) void Openssl_EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) { - EVP_CIPHER_CTX_free(ctx); + if (ctx != NULL) { + EVP_CIPHER_CTX_free(ctx); + } } const EVP_CIPHER *Openssl_EVP_aes_128_ecb(void) @@ -1476,11 +1716,154 @@ int Openssl_PKCS5_PBKDF2_HMAC(const char *pass, int passlen, const unsigned char return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, digest, keylen, out); } - int OPENSSL_EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) { if (Is_Need_Mock()) { return -1; } return EVP_CIPHER_CTX_ctrl(ctx, type, arg, ptr); -} \ No newline at end of file +} + +DH *Openssl_DH_new(void) +{ + if (Is_Need_Mock()) { + return NULL; + } + return DH_new(); +} + +int Openssl_DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh) +{ + if (Is_Need_Mock()) { + return -1; + } + return DH_compute_key_padded(key, pub_key, dh); +} + +void Openssl_DH_free(DH *dh) +{ + if (dh != NULL) { + return DH_free(dh); + } +} + +int Openssl_DH_generate_key(DH *dh) +{ + if (Is_Need_Mock()) { + return -1; + } + return DH_generate_key(dh); +} + +const BIGNUM *Openssl_DH_get0_p(const DH *dh) +{ + if (Is_Need_Mock()) { + return NULL; + } + return DH_get0_p(dh); +} + +const BIGNUM *Openssl_DH_get0_q(const DH *dh) +{ + if (Is_Need_Mock()) { + return NULL; + } + return DH_get0_q(dh); +} + +const BIGNUM *Openssl_DH_get0_g(const DH *dh) +{ + if (Is_Need_Mock()) { + return NULL; + } + return DH_get0_g(dh); +} + +long Openssl_DH_get_length(const DH *dh) +{ + if (Is_Need_Mock()) { + return -1; + } + return DH_get_length(dh); +} + +int Openssl_DH_set_length(DH *dh, long length) +{ + if (Is_Need_Mock()) { + return -1; + } + return DH_set_length(dh, length); +} + +const BIGNUM *Openssl_DH_get0_pub_key(const DH *dh) +{ + if (Is_Need_Mock()) { + return NULL; + } + return DH_get0_pub_key(dh); +} + +const BIGNUM *Openssl_DH_get0_priv_key(const DH *dh) +{ + if (Is_Need_Mock()) { + return NULL; + } + return DH_get0_priv_key(dh); +} + +int Openssl_EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) +{ + if (Is_Need_Mock()) { + return -1; + } + return EVP_PKEY_set1_DH(pkey, key); +} + +DH *Openssl_EVP_PKEY_get1_DH(EVP_PKEY *pkey) +{ + if (Is_Need_Mock()) { + return NULL; + } + return EVP_PKEY_get1_DH(pkey); +} + +int Openssl_EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key) +{ + if (Is_Need_Mock()) { + return -1; + } + return EVP_PKEY_assign_DH(pkey, key); +} + +int Openssl_EVP_PKEY_CTX_set_dh_paramgen_prime_len(EVP_PKEY_CTX *ctx, int pbits) +{ + if (Is_Need_Mock()) { + return -1; + } + return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, pbits); +} + +int Openssl_DH_up_ref(DH *r) +{ + if (Is_Need_Mock()) { + return -1; + } + return DH_up_ref(r); +} + +int Openssl_DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) +{ + if (Is_Need_Mock()) { + return -1; + } + return DH_set0_pqg(dh, p, q, g); +} + +int Openssl_DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) +{ + if (Is_Need_Mock()) { + return -1; + } + return DH_set0_key(dh, pub_key, priv_key); +} +