2022-07-12 14:41:18 +02:00
|
|
|
#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]) {
|
|
|
|
for (int j = 0; j < 16; j++) {
|
|
|
|
messageDigestBuf[16 + j] = block[j];
|
|
|
|
messageDigestBuf[32 + j] = (messageDigestBuf[16 + j] ^ messageDigestBuf[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_int8_t t = 0;
|
|
|
|
|
|
|
|
for (int j = 0; j < 18; j++) {
|
|
|
|
for (int k = 0; k < 48; k++) {
|
|
|
|
t = messageDigestBuf[k] = messageDigestBuf[k] ^ MD2_PI_SUBST[t];
|
|
|
|
}
|
|
|
|
t = (t + j) % 256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void process_block_checksum(uint8_t block[16], uint8_t checksum[16],
|
|
|
|
uint8_t* l) {
|
|
|
|
for (int j = 0; j < 16; j++) {
|
|
|
|
u_int8_t c = block[j];
|
|
|
|
// reference is wrong. It says: Set C[j] to S[c xor L]. But it should be:
|
|
|
|
(*l) = checksum[j] ^= MD2_PI_SUBST[c ^ (*l)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:33:01 +02:00
|
|
|
void apply_padding(size_t len, uint8_t buf[16]) {
|
2022-07-12 14:41:18 +02:00
|
|
|
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;
|
2022-07-12 14:41:18 +02:00
|
|
|
paddingNeeded--;
|
|
|
|
}
|
2022-07-12 22:33:01 +02:00
|
|
|
printf("buf with padding: ");
|
|
|
|
md2_print_buf(len, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// unused!
|
|
|
|
void md2_checksum_2(size_t, uint8_t*) {}
|
|
|
|
|
|
|
|
void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) {
|
|
|
|
FILE* file;
|
|
|
|
struct stat file_stat;
|
|
|
|
open_file((char*)buf, &file, &file_stat);
|
2022-07-12 14:41:18 +02:00
|
|
|
|
|
|
|
// === step 3 ===
|
|
|
|
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
|
|
|
|
|
|
|
|
// === step 4 ===
|
|
|
|
uint8_t l = 0;
|
2022-07-12 16:02:30 +02:00
|
|
|
uint8_t* checksum = calloc(16, sizeof(uint8_t));
|
|
|
|
|
2022-07-12 22:33:01 +02:00
|
|
|
uint8_t* data = malloc(16);
|
|
|
|
size_t bytes_left_to_read = file_stat.st_size;
|
|
|
|
size_t bytes_left_to_process = 0;
|
2022-07-12 14:41:18 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
fread(data, 1, bytes_left_to_process, file);
|
|
|
|
if (ferror(file) || feof(file)) {
|
|
|
|
fprintf(stderr, "Error reading the file!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
process_block_checksum(data, checksum, &l);
|
|
|
|
process_block_hash(data, messageDigestBuf);
|
|
|
|
|
|
|
|
bytes_left_to_read -= bytes_left_to_process;
|
|
|
|
};
|
2022-07-12 14:41:18 +02:00
|
|
|
|
2022-07-12 22:33:01 +02:00
|
|
|
apply_padding(bytes_left_to_process % 16, data);
|
|
|
|
process_block_checksum(data, checksum, &l);
|
|
|
|
process_block_hash(data, messageDigestBuf);
|
|
|
|
|
|
|
|
process_block_hash(checksum, messageDigestBuf);
|
2022-07-12 14:41:18 +02:00
|
|
|
memcpy(out, messageDigestBuf, 16);
|
|
|
|
|
2022-07-12 22:33:01 +02:00
|
|
|
free(data);
|
2022-07-12 16:02:30 +02:00
|
|
|
free(messageDigestBuf);
|
|
|
|
free(checksum);
|
2022-07-12 14:41:18 +02:00
|
|
|
}
|