Feat: Only use one loop and calculte checksum on the fly

This commit is contained in:
Dorian Zedler 2022-07-12 14:41:18 +02:00
parent d105a6aea9
commit 1e1f4450e8
Signed by: dorian
GPG Key ID: 989DE36109AFA354
4 changed files with 104 additions and 1 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 = 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
OBJ = ${subst src,build,${SRC:.c=.o}}
CC = gcc
CFLAGS = -Ilib -ggdb -std=c11 -g -Wall -Wextra -no-pie -O3

View File

@ -0,0 +1,21 @@
#ifndef MD2_2_H
#define MD2_2_H
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.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_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

View File

@ -2,6 +2,7 @@
// include all implementations
#include "../lib/md2_impls/md2_0.h"
#include "../lib/md2_impls/md2_2.h"
md2_hash_func md2_hash;
md2_checksum_func md2_checksum;
@ -22,6 +23,11 @@ bool md2_choose_implementation(int i) {
md2_checksum = md2_checksum_0;
return true;
case 2:
md2_hash = md2_hash_2;
md2_checksum = md2_checksum_2;
return true;
default:
return false;
}

View File

@ -0,0 +1,76 @@
#include "../../lib/md2_impls/md2_2.h"
#include "../../lib/md2_impls/md2_common.h"
void process_block_hash(uint8_t block[16], uint8_t messageDigestBuf[48]) {
for (int j = 0; j < 16; j++) {
messageDigestBuf[16 + j] = block[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_block_checksum(uint8_t block[16], uint8_t checksum[16],
uint8_t* l) {
for (int j = 0; j < 16; j++) {
u_int8_t c = block[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)];
}
}
void md2_checksum_2(size_t len, uint8_t* buf) {
uint8_t l = 0;
for (size_t i = 0; i < len / 16; i++) {
process_block_checksum(buf + (i * 16), buf + len, &l);
}
}
void md2_hash_2(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;
// 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 3 ===
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
// === step 4 ===
uint8_t l = 0;
// <= because we need to hash the last block too
for (size_t i = 0; i < len / 16; i++) {
process_block_checksum(newBuf + (i * 16), newBuf + len, &l);
process_block_hash(newBuf + (i * 16), messageDigestBuf);
}
process_block_hash(newBuf + len, messageDigestBuf);
memcpy(out, messageDigestBuf, 16);
free(newBuf);
}