From b8670dfe623d61b3193bee526d733e061bee9483 Mon Sep 17 00:00:00 2001 From: Thomas Florian Date: Fri, 15 Jul 2022 00:01:35 +0200 Subject: [PATCH 1/5] feat: implement threading --- Implementierung/Makefile | 2 +- Implementierung/lib/md2_impls/md2_3.h | 23 +++++ Implementierung/src/md2.c | 6 ++ Implementierung/src/md2_impls/md2_2.c | 2 +- Implementierung/src/md2_impls/md2_3.c | 130 ++++++++++++++++++++++++++ 5 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 Implementierung/lib/md2_impls/md2_3.h create mode 100644 Implementierung/src/md2_impls/md2_3.c diff --git a/Implementierung/Makefile b/Implementierung/Makefile index 843d95b..f10aa08 100644 --- a/Implementierung/Makefile +++ b/Implementierung/Makefile @@ -1,6 +1,6 @@ -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 = 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 diff --git a/Implementierung/lib/md2_impls/md2_3.h b/Implementierung/lib/md2_impls/md2_3.h new file mode 100644 index 0000000..eff7430 --- /dev/null +++ b/Implementierung/lib/md2_impls/md2_3.h @@ -0,0 +1,23 @@ +#ifndef MD2_3_H +#define MD2_3_H + +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * @brief This implementation loads the file in bits and not at once + * + * @param _ unused + * @param filename name of the file to load + * @param out + */ +void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]); +void md2_checksum_3(size_t len, uint8_t* buf); + +#endif // MD2_3_H \ No newline at end of file diff --git a/Implementierung/src/md2.c b/Implementierung/src/md2.c index da49453..c4c50da 100644 --- a/Implementierung/src/md2.c +++ b/Implementierung/src/md2.c @@ -3,6 +3,7 @@ // include all implementations #include "../lib/md2_impls/md2_0.h" #include "../lib/md2_impls/md2_2.h" +#include "../lib/md2_impls/md2_3.h" md2_hash_func md2_hash; md2_checksum_func md2_checksum; @@ -28,6 +29,11 @@ bool md2_choose_implementation(int i) { md2_checksum = md2_checksum_2; return true; + case 3: + md2_hash = md2_hash_3; + md2_checksum = md2_checksum_3; + return true; + default: return false; } diff --git a/Implementierung/src/md2_impls/md2_2.c b/Implementierung/src/md2_impls/md2_2.c index 1349c0f..7f361fe 100644 --- a/Implementierung/src/md2_impls/md2_2.c +++ b/Implementierung/src/md2_impls/md2_2.c @@ -39,7 +39,7 @@ void apply_padding(size_t len, uint8_t buf[16]) { } // unused! -void md2_checksum_2(size_t, uint8_t*) {} +void md2_checksum_2(size_t _, uint8_t* __) {} void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) { FILE* file; diff --git a/Implementierung/src/md2_impls/md2_3.c b/Implementierung/src/md2_impls/md2_3.c new file mode 100644 index 0000000..524e4e6 --- /dev/null +++ b/Implementierung/src/md2_impls/md2_3.c @@ -0,0 +1,130 @@ +#include "../../lib/md2_impls/md2_3.h" + +#include "../../lib/md2_impls/md2_common.h" + +void process_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]; + 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; + } + } +} + +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++) { + 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]; + } + } +} + +// 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! +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; + + 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--; + } + + // === 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; + } + + 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); + + // 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); +} From 0ee32c8a84b47be262ae50fa45ba967efb4e6b26 Mon Sep 17 00:00:00 2001 From: Thomas Florian Date: Tue, 19 Jul 2022 22:27:56 +0200 Subject: [PATCH 2/5] 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); From 8d171fe0c6a981e8829a72dbb4802c36f43096c4 Mon Sep 17 00:00:00 2001 From: Thomas Florian Date: Tue, 19 Jul 2022 23:07:05 +0200 Subject: [PATCH 3/5] fix: correct hash calculation on V3 --- Implementierung/src/md2_impls/md2_3.c | 97 +++++++++++---------------- 1 file changed, 38 insertions(+), 59 deletions(-) diff --git a/Implementierung/src/md2_impls/md2_3.c b/Implementierung/src/md2_impls/md2_3.c index d3c332f..c8334be 100644 --- a/Implementierung/src/md2_impls/md2_3.c +++ b/Implementierung/src/md2_impls/md2_3.c @@ -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* args) { - struct arg_checksum* arg = (struct arg_checksum*)args; +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; - 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); } From 94a4e5332a68b079a63932229fc3e1774ffaf498 Mon Sep 17 00:00:00 2001 From: Thomas Florian Date: Tue, 19 Jul 2022 23:12:04 +0200 Subject: [PATCH 4/5] docs: update comment of V3 --- Implementierung/lib/md2_impls/md2_3.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Implementierung/lib/md2_impls/md2_3.h b/Implementierung/lib/md2_impls/md2_3.h index 8d1ff1f..0f3671e 100644 --- a/Implementierung/lib/md2_impls/md2_3.h +++ b/Implementierung/lib/md2_impls/md2_3.h @@ -9,13 +9,13 @@ #include #include #include -#include q +#include /** - * @brief This implementation loads the file in bits and not at once + * @brief Diese Implementierung benutzt Threads zum Berechnen des Hashs * - * @param _ unused - * @param filename name of the file to load + * @param len + * @param buf * @param out */ void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]); From 74746a91b4e8a305dd2c68bba0ff2db79ce1052e Mon Sep 17 00:00:00 2001 From: Thomas Florian Date: Wed, 20 Jul 2022 10:36:19 +0200 Subject: [PATCH 5/5] fix: console warnings --- Implementierung/lib/md2_impls/md2_2.h | 1 - Implementierung/lib/md2_impls/md2_3.h | 2 -- Implementierung/lib/md2_impls/md2_common.h | 1 + Implementierung/src/main.c | 1 - Implementierung/src/md2.c | 4 +-- Implementierung/src/md2_impls/md2_2.c | 7 ++-- Implementierung/src/md2_impls/md2_3.c | 38 +++++++--------------- 7 files changed, 17 insertions(+), 37 deletions(-) diff --git a/Implementierung/lib/md2_impls/md2_2.h b/Implementierung/lib/md2_impls/md2_2.h index 4e64689..d7b370d 100644 --- a/Implementierung/lib/md2_impls/md2_2.h +++ b/Implementierung/lib/md2_impls/md2_2.h @@ -18,6 +18,5 @@ * @param out */ void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]); -void md2_checksum_2(size_t len, uint8_t* buf); #endif // MD2_2_H \ No newline at end of file diff --git a/Implementierung/lib/md2_impls/md2_3.h b/Implementierung/lib/md2_impls/md2_3.h index 0f3671e..e51c783 100644 --- a/Implementierung/lib/md2_impls/md2_3.h +++ b/Implementierung/lib/md2_impls/md2_3.h @@ -8,7 +8,6 @@ #include #include #include -#include #include /** @@ -19,6 +18,5 @@ * @param out */ void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]); -void md2_checksum_3(size_t len, uint8_t* buf); #endif // MD2_3_H \ No newline at end of file diff --git a/Implementierung/lib/md2_impls/md2_common.h b/Implementierung/lib/md2_impls/md2_common.h index f2b72c2..c66e756 100644 --- a/Implementierung/lib/md2_impls/md2_common.h +++ b/Implementierung/lib/md2_impls/md2_common.h @@ -1,6 +1,7 @@ #ifndef MD2_COMMON_H #define MD2_COMMON_H +#include #include #include #include diff --git a/Implementierung/src/main.c b/Implementierung/src/main.c index 42b8e6f..94b7a7a 100644 --- a/Implementierung/src/main.c +++ b/Implementierung/src/main.c @@ -4,7 +4,6 @@ #include "../lib/io.h" #include "../lib/md2.h" -// Returns true when val is approx. equal to exp. static bool runTest(struct configuration* c, const char* message, const char* expectedHash) { uint8_t out[16]; diff --git a/Implementierung/src/md2.c b/Implementierung/src/md2.c index c4c50da..9cd83ed 100644 --- a/Implementierung/src/md2.c +++ b/Implementierung/src/md2.c @@ -26,12 +26,12 @@ bool md2_choose_implementation(int i) { case 2: md2_hash = md2_hash_2; - md2_checksum = md2_checksum_2; + md2_checksum = NULL; return true; case 3: md2_hash = md2_hash_3; - md2_checksum = md2_checksum_3; + md2_checksum = NULL; return true; default: diff --git a/Implementierung/src/md2_impls/md2_2.c b/Implementierung/src/md2_impls/md2_2.c index 7f361fe..1376bf4 100644 --- a/Implementierung/src/md2_impls/md2_2.c +++ b/Implementierung/src/md2_impls/md2_2.c @@ -38,9 +38,6 @@ void apply_padding(size_t len, uint8_t buf[16]) { } } -// unused! -void md2_checksum_2(size_t _, uint8_t* __) {} - void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) { FILE* file; struct stat file_stat; @@ -62,8 +59,8 @@ void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) { while (bytes_left_to_read != 0) { bytes_left_to_process = bytes_left_to_read >= 16 ? 16 : bytes_left_to_read; - fread(data, 1, bytes_left_to_process, file); - if (ferror(file) || feof(file)) { + size_t size = fread(data, 1, bytes_left_to_process, file); + if (size == 0 || ferror(file) || feof(file)) { if (errno == 0) errno = EIO; return; } diff --git a/Implementierung/src/md2_impls/md2_3.c b/Implementierung/src/md2_impls/md2_3.c index c8334be..63aebad 100644 --- a/Implementierung/src/md2_impls/md2_3.c +++ b/Implementierung/src/md2_impls/md2_3.c @@ -7,7 +7,7 @@ struct thread_args { const uint8_t* buf; }; -void process_nothread_hash(size_t len, uint8_t buf[len], +void process_nothread_hash(size_t len, const 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++) { @@ -32,25 +32,10 @@ void* process_hash(void* threadArgs) { uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t)); if (messageDigestBuf == NULL) { - return; + return NULL; } - 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; - } - } + process_nothread_hash(args->len, args->buf, messageDigestBuf); pthread_exit(messageDigestBuf); } @@ -59,7 +44,7 @@ void* process_checksum(void* threasdArgs) { uint8_t* checksum = calloc(16, sizeof(uint8_t)); if (checksum == NULL) { - return; + return NULL; } uint8_t l = 0; @@ -74,9 +59,6 @@ void* process_checksum(void* threasdArgs) { 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]) { int paddingNeeded = 16 - (len % 16); uint8_t originalPadding = paddingNeeded; @@ -100,12 +82,14 @@ void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]) { if (pthread_create(&thread_1, NULL, process_hash, (void*)&thread_args) != 0) { printf("Error creating thread 1\n"); - exit(EXIT_FAILURE); + if (errno == 0) errno = EAGAIN; + return; } if (pthread_create(&thread_2, NULL, process_checksum, (void*)&thread_args) != 0) { printf("Error creating thread 2\n"); - exit(EXIT_FAILURE); + if (errno == 0) errno = EAGAIN; + return; } u_int8_t* messageDigestBuf; @@ -113,11 +97,13 @@ void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]) { if (pthread_join(thread_1, (void**)&messageDigestBuf) != 0) { printf("Error joining thread 1\n"); - exit(EXIT_FAILURE); + if (errno == 0) errno = EINVAL; + return; } if (pthread_join(thread_2, (void**)&checksum) != 0) { printf("Error joining thread 2\n"); - exit(EXIT_FAILURE); + if (errno == 0) errno = EINVAL; + return; } process_nothread_hash(16, checksum, messageDigestBuf);