Annotation of truecrypt/crypto/sha2.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  ---------------------------------------------------------------------------
                      3:  Copyright (c) 2002, Dr Brian Gladman, Worcester, UK.   All rights reserved.
                      4: 
                      5:  LICENSE TERMS
                      6: 
                      7:  The free distribution and use of this software in both source and binary
                      8:  form is allowed (with or without changes) provided that:
                      9: 
                     10:    1. distributions of this source code include the above copyright
                     11:       notice, this list of conditions and the following disclaimer;
                     12: 
                     13:    2. distributions in binary form include the above copyright
                     14:       notice, this list of conditions and the following disclaimer
                     15:       in the documentation and/or other associated materials;
                     16: 
                     17:    3. the copyright holder's name is not used to endorse products
                     18:       built using this software without specific written permission.
                     19: 
                     20:  ALTERNATIVELY, provided that this notice is retained in full, this product
                     21:  may be distributed under the terms of the GNU General Public License (GPL),
                     22:  in which case the provisions of the GPL apply INSTEAD OF those given above.
                     23: 
                     24:  DISCLAIMER
                     25: 
                     26:  This software is provided 'as is' with no explicit or implied warranties
                     27:  in respect of its properties, including, but not limited to, correctness
                     28:  and/or fitness for purpose.
                     29:  ---------------------------------------------------------------------------
                     30:  Issue Date: 01/08/2005
                     31: 
                     32:  This is a byte oriented version of SHA2 that operates on arrays of bytes
                     33:  stored in memory. This code implements sha256, sha384 and sha512 but the
                     34:  latter two functions rely on efficient 64-bit integer operations that
                     35:  may not be very efficient on 32-bit machines
                     36: 
                     37:  The sha256 functions use a type 'sha256_ctx' to hold details of the
                     38:  current hash state and uses the following three calls:
                     39: 
                     40:        void sha256_begin(sha256_ctx ctx[1])
                     41:        void sha256_hash(const unsigned char data[],
                     42:                             unsigned long len, sha256_ctx ctx[1])
                     43:        void sha_end1(unsigned char hval[], sha256_ctx ctx[1])
                     44: 
                     45:  The first subroutine initialises a hash computation by setting up the
                     46:  context in the sha256_ctx context. The second subroutine hashes 8-bit
                     47:  bytes from array data[] into the hash state withinh sha256_ctx context,
                     48:  the number of bytes to be hashed being given by the the unsigned long
                     49:  integer len.  The third subroutine completes the hash calculation and
                     50:  places the resulting digest value in the array of 8-bit bytes hval[].
                     51: 
                     52:  The sha384 and sha512 functions are similar and use the interfaces:
                     53: 
                     54:        void sha384_begin(sha384_ctx ctx[1]);
                     55:        void sha384_hash(const unsigned char data[],
                     56:                             unsigned long len, sha384_ctx ctx[1]);
                     57:        void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
                     58: 
                     59:        void sha512_begin(sha512_ctx ctx[1]);
                     60:        void sha512_hash(const unsigned char data[],
                     61:                             unsigned long len, sha512_ctx ctx[1]);
                     62:        void sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
                     63: 
                     64:  In addition there is a function sha2 that can be used to call all these
                     65:  functions using a call with a hash length parameter as follows:
                     66: 
                     67:        int sha2_begin(unsigned long len, sha2_ctx ctx[1]);
                     68:        void sha2_hash(const unsigned char data[],
                     69:                             unsigned long len, sha2_ctx ctx[1]);
                     70:        void sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
                     71: 
                     72:  My thanks to Erik Andersen <[email protected]> for testing this code
                     73:  on big-endian systems and for his assistance with corrections
                     74: */
                     75: 
                     76: #include "Common/Endian.h"
                     77: #define PLATFORM_BYTE_ORDER BYTE_ORDER
                     78: #define IS_LITTLE_ENDIAN LITTLE_ENDIAN
                     79: 
                     80: #if 0
                     81: #define UNROLL_SHA2     /* for SHA2 loop unroll     */
                     82: #endif
                     83: 
                     84: #include <string.h>     /* for memcpy() etc.        */
                     85: 
                     86: #include "Sha2.h"
                     87: 
                     88: #if defined(__cplusplus)
                     89: extern "C"
                     90: {
                     91: #endif
                     92: 
                     93: #if defined( _MSC_VER ) && ( _MSC_VER > 800 )
                     94: #pragma intrinsic(memcpy)
                     95: #endif
                     96: 
                     97: #if 0 && defined(_MSC_VER)
                     98: #define rotl32 _lrotl
                     99: #define rotr32 _lrotr
                    100: #else
                    101: #define rotl32(x,n)   (((x) << n) | ((x) >> (32 - n)))
                    102: #define rotr32(x,n)   (((x) >> n) | ((x) << (32 - n)))
                    103: #endif
                    104: 
                    105: #if !defined(bswap_32)
                    106: #define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
                    107: #endif
                    108: 
                    109: #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
                    110: #define SWAP_BYTES
                    111: #else
                    112: #undef  SWAP_BYTES
                    113: #endif
                    114: 
                    115: #if 0
                    116: 
                    117: #define ch(x,y,z)       (((x) & (y)) ^ (~(x) & (z)))
                    118: #define maj(x,y,z)      (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
                    119: 
                    120: #else   /* Thanks to Rich Schroeppel and Colin Plumb for the following      */
                    121: 
                    122: #define ch(x,y,z)       ((z) ^ ((x) & ((y) ^ (z))))
                    123: #define maj(x,y,z)      (((x) & (y)) | ((z) & ((x) ^ (y))))
                    124: 
                    125: #endif
                    126: 
                    127: /* round transforms for SHA256 and SHA512 compression functions */
                    128: 
                    129: #define vf(n,i) v[(n - i) & 7]
                    130: 
                    131: #define hf(i) (p[i & 15] += \
                    132:     g_1(p[(i + 14) & 15]) + p[(i + 9) & 15] + g_0(p[(i + 1) & 15]))
                    133: 
                    134: #define v_cycle(i,j)                                \
                    135:     vf(7,i) += (j ? hf(i) : p[i]) + k_0[i+j]        \
                    136:     + s_1(vf(4,i)) + ch(vf(4,i),vf(5,i),vf(6,i));   \
                    137:     vf(3,i) += vf(7,i);                             \
                    138:     vf(7,i) += s_0(vf(0,i))+ maj(vf(0,i),vf(1,i),vf(2,i))
                    139: 
                    140: #if defined(SHA_224) || defined(SHA_256)
                    141: 
                    142: #define SHA256_MASK (SHA256_BLOCK_SIZE - 1)
                    143: 
                    144: #if defined(SWAP_BYTES)
                    145: #define bsw_32(p,n) \
                    146:     { int _i = (n); while(_i--) ((uint_32t*)p)[_i] = bswap_32(((uint_32t*)p)[_i]); }
                    147: #else
                    148: #define bsw_32(p,n)
                    149: #endif
                    150: 
                    151: #define s_0(x)  (rotr32((x),  2) ^ rotr32((x), 13) ^ rotr32((x), 22))
                    152: #define s_1(x)  (rotr32((x),  6) ^ rotr32((x), 11) ^ rotr32((x), 25))
                    153: #define g_0(x)  (rotr32((x),  7) ^ rotr32((x), 18) ^ ((x) >>  3))
                    154: #define g_1(x)  (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
                    155: #define k_0     k256
                    156: 
                    157: /* rotated SHA256 round definition. Rather than swapping variables as in    */
                    158: /* FIPS-180, different variables are 'rotated' on each round, returning     */
                    159: /* to their starting positions every eight rounds                           */
                    160: 
                    161: #define q(n)  v##n
                    162: 
                    163: #define one_cycle(a,b,c,d,e,f,g,h,k,w)  \
                    164:     q(h) += s_1(q(e)) + ch(q(e), q(f), q(g)) + k + w; \
                    165:     q(d) += q(h); q(h) += s_0(q(a)) + maj(q(a), q(b), q(c))
                    166: 
                    167: /* SHA256 mixing data   */
                    168: 
                    169: const uint_32t k256[64] =
                    170: {   0x428a2f98ul, 0x71374491ul, 0xb5c0fbcful, 0xe9b5dba5ul,
                    171:     0x3956c25bul, 0x59f111f1ul, 0x923f82a4ul, 0xab1c5ed5ul,
                    172:     0xd807aa98ul, 0x12835b01ul, 0x243185beul, 0x550c7dc3ul,
                    173:     0x72be5d74ul, 0x80deb1feul, 0x9bdc06a7ul, 0xc19bf174ul,
                    174:     0xe49b69c1ul, 0xefbe4786ul, 0x0fc19dc6ul, 0x240ca1ccul,
                    175:     0x2de92c6ful, 0x4a7484aaul, 0x5cb0a9dcul, 0x76f988daul,
                    176:     0x983e5152ul, 0xa831c66dul, 0xb00327c8ul, 0xbf597fc7ul,
                    177:     0xc6e00bf3ul, 0xd5a79147ul, 0x06ca6351ul, 0x14292967ul,
                    178:     0x27b70a85ul, 0x2e1b2138ul, 0x4d2c6dfcul, 0x53380d13ul,
                    179:     0x650a7354ul, 0x766a0abbul, 0x81c2c92eul, 0x92722c85ul,
                    180:     0xa2bfe8a1ul, 0xa81a664bul, 0xc24b8b70ul, 0xc76c51a3ul,
                    181:     0xd192e819ul, 0xd6990624ul, 0xf40e3585ul, 0x106aa070ul,
                    182:     0x19a4c116ul, 0x1e376c08ul, 0x2748774cul, 0x34b0bcb5ul,
                    183:     0x391c0cb3ul, 0x4ed8aa4aul, 0x5b9cca4ful, 0x682e6ff3ul,
                    184:     0x748f82eeul, 0x78a5636ful, 0x84c87814ul, 0x8cc70208ul,
                    185:     0x90befffaul, 0xa4506cebul, 0xbef9a3f7ul, 0xc67178f2ul,
                    186: };
                    187: 
                    188: /* Compile 64 bytes of hash data into SHA256 digest value   */
                    189: /* NOTE: this routine assumes that the byte order in the    */
                    190: /* ctx->wbuf[] at this point is such that low address bytes */
                    191: /* in the ORIGINAL byte stream will go into the high end of */
                    192: /* words on BOTH big and little endian systems              */
                    193: 
                    194: VOID_RETURN sha256_compile(sha256_ctx ctx[1])
                    195: {
                    196: #if !defined(UNROLL_SHA2)
                    197: 
                    198:     uint_32t j, *p = ctx->wbuf, v[8];
                    199: 
                    200:     memcpy(v, ctx->hash, 8 * sizeof(uint_32t));
                    201: 
                    202:     for(j = 0; j < 64; j += 16)
                    203:     {
                    204:         v_cycle( 0, j); v_cycle( 1, j);
                    205:         v_cycle( 2, j); v_cycle( 3, j);
                    206:         v_cycle( 4, j); v_cycle( 5, j);
                    207:         v_cycle( 6, j); v_cycle( 7, j);
                    208:         v_cycle( 8, j); v_cycle( 9, j);
                    209:         v_cycle(10, j); v_cycle(11, j);
                    210:         v_cycle(12, j); v_cycle(13, j);
                    211:         v_cycle(14, j); v_cycle(15, j);
                    212:     }
                    213: 
                    214:     ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
                    215:     ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
                    216:     ctx->hash[4] += v[4]; ctx->hash[5] += v[5];
                    217:     ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
                    218: 
                    219: #else
                    220: 
                    221:     uint_32t *p = ctx->wbuf,v0,v1,v2,v3,v4,v5,v6,v7;
                    222: 
                    223:     v0 = ctx->hash[0]; v1 = ctx->hash[1];
                    224:     v2 = ctx->hash[2]; v3 = ctx->hash[3];
                    225:     v4 = ctx->hash[4]; v5 = ctx->hash[5];
                    226:     v6 = ctx->hash[6]; v7 = ctx->hash[7];
                    227: 
                    228:     one_cycle(0,1,2,3,4,5,6,7,k256[ 0],p[ 0]);
                    229:     one_cycle(7,0,1,2,3,4,5,6,k256[ 1],p[ 1]);
                    230:     one_cycle(6,7,0,1,2,3,4,5,k256[ 2],p[ 2]);
                    231:     one_cycle(5,6,7,0,1,2,3,4,k256[ 3],p[ 3]);
                    232:     one_cycle(4,5,6,7,0,1,2,3,k256[ 4],p[ 4]);
                    233:     one_cycle(3,4,5,6,7,0,1,2,k256[ 5],p[ 5]);
                    234:     one_cycle(2,3,4,5,6,7,0,1,k256[ 6],p[ 6]);
                    235:     one_cycle(1,2,3,4,5,6,7,0,k256[ 7],p[ 7]);
                    236:     one_cycle(0,1,2,3,4,5,6,7,k256[ 8],p[ 8]);
                    237:     one_cycle(7,0,1,2,3,4,5,6,k256[ 9],p[ 9]);
                    238:     one_cycle(6,7,0,1,2,3,4,5,k256[10],p[10]);
                    239:     one_cycle(5,6,7,0,1,2,3,4,k256[11],p[11]);
                    240:     one_cycle(4,5,6,7,0,1,2,3,k256[12],p[12]);
                    241:     one_cycle(3,4,5,6,7,0,1,2,k256[13],p[13]);
                    242:     one_cycle(2,3,4,5,6,7,0,1,k256[14],p[14]);
                    243:     one_cycle(1,2,3,4,5,6,7,0,k256[15],p[15]);
                    244: 
                    245:     one_cycle(0,1,2,3,4,5,6,7,k256[16],hf( 0));
                    246:     one_cycle(7,0,1,2,3,4,5,6,k256[17],hf( 1));
                    247:     one_cycle(6,7,0,1,2,3,4,5,k256[18],hf( 2));
                    248:     one_cycle(5,6,7,0,1,2,3,4,k256[19],hf( 3));
                    249:     one_cycle(4,5,6,7,0,1,2,3,k256[20],hf( 4));
                    250:     one_cycle(3,4,5,6,7,0,1,2,k256[21],hf( 5));
                    251:     one_cycle(2,3,4,5,6,7,0,1,k256[22],hf( 6));
                    252:     one_cycle(1,2,3,4,5,6,7,0,k256[23],hf( 7));
                    253:     one_cycle(0,1,2,3,4,5,6,7,k256[24],hf( 8));
                    254:     one_cycle(7,0,1,2,3,4,5,6,k256[25],hf( 9));
                    255:     one_cycle(6,7,0,1,2,3,4,5,k256[26],hf(10));
                    256:     one_cycle(5,6,7,0,1,2,3,4,k256[27],hf(11));
                    257:     one_cycle(4,5,6,7,0,1,2,3,k256[28],hf(12));
                    258:     one_cycle(3,4,5,6,7,0,1,2,k256[29],hf(13));
                    259:     one_cycle(2,3,4,5,6,7,0,1,k256[30],hf(14));
                    260:     one_cycle(1,2,3,4,5,6,7,0,k256[31],hf(15));
                    261: 
                    262:     one_cycle(0,1,2,3,4,5,6,7,k256[32],hf( 0));
                    263:     one_cycle(7,0,1,2,3,4,5,6,k256[33],hf( 1));
                    264:     one_cycle(6,7,0,1,2,3,4,5,k256[34],hf( 2));
                    265:     one_cycle(5,6,7,0,1,2,3,4,k256[35],hf( 3));
                    266:     one_cycle(4,5,6,7,0,1,2,3,k256[36],hf( 4));
                    267:     one_cycle(3,4,5,6,7,0,1,2,k256[37],hf( 5));
                    268:     one_cycle(2,3,4,5,6,7,0,1,k256[38],hf( 6));
                    269:     one_cycle(1,2,3,4,5,6,7,0,k256[39],hf( 7));
                    270:     one_cycle(0,1,2,3,4,5,6,7,k256[40],hf( 8));
                    271:     one_cycle(7,0,1,2,3,4,5,6,k256[41],hf( 9));
                    272:     one_cycle(6,7,0,1,2,3,4,5,k256[42],hf(10));
                    273:     one_cycle(5,6,7,0,1,2,3,4,k256[43],hf(11));
                    274:     one_cycle(4,5,6,7,0,1,2,3,k256[44],hf(12));
                    275:     one_cycle(3,4,5,6,7,0,1,2,k256[45],hf(13));
                    276:     one_cycle(2,3,4,5,6,7,0,1,k256[46],hf(14));
                    277:     one_cycle(1,2,3,4,5,6,7,0,k256[47],hf(15));
                    278: 
                    279:     one_cycle(0,1,2,3,4,5,6,7,k256[48],hf( 0));
                    280:     one_cycle(7,0,1,2,3,4,5,6,k256[49],hf( 1));
                    281:     one_cycle(6,7,0,1,2,3,4,5,k256[50],hf( 2));
                    282:     one_cycle(5,6,7,0,1,2,3,4,k256[51],hf( 3));
                    283:     one_cycle(4,5,6,7,0,1,2,3,k256[52],hf( 4));
                    284:     one_cycle(3,4,5,6,7,0,1,2,k256[53],hf( 5));
                    285:     one_cycle(2,3,4,5,6,7,0,1,k256[54],hf( 6));
                    286:     one_cycle(1,2,3,4,5,6,7,0,k256[55],hf( 7));
                    287:     one_cycle(0,1,2,3,4,5,6,7,k256[56],hf( 8));
                    288:     one_cycle(7,0,1,2,3,4,5,6,k256[57],hf( 9));
                    289:     one_cycle(6,7,0,1,2,3,4,5,k256[58],hf(10));
                    290:     one_cycle(5,6,7,0,1,2,3,4,k256[59],hf(11));
                    291:     one_cycle(4,5,6,7,0,1,2,3,k256[60],hf(12));
                    292:     one_cycle(3,4,5,6,7,0,1,2,k256[61],hf(13));
                    293:     one_cycle(2,3,4,5,6,7,0,1,k256[62],hf(14));
                    294:     one_cycle(1,2,3,4,5,6,7,0,k256[63],hf(15));
                    295: 
                    296:     ctx->hash[0] += v0; ctx->hash[1] += v1;
                    297:     ctx->hash[2] += v2; ctx->hash[3] += v3;
                    298:     ctx->hash[4] += v4; ctx->hash[5] += v5;
                    299:     ctx->hash[6] += v6; ctx->hash[7] += v7;
                    300: #endif
                    301: }
                    302: 
                    303: /* SHA256 hash data in an array of bytes into hash buffer   */
                    304: /* and call the hash_compile function as required.          */
                    305: 
                    306: VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1])
                    307: {   uint_32t pos = (uint_32t)(ctx->count[0] & SHA256_MASK),
                    308:              space = SHA256_BLOCK_SIZE - pos;
                    309:     const unsigned char *sp = data;
                    310: 
                    311:     if((ctx->count[0] += len) < len)
                    312:         ++(ctx->count[1]);
                    313: 
                    314:     while(len >= space)     /* tranfer whole blocks while possible  */
                    315:     {
                    316:         memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
                    317:         sp += space; len -= space; space = SHA256_BLOCK_SIZE; pos = 0;
                    318:         bsw_32(ctx->wbuf, SHA256_BLOCK_SIZE >> 2)
                    319:         sha256_compile(ctx);
                    320:     }
                    321: 
                    322:     memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
                    323: }
                    324: 
                    325: /* SHA256 Final padding and digest calculation  */
                    326: 
                    327: static void sha_end1(unsigned char hval[], sha256_ctx ctx[1], const unsigned int hlen)
                    328: {   uint_32t    i = (uint_32t)(ctx->count[0] & SHA256_MASK);
                    329: 
                    330:     /* put bytes in the buffer in an order in which references to   */
                    331:     /* 32-bit words will put bytes with lower addresses into the    */
                    332:     /* top of 32 bit words on BOTH big and little endian machines   */
                    333:     bsw_32(ctx->wbuf, (i + 3) >> 2)
                    334: 
                    335:     /* we now need to mask valid bytes and add the padding which is */
                    336:     /* a single 1 bit and as many zero bits as necessary. Note that */
                    337:     /* we can always add the first padding byte here because the    */
                    338:     /* buffer always has at least one empty slot                    */
                    339:     ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3);
                    340:     ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3);
                    341: 
                    342:     /* we need 9 or more empty positions, one for the padding byte  */
                    343:     /* (above) and eight for the length count.  If there is not     */
                    344:     /* enough space pad and empty the buffer                        */
                    345:     if(i > SHA256_BLOCK_SIZE - 9)
                    346:     {
                    347:         if(i < 60) ctx->wbuf[15] = 0;
                    348:         sha256_compile(ctx);
                    349:         i = 0;
                    350:     }
                    351:     else    /* compute a word index for the empty buffer positions  */
                    352:         i = (i >> 2) + 1;
                    353: 
                    354:     while(i < 14) /* and zero pad all but last two positions        */
                    355:         ctx->wbuf[i++] = 0;
                    356: 
                    357:     /* the following 32-bit length fields are assembled in the      */
                    358:     /* wrong byte order on little endian machines but this is       */
                    359:     /* corrected later since they are only ever used as 32-bit      */
                    360:     /* word values.                                                 */
                    361:     ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
                    362:     ctx->wbuf[15] = ctx->count[0] << 3;
                    363:     sha256_compile(ctx);
                    364: 
                    365:     /* extract the hash value as bytes in case the hash buffer is   */
                    366:     /* mislaigned for 32-bit words                                  */
                    367:     for(i = 0; i < hlen; ++i)
                    368:         hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
                    369: }
                    370: 
                    371: #endif
                    372: 
                    373: #if defined(SHA_224)
                    374: 
                    375: const uint_32t i224[8] =
                    376: {
                    377:     0xc1059ed8ul, 0x367cd507ul, 0x3070dd17ul, 0xf70e5939ul,
                    378:     0xffc00b31ul, 0x68581511ul, 0x64f98fa7ul, 0xbefa4fa4ul
                    379: };
                    380: 
                    381: VOID_RETURN sha224_begin(sha224_ctx ctx[1])
                    382: {
                    383:     ctx->count[0] = ctx->count[1] = 0;
                    384:     memcpy(ctx->hash, i224, 8 * sizeof(uint_32t));
                    385: }
                    386: 
                    387: VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1])
                    388: {
                    389:     sha_end1(hval, ctx, SHA224_DIGEST_SIZE);
                    390: }
                    391: 
                    392: VOID_RETURN sha224(unsigned char hval[], const unsigned char data[], unsigned long len)
                    393: {   sha224_ctx  cx[1];
                    394: 
                    395:     sha224_begin(cx);
                    396:     sha224_hash(data, len, cx);
                    397:     sha_end1(hval, cx, SHA224_DIGEST_SIZE);
                    398: }
                    399: 
                    400: #endif
                    401: 
                    402: #if defined(SHA_256)
                    403: 
                    404: const uint_32t i256[8] =
                    405: {
                    406:     0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul,
                    407:     0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul
                    408: };
                    409: 
                    410: VOID_RETURN sha256_begin(sha256_ctx ctx[1])
                    411: {
                    412:     ctx->count[0] = ctx->count[1] = 0;
                    413:     memcpy(ctx->hash, i256, 8 * sizeof(uint_32t));
                    414: }
                    415: 
                    416: VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1])
                    417: {
                    418:     sha_end1(hval, ctx, SHA256_DIGEST_SIZE);
                    419: }
                    420: 
                    421: VOID_RETURN sha256(unsigned char hval[], const unsigned char data[], unsigned long len)
                    422: {   sha256_ctx  cx[1];
                    423: 
                    424:     sha256_begin(cx);
                    425:     sha256_hash(data, len, cx);
                    426:     sha_end1(hval, cx, SHA256_DIGEST_SIZE);
                    427: }
                    428: 
                    429: #endif
                    430: 
                    431: #if defined(SHA_384) || defined(SHA_512)
                    432: 
                    433: #define SHA512_MASK (SHA512_BLOCK_SIZE - 1)
                    434: 
                    435: #define rotr64(x,n)   (((x) >> n) | ((x) << (64 - n)))
                    436: 
                    437: #if !defined(bswap_64)
                    438: #define bswap_64(x) (((uint_64t)(bswap_32((uint_32t)(x)))) << 32 | bswap_32((uint_32t)((x) >> 32)))
                    439: #endif
                    440: 
                    441: #if defined(SWAP_BYTES)
                    442: #define bsw_64(p,n) \
                    443:     { int _i = (n); while(_i--) ((uint_64t*)p)[_i] = bswap_64(((uint_64t*)p)[_i]); }
                    444: #else
                    445: #define bsw_64(p,n)
                    446: #endif
                    447: 
                    448: /* SHA512 mixing function definitions   */
                    449: 
                    450: #ifdef   s_0
                    451: # undef  s_0
                    452: # undef  s_1
                    453: # undef  g_0
                    454: # undef  g_1
                    455: # undef  k_0
                    456: #endif
                    457: 
                    458: #define s_0(x)  (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))
                    459: #define s_1(x)  (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))
                    460: #define g_0(x)  (rotr64((x),  1) ^ rotr64((x),  8) ^ ((x) >>  7))
                    461: #define g_1(x)  (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >>  6))
                    462: #define k_0     k512
                    463: 
                    464: /* SHA384/SHA512 mixing data    */
                    465: 
                    466: const uint_64t  k512[80] =
                    467: {
                    468:     li_64(428a2f98d728ae22), li_64(7137449123ef65cd),
                    469:     li_64(b5c0fbcfec4d3b2f), li_64(e9b5dba58189dbbc),
                    470:     li_64(3956c25bf348b538), li_64(59f111f1b605d019),
                    471:     li_64(923f82a4af194f9b), li_64(ab1c5ed5da6d8118),
                    472:     li_64(d807aa98a3030242), li_64(12835b0145706fbe),
                    473:     li_64(243185be4ee4b28c), li_64(550c7dc3d5ffb4e2),
                    474:     li_64(72be5d74f27b896f), li_64(80deb1fe3b1696b1),
                    475:     li_64(9bdc06a725c71235), li_64(c19bf174cf692694),
                    476:     li_64(e49b69c19ef14ad2), li_64(efbe4786384f25e3),
                    477:     li_64(0fc19dc68b8cd5b5), li_64(240ca1cc77ac9c65),
                    478:     li_64(2de92c6f592b0275), li_64(4a7484aa6ea6e483),
                    479:     li_64(5cb0a9dcbd41fbd4), li_64(76f988da831153b5),
                    480:     li_64(983e5152ee66dfab), li_64(a831c66d2db43210),
                    481:     li_64(b00327c898fb213f), li_64(bf597fc7beef0ee4),
                    482:     li_64(c6e00bf33da88fc2), li_64(d5a79147930aa725),
                    483:     li_64(06ca6351e003826f), li_64(142929670a0e6e70),
                    484:     li_64(27b70a8546d22ffc), li_64(2e1b21385c26c926),
                    485:     li_64(4d2c6dfc5ac42aed), li_64(53380d139d95b3df),
                    486:     li_64(650a73548baf63de), li_64(766a0abb3c77b2a8),
                    487:     li_64(81c2c92e47edaee6), li_64(92722c851482353b),
                    488:     li_64(a2bfe8a14cf10364), li_64(a81a664bbc423001),
                    489:     li_64(c24b8b70d0f89791), li_64(c76c51a30654be30),
                    490:     li_64(d192e819d6ef5218), li_64(d69906245565a910),
                    491:     li_64(f40e35855771202a), li_64(106aa07032bbd1b8),
                    492:     li_64(19a4c116b8d2d0c8), li_64(1e376c085141ab53),
                    493:     li_64(2748774cdf8eeb99), li_64(34b0bcb5e19b48a8),
                    494:     li_64(391c0cb3c5c95a63), li_64(4ed8aa4ae3418acb),
                    495:     li_64(5b9cca4f7763e373), li_64(682e6ff3d6b2b8a3),
                    496:     li_64(748f82ee5defb2fc), li_64(78a5636f43172f60),
                    497:     li_64(84c87814a1f0ab72), li_64(8cc702081a6439ec),
                    498:     li_64(90befffa23631e28), li_64(a4506cebde82bde9),
                    499:     li_64(bef9a3f7b2c67915), li_64(c67178f2e372532b),
                    500:     li_64(ca273eceea26619c), li_64(d186b8c721c0c207),
                    501:     li_64(eada7dd6cde0eb1e), li_64(f57d4f7fee6ed178),
                    502:     li_64(06f067aa72176fba), li_64(0a637dc5a2c898a6),
                    503:     li_64(113f9804bef90dae), li_64(1b710b35131c471b),
                    504:     li_64(28db77f523047d84), li_64(32caab7b40c72493),
                    505:     li_64(3c9ebe0a15c9bebc), li_64(431d67c49c100d4c),
                    506:     li_64(4cc5d4becb3e42b6), li_64(597f299cfc657e2a),
                    507:     li_64(5fcb6fab3ad6faec), li_64(6c44198c4a475817)
                    508: };
                    509: 
                    510: /* Compile 128 bytes of hash data into SHA384/512 digest    */
                    511: /* NOTE: this routine assumes that the byte order in the    */
                    512: /* ctx->wbuf[] at this point is such that low address bytes */
                    513: /* in the ORIGINAL byte stream will go into the high end of */
                    514: /* words on BOTH big and little endian systems              */
                    515: 
                    516: VOID_RETURN sha512_compile(sha512_ctx ctx[1])
                    517: {   uint_64t    v[8], *p = ctx->wbuf;
                    518:     uint_32t    j;
                    519: 
                    520:     memcpy(v, ctx->hash, 8 * sizeof(uint_64t));
                    521: 
                    522:     for(j = 0; j < 80; j += 16)
                    523:     {
                    524:         v_cycle( 0, j); v_cycle( 1, j);
                    525:         v_cycle( 2, j); v_cycle( 3, j);
                    526:         v_cycle( 4, j); v_cycle( 5, j);
                    527:         v_cycle( 6, j); v_cycle( 7, j);
                    528:         v_cycle( 8, j); v_cycle( 9, j);
                    529:         v_cycle(10, j); v_cycle(11, j);
                    530:         v_cycle(12, j); v_cycle(13, j);
                    531:         v_cycle(14, j); v_cycle(15, j);
                    532:     }
                    533: 
                    534:     ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
                    535:     ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
                    536:     ctx->hash[4] += v[4]; ctx->hash[5] += v[5];
                    537:     ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
                    538: }
                    539: 
                    540: /* Compile 128 bytes of hash data into SHA256 digest value  */
                    541: /* NOTE: this routine assumes that the byte order in the    */
                    542: /* ctx->wbuf[] at this point is in such an order that low   */
                    543: /* address bytes in the ORIGINAL byte stream placed in this */
                    544: /* buffer will now go to the high end of words on BOTH big  */
                    545: /* and little endian systems                                */
                    546: 
                    547: VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1])
                    548: {   uint_32t pos = (uint_32t)(ctx->count[0] & SHA512_MASK),
                    549:              space = SHA512_BLOCK_SIZE - pos;
                    550:     const unsigned char *sp = data;
                    551: 
                    552:     if((ctx->count[0] += len) < len)
                    553:         ++(ctx->count[1]);
                    554: 
                    555:     while(len >= space)     /* tranfer whole blocks while possible  */
                    556:     {
                    557:         memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
                    558:         sp += space; len -= space; space = SHA512_BLOCK_SIZE; pos = 0;
                    559:         bsw_64(ctx->wbuf, SHA512_BLOCK_SIZE >> 3);
                    560:         sha512_compile(ctx);
                    561:     }
                    562: 
                    563:     memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
                    564: }
                    565: 
                    566: /* SHA384/512 Final padding and digest calculation  */
                    567: 
                    568: static void sha_end2(unsigned char hval[], sha512_ctx ctx[1], const unsigned int hlen)
                    569: {   uint_32t    i = (uint_32t)(ctx->count[0] & SHA512_MASK);
                    570: 
                    571:     /* put bytes in the buffer in an order in which references to   */
                    572:     /* 32-bit words will put bytes with lower addresses into the    */
                    573:     /* top of 32 bit words on BOTH big and little endian machines   */
                    574:     bsw_64(ctx->wbuf, (i + 7) >> 3);
                    575: 
                    576:     /* we now need to mask valid bytes and add the padding which is */
                    577:     /* a single 1 bit and as many zero bits as necessary. Note that */
                    578:     /* we can always add the first padding byte here because the    */
                    579:     /* buffer always has at least one empty slot                    */
                    580:     ctx->wbuf[i >> 3] &= li_64(ffffffffffffff00) << 8 * (~i & 7);
                    581:     ctx->wbuf[i >> 3] |= li_64(0000000000000080) << 8 * (~i & 7);
                    582: 
                    583:     /* we need 17 or more empty byte positions, one for the padding */
                    584:     /* byte (above) and sixteen for the length count.  If there is  */
                    585:     /* not enough space pad and empty the buffer                    */
                    586:     if(i > SHA512_BLOCK_SIZE - 17)
                    587:     {
                    588:         if(i < 120) ctx->wbuf[15] = 0;
                    589:         sha512_compile(ctx);
                    590:         i = 0;
                    591:     }
                    592:     else
                    593:         i = (i >> 3) + 1;
                    594: 
                    595:     while(i < 14)
                    596:         ctx->wbuf[i++] = 0;
                    597: 
                    598:     /* the following 64-bit length fields are assembled in the      */
                    599:     /* wrong byte order on little endian machines but this is       */
                    600:     /* corrected later since they are only ever used as 64-bit      */
                    601:     /* word values.                                                 */
                    602:     ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 61);
                    603:     ctx->wbuf[15] = ctx->count[0] << 3;
                    604:     sha512_compile(ctx);
                    605: 
                    606:     /* extract the hash value as bytes in case the hash buffer is   */
                    607:     /* misaligned for 32-bit words                                  */
                    608:     for(i = 0; i < hlen; ++i)
                    609:         hval[i] = (unsigned char)(ctx->hash[i >> 3] >> (8 * (~i & 7)));
                    610: }
                    611: 
                    612: #endif
                    613: 
                    614: #if defined(SHA_384)
                    615: 
                    616: /* SHA384 initialisation data   */
                    617: 
                    618: const uint_64t  i384[80] =
                    619: {
                    620:     li_64(cbbb9d5dc1059ed8), li_64(629a292a367cd507),
                    621:     li_64(9159015a3070dd17), li_64(152fecd8f70e5939),
                    622:     li_64(67332667ffc00b31), li_64(8eb44a8768581511),
                    623:     li_64(db0c2e0d64f98fa7), li_64(47b5481dbefa4fa4)
                    624: };
                    625: 
                    626: VOID_RETURN sha384_begin(sha384_ctx ctx[1])
                    627: {
                    628:     ctx->count[0] = ctx->count[1] = 0;
                    629:     memcpy(ctx->hash, i384, 8 * sizeof(uint_64t));
                    630: }
                    631: 
                    632: VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1])
                    633: {
                    634:     sha_end2(hval, ctx, SHA384_DIGEST_SIZE);
                    635: }
                    636: 
                    637: VOID_RETURN sha384(unsigned char hval[], const unsigned char data[], unsigned long len)
                    638: {   sha384_ctx  cx[1];
                    639: 
                    640:     sha384_begin(cx);
                    641:     sha384_hash(data, len, cx);
                    642:     sha_end2(hval, cx, SHA384_DIGEST_SIZE);
                    643: }
                    644: 
                    645: #endif
                    646: 
                    647: #if defined(SHA_512)
                    648: 
                    649: /* SHA512 initialisation data   */
                    650: 
                    651: const uint_64t  i512[80] =
                    652: {
                    653:     li_64(6a09e667f3bcc908), li_64(bb67ae8584caa73b),
                    654:     li_64(3c6ef372fe94f82b), li_64(a54ff53a5f1d36f1),
                    655:     li_64(510e527fade682d1), li_64(9b05688c2b3e6c1f),
                    656:     li_64(1f83d9abfb41bd6b), li_64(5be0cd19137e2179)
                    657: };
                    658: 
                    659: VOID_RETURN sha512_begin(sha512_ctx ctx[1])
                    660: {
                    661:     ctx->count[0] = ctx->count[1] = 0;
                    662:     memcpy(ctx->hash, i512, 8 * sizeof(uint_64t));
                    663: }
                    664: 
                    665: VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1])
                    666: {
                    667:     sha_end2(hval, ctx, SHA512_DIGEST_SIZE);
                    668: }
                    669: 
                    670: VOID_RETURN sha512(unsigned char hval[], const unsigned char data[], unsigned long len)
                    671: {   sha512_ctx  cx[1];
                    672: 
                    673:     sha512_begin(cx);
                    674:     sha512_hash(data, len, cx);
                    675:     sha_end2(hval, cx, SHA512_DIGEST_SIZE);
                    676: }
                    677: 
                    678: #endif
                    679: 
                    680: #if defined(SHA_2)
                    681: 
                    682: #define CTX_224(x)  ((x)->uu->ctx256)
                    683: #define CTX_256(x)  ((x)->uu->ctx256)
                    684: #define CTX_384(x)  ((x)->uu->ctx512)
                    685: #define CTX_512(x)  ((x)->uu->ctx512)
                    686: 
                    687: /* SHA2 initialisation */
                    688: 
                    689: INT_RETURN sha2_begin(unsigned long len, sha2_ctx ctx[1])
                    690: {
                    691:     switch(len)
                    692:     {
                    693: #if defined(SHA_224)
                    694:         case 224:
                    695:         case  28:   CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
                    696:                     memcpy(CTX_256(ctx)->hash, i224, 32);
                    697:                     ctx->sha2_len = 28; return EXIT_SUCCESS;
                    698: #endif
                    699: #if defined(SHA_256)
                    700:         case 256:
                    701:         case  32:   CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
                    702:                     memcpy(CTX_256(ctx)->hash, i256, 32);
                    703:                     ctx->sha2_len = 32; return EXIT_SUCCESS;
                    704: #endif
                    705: #if defined(SHA_384)
                    706:         case 384:
                    707:         case  48:   CTX_384(ctx)->count[0] = CTX_384(ctx)->count[1] = 0;
                    708:                     memcpy(CTX_384(ctx)->hash, i384, 64);
                    709:                     ctx->sha2_len = 48; return EXIT_SUCCESS;
                    710: #endif
                    711: #if defined(SHA_512)
                    712:         case 512:
                    713:         case  64:   CTX_512(ctx)->count[0] = CTX_512(ctx)->count[1] = 0;
                    714:                     memcpy(CTX_512(ctx)->hash, i512, 64);
                    715:                     ctx->sha2_len = 64; return EXIT_SUCCESS;
                    716: #endif
                    717:         default:    return EXIT_FAILURE;
                    718:     }
                    719: }
                    720: 
                    721: VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1])
                    722: {
                    723:     switch(ctx->sha2_len)
                    724:     {
                    725: #if defined(SHA_224)
                    726:         case 28: sha224_hash(data, len, CTX_224(ctx)); return;
                    727: #endif
                    728: #if defined(SHA_256)
                    729:         case 32: sha256_hash(data, len, CTX_256(ctx)); return;
                    730: #endif
                    731: #if defined(SHA_384)
                    732:         case 48: sha384_hash(data, len, CTX_384(ctx)); return;
                    733: #endif
                    734: #if defined(SHA_512)
                    735:         case 64: sha512_hash(data, len, CTX_512(ctx)); return;
                    736: #endif
                    737:     }
                    738: }
                    739: 
                    740: VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1])
                    741: {
                    742:     switch(ctx->sha2_len)
                    743:     {
                    744: #if defined(SHA_224)
                    745:         case 28: sha_end1(hval, CTX_224(ctx), SHA224_DIGEST_SIZE); return;
                    746: #endif
                    747: #if defined(SHA_256)
                    748:         case 32: sha_end1(hval, CTX_256(ctx), SHA256_DIGEST_SIZE); return;
                    749: #endif
                    750: #if defined(SHA_384)
                    751:         case 48: sha_end2(hval, CTX_384(ctx), SHA384_DIGEST_SIZE); return;
                    752: #endif
                    753: #if defined(SHA_512)
                    754:         case 64: sha_end2(hval, CTX_512(ctx), SHA512_DIGEST_SIZE); return;
                    755: #endif
                    756:     }
                    757: }
                    758: 
                    759: INT_RETURN sha2(unsigned char hval[], unsigned long size,
                    760:                                 const unsigned char data[], unsigned long len)
                    761: {   sha2_ctx    cx[1];
                    762: 
                    763:     if(sha2_begin(size, cx) == EXIT_SUCCESS)
                    764:     {
                    765:         sha2_hash(data, len, cx); sha2_end(hval, cx); return EXIT_SUCCESS;
                    766:     }
                    767:     else
                    768:         return EXIT_FAILURE;
                    769: }
                    770: 
                    771: #endif
                    772: 
                    773: #if defined(__cplusplus)
                    774: }
                    775: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.