Compare commits

..

No commits in common. "94a4e5332a68b079a63932229fc3e1774ffaf498" and "b8670dfe623d61b3193bee526d733e061bee9483" have entirely different histories.

3 changed files with 78 additions and 77 deletions

View file

@ -4,7 +4,7 @@ SRC = src/main.c src/helper.c src/io.c src/md2.c src/md2_impls/md2_common.c src/
OBJ = ${subst src,build,${SRC:.c=.o}} OBJ = ${subst src,build,${SRC:.c=.o}}
CC = gcc CC = gcc
CFLAGS = -Ilib -ggdb -std=c11 -g -Wall -Wextra -no-pie -O3 CFLAGS = -Ilib -ggdb -std=c11 -g -Wall -Wextra -no-pie -O3
LDFLAGS = -pthread LDFLAGS =
all: md2 all: md2

View file

@ -1,7 +1,6 @@
#ifndef MD2_3_H #ifndef MD2_3_H
#define MD2_3_H #define MD2_3_H
#include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -12,10 +11,10 @@
#include <unistd.h> #include <unistd.h>
/** /**
* @brief Diese Implementierung benutzt Threads zum Berechnen des Hashs * @brief This implementation loads the file in bits and not at once
* *
* @param len * @param _ unused
* @param buf * @param filename name of the file to load
* @param out * @param out
*/ */
void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]); void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]);

View file

@ -2,13 +2,7 @@
#include "../../lib/md2_impls/md2_common.h" #include "../../lib/md2_impls/md2_common.h"
struct thread_args { void process_hash(size_t len, uint8_t buf[len], uint8_t messageDigestBuf[48]) {
size_t len;
const uint8_t* buf;
};
void process_nothread_hash(size_t len, uint8_t buf[len],
uint8_t messageDigestBuf[48]) {
for (size_t i = 0; i < (len + 16) / 16 - 1; i++) { for (size_t i = 0; i < (len + 16) / 16 - 1; i++) {
for (int j = 0; j < 16; j++) { for (int j = 0; j < 16; j++) {
messageDigestBuf[16 + j] = buf[i * 16 + j]; messageDigestBuf[16 + j] = buf[i * 16 + j];
@ -27,57 +21,36 @@ void process_nothread_hash(size_t len, uint8_t buf[len],
} }
} }
void* process_hash(void* threadArgs) { void process_checksum(size_t len, uint8_t buf[len], uint8_t checksum[16]) {
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++) {
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 = messageDigestBuf[k] = messageDigestBuf[k] ^ MD2_PI_SUBST[t];
}
t = (t + j) % 256;
}
}
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;
}
uint8_t l = 0; uint8_t l = 0;
for (size_t i = 0; i < args->len / 16; i++) { for (size_t i = 0; i < len / 16; i++) {
for (int j = 0; j < 16; j++) { for (int j = 0; j < 16; j++) {
u_int8_t c = args->buf[i * 16 + j]; u_int8_t c = buf[i * 16 + j];
// reference is wrong. It says: Set C[j] to S[c xor L]. But it should be: // 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]; l = checksum[j] ^= MD2_PI_SUBST[c ^ l];
} }
} }
pthread_exit(checksum);
} }
// DUPLICATE CODE FROM md2_2.c
// int apply_padding(size_t len, uint8_t buf[len]) {
// int paddingNeeded = 16 - (len % 16);
// uint8_t originalPadding = paddingNeeded;
// len += paddingNeeded;
// while (paddingNeeded > 0) {
// buf[len - paddingNeeded] = originalPadding;
// paddingNeeded--;
// }
// return len;
// }
// unused! // unused!
void md2_checksum_3(size_t _, uint8_t* __) {} void md2_checksum_3(size_t _, uint8_t* __) {}
void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]) { void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]) {
// === step 1 ===
int paddingNeeded = 16 - (len % 16); int paddingNeeded = 16 - (len % 16);
uint8_t originalPadding = paddingNeeded; uint8_t originalPadding = paddingNeeded;
len += paddingNeeded; len += paddingNeeded;
@ -94,35 +67,64 @@ void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]) {
paddingNeeded--; paddingNeeded--;
} }
pthread_t thread_1, thread_2; // === step 3 ===
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
struct thread_args thread_args = {len, newBuf}; if (messageDigestBuf == NULL) {
return;
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*)&thread_args) !=
0) {
printf("Error creating thread 2\n");
exit(EXIT_FAILURE);
} }
u_int8_t* messageDigestBuf; // === step 4 ===
u_int8_t* checksum; uint8_t* checksum = calloc(16, sizeof(uint8_t));
if (checksum == NULL) {
if (pthread_join(thread_1, (void**)&messageDigestBuf) != 0) { return;
printf("Error joining thread 1\n");
exit(EXIT_FAILURE);
}
if (pthread_join(thread_2, (void**)&checksum) != 0) {
printf("Error joining thread 2\n");
exit(EXIT_FAILURE);
} }
process_nothread_hash(16, checksum, messageDigestBuf); int hashPipe[2];
int checksumPipe[2];
if (pipe(hashPipe) == -1 || pipe(checksumPipe) == -1) {
return;
}
pid_t pid_1, pid_2;
(pid_1 = fork()) && (pid_2 = fork());
if (pid_1 == 0) {
// Child 1 process
close(hashPipe[0]);
process_hash(len, newBuf, messageDigestBuf);
write(hashPipe[1], messageDigestBuf, 48);
exit(0);
} else if (pid_2 == 0) {
// Child 2 process
close(checksumPipe[0]);
process_checksum(len, newBuf, checksum);
write(checksumPipe[1], checksum, 16);
exit(0);
} else {
// int status;
// Parent process
close(hashPipe[1]);
close(checksumPipe[1]);
waitpid(pid_1, NULL, 0);
waitpid(pid_2, NULL, 0);
read(hashPipe[0], messageDigestBuf, 48);
read(checksumPipe[0], checksum, 16);
process_hash(16, checksum, messageDigestBuf);
memcpy(out, messageDigestBuf, 16); memcpy(out, messageDigestBuf, 16);
// free(data);
free(messageDigestBuf); free(messageDigestBuf);
free(checksum); free(checksum);
} }
// process_block_checksum(data, checksum, &l);
// process_block_hash(data, messageDigestBuf);
// process_block_checksum(data, checksum, &l);
// process_block_hash(data, messageDigestBuf);
}