diff --git a/Implementierung/lib/md2_impls/md2_1.h b/Implementierung/lib/md2_impls/md2_1.h new file mode 100644 index 0000000..8777277 --- /dev/null +++ b/Implementierung/lib/md2_impls/md2_1.h @@ -0,0 +1,23 @@ +#ifndef MD2_1_H +#define MD2_1_H + +#include +#include +#include +#include +#include +#include + +#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 \ No newline at end of file diff --git a/Implementierung/src/md2_impls/md2_1.c b/Implementierung/src/md2_impls/md2_1.c new file mode 100644 index 0000000..6e997ed --- /dev/null +++ b/Implementierung/src/md2_impls/md2_1.c @@ -0,0 +1,75 @@ +#include "../../lib/md2_impls/md2_1.h" + +#include "../../lib/md2_impls/md2_common.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]; + } + } +} + +void md2_hash_1(size_t len, const uint8_t buf[len], uint8_t out[16]) { + // === step 1 === + int paddingNeeded = 16 - (len & 7); + 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 2 === + md2_checksum_1(len, newBuf); + + // printf("buf with cecksum: "); + // printBuf(len + 16, newBuf); + + // === step 3 === + uint8_t* messageDigestBuf = calloc(48, sizeof(uint8_t)); + // TODO: add null check + + // === step 4 === + // <= because we need to hash the last block too + for (size_t 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] ^ MD2_PI_SUBST[t]; + } + t = (t + j) & 127; + } + } + // printf("messageDigestBuf: \n"); + // printBuf(16, messageDigestBuf); + + memcpy(out, messageDigestBuf, 16); + + free(messageDigestBuf); + free(newBuf); +} \ No newline at end of file