[BACK]Return to sha1.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_ntl / crypt / sha1

Diff for /OpenXM/src/ox_ntl/crypt/sha1/sha1.c between version 1.1 and 1.6

version 1.1, 2004/01/12 13:16:28 version 1.6, 2004/07/22 12:12:05
Line 1 
Line 1 
 /* $OpenXM$ */  /* $OpenXM: OpenXM/src/ox_ntl/crypt/sha1/sha1.c,v 1.5 2004/07/04 02:31:51 iwane Exp $ */
 /* RFC 3174 */  /* RFC 3174 - SHA-1 (US Secure Hash Algorithm 1 (SHA1))*/
   
   #include <stdio.h>
   #include <string.h>
   
   #include "sha1.h"
   
   #include <unistd.h>
   #include <errno.h>
   #include <sys/stat.h>
   
   #ifdef lint
   #define inline
   #endif
   
   
 /* Global Constant */  /* Global Constant */
 static const unsigned int K[4] = {0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6};  static const uint32_t K[4] = {0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6};
 static const unsigned int H[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0};  static const uint32_t H[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0};
   
 #define BLOCK (512 / 8)  #define BLOCK (512 / 8)
   
   
 static inline unsigned int lshift32(unsigned int x, int n)  static inline uint32_t lshift32(uint32_t x, int n)
 {  {
         return ((x << n) | (x >> (32 - n)));          return ((x << n) | (x >> (32 - n)));
 };  }
   
   
   
Line 20  static inline unsigned int lshift32(unsigned int x, in
Line 33  static inline unsigned int lshift32(unsigned int x, in
  * sizeof(buf) >= 512 * ((len * 8 + 1) / 64)   * sizeof(buf) >= 512 * ((len * 8 + 1) / 64)
  * len * 8 < 2^64 ==> len < 2^61   * len * 8 < 2^64 ==> len < 2^61
  */   */
 static unsigned int  static uint32_t
 padding(unsigned char *buf, const unsigned char *msg, int length)  padding(unsigned char *buf, const unsigned char *msg, size_t length)
 {  {
         int n;          int n;
         int i;          int i;
         int len = length % BLOCK;          size_t len = length % BLOCK;
   
         if (len == 0)  
                 len = BLOCK;  
   
         memcpy(buf, msg, len);          memcpy(buf, msg, len);
   
         buf[len++] = 0x80;          buf[len++] = 0x80;
Line 58  padding(unsigned char *buf, const unsigned char *msg, 
Line 68  padding(unsigned char *buf, const unsigned char *msg, 
   
   
   
 static unsigned int  static uint32_t
 f(unsigned int t, unsigned int b, unsigned int c, unsigned int d)  f(uint32_t t, uint32_t b, uint32_t c, uint32_t d)
 {  {
         if (t < 20) {          if (t < 20) {
                 return ((b & c) | ((~b) & d));                  return ((b & c) | ((~b) & d));
Line 72  f(unsigned int t, unsigned int b, unsigned int c, unsi
Line 82  f(unsigned int t, unsigned int b, unsigned int c, unsi
                 return ((b & c) | (b & d) | (c & d));                  return ((b & c) | (b & d) | (c & d));
         }          }
   
         // Invalid Parameter.          /* Invalid Parameter. */
         return (0);          return (0);
 }  }
   
Line 80  f(unsigned int t, unsigned int b, unsigned int c, unsi
Line 90  f(unsigned int t, unsigned int b, unsigned int c, unsi
 /* sizeof(msg) == 512 / 8.  /* sizeof(msg) == 512 / 8.
  * padding.   * padding.
  */   */
 static void  void
 md(unsigned int *h, const unsigned char *msg)  sha1_md(uint32_t *h, const unsigned char *msg)
 {  {
         int t;          int t;
         unsigned int a, b, c, d, e, temp;  
         int i;          int i;
         unsigned int w[80];  
   
         /* ....  */          uint32_t a, b, c, d, e, temp;
           uint32_t w[80];
   
           /* ... */
         for (t = 0; t < 16; t++) {          for (t = 0; t < 16; t++) {
                 w[t] = 0;                  w[t] = 0;
                 for (i = 0; i < 4; i++) {                  for (i = 0; i < 4; i++) {
Line 113  md(unsigned int *h, const unsigned char *msg)
Line 124  md(unsigned int *h, const unsigned char *msg)
                 c = lshift32(b, 30);                  c = lshift32(b, 30);
                 b = a;                  b = a;
                 a = temp;                  a = temp;
   
         }          }
   
         h[0] += a;          h[0] += a;
Line 124  md(unsigned int *h, const unsigned char *msg)
Line 134  md(unsigned int *h, const unsigned char *msg)
 }  }
   
 int  int
 sha1_h(unsigned char *Ph, const unsigned char *msg, int len, unsigned int *h)  sha1_h(unsigned char *Ph, const unsigned char *msg, size_t len, const uint32_t *hp)
 {  {
         int i, j, cnt, l = len;          int i, j, cnt;
           size_t l = len;
         unsigned char buf[1024];          unsigned char buf[1024];
           uint32_t h[sizeof(H) / sizeof(H[0])];
   
         while (l > BLOCK) {          if (hp == NULL)
                 md(h, msg);                  memcpy(h, H, sizeof(H));
           else
                   memcpy(h, hp, sizeof(h));
   
           while (l >= BLOCK) {
                   sha1_md(h, msg);
                 msg += BLOCK;                  msg += BLOCK;
                 l -= BLOCK;                  l -= BLOCK;
         }          }
   
         cnt = padding(buf, msg, len);          cnt = padding(buf, msg, len);
         for (i = 0; i < cnt; i++) {          for (i = 0; i < cnt; i++) {
                 md(h, buf + BLOCK * i);                  sha1_md(h, buf + BLOCK * i);
         }          }
   
         memset(Ph, 0x00, sizeof(H));          memset(Ph, 0x00, sizeof(H));
         for (i = 0; i < sizeof(H) / sizeof(H[0]); i++) {          for (i = 0; i < (int)(sizeof(H) / sizeof(H[0])); i++) {
                 for (j = 0; j < 32; j++) {                  for (j = 0; j < 32; j++) {
                         Ph[4 * i + j / 8] |= ((h[i] >> (31 - j)) & 1) << (7 - j % 8);                          Ph[4 * i + j / 8] |= ((h[i] >> (31 - j)) & 1) << (7 - j % 8);
                 }                  }
         }          }
   
   
         return (0);          return (0);
 }  }
   
   
 int  int
 sha1(unsigned char *Ph, const unsigned char *msg, int len)  sha1(unsigned char *Ph, const unsigned char *msg, size_t len)
 {  {
         unsigned int h[sizeof(H) / sizeof(H[0])];          return (sha1_h(Ph, msg, len, NULL));
   }
   
         memcpy(h, H, sizeof(H));  
   
         return (sha1_h(Ph, msg, len, h));  int
   fsha1_h(unsigned char *Ph, int fd, const uint32_t *hp)
   {
           int i, j, cnt, ret;
           int l;
           off_t len;
           unsigned char buf[1024], *msg, msgbuf[1024];
           uint32_t h[sizeof(H) / sizeof(H[0])];
           struct stat stbuf;
   
 }          if (hp == NULL)
                   memcpy(h, H, sizeof(H));
           else
                   memcpy(h, hp, sizeof(h));
   
           ret = fstat(fd, &stbuf);
           if (ret != 0) {
                   return (errno);
           }
   
 #ifdef SHA_DEBUG  
 /* debug */  
 #include <stdio.h>  
   
 int          msg = msgbuf;
 main()          len = stbuf.st_size;
 {          if (len == 0)
         char *a;                  goto _PADDING;
         int m, i;  
         unsigned char h[32 * 5];  
         char b[10000000];  
   
         for (i = 0; i < 1000000; i++)          for (;;) {
                 b[i] = 'a';                  l = read(fd, msgbuf, sizeof(msgbuf));
         b[i] = '\0';                  if (l < 0) {
                           return (errno);
                   }
                   if (l == 0)
                           break;
   
         a = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";                  len -= l;
         a = b;                  msg = msgbuf;
         a = "abc";  
   
         m = sha1(h, a, strlen(a));                  while (l >= BLOCK) {
                           sha1_md(h, msg);
                           msg += BLOCK;
                           l -= BLOCK;
                   }
   
         for (i = 0; i < 160 / 8; i++) {                  if (len == 0) {
                 printf("%02x", h[i] & 0xff);  _PADDING:
                 if (i % 4 == 3)                          cnt = padding(buf, msg, (size_t)(stbuf.st_size % BLOCK));
                         printf(" ");                          for (i = 0; i < cnt; i++) {
                                   sha1_md(h, buf + BLOCK * i);
                           }
                   }
         }          }
         printf("\n");  
   
   
           memset(Ph, 0x00, sizeof(H));
           for (i = 0; i < (int)(sizeof(H) / sizeof(H[0])); i++) {
                   for (j = 0; j < 32; j++) {
                           Ph[4 * i + j / 8] |= ((h[i] >> (31 - j)) & 1) << (7 - j % 8);
                   }
           }
   
         return (0);          return (0);
 }  }
   
 #endif  
   
   int
   fsha1(unsigned char *Ph, int fd)
   {
           return (fsha1_h(Ph, fd, NULL));
   }
   

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.6

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>