Annotation of truecrypt/crypto/sha1.c, revision 1.1.1.3

1.1.1.3 ! root        1: /* Deprecated/legacy */
        !             2: 
1.1.1.2   root        3: /*
                      4:  ---------------------------------------------------------------------------
                      5:  Copyright (c) 2002, Dr Brian Gladman, Worcester, UK.   All rights reserved.
1.1       root        6: 
1.1.1.2   root        7:  LICENSE TERMS
1.1       root        8: 
1.1.1.2   root        9:  The free distribution and use of this software in both source and binary
                     10:  form is allowed (with or without changes) provided that:
1.1       root       11: 
1.1.1.2   root       12:    1. distributions of this source code include the above copyright
                     13:       notice, this list of conditions and the following disclaimer;
1.1       root       14: 
1.1.1.2   root       15:    2. distributions in binary form include the above copyright
                     16:       notice, this list of conditions and the following disclaimer
                     17:       in the documentation and/or other associated materials;
1.1       root       18: 
1.1.1.2   root       19:    3. the copyright holder's name is not used to endorse products
                     20:       built using this software without specific written permission.
1.1       root       21: 
1.1.1.2   root       22:  ALTERNATIVELY, provided that this notice is retained in full, this product
                     23:  may be distributed under the terms of the GNU General Public License (GPL),
                     24:  in which case the provisions of the GPL apply INSTEAD OF those given above.
                     25: 
                     26:  DISCLAIMER
                     27: 
                     28:  This software is provided 'as is' with no explicit or implied warranties
                     29:  in respect of its properties, including, but not limited to, correctness
                     30:  and/or fitness for purpose.
                     31:  ---------------------------------------------------------------------------
                     32:  Issue Date: 18/06/2004
                     33: 
                     34:  This is a byte oriented version of SHA1 that operates on arrays of bytes
                     35:  stored in memory.
                     36: */
                     37: 
                     38: #include <string.h>     /* for memcpy() etc.        */
                     39: #include <stdlib.h>     /* for _lrotl with VC++     */
                     40: 
                     41: #include "Sha1.h"
                     42: 
                     43: #if defined(__cplusplus)
                     44: extern "C"
                     45: {
                     46: #endif
                     47: 
                     48: /*
                     49:     To obtain the highest speed on processors with 32-bit words, this code
                     50:     needs to determine the order in which bytes are packed into such words.
                     51:     The following block of code is an attempt to capture the most obvious
                     52:     ways in which various environemnts specify their endian definitions.
                     53:     It may well fail, in which case the definitions will need to be set by
                     54:     editing at the points marked **** EDIT HERE IF NECESSARY **** below.
                     55: */
                     56: 
                     57: /*  PLATFORM SPECIFIC INCLUDES */
                     58: 
                     59: /* Original byte order detection removed */
                     60: #include "../Common/Endian.h"
                     61: 
                     62: #define BRG_LITTLE_ENDIAN   1234 /* byte 0 is least significant (i386) */
                     63: #define BRG_BIG_ENDIAN      4321 /* byte 0 is most significant (mc68k) */
                     64: 
                     65: #if BYTE_ORDER == LITTLE_ENDIAN
                     66: #  define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
                     67: #endif
                     68: 
                     69: #if BYTE_ORDER == BIG_ENDIAN
                     70: #  define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
                     71: #endif
                     72: 
                     73: #ifdef _MSC_VER
                     74: #pragma intrinsic(memcpy)
                     75: #endif
                     76: 
                     77: #if 1 && defined(_MSC_VER) && !defined(_DEBUG)
                     78: #define rotl32  _rotl
                     79: #define rotr32  _rotr
                     80: #else
                     81: #define rotl32(x,n)   (((x) << n) | ((x) >> (32 - n)))
                     82: #define rotr32(x,n)   (((x) >> n) | ((x) << (32 - n)))
                     83: #endif
                     84: 
                     85: #if !defined(bswap_32)
                     86: #define bswap_32(x) (rotr32((x), 24) & 0x00ff00ff | rotr32((x), 8) & 0xff00ff00)
                     87: #endif
                     88: 
                     89: #if (PLATFORM_BYTE_ORDER == BRG_LITTLE_ENDIAN)
                     90: #define SWAP_BYTES
1.1       root       91: #else
1.1.1.2   root       92: #undef  SWAP_BYTES
1.1       root       93: #endif
                     94: 
1.1.1.2   root       95: #if defined(SWAP_BYTES)
                     96: #define bsw_32(p,n) \
                     97:     { int _i = (n); while(_i--) ((sha1_32t*)p)[_i] = bswap_32(((sha1_32t*)p)[_i]); }
                     98: #else
                     99: #define bsw_32(p,n)
                    100: #endif
1.1       root      101: 
1.1.1.2   root      102: #define SHA1_MASK   (SHA1_BLOCK_SIZE - 1)
1.1       root      103: 
1.1.1.2   root      104: #if 0
1.1       root      105: 
1.1.1.2   root      106: #define ch(x,y,z)       (((x) & (y)) ^ (~(x) & (z)))
                    107: #define parity(x,y,z)   ((x) ^ (y) ^ (z))
                    108: #define maj(x,y,z)      (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
                    109: 
                    110: #else   /* Discovered by Rich Schroeppel and Colin Plumb   */
1.1       root      111: 
1.1.1.2   root      112: #define ch(x,y,z)       ((z) ^ ((x) & ((y) ^ (z))))
                    113: #define parity(x,y,z)   ((x) ^ (y) ^ (z))
                    114: #define maj(x,y,z)      (((x) & (y)) | ((z) & ((x) ^ (y))))
1.1       root      115: 
1.1.1.2   root      116: #endif
                    117: 
                    118: /* Compile 64 bytes of hash data into SHA1 context. Note    */
                    119: /* that this routine assumes that the byte order in the     */
                    120: /* ctx->wbuf[] at this point is in such an order that low   */
                    121: /* address bytes in the ORIGINAL byte stream will go in     */
                    122: /* this buffer to the high end of 32-bit words on BOTH big  */
                    123: /* and little endian systems                                */
                    124: 
                    125: #ifdef ARRAY
                    126: #define q(v,n)  v[n]
                    127: #else
                    128: #define q(v,n)  v##n
                    129: #endif
                    130: 
                    131: #define one_cycle(v,a,b,c,d,e,f,k,h)            \
                    132:     q(v,e) += rotr32(q(v,a),27) +               \
                    133:               f(q(v,b),q(v,c),q(v,d)) + k + h;  \
                    134:     q(v,b)  = rotr32(q(v,b), 2)
                    135: 
                    136: #define five_cycle(v,f,k,i)                 \
                    137:     one_cycle(v, 0,1,2,3,4, f,k,hf(i  ));   \
                    138:     one_cycle(v, 4,0,1,2,3, f,k,hf(i+1));   \
                    139:     one_cycle(v, 3,4,0,1,2, f,k,hf(i+2));   \
                    140:     one_cycle(v, 2,3,4,0,1, f,k,hf(i+3));   \
                    141:     one_cycle(v, 1,2,3,4,0, f,k,hf(i+4))
                    142: 
                    143: void sha1_compile(sha1_ctx ctx[1])
                    144: {   sha1_32t    *w = ctx->wbuf;
                    145: 
                    146: #ifdef ARRAY
                    147:     sha1_32t    v[5];
                    148:     memcpy(v, ctx->hash, 5 * sizeof(sha1_32t));
                    149: #else
                    150:     sha1_32t    v0, v1, v2, v3, v4;
                    151:     v0 = ctx->hash[0]; v1 = ctx->hash[1];
                    152:     v2 = ctx->hash[2]; v3 = ctx->hash[3];
                    153:     v4 = ctx->hash[4];
                    154: #endif
                    155: 
                    156: #define hf(i)   w[i]
                    157: 
                    158:     five_cycle(v, ch, 0x5a827999,  0);
                    159:     five_cycle(v, ch, 0x5a827999,  5);
                    160:     five_cycle(v, ch, 0x5a827999, 10);
                    161:     one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \
                    162: 
                    163: #undef  hf
                    164: #define hf(i) (w[(i) & 15] = rotl32(                    \
                    165:                  w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \
                    166:                ^ w[((i) +  2) & 15] ^ w[(i) & 15], 1))
                    167: 
                    168:     one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16));
                    169:     one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17));
                    170:     one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18));
                    171:     one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19));
                    172: 
                    173:     five_cycle(v, parity, 0x6ed9eba1,  20);
                    174:     five_cycle(v, parity, 0x6ed9eba1,  25);
                    175:     five_cycle(v, parity, 0x6ed9eba1,  30);
                    176:     five_cycle(v, parity, 0x6ed9eba1,  35);
                    177: 
                    178:     five_cycle(v, maj, 0x8f1bbcdc,  40);
                    179:     five_cycle(v, maj, 0x8f1bbcdc,  45);
                    180:     five_cycle(v, maj, 0x8f1bbcdc,  50);
                    181:     five_cycle(v, maj, 0x8f1bbcdc,  55);
                    182: 
                    183:     five_cycle(v, parity, 0xca62c1d6,  60);
                    184:     five_cycle(v, parity, 0xca62c1d6,  65);
                    185:     five_cycle(v, parity, 0xca62c1d6,  70);
                    186:     five_cycle(v, parity, 0xca62c1d6,  75);
                    187: 
                    188: #ifdef ARRAY
                    189:     ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
                    190:     ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
                    191:     ctx->hash[4] += v[4];
                    192: #else
                    193:     ctx->hash[0] += v0; ctx->hash[1] += v1;
                    194:     ctx->hash[2] += v2; ctx->hash[3] += v3;
                    195:     ctx->hash[4] += v4;
                    196: #endif
                    197: }
                    198: 
                    199: void sha1_begin(sha1_ctx ctx[1])
1.1       root      200: {
1.1.1.2   root      201:     ctx->count[0] = ctx->count[1] = 0;
                    202:     ctx->hash[0] = 0x67452301;
                    203:     ctx->hash[1] = 0xefcdab89;
                    204:     ctx->hash[2] = 0x98badcfe;
                    205:     ctx->hash[3] = 0x10325476;
                    206:     ctx->hash[4] = 0xc3d2e1f0;
1.1       root      207: }
                    208: 
1.1.1.2   root      209: /* SHA1 hash data in an array of bytes into hash buffer and */
                    210: /* call the hash_compile function as required.              */
1.1       root      211: 
1.1.1.2   root      212: void sha1_hash(const unsigned char data[], unsigned __int32 len, sha1_ctx ctx[1])
                    213: {   sha1_32t pos = (sha1_32t)(ctx->count[0] & SHA1_MASK),
                    214:             space = SHA1_BLOCK_SIZE - pos;
                    215:     const unsigned char *sp = data;
1.1       root      216: 
1.1.1.2   root      217:     if((ctx->count[0] += len) < len)
                    218:         ++(ctx->count[1]);
1.1       root      219: 
1.1.1.2   root      220:     while(len >= space)     /* tranfer whole blocks if possible  */
1.1       root      221:     {
1.1.1.2   root      222:         memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
                    223:         sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0;
                    224:         bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2);
                    225:         sha1_compile(ctx);
1.1       root      226:     }
                    227: 
1.1.1.2   root      228:     memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
                    229: }
1.1       root      230: 
1.1.1.2   root      231: /* SHA1 final padding and digest calculation  */
1.1       root      232: 
1.1.1.2   root      233: void sha1_end(unsigned char hval[], sha1_ctx ctx[1])
                    234: {   sha1_32t    i = (sha1_32t)(ctx->count[0] & SHA1_MASK);
1.1       root      235: 
1.1.1.2   root      236:     /* put bytes in the buffer in an order in which references to   */
                    237:     /* 32-bit words will put bytes with lower addresses into the    */
                    238:     /* top of 32 bit words on BOTH big and little endian machines   */
                    239:     bsw_32(ctx->wbuf, (i + 3) >> 2);
                    240: 
                    241:     /* we now need to mask valid bytes and add the padding which is */
                    242:     /* a single 1 bit and as many zero bits as necessary. Note that */
                    243:     /* we can always add the first padding byte here because the    */
                    244:     /* buffer always has at least one empty slot                    */
                    245:     ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3);
                    246:     ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3);
                    247: 
                    248:     /* we need 9 or more empty positions, one for the padding byte  */
                    249:     /* (above) and eight for the length count. If there is not      */
                    250:     /* enough space, pad and empty the buffer                       */
                    251:     if(i > SHA1_BLOCK_SIZE - 9)
1.1       root      252:     {
1.1.1.2   root      253:         if(i < 60) ctx->wbuf[15] = 0;
                    254:         sha1_compile(ctx);
                    255:         i = 0;
1.1       root      256:     }
1.1.1.2   root      257:     else    /* compute a word index for the empty buffer positions  */
                    258:         i = (i >> 2) + 1;
                    259: 
                    260:     while(i < 14) /* and zero pad all but last two positions        */
                    261:         ctx->wbuf[i++] = 0;
                    262: 
                    263:     /* the following 32-bit length fields are assembled in the      */
                    264:     /* wrong byte order on little endian machines but this is       */
                    265:     /* corrected later since they are only ever used as 32-bit      */
                    266:     /* word values.                                                 */
                    267:     ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
                    268:     ctx->wbuf[15] = ctx->count[0] << 3;
                    269:     sha1_compile(ctx);
                    270: 
                    271:     /* extract the hash value as bytes in case the hash buffer is   */
                    272:     /* misaligned for 32-bit words                                  */
                    273:     for(i = 0; i < SHA1_DIGEST_SIZE; ++i)
                    274:         hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
                    275: }
1.1       root      276: 
1.1.1.2   root      277: void sha1(unsigned char hval[], const unsigned char data[], unsigned __int32 len)
                    278: {   sha1_ctx    cx[1];
1.1       root      279: 
1.1.1.2   root      280:     sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx);
1.1       root      281: }
1.1.1.2   root      282: 
                    283: #if defined(__cplusplus)
                    284: }
                    285: #endif

unix.superglobalmegacorp.com

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