From 0ee32c8a84b47be262ae50fa45ba967efb4e6b26 Mon Sep 17 00:00:00 2001 From: Thomas Florian Date: Tue, 19 Jul 2022 22:27:56 +0200 Subject: [PATCH] feat: add data transfer between threads --- Implementierung/Makefile | 4 +- Implementierung/lib/md2_impls/md2_3.h | 3 +- Implementierung/src/md2_impls/md2_3.c | 133 +++++++++++++++----------- 3 files changed, 80 insertions(+), 60 deletions(-) diff --git a/Implementierung/Makefile b/Implementierung/Makefile index f10aa08..9bc5a18 100644 --- a/Implementierung/Makefile +++ b/Implementierung/Makefile @@ -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 OBJ = ${subst src,build,${SRC:.c=.o}} CC = gcc -CFLAGS = -Ilib -ggdb -std=c11 -g -Wall -Wextra -no-pie -O3 -LDFLAGS = +CFLAGS = -Ilib -ggdb -std=c11 -g -Wall -Wextra -no-pie -O3 +LDFLAGS = -pthread all: md2 diff --git a/Implementierung/lib/md2_impls/md2_3.h b/Implementierung/lib/md2_impls/md2_3.h index eff7430..8d1ff1f 100644 --- a/Implementierung/lib/md2_impls/md2_3.h +++ b/Implementierung/lib/md2_impls/md2_3.h @@ -1,6 +1,7 @@ #ifndef MD2_3_H #define MD2_3_H +#include #include #include #include @@ -8,7 +9,7 @@ #include #include #include -#include +#include q /** * @brief This implementation loads the file in bits and not at once diff --git a/Implementierung/src/md2_impls/md2_3.c b/Implementierung/src/md2_impls/md2_3.c index 524e4e6..d3c332f 100644 --- a/Implementierung/src/md2_impls/md2_3.c +++ b/Implementierung/src/md2_impls/md2_3.c @@ -2,7 +2,20 @@ #include "../../lib/md2_impls/md2_common.h" -void process_hash(size_t len, uint8_t buf[len], uint8_t messageDigestBuf[48]) { +struct arg_hash { + 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], + uint8_t messageDigestBuf[48]) { 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]; @@ -21,30 +34,43 @@ 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]) { - uint8_t l = 0; - - for (size_t i = 0; i < len / 16; i++) { +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++) { for (int j = 0; j < 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: - l = checksum[j] ^= MD2_PI_SUBST[c ^ l]; + arg->messageDigestBuf[16 + j] = arg->buf[i * 16 + j]; + arg->messageDigestBuf[32 + j] = + (arg->messageDigestBuf[16 + j] ^ arg->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 = (t + j) % 256; } } + md2_print_buf(48, arg->messageDigestBuf); + pthread_exit(NULL); } -// 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; +void* process_checksum(void* args) { + struct arg_checksum* arg = (struct arg_checksum*)args; + uint8_t l = 0; -// while (paddingNeeded > 0) { -// buf[len - paddingNeeded] = originalPadding; -// paddingNeeded--; -// } -// return len; -// } + for (size_t i = 0; i < arg->len / 16; i++) { + for (int j = 0; j < 16; j++) { + u_int8_t c = arg->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]; + } + } + pthread_exit(&arg->checksum); +} // unused! void md2_checksum_3(size_t _, uint8_t* __) {} @@ -79,48 +105,41 @@ void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]) { return; } - int hashPipe[2]; - int checksumPipe[2]; + pthread_t thread_1, thread_2; - if (pipe(hashPipe) == -1 || pipe(checksumPipe) == -1) { - return; + struct arg_hash arg_1 = {len, newBuf, messageDigestBuf}; + struct arg_checksum arg_2 = {len, newBuf, checksum}; + + if (pthread_create(&thread_1, NULL, process_hash, (void*)&arg_1) != 0) { + printf("Error creating thread 1\n"); + exit(EXIT_FAILURE); + } + if (pthread_create(&thread_2, NULL, process_checksum, (void*)&arg_2) != 0) { + printf("Error creating thread 2\n"); + exit(EXIT_FAILURE); } - 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); + if (pthread_join(thread_1, NULL) != 0) { + printf("Error joining thread 1\n"); + exit(EXIT_FAILURE); } + if (pthread_join(thread_2, NULL) != 0) { + printf("Error joining thread 2\n"); + exit(EXIT_FAILURE); + } + + md2_print_buf(48, arg_1.messageDigestBuf); + // md2_print_buf(16, checksum); + + // 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);