gra-projekt/Implementierung/src/md2_impls/md2_2.c

79 lines
1.9 KiB
C
Raw Normal View History

#include "../../lib/md2_impls/md2_2.h"
#include "../../lib/md2_impls/md2_common.h"
void process_block_hash(uint8_t block[16], uint8_t messageDigestBuf[48]) {
2022-07-20 23:23:18 +02:00
md2_first_loop(block, messageDigestBuf, 0);
md2_second_loop(messageDigestBuf);
}
2022-07-12 22:33:01 +02:00
void apply_padding(size_t len, uint8_t buf[16]) {
int paddingNeeded = 16 - (len % 16);
uint8_t originalPadding = paddingNeeded;
len += paddingNeeded;
while (paddingNeeded > 0) {
2022-07-12 22:33:01 +02:00
buf[len - paddingNeeded] = originalPadding;
paddingNeeded--;
}
2022-07-12 22:33:01 +02:00
}
void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) {
FILE* file;
struct stat file_stat;
2022-07-12 22:36:26 +02:00
if (!open_file((char*)buf, &file, &file_stat)) {
return;
}
// === step 3 ===
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
if (messageDigestBuf == NULL) {
2022-07-20 12:11:16 +02:00
return;
}
// === step 4 ===
uint8_t l = 0;
2022-07-12 16:02:30 +02:00
uint8_t* checksum = calloc(16, sizeof(uint8_t));
if (checksum == NULL) {
2022-07-20 12:16:01 +02:00
return;
}
2022-07-12 16:02:30 +02:00
2022-07-12 22:33:01 +02:00
uint8_t* data = malloc(16);
if (data == NULL) {
2022-07-20 12:16:01 +02:00
return;
}
2022-07-12 22:33:01 +02:00
size_t bytes_left_to_read = file_stat.st_size;
size_t bytes_left_to_process = 0;
2022-07-12 22:33:01 +02:00
while (bytes_left_to_read != 0) {
bytes_left_to_process = bytes_left_to_read >= 16 ? 16 : bytes_left_to_read;
2022-07-20 10:36:19 +02:00
size_t size = fread(data, 1, bytes_left_to_process, file);
if (size == 0 || ferror(file) || feof(file)) {
if (errno == 0) errno = EIO;
2022-07-12 22:33:01 +02:00
return;
}
2022-07-20 17:46:35 +02:00
CHECKSUM_START_MARK
2022-07-20 23:23:18 +02:00
md2_process_block_checksum(data, checksum, &l);
2022-07-20 17:46:35 +02:00
CHECKSUM_END_MARK
2022-07-12 22:33:01 +02:00
process_block_hash(data, messageDigestBuf);
bytes_left_to_read -= bytes_left_to_process;
};
2022-07-12 22:33:01 +02:00
apply_padding(bytes_left_to_process % 16, data);
2022-07-20 23:23:18 +02:00
md2_process_block_checksum(data, checksum, &l);
2022-07-12 22:33:01 +02:00
process_block_hash(data, messageDigestBuf);
process_block_hash(checksum, messageDigestBuf);
2022-07-20 17:46:35 +02:00
END_MARK
memcpy(out, messageDigestBuf, 16);
2022-07-20 17:46:35 +02:00
fclose(file);
2022-07-12 22:33:01 +02:00
free(data);
2022-07-12 16:02:30 +02:00
free(messageDigestBuf);
free(checksum);
}