fix: everything
This commit is contained in:
parent
0d3771c719
commit
9194475153
2 changed files with 20 additions and 16 deletions
|
@ -21,7 +21,7 @@ int main(int argc, char** argv) {
|
||||||
c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles);
|
c.filename, c.implementationToUse, c.doBenchmark, c.benchmarkingCycles);
|
||||||
|
|
||||||
uint8_t out[16];
|
uint8_t out[16];
|
||||||
md2_hash(0, "", out);
|
md2_hash(3, "abc", out);
|
||||||
|
|
||||||
printf("Hash: ");
|
printf("Hash: ");
|
||||||
for (int i = 0; i < 16; i++) {
|
for (int i = 0; i < 16; i++) {
|
||||||
|
|
|
@ -26,7 +26,7 @@ static unsigned char PI_SUBST[256] = {
|
||||||
|
|
||||||
void printBuf(size_t len, uint8_t buf[len]) {
|
void printBuf(size_t len, uint8_t buf[len]) {
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
printf("'%lx',", buf[i]);
|
printf("'%02x',", buf[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
@ -37,42 +37,46 @@ void md2_checksum(size_t len, uint8_t* buf) {
|
||||||
for (int i = 0; i < len / 16; i++) {
|
for (int i = 0; i < len / 16; i++) {
|
||||||
for (int j = 0; j < 16; j++) {
|
for (int j = 0; j < 16; j++) {
|
||||||
u_int8_t c = buf[i * 16 + j];
|
u_int8_t c = buf[i * 16 + j];
|
||||||
buf[len + j] = PI_SUBST[c ^ l];
|
l = buf[len + j] = PI_SUBST[c ^ l];
|
||||||
l = buf[len + j];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void md2_hash(size_t len, const uint8_t buf[len], uint8_t out[16]) {
|
void md2_hash(size_t len, const uint8_t buf[len], uint8_t out[16]) {
|
||||||
// add padding
|
// === step 1 ===
|
||||||
int paddingNeeded = 16 - (len % 16);
|
int paddingNeeded = 16 - (len % 16);
|
||||||
uint8_t originalPadding = paddingNeeded;
|
uint8_t originalPadding = paddingNeeded;
|
||||||
len += paddingNeeded;
|
len += paddingNeeded;
|
||||||
|
|
||||||
printf("len: %d\n", len);
|
printf("len: %d\n", len);
|
||||||
|
|
||||||
uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t)); // +16 = Checksum
|
// +16 for the checksum
|
||||||
|
uint8_t* newBuf = calloc(len + 16, sizeof(uint8_t));
|
||||||
memcpy(newBuf, buf, len - paddingNeeded);
|
memcpy(newBuf, buf, len - paddingNeeded);
|
||||||
newBuf[4] = 'f';
|
|
||||||
|
|
||||||
while (paddingNeeded > 0) {
|
while (paddingNeeded > 0) {
|
||||||
newBuf[len - paddingNeeded] = originalPadding;
|
newBuf[len - paddingNeeded] = originalPadding;
|
||||||
paddingNeeded--;
|
paddingNeeded--;
|
||||||
}
|
}
|
||||||
printBuf(len, newBuf);
|
printf("buf with padding: ");
|
||||||
|
|
||||||
md2_checksum(len, newBuf);
|
|
||||||
|
|
||||||
printBuf(len + 16, newBuf);
|
printBuf(len + 16, newBuf);
|
||||||
|
|
||||||
// implement step 3
|
// === step 2 ===
|
||||||
u_int8_t* messageDigestBuf = calloc(48, sizeof(uint8_t));
|
md2_checksum(len, newBuf);
|
||||||
|
|
||||||
for (int i = 0; i <= len / 16;
|
printf("buf with cecksum: ");
|
||||||
i++) { // <= because we need to hash the last block too
|
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++) {
|
for (int j = 0; j < 16; j++) {
|
||||||
messageDigestBuf[16 + j] = newBuf[i * 16 + j];
|
messageDigestBuf[16 + j] = newBuf[i * 16 + j];
|
||||||
messageDigestBuf[32 + j] = (newBuf[16 + j] ^ messageDigestBuf[j]);
|
messageDigestBuf[32 + j] =
|
||||||
|
(messageDigestBuf[16 + j] ^ messageDigestBuf[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
u_int8_t t = 0;
|
u_int8_t t = 0;
|
||||||
|
|
Loading…
Reference in a new issue