root/maint/gnulib/lib/md4.c

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

DEFINITIONS

This source file includes following definitions.
  1. md4_init_ctx
  2. set_uint32
  3. md4_read_ctx
  4. md4_finish_ctx
  5. md4_buffer
  6. md4_process_bytes
  7. md4_process_block

   1 /* Functions to compute MD4 message digest of files or memory blocks.
   2    according to the definition of MD4 in RFC 1320 from April 1992.
   3    Copyright (C) 1995-1997, 1999-2003, 2005-2006, 2008-2021 Free Software
   4    Foundation, Inc.
   5 
   6    This file is free software: you can redistribute it and/or modify
   7    it under the terms of the GNU Lesser General Public License as
   8    published by the Free Software Foundation; either version 2.1 of the
   9    License, or (at your option) any later version.
  10 
  11    This file is distributed in the hope that it will be useful,
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14    GNU Lesser General Public License for more details.
  15 
  16    You should have received a copy of the GNU Lesser General Public License
  17    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  18 
  19 /* Adapted by Simon Josefsson from gnulib md5.? and Libgcrypt
  20    cipher/md4.c . */
  21 
  22 #include <config.h>
  23 
  24 /* Specification.  */
  25 #include "md4.h"
  26 
  27 #include <stdalign.h>
  28 #include <stdint.h>
  29 #include <string.h>
  30 #include <sys/types.h>
  31 
  32 #include <byteswap.h>
  33 #ifdef WORDS_BIGENDIAN
  34 # define SWAP(n) bswap_32 (n)
  35 #else
  36 # define SWAP(n) (n)
  37 #endif
  38 
  39 /* This array contains the bytes used to pad the buffer to the next
  40    64-byte boundary.  (RFC 1320, 3.1: Step 1)  */
  41 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */  };
  42 
  43 
  44 /* Initialize structure containing state of computation.
  45    (RFC 1320, 3.3: Step 3)  */
  46 void
  47 md4_init_ctx (struct md4_ctx *ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49   ctx->A = 0x67452301;
  50   ctx->B = 0xefcdab89;
  51   ctx->C = 0x98badcfe;
  52   ctx->D = 0x10325476;
  53 
  54   ctx->total[0] = ctx->total[1] = 0;
  55   ctx->buflen = 0;
  56 }
  57 
  58 /* Copy the 4 byte value from v into the memory location pointed to by *cp,
  59    If your architecture allows unaligned access this is equivalent to
  60    * (uint32_t *) cp = v  */
  61 static void
  62 set_uint32 (char *cp, uint32_t v)
     /* [previous][next][first][last][top][bottom][index][help] */
  63 {
  64   memcpy (cp, &v, sizeof v);
  65 }
  66 
  67 /* Put result from CTX in first 16 bytes following RESBUF.  The result
  68    must be in little endian byte order.  */
  69 void *
  70 md4_read_ctx (const struct md4_ctx *ctx, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
  71 {
  72   char *r = resbuf;
  73   set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
  74   set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
  75   set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
  76   set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
  77 
  78   return resbuf;
  79 }
  80 
  81 /* Process the remaining bytes in the internal buffer and the usual
  82    prolog according to the standard and write the result to RESBUF.  */
  83 void *
  84 md4_finish_ctx (struct md4_ctx *ctx, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86   /* Take yet unprocessed bytes into account.  */
  87   uint32_t bytes = ctx->buflen;
  88   size_t pad;
  89 
  90   /* Now count remaining bytes.  */
  91   ctx->total[0] += bytes;
  92   if (ctx->total[0] < bytes)
  93     ++ctx->total[1];
  94 
  95   pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
  96   memcpy (&((char*)ctx->buffer)[bytes], fillbuf, pad);
  97 
  98   /* Put the 64-bit file length in *bits* at the end of the buffer.  */
  99   ctx->buffer[(bytes + pad) / 4] = SWAP (ctx->total[0] << 3);
 100   ctx->buffer[(bytes + pad) / 4 + 1] = SWAP ((ctx->total[1] << 3) |
 101                                              (ctx->total[0] >> 29));
 102 
 103   /* Process last bytes.  */
 104   md4_process_block (ctx->buffer, bytes + pad + 8, ctx);
 105 
 106   return md4_read_ctx (ctx, resbuf);
 107 }
 108 
 109 /* Compute MD4 message digest for LEN bytes beginning at BUFFER.  The
 110    result is always in little endian byte order, so that a byte-wise
 111    output yields to the wanted ASCII representation of the message
 112    digest.  */
 113 void *
 114 md4_buffer (const char *buffer, size_t len, void *resblock)
     /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116   struct md4_ctx ctx;
 117 
 118   /* Initialize the computation context.  */
 119   md4_init_ctx (&ctx);
 120 
 121   /* Process whole buffer but last len % 64 bytes.  */
 122   md4_process_bytes (buffer, len, &ctx);
 123 
 124   /* Put result in desired memory area.  */
 125   return md4_finish_ctx (&ctx, resblock);
 126 }
 127 
 128 void
 129 md4_process_bytes (const void *buffer, size_t len, struct md4_ctx *ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131   /* When we already have some bits in our internal buffer concatenate
 132      both inputs first.  */
 133   if (ctx->buflen != 0)
 134     {
 135       size_t left_over = ctx->buflen;
 136       size_t add = 128 - left_over > len ? len : 128 - left_over;
 137 
 138       memcpy (&((char*)ctx->buffer)[left_over], buffer, add);
 139       ctx->buflen += add;
 140 
 141       if (ctx->buflen > 64)
 142         {
 143           md4_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 144 
 145           ctx->buflen &= 63;
 146           /* The regions in the following copy operation cannot overlap.  */
 147           memcpy (ctx->buffer, &((char*)ctx->buffer)[(left_over + add) & ~63],
 148                   ctx->buflen);
 149         }
 150 
 151       buffer = (const char *) buffer + add;
 152       len -= add;
 153     }
 154 
 155   /* Process available complete blocks.  */
 156   if (len >= 64)
 157     {
 158 #if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
 159 # define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
 160       if (UNALIGNED_P (buffer))
 161         while (len > 64)
 162           {
 163             md4_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
 164             buffer = (const char *) buffer + 64;
 165             len -= 64;
 166           }
 167       else
 168 #endif
 169         {
 170           md4_process_block (buffer, len & ~63, ctx);
 171           buffer = (const char *) buffer + (len & ~63);
 172           len &= 63;
 173         }
 174     }
 175 
 176   /* Move remaining bytes in internal buffer.  */
 177   if (len > 0)
 178     {
 179       size_t left_over = ctx->buflen;
 180 
 181       memcpy (&((char*)ctx->buffer)[left_over], buffer, len);
 182       left_over += len;
 183       if (left_over >= 64)
 184         {
 185           md4_process_block (ctx->buffer, 64, ctx);
 186           left_over -= 64;
 187           memcpy (ctx->buffer, &ctx->buffer[16], left_over);
 188         }
 189       ctx->buflen = left_over;
 190     }
 191 }
 192 
 193 /* --- Code below is the primary difference between md5.c and md4.c --- */
 194 
 195 /* MD4 round constants */
 196 #define K1 0x5a827999
 197 #define K2 0x6ed9eba1
 198 
 199 /* Round functions.  */
 200 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
 201 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
 202 #define H(x, y, z) ((x) ^ (y) ^ (z))
 203 #define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
 204 #define R1(a,b,c,d,k,s) a=rol(a+F(b,c,d)+x[k],s);
 205 #define R2(a,b,c,d,k,s) a=rol(a+G(b,c,d)+x[k]+K1,s);
 206 #define R3(a,b,c,d,k,s) a=rol(a+H(b,c,d)+x[k]+K2,s);
 207 
 208 /* Process LEN bytes of BUFFER, accumulating context into CTX.
 209    It is assumed that LEN % 64 == 0.  */
 210 
 211 void
 212 md4_process_block (const void *buffer, size_t len, struct md4_ctx *ctx)
     /* [previous][next][first][last][top][bottom][index][help] */
 213 {
 214   const uint32_t *words = buffer;
 215   size_t nwords = len / sizeof (uint32_t);
 216   const uint32_t *endp = words + nwords;
 217   uint32_t x[16];
 218   uint32_t A = ctx->A;
 219   uint32_t B = ctx->B;
 220   uint32_t C = ctx->C;
 221   uint32_t D = ctx->D;
 222   uint32_t lolen = len;
 223 
 224   /* First increment the byte count.  RFC 1320 specifies the possible
 225      length of the file up to 2^64 bits.  Here we only compute the
 226      number of bytes.  Do a double word increment.  */
 227   ctx->total[0] += lolen;
 228   ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
 229 
 230   /* Process all bytes in the buffer with 64 bytes in each round of
 231      the loop.  */
 232   while (words < endp)
 233     {
 234       int t;
 235       for (t = 0; t < 16; t++)
 236         {
 237           x[t] = SWAP (*words);
 238           words++;
 239         }
 240 
 241       /* Round 1.  */
 242       R1 (A, B, C, D, 0, 3);
 243       R1 (D, A, B, C, 1, 7);
 244       R1 (C, D, A, B, 2, 11);
 245       R1 (B, C, D, A, 3, 19);
 246       R1 (A, B, C, D, 4, 3);
 247       R1 (D, A, B, C, 5, 7);
 248       R1 (C, D, A, B, 6, 11);
 249       R1 (B, C, D, A, 7, 19);
 250       R1 (A, B, C, D, 8, 3);
 251       R1 (D, A, B, C, 9, 7);
 252       R1 (C, D, A, B, 10, 11);
 253       R1 (B, C, D, A, 11, 19);
 254       R1 (A, B, C, D, 12, 3);
 255       R1 (D, A, B, C, 13, 7);
 256       R1 (C, D, A, B, 14, 11);
 257       R1 (B, C, D, A, 15, 19);
 258 
 259       /* Round 2.  */
 260       R2 (A, B, C, D, 0, 3);
 261       R2 (D, A, B, C, 4, 5);
 262       R2 (C, D, A, B, 8, 9);
 263       R2 (B, C, D, A, 12, 13);
 264       R2 (A, B, C, D, 1, 3);
 265       R2 (D, A, B, C, 5, 5);
 266       R2 (C, D, A, B, 9, 9);
 267       R2 (B, C, D, A, 13, 13);
 268       R2 (A, B, C, D, 2, 3);
 269       R2 (D, A, B, C, 6, 5);
 270       R2 (C, D, A, B, 10, 9);
 271       R2 (B, C, D, A, 14, 13);
 272       R2 (A, B, C, D, 3, 3);
 273       R2 (D, A, B, C, 7, 5);
 274       R2 (C, D, A, B, 11, 9);
 275       R2 (B, C, D, A, 15, 13);
 276 
 277       /* Round 3.  */
 278       R3 (A, B, C, D, 0, 3);
 279       R3 (D, A, B, C, 8, 9);
 280       R3 (C, D, A, B, 4, 11);
 281       R3 (B, C, D, A, 12, 15);
 282       R3 (A, B, C, D, 2, 3);
 283       R3 (D, A, B, C, 10, 9);
 284       R3 (C, D, A, B, 6, 11);
 285       R3 (B, C, D, A, 14, 15);
 286       R3 (A, B, C, D, 1, 3);
 287       R3 (D, A, B, C, 9, 9);
 288       R3 (C, D, A, B, 5, 11);
 289       R3 (B, C, D, A, 13, 15);
 290       R3 (A, B, C, D, 3, 3);
 291       R3 (D, A, B, C, 11, 9);
 292       R3 (C, D, A, B, 7, 11);
 293       R3 (B, C, D, A, 15, 15);
 294 
 295       A = ctx->A += A;
 296       B = ctx->B += B;
 297       C = ctx->C += C;
 298       D = ctx->D += D;
 299     }
 300 }

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