Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 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 SHA1 that operates on arrays of bytes |
| 33 | stored in memory. |
| 34 | */ |
| 35 | |
| 36 | #include <string.h> /* for memcpy() etc. */ |
| 37 | |
| 38 | #include "sha1.h" |
| 39 | |
| 40 | #if defined(__cplusplus) |
| 41 | extern "C" |
| 42 | { |
| 43 | #endif |
| 44 | |
| 45 | #if defined( _MSC_VER ) && ( _MSC_VER > 800 ) |
| 46 | #pragma intrinsic(memcpy) |
| 47 | #endif |
| 48 | |
| 49 | #if 0 && defined(_MSC_VER) |
| 50 | #define rotl32 _lrotl |
| 51 | #define rotr32 _lrotr |
| 52 | #else |
| 53 | #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n))) |
| 54 | #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n))) |
| 55 | #endif |
| 56 | |
| 57 | #if !defined(bswap_32) |
| 58 | #define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00)) |
| 59 | #endif |
| 60 | |
| 61 | #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN) |
| 62 | #define SWAP_BYTES |
| 63 | #else |
| 64 | #undef SWAP_BYTES |
| 65 | #endif |
| 66 | |
| 67 | #if defined(SWAP_BYTES) |
| 68 | #define bsw_32(p,n) \ |
| 69 | { int _i = (n); while(_i--) ((uint_32t*)p)[_i] = bswap_32(((uint_32t*)p)[_i]); } |
| 70 | #else |
| 71 | #define bsw_32(p,n) |
| 72 | #endif |
| 73 | |
| 74 | #define SHA1_MASK (SHA1_BLOCK_SIZE - 1) |
| 75 | |
| 76 | #if 0 |
| 77 | |
| 78 | #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z))) |
| 79 | #define parity(x,y,z) ((x) ^ (y) ^ (z)) |
| 80 | #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
| 81 | |
| 82 | #else /* Discovered by Rich Schroeppel and Colin Plumb */ |
| 83 | |
| 84 | #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) |
| 85 | #define parity(x,y,z) ((x) ^ (y) ^ (z)) |
| 86 | #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y)))) |
| 87 | |
| 88 | #endif |
| 89 | |
| 90 | /* Compile 64 bytes of hash data into SHA1 context. Note */ |
| 91 | /* that this routine assumes that the byte order in the */ |
| 92 | /* ctx->wbuf[] at this point is in such an order that low */ |
| 93 | /* address bytes in the ORIGINAL byte stream will go in */ |
| 94 | /* this buffer to the high end of 32-bit words on BOTH big */ |
| 95 | /* and little endian systems */ |
| 96 | |
| 97 | #ifdef ARRAY |
| 98 | #define q(v,n) v[n] |
| 99 | #else |
| 100 | #define q(v,n) v##n |
| 101 | #endif |
| 102 | |
| 103 | #define one_cycle(v,a,b,c,d,e,f,k,h) \ |
| 104 | q(v,e) += rotr32(q(v,a),27) + \ |
| 105 | f(q(v,b),q(v,c),q(v,d)) + k + h; \ |
| 106 | q(v,b) = rotr32(q(v,b), 2) |
| 107 | |
| 108 | #define five_cycle(v,f,k,i) \ |
| 109 | one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \ |
| 110 | one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \ |
| 111 | one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \ |
| 112 | one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \ |
| 113 | one_cycle(v, 1,2,3,4,0, f,k,hf(i+4)) |
| 114 | |
| 115 | VOID_RETURN sha1_compile(sha1_ctx ctx[1]) |
| 116 | { uint_32t *w = ctx->wbuf; |
| 117 | |
| 118 | #ifdef ARRAY |
| 119 | uint_32t v[5]; |
| 120 | memcpy(v, ctx->hash, 5 * sizeof(uint_32t)); |
| 121 | #else |
| 122 | uint_32t v0, v1, v2, v3, v4; |
| 123 | v0 = ctx->hash[0]; v1 = ctx->hash[1]; |
| 124 | v2 = ctx->hash[2]; v3 = ctx->hash[3]; |
| 125 | v4 = ctx->hash[4]; |
| 126 | #endif |
| 127 | |
| 128 | #define hf(i) w[i] |
| 129 | |
| 130 | five_cycle(v, ch, 0x5a827999, 0); |
| 131 | five_cycle(v, ch, 0x5a827999, 5); |
| 132 | five_cycle(v, ch, 0x5a827999, 10); |
| 133 | one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \ |
| 134 | |
| 135 | #undef hf |
| 136 | #define hf(i) (w[(i) & 15] = rotl32( \ |
| 137 | w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \ |
| 138 | ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1)) |
| 139 | |
| 140 | one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16)); |
| 141 | one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17)); |
| 142 | one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18)); |
| 143 | one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19)); |
| 144 | |
| 145 | five_cycle(v, parity, 0x6ed9eba1, 20); |
| 146 | five_cycle(v, parity, 0x6ed9eba1, 25); |
| 147 | five_cycle(v, parity, 0x6ed9eba1, 30); |
| 148 | five_cycle(v, parity, 0x6ed9eba1, 35); |
| 149 | |
| 150 | five_cycle(v, maj, 0x8f1bbcdc, 40); |
| 151 | five_cycle(v, maj, 0x8f1bbcdc, 45); |
| 152 | five_cycle(v, maj, 0x8f1bbcdc, 50); |
| 153 | five_cycle(v, maj, 0x8f1bbcdc, 55); |
| 154 | |
| 155 | five_cycle(v, parity, 0xca62c1d6, 60); |
| 156 | five_cycle(v, parity, 0xca62c1d6, 65); |
| 157 | five_cycle(v, parity, 0xca62c1d6, 70); |
| 158 | five_cycle(v, parity, 0xca62c1d6, 75); |
| 159 | |
| 160 | #ifdef ARRAY |
| 161 | ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; |
| 162 | ctx->hash[2] += v[2]; ctx->hash[3] += v[3]; |
| 163 | ctx->hash[4] += v[4]; |
| 164 | #else |
| 165 | ctx->hash[0] += v0; ctx->hash[1] += v1; |
| 166 | ctx->hash[2] += v2; ctx->hash[3] += v3; |
| 167 | ctx->hash[4] += v4; |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | VOID_RETURN sha1_begin(sha1_ctx ctx[1]) |
| 172 | { |
| 173 | ctx->count[0] = ctx->count[1] = 0; |
| 174 | ctx->hash[0] = 0x67452301; |
| 175 | ctx->hash[1] = 0xefcdab89; |
| 176 | ctx->hash[2] = 0x98badcfe; |
| 177 | ctx->hash[3] = 0x10325476; |
| 178 | ctx->hash[4] = 0xc3d2e1f0; |
| 179 | } |
| 180 | |
| 181 | /* SHA1 hash data in an array of bytes into hash buffer and */ |
| 182 | /* call the hash_compile function as required. */ |
| 183 | |
| 184 | VOID_RETURN sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1]) |
| 185 | { |
| 186 | uint_32t pos = (uint_32t)(ctx->count[0] & SHA1_MASK), |
| 187 | space = SHA1_BLOCK_SIZE - pos; |
| 188 | const unsigned char *sp = data; |
| 189 | |
| 190 | if((ctx->count[0] += len) < len) |
| 191 | ++(ctx->count[1]); |
| 192 | |
| 193 | while(len >= space) /* tranfer whole blocks if possible */ |
| 194 | { |
| 195 | memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space); |
| 196 | sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0; |
| 197 | bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2); |
| 198 | sha1_compile(ctx); |
| 199 | } |
| 200 | |
| 201 | memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len); |
| 202 | } |
| 203 | |
| 204 | /* SHA1 final padding and digest calculation */ |
| 205 | |
| 206 | VOID_RETURN sha1_end(unsigned char hval[], sha1_ctx ctx[1]) |
| 207 | { uint_32t i = (uint_32t)(ctx->count[0] & SHA1_MASK); |
| 208 | |
| 209 | /* put bytes in the buffer in an order in which references to */ |
| 210 | /* 32-bit words will put bytes with lower addresses into the */ |
| 211 | /* top of 32 bit words on BOTH big and little endian machines */ |
| 212 | bsw_32(ctx->wbuf, (i + 3) >> 2); |
| 213 | |
| 214 | /* we now need to mask valid bytes and add the padding which is */ |
| 215 | /* a single 1 bit and as many zero bits as necessary. Note that */ |
| 216 | /* we can always add the first padding byte here because the */ |
| 217 | /* buffer always has at least one empty slot */ |
| 218 | ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3); |
| 219 | ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3); |
| 220 | |
| 221 | /* we need 9 or more empty positions, one for the padding byte */ |
| 222 | /* (above) and eight for the length count. If there is not */ |
| 223 | /* enough space, pad and empty the buffer */ |
| 224 | if(i > SHA1_BLOCK_SIZE - 9) |
| 225 | { |
| 226 | if(i < 60) ctx->wbuf[15] = 0; |
| 227 | sha1_compile(ctx); |
| 228 | i = 0; |
| 229 | } |
| 230 | else /* compute a word index for the empty buffer positions */ |
| 231 | i = (i >> 2) + 1; |
| 232 | |
| 233 | while(i < 14) /* and zero pad all but last two positions */ |
| 234 | ctx->wbuf[i++] = 0; |
| 235 | |
| 236 | /* the following 32-bit length fields are assembled in the */ |
| 237 | /* wrong byte order on little endian machines but this is */ |
| 238 | /* corrected later since they are only ever used as 32-bit */ |
| 239 | /* word values. */ |
| 240 | ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29); |
| 241 | ctx->wbuf[15] = ctx->count[0] << 3; |
| 242 | sha1_compile(ctx); |
| 243 | |
| 244 | /* extract the hash value as bytes in case the hash buffer is */ |
| 245 | /* misaligned for 32-bit words */ |
| 246 | for(i = 0; i < SHA1_DIGEST_SIZE; ++i) |
| 247 | hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3))); |
| 248 | } |
| 249 | |
| 250 | VOID_RETURN bg_sha1(unsigned char hval[], const unsigned char data[], unsigned long len) |
| 251 | { sha1_ctx cx[1]; |
| 252 | |
| 253 | sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx); |
| 254 | } |
| 255 | |
| 256 | #if defined(__cplusplus) |
| 257 | } |
| 258 | #endif |