Merge pull request 'feat/impl_1' (#7) from feat/-1 into master
Reviewed-on: https://gitlab-gepasp.in.tum.de/gra22s/team199/pulls/7
This commit is contained in:
commit
332b2a8b7c
7 changed files with 149 additions and 3 deletions
|
@ -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/md2_impls/md2_3.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}}
|
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
|
||||||
|
|
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
|
|
@ -1,4 +1,5 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <sys/resource.h>
|
||||||
|
|
||||||
#include "../lib/helper.h"
|
#include "../lib/helper.h"
|
||||||
#include "../lib/io.h"
|
#include "../lib/io.h"
|
||||||
|
|
|
@ -2,6 +2,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_1.h"
|
||||||
#include "../lib/md2_impls/md2_2.h"
|
#include "../lib/md2_impls/md2_2.h"
|
||||||
#include "../lib/md2_impls/md2_3.h"
|
#include "../lib/md2_impls/md2_3.h"
|
||||||
|
|
||||||
|
@ -24,6 +25,11 @@ bool md2_choose_implementation(int i) {
|
||||||
md2_checksum = md2_checksum_0;
|
md2_checksum = md2_checksum_0;
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
md2_hash = md2_hash_1;
|
||||||
|
md2_checksum = md2_checksum_1;
|
||||||
|
return true;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
md2_hash = md2_hash_2;
|
md2_hash = md2_hash_2;
|
||||||
md2_checksum = NULL;
|
md2_checksum = NULL;
|
||||||
|
|
|
@ -25,7 +25,9 @@ void md2_hash_0(size_t len, const uint8_t buf[len], uint8_t out[16]) {
|
||||||
|
|
||||||
// +16 for the checksum
|
// +16 for the checksum
|
||||||
uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t));
|
uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t));
|
||||||
// TODO: null check
|
if (newBuf == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
memcpy(newBuf, buf, len - paddingNeeded);
|
memcpy(newBuf, buf, len - paddingNeeded);
|
||||||
|
|
||||||
// printBuf(len + 16, newBuf);
|
// 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 ===
|
// === step 3 ===
|
||||||
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
|
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
|
||||||
// TODO: add null check
|
if (messageDigestBuf == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// === step 4 ===
|
// === step 4 ===
|
||||||
// <= because we need to hash the last block too
|
// <= 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);
|
||||||
|
}
|
|
@ -47,12 +47,21 @@ void md2_hash_2(size_t len, const uint8_t buf[len], uint8_t out[16]) {
|
||||||
|
|
||||||
// === step 3 ===
|
// === step 3 ===
|
||||||
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
|
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
|
||||||
|
if (messageDigestBuf == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// === step 4 ===
|
// === step 4 ===
|
||||||
uint8_t l = 0;
|
uint8_t l = 0;
|
||||||
uint8_t* checksum = calloc(16, sizeof(uint8_t));
|
uint8_t* checksum = calloc(16, sizeof(uint8_t));
|
||||||
|
if (checksum == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t* data = malloc(16);
|
uint8_t* data = malloc(16);
|
||||||
|
if (data == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
size_t bytes_left_to_read = file_stat.st_size;
|
size_t bytes_left_to_read = file_stat.st_size;
|
||||||
size_t bytes_left_to_process = 0;
|
size_t bytes_left_to_process = 0;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue