Feat: added Nullchecks for malloc/calloc

This commit is contained in:
finn 2022-07-18 21:28:38 +02:00
parent 88ee99b37c
commit fda4ee592d
4 changed files with 36 additions and 44 deletions

View File

@ -34,6 +34,10 @@ uint8_t* read_file(const char* path, size_t* size) {
}
uint8_t* data = malloc(statOfFile.st_size);
if (data == NULL) {
printf("Failure: malloc returns NULL");
return -1;
}
size_t bytesRead =
fread(data, statOfFile.st_blksize, statOfFile.st_blocks, f);

View File

@ -25,7 +25,10 @@ void md2_hash_0(size_t len, const uint8_t buf[len], uint8_t out[16]) {
// +16 for the checksum
uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t));
// TODO: null check
if (newBuf == NULL) {
printf("Failure: calloc returns NULL");
return -1;
}
memcpy(newBuf, buf, len - paddingNeeded);
// printBuf(len + 16, newBuf);
@ -45,7 +48,11 @@ void md2_hash_0(size_t len, const uint8_t buf[len], uint8_t out[16]) {
// === step 3 ===
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
// TODO: add null check
if (messageDigestBuf == NULL) {
printf("Failure: calloc returns NULL");
return -1;
}
// === step 4 ===
// <= because we need to hash the last block too

View File

@ -45,74 +45,45 @@ void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16])
int paddingNeeded = 16 - (len & 15);
len += paddingNeeded;
// printf("len: %d\n", len);
// +16 for the checksum
uint8_t* newBuf = aligned_alloc(16, sizeof(uint8_t)*(len + 16));
if (newBuf == NULL) {
printf("Failure: aligned_alloc returns NULL");
return -1;
}
for(size_t i = 0; i < 16; i++) {
newBuf[len + i] = 0;
}
// uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t));
// TODO: null check
memcpy(newBuf, buf, len - paddingNeeded);
//md2_print_buf(len + 16, newBuf);
memcpy(newBuf + len - paddingNeeded, PADDING + paddingNeeded, paddingNeeded);
// printf("buf with padding: ");
//md2_print_buf(len + 16, newBuf);
// === step 2 ===
md2_checksum_1(len, newBuf);
// printf("buf with cecksum: ");
// printBuf(len + 16, newBuf);
// === step 3 ===
uint8_t *messageDigestBuf = aligned_alloc(16, sizeof(uint8_t) * 48);
// TODO: add null check
if (messageDigestBuf == NULL) {
printf("Failure: aligned_alloc returns NULL");
return -1;
}
for (size_t i = 0; i < 48; i++) {
messageDigestBuf[i] = 0;
}
// === step 4 ===
// <= because we need to hash the last block too
__m128i vx;
__m128i vy;
for (size_t i = 0; i <= (len + 16) / 16 - 1; i++)
{
vx = _mm_load_si128((__m128i*) (newBuf + i * 16));
_mm_store_si128((__m128i*) (messageDigestBuf + 16), vx);
vy = _mm_load_si128((__m128i*) (messageDigestBuf));
vy = _mm_xor_si128(vy, vx);
_mm_store_si128((__m128i*) (messageDigestBuf + 32), vy);
/*
for (int j = 0; j < 16; j++)
{
//messageDigestBuf[16 + j] = newBuf[i * 16 + j];
messageDigestBuf[32 + j] =
(messageDigestBuf[16 + j] ^ messageDigestBuf[j]);
}
/*
vy = _mm_load_si128((__m128i*) (messageDigestBuf));
_mm_xor_si128(vy, vx);
_mm_store_si128((__m128i*) messageDigestBuf, vy);
md2_print_buf(48, messageDigestBuf);
md2_print_buf(len + 16, newBuf);
/*
printf("newBuf: erstes Element:");
printf(*(newBuf + i * 16));
printf("mdb:");
printf(*(messageDigestBuf + 16));
*/
u_int8_t t = 0;
@ -125,8 +96,6 @@ void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16])
t = (t + j) & 255;
}
}
// printf("messageDigestBuf: \n");
// printBuf(16, messageDigestBuf);
memcpy(out, messageDigestBuf, 16);

View File

@ -50,12 +50,24 @@ void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) {
// === step 3 ===
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
if (messageDigestBuf == NULL) {
printf("Failure: calloc returns NULL");
return -1;
}
// === step 4 ===
uint8_t l = 0;
uint8_t* checksum = calloc(16, sizeof(uint8_t));
if (checksum == NULL) {
printf("Failure: calloc returns NULL");
return -1;
}
uint8_t* data = malloc(16);
if (data == NULL) {
printf("Failure: malloc returns NULL");
return -1;
}
size_t bytes_left_to_read = file_stat.st_size;
size_t bytes_left_to_process = 0;