Remove duplicate code for endianness manipulation

Remove duplicate code for endianness manipulation and reuse the standard
library on Linux, which will allow make the code usable on big endian
platforms.

Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com>
diff --git a/Makefile.am b/Makefile.am
index ac6f6fa..0d30bf9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -27,7 +27,7 @@
 
 include src_vars.mk
 
-INCLUDE_DIRS    = -I$(srcdir)/include -I$(srcdir)/sysapi/include
+INCLUDE_DIRS    = -I$(srcdir)/include -I$(srcdir)/sysapi/include -I$(srcdir)/marshal
 ACLOCAL_AMFLAGS = -I m4
 AM_CFLAGS       = $(INCLUDE_DIRS) $(EXTRA_CFLAGS)
 AM_LDFLAGS      = $(EXTRA_LDFLAGS)
@@ -145,8 +145,7 @@
 tcti_libtcti_socket_la_CFLAGS   = $(AM_CFLAGS)
 tcti_libtcti_socket_la_LDFLAGS  = -Wl,--version-script=$(srcdir)/tcti/tcti_socket.map
 tcti_libtcti_socket_la_SOURCES  = tcti/platformcommand.c tcti/tcti_socket.c \
-    sysapi/sysapi_util/changeEndian.c tcti/commonchecks.c common/sockets.c \
-    common/debug.c
+                                  tcti/commonchecks.c common/sockets.c common/debug.c
 
 test_tpmclient_tpmclient_CFLAGS   = $(AM_CFLAGS) -DNO_RM_TESTS
 test_tpmclient_tpmclient_LDADD    = $(libsapi) $(libtcti_socket) $(libtcti_device) $(libmarshal)
diff --git a/marshal/base-types.c b/marshal/base-types.c
index 96734df..74f8765 100644
--- a/marshal/base-types.c
+++ b/marshal/base-types.c
@@ -30,9 +30,8 @@
 
 #include "sapi/marshal.h"
 #include "sapi/tpm20.h"
-
+#include "tss2_endian.h"
 #include "log.h"
-#include "base-types.h"
 
 #define BASE_MARSHAL(type, marshal_func) \
 TSS2_RC \
@@ -144,6 +143,10 @@
  * These macros expand to (un)marshal functions for each of the base types
  * the specification part 2, table 3: Definition of Base Types.
  */
+
+#define HOST_TO_BE_8(value) (value)
+#define BE_TO_HOST_8(value) (value)
+
 BASE_MARSHAL   (BYTE,   HOST_TO_BE_8);
 BASE_UNMARSHAL (BYTE,   BE_TO_HOST_8);
 BASE_MARSHAL   (INT8,   HOST_TO_BE_8);
@@ -163,37 +166,6 @@
 BASE_MARSHAL   (UINT64, HOST_TO_BE_64);
 BASE_UNMARSHAL (UINT64, BE_TO_HOST_64);
 
-UINT8
-endian_conv_8 (UINT8 value)
-{
-    return value;
-}
-UINT16
-endian_conv_16 (UINT16 value)
-{
-    return ((value & (0xff))      << 8) | \
-           ((value & (0xff << 8)) >> 8);
-}
-UINT32
-endian_conv_32 (UINT32 value)
-{
-    return ((value & (0xff))       << 24) | \
-           ((value & (0xff << 8))  << 8)  | \
-           ((value & (0xff << 16)) >> 8)  | \
-           ((value & (0xff << 24)) >> 24);
-}
-UINT64
-endian_conv_64 (UINT64 value)
-{
-    return ((value & (0xffULL))       << 56) | \
-           ((value & (0xffULL << 8))  << 40) | \
-           ((value & (0xffULL << 16)) << 24) | \
-           ((value & (0xffULL << 24)) << 8)  | \
-           ((value & (0xffULL << 32)) >> 8)  | \
-           ((value & (0xffULL << 40)) >> 24) | \
-           ((value & (0xffULL << 48)) >> 40) | \
-           ((value & (0xffULL << 56)) >> 56);
-}
 TSS2_RC
 TPM_CC_Marshal (
     TPM_CC          src,
diff --git a/marshal/base-types.h b/marshal/base-types.h
deleted file mode 100644
index b05f839..0000000
--- a/marshal/base-types.h
+++ /dev/null
@@ -1,73 +0,0 @@
-//**********************************************************************;
-// Copyright (c) 2015, Intel Corporation
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are met:
-//
-// 1. Redistributions of source code must retain the above copyright notice,
-// this list of conditions and the following disclaimer.
-//
-// 2. Redistributions in binary form must reproduce the above copyright notice,
-// this list of conditions and the following disclaimer in the documentation
-// and/or other materials provided with the distribution.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
-// THE POSSIBILITY OF SUCH DAMAGE.
-//**********************************************************************;
-
-#ifndef BASE_TYPES_H
-#define BASE_TYPES_H
-
-#include "sapi/tpm20.h"
-
-/*
- * These no-op functions are used on big endian (BE) systems for the host
- * to / from BE macros. We need them so that we have a valid function
- * pointers for the base type marshalling function template.
- */
-static inline UINT8  noop_8  (UINT8 value)  { return value; };
-static inline UINT16 noop_16 (UINT16 value) { return value; };
-static inline UINT32 noop_32 (UINT32 value) { return value; };
-static inline UINT64 noop_64 (UINT64 value) { return value; };
-/*
- * These functions are used to convert the endianness of base types. We
- * use them on little endian (LE) systems for the host to / from BE macros.
- */
-UINT8  endian_conv_8  (UINT8 value);
-UINT16 endian_conv_16 (UINT16 value);
-UINT32 endian_conv_32 (UINT32 value);
-UINT64 endian_conv_64 (UINT64 value);
-/*
- * These macros should be used in any place where a base type needs to be
- * converted from the host byte order (host) to a big endian representation.
- */
-#if defined(WORDS_BIGENDIAN)
-#define HOST_TO_BE_8(value)  noop_8  (value)
-#define HOST_TO_BE_16(value) noop_16 (value)
-#define HOST_TO_BE_32(value) noop_32 (value)
-#define HOST_TO_BE_64(value) noop_64 (value)
-#define BE_TO_HOST_8(value)  noop_8  (value)
-#define BE_TO_HOST_16(value) noop_16 (value)
-#define BE_TO_HOST_32(value) noop_32 (value)
-#define BE_TO_HOST_64(value) noop_64 (value)
-#else
-#define HOST_TO_BE_8(value)  endian_conv_8  (value)
-#define HOST_TO_BE_16(value) endian_conv_16 (value)
-#define HOST_TO_BE_32(value) endian_conv_32 (value)
-#define HOST_TO_BE_64(value) endian_conv_64 (value)
-#define BE_TO_HOST_8(value)  endian_conv_8  (value)
-#define BE_TO_HOST_16(value) endian_conv_16 (value)
-#define BE_TO_HOST_32(value) endian_conv_32 (value)
-#define BE_TO_HOST_64(value) endian_conv_64 (value)
-#endif /* WORDS_BIGENDIAN */
-#endif /* BASE_TYPES_H  */
diff --git a/marshal/tss2_endian.h b/marshal/tss2_endian.h
new file mode 100644
index 0000000..5a73865
--- /dev/null
+++ b/marshal/tss2_endian.h
@@ -0,0 +1,90 @@
+/***********************************************************************;
+ * Copyright (c) 2015 - 2017, Intel Corporation
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ ***********************************************************************/
+
+#ifndef TSS2_ENDIAN_H
+#define TSS2_ENDIAN_H
+
+#if defined(__linux__) || defined(__unix__)
+#include <endian.h>
+
+#define HOST_TO_BE_16(value) htobe16(value)
+#define HOST_TO_BE_32(value) htobe32(value)
+#define HOST_TO_BE_64(value) htobe64(value)
+#define BE_TO_HOST_16(value) be16toh(value)
+#define BE_TO_HOST_32(value) be32toh(value)
+#define BE_TO_HOST_64(value) be64toh(value)
+
+#else /* linux || unix */
+
+#if defined(WORDS_BIGENDIAN)
+
+#define HOST_TO_BE_16(value) (value)
+#define HOST_TO_BE_32(value) (value)
+#define HOST_TO_BE_64(value) (value)
+#define BE_TO_HOST_16(value) (value)
+#define BE_TO_HOST_32(value) (value)
+#define BE_TO_HOST_64(value) (value)
+
+#else
+
+static inline uint16_t endian_conv_16(UINT16 value)
+{
+    return ((value & (0xff))      << 8) | \
+           ((value & (0xff << 8)) >> 8);
+}
+
+static inline uint32_t endian_conv_32(uint32_t value)
+{
+    return ((value & (0xff))       << 24) | \
+           ((value & (0xff << 8))  << 8)  | \
+           ((value & (0xff << 16)) >> 8)  | \
+           ((value & (0xff << 24)) >> 24);
+}
+
+static inline uint64_t endian_conv_64(uint64_t value)
+{
+    return ((value & (0xffULL))       << 56) | \
+           ((value & (0xffULL << 8))  << 40) | \
+           ((value & (0xffULL << 16)) << 24) | \
+           ((value & (0xffULL << 24)) << 8)  | \
+           ((value & (0xffULL << 32)) >> 8)  | \
+           ((value & (0xffULL << 40)) >> 24) | \
+           ((value & (0xffULL << 48)) >> 40) | \
+           ((value & (0xffULL << 56)) >> 56);
+}
+
+#define HOST_TO_BE_16(value) endian_conv_16(value)
+#define HOST_TO_BE_32(value) endian_conv_32(value)
+#define HOST_TO_BE_64(value) endian_conv_64(value)
+#define BE_TO_HOST_16(value) endian_conv_16(value)
+#define BE_TO_HOST_32(value) endian_conv_32(value)
+#define BE_TO_HOST_64(value) endian_conv_64(value)
+
+#endif /* WORDS_BIGENDIAN */
+#endif /* linux || unix */
+#endif /* TSS2_ENDIAN_H */
diff --git a/sysapi/include/endianConv.h b/sysapi/include/endianConv.h
deleted file mode 100644
index 441d4e5..0000000
--- a/sysapi/include/endianConv.h
+++ /dev/null
@@ -1,67 +0,0 @@
-//**********************************************************************;
-// Copyright (c) 2015, Intel Corporation
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are met:
-//
-// 1. Redistributions of source code must retain the above copyright notice,
-// this list of conditions and the following disclaimer.
-//
-// 2. Redistributions in binary form must reproduce the above copyright notice,
-// this list of conditions and the following disclaimer in the documentation
-// and/or other materials provided with the distribution.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
-// THE POSSIBILITY OF SUCH DAMAGE.
-//**********************************************************************;
-
-#ifndef ENDIANCONV_H
-#define ENDIANCONV_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-//
-// Comment out following line for big endian CPUs.
-//
-#define LITTLE_ENDIAN_CPU
-
-#ifdef LITTLE_ENDIAN_CPU
-
-UINT64 ChangeEndianQword( UINT64 p );
-UINT32 ChangeEndianDword( UINT32 p );
-UINT16 ChangeEndianWord( UINT16 p );
-
-// CPU is little endian, so bytes need to be swapped.
-#define CHANGE_ENDIAN_WORD(p) ( ChangeEndianWord (p) )
-
-#define CHANGE_ENDIAN_DWORD(p) ( ChangeEndianDword(p) )
-
-#define CHANGE_ENDIAN_QWORD(p) ( ChangeEndianQword(p) )
-#else
- // If CPU is big-endian, no need to do endianness swapping.
-
-#define CHANGE_ENDIAN_WORD(p)  p
-
-#define CHANGE_ENDIAN_DWORD(p) p
-
-#define CHANGE_ENDIAN_QWORD(p) p
-#endif
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/sysapi/include/sysapi_util.h b/sysapi/include/sysapi_util.h
index 39cdcd7..9e71206 100644
--- a/sysapi/include/sysapi_util.h
+++ b/sysapi/include/sysapi_util.h
@@ -32,7 +32,6 @@
 extern "C" {
 #endif
 
-#include "endianConv.h"
 #include "tcti_util.h"
 
 // TBD:  delete this after porting completed.
diff --git a/sysapi/sysapi/DecryptParam.c b/sysapi/sysapi/DecryptParam.c
index 38639d6..d536a3e 100644
--- a/sysapi/sysapi/DecryptParam.c
+++ b/sysapi/sysapi/DecryptParam.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 TSS2_RC Tss2_Sys_GetDecryptParam(
 	TSS2_SYS_CONTEXT 		*sysContext,
@@ -54,7 +55,7 @@
         // Get first parameter and return its
         // size and a pointer to it.
         decryptParam = (TPM2B *)( SYS_CONTEXT->cpBuffer );
-        *decryptParamSize = CHANGE_ENDIAN_WORD( decryptParam->size );
+        *decryptParamSize = BE_TO_HOST_16(decryptParam->size);
         *decryptParamBuffer = &( decryptParam->buffer[0] );
     }
     return rval;
@@ -84,7 +85,7 @@
         return rval;
     }
 
-    sizeToBeUsed = CHANGE_ENDIAN_DWORD( ( (TPM20_Header_In *)( SYS_CONTEXT->tpmInBuffPtr ) )->commandSize ) + decryptParamSize;
+    sizeToBeUsed = BE_TO_HOST_32(((TPM20_Header_In *)(SYS_CONTEXT->tpmInBuffPtr))->commandSize) + decryptParamSize;
     if( sizeToBeUsed > SYS_CONTEXT->maxCommandSize )
     {
         return TSS2_SYS_RC_INSUFFICIENT_CONTEXT;
@@ -108,7 +109,7 @@
         SYS_CONTEXT->cpBufferUsedSize += decryptParamSize;
 
         // Now copy in the encrypted decrypt param.
-        *(UINT16 *)SYS_CONTEXT->cpBuffer = CHANGE_ENDIAN_WORD( decryptParamSize );
+        *(UINT16 *)SYS_CONTEXT->cpBuffer = HOST_TO_BE_16(decryptParamSize);
         rval = CopyMem( (uint8_t *)currDecryptParamBuffer, decryptParamBuffer, decryptParamSize, SYS_CONTEXT->tpmInBuffPtr + SYS_CONTEXT->maxCommandSize );
         if( rval != TSS2_RC_SUCCESS )
         {
@@ -116,9 +117,9 @@
         }
 
         // And fixup the command size.
-        currCommandSize = CHANGE_ENDIAN_DWORD( ( (TPM20_Header_In *)( SYS_CONTEXT->tpmInBuffPtr ) )->commandSize );
+        currCommandSize = BE_TO_HOST_32(((TPM20_Header_In *)(SYS_CONTEXT->tpmInBuffPtr))->commandSize);
         currCommandSize += decryptParamSize;
-        ( (TPM20_Header_In *)( SYS_CONTEXT->tpmInBuffPtr ) )->commandSize = CHANGE_ENDIAN_DWORD( currCommandSize );
+        ((TPM20_Header_In *)(SYS_CONTEXT->tpmInBuffPtr))->commandSize = HOST_TO_BE_32(currCommandSize);
     }
     else
     {
@@ -126,7 +127,7 @@
         {
             return TSS2_SYS_RC_BAD_SIZE;
         }
-        *(UINT16 *)SYS_CONTEXT->cpBuffer = CHANGE_ENDIAN_WORD( decryptParamSize );
+        *(UINT16 *)SYS_CONTEXT->cpBuffer = HOST_TO_BE_16(decryptParamSize);
         rval = CopyMem( (uint8_t *)currDecryptParamBuffer, decryptParamBuffer, decryptParamSize, SYS_CONTEXT->tpmInBuffPtr + SYS_CONTEXT->maxCommandSize );
         if( rval != TSS2_RC_SUCCESS )
         {
diff --git a/sysapi/sysapi/EncryptParam.c b/sysapi/sysapi/EncryptParam.c
index 6a141f0..707ce98 100644
--- a/sysapi/sysapi/EncryptParam.c
+++ b/sysapi/sysapi/EncryptParam.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 TSS2_RC Tss2_Sys_GetEncryptParam(
 	TSS2_SYS_CONTEXT 	*sysContext,
@@ -47,7 +48,7 @@
         rval = TSS2_SYS_RC_BAD_SEQUENCE;
     }
     else if( SYS_CONTEXT->encryptAllowed == 0 ||
-            ( CHANGE_ENDIAN_WORD( ( (TPM20_Header_Out *)( SYS_CONTEXT->tpmOutBuffPtr )  )->tag ) == TPM_ST_NO_SESSIONS ) )
+            (BE_TO_HOST_16(((TPM20_Header_Out *)(SYS_CONTEXT->tpmOutBuffPtr))->tag) == TPM_ST_NO_SESSIONS))
     {
         rval = TSS2_SYS_RC_NO_ENCRYPT_PARAM;
     }
@@ -59,7 +60,7 @@
         SYS_CONTEXT->rpBuffer = otherData;
         SYS_CONTEXT->rpBuffer += 4; // Skip over params size field.
         encryptParam = (TPM2B *)( SYS_CONTEXT->rpBuffer );
-        *encryptParamSize = CHANGE_ENDIAN_WORD( encryptParam->size );
+        *encryptParamSize = BE_TO_HOST_16(encryptParam->size);
         *encryptParamBuffer = &( encryptParam->buffer[0] );
     }
     return rval;
diff --git a/sysapi/sysapi/GetCommandCode.c b/sysapi/sysapi/GetCommandCode.c
index 5351343..23b2592 100644
--- a/sysapi/sysapi/GetCommandCode.c
+++ b/sysapi/sysapi/GetCommandCode.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 TSS2_RC Tss2_Sys_GetCommandCode(
     TSS2_SYS_CONTEXT *sysContext,
@@ -45,7 +46,7 @@
     }
     else
     {
-        *(UINT32 *)*commandCode = CHANGE_ENDIAN_DWORD( SYS_CONTEXT->commandCodeSwapped );
+        *(UINT32 *)*commandCode = BE_TO_HOST_32(SYS_CONTEXT->commandCodeSwapped);
     }
 
     return( rval );
diff --git a/sysapi/sysapi/authorizations.c b/sysapi/sysapi/authorizations.c
index c8206e2..f56396b 100644
--- a/sysapi/sysapi/authorizations.c
+++ b/sysapi/sysapi/authorizations.c
@@ -25,7 +25,7 @@
 // THE POSSIBILITY OF SUCH DAMAGE.
 //**********************************************************************;
 
-#include "marshal/base-types.h"
+#include "tss2_endian.h"
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
 
diff --git a/sysapi/sysapi/execute.c b/sysapi/sysapi/execute.c
index d588430..9919af9 100644
--- a/sysapi/sysapi/execute.c
+++ b/sysapi/sysapi/execute.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 TSS2_RC Tss2_Sys_ExecuteAsync(
     TSS2_SYS_CONTEXT 		*sysContext
@@ -44,9 +45,9 @@
     }
     else
     {
-        rval = tss2_tcti_transmit (SYS_CONTEXT->tctiContext,
-                                   CHANGE_ENDIAN_DWORD( ((TPM20_Header_In *)SYS_CONTEXT->tpmInBuffPtr )->commandSize),
-                                   SYS_CONTEXT->tpmInBuffPtr);
+        rval = tss2_tcti_transmit(SYS_CONTEXT->tctiContext,
+                                  HOST_TO_BE_32(((TPM20_Header_In *)SYS_CONTEXT->tpmInBuffPtr)->commandSize),
+                                  SYS_CONTEXT->tpmInBuffPtr);
     }
 
     if( rval == TSS2_RC_SUCCESS )
diff --git a/sysapi/sysapi_util/CommandUtil.c b/sysapi/sysapi_util/CommandUtil.c
index 36a0314..2e9ea1b 100644
--- a/sysapi/sysapi_util/CommandUtil.c
+++ b/sysapi/sysapi_util/CommandUtil.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -87,7 +88,7 @@
 
 UINT32 GetCommandSize( TSS2_SYS_CONTEXT *sysContext )
 {
-    return( CHANGE_ENDIAN_DWORD( ((TPM20_Header_In *) SYS_CONTEXT->tpmInBuffPtr)->commandSize  ) );
+    return BE_TO_HOST_32(((TPM20_Header_In *) SYS_CONTEXT->tpmInBuffPtr)->commandSize);
 }
 
 void CopyCommandHeader( _TSS2_SYS_CONTEXT_BLOB *sysContext, TPM_CC commandCode )
@@ -101,7 +102,7 @@
                     TPM_ST_NO_SESSIONS,
                     &(SYS_CONTEXT->rval));
 
-  ((TPM20_Header_In *) sysContext->tpmInBuffPtr)->commandCode = CHANGE_ENDIAN_DWORD( commandCode );
+  ((TPM20_Header_In *)sysContext->tpmInBuffPtr)->commandCode = BE_TO_HOST_32(commandCode);
 
   SYS_CONTEXT->rval = TSS2_RC_SUCCESS;
 
@@ -154,7 +155,7 @@
 
         SYS_CONTEXT->numResponseHandles = GetNumResponseHandles( commandCode );
 
-        SYS_CONTEXT->commandCodeSwapped = CHANGE_ENDIAN_DWORD( commandCode );
+        SYS_CONTEXT->commandCodeSwapped = BE_TO_HOST_32(commandCode);
 
         SYS_CONTEXT->rspParamsSize = (UINT32 *)&(((TPM20_Header_Out *)( SYS_CONTEXT->tpmOutBuffPtr ) )->otherData ) +
                 GetNumResponseHandles( commandCode );
@@ -179,7 +180,7 @@
 
    // Set current command size.
    ((TPM20_Header_In *) SYS_CONTEXT->tpmInBuffPtr)->commandSize =
-           CHANGE_ENDIAN_DWORD( SYS_CONTEXT->nextData - SYS_CONTEXT->tpmInBuffPtr );
+           BE_TO_HOST_32(SYS_CONTEXT->nextData - SYS_CONTEXT->tpmInBuffPtr);
 
    SYS_CONTEXT->previousStage = CMD_STAGE_PREPARE;
 
@@ -197,7 +198,7 @@
     }
     else
     {
-        rspSize = CHANGE_ENDIAN_DWORD( ( (TPM20_Header_Out *)( SYS_CONTEXT->tpmOutBuffPtr )  )->responseSize );
+        rspSize = BE_TO_HOST_32(((TPM20_Header_Out *)(SYS_CONTEXT->tpmOutBuffPtr))->responseSize);
     }
 
     if( SYS_CONTEXT->previousStage != CMD_STAGE_RECEIVE_RESPONSE || SYS_CONTEXT->rval != TSS2_RC_SUCCESS )
@@ -229,7 +230,7 @@
         SYS_CONTEXT->rpBuffer = SYS_CONTEXT->nextData;
 
         // Save response params size if command does not have an authorization area.
-        if( CHANGE_ENDIAN_WORD( ( (TPM20_Header_Out *)( SYS_CONTEXT->tpmOutBuffPtr )  )->tag ) != TPM_ST_SESSIONS )
+        if (BE_TO_HOST_16(((TPM20_Header_Out *)(SYS_CONTEXT->tpmOutBuffPtr))->tag) != TPM_ST_SESSIONS)
         {
             SYS_CONTEXT->rpBufferUsedSize = rspSize - ( SYS_CONTEXT->rpBuffer - SYS_CONTEXT->tpmOutBuffPtr );
         }
@@ -262,8 +263,8 @@
         {
             if( SYS_CONTEXT->rsp_header.rsp_code == TPM_RC_SUCCESS )
             {
-                if( CHANGE_ENDIAN_WORD( ( (TPM20_Header_Out *)( SYS_CONTEXT->tpmOutBuffPtr )  )->tag ) == TPM_ST_SESSIONS &&
-                        rspAuthsArray != 0 )
+                if (BE_TO_HOST_16(((TPM20_Header_Out *)( SYS_CONTEXT->tpmOutBuffPtr))->tag) == TPM_ST_SESSIONS &&
+                        rspAuthsArray != 0)
                 {
                     SYS_CONTEXT->rval = Tss2_Sys_GetRspAuths( sysContext, rspAuthsArray );
                 }
diff --git a/sysapi/sysapi_util/CopySessionData.c b/sysapi/sysapi_util/CopySessionData.c
index 9756041..f60df70 100644
--- a/sysapi/sysapi_util/CopySessionData.c
+++ b/sysapi/sysapi_util/CopySessionData.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 #define SESSION_MARSHAL_UINT32( buffer, size, currentPtr, value, rval, exitLoc ) \
     Marshal_UINT32( buffer, size, currentPtr, value, rval ); \
@@ -82,10 +83,9 @@
 	}
 
     // Size of session data
-    *sessionSizePtr += CHANGE_ENDIAN_DWORD(
-            sizeof( TPMI_SH_AUTH_SESSION ) + sizeof( UINT16 ) +
-            sessionData->nonce.t.size + sizeof( UINT8 ) +
-            sizeof( UINT16 ) + sessionData->hmac.t.size );
+    *sessionSizePtr += BE_TO_HOST_32(sizeof(TPMI_SH_AUTH_SESSION) +
+                         sizeof(UINT16) + sessionData->nonce.t.size + sizeof(UINT8) +
+                         sizeof(UINT16) + sessionData->hmac.t.size);
 
     // copy session handle
     SESSION_MARSHAL_UINT32( inBuffPtr, *sessionSizePtr, (UINT8 **)otherData, sessionDataCopy->sessionHandle, &rval, exitCopySessionDataIn );
diff --git a/sysapi/sysapi_util/Marshal_TPM2B_ECC_POINT.c b/sysapi/sysapi_util/Marshal_TPM2B_ECC_POINT.c
index 3d45349..ba30d91 100644
--- a/sysapi/sysapi_util/Marshal_TPM2B_ECC_POINT.c
+++ b/sysapi/sysapi_util/Marshal_TPM2B_ECC_POINT.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 void Marshal_TPM2B_ECC_POINT(
 	TSS2_SYS_CONTEXT *sysContext,
@@ -47,7 +48,7 @@
 		Marshal_TPMS_ECC_POINT( sysContext, &eccPoint->t.point );
 	}
 
-	*(UINT16 *)sizePtr = CHANGE_ENDIAN_WORD( SYS_CONTEXT->nextData - (UINT8 *)sizePtr - 2 );
+	*(UINT16 *)sizePtr = BE_TO_HOST_16(SYS_CONTEXT->nextData - (UINT8 *)sizePtr - 2);
 
 	return;
 }
diff --git a/sysapi/sysapi_util/Marshal_TPM2B_NV_PUBLIC.c b/sysapi/sysapi_util/Marshal_TPM2B_NV_PUBLIC.c
index 0667748..597693c 100644
--- a/sysapi/sysapi_util/Marshal_TPM2B_NV_PUBLIC.c
+++ b/sysapi/sysapi_util/Marshal_TPM2B_NV_PUBLIC.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 void Marshal_TPM2B_NV_PUBLIC(
 	TSS2_SYS_CONTEXT *sysContext,
@@ -47,7 +48,7 @@
 		Marshal_TPMS_NV_PUBLIC( sysContext, &nvPublic->t.nvPublic );
 	}
 
-	*(UINT16 *)sizePtr = CHANGE_ENDIAN_WORD( SYS_CONTEXT->nextData - (UINT8 *)sizePtr - 2 );
+	*(UINT16 *)sizePtr = BE_TO_HOST_16(SYS_CONTEXT->nextData - (UINT8 *)sizePtr - 2);
 
 	return;
 }
diff --git a/sysapi/sysapi_util/Marshal_TPM2B_PUBLIC.c b/sysapi/sysapi_util/Marshal_TPM2B_PUBLIC.c
index b1dad73..258c524 100644
--- a/sysapi/sysapi_util/Marshal_TPM2B_PUBLIC.c
+++ b/sysapi/sysapi_util/Marshal_TPM2B_PUBLIC.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 void Marshal_TPM2B_PUBLIC(
 	TSS2_SYS_CONTEXT *sysContext,
@@ -47,7 +48,7 @@
 		Marshal_TPMT_PUBLIC( sysContext, &publicVar->t.publicArea );
 	}
 
-	*(UINT16 *)sizePtr = CHANGE_ENDIAN_WORD( SYS_CONTEXT->nextData - (UINT8 *)sizePtr - 2 );
+	*(UINT16 *)sizePtr = BE_TO_HOST_16(SYS_CONTEXT->nextData - (UINT8 *)sizePtr - 2);
 
 	return;
 }
diff --git a/sysapi/sysapi_util/Marshal_TPM2B_SENSITIVE.c b/sysapi/sysapi_util/Marshal_TPM2B_SENSITIVE.c
index 68ede3e..8ddd88e 100644
--- a/sysapi/sysapi_util/Marshal_TPM2B_SENSITIVE.c
+++ b/sysapi/sysapi_util/Marshal_TPM2B_SENSITIVE.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 void Marshal_TPM2B_SENSITIVE(
 	TSS2_SYS_CONTEXT *sysContext,
@@ -47,7 +48,7 @@
 		Marshal_TPMT_SENSITIVE( sysContext, &sensitive->t.sensitiveArea );
 	}
 
-	*(UINT16 *)sizePtr = CHANGE_ENDIAN_WORD( SYS_CONTEXT->nextData - (UINT8 *)sizePtr - 2 );
+	*(UINT16 *)sizePtr = BE_TO_HOST_16(SYS_CONTEXT->nextData - (UINT8 *)sizePtr - 2);
 
 	return;
 }
diff --git a/sysapi/sysapi_util/Marshal_TPM2B_SENSITIVE_CREATE.c b/sysapi/sysapi_util/Marshal_TPM2B_SENSITIVE_CREATE.c
index 0c07d8d..0ca165c 100644
--- a/sysapi/sysapi_util/Marshal_TPM2B_SENSITIVE_CREATE.c
+++ b/sysapi/sysapi_util/Marshal_TPM2B_SENSITIVE_CREATE.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 void Marshal_TPM2B_SENSITIVE_CREATE(
 	TSS2_SYS_CONTEXT *sysContext,
@@ -47,7 +48,7 @@
 		Marshal_TPMS_SENSITIVE_CREATE( sysContext, &sensitiveCreate->t.sensitive );
 	}
 
-	*(UINT16 *)sizePtr = CHANGE_ENDIAN_WORD( SYS_CONTEXT->nextData - (UINT8 *)sizePtr - 2 );
+	*(UINT16 *)sizePtr = BE_TO_HOST_16(SYS_CONTEXT->nextData - (UINT8 *)sizePtr - 2);
 
 	return;
 }
diff --git a/sysapi/sysapi_util/changeEndian.c b/sysapi/sysapi_util/changeEndian.c
deleted file mode 100644
index 4c30af7..0000000
--- a/sysapi/sysapi_util/changeEndian.c
+++ /dev/null
@@ -1,55 +0,0 @@
-//**********************************************************************;
-// Copyright (c) 2015, Intel Corporation
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are met:
-//
-// 1. Redistributions of source code must retain the above copyright notice,
-// this list of conditions and the following disclaimer.
-//
-// 2. Redistributions in binary form must reproduce the above copyright notice,
-// this list of conditions and the following disclaimer in the documentation
-// and/or other materials provided with the distribution.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
-// THE POSSIBILITY OF SUCH DAMAGE.
-//**********************************************************************;
-
-#include "sapi/tpm20.h"
-#include "sysapi_util.h"
-
-UINT64 ChangeEndianQword( UINT64 p )
-{
-    return( ((const UINT64)(((p)& 0xFF) << 56))    | \
-          ((const UINT64)(((p)& 0xFF00) << 40))   | \
-          ((const UINT64)(((p)& 0xFF0000) << 24)) | \
-          ((const UINT64)(((p)& 0xFF000000) << 8)) | \
-          ((const UINT64)(((p)& 0xFF00000000) >> 8)) | \
-          ((const UINT64)(((p)& 0xFF0000000000) >> 24)) | \
-          ((const UINT64)(((p)& 0xFF000000000000) >> 40)) | \
-          ((const UINT64)(((p)& 0xFF00000000000000) >> 56)) );
-}
-
-UINT32 ChangeEndianDword( UINT32 p )
-{
-    return( ((const UINT32)(((p)& 0xFF) << 24))    | \
-          ((const UINT32)(((p)& 0xFF00) << 8))   | \
-          ((const UINT32)(((p)& 0xFF0000) >> 8)) | \
-          ((const UINT32)(((p)& 0xFF000000) >> 24)));
-}
-
-UINT16 ChangeEndianWord( UINT16 p )
-{
-    return( ((const UINT16)(((p)& 0xFF) << 8)) | ((const UINT16)(((p)& 0xFF00) >> 8)));
-}
-
diff --git a/sysapi/sysapi_util/unmarshal_simple_tpm2b.c b/sysapi/sysapi_util/unmarshal_simple_tpm2b.c
index de7cbaf..b667a14 100644
--- a/sysapi/sysapi_util/unmarshal_simple_tpm2b.c
+++ b/sysapi/sysapi_util/unmarshal_simple_tpm2b.c
@@ -27,6 +27,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 void Unmarshal_Simple_TPM2B( UINT8 *outBuffPtr, UINT32 maxResponseSize, UINT8 **nextData, TPM2B *outTPM2B, TSS2_RC *rval )
 {
@@ -47,7 +48,7 @@
         {
             if( *rval == TSS2_RC_SUCCESS )
             {
-                length = CHANGE_ENDIAN_WORD( *(UINT16 *)*nextData );
+                length = BE_TO_HOST_16(*(UINT16 *)*nextData);
 
                 if( outTPM2B != 0 )
                 {
diff --git a/sysapi/sysapi_util/unmarshal_simple_tpm2b_no_size_check.c b/sysapi/sysapi_util/unmarshal_simple_tpm2b_no_size_check.c
index 966afbd..56dedb9 100644
--- a/sysapi/sysapi_util/unmarshal_simple_tpm2b_no_size_check.c
+++ b/sysapi/sysapi_util/unmarshal_simple_tpm2b_no_size_check.c
@@ -26,6 +26,7 @@
 
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 void Unmarshal_Simple_TPM2B_NoSizeCheck( UINT8 *outBuffPtr, UINT32 maxResponseSize, UINT8 **nextData, TPM2B *outTPM2B, TSS2_RC *rval )
 {
@@ -46,7 +47,7 @@
         {
             if( *rval == TSS2_RC_SUCCESS )
             {
-                length = CHANGE_ENDIAN_WORD( *(UINT16 *)*nextData );
+                length = BE_TO_HOST_16(*(UINT16 *)*nextData);
 
                 if( outTPM2B != 0 )
                 {
diff --git a/tcti/tcti_socket.c b/tcti/tcti_socket.c
index cc8322e..eadf2a6 100644
--- a/tcti/tcti_socket.c
+++ b/tcti/tcti_socket.c
@@ -31,12 +31,12 @@
 
 #include "sapi/tpm20.h"
 #include "sapi/marshal.h"
-#include "marshal/base-types.h"
 #include "tcti/tcti_socket.h"
 #include "sysapi_util.h"
 #include "common/debug.h"
 #include "commonchecks.h"
 #include "logging.h"
+#include "tss2_endian.h"
 
 static TSS2_RC tctiRecvBytes( TSS2_TCTI_CONTEXT *tctiContext, SOCKET sock, unsigned char *data, int len )
 {
diff --git a/test/common/sample/TpmCalcPHash.c b/test/common/sample/TpmCalcPHash.c
index 2477c53..9a96842 100644
--- a/test/common/sample/TpmCalcPHash.c
+++ b/test/common/sample/TpmCalcPHash.c
@@ -30,6 +30,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 //
 // This function is a helper function used to calculate cpHash and rpHash.
@@ -112,7 +113,7 @@
     if( responseCode != TPM_RC_NO_RESPONSE )
     {
         hashInputPtr = &( hashInput.t.buffer[hashInput.b.size] );
-        *(UINT32 *)hashInputPtr = CHANGE_ENDIAN_DWORD( responseCode );
+        *(UINT32 *)hashInputPtr = BE_TO_HOST_32(responseCode);
         hashInput.b.size += 4;
         hashInputPtr += 4;
     }
@@ -123,7 +124,7 @@
         return rval;
 
     hashInputPtr = &( hashInput.t.buffer[hashInput.b.size] );
-    *(UINT32 *)hashInputPtr = CHANGE_ENDIAN_DWORD( *(UINT32 *)cmdCodePtr );
+    *(UINT32 *)hashInputPtr = BE_TO_HOST_32(*(UINT32 *)cmdCodePtr);
     hashInput.t.size += 4;
 
     // Create pHash input byte stream:  now add in names for the handles.
diff --git a/test/common/sample/TpmHandleToName.c b/test/common/sample/TpmHandleToName.c
index ce9298f..502e951 100644
--- a/test/common/sample/TpmHandleToName.c
+++ b/test/common/sample/TpmHandleToName.c
@@ -28,6 +28,7 @@
 #include "sapi/tpm20.h"
 #include "sample.h"
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 //
 //
@@ -77,7 +78,7 @@
             default:
                 rval = TPM_RC_SUCCESS;
                 name->b.size = sizeof( TPM_HANDLE );
-                *(TPM_HANDLE *)namePtr = CHANGE_ENDIAN_DWORD( handle );
+                *(TPM_HANDLE *)namePtr = BE_TO_HOST_32(handle);
         }
     }
     return rval;
diff --git a/test/common/sample/kdfa.c b/test/common/sample/kdfa.c
index 626b7b2..e5e7325 100644
--- a/test/common/sample/kdfa.c
+++ b/test/common/sample/kdfa.c
@@ -30,6 +30,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "sysapi_util.h"
+#include "tss2_endian.h"
 
 //
 //
@@ -57,7 +58,7 @@
     tpm2b_i_2.t.size = 4;
 
     tpm2bBits.t.size = 4;
-    bitsSwizzled = CHANGE_ENDIAN_DWORD( bits );
+    bitsSwizzled = BE_TO_HOST_32(bits);
     *(UINT32 *)tpm2bBitsPtr = bitsSwizzled;
 
     for(i = 0; label[i] != 0 ;i++ );
@@ -87,7 +88,7 @@
     {
         // Inner loop
 
-        i_Swizzled = CHANGE_ENDIAN_DWORD( i++ );
+        i_Swizzled = BE_TO_HOST_32(i++);
         *(UINT32 *)tpm2b_i_2Ptr = i_Swizzled;
 
         j = 0;
diff --git a/test/tpmclient/tpmclient.c b/test/tpmclient/tpmclient.c
index af69aa1..8bad79c 100644
--- a/test/tpmclient/tpmclient.c
+++ b/test/tpmclient/tpmclient.c
@@ -48,12 +48,12 @@
 #include <stdlib.h>   // Needed for _wtoi
 #include <string.h>
 
-#include "marshal/base-types.h"
 #include "sapi/tpm20.h"
 #include "sysapi_util.h"
 #include "test/common/sample/sample.h"
 #include "tpmclient.h"
 #include "common/tcti_util.h"
+#include "tss2_endian.h"
 
 // This is done to allow the tests to access fields
 // in the sysContext structure that are needed for
@@ -930,7 +930,7 @@
     // Get the command results
     // NOTE: this test modifies internal fields of the sysContext structure.
     // DON'T DO THIS IN REAL APPS!!
-    savedRspSize = CHANGE_ENDIAN_DWORD( ( (TPM20_Header_Out *)( SYS_CONTEXT->tpmOutBuffPtr )  )->responseSize );
+    savedRspSize = BE_TO_HOST_32(((TPM20_Header_Out *)(SYS_CONTEXT->tpmOutBuffPtr))->responseSize);
     ( (TPM20_Header_Out *)( SYS_CONTEXT->tpmOutBuffPtr )  )->responseSize = 4097;
     INIT_SIMPLE_TPM2B_SIZE( outData );
     rval = Tss2_Sys_GetTestResult_Complete( sysContext, &outData, &testResult );
@@ -960,7 +960,7 @@
     rval = Tss2_Sys_GetCapability( sysContext, 0, TPM_CAP_TPM_PROPERTIES, TPM_PT_MANUFACTURER, 1, &moreData, &capabilityData, 0 );
     CheckPassed( rval );
 
-    *( (UINT32 *)manuIDPtr ) = CHANGE_ENDIAN_DWORD( capabilityData.data.tpmProperties.tpmProperty[0].value );
+    *((UINT32 *)manuIDPtr) = BE_TO_HOST_32(capabilityData.data.tpmProperties.tpmProperty[0].value);
     DebugPrintf( NO_PREFIX, "\t\tcount: %d, property: %x, manuId: %s\n",
             capabilityData.data.tpmProperties.count,
             capabilityData.data.tpmProperties.tpmProperty[0].property,
@@ -5595,8 +5595,8 @@
     rval = Tss2_Sys_GetCpBuffer( decryptParamTestSysContext, &cpBufferUsedSize2, &cpBuffer2 );
     CheckPassed( rval );
     nvWriteData.t.size = MAX_NV_BUFFER_SIZE -
-            CHANGE_ENDIAN_DWORD( ( (TPM20_Header_In *)( ( (_TSS2_SYS_CONTEXT_BLOB *)decryptParamTestSysContext )->tpmInBuffPtr ) )->commandSize ) +
-            1;
+            BE_TO_HOST_32(((TPM20_Header_In *)(((_TSS2_SYS_CONTEXT_BLOB *)decryptParamTestSysContext)->tpmInBuffPtr))->commandSize) + 1;
+
     rval = Tss2_Sys_SetDecryptParam( decryptParamTestSysContext, nvWriteData.t.size, &( nvWriteData.t.buffer[0] ) );
     CheckFailed( rval, TSS2_SYS_RC_INSUFFICIENT_CONTEXT );
 
diff --git a/test/unit/UINT32-marshal.c b/test/unit/UINT32-marshal.c
index 514c7ea..7d69be8 100644
--- a/test/unit/UINT32-marshal.c
+++ b/test/unit/UINT32-marshal.c
@@ -4,8 +4,8 @@
 #include <setjmp.h>
 #include <cmocka.h>
 
-#include "marshal/base-types.h"
 #include "sapi/marshal.h"
+#include "tss2_endian.h"
 
 /*
  * Test case for successful UINT32 marshaling with NULL offset.
diff --git a/test/unit/UINT64-marshal.c b/test/unit/UINT64-marshal.c
index 58514d8..a18a59b 100644
--- a/test/unit/UINT64-marshal.c
+++ b/test/unit/UINT64-marshal.c
@@ -4,8 +4,8 @@
 #include <setjmp.h>
 #include <cmocka.h>
 
-#include "marshal/base-types.h"
 #include "sapi/marshal.h"
+#include "tss2_endian.h"
 
 /*
  * Test case for successful UINT64 marshaling with NULL offset.