Skip to content

Commit 05d5a0b

Browse files
author
elms
authored
Merge pull request #191 from dgarske/be
2 parents 00c9275 + 7b155b5 commit 05d5a0b

3 files changed

Lines changed: 113 additions & 61 deletions

File tree

src/tpm2_packet.c

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -31,67 +31,6 @@
3131
#define be16_to_cpu(d) ByteReverseWord16(d)
3232
#define be32_to_cpu(d) ByteReverseWord32(d)
3333
#define be64_to_cpu(d) ByteReverseWord64(d)
34-
35-
#ifdef PPC_INTRINSICS
36-
37-
#elif defined(__ICCARM__)
38-
#include "intrinsics.h"
39-
#elif defined(KEIL_INTRINSICS)
40-
41-
#elif defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
42-
43-
#else
44-
#if defined(FAST_ROTATE)
45-
static inline word32 rotrFixed(word32 x, word32 y) {
46-
return (x >> y) | (x << (sizeof(y) * 8 - y));
47-
}
48-
#endif
49-
static inline word32 rotlFixed(word32 x, word32 y) {
50-
return (x << y) | (x >> (sizeof(y) * 8 - y));
51-
}
52-
#endif
53-
54-
static inline word16 ByteReverseWord16(word16 value)
55-
{
56-
#if defined(__ICCARM__)
57-
return (word16)__REV16(value);
58-
#elif defined(KEIL_INTRINSICS)
59-
return (word16)__rev16(value);
60-
#elif defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
61-
return (word16)__builtin_bswap16(value);
62-
#else
63-
return (value >> 8) | (value << 8);
64-
#endif
65-
}
66-
67-
static inline word32 ByteReverseWord32(word32 value)
68-
{
69-
#ifdef PPC_INTRINSICS
70-
/* PPC: load reverse indexed instruction */
71-
return (word32)__lwbrx(&value,0);
72-
#elif defined(__ICCARM__)
73-
return (word32)__REV(value);
74-
#elif defined(KEIL_INTRINSICS)
75-
return (word32)__rev(value);
76-
#elif defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
77-
return (word32)__builtin_bswap32(value);
78-
#elif defined(FAST_ROTATE)
79-
/* 5 instructions with rotate instruction, 9 without */
80-
return (rotrFixed(value, 8U) & 0xff00ff00) |
81-
(rotlFixed(value, 8U) & 0x00ff00ff);
82-
#else
83-
/* 6 instructions with rotate instruction, 8 without */
84-
value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
85-
return rotlFixed(value, 16U);
86-
#endif
87-
}
88-
89-
static inline word64 ByteReverseWord64(word64 value)
90-
{
91-
return (word64)((word64)ByteReverseWord32((word32) value)) << 32 |
92-
(word64)ByteReverseWord32((word32)(value >> 32));
93-
}
94-
9534
#else
9635
#define cpu_to_be16(d) (d)
9736
#define cpu_to_be32(d) (d)

src/tpm2_tis.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,27 @@ int TPM2_TIS_GetInfo(TPM2_CTX* ctx)
320320

321321
rc = TPM2_TIS_Read(ctx, TPM_INTF_CAPS(ctx->locality), (byte*)&reg,
322322
sizeof(reg));
323+
#ifdef BIG_ENDIAN_ORDER
324+
reg = ByteReverseWord32(reg);
325+
#endif
323326
if (rc == TPM_RC_SUCCESS) {
324327
ctx->caps = reg;
325328
}
326329

327330
rc = TPM2_TIS_Read(ctx, TPM_DID_VID(ctx->locality), (byte*)&reg,
328331
sizeof(reg));
332+
#ifdef BIG_ENDIAN_ORDER
333+
reg = ByteReverseWord32(reg);
334+
#endif
329335
if (rc == TPM_RC_SUCCESS) {
330336
ctx->did_vid = reg;
331337
}
332338

333339
reg = 0;
334340
rc = TPM2_TIS_Read(ctx, TPM_RID(ctx->locality), (byte*)&reg, 1);
341+
#ifdef BIG_ENDIAN_ORDER
342+
reg = ByteReverseWord32(reg);
343+
#endif
335344
if (rc == TPM_RC_SUCCESS) {
336345
ctx->rid = reg;
337346
}
@@ -390,6 +399,9 @@ int TPM2_TIS_GetBurstCount(TPM2_CTX* ctx, word16* burstCount)
390399
do {
391400
rc = TPM2_TIS_Read(ctx, TPM_BURST_COUNT(ctx->locality),
392401
(byte*)burstCount, sizeof(*burstCount));
402+
#ifdef BIG_ENDIAN_ORDER
403+
*burstCount = ByteReverseWord16(*burstCount);
404+
#endif
393405
if (rc == TPM_RC_SUCCESS && *burstCount > 0)
394406
break;
395407
XTPM_WAIT();

wolftpm/tpm2_types.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,107 @@ typedef int64_t INT64;
550550
#define WOLFTPM2_WRAP_ECC_KEY_BITS (MAX_ECC_KEY_BITS*8)
551551
#endif
552552

553+
554+
/* ---------------------------------------------------------------------------*/
555+
/* ENDIANESS HELPERS */
556+
/* ---------------------------------------------------------------------------*/
557+
558+
#ifdef __ICCARM__
559+
#include "intrinsics.h"
560+
#endif
561+
562+
#ifdef INTEL_INTRINSICS
563+
/* for non visual studio probably need no long version, 32 bit only
564+
* i.e., _rotl and _rotr */
565+
#include <stdlib.h> /* get intrinsic definitions */
566+
#pragma intrinsic(_lrotl, _lrotr)
567+
static inline word32 rotlFixed(word32 x, word32 y) {
568+
return y ? _lrotl(x, y) : x;
569+
}
570+
static inline word32 rotrFixed(word32 x, word32 y) {
571+
return y ? _lrotr(x, y) : x;
572+
}
573+
#elif defined(__CCRX__)
574+
#include <builtin.h> /* get intrinsic definitions */
575+
static inline word32 rotlFixed(word32 x, word32 y) {
576+
return _builtin_rotl(x, y);
577+
}
578+
static inline word32 rotrFixed(word32 x, word32 y) {
579+
return _builtin_rotr(x, y);
580+
}
581+
#else /* generic */
582+
/* This routine performs a left circular arithmetic shift of <x> by <y> value. */
583+
static inline word32 rotlFixed(word32 x, word32 y) {
584+
return (x << y) | (x >> (sizeof(y) * 8 - y));
585+
}
586+
/* This routine performs a right circular arithmetic shift of <x> by <y> value. */
587+
static inline word32 rotrFixed(word32 x, word32 y)
588+
{
589+
return (x >> y) | (x << (sizeof(y) * 8 - y));
590+
}
591+
#endif
592+
593+
static inline word16 ByteReverseWord16(word16 value)
594+
{
595+
#if defined(__ICCARM__)
596+
return (word16)__REV16(value);
597+
#elif defined(KEIL_INTRINSICS)
598+
return (word16)__rev16(value);
599+
#elif defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
600+
return (word16)__builtin_bswap16(value);
601+
#else
602+
return (value >> 8) | (value << 8);
603+
#endif
604+
}
605+
606+
static inline word32 ByteReverseWord32(word32 value)
607+
{
608+
#ifdef PPC_INTRINSICS
609+
/* PPC: load reverse indexed instruction */
610+
return (word32)__lwbrx(&value,0);
611+
#elif defined(__ICCARM__)
612+
return (word32)__REV(value);
613+
#elif defined(KEIL_INTRINSICS)
614+
return (word32)__rev(value);
615+
#elif defined(__CCRX__)
616+
return (word32)_builtin_revl(value);
617+
#elif defined(WOLF_ALLOW_BUILTIN) && \
618+
defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
619+
return (word32)__builtin_bswap32(value);
620+
#elif defined(WOLFSSL_BYTESWAP32_ASM) && defined(__GNUC__) && \
621+
defined(__aarch64__)
622+
__asm__ volatile (
623+
"REV32 %0, %0 \n"
624+
: "+r" (value)
625+
:
626+
);
627+
return value;
628+
#elif defined(WOLFSSL_BYTESWAP32_ASM) && defined(__GNUC__) && \
629+
(defined(__thumb__) || defined(__arm__))
630+
__asm__ volatile (
631+
"REV %0, %0 \n"
632+
: "+r" (value)
633+
:
634+
);
635+
return value;
636+
#elif defined(FAST_ROTATE)
637+
/* 5 instructions with rotate instruction, 9 without */
638+
return (rotrFixed(value, 8U) & 0xff00ff00) |
639+
(rotlFixed(value, 8U) & 0x00ff00ff);
640+
#else
641+
/* 6 instructions with rotate instruction, 8 without */
642+
value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
643+
return rotlFixed(value, 16U);
644+
#endif
645+
}
646+
647+
static inline word64 ByteReverseWord64(word64 value)
648+
{
649+
return (word64)((word64)ByteReverseWord32((word32)value)) << 32 |
650+
(word64)ByteReverseWord32((word32)(value >> 32));
651+
}
652+
653+
553654
#ifdef __cplusplus
554655
} /* extern "C" */
555656
#endif

0 commit comments

Comments
 (0)