feat: implement threading

This commit is contained in:
Thomas Florian 2022-07-15 00:01:35 +02:00
parent befa4de16f
commit b8670dfe62
5 changed files with 161 additions and 2 deletions

View file

@ -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}} 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

View file

@ -0,0 +1,23 @@
#ifndef MD2_3_H
#define MD2_3_H
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
/**
* @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

View file

@ -3,6 +3,7 @@
// include all implementations // include all implementations
#include "../lib/md2_impls/md2_0.h" #include "../lib/md2_impls/md2_0.h"
#include "../lib/md2_impls/md2_2.h" #include "../lib/md2_impls/md2_2.h"
#include "../lib/md2_impls/md2_3.h"
md2_hash_func md2_hash; md2_hash_func md2_hash;
md2_checksum_func md2_checksum; md2_checksum_func md2_checksum;
@ -28,6 +29,11 @@ bool md2_choose_implementation(int i) {
md2_checksum = md2_checksum_2; md2_checksum = md2_checksum_2;
return true; return true;
case 3:
md2_hash = md2_hash_3;
md2_checksum = md2_checksum_3;
return true;
default: default:
return false; return false;
} }

View file

@ -39,7 +39,7 @@ void apply_padding(size_t len, uint8_t buf[16]) {
} }
// unused! // 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]) { void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) {
FILE* file; FILE* file;

View file

@ -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);
}