1 #ifdef BRCM_MD5_SUPPORT
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 * ** **************************************************************************
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)
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.
41 * code uses WORDS_BIGENDIAN defined by configure now -- WH 9/27/95
45 * Compile-time includes
48 #include <net-snmp/net-snmp-config.h>
51 #include <sys/types.h>
65 #include <net-snmp/utilities.h>
66 #include <net-snmp/library/md5.h>
69 * Compile-time declarations of MD5 ``magic constants''.
71 #define I0 0x67452301 /* Initial values for MD buffer */
75 #define fs1 7 /* round 1 shift amounts */
79 #define gs1 5 /* round 2 shift amounts */
83 #define hs1 4 /* round 3 shift amounts */
87 #define is1 6 /* round 4 shift amounts */
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.
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
110 #define Uns(num) num##U
113 #endif /* STDC_HEADERS */
115 void MDreverse(unsigned int *);
116 static void MDblock(MDptr, unsigned int *);
118 #ifdef SNMP_TESTING_CODE
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.
130 for (i = 0; i < 4; i++)
131 for (j = 0; j < 32; j = j + 8)
132 printf("%02x", (MDp->buffer[i] >> j) & 0xFF);
136 #endif /* SNMP_TESTING_CODE */
140 * ** Initialize message digest buffer MDp.
141 * ** This is a user-callable routine.
151 for (i = 0; i < 8; i++)
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.
162 #define revx { t = (*X << 16) | (*X >> 16); \
163 *X++ = ((t & 0xFF00FF00) >> 8) | ((t & 0x00FF00FF) << 8); }
166 MDreverse(unsigned int *X)
168 register unsigned int t;
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.
195 MDblock(MDptr MDp, unsigned int *X)
197 register unsigned int tmp, A, B, C, D; /* hpux sysv sun */
198 #ifdef WORDS_BIGENDIAN
207 * Update the message digest buffer
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));
278 #ifdef WORDS_BIGENDIAN
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
301 MDupdate(MDptr MDp, unsigned char *X, unsigned int count)
303 unsigned int i, tmp, bit, byte, mask;
304 unsigned char XX[64];
307 * return with no error if this is a courtesy close with count
308 * ** zero and MDp->done is true.
310 if (count == 0 && MDp->done)
313 * check to see if MD is already done and report error
319 * if (MDp->done) { fprintf(stderr,"\nError: MDupdate MD already done."); return; }
322 * Add count to MDp->count
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 */
339 * { fprintf(stderr,"\nError: MDupdate called with illegal count value %d.",count);
343 else { /* partial block -- must be last block so finish up */
345 * Find out how many bytes and residual bits there are
354 * Copy X into XX since we need to modify it
356 memset(XX, 0, sizeof(XX));
357 memcpy(XX, X, copycount);
360 * Add padding '1' bit and low-order zeros in last byte
362 mask = ((unsigned long) 1) << (7 - bit);
363 XX[byte] = (XX[byte] | mask) & ~(mask - 1);
365 * If room for bit count, finish up with this block
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++)
375 for (i = 0; i < 8; i++)
376 XX[56 + i] = MDp->count[i];
377 MDblock(MDp, (unsigned int *) XX);
380 * Set flag saying we're done with MD computation
388 * MDchecksum(data, len, MD5): do a checksum on an arbirtrary amount of data
391 MDchecksum(u_char * data, size_t len, u_char * mac, size_t maclen)
399 rc = MDupdate(MD, data, 64 * 8);
405 rc = MDupdate(MD, data, len * 8);
410 * copy the checksum to the outgoing data (all of it that is requested).
412 MDget(MD, mac, maclen);
415 memset(&md, 0, sizeof(md));
421 * MDsign(data, len, MD5): do a checksum on an arbirtrary amount
422 * of data, and prepended with a secret in the standard fashion
425 MDsign(u_char * data, size_t len, u_char * mac, size_t maclen,
426 u_char * secret, size_t secretlen)
428 #define HASHKEYLEN 64
431 u_char K1[HASHKEYLEN];
432 u_char K2[HASHKEYLEN];
433 u_char extendedAuthKey[HASHKEYLEN];
434 u_char buf[HASHKEYLEN];
436 u_char *cp, *newdata = 0;
440 * memset(K1,0,HASHKEYLEN);
441 * memset(K2,0,HASHKEYLEN);
442 * memset(buf,0,HASHKEYLEN);
443 * memset(extendedAuthKey,0,HASHKEYLEN);
446 if (secretlen != 16 || secret == NULL || mac == NULL || data == NULL ||
447 len <= 0 || maclen <= 0) {
449 * DEBUGMSGTL(("md5","MD5 signing not properly initialized"));
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;
462 rc = MDupdate(&MD, K1, HASHKEYLEN * 8);
467 if (((unsigned int) data) % 32 != 0) {
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
472 memdup(&newdata, data, len);
479 rc = MDupdate(&MD, cp, 64 * 8);
486 rc = MDupdate(&MD, cp, i * 8);
490 memset(buf, 0, HASHKEYLEN);
491 MDget(&MD, buf, HASHKEYLEN);
494 rc = MDupdate(&MD, K2, HASHKEYLEN * 8);
497 rc = MDupdate(&MD, buf, 16 * 8);
502 * copy the sign checksum to the outgoing pointer
504 MDget(&MD, mac, maclen);
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));
519 MDget(MDstruct * MD, u_char * buf, size_t buflen)
524 * copy the checksum to the outgoing data (all of it that is requested).
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;
533 * ****************************(cut)****************************************
536 #endif /* BRCM_MD5_SUPPORT */