Merge branch 'master' into ReferenceImplementation
This commit is contained in:
commit
ee94b144b5
14 changed files with 347 additions and 19 deletions
2
Implementierung/.gitignore
vendored
2
Implementierung/.gitignore
vendored
|
@ -1,3 +1,3 @@
|
|||
build
|
||||
md2
|
||||
testfile*
|
||||
t
|
|
@ -1,10 +1,12 @@
|
|||
|
||||
|
||||
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_1.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
|
||||
TESTFILES = t/10000 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,34 @@ build/%.o: src/%.c
|
|||
md2: ${OBJ}
|
||||
${CC} -o $@ $(OBJ) ${LDFLAGS}
|
||||
|
||||
.PHONY: all clean help
|
||||
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: md2 ${TESTFILES}
|
||||
@rm -f $@
|
||||
@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 "SKIPPED!"; \
|
||||
echo "$${i};$${t};0" >> $@; \
|
||||
else \
|
||||
r=$$(echo $$rr | xargs | sed -e 's/.*took \(.*\) seconds.*/\1/'); \
|
||||
echo "$${r}s"; \
|
||||
echo "$${i};$${t};$${r}" >> $@; \
|
||||
fi; \
|
||||
done; \
|
||||
echo "=== done ===";\
|
||||
echo;\
|
||||
done
|
||||
|
||||
benchmarks: benchmarks.csv
|
||||
|
||||
.PHONY: all clean help benchmarks
|
|
@ -9,6 +9,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysinfo.h>
|
||||
|
||||
/**
|
||||
* @brief Open a file and load its stats
|
||||
|
|
23
Implementierung/lib/md2_impls/md2_1.h
Normal file
23
Implementierung/lib/md2_impls/md2_1.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef MD2_1_H
|
||||
#define MD2_1_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#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
|
|
@ -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
|
22
Implementierung/lib/md2_impls/md2_3.h
Normal file
22
Implementierung/lib/md2_impls/md2_3.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#ifndef MD2_3_H
|
||||
#define MD2_3_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* @brief Diese Implementierung benutzt Threads zum Berechnen des Hashs
|
||||
*
|
||||
* @param len
|
||||
* @param buf
|
||||
* @param out
|
||||
*/
|
||||
void md2_hash_3(size_t len, const uint8_t buf[len], uint8_t out[16]);
|
||||
|
||||
#endif // MD2_3_H
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef MD2_COMMON_H
|
||||
#define MD2_COMMON_H
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
#include <stdbool.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
#include "../lib/helper.h"
|
||||
#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];
|
||||
|
@ -139,8 +139,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;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
// include all implementations
|
||||
#include "../lib/md2_impls/md2_0.h"
|
||||
#include "../lib/md2_impls/md2_1.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;
|
||||
|
@ -23,9 +25,19 @@ 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;
|
||||
md2_checksum = NULL;
|
||||
return true;
|
||||
|
||||
case 3:
|
||||
md2_hash = md2_hash_3;
|
||||
md2_checksum = NULL;
|
||||
return true;
|
||||
|
||||
default:
|
||||
|
|
|
@ -25,7 +25,9 @@ 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) {
|
||||
return;
|
||||
}
|
||||
memcpy(newBuf, buf, len - paddingNeeded);
|
||||
|
||||
// printBuf(len + 16, newBuf);
|
||||
|
@ -45,7 +47,10 @@ 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) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// === step 4 ===
|
||||
// <= because we need to hash the last block too
|
||||
|
|
102
Implementierung/src/md2_impls/md2_1.c
Normal file
102
Implementierung/src/md2_impls/md2_1.c
Normal file
|
@ -0,0 +1,102 @@
|
|||
#include "../../lib/md2_impls/md2_1.h"
|
||||
|
||||
#include "../../lib/md2_impls/md2_common.h"
|
||||
#include <immintrin.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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 & 15);
|
||||
len += paddingNeeded;
|
||||
|
||||
uint8_t* newBuf = aligned_alloc(16, sizeof(uint8_t)*(len + 16));
|
||||
if (newBuf == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < 16; i++) {
|
||||
newBuf[len + i] = 0;
|
||||
}
|
||||
|
||||
memcpy(newBuf, buf, len - paddingNeeded);
|
||||
|
||||
memcpy(newBuf + len - paddingNeeded, PADDING + paddingNeeded, paddingNeeded);
|
||||
|
||||
// === step 2 ===
|
||||
md2_checksum_1(len, newBuf);
|
||||
|
||||
// === step 3 ===
|
||||
uint8_t *messageDigestBuf = aligned_alloc(16, sizeof(uint8_t) * 48);
|
||||
if (messageDigestBuf == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 48; i++) {
|
||||
messageDigestBuf[i] = 0;
|
||||
}
|
||||
|
||||
// === step 4 ===
|
||||
__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);
|
||||
|
||||
|
||||
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) & 255;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(out, messageDigestBuf, 16);
|
||||
|
||||
free(messageDigestBuf);
|
||||
free(newBuf);
|
||||
}
|
|
@ -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;
|
||||
|
@ -50,20 +47,29 @@ 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) {
|
||||
return;
|
||||
}
|
||||
|
||||
// === step 4 ===
|
||||
uint8_t l = 0;
|
||||
uint8_t* checksum = calloc(16, sizeof(uint8_t));
|
||||
if (checksum == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t* data = malloc(16);
|
||||
if (data == NULL) {
|
||||
return;
|
||||
}
|
||||
size_t bytes_left_to_read = file_stat.st_size;
|
||||
size_t bytes_left_to_process = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
114
Implementierung/src/md2_impls/md2_3.c
Normal file
114
Implementierung/src/md2_impls/md2_3.c
Normal file
|
@ -0,0 +1,114 @@
|
|||
#include "../../lib/md2_impls/md2_3.h"
|
||||
|
||||
#include "../../lib/md2_impls/md2_common.h"
|
||||
|
||||
struct thread_args {
|
||||
size_t len;
|
||||
const uint8_t* buf;
|
||||
};
|
||||
|
||||
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++) {
|
||||
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_hash(void* threadArgs) {
|
||||
struct thread_args* args = (struct thread_args*)threadArgs;
|
||||
|
||||
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
|
||||
if (messageDigestBuf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
process_nothread_hash(args->len, args->buf, messageDigestBuf);
|
||||
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 NULL;
|
||||
}
|
||||
|
||||
uint8_t l = 0;
|
||||
|
||||
for (size_t i = 0; i < args->len / 16; i++) {
|
||||
for (int j = 0; j < 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 = checksum[j] ^= MD2_PI_SUBST[c ^ l];
|
||||
}
|
||||
}
|
||||
pthread_exit(checksum);
|
||||
}
|
||||
|
||||
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;
|
||||
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--;
|
||||
}
|
||||
|
||||
pthread_t thread_1, thread_2;
|
||||
|
||||
struct thread_args thread_args = {len, newBuf};
|
||||
|
||||
if (pthread_create(&thread_1, NULL, process_hash, (void*)&thread_args) != 0) {
|
||||
printf("Error creating thread 1\n");
|
||||
if (errno == 0) errno = EAGAIN;
|
||||
return;
|
||||
}
|
||||
if (pthread_create(&thread_2, NULL, process_checksum, (void*)&thread_args) !=
|
||||
0) {
|
||||
printf("Error creating thread 2\n");
|
||||
if (errno == 0) errno = EAGAIN;
|
||||
return;
|
||||
}
|
||||
|
||||
u_int8_t* messageDigestBuf;
|
||||
u_int8_t* checksum;
|
||||
|
||||
if (pthread_join(thread_1, (void**)&messageDigestBuf) != 0) {
|
||||
printf("Error joining thread 1\n");
|
||||
if (errno == 0) errno = EINVAL;
|
||||
return;
|
||||
}
|
||||
if (pthread_join(thread_2, (void**)&checksum) != 0) {
|
||||
printf("Error joining thread 2\n");
|
||||
if (errno == 0) errno = EINVAL;
|
||||
return;
|
||||
}
|
||||
|
||||
process_nothread_hash(16, checksum, messageDigestBuf);
|
||||
memcpy(out, messageDigestBuf, 16);
|
||||
|
||||
free(messageDigestBuf);
|
||||
free(checksum);
|
||||
}
|
Loading…
Reference in a new issue