📄 sha2.c
字号:
0xd192e819ul, 0xd6990624ul, 0xf40e3585ul, 0x106aa070ul, 0x19a4c116ul, 0x1e376c08ul, 0x2748774cul, 0x34b0bcb5ul, 0x391c0cb3ul, 0x4ed8aa4aul, 0x5b9cca4ful, 0x682e6ff3ul, 0x748f82eeul, 0x78a5636ful, 0x84c87814ul, 0x8cc70208ul, 0x90befffaul, 0xa4506cebul, 0xbef9a3f7ul, 0xc67178f2ul,};/* Compile 64 bytes of hash data into SHA256 digest value *//* NOTE: this routine assumes that the byte order in the *//* ctx->wbuf[] at this point is such that low address bytes *//* in the ORIGINAL byte stream will go into the high end of *//* words on BOTH big and little endian systems */sha2_void sha256_compile(sha256_ctx ctx[1]){#if !defined(UNROLL_SHA2) sha2_32t j, *p = ctx->wbuf, v[8]; memcpy(v, ctx->hash, 8 * sizeof(sha2_32t)); for(j = 0; j < 64; j += 16) { v_cycle( 0, j); v_cycle( 1, j); v_cycle( 2, j); v_cycle( 3, j); v_cycle( 4, j); v_cycle( 5, j); v_cycle( 6, j); v_cycle( 7, j); v_cycle( 8, j); v_cycle( 9, j); v_cycle(10, j); v_cycle(11, j); v_cycle(12, j); v_cycle(13, j); v_cycle(14, j); v_cycle(15, j); } ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ctx->hash[2] += v[2]; ctx->hash[3] += v[3]; ctx->hash[4] += v[4]; ctx->hash[5] += v[5]; ctx->hash[6] += v[6]; ctx->hash[7] += v[7];#else sha2_32t *p = ctx->wbuf,v0,v1,v2,v3,v4,v5,v6,v7; v0 = ctx->hash[0]; v1 = ctx->hash[1]; v2 = ctx->hash[2]; v3 = ctx->hash[3]; v4 = ctx->hash[4]; v5 = ctx->hash[5]; v6 = ctx->hash[6]; v7 = ctx->hash[7]; one_cycle(0,1,2,3,4,5,6,7,k256[ 0],p[ 0]); one_cycle(7,0,1,2,3,4,5,6,k256[ 1],p[ 1]); one_cycle(6,7,0,1,2,3,4,5,k256[ 2],p[ 2]); one_cycle(5,6,7,0,1,2,3,4,k256[ 3],p[ 3]); one_cycle(4,5,6,7,0,1,2,3,k256[ 4],p[ 4]); one_cycle(3,4,5,6,7,0,1,2,k256[ 5],p[ 5]); one_cycle(2,3,4,5,6,7,0,1,k256[ 6],p[ 6]); one_cycle(1,2,3,4,5,6,7,0,k256[ 7],p[ 7]); one_cycle(0,1,2,3,4,5,6,7,k256[ 8],p[ 8]); one_cycle(7,0,1,2,3,4,5,6,k256[ 9],p[ 9]); one_cycle(6,7,0,1,2,3,4,5,k256[10],p[10]); one_cycle(5,6,7,0,1,2,3,4,k256[11],p[11]); one_cycle(4,5,6,7,0,1,2,3,k256[12],p[12]); one_cycle(3,4,5,6,7,0,1,2,k256[13],p[13]); one_cycle(2,3,4,5,6,7,0,1,k256[14],p[14]); one_cycle(1,2,3,4,5,6,7,0,k256[15],p[15]); one_cycle(0,1,2,3,4,5,6,7,k256[16],hf( 0)); one_cycle(7,0,1,2,3,4,5,6,k256[17],hf( 1)); one_cycle(6,7,0,1,2,3,4,5,k256[18],hf( 2)); one_cycle(5,6,7,0,1,2,3,4,k256[19],hf( 3)); one_cycle(4,5,6,7,0,1,2,3,k256[20],hf( 4)); one_cycle(3,4,5,6,7,0,1,2,k256[21],hf( 5)); one_cycle(2,3,4,5,6,7,0,1,k256[22],hf( 6)); one_cycle(1,2,3,4,5,6,7,0,k256[23],hf( 7)); one_cycle(0,1,2,3,4,5,6,7,k256[24],hf( 8)); one_cycle(7,0,1,2,3,4,5,6,k256[25],hf( 9)); one_cycle(6,7,0,1,2,3,4,5,k256[26],hf(10)); one_cycle(5,6,7,0,1,2,3,4,k256[27],hf(11)); one_cycle(4,5,6,7,0,1,2,3,k256[28],hf(12)); one_cycle(3,4,5,6,7,0,1,2,k256[29],hf(13)); one_cycle(2,3,4,5,6,7,0,1,k256[30],hf(14)); one_cycle(1,2,3,4,5,6,7,0,k256[31],hf(15)); one_cycle(0,1,2,3,4,5,6,7,k256[32],hf( 0)); one_cycle(7,0,1,2,3,4,5,6,k256[33],hf( 1)); one_cycle(6,7,0,1,2,3,4,5,k256[34],hf( 2)); one_cycle(5,6,7,0,1,2,3,4,k256[35],hf( 3)); one_cycle(4,5,6,7,0,1,2,3,k256[36],hf( 4)); one_cycle(3,4,5,6,7,0,1,2,k256[37],hf( 5)); one_cycle(2,3,4,5,6,7,0,1,k256[38],hf( 6)); one_cycle(1,2,3,4,5,6,7,0,k256[39],hf( 7)); one_cycle(0,1,2,3,4,5,6,7,k256[40],hf( 8)); one_cycle(7,0,1,2,3,4,5,6,k256[41],hf( 9)); one_cycle(6,7,0,1,2,3,4,5,k256[42],hf(10)); one_cycle(5,6,7,0,1,2,3,4,k256[43],hf(11)); one_cycle(4,5,6,7,0,1,2,3,k256[44],hf(12)); one_cycle(3,4,5,6,7,0,1,2,k256[45],hf(13)); one_cycle(2,3,4,5,6,7,0,1,k256[46],hf(14)); one_cycle(1,2,3,4,5,6,7,0,k256[47],hf(15)); one_cycle(0,1,2,3,4,5,6,7,k256[48],hf( 0)); one_cycle(7,0,1,2,3,4,5,6,k256[49],hf( 1)); one_cycle(6,7,0,1,2,3,4,5,k256[50],hf( 2)); one_cycle(5,6,7,0,1,2,3,4,k256[51],hf( 3)); one_cycle(4,5,6,7,0,1,2,3,k256[52],hf( 4)); one_cycle(3,4,5,6,7,0,1,2,k256[53],hf( 5)); one_cycle(2,3,4,5,6,7,0,1,k256[54],hf( 6)); one_cycle(1,2,3,4,5,6,7,0,k256[55],hf( 7)); one_cycle(0,1,2,3,4,5,6,7,k256[56],hf( 8)); one_cycle(7,0,1,2,3,4,5,6,k256[57],hf( 9)); one_cycle(6,7,0,1,2,3,4,5,k256[58],hf(10)); one_cycle(5,6,7,0,1,2,3,4,k256[59],hf(11)); one_cycle(4,5,6,7,0,1,2,3,k256[60],hf(12)); one_cycle(3,4,5,6,7,0,1,2,k256[61],hf(13)); one_cycle(2,3,4,5,6,7,0,1,k256[62],hf(14)); one_cycle(1,2,3,4,5,6,7,0,k256[63],hf(15)); ctx->hash[0] += v0; ctx->hash[1] += v1; ctx->hash[2] += v2; ctx->hash[3] += v3; ctx->hash[4] += v4; ctx->hash[5] += v5; ctx->hash[6] += v6; ctx->hash[7] += v7;#endif}/* SHA256 hash data in an array of bytes into hash buffer *//* and call the hash_compile function as required. */sha2_void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]){ sha2_32t pos = (sha2_32t)(ctx->count[0] & SHA256_MASK), space = SHA256_BLOCK_SIZE - pos; const unsigned char *sp = data; if((ctx->count[0] += len) < len) ++(ctx->count[1]); while(len >= space) /* tranfer whole blocks while possible */ { memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space); sp += space; len -= space; space = SHA256_BLOCK_SIZE; pos = 0; bsw_32(ctx->wbuf, SHA256_BLOCK_SIZE >> 2) sha256_compile(ctx); } memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);}/* SHA256 Final padding and digest calculation */static sha2_void sha_end1(unsigned char hval[], sha256_ctx ctx[1], const unsigned int hlen){ sha2_32t i = (sha2_32t)(ctx->count[0] & SHA256_MASK); /* put bytes in the buffer in an order in which references to */ /* 32-bit words will put bytes with lower addresses into the */ /* top of 32 bit words on BOTH big and little endian machines */ bsw_32(ctx->wbuf, (i + 3) >> 2) /* we now need to mask valid bytes and add the padding which is */ /* a single 1 bit and as many zero bits as necessary. Note that */ /* we can always add the first padding byte here because the */ /* buffer always has at least one empty slot */ ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3); ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3); /* we need 9 or more empty positions, one for the padding byte */ /* (above) and eight for the length count. If there is not */ /* enough space pad and empty the buffer */ if(i > SHA256_BLOCK_SIZE - 9) { if(i < 60) ctx->wbuf[15] = 0; sha256_compile(ctx); i = 0; } else /* compute a word index for the empty buffer positions */ i = (i >> 2) + 1; while(i < 14) /* and zero pad all but last two positions */ ctx->wbuf[i++] = 0; /* the following 32-bit length fields are assembled in the */ /* wrong byte order on little endian machines but this is */ /* corrected later since they are only ever used as 32-bit */ /* word values. */ ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29); ctx->wbuf[15] = ctx->count[0] << 3; sha256_compile(ctx); /* extract the hash value as bytes in case the hash buffer is */ /* mislaigned for 32-bit words */ for(i = 0; i < hlen; ++i) hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));}#endif#if defined(SHA_224)const sha2_32t i224[8] ={ 0xc1059ed8ul, 0x367cd507ul, 0x3070dd17ul, 0xf70e5939ul, 0xffc00b31ul, 0x68581511ul, 0x64f98fa7ul, 0xbefa4fa4ul};sha2_void sha224_begin(sha224_ctx ctx[1]){ ctx->count[0] = ctx->count[1] = 0; memcpy(ctx->hash, i224, 8 * sizeof(sha2_32t));}sha2_void sha224_end(unsigned char hval[], sha224_ctx ctx[1]){ sha_end1(hval, ctx, SHA224_DIGEST_SIZE);}sha2_void sha224(unsigned char hval[], const unsigned char data[], unsigned long len){ sha224_ctx cx[1]; sha224_begin(cx); sha224_hash(data, len, cx); sha_end1(hval, cx, SHA224_DIGEST_SIZE);}#endif#if defined(SHA_256)const sha2_32t i256[8] ={ 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul};sha2_void sha256_begin(sha256_ctx ctx[1]){ ctx->count[0] = ctx->count[1] = 0; memcpy(ctx->hash, i256, 8 * sizeof(sha2_32t));}sha2_void sha256_end(unsigned char hval[], sha256_ctx ctx[1]){ sha_end1(hval, ctx, SHA256_DIGEST_SIZE);}sha2_void sha256(unsigned char hval[], const unsigned char data[], unsigned long len){ sha256_ctx cx[1]; sha256_begin(cx); sha256_hash(data, len, cx); sha_end1(hval, cx, SHA256_DIGEST_SIZE);}#endif#if defined(SHA_384) || defined(SHA_512)#define SHA512_MASK (SHA512_BLOCK_SIZE - 1)#define rotr64(x,n) (((x) >> n) | ((x) << (64 - n)))#if !defined(bswap_64)#define bswap_64(x) (((sha2_64t)(bswap_32((sha2_32t)(x)))) << 32 | bswap_32((sha2_32t)((x) >> 32)))#endif#if defined(SWAP_BYTES)#define bsw_64(p,n) \ { int _i = (n); while(_i--) ((sha2_64t*)p)[_i] = bswap_64(((sha2_64t*)p)[_i]); }#else#define bsw_64(p,n)#endif/* SHA512 mixing function definitions */#ifdef s_0# undef s_0# undef s_1# undef g_0# undef g_1# undef k_0#endif#define s_0(x) (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))#define s_1(x) (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))#define g_0(x) (rotr64((x), 1) ^ rotr64((x), 8) ^ ((x) >> 7))#define g_1(x) (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >> 6))#define k_0 k512/* SHA384/SHA512 mixing data */const sha2_64t k512[80] ={ li_64(428a2f98d728ae22), li_64(7137449123ef65cd), li_64(b5c0fbcfec4d3b2f), li_64(e9b5dba58189dbbc), li_64(3956c25bf348b538), li_64(59f111f1b605d019), li_64(923f82a4af194f9b), li_64(ab1c5ed5da6d8118), li_64(d807aa98a3030242), li_64(12835b0145706fbe), li_64(243185be4ee4b28c), li_64(550c7dc3d5ffb4e2), li_64(72be5d74f27b896f), li_64(80deb1fe3b1696b1), li_64(9bdc06a725c71235), li_64(c19bf174cf692694), li_64(e49b69c19ef14ad2), li_64(efbe4786384f25e3), li_64(0fc19dc68b8cd5b5), li_64(240ca1cc77ac9c65), li_64(2de92c6f592b0275), li_64(4a7484aa6ea6e483), li_64(5cb0a9dcbd41fbd4), li_64(76f988da831153b5),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -