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

4.104.11
Last change on this file since b25b88e7 was b25b88e7, checked in by Ralf Corsepius <ralf.corsepius@…>, on Mar 28, 2010 at 5:50:29 AM

Add HAVE_CONFIG_H support to let files receive configure defines.

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