sha1dc / sha1.con commit Merge branch 'sb/submodule-rm-absorb' (d04787e)
   1/***
   2* Copyright 2017 Marc Stevens <marc@marc-stevens.nl>, Dan Shumow (danshu@microsoft.com)
   3* Distributed under the MIT Software License.
   4* See accompanying file LICENSE.txt or copy at
   5* https://opensource.org/licenses/MIT
   6***/
   7
   8#ifndef SHA1DC_NO_STANDARD_INCLUDES
   9#include <string.h>
  10#include <memory.h>
  11#include <stdio.h>
  12#include <stdlib.h>
  13#endif
  14
  15#ifdef SHA1DC_CUSTOM_INCLUDE_SHA1_C
  16#include SHA1DC_CUSTOM_INCLUDE_SHA1_C
  17#endif
  18
  19#ifndef SHA1DC_INIT_SAFE_HASH_DEFAULT
  20#define SHA1DC_INIT_SAFE_HASH_DEFAULT 1
  21#endif
  22
  23#include "sha1.h"
  24#include "ubc_check.h"
  25
  26
  27/*
  28   Because Little-Endian architectures are most common,
  29   we only set SHA1DC_BIGENDIAN if one of these conditions is met.
  30   Note that all MSFT platforms are little endian,
  31   so none of these will be defined under the MSC compiler.
  32   If you are compiling on a big endian platform and your compiler does not define one of these,
  33   you will have to add whatever macros your tool chain defines to indicate Big-Endianness.
  34 */
  35#ifdef SHA1DC_BIGENDIAN
  36#undef SHA1DC_BIGENDIAN
  37#endif
  38
  39#if (defined(_BYTE_ORDER) || defined(__BYTE_ORDER) || defined(__BYTE_ORDER__))
  40
  41#if ((defined(_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN)) || \
  42     (defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN)) || \
  43     (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__)) )
  44#define SHA1DC_BIGENDIAN
  45#endif
  46
  47#else
  48
  49#if (defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN) || defined(__BIG_ENDIAN__) || \
  50     defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
  51     defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || \
  52     defined(__sparc))
  53#define SHA1DC_BIGENDIAN
  54#endif
  55
  56#endif
  57
  58#if (defined(SHA1DC_FORCE_LITTLEENDIAN) && defined(SHA1DC_BIGENDIAN))
  59#undef SHA1DC_BIGENDIAN
  60#endif
  61#if (defined(SHA1DC_FORCE_BIGENDIAN) && !defined(SHA1DC_BIGENDIAN))
  62#define SHA1DC_BIGENDIAN
  63#endif
  64/*ENDIANNESS SELECTION*/
  65
  66#if (defined SHA1DC_FORCE_UNALIGNED_ACCESS || \
  67     defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || \
  68     defined(i386) || defined(__i386) || defined(__i386__) || defined(__i486__)  || \
  69     defined(__i586__) || defined(__i686__) || defined(_M_IX86) || defined(__X86__) || \
  70     defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__) || defined(__INTEL__) || \
  71     defined(__386) || defined(_M_X64) || defined(_M_AMD64))
  72
  73#define SHA1DC_ALLOW_UNALIGNED_ACCESS
  74
  75#endif /*UNALIGNMENT DETECTION*/
  76
  77
  78#define rotate_right(x,n) (((x)>>(n))|((x)<<(32-(n))))
  79#define rotate_left(x,n)  (((x)<<(n))|((x)>>(32-(n))))
  80
  81#define sha1_bswap32(x) \
  82        {x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF); x = (x << 16) | (x >> 16);}
  83
  84#define sha1_mix(W, t)  (rotate_left(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1))
  85
  86#ifdef SHA1DC_BIGENDIAN
  87        #define sha1_load(m, t, temp)  { temp = m[t]; }
  88#else
  89        #define sha1_load(m, t, temp)  { temp = m[t]; sha1_bswap32(temp); }
  90#endif
  91
  92#define sha1_store(W, t, x)     *(volatile uint32_t *)&W[t] = x
  93
  94#define sha1_f1(b,c,d) ((d)^((b)&((c)^(d))))
  95#define sha1_f2(b,c,d) ((b)^(c)^(d))
  96#define sha1_f3(b,c,d) (((b)&(c))+((d)&((b)^(c))))
  97#define sha1_f4(b,c,d) ((b)^(c)^(d))
  98
  99#define HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, m, t) \
 100        { e += rotate_left(a, 5) + sha1_f1(b,c,d) + 0x5A827999 + m[t]; b = rotate_left(b, 30); }
 101#define HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, m, t) \
 102        { e += rotate_left(a, 5) + sha1_f2(b,c,d) + 0x6ED9EBA1 + m[t]; b = rotate_left(b, 30); }
 103#define HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, m, t) \
 104        { e += rotate_left(a, 5) + sha1_f3(b,c,d) + 0x8F1BBCDC + m[t]; b = rotate_left(b, 30); }
 105#define HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, m, t) \
 106        { e += rotate_left(a, 5) + sha1_f4(b,c,d) + 0xCA62C1D6 + m[t]; b = rotate_left(b, 30); }
 107
 108#define HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(a, b, c, d, e, m, t) \
 109        { b = rotate_right(b, 30); e -= rotate_left(a, 5) + sha1_f1(b,c,d) + 0x5A827999 + m[t]; }
 110#define HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(a, b, c, d, e, m, t) \
 111        { b = rotate_right(b, 30); e -= rotate_left(a, 5) + sha1_f2(b,c,d) + 0x6ED9EBA1 + m[t]; }
 112#define HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(a, b, c, d, e, m, t) \
 113        { b = rotate_right(b, 30); e -= rotate_left(a, 5) + sha1_f3(b,c,d) + 0x8F1BBCDC + m[t]; }
 114#define HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(a, b, c, d, e, m, t) \
 115        { b = rotate_right(b, 30); e -= rotate_left(a, 5) + sha1_f4(b,c,d) + 0xCA62C1D6 + m[t]; }
 116
 117#define SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(a, b, c, d, e, m, W, t, temp) \
 118        {sha1_load(m, t, temp); sha1_store(W, t, temp); e += temp + rotate_left(a, 5) + sha1_f1(b,c,d) + 0x5A827999; b = rotate_left(b, 30);}
 119
 120#define SHA1COMPRESS_FULL_ROUND1_STEP_EXPAND(a, b, c, d, e, W, t, temp) \
 121        {temp = sha1_mix(W, t); sha1_store(W, t, temp); e += temp + rotate_left(a, 5) + sha1_f1(b,c,d) + 0x5A827999; b = rotate_left(b, 30); }
 122
 123#define SHA1COMPRESS_FULL_ROUND2_STEP(a, b, c, d, e, W, t, temp) \
 124        {temp = sha1_mix(W, t); sha1_store(W, t, temp); e += temp + rotate_left(a, 5) + sha1_f2(b,c,d) + 0x6ED9EBA1; b = rotate_left(b, 30); }
 125
 126#define SHA1COMPRESS_FULL_ROUND3_STEP(a, b, c, d, e, W, t, temp) \
 127        {temp = sha1_mix(W, t); sha1_store(W, t, temp); e += temp + rotate_left(a, 5) + sha1_f3(b,c,d) + 0x8F1BBCDC; b = rotate_left(b, 30); }
 128
 129#define SHA1COMPRESS_FULL_ROUND4_STEP(a, b, c, d, e, W, t, temp) \
 130        {temp = sha1_mix(W, t); sha1_store(W, t, temp); e += temp + rotate_left(a, 5) + sha1_f4(b,c,d) + 0xCA62C1D6; b = rotate_left(b, 30); }
 131
 132
 133#define SHA1_STORE_STATE(i) states[i][0] = a; states[i][1] = b; states[i][2] = c; states[i][3] = d; states[i][4] = e;
 134
 135#ifdef BUILDNOCOLLDETECTSHA1COMPRESSION
 136void sha1_compression(uint32_t ihv[5], const uint32_t m[16])
 137{
 138        uint32_t W[80];
 139        uint32_t a,b,c,d,e;
 140        unsigned i;
 141
 142        memcpy(W, m, 16 * 4);
 143        for (i = 16; i < 80; ++i)
 144                W[i] = sha1_mix(W, i);
 145
 146        a = ihv[0]; b = ihv[1]; c = ihv[2]; d = ihv[3]; e = ihv[4];
 147
 148        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 0);
 149        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 1);
 150        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 2);
 151        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 3);
 152        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 4);
 153        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 5);
 154        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 6);
 155        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 7);
 156        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 8);
 157        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 9);
 158        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 10);
 159        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 11);
 160        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 12);
 161        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 13);
 162        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 14);
 163        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 15);
 164        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 16);
 165        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 17);
 166        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 18);
 167        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 19);
 168
 169        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 20);
 170        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 21);
 171        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 22);
 172        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 23);
 173        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 24);
 174        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 25);
 175        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 26);
 176        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 27);
 177        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 28);
 178        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 29);
 179        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 30);
 180        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 31);
 181        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 32);
 182        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 33);
 183        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 34);
 184        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 35);
 185        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 36);
 186        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 37);
 187        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 38);
 188        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 39);
 189
 190        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 40);
 191        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 41);
 192        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 42);
 193        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 43);
 194        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 44);
 195        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 45);
 196        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 46);
 197        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 47);
 198        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 48);
 199        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 49);
 200        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 50);
 201        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 51);
 202        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 52);
 203        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 53);
 204        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 54);
 205        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 55);
 206        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 56);
 207        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 57);
 208        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 58);
 209        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 59);
 210
 211        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 60);
 212        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 61);
 213        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 62);
 214        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 63);
 215        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 64);
 216        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 65);
 217        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 66);
 218        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 67);
 219        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 68);
 220        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 69);
 221        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 70);
 222        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 71);
 223        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 72);
 224        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 73);
 225        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 74);
 226        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 75);
 227        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 76);
 228        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 77);
 229        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 78);
 230        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 79);
 231
 232        ihv[0] += a; ihv[1] += b; ihv[2] += c; ihv[3] += d; ihv[4] += e;
 233}
 234#endif /*BUILDNOCOLLDETECTSHA1COMPRESSION*/
 235
 236
 237static void sha1_compression_W(uint32_t ihv[5], const uint32_t W[80])
 238{
 239        uint32_t a = ihv[0], b = ihv[1], c = ihv[2], d = ihv[3], e = ihv[4];
 240
 241        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 0);
 242        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 1);
 243        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 2);
 244        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 3);
 245        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 4);
 246        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 5);
 247        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 6);
 248        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 7);
 249        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 8);
 250        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 9);
 251        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 10);
 252        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 11);
 253        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 12);
 254        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 13);
 255        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 14);
 256        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, W, 15);
 257        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, W, 16);
 258        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, W, 17);
 259        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, W, 18);
 260        HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, W, 19);
 261
 262        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 20);
 263        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 21);
 264        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 22);
 265        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 23);
 266        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 24);
 267        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 25);
 268        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 26);
 269        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 27);
 270        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 28);
 271        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 29);
 272        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 30);
 273        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 31);
 274        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 32);
 275        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 33);
 276        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 34);
 277        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, W, 35);
 278        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, W, 36);
 279        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, W, 37);
 280        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, W, 38);
 281        HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, W, 39);
 282
 283        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 40);
 284        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 41);
 285        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 42);
 286        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 43);
 287        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 44);
 288        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 45);
 289        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 46);
 290        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 47);
 291        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 48);
 292        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 49);
 293        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 50);
 294        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 51);
 295        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 52);
 296        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 53);
 297        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 54);
 298        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, W, 55);
 299        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, W, 56);
 300        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, W, 57);
 301        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, W, 58);
 302        HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, W, 59);
 303
 304        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 60);
 305        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 61);
 306        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 62);
 307        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 63);
 308        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 64);
 309        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 65);
 310        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 66);
 311        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 67);
 312        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 68);
 313        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 69);
 314        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 70);
 315        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 71);
 316        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 72);
 317        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 73);
 318        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 74);
 319        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, W, 75);
 320        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, W, 76);
 321        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, W, 77);
 322        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, W, 78);
 323        HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, W, 79);
 324
 325        ihv[0] += a; ihv[1] += b; ihv[2] += c; ihv[3] += d; ihv[4] += e;
 326}
 327
 328
 329
 330void sha1_compression_states(uint32_t ihv[5], const uint32_t m[16], uint32_t W[80], uint32_t states[80][5])
 331{
 332        uint32_t a = ihv[0], b = ihv[1], c = ihv[2], d = ihv[3], e = ihv[4];
 333        uint32_t temp;
 334
 335#ifdef DOSTORESTATE00
 336        SHA1_STORE_STATE(0)
 337#endif
 338        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(a, b, c, d, e, m, W, 0, temp);
 339
 340#ifdef DOSTORESTATE01
 341        SHA1_STORE_STATE(1)
 342#endif
 343        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(e, a, b, c, d, m, W, 1, temp);
 344
 345#ifdef DOSTORESTATE02
 346        SHA1_STORE_STATE(2)
 347#endif
 348        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(d, e, a, b, c, m, W, 2, temp);
 349
 350#ifdef DOSTORESTATE03
 351        SHA1_STORE_STATE(3)
 352#endif
 353        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(c, d, e, a, b, m, W, 3, temp);
 354
 355#ifdef DOSTORESTATE04
 356        SHA1_STORE_STATE(4)
 357#endif
 358        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(b, c, d, e, a, m, W, 4, temp);
 359
 360#ifdef DOSTORESTATE05
 361        SHA1_STORE_STATE(5)
 362#endif
 363        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(a, b, c, d, e, m, W, 5, temp);
 364
 365#ifdef DOSTORESTATE06
 366        SHA1_STORE_STATE(6)
 367#endif
 368        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(e, a, b, c, d, m, W, 6, temp);
 369
 370#ifdef DOSTORESTATE07
 371        SHA1_STORE_STATE(7)
 372#endif
 373        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(d, e, a, b, c, m, W, 7, temp);
 374
 375#ifdef DOSTORESTATE08
 376        SHA1_STORE_STATE(8)
 377#endif
 378        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(c, d, e, a, b, m, W, 8, temp);
 379
 380#ifdef DOSTORESTATE09
 381        SHA1_STORE_STATE(9)
 382#endif
 383        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(b, c, d, e, a, m, W, 9, temp);
 384
 385#ifdef DOSTORESTATE10
 386        SHA1_STORE_STATE(10)
 387#endif
 388        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(a, b, c, d, e, m, W, 10, temp);
 389
 390#ifdef DOSTORESTATE11
 391        SHA1_STORE_STATE(11)
 392#endif
 393        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(e, a, b, c, d, m, W, 11, temp);
 394
 395#ifdef DOSTORESTATE12
 396        SHA1_STORE_STATE(12)
 397#endif
 398        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(d, e, a, b, c, m, W, 12, temp);
 399
 400#ifdef DOSTORESTATE13
 401        SHA1_STORE_STATE(13)
 402#endif
 403        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(c, d, e, a, b, m, W, 13, temp);
 404
 405#ifdef DOSTORESTATE14
 406        SHA1_STORE_STATE(14)
 407#endif
 408        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(b, c, d, e, a, m, W, 14, temp);
 409
 410#ifdef DOSTORESTATE15
 411        SHA1_STORE_STATE(15)
 412#endif
 413        SHA1COMPRESS_FULL_ROUND1_STEP_LOAD(a, b, c, d, e, m, W, 15, temp);
 414
 415#ifdef DOSTORESTATE16
 416        SHA1_STORE_STATE(16)
 417#endif
 418        SHA1COMPRESS_FULL_ROUND1_STEP_EXPAND(e, a, b, c, d, W, 16, temp);
 419
 420#ifdef DOSTORESTATE17
 421        SHA1_STORE_STATE(17)
 422#endif
 423        SHA1COMPRESS_FULL_ROUND1_STEP_EXPAND(d, e, a, b, c, W, 17, temp);
 424
 425#ifdef DOSTORESTATE18
 426        SHA1_STORE_STATE(18)
 427#endif
 428        SHA1COMPRESS_FULL_ROUND1_STEP_EXPAND(c, d, e, a, b, W, 18, temp);
 429
 430#ifdef DOSTORESTATE19
 431        SHA1_STORE_STATE(19)
 432#endif
 433        SHA1COMPRESS_FULL_ROUND1_STEP_EXPAND(b, c, d, e, a, W, 19, temp);
 434
 435
 436
 437#ifdef DOSTORESTATE20
 438        SHA1_STORE_STATE(20)
 439#endif
 440        SHA1COMPRESS_FULL_ROUND2_STEP(a, b, c, d, e, W, 20, temp);
 441
 442#ifdef DOSTORESTATE21
 443        SHA1_STORE_STATE(21)
 444#endif
 445        SHA1COMPRESS_FULL_ROUND2_STEP(e, a, b, c, d, W, 21, temp);
 446
 447#ifdef DOSTORESTATE22
 448        SHA1_STORE_STATE(22)
 449#endif
 450        SHA1COMPRESS_FULL_ROUND2_STEP(d, e, a, b, c, W, 22, temp);
 451
 452#ifdef DOSTORESTATE23
 453        SHA1_STORE_STATE(23)
 454#endif
 455        SHA1COMPRESS_FULL_ROUND2_STEP(c, d, e, a, b, W, 23, temp);
 456
 457#ifdef DOSTORESTATE24
 458        SHA1_STORE_STATE(24)
 459#endif
 460        SHA1COMPRESS_FULL_ROUND2_STEP(b, c, d, e, a, W, 24, temp);
 461
 462#ifdef DOSTORESTATE25
 463        SHA1_STORE_STATE(25)
 464#endif
 465        SHA1COMPRESS_FULL_ROUND2_STEP(a, b, c, d, e, W, 25, temp);
 466
 467#ifdef DOSTORESTATE26
 468        SHA1_STORE_STATE(26)
 469#endif
 470        SHA1COMPRESS_FULL_ROUND2_STEP(e, a, b, c, d, W, 26, temp);
 471
 472#ifdef DOSTORESTATE27
 473        SHA1_STORE_STATE(27)
 474#endif
 475        SHA1COMPRESS_FULL_ROUND2_STEP(d, e, a, b, c, W, 27, temp);
 476
 477#ifdef DOSTORESTATE28
 478        SHA1_STORE_STATE(28)
 479#endif
 480        SHA1COMPRESS_FULL_ROUND2_STEP(c, d, e, a, b, W, 28, temp);
 481
 482#ifdef DOSTORESTATE29
 483        SHA1_STORE_STATE(29)
 484#endif
 485        SHA1COMPRESS_FULL_ROUND2_STEP(b, c, d, e, a, W, 29, temp);
 486
 487#ifdef DOSTORESTATE30
 488        SHA1_STORE_STATE(30)
 489#endif
 490        SHA1COMPRESS_FULL_ROUND2_STEP(a, b, c, d, e, W, 30, temp);
 491
 492#ifdef DOSTORESTATE31
 493        SHA1_STORE_STATE(31)
 494#endif
 495        SHA1COMPRESS_FULL_ROUND2_STEP(e, a, b, c, d, W, 31, temp);
 496
 497#ifdef DOSTORESTATE32
 498        SHA1_STORE_STATE(32)
 499#endif
 500        SHA1COMPRESS_FULL_ROUND2_STEP(d, e, a, b, c, W, 32, temp);
 501
 502#ifdef DOSTORESTATE33
 503        SHA1_STORE_STATE(33)
 504#endif
 505        SHA1COMPRESS_FULL_ROUND2_STEP(c, d, e, a, b, W, 33, temp);
 506
 507#ifdef DOSTORESTATE34
 508        SHA1_STORE_STATE(34)
 509#endif
 510        SHA1COMPRESS_FULL_ROUND2_STEP(b, c, d, e, a, W, 34, temp);
 511
 512#ifdef DOSTORESTATE35
 513        SHA1_STORE_STATE(35)
 514#endif
 515        SHA1COMPRESS_FULL_ROUND2_STEP(a, b, c, d, e, W, 35, temp);
 516
 517#ifdef DOSTORESTATE36
 518        SHA1_STORE_STATE(36)
 519#endif
 520        SHA1COMPRESS_FULL_ROUND2_STEP(e, a, b, c, d, W, 36, temp);
 521
 522#ifdef DOSTORESTATE37
 523        SHA1_STORE_STATE(37)
 524#endif
 525        SHA1COMPRESS_FULL_ROUND2_STEP(d, e, a, b, c, W, 37, temp);
 526
 527#ifdef DOSTORESTATE38
 528        SHA1_STORE_STATE(38)
 529#endif
 530        SHA1COMPRESS_FULL_ROUND2_STEP(c, d, e, a, b, W, 38, temp);
 531
 532#ifdef DOSTORESTATE39
 533        SHA1_STORE_STATE(39)
 534#endif
 535        SHA1COMPRESS_FULL_ROUND2_STEP(b, c, d, e, a, W, 39, temp);
 536
 537
 538
 539#ifdef DOSTORESTATE40
 540        SHA1_STORE_STATE(40)
 541#endif
 542        SHA1COMPRESS_FULL_ROUND3_STEP(a, b, c, d, e, W, 40, temp);
 543
 544#ifdef DOSTORESTATE41
 545        SHA1_STORE_STATE(41)
 546#endif
 547        SHA1COMPRESS_FULL_ROUND3_STEP(e, a, b, c, d, W, 41, temp);
 548
 549#ifdef DOSTORESTATE42
 550        SHA1_STORE_STATE(42)
 551#endif
 552        SHA1COMPRESS_FULL_ROUND3_STEP(d, e, a, b, c, W, 42, temp);
 553
 554#ifdef DOSTORESTATE43
 555        SHA1_STORE_STATE(43)
 556#endif
 557        SHA1COMPRESS_FULL_ROUND3_STEP(c, d, e, a, b, W, 43, temp);
 558
 559#ifdef DOSTORESTATE44
 560        SHA1_STORE_STATE(44)
 561#endif
 562        SHA1COMPRESS_FULL_ROUND3_STEP(b, c, d, e, a, W, 44, temp);
 563
 564#ifdef DOSTORESTATE45
 565        SHA1_STORE_STATE(45)
 566#endif
 567        SHA1COMPRESS_FULL_ROUND3_STEP(a, b, c, d, e, W, 45, temp);
 568
 569#ifdef DOSTORESTATE46
 570        SHA1_STORE_STATE(46)
 571#endif
 572        SHA1COMPRESS_FULL_ROUND3_STEP(e, a, b, c, d, W, 46, temp);
 573
 574#ifdef DOSTORESTATE47
 575        SHA1_STORE_STATE(47)
 576#endif
 577        SHA1COMPRESS_FULL_ROUND3_STEP(d, e, a, b, c, W, 47, temp);
 578
 579#ifdef DOSTORESTATE48
 580        SHA1_STORE_STATE(48)
 581#endif
 582        SHA1COMPRESS_FULL_ROUND3_STEP(c, d, e, a, b, W, 48, temp);
 583
 584#ifdef DOSTORESTATE49
 585        SHA1_STORE_STATE(49)
 586#endif
 587        SHA1COMPRESS_FULL_ROUND3_STEP(b, c, d, e, a, W, 49, temp);
 588
 589#ifdef DOSTORESTATE50
 590        SHA1_STORE_STATE(50)
 591#endif
 592        SHA1COMPRESS_FULL_ROUND3_STEP(a, b, c, d, e, W, 50, temp);
 593
 594#ifdef DOSTORESTATE51
 595        SHA1_STORE_STATE(51)
 596#endif
 597        SHA1COMPRESS_FULL_ROUND3_STEP(e, a, b, c, d, W, 51, temp);
 598
 599#ifdef DOSTORESTATE52
 600        SHA1_STORE_STATE(52)
 601#endif
 602        SHA1COMPRESS_FULL_ROUND3_STEP(d, e, a, b, c, W, 52, temp);
 603
 604#ifdef DOSTORESTATE53
 605        SHA1_STORE_STATE(53)
 606#endif
 607        SHA1COMPRESS_FULL_ROUND3_STEP(c, d, e, a, b, W, 53, temp);
 608
 609#ifdef DOSTORESTATE54
 610        SHA1_STORE_STATE(54)
 611#endif
 612        SHA1COMPRESS_FULL_ROUND3_STEP(b, c, d, e, a, W, 54, temp);
 613
 614#ifdef DOSTORESTATE55
 615        SHA1_STORE_STATE(55)
 616#endif
 617        SHA1COMPRESS_FULL_ROUND3_STEP(a, b, c, d, e, W, 55, temp);
 618
 619#ifdef DOSTORESTATE56
 620        SHA1_STORE_STATE(56)
 621#endif
 622        SHA1COMPRESS_FULL_ROUND3_STEP(e, a, b, c, d, W, 56, temp);
 623
 624#ifdef DOSTORESTATE57
 625        SHA1_STORE_STATE(57)
 626#endif
 627        SHA1COMPRESS_FULL_ROUND3_STEP(d, e, a, b, c, W, 57, temp);
 628
 629#ifdef DOSTORESTATE58
 630        SHA1_STORE_STATE(58)
 631#endif
 632        SHA1COMPRESS_FULL_ROUND3_STEP(c, d, e, a, b, W, 58, temp);
 633
 634#ifdef DOSTORESTATE59
 635        SHA1_STORE_STATE(59)
 636#endif
 637        SHA1COMPRESS_FULL_ROUND3_STEP(b, c, d, e, a, W, 59, temp);
 638
 639
 640
 641
 642#ifdef DOSTORESTATE60
 643        SHA1_STORE_STATE(60)
 644#endif
 645        SHA1COMPRESS_FULL_ROUND4_STEP(a, b, c, d, e, W, 60, temp);
 646
 647#ifdef DOSTORESTATE61
 648        SHA1_STORE_STATE(61)
 649#endif
 650        SHA1COMPRESS_FULL_ROUND4_STEP(e, a, b, c, d, W, 61, temp);
 651
 652#ifdef DOSTORESTATE62
 653        SHA1_STORE_STATE(62)
 654#endif
 655        SHA1COMPRESS_FULL_ROUND4_STEP(d, e, a, b, c, W, 62, temp);
 656
 657#ifdef DOSTORESTATE63
 658        SHA1_STORE_STATE(63)
 659#endif
 660        SHA1COMPRESS_FULL_ROUND4_STEP(c, d, e, a, b, W, 63, temp);
 661
 662#ifdef DOSTORESTATE64
 663        SHA1_STORE_STATE(64)
 664#endif
 665        SHA1COMPRESS_FULL_ROUND4_STEP(b, c, d, e, a, W, 64, temp);
 666
 667#ifdef DOSTORESTATE65
 668        SHA1_STORE_STATE(65)
 669#endif
 670        SHA1COMPRESS_FULL_ROUND4_STEP(a, b, c, d, e, W, 65, temp);
 671
 672#ifdef DOSTORESTATE66
 673        SHA1_STORE_STATE(66)
 674#endif
 675        SHA1COMPRESS_FULL_ROUND4_STEP(e, a, b, c, d, W, 66, temp);
 676
 677#ifdef DOSTORESTATE67
 678        SHA1_STORE_STATE(67)
 679#endif
 680        SHA1COMPRESS_FULL_ROUND4_STEP(d, e, a, b, c, W, 67, temp);
 681
 682#ifdef DOSTORESTATE68
 683        SHA1_STORE_STATE(68)
 684#endif
 685        SHA1COMPRESS_FULL_ROUND4_STEP(c, d, e, a, b, W, 68, temp);
 686
 687#ifdef DOSTORESTATE69
 688        SHA1_STORE_STATE(69)
 689#endif
 690        SHA1COMPRESS_FULL_ROUND4_STEP(b, c, d, e, a, W, 69, temp);
 691
 692#ifdef DOSTORESTATE70
 693        SHA1_STORE_STATE(70)
 694#endif
 695        SHA1COMPRESS_FULL_ROUND4_STEP(a, b, c, d, e, W, 70, temp);
 696
 697#ifdef DOSTORESTATE71
 698        SHA1_STORE_STATE(71)
 699#endif
 700        SHA1COMPRESS_FULL_ROUND4_STEP(e, a, b, c, d, W, 71, temp);
 701
 702#ifdef DOSTORESTATE72
 703        SHA1_STORE_STATE(72)
 704#endif
 705        SHA1COMPRESS_FULL_ROUND4_STEP(d, e, a, b, c, W, 72, temp);
 706
 707#ifdef DOSTORESTATE73
 708        SHA1_STORE_STATE(73)
 709#endif
 710        SHA1COMPRESS_FULL_ROUND4_STEP(c, d, e, a, b, W, 73, temp);
 711
 712#ifdef DOSTORESTATE74
 713        SHA1_STORE_STATE(74)
 714#endif
 715        SHA1COMPRESS_FULL_ROUND4_STEP(b, c, d, e, a, W, 74, temp);
 716
 717#ifdef DOSTORESTATE75
 718        SHA1_STORE_STATE(75)
 719#endif
 720        SHA1COMPRESS_FULL_ROUND4_STEP(a, b, c, d, e, W, 75, temp);
 721
 722#ifdef DOSTORESTATE76
 723        SHA1_STORE_STATE(76)
 724#endif
 725        SHA1COMPRESS_FULL_ROUND4_STEP(e, a, b, c, d, W, 76, temp);
 726
 727#ifdef DOSTORESTATE77
 728        SHA1_STORE_STATE(77)
 729#endif
 730        SHA1COMPRESS_FULL_ROUND4_STEP(d, e, a, b, c, W, 77, temp);
 731
 732#ifdef DOSTORESTATE78
 733        SHA1_STORE_STATE(78)
 734#endif
 735        SHA1COMPRESS_FULL_ROUND4_STEP(c, d, e, a, b, W, 78, temp);
 736
 737#ifdef DOSTORESTATE79
 738        SHA1_STORE_STATE(79)
 739#endif
 740        SHA1COMPRESS_FULL_ROUND4_STEP(b, c, d, e, a, W, 79, temp);
 741
 742
 743
 744        ihv[0] += a; ihv[1] += b; ihv[2] += c; ihv[3] += d; ihv[4] += e;
 745}
 746
 747
 748
 749
 750#define SHA1_RECOMPRESS(t) \
 751static void sha1recompress_fast_ ## t (uint32_t ihvin[5], uint32_t ihvout[5], const uint32_t me2[80], const uint32_t state[5]) \
 752{ \
 753        uint32_t a = state[0], b = state[1], c = state[2], d = state[3], e = state[4]; \
 754        if (t > 79) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(b, c, d, e, a, me2, 79); \
 755        if (t > 78) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(c, d, e, a, b, me2, 78); \
 756        if (t > 77) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(d, e, a, b, c, me2, 77); \
 757        if (t > 76) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(e, a, b, c, d, me2, 76); \
 758        if (t > 75) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(a, b, c, d, e, me2, 75); \
 759        if (t > 74) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(b, c, d, e, a, me2, 74); \
 760        if (t > 73) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(c, d, e, a, b, me2, 73); \
 761        if (t > 72) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(d, e, a, b, c, me2, 72); \
 762        if (t > 71) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(e, a, b, c, d, me2, 71); \
 763        if (t > 70) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(a, b, c, d, e, me2, 70); \
 764        if (t > 69) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(b, c, d, e, a, me2, 69); \
 765        if (t > 68) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(c, d, e, a, b, me2, 68); \
 766        if (t > 67) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(d, e, a, b, c, me2, 67); \
 767        if (t > 66) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(e, a, b, c, d, me2, 66); \
 768        if (t > 65) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(a, b, c, d, e, me2, 65); \
 769        if (t > 64) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(b, c, d, e, a, me2, 64); \
 770        if (t > 63) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(c, d, e, a, b, me2, 63); \
 771        if (t > 62) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(d, e, a, b, c, me2, 62); \
 772        if (t > 61) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(e, a, b, c, d, me2, 61); \
 773        if (t > 60) HASHCLASH_SHA1COMPRESS_ROUND4_STEP_BW(a, b, c, d, e, me2, 60); \
 774        if (t > 59) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(b, c, d, e, a, me2, 59); \
 775        if (t > 58) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(c, d, e, a, b, me2, 58); \
 776        if (t > 57) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(d, e, a, b, c, me2, 57); \
 777        if (t > 56) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(e, a, b, c, d, me2, 56); \
 778        if (t > 55) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(a, b, c, d, e, me2, 55); \
 779        if (t > 54) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(b, c, d, e, a, me2, 54); \
 780        if (t > 53) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(c, d, e, a, b, me2, 53); \
 781        if (t > 52) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(d, e, a, b, c, me2, 52); \
 782        if (t > 51) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(e, a, b, c, d, me2, 51); \
 783        if (t > 50) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(a, b, c, d, e, me2, 50); \
 784        if (t > 49) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(b, c, d, e, a, me2, 49); \
 785        if (t > 48) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(c, d, e, a, b, me2, 48); \
 786        if (t > 47) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(d, e, a, b, c, me2, 47); \
 787        if (t > 46) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(e, a, b, c, d, me2, 46); \
 788        if (t > 45) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(a, b, c, d, e, me2, 45); \
 789        if (t > 44) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(b, c, d, e, a, me2, 44); \
 790        if (t > 43) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(c, d, e, a, b, me2, 43); \
 791        if (t > 42) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(d, e, a, b, c, me2, 42); \
 792        if (t > 41) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(e, a, b, c, d, me2, 41); \
 793        if (t > 40) HASHCLASH_SHA1COMPRESS_ROUND3_STEP_BW(a, b, c, d, e, me2, 40); \
 794        if (t > 39) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(b, c, d, e, a, me2, 39); \
 795        if (t > 38) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(c, d, e, a, b, me2, 38); \
 796        if (t > 37) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(d, e, a, b, c, me2, 37); \
 797        if (t > 36) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(e, a, b, c, d, me2, 36); \
 798        if (t > 35) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(a, b, c, d, e, me2, 35); \
 799        if (t > 34) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(b, c, d, e, a, me2, 34); \
 800        if (t > 33) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(c, d, e, a, b, me2, 33); \
 801        if (t > 32) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(d, e, a, b, c, me2, 32); \
 802        if (t > 31) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(e, a, b, c, d, me2, 31); \
 803        if (t > 30) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(a, b, c, d, e, me2, 30); \
 804        if (t > 29) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(b, c, d, e, a, me2, 29); \
 805        if (t > 28) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(c, d, e, a, b, me2, 28); \
 806        if (t > 27) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(d, e, a, b, c, me2, 27); \
 807        if (t > 26) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(e, a, b, c, d, me2, 26); \
 808        if (t > 25) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(a, b, c, d, e, me2, 25); \
 809        if (t > 24) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(b, c, d, e, a, me2, 24); \
 810        if (t > 23) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(c, d, e, a, b, me2, 23); \
 811        if (t > 22) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(d, e, a, b, c, me2, 22); \
 812        if (t > 21) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(e, a, b, c, d, me2, 21); \
 813        if (t > 20) HASHCLASH_SHA1COMPRESS_ROUND2_STEP_BW(a, b, c, d, e, me2, 20); \
 814        if (t > 19) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(b, c, d, e, a, me2, 19); \
 815        if (t > 18) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(c, d, e, a, b, me2, 18); \
 816        if (t > 17) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(d, e, a, b, c, me2, 17); \
 817        if (t > 16) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(e, a, b, c, d, me2, 16); \
 818        if (t > 15) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(a, b, c, d, e, me2, 15); \
 819        if (t > 14) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(b, c, d, e, a, me2, 14); \
 820        if (t > 13) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(c, d, e, a, b, me2, 13); \
 821        if (t > 12) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(d, e, a, b, c, me2, 12); \
 822        if (t > 11) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(e, a, b, c, d, me2, 11); \
 823        if (t > 10) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(a, b, c, d, e, me2, 10); \
 824        if (t > 9) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(b, c, d, e, a, me2, 9); \
 825        if (t > 8) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(c, d, e, a, b, me2, 8); \
 826        if (t > 7) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(d, e, a, b, c, me2, 7); \
 827        if (t > 6) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(e, a, b, c, d, me2, 6); \
 828        if (t > 5) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(a, b, c, d, e, me2, 5); \
 829        if (t > 4) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(b, c, d, e, a, me2, 4); \
 830        if (t > 3) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(c, d, e, a, b, me2, 3); \
 831        if (t > 2) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(d, e, a, b, c, me2, 2); \
 832        if (t > 1) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(e, a, b, c, d, me2, 1); \
 833        if (t > 0) HASHCLASH_SHA1COMPRESS_ROUND1_STEP_BW(a, b, c, d, e, me2, 0); \
 834        ihvin[0] = a; ihvin[1] = b; ihvin[2] = c; ihvin[3] = d; ihvin[4] = e; \
 835        a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4]; \
 836        if (t <= 0) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, me2, 0); \
 837        if (t <= 1) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, me2, 1); \
 838        if (t <= 2) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, me2, 2); \
 839        if (t <= 3) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, me2, 3); \
 840        if (t <= 4) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, me2, 4); \
 841        if (t <= 5) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, me2, 5); \
 842        if (t <= 6) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, me2, 6); \
 843        if (t <= 7) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, me2, 7); \
 844        if (t <= 8) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, me2, 8); \
 845        if (t <= 9) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, me2, 9); \
 846        if (t <= 10) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, me2, 10); \
 847        if (t <= 11) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, me2, 11); \
 848        if (t <= 12) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, me2, 12); \
 849        if (t <= 13) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, me2, 13); \
 850        if (t <= 14) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, me2, 14); \
 851        if (t <= 15) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(a, b, c, d, e, me2, 15); \
 852        if (t <= 16) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(e, a, b, c, d, me2, 16); \
 853        if (t <= 17) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(d, e, a, b, c, me2, 17); \
 854        if (t <= 18) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(c, d, e, a, b, me2, 18); \
 855        if (t <= 19) HASHCLASH_SHA1COMPRESS_ROUND1_STEP(b, c, d, e, a, me2, 19); \
 856        if (t <= 20) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, me2, 20); \
 857        if (t <= 21) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, me2, 21); \
 858        if (t <= 22) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, me2, 22); \
 859        if (t <= 23) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, me2, 23); \
 860        if (t <= 24) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, me2, 24); \
 861        if (t <= 25) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, me2, 25); \
 862        if (t <= 26) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, me2, 26); \
 863        if (t <= 27) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, me2, 27); \
 864        if (t <= 28) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, me2, 28); \
 865        if (t <= 29) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, me2, 29); \
 866        if (t <= 30) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, me2, 30); \
 867        if (t <= 31) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, me2, 31); \
 868        if (t <= 32) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, me2, 32); \
 869        if (t <= 33) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, me2, 33); \
 870        if (t <= 34) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, me2, 34); \
 871        if (t <= 35) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(a, b, c, d, e, me2, 35); \
 872        if (t <= 36) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(e, a, b, c, d, me2, 36); \
 873        if (t <= 37) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(d, e, a, b, c, me2, 37); \
 874        if (t <= 38) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(c, d, e, a, b, me2, 38); \
 875        if (t <= 39) HASHCLASH_SHA1COMPRESS_ROUND2_STEP(b, c, d, e, a, me2, 39); \
 876        if (t <= 40) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, me2, 40); \
 877        if (t <= 41) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, me2, 41); \
 878        if (t <= 42) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, me2, 42); \
 879        if (t <= 43) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, me2, 43); \
 880        if (t <= 44) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, me2, 44); \
 881        if (t <= 45) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, me2, 45); \
 882        if (t <= 46) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, me2, 46); \
 883        if (t <= 47) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, me2, 47); \
 884        if (t <= 48) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, me2, 48); \
 885        if (t <= 49) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, me2, 49); \
 886        if (t <= 50) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, me2, 50); \
 887        if (t <= 51) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, me2, 51); \
 888        if (t <= 52) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, me2, 52); \
 889        if (t <= 53) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, me2, 53); \
 890        if (t <= 54) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, me2, 54); \
 891        if (t <= 55) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(a, b, c, d, e, me2, 55); \
 892        if (t <= 56) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(e, a, b, c, d, me2, 56); \
 893        if (t <= 57) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(d, e, a, b, c, me2, 57); \
 894        if (t <= 58) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(c, d, e, a, b, me2, 58); \
 895        if (t <= 59) HASHCLASH_SHA1COMPRESS_ROUND3_STEP(b, c, d, e, a, me2, 59); \
 896        if (t <= 60) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, me2, 60); \
 897        if (t <= 61) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, me2, 61); \
 898        if (t <= 62) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, me2, 62); \
 899        if (t <= 63) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, me2, 63); \
 900        if (t <= 64) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, me2, 64); \
 901        if (t <= 65) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, me2, 65); \
 902        if (t <= 66) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, me2, 66); \
 903        if (t <= 67) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, me2, 67); \
 904        if (t <= 68) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, me2, 68); \
 905        if (t <= 69) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, me2, 69); \
 906        if (t <= 70) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, me2, 70); \
 907        if (t <= 71) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, me2, 71); \
 908        if (t <= 72) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, me2, 72); \
 909        if (t <= 73) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, me2, 73); \
 910        if (t <= 74) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, me2, 74); \
 911        if (t <= 75) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(a, b, c, d, e, me2, 75); \
 912        if (t <= 76) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(e, a, b, c, d, me2, 76); \
 913        if (t <= 77) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(d, e, a, b, c, me2, 77); \
 914        if (t <= 78) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(c, d, e, a, b, me2, 78); \
 915        if (t <= 79) HASHCLASH_SHA1COMPRESS_ROUND4_STEP(b, c, d, e, a, me2, 79); \
 916        ihvout[0] = ihvin[0] + a; ihvout[1] = ihvin[1] + b; ihvout[2] = ihvin[2] + c; ihvout[3] = ihvin[3] + d; ihvout[4] = ihvin[4] + e; \
 917}
 918
 919#ifdef _MSC_VER
 920#pragma warning(push)
 921#pragma warning(disable: 4127)  /* Complier complains about the checks in the above macro being constant. */
 922#endif
 923
 924#ifdef DOSTORESTATE0
 925SHA1_RECOMPRESS(0)
 926#endif
 927
 928#ifdef DOSTORESTATE1
 929SHA1_RECOMPRESS(1)
 930#endif
 931
 932#ifdef DOSTORESTATE2
 933SHA1_RECOMPRESS(2)
 934#endif
 935
 936#ifdef DOSTORESTATE3
 937SHA1_RECOMPRESS(3)
 938#endif
 939
 940#ifdef DOSTORESTATE4
 941SHA1_RECOMPRESS(4)
 942#endif
 943
 944#ifdef DOSTORESTATE5
 945SHA1_RECOMPRESS(5)
 946#endif
 947
 948#ifdef DOSTORESTATE6
 949SHA1_RECOMPRESS(6)
 950#endif
 951
 952#ifdef DOSTORESTATE7
 953SHA1_RECOMPRESS(7)
 954#endif
 955
 956#ifdef DOSTORESTATE8
 957SHA1_RECOMPRESS(8)
 958#endif
 959
 960#ifdef DOSTORESTATE9
 961SHA1_RECOMPRESS(9)
 962#endif
 963
 964#ifdef DOSTORESTATE10
 965SHA1_RECOMPRESS(10)
 966#endif
 967
 968#ifdef DOSTORESTATE11
 969SHA1_RECOMPRESS(11)
 970#endif
 971
 972#ifdef DOSTORESTATE12
 973SHA1_RECOMPRESS(12)
 974#endif
 975
 976#ifdef DOSTORESTATE13
 977SHA1_RECOMPRESS(13)
 978#endif
 979
 980#ifdef DOSTORESTATE14
 981SHA1_RECOMPRESS(14)
 982#endif
 983
 984#ifdef DOSTORESTATE15
 985SHA1_RECOMPRESS(15)
 986#endif
 987
 988#ifdef DOSTORESTATE16
 989SHA1_RECOMPRESS(16)
 990#endif
 991
 992#ifdef DOSTORESTATE17
 993SHA1_RECOMPRESS(17)
 994#endif
 995
 996#ifdef DOSTORESTATE18
 997SHA1_RECOMPRESS(18)
 998#endif
 999
1000#ifdef DOSTORESTATE19
1001SHA1_RECOMPRESS(19)
1002#endif
1003
1004#ifdef DOSTORESTATE20
1005SHA1_RECOMPRESS(20)
1006#endif
1007
1008#ifdef DOSTORESTATE21
1009SHA1_RECOMPRESS(21)
1010#endif
1011
1012#ifdef DOSTORESTATE22
1013SHA1_RECOMPRESS(22)
1014#endif
1015
1016#ifdef DOSTORESTATE23
1017SHA1_RECOMPRESS(23)
1018#endif
1019
1020#ifdef DOSTORESTATE24
1021SHA1_RECOMPRESS(24)
1022#endif
1023
1024#ifdef DOSTORESTATE25
1025SHA1_RECOMPRESS(25)
1026#endif
1027
1028#ifdef DOSTORESTATE26
1029SHA1_RECOMPRESS(26)
1030#endif
1031
1032#ifdef DOSTORESTATE27
1033SHA1_RECOMPRESS(27)
1034#endif
1035
1036#ifdef DOSTORESTATE28
1037SHA1_RECOMPRESS(28)
1038#endif
1039
1040#ifdef DOSTORESTATE29
1041SHA1_RECOMPRESS(29)
1042#endif
1043
1044#ifdef DOSTORESTATE30
1045SHA1_RECOMPRESS(30)
1046#endif
1047
1048#ifdef DOSTORESTATE31
1049SHA1_RECOMPRESS(31)
1050#endif
1051
1052#ifdef DOSTORESTATE32
1053SHA1_RECOMPRESS(32)
1054#endif
1055
1056#ifdef DOSTORESTATE33
1057SHA1_RECOMPRESS(33)
1058#endif
1059
1060#ifdef DOSTORESTATE34
1061SHA1_RECOMPRESS(34)
1062#endif
1063
1064#ifdef DOSTORESTATE35
1065SHA1_RECOMPRESS(35)
1066#endif
1067
1068#ifdef DOSTORESTATE36
1069SHA1_RECOMPRESS(36)
1070#endif
1071
1072#ifdef DOSTORESTATE37
1073SHA1_RECOMPRESS(37)
1074#endif
1075
1076#ifdef DOSTORESTATE38
1077SHA1_RECOMPRESS(38)
1078#endif
1079
1080#ifdef DOSTORESTATE39
1081SHA1_RECOMPRESS(39)
1082#endif
1083
1084#ifdef DOSTORESTATE40
1085SHA1_RECOMPRESS(40)
1086#endif
1087
1088#ifdef DOSTORESTATE41
1089SHA1_RECOMPRESS(41)
1090#endif
1091
1092#ifdef DOSTORESTATE42
1093SHA1_RECOMPRESS(42)
1094#endif
1095
1096#ifdef DOSTORESTATE43
1097SHA1_RECOMPRESS(43)
1098#endif
1099
1100#ifdef DOSTORESTATE44
1101SHA1_RECOMPRESS(44)
1102#endif
1103
1104#ifdef DOSTORESTATE45
1105SHA1_RECOMPRESS(45)
1106#endif
1107
1108#ifdef DOSTORESTATE46
1109SHA1_RECOMPRESS(46)
1110#endif
1111
1112#ifdef DOSTORESTATE47
1113SHA1_RECOMPRESS(47)
1114#endif
1115
1116#ifdef DOSTORESTATE48
1117SHA1_RECOMPRESS(48)
1118#endif
1119
1120#ifdef DOSTORESTATE49
1121SHA1_RECOMPRESS(49)
1122#endif
1123
1124#ifdef DOSTORESTATE50
1125SHA1_RECOMPRESS(50)
1126#endif
1127
1128#ifdef DOSTORESTATE51
1129SHA1_RECOMPRESS(51)
1130#endif
1131
1132#ifdef DOSTORESTATE52
1133SHA1_RECOMPRESS(52)
1134#endif
1135
1136#ifdef DOSTORESTATE53
1137SHA1_RECOMPRESS(53)
1138#endif
1139
1140#ifdef DOSTORESTATE54
1141SHA1_RECOMPRESS(54)
1142#endif
1143
1144#ifdef DOSTORESTATE55
1145SHA1_RECOMPRESS(55)
1146#endif
1147
1148#ifdef DOSTORESTATE56
1149SHA1_RECOMPRESS(56)
1150#endif
1151
1152#ifdef DOSTORESTATE57
1153SHA1_RECOMPRESS(57)
1154#endif
1155
1156#ifdef DOSTORESTATE58
1157SHA1_RECOMPRESS(58)
1158#endif
1159
1160#ifdef DOSTORESTATE59
1161SHA1_RECOMPRESS(59)
1162#endif
1163
1164#ifdef DOSTORESTATE60
1165SHA1_RECOMPRESS(60)
1166#endif
1167
1168#ifdef DOSTORESTATE61
1169SHA1_RECOMPRESS(61)
1170#endif
1171
1172#ifdef DOSTORESTATE62
1173SHA1_RECOMPRESS(62)
1174#endif
1175
1176#ifdef DOSTORESTATE63
1177SHA1_RECOMPRESS(63)
1178#endif
1179
1180#ifdef DOSTORESTATE64
1181SHA1_RECOMPRESS(64)
1182#endif
1183
1184#ifdef DOSTORESTATE65
1185SHA1_RECOMPRESS(65)
1186#endif
1187
1188#ifdef DOSTORESTATE66
1189SHA1_RECOMPRESS(66)
1190#endif
1191
1192#ifdef DOSTORESTATE67
1193SHA1_RECOMPRESS(67)
1194#endif
1195
1196#ifdef DOSTORESTATE68
1197SHA1_RECOMPRESS(68)
1198#endif
1199
1200#ifdef DOSTORESTATE69
1201SHA1_RECOMPRESS(69)
1202#endif
1203
1204#ifdef DOSTORESTATE70
1205SHA1_RECOMPRESS(70)
1206#endif
1207
1208#ifdef DOSTORESTATE71
1209SHA1_RECOMPRESS(71)
1210#endif
1211
1212#ifdef DOSTORESTATE72
1213SHA1_RECOMPRESS(72)
1214#endif
1215
1216#ifdef DOSTORESTATE73
1217SHA1_RECOMPRESS(73)
1218#endif
1219
1220#ifdef DOSTORESTATE74
1221SHA1_RECOMPRESS(74)
1222#endif
1223
1224#ifdef DOSTORESTATE75
1225SHA1_RECOMPRESS(75)
1226#endif
1227
1228#ifdef DOSTORESTATE76
1229SHA1_RECOMPRESS(76)
1230#endif
1231
1232#ifdef DOSTORESTATE77
1233SHA1_RECOMPRESS(77)
1234#endif
1235
1236#ifdef DOSTORESTATE78
1237SHA1_RECOMPRESS(78)
1238#endif
1239
1240#ifdef DOSTORESTATE79
1241SHA1_RECOMPRESS(79)
1242#endif
1243
1244#ifdef _MSC_VER
1245#pragma warning(pop)
1246#endif
1247
1248static void sha1_recompression_step(uint32_t step, uint32_t ihvin[5], uint32_t ihvout[5], const uint32_t me2[80], const uint32_t state[5])
1249{
1250        switch (step)
1251        {
1252#ifdef DOSTORESTATE0
1253        case 0:
1254                sha1recompress_fast_0(ihvin, ihvout, me2, state);
1255                break;
1256#endif
1257#ifdef DOSTORESTATE1
1258        case 1:
1259                sha1recompress_fast_1(ihvin, ihvout, me2, state);
1260                break;
1261#endif
1262#ifdef DOSTORESTATE2
1263        case 2:
1264                sha1recompress_fast_2(ihvin, ihvout, me2, state);
1265                break;
1266#endif
1267#ifdef DOSTORESTATE3
1268        case 3:
1269                sha1recompress_fast_3(ihvin, ihvout, me2, state);
1270                break;
1271#endif
1272#ifdef DOSTORESTATE4
1273        case 4:
1274                sha1recompress_fast_4(ihvin, ihvout, me2, state);
1275                break;
1276#endif
1277#ifdef DOSTORESTATE5
1278        case 5:
1279                sha1recompress_fast_5(ihvin, ihvout, me2, state);
1280                break;
1281#endif
1282#ifdef DOSTORESTATE6
1283        case 6:
1284                sha1recompress_fast_6(ihvin, ihvout, me2, state);
1285                break;
1286#endif
1287#ifdef DOSTORESTATE7
1288        case 7:
1289                sha1recompress_fast_7(ihvin, ihvout, me2, state);
1290                break;
1291#endif
1292#ifdef DOSTORESTATE8
1293        case 8:
1294                sha1recompress_fast_8(ihvin, ihvout, me2, state);
1295                break;
1296#endif
1297#ifdef DOSTORESTATE9
1298        case 9:
1299                sha1recompress_fast_9(ihvin, ihvout, me2, state);
1300                break;
1301#endif
1302#ifdef DOSTORESTATE10
1303        case 10:
1304                sha1recompress_fast_10(ihvin, ihvout, me2, state);
1305                break;
1306#endif
1307#ifdef DOSTORESTATE11
1308        case 11:
1309                sha1recompress_fast_11(ihvin, ihvout, me2, state);
1310                break;
1311#endif
1312#ifdef DOSTORESTATE12
1313        case 12:
1314                sha1recompress_fast_12(ihvin, ihvout, me2, state);
1315                break;
1316#endif
1317#ifdef DOSTORESTATE13
1318        case 13:
1319                sha1recompress_fast_13(ihvin, ihvout, me2, state);
1320                break;
1321#endif
1322#ifdef DOSTORESTATE14
1323        case 14:
1324                sha1recompress_fast_14(ihvin, ihvout, me2, state);
1325                break;
1326#endif
1327#ifdef DOSTORESTATE15
1328        case 15:
1329                sha1recompress_fast_15(ihvin, ihvout, me2, state);
1330                break;
1331#endif
1332#ifdef DOSTORESTATE16
1333        case 16:
1334                sha1recompress_fast_16(ihvin, ihvout, me2, state);
1335                break;
1336#endif
1337#ifdef DOSTORESTATE17
1338        case 17:
1339                sha1recompress_fast_17(ihvin, ihvout, me2, state);
1340                break;
1341#endif
1342#ifdef DOSTORESTATE18
1343        case 18:
1344                sha1recompress_fast_18(ihvin, ihvout, me2, state);
1345                break;
1346#endif
1347#ifdef DOSTORESTATE19
1348        case 19:
1349                sha1recompress_fast_19(ihvin, ihvout, me2, state);
1350                break;
1351#endif
1352#ifdef DOSTORESTATE20
1353        case 20:
1354                sha1recompress_fast_20(ihvin, ihvout, me2, state);
1355                break;
1356#endif
1357#ifdef DOSTORESTATE21
1358        case 21:
1359                sha1recompress_fast_21(ihvin, ihvout, me2, state);
1360                break;
1361#endif
1362#ifdef DOSTORESTATE22
1363        case 22:
1364                sha1recompress_fast_22(ihvin, ihvout, me2, state);
1365                break;
1366#endif
1367#ifdef DOSTORESTATE23
1368        case 23:
1369                sha1recompress_fast_23(ihvin, ihvout, me2, state);
1370                break;
1371#endif
1372#ifdef DOSTORESTATE24
1373        case 24:
1374                sha1recompress_fast_24(ihvin, ihvout, me2, state);
1375                break;
1376#endif
1377#ifdef DOSTORESTATE25
1378        case 25:
1379                sha1recompress_fast_25(ihvin, ihvout, me2, state);
1380                break;
1381#endif
1382#ifdef DOSTORESTATE26
1383        case 26:
1384                sha1recompress_fast_26(ihvin, ihvout, me2, state);
1385                break;
1386#endif
1387#ifdef DOSTORESTATE27
1388        case 27:
1389                sha1recompress_fast_27(ihvin, ihvout, me2, state);
1390                break;
1391#endif
1392#ifdef DOSTORESTATE28
1393        case 28:
1394                sha1recompress_fast_28(ihvin, ihvout, me2, state);
1395                break;
1396#endif
1397#ifdef DOSTORESTATE29
1398        case 29:
1399                sha1recompress_fast_29(ihvin, ihvout, me2, state);
1400                break;
1401#endif
1402#ifdef DOSTORESTATE30
1403        case 30:
1404                sha1recompress_fast_30(ihvin, ihvout, me2, state);
1405                break;
1406#endif
1407#ifdef DOSTORESTATE31
1408        case 31:
1409                sha1recompress_fast_31(ihvin, ihvout, me2, state);
1410                break;
1411#endif
1412#ifdef DOSTORESTATE32
1413        case 32:
1414                sha1recompress_fast_32(ihvin, ihvout, me2, state);
1415                break;
1416#endif
1417#ifdef DOSTORESTATE33
1418        case 33:
1419                sha1recompress_fast_33(ihvin, ihvout, me2, state);
1420                break;
1421#endif
1422#ifdef DOSTORESTATE34
1423        case 34:
1424                sha1recompress_fast_34(ihvin, ihvout, me2, state);
1425                break;
1426#endif
1427#ifdef DOSTORESTATE35
1428        case 35:
1429                sha1recompress_fast_35(ihvin, ihvout, me2, state);
1430                break;
1431#endif
1432#ifdef DOSTORESTATE36
1433        case 36:
1434                sha1recompress_fast_36(ihvin, ihvout, me2, state);
1435                break;
1436#endif
1437#ifdef DOSTORESTATE37
1438        case 37:
1439                sha1recompress_fast_37(ihvin, ihvout, me2, state);
1440                break;
1441#endif
1442#ifdef DOSTORESTATE38
1443        case 38:
1444                sha1recompress_fast_38(ihvin, ihvout, me2, state);
1445                break;
1446#endif
1447#ifdef DOSTORESTATE39
1448        case 39:
1449                sha1recompress_fast_39(ihvin, ihvout, me2, state);
1450                break;
1451#endif
1452#ifdef DOSTORESTATE40
1453        case 40:
1454                sha1recompress_fast_40(ihvin, ihvout, me2, state);
1455                break;
1456#endif
1457#ifdef DOSTORESTATE41
1458        case 41:
1459                sha1recompress_fast_41(ihvin, ihvout, me2, state);
1460                break;
1461#endif
1462#ifdef DOSTORESTATE42
1463        case 42:
1464                sha1recompress_fast_42(ihvin, ihvout, me2, state);
1465                break;
1466#endif
1467#ifdef DOSTORESTATE43
1468        case 43:
1469                sha1recompress_fast_43(ihvin, ihvout, me2, state);
1470                break;
1471#endif
1472#ifdef DOSTORESTATE44
1473        case 44:
1474                sha1recompress_fast_44(ihvin, ihvout, me2, state);
1475                break;
1476#endif
1477#ifdef DOSTORESTATE45
1478        case 45:
1479                sha1recompress_fast_45(ihvin, ihvout, me2, state);
1480                break;
1481#endif
1482#ifdef DOSTORESTATE46
1483        case 46:
1484                sha1recompress_fast_46(ihvin, ihvout, me2, state);
1485                break;
1486#endif
1487#ifdef DOSTORESTATE47
1488        case 47:
1489                sha1recompress_fast_47(ihvin, ihvout, me2, state);
1490                break;
1491#endif
1492#ifdef DOSTORESTATE48
1493        case 48:
1494                sha1recompress_fast_48(ihvin, ihvout, me2, state);
1495                break;
1496#endif
1497#ifdef DOSTORESTATE49
1498        case 49:
1499                sha1recompress_fast_49(ihvin, ihvout, me2, state);
1500                break;
1501#endif
1502#ifdef DOSTORESTATE50
1503        case 50:
1504                sha1recompress_fast_50(ihvin, ihvout, me2, state);
1505                break;
1506#endif
1507#ifdef DOSTORESTATE51
1508        case 51:
1509                sha1recompress_fast_51(ihvin, ihvout, me2, state);
1510                break;
1511#endif
1512#ifdef DOSTORESTATE52
1513        case 52:
1514                sha1recompress_fast_52(ihvin, ihvout, me2, state);
1515                break;
1516#endif
1517#ifdef DOSTORESTATE53
1518        case 53:
1519                sha1recompress_fast_53(ihvin, ihvout, me2, state);
1520                break;
1521#endif
1522#ifdef DOSTORESTATE54
1523        case 54:
1524                sha1recompress_fast_54(ihvin, ihvout, me2, state);
1525                break;
1526#endif
1527#ifdef DOSTORESTATE55
1528        case 55:
1529                sha1recompress_fast_55(ihvin, ihvout, me2, state);
1530                break;
1531#endif
1532#ifdef DOSTORESTATE56
1533        case 56:
1534                sha1recompress_fast_56(ihvin, ihvout, me2, state);
1535                break;
1536#endif
1537#ifdef DOSTORESTATE57
1538        case 57:
1539                sha1recompress_fast_57(ihvin, ihvout, me2, state);
1540                break;
1541#endif
1542#ifdef DOSTORESTATE58
1543        case 58:
1544                sha1recompress_fast_58(ihvin, ihvout, me2, state);
1545                break;
1546#endif
1547#ifdef DOSTORESTATE59
1548        case 59:
1549                sha1recompress_fast_59(ihvin, ihvout, me2, state);
1550                break;
1551#endif
1552#ifdef DOSTORESTATE60
1553        case 60:
1554                sha1recompress_fast_60(ihvin, ihvout, me2, state);
1555                break;
1556#endif
1557#ifdef DOSTORESTATE61
1558        case 61:
1559                sha1recompress_fast_61(ihvin, ihvout, me2, state);
1560                break;
1561#endif
1562#ifdef DOSTORESTATE62
1563        case 62:
1564                sha1recompress_fast_62(ihvin, ihvout, me2, state);
1565                break;
1566#endif
1567#ifdef DOSTORESTATE63
1568        case 63:
1569                sha1recompress_fast_63(ihvin, ihvout, me2, state);
1570                break;
1571#endif
1572#ifdef DOSTORESTATE64
1573        case 64:
1574                sha1recompress_fast_64(ihvin, ihvout, me2, state);
1575                break;
1576#endif
1577#ifdef DOSTORESTATE65
1578        case 65:
1579                sha1recompress_fast_65(ihvin, ihvout, me2, state);
1580                break;
1581#endif
1582#ifdef DOSTORESTATE66
1583        case 66:
1584                sha1recompress_fast_66(ihvin, ihvout, me2, state);
1585                break;
1586#endif
1587#ifdef DOSTORESTATE67
1588        case 67:
1589                sha1recompress_fast_67(ihvin, ihvout, me2, state);
1590                break;
1591#endif
1592#ifdef DOSTORESTATE68
1593        case 68:
1594                sha1recompress_fast_68(ihvin, ihvout, me2, state);
1595                break;
1596#endif
1597#ifdef DOSTORESTATE69
1598        case 69:
1599                sha1recompress_fast_69(ihvin, ihvout, me2, state);
1600                break;
1601#endif
1602#ifdef DOSTORESTATE70
1603        case 70:
1604                sha1recompress_fast_70(ihvin, ihvout, me2, state);
1605                break;
1606#endif
1607#ifdef DOSTORESTATE71
1608        case 71:
1609                sha1recompress_fast_71(ihvin, ihvout, me2, state);
1610                break;
1611#endif
1612#ifdef DOSTORESTATE72
1613        case 72:
1614                sha1recompress_fast_72(ihvin, ihvout, me2, state);
1615                break;
1616#endif
1617#ifdef DOSTORESTATE73
1618        case 73:
1619                sha1recompress_fast_73(ihvin, ihvout, me2, state);
1620                break;
1621#endif
1622#ifdef DOSTORESTATE74
1623        case 74:
1624                sha1recompress_fast_74(ihvin, ihvout, me2, state);
1625                break;
1626#endif
1627#ifdef DOSTORESTATE75
1628        case 75:
1629                sha1recompress_fast_75(ihvin, ihvout, me2, state);
1630                break;
1631#endif
1632#ifdef DOSTORESTATE76
1633        case 76:
1634                sha1recompress_fast_76(ihvin, ihvout, me2, state);
1635                break;
1636#endif
1637#ifdef DOSTORESTATE77
1638        case 77:
1639                sha1recompress_fast_77(ihvin, ihvout, me2, state);
1640                break;
1641#endif
1642#ifdef DOSTORESTATE78
1643        case 78:
1644                sha1recompress_fast_78(ihvin, ihvout, me2, state);
1645                break;
1646#endif
1647#ifdef DOSTORESTATE79
1648        case 79:
1649                sha1recompress_fast_79(ihvin, ihvout, me2, state);
1650                break;
1651#endif
1652        default:
1653                abort();
1654        }
1655
1656}
1657
1658
1659
1660static void sha1_process(SHA1_CTX* ctx, const uint32_t block[16])
1661{
1662        unsigned i, j;
1663        uint32_t ubc_dv_mask[DVMASKSIZE] = { 0xFFFFFFFF };
1664        uint32_t ihvtmp[5];
1665
1666        ctx->ihv1[0] = ctx->ihv[0];
1667        ctx->ihv1[1] = ctx->ihv[1];
1668        ctx->ihv1[2] = ctx->ihv[2];
1669        ctx->ihv1[3] = ctx->ihv[3];
1670        ctx->ihv1[4] = ctx->ihv[4];
1671
1672        sha1_compression_states(ctx->ihv, block, ctx->m1, ctx->states);
1673
1674        if (ctx->detect_coll)
1675        {
1676                if (ctx->ubc_check)
1677                {
1678                        ubc_check(ctx->m1, ubc_dv_mask);
1679                }
1680
1681                if (ubc_dv_mask[0] != 0)
1682                {
1683                        for (i = 0; sha1_dvs[i].dvType != 0; ++i)
1684                        {
1685                                if (ubc_dv_mask[0] & ((uint32_t)(1) << sha1_dvs[i].maskb))
1686                                {
1687                                        for (j = 0; j < 80; ++j)
1688                                                ctx->m2[j] = ctx->m1[j] ^ sha1_dvs[i].dm[j];
1689
1690                                        sha1_recompression_step(sha1_dvs[i].testt, ctx->ihv2, ihvtmp, ctx->m2, ctx->states[sha1_dvs[i].testt]);
1691
1692                                        /* to verify SHA-1 collision detection code with collisions for reduced-step SHA-1 */
1693                                        if ((0 == ((ihvtmp[0] ^ ctx->ihv[0]) | (ihvtmp[1] ^ ctx->ihv[1]) | (ihvtmp[2] ^ ctx->ihv[2]) | (ihvtmp[3] ^ ctx->ihv[3]) | (ihvtmp[4] ^ ctx->ihv[4])))
1694                                                || (ctx->reduced_round_coll && 0==((ctx->ihv1[0] ^ ctx->ihv2[0]) | (ctx->ihv1[1] ^ ctx->ihv2[1]) | (ctx->ihv1[2] ^ ctx->ihv2[2]) | (ctx->ihv1[3] ^ ctx->ihv2[3]) | (ctx->ihv1[4] ^ ctx->ihv2[4]))))
1695                                        {
1696                                                ctx->found_collision = 1;
1697
1698                                                if (ctx->safe_hash)
1699                                                {
1700                                                        sha1_compression_W(ctx->ihv, ctx->m1);
1701                                                        sha1_compression_W(ctx->ihv, ctx->m1);
1702                                                }
1703
1704                                                break;
1705                                        }
1706                                }
1707                        }
1708                }
1709        }
1710}
1711
1712void SHA1DCInit(SHA1_CTX* ctx)
1713{
1714        ctx->total = 0;
1715        ctx->ihv[0] = 0x67452301;
1716        ctx->ihv[1] = 0xEFCDAB89;
1717        ctx->ihv[2] = 0x98BADCFE;
1718        ctx->ihv[3] = 0x10325476;
1719        ctx->ihv[4] = 0xC3D2E1F0;
1720        ctx->found_collision = 0;
1721        ctx->safe_hash = SHA1DC_INIT_SAFE_HASH_DEFAULT;
1722        ctx->ubc_check = 1;
1723        ctx->detect_coll = 1;
1724        ctx->reduced_round_coll = 0;
1725        ctx->callback = NULL;
1726}
1727
1728void SHA1DCSetSafeHash(SHA1_CTX* ctx, int safehash)
1729{
1730        if (safehash)
1731                ctx->safe_hash = 1;
1732        else
1733                ctx->safe_hash = 0;
1734}
1735
1736
1737void SHA1DCSetUseUBC(SHA1_CTX* ctx, int ubc_check)
1738{
1739        if (ubc_check)
1740                ctx->ubc_check = 1;
1741        else
1742                ctx->ubc_check = 0;
1743}
1744
1745void SHA1DCSetUseDetectColl(SHA1_CTX* ctx, int detect_coll)
1746{
1747        if (detect_coll)
1748                ctx->detect_coll = 1;
1749        else
1750                ctx->detect_coll = 0;
1751}
1752
1753void SHA1DCSetDetectReducedRoundCollision(SHA1_CTX* ctx, int reduced_round_coll)
1754{
1755        if (reduced_round_coll)
1756                ctx->reduced_round_coll = 1;
1757        else
1758                ctx->reduced_round_coll = 0;
1759}
1760
1761void SHA1DCSetCallback(SHA1_CTX* ctx, collision_block_callback callback)
1762{
1763        ctx->callback = callback;
1764}
1765
1766void SHA1DCUpdate(SHA1_CTX* ctx, const char* buf, size_t len)
1767{
1768        unsigned left, fill;
1769
1770        if (len == 0)
1771                return;
1772
1773        left = ctx->total & 63;
1774        fill = 64 - left;
1775
1776        if (left && len >= fill)
1777        {
1778                ctx->total += fill;
1779                memcpy(ctx->buffer + left, buf, fill);
1780                sha1_process(ctx, (uint32_t*)(ctx->buffer));
1781                buf += fill;
1782                len -= fill;
1783                left = 0;
1784        }
1785        while (len >= 64)
1786        {
1787                ctx->total += 64;
1788
1789#if defined(SHA1DC_ALLOW_UNALIGNED_ACCESS)
1790                sha1_process(ctx, (uint32_t*)(buf));
1791#else
1792                memcpy(ctx->buffer, buf, 64);
1793                sha1_process(ctx, (uint32_t*)(ctx->buffer));
1794#endif /* defined(SHA1DC_ALLOW_UNALIGNED_ACCESS) */
1795                buf += 64;
1796                len -= 64;
1797        }
1798        if (len > 0)
1799        {
1800                ctx->total += len;
1801                memcpy(ctx->buffer + left, buf, len);
1802        }
1803}
1804
1805static const unsigned char sha1_padding[64] =
1806{
1807        0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1808        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1809        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1810        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1811};
1812
1813int SHA1DCFinal(unsigned char output[20], SHA1_CTX *ctx)
1814{
1815        uint32_t last = ctx->total & 63;
1816        uint32_t padn = (last < 56) ? (56 - last) : (120 - last);
1817        uint64_t total;
1818        SHA1DCUpdate(ctx, (const char*)(sha1_padding), padn);
1819
1820        total = ctx->total - padn;
1821        total <<= 3;
1822        ctx->buffer[56] = (unsigned char)(total >> 56);
1823        ctx->buffer[57] = (unsigned char)(total >> 48);
1824        ctx->buffer[58] = (unsigned char)(total >> 40);
1825        ctx->buffer[59] = (unsigned char)(total >> 32);
1826        ctx->buffer[60] = (unsigned char)(total >> 24);
1827        ctx->buffer[61] = (unsigned char)(total >> 16);
1828        ctx->buffer[62] = (unsigned char)(total >> 8);
1829        ctx->buffer[63] = (unsigned char)(total);
1830        sha1_process(ctx, (uint32_t*)(ctx->buffer));
1831        output[0] = (unsigned char)(ctx->ihv[0] >> 24);
1832        output[1] = (unsigned char)(ctx->ihv[0] >> 16);
1833        output[2] = (unsigned char)(ctx->ihv[0] >> 8);
1834        output[3] = (unsigned char)(ctx->ihv[0]);
1835        output[4] = (unsigned char)(ctx->ihv[1] >> 24);
1836        output[5] = (unsigned char)(ctx->ihv[1] >> 16);
1837        output[6] = (unsigned char)(ctx->ihv[1] >> 8);
1838        output[7] = (unsigned char)(ctx->ihv[1]);
1839        output[8] = (unsigned char)(ctx->ihv[2] >> 24);
1840        output[9] = (unsigned char)(ctx->ihv[2] >> 16);
1841        output[10] = (unsigned char)(ctx->ihv[2] >> 8);
1842        output[11] = (unsigned char)(ctx->ihv[2]);
1843        output[12] = (unsigned char)(ctx->ihv[3] >> 24);
1844        output[13] = (unsigned char)(ctx->ihv[3] >> 16);
1845        output[14] = (unsigned char)(ctx->ihv[3] >> 8);
1846        output[15] = (unsigned char)(ctx->ihv[3]);
1847        output[16] = (unsigned char)(ctx->ihv[4] >> 24);
1848        output[17] = (unsigned char)(ctx->ihv[4] >> 16);
1849        output[18] = (unsigned char)(ctx->ihv[4] >> 8);
1850        output[19] = (unsigned char)(ctx->ihv[4]);
1851        return ctx->found_collision;
1852}
1853
1854#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C
1855#include SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C
1856#endif