Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / md5.c
1 #ifdef BRCM_MD5_SUPPORT
2 /*
3  * ** **************************************************************************
4  * ** md5.c -- Implementation of MD5 Message Digest Algorithm                 **
5  * ** Updated: 2/16/90 by Ronald L. Rivest                                    **
6  * ** (C) 1990 RSA Data Security, Inc.                                        **
7  * ** **************************************************************************
8  */
9
10 /*
11  * ** To use MD5:
12  * **   -- Include md5.h in your program
13  * **   -- Declare an MDstruct MD to hold the state of the digest computation.
14  * **   -- Initialize MD using MDbegin(&MD)
15  * **   -- For each full block (64 bytes) X you wish to process, call
16  * **          MDupdate(&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  * **          MDupdate(&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 should
22  * **      terminate by processing a partial block, even if it has n = 0.
23  * **   -- The message digest is available in MD.buffer[0] ... MD.buffer[3].
24  * **      (Least-significant byte of each word should be output first.)
25  * **   -- You can print out the digest using MDprint(&MD)
26  */
27
28 /*
29  * Implementation notes:
30  * ** This implementation assumes that ints are 32-bit quantities.
31  * ** If the machine stores the least-significant byte of an int in the
32  * ** least-addressed byte (eg., VAX and 8086), then LOWBYTEFIRST should be
33  * ** set to TRUE.  Otherwise (eg., SUNS), LOWBYTEFIRST should be set to
34  * ** FALSE.  Note that on machines with LOWBYTEFIRST FALSE the routine
35  * ** MDupdate modifies has a side-effect on its input array (the order of bytes
36  * ** in each word are reversed).  If this is undesired a call to MDreverse(X) can
37  * ** reverse the bytes of X back into order after each call to MDupdate.
38  */
39
40 /*
41  * code uses WORDS_BIGENDIAN defined by configure now  -- WH 9/27/95 
42  */
43
44 /*
45  * Compile-time includes 
46  */
47
48 #include <net-snmp/net-snmp-config.h>
49
50 #include <stdio.h>
51 #include <sys/types.h>
52 #if HAVE_STRING_H
53 #include <string.h>
54 #else
55 #include <strings.h>
56 #endif
57 #if HAVE_WINSOCK_H
58 #include <winsock.h>
59 #endif
60
61 #if HAVE_STDLIB_H
62 #include <stdlib.h>
63 #endif
64
65 #include <net-snmp/utilities.h>
66 #include <net-snmp/library/md5.h>
67
68 /*
69  * Compile-time declarations of MD5 ``magic constants''.
70  */
71 #define I0  0x67452301          /* Initial values for MD buffer */
72 #define I1  0xefcdab89
73 #define I2  0x98badcfe
74 #define I3  0x10325476
75 #define fs1  7                  /* round 1 shift amounts */
76 #define fs2 12
77 #define fs3 17
78 #define fs4 22
79 #define gs1  5                  /* round 2 shift amounts */
80 #define gs2  9
81 #define gs3 14
82 #define gs4 20
83 #define hs1  4                  /* round 3 shift amounts */
84 #define hs2 11
85 #define hs3 16
86 #define hs4 23
87 #define is1  6                  /* round 4 shift amounts */
88 #define is2 10
89 #define is3 15
90 #define is4 21
91
92
93 /*
94  * Compile-time macro declarations for MD5.
95  * ** Note: The ``rot'' operator uses the variable ``tmp''.
96  * ** It assumes tmp is declared as unsigned int, so that the >>
97  * ** operator will shift in zeros rather than extending the sign bit.
98  */
99 #define f(X,Y,Z)             ((X&Y) | ((~X)&Z))
100 #define g(X,Y,Z)             ((X&Z) | (Y&(~Z)))
101 #define h(X,Y,Z)             (X^Y^Z)
102 #define i_(X,Y,Z)            (Y ^ ((X) | (~Z)))
103 #define rot(X,S)             (tmp=X,(tmp<<S) | (tmp>>(32-S)))
104 #define ff(A,B,C,D,i,s,lp)   A = rot((A + f(B,C,D) + X[i] + lp),s) + B
105 #define gg(A,B,C,D,i,s,lp)   A = rot((A + g(B,C,D) + X[i] + lp),s) + B
106 #define hh(A,B,C,D,i,s,lp)   A = rot((A + h(B,C,D) + X[i] + lp),s) + B
107 #define ii(A,B,C,D,i,s,lp)   A = rot((A + i_(B,C,D) + X[i] + lp),s) + B
108
109 #ifdef STDC_HEADERS
110 #define Uns(num) num##U
111 #else
112 #define Uns(num) num
113 #endif                          /* STDC_HEADERS */
114
115 void            MDreverse(unsigned int *);
116 static void     MDblock(MDptr, unsigned int *);
117
118 #ifdef SNMP_TESTING_CODE
119 /*
120  * MDprint(MDp)
121  * ** Print message digest buffer MDp as 32 hexadecimal digits.
122  * ** Order is from low-order byte of buffer[0] to high-order byte of buffer[3].
123  * ** Each byte is printed with high-order hexadecimal digit first.
124  * ** This is a user-callable routine.
125  */
126 void
127 MDprint(MDptr MDp)
128 {
129     int             i, j;
130     for (i = 0; i < 4; i++)
131         for (j = 0; j < 32; j = j + 8)
132             printf("%02x", (MDp->buffer[i] >> j) & 0xFF);
133     printf("\n");
134     fflush(stdout);
135 }
136 #endif                          /* SNMP_TESTING_CODE */
137
138 /*
139  * MDbegin(MDp)
140  * ** Initialize message digest buffer MDp. 
141  * ** This is a user-callable routine.
142  */
143 void
144 MDbegin(MDptr MDp)
145 {
146     int             i;
147     MDp->buffer[0] = I0;
148     MDp->buffer[1] = I1;
149     MDp->buffer[2] = I2;
150     MDp->buffer[3] = I3;
151     for (i = 0; i < 8; i++)
152         MDp->count[i] = 0;
153     MDp->done = 0;
154 }
155
156 /*
157  * MDreverse(X)
158  * ** Reverse the byte-ordering of every int in X.
159  * ** Assumes X is an array of 16 ints.
160  * ** The macro revx reverses the byte-ordering of the next word of X.
161  */
162 #define revx { t = (*X << 16) | (*X >> 16); \
163                *X++ = ((t & 0xFF00FF00) >> 8) | ((t & 0x00FF00FF) << 8); }
164
165 void
166 MDreverse(unsigned int *X)
167 {
168     register unsigned int t;
169     revx;
170     revx;
171     revx;
172     revx;
173     revx;
174     revx;
175     revx;
176     revx;
177     revx;
178     revx;
179     revx;
180     revx;
181     revx;
182     revx;
183     revx;
184     revx;
185 }
186
187 /*
188  * MDblock(MDp,X)
189  * ** Update message digest buffer MDp->buffer using 16-word data block X.
190  * ** Assumes all 16 words of X are full of data.
191  * ** Does not update MDp->count.
192  * ** This routine is not user-callable. 
193  */
194 static void
195 MDblock(MDptr MDp, unsigned int *X)
196 {
197     register unsigned int tmp, A, B, C, D;      /* hpux sysv sun */
198 #ifdef WORDS_BIGENDIAN
199     MDreverse(X);
200 #endif
201     A = MDp->buffer[0];
202     B = MDp->buffer[1];
203     C = MDp->buffer[2];
204     D = MDp->buffer[3];
205
206     /*
207      * Update the message digest buffer 
208      */
209     ff(A, B, C, D, 0, fs1, Uns(3614090360));    /* Round 1 */
210     ff(D, A, B, C, 1, fs2, Uns(3905402710));
211     ff(C, D, A, B, 2, fs3, Uns(606105819));
212     ff(B, C, D, A, 3, fs4, Uns(3250441966));
213     ff(A, B, C, D, 4, fs1, Uns(4118548399));
214     ff(D, A, B, C, 5, fs2, Uns(1200080426));
215     ff(C, D, A, B, 6, fs3, Uns(2821735955));
216     ff(B, C, D, A, 7, fs4, Uns(4249261313));
217     ff(A, B, C, D, 8, fs1, Uns(1770035416));
218     ff(D, A, B, C, 9, fs2, Uns(2336552879));
219     ff(C, D, A, B, 10, fs3, Uns(4294925233));
220     ff(B, C, D, A, 11, fs4, Uns(2304563134));
221     ff(A, B, C, D, 12, fs1, Uns(1804603682));
222     ff(D, A, B, C, 13, fs2, Uns(4254626195));
223     ff(C, D, A, B, 14, fs3, Uns(2792965006));
224     ff(B, C, D, A, 15, fs4, Uns(1236535329));
225     gg(A, B, C, D, 1, gs1, Uns(4129170786));    /* Round 2 */
226     gg(D, A, B, C, 6, gs2, Uns(3225465664));
227     gg(C, D, A, B, 11, gs3, Uns(643717713));
228     gg(B, C, D, A, 0, gs4, Uns(3921069994));
229     gg(A, B, C, D, 5, gs1, Uns(3593408605));
230     gg(D, A, B, C, 10, gs2, Uns(38016083));
231     gg(C, D, A, B, 15, gs3, Uns(3634488961));
232     gg(B, C, D, A, 4, gs4, Uns(3889429448));
233     gg(A, B, C, D, 9, gs1, Uns(568446438));
234     gg(D, A, B, C, 14, gs2, Uns(3275163606));
235     gg(C, D, A, B, 3, gs3, Uns(4107603335));
236     gg(B, C, D, A, 8, gs4, Uns(1163531501));
237     gg(A, B, C, D, 13, gs1, Uns(2850285829));
238     gg(D, A, B, C, 2, gs2, Uns(4243563512));
239     gg(C, D, A, B, 7, gs3, Uns(1735328473));
240     gg(B, C, D, A, 12, gs4, Uns(2368359562));
241     hh(A, B, C, D, 5, hs1, Uns(4294588738));    /* Round 3 */
242     hh(D, A, B, C, 8, hs2, Uns(2272392833));
243     hh(C, D, A, B, 11, hs3, Uns(1839030562));
244     hh(B, C, D, A, 14, hs4, Uns(4259657740));
245     hh(A, B, C, D, 1, hs1, Uns(2763975236));
246     hh(D, A, B, C, 4, hs2, Uns(1272893353));
247     hh(C, D, A, B, 7, hs3, Uns(4139469664));
248     hh(B, C, D, A, 10, hs4, Uns(3200236656));
249     hh(A, B, C, D, 13, hs1, Uns(681279174));
250     hh(D, A, B, C, 0, hs2, Uns(3936430074));
251     hh(C, D, A, B, 3, hs3, Uns(3572445317));
252     hh(B, C, D, A, 6, hs4, Uns(76029189));
253     hh(A, B, C, D, 9, hs1, Uns(3654602809));
254     hh(D, A, B, C, 12, hs2, Uns(3873151461));
255     hh(C, D, A, B, 15, hs3, Uns(530742520));
256     hh(B, C, D, A, 2, hs4, Uns(3299628645));
257     ii(A, B, C, D, 0, is1, Uns(4096336452));    /* Round 4 */
258     ii(D, A, B, C, 7, is2, Uns(1126891415));
259     ii(C, D, A, B, 14, is3, Uns(2878612391));
260     ii(B, C, D, A, 5, is4, Uns(4237533241));
261     ii(A, B, C, D, 12, is1, Uns(1700485571));
262     ii(D, A, B, C, 3, is2, Uns(2399980690));
263     ii(C, D, A, B, 10, is3, Uns(4293915773));
264     ii(B, C, D, A, 1, is4, Uns(2240044497));
265     ii(A, B, C, D, 8, is1, Uns(1873313359));
266     ii(D, A, B, C, 15, is2, Uns(4264355552));
267     ii(C, D, A, B, 6, is3, Uns(2734768916));
268     ii(B, C, D, A, 13, is4, Uns(1309151649));
269     ii(A, B, C, D, 4, is1, Uns(4149444226));
270     ii(D, A, B, C, 11, is2, Uns(3174756917));
271     ii(C, D, A, B, 2, is3, Uns(718787259));
272     ii(B, C, D, A, 9, is4, Uns(3951481745));
273
274     MDp->buffer[0] += A;
275     MDp->buffer[1] += B;
276     MDp->buffer[2] += C;
277     MDp->buffer[3] += D;
278 #ifdef WORDS_BIGENDIAN
279     MDreverse(X);
280 #endif
281 }
282
283 /*
284  * MDupdate(MDp,X,count)
285  * ** Input: MDp -- an MDptr
286  * **        X -- a pointer to an array of unsigned characters.
287  * **        count -- the number of bits of X to use.
288  * **                 (if not a multiple of 8, uses high bits of last byte.)
289  * ** Update MDp using the number of bits of X given by count.
290  * ** This is the basic input routine for an MD5 user.
291  * ** The routine completes the MD computation when count < 512, so
292  * ** every MD computation should end with one call to MDupdate with a
293  * ** count less than 512.  A call with count 0 will be ignored if the
294  * ** MD has already been terminated (done != 0), so an extra call with count
295  * ** 0 can be given as a ``courtesy close'' to force termination if desired.
296  * ** Returns : 0 if processing succeeds or was already done;
297  * **          -1 if processing was already done
298  * **          -2 if count was too large
299  */
300 int
301 MDupdate(MDptr MDp, unsigned char *X, unsigned int count)
302 {
303     unsigned int    i, tmp, bit, byte, mask;
304     unsigned char   XX[64];
305     unsigned char  *p;
306     /*
307      * return with no error if this is a courtesy close with count
308      * ** zero and MDp->done is true.
309      */
310     if (count == 0 && MDp->done)
311         return 0;
312     /*
313      * check to see if MD is already done and report error 
314      */
315     if (MDp->done) {
316         return -1;
317     }
318     /*
319      * if (MDp->done) { fprintf(stderr,"\nError: MDupdate MD already done."); return; }
320      */
321     /*
322      * Add count to MDp->count 
323      */
324     tmp = count;
325     p = MDp->count;
326     while (tmp) {
327         tmp += *p;
328         *p++ = tmp;
329         tmp = tmp >> 8;
330     }
331     /*
332      * Process data 
333      */
334     if (count == 512) {         /* Full block of data to handle */
335         MDblock(MDp, (unsigned int *) X);
336     } else if (count > 512)     /* Check for count too large */
337         return -2;
338     /*
339      * { fprintf(stderr,"\nError: MDupdate called with illegal count value %d.",count);
340      * return;
341      * }
342      */
343     else {                      /* partial block -- must be last block so finish up */
344         /*
345          * Find out how many bytes and residual bits there are 
346          */
347         int             copycount;
348         byte = count >> 3;
349         bit = count & 7;
350         copycount = byte;
351         if (bit)
352             copycount++;
353         /*
354          * Copy X into XX since we need to modify it 
355          */
356         memset(XX, 0, sizeof(XX));
357         memcpy(XX, X, copycount);
358
359         /*
360          * Add padding '1' bit and low-order zeros in last byte 
361          */
362         mask = ((unsigned long) 1) << (7 - bit);
363         XX[byte] = (XX[byte] | mask) & ~(mask - 1);
364         /*
365          * If room for bit count, finish up with this block 
366          */
367         if (byte <= 55) {
368             for (i = 0; i < 8; i++)
369                 XX[56 + i] = MDp->count[i];
370             MDblock(MDp, (unsigned int *) XX);
371         } else {                /* need to do two blocks to finish up */
372             MDblock(MDp, (unsigned int *) XX);
373             for (i = 0; i < 56; i++)
374                 XX[i] = 0;
375             for (i = 0; i < 8; i++)
376                 XX[56 + i] = MDp->count[i];
377             MDblock(MDp, (unsigned int *) XX);
378         }
379         /*
380          * Set flag saying we're done with MD computation 
381          */
382         MDp->done = 1;
383     }
384     return 0;
385 }
386
387 /*
388  * MDchecksum(data, len, MD5): do a checksum on an arbirtrary amount of data 
389  */
390 int
391 MDchecksum(u_char * data, size_t len, u_char * mac, size_t maclen)
392 {
393     MDstruct        md;
394     MDstruct       *MD = &md;
395     int             rc = 0;
396
397     MDbegin(MD);
398     while (len >= 64) {
399         rc = MDupdate(MD, data, 64 * 8);
400         if (rc)
401             goto check_end;
402         data += 64;
403         len -= 64;
404     }
405     rc = MDupdate(MD, data, len * 8);
406     if (rc)
407         goto check_end;
408
409     /*
410      * copy the checksum to the outgoing data (all of it that is requested). 
411      */
412     MDget(MD, mac, maclen);
413
414   check_end:
415     memset(&md, 0, sizeof(md));
416     return rc;
417 }
418
419
420 /*
421  * MDsign(data, len, MD5): do a checksum on an arbirtrary amount
422  * of data, and prepended with a secret in the standard fashion 
423  */
424 int
425 MDsign(u_char * data, size_t len, u_char * mac, size_t maclen,
426        u_char * secret, size_t secretlen)
427 {
428 #define HASHKEYLEN 64
429
430     MDstruct        MD;
431     u_char          K1[HASHKEYLEN];
432     u_char          K2[HASHKEYLEN];
433     u_char          extendedAuthKey[HASHKEYLEN];
434     u_char          buf[HASHKEYLEN];
435     size_t          i;
436     u_char         *cp, *newdata = 0;
437     int             rc = 0;
438
439     /*
440      * memset(K1,0,HASHKEYLEN);
441      * memset(K2,0,HASHKEYLEN);
442      * memset(buf,0,HASHKEYLEN);
443      * memset(extendedAuthKey,0,HASHKEYLEN);
444      */
445
446     if (secretlen != 16 || secret == NULL || mac == NULL || data == NULL ||
447         len <= 0 || maclen <= 0) {
448         /*
449          * DEBUGMSGTL(("md5","MD5 signing not properly initialized")); 
450          */
451         return -1;
452     }
453
454     memset(extendedAuthKey, 0, HASHKEYLEN);
455     memcpy(extendedAuthKey, secret, secretlen);
456     for (i = 0; i < HASHKEYLEN; i++) {
457         K1[i] = extendedAuthKey[i] ^ 0x36;
458         K2[i] = extendedAuthKey[i] ^ 0x5c;
459     }
460
461     MDbegin(&MD);
462     rc = MDupdate(&MD, K1, HASHKEYLEN * 8);
463     if (rc)
464         goto update_end;
465
466     i = len;
467     if (((unsigned int) data) % 32 != 0) {
468         /*
469          * this relies on the ability to use integer math and thus we
470          * must rely on data that aligns on 32-bit-word-boundries 
471          */
472         memdup(&newdata, data, len);
473         cp = newdata;
474     } else {
475         cp = data;
476     }
477
478     while (i >= 64) {
479         rc = MDupdate(&MD, cp, 64 * 8);
480         if (rc)
481             goto update_end;
482         cp += 64;
483         i -= 64;
484     }
485
486     rc = MDupdate(&MD, cp, i * 8);
487     if (rc)
488         goto update_end;
489
490     memset(buf, 0, HASHKEYLEN);
491     MDget(&MD, buf, HASHKEYLEN);
492
493     MDbegin(&MD);
494     rc = MDupdate(&MD, K2, HASHKEYLEN * 8);
495     if (rc)
496         goto update_end;
497     rc = MDupdate(&MD, buf, 16 * 8);
498     if (rc)
499         goto update_end;
500
501     /*
502      * copy the sign checksum to the outgoing pointer 
503      */
504     MDget(&MD, mac, maclen);
505
506   update_end:
507     memset(buf, 0, HASHKEYLEN);
508     memset(K1, 0, HASHKEYLEN);
509     memset(K2, 0, HASHKEYLEN);
510     memset(extendedAuthKey, 0, HASHKEYLEN);
511     memset(&MD, 0, sizeof(MD));
512
513     if (newdata)
514         free(newdata);
515     return rc;
516 }
517
518 void
519 MDget(MDstruct * MD, u_char * buf, size_t buflen)
520 {
521     int             i, j;
522
523     /*
524      * copy the checksum to the outgoing data (all of it that is requested). 
525      */
526     for (i = 0; i < 4 && i * 4 < (int) buflen; i++)
527         for (j = 0; j < 4 && i * 4 + j < (int) buflen; j++)
528             buf[i * 4 + j] = (MD->buffer[i] >> j * 8) & 0xff;
529 }
530
531 /*
532  * ** End of md5.c
533  * ****************************(cut)****************************************
534  */
535
536 #endif /* BRCM_MD5_SUPPORT */