⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sha.c

📁 wm PNE 3.3 source code, running at more than vxworks6.x version.
💻 C
字号:
/* * $Log: sha.c,v $ * Revision 1.3  2003/01/16 18:18:56  josh * directory structure shifting * * Revision 1.2  2001/11/06 22:15:53  tneale * Fixed for newest file layout * * Revision 1.1.1.1  2001/11/05 17:48:39  tneale * Tornado shuffle * * Revision 1.4  2001/01/19 22:21:31  paul * Update copyright. * * Revision 1.3  2000/03/17 00:16:56  meister * Update copyright message * * Revision 1.2  1998/06/16 05:12:26  sar * Clean up some type questions * * Revision 1.1  1998/05/22 18:03:35  sar * SHA digest functions * *//* [clearcase]modification history-------------------01b,20apr05,job  update copyright notices01a,11dec03,job  fix copyright statements*//* NIST Secure Hash Algorithm *//* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> *//* from Peter C. Gutmann's implementation as found in *//* Applied Cryptography by Bruce Schneier *//* Further modifications to include the "UNRAVEL" stuff, below *//* This code is in the public domain */#include <wrn/wm/common/install.h>#include <wrn/wm/common/types.h>#include <wrn/wm/common/config.h>#include <wrn/wm/common/glue.h>#include <wrn/wm/common/sha.h>/* Always use version 1, but leave the code around in case   somebody wants to know that the difference is */#define SHA_VERSION 1/* SHA f()-functions */#define f1(x,y,z)	((x & y) | (~x & z))#define f2(x,y,z)	(x ^ y ^ z)#define f3(x,y,z)	((x & y) | (x & z) | (y & z))#define f4(x,y,z)	(x ^ y ^ z)/* SHA constants */#define CONST1		0x5a827999L#define CONST2		0x6ed9eba1L#define CONST3		0x8f1bbcdcL#define CONST4		0xca62c1d6L/* 32-bit rotate */#define R32(x,n)	((x << n) | (x >> (32 - n)))#define FG(n)	\    T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n;	\    E = D; D = C; C = R32(B,30); B = A; A = T/* do SHA transformation */static void sha_transform (SHA_CTX *sha_info){    int i;    bits8_t *dp;    bits32_t T, A, B, C, D, E, W[80], *WP;    dp = sha_info->data;#if (INSTALL_ON_LITTLE_ENDIAN)    for (i = 0; i < 16; ++i)     {	T = *((bits32_t *) dp);	dp += 4;	W[i] =  ((T << 24) & 0xff000000L) | ((T <<  8) & 0x00ff0000L) |		((T >>  8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);    }#endif /* #if (INSTALL_ON_LITTLE_ENDIAN) */#if (INSTALL_ON_BIG_ENDIAN)    for (i = 0; i < 16; ++i)     {	T = *((bits32_t *) dp);	dp += 4;	W[i] = T;    }#endif /* #if (INSTALL_ON_BIG_ENDIAN) **/    for (i = 16; i < 80; ++i)     {	W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];#if (SHA_VERSION == 1)	W[i] = R32(W[i], 1);#endif /* SHA_VERSION */    }    A = sha_info->buf[0];    B = sha_info->buf[1];    C = sha_info->buf[2];    D = sha_info->buf[3];    E = sha_info->buf[4];    WP = W;    for (i =  0; i < 20; ++i) { FG(1); }    for (i = 20; i < 40; ++i) { FG(2); }    for (i = 40; i < 60; ++i) { FG(3); }    for (i = 60; i < 80; ++i) { FG(4); }    sha_info->buf[0] += A;    sha_info->buf[1] += B;    sha_info->buf[2] += C;    sha_info->buf[3] += D;    sha_info->buf[4] += E;}/* initialize the SHA digest */void sha_init(SHA_CTX *sha_info){    sha_info->buf[0] = 0x67452301L;    sha_info->buf[1] = 0xefcdab89L;    sha_info->buf[2] = 0x98badcfeL;    sha_info->buf[3] = 0x10325476L;    sha_info->buf[4] = 0xc3d2e1f0L;    sha_info->count_lo = 0L;    sha_info->count_hi = 0L;    sha_info->local = 0;}/* update the SHA digest */void sha_update(SHA_CTX  *sha_info,		bits8_t  *buffer,		bits32_t  count){    bits32_t clo;    size_t i;    clo = sha_info->count_lo + ((bits32_t) count << 3);    if (clo < sha_info->count_lo) {	++sha_info->count_hi;    }    sha_info->count_lo = clo;    sha_info->count_hi += (bits32_t) count >> 29;    if (sha_info->local)     {	i = SHA_BLOCKSIZE - sha_info->local;	if (i > count)             i = (size_t)count;		MEMCPY(sha_info->data + sha_info->local, buffer, i);	count -= i;	buffer += i;	sha_info->local += i;	if (sha_info->local == SHA_BLOCKSIZE) 	    sha_transform(sha_info);        else 	    return;    }    while (count >= SHA_BLOCKSIZE)     {      MEMCPY (sha_info->data, buffer, SHA_BLOCKSIZE);           buffer += SHA_BLOCKSIZE;           count -= SHA_BLOCKSIZE;           sha_transform (sha_info);    }    if (count) {        MEMCPY (sha_info->data, buffer, (size_t)count);        }    sha_info->local = (size_t)count;}/* finish computing the SHA digest */void sha_final (SHA_CTX *sha_info){    int count, i, ii;    bits32_t lo_bit_count, hi_bit_count;    lo_bit_count = sha_info->count_lo;    hi_bit_count = sha_info->count_hi;    count = (int) ((lo_bit_count >> 3) & 0x3f);    ((bits8_t *) sha_info->data)[count++] = 0x80;    if (count > SHA_BLOCKSIZE - 8) {	MEMSET(sha_info->data + count, 0, SHA_BLOCKSIZE - count);	sha_transform(sha_info);	MEMSET(sha_info->data, 0, SHA_BLOCKSIZE - 8);    } else {	MEMSET(sha_info->data + count, 0, SHA_BLOCKSIZE - 8 - count);    }    sha_info->data[56] = (bits8_t) ((hi_bit_count >> 24) & 0xffL);    sha_info->data[57] = (bits8_t) ((hi_bit_count >> 16) & 0xffL);    sha_info->data[58] = (bits8_t) ((hi_bit_count >>  8) & 0xffL);    sha_info->data[59] = (bits8_t) ((hi_bit_count      ) & 0xffL);    sha_info->data[60] = (bits8_t) ((lo_bit_count >> 24) & 0xffL);    sha_info->data[61] = (bits8_t) ((lo_bit_count >> 16) & 0xffL);    sha_info->data[62] = (bits8_t) ((lo_bit_count >>  8) & 0xffL);    sha_info->data[63] = (bits8_t) ((lo_bit_count      ) & 0xffL);    sha_transform(sha_info);    /* store buffer in digest */    for(i = 0, ii = 0; i < 5; i++, ii += 4) {        sha_info->digest[ii]   = (bits8_t)((sha_info->buf[i] >> 24) & 0xFF);        sha_info->digest[ii+1] = (bits8_t)((sha_info->buf[i] >> 16) & 0xFF);        sha_info->digest[ii+2] = (bits8_t)((sha_info->buf[i] >>  8) & 0xFF);        sha_info->digest[ii+3] = (bits8_t)((sha_info->buf[i]) & 0xFF);        }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -