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

📄 sha2.c

📁 cryptlib是功能强大的安全工具集。允许开发人员快速在自己的软件中集成加密和认证服务。
💻 C
📖 第 1 页 / 共 3 页
字号:
/* --------------------------------------------------------------------------- Copyright (c) 2002, Dr Brian Gladman, Worcester, UK.   All rights reserved. LICENSE TERMS The free distribution and use of this software in both source and binary form is allowed (with or without changes) provided that:   1. distributions of this source code include the above copyright      notice, this list of conditions and the following disclaimer;   2. distributions in binary form include the above copyright      notice, this list of conditions and the following disclaimer      in the documentation and/or other associated materials;   3. the copyright holder's name is not used to endorse products      built using this software without specific written permission. ALTERNATIVELY, provided that this notice is retained in full, this product may be distributed under the terms of the GNU General Public License (GPL), in which case the provisions of the GPL apply INSTEAD OF those given above. DISCLAIMER This software is provided 'as is' with no explicit or implied warranties in respect of its properties, including, but not limited to, correctness and/or fitness for purpose. --------------------------------------------------------------------------- Issue Date: 23/02/2005 This is a byte oriented version of SHA2 that operates on arrays of bytes stored in memory. This code implements sha256, sha384 and sha512 but the latter two functions rely on efficient 64-bit integer operations that may not be very efficient on 32-bit machines The sha256 functions use a type 'sha256_ctx' to hold details of the current hash state and uses the following three calls:       void sha256_begin(sha256_ctx ctx[1])       void sha256_hash(const unsigned char data[],                            unsigned long len, sha256_ctx ctx[1])       void sha_end1(unsigned char hval[], sha256_ctx ctx[1]) The first subroutine initialises a hash computation by setting up the context in the sha256_ctx context. The second subroutine hashes 8-bit bytes from array data[] into the hash state withinh sha256_ctx context, the number of bytes to be hashed being given by the the unsigned long integer len.  The third subroutine completes the hash calculation and places the resulting digest value in the array of 8-bit bytes hval[]. The sha384 and sha512 functions are similar and use the interfaces:       void sha384_begin(sha384_ctx ctx[1]);       void sha384_hash(const unsigned char data[],                            unsigned long len, sha384_ctx ctx[1]);       void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);       void sha512_begin(sha512_ctx ctx[1]);       void sha512_hash(const unsigned char data[],                            unsigned long len, sha512_ctx ctx[1]);       void sha512_end(unsigned char hval[], sha512_ctx ctx[1]); In addition there is a function sha2 that can be used to call all these functions using a call with a hash length parameter as follows:       int sha2_begin(unsigned long len, sha2_ctx ctx[1]);       void sha2_hash(const unsigned char data[],                            unsigned long len, sha2_ctx ctx[1]);       void sha2_end(unsigned char hval[], sha2_ctx ctx[1]); My thanks to Erik Andersen <andersen@codepoet.org> for testing this code on big-endian systems and for his assistance with corrections*/#if 0#define UNROLL_SHA2     /* for SHA2 loop unroll     */#endif#include <string.h>     /* for memcpy() etc.        */#include <stdlib.h>     /* for _lrotr with VC++     */#if defined( INC_ALL ) || defined( INC_CHILD )  #include "sha2.h"#else  #include "crypt/sha2.h"#endif#if defined(__cplusplus)extern "C"{#endif/*  PLATFORM SPECIFIC INCLUDES AND BYTE ORDER IN 32-BIT WORDS    To obtain the highest speed on processors with 32-bit words, this code    needs to determine the byte order of the target machine. The following    block of code is an attempt to capture the most obvious ways in which    various environemnts define byte order. It may well fail, in which case    the definitions will need to be set by editing at the points marked    **** EDIT HERE IF NECESSARY **** below.  My thanks go to Peter Gutmann    for his assistance with this endian detection nightmare.*/#define BRG_LITTLE_ENDIAN   1234 /* byte 0 is least significant (i386) */#define BRG_BIG_ENDIAN      4321 /* byte 0 is most significant (mc68k) */#define __CRYPTLIB__#if defined(__CRYPTLIB__)#  if defined( INC_ALL )#    include "crypt.h"#  elif defined( INC_CHILD )#    include "../crypt.h"#  else#    include "crypt.h"#  endif#  if defined(DATA_LITTLEENDIAN)#    define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN#  else#    define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN#  endif#else#if defined(__GNUC__) || defined(__GNU_LIBRARY__)#  if defined(__FreeBSD__) || defined(__OpenBSD__)#    include <sys/endian.h>#  elif defined( BSD ) && ( BSD >= 199103 )#      include <machine/endian.h>#  elif defined( __DJGPP__ ) || defined( __CYGWIN32__ )#      include <machine/endian.h>#  elif defined(__APPLE__)#    if defined(__BIG_ENDIAN__) && !defined( BIG_ENDIAN )#      define BIG_ENDIAN#    elif defined(__LITTLE_ENDIAN__) && !defined( LITTLE_ENDIAN )#      define LITTLE_ENDIAN#    endif#  else#    include <endian.h>#    if !defined(__BEOS__)#      include <byteswap.h>#    endif#  endif#endif#endif /* cryptlib */#if !defined(PLATFORM_BYTE_ORDER)#  if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)#    if    defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)#      define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN#    elif !defined(LITTLE_ENDIAN) &&  defined(BIG_ENDIAN)#      define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN#    elif defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)#      define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN#    elif defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)#      define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN#    endif#  elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)#    if    defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)#      define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN#    elif !defined(_LITTLE_ENDIAN) &&  defined(_BIG_ENDIAN)#      define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN#    elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _LITTLE_ENDIAN)#      define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN#    elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN)#      define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN#   endif#  elif defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__)#    if    defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)#      define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN#    elif !defined(__LITTLE_ENDIAN__) &&  defined(__BIG_ENDIAN__)#      define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN#    elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __LITTLE_ENDIAN__)#      define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN#    elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__)#      define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN#    endif#  endif#endif/*  if the platform is still unknown, try to find its byte order    *//*  from commonly used machine defines                              */#if !defined(PLATFORM_BYTE_ORDER)#if   defined( __alpha__ ) || defined( __alpha ) || defined( i386 )       || \      defined( __i386__ )  || defined( _M_I86 )  || defined( _M_IX86 )    || \      defined( __OS2__ )   || defined( sun386 )  || defined( __TURBOC__ ) || \      defined( vax )       || defined( vms )     || defined( VMS )        || \      defined( __VMS )#  define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN#elif defined( AMIGA )    || defined( applec )  || defined( __AS400__ )  || \      defined( _CRAY )    || defined( __hppa )  || defined( __hp9000 )   || \      defined( ibm370 )   || defined( mc68000 ) || defined( m68k )       || \      defined( __MRC__ )  || defined( __MVS__ ) || defined( __MWERKS__ ) || \      defined( sparc )    || defined( __sparc)  || defined( SYMANTEC_C ) || \      defined( __TANDEM ) || defined( THINK_C ) || defined( __VMCMS__ )#  define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN#elif 0     /* **** EDIT HERE IF NECESSARY **** */#  define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN#elif 0     /* **** EDIT HERE IF NECESSARY **** */#  define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN#else#  error Please edit sha2.c (line 185 or 187) to set the platform byte order#endif#endif#ifdef _MSC_VER#pragma intrinsic(memcpy)#endif#if 0 && defined(_MSC_VER)#define rotl32 _lrotl#define rotr32 _lrotr#else#define rotl32(x,n)   (((x) << n) | ((x) >> (32 - n)))#define rotr32(x,n)   (((x) >> n) | ((x) << (32 - n)))#endif#if !defined(bswap_32)#define bswap_32(x) (rotr32((x), 24) & 0x00ff00ff | rotr32((x), 8) & 0xff00ff00)#endif#if (PLATFORM_BYTE_ORDER == BRG_LITTLE_ENDIAN)#define SWAP_BYTES#else#undef  SWAP_BYTES#endif#if 0#define ch(x,y,z)       (((x) & (y)) ^ (~(x) & (z)))#define maj(x,y,z)      (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))#else   /* Thanks to Rich Schroeppel and Colin Plumb for the following      */#define ch(x,y,z)       ((z) ^ ((x) & ((y) ^ (z))))#define maj(x,y,z)      (((x) & (y)) | ((z) & ((x) ^ (y))))#endif/* round transforms for SHA256 and SHA512 compression functions */#define vf(n,i) v[(n - i) & 7]#define hf(i) (p[i & 15] += \    g_1(p[(i + 14) & 15]) + p[(i + 9) & 15] + g_0(p[(i + 1) & 15]))#define v_cycle(i,j)                                \    vf(7,i) += (j ? hf(i) : p[i]) + k_0[i+j]        \    + s_1(vf(4,i)) + ch(vf(4,i),vf(5,i),vf(6,i));   \    vf(3,i) += vf(7,i);                             \    vf(7,i) += s_0(vf(0,i))+ maj(vf(0,i),vf(1,i),vf(2,i))#if defined(SHA_224) || defined(SHA_256)#define SHA256_MASK (SHA256_BLOCK_SIZE - 1)#if defined(SWAP_BYTES)#define bsw_32(p,n) \    { int _i = (n); while(_i--) ((sha2_32t*)p)[_i] = bswap_32(((sha2_32t*)p)[_i]); }#else#define bsw_32(p,n)#endif#define s_0(x)  (rotr32((x),  2) ^ rotr32((x), 13) ^ rotr32((x), 22))#define s_1(x)  (rotr32((x),  6) ^ rotr32((x), 11) ^ rotr32((x), 25))#define g_0(x)  (rotr32((x),  7) ^ rotr32((x), 18) ^ ((x) >>  3))#define g_1(x)  (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))#define k_0     k256/* rotated SHA256 round definition. Rather than swapping variables as in    *//* FIPS-180, different variables are 'rotated' on each round, returning     *//* to their starting positions every eight rounds                           */#define q(n)  v##n#define one_cycle(a,b,c,d,e,f,g,h,k,w)  \    q(h) += s_1(q(e)) + ch(q(e), q(f), q(g)) + k + w; \    q(d) += q(h); q(h) += s_0(q(a)) + maj(q(a), q(b), q(c))/* SHA256 mixing data   */const sha2_32t k256[64] ={   0x428a2f98ul, 0x71374491ul, 0xb5c0fbcful, 0xe9b5dba5ul,    0x3956c25bul, 0x59f111f1ul, 0x923f82a4ul, 0xab1c5ed5ul,    0xd807aa98ul, 0x12835b01ul, 0x243185beul, 0x550c7dc3ul,    0x72be5d74ul, 0x80deb1feul, 0x9bdc06a7ul, 0xc19bf174ul,    0xe49b69c1ul, 0xefbe4786ul, 0x0fc19dc6ul, 0x240ca1ccul,    0x2de92c6ful, 0x4a7484aaul, 0x5cb0a9dcul, 0x76f988daul,    0x983e5152ul, 0xa831c66dul, 0xb00327c8ul, 0xbf597fc7ul,    0xc6e00bf3ul, 0xd5a79147ul, 0x06ca6351ul, 0x14292967ul,    0x27b70a85ul, 0x2e1b2138ul, 0x4d2c6dfcul, 0x53380d13ul,    0x650a7354ul, 0x766a0abbul, 0x81c2c92eul, 0x92722c85ul,    0xa2bfe8a1ul, 0xa81a664bul, 0xc24b8b70ul, 0xc76c51a3ul,

⌨️ 快捷键说明

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