1 /*
2 * Copyright (c) 2025 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 <cstddef>
17 #include <cstdint>
18 #include <memory>
19 #include <string>
20 #include <unistd.h>
21 #include <unordered_map>
22 #include <fuzzer/FuzzedDataProvider.h>
23
24 #include "crypto_mgr.h"
25 #include "crypto_mgr_fuzzer.h"
26 #include "dm_constants.h"
27 #include "json_object.h"
28
29
30 namespace OHOS {
31 namespace DistributedHardware {
32
33 namespace {
34 constexpr uint32_t CIPHER_TEXT_LEN = 128;
35 constexpr uint32_t PLAIN_TEXT_SIZE = 5;
36 constexpr uint32_t PLAIN_LEN = 50;
37 constexpr uint32_t CIPHER_TEXT_SIZE = 30;
38 constexpr uint32_t KEY_LEN = 1024;
39 }
40
41 std::shared_ptr<CryptoMgr> cryproMgr_ = std::make_shared<CryptoMgr>();
CryptoMgrFuzzTest(const uint8_t * data,size_t size)42 void CryptoMgrFuzzTest(const uint8_t* data, size_t size)
43 {
44 if ((data == nullptr) || (size < sizeof(int32_t))) {
45 return;
46 }
47 FuzzedDataProvider fdp(data, size);
48 std::string inputMsg(reinterpret_cast<const char*>(data), size);
49 std::string outputMsg(reinterpret_cast<const char*>(data), size);
50 cryproMgr_->EncryptMessage(inputMsg, outputMsg);
51 cryproMgr_->DecryptMessage(inputMsg, outputMsg);
52
53 AesGcmCipherKey *cipherKey = nullptr;
54 unsigned char *input = nullptr;
55 uint32_t inLen = 0;
56 unsigned char *encryptData = nullptr;
57 uint32_t *encryptLen = nullptr;
58 cryproMgr_->DoEncryptData(cipherKey, input, inLen, encryptData, encryptLen);
59
60 AesGcmCipherKey cipherKeyPro;
61 cipherKey = &cipherKeyPro;
62 std::string cipherStr(reinterpret_cast<const char*>(data), size);
63 input = reinterpret_cast<unsigned char *>(cipherStr.data());
64 inLen = static_cast<uint32_t>(cipherStr.length());
65 std::string encryStr(reinterpret_cast<const char*>(data), size);
66 encryptData = reinterpret_cast<unsigned char *>(encryStr.data());
67 uint32_t encryNum = static_cast<uint32_t>(encryStr.length());
68 encryptLen = &encryNum;
69 cryproMgr_->DoEncryptData(cipherKey, input, inLen, encryptData, encryptLen);
70
71 unsigned char *randStr = nullptr;
72 uint32_t len = 0;
73 cryproMgr_->GenerateRandomArray(randStr, len);
74 std::string randStrTemp(reinterpret_cast<const char*>(data), size);
75 randStr = reinterpret_cast<unsigned char *>(randStrTemp.data());
76 len = static_cast<uint32_t>(randStrTemp.length());
77 cryproMgr_->GenerateRandomArray(randStr, len);
78 }
79
CryptoMgrFirstFuzzTest(const uint8_t * data,size_t size)80 void CryptoMgrFirstFuzzTest(const uint8_t* data, size_t size)
81 {
82 if ((data == nullptr) || (size < sizeof(int32_t))) {
83 return;
84 }
85 AesGcmCipherKey *cipherKey = nullptr;
86 unsigned char *plainText = nullptr;
87 uint32_t plainTextSize = 0;
88 unsigned char *cipherText = nullptr;
89 uint32_t cipherTextLen = 0;
90 uint32_t cipherTextSize = 0;
91 unsigned char *plain = nullptr;
92 uint32_t plainLen = 0;
93 cryproMgr_->MbedAesGcmEncrypt(cipherKey, plainText, plainTextSize, cipherText, cipherTextLen);
94 cryproMgr_->MbedAesGcmDecrypt(cipherKey, cipherText, cipherTextSize, plain, plainLen);
95 AesGcmCipherKey cipherKeyPro;
96 cipherKey = &cipherKeyPro;
97 char plainTextTemp[PLAIN_TEXT_SIZE] = {1};
98 plainText = reinterpret_cast<unsigned char *>(plainTextTemp);
99 plainTextSize = PLAIN_TEXT_SIZE;
100 char cipherTextTemp[CIPHER_TEXT_LEN] = {11};
101 cipherText = reinterpret_cast<unsigned char*>(cipherTextTemp);
102 cipherTextLen = CIPHER_TEXT_LEN;
103 cryproMgr_->MbedAesGcmEncrypt(cipherKey, plainText, plainTextSize, cipherText, cipherTextLen);
104
105 char cipherTextInfo[CIPHER_TEXT_SIZE] = {10};
106 cipherText = reinterpret_cast<unsigned char*>(cipherTextInfo);
107 cipherTextSize = CIPHER_TEXT_SIZE;
108 char plainTemp[PLAIN_LEN] = {12};
109 plain = reinterpret_cast<unsigned char*>(plainTemp);
110 plainLen = PLAIN_LEN;
111 cryproMgr_->MbedAesGcmDecrypt(cipherKey, cipherText, cipherTextSize, plain, plainLen);
112 }
113
114
CryptoMgrSecondFuzzTest(const uint8_t * data,size_t size)115 void CryptoMgrSecondFuzzTest(const uint8_t* data, size_t size)
116 {
117 if ((data == nullptr) || (size < sizeof(int32_t))) {
118 return;
119 }
120
121 AesGcmCipherKey *cipherKey = nullptr;
122 unsigned char *input = nullptr;
123 uint32_t inLen = 0;
124 unsigned char *decryptData = nullptr;
125 uint32_t *decryptLen = nullptr;
126 cryproMgr_->DoDecryptData(cipherKey, input, inLen, decryptData, decryptLen);
127
128 AesGcmCipherKey cipherKeyPro;
129 cipherKey = &cipherKeyPro;
130 std::string inputTemp(reinterpret_cast<const char*>(data), size);
131 input = reinterpret_cast<unsigned char *>(inputTemp.data());
132 inLen = static_cast<uint32_t>(inputTemp.length());
133 std::string decryInfo(reinterpret_cast<const char*>(data), size);
134 decryptData = reinterpret_cast<unsigned char*>(decryInfo.data());
135 uint32_t decryNum = static_cast<uint32_t>(decryInfo.length());
136 decryptLen = &decryNum;
137 cryproMgr_->DoDecryptData(cipherKey, input, inLen, decryptData, decryptLen);
138
139 std::string sessionInfo(reinterpret_cast<const char*>(data), size);
140 uint8_t *sessionKey = reinterpret_cast<uint8_t *>(sessionInfo.data());
141 uint32_t keyLen = static_cast<uint32_t>(sessionInfo.length());
142 cryproMgr_->SaveSessionKey(sessionKey, keyLen);
143 cryproMgr_->ClearSessionKey();
144 keyLen = KEY_LEN;
145 cryproMgr_->SaveSessionKey(sessionKey, keyLen);
146 }
147 }
148 }
149
150 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)151 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
152 {
153 /* Run your code on data */
154 OHOS::DistributedHardware::CryptoMgrFuzzTest(data, size);
155 OHOS::DistributedHardware::CryptoMgrFirstFuzzTest(data, size);
156 OHOS::DistributedHardware::CryptoMgrSecondFuzzTest(data, size);
157 return 0;
158 }
159