Feat: Boilerplate for multiple impls
This commit is contained in:
parent
65b433e41c
commit
cab96d88c0
6 changed files with 51 additions and 108 deletions
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -12,7 +12,8 @@
|
||||||
"variant": "c",
|
"variant": "c",
|
||||||
"array": "c",
|
"array": "c",
|
||||||
"md2.h": "c",
|
"md2.h": "c",
|
||||||
"io.h": "c"
|
"io.h": "c",
|
||||||
|
"md2_0.h": "c"
|
||||||
},
|
},
|
||||||
"editor.formatOnSave": true,
|
"editor.formatOnSave": true,
|
||||||
"C_Cpp.default.includePath": [
|
"C_Cpp.default.includePath": [
|
||||||
|
|
1
.vscode/tasks.json
vendored
1
.vscode/tasks.json
vendored
|
@ -8,6 +8,7 @@
|
||||||
"-fdiagnostics-color=always",
|
"-fdiagnostics-color=always",
|
||||||
"-g",
|
"-g",
|
||||||
"${fileDirname}/*.c",
|
"${fileDirname}/*.c",
|
||||||
|
"${fileDirname}/md2_impls/*.c",
|
||||||
"-o",
|
"-o",
|
||||||
"${fileDirname}/../md2"
|
"${fileDirname}/../md2"
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
|
|
||||||
SRC = src/main.c src/helper.c src/io.c src/md2.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
|
||||||
OBJ = ${subst src,build,${SRC:.c=.o}}
|
OBJ = ${subst src,build,${SRC:.c=.o}}
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -Ilib -ggdb
|
CFLAGS = -Ilib -ggdb
|
||||||
|
@ -23,7 +23,7 @@ build:
|
||||||
mkdir build
|
mkdir build
|
||||||
|
|
||||||
build/%.o: src/%.c
|
build/%.o: src/%.c
|
||||||
@mkdir -p build
|
@mkdir -p build/md2_impls
|
||||||
${CC} -c ${CFLAGS} -o $@ $<
|
${CC} -c ${CFLAGS} -o $@ $<
|
||||||
|
|
||||||
md2: ${OBJ}
|
md2: ${OBJ}
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
#ifndef MD2_H
|
#ifndef MD2_H
|
||||||
#define MD2_H
|
#define MD2_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
typedef void (*md2_hash_func)(size_t len, const uint8_t buf[], uint8_t out[16]);
|
||||||
|
typedef void (*md2_checksum_func)(size_t len, uint8_t* buf);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Calculates checksum of buf and stores it in out
|
* @brief Calculates checksum of buf and stores it in out
|
||||||
*
|
*
|
||||||
|
@ -14,7 +18,7 @@
|
||||||
* @param buf Location of the data
|
* @param buf Location of the data
|
||||||
* @param out Destination array of for the checksum
|
* @param out Destination array of for the checksum
|
||||||
*/
|
*/
|
||||||
void md2_hash(size_t len, const uint8_t buf[], uint8_t out[16]);
|
extern md2_hash_func md2_hash;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Calculates checksum of buf and appends it to buf
|
* @brief Calculates checksum of buf and appends it to buf
|
||||||
|
@ -23,7 +27,16 @@ void md2_hash(size_t len, const uint8_t buf[], uint8_t out[16]);
|
||||||
* @param buf Location of the data. Make sure to reserve 16 bytes more so the
|
* @param buf Location of the data. Make sure to reserve 16 bytes more so the
|
||||||
* chechsum fits!
|
* chechsum fits!
|
||||||
*/
|
*/
|
||||||
void md2_checksum(size_t len, uint8_t* buf);
|
extern md2_checksum_func md2_checksum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Choose the implementation to use
|
||||||
|
*
|
||||||
|
* @param i the implementation
|
||||||
|
* @return true when the implemenatation was changed
|
||||||
|
* @return false when the implementation does not exist
|
||||||
|
*/
|
||||||
|
bool md2_choose_implementation(int i);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Takes a uint8_t array and converts it into a string
|
* @brief Takes a uint8_t array and converts it into a string
|
||||||
|
@ -31,6 +44,6 @@ void md2_checksum(size_t len, uint8_t* buf);
|
||||||
* @param hash the array containing the hash
|
* @param hash the array containing the hash
|
||||||
* @param stringHash pointer to store the string, has to have 32 bytes space
|
* @param stringHash pointer to store the string, has to have 32 bytes space
|
||||||
*/
|
*/
|
||||||
void encodeHash(uint8_t hash[16], char* stringHash);
|
void md2_encode_hash(uint8_t hash[16], char* string_hash);
|
||||||
|
|
||||||
#endif // MD2_H
|
#endif // MD2_H
|
|
@ -10,7 +10,7 @@ static bool runTest(const char* message, const char* expectedHash) {
|
||||||
md2_hash(strlen(message), message, out);
|
md2_hash(strlen(message), message, out);
|
||||||
|
|
||||||
char hash[32];
|
char hash[32];
|
||||||
encodeHash(out, hash);
|
md2_encode_hash(out, hash);
|
||||||
|
|
||||||
bool ok = !strcmp(hash, expectedHash);
|
bool ok = !strcmp(hash, expectedHash);
|
||||||
printf("%s: md2(%s) %s == %s\n", "not ok" + (4 * ok), message, hash,
|
printf("%s: md2(%s) %s == %s\n", "not ok" + (4 * ok), message, hash,
|
||||||
|
@ -20,7 +20,7 @@ static bool runTest(const char* message, const char* expectedHash) {
|
||||||
|
|
||||||
unsigned runTests(void) {
|
unsigned runTests(void) {
|
||||||
unsigned failed = 0;
|
unsigned failed = 0;
|
||||||
// Note: cases with n<2 are not tested here.
|
// src: https://datatracker.ietf.org/doc/html/rfc1319#appendix-A.5
|
||||||
failed += !runTest("", "8350e5a3e24c153df2275c9f80692773");
|
failed += !runTest("", "8350e5a3e24c153df2275c9f80692773");
|
||||||
failed += !runTest("a", "32ec01ec4a6dac72c0ab96fb34c0b5d1");
|
failed += !runTest("a", "32ec01ec4a6dac72c0ab96fb34c0b5d1");
|
||||||
failed += !runTest("abc", "da853b0d3f88d99b30283a69e6ded6bb");
|
failed += !runTest("abc", "da853b0d3f88d99b30283a69e6ded6bb");
|
||||||
|
@ -60,12 +60,20 @@ int main(int argc, char** argv) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!md2_choose_implementation(c.implementationToUse)) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"%s: invalid argument, implementation '%d' does not exist!\n",
|
||||||
|
argv[0], c.implementationToUse);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
printf(
|
printf(
|
||||||
"Hashing file: %s\nUsing implementation: %d, doing benchmark: %d, "
|
"Hashing file: %s\nUsing implementation: %d, doing benchmark: %d, "
|
||||||
"benchmark cycles: %d\n",
|
"benchmark cycles: %d\n",
|
||||||
c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles);
|
c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles);
|
||||||
|
|
||||||
// runTests();
|
// runTests();
|
||||||
|
// return 0;
|
||||||
size_t len;
|
size_t len;
|
||||||
uint8_t* data = read_file(c.filename, &len);
|
uint8_t* data = read_file(c.filename, &len);
|
||||||
if (data == NULL) {
|
if (data == NULL) {
|
||||||
|
@ -81,7 +89,7 @@ int main(int argc, char** argv) {
|
||||||
|
|
||||||
printf("Hash: ");
|
printf("Hash: ");
|
||||||
char hash[32];
|
char hash[32];
|
||||||
encodeHash(out, hash);
|
md2_encode_hash(out, hash);
|
||||||
printf("%s\n", hash);
|
printf("%s\n", hash);
|
||||||
|
|
||||||
free(data);
|
free(data);
|
||||||
|
|
|
@ -1,107 +1,27 @@
|
||||||
#include "../lib/md2.h"
|
#include "../lib/md2.h"
|
||||||
|
|
||||||
|
// include all implementations
|
||||||
|
#include "../lib/md2_impls/md2_0.h"
|
||||||
|
|
||||||
|
md2_hash_func md2_hash;
|
||||||
|
md2_checksum_func md2_checksum;
|
||||||
|
|
||||||
// The file "testfile" should lead to this hash:
|
// The file "testfile" should lead to this hash:
|
||||||
// fc982e558db259f298b43cd4c1241c66
|
// fc982e558db259f298b43cd4c1241c66
|
||||||
|
|
||||||
// Source: https://datatracker.ietf.org/doc/html/rfc1319
|
void md2_encode_hash(uint8_t hash[16], char* string_hash) {
|
||||||
static unsigned char PI_SUBST[256] = {
|
|
||||||
41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
|
|
||||||
19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76,
|
|
||||||
130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138,
|
|
||||||
23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142,
|
|
||||||
187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16,
|
|
||||||
137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, 39, 53, 62,
|
|
||||||
204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, 181, 209, 215,
|
|
||||||
94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, 150, 164, 125, 182,
|
|
||||||
118, 252, 107, 226, 156, 116, 4, 241, 69, 157, 112, 89, 100, 113, 135,
|
|
||||||
32, 134, 91, 207, 101, 230, 45, 168, 2, 27, 96, 37, 173, 174, 176,
|
|
||||||
185, 246, 28, 70, 97, 105, 52, 64, 126, 15, 85, 71, 163, 35, 221,
|
|
||||||
81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38, 44, 83, 13, 110,
|
|
||||||
133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82, 106, 220, 55,
|
|
||||||
200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74, 120, 136,
|
|
||||||
149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57, 242,
|
|
||||||
239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10,
|
|
||||||
49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131,
|
|
||||||
20};
|
|
||||||
|
|
||||||
void printBuf(size_t len, uint8_t buf[len]) {
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
printf("'%02x',", buf[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void md2_checksum(size_t len, uint8_t* buf) {
|
|
||||||
uint8_t l = 0;
|
|
||||||
|
|
||||||
for (int 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 = buf[len + j] ^= PI_SUBST[c ^ l];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void md2_hash(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));
|
|
||||||
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(len, newBuf);
|
|
||||||
|
|
||||||
// printf("buf with cecksum: ");
|
|
||||||
// printBuf(len + 16, newBuf);
|
|
||||||
|
|
||||||
// === step 3 ===
|
|
||||||
uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
|
|
||||||
|
|
||||||
// === step 4 ===
|
|
||||||
// <= because we need to hash the last block too
|
|
||||||
for (int 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] ^ PI_SUBST[t];
|
|
||||||
}
|
|
||||||
t = (t + j) % 256;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// printf("messageDigestBuf: \n");
|
|
||||||
// printBuf(16, messageDigestBuf);
|
|
||||||
|
|
||||||
memcpy(out, messageDigestBuf, 16);
|
|
||||||
|
|
||||||
free(messageDigestBuf);
|
|
||||||
free(newBuf);
|
|
||||||
}
|
|
||||||
|
|
||||||
void encodeHash(uint8_t hash[16], char* stringHash) {
|
|
||||||
for (int i = 0; i < 16; i++) {
|
for (int i = 0; i < 16; i++) {
|
||||||
sprintf(stringHash + (2 * i), "%02x", hash[i]);
|
sprintf(string_hash + (2 * i), "%02x", hash[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool md2_choose_implementation(int i) {
|
||||||
|
switch (i) {
|
||||||
|
case 0:
|
||||||
|
md2_hash = md2_hash_0;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue