荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Lg (创造人生的传奇), 信区: Linux
标  题: [转载] md4.c
发信站: BBS 荔园晨风站 (Fri Jan 21 22:34:16 2000), 站内信件

【 以下文字转载自 Hacker 讨论区 】
【 原文由 bstone 所发表 】
发信人: cloudsky (小四), 信区: Security
标  题: md4.c
发信站: 武汉白云黄鹤站 (Wed Jan 12 16:31:39 2000), 站内信件


/*
 * md4.c
 *
 *   signature function hook for MD4 (the RSA Data Security, Inc. MD4
 *   Message Digesting Algorithm) for Tripwire.
 *
 * Gene Kim
 * Purdue University
 * October 14, 1992
 * Revised by Wang,D.B.,Dec.16,1999.
 */

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <string.h>

#define BUFSIZE 64   /* limit of in-struct buffer size */
#define SIG_MAX_LEN 200

/* Compile-time declarations of MD4 ``magic constants''.
*/
#define I0  0x67452301   /* Initial values for MD buffer */
#define I1  0xefcdab89
#define I2  0x98badcfe
#define I3  0x10325476
#define C2  013240474631   /* round 2 constant = sqrt(2) in octal */
#define C3  015666365641    /* round 3 constant = sqrt(3) in octal */
/* C2 and C3 are from Knuth, The Art of Programming, Volume 2
** (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
** Table 2, page 660.
*/
#define fs1  3      /* round 1 shift amounts */
#define fs2  7
#define fs3 11
#define fs4 19
#define gs1  3      /* round 2 shift amounts */
#define gs2  5
#define gs2  5
#define gs3  9
#define gs4 13
#define hs1  3      /* round 3 shift amounts */
#define hs2  9
#define hs3 11
#define hs4 15
/* Compile-time macro declarations for MD4.
** Note: The ``rot'' operator uses the variable ``tmp''.
** It assumes tmp is declared as unsigned int, so that the >>
** operator will shift in zeros rather than extending the sign bit.
*/
#define f(X,Y,Z)             ((X&Y) | ((~X)&Z))
#define g(X,Y,Z)             ((X&Y) | (X&Z) | (Y&Z))
#define h(X,Y,Z)             (X^Y^Z)
#define rot(X,S)             (tmp=X,(tmp<<S) | (tmp>>(32-S)))
#define ff(A,B,C,D,i,s)      A = rot((A + f(B,C,D) + X[i]),s)
#define gg(A,B,C,D,i,s)      A = rot((A + g(B,C,D) + X[i] + C2),s)
#define hh(A,B,C,D,i,s)      A = rot((A + h(B,C,D) + X[i] + C3),s)

typedef unsigned long uint32;

typedef struct {
#define gs2  5
#define gs3  9
#define gs4 13
#define hs1  3      /* round 3 shift amounts */
#define hs2  9
#define hs3 11
#define hs4 15
/* Compile-time macro declarations for MD4.
** Note: The ``rot'' operator uses the variable ``tmp''.
** It assumes tmp is declared as unsigned int, so that the >>
** operator will shift in zeros rather than extending the sign bit.
*/
#define f(X,Y,Z)             ((X&Y) | ((~X)&Z))
#define g(X,Y,Z)             ((X&Y) | (X&Z) | (Y&Z))
#define h(X,Y,Z)             (X^Y^Z)
#define rot(X,S)             (tmp=X,(tmp<<S) | (tmp>>(32-S)))
#define ff(A,B,C,D,i,s)      A = rot((A + f(B,C,D) + X[i]),s)
#define gg(A,B,C,D,i,s)      A = rot((A + g(B,C,D) + X[i] + C2),s)
#define hh(A,B,C,D,i,s)      A = rot((A + h(B,C,D) + X[i] + C3),s)

typedef unsigned long uint32;

typedef struct {
typedef struct {
  unsigned long/*int*/ buffer[4];    /* Holds 4-word result of MD computation */
  unsigned char count[8];    /* Number of bits processed so far */
  unsigned int done;    /* Nonzero means MD computation finished */
} MDstruct, *MDptr;

static MDstruct mdbucket;   /* MD4 data structure */
extern char sigs[SIG_MAX_LEN];

/* Function prototype declaration. */
extern void MDsprint(char *dest, MDptr MDp);
extern void MDsprint64(char *dest, MDptr MDp);
extern void MDbegin(MDptr MDp);
extern void MDupdate(MDptr MDp, unsigned char *X, unsigned int count);

/* This function is main routine to be called for signature.
 *
 * char *sig_md4_get (char *filename);
 *
 *   --- filename:  file that needs signature.
 */

char *sig_md4_get (char *filename)
char *sig_md4_get (char *filename)
{
    int fd_in, siglen;
    char *ps_signature;

    unsigned char buffer[BUFSIZE];
    int         readin = -1;
    MDstruct    *mdbuf;
    FILE        *fp;

    if ((fd_in = open(filename, O_RDONLY)) < 0) {
        /* skip it if we had an error */
        printf("Trying to open %s for signature", filename);
        return NULL;
    }
    ps_signature = (char *)&sigs;
    siglen = SIG_MAX_LEN;

    mdbuf = &mdbucket;

    ps_signature[0] = '\0';

    /* get stdio handle
    /* get stdio handle
     *          we use dup() so we can close() it later
     */
    if (!(fp = (FILE *) fdopen(dup(fd_in), "rb"))) {
        perror("sig_haval_get: fdopen()");
        exit(1);
    }

    /* rewind the file descriptor */
    rewind(fp);

    MDbegin (mdbuf);

    while ((readin = fread(buffer, 1, BUFSIZE, fp)) > 0) {
        MDupdate(mdbuf, buffer, readin*8);
    }
    if (readin < 0) {
        perror("sig_md4_get: read()");
        exit(1);
    }
    MDupdate(mdbuf, buffer, (unsigned)readin);

    MDsprint64(ps_signature, mdbuf);
    MDsprint64(ps_signature, mdbuf);

    fclose(fp);
    (void) close(fd_in);
    ps_signature = (char *)&sigs;
    return ps_signature;
}

/*
** ****************************************************************
** Following is implementation of MD4 Message Digest Algorithm   **
** Updated: 2/16/90 by Ronald L. Rivest                          **
** (C) 1990 RSA Data Security, Inc.                              **
** Revised by Wang,D.B.,Dec.16,1999.                             **
** ****************************************************************/

/* Implementation notes:
** This implementation assumes that ints are 32-bit quantities.
** If the machine stores the least-significant byte of an int in the
** least-addressed byte (eg., VAX and 8086), then LOWBYTEFIRST should be
** set to TRUE.  Otherwise (eg., SUNS), LOWBYTEFIRST should be set to
** FALSE.  Note that on machines with LOWBYTEFIRST FALSE the routine
** MDupdate modifies has a side-effect on its input array (the order of bytes
** MDupdate modifies has a side-effect on its input array (the order of bytes
** in each word are reversed).  If this is undesired a call to MDreverse(X) can
** reverse the bytes of X back into order after each call to MDupdate.
*/

/* MDprint(MDp)
** Print message digest buffer MDp as 32 hexadecimal digits.
** Order is from low-order byte of buffer[0] to high-order byte of buffer[3].
** Each byte is printed with high-order hexadecimal digit first.
** This is a user-callable routine.
*/
long lbuf[4];
void MDsprint(char *dest, MDptr MDp)
{ int i,j;
  char s[200], *pc;
  for (i=0;i<4;i++) {
    pc = s;
    for (j=0;j<32;j=j+8) {
      sprintf(pc, "%02x", (unsigned int )( (MDp->buffer[i]>>j) & 0xFF ));
      pc += 2;
    }
    sscanf(s, "%lx", &lbuf[i]);
  }
  }
  for (i = 0, pc = dest; i < 4; i++, pc += 8) {
      sprintf(pc, "%08lx", lbuf[i]);
  }
}

extern char *pltob64(unsigned long *pl, char *pcout, int numlongs);

void MDsprint64(char *dest, MDptr MDp)
{
  pltob64(MDp->buffer, dest, 4);
}

/* MDbegin(MDp)
** Initialize message digest buffer MDp.
** This is a user-callable routine.
*/
void MDbegin(MDptr MDp)
{ int i;
  MDp->buffer[0] = I0;
  MDp->buffer[1] = I1;
  MDp->buffer[2] = I2;
  MDp->buffer[3] = I3;
  MDp->buffer[3] = I3;
  for (i=0;i<8;i++) MDp->count[i] = 0;
  MDp->done = 0;
}

/* MDreverse(X)
** Reverse the byte-ordering of every int in X.
** Assumes X is an array of 16 ints.
** The macro revx reverses the byte-ordering of the next word of X.
*/
#define revx { t = (*X << 16) | (*X >> 16); \
               *X++ = ((t & 0xFF00FF00) >> 8) | ((t & 0x00FF00FF) << 8); }
void MDreverse(unsigned int *X)
{
  register unsigned int t;
  revx; revx; revx; revx; revx; revx; revx; revx;
  revx; revx; revx; revx; revx; revx; revx; revx;
}

/* MDblock(MDp,X)
** Update message digest buffer MDp->buffer using 16-word data block X.
** Assumes all 16 words of X are full of data.
** Does not update MDp->count.
** Does not update MDp->count.
** This routine is not user-callable.
*/
static void MDblock(MDptr MDp, unsigned int *X)
{
  register unsigned int tmp, A, B, C, D;
#if BYTEORDER == 0x4321
  MDreverse(X);
#endif
  A = MDp->buffer[0];
  B = MDp->buffer[1];
  C = MDp->buffer[2];
  D = MDp->buffer[3];
  /* Update the message digest buffer */
  ff(A , B , C , D ,  0 , fs1); /* Round 1 */
  ff(D , A , B , C ,  1 , fs2);
  ff(C , D , A , B ,  2 , fs3);
  ff(B , C , D , A ,  3 , fs4);
  ff(A , B , C , D ,  4 , fs1);
  ff(D , A , B , C ,  5 , fs2);
  ff(C , D , A , B ,  6 , fs3);
  ff(B , C , D , A ,  7 , fs4);
  ff(A , B , C , D ,  8 , fs1);
  gg(C , D , A , B , 11 , gs3);
  gg(B , C , D , A , 15 , gs4);
  hh(A , B , C , D ,  0 , hs1); /* Round 3 */
  hh(D , A , B , C ,  8 , hs2);
  hh(C , D , A , B ,  4 , hs3);
  hh(B , C , D , A , 12 , hs4);
  hh(A , B , C , D ,  2 , hs1);
  hh(D , A , B , C , 10 , hs2);
  hh(C , D , A , B ,  6 , hs3);
  hh(B , C , D , A , 14 , hs4);
  hh(A , B , C , D ,  1 , hs1);
  hh(D , A , B , C ,  9 , hs2);
  hh(C , D , A , B ,  5 , hs3);
  hh(B , C , D , A , 13 , hs4);
  hh(A , B , C , D ,  3 , hs1);
  hh(D , A , B , C , 11 , hs2);
  hh(C , D , A , B ,  7 , hs3);
  hh(B , C , D , A , 15 , hs4);
  MDp->buffer[0] += A;
  MDp->buffer[1] += B;
  MDp->buffer[2] += C;
  MDp->buffer[3] += D;
}
}

/* MDupdate(MDp,X,count)
** Input: MDp -- an MDptr
**        X -- a pointer to an array of unsigned characters.
**        count -- the number of bits of X to use.
**                 (if not a multiple of 8, uses high bits of last byte.)
** Update MDp using the number of bits of X given by count.
** This is the basic input routine for an MD4 user.
** The routine completes the MD computation when count < 512, so
** every MD computation should end with one call to MDupdate with a
** count less than 512.  A call with count 0 will be ignored if the
** MD has already been terminated (done != 0), so an extra call with count
** 0 can be given as a ``courtesy close'' to force termination if desired.
*/

void MDupdate(MDptr MDp, unsigned char *X, unsigned int count)
{
  unsigned int i, tmp, bit, byte, mask;
  unsigned char XX[64];
  unsigned char *p;
  /* return with no error if this is a courtesy close with count
  ** zero and MDp->done is true.
  ** zero and MDp->done is true.
  */
  if (count == 0 && MDp->done) return;
  /* check to see if MD is already done and report error */
  if (MDp->done) { printf("\nError: MDupdate MD already done."); return; }
  /* Add count to MDp->count */
  tmp = count;
  p = MDp->count;
  while (tmp)
    { tmp += *p;
      *p++ = tmp;
      tmp = tmp >> 8;
    }
  /* Process data */
  if (count == 512)
    { /* Full block of data to handle */
      MDblock(MDp,(unsigned int *)X);
    }
  else if (count > 512) /* Check for count too large */
    { printf("\nError: MDupdate called with illegal count value %d.",count);
      return;
    }
  else /* partial block -- must be last block so finish up */
  else /* partial block -- must be last block so finish up */
    { /* Find out how many bytes and residual bits there are */
      byte = count >> 3;
      bit =  count & 7;
      /* Copy X into XX since we need to modify it */
      for (i=0;i<=byte;i++)   XX[i] = X[i];
      for (i=byte+1;i<64;i++) XX[i] = 0;
      /* Add padding '1' bit and low-order zeros in last byte */
      mask = 1 << (7 - bit);
      XX[byte] = (XX[byte] | mask) & ~( mask - 1);
      /* If room for bit count, finish up with this block */
      if (byte <= 55)
        { for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
          MDblock(MDp,(unsigned int *)XX);
        }
      else /* need to do two blocks to finish up */
        { MDblock(MDp,(unsigned int *)XX);
          for (i=0;i<56;i++) XX[i] = 0;
          for (i=0;i<8;i++)  XX[56+i] = MDp->count[i];
          MDblock(MDp,(unsigned int *)XX);
        }
      /* Set flag saying we're done with MD computation */
      MDp->done = 1;
        { for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
    }
}

--
            我问飘逝的风:来迟了?
            风感慨:是的,他们已经宣战。
            我问苏醒的大地:还有希望么?
            大地揉了揉眼睛:还有,还有无数代的少年。
            我问长空中的英魂:你们相信?
            英魂带着笑意离去:相信,希望还在。

※ 来源:.武汉白云黄鹤站 bbs.whnet.edu.cn.[FROM: 203.207.226.124]

--
☆ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: bbs@192.168.28.28]
--
※ 转载:·BBS 荔园晨风站 bbs.szu.edu.cn·[FROM: 210.39.3.97]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店