net-snmp 5.7
md5.c
00001 /*
00002  * ** **************************************************************************
00003  * ** md5.c -- Implementation of MD5 Message Digest Algorithm                 **
00004  * ** Updated: 2/16/90 by Ronald L. Rivest                                    **
00005  * ** (C) 1990 RSA Data Security, Inc.                                        **
00006  * ** **************************************************************************
00007  */
00008 
00009 /*
00010  * ** To use MD5:
00011  * **   -- Include md5.h in your program
00012  * **   -- Declare an MDstruct MD to hold the state of the digest computation.
00013  * **   -- Initialize MD using MDbegin(&MD)
00014  * **   -- For each full block (64 bytes) X you wish to process, call
00015  * **          MDupdate(&MD,X,512)
00016  * **      (512 is the number of bits in a full block.)
00017  * **   -- For the last block (less than 64 bytes) you wish to process,
00018  * **          MDupdate(&MD,X,n)
00019  * **      where n is the number of bits in the partial block. A partial
00020  * **      block terminates the computation, so every MD computation should
00021  * **      terminate by processing a partial block, even if it has n = 0.
00022  * **   -- The message digest is available in MD.buffer[0] ... MD.buffer[3].
00023  * **      (Least-significant byte of each word should be output first.)
00024  * **   -- You can print out the digest using MDprint(&MD)
00025  */
00026 
00027 /*
00028  * Implementation notes:
00029  * ** This implementation assumes that ints are 32-bit quantities.
00030  * ** If the machine stores the least-significant byte of an int in the
00031  * ** least-addressed byte (eg., VAX and 8086), then LOWBYTEFIRST should be
00032  * ** set to TRUE.  Otherwise (eg., SUNS), LOWBYTEFIRST should be set to
00033  * ** FALSE.  Note that on machines with LOWBYTEFIRST FALSE the routine
00034  * ** MDupdate modifies has a side-effect on its input array (the order of bytes
00035  * ** in each word are reversed).  If this is undesired a call to MDreverse(X) can
00036  * ** reverse the bytes of X back into order after each call to MDupdate.
00037  */
00038 
00039 /*
00040  * code uses WORDS_BIGENDIAN defined by configure now  -- WH 9/27/95 
00041  */
00042 
00043 /*
00044  * Compile-time includes 
00045  */
00046 
00047 #include <net-snmp/net-snmp-config.h>
00048 
00049 #ifndef NETSNMP_DISABLE_MD5
00050 
00051 #include <stdio.h>
00052 #include <sys/types.h>
00053 #if HAVE_STRING_H
00054 #include <string.h>
00055 #else
00056 #include <strings.h>
00057 #endif
00058 
00059 #if HAVE_STDLIB_H
00060 #include <stdlib.h>
00061 #endif
00062 
00063 #include <net-snmp/net-snmp-includes.h>
00064 #include <net-snmp/utilities.h>
00065 #include <net-snmp/library/md5.h>
00066 
00067 /*
00068  * Compile-time declarations of MD5 ``magic constants''.
00069  */
00070 #define I0  0x67452301          /* Initial values for MD buffer */
00071 #define I1  0xefcdab89
00072 #define I2  0x98badcfe
00073 #define I3  0x10325476
00074 #define fs1  7                  /* round 1 shift amounts */
00075 #define fs2 12
00076 #define fs3 17
00077 #define fs4 22
00078 #define gs1  5                  /* round 2 shift amounts */
00079 #define gs2  9
00080 #define gs3 14
00081 #define gs4 20
00082 #define hs1  4                  /* round 3 shift amounts */
00083 #define hs2 11
00084 #define hs3 16
00085 #define hs4 23
00086 #define is1  6                  /* round 4 shift amounts */
00087 #define is2 10
00088 #define is3 15
00089 #define is4 21
00090 
00091 
00092 /*
00093  * Compile-time macro declarations for MD5.
00094  * ** Note: The ``rot'' operator uses the variable ``tmp''.
00095  * ** It assumes tmp is declared as unsigned int, so that the >>
00096  * ** operator will shift in zeros rather than extending the sign bit.
00097  */
00098 #define f(X,Y,Z)             ((X&Y) | ((~X)&Z))
00099 #define g(X,Y,Z)             ((X&Z) | (Y&(~Z)))
00100 #define h(X,Y,Z)             (X^Y^Z)
00101 #define i_(X,Y,Z)            (Y ^ ((X) | (~Z)))
00102 #define rot(X,S)             (tmp=X,(tmp<<S) | (tmp>>(32-S)))
00103 #define ff(A,B,C,D,i,s,lp)   A = rot((A + f(B,C,D) + X[i] + lp),s) + B
00104 #define gg(A,B,C,D,i,s,lp)   A = rot((A + g(B,C,D) + X[i] + lp),s) + B
00105 #define hh(A,B,C,D,i,s,lp)   A = rot((A + h(B,C,D) + X[i] + lp),s) + B
00106 #define ii(A,B,C,D,i,s,lp)   A = rot((A + i_(B,C,D) + X[i] + lp),s) + B
00107 
00108 #ifdef STDC_HEADERS
00109 #define Uns(num) num##U
00110 #else
00111 #define Uns(num) num
00112 #endif                          /* STDC_HEADERS */
00113 
00114 void            MDreverse(unsigned int *);
00115 static void     MDblock(MDptr, const unsigned int *);
00116 
00117 #ifdef NETSNMP_ENABLE_TESTING_CODE
00118 /*
00119  * MDprint(MDp)
00120  * ** Print message digest buffer MDp as 32 hexadecimal digits.
00121  * ** Order is from low-order byte of buffer[0] to high-order byte of buffer[3].
00122  * ** Each byte is printed with high-order hexadecimal digit first.
00123  * ** This is a user-callable routine.
00124  */
00125 void
00126 MDprint(MDptr MDp)
00127 {
00128     int             i, j;
00129     for (i = 0; i < 4; i++)
00130         for (j = 0; j < 32; j = j + 8)
00131             printf("%02x", (MDp->buffer[i] >> j) & 0xFF);
00132     printf("\n");
00133     fflush(stdout);
00134 }
00135 #endif                          /* NETSNMP_ENABLE_TESTING_CODE */
00136 
00137 /*
00138  * MDbegin(MDp)
00139  * ** Initialize message digest buffer MDp. 
00140  * ** This is a user-callable routine.
00141  */
00142 void
00143 MDbegin(MDptr MDp)
00144 {
00145     int             i;
00146     MDp->buffer[0] = I0;
00147     MDp->buffer[1] = I1;
00148     MDp->buffer[2] = I2;
00149     MDp->buffer[3] = I3;
00150     for (i = 0; i < 8; i++)
00151         MDp->count[i] = 0;
00152     MDp->done = 0;
00153 }
00154 
00155 /*
00156  * MDreverse(X)
00157  * ** Reverse the byte-ordering of every int in X.
00158  * ** Assumes X is an array of 16 ints.
00159  * ** The macro revx reverses the byte-ordering of the next word of X.
00160  */
00161 #define revx { t = (*X << 16) | (*X >> 16); \
00162                *X++ = ((t & 0xFF00FF00) >> 8) | ((t & 0x00FF00FF) << 8); }
00163 
00164 void
00165 MDreverse(unsigned int *X)
00166 {
00167     register unsigned int t;
00168     revx;
00169     revx;
00170     revx;
00171     revx;
00172     revx;
00173     revx;
00174     revx;
00175     revx;
00176     revx;
00177     revx;
00178     revx;
00179     revx;
00180     revx;
00181     revx;
00182     revx;
00183     revx;
00184 }
00185 
00186 /*
00187  * MDblock(MDp,X)
00188  * ** Update message digest buffer MDp->buffer using 16-word data block X.
00189  * ** Assumes all 16 words of X are full of data.
00190  * ** Does not update MDp->count.
00191  * ** This routine is not user-callable. 
00192  */
00193 static void
00194 MDblock(MDptr MDp, const unsigned int *X)
00195 {
00196     register unsigned int tmp, A, B, C, D;      /* hpux sysv sun */
00197 #ifdef WORDS_BIGENDIAN
00198     MDreverse(X);
00199 #endif
00200     A = MDp->buffer[0];
00201     B = MDp->buffer[1];
00202     C = MDp->buffer[2];
00203     D = MDp->buffer[3];
00204 
00205     /*
00206      * Update the message digest buffer 
00207      */
00208     ff(A, B, C, D, 0, fs1, Uns(3614090360));    /* Round 1 */
00209     ff(D, A, B, C, 1, fs2, Uns(3905402710));
00210     ff(C, D, A, B, 2, fs3, Uns(606105819));
00211     ff(B, C, D, A, 3, fs4, Uns(3250441966));
00212     ff(A, B, C, D, 4, fs1, Uns(4118548399));
00213     ff(D, A, B, C, 5, fs2, Uns(1200080426));
00214     ff(C, D, A, B, 6, fs3, Uns(2821735955));
00215     ff(B, C, D, A, 7, fs4, Uns(4249261313));
00216     ff(A, B, C, D, 8, fs1, Uns(1770035416));
00217     ff(D, A, B, C, 9, fs2, Uns(2336552879));
00218     ff(C, D, A, B, 10, fs3, Uns(4294925233));
00219     ff(B, C, D, A, 11, fs4, Uns(2304563134));
00220     ff(A, B, C, D, 12, fs1, Uns(1804603682));
00221     ff(D, A, B, C, 13, fs2, Uns(4254626195));
00222     ff(C, D, A, B, 14, fs3, Uns(2792965006));
00223     ff(B, C, D, A, 15, fs4, Uns(1236535329));
00224     gg(A, B, C, D, 1, gs1, Uns(4129170786));    /* Round 2 */
00225     gg(D, A, B, C, 6, gs2, Uns(3225465664));
00226     gg(C, D, A, B, 11, gs3, Uns(643717713));
00227     gg(B, C, D, A, 0, gs4, Uns(3921069994));
00228     gg(A, B, C, D, 5, gs1, Uns(3593408605));
00229     gg(D, A, B, C, 10, gs2, Uns(38016083));
00230     gg(C, D, A, B, 15, gs3, Uns(3634488961));
00231     gg(B, C, D, A, 4, gs4, Uns(3889429448));
00232     gg(A, B, C, D, 9, gs1, Uns(568446438));
00233     gg(D, A, B, C, 14, gs2, Uns(3275163606));
00234     gg(C, D, A, B, 3, gs3, Uns(4107603335));
00235     gg(B, C, D, A, 8, gs4, Uns(1163531501));
00236     gg(A, B, C, D, 13, gs1, Uns(2850285829));
00237     gg(D, A, B, C, 2, gs2, Uns(4243563512));
00238     gg(C, D, A, B, 7, gs3, Uns(1735328473));
00239     gg(B, C, D, A, 12, gs4, Uns(2368359562));
00240     hh(A, B, C, D, 5, hs1, Uns(4294588738));    /* Round 3 */
00241     hh(D, A, B, C, 8, hs2, Uns(2272392833));
00242     hh(C, D, A, B, 11, hs3, Uns(1839030562));
00243     hh(B, C, D, A, 14, hs4, Uns(4259657740));
00244     hh(A, B, C, D, 1, hs1, Uns(2763975236));
00245     hh(D, A, B, C, 4, hs2, Uns(1272893353));
00246     hh(C, D, A, B, 7, hs3, Uns(4139469664));
00247     hh(B, C, D, A, 10, hs4, Uns(3200236656));
00248     hh(A, B, C, D, 13, hs1, Uns(681279174));
00249     hh(D, A, B, C, 0, hs2, Uns(3936430074));
00250     hh(C, D, A, B, 3, hs3, Uns(3572445317));
00251     hh(B, C, D, A, 6, hs4, Uns(76029189));
00252     hh(A, B, C, D, 9, hs1, Uns(3654602809));
00253     hh(D, A, B, C, 12, hs2, Uns(3873151461));
00254     hh(C, D, A, B, 15, hs3, Uns(530742520));
00255     hh(B, C, D, A, 2, hs4, Uns(3299628645));
00256     ii(A, B, C, D, 0, is1, Uns(4096336452));    /* Round 4 */
00257     ii(D, A, B, C, 7, is2, Uns(1126891415));
00258     ii(C, D, A, B, 14, is3, Uns(2878612391));
00259     ii(B, C, D, A, 5, is4, Uns(4237533241));
00260     ii(A, B, C, D, 12, is1, Uns(1700485571));
00261     ii(D, A, B, C, 3, is2, Uns(2399980690));
00262     ii(C, D, A, B, 10, is3, Uns(4293915773));
00263     ii(B, C, D, A, 1, is4, Uns(2240044497));
00264     ii(A, B, C, D, 8, is1, Uns(1873313359));
00265     ii(D, A, B, C, 15, is2, Uns(4264355552));
00266     ii(C, D, A, B, 6, is3, Uns(2734768916));
00267     ii(B, C, D, A, 13, is4, Uns(1309151649));
00268     ii(A, B, C, D, 4, is1, Uns(4149444226));
00269     ii(D, A, B, C, 11, is2, Uns(3174756917));
00270     ii(C, D, A, B, 2, is3, Uns(718787259));
00271     ii(B, C, D, A, 9, is4, Uns(3951481745));
00272 
00273     MDp->buffer[0] += A;
00274     MDp->buffer[1] += B;
00275     MDp->buffer[2] += C;
00276     MDp->buffer[3] += D;
00277 #ifdef WORDS_BIGENDIAN
00278     MDreverse(X);
00279 #endif
00280 }
00281 
00282 /*
00283  * MDupdate(MDp,X,count)
00284  * ** Input: MDp -- an MDptr
00285  * **        X -- a pointer to an array of unsigned characters.
00286  * **        count -- the number of bits of X to use.
00287  * **                 (if not a multiple of 8, uses high bits of last byte.)
00288  * ** Update MDp using the number of bits of X given by count.
00289  * ** This is the basic input routine for an MD5 user.
00290  * ** The routine completes the MD computation when count < 512, so
00291  * ** every MD computation should end with one call to MDupdate with a
00292  * ** count less than 512.  A call with count 0 will be ignored if the
00293  * ** MD has already been terminated (done != 0), so an extra call with count
00294  * ** 0 can be given as a ``courtesy close'' to force termination if desired.
00295  * ** Returns : 0 if processing succeeds or was already done;
00296  * **          -1 if processing was already done
00297  * **          -2 if count was too large
00298  */
00299 int
00300 MDupdate(MDptr MDp, const unsigned char *X, unsigned int count)
00301 {
00302     unsigned int    i, tmp, bit, byte, mask;
00303     unsigned char   XX[64];
00304     unsigned char  *p;
00305     /*
00306      * return with no error if this is a courtesy close with count
00307      * ** zero and MDp->done is true.
00308      */
00309     if (count == 0 && MDp->done)
00310         return 0;
00311     /*
00312      * check to see if MD is already done and report error 
00313      */
00314     if (MDp->done) {
00315         return -1;
00316     }
00317     /*
00318      * if (MDp->done) { fprintf(stderr,"\nError: MDupdate MD already done."); return; }
00319      */
00320     /*
00321      * Add count to MDp->count 
00322      */
00323     tmp = count;
00324     p = MDp->count;
00325     while (tmp) {
00326         tmp += *p;
00327         *p++ = tmp;
00328         tmp = tmp >> 8;
00329     }
00330     /*
00331      * Process data 
00332      */
00333     if (count == 512) {         /* Full block of data to handle */
00334         MDblock(MDp, (const unsigned int *) X);
00335     } else if (count > 512)     /* Check for count too large */
00336         return -2;
00337     /*
00338      * { fprintf(stderr,"\nError: MDupdate called with illegal count value %d.",count);
00339      * return;
00340      * }
00341      */
00342     else {                      /* partial block -- must be last block so finish up */
00343         /*
00344          * Find out how many bytes and residual bits there are 
00345          */
00346         int             copycount;
00347         byte = count >> 3;
00348         bit = count & 7;
00349         copycount = byte;
00350         if (bit)
00351             copycount++;
00352         /*
00353          * Copy X into XX since we need to modify it 
00354          */
00355         memset(XX, 0, sizeof(XX));
00356         memcpy(XX, X, copycount);
00357 
00358         /*
00359          * Add padding '1' bit and low-order zeros in last byte 
00360          */
00361         mask = ((unsigned long) 1) << (7 - bit);
00362         XX[byte] = (XX[byte] | mask) & ~(mask - 1);
00363         /*
00364          * If room for bit count, finish up with this block 
00365          */
00366         if (byte <= 55) {
00367             for (i = 0; i < 8; i++)
00368                 XX[56 + i] = MDp->count[i];
00369             MDblock(MDp, (unsigned int *) XX);
00370         } else {                /* need to do two blocks to finish up */
00371             MDblock(MDp, (unsigned int *) XX);
00372             for (i = 0; i < 56; i++)
00373                 XX[i] = 0;
00374             for (i = 0; i < 8; i++)
00375                 XX[56 + i] = MDp->count[i];
00376             MDblock(MDp, (unsigned int *) XX);
00377         }
00378         /*
00379          * Set flag saying we're done with MD computation 
00380          */
00381         MDp->done = 1;
00382     }
00383     return 0;
00384 }
00385 
00386 /*
00387  * MDchecksum(data, len, MD5): do a checksum on an arbirtrary amount of data 
00388  */
00389 int
00390 MDchecksum(const u_char * data, size_t len, u_char * mac, size_t maclen)
00391 {
00392     MDstruct        md;
00393     MDstruct       *MD = &md;
00394     int             rc = 0;
00395 
00396     MDbegin(MD);
00397     while (len >= 64) {
00398         rc = MDupdate(MD, data, 64 * 8);
00399         if (rc)
00400             goto check_end;
00401         data += 64;
00402         len -= 64;
00403     }
00404     rc = MDupdate(MD, data, len * 8);
00405     if (rc)
00406         goto check_end;
00407 
00408     /*
00409      * copy the checksum to the outgoing data (all of it that is requested). 
00410      */
00411     MDget(MD, mac, maclen);
00412 
00413   check_end:
00414     memset(&md, 0, sizeof(md));
00415     return rc;
00416 }
00417 
00418 
00419 /*
00420  * MDsign(data, len, MD5): do a checksum on an arbirtrary amount
00421  * of data, and prepended with a secret in the standard fashion 
00422  */
00423 int
00424 MDsign(const u_char * data, size_t len, u_char * mac, size_t maclen,
00425        const u_char * secret, size_t secretlen)
00426 {
00427 #define HASHKEYLEN 64
00428 
00429     MDstruct        MD;
00430     u_char          K1[HASHKEYLEN];
00431     u_char          K2[HASHKEYLEN];
00432     u_char          extendedAuthKey[HASHKEYLEN];
00433     u_char          buf[HASHKEYLEN];
00434     size_t          i;
00435     const u_char   *cp;
00436     u_char         *newdata = NULL;
00437     int             rc = 0;
00438 
00439     /*
00440      * memset(K1,0,HASHKEYLEN);
00441      * memset(K2,0,HASHKEYLEN);
00442      * memset(buf,0,HASHKEYLEN);
00443      * memset(extendedAuthKey,0,HASHKEYLEN);
00444      */
00445 
00446     if (secretlen != 16 || secret == NULL || mac == NULL || data == NULL ||
00447         len <= 0 || maclen <= 0) {
00448         /*
00449          * DEBUGMSGTL(("md5","MD5 signing not properly initialized")); 
00450          */
00451         return -1;
00452     }
00453 
00454     memset(extendedAuthKey, 0, HASHKEYLEN);
00455     memcpy(extendedAuthKey, secret, secretlen);
00456     for (i = 0; i < HASHKEYLEN; i++) {
00457         K1[i] = extendedAuthKey[i] ^ 0x36;
00458         K2[i] = extendedAuthKey[i] ^ 0x5c;
00459     }
00460 
00461     MDbegin(&MD);
00462     rc = MDupdate(&MD, K1, HASHKEYLEN * 8);
00463     if (rc)
00464         goto update_end;
00465 
00466     i = len;
00467     if (((uintptr_t) data) % sizeof(long) != 0) {
00468         /*
00469          * this relies on the ability to use integer math and thus we
00470          * must rely on data that aligns on 32-bit-word-boundries 
00471          */
00472         memdup(&newdata, data, len);
00473         cp = newdata;
00474     } else {
00475         cp = data;
00476     }
00477 
00478     while (i >= 64) {
00479         rc = MDupdate(&MD, cp, 64 * 8);
00480         if (rc)
00481             goto update_end;
00482         cp += 64;
00483         i -= 64;
00484     }
00485 
00486     rc = MDupdate(&MD, cp, i * 8);
00487     if (rc)
00488         goto update_end;
00489 
00490     memset(buf, 0, HASHKEYLEN);
00491     MDget(&MD, buf, HASHKEYLEN);
00492 
00493     MDbegin(&MD);
00494     rc = MDupdate(&MD, K2, HASHKEYLEN * 8);
00495     if (rc)
00496         goto update_end;
00497     rc = MDupdate(&MD, buf, 16 * 8);
00498     if (rc)
00499         goto update_end;
00500 
00501     /*
00502      * copy the sign checksum to the outgoing pointer 
00503      */
00504     MDget(&MD, mac, maclen);
00505 
00506   update_end:
00507     memset(buf, 0, HASHKEYLEN);
00508     memset(K1, 0, HASHKEYLEN);
00509     memset(K2, 0, HASHKEYLEN);
00510     memset(extendedAuthKey, 0, HASHKEYLEN);
00511     memset(&MD, 0, sizeof(MD));
00512 
00513     if (newdata)
00514         free(newdata);
00515     return rc;
00516 }
00517 
00518 void
00519 MDget(MDstruct * MD, u_char * buf, size_t buflen)
00520 {
00521     int             i, j;
00522 
00523     /*
00524      * copy the checksum to the outgoing data (all of it that is requested). 
00525      */
00526     for (i = 0; i < 4 && i * 4 < (int) buflen; i++)
00527         for (j = 0; j < 4 && i * 4 + j < (int) buflen; j++)
00528             buf[i * 4 + j] = (MD->buffer[i] >> j * 8) & 0xff;
00529 }
00530 
00531 /*
00532  * ** End of md5.c
00533  * ****************************(cut)****************************************
00534  */
00535 
00536 #endif /* NETSNMP_DISABLE_MD5 */