| 1 |
/* Microsoft Reference Implementation for TPM 2.0 |
| 2 |
* |
| 3 |
* The copyright in this software is being made available under the BSD License, |
| 4 |
* included below. This software may be subject to other third party and |
| 5 |
* contributor rights, including patent rights, and no such rights are granted |
| 6 |
* under this license. |
| 7 |
* |
| 8 |
* Copyright (c) Microsoft Corporation |
| 9 |
* |
| 10 |
* All rights reserved. |
| 11 |
* |
| 12 |
* BSD License |
| 13 |
* |
| 14 |
* Redistribution and use in source and binary forms, with or without modification, |
| 15 |
* are permitted provided that the following conditions are met: |
| 16 |
* |
| 17 |
* Redistributions of source code must retain the above copyright notice, this list |
| 18 |
* of conditions and the following disclaimer. |
| 19 |
* |
| 20 |
* Redistributions in binary form must reproduce the above copyright notice, this |
| 21 |
* list of conditions and the following disclaimer in the documentation and/or |
| 22 |
* other materials provided with the distribution. |
| 23 |
* |
| 24 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS"" |
| 25 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 28 |
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 31 |
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 |
*/ |
| 35 |
#ifndef _SWAP_H |
| 36 |
#define _SWAP_H |
| 37 |
|
| 38 |
#if LITTLE_ENDIAN_TPM |
| 39 |
#define TO_BIG_ENDIAN_UINT16(i) REVERSE_ENDIAN_16(i) |
| 40 |
#define FROM_BIG_ENDIAN_UINT16(i) REVERSE_ENDIAN_16(i) |
| 41 |
#define TO_BIG_ENDIAN_UINT32(i) REVERSE_ENDIAN_32(i) |
| 42 |
#define FROM_BIG_ENDIAN_UINT32(i) REVERSE_ENDIAN_32(i) |
| 43 |
#define TO_BIG_ENDIAN_UINT64(i) REVERSE_ENDIAN_64(i) |
| 44 |
#define FROM_BIG_ENDIAN_UINT64(i) REVERSE_ENDIAN_64(i) |
| 45 |
#else |
| 46 |
#define TO_BIG_ENDIAN_UINT16(i) (i) |
| 47 |
#define FROM_BIG_ENDIAN_UINT16(i) (i) |
| 48 |
#define TO_BIG_ENDIAN_UINT32(i) (i) |
| 49 |
#define FROM_BIG_ENDIAN_UINT32(i) (i) |
| 50 |
#define TO_BIG_ENDIAN_UINT64(i) (i) |
| 51 |
#define FROM_BIG_ENDIAN_UINT64(i) (i) |
| 52 |
#endif |
| 53 |
|
| 54 |
#if AUTO_ALIGN == NO |
| 55 |
|
| 56 |
// The aggregation macros for machines that do not allow unaligned access or for |
| 57 |
// little-endian machines. |
| 58 |
|
| 59 |
// Aggregate bytes into an UINT |
| 60 |
|
| 61 |
#define BYTE_ARRAY_TO_UINT8(b) (uint8_t)((b)[0]) |
| 62 |
#define BYTE_ARRAY_TO_UINT16(b) ByteArrayToUint16((BYTE *)(b)) |
| 63 |
#define BYTE_ARRAY_TO_UINT32(b) ByteArrayToUint32((BYTE *)(b)) |
| 64 |
#define BYTE_ARRAY_TO_UINT64(b) ByteArrayToUint64((BYTE *)(b)) |
| 65 |
#define UINT8_TO_BYTE_ARRAY(i, b) ((b)[0] = (uint8_t)(i)) |
| 66 |
#define UINT16_TO_BYTE_ARRAY(i, b) Uint16ToByteArray((i), (BYTE *)(b)) |
| 67 |
#define UINT32_TO_BYTE_ARRAY(i, b) Uint32ToByteArray((i), (BYTE *)(b)) |
| 68 |
#define UINT64_TO_BYTE_ARRAY(i, b) Uint64ToByteArray((i), (BYTE *)(b)) |
| 69 |
|
| 70 |
|
| 71 |
#else // AUTO_ALIGN |
| 72 |
|
| 73 |
#if BIG_ENDIAN_TPM |
| 74 |
// the big-endian macros for machines that allow unaligned memory access |
| 75 |
// Aggregate a byte array into a UINT |
| 76 |
#define BYTE_ARRAY_TO_UINT8(b) *((uint8_t *)(b)) |
| 77 |
#define BYTE_ARRAY_TO_UINT16(b) *((uint16_t *)(b)) |
| 78 |
#define BYTE_ARRAY_TO_UINT32(b) *((uint32_t *)(b)) |
| 79 |
#define BYTE_ARRAY_TO_UINT64(b) *((uint64_t *)(b)) |
| 80 |
|
| 81 |
// Disaggregate a UINT into a byte array |
| 82 |
|
| 83 |
#define UINT8_TO_BYTE_ARRAY(i, b) {*((uint8_t *)(b)) = (i);} |
| 84 |
#define UINT16_TO_BYTE_ARRAY(i, b) {*((uint16_t *)(b)) = (i);} |
| 85 |
#define UINT32_TO_BYTE_ARRAY(i, b) {*((uint32_t *)(b)) = (i);} |
| 86 |
#define UINT64_TO_BYTE_ARRAY(i, b) {*((uint64_t *)(b)) = (i);} |
| 87 |
#else |
| 88 |
// the little endian macros for machines that allow unaligned memory access |
| 89 |
// the big-endian macros for machines that allow unaligned memory access |
| 90 |
// Aggregate a byte array into a UINT |
| 91 |
#define BYTE_ARRAY_TO_UINT8(b) *((uint8_t *)(b)) |
| 92 |
#define BYTE_ARRAY_TO_UINT16(b) REVERSE_ENDIAN_16(*((uint16_t *)(b))) |
| 93 |
#define BYTE_ARRAY_TO_UINT32(b) REVERSE_ENDIAN_32(*((uint32_t *)(b))) |
| 94 |
#define BYTE_ARRAY_TO_UINT64(b) REVERSE_ENDIAN_64(*((uint64_t *)(b))) |
| 95 |
|
| 96 |
// Disaggregate a UINT into a byte array |
| 97 |
|
| 98 |
#define UINT8_TO_BYTE_ARRAY(i, b) {*((uint8_t *)(b)) = (i);} |
| 99 |
#define UINT16_TO_BYTE_ARRAY(i, b) {*((uint16_t *)(b)) = REVERSE_ENDIAN_16(i);} |
| 100 |
#define UINT32_TO_BYTE_ARRAY(i, b) {*((uint32_t *)(b)) = REVERSE_ENDIAN_32(i);} |
| 101 |
#define UINT64_TO_BYTE_ARRAY(i, b) {*((uint64_t *)(b)) = REVERSE_ENDIAN_64(i);} |
| 102 |
#endif // BIG_ENDIAN_TPM |
| 103 |
|
| 104 |
#endif // AUTO_ALIGN == NO |
| 105 |
|
| 106 |
#endif // _SWAP_H |