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

📄 sha2.c

📁 cryptlib是功能强大的安全工具集。允许开发人员快速在自己的软件中集成加密和认证服务。
💻 C
📖 第 1 页 / 共 3 页
字号:
    li_64(983e5152ee66dfab), li_64(a831c66d2db43210),    li_64(b00327c898fb213f), li_64(bf597fc7beef0ee4),    li_64(c6e00bf33da88fc2), li_64(d5a79147930aa725),    li_64(06ca6351e003826f), li_64(142929670a0e6e70),    li_64(27b70a8546d22ffc), li_64(2e1b21385c26c926),    li_64(4d2c6dfc5ac42aed), li_64(53380d139d95b3df),    li_64(650a73548baf63de), li_64(766a0abb3c77b2a8),    li_64(81c2c92e47edaee6), li_64(92722c851482353b),    li_64(a2bfe8a14cf10364), li_64(a81a664bbc423001),    li_64(c24b8b70d0f89791), li_64(c76c51a30654be30),    li_64(d192e819d6ef5218), li_64(d69906245565a910),    li_64(f40e35855771202a), li_64(106aa07032bbd1b8),    li_64(19a4c116b8d2d0c8), li_64(1e376c085141ab53),    li_64(2748774cdf8eeb99), li_64(34b0bcb5e19b48a8),    li_64(391c0cb3c5c95a63), li_64(4ed8aa4ae3418acb),    li_64(5b9cca4f7763e373), li_64(682e6ff3d6b2b8a3),    li_64(748f82ee5defb2fc), li_64(78a5636f43172f60),    li_64(84c87814a1f0ab72), li_64(8cc702081a6439ec),    li_64(90befffa23631e28), li_64(a4506cebde82bde9),    li_64(bef9a3f7b2c67915), li_64(c67178f2e372532b),    li_64(ca273eceea26619c), li_64(d186b8c721c0c207),    li_64(eada7dd6cde0eb1e), li_64(f57d4f7fee6ed178),    li_64(06f067aa72176fba), li_64(0a637dc5a2c898a6),    li_64(113f9804bef90dae), li_64(1b710b35131c471b),    li_64(28db77f523047d84), li_64(32caab7b40c72493),    li_64(3c9ebe0a15c9bebc), li_64(431d67c49c100d4c),    li_64(4cc5d4becb3e42b6), li_64(597f299cfc657e2a),    li_64(5fcb6fab3ad6faec), li_64(6c44198c4a475817)};/* Compile 128 bytes of hash data into SHA384/512 digest    *//* 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 sha512_compile(sha512_ctx ctx[1]){   sha2_64t    v[8], *p = ctx->wbuf;    sha2_32t    j;    memcpy(v, ctx->hash, 8 * sizeof(sha2_64t));    for(j = 0; j < 80; 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];}/* Compile 128 bytes of hash data into SHA256 digest value  *//* NOTE: this routine assumes that the byte order in the    *//* ctx->wbuf[] at this point is in such an order that low   *//* address bytes in the ORIGINAL byte stream placed in this *//* buffer will now go to the high end of words on BOTH big  *//* and little endian systems                                */sha2_void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]){   sha2_32t pos = (sha2_32t)(ctx->count[0] & SHA512_MASK),             space = SHA512_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 = SHA512_BLOCK_SIZE; pos = 0;        bsw_64(ctx->wbuf, SHA512_BLOCK_SIZE >> 3);        sha512_compile(ctx);    }    memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);}/* SHA384/512 Final padding and digest calculation  */static void sha_end2(unsigned char hval[], sha512_ctx ctx[1], const unsigned int hlen){   sha2_32t    i = (sha2_32t)(ctx->count[0] & SHA512_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_64(ctx->wbuf, (i + 7) >> 3);    /* 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 >> 3] &= li_64(ffffffffffffff00) << 8 * (~i & 7);    ctx->wbuf[i >> 3] |= li_64(0000000000000080) << 8 * (~i & 7);    /* we need 17 or more empty byte positions, one for the padding */    /* byte (above) and sixteen for the length count.  If there is  */    /* not enough space pad and empty the buffer                    */    if(i > SHA512_BLOCK_SIZE - 17)    {        if(i < 120) ctx->wbuf[15] = 0;        sha512_compile(ctx);        i = 0;    }    else        i = (i >> 3) + 1;    while(i < 14)        ctx->wbuf[i++] = 0;    /* the following 64-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 64-bit      */    /* word values.                                                 */    ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 61);    ctx->wbuf[15] = ctx->count[0] << 3;    sha512_compile(ctx);    /* extract the hash value as bytes in case the hash buffer is   */    /* misaligned for 32-bit words                                  */    for(i = 0; i < hlen; ++i)        hval[i] = (unsigned char)(ctx->hash[i >> 3] >> (8 * (~i & 7)));}#endif#if defined(SHA_384)/* SHA384 initialisation data   */const sha2_64t  i384[80] ={    li_64(cbbb9d5dc1059ed8), li_64(629a292a367cd507),    li_64(9159015a3070dd17), li_64(152fecd8f70e5939),    li_64(67332667ffc00b31), li_64(8eb44a8768581511),    li_64(db0c2e0d64f98fa7), li_64(47b5481dbefa4fa4)};sha2_void sha384_begin(sha384_ctx ctx[1]){    ctx->count[0] = ctx->count[1] = 0;    memcpy(ctx->hash, i384, 8 * sizeof(sha2_64t));}sha2_void sha384_end(unsigned char hval[], sha384_ctx ctx[1]){    sha_end2(hval, ctx, SHA384_DIGEST_SIZE);}sha2_void sha384(unsigned char hval[], const unsigned char data[], unsigned long len){   sha384_ctx  cx[1];    sha384_begin(cx);    sha384_hash(data, len, cx);    sha_end2(hval, cx, SHA384_DIGEST_SIZE);}#endif#if defined(SHA_512)/* SHA512 initialisation data   */const sha2_64t  i512[80] ={    li_64(6a09e667f3bcc908), li_64(bb67ae8584caa73b),    li_64(3c6ef372fe94f82b), li_64(a54ff53a5f1d36f1),    li_64(510e527fade682d1), li_64(9b05688c2b3e6c1f),    li_64(1f83d9abfb41bd6b), li_64(5be0cd19137e2179)};sha2_void sha512_begin(sha512_ctx ctx[1]){    ctx->count[0] = ctx->count[1] = 0;    memcpy(ctx->hash, i512, 8 * sizeof(sha2_64t));}sha2_void sha512_end(unsigned char hval[], sha512_ctx ctx[1]){    sha_end2(hval, ctx, SHA512_DIGEST_SIZE);}sha2_void sha512(unsigned char hval[], const unsigned char data[], unsigned long len){   sha512_ctx  cx[1];    sha512_begin(cx);    sha512_hash(data, len, cx);    sha_end2(hval, cx, SHA512_DIGEST_SIZE);}#endif#if defined(SHA_2)#define CTX_224(x)  ((x)->uu->ctx256)#define CTX_256(x)  ((x)->uu->ctx256)#define CTX_384(x)  ((x)->uu->ctx512)#define CTX_512(x)  ((x)->uu->ctx512)/* SHA2 initialisation */sha2_int sha2_begin(unsigned long len, sha2_ctx ctx[1]){    switch(len)    {#if defined(SHA_224)        case 224:        case  28:   CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;                    memcpy(CTX_256(ctx)->hash, i224, 32);                    ctx->sha2_len = 28; return SHA2_GOOD;#endif#if defined(SHA_256)        case 256:        case  32:   CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;                    memcpy(CTX_256(ctx)->hash, i256, 32);                    ctx->sha2_len = 32; return SHA2_GOOD;#endif#if defined(SHA_384)        case 384:        case  48:   CTX_384(ctx)->count[0] = CTX_384(ctx)->count[1] = 0;                    memcpy(CTX_384(ctx)->hash, i384, 64);                    ctx->sha2_len = 48; return SHA2_GOOD;#endif#if defined(SHA_512)        case 512:        case  64:   CTX_512(ctx)->count[0] = CTX_512(ctx)->count[1] = 0;                    memcpy(CTX_512(ctx)->hash, i512, 64);                    ctx->sha2_len = 64; return SHA2_GOOD;#endif        default:    return SHA2_BAD;    }}sha2_void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]){    switch(ctx->sha2_len)    {#if defined(SHA_224)        case 28: sha224_hash(data, len, CTX_224(ctx)); return;#endif#if defined(SHA_256)        case 32: sha256_hash(data, len, CTX_256(ctx)); return;#endif#if defined(SHA_384)        case 48: sha384_hash(data, len, CTX_384(ctx)); return;#endif#if defined(SHA_512)        case 64: sha512_hash(data, len, CTX_512(ctx)); return;#endif    }}sha2_void sha2_end(unsigned char hval[], sha2_ctx ctx[1]){    switch(ctx->sha2_len)    {#if defined(SHA_224)        case 28: sha_end1(hval, CTX_224(ctx), SHA224_DIGEST_SIZE); return;#endif#if defined(SHA_256)        case 32: sha_end1(hval, CTX_256(ctx), SHA256_DIGEST_SIZE); return;#endif#if defined(SHA_384)        case 48: sha_end2(hval, CTX_384(ctx), SHA384_DIGEST_SIZE); return;#endif#if defined(SHA_512)        case 64: sha_end2(hval, CTX_512(ctx), SHA512_DIGEST_SIZE); return;#endif    }}sha2_int sha2(unsigned char hval[], unsigned long size,                                const unsigned char data[], unsigned long len){   sha2_ctx    cx[1];    if(sha2_begin(size, cx) == SHA2_GOOD)    {        sha2_hash(data, len, cx); sha2_end(hval, cx); return SHA2_GOOD;    }    else        return SHA2_BAD;}#endif#if defined(__cplusplus)}#endif

⌨️ 快捷键说明

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