Blame alg-md4.c

Packit 13e0ca
/*
Packit 13e0ca
 * MD4 (RFC-1320) message digest.
Packit 13e0ca
 * Modified from MD5 code by Andrey Panin <pazke@donpac.ru>
Packit 13e0ca
 *
Packit 13e0ca
 * Written by Solar Designer <solar@openwall.com> in 2001, and placed in
Packit 13e0ca
 * the public domain.  There's absolutely no warranty.
Packit 13e0ca
 *
Packit 13e0ca
 * This differs from Colin Plumb's older public domain implementation in
Packit 13e0ca
 * that no 32-bit integer data type is required, there's no compile-time
Packit 13e0ca
 * endianness configuration.
Packit 13e0ca
 * The primary goals are portability and ease of use.
Packit 13e0ca
 *
Packit 13e0ca
 * This implementation is meant to be fast, but not as fast as possible.
Packit 13e0ca
 * Some known optimizations are not included to reduce source code size
Packit 13e0ca
 * and avoid compile-time configuration.
Packit 13e0ca
 */
Packit 13e0ca
Packit 13e0ca
#include "crypt-port.h"
Packit 13e0ca
#include "alg-md4.h"
Packit 13e0ca
#include "byteorder.h"
Packit 13e0ca
Packit 13e0ca
#if INCLUDE_nthash
Packit 13e0ca
Packit 13e0ca
/*
Packit 13e0ca
 * The basic MD4 functions.
Packit 13e0ca
 */
Packit 13e0ca
#define F(x, y, z)  ((z) ^ ((x) & ((y) ^ (z))))
Packit 13e0ca
#define G(x, y, z)  (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
Packit 13e0ca
#define H(x, y, z)  ((x) ^ (y) ^ (z))
Packit 13e0ca
Packit 13e0ca
/*
Packit 13e0ca
 * The MD4 transformation for all four rounds.
Packit 13e0ca
 */
Packit 13e0ca
#define STEP(f, a, b, c, d, x, s) \
Packit 13e0ca
  (a) += f((b), (c), (d)) + (x);   \
Packit 13e0ca
  (a) = ((a) << (s)) | ((a) >> (32 - (s)))
Packit 13e0ca
Packit 13e0ca
/*
Packit 13e0ca
 * SET reads 4 input bytes in little-endian byte order and stores them
Packit 13e0ca
 * in a properly aligned word in host byte order.
Packit 13e0ca
 *
Packit 13e0ca
 * The check for little-endian architectures which tolerate unaligned
Packit 13e0ca
 * memory accesses is just an optimization.  Nothing will break if it
Packit 13e0ca
 * doesn't work.
Packit 13e0ca
 */
Packit 13e0ca
#define SET(n) \
Packit 13e0ca
  (ctx->block[(n)] = le32_to_cpu (&ptr[(n) * 4]))
Packit 13e0ca
#define GET(n) \
Packit 13e0ca
  (ctx->block[(n)])
Packit 13e0ca
Packit 13e0ca
/*
Packit 13e0ca
 * This processes one or more 64-byte data blocks, but does NOT update
Packit 13e0ca
 * the bit counters.  There're no alignment requirements.
Packit 13e0ca
 */
Packit 13e0ca
static const unsigned char *
Packit 13e0ca
body (struct md4_ctx *ctx, const unsigned char *data, size_t size)
Packit 13e0ca
{
Packit 13e0ca
  const unsigned char *ptr;
Packit 13e0ca
  uint32_t a, b, c, d;
Packit 13e0ca
  uint32_t saved_a, saved_b, saved_c, saved_d;
Packit 13e0ca
Packit 13e0ca
  ptr = data;
Packit 13e0ca
Packit 13e0ca
  a = ctx->a;
Packit 13e0ca
  b = ctx->b;
Packit 13e0ca
  c = ctx->c;
Packit 13e0ca
  d = ctx->d;
Packit 13e0ca
Packit 13e0ca
  do
Packit 13e0ca
    {
Packit 13e0ca
      saved_a = a;
Packit 13e0ca
      saved_b = b;
Packit 13e0ca
      saved_c = c;
Packit 13e0ca
      saved_d = d;
Packit 13e0ca
Packit 13e0ca
      /* Round 1 */
Packit 13e0ca
      STEP(F, a, b, c, d, SET( 0),  3);
Packit 13e0ca
      STEP(F, d, a, b, c, SET( 1),  7);
Packit 13e0ca
      STEP(F, c, d, a, b, SET( 2), 11);
Packit 13e0ca
      STEP(F, b, c, d, a, SET( 3), 19);
Packit 13e0ca
Packit 13e0ca
      STEP(F, a, b, c, d, SET( 4),  3);
Packit 13e0ca
      STEP(F, d, a, b, c, SET( 5),  7);
Packit 13e0ca
      STEP(F, c, d, a, b, SET( 6), 11);
Packit 13e0ca
      STEP(F, b, c, d, a, SET( 7), 19);
Packit 13e0ca
Packit 13e0ca
      STEP(F, a, b, c, d, SET( 8),  3);
Packit 13e0ca
      STEP(F, d, a, b, c, SET( 9),  7);
Packit 13e0ca
      STEP(F, c, d, a, b, SET(10), 11);
Packit 13e0ca
      STEP(F, b, c, d, a, SET(11), 19);
Packit 13e0ca
Packit 13e0ca
      STEP(F, a, b, c, d, SET(12),  3);
Packit 13e0ca
      STEP(F, d, a, b, c, SET(13),  7);
Packit 13e0ca
      STEP(F, c, d, a, b, SET(14), 11);
Packit 13e0ca
      STEP(F, b, c, d, a, SET(15), 19);
Packit 13e0ca
Packit 13e0ca
      /* Round 2 */
Packit 13e0ca
      STEP(G, a, b, c, d, GET( 0) + 0x5A827999,  3);
Packit 13e0ca
      STEP(G, d, a, b, c, GET( 4) + 0x5A827999,  5);
Packit 13e0ca
      STEP(G, c, d, a, b, GET( 8) + 0x5A827999,  9);
Packit 13e0ca
      STEP(G, b, c, d, a, GET(12) + 0x5A827999, 13);
Packit 13e0ca
Packit 13e0ca
      STEP(G, a, b, c, d, GET( 1) + 0x5A827999,  3);
Packit 13e0ca
      STEP(G, d, a, b, c, GET( 5) + 0x5A827999,  5);
Packit 13e0ca
      STEP(G, c, d, a, b, GET( 9) + 0x5A827999,  9);
Packit 13e0ca
      STEP(G, b, c, d, a, GET(13) + 0x5A827999, 13);
Packit 13e0ca
Packit 13e0ca
      STEP(G, a, b, c, d, GET( 2) + 0x5A827999,  3);
Packit 13e0ca
      STEP(G, d, a, b, c, GET( 6) + 0x5A827999,  5);
Packit 13e0ca
      STEP(G, c, d, a, b, GET(10) + 0x5A827999,  9);
Packit 13e0ca
      STEP(G, b, c, d, a, GET(14) + 0x5A827999, 13);
Packit 13e0ca
Packit 13e0ca
      STEP(G, a, b, c, d, GET( 3) + 0x5A827999,  3);
Packit 13e0ca
      STEP(G, d, a, b, c, GET( 7) + 0x5A827999,  5);
Packit 13e0ca
      STEP(G, c, d, a, b, GET(11) + 0x5A827999,  9);
Packit 13e0ca
      STEP(G, b, c, d, a, GET(15) + 0x5A827999, 13);
Packit 13e0ca
Packit 13e0ca
      /* Round 3 */
Packit 13e0ca
      STEP(H, a, b, c, d, GET( 0) + 0x6ED9EBA1,  3);
Packit 13e0ca
      STEP(H, d, a, b, c, GET( 8) + 0x6ED9EBA1,  9);
Packit 13e0ca
      STEP(H, c, d, a, b, GET( 4) + 0x6ED9EBA1, 11);
Packit 13e0ca
      STEP(H, b, c, d, a, GET(12) + 0x6ED9EBA1, 15);
Packit 13e0ca
Packit 13e0ca
      STEP(H, a, b, c, d, GET( 2) + 0x6ED9EBA1,  3);
Packit 13e0ca
      STEP(H, d, a, b, c, GET(10) + 0x6ED9EBA1,  9);
Packit 13e0ca
      STEP(H, c, d, a, b, GET( 6) + 0x6ED9EBA1, 11);
Packit 13e0ca
      STEP(H, b, c, d, a, GET(14) + 0x6ED9EBA1, 15);
Packit 13e0ca
Packit 13e0ca
      STEP(H, a, b, c, d, GET( 1) + 0x6ED9EBA1,  3);
Packit 13e0ca
      STEP(H, d, a, b, c, GET( 9) + 0x6ED9EBA1,  9);
Packit 13e0ca
      STEP(H, c, d, a, b, GET( 5) + 0x6ED9EBA1, 11);
Packit 13e0ca
      STEP(H, b, c, d, a, GET(13) + 0x6ED9EBA1, 15);
Packit 13e0ca
Packit 13e0ca
      STEP(H, a, b, c, d, GET( 3) + 0x6ED9EBA1,  3);
Packit 13e0ca
      STEP(H, d, a, b, c, GET(11) + 0x6ED9EBA1,  9);
Packit 13e0ca
      STEP(H, c, d, a, b, GET( 7) + 0x6ED9EBA1, 11);
Packit 13e0ca
      STEP(H, b, c, d, a, GET(15) + 0x6ED9EBA1, 15);
Packit 13e0ca
Packit 13e0ca
      a += saved_a;
Packit 13e0ca
      b += saved_b;
Packit 13e0ca
      c += saved_c;
Packit 13e0ca
      d += saved_d;
Packit 13e0ca
Packit 13e0ca
      ptr += 64;
Packit 13e0ca
    }
Packit 13e0ca
  while (size -= 64);
Packit 13e0ca
Packit 13e0ca
  ctx->a = a;
Packit 13e0ca
  ctx->b = b;
Packit 13e0ca
  ctx->c = c;
Packit 13e0ca
  ctx->d = d;
Packit 13e0ca
Packit 13e0ca
  return ptr;
Packit 13e0ca
}
Packit 13e0ca
Packit 13e0ca
/* Put result from CTX in first 16 bytes following RESBUF.  The result
Packit 13e0ca
   will be in little endian byte order.  */
Packit 13e0ca
static void *
Packit 13e0ca
md4_read_ctx (struct md4_ctx *ctx, void *resbuf)
Packit 13e0ca
{
Packit 13e0ca
  unsigned char *buf = resbuf;
Packit 13e0ca
  cpu_to_le32 (buf +  0, ctx->a);
Packit 13e0ca
  cpu_to_le32 (buf +  4, ctx->b);
Packit 13e0ca
  cpu_to_le32 (buf +  8, ctx->c);
Packit 13e0ca
  cpu_to_le32 (buf + 12, ctx->d);
Packit 13e0ca
  XCRYPT_SECURE_MEMSET (ctx, sizeof(struct md4_ctx));
Packit 13e0ca
  return resbuf;
Packit 13e0ca
}
Packit 13e0ca
Packit 13e0ca
void
Packit 13e0ca
md4_init_ctx (struct md4_ctx *ctx)
Packit 13e0ca
{
Packit 13e0ca
  ctx->a = 0x67452301;
Packit 13e0ca
  ctx->b = 0xefcdab89;
Packit 13e0ca
  ctx->c = 0x98badcfe;
Packit 13e0ca
  ctx->d = 0x10325476;
Packit 13e0ca
Packit 13e0ca
  ctx->lo = 0;
Packit 13e0ca
  ctx->hi = 0;
Packit 13e0ca
}
Packit 13e0ca
Packit 13e0ca
void
Packit 13e0ca
md4_process_bytes (const void *buffer, struct md4_ctx *ctx, size_t size)
Packit 13e0ca
{
Packit 13e0ca
  uint32_t saved_lo;
Packit 13e0ca
  size_t used, free;
Packit 13e0ca
Packit 13e0ca
  saved_lo = ctx->lo;
Packit 13e0ca
  if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
Packit 13e0ca
    ctx->hi++;
Packit 13e0ca
  ctx->hi += (uint32_t)(size >> 29);
Packit 13e0ca
Packit 13e0ca
  used = saved_lo & 0x3f;
Packit 13e0ca
Packit 13e0ca
  if (used)
Packit 13e0ca
    {
Packit 13e0ca
      free = 64 - used;
Packit 13e0ca
Packit 13e0ca
      if (size < free)
Packit 13e0ca
        {
Packit 13e0ca
          memcpy(&ctx->buffer[used], buffer, size);
Packit 13e0ca
          return;
Packit 13e0ca
        }
Packit 13e0ca
Packit 13e0ca
      memcpy(&ctx->buffer[used], buffer, free);
Packit 13e0ca
      buffer = (const unsigned char *) buffer + free;
Packit 13e0ca
      size -= free;
Packit 13e0ca
      body(ctx, ctx->buffer, 64);
Packit 13e0ca
    }
Packit 13e0ca
Packit 13e0ca
  if (size >= 64)
Packit 13e0ca
    {
Packit 13e0ca
      buffer = body(ctx, buffer, size & ~(uint32_t)0x3f);
Packit 13e0ca
      size &= 0x3f;
Packit 13e0ca
    }
Packit 13e0ca
Packit 13e0ca
  memcpy(ctx->buffer, buffer, size);
Packit 13e0ca
}
Packit 13e0ca
Packit 13e0ca
void *
Packit 13e0ca
md4_finish_ctx (struct md4_ctx *ctx, void *resbuf)
Packit 13e0ca
{
Packit 13e0ca
  size_t used, free;
Packit 13e0ca
Packit 13e0ca
  used = ctx->lo & 0x3f;
Packit 13e0ca
Packit 13e0ca
  ctx->buffer[used++] = 0x80;
Packit 13e0ca
Packit 13e0ca
  free = 64 - used;
Packit 13e0ca
Packit 13e0ca
  if (free < 8)
Packit 13e0ca
    {
Packit 13e0ca
      XCRYPT_SECURE_MEMSET (&ctx->buffer[used], free);
Packit 13e0ca
      body(ctx, ctx->buffer, 64);
Packit 13e0ca
      used = 0;
Packit 13e0ca
      free = 64;
Packit 13e0ca
    }
Packit 13e0ca
Packit 13e0ca
  XCRYPT_SECURE_MEMSET (&ctx->buffer[used], free - 8);
Packit 13e0ca
Packit 13e0ca
  ctx->lo <<= 3;
Packit 13e0ca
  ctx->buffer[56] = (unsigned char)((ctx->lo) & 0xff);
Packit 13e0ca
  ctx->buffer[57] = (unsigned char)((ctx->lo >> 8) & 0xff);
Packit 13e0ca
  ctx->buffer[58] = (unsigned char)((ctx->lo >> 16) & 0xff);
Packit 13e0ca
  ctx->buffer[59] = (unsigned char)((ctx->lo >> 24) & 0xff);
Packit 13e0ca
  ctx->buffer[60] = (unsigned char)((ctx->hi) & 0xff);
Packit 13e0ca
  ctx->buffer[61] = (unsigned char)((ctx->hi >> 8) & 0xff);
Packit 13e0ca
  ctx->buffer[62] = (unsigned char)((ctx->hi >> 16) & 0xff);
Packit 13e0ca
  ctx->buffer[63] = (unsigned char)((ctx->hi >> 24) & 0xff);
Packit 13e0ca
Packit 13e0ca
  body(ctx, ctx->buffer, 64);
Packit 13e0ca
Packit 13e0ca
  return md4_read_ctx (ctx, resbuf);
Packit 13e0ca
}
Packit 13e0ca
Packit 13e0ca
#endif