00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <string.h>
00026 #include <vlc/vlc.h>
00027 #include <vlc_md5.h>
00028
00029 #ifdef WORDS_BIGENDIAN
00030
00031
00032
00033 static inline void Reverse( uint32_t *p_buffer, int n )
00034 {
00035 int i;
00036
00037 for( i = 0; i < n; i++ )
00038 {
00039 p_buffer[ i ] = GetDWLE(&p_buffer[ i ]);
00040 }
00041 }
00042 # define REVERSE( p, n ) Reverse( p, n )
00043 #else
00044 # define REVERSE( p, n )
00045 #endif
00046
00047 #define F1( x, y, z ) ((z) ^ ((x) & ((y) ^ (z))))
00048 #define F2( x, y, z ) F1((z), (x), (y))
00049 #define F3( x, y, z ) ((x) ^ (y) ^ (z))
00050 #define F4( x, y, z ) ((y) ^ ((x) | ~(z)))
00051
00052 #define MD5_DO( f, w, x, y, z, data, s ) \
00053 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
00054
00055
00056
00057
00058 void DigestMD5( struct md5_s *p_md5, uint32_t *p_input )
00059 {
00060 uint32_t a, b, c, d;
00061
00062 REVERSE( p_input, 16 );
00063
00064 a = p_md5->p_digest[ 0 ];
00065 b = p_md5->p_digest[ 1 ];
00066 c = p_md5->p_digest[ 2 ];
00067 d = p_md5->p_digest[ 3 ];
00068
00069 MD5_DO( F1, a, b, c, d, p_input[ 0 ] + 0xd76aa478, 7 );
00070 MD5_DO( F1, d, a, b, c, p_input[ 1 ] + 0xe8c7b756, 12 );
00071 MD5_DO( F1, c, d, a, b, p_input[ 2 ] + 0x242070db, 17 );
00072 MD5_DO( F1, b, c, d, a, p_input[ 3 ] + 0xc1bdceee, 22 );
00073 MD5_DO( F1, a, b, c, d, p_input[ 4 ] + 0xf57c0faf, 7 );
00074 MD5_DO( F1, d, a, b, c, p_input[ 5 ] + 0x4787c62a, 12 );
00075 MD5_DO( F1, c, d, a, b, p_input[ 6 ] + 0xa8304613, 17 );
00076 MD5_DO( F1, b, c, d, a, p_input[ 7 ] + 0xfd469501, 22 );
00077 MD5_DO( F1, a, b, c, d, p_input[ 8 ] + 0x698098d8, 7 );
00078 MD5_DO( F1, d, a, b, c, p_input[ 9 ] + 0x8b44f7af, 12 );
00079 MD5_DO( F1, c, d, a, b, p_input[ 10 ] + 0xffff5bb1, 17 );
00080 MD5_DO( F1, b, c, d, a, p_input[ 11 ] + 0x895cd7be, 22 );
00081 MD5_DO( F1, a, b, c, d, p_input[ 12 ] + 0x6b901122, 7 );
00082 MD5_DO( F1, d, a, b, c, p_input[ 13 ] + 0xfd987193, 12 );
00083 MD5_DO( F1, c, d, a, b, p_input[ 14 ] + 0xa679438e, 17 );
00084 MD5_DO( F1, b, c, d, a, p_input[ 15 ] + 0x49b40821, 22 );
00085
00086 MD5_DO( F2, a, b, c, d, p_input[ 1 ] + 0xf61e2562, 5 );
00087 MD5_DO( F2, d, a, b, c, p_input[ 6 ] + 0xc040b340, 9 );
00088 MD5_DO( F2, c, d, a, b, p_input[ 11 ] + 0x265e5a51, 14 );
00089 MD5_DO( F2, b, c, d, a, p_input[ 0 ] + 0xe9b6c7aa, 20 );
00090 MD5_DO( F2, a, b, c, d, p_input[ 5 ] + 0xd62f105d, 5 );
00091 MD5_DO( F2, d, a, b, c, p_input[ 10 ] + 0x02441453, 9 );
00092 MD5_DO( F2, c, d, a, b, p_input[ 15 ] + 0xd8a1e681, 14 );
00093 MD5_DO( F2, b, c, d, a, p_input[ 4 ] + 0xe7d3fbc8, 20 );
00094 MD5_DO( F2, a, b, c, d, p_input[ 9 ] + 0x21e1cde6, 5 );
00095 MD5_DO( F2, d, a, b, c, p_input[ 14 ] + 0xc33707d6, 9 );
00096 MD5_DO( F2, c, d, a, b, p_input[ 3 ] + 0xf4d50d87, 14 );
00097 MD5_DO( F2, b, c, d, a, p_input[ 8 ] + 0x455a14ed, 20 );
00098 MD5_DO( F2, a, b, c, d, p_input[ 13 ] + 0xa9e3e905, 5 );
00099 MD5_DO( F2, d, a, b, c, p_input[ 2 ] + 0xfcefa3f8, 9 );
00100 MD5_DO( F2, c, d, a, b, p_input[ 7 ] + 0x676f02d9, 14 );
00101 MD5_DO( F2, b, c, d, a, p_input[ 12 ] + 0x8d2a4c8a, 20 );
00102
00103 MD5_DO( F3, a, b, c, d, p_input[ 5 ] + 0xfffa3942, 4 );
00104 MD5_DO( F3, d, a, b, c, p_input[ 8 ] + 0x8771f681, 11 );
00105 MD5_DO( F3, c, d, a, b, p_input[ 11 ] + 0x6d9d6122, 16 );
00106 MD5_DO( F3, b, c, d, a, p_input[ 14 ] + 0xfde5380c, 23 );
00107 MD5_DO( F3, a, b, c, d, p_input[ 1 ] + 0xa4beea44, 4 );
00108 MD5_DO( F3, d, a, b, c, p_input[ 4 ] + 0x4bdecfa9, 11 );
00109 MD5_DO( F3, c, d, a, b, p_input[ 7 ] + 0xf6bb4b60, 16 );
00110 MD5_DO( F3, b, c, d, a, p_input[ 10 ] + 0xbebfbc70, 23 );
00111 MD5_DO( F3, a, b, c, d, p_input[ 13 ] + 0x289b7ec6, 4 );
00112 MD5_DO( F3, d, a, b, c, p_input[ 0 ] + 0xeaa127fa, 11 );
00113 MD5_DO( F3, c, d, a, b, p_input[ 3 ] + 0xd4ef3085, 16 );
00114 MD5_DO( F3, b, c, d, a, p_input[ 6 ] + 0x04881d05, 23 );
00115 MD5_DO( F3, a, b, c, d, p_input[ 9 ] + 0xd9d4d039, 4 );
00116 MD5_DO( F3, d, a, b, c, p_input[ 12 ] + 0xe6db99e5, 11 );
00117 MD5_DO( F3, c, d, a, b, p_input[ 15 ] + 0x1fa27cf8, 16 );
00118 MD5_DO( F3, b, c, d, a, p_input[ 2 ] + 0xc4ac5665, 23 );
00119
00120 MD5_DO( F4, a, b, c, d, p_input[ 0 ] + 0xf4292244, 6 );
00121 MD5_DO( F4, d, a, b, c, p_input[ 7 ] + 0x432aff97, 10 );
00122 MD5_DO( F4, c, d, a, b, p_input[ 14 ] + 0xab9423a7, 15 );
00123 MD5_DO( F4, b, c, d, a, p_input[ 5 ] + 0xfc93a039, 21 );
00124 MD5_DO( F4, a, b, c, d, p_input[ 12 ] + 0x655b59c3, 6 );
00125 MD5_DO( F4, d, a, b, c, p_input[ 3 ] + 0x8f0ccc92, 10 );
00126 MD5_DO( F4, c, d, a, b, p_input[ 10 ] + 0xffeff47d, 15 );
00127 MD5_DO( F4, b, c, d, a, p_input[ 1 ] + 0x85845dd1, 21 );
00128 MD5_DO( F4, a, b, c, d, p_input[ 8 ] + 0x6fa87e4f, 6 );
00129 MD5_DO( F4, d, a, b, c, p_input[ 15 ] + 0xfe2ce6e0, 10 );
00130 MD5_DO( F4, c, d, a, b, p_input[ 6 ] + 0xa3014314, 15 );
00131 MD5_DO( F4, b, c, d, a, p_input[ 13 ] + 0x4e0811a1, 21 );
00132 MD5_DO( F4, a, b, c, d, p_input[ 4 ] + 0xf7537e82, 6 );
00133 MD5_DO( F4, d, a, b, c, p_input[ 11 ] + 0xbd3af235, 10 );
00134 MD5_DO( F4, c, d, a, b, p_input[ 2 ] + 0x2ad7d2bb, 15 );
00135 MD5_DO( F4, b, c, d, a, p_input[ 9 ] + 0xeb86d391, 21 );
00136
00137 p_md5->p_digest[ 0 ] += a;
00138 p_md5->p_digest[ 1 ] += b;
00139 p_md5->p_digest[ 2 ] += c;
00140 p_md5->p_digest[ 3 ] += d;
00141 }
00142
00143
00144
00145
00146
00147
00148 void InitMD5( struct md5_s *p_md5 )
00149 {
00150 p_md5->p_digest[ 0 ] = 0x67452301;
00151 p_md5->p_digest[ 1 ] = 0xefcdab89;
00152 p_md5->p_digest[ 2 ] = 0x98badcfe;
00153 p_md5->p_digest[ 3 ] = 0x10325476;
00154
00155 memset( p_md5->p_data, 0, 64 );
00156 p_md5->i_bits = 0;
00157 }
00158
00159
00160
00161
00162 void AddMD5( struct md5_s *p_md5, const uint8_t *p_src, uint32_t i_len )
00163 {
00164 unsigned int i_current;
00165 unsigned int i_offset = 0;
00166
00167 i_current = (p_md5->i_bits / 8) & 63;
00168
00169 p_md5->i_bits += 8 * i_len;
00170
00171
00172
00173 if( i_len >= (64 - i_current) )
00174 {
00175 memcpy( ((uint8_t *)p_md5->p_data) + i_current, p_src,
00176 (64 - i_current) );
00177 DigestMD5( p_md5, p_md5->p_data );
00178
00179 i_offset += (64 - i_current);
00180 i_len -= (64 - i_current);
00181 i_current = 0;
00182 }
00183
00184
00185 while( i_len >= 64 )
00186 {
00187 uint32_t p_tmp[ 16 ];
00188 memcpy( p_tmp, p_src + i_offset, 64 );
00189 DigestMD5( p_md5, p_tmp );
00190 i_offset += 64;
00191 i_len -= 64;
00192 }
00193
00194
00195 memcpy( ((uint8_t *)p_md5->p_data) + i_current, p_src + i_offset, i_len );
00196 }
00197
00198
00199
00200
00201
00202
00203
00204 void EndMD5( struct md5_s *p_md5 )
00205 {
00206 unsigned int i_current;
00207
00208 i_current = (p_md5->i_bits / 8) & 63;
00209
00210
00211
00212 ((uint8_t *)p_md5->p_data)[ i_current++ ] = 0x80;
00213
00214
00215
00216
00217 if( i_current > 56 )
00218 {
00219 memset( ((uint8_t *)p_md5->p_data) + i_current, 0, (64 - i_current) );
00220 DigestMD5( p_md5, p_md5->p_data );
00221 i_current = 0;
00222 }
00223
00224
00225
00226 memset( ((uint8_t *)p_md5->p_data) + i_current, 0, (56 - i_current) );
00227 p_md5->p_data[ 14 ] = p_md5->i_bits & 0xffffffff;
00228 p_md5->p_data[ 15 ] = (p_md5->i_bits >> 32);
00229 REVERSE( &p_md5->p_data[ 14 ], 2 );
00230
00231 DigestMD5( p_md5, p_md5->p_data );
00232 }
00233
00234