"Fossies" - the Fresh Open Source Software Archive

Member "bftpd/md5.c" (8 Nov 2012, 16582 Bytes) of package /linux/privat/bftpd-5.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "md5.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Functions to compute MD5 message digest of files or memory blocks
    3  * according to the definition of MD5 in RFC 1321 from April 1992.
    4  * Copyright (C) 1995, 1996 Free Software Foundation, Inc.  NOTE: The
    5  * canonical source of this file is maintained with the GNU C Library.
    6  * Bugs can be reported to bug-glibc@prep.ai.mit.edu.
    7  *
    8  * This program is free software; you can redistribute it and/or modify
    9  * it under the terms of the GNU General Public License as published
   10  * by the Free Software Foundation; either version 2, or (at your
   11  * option) any later version.
   12  *
   13  * This program is distributed in the hope that it will be useful, but
   14  * WITHOUT ANY WARRANTY; without even the implied warranty of
   15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   16  * General Public License for more details.
   17  *
   18  * You should have received a copy of the GNU General Public License
   19  * along with this program; if not, write to the Free Software
   20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   21  * 02111-1307, USA.
   22  *
   23  * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
   24  * Modified by Gray Watson <http://256.com/gray/>, 1997.
   25  *
   26  * $Id: md5.c,v 1.7 2006/03/05 15:38:09 gray Exp $
   27  */
   28 
   29 /*
   30  * NOTE: during quick performance tests on a Sun Sparc Ultra 1 and an
   31  * Alpha 255 300, these functions performed upwards of 3mb/sec
   32  * including disk I/O time.
   33  */
   34 
   35 /*
   36  * MD5 Test Suite from RFC1321: http://ds.internic.net:/rfc/rfc1321.txt
   37  *
   38  * MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
   39  * MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
   40  * MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
   41  * MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
   42  * MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
   43  * MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
   44  * d174ab98d277d9f5a5611c2c9f419d9f
   45  * MD5 ("123456789012345678901234567890123456789012345678901234567890123456
   46  * 78901234567890") = 57edf4a22be3c955ac49da2e2107b67a
   47  */
   48 
   49 #include <stdlib.h>
   50 #include <string.h>
   51 #include <sys/types.h>
   52 
   53 #include "md5.h"
   54 #include "md5_loc.h"
   55 
   56 /*
   57 static  char    *rcs_id =
   58   "$Id: md5.c,v 1.7 2006/03/05 15:38:09 gray Exp $";
   59 */
   60 /* version id for the library
   61 static char *version_id = "$MD5Version: 1.0.0 November-19-1997 $";
   62 */
   63 
   64 /****************************** local routines *******************************/
   65 
   66 /*
   67  * process_block
   68  *
   69  * DESCRIPTION:
   70  *
   71  * Process a block of bytes into a MD5 state structure.
   72  *
   73  * RETURNS:
   74  *
   75  * None.
   76  *
   77  * ARGUMENTS:
   78  *
   79  * md5_p - Pointer to MD5 structure from which we are getting the result.
   80  *
   81  * buffer - A buffer of bytes whose MD5 signature we are calculating.
   82  *
   83  * buf_len - The length of the buffer.
   84  */
   85 static  void    process_block(md5_t *md5_p, const void *buffer,
   86                   const unsigned int buf_len)
   87 {
   88   md5_uint32    correct[16];
   89   const void    *buf_p = buffer, *end_p;
   90   unsigned int  words_n;
   91   md5_uint32    A, B, C, D;
   92 
   93   words_n = buf_len / sizeof(md5_uint32);
   94   end_p = (char *)buf_p + words_n * sizeof(md5_uint32);
   95 
   96   A = md5_p->md_A;
   97   B = md5_p->md_B;
   98   C = md5_p->md_C;
   99   D = md5_p->md_D;
  100 
  101   /*
  102    * First increment the byte count.  RFC 1321 specifies the possible
  103    * length of the file up to 2^64 bits.  Here we only compute the
  104    * number of bytes with a double word increment.  Modified to do
  105    * this to better avoid overflows in the lower word -- Gray 10/97.
  106    */
  107   if (md5_p->md_total[0] > MAX_MD5_UINT32 - buf_len) {
  108     md5_p->md_total[1]++;
  109     md5_p->md_total[0] -= (MAX_MD5_UINT32 + 1 - buf_len);
  110   }
  111   else {
  112     md5_p->md_total[0] += buf_len;
  113   }
  114 
  115   /*
  116    * Process all bytes in the buffer with MD5_BLOCK bytes in each
  117    * round of the loop.
  118    */
  119   while (buf_p < end_p) {
  120     md5_uint32  A_save, B_save, C_save, D_save;
  121     md5_uint32  *corr_p = correct;
  122 
  123     A_save = A;
  124     B_save = B;
  125     C_save = C;
  126     D_save = D;
  127 
  128     /*
  129      * Before we start, one word to the strange constants.  They are
  130      * defined in RFC 1321 as
  131      *
  132      * T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..MD5_BLOCK
  133      */
  134 
  135     /* Round 1. */
  136     OP1 (A, B, C, D, buf_p, corr_p,  7, 0xd76aa478);
  137     OP1 (D, A, B, C, buf_p, corr_p, 12, 0xe8c7b756);
  138     OP1 (C, D, A, B, buf_p, corr_p, 17, 0x242070db);
  139     OP1 (B, C, D, A, buf_p, corr_p, 22, 0xc1bdceee);
  140     OP1 (A, B, C, D, buf_p, corr_p,  7, 0xf57c0faf);
  141     OP1 (D, A, B, C, buf_p, corr_p, 12, 0x4787c62a);
  142     OP1 (C, D, A, B, buf_p, corr_p, 17, 0xa8304613);
  143     OP1 (B, C, D, A, buf_p, corr_p, 22, 0xfd469501);
  144     OP1 (A, B, C, D, buf_p, corr_p,  7, 0x698098d8);
  145     OP1 (D, A, B, C, buf_p, corr_p, 12, 0x8b44f7af);
  146     OP1 (C, D, A, B, buf_p, corr_p, 17, 0xffff5bb1);
  147     OP1 (B, C, D, A, buf_p, corr_p, 22, 0x895cd7be);
  148     OP1 (A, B, C, D, buf_p, corr_p,  7, 0x6b901122);
  149     OP1 (D, A, B, C, buf_p, corr_p, 12, 0xfd987193);
  150     OP1 (C, D, A, B, buf_p, corr_p, 17, 0xa679438e);
  151     OP1 (B, C, D, A, buf_p, corr_p, 22, 0x49b40821);
  152 
  153     /* Round 2. */
  154     OP234 (FG, A, B, C, D, correct[  1],  5, 0xf61e2562);
  155     OP234 (FG, D, A, B, C, correct[  6],  9, 0xc040b340);
  156     OP234 (FG, C, D, A, B, correct[ 11], 14, 0x265e5a51);
  157     OP234 (FG, B, C, D, A, correct[  0], 20, 0xe9b6c7aa);
  158     OP234 (FG, A, B, C, D, correct[  5],  5, 0xd62f105d);
  159     OP234 (FG, D, A, B, C, correct[ 10],  9, 0x02441453);
  160     OP234 (FG, C, D, A, B, correct[ 15], 14, 0xd8a1e681);
  161     OP234 (FG, B, C, D, A, correct[  4], 20, 0xe7d3fbc8);
  162     OP234 (FG, A, B, C, D, correct[  9],  5, 0x21e1cde6);
  163     OP234 (FG, D, A, B, C, correct[ 14],  9, 0xc33707d6);
  164     OP234 (FG, C, D, A, B, correct[  3], 14, 0xf4d50d87);
  165     OP234 (FG, B, C, D, A, correct[  8], 20, 0x455a14ed);
  166     OP234 (FG, A, B, C, D, correct[ 13],  5, 0xa9e3e905);
  167     OP234 (FG, D, A, B, C, correct[  2],  9, 0xfcefa3f8);
  168     OP234 (FG, C, D, A, B, correct[  7], 14, 0x676f02d9);
  169     OP234 (FG, B, C, D, A, correct[ 12], 20, 0x8d2a4c8a);
  170 
  171     /* Round 3. */
  172     OP234 (FH, A, B, C, D, correct[  5],  4, 0xfffa3942);
  173     OP234 (FH, D, A, B, C, correct[  8], 11, 0x8771f681);
  174     OP234 (FH, C, D, A, B, correct[ 11], 16, 0x6d9d6122);
  175     OP234 (FH, B, C, D, A, correct[ 14], 23, 0xfde5380c);
  176     OP234 (FH, A, B, C, D, correct[  1],  4, 0xa4beea44);
  177     OP234 (FH, D, A, B, C, correct[  4], 11, 0x4bdecfa9);
  178     OP234 (FH, C, D, A, B, correct[  7], 16, 0xf6bb4b60);
  179     OP234 (FH, B, C, D, A, correct[ 10], 23, 0xbebfbc70);
  180     OP234 (FH, A, B, C, D, correct[ 13],  4, 0x289b7ec6);
  181     OP234 (FH, D, A, B, C, correct[  0], 11, 0xeaa127fa);
  182     OP234 (FH, C, D, A, B, correct[  3], 16, 0xd4ef3085);
  183     OP234 (FH, B, C, D, A, correct[  6], 23, 0x04881d05);
  184     OP234 (FH, A, B, C, D, correct[  9],  4, 0xd9d4d039);
  185     OP234 (FH, D, A, B, C, correct[ 12], 11, 0xe6db99e5);
  186     OP234 (FH, C, D, A, B, correct[ 15], 16, 0x1fa27cf8);
  187     OP234 (FH, B, C, D, A, correct[  2], 23, 0xc4ac5665);
  188 
  189     /* Round 4. */
  190     OP234 (FI, A, B, C, D, correct[  0],  6, 0xf4292244);
  191     OP234 (FI, D, A, B, C, correct[  7], 10, 0x432aff97);
  192     OP234 (FI, C, D, A, B, correct[ 14], 15, 0xab9423a7);
  193     OP234 (FI, B, C, D, A, correct[  5], 21, 0xfc93a039);
  194     OP234 (FI, A, B, C, D, correct[ 12],  6, 0x655b59c3);
  195     OP234 (FI, D, A, B, C, correct[  3], 10, 0x8f0ccc92);
  196     OP234 (FI, C, D, A, B, correct[ 10], 15, 0xffeff47d);
  197     OP234 (FI, B, C, D, A, correct[  1], 21, 0x85845dd1);
  198     OP234 (FI, A, B, C, D, correct[  8],  6, 0x6fa87e4f);
  199     OP234 (FI, D, A, B, C, correct[ 15], 10, 0xfe2ce6e0);
  200     OP234 (FI, C, D, A, B, correct[  6], 15, 0xa3014314);
  201     OP234 (FI, B, C, D, A, correct[ 13], 21, 0x4e0811a1);
  202     OP234 (FI, A, B, C, D, correct[  4],  6, 0xf7537e82);
  203     OP234 (FI, D, A, B, C, correct[ 11], 10, 0xbd3af235);
  204     OP234 (FI, C, D, A, B, correct[  2], 15, 0x2ad7d2bb);
  205     OP234 (FI, B, C, D, A, correct[  9], 21, 0xeb86d391);
  206 
  207     /* Add the starting values of the context. */
  208     A += A_save;
  209     B += B_save;
  210     C += C_save;
  211     D += D_save;
  212   }
  213 
  214   /* Put checksum in context given as argument. */
  215   md5_p->md_A = A;
  216   md5_p->md_B = B;
  217   md5_p->md_C = C;
  218   md5_p->md_D = D;
  219 }
  220 
  221 /*
  222  * md5_get_result
  223  *
  224  * DESCRIPTION:
  225  *
  226  * Copy the resulting MD5 signature from MD5_P into the first 16 bytes
  227  * (MD5_SIZE) of the result buffer.
  228  *
  229  * RETURNS:
  230  *
  231  * None.
  232  *
  233  * ARGUMENTS:
  234  *
  235  * md5_p - Pointer to MD5 structure from which we are getting the result.
  236  *
  237  * result - A 16 byte buffer that will contain the MD5 signature.
  238  */
  239 static  void    md5_get_result(const md5_t *md5_p, void *result)
  240 {
  241   md5_uint32    hold;
  242   void      *res_p = result;
  243 
  244   hold = SWAP(md5_p->md_A);
  245   memcpy(res_p, &hold, sizeof(md5_uint32));
  246   res_p = (char *)res_p + sizeof(md5_uint32);
  247 
  248   hold = SWAP(md5_p->md_B);
  249   memcpy(res_p, &hold, sizeof(md5_uint32));
  250   res_p = (char *)res_p + sizeof(md5_uint32);
  251 
  252   hold = SWAP(md5_p->md_C);
  253   memcpy(res_p, &hold, sizeof(md5_uint32));
  254   res_p = (char *)res_p + sizeof(md5_uint32);
  255 
  256   hold = SWAP(md5_p->md_D);
  257   memcpy(res_p, &hold, sizeof(md5_uint32));
  258 }
  259 
  260 /***************************** exported routines *****************************/
  261 
  262 /*
  263  * md5_init
  264  *
  265  * DESCRIPTION:
  266  *
  267  * Initialize structure containing state of MD5 computation. (RFC 1321,
  268  * 3.3: Step 3).  This is for progressive MD5 calculations only.  If
  269  * you have the complete string available, md5_buffer should be used.
  270  * md5_process should be called for each bunch of bytes and after the
  271  * last process call, md5_finish should be called to get the
  272  * signature.
  273  *
  274  * RETURNS:
  275  *
  276  * None.
  277  *
  278  * ARGUMENTS:
  279  *
  280  * md5_p - Pointer to md5 structure that we are initializing.
  281  */
  282 void    md5_init(md5_t *md5_p)
  283 {
  284   md5_p->md_A = 0x67452301;
  285   md5_p->md_B = 0xefcdab89;
  286   md5_p->md_C = 0x98badcfe;
  287   md5_p->md_D = 0x10325476;
  288 
  289   md5_p->md_total[0] = 0;
  290   md5_p->md_total[1] = 0;
  291   md5_p->md_buf_len = 0;
  292 }
  293 
  294 /*
  295  * md5_process
  296  *
  297  * DESCRIPTION:
  298  *
  299  * This function is used to progressively calculate a MD5 signature some
  300  * number of bytes at a time.  If you have the complete string
  301  * available, md5_buffer should be used.  The MD5 structure should
  302  * have been initialized with md5_init and after the last process
  303  * call, md5_finish should be called to get the results.
  304  *
  305  * RETURNS:
  306  *
  307  * None.
  308  *
  309  * ARGUMENTS:
  310  *
  311  * md5_p - Pointer to MD5 structure which we are progressively updating.
  312  *
  313  * buffer - A buffer of bytes whose MD5 signature we are calculating.
  314  *
  315  * buf_len - The length of the buffer.
  316  */
  317 void    md5_process(md5_t *md5_p, const void *buffer,
  318             const unsigned int buf_len)
  319 {
  320   unsigned int  len = buf_len;
  321   unsigned int  in_block, add;
  322 
  323   /*
  324    * When we already have some bytes in our internal buffer, copy some
  325    * from the user to fill the block.
  326    */
  327   if (md5_p->md_buf_len > 0) {
  328     
  329     in_block = md5_p->md_buf_len;
  330     if (in_block + len > sizeof(md5_p->md_buffer)) {
  331       add = sizeof(md5_p->md_buffer) - in_block;
  332     }
  333     else {
  334       add = len;
  335     }
  336 
  337     memcpy (md5_p->md_buffer + in_block, buffer, add);
  338     md5_p->md_buf_len += add;
  339     in_block += add;
  340 
  341     if (in_block > MD5_BLOCK_SIZE) {
  342       process_block (md5_p, md5_p->md_buffer, in_block & ~BLOCK_SIZE_MASK);
  343       /* the regions in the following copy operation will not overlap. */
  344       memcpy (md5_p->md_buffer,
  345           md5_p->md_buffer + (in_block & ~BLOCK_SIZE_MASK),
  346           in_block & BLOCK_SIZE_MASK);
  347       md5_p->md_buf_len = in_block & BLOCK_SIZE_MASK;
  348     }
  349 
  350     buffer = (const char *)buffer + add;
  351     len -= add;
  352   }
  353 
  354   /* process available complete blocks right from the user buffer */
  355   if (len > MD5_BLOCK_SIZE) {
  356     process_block (md5_p, buffer, len & ~BLOCK_SIZE_MASK);
  357     buffer = (const char *) buffer + (len & ~BLOCK_SIZE_MASK);
  358     len &= BLOCK_SIZE_MASK;
  359   }
  360 
  361   /* copy remaining bytes into the internal buffer */
  362   if (len > 0) {
  363     memcpy (md5_p->md_buffer, buffer, len);
  364     md5_p->md_buf_len = len;
  365   }
  366 }
  367 
  368 /*
  369  * md5_finish
  370  *
  371  * DESCRIPTION:
  372  *
  373  * Finish a progressing MD5 calculation and copy the resulting MD5
  374  * signature into the result buffer which should be 16 bytes
  375  * (MD5_SIZE).  After this call, the MD5 structure is invalid.
  376  *
  377  * RETURNS:
  378  *
  379  * None.
  380  *
  381  * ARGUMENTS:
  382  *
  383  * md5_p - Pointer to MD5 structure which we are finishing.
  384  *
  385  * signature - A 16 byte buffer that will contain the MD5 signature.
  386  */
  387 void    md5_finish(md5_t *md5_p, void *signature)
  388 {
  389   md5_uint32    bytes, hold;
  390   int       pad;
  391 
  392   /* take yet unprocessed bytes into account */
  393   bytes = md5_p->md_buf_len;
  394 
  395   /*
  396    * Count remaining bytes.  Modified to do this to better avoid
  397    * overflows in the lower word -- Gray 10/97.
  398    */
  399   if (md5_p->md_total[0] > MAX_MD5_UINT32 - bytes) {
  400     md5_p->md_total[1]++;
  401     md5_p->md_total[0] -= (MAX_MD5_UINT32 + 1 - bytes);
  402   }
  403   else {
  404     md5_p->md_total[0] += bytes;
  405   }
  406 
  407   /*
  408    * Pad the buffer to the next MD5_BLOCK-byte boundary.  (RFC 1321,
  409    * 3.1: Step 1).  We need enough room for two size words and the
  410    * bytes left in the buffer.  For some reason even if we are equal
  411    * to the block-size, we add an addition block of pad bytes.
  412    */
  413   pad = MD5_BLOCK_SIZE - (sizeof(md5_uint32) * 2) - bytes;
  414   if (pad <= 0) {
  415     pad += MD5_BLOCK_SIZE;
  416   }
  417 
  418   /*
  419    * Modified from a fixed array to this assignment and memset to be
  420    * more flexible with block-sizes -- Gray 10/97.
  421    */
  422   if (pad > 0) {
  423     /* some sort of padding start byte */
  424     md5_p->md_buffer[bytes] = (unsigned char)0x80;
  425     if (pad > 1) {
  426       memset (md5_p->md_buffer + bytes + 1, 0, pad - 1);
  427     }
  428     bytes += pad;
  429   }
  430 
  431   /*
  432    * Put the 64-bit file length in _bits_ (i.e. *8) at the end of the
  433    * buffer.  
  434    */
  435   hold = SWAP((md5_p->md_total[0] & 0x1FFFFFFF) << 3);
  436   memcpy(md5_p->md_buffer + bytes, &hold, sizeof(md5_uint32));
  437   bytes += sizeof(md5_uint32);
  438 
  439   /* shift the high word over by 3 and add in the top 3 bits from the low */
  440   hold = SWAP((md5_p->md_total[1] << 3) |
  441           ((md5_p->md_total[0] & 0xE0000000) >> 29));
  442   memcpy(md5_p->md_buffer + bytes, &hold, sizeof(md5_uint32));
  443   bytes += sizeof(md5_uint32);
  444 
  445   /* process last bytes, the padding chars, and size words */
  446   process_block(md5_p, md5_p->md_buffer, bytes);
  447   md5_get_result(md5_p, signature);
  448 }
  449 
  450 /*
  451  * md5_buffer
  452  *
  453  * DESCRIPTION:
  454  *
  455  * This function is used to calculate a MD5 signature for a buffer of
  456  * bytes.  If you only have part of a buffer that you want to process
  457  * then md5_init, md5_process, and md5_finish should be used.
  458  *
  459  * RETURNS:
  460  *
  461  * None.
  462  *
  463  * ARGUMENTS:
  464  *
  465  * buffer - A buffer of bytes whose MD5 signature we are calculating.
  466  *
  467  * buf_len - The length of the buffer.
  468  *
  469  * signature - A 16 byte buffer that will contain the MD5 signature.
  470  */
  471 void    md5_buffer(const char *buffer, const unsigned int buf_len,
  472            void *signature)
  473 {
  474   md5_t     md5;
  475 
  476   /* initialize the computation context */
  477   md5_init(&md5);
  478 
  479   /* process whole buffer but last buf_len % MD5_BLOCK bytes */
  480   md5_process(&md5, buffer, buf_len);
  481 
  482   /* put result in desired memory area */
  483   md5_finish(&md5, signature);
  484 }
  485 
  486 /*
  487  * md5_sig_to_string
  488  *
  489  * DESCRIPTION:
  490  *
  491  * Convert a MD5 signature in a 16 byte buffer into a hexadecimal string
  492  * representation.
  493  *
  494  * RETURNS:
  495  *
  496  * None.
  497  *
  498  * ARGUMENTS:
  499  *
  500  * signature - a 16 byte buffer that contains the MD5 signature.
  501  *
  502  * str - a string of charactes which should be at least 33 bytes long (2
  503  * characters per MD5 byte and 1 for the \0).
  504  *
  505  * str_len - the length of the string.
  506  */
  507 void    md5_sig_to_string(void *signature, char *str, const int str_len)
  508 {
  509   unsigned char *sig_p;
  510   char      *str_p, *max_p;
  511   unsigned int  high, low;
  512   
  513   str_p = str;
  514   max_p = str + str_len;
  515   
  516   for (sig_p = (unsigned char *)signature;
  517        sig_p < (unsigned char *)signature + MD5_SIZE;
  518        sig_p++) {
  519     high = *sig_p / 16;
  520     low = *sig_p % 16;
  521     /* account for 2 chars */
  522     if (str_p + 1 >= max_p) {
  523       break;
  524     }
  525     *str_p++ = HEX_STRING[high];
  526     *str_p++ = HEX_STRING[low];
  527   }
  528   /* account for 2 chars */
  529   if (str_p < max_p) {
  530     *str_p++ = '\0';
  531   }
  532 }
  533 
  534 /*
  535  * md5_sig_from_string
  536  *
  537  * DESCRIPTION:
  538  *
  539  * Convert a MD5 signature from a hexadecimal string representation into
  540  * a 16 byte buffer.
  541  *
  542  * RETURNS:
  543  *
  544  * None.
  545  *
  546  * ARGUMENTS:
  547  *
  548  * signature - A 16 byte buffer that will contain the MD5 signature.
  549  *
  550  * str - A string of charactes which _must_ be at least 32 bytes long (2
  551  * characters per MD5 byte).
  552  */
  553 void    md5_sig_from_string(void *signature, const char *str)
  554 {
  555   unsigned char *sig_p;
  556   const char    *str_p;
  557   char      *hex;
  558   unsigned int  high, low, val;
  559   
  560   hex = HEX_STRING;
  561   sig_p = signature;
  562   
  563   for (str_p = str; str_p < str + MD5_SIZE * 2; str_p += 2) {
  564     high = strchr(hex, *str_p) - hex;
  565     low = strchr(hex, *(str_p + 1)) - hex;
  566     val = high * 16 + low;
  567     *sig_p++ = val;
  568   }
  569 }