Compare commits

...

3 commits

Author SHA1 Message Date
Thomas Florian
94a4e5332a docs: update comment of V3 2022-07-19 23:12:04 +02:00
Thomas Florian
8d171fe0c6 fix: correct hash calculation on V3 2022-07-19 23:07:05 +02:00
Thomas Florian
0ee32c8a84 feat: add data transfer between threads 2022-07-19 22:27:56 +02:00
3 changed files with 77 additions and 78 deletions

View file

@ -3,8 +3,8 @@
SRC = src/main.c src/helper.c src/io.c src/md2.c src/md2_impls/md2_common.c src/md2_impls/md2_0.c src/md2_impls/md2_2.c src/md2_impls/md2_3.c SRC = src/main.c src/helper.c src/io.c src/md2.c src/md2_impls/md2_common.c src/md2_impls/md2_0.c src/md2_impls/md2_2.c src/md2_impls/md2_3.c
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 = LDFLAGS = -pthread
all: md2 all: md2

View file

@ -1,6 +1,7 @@
#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>
@ -11,10 +12,10 @@
#include <unistd.h> #include <unistd.h>
/** /**
* @brief This implementation loads the file in bits and not at once * @brief Diese Implementierung benutzt Threads zum Berechnen des Hashs
* *
* @param _ unused * @param len
* @param filename name of the file to load * @param buf
* @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,7 +2,13 @@
#include "../../lib/md2_impls/md2_common.h" #include "../../lib/md2_impls/md2_common.h"
void process_hash(size_t len, uint8_t buf[len], uint8_t messageDigestBuf[48]) { struct thread_args {
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];
@ -21,36 +27,57 @@ void process_hash(size_t len, uint8_t buf[len], uint8_t messageDigestBuf[48]) {
} }
} }
void process_checksum(size_t len, uint8_t buf[len], uint8_t checksum[16]) { 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++) {
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 < len / 16; i++) { for (size_t i = 0; i < args->len / 16; i++) {
for (int j = 0; j < 16; j++) { for (int j = 0; j < 16; j++) {
u_int8_t c = 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: // 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;
@ -67,64 +94,35 @@ void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]) {
paddingNeeded--; paddingNeeded--;
} }
// === step 3 === pthread_t thread_1, thread_2;
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
if (messageDigestBuf == NULL) { struct thread_args thread_args = {len, newBuf};
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);
} }
// === step 4 === u_int8_t* messageDigestBuf;
uint8_t* checksum = calloc(16, sizeof(uint8_t)); u_int8_t* checksum;
if (checksum == NULL) {
return; if (pthread_join(thread_1, (void**)&messageDigestBuf) != 0) {
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);
} }
int hashPipe[2]; process_nothread_hash(16, checksum, messageDigestBuf);
int checksumPipe[2]; memcpy(out, messageDigestBuf, 16);
if (pipe(hashPipe) == -1 || pipe(checksumPipe) == -1) { free(messageDigestBuf);
return; free(checksum);
}
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);
// 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);
} }