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
36 //** Includes and Data Definitions
37
38 // This definition allows this module to "see" the values that are private
39 // to this module but kept in Global.c for ease of state migration.
40 #define IO_BUFFER_C
41 #include "Tpm.h"
42 #include "IoBuffers_fp.h"
43
44 //** Buffers and Functions
45
46 // These buffers are set aside to hold command and response values. In this
47 // implementation, it is not guaranteed that the code will stop accessing
48 // the s_actionInputBuffer before starting to put values in the
49 // s_actionOutputBuffer so different buffers are required.
50 //
51
52 //*** MemoryIoBufferAllocationReset()
53 // This function is used to reset the allocation of buffers.
54 void
MemoryIoBufferAllocationReset(void)55 MemoryIoBufferAllocationReset(
56 void
57 )
58 {
59 s_actionIoAllocation = 0;
60 }
61
62 //*** MemoryIoBufferZero()
63 // Function zeros the action I/O buffer at the end of a command. Calling this is
64 // not mandatory for proper functionality.
65 void
MemoryIoBufferZero(void)66 MemoryIoBufferZero(
67 void
68 )
69 {
70 memset(s_actionIoBuffer, 0, s_actionIoAllocation);
71 }
72
73 //*** MemoryGetInBuffer()
74 // This function returns the address of the buffer into which the
75 // command parameters will be unmarshaled in preparation for calling
76 // the command actions.
77 BYTE *
MemoryGetInBuffer(UINT32 size)78 MemoryGetInBuffer(
79 UINT32 size // Size, in bytes, required for the input
80 // unmarshaling
81 )
82 {
83 pAssert(size <= sizeof(s_actionIoBuffer));
84 // In this implementation, a static buffer is set aside for the command action
85 // buffers. The buffer is shared between input and output. This is because
86 // there is no need to allocate for the worst case input and worst case output
87 // at the same time.
88 // Round size up
89 #define UoM (sizeof(s_actionIoBuffer[0]))
90 size = (size + (UoM - 1)) & (UINT32_MAX - (UoM - 1));
91 memset(s_actionIoBuffer, 0, size);
92 s_actionIoAllocation = size;
93 return (BYTE *)&s_actionIoBuffer[0];
94 }
95
96 //*** MemoryGetOutBuffer()
97 // This function returns the address of the buffer into which the command
98 // action code places its output values.
99 BYTE *
MemoryGetOutBuffer(UINT32 size)100 MemoryGetOutBuffer(
101 UINT32 size // required size of the buffer
102 )
103 {
104 BYTE *retVal = (BYTE *)(&s_actionIoBuffer[s_actionIoAllocation / UoM]);
105 pAssert((size + s_actionIoAllocation) < (sizeof(s_actionIoBuffer)));
106 // In this implementation, a static buffer is set aside for the command action
107 // output buffer.
108 memset(retVal, 0, size);
109 s_actionIoAllocation += size;
110 return retVal;
111 }
112
113 //*** IsLabelProperlyFormatted()
114 // This function checks that a label is a null-terminated string.
115 // NOTE: this function is here because there was no better place for it.
116 // Return Type: BOOL
117 // TRUE(1) string is null terminated
118 // FALSE(0) string is not null terminated
119 BOOL
IsLabelProperlyFormatted(TPM2B * x)120 IsLabelProperlyFormatted(
121 TPM2B *x
122 )
123 {
124 return (((x)->size == 0) || ((x)->buffer[(x)->size - 1] == 0));
125 }
126