Skip to content

Instantly share code, notes, and snippets.

/md5_propagation.c
Last active Jul 8, 2016

Embed
What would you like to do?
md5 propagation study
#include <stdlib.h>
#include <stdio.h>
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
unsigned int rot(unsigned int val, unsigned int idx)
{
return val << idx | val >> (32 - idx);
}
unsigned int f(unsigned int A, unsigned int B, unsigned int C, unsigned int D){
unsigned int T = 0;
unsigned int n = 0;
unsigned int s = 0;
unsigned int index[16] = {7,12,17,22,5,9,14,20,4,11,16,23,6,10,15,21};
for(n = 0; n < 64; n++)
{
A = A | B | C | D;
s = 4 * (n % 16) + n % 4;
A = rot(A ,index[s]);
T = A;
A = D;
D = C;
C = B;
B = T;
printf("round %2i:",n);
printf(" "BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN,
BYTE_TO_BINARY(A>>24), BYTE_TO_BINARY(A>>16), BYTE_TO_BINARY(A>>8), BYTE_TO_BINARY(A));
printf(" "BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN,
BYTE_TO_BINARY(B>>24), BYTE_TO_BINARY(B>>16), BYTE_TO_BINARY(B>>8), BYTE_TO_BINARY(B));
printf(" "BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN,
BYTE_TO_BINARY(C>>24), BYTE_TO_BINARY(C>>16), BYTE_TO_BINARY(C>>8), BYTE_TO_BINARY(C));
printf(" "BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN BYTE_TO_BINARY_PATTERN,
BYTE_TO_BINARY(D>>24), BYTE_TO_BINARY(D>>16), BYTE_TO_BINARY(D>>8), BYTE_TO_BINARY(D));
printf("\n");
if(A == B && A == C && A == D && A == 0xFFFFFFFF)
return n;
}
return 64;
}
int main()
{
unsigned int res[128];
unsigned int i;
unsigned int max = 0;
for(i = 0; i < 32; i ++)
{
printf("%8x\n",1 << i);
res[i] = f(0,0,0,1 << i);
}
for(i = 0; i < 32; i ++)
{
printf("%8x\n",1 << i);
res[32+i] = f(0,0, 1 << i,0);
}
for(i = 0; i < 32; i ++)
{
printf("%8x\n",1 << i);
res[64+i] = f(0,1 << i,0,0);
}
for(i = 0; i < 32; i ++)
{
printf("%8x\n",1 << i);
res[96+i] = f(1 << i,0,0,0);
}
for(i = 0; i < 128; i++){
if(res[i] > max)
max = res[i];
}
printf("max num of rounds: %i\n",max);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.