| 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 |
//** Description |
| 36 |
// This file contains a set of miscellaneous memory manipulation routines. Many |
| 37 |
// of the functions have the same semantics as functions defined in string.h. |
| 38 |
// Those functions are not used directly in the TPM because they are not 'safe' |
| 39 |
// |
| 40 |
// This version uses string.h after adding guards. This is because the math |
| 41 |
// libraries invariably use those functions so it is not practical to prevent |
| 42 |
// those library functions from being pulled into the build. |
| 43 |
|
| 44 |
//** Includes and Data Definitions |
| 45 |
#include "Tpm.h" |
| 46 |
#include "Memory_fp.h" |
| 47 |
|
| 48 |
//** Functions |
| 49 |
|
| 50 |
//*** MemoryCopy() |
| 51 |
// This is an alias for memmove. This is used in place of memcpy because |
| 52 |
// some of the moves may overlap and rather than try to make sure that |
| 53 |
// memmove is used when necessary, it is always used. |
| 54 |
void |
| 55 |
MemoryCopy( |
| 56 |
void *dest, |
| 57 |
const void *src, |
| 58 |
int sSize |
| 59 |
) |
| 60 |
{ |
| 61 |
if(dest != src) |
| 62 |
memmove(dest, src, sSize); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
//*** MemoryEqual() |
| 67 |
// This function indicates if two buffers have the same values in the indicated |
| 68 |
// number of bytes. |
| 69 |
// Return Type: BOOL |
| 70 |
// TRUE(1) all octets are the same |
| 71 |
// FALSE(0) all octets are not the same |
| 72 |
BOOL |
| 73 |
MemoryEqual( |
| 74 |
const void *buffer1, // IN: compare buffer1 |
| 75 |
const void *buffer2, // IN: compare buffer2 |
| 76 |
unsigned int size // IN: size of bytes being compared |
| 77 |
) |
| 78 |
{ |
| 79 |
BYTE equal = 0; |
| 80 |
const BYTE *b1 = (BYTE *)buffer1; |
| 81 |
const BYTE *b2 = (BYTE *)buffer2; |
| 82 |
// |
| 83 |
// Compare all bytes so that there is no leakage of information |
| 84 |
// due to timing differences. |
| 85 |
for(; size > 0; size--) |
| 86 |
equal |= (*b1++ ^ *b2++); |
| 87 |
return (equal == 0); |
| 88 |
} |
| 89 |
|
| 90 |
//*** MemoryCopy2B() |
| 91 |
// This function copies a TPM2B. This can be used when the TPM2B types are |
| 92 |
// the same or different. |
| 93 |
// |
| 94 |
// This function returns the number of octets in the data buffer of the TPM2B. |
| 95 |
LIB_EXPORT INT16 |
| 96 |
MemoryCopy2B( |
| 97 |
TPM2B *dest, // OUT: receiving TPM2B |
| 98 |
const TPM2B *source, // IN: source TPM2B |
| 99 |
unsigned int dSize // IN: size of the receiving buffer |
| 100 |
) |
| 101 |
{ |
| 102 |
pAssert(dest != NULL); |
| 103 |
if(source == NULL) |
| 104 |
dest->size = 0; |
| 105 |
else |
| 106 |
{ |
| 107 |
pAssert(source->size <= dSize); |
| 108 |
MemoryCopy(dest->buffer, source->buffer, source->size); |
| 109 |
dest->size = source->size; |
| 110 |
} |
| 111 |
return dest->size; |
| 112 |
} |
| 113 |
|
| 114 |
//*** MemoryConcat2B() |
| 115 |
// This function will concatenate the buffer contents of a TPM2B to an |
| 116 |
// the buffer contents of another TPM2B and adjust the size accordingly |
| 117 |
// ('a' := ('a' | 'b')). |
| 118 |
void |
| 119 |
MemoryConcat2B( |
| 120 |
TPM2B *aInOut, // IN/OUT: destination 2B |
| 121 |
TPM2B *bIn, // IN: second 2B |
| 122 |
unsigned int aMaxSize // IN: The size of aInOut.buffer (max values for |
| 123 |
// aInOut.size) |
| 124 |
) |
| 125 |
{ |
| 126 |
pAssert(bIn->size <= aMaxSize - aInOut->size); |
| 127 |
MemoryCopy(&aInOut->buffer[aInOut->size], &bIn->buffer, bIn->size); |
| 128 |
aInOut->size = aInOut->size + bIn->size; |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
//*** MemoryEqual2B() |
| 133 |
// This function will compare two TPM2B structures. To be equal, they |
| 134 |
// need to be the same size and the buffer contexts need to be the same |
| 135 |
// in all octets. |
| 136 |
// Return Type: BOOL |
| 137 |
// TRUE(1) size and buffer contents are the same |
| 138 |
// FALSE(0) size or buffer contents are not the same |
| 139 |
BOOL |
| 140 |
MemoryEqual2B( |
| 141 |
const TPM2B *aIn, // IN: compare value |
| 142 |
const TPM2B *bIn // IN: compare value |
| 143 |
) |
| 144 |
{ |
| 145 |
if(aIn->size != bIn->size) |
| 146 |
return FALSE; |
| 147 |
return MemoryEqual(aIn->buffer, bIn->buffer, aIn->size); |
| 148 |
} |
| 149 |
|
| 150 |
//*** MemorySet() |
| 151 |
// This function will set all the octets in the specified memory range to |
| 152 |
// the specified octet value. |
| 153 |
// Note: A previous version had an additional parameter (dSize) that was |
| 154 |
// intended to make sure that the destination would not be overrun. The |
| 155 |
// problem is that, in use, all that was happening was that the value of |
| 156 |
// size was used for dSize so there was no benefit in the extra parameter. |
| 157 |
void |
| 158 |
MemorySet( |
| 159 |
void *dest, |
| 160 |
int value, |
| 161 |
size_t size |
| 162 |
) |
| 163 |
{ |
| 164 |
memset(dest, value, size); |
| 165 |
} |
| 166 |
|
| 167 |
//*** MemoryPad2B() |
| 168 |
// Function to pad a TPM2B with zeros and adjust the size. |
| 169 |
void |
| 170 |
MemoryPad2B( |
| 171 |
TPM2B *b, |
| 172 |
UINT16 newSize |
| 173 |
) |
| 174 |
{ |
| 175 |
MemorySet(&b->buffer[b->size], 0, newSize - b->size); |
| 176 |
b->size = newSize; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
//*** Uint16ToByteArray() |
| 181 |
// Function to write an integer to a byte array |
| 182 |
void |
| 183 |
Uint16ToByteArray( |
| 184 |
UINT16 i, |
| 185 |
BYTE *a |
| 186 |
) |
| 187 |
{ |
| 188 |
a[1] = (BYTE)(i); i >>= 8; |
| 189 |
a[0] = (BYTE)(i); |
| 190 |
} |
| 191 |
|
| 192 |
//*** Uint32ToByteArray() |
| 193 |
// Function to write an integer to a byte array |
| 194 |
void |
| 195 |
Uint32ToByteArray( |
| 196 |
UINT32 i, |
| 197 |
BYTE *a |
| 198 |
) |
| 199 |
{ |
| 200 |
a[3] = (BYTE)(i); i >>= 8; |
| 201 |
a[2] = (BYTE)(i); i >>= 8; |
| 202 |
a[1] = (BYTE)(i); i >>= 8; |
| 203 |
a[0] = (BYTE)(i); |
| 204 |
} |
| 205 |
|
| 206 |
//*** Uint64ToByteArray() |
| 207 |
// Function to write an integer to a byte array |
| 208 |
void |
| 209 |
Uint64ToByteArray( |
| 210 |
UINT64 i, |
| 211 |
BYTE *a |
| 212 |
) |
| 213 |
{ |
| 214 |
a[7] = (BYTE)(i); i >>= 8; |
| 215 |
a[6] = (BYTE)(i); i >>= 8; |
| 216 |
a[5] = (BYTE)(i); i >>= 8; |
| 217 |
a[4] = (BYTE)(i); i >>= 8; |
| 218 |
a[3] = (BYTE)(i); i >>= 8; |
| 219 |
a[2] = (BYTE)(i); i >>= 8; |
| 220 |
a[1] = (BYTE)(i); i >>= 8; |
| 221 |
a[0] = (BYTE)(i); |
| 222 |
} |
| 223 |
|
| 224 |
//*** ByteArrayToUint8() |
| 225 |
// Function to write a UINT8 to a byte array. This is included for completeness |
| 226 |
// and to allow certain macro expansions |
| 227 |
UINT8 |
| 228 |
ByteArrayToUint8( |
| 229 |
BYTE *a |
| 230 |
) |
| 231 |
{ |
| 232 |
return *a; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
//*** ByteArrayToUint16() |
| 237 |
// Function to write an integer to a byte array |
| 238 |
UINT16 |
| 239 |
ByteArrayToUint16( |
| 240 |
BYTE *a |
| 241 |
) |
| 242 |
{ |
| 243 |
return ((UINT16)a[0] << 8) + a[1]; |
| 244 |
} |
| 245 |
|
| 246 |
//*** ByteArrayToUint32() |
| 247 |
// Function to write an integer to a byte array |
| 248 |
UINT32 |
| 249 |
ByteArrayToUint32( |
| 250 |
BYTE *a |
| 251 |
) |
| 252 |
{ |
| 253 |
return (UINT32)((((((UINT32)a[0] << 8) + a[1]) << 8) + (UINT32)a[2]) << 8) + a[3]; |
| 254 |
} |
| 255 |
|
| 256 |
//*** ByteArrayToUint64() |
| 257 |
// Function to write an integer to a byte array |
| 258 |
UINT64 |
| 259 |
ByteArrayToUint64( |
| 260 |
BYTE *a |
| 261 |
) |
| 262 |
{ |
| 263 |
return (((UINT64)BYTE_ARRAY_TO_UINT32(a)) << 32) + BYTE_ARRAY_TO_UINT32(&a[4]); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|