"Fossies" - the Fresh Open Source Software Archive

Member "xorriso-1.5.4/libjte/md5.c" (30 Jan 2021, 10006 Bytes) of package /linux/misc/xorriso-1.5.4.pl02.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 and the last Fossies "Diffs" side-by-side code changes report: 1.5.2_vs_1.5.4.

    1 /*
    2  * This file has been modified for the cdrkit suite.
    3  *
    4  * The behaviour and appearance of the program code below can differ to a major
    5  * extent from the version distributed by the original author(s).
    6  *
    7  * For details, see Changelog file distributed with the cdrkit package. If you
    8  * received this file from another source then ask the distributing person for
    9  * a log of modifications.
   10  *
   11  */
   12 
   13 /*
   14  * This code implements the MD5 message-digest algorithm.
   15  * The algorithm is due to Ron Rivest.  This code was
   16  * written by Colin Plumb in 1993, no copyright is claimed.
   17  * This code is in the public domain; do with it what you wish.
   18  *
   19  * Equivalent code is available from RSA Data Security, Inc.
   20  * This code has been tested against that, and is equivalent,
   21  * except that you don't need to include two pages of legalese
   22  * with every copy.
   23  *
   24  * To compute the message digest of a chunk of bytes, declare an
   25  * MD5Context structure, pass it to MD5Init, call MD5Update as
   26  * needed on buffers full of bytes, and then call MD5Final, which
   27  * will fill a supplied 16-byte array with the digest.
   28  */
   29 
   30 /* This code was modified in 1997 by Jim Kingdon of Cyclic Software to
   31    not require an integer type which is exactly 32 bits.  This work
   32    draws on the changes for the same purpose by Tatu Ylonen
   33    <ylo@cs.hut.fi> as part of SSH, but since I didn't actually use
   34    that code, there is no copyright issue.  I hereby disclaim
   35    copyright in any changes I have made; this code remains in the
   36    public domain.  */
   37 
   38 /* Note regarding cvs_* namespace: this avoids potential conflicts
   39    with libraries such as some versions of Kerberos.  No particular
   40    need to worry about whether the system supplies an MD5 library, as
   41    this file is only about 3k of object code.  */
   42 
   43 /* Steve McIntyre, 2004/05/31: borrowed this code from the CVS
   44    library. s/cvs_/mk_/ across the source */
   45 
   46 #ifdef HAVE_CONFIG_H
   47 #include "../config.h"
   48 #endif
   49 
   50 #include <string.h> /* for memcpy() and memset() */
   51 #include <stdio.h>
   52 #include <errno.h>
   53 #include <stdlib.h>
   54 
   55 #ifdef HAVE_STDINT_H
   56 #include <stdint.h>
   57 #else
   58 #ifdef HAVE_INTTYPES_H
   59 #include <inttypes.h>
   60 #endif
   61 #endif
   62 
   63 #include "md5.h"
   64 
   65 /* Little-endian byte-swapping routines.  Note that these do not
   66    depend on the size of datatypes such as mk_uint32, nor do they require
   67    us to detect the endianness of the machine we are running on.  It
   68    is possible they should be macros for speed, but I would be
   69    surprised if they were a performance bottleneck for MD5.  */
   70 
   71 static mk_uint32
   72 getu32 (const unsigned char *addr)
   73 {
   74     return (((((unsigned long)addr[3] << 8) | addr[2]) << 8)
   75         | addr[1]) << 8 | addr[0];
   76 }
   77 
   78 static void
   79 putu32 (mk_uint32 data, unsigned char *addr)
   80 {
   81     addr[0] = (unsigned char)data;
   82     addr[1] = (unsigned char)(data >> 8);
   83     addr[2] = (unsigned char)(data >> 16);
   84     addr[3] = (unsigned char)(data >> 24);
   85 }
   86 
   87 /*
   88  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
   89  * initialization constants.
   90  */
   91 void
   92 mk_MD5Init (struct mk_MD5Context *ctx)
   93 {
   94     ctx->buf[0] = 0x67452301;
   95     ctx->buf[1] = 0xefcdab89;
   96     ctx->buf[2] = 0x98badcfe;
   97     ctx->buf[3] = 0x10325476;
   98 
   99     ctx->bits[0] = 0;
  100     ctx->bits[1] = 0;
  101 }
  102 
  103 /*
  104  * Update context to reflect the concatenation of another buffer full
  105  * of bytes.
  106  */
  107 void
  108 mk_MD5Update (struct mk_MD5Context *ctx, unsigned char const *buf, unsigned len)
  109 {
  110     mk_uint32 t;
  111 
  112     /* Update bitcount */
  113 
  114     t = ctx->bits[0];
  115     if ((ctx->bits[0] = (t + ((mk_uint32)len << 3)) & 0xffffffff) < t)
  116         ctx->bits[1]++; /* Carry from low to high */
  117     ctx->bits[1] += len >> 29;
  118 
  119     t = (t >> 3) & 0x3f;    /* Bytes already in shsInfo->data */
  120 
  121     /* Handle any leading odd-sized chunks */
  122 
  123     if ( t ) {
  124         unsigned char *p = ctx->in + t;
  125 
  126         t = 64-t;
  127         if (len < t) {
  128             memcpy(p, buf, len);
  129             return;
  130         }
  131         memcpy(p, buf, t);
  132         mk_MD5Transform (ctx->buf, ctx->in);
  133         buf += t;
  134         len -= t;
  135     }
  136 
  137     /* Process data in 64-byte chunks */
  138 
  139     while (len >= 64) {
  140         memcpy(ctx->in, buf, 64);
  141         mk_MD5Transform (ctx->buf, ctx->in);
  142         buf += 64;
  143         len -= 64;
  144     }
  145 
  146     /* Handle any remaining bytes of data. */
  147 
  148     memcpy(ctx->in, buf, len);
  149 }
  150 
  151 /*
  152  * Final wrapup - pad to 64-byte boundary with the bit pattern 
  153  * 1 0* (64-bit count of bits processed, MSB-first)
  154  */
  155 void
  156 mk_MD5Final (unsigned char digest[16], struct mk_MD5Context *ctx)
  157 {
  158     unsigned count;
  159     unsigned char *p;
  160 
  161     /* Compute number of bytes mod 64 */
  162     count = (ctx->bits[0] >> 3) & 0x3F;
  163 
  164     /* Set the first char of padding to 0x80.  This is safe since there is
  165        always at least one byte free */
  166     p = ctx->in + count;
  167     *p++ = 0x80;
  168 
  169     /* Bytes of padding needed to make 64 bytes */
  170     count = 64 - 1 - count;
  171 
  172     /* Pad out to 56 mod 64 */
  173     if (count < 8) {
  174         /* Two lots of padding:  Pad the first block to 64 bytes */
  175         memset(p, 0, count);
  176         mk_MD5Transform (ctx->buf, ctx->in);
  177 
  178         /* Now fill the next block with 56 bytes */
  179         memset(ctx->in, 0, 56);
  180     } else {
  181         /* Pad block to 56 bytes */
  182         memset(p, 0, count-8);
  183     }
  184 
  185     /* Append length in bits and transform */
  186     putu32(ctx->bits[0], ctx->in + 56);
  187     putu32(ctx->bits[1], ctx->in + 60);
  188 
  189     mk_MD5Transform (ctx->buf, ctx->in);
  190     putu32(ctx->buf[0], digest);
  191     putu32(ctx->buf[1], digest + 4);
  192     putu32(ctx->buf[2], digest + 8);
  193     putu32(ctx->buf[3], digest + 12);
  194     memset(ctx, 0, sizeof(*ctx));   /* In case it's sensitive */
  195 }
  196 
  197 /* The four core functions - F1 is optimized somewhat */
  198 
  199 /* #define F1(x, y, z) (x & y | ~x & z) */
  200 #define F1(x, y, z) (z ^ (x & (y ^ z)))
  201 #define F2(x, y, z) F1(z, x, y)
  202 #define F3(x, y, z) (x ^ y ^ z)
  203 #define F4(x, y, z) (y ^ (x | ~z))
  204 
  205 /* This is the central step in the MD5 algorithm. */
  206 #define MD5STEP(f, w, x, y, z, data, s) \
  207     ( w += f(x, y, z) + data, w &= 0xffffffff, w = w<<s | w>>(32-s), w += x )
  208 
  209 /*
  210  * The core of the MD5 algorithm, this alters an existing MD5 hash to
  211  * reflect the addition of 16 longwords of new data.  MD5Update blocks
  212  * the data and converts bytes into longwords for this routine.
  213  */
  214 void
  215 mk_MD5Transform (mk_uint32 buf[4], const unsigned char inraw[64])
  216 {
  217     register mk_uint32 a, b, c, d;
  218     mk_uint32 in[16];
  219     int i;
  220 
  221     for (i = 0; i < 16; ++i)
  222         in[i] = getu32 (inraw + 4 * i);
  223 
  224     a = buf[0];
  225     b = buf[1];
  226     c = buf[2];
  227     d = buf[3];
  228 
  229     MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478,  7);
  230     MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
  231     MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
  232     MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
  233     MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf,  7);
  234     MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
  235     MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
  236     MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
  237     MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8,  7);
  238     MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
  239     MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
  240     MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
  241     MD5STEP(F1, a, b, c, d, in[12]+0x6b901122,  7);
  242     MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
  243     MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
  244     MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
  245 
  246     MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562,  5);
  247     MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340,  9);
  248     MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
  249     MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
  250     MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d,  5);
  251     MD5STEP(F2, d, a, b, c, in[10]+0x02441453,  9);
  252     MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
  253     MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
  254     MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6,  5);
  255     MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6,  9);
  256     MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
  257     MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
  258     MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905,  5);
  259     MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8,  9);
  260     MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
  261     MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
  262 
  263     MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942,  4);
  264     MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
  265     MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
  266     MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
  267     MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44,  4);
  268     MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
  269     MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
  270     MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
  271     MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6,  4);
  272     MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
  273     MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
  274     MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
  275     MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039,  4);
  276     MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
  277     MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
  278     MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
  279 
  280     MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244,  6);
  281     MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
  282     MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
  283     MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
  284     MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3,  6);
  285     MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
  286     MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
  287     MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
  288     MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f,  6);
  289     MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
  290     MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
  291     MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
  292     MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82,  6);
  293     MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
  294     MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
  295     MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
  296 
  297     buf[0] += a;
  298     buf[1] += b;
  299     buf[2] += c;
  300     buf[3] += d;
  301 }
  302 
  303 #ifdef TEST
  304 /* Simple test program.  Can use it to manually run the tests from
  305    RFC1321 for example.  */
  306 #include <stdio.h>
  307 
  308 int
  309 main (int argc, char *argv[])
  310 {
  311     struct mk_MD5Context context;
  312     unsigned char checksum[16];
  313     int i;
  314     int j;
  315 
  316     if (argc < 2)
  317     {
  318         fprintf (stderr, "usage: %s string-to-hash\n", argv[0]);
  319         exit (1);
  320     }
  321     for (j = 1; j < argc; ++j)
  322     {
  323         printf ("MD5 (\"%s\") = ", argv[j]);
  324         mk_MD5Init (&context);
  325         mk_MD5Update (&context, argv[j], strlen (argv[j]));
  326         mk_MD5Final (checksum, &context);
  327         for (i = 0; i < 16; i++)
  328         {
  329             printf ("%02x", (unsigned int) checksum[i]);
  330         }
  331         printf ("\n");
  332     }
  333     return 0;
  334 }
  335 #endif /* TEST */