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

5
Last change on this file since d997aa1 was 17ecd5a0, checked in by Sebastian Huber <sebastian.huber@…>, on 07/16/10 at 07:45:18

2010-07-16 Sebastian Huber <sebastian.huber@…>

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