[Nauty] Big endian sha256
Jerry James
loganjerry at gmail.com
Thu Jul 31 03:58:55 AEST 2025
I tried building nauty 2.9.0 for Fedora. The tests failed on our s390x
(big endian) builder because the code in nausha.c assumes little
endianness. Applying the following patch led to a successful build. The
endian.h header is part of the POSIX specification, but if you build for
non-POSIX systems, you might want to use the AC_C_BIGENDIAN macro in
configure.ac instead. It defines WORDS_BIGENDIAN on big endian machines.
--- a/nausha.c
+++ b/nausha.c
@@ -1,6 +1,7 @@
/* nausha.c version 1.0; B D McKay, January 2025 */
#include "nausha.h"
+#include <endian.h>
/****************************************************************************
* This implementation of SHA256 is by Brad Conte, released to public
domain.
@@ -174,6 +175,7 @@ sha256_final(SHA256_CTX *ctx, nsword8 ha
// Since this implementation uses little endian byte ordering and SHA
uses big endian,
// reverse all the bytes when copying the final state to the output
hash.
for (i = 0; i < 4; ++i) {
+#if __BYTE_ORDER == __LITTLE_ENDIAN
hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
@@ -182,6 +184,16 @@ sha256_final(SHA256_CTX *ctx, nsword8 ha
hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
+#else
+ hash[i] = (ctx->state[1] >> (i * 8)) & 0x000000ff;
+ hash[i + 4] = (ctx->state[0] >> (i * 8)) & 0x000000ff;
+ hash[i + 8] = (ctx->state[3] >> (i * 8)) & 0x000000ff;
+ hash[i + 12] = (ctx->state[2] >> (i * 8)) & 0x000000ff;
+ hash[i + 16] = (ctx->state[5] >> (i * 8)) & 0x000000ff;
+ hash[i + 20] = (ctx->state[4] >> (i * 8)) & 0x000000ff;
+ hash[i + 24] = (ctx->state[7] >> (i * 8)) & 0x000000ff;
+ hash[i + 28] = (ctx->state[6] >> (i * 8)) & 0x000000ff;
+#endif
}
}
--
Jerry James
http://www.jamezone.org/
More information about the Nauty
mailing list