fix: correct hash calculation on V3

This commit is contained in:
Thomas Florian 2022-07-19 23:07:05 +02:00
parent 0ee32c8a84
commit 8d171fe0c6

View file

@ -2,16 +2,9 @@
#include "../../lib/md2_impls/md2_common.h"
struct arg_hash {
struct thread_args {
size_t len;
const uint8_t* buf;
uint8_t messageDigestBuf[48];
};
struct arg_checksum {
size_t len;
uint8_t* buf;
uint8_t checksum[16];
};
void process_nothread_hash(size_t len, uint8_t buf[len],
@ -34,49 +27,57 @@ void process_nothread_hash(size_t len, uint8_t buf[len],
}
}
void* process_hash(void* args) {
struct arg_hash* arg = (struct arg_hash*)args;
printf("process_hash: %zu\n", arg->len);
for (size_t i = 0; i < (arg->len + 16) / 16 - 1; i++) {
void* process_hash(void* threadArgs) {
struct thread_args* args = (struct thread_args*)threadArgs;
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
if (messageDigestBuf == NULL) {
return;
}
for (size_t i = 0; i < (args->len + 16) / 16 - 1; i++) {
for (int j = 0; j < 16; j++) {
arg->messageDigestBuf[16 + j] = arg->buf[i * 16 + j];
arg->messageDigestBuf[32 + j] =
(arg->messageDigestBuf[16 + j] ^ arg->messageDigestBuf[j]);
messageDigestBuf[16 + j] = args->buf[i * 16 + 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 = arg->messageDigestBuf[k] =
arg->messageDigestBuf[k] ^ MD2_PI_SUBST[t];
t = messageDigestBuf[k] = messageDigestBuf[k] ^ MD2_PI_SUBST[t];
}
t = (t + j) % 256;
}
}
md2_print_buf(48, arg->messageDigestBuf);
pthread_exit(NULL);
pthread_exit(messageDigestBuf);
}
void* process_checksum(void* threasdArgs) {
struct thread_args* args = (struct thread_args*)threasdArgs;
uint8_t* checksum = calloc(16, sizeof(uint8_t));
if (checksum == NULL) {
return;
}
void* process_checksum(void* args) {
struct arg_checksum* arg = (struct arg_checksum*)args;
uint8_t l = 0;
for (size_t i = 0; i < arg->len / 16; i++) {
for (size_t i = 0; i < args->len / 16; i++) {
for (int j = 0; j < 16; j++) {
u_int8_t c = arg->buf[i * 16 + j];
u_int8_t c = args->buf[i * 16 + j];
// reference is wrong. It says: Set C[j] to S[c xor L]. But it should be:
l = arg->checksum[j] ^= MD2_PI_SUBST[c ^ l];
l = checksum[j] ^= MD2_PI_SUBST[c ^ l];
}
}
pthread_exit(&arg->checksum);
pthread_exit(checksum);
}
// unused!
void md2_checksum_3(size_t _, uint8_t* __) {}
void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]) {
// === step 1 ===
int paddingNeeded = 16 - (len % 16);
uint8_t originalPadding = paddingNeeded;
len += paddingNeeded;
@ -93,57 +94,35 @@ void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]) {
paddingNeeded--;
}
// === step 3 ===
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
if (messageDigestBuf == NULL) {
return;
}
// === step 4 ===
uint8_t* checksum = calloc(16, sizeof(uint8_t));
if (checksum == NULL) {
return;
}
pthread_t thread_1, thread_2;
struct arg_hash arg_1 = {len, newBuf, messageDigestBuf};
struct arg_checksum arg_2 = {len, newBuf, checksum};
struct thread_args thread_args = {len, newBuf};
if (pthread_create(&thread_1, NULL, process_hash, (void*)&arg_1) != 0) {
if (pthread_create(&thread_1, NULL, process_hash, (void*)&thread_args) != 0) {
printf("Error creating thread 1\n");
exit(EXIT_FAILURE);
}
if (pthread_create(&thread_2, NULL, process_checksum, (void*)&arg_2) != 0) {
if (pthread_create(&thread_2, NULL, process_checksum, (void*)&thread_args) !=
0) {
printf("Error creating thread 2\n");
exit(EXIT_FAILURE);
}
if (pthread_join(thread_1, NULL) != 0) {
u_int8_t* messageDigestBuf;
u_int8_t* checksum;
if (pthread_join(thread_1, (void**)&messageDigestBuf) != 0) {
printf("Error joining thread 1\n");
exit(EXIT_FAILURE);
}
if (pthread_join(thread_2, NULL) != 0) {
if (pthread_join(thread_2, (void**)&checksum) != 0) {
printf("Error joining thread 2\n");
exit(EXIT_FAILURE);
}
md2_print_buf(48, arg_1.messageDigestBuf);
// md2_print_buf(16, checksum);
process_nothread_hash(16, checksum, messageDigestBuf);
memcpy(out, messageDigestBuf, 16);
// struct arg_hash arg_last = {16, checksum, messageDigestBuf};
process_nothread_hash(16, arg_2.checksum, arg_1.messageDigestBuf);
// md2_print_buf(16, messageDigestBuf);
memcpy(out, arg_1.messageDigestBuf, 16);
// free(data);
free(messageDigestBuf);
free(checksum);
// process_block_checksum(data, checksum, &l);
// process_block_hash(data, messageDigestBuf);
// process_block_checksum(data, checksum, &l);
// process_block_hash(data, messageDigestBuf);
}