2022-07-15 00:01:35 +02:00
|
|
|
#include "../../lib/md2_impls/md2_3.h"
|
|
|
|
|
|
|
|
#include "../../lib/md2_impls/md2_common.h"
|
|
|
|
|
2022-07-19 23:07:05 +02:00
|
|
|
struct thread_args {
|
2022-07-19 22:27:56 +02:00
|
|
|
size_t len;
|
|
|
|
const uint8_t* buf;
|
|
|
|
};
|
|
|
|
|
2022-07-20 10:36:19 +02:00
|
|
|
void process_nothread_hash(size_t len, const uint8_t buf[len],
|
2022-07-19 22:27:56 +02:00
|
|
|
uint8_t messageDigestBuf[48]) {
|
2022-07-15 00:01:35 +02:00
|
|
|
for (size_t i = 0; i < (len + 16) / 16 - 1; i++) {
|
|
|
|
for (int j = 0; j < 16; j++) {
|
|
|
|
messageDigestBuf[16 + j] = 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 = messageDigestBuf[k] = messageDigestBuf[k] ^ MD2_PI_SUBST[t];
|
|
|
|
}
|
|
|
|
t = (t + j) % 256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 23:07:05 +02:00
|
|
|
void* process_hash(void* threadArgs) {
|
|
|
|
struct thread_args* args = (struct thread_args*)threadArgs;
|
|
|
|
|
|
|
|
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
|
|
|
|
if (messageDigestBuf == NULL) {
|
2022-07-20 10:36:19 +02:00
|
|
|
return NULL;
|
2022-07-19 23:07:05 +02:00
|
|
|
}
|
|
|
|
|
2022-07-20 10:36:19 +02:00
|
|
|
process_nothread_hash(args->len, args->buf, messageDigestBuf);
|
2022-07-19 23:07:05 +02:00
|
|
|
pthread_exit(messageDigestBuf);
|
2022-07-19 22:27:56 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 23:07:05 +02:00
|
|
|
void* process_checksum(void* threasdArgs) {
|
|
|
|
struct thread_args* args = (struct thread_args*)threasdArgs;
|
|
|
|
|
|
|
|
uint8_t* checksum = calloc(16, sizeof(uint8_t));
|
|
|
|
if (checksum == NULL) {
|
2022-07-20 10:36:19 +02:00
|
|
|
return NULL;
|
2022-07-19 23:07:05 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 00:01:35 +02:00
|
|
|
uint8_t l = 0;
|
|
|
|
|
2022-07-19 23:07:05 +02:00
|
|
|
for (size_t i = 0; i < args->len / 16; i++) {
|
2022-07-15 00:01:35 +02:00
|
|
|
for (int j = 0; j < 16; j++) {
|
2022-07-19 23:07:05 +02:00
|
|
|
u_int8_t c = args->buf[i * 16 + j];
|
2022-07-15 00:01:35 +02:00
|
|
|
// reference is wrong. It says: Set C[j] to S[c xor L]. But it should be:
|
2022-07-19 23:07:05 +02:00
|
|
|
l = checksum[j] ^= MD2_PI_SUBST[c ^ l];
|
2022-07-15 00:01:35 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-19 23:07:05 +02:00
|
|
|
pthread_exit(checksum);
|
2022-07-15 00:01:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]) {
|
|
|
|
int paddingNeeded = 16 - (len % 16);
|
|
|
|
uint8_t originalPadding = paddingNeeded;
|
|
|
|
len += paddingNeeded;
|
|
|
|
|
|
|
|
uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t));
|
|
|
|
if (newBuf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(newBuf, buf, len - paddingNeeded);
|
|
|
|
|
|
|
|
while (paddingNeeded > 0) {
|
|
|
|
newBuf[len - paddingNeeded] = originalPadding;
|
|
|
|
paddingNeeded--;
|
|
|
|
}
|
|
|
|
|
2022-07-19 22:27:56 +02:00
|
|
|
pthread_t thread_1, thread_2;
|
2022-07-15 00:01:35 +02:00
|
|
|
|
2022-07-19 23:07:05 +02:00
|
|
|
struct thread_args thread_args = {len, newBuf};
|
2022-07-19 22:27:56 +02:00
|
|
|
|
2022-07-19 23:07:05 +02:00
|
|
|
if (pthread_create(&thread_1, NULL, process_hash, (void*)&thread_args) != 0) {
|
2022-07-19 22:27:56 +02:00
|
|
|
printf("Error creating thread 1\n");
|
2022-07-20 10:36:19 +02:00
|
|
|
if (errno == 0) errno = EAGAIN;
|
|
|
|
return;
|
2022-07-19 22:27:56 +02:00
|
|
|
}
|
2022-07-19 23:07:05 +02:00
|
|
|
if (pthread_create(&thread_2, NULL, process_checksum, (void*)&thread_args) !=
|
|
|
|
0) {
|
2022-07-19 22:27:56 +02:00
|
|
|
printf("Error creating thread 2\n");
|
2022-07-20 10:36:19 +02:00
|
|
|
if (errno == 0) errno = EAGAIN;
|
|
|
|
return;
|
2022-07-15 00:01:35 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 23:07:05 +02:00
|
|
|
u_int8_t* messageDigestBuf;
|
|
|
|
u_int8_t* checksum;
|
|
|
|
|
|
|
|
if (pthread_join(thread_1, (void**)&messageDigestBuf) != 0) {
|
2022-07-19 22:27:56 +02:00
|
|
|
printf("Error joining thread 1\n");
|
2022-07-20 10:36:19 +02:00
|
|
|
if (errno == 0) errno = EINVAL;
|
|
|
|
return;
|
2022-07-15 00:01:35 +02:00
|
|
|
}
|
2022-07-19 23:07:05 +02:00
|
|
|
if (pthread_join(thread_2, (void**)&checksum) != 0) {
|
2022-07-19 22:27:56 +02:00
|
|
|
printf("Error joining thread 2\n");
|
2022-07-20 10:36:19 +02:00
|
|
|
if (errno == 0) errno = EINVAL;
|
|
|
|
return;
|
2022-07-19 22:27:56 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 23:07:05 +02:00
|
|
|
process_nothread_hash(16, checksum, messageDigestBuf);
|
|
|
|
memcpy(out, messageDigestBuf, 16);
|
2022-07-19 22:27:56 +02:00
|
|
|
|
|
|
|
free(messageDigestBuf);
|
|
|
|
free(checksum);
|
2022-07-15 00:01:35 +02:00
|
|
|
}
|