PeterHuewe / tpm-emulator

The famous tpm-emulator by Mario Strasser, previously hosted on BerliOs. It supports TPM1.2 only!
GNU General Public License v2.0
177 stars 75 forks source link

Big Endian machines should define __BIG_ENDIAN__ or the sha1 code will fail #20

Open mbaushke opened 7 years ago

mbaushke commented 7 years ago

By default, there is no code or compile options to put -D__BIG_ENDIAN__=1 into the build environment. As a result, the selftest for sha1 will fail. One way to do this would be via the following patch (tested under BSD): `diff --git a/crypto/sha1.h b/crypto/sha1.h index c94ed39..f7c6a7b 100644 --- a/crypto/sha1.h +++ b/crypto/sha1.h @@ -36,4 +36,21 @@ void tpm_sha1_update_be32(tpm_sha1_ctx_t *ctx, uint32_t data);

void tpm_sha1_final(tpm_sha1_ctx_t *ctx, uint8_t digest[SHA1_DIGEST_LENGTH]);

+#if (defined(i386) || defined(x86_64)) && !defined(LITTLE_ENDIAN) +#define LITTLE_ENDIAN 1 +#endif + +#if defined(FreeBSD) || defined(NetBSD) || defined(OpenBSD) +#include <sys/endian.h> +#if _BYTE_ORDER == _LITTLE_ENDIAN +#define LITTLE_ENDIAN 1 +#elif _BYTE_ORDER == _BIG_ENDIAN +#define BIG_ENDIAN 1 +#endif +#endif + +#if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) +#error "Neither BIG_ENDIAN nor __LITTLE_ENDIAN__ are defined" +#endif +

endif / _SHA1H /

`

For those using a clang compiler, you may also want a local patch to disable the pointer-bool-conversion error:

add_definitions(-Wall -Werror -Wno-unused-parameter -Wpointer-arith -Wcast-align -Wwrite-strings -Wno-error=pointer-bool-conversion)

There are, of course, other ways to address the endian issue. The above is just a suggestion.

PeterHuewe commented 5 years ago

Maybe you can create a patch/pull-request? That would be great. I don't have any big endian platforms to test (which one are you using?)