修改fuzz问题

Signed-off-by: lcc <lichaochen@huawei.com>
This commit is contained in:
lcc 2024-11-27 17:48:53 +08:00
parent bc8d9a195b
commit 56db7bc6f7

View File

@ -25,15 +25,16 @@
#include "cipher.h"
#include "mac.h"
#include "result.h"
#include "memory.h"
#include "sym_key_generator.h"
#include "detailed_gcm_params.h"
namespace OHOS {
static int32_t AesEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input,
uint8_t *cipherText, int *cipherTextLen)
uint8_t **cipherText, int *cipherTextLen)
{
HcfBlob output = {};
int32_t maxLen = *cipherTextLen;
int32_t maxLen = input->len + 16;
int32_t ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr);
if (ret != 0) {
return ret;
@ -41,11 +42,15 @@ namespace OHOS {
ret = cipher->update(cipher, input, &output);
if (ret != 0) {
if (output.data != nullptr) {
HcfBlobDataClearAndFree(&output);
}
return ret;
}
*cipherTextLen = output.len;
*cipherText = static_cast<uint8_t *>(HcfMalloc(maxLen, 0));
if (output.len > 0 && output.data != nullptr) {
(void)memcpy_s(cipherText, maxLen, output.data, output.len);
(void)memcpy_s(*cipherText, maxLen, output.data, output.len);
}
if (output.data != nullptr) {
HcfBlobDataClearAndFree(&output);
@ -53,10 +58,11 @@ namespace OHOS {
}
ret = cipher->doFinal(cipher, nullptr, &output);
if (ret != 0) {
HcfBlobDataClearAndFree(&output);
return ret;
}
if (output.len > 0 && output.data != nullptr) {
(void)memcpy_s(cipherText + *cipherTextLen, maxLen - *cipherTextLen, output.data, output.len);
(void)memcpy_s(*cipherText + *cipherTextLen, output.len, output.data, output.len);
}
*cipherTextLen += output.len;
if (output.data != nullptr) {
@ -66,55 +72,62 @@ namespace OHOS {
return 0;
}
static int32_t AesDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input,
uint8_t *cipherText, int cipherTextLen)
static int32_t AesDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, uint8_t *cipherText, int cipherTextLen)
{
HcfBlob output = {};
HcfBlob decryptedData = {};
if (cipherTextLen <= 0) {
return -1;
}
int32_t maxLen = cipherTextLen;
int32_t ret = cipher->init(cipher, DECRYPT_MODE, &(key->key), nullptr);
if (ret != 0) {
return ret;
}
ret = cipher->update(cipher, input, &output);
HcfBlob cipherBlob = { .data = cipherText, .len = cipherTextLen };
ret = cipher->update(cipher, &cipherBlob, &output);
if (ret != 0) {
return ret;
goto EXIT;
}
decryptedData.data = (uint8_t *)HcfMalloc(cipherTextLen, 0);
if (decryptedData.data == nullptr) {
ret = -1;
goto EXIT;
}
if (output.len > 0 && output.data != nullptr) {
(void)memcpy_s(cipherText, maxLen, output.data, output.len);
}
cipherTextLen = output.len;
if (output.data != nullptr) {
HcfBlobDataClearAndFree(&output);
output.data = nullptr;
output.len = 0;
(void)memcpy_s(decryptedData.data, cipherTextLen, output.data, output.len);
decryptedData.len = output.len;
}
HcfBlobDataClearAndFree(&output);
ret = cipher->doFinal(cipher, nullptr, &output);
if (ret != 0) {
return ret;
goto EXIT;
}
if (output.len > 0 && output.data != nullptr) {
(void)memcpy_s(cipherText + cipherTextLen, maxLen - cipherTextLen, output.data, output.len);
(void)memcpy_s(decryptedData.data + decryptedData.len, output.len, output.data, output.len);
decryptedData.len += output.len;
}
cipherTextLen += output.len;
if (decryptedData.len != input->len - 1 || memcmp(decryptedData.data, input->data, decryptedData.len) != 0) {
ret = 1;
} else {
ret = 0;
}
EXIT:
if (output.data != nullptr) {
HcfBlobDataClearAndFree(&output);
output.data = nullptr;
output.len = 0;
}
ret = memcmp(cipherText, input->data, cipherTextLen);
ret = ret || (cipherTextLen == input->len - 1) ? 0 : 1;
if (decryptedData.data != nullptr) {
HcfBlobDataClearAndFree(&decryptedData);
}
return ret;
}
static int32_t Sm4Encrypt(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input,
uint8_t *cipherText, int *cipherTextLen)
uint8_t **cipherText, int *cipherTextLen)
{
HcfBlob output = {};
int32_t maxLen = *cipherTextLen;
int32_t maxLen = input->len + 16;
int32_t ret = cipher->init(cipher, ENCRYPT_MODE, reinterpret_cast<HcfKey *>(key), nullptr);
if (ret != 0) {
return ret;
@ -122,11 +135,15 @@ namespace OHOS {
ret = cipher->update(cipher, input, &output);
if (ret != 0) {
if (output.data != nullptr) {
HcfBlobDataClearAndFree(&output);
}
return ret;
}
*cipherTextLen = output.len;
*cipherText = static_cast<uint8_t *>(HcfMalloc(maxLen, 0));
if (output.data != nullptr) {
if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) {
if (memcpy_s(*cipherText, output.len, output.data, output.len) != EOK) {
HcfBlobDataFree(&output);
return -1;
}
@ -135,10 +152,11 @@ namespace OHOS {
ret = cipher->doFinal(cipher, nullptr, &output);
if (ret != 0) {
HcfFree(*cipherText);
return ret;
}
if (output.data != nullptr) {
if (memcpy_s(cipherText + *cipherTextLen, maxLen - *cipherTextLen, output.data, output.len) != EOK) {
if (output.len > 0 && output.data != nullptr) {
if (memcpy_s(*cipherText + *cipherTextLen, output.len, output.data, output.len) != EOK) {
HcfBlobDataFree(&output);
return -1;
}
@ -152,53 +170,67 @@ namespace OHOS {
uint8_t *cipherText, int cipherTextLen)
{
HcfBlob output = {};
HcfBlob decryptedData = {};
int32_t ret;
if (cipherTextLen <= 0) {
return -1;
}
int32_t maxLen = cipherTextLen;
int32_t ret = cipher->init(cipher, DECRYPT_MODE, reinterpret_cast<HcfKey *>(key), nullptr);
ret = cipher->init(cipher, DECRYPT_MODE, reinterpret_cast<HcfKey *>(key), nullptr);
if (ret != 0) {
return ret;
}
ret = cipher->update(cipher, input, &output);
HcfBlob cipherBlob = { .data = cipherText, .len = cipherTextLen };
ret = cipher->update(cipher, &cipherBlob, &output);
if (ret != 0) {
return ret;
}
cipherTextLen = output.len;
if (output.data != nullptr) {
if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) {
HcfBlobDataFree(&output);
return -1;
}
HcfBlobDataFree(&output);
goto EXIT;
}
decryptedData.data = (uint8_t *)HcfMalloc(cipherTextLen, 0);
if (decryptedData.data == nullptr) {
ret = -1;
goto EXIT;
}
if (output.len > 0 && output.data != nullptr) {
(void)memcpy_s(decryptedData.data, output.len, output.data, output.len);
decryptedData.len = output.len;
}
HcfBlobDataClearAndFree(&output);
ret = cipher->doFinal(cipher, nullptr, &output);
if (ret != 0) {
return ret;
}
if (output.data != nullptr) {
if (memcpy_s(cipherText + cipherTextLen, maxLen - cipherTextLen, output.data, output.len) != EOK) {
HcfBlobDataFree(&output);
return -1;
}
cipherTextLen += output.len;
HcfBlobDataFree(&output);
goto EXIT;
}
if (cipherTextLen != input->len - 1) {
return -1;
if (output.len > 0 && output.data != nullptr) {
(void)memcpy_s(decryptedData.data + decryptedData.len, output.len, output.data, output.len);
decryptedData.len += output.len;
}
return memcmp(cipherText, input->data, cipherTextLen);
if (decryptedData.len != input->len - 1 ||
memcmp(decryptedData.data, input->data, decryptedData.len) != 0) {
ret = 1;
} else {
ret = 0;
}
EXIT:
if (output.data != nullptr) {
HcfBlobDataClearAndFree(&output);
}
if (decryptedData.data != nullptr) {
HcfBlobDataClearAndFree(&decryptedData);
}
return ret;
}
static void TestAesCipher(const uint8_t* plan, size_t size)
{
int ret = 0;
HcfBlob input = {.data = const_cast<uint8_t *>(plan), .len = size};
uint8_t cipherText[128] = {0};
int cipherTextLen = 128;
uint8_t *cipherText = nullptr;
int cipherTextLen = 0;
HcfSymKeyGenerator *generator = nullptr;
HcfCipher *cipher = nullptr;
HcfSymKey *key = nullptr;
@ -217,9 +249,9 @@ namespace OHOS {
HcfObjDestroy(key);
return;
}
(void)AesEncrypt(cipher, key, &input, cipherText, &cipherTextLen);
(void)AesEncrypt(cipher, key, &input, &cipherText, &cipherTextLen);
(void)AesDecrypt(cipher, key, &input, cipherText, cipherTextLen);
HcfFree(cipherText);
HcfObjDestroy(generator);
HcfObjDestroy(key);
HcfObjDestroy(cipher);
@ -229,8 +261,8 @@ namespace OHOS {
{
int ret = 0;
HcfBlob input = {.data = const_cast<uint8_t *>(plan), .len = size};
uint8_t cipherText[128] = {0};
int cipherTextLen = 128;
uint8_t *cipherText = nullptr;
int cipherTextLen = 0;
HcfSymKeyGenerator *generator = nullptr;
HcfCipher *cipher = nullptr;
HcfSymKey *key = nullptr;
@ -250,8 +282,9 @@ namespace OHOS {
return;
}
(void)Sm4Encrypt(cipher, key, &input, cipherText, &cipherTextLen);
(void)Sm4Encrypt(cipher, key, &input, &cipherText, &cipherTextLen);
(void)Sm4Decrypt(cipher, key, &input, cipherText, cipherTextLen);
HcfFree(cipherText);
HcfObjDestroy(generator);
HcfObjDestroy(key);
HcfObjDestroy(cipher);
@ -264,8 +297,8 @@ namespace OHOS {
uint8_t aad[8] = {0};
uint8_t tag[16] = {0};
uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
uint8_t cipherText[128] = {0};
int cipherTextLen = 128;
uint8_t *cipherText = nullptr;
int cipherTextLen = 0;
HcfGcmParamsSpec spec = {};
spec.aad.data = aad;
@ -293,7 +326,7 @@ namespace OHOS {
return;
}
(void)Sm4Encrypt(cipher, key, &input, cipherText, &cipherTextLen);
(void)Sm4Encrypt(cipher, key, &input, &cipherText, &cipherTextLen);
(void)Sm4Decrypt(cipher, key, &input, cipherText, cipherTextLen);
HcfObjDestroy(generator);
HcfObjDestroy(key);
@ -335,6 +368,7 @@ namespace OHOS {
if (res != HCF_SUCCESS) {
HcfObjDestroy(generator);
HcfObjDestroy(keyPair);
HcfBlobDataClearAndFree(&encoutput);
return;
}
(void)cipher->init(cipher, DECRYPT_MODE, reinterpret_cast<HcfKey *>(keyPair->priKey), nullptr);