1010#include <memory.h>
1111#include <stdio.h>
1212#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
1322
1423#include "sha1.h"
1524#include "ubc_check.h"
1625
1726
18- /*
27+ /*
1928 Because Little-Endian architectures are most common,
20- we only set BIGENDIAN if one of these conditions is met.
29+ we only set SHA1DC_BIGENDIAN if one of these conditions is met.
2130 Note that all MSFT platforms are little endian,
2231 so none of these will be defined under the MSC compiler.
2332 If you are compiling on a big endian platform and your compiler does not define one of these,
2433 you will have to add whatever macros your tool chain defines to indicate Big-Endianness.
2534 */
26- #if (defined(__BYTE_ORDER ) && (__BYTE_ORDER == __BIG_ENDIAN )) || \
27- (defined(__BYTE_ORDER__ ) && (__BYTE_ORDER__ == __BIG_ENDIAN__ )) || \
28- defined(__BIG_ENDIAN__ ) || defined(__ARMEB__ ) || defined(__THUMBEB__ ) || defined(__AARCH64EB__ ) || \
29- defined(_MIPSEB ) || defined(__MIPSEB ) || defined(__MIPSEB__ )
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
3057
31- #define BIGENDIAN (1)
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*/
3276
33- #endif /*ENDIANNESS SELECTION*/
3477
3578#define rotate_right (x ,n ) (((x)>>(n))|((x)<<(32-(n))))
3679#define rotate_left (x ,n ) (((x)<<(n))|((x)>>(32-(n))))
4083
4184#define sha1_mix (W , t ) (rotate_left(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1))
4285
43- #if defined( BIGENDIAN )
86+ #ifdef SHA1DC_BIGENDIAN
4487#define sha1_load (m , t , temp ){temp = m[t]}
4588#else
4689#define sha1_load (m , t , temp ){temp = m[t]; sha1_bswap32(temp)}
47- #endif /*define(BIGENDIAN)*/
90+ #endif
4891
4992#define sha1_store (W , t , x ) *(volatile uint32_t *)&W[t] = x
5093
@@ -873,6 +916,11 @@ static void sha1recompress_fast_ ## t (uint32_t ihvin[5], uint32_t ihvout[5], co
873916 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; \
874917}
875918
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+
876924#ifdef DOSTORESTATE0
877925SHA1_RECOMPRESS (0 )
878926#endif
@@ -1193,6 +1241,10 @@ SHA1_RECOMPRESS(78)
11931241SHA1_RECOMPRESS (79 )
11941242#endif
11951243
1244+ #ifdef _MSC_VER
1245+ #pragma warning(pop)
1246+ #endif
1247+
11961248static 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 ])
11971249{
11981250switch (step )
@@ -1610,7 +1662,7 @@ static void sha1_process(SHA1_CTX* ctx, const uint32_t block[16])
16101662unsigned i , j ;
16111663uint32_t ubc_dv_mask [DVMASKSIZE ] = {0xFFFFFFFF };
16121664uint32_t ihvtmp [5 ];
1613-
1665+
16141666ctx -> ihv1 [0 ] = ctx -> ihv [0 ];
16151667ctx -> ihv1 [1 ] = ctx -> ihv [1 ];
16161668ctx -> ihv1 [2 ] = ctx -> ihv [2 ];
@@ -1714,6 +1766,7 @@ void SHA1DCSetCallback(SHA1_CTX* ctx, collision_block_callback callback)
17141766void SHA1DCUpdate (SHA1_CTX * ctx , const char * buf , size_t len )
17151767{
17161768unsigned left , fill ;
1769+
17171770if (len == 0 )
17181771return ;
17191772
@@ -1732,7 +1785,13 @@ void SHA1DCUpdate(SHA1_CTX* ctx, const char* buf, size_t len)
17321785while (len >= 64 )
17331786{
17341787ctx -> total += 64 ;
1788+
1789+ #if defined(SHA1DC_ALLOW_UNALIGNED_ACCESS )
17351790sha1_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) */
17361795buf += 64 ;
17371796len -= 64 ;
17381797 }
0 commit comments