Fossil SCM

Improve the big-/little-endian compile-time detection in sha1hard.c so that it works on PowerPC. Full details of the report and fix are in the wiki page attached to [384705b56ac], which this merge integrates.

stephan 2026-11-21 13:05 UTC trunk merge
Commit 201cf952db7637bf15efa24ae15193599a929975439acf1f87ebcdbf3b33b7de
1 file changed +39 -3
+39 -3
--- src/sha1hard.c
+++ src/sha1hard.c
@@ -441,15 +441,40 @@
441441
#define sha1_bswap32(x) \
442442
{x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF); x = (x << 16) | (x >> 16);}
443443
444444
#define sha1_mix(W, t) (rotate_left(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1))
445445
446
-#if defined(BIGENDIAN)
447
- #define sha1_load(m, t) (m[t])
446
+/*
447
+** SHA1 message words are big-endian, so they must be byte-swapped on
448
+** little-endian hosts and loaded as-is on big-endian ones. Upstream
449
+** sha1dc has a large detection block for SHA1DC_BIGENDIAN. This is a
450
+** condensed form of it. SHA1DCInit() double-checks the result at
451
+** runtime so that a misdetection fails loudly instead of silently
452
+** producing wrong hashes.
453
+*/
454
+#if !defined(SHA1DC_BIGENDIAN) && !defined(SHA1DC_LITTLEENDIAN)
455
+# if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
456
+# if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
457
+# define SHA1DC_BIGENDIAN 1
458
+# endif
459
+# elif defined(__BIG_ENDIAN__) || defined(__ARMEB__) \
460
+ || defined(__THUMBEB__) || defined(__AARCH64EB__) \
461
+ || defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) \
462
+ || defined(__ppc__) || defined(__powerpc__) || defined(_POWER) \
463
+ || defined(__sparc) || defined(__sparc__) || defined(__hppa__) \
464
+ || defined(__s390__) || defined(__m68k__)
465
+ /* __ppc__/__powerpc__ without __BYTE_ORDER__ means an old compiler,
466
+ ** which predates ppc64le, so big-endian is a safe assumption. */
467
+# define SHA1DC_BIGENDIAN 1
468
+# endif
469
+#endif
470
+
471
+#if defined(SHA1DC_BIGENDIAN)
472
+ #define sha1_load(m, t, temp) { temp = m[t]; }
448473
#else
449474
#define sha1_load(m, t, temp) { temp = m[t]; sha1_bswap32(temp); }
450
-#endif /*define(BIGENDIAN)*/
475
+#endif /*defined(SHA1DC_BIGENDIAN)*/
451476
452477
#define sha1_store(W, t, x) *(volatile uint32_t *)&W[t] = x
453478
454479
#define sha1_f1(b,c,d) ((d)^((b)&((c)^(d))))
455480
#define sha1_f2(b,c,d) ((b)^(c)^(d))
@@ -1324,10 +1349,21 @@
13241349
}
13251350
}
13261351
13271352
void SHA1DCInit(SHA1_CTX* ctx)
13281353
{
1354
+ static const union { unsigned char b[4]; uint32_t v; } e = {{0,1,2,3}};
1355
+#if defined(SHA1DC_BIGENDIAN)
1356
+ if( e.v!=0x00010203 )
1357
+#else
1358
+ if( e.v!=0x03020100 )
1359
+#endif
1360
+ {
1361
+ fprintf(stderr, "sha1hard.c: compile-time byte-order detection "
1362
+ "is wrong for this platform\n");
1363
+ abort();
1364
+ }
13291365
ctx->total = 0;
13301366
ctx->ihv[0] = 0x67452301;
13311367
ctx->ihv[1] = 0xEFCDAB89;
13321368
ctx->ihv[2] = 0x98BADCFE;
13331369
ctx->ihv[3] = 0x10325476;
13341370
--- src/sha1hard.c
+++ src/sha1hard.c
@@ -441,15 +441,40 @@
441 #define sha1_bswap32(x) \
442 {x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF); x = (x << 16) | (x >> 16);}
443
444 #define sha1_mix(W, t) (rotate_left(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1))
445
446 #if defined(BIGENDIAN)
447 #define sha1_load(m, t) (m[t])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
448 #else
449 #define sha1_load(m, t, temp) { temp = m[t]; sha1_bswap32(temp); }
450 #endif /*define(BIGENDIAN)*/
451
452 #define sha1_store(W, t, x) *(volatile uint32_t *)&W[t] = x
453
454 #define sha1_f1(b,c,d) ((d)^((b)&((c)^(d))))
455 #define sha1_f2(b,c,d) ((b)^(c)^(d))
@@ -1324,10 +1349,21 @@
1324 }
1325 }
1326
1327 void SHA1DCInit(SHA1_CTX* ctx)
1328 {
 
 
 
 
 
 
 
 
 
 
 
1329 ctx->total = 0;
1330 ctx->ihv[0] = 0x67452301;
1331 ctx->ihv[1] = 0xEFCDAB89;
1332 ctx->ihv[2] = 0x98BADCFE;
1333 ctx->ihv[3] = 0x10325476;
1334
--- src/sha1hard.c
+++ src/sha1hard.c
@@ -441,15 +441,40 @@
441 #define sha1_bswap32(x) \
442 {x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF); x = (x << 16) | (x >> 16);}
443
444 #define sha1_mix(W, t) (rotate_left(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1))
445
446 /*
447 ** SHA1 message words are big-endian, so they must be byte-swapped on
448 ** little-endian hosts and loaded as-is on big-endian ones. Upstream
449 ** sha1dc has a large detection block for SHA1DC_BIGENDIAN. This is a
450 ** condensed form of it. SHA1DCInit() double-checks the result at
451 ** runtime so that a misdetection fails loudly instead of silently
452 ** producing wrong hashes.
453 */
454 #if !defined(SHA1DC_BIGENDIAN) && !defined(SHA1DC_LITTLEENDIAN)
455 # if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
456 # if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
457 # define SHA1DC_BIGENDIAN 1
458 # endif
459 # elif defined(__BIG_ENDIAN__) || defined(__ARMEB__) \
460 || defined(__THUMBEB__) || defined(__AARCH64EB__) \
461 || defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) \
462 || defined(__ppc__) || defined(__powerpc__) || defined(_POWER) \
463 || defined(__sparc) || defined(__sparc__) || defined(__hppa__) \
464 || defined(__s390__) || defined(__m68k__)
465 /* __ppc__/__powerpc__ without __BYTE_ORDER__ means an old compiler,
466 ** which predates ppc64le, so big-endian is a safe assumption. */
467 # define SHA1DC_BIGENDIAN 1
468 # endif
469 #endif
470
471 #if defined(SHA1DC_BIGENDIAN)
472 #define sha1_load(m, t, temp) { temp = m[t]; }
473 #else
474 #define sha1_load(m, t, temp) { temp = m[t]; sha1_bswap32(temp); }
475 #endif /*defined(SHA1DC_BIGENDIAN)*/
476
477 #define sha1_store(W, t, x) *(volatile uint32_t *)&W[t] = x
478
479 #define sha1_f1(b,c,d) ((d)^((b)&((c)^(d))))
480 #define sha1_f2(b,c,d) ((b)^(c)^(d))
@@ -1324,10 +1349,21 @@
1349 }
1350 }
1351
1352 void SHA1DCInit(SHA1_CTX* ctx)
1353 {
1354 static const union { unsigned char b[4]; uint32_t v; } e = {{0,1,2,3}};
1355 #if defined(SHA1DC_BIGENDIAN)
1356 if( e.v!=0x00010203 )
1357 #else
1358 if( e.v!=0x03020100 )
1359 #endif
1360 {
1361 fprintf(stderr, "sha1hard.c: compile-time byte-order detection "
1362 "is wrong for this platform\n");
1363 abort();
1364 }
1365 ctx->total = 0;
1366 ctx->ihv[0] = 0x67452301;
1367 ctx->ihv[1] = 0xEFCDAB89;
1368 ctx->ihv[2] = 0x98BADCFE;
1369 ctx->ihv[3] = 0x10325476;
1370

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button