From b8670dfe623d61b3193bee526d733e061bee9483 Mon Sep 17 00:00:00 2001 From: Thomas Florian Date: Fri, 15 Jul 2022 00:01:35 +0200 Subject: [PATCH 01/16] 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 df9f09d9df351fa75f93ab855d993bc04d8412f8 Mon Sep 17 00:00:00 2001 From: finn Date: Fri, 15 Jul 2022 11:30:19 +0200 Subject: [PATCH 02/16] Implementation 1 created --- Implementierung/Makefile | 2 +- Implementierung/src/md2.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Implementierung/Makefile b/Implementierung/Makefile index 843d95b..228eb39 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_1.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/src/md2.c b/Implementierung/src/md2.c index da49453..de9b908 100644 --- a/Implementierung/src/md2.c +++ b/Implementierung/src/md2.c @@ -2,6 +2,7 @@ // include all implementations #include "../lib/md2_impls/md2_0.h" +#include "../lib/md2_impls/md2_1.h" #include "../lib/md2_impls/md2_2.h" md2_hash_func md2_hash; @@ -23,6 +24,11 @@ bool md2_choose_implementation(int i) { md2_checksum = md2_checksum_0; return true; + case 1: + md2_hash = md2_hash_1; + md2_checksum = md2_checksum_1; + return true; + case 2: md2_hash = md2_hash_2; md2_checksum = md2_checksum_2; From 8c997087e9d68caf443bd676a6ce5f687cf64976 Mon Sep 17 00:00:00 2001 From: finn Date: Fri, 15 Jul 2022 11:32:22 +0200 Subject: [PATCH 03/16] Implementation 1 created --- Implementierung/lib/md2_impls/md2_1.h | 23 ++++++++ Implementierung/src/md2_impls/md2_1.c | 75 +++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 Implementierung/lib/md2_impls/md2_1.h create mode 100644 Implementierung/src/md2_impls/md2_1.c diff --git a/Implementierung/lib/md2_impls/md2_1.h b/Implementierung/lib/md2_impls/md2_1.h new file mode 100644 index 0000000..8777277 --- /dev/null +++ b/Implementierung/lib/md2_impls/md2_1.h @@ -0,0 +1,23 @@ +#ifndef MD2_1_H +#define MD2_1_H + +#include +#include +#include +#include +#include +#include + +#include "../io.h" + +/** + * @brief This implementation optimizes small operations and uses SIMD + * + * @param _ unused + * @param filename name of the file to load + * @param out + */ +void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]); +void md2_checksum_1(size_t len, uint8_t* buf); + +#endif // MD2_1_H \ No newline at end of file diff --git a/Implementierung/src/md2_impls/md2_1.c b/Implementierung/src/md2_impls/md2_1.c new file mode 100644 index 0000000..6e997ed --- /dev/null +++ b/Implementierung/src/md2_impls/md2_1.c @@ -0,0 +1,75 @@ +#include "../../lib/md2_impls/md2_1.h" + +#include "../../lib/md2_impls/md2_common.h" + +void md2_checksum_1(size_t len, uint8_t* buf) { + 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: + buf[len + j] ^= MD2_PI_SUBST[c ^ l]; + l = buf[len + j]; + } + } +} + +void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) { + // === step 1 === + int paddingNeeded = 16 - (len & 7); + uint8_t originalPadding = paddingNeeded; + len += paddingNeeded; + + // printf("len: %d\n", len); + + // +16 for the checksum + uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t)); + // TODO: null check + memcpy(newBuf, buf, len - paddingNeeded); + + // printBuf(len + 16, newBuf); + + while (paddingNeeded > 0) { + newBuf[len - paddingNeeded] = originalPadding; + paddingNeeded--; + } + // printf("buf with padding: "); + // printBuf(len + 16, newBuf); + + // === step 2 === + md2_checksum_1(len, newBuf); + + // printf("buf with cecksum: "); + // printBuf(len + 16, newBuf); + + // === step 3 === + uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t)); + // TODO: add null check + + // === step 4 === + // <= because we need to hash the last block too + for (size_t i = 0; i <= (len + 16) / 16 - 1; i++) { + for (int j = 0; j < 16; j++) { + messageDigestBuf[16 + j] = newBuf[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) & 127; + } + } + // printf("messageDigestBuf: \n"); + // printBuf(16, messageDigestBuf); + + memcpy(out, messageDigestBuf, 16); + + free(messageDigestBuf); + free(newBuf); +} \ No newline at end of file From 88ee99b37c8ad7e8dca8664fb10ca693fcd96a38 Mon Sep 17 00:00:00 2001 From: finn Date: Mon, 18 Jul 2022 21:22:03 +0200 Subject: [PATCH 04/16] Feat: SIMD of for loop --- Implementierung/src/md2_impls/md2_1.c | 100 ++++++++++++++++++++------ 1 file changed, 80 insertions(+), 20 deletions(-) diff --git a/Implementierung/src/md2_impls/md2_1.c b/Implementierung/src/md2_impls/md2_1.c index 6e997ed..d4e2943 100644 --- a/Implementierung/src/md2_impls/md2_1.c +++ b/Implementierung/src/md2_impls/md2_1.c @@ -1,12 +1,17 @@ #include "../../lib/md2_impls/md2_1.h" #include "../../lib/md2_impls/md2_common.h" +#include -void md2_checksum_1(size_t len, uint8_t* buf) { + +void md2_checksum_1(size_t len, uint8_t *buf) +{ uint8_t l = 0; - for (size_t i = 0; i < len / 16; i++) { - for (int j = 0; j < 16; j++) { + 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: buf[len + j] ^= MD2_PI_SUBST[c ^ l]; @@ -15,27 +20,50 @@ void md2_checksum_1(size_t len, uint8_t* buf) { } } -void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) { +static uint8_t PADDING[17][16] = { + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {5, 5, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {8, 8, 8, 8, 8, 8, 8, 8, 0, 0, 0, 0, 0, 0, 0, 0}, + {9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 0, 0, 0, 0, 0}, + {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0}, + {11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 0, 0, 0, 0, 0}, + {12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 0, 0, 0, 0}, + {13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 0, 0, 0}, + {14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0}, + {15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0}, + {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16}}; + +void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) +{ // === step 1 === - int paddingNeeded = 16 - (len & 7); - uint8_t originalPadding = paddingNeeded; + int paddingNeeded = 16 - (len & 15); len += paddingNeeded; // printf("len: %d\n", len); // +16 for the checksum - uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t)); + uint8_t* newBuf = aligned_alloc(16, sizeof(uint8_t)*(len + 16)); + + for(size_t i = 0; i < 16; i++) { + newBuf[len + i] = 0; + } + + // uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t)); // TODO: null check memcpy(newBuf, buf, len - paddingNeeded); - // printBuf(len + 16, newBuf); + //md2_print_buf(len + 16, newBuf); + + memcpy(newBuf + len - paddingNeeded, PADDING + paddingNeeded, paddingNeeded); - while (paddingNeeded > 0) { - newBuf[len - paddingNeeded] = originalPadding; - paddingNeeded--; - } // printf("buf with padding: "); - // printBuf(len + 16, newBuf); + //md2_print_buf(len + 16, newBuf); // === step 2 === md2_checksum_1(len, newBuf); @@ -44,25 +72,57 @@ void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) { // printBuf(len + 16, newBuf); // === step 3 === - uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t)); + uint8_t *messageDigestBuf = aligned_alloc(16, sizeof(uint8_t) * 48); // TODO: add null check + for (size_t i = 0; i < 48; i++) { + messageDigestBuf[i] = 0; + } // === step 4 === // <= because we need to hash the last block too - for (size_t i = 0; i <= (len + 16) / 16 - 1; i++) { - for (int j = 0; j < 16; j++) { - messageDigestBuf[16 + j] = newBuf[i * 16 + j]; + __m128i vx; + __m128i vy; + for (size_t i = 0; i <= (len + 16) / 16 - 1; i++) + { + + vx = _mm_load_si128((__m128i*) (newBuf + i * 16)); + _mm_store_si128((__m128i*) (messageDigestBuf + 16), vx); + vy = _mm_load_si128((__m128i*) (messageDigestBuf)); + vy = _mm_xor_si128(vy, vx); + _mm_store_si128((__m128i*) (messageDigestBuf + 32), vy); + + + /* + for (int j = 0; j < 16; j++) + { + //messageDigestBuf[16 + j] = newBuf[i * 16 + j]; messageDigestBuf[32 + j] = (messageDigestBuf[16 + j] ^ messageDigestBuf[j]); } + /* + vy = _mm_load_si128((__m128i*) (messageDigestBuf)); + _mm_xor_si128(vy, vx); + _mm_store_si128((__m128i*) messageDigestBuf, vy); + md2_print_buf(48, messageDigestBuf); + md2_print_buf(len + 16, newBuf); + /* + printf("newBuf: erstes Element:"); + printf(*(newBuf + i * 16)); + printf("mdb:"); + printf(*(messageDigestBuf + 16)); + */ + + u_int8_t t = 0; - for (int j = 0; j < 18; j++) { - for (int k = 0; k < 48; k++) { + 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) & 127; + t = (t + j) & 255; } } // printf("messageDigestBuf: \n"); From fda4ee592da04fc393cced62b622d7b9248475ba Mon Sep 17 00:00:00 2001 From: finn Date: Mon, 18 Jul 2022 21:28:38 +0200 Subject: [PATCH 05/16] Feat: added Nullchecks for malloc/calloc --- Implementierung/src/io.c | 4 ++ Implementierung/src/md2_impls/md2_0.c | 11 +++++- Implementierung/src/md2_impls/md2_1.c | 53 ++++++--------------------- Implementierung/src/md2_impls/md2_2.c | 12 ++++++ 4 files changed, 36 insertions(+), 44 deletions(-) diff --git a/Implementierung/src/io.c b/Implementierung/src/io.c index 339bd75..048aee7 100644 --- a/Implementierung/src/io.c +++ b/Implementierung/src/io.c @@ -34,6 +34,10 @@ uint8_t* read_file(const char* path, size_t* size) { } uint8_t* data = malloc(statOfFile.st_size); + if (data == NULL) { + printf("Failure: malloc returns NULL"); + return -1; + } size_t bytesRead = fread(data, statOfFile.st_blksize, statOfFile.st_blocks, f); diff --git a/Implementierung/src/md2_impls/md2_0.c b/Implementierung/src/md2_impls/md2_0.c index eeb4354..c344325 100644 --- a/Implementierung/src/md2_impls/md2_0.c +++ b/Implementierung/src/md2_impls/md2_0.c @@ -25,7 +25,10 @@ void md2_hash_0(size_t len, const uint8_t buf[len], uint8_t out[16]) { // +16 for the checksum uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t)); - // TODO: null check + if (newBuf == NULL) { + printf("Failure: calloc returns NULL"); + return -1; + } memcpy(newBuf, buf, len - paddingNeeded); // printBuf(len + 16, newBuf); @@ -45,7 +48,11 @@ void md2_hash_0(size_t len, const uint8_t buf[len], uint8_t out[16]) { // === step 3 === uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t)); - // TODO: add null check + if (messageDigestBuf == NULL) { + printf("Failure: calloc returns NULL"); + return -1; + } + // === step 4 === // <= because we need to hash the last block too diff --git a/Implementierung/src/md2_impls/md2_1.c b/Implementierung/src/md2_impls/md2_1.c index d4e2943..ff2f1ff 100644 --- a/Implementierung/src/md2_impls/md2_1.c +++ b/Implementierung/src/md2_impls/md2_1.c @@ -45,74 +45,45 @@ void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) int paddingNeeded = 16 - (len & 15); len += paddingNeeded; - // printf("len: %d\n", len); - - // +16 for the checksum uint8_t* newBuf = aligned_alloc(16, sizeof(uint8_t)*(len + 16)); + if (newBuf == NULL) { + printf("Failure: aligned_alloc returns NULL"); + return -1; + } for(size_t i = 0; i < 16; i++) { newBuf[len + i] = 0; } - - // uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t)); - // TODO: null check + memcpy(newBuf, buf, len - paddingNeeded); - //md2_print_buf(len + 16, newBuf); - memcpy(newBuf + len - paddingNeeded, PADDING + paddingNeeded, paddingNeeded); - // printf("buf with padding: "); - //md2_print_buf(len + 16, newBuf); - // === step 2 === md2_checksum_1(len, newBuf); - // printf("buf with cecksum: "); - // printBuf(len + 16, newBuf); - // === step 3 === uint8_t *messageDigestBuf = aligned_alloc(16, sizeof(uint8_t) * 48); - // TODO: add null check + if (messageDigestBuf == NULL) { + printf("Failure: aligned_alloc returns NULL"); + return -1; + } + for (size_t i = 0; i < 48; i++) { messageDigestBuf[i] = 0; } // === step 4 === - // <= because we need to hash the last block too __m128i vx; __m128i vy; for (size_t i = 0; i <= (len + 16) / 16 - 1; i++) { - vx = _mm_load_si128((__m128i*) (newBuf + i * 16)); _mm_store_si128((__m128i*) (messageDigestBuf + 16), vx); vy = _mm_load_si128((__m128i*) (messageDigestBuf)); vy = _mm_xor_si128(vy, vx); _mm_store_si128((__m128i*) (messageDigestBuf + 32), vy); - - - /* - for (int j = 0; j < 16; j++) - { - //messageDigestBuf[16 + j] = newBuf[i * 16 + j]; - messageDigestBuf[32 + j] = - (messageDigestBuf[16 + j] ^ messageDigestBuf[j]); - } - /* - vy = _mm_load_si128((__m128i*) (messageDigestBuf)); - _mm_xor_si128(vy, vx); - _mm_store_si128((__m128i*) messageDigestBuf, vy); - md2_print_buf(48, messageDigestBuf); - md2_print_buf(len + 16, newBuf); - /* - printf("newBuf: erstes Element:"); - printf(*(newBuf + i * 16)); - printf("mdb:"); - printf(*(messageDigestBuf + 16)); - */ - - + u_int8_t t = 0; @@ -125,8 +96,6 @@ void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) t = (t + j) & 255; } } - // printf("messageDigestBuf: \n"); - // printBuf(16, messageDigestBuf); memcpy(out, messageDigestBuf, 16); diff --git a/Implementierung/src/md2_impls/md2_2.c b/Implementierung/src/md2_impls/md2_2.c index 1349c0f..b0955ba 100644 --- a/Implementierung/src/md2_impls/md2_2.c +++ b/Implementierung/src/md2_impls/md2_2.c @@ -50,12 +50,24 @@ void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) { // === step 3 === uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t)); + if (messageDigestBuf == NULL) { + printf("Failure: calloc returns NULL"); + return -1; + } // === step 4 === uint8_t l = 0; uint8_t* checksum = calloc(16, sizeof(uint8_t)); + if (checksum == NULL) { + printf("Failure: calloc returns NULL"); + return -1; + } uint8_t* data = malloc(16); + if (data == NULL) { + printf("Failure: malloc returns NULL"); + return -1; + } size_t bytes_left_to_read = file_stat.st_size; size_t bytes_left_to_process = 0; From 0ee32c8a84b47be262ae50fa45ba967efb4e6b26 Mon Sep 17 00:00:00 2001 From: Thomas Florian Date: Tue, 19 Jul 2022 22:27:56 +0200 Subject: [PATCH 06/16] 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 07/16] 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 08/16] 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 e516595eb288daadd597f8e7f03f3694d01ce636 Mon Sep 17 00:00:00 2001 From: finn Date: Wed, 20 Jul 2022 09:21:48 +0200 Subject: [PATCH 09/16] feat: return nullchecks updated --- Implementierung/src/io.c | 2 +- Implementierung/src/md2_impls/md2_0.c | 4 ++-- Implementierung/src/md2_impls/md2_1.c | 4 ++-- Implementierung/src/md2_impls/md2_2.c | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Implementierung/src/io.c b/Implementierung/src/io.c index 048aee7..942f235 100644 --- a/Implementierung/src/io.c +++ b/Implementierung/src/io.c @@ -36,7 +36,7 @@ uint8_t* read_file(const char* path, size_t* size) { uint8_t* data = malloc(statOfFile.st_size); if (data == NULL) { printf("Failure: malloc returns NULL"); - return -1; + return EXIT_FAILURE; } size_t bytesRead = diff --git a/Implementierung/src/md2_impls/md2_0.c b/Implementierung/src/md2_impls/md2_0.c index c344325..941b352 100644 --- a/Implementierung/src/md2_impls/md2_0.c +++ b/Implementierung/src/md2_impls/md2_0.c @@ -27,7 +27,7 @@ void md2_hash_0(size_t len, const uint8_t buf[len], uint8_t out[16]) { uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t)); if (newBuf == NULL) { printf("Failure: calloc returns NULL"); - return -1; + return EXIT_FAILURE; } memcpy(newBuf, buf, len - paddingNeeded); @@ -50,7 +50,7 @@ void md2_hash_0(size_t len, const uint8_t buf[len], uint8_t out[16]) { uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t)); if (messageDigestBuf == NULL) { printf("Failure: calloc returns NULL"); - return -1; + return EXIT_FAILURE; } diff --git a/Implementierung/src/md2_impls/md2_1.c b/Implementierung/src/md2_impls/md2_1.c index ff2f1ff..d4fb80b 100644 --- a/Implementierung/src/md2_impls/md2_1.c +++ b/Implementierung/src/md2_impls/md2_1.c @@ -48,7 +48,7 @@ void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) uint8_t* newBuf = aligned_alloc(16, sizeof(uint8_t)*(len + 16)); if (newBuf == NULL) { printf("Failure: aligned_alloc returns NULL"); - return -1; + return EXIT_FAILURE; } for(size_t i = 0; i < 16; i++) { @@ -66,7 +66,7 @@ void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) uint8_t *messageDigestBuf = aligned_alloc(16, sizeof(uint8_t) * 48); if (messageDigestBuf == NULL) { printf("Failure: aligned_alloc returns NULL"); - return -1; + return EXIT_FAILURE; } for (size_t i = 0; i < 48; i++) { diff --git a/Implementierung/src/md2_impls/md2_2.c b/Implementierung/src/md2_impls/md2_2.c index b0955ba..5253ce7 100644 --- a/Implementierung/src/md2_impls/md2_2.c +++ b/Implementierung/src/md2_impls/md2_2.c @@ -52,7 +52,7 @@ void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) { uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t)); if (messageDigestBuf == NULL) { printf("Failure: calloc returns NULL"); - return -1; + return EXIT_FAILURE; } // === step 4 === @@ -60,13 +60,13 @@ void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) { uint8_t* checksum = calloc(16, sizeof(uint8_t)); if (checksum == NULL) { printf("Failure: calloc returns NULL"); - return -1; + return EXIT_FAILURE; } uint8_t* data = malloc(16); if (data == NULL) { printf("Failure: malloc returns NULL"); - return -1; + return EXIT_FAILURE; } size_t bytes_left_to_read = file_stat.st_size; size_t bytes_left_to_process = 0; From 74746a91b4e8a305dd2c68bba0ff2db79ce1052e Mon Sep 17 00:00:00 2001 From: Thomas Florian Date: Wed, 20 Jul 2022 10:36:19 +0200 Subject: [PATCH 10/16] 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); From cdaa1e0196b1025b526bea061cf6217ff280bb99 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Wed, 20 Jul 2022 11:21:13 +0200 Subject: [PATCH 11/16] Feat: Benchmarks --- Implementierung/.gitignore | 2 +- Implementierung/Makefile | 38 +++++++++++++++++++++++++++++++--- Implementierung/benchmarks.csv | 1 + 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 Implementierung/benchmarks.csv diff --git a/Implementierung/.gitignore b/Implementierung/.gitignore index d6dce6b..37e27d3 100644 --- a/Implementierung/.gitignore +++ b/Implementierung/.gitignore @@ -1,3 +1,3 @@ build md2 -testfile* \ No newline at end of file +t \ No newline at end of file diff --git a/Implementierung/Makefile b/Implementierung/Makefile index 843d95b..7be2eba 100644 --- a/Implementierung/Makefile +++ b/Implementierung/Makefile @@ -5,6 +5,8 @@ OBJ = ${subst src,build,${SRC:.c=.o}} CC = gcc CFLAGS = -Ilib -ggdb -std=c11 -g -Wall -Wextra -no-pie -O3 LDFLAGS = +TESTFILES = t/1 t/10 t/100 #t/1000 t/2000 t/5000 t/10000 +TESTFILES_SIZES = ${subst t/,,${TESTFILES}} all: md2 @@ -18,9 +20,7 @@ help: @echo - all: build everything @echo - clean: clean distfiles @echo - help: show this help - -build: - mkdir build + @echo - benchmarks: run benchmarks (only works on linux!) build/%.o: src/%.c @mkdir -p build/md2_impls @@ -29,4 +29,36 @@ build/%.o: src/%.c md2: ${OBJ} ${CC} -o $@ $(OBJ) ${LDFLAGS} +t/%: + @echo + @echo "=== Generating ${subst t/,,$@}MB of random data... ===" + dd if=/dev/random of=$@ bs=1M count=${subst t/,,$@} status=progress + @echo "=== done ===" + @echo + +benchmarks.csv: ${TESTFILES} + @echo "" > $@ + @for i in 0 1 2 3; do \ + echo ;\ + echo "=== Testing implementation $$i ===";\ + for t in $(TESTFILES_SIZES); do \ + echo -n "- with $${t}MB ... "; \ + if ! r=$$(./md2 t/$${t} -B1 -V1); then \ + echo; \ + echo "ERROR!"; \ + exit 1; \ + fi; \ + r=$$(echo $$r | grep "cycles took" | sed 's/ seconds//g' | sed 's/took /\n/g' | tail -1) \ + echo $$?; \ + echo "$${r}s"; \ + echo "$${i};$${t};$${r}" >> $@; \ + done; \ + echo "=== done ===";\ + echo;\ + done + + +tests: md2 + + .PHONY: all clean help \ No newline at end of file diff --git a/Implementierung/benchmarks.csv b/Implementierung/benchmarks.csv new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Implementierung/benchmarks.csv @@ -0,0 +1 @@ + From 558f5092818cf2e1d76f7b159234263fe8e794fa Mon Sep 17 00:00:00 2001 From: finn Date: Wed, 20 Jul 2022 12:11:16 +0200 Subject: [PATCH 12/16] feat: nullchecks updated --- Implementierung/src/io.c | 3 +-- Implementierung/src/main.c | 1 + Implementierung/src/md2_impls/md2_0.c | 6 ++---- Implementierung/src/md2_impls/md2_1.c | 6 ++---- Implementierung/src/md2_impls/md2_2.c | 3 +-- 5 files changed, 7 insertions(+), 12 deletions(-) diff --git a/Implementierung/src/io.c b/Implementierung/src/io.c index 942f235..3dbc7ff 100644 --- a/Implementierung/src/io.c +++ b/Implementierung/src/io.c @@ -35,8 +35,7 @@ uint8_t* read_file(const char* path, size_t* size) { uint8_t* data = malloc(statOfFile.st_size); if (data == NULL) { - printf("Failure: malloc returns NULL"); - return EXIT_FAILURE; + return; } size_t bytesRead = diff --git a/Implementierung/src/main.c b/Implementierung/src/main.c index 42b8e6f..f4e5fc8 100644 --- a/Implementierung/src/main.c +++ b/Implementierung/src/main.c @@ -1,4 +1,5 @@ #include +#include #include "../lib/helper.h" #include "../lib/io.h" diff --git a/Implementierung/src/md2_impls/md2_0.c b/Implementierung/src/md2_impls/md2_0.c index 941b352..93b7dee 100644 --- a/Implementierung/src/md2_impls/md2_0.c +++ b/Implementierung/src/md2_impls/md2_0.c @@ -26,8 +26,7 @@ void md2_hash_0(size_t len, const uint8_t buf[len], uint8_t out[16]) { // +16 for the checksum uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t)); if (newBuf == NULL) { - printf("Failure: calloc returns NULL"); - return EXIT_FAILURE; + return; } memcpy(newBuf, buf, len - paddingNeeded); @@ -49,8 +48,7 @@ void md2_hash_0(size_t len, const uint8_t buf[len], uint8_t out[16]) { // === step 3 === uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t)); if (messageDigestBuf == NULL) { - printf("Failure: calloc returns NULL"); - return EXIT_FAILURE; + return; } diff --git a/Implementierung/src/md2_impls/md2_1.c b/Implementierung/src/md2_impls/md2_1.c index d4fb80b..ac1d867 100644 --- a/Implementierung/src/md2_impls/md2_1.c +++ b/Implementierung/src/md2_impls/md2_1.c @@ -47,8 +47,7 @@ void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) uint8_t* newBuf = aligned_alloc(16, sizeof(uint8_t)*(len + 16)); if (newBuf == NULL) { - printf("Failure: aligned_alloc returns NULL"); - return EXIT_FAILURE; + return; } for(size_t i = 0; i < 16; i++) { @@ -65,8 +64,7 @@ void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) // === step 3 === uint8_t *messageDigestBuf = aligned_alloc(16, sizeof(uint8_t) * 48); if (messageDigestBuf == NULL) { - printf("Failure: aligned_alloc returns NULL"); - return EXIT_FAILURE; + return; } for (size_t i = 0; i < 48; i++) { diff --git a/Implementierung/src/md2_impls/md2_2.c b/Implementierung/src/md2_impls/md2_2.c index 5253ce7..279af73 100644 --- a/Implementierung/src/md2_impls/md2_2.c +++ b/Implementierung/src/md2_impls/md2_2.c @@ -51,8 +51,7 @@ void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) { // === step 3 === uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t)); if (messageDigestBuf == NULL) { - printf("Failure: calloc returns NULL"); - return EXIT_FAILURE; + return; } // === step 4 === From c28c2d16d00488afb434d31e77debb2698292e46 Mon Sep 17 00:00:00 2001 From: finn Date: Wed, 20 Jul 2022 12:16:01 +0200 Subject: [PATCH 13/16] feat: md2_2 nullcheck updated --- Implementierung/src/md2_impls/md2_2.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Implementierung/src/md2_impls/md2_2.c b/Implementierung/src/md2_impls/md2_2.c index 279af73..90fd232 100644 --- a/Implementierung/src/md2_impls/md2_2.c +++ b/Implementierung/src/md2_impls/md2_2.c @@ -58,14 +58,12 @@ void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) { uint8_t l = 0; uint8_t* checksum = calloc(16, sizeof(uint8_t)); if (checksum == NULL) { - printf("Failure: calloc returns NULL"); - return EXIT_FAILURE; + return; } uint8_t* data = malloc(16); if (data == NULL) { - printf("Failure: malloc returns NULL"); - return EXIT_FAILURE; + return; } size_t bytes_left_to_read = file_stat.st_size; size_t bytes_left_to_process = 0; From 81bcf3ea87f0af992a7cdcc6078db0777487e4b8 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Wed, 20 Jul 2022 12:19:58 +0200 Subject: [PATCH 14/16] Fix: makefile benachmarks --- Implementierung/Makefile | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Implementierung/Makefile b/Implementierung/Makefile index 465c11a..07a4b2b 100644 --- a/Implementierung/Makefile +++ b/Implementierung/Makefile @@ -5,7 +5,7 @@ OBJ = ${subst src,build,${SRC:.c=.o}} CC = gcc CFLAGS = -Ilib -ggdb -std=c11 -g -Wall -Wextra -no-pie -O3 LDFLAGS = -pthread -TESTFILES = t/1 t/10 t/100 #t/1000 t/2000 t/5000 t/10000 +TESTFILES = t/10000 t/1 t/10 t/100 t/1000 #t/2000 t/5000 t/10000 TESTFILES_SIZES = ${subst t/,,${TESTFILES}} all: md2 @@ -36,20 +36,19 @@ t/%: @echo "=== done ===" @echo -benchmarks.csv: ${TESTFILES} - @echo "" > $@ +benchmarks.csv: md2 ${TESTFILES} + @rm -f $@ @for i in 0 2 3; do \ echo ;\ echo "=== Testing implementation $$i ===";\ for t in $(TESTFILES_SIZES); do \ echo -n "- with $${t}MB ... "; \ - if ! r=$$(./md2 t/$${t} -B1 -V1); then \ + if ! rr=$$(./md2 t/$${t} -B1 -V$${i}); then \ echo; \ echo "ERROR!"; \ exit 1; \ fi; \ - r=$$(echo $$r | grep "cycles took" | sed 's/ seconds//g' | sed 's/took /\n/g' | tail -1) \ - echo $$?; \ + r=$$(echo $$rr | xargs | sed -e 's/.*took \(.*\) seconds.*/\1/'); \ echo "$${r}s"; \ echo "$${i};$${t};$${r}" >> $@; \ done; \ @@ -57,8 +56,6 @@ benchmarks.csv: ${TESTFILES} echo;\ done +benchmarks: benchmarks.csv -tests: md2 - - -.PHONY: all clean help \ No newline at end of file +.PHONY: all clean help benchmarks \ No newline at end of file From ace85e64a0988a6cad97742af35cd65babd63d5b Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Wed, 20 Jul 2022 12:22:43 +0200 Subject: [PATCH 15/16] Fix: make sure there is enough ram available --- Implementierung/benchmarks.csv | 1 - Implementierung/lib/io.h | 1 + Implementierung/src/io.c | 11 +++++++++++ Implementierung/src/main.c | 4 +++- 4 files changed, 15 insertions(+), 2 deletions(-) delete mode 100644 Implementierung/benchmarks.csv diff --git a/Implementierung/benchmarks.csv b/Implementierung/benchmarks.csv deleted file mode 100644 index 8b13789..0000000 --- a/Implementierung/benchmarks.csv +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Implementierung/lib/io.h b/Implementierung/lib/io.h index 6887f25..532b58c 100644 --- a/Implementierung/lib/io.h +++ b/Implementierung/lib/io.h @@ -9,6 +9,7 @@ #include #include #include +#include /** * @brief Open a file and load its stats diff --git a/Implementierung/src/io.c b/Implementierung/src/io.c index 339bd75..857a996 100644 --- a/Implementierung/src/io.c +++ b/Implementierung/src/io.c @@ -33,7 +33,18 @@ uint8_t* read_file(const char* path, size_t* size) { return NULL; } + struct sysinfo info; + int r = sysinfo(&info); + if (r != 0 || info.freeram < (unsigned long)statOfFile.st_size * 2) { + errno = ENOMEM; + return NULL; + } + uint8_t* data = malloc(statOfFile.st_size); + if (data == NULL) { + fclose(f); + return NULL; + } size_t bytesRead = fread(data, statOfFile.st_blksize, statOfFile.st_blocks, f); diff --git a/Implementierung/src/main.c b/Implementierung/src/main.c index 94b7a7a..5e8a951 100644 --- a/Implementierung/src/main.c +++ b/Implementierung/src/main.c @@ -138,8 +138,10 @@ int main(int argc, char** argv) { char hash[32]; if (!calculate_hash(c, hash)) { if (errno != 0) { - fprintf(stderr, "\n\033[0;31mAn error occured: %s\033[0m\n", + fprintf(stderr, "\n\033[1;31mAn error occured: %s\033[0m\n", strerror(errno)); + if (errno == ENOMEM) + fprintf(stderr, "\033[1;36mPlease try to use -V2!\033[0m\n"); } return EXIT_FAILURE; } From 186ea1f272dc30270bf55fea4b3f1fa32cf18910 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Wed, 20 Jul 2022 12:35:46 +0200 Subject: [PATCH 16/16] Fix: skip on errors --- Implementierung/Makefile | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Implementierung/Makefile b/Implementierung/Makefile index 66d8453..a418eb1 100644 --- a/Implementierung/Makefile +++ b/Implementierung/Makefile @@ -38,19 +38,20 @@ t/%: benchmarks.csv: md2 ${TESTFILES} @rm -f $@ - @for i in 0 2 3; do \ + @for i in 0 1 2 3; do \ echo ;\ echo "=== Testing implementation $$i ===";\ for t in $(TESTFILES_SIZES); do \ echo -n "- with $${t}MB ... "; \ if ! rr=$$(./md2 t/$${t} -B1 -V$${i}); then \ echo; \ - echo "ERROR!"; \ - exit 1; \ + echo "SKIPPED!"; \ + echo "$${i};$${t};0" >> $@; \ + else \ + r=$$(echo $$rr | xargs | sed -e 's/.*took \(.*\) seconds.*/\1/'); \ + echo "$${r}s"; \ + echo "$${i};$${t};$${r}" >> $@; \ fi; \ - r=$$(echo $$rr | xargs | sed -e 's/.*took \(.*\) seconds.*/\1/'); \ - echo "$${r}s"; \ - echo "$${i};$${t};$${r}" >> $@; \ done; \ echo "=== done ===";\ echo;\