Skip to content

Commit 60bfe07

Browse files
pks-tGravisZro
authored andcommitted
sha1dc: update to fix errors with endianess and unaligned access
This updates our version of SHA1DC to e139984 (Merge pull request #35 from lidl/master, 2017-05-30).
1 parent 3f78548 commit 60bfe07

File tree

2 files changed

+76
-12
lines changed

2 files changed

+76
-12
lines changed

‎sha1.c‎

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,70 @@
1010
#include<memory.h>
1111
#include<stdio.h>
1212
#include<stdlib.h>
13+
#endif
14+
15+
#ifdefSHA1DC_CUSTOM_INCLUDE_SHA1_C
16+
#includeSHA1DC_CUSTOM_INCLUDE_SHA1_C
17+
#endif
18+
19+
#ifndefSHA1DC_INIT_SAFE_HASH_DEFAULT
20+
#defineSHA1DC_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+
#ifdefSHA1DC_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+
#defineSHA1DC_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+
#defineSHA1DC_BIGENDIAN
54+
#endif
55+
56+
#endif
3057

31-
#defineBIGENDIAN (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+
#defineSHA1DC_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+
#defineSHA1DC_ALLOW_UNALIGNED_ACCESS
74+
75+
#endif/*UNALIGNMENT DETECTION*/
3276

33-
#endif/*ENDIANNESS SELECTION*/
3477

3578
#definerotate_right(x,n) (((x)>>(n))|((x)<<(32-(n))))
3679
#definerotate_left(x,n) (((x)<<(n))|((x)>>(32-(n))))
@@ -40,11 +83,11 @@
4083

4184
#definesha1_mix(W, t) (rotate_left(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1))
4285

43-
#if defined(BIGENDIAN)
86+
#ifdefSHA1DC_BIGENDIAN
4487
#definesha1_load(m, t, temp){temp = m[t]}
4588
#else
4689
#definesha1_load(m, t, temp){temp = m[t]; sha1_bswap32(temp)}
47-
#endif/*define(BIGENDIAN)*/
90+
#endif
4891

4992
#definesha1_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
#ifdefDOSTORESTATE0
877925
SHA1_RECOMPRESS(0)
878926
#endif
@@ -1193,6 +1241,10 @@ SHA1_RECOMPRESS(78)
11931241
SHA1_RECOMPRESS(79)
11941242
#endif
11951243

1244+
#ifdef_MSC_VER
1245+
#pragma warning(pop)
1246+
#endif
1247+
11961248
staticvoidsha1_recompression_step(uint32_tstep, uint32_tihvin[5], uint32_tihvout[5], constuint32_tme2[80], constuint32_tstate[5])
11971249
{
11981250
switch (step)
@@ -1610,7 +1662,7 @@ static void sha1_process(SHA1_CTX* ctx, const uint32_t block[16])
16101662
unsignedi, j;
16111663
uint32_tubc_dv_mask[DVMASKSIZE] ={0xFFFFFFFF };
16121664
uint32_tihvtmp[5];
1613-
1665+
16141666
ctx->ihv1[0] =ctx->ihv[0];
16151667
ctx->ihv1[1] =ctx->ihv[1];
16161668
ctx->ihv1[2] =ctx->ihv[2];
@@ -1714,6 +1766,7 @@ void SHA1DCSetCallback(SHA1_CTX* ctx, collision_block_callback callback)
17141766
voidSHA1DCUpdate(SHA1_CTX*ctx, constchar*buf, size_tlen)
17151767
{
17161768
unsignedleft, fill;
1769+
17171770
if (len==0)
17181771
return;
17191772

@@ -1732,7 +1785,13 @@ void SHA1DCUpdate(SHA1_CTX* ctx, const char* buf, size_t len)
17321785
while (len >= 64)
17331786
{
17341787
ctx->total+=64;
1788+
1789+
#if defined(SHA1DC_ALLOW_UNALIGNED_ACCESS)
17351790
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) */
17361795
buf+=64;
17371796
len-=64;
17381797
}

‎ubc_check.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
extern"C"{
2828
#endif
2929

30+
#ifndefSHA1DC_NO_STANDARD_INCLUDES
3031
#include<stdint.h>
3132
#endif
3233

@@ -44,4 +45,8 @@ void ubc_check(const uint32_t W[80], uint32_t dvmask[DVMASKSIZE]);
4445
}
4546
#endif
4647

47-
#endif/* UBC_CHECK_H */
48+
#ifdefSHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_H
49+
#includeSHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_H
50+
#endif
51+
52+
#endif

0 commit comments

Comments
(0)