root/maint/gnulib/lib/md5.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. md5_init_ctx
  2. set_uint32
  3. md5_read_ctx
  4. md5_finish_ctx
  5. md5_buffer
  6. md5_process_bytes
  7. md5_process_block

   1 /* Functions to compute MD5 message digest of files or memory blocks.
   2    according to the definition of MD5 in RFC 1321 from April 1992.
   3    Copyright (C) 1995-1997, 1999-2001, 2005-2006, 2008-2021 Free Software
   4    Foundation, Inc.
   5    This file is part of the GNU C Library.
   6 
   7    This file is free software: you can redistribute it and/or modify
   8    it under the terms of the GNU Lesser General Public License as
   9    published by the Free Software Foundation; either version 2.1 of the
  10    License, or (at your option) any later version.
  11 
  12    This file is distributed in the hope that it will be useful,
  13    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15    GNU Lesser General Public License for more details.
  16 
  17    You should have received a copy of the GNU Lesser General Public License
  18    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  19 
  20 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
  21 
  22 #include <config.h>
  23 
  24 /* Specification.  */
  25 #if HAVE_OPENSSL_MD5
  26 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
  27 #endif
  28 #include "md5.h"
  29 
  30 #include <stdalign.h>
  31 #include <stdint.h>
  32 #include <string.h>
  33 #include <sys/types.h>
  34 
  35 #ifdef _LIBC
  36 # include <endian.h>
  37 # if __BYTE_ORDER == __BIG_ENDIAN
  38 #  define WORDS_BIGENDIAN 1
  39 # endif
  40 /* We need to keep the namespace clean so define the MD5 function
  41    protected using leading __ .  */
  42 # define md5_init_ctx __md5_init_ctx
  43 # define md5_process_block __md5_process_block
  44 # define md5_process_bytes __md5_process_bytes
  45 # define md5_finish_ctx __md5_finish_ctx
  46 # define md5_read_ctx __md5_read_ctx
  47 # define md5_buffer __md5_buffer
  48 #endif
  49 
  50 #include <byteswap.h>
  51 #ifdef WORDS_BIGENDIAN
  52 # define SWAP(n) bswap_32 (n)
  53 #else
  54 # define SWAP(n) (n)
  55 #endif
  56 
  57 #if ! HAVE_OPENSSL_MD5
  58 
  59 /* This array contains the bytes used to pad the buffer to the next
  60    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
  61 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
  62 
  63 
  64 /* Initialize structure containing state of computation.
  65    (RFC 1321, 3.3: Step 3)  */
  66 void
  67 md5_init_ctx (struct md5_ctx *ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69   ctx->A = 0x67452301;
  70   ctx->B = 0xefcdab89;
  71   ctx->C = 0x98badcfe;
  72   ctx->D = 0x10325476;
  73 
  74   ctx->total[0] = ctx->total[1] = 0;
  75   ctx->buflen = 0;
  76 }
  77 
  78 /* Copy the 4 byte value from v into the memory location pointed to by *cp,
  79    If your architecture allows unaligned access this is equivalent to
  80    * (uint32_t *) cp = v  */
  81 static void
  82 set_uint32 (char *cp, uint32_t v)
     /* [previous][next][first][last][top][bottom][index][help] */
  83 {
  84   memcpy (cp, &v, sizeof v);
  85 }
  86 
  87 /* Put result from CTX in first 16 bytes following RESBUF.  The result
  88    must be in little endian byte order.  */
  89 void *
  90 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92   char *r = resbuf;
  93   set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
  94   set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
  95   set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
  96   set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
  97 
  98   return resbuf;
  99 }
 100 
 101 /* Process the remaining bytes in the internal buffer and the usual
 102    prolog according to the standard and write the result to RESBUF.  */
 103 void *
 104 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 105 {
 106   /* Take yet unprocessed bytes into account.  */
 107   uint32_t bytes = ctx->buflen;
 108   size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
 109 
 110   /* Now count remaining bytes.  */
 111   ctx->total[0] += bytes;
 112   if (ctx->total[0] < bytes)
 113     ++ctx->total[1];
 114 
 115   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
 116   ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3);
 117   ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
 118 
 119   memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
 120 
 121   /* Process last bytes.  */
 122   md5_process_block (ctx->buffer, size * 4, ctx);
 123 
 124   return md5_read_ctx (ctx, resbuf);
 125 }
 126 
 127 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
 128    result is always in little endian byte order, so that a byte-wise
 129    output yields to the wanted ASCII representation of the message
 130    digest.  */
 131 void *
 132 md5_buffer (const char *buffer, size_t len, void *resblock)
     /* [previous][next][first][last][top][bottom][index][help] */
 133 {
 134   struct md5_ctx ctx;
 135 
 136   /* Initialize the computation context.  */
 137   md5_init_ctx (&ctx);
 138 
 139   /* Process whole buffer but last len % 64 bytes.  */
 140   md5_process_bytes (buffer, len, &ctx);
 141 
 142   /* Put result in desired memory area.  */
 143   return md5_finish_ctx (&ctx, resblock);
 144 }
 145 
 146 
 147 void
 148 md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
 149 {
 150   /* When we already have some bits in our internal buffer concatenate
 151      both inputs first.  */
 152   if (ctx->buflen != 0)
 153     {
 154       size_t left_over = ctx->buflen;
 155       size_t add = 128 - left_over > len ? len : 128 - left_over;
 156 
 157       memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
 158       ctx->buflen += add;
 159 
 160       if (ctx->buflen > 64)
 161         {
 162           md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 163 
 164           ctx->buflen &= 63;
 165           /* The regions in the following copy operation cannot overlap,
 166              because ctx->buflen < 64 ≤ (left_over + add) & ~63.  */
 167           memcpy (ctx->buffer,
 168                   &((char *) ctx->buffer)[(left_over + add) & ~63],
 169                   ctx->buflen);
 170         }
 171 
 172       buffer = (const char *) buffer + add;
 173       len -= add;
 174     }
 175 
 176   /* Process available complete blocks.  */
 177   if (len >= 64)
 178     {
 179 #if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
 180 # define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
 181       if (UNALIGNED_P (buffer))
 182         while (len > 64)
 183           {
 184             md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
 185             buffer = (const char *) buffer + 64;
 186             len -= 64;
 187           }
 188       else
 189 #endif
 190         {
 191           md5_process_block (buffer, len & ~63, ctx);
 192           buffer = (const char *) buffer + (len & ~63);
 193           len &= 63;
 194         }
 195     }
 196 
 197   /* Move remaining bytes in internal buffer.  */
 198   if (len > 0)
 199     {
 200       size_t left_over = ctx->buflen;
 201 
 202       memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
 203       left_over += len;
 204       if (left_over >= 64)
 205         {
 206           md5_process_block (ctx->buffer, 64, ctx);
 207           left_over -= 64;
 208           /* The regions in the following copy operation cannot overlap,
 209              because left_over ≤ 64.  */
 210           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
 211         }
 212       ctx->buflen = left_over;
 213     }
 214 }
 215 
 216 
 217 /* These are the four functions used in the four steps of the MD5 algorithm
 218    and defined in the RFC 1321.  The first function is a little bit optimized
 219    (as found in Colin Plumbs public domain implementation).  */
 220 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
 221 #define FF(b, c, d) (d ^ (b & (c ^ d)))
 222 #define FG(b, c, d) FF (d, b, c)
 223 #define FH(b, c, d) (b ^ c ^ d)
 224 #define FI(b, c, d) (c ^ (b | ~d))
 225 
 226 /* Process LEN bytes of BUFFER, accumulating context into CTX.
 227    It is assumed that LEN % 64 == 0.  */
 228 
 229 void
 230 md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
 231 {
 232   uint32_t correct_words[16];
 233   const uint32_t *words = buffer;
 234   size_t nwords = len / sizeof (uint32_t);
 235   const uint32_t *endp = words + nwords;
 236   uint32_t A = ctx->A;
 237   uint32_t B = ctx->B;
 238   uint32_t C = ctx->C;
 239   uint32_t D = ctx->D;
 240   uint32_t lolen = len;
 241 
 242   /* First increment the byte count.  RFC 1321 specifies the possible
 243      length of the file up to 2^64 bits.  Here we only compute the
 244      number of bytes.  Do a double word increment.  */
 245   ctx->total[0] += lolen;
 246   ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
 247 
 248   /* Process all bytes in the buffer with 64 bytes in each round of
 249      the loop.  */
 250   while (words < endp)
 251     {
 252       uint32_t *cwp = correct_words;
 253       uint32_t A_save = A;
 254       uint32_t B_save = B;
 255       uint32_t C_save = C;
 256       uint32_t D_save = D;
 257 
 258       /* First round: using the given function, the context and a constant
 259          the next context is computed.  Because the algorithms processing
 260          unit is a 32-bit word and it is determined to work on words in
 261          little endian byte order we perhaps have to change the byte order
 262          before the computation.  To reduce the work for the next steps
 263          we store the swapped words in the array CORRECT_WORDS.  */
 264 
 265 #define OP(a, b, c, d, s, T)                                            \
 266       do                                                                \
 267         {                                                               \
 268           a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;             \
 269           ++words;                                                      \
 270           CYCLIC (a, s);                                                \
 271           a += b;                                                       \
 272         }                                                               \
 273       while (0)
 274 
 275       /* It is unfortunate that C does not provide an operator for
 276          cyclic rotation.  Hope the C compiler is smart enough.  */
 277 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
 278 
 279       /* Before we start, one word to the strange constants.
 280          They are defined in RFC 1321 as
 281 
 282          T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
 283 
 284          Here is an equivalent invocation using Perl:
 285 
 286          perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
 287        */
 288 
 289       /* Round 1.  */
 290       OP (A, B, C, D, 7, 0xd76aa478);
 291       OP (D, A, B, C, 12, 0xe8c7b756);
 292       OP (C, D, A, B, 17, 0x242070db);
 293       OP (B, C, D, A, 22, 0xc1bdceee);
 294       OP (A, B, C, D, 7, 0xf57c0faf);
 295       OP (D, A, B, C, 12, 0x4787c62a);
 296       OP (C, D, A, B, 17, 0xa8304613);
 297       OP (B, C, D, A, 22, 0xfd469501);
 298       OP (A, B, C, D, 7, 0x698098d8);
 299       OP (D, A, B, C, 12, 0x8b44f7af);
 300       OP (C, D, A, B, 17, 0xffff5bb1);
 301       OP (B, C, D, A, 22, 0x895cd7be);
 302       OP (A, B, C, D, 7, 0x6b901122);
 303       OP (D, A, B, C, 12, 0xfd987193);
 304       OP (C, D, A, B, 17, 0xa679438e);
 305       OP (B, C, D, A, 22, 0x49b40821);
 306 
 307       /* For the second to fourth round we have the possibly swapped words
 308          in CORRECT_WORDS.  Redefine the macro to take an additional first
 309          argument specifying the function to use.  */
 310 #undef OP
 311 #define OP(f, a, b, c, d, k, s, T)                                      \
 312       do                                                                \
 313         {                                                               \
 314           a += f (b, c, d) + correct_words[k] + T;                      \
 315           CYCLIC (a, s);                                                \
 316           a += b;                                                       \
 317         }                                                               \
 318       while (0)
 319 
 320       /* Round 2.  */
 321       OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
 322       OP (FG, D, A, B, C, 6, 9, 0xc040b340);
 323       OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
 324       OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
 325       OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
 326       OP (FG, D, A, B, C, 10, 9, 0x02441453);
 327       OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
 328       OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
 329       OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
 330       OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
 331       OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
 332       OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
 333       OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
 334       OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
 335       OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
 336       OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
 337 
 338       /* Round 3.  */
 339       OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
 340       OP (FH, D, A, B, C, 8, 11, 0x8771f681);
 341       OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
 342       OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
 343       OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
 344       OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
 345       OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
 346       OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
 347       OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
 348       OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
 349       OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
 350       OP (FH, B, C, D, A, 6, 23, 0x04881d05);
 351       OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
 352       OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
 353       OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
 354       OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
 355 
 356       /* Round 4.  */
 357       OP (FI, A, B, C, D, 0, 6, 0xf4292244);
 358       OP (FI, D, A, B, C, 7, 10, 0x432aff97);
 359       OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
 360       OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
 361       OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
 362       OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
 363       OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
 364       OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
 365       OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
 366       OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
 367       OP (FI, C, D, A, B, 6, 15, 0xa3014314);
 368       OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
 369       OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
 370       OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
 371       OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
 372       OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
 373 
 374       /* Add the starting values of the context.  */
 375       A += A_save;
 376       B += B_save;
 377       C += C_save;
 378       D += D_save;
 379     }
 380 
 381   /* Put checksum in context given as argument.  */
 382   ctx->A = A;
 383   ctx->B = B;
 384   ctx->C = C;
 385   ctx->D = D;
 386 }
 387 
 388 #endif
 389 
 390 /*
 391  * Hey Emacs!
 392  * Local Variables:
 393  * coding: utf-8
 394  * End:
 395  */

/* [previous][next][first][last][top][bottom][index][help] */