diff --git a/BUILD.gn b/BUILD.gn index cab89629c627888db43df4869e3cb9b2163df9f6..24224a96313e601498366a156d5440ab74ebb4e3 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -38,10 +38,12 @@ group("crypto_framework_fuzztest") { if (os_level == "standard") { deps += [ "test/fuzztest/crypto_operation/hcfciphercreate_fuzzer:fuzztest", + "test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer:fuzztest", "test/fuzztest/crypto_operation/hcfkeyagreementcreate_fuzzer:fuzztest", "test/fuzztest/crypto_operation/hcfmaccreate_fuzzer:fuzztest", "test/fuzztest/crypto_operation/hcfmdcreate_fuzzer:fuzztest", "test/fuzztest/crypto_operation/hcfsigncreate_fuzzer:fuzztest", + "test/fuzztest/crypto_operation/hcfsm2create_fuzzer:fuzztest", "test/fuzztest/crypto_operation/hcfverifycreate_fuzzer:fuzztest", "test/fuzztest/key/asykeygenerator_fuzzer:fuzztest", "test/fuzztest/key/dhkeyutil_fuzzer:fuzztest", diff --git a/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/BUILD.gn b/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..22444c0e46b94450c215e48bf72df215b76f88df --- /dev/null +++ b/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/BUILD.gn @@ -0,0 +1,51 @@ +# 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. + +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("HcfKdfCreateFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/security/crypto_framework/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer" + include_dirs = framework_inc_path + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "hcfkdfcreate_fuzzer.cpp" ] + + external_deps = [ + "bounds_checking_function:libsec_shared", + "c_utils:utils", + "crypto_framework:crypto_framework_lib", + "hilog:libhilog", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":HcfKdfCreateFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/corpus/init b/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..f0031afdfd7dfa1743d23040a8528429572f9d69 --- /dev/null +++ b/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/hcfkdfcreate_fuzzer.cpp b/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/hcfkdfcreate_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..589ed93ca33afda76b88703aa9e4e6e29e7770d7 --- /dev/null +++ b/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/hcfkdfcreate_fuzzer.cpp @@ -0,0 +1,152 @@ +/* + * 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. + */ + +#include "hcfkdfcreate_fuzzer.h" + +#include +#include +#include +#include "securec.h" +#include "detailed_hkdf_params.h" +#include "detailed_pbkdf2_params.h" +#include "object_base.h" +#include "blob.h" +#include "kdf.h" +#include "result.h" + +namespace OHOS { + static bool g_testFlag = true; + static const char *g_testHkdfAlg[] = { "HKDF|SHA1", "HKDF|SHA224", "HKDF|SHA256", "HKDF|SHA384", "HKDF|SHA512", + "HKDF|SM3" }; + static const char *g_testPbkdfAlg[] = { "PBKDF2|SHA1", "PBKDF2|SHA224", "PBKDF2|SHA256", "PBKDF2|SHA384", + "PBKDF2|SHA512", "PBKDF2|SM3" }; + static const char *KEY_DATA = "012345678901234567890123456789"; + static const char *INFO_DATA = "infostring"; + static const char *SALT_DATA = "saltstring"; + + static const char *g_password = "123456"; + + constexpr uint32_t OUT_PUT_MAX_LENGTH = 128; + constexpr uint32_t OUT_PUT_NORMAL_LENGTH = 32; + constexpr uint32_t SALT_NORMAL_LENGTH = 16; + static void TestHkdfGenerateSecretWithoutInfo(const char *kdfAlg) + { + HcfKdf *generator = nullptr; + HcfResult ret = HcfKdfCreate(kdfAlg, &generator); + if (ret != HCF_SUCCESS) { + return; + } + uint8_t out[OUT_PUT_MAX_LENGTH] = {0}; + HcfBlob output = {.data = out, .len = OUT_PUT_NORMAL_LENGTH}; + HcfBlob salt = {.data = reinterpret_cast(const_cast(SALT_DATA)), + .len = strlen(SALT_DATA)}; + HcfBlob key = {.data = reinterpret_cast(const_cast(KEY_DATA)), + .len = strlen(KEY_DATA)}; + HcfBlob info = {.data = reinterpret_cast(const_cast(INFO_DATA)), + .len = strlen(INFO_DATA)}; + HcfHkdfParamsSpec params = { + .base = { .algName = "HKDF", }, + .key = key, + .salt = salt, + .info = info, + .output = output, + }; + if (generator != nullptr) { + generator->generateSecret(generator, &(params.base)); + } + HcfObjDestroy(generator); + } + + static void TestPbkdfGenerateSecretWithoutInfo(const char *kdfAlg) + { + HcfKdf *generator = nullptr; + HcfResult ret = HcfKdfCreate(kdfAlg, &generator); + if (ret != HCF_SUCCESS) { + return; + } + uint8_t out[OUT_PUT_MAX_LENGTH] = {0}; + uint8_t saltData[SALT_NORMAL_LENGTH] = {0}; + HcfBlob output = {.data = out, .len = OUT_PUT_NORMAL_LENGTH}; + HcfBlob salt = {.data = saltData, .len = SALT_NORMAL_LENGTH}; + HcfBlob password = {.data = reinterpret_cast(const_cast(g_password)), + .len = strlen(g_password)}; + HcfPBKDF2ParamsSpec params = { + .base.algName = "PBKDF2", + .password = password, + .salt = salt, + .iterations = 10000, + .output = output, + }; + ret = generator->generateSecret(generator, &(params.base)); + HcfObjDestroy(generator); + } + + static void TestGetOneAlgoName(const char *kdfAlg) + { + HcfKdf *generator = nullptr; + HcfResult ret = HcfKdfCreate(kdfAlg, &generator); + if (ret != HCF_SUCCESS) { + return; + } + if (generator != nullptr) { + generator->getAlgorithm(generator); + } + } + + static void TestGenerateSecret(void) + { + for (size_t i = 0; i < sizeof(g_testHkdfAlg) / sizeof(g_testHkdfAlg[0]); i++) { + TestHkdfGenerateSecretWithoutInfo(g_testHkdfAlg[i]); + } + for (size_t i = 0; i < sizeof(g_testHkdfAlg) / sizeof(g_testHkdfAlg[0]); i++) { + TestPbkdfGenerateSecretWithoutInfo(g_testHkdfAlg[i]); + } + } + + static void TestGetAlgoName(void) + { + for (size_t i = 0; i < sizeof(g_testHkdfAlg) / sizeof(g_testHkdfAlg[0]); i++) { + TestGetOneAlgoName(g_testHkdfAlg[i]); + } + for (size_t i = 0; i < sizeof(g_testPbkdfAlg) / sizeof(g_testPbkdfAlg[0]); i++) { + TestGetOneAlgoName(g_testPbkdfAlg[i]); + } + } + + bool HcfKdfCreateFuzzTest(const uint8_t* data, size_t size) + { + if (g_testFlag) { + TestGenerateSecret(); + TestGetAlgoName(); + g_testFlag = false; + } + HcfKdf *generator = nullptr; + std::string algoName(reinterpret_cast(data), size); + HcfResult res = HcfKdfCreate(algoName.c_str(), &generator); + if (res != HCF_SUCCESS) { + return false; + } + HcfObjDestroy(generator); + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::HcfKdfCreateFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/hcfkdfcreate_fuzzer.h b/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/hcfkdfcreate_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..50e3a846c07270f56bd5a8c876ef9e0a2e4615c0 --- /dev/null +++ b/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/hcfkdfcreate_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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_KDF_CREATE_FUZZER_H +#define HCF_KDF_CREATE_FUZZER_H + +#define FUZZ_PROJECT_NAME "hcfkdfcreate_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/project.xml b/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..5e3b584f39efcde7a43e48c9d95f03e55efb9985 --- /dev/null +++ b/test/fuzztest/crypto_operation/hcfkdfcreate_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/BUILD.gn b/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..5c344b03b001194155966dfeca119aade70e9e5f --- /dev/null +++ b/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/BUILD.gn @@ -0,0 +1,51 @@ +# 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. + +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("HcfSm2CreateFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/security/crypto_framework/test/fuzztest/crypto_operation/hcfsm2create_fuzzer" + include_dirs = framework_inc_path + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "hcfsm2create_fuzzer.cpp" ] + + external_deps = [ + "bounds_checking_function:libsec_shared", + "c_utils:utils", + "crypto_framework:crypto_framework_lib", + "hilog:libhilog", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":HcfSm2CreateFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/corpus/init b/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..f0031afdfd7dfa1743d23040a8528429572f9d69 --- /dev/null +++ b/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/hcfsm2create_fuzzer.cpp b/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/hcfsm2create_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e51d47593f89172a55fbeb48996d8d7dabfd5cc3 --- /dev/null +++ b/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/hcfsm2create_fuzzer.cpp @@ -0,0 +1,144 @@ +/* + * 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. + */ + +#include "hcfsm2create_fuzzer.h" + +#include +#include +#include +#include "securec.h" + +#include "blob.h" +#include "memory.h" +#include "sm2_crypto_params.h" +#include "sm2_crypto_util.h" +#include "result.h" + +namespace OHOS { + static bool g_testFlag = true; + static const char *g_sm2ModeC1C3C2 = "C1C3C2"; + static const int INPUT_LEN = 121; + static uint8_t g_input[INPUT_LEN] = { + 48, 119, 2, 33, 0, 183, 70, 70, 149, 188, 64, 6, 110, 236, 85, 149, 216, 224, 102, 95, 92, 41, 105, 232, 5, + 248, 122, 21, 174, 43, 226, 221, 104, 82, 88, 153, 45, 2, 32, 96, 229, 78, 209, 233, 110, 5, 149, 91, 110, + 109, 181, 17, 75, 109, 146, 128, 170, 113, 205, 158, 193, 156, 90, 110, 40, 18, 119, 247, 198, 93, 107, 4, + 32, 87, 167, 167, 247, 88, 146, 203, 234, 83, 126, 117, 129, 52, 142, 82, 54, 152, 226, 201, 111, 143, 115, + 169, 125, 128, 42, 157, 31, 114, 198, 109, 244, 4, 14, 100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 10, + 65, 123 + }; + static HcfBlob g_correctInput = { + .data = g_input, + .len = INPUT_LEN + }; + static const int X_COORDINATE_LEN = 32; + static unsigned char g_xCoordinate[] = { + 45, 153, 88, 82, 104, 221, 226, 43, 174, 21, 122, 248, 5, 232, 105, 41, 92, 95, 102, 224, 216, 149, 85, 236, + 110, 6, 64, 188, 149, 70, 70, 183 + }; + static const int Y_COORDINATE_LEN = 32; + static unsigned char g_yCoordinate[] = { + 107, 93, 198, 247, 119, 18, 40, 110, 90, 156, 193, 158, 205, 113, 170, 128, 146, 109, 75, 17, 181, 109, 110, + 91, 149, 5, 110, 233, 209, 78, 229, 96 + }; + static const int HASH_DATA_LEN = 32; + static unsigned char g_hashData[] = { + 87, 167, 167, 247, 88, 146, 203, 234, 83, 126, 117, 129, 52, 142, 82, 54, 152, 226, 201, 111, 143, 115, 169, + 125, 128, 42, 157, 31, 114, 198, 109, 244 + }; + static const int CIPHER_TEXT_DATA_LEN = 14; + static unsigned char g_cipherTextData[] = { + 100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 10, 65, 123 + }; + + HcfResult ConstructCorrectSm2CipherTextSpec(Sm2CipherTextSpec **spec) + { + Sm2CipherTextSpec *tempSpec = static_cast(HcfMalloc(sizeof(Sm2CipherTextSpec), 0)); + if (tempSpec == nullptr) { + return HCF_ERR_MALLOC; + } + tempSpec->xCoordinate.data = g_xCoordinate; + tempSpec->xCoordinate.len = X_COORDINATE_LEN; + tempSpec->yCoordinate.data = g_yCoordinate; + tempSpec->yCoordinate.len = Y_COORDINATE_LEN; + tempSpec->cipherTextData.data = g_cipherTextData; + tempSpec->cipherTextData.len = CIPHER_TEXT_DATA_LEN; + tempSpec->hashData.data = g_hashData; + tempSpec->hashData.len = HASH_DATA_LEN; + *spec = tempSpec; + return HCF_SUCCESS; + } + + static void TestHcfGenCipherTextBySpec() + { + int res = 0; + Sm2CipherTextSpec *spec = nullptr; + res = ConstructCorrectSm2CipherTextSpec(&spec); + if (res != HCF_SUCCESS) { + return; + } + HcfBlob output = { .data = nullptr, .len = 0 }; + res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output); + if (res != HCF_SUCCESS) { + HcfFree(spec); + return; + } + res = memcmp(output.data, g_correctInput.data, g_correctInput.len); + HcfBlobDataFree(&output); + HcfFree(spec); + } + + static void TestHcfGetCipherTextSpec() + { + Sm2CipherTextSpec *spec = nullptr; + HcfResult res = HcfGetCipherTextSpec(&g_correctInput, g_sm2ModeC1C3C2, &spec); + if (res != HCF_SUCCESS) { + return; + } + DestroySm2CipherTextSpec(spec); + } + + bool HcfSm2CreateFuzzTest(const uint8_t* data, size_t size) + { + if (g_testFlag) { + TestHcfGenCipherTextBySpec(); + TestHcfGetCipherTextSpec(); + g_testFlag = false; + } + + Sm2CipherTextSpec spec = {}; + spec.xCoordinate.data = g_xCoordinate; + spec.xCoordinate.len = X_COORDINATE_LEN; + spec.yCoordinate.data = g_yCoordinate; + spec.yCoordinate.len = Y_COORDINATE_LEN; + spec.cipherTextData.data = const_cast(data); + spec.cipherTextData.len = size; + spec.hashData.data = g_hashData; + spec.hashData.len = HASH_DATA_LEN; + + HcfBlob output = { .data = nullptr, .len = 0 }; + HcfGenCipherTextBySpec(&spec, g_sm2ModeC1C3C2, &output); + + HcfBlobDataFree(&output); + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::HcfSm2CreateFuzzTest(data, size); + return 0; +} diff --git a/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/hcfsm2create_fuzzer.h b/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/hcfsm2create_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..2948191f12bd60dad0776ac30daaff5737ac1388 --- /dev/null +++ b/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/hcfsm2create_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_SM2_CREATE_FUZZER_H +#define HCF_SM2_CREATE_FUZZER_H + +#define FUZZ_PROJECT_NAME "hcfsm2create_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/project.xml b/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..5e3b584f39efcde7a43e48c9d95f03e55efb9985 --- /dev/null +++ b/test/fuzztest/crypto_operation/hcfsm2create_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +