📄 mech_md5.c
字号:
/* the way C will index them, from 0..63 instead of 1..64.) *//* *//* If the index of each entry is i, where i ranges from 1 to 64, then the *//* value in each entry is given by the following formula. *//* *//* T[i] = int ( 4294967296 * abs ( sin ( i ) ) ) *//* *//* where the function sin(i) expects i in radians, and the function int(x) *//* returns the integer portion of a floating point number x. *//* *//*----------------------------------------------------------------------------*/static CK_ULONG T[64] = { 0xD76AA478, /* T[01] */ 0xE8C7B756, /* T[02] */ 0x242070DB, /* T[03] */ 0xC1BDCEEE, /* T[04] */ 0xF57C0FAF, /* T[05] */ 0x4787C62A, /* T[06] */ 0xA8304613, /* T[07] */ 0xFD469501, /* T[08] */ 0x698098D8, /* T[09] */ 0x8B44F7AF, /* T[10] */ 0xFFFF5BB1, /* T[11] */ 0x895CD7BE, /* T[12] */ 0x6B901122, /* T[13] */ 0xFD987193, /* T[14] */ 0xA679438E, /* T[15] */ 0x49B40821, /* T[16] */ 0xF61E2562, /* T[17] */ 0xC040B340, /* T[18] */ 0x265E5A51, /* T[19] */ 0xE9B6C7AA, /* T[20] */ 0xD62F105D, /* T[21] */ 0x02441453, /* T[22] */ 0xD8A1E681, /* T[23] */ 0xE7D3FBC8, /* T[24] */ 0x21E1CDE6, /* T[25] */ 0xC33707D6, /* T[26] */ 0xF4D50D87, /* T[27] */ 0x455A14ED, /* T[28] */ 0xA9E3E905, /* T[29] */ 0xFCEFA3F8, /* T[30] */ 0x676F02D9, /* T[31] */ 0x8D2A4C8A, /* T[32] */ 0xFFFA3942, /* T[33] */ 0x8771F681, /* T[34] */ 0x6D9D6122, /* T[35] */ 0xFDE5380C, /* T[36] */ 0xA4BEEA44, /* T[37] */ 0x4BDECFA9, /* T[38] */ 0xF6BB4B60, /* T[39] */ 0xBEBFBC70, /* T[40] */ 0x289B7EC6, /* T[41] */ 0xEAA127FA, /* T[42] */ 0xD4EF3085, /* T[43] */ 0x04881D05, /* T[44] */ 0xD9D4D039, /* T[45] */ 0xE6DB99E5, /* T[46] */ 0x1FA27CF8, /* T[47] */ 0xC4AC5665, /* T[48] */ 0xF4292244, /* T[49] */ 0x432AFF97, /* T[50] */ 0xAB9423A7, /* T[51] */ 0xFC93A039, /* T[52] */ 0x655B59C3, /* T[53] */ 0x8F0CCC92, /* T[54] */ 0xFFEFF47D, /* T[55] */ 0x85845DD1, /* T[56] */ 0x6FA87E4F, /* T[57] */ 0xFE2CE6E0, /* T[58] */ 0xA3014314, /* T[59] */ 0x4E0811A1, /* T[60] */ 0xF7537E82, /* T[61] */ 0xBD3AF235, /* T[62] */ 0x2AD7D2BB, /* T[63] */ 0xEB86D391 }; /* T[64] */// Basic MD5 step. Transform buf based on in.//voidckm_md5_transform( CK_ULONG *long_buf, CK_ULONG *long_in ){ /*-------------------------------------------------------------------------*/ /* The inputs to this function SHOULD be 4 4-byte elements of buf[] and */ /* 16 4-byte elements of in[]. There are architectures, however -- */ /* 64-bit Linux390 among them--in which CK_ULONG translates to an 8-byte */ /* number. Therefore this function must copy inputs to 4-byte temps and */ /* copy the temps back into the 8-byte arrays at the end. */ /*-------------------------------------------------------------------------*/ /* */ /* Macro ROUND_FCN defines the common function that is performed */ /* throughout the MD5 round. Parameters are: */ /* */ /* - The name of the function to be performed on the data for this */ /* round. There are four logical functions, F, G, H, and I, and they */ /* are each used throughout the algorithm. */ /* */ /* - a, b, c, and d are the four md5-word parameters to the functions */ /* F, G, H, and I. They are replaced with varying permutations of the */ /* accumulated hash values in A, B, C, and D. */ /* */ /* - x_index is an index into the in[] array, where in[] is the input */ /* block of message text. */ /* */ /* - t_index is an index into the T[] array, a set of constants. */ /* */ /* - rotate_cnt is the number of bits the result must be rotated. */ /* */ /*-------------------------------------------------------------------------*/ CK_ULONG AA = 0x00000000; /* Temp. save areas for A, B, C, D */ CK_ULONG BB = 0x00000000; /* Temp. save areas for A, B, C, D */ CK_ULONG CC = 0x00000000; /* Temp. save areas for A, B, C, D */ CK_ULONG DD = 0x00000000; /* Temp. save areas for A, B, C, D */ CK_ULONG_32 buf[4]; // temps for long_buf[i] CK_ULONG_32 in[16]; // temps for long_in[i] int i; // loop counter #define ROUND_FCN(FCN, a, b, c, d, x_index, rotate_cnt, t_index) \ { a += FCN(b,c,d) + in[x_index] + T[t_index-1]; \ rotate_left( a, rotate_cnt); \ a += b; \ } /* Save the MD buffer in the temporary locations AA-DD. */ AA = long_buf[0]; BB = long_buf[1]; CC = long_buf[2]; DD = long_buf[3]; // Copy the input long_buf elements into buf and long_in elements into in for (i=0;i<4;i++) { buf[i] = (CK_ULONG_32)long_buf[i]; in[i] = (CK_ULONG_32)long_in[i]; } for (i=4;i<16;i++) in[i] = (CK_ULONG_32)long_in[i]; /*==================================================================*/ /* */ /* Process the four rounds for each 16-word block. */ /* */ /* The function for each of these has the form: */ /* */ /* a = b + (( a + fcn( b, c, d ) + in[k] + T[i] ) <<< s ) */ /* */ /* for a function fcn() which can be F, G, H, or I, and for input */ /* values a, b, c, d, k, i, and s. Array T is the array of */ /* constants, computed from the sin() function. Array in is the */ /* current input block. Value s is the number of bits to rotate */ /* left, where <<< represents a 32-bit left rotation. */ /* */ /* The definitions of these functions are taken directly from the */ /* definition of MD5 in RFC 1321. */ /* */ /*==================================================================*/ /*------------------------------------------------------------------*/ /* */ /* Round 1 */ /* */ /*------------------------------------------------------------------*/ ROUND_FCN(F, buf[0], buf[1], buf[2], buf[3], 0, 7, 1); ROUND_FCN(F, buf[3], buf[0], buf[1], buf[2], 1, 12, 2); ROUND_FCN(F, buf[2], buf[3], buf[0], buf[1], 2, 17, 3); ROUND_FCN(F, buf[1], buf[2], buf[3], buf[0], 3, 22, 4); ROUND_FCN(F, buf[0], buf[1], buf[2], buf[3], 4, 7, 5); ROUND_FCN(F, buf[3], buf[0], buf[1], buf[2], 5, 12, 6); ROUND_FCN(F, buf[2], buf[3], buf[0], buf[1], 6, 17, 7); ROUND_FCN(F, buf[1], buf[2], buf[3], buf[0], 7, 22, 8); ROUND_FCN(F, buf[0], buf[1], buf[2], buf[3], 8, 7, 9); ROUND_FCN(F, buf[3], buf[0], buf[1], buf[2], 9, 12, 10); ROUND_FCN(F, buf[2], buf[3], buf[0], buf[1], 10, 17, 11); ROUND_FCN(F, buf[1], buf[2], buf[3], buf[0], 11, 22, 12); ROUND_FCN(F, buf[0], buf[1], buf[2], buf[3], 12, 7, 13); ROUND_FCN(F, buf[3], buf[0], buf[1], buf[2], 13, 12, 14); ROUND_FCN(F, buf[2], buf[3], buf[0], buf[1], 14, 17, 15); ROUND_FCN(F, buf[1], buf[2], buf[3], buf[0], 15, 22, 16); /*------------------------------------------------------------------*/ /* */ /* Round 2 */ /* */ /*------------------------------------------------------------------*/ ROUND_FCN(G, buf[0], buf[1], buf[2], buf[3], 1, 5, 17); ROUND_FCN(G, buf[3], buf[0], buf[1], buf[2], 6, 9, 18); ROUND_FCN(G, buf[2], buf[3], buf[0], buf[1], 11, 14, 19); ROUND_FCN(G, buf[1], buf[2], buf[3], buf[0], 0, 20, 20); ROUND_FCN(G, buf[0], buf[1], buf[2], buf[3], 5, 5, 21); ROUND_FCN(G, buf[3], buf[0], buf[1], buf[2], 10, 9, 22); ROUND_FCN(G, buf[2], buf[3], buf[0], buf[1], 15, 14, 23); ROUND_FCN(G, buf[1], buf[2], buf[3], buf[0], 4, 20, 24); ROUND_FCN(G, buf[0], buf[1], buf[2], buf[3], 9, 5, 25); ROUND_FCN(G, buf[3], buf[0], buf[1], buf[2], 14, 9, 26); ROUND_FCN(G, buf[2], buf[3], buf[0], buf[1], 3, 14, 27); ROUND_FCN(G, buf[1], buf[2], buf[3], buf[0], 8, 20, 28); ROUND_FCN(G, buf[0], buf[1], buf[2], buf[3], 13, 5, 29); ROUND_FCN(G, buf[3], buf[0], buf[1], buf[2], 2, 9, 30); ROUND_FCN(G, buf[2], buf[3], buf[0], buf[1], 7, 14, 31); ROUND_FCN(G, buf[1], buf[2], buf[3], buf[0], 12, 20, 32); /*------------------------------------------------------------------*/ /* */ /* Round 3 */ /* */ /*------------------------------------------------------------------*/ ROUND_FCN(H, buf[0], buf[1], buf[2], buf[3], 5, 4, 33); ROUND_FCN(H, buf[3], buf[0], buf[1], buf[2], 8, 11, 34); ROUND_FCN(H, buf[2], buf[3], buf[0], buf[1], 11, 16, 35); ROUND_FCN(H, buf[1], buf[2], buf[3], buf[0], 14, 23, 36); ROUND_FCN(H, buf[0], buf[1], buf[2], buf[3], 1, 4, 37); ROUND_FCN(H, buf[3], buf[0], buf[1], buf[2], 4, 11, 38); ROUND_FCN(H, buf[2], buf[3], buf[0], buf[1], 7, 16, 39); ROUND_FCN(H, buf[1], buf[2], buf[3], buf[0], 10, 23, 40); ROUND_FCN(H, buf[0], buf[1], buf[2], buf[3], 13, 4, 41); ROUND_FCN(H, buf[3], buf[0], buf[1], buf[2], 0, 11, 42); ROUND_FCN(H, buf[2], buf[3], buf[0], buf[1], 3, 16, 43); ROUND_FCN(H, buf[1], buf[2], buf[3], buf[0], 6, 23, 44); ROUND_FCN(H, buf[0], buf[1], buf[2], buf[3], 9, 4, 45); ROUND_FCN(H, buf[3], buf[0], buf[1], buf[2], 12, 11, 46); ROUND_FCN(H, buf[2], buf[3], buf[0], buf[1], 15, 16, 47); ROUND_FCN(H, buf[1], buf[2], buf[3], buf[0], 2, 23, 48); /*------------------------------------------------------------------*/ /* */ /* Round 4 */ /* */ /*------------------------------------------------------------------*/ ROUND_FCN(I, buf[0], buf[1], buf[2], buf[3], 0, 6, 49); ROUND_FCN(I, buf[3], buf[0], buf[1], buf[2], 7, 10, 50); ROUND_FCN(I, buf[2], buf[3], buf[0], buf[1], 14, 15, 51); ROUND_FCN(I, buf[1], buf[2], buf[3], buf[0], 5, 21, 52); ROUND_FCN(I, buf[0], buf[1], buf[2], buf[3], 12, 6, 53); ROUND_FCN(I, buf[3], buf[0], buf[1], buf[2], 3, 10, 54); ROUND_FCN(I, buf[2], buf[3], buf[0], buf[1], 10, 15, 55); ROUND_FCN(I, buf[1], buf[2], buf[3], buf[0], 1, 21, 56); ROUND_FCN(I, buf[0], buf[1], buf[2], buf[3], 8, 6, 57); ROUND_FCN(I, buf[3], buf[0], buf[1], buf[2], 15, 10, 58); ROUND_FCN(I, buf[2], buf[3], buf[0], buf[1], 6, 15, 59); ROUND_FCN(I, buf[1], buf[2], buf[3], buf[0], 13, 21, 60); ROUND_FCN(I, buf[0], buf[1], buf[2], buf[3], 4, 6, 61); ROUND_FCN(I, buf[3], buf[0], buf[1], buf[2], 11, 10, 62); ROUND_FCN(I, buf[2], buf[3], buf[0], buf[1], 2, 15, 63); ROUND_FCN(I, buf[1], buf[2], buf[3], buf[0], 9, 21, 64); // Copy the elements of buf into long_buf for (i=0;i<4;i++) long_buf[i] = (CK_ULONG) buf[i]; /* Add to each MD buffer variable the value it had before this block was */ /* started. */ long_buf[0] += AA; long_buf[1] += BB; long_buf[2] += CC; long_buf[3] += DD; /* Undefine the ROUND_FCN macro we used in this function. */ #undef ROUND_FCN}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -