|
|
#include "global.h" ( G$ l# D: T' v, `
#include "md5.h" + s6 a) h0 a8 O8 m3 M' B: q
: f$ {- S8 O* v6 Y* [/* Constants for MD5Transform routine. */
7 S) W( y& l! a* ~ : o; h8 y7 g/ p/ U
2 k: R/ J p7 f3 a q/ T2 u
#define S11 7
% a- J3 s: Y2 |0 R/ D/ v5 S#define S12 12 3 G3 Q7 E* Z3 X- i! z& e0 O. p
#define S13 17
2 r# ]. j. y+ z4 U#define S14 22
4 F9 i/ t/ h$ {0 @$ |# ]#define S21 5
. T; l# Z! M* z8 _5 y: J#define S22 9
1 S/ `% a+ E% ?9 J/ C#define S23 14
8 J. ~! G. ?( ~& `#define S24 20 * i& ]- F/ C( F0 E" o
#define S31 4 , e; \- E5 S$ _: F. i1 V
#define S32 11
# j# A. R. |. q#define S33 16
; G: `! C1 h0 E#define S34 23 , e* G+ X8 q2 a4 }- U
#define S41 6 & `0 x) D% c- u) R. e
#define S42 10 9 k7 R A$ |! V% D z1 Z
#define S43 15
. A/ F6 Y$ j3 A8 ]. K' K1 `$ ~#define S44 21
7 |6 t: y4 a3 d# K# G# Z ( b# A* w4 S5 w) y
static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
7 `0 u* l* N$ p9 |static void Encode PROTO_LIST ((unsigned char *, UINT4 *, unsigned int)); - u+ k9 \' @* k8 h7 R
static void Decode PROTO_LIST ((UINT4 *, unsigned char *, unsigned int)); " g$ @( r0 W1 u5 ^. r2 E; a
static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
$ ~+ W: _) P5 T" n0 k$ \ W8 R2 a5 dstatic void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
. l/ K; l' n( P: m) ~& z & F/ C6 [$ w- Z$ q
static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 % {- f% m( A. J T% r7 a# u+ q
}; 1 `' ]: ^2 j2 m& c, T9 b& R
/ E+ n- f t; f L6 H5 [9 M; J5 o
/* F, G, H and I are basic MD5 functions. */
& L3 P# _2 K0 t! O$ s+ `#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 2 A& `" T) R# N% ?, ^& x _! o
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
) I. s7 F3 b& J v! [#define H(x, y, z) ((x) ^ (y) ^ (z))
2 N! H4 |' U$ n) s, y#define I(x, y, z) ((y) ^ ((x) | (~z)))
, f% o7 G8 _; z* W$ d5 J' C* l
N1 ~! I2 @4 P9 B, L/* ROTATE_LEFT rotates x left n bits. */
6 \; t9 t/ v6 B; g2 B#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
! I, t& ]* S c 3 n( v7 I" V5 e% S
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* {/ y+ S& J- D5 k) ~4 zRotation is separate from addition to prevent recomputation. */
6 |4 e, R: i1 B/ v) E0 X#define FF(a, b, c, d, x, s, ac) { \ (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ }
4 z) W+ n h/ E+ }; B#define GG(a, b, c, d, x, s, ac) { \ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } ' x& }; P8 S0 q0 p
#define HH(a, b, c, d, x, s, ac) { \ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } 1 N3 r. z9 s& w- T- e& g
#define II(a, b, c, d, x, s, ac) { \ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ }
$ r& T! g) P* q( |% ^& V) n- E
. Z% j- @$ w+ f( {1 R0 A/* MD5 initialization. Begins an MD5 operation, writing a new context. */
2 R" i' K; b% O( R7 a( a# |5 n" V. Evoid MD5Init (context)
0 V7 v% ?! Y/ c. E# L! dMD5_CTX *context; /* context */ ( k. x% c9 p5 F, U8 g( E U
{ context->count[0] = context->count[1] = 0; /* Load magic initialization constants. [; r, }! ], N- h2 Q5 f& \
*/ context->state[0] = 0x67452301; context->state[1] = 0xefcdab89; context->state[2] = 0x98badcfe; context->state[3] = 0x10325476; ; x6 X9 a4 K- V: M" F
} ' O( I. v8 R- j5 h7 R N
% i5 W$ T3 X. m
/* MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. */ u2 y3 l7 M, E$ ^: y, V) N
void MD5Update (context, input, inputLen)
; i0 ?6 W1 l/ W6 s# F/ xMD5_CTX *context; /* context */ # I* _) S$ V M2 E1 h# g7 ^# F
unsigned char *input; /* input block */ 6 U; I7 _$ `' F+ v* g1 b+ u
unsigned int inputLen; /* length of input block */
9 V% U# X0 e0 l, O2 t, a& z, t{ unsigned int i, index, partLen; 8 G3 D$ U) K a8 R
/* Compute number of bytes mod 64 */ index = (unsigned int)((context->count[0] >> 3) & 0x3F); % |4 _+ W/ X6 A" l+ C3 h; u
/* Update number of bits */ if ((context->count[0] += ((UINT4)inputLen << 3)) & |
|