2022-07-15 11:32:22 +02:00
|
|
|
#include "../../lib/md2_impls/md2_1.h"
|
|
|
|
|
|
|
|
#include "../../lib/md2_impls/md2_common.h"
|
2022-07-18 21:22:03 +02:00
|
|
|
#include <immintrin.h>
|
2022-07-15 11:32:22 +02:00
|
|
|
|
2022-07-18 21:22:03 +02:00
|
|
|
|
|
|
|
void md2_checksum_1(size_t len, uint8_t *buf)
|
|
|
|
{
|
2022-07-15 11:32:22 +02:00
|
|
|
uint8_t l = 0;
|
|
|
|
|
2022-07-18 21:22:03 +02:00
|
|
|
for (size_t i = 0; i < len / 16; i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < 16; j++)
|
|
|
|
{
|
2022-07-15 11:32:22 +02:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 21:22:03 +02:00
|
|
|
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])
|
|
|
|
{
|
2022-07-15 11:32:22 +02:00
|
|
|
// === step 1 ===
|
2022-07-18 21:22:03 +02:00
|
|
|
int paddingNeeded = 16 - (len & 15);
|
2022-07-15 11:32:22 +02:00
|
|
|
len += paddingNeeded;
|
|
|
|
|
|
|
|
// printf("len: %d\n", len);
|
|
|
|
|
|
|
|
// +16 for the checksum
|
2022-07-18 21:22:03 +02:00
|
|
|
uint8_t* newBuf = aligned_alloc(16, sizeof(uint8_t)*(len + 16));
|
|
|
|
|
|
|
|
for(size_t i = 0; i < 16; i++) {
|
|
|
|
newBuf[len + i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t));
|
2022-07-15 11:32:22 +02:00
|
|
|
// TODO: null check
|
|
|
|
memcpy(newBuf, buf, len - paddingNeeded);
|
|
|
|
|
2022-07-18 21:22:03 +02:00
|
|
|
//md2_print_buf(len + 16, newBuf);
|
|
|
|
|
|
|
|
memcpy(newBuf + len - paddingNeeded, PADDING + paddingNeeded, paddingNeeded);
|
2022-07-15 11:32:22 +02:00
|
|
|
|
|
|
|
// printf("buf with padding: ");
|
2022-07-18 21:22:03 +02:00
|
|
|
//md2_print_buf(len + 16, newBuf);
|
2022-07-15 11:32:22 +02:00
|
|
|
|
|
|
|
// === step 2 ===
|
|
|
|
md2_checksum_1(len, newBuf);
|
|
|
|
|
|
|
|
// printf("buf with cecksum: ");
|
|
|
|
// printBuf(len + 16, newBuf);
|
|
|
|
|
|
|
|
// === step 3 ===
|
2022-07-18 21:22:03 +02:00
|
|
|
uint8_t *messageDigestBuf = aligned_alloc(16, sizeof(uint8_t) * 48);
|
2022-07-15 11:32:22 +02:00
|
|
|
// TODO: add null check
|
2022-07-18 21:22:03 +02:00
|
|
|
for (size_t i = 0; i < 48; i++) {
|
|
|
|
messageDigestBuf[i] = 0;
|
|
|
|
}
|
2022-07-15 11:32:22 +02:00
|
|
|
|
|
|
|
// === step 4 ===
|
|
|
|
// <= because we need to hash the last block too
|
2022-07-18 21:22:03 +02:00
|
|
|
__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);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
for (int j = 0; j < 16; j++)
|
|
|
|
{
|
|
|
|
//messageDigestBuf[16 + j] = newBuf[i * 16 + j];
|
2022-07-15 11:32:22 +02:00
|
|
|
messageDigestBuf[32 + j] =
|
|
|
|
(messageDigestBuf[16 + j] ^ messageDigestBuf[j]);
|
|
|
|
}
|
2022-07-18 21:22:03 +02:00
|
|
|
/*
|
|
|
|
vy = _mm_load_si128((__m128i*) (messageDigestBuf));
|
|
|
|
_mm_xor_si128(vy, vx);
|
|
|
|
_mm_store_si128((__m128i*) messageDigestBuf, vy);
|
|
|
|
md2_print_buf(48, messageDigestBuf);
|
|
|
|
md2_print_buf(len + 16, newBuf);
|
|
|
|
/*
|
|
|
|
printf("newBuf: erstes Element:");
|
|
|
|
printf(*(newBuf + i * 16));
|
|
|
|
printf("mdb:");
|
|
|
|
printf(*(messageDigestBuf + 16));
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2022-07-15 11:32:22 +02:00
|
|
|
|
|
|
|
u_int8_t t = 0;
|
|
|
|
|
2022-07-18 21:22:03 +02:00
|
|
|
for (int j = 0; j < 18; j++)
|
|
|
|
{
|
|
|
|
for (int k = 0; k < 48; k++)
|
|
|
|
{
|
2022-07-15 11:32:22 +02:00
|
|
|
t = messageDigestBuf[k] = messageDigestBuf[k] ^ MD2_PI_SUBST[t];
|
|
|
|
}
|
2022-07-18 21:22:03 +02:00
|
|
|
t = (t + j) & 255;
|
2022-07-15 11:32:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// printf("messageDigestBuf: \n");
|
|
|
|
// printBuf(16, messageDigestBuf);
|
|
|
|
|
|
|
|
memcpy(out, messageDigestBuf, 16);
|
|
|
|
|
|
|
|
free(messageDigestBuf);
|
|
|
|
free(newBuf);
|
|
|
|
}
|