source: rtems/cpukit/libmd/md4.c @ e0c4c10

4.104.114.84.95
Last change on this file since e0c4c10 was e0c4c10, checked in by Ralf Corsepius <ralf.corsepius@…>, on 06/12/07 at 15:59:36

New (moved out from pppd).

  • Property mode set to 100644
File size: 8.5 KB
Line 
1/*
2** ********************************************************************
3** md4.c -- Implementation of MD4 Message Digest Algorithm           **
4** Updated: 2/16/90 by Ronald L. Rivest                              **
5** (C) 1990 RSA Data Security, Inc.                                  **
6** ********************************************************************
7*/
8
9/*
10** To use MD4:
11**   -- Include md4.h in your program
12**   -- Declare an MDstruct MD to hold the state of the digest
13**          computation.
14**   -- Initialize MD using MDbegin(&MD)
15**   -- For each full block (64 bytes) X you wish to process, call
16**          MD4Update(&MD,X,512)
17**      (512 is the number of bits in a full block.)
18**   -- For the last block (less than 64 bytes) you wish to process,
19**          MD4Update(&MD,X,n)
20**      where n is the number of bits in the partial block. A partial
21**      block terminates the computation, so every MD computation
22**      should terminate by processing a partial block, even if it
23**      has n = 0.
24**   -- The message digest is available in MD.buffer[0] ...
25**      MD.buffer[3].  (Least-significant byte of each word
26**      should be output first.)
27**   -- You can print out the digest using MDprint(&MD)
28*/
29
30/* Implementation notes:
31** This implementation assumes that ints are 32-bit quantities.
32*/
33
34#define TRUE  1
35#define FALSE 0
36
37/* Compile-time includes
38*/
39#include <stdio.h>
40#include "md4.h"
41
42#include <inttypes.h>
43
44/* Compile-time declarations of MD4 "magic constants".
45*/
46#define I0  0x67452301L       /* Initial values for MD buffer */
47#define I1  0xefcdab89L
48#define I2  0x98badcfeL
49#define I3  0x10325476L
50#define C2  013240474631L     /* round 2 constant = sqrt(2) in octal */
51#define C3  015666365641L     /* round 3 constant = sqrt(3) in octal */
52/* C2 and C3 are from Knuth, The Art of Programming, Volume 2
53** (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
54** Table 2, page 660.
55*/
56
57#define fs1  3               /* round 1 shift amounts */
58#define fs2  7
59#define fs3 11
60#define fs4 19
61#define gs1  3               /* round 2 shift amounts */
62#define gs2  5
63#define gs3  9
64#define gs4 13
65#define hs1  3               /* round 3 shift amounts */
66#define hs2  9
67#define hs3 11
68#define hs4 15
69
70/* Compile-time macro declarations for MD4.
71** Note: The "rot" operator uses the variable "tmp".
72** It assumes tmp is declared as unsigned int, so that the >>
73** operator will shift in zeros rather than extending the sign bit.
74*/
75#define f(X,Y,Z)             ((X&Y) | ((~X)&Z))
76#define g(X,Y,Z)             ((X&Y) | (X&Z) | (Y&Z))
77#define h(X,Y,Z)             (X^Y^Z)
78#define rot(X,S)             (tmp=X,(tmp<<S) | (tmp>>(32-S)))
79#define ff(A,B,C,D,i,s)      A = rot((A + f(B,C,D) + X[i]),s)
80#define gg(A,B,C,D,i,s)      A = rot((A + g(B,C,D) + X[i] + C2),s)
81#define hh(A,B,C,D,i,s)      A = rot((A + h(B,C,D) + X[i] + C3),s)
82
83/* MD4print(MDp)
84** Print message digest buffer MDp as 32 hexadecimal digits.
85** Order is from low-order byte of buffer[0] to high-order byte of
86** buffer[3].
87** Each byte is printed with high-order hexadecimal digit first.
88** This is a user-callable routine.
89*/
90void
91MD4Print(MDp)
92MD4_CTX *MDp;
93{
94  int i,j;
95  for (i=0;i<4;i++)
96    for (j=0;j<32;j=j+8)
97      printf("%02" PRIx32,(MDp->buffer[i]>>j) & 0xFF);
98}
99
100/* MD4Init(MDp)
101** Initialize message digest buffer MDp.
102** This is a user-callable routine.
103*/
104void
105MD4Init(MDp)
106MD4_CTX *MDp;
107{
108  int i;
109  MDp->buffer[0] = I0;
110  MDp->buffer[1] = I1;
111  MDp->buffer[2] = I2;
112  MDp->buffer[3] = I3;
113  for (i=0;i<8;i++) MDp->count[i] = 0;
114  MDp->done = 0;
115}
116
117/* MDblock(MDp,X)
118** Update message digest buffer MDp->buffer using 16-word data block X.
119** Assumes all 16 words of X are full of data.
120** Does not update MDp->count.
121** This routine is not user-callable.
122*/
123static void
124MDblock(MDp,Xb)
125MD4_CTX *MDp;
126unsigned char *Xb;
127{
128  register uint32_t tmp, A, B, C, D;
129  uint32_t X[16];
130  int i;
131
132  for (i = 0; i < 16; ++i) {
133    X[i] = Xb[0] + ((uint32_t)Xb[1] << 8) +
134           ((uint32_t)Xb[2] << 16) + ((uint32_t)Xb[3] << 24);
135    Xb += 4;
136  }
137
138  A = MDp->buffer[0];
139  B = MDp->buffer[1];
140  C = MDp->buffer[2];
141  D = MDp->buffer[3];
142  /* Update the message digest buffer */
143  ff(A , B , C , D ,  0 , fs1); /* Round 1 */
144  ff(D , A , B , C ,  1 , fs2);
145  ff(C , D , A , B ,  2 , fs3);
146  ff(B , C , D , A ,  3 , fs4);
147  ff(A , B , C , D ,  4 , fs1);
148  ff(D , A , B , C ,  5 , fs2);
149  ff(C , D , A , B ,  6 , fs3);
150  ff(B , C , D , A ,  7 , fs4);
151  ff(A , B , C , D ,  8 , fs1);
152  ff(D , A , B , C ,  9 , fs2);
153  ff(C , D , A , B , 10 , fs3);
154  ff(B , C , D , A , 11 , fs4);
155  ff(A , B , C , D , 12 , fs1);
156  ff(D , A , B , C , 13 , fs2);
157  ff(C , D , A , B , 14 , fs3);
158  ff(B , C , D , A , 15 , fs4);
159  gg(A , B , C , D ,  0 , gs1); /* Round 2 */
160  gg(D , A , B , C ,  4 , gs2);
161  gg(C , D , A , B ,  8 , gs3);
162  gg(B , C , D , A , 12 , gs4);
163  gg(A , B , C , D ,  1 , gs1);
164  gg(D , A , B , C ,  5 , gs2);
165  gg(C , D , A , B ,  9 , gs3);
166  gg(B , C , D , A , 13 , gs4);
167  gg(A , B , C , D ,  2 , gs1);
168  gg(D , A , B , C ,  6 , gs2);
169  gg(C , D , A , B , 10 , gs3);
170  gg(B , C , D , A , 14 , gs4);
171  gg(A , B , C , D ,  3 , gs1);
172  gg(D , A , B , C ,  7 , gs2);
173  gg(C , D , A , B , 11 , gs3);
174  gg(B , C , D , A , 15 , gs4);
175  hh(A , B , C , D ,  0 , hs1); /* Round 3 */
176  hh(D , A , B , C ,  8 , hs2);
177  hh(C , D , A , B ,  4 , hs3);
178  hh(B , C , D , A , 12 , hs4);
179  hh(A , B , C , D ,  2 , hs1);
180  hh(D , A , B , C , 10 , hs2);
181  hh(C , D , A , B ,  6 , hs3);
182  hh(B , C , D , A , 14 , hs4);
183  hh(A , B , C , D ,  1 , hs1);
184  hh(D , A , B , C ,  9 , hs2);
185  hh(C , D , A , B ,  5 , hs3);
186  hh(B , C , D , A , 13 , hs4);
187  hh(A , B , C , D ,  3 , hs1);
188  hh(D , A , B , C , 11 , hs2);
189  hh(C , D , A , B ,  7 , hs3);
190  hh(B , C , D , A , 15 , hs4);
191  MDp->buffer[0] += A;
192  MDp->buffer[1] += B;
193  MDp->buffer[2] += C;
194  MDp->buffer[3] += D;
195}
196
197/* MD4Update(MDp,X,count)
198** Input: X -- a pointer to an array of unsigned characters.
199**        count -- the number of bits of X to use.
200**          (if not a multiple of 8, uses high bits of last byte.)
201** Update MDp using the number of bits of X given by count.
202** This is the basic input routine for an MD4 user.
203** The routine completes the MD computation when count < 512, so
204** every MD computation should end with one call to MD4Update with a
205** count less than 512.  A call with count 0 will be ignored if the
206** MD has already been terminated (done != 0), so an extra call with
207** count 0 can be given as a "courtesy close" to force termination
208** if desired.
209*/
210void
211MD4Update(MDp,X,count)
212MD4_CTX *MDp;
213unsigned char *X;
214unsigned int count;
215{
216  unsigned int i, tmp, bit, byte, mask;
217  unsigned char XX[64];
218  unsigned char *p;
219
220  /* return with no error if this is a courtesy close with count
221  ** zero and MDp->done is true.
222  */
223  if (count == 0 && MDp->done) return;
224  /* check to see if MD is already done and report error */
225  if (MDp->done)
226  { printf("\nError: MD4Update MD already done."); return; }
227
228  /* Add count to MDp->count */
229  tmp = count;
230  p = MDp->count;
231  while (tmp)
232  { tmp += *p;
233  *p++ = tmp;
234  tmp = tmp >> 8;
235  }
236
237  /* Process data */
238  if (count == 512)
239  { /* Full block of data to handle */
240    MDblock(MDp,X);
241  }
242  else if (count > 512) /* Check for count too large */
243  {
244    printf("\nError: MD4Update called with illegal count value %d.",
245           count);
246    return;
247  }
248  else /* partial block -- must be last block so finish up */
249  {
250    /* Find out how many bytes and residual bits there are */
251    byte = count >> 3;
252    bit =  count & 7;
253    /* Copy X into XX since we need to modify it */
254    for (i=0;i<=byte;i++)   XX[i] = X[i];
255    for (i=byte+1;i<64;i++) XX[i] = 0;
256    /* Add padding '1' bit and low-order zeros in last byte */
257    mask = 1 << (7 - bit);
258    XX[byte] = (XX[byte] | mask) & ~( mask - 1);
259    /* If room for bit count, finish up with this block */
260    if (byte <= 55)
261    {
262      for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
263      MDblock(MDp,XX);
264    }
265    else /* need to do two blocks to finish up */
266    {
267      MDblock(MDp,XX);
268      for (i=0;i<56;i++) XX[i] = 0;
269      for (i=0;i<8;i++)  XX[56+i] = MDp->count[i];
270      MDblock(MDp,XX);
271    }
272    /* Set flag saying we're done with MD computation */
273    MDp->done = 1;
274  }
275}
276
277/*
278** Finish up MD4 computation and return message digest.
279*/
280void
281MD4Final(buf, MD)
282unsigned char *buf;
283MD4_CTX *MD;
284{
285  int i, j;
286  unsigned int w;
287
288  MD4Update(MD, NULL, 0);
289  for (i = 0; i < 4; ++i) {
290    w = MD->buffer[i];
291    for (j = 0; j < 4; ++j) {
292      *buf++ = w;
293      w >>= 8;
294    }
295  }
296}
297
298/*
299** End of md4.c
300****************************(cut)***********************************/
Note: See TracBrowser for help on using the repository browser.