1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "asykeygenerator_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21
22 #include "asy_key_generator.h"
23 #include "blob.h"
24 #include "result.h"
25
26 namespace OHOS {
27 static bool g_testFlag = true;
28 static const int ECC224_PUB_KEY_LEN = 80;
29 static const int ECC224_PRI_KEY_LEN = 44;
30 static uint8_t g_mockEcc224PubKey[ECC224_PUB_KEY_LEN] = { 48, 78, 48, 16, 6, 7, 42, 134, 72, 206,
31 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,
32 141, 146, 235, 133, 37, 218, 180, 8, 149, 47, 244, 137, 238, 207, 95, 153, 65, 250, 32, 77, 184, 249, 181,
33 172, 192, 2, 99, 194, 170, 25, 44, 255, 87, 246, 42, 133, 83, 66, 197, 97, 95, 12, 84 };
34
35 static uint8_t g_mockEcc224PriKey[ECC224_PRI_KEY_LEN] = { 48, 42, 2, 1, 1, 4, 28, 250, 86, 6,
36 147, 222, 43, 252, 139, 90, 139, 5, 33, 184, 230, 26, 68, 94, 57, 145, 229, 146, 49, 221, 119, 206, 32, 198,
37 19, 160, 7, 6, 5, 43, 129, 4, 0, 33 };
38
TestEccKey(void)39 static void TestEccKey(void)
40 {
41 HcfAsyKeyGenerator *generator = nullptr;
42 int32_t res = HcfAsyKeyGeneratorCreate("ECC224", &generator);
43 if (res != HCF_SUCCESS) {
44 return;
45 }
46 (void)generator->getAlgoName(generator);
47 HcfKeyPair *keyPair = nullptr;
48 res = generator->generateKeyPair(generator, nullptr, &keyPair);
49 if (res != HCF_SUCCESS) {
50 HcfObjDestroy(generator);
51 return;
52 }
53 HcfKeyPair *convertKeyPair = nullptr;
54 static HcfBlob mockEcc224PubKeyBlob = {
55 .data = g_mockEcc224PubKey,
56 .len = ECC224_PUB_KEY_LEN
57 };
58
59 static HcfBlob mockEcc224PriKeyBlob = {
60 .data = g_mockEcc224PriKey,
61 .len = ECC224_PRI_KEY_LEN
62 };
63 (void)generator->convertKey(generator, nullptr, &mockEcc224PubKeyBlob, &mockEcc224PriKeyBlob, &convertKeyPair);
64 HcfObjDestroy(keyPair);
65 HcfObjDestroy(generator);
66 HcfObjDestroy(convertKeyPair);
67 }
68
TestRsaKey(void)69 static void TestRsaKey(void)
70 {
71 HcfAsyKeyGenerator *generator = nullptr;
72 HcfResult res = HcfAsyKeyGeneratorCreate("RSA1024", &generator);
73 if (res != HCF_SUCCESS) {
74 return;
75 }
76 HcfKeyPair *keyPair = nullptr;
77 res = generator->generateKeyPair(generator, nullptr, &keyPair);
78 if (res != HCF_SUCCESS) {
79 HcfObjDestroy(generator);
80 return;
81 }
82 HcfBlob pubKeyBlob = {.data = nullptr, .len = 0};
83 HcfBlob priKeyBlob = {.data = nullptr, .len = 0};
84 (void)keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &pubKeyBlob);
85 (void)keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &priKeyBlob);
86
87 HcfKeyPair *dupKeyPair = nullptr;
88 (void)generator->convertKey(generator, nullptr, &pubKeyBlob, &priKeyBlob, &dupKeyPair);
89 HcfPubKey *pubKey = dupKeyPair->pubKey;
90 (void)pubKey->base.getAlgorithm(&(pubKey->base));
91 (void)pubKey->base.getFormat(&(pubKey->base));
92 (void)pubKey->base.base.getClass();
93
94 HcfBlobDataFree(&pubKeyBlob);
95 HcfBlobDataFree(&priKeyBlob);
96 HcfObjDestroy(generator);
97 HcfObjDestroy(keyPair);
98 HcfObjDestroy(dupKeyPair);
99 }
100
AsyKeyGeneratorFuzzTest(const uint8_t * data,size_t size)101 bool AsyKeyGeneratorFuzzTest(const uint8_t* data, size_t size)
102 {
103 if (g_testFlag) {
104 TestEccKey();
105 TestRsaKey();
106 g_testFlag = false;
107 }
108 HcfAsyKeyGenerator *generator = nullptr;
109 std::string algoName(reinterpret_cast<const char *>(data), size);
110 HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator);
111 if (res != HCF_SUCCESS) {
112 return false;
113 }
114 HcfObjDestroy(generator);
115 return true;
116 }
117 }
118
119 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)120 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
121 {
122 /* Run your code on data */
123 OHOS::AsyKeyGeneratorFuzzTest(data, size);
124 return 0;
125 }
126