📄 md5c.c
字号:
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
/* Zeroize sensitive information.
*/
memset ((POINTER)x, 0, sizeof (x));
}
#endif
/* Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4.
*/
static void Encode (output, input, len)
unsigned char *output;
UINT4 *input;
unsigned int len;
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (unsigned char)(input[i] & 0xff);
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
}
}
#ifndef CPU386 /* Not needed in assembler version */
/* Decodes input (unsigned char) into output (UINT4). Assumes len is
a multiple of 4.
*/
static void Decode (output, input, len)
UINT4 *output;
unsigned char *input;
unsigned int len;
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
(((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
}
#else /* CPU386 */
/* Fast 386 Borland C inline assembler version of the transform() function
* from the RSA Data Security, Inc, MD5 Message Digest Algorithm.
*
* This version uses native 32 bit registers, so it needs a 386 or 486 CPU.
* It also assumes large model (-ml)
*
* Because this function does *lots* of 32-bit operations, this version is
* MUCH faster than the reference C version compiled with a garden-
* variety 16-bit MS-DOS C compiler.
*
* Originally written and placed into the public domain on
* 22 February 1992 by Phil Karn, KA9Q
*
* Updated 1 Sept 1994 by Phil Karn to match newer version of MD5.C released
* in the RSAREF package. Also changed some register assignments to get rid
* of the segment override prefixes on memory references
*/
/* Code sequence common to all four rounds.
* evaluates a = b + (a + edi + x + t) <<< s
* where it is assumed a and b are registers, x is a memory location,
* edi is the edi register, and s and t are integer constants
*/
#define COM(a,b,x,s,t)\
lea a,t[a+edi];\
add a,x;\
rol a,s;\
add a,b;
/* Round 1 functions */
/* edi = F(x,y,z) = (x & y) | (~x & z) */
#define F(x,y,z)\
mov edi,x;\
and edi,y;\
mov ebp,x;\
not ebp;\
and ebp,z;\
or edi,ebp
/* a = b + ((a + F(b,c,d) + x + t) <<< s); */
#define FF(a,b,c,d,x,s,t)\
F(b,c,d);\
COM(a,b,x,s,t)
/* Round 2 functions */
/* edi = G(x,y,z) = F(z,x,y) = (x & z) | (y & ~z) */
#define G(x,y,z) F(z,x,y)
/* a = b + ((a + G(b,c,d) + x + t) <<< s) */
#define GG(a,b,c,d,x,s,t)\
G(b,c,d);\
COM(a,b,x,s,t)
/* Round 3 functions */
/* edi = H(x,y,z) = x ^ y ^ z */
#define H(x,y,z)\
mov edi,x;\
xor edi,y;\
xor edi,z
/* a = b + ((a + H(b,c,d) + x + t) <<< s) */
#define HH(a,b,c,d,x,s,t)\
H(b,c,d);\
COM(a,b,x,s,t)
/* Round 4 functions */
/* edi = I(x,y,z) = y ^ (x | ~z) */
#define I(x,y,z)\
mov edi,z;\
not edi;\
or edi,x;\
xor edi,y
/* a = b + ((a + I(b,c,d) + x + t) <<< s) */
#define II(a,b,c,d,x,s,t)\
I(b,c,d);\
COM(a,b,x,s,t)
/* Register assignments */
#define A eax
#define B ebx
#define C ecx
#define D edx
static void
MD5Transform (state, block)
UINT4 state[4];
unsigned char block[64];
{
asm {
/* Save caller's registers */
push si;
push edi;
push ds;
lds si,state; /* Read input state */
mov A,dword ptr si[0*4]; /* A = state[0] */
mov B,dword ptr si[1*4]; /* B = state[1] */
mov C,dword ptr si[2*4]; /* C = state[2] */
mov D,dword ptr si[3*4]; /* D = state[3] */
lds si,block; /* Set up for data block read */
/* The FF macro uses ebp as scratch. This makes our args
* inaccessible until it is restored!
*/
push ebp;
/* Round 1. The *4 factors in the subscripts to si account for the
* byte offsets of each long element in the input array. The input
* is actually a byte array, but we can treat it directly as a long
* array because MD5 is little-endian, like the 386/486.
*
* The only hazard is if the input buffer isn't 32-bit aligned,
* things will run a little more slowly.
*/
FF(A,B,C,D,si[ 0*4],S11,3614090360); /* 1 */
FF(D,A,B,C,si[ 1*4],S12,3905402710); /* 2 */
FF(C,D,A,B,si[ 2*4],S13, 606105819); /* 3 */
FF(B,C,D,A,si[ 3*4],S14,3250441966); /* 4 */
FF(A,B,C,D,si[ 4*4],S11,4118548399); /* 5 */
FF(D,A,B,C,si[ 5*4],S12,1200080426); /* 6 */
FF(C,D,A,B,si[ 6*4],S13,2821735955); /* 7 */
FF(B,C,D,A,si[ 7*4],S14,4249261313); /* 8 */
FF(A,B,C,D,si[ 8*4],S11,1770035416); /* 9 */
FF(D,A,B,C,si[ 9*4],S12,2336552879); /* 10 */
FF(C,D,A,B,si[10*4],S13,4294925233); /* 11 */
FF(B,C,D,A,si[11*4],S14,2304563134); /* 12 */
FF(A,B,C,D,si[12*4],S11,1804603682); /* 13 */
FF(D,A,B,C,si[13*4],S12,4254626195); /* 14 */
FF(C,D,A,B,si[14*4],S13,2792965006); /* 15 */
FF(B,C,D,A,si[15*4],S14,1236535329); /* 16 */
/* Round 2 */
GG(A,B,C,D,si[ 1*4],S21,4129170786); /* 17 */
GG(D,A,B,C,si[ 6*4],S22,3225465664); /* 18 */
GG(C,D,A,B,si[11*4],S23, 643717713); /* 19 */
GG(B,C,D,A,si[ 0*4],S24,3921069994); /* 20 */
GG(A,B,C,D,si[ 5*4],S21,3593408605); /* 21 */
GG(D,A,B,C,si[10*4],S22, 38016083); /* 22 */
GG(C,D,A,B,si[15*4],S23,3634488961); /* 23 */
GG(B,C,D,A,si[ 4*4],S24,3889429448); /* 24 */
GG(A,B,C,D,si[ 9*4],S21, 568446438); /* 25 */
GG(D,A,B,C,si[14*4],S22,3275163606); /* 26 */
GG(C,D,A,B,si[ 3*4],S23,4107603335); /* 27 */
GG(B,C,D,A,si[ 8*4],S24,1163531501); /* 28 */
GG(A,B,C,D,si[13*4],S21,2850285829); /* 29 */
GG(D,A,B,C,si[ 2*4],S22,4243563512); /* 30 */
GG(C,D,A,B,si[ 7*4],S23,1735328473); /* 31 */
GG(B,C,D,A,si[12*4],S24,2368359562); /* 32 */
/* Round 3 */
HH(A,B,C,D,si[ 5*4],S31,4294588738); /* 33 */
HH(D,A,B,C,si[ 8*4],S32,2272392833); /* 34 */
HH(C,D,A,B,si[11*4],S33,1839030562); /* 35 */
HH(B,C,D,A,si[14*4],S34,4259657740); /* 36 */
HH(A,B,C,D,si[ 1*4],S31,2763975236); /* 37 */
HH(D,A,B,C,si[ 4*4],S32,1272893353); /* 38 */
HH(C,D,A,B,si[ 7*4],S33,4139469664); /* 39 */
HH(B,C,D,A,si[10*4],S34,3200236656); /* 40 */
HH(A,B,C,D,si[13*4],S31, 681279174); /* 41 */
HH(D,A,B,C,si[ 0*4],S32,3936430074); /* 42 */
HH(C,D,A,B,si[ 3*4],S33,3572445317); /* 43 */
HH(B,C,D,A,si[ 6*4],S34, 76029189); /* 44 */
HH(A,B,C,D,si[ 9*4],S31,3654602809); /* 45 */
HH(D,A,B,C,si[12*4],S32,3873151461); /* 46 */
HH(C,D,A,B,si[15*4],S33, 530742520); /* 47 */
HH(B,C,D,A,si[ 2*4],S34,3299628645); /* 48 */
/* Round 4 */
II(A,B,C,D,si[ 0*4],S41,4096336452); /* 49 */
II(D,A,B,C,si[ 7*4],S42,1126891415); /* 50 */
II(C,D,A,B,si[14*4],S43,2878612391); /* 51 */
II(B,C,D,A,si[ 5*4],S44,4237533241); /* 52 */
II(A,B,C,D,si[12*4],S41,1700485571); /* 53 */
II(D,A,B,C,si[ 3*4],S42,2399980690); /* 54 */
II(C,D,A,B,si[10*4],S43,4293915773); /* 55 */
II(B,C,D,A,si[ 1*4],S44,2240044497); /* 56 */
II(A,B,C,D,si[ 8*4],S41,1873313359); /* 57 */
II(D,A,B,C,si[15*4],S42,4264355552); /* 58 */
II(C,D,A,B,si[ 6*4],S43,2734768916); /* 59 */
II(B,C,D,A,si[13*4],S44,1309151649); /* 60 */
II(A,B,C,D,si[ 4*4],S41,4149444226); /* 61 */
II(D,A,B,C,si[11*4],S42,3174756917); /* 62 */
II(C,D,A,B,si[ 2*4],S43, 718787259); /* 63 */
II(B,C,D,A,si[ 9*4],S44,3951481745); /* 64 */
pop ebp; /* We can address our args again */
lds si,state; /* Update the caller's state */
add dword ptr si[0*4],A; /* state[0] += A */
add dword ptr si[1*4],B; /* state[1] += B */
add dword ptr si[2*4],C; /* state[2] += C */
add dword ptr si[3*4],D; /* state[3] += D */
/* Restore caller's registers */
pop ds;
pop edi;
pop si;
}
}
#endif /* CPU386 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -