/src/libtpms/src/tpm2/Memory.c
Line | Count | Source (jump to first uncovered line) |
1 | | /********************************************************************************/ |
2 | | /* */ |
3 | | /* Miscellaneous Memory Manipulation Routines */ |
4 | | /* Written by Ken Goldman */ |
5 | | /* IBM Thomas J. Watson Research Center */ |
6 | | /* $Id: Memory.c 1658 2021-01-22 23:14:01Z kgoldman $ */ |
7 | | /* */ |
8 | | /* Licenses and Notices */ |
9 | | /* */ |
10 | | /* 1. Copyright Licenses: */ |
11 | | /* */ |
12 | | /* - Trusted Computing Group (TCG) grants to the user of the source code in */ |
13 | | /* this specification (the "Source Code") a worldwide, irrevocable, */ |
14 | | /* nonexclusive, royalty free, copyright license to reproduce, create */ |
15 | | /* derivative works, distribute, display and perform the Source Code and */ |
16 | | /* derivative works thereof, and to grant others the rights granted herein. */ |
17 | | /* */ |
18 | | /* - The TCG grants to the user of the other parts of the specification */ |
19 | | /* (other than the Source Code) the rights to reproduce, distribute, */ |
20 | | /* display, and perform the specification solely for the purpose of */ |
21 | | /* developing products based on such documents. */ |
22 | | /* */ |
23 | | /* 2. Source Code Distribution Conditions: */ |
24 | | /* */ |
25 | | /* - Redistributions of Source Code must retain the above copyright licenses, */ |
26 | | /* this list of conditions and the following disclaimers. */ |
27 | | /* */ |
28 | | /* - Redistributions in binary form must reproduce the above copyright */ |
29 | | /* licenses, this list of conditions and the following disclaimers in the */ |
30 | | /* documentation and/or other materials provided with the distribution. */ |
31 | | /* */ |
32 | | /* 3. Disclaimers: */ |
33 | | /* */ |
34 | | /* - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF */ |
35 | | /* LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH */ |
36 | | /* RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES) */ |
37 | | /* THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE. */ |
38 | | /* Contact TCG Administration (admin@trustedcomputinggroup.org) for */ |
39 | | /* information on specification licensing rights available through TCG */ |
40 | | /* membership agreements. */ |
41 | | /* */ |
42 | | /* - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED */ |
43 | | /* WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR */ |
44 | | /* FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR */ |
45 | | /* NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY */ |
46 | | /* OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE. */ |
47 | | /* */ |
48 | | /* - Without limitation, TCG and its members and licensors disclaim all */ |
49 | | /* liability, including liability for infringement of any proprietary */ |
50 | | /* rights, relating to use of information in this specification and to the */ |
51 | | /* implementation of this specification, and TCG disclaims all liability for */ |
52 | | /* cost of procurement of substitute goods or services, lost profits, loss */ |
53 | | /* of use, loss of data or any incidental, consequential, direct, indirect, */ |
54 | | /* or special damages, whether under contract, tort, warranty or otherwise, */ |
55 | | /* arising in any way out of use or reliance upon this specification or any */ |
56 | | /* information herein. */ |
57 | | /* */ |
58 | | /* (c) Copyright IBM Corp. and others, 2016 - 2021 */ |
59 | | /* */ |
60 | | /********************************************************************************/ |
61 | | |
62 | | //** Description |
63 | | // This file contains a set of miscellaneous memory manipulation routines. Many |
64 | | // of the functions have the same semantics as functions defined in string.h. |
65 | | // Those functions are not used directly in the TPM because they are not 'safe' |
66 | | // |
67 | | // This version uses string.h after adding guards. This is because the math |
68 | | // libraries invariably use those functions so it is not practical to prevent |
69 | | // those library functions from being pulled into the build. |
70 | | |
71 | | //** Includes and Data Definitions |
72 | | #include "Tpm.h" |
73 | | #include "Memory_fp.h" |
74 | | |
75 | | //** Functions |
76 | | |
77 | | //*** MemoryCopy() |
78 | | // This is an alias for memmove. This is used in place of memcpy because |
79 | | // some of the moves may overlap and rather than try to make sure that |
80 | | // memmove is used when necessary, it is always used. |
81 | | void MemoryCopy(void* dest, const void* src, int sSize) |
82 | 32.8k | { |
83 | 32.8k | if(dest != src) |
84 | 32.8k | memmove(dest, src, sSize); |
85 | 32.8k | } |
86 | | |
87 | | //*** MemoryEqual() |
88 | | // This function indicates if two buffers have the same values in the indicated |
89 | | // number of bytes. |
90 | | // Return Type: BOOL |
91 | | // TRUE(1) all octets are the same |
92 | | // FALSE(0) all octets are not the same |
93 | | BOOL MemoryEqual(const void* buffer1, // IN: compare buffer1 |
94 | | const void* buffer2, // IN: compare buffer2 |
95 | | unsigned int size // IN: size of bytes being compared |
96 | | ) |
97 | 4.25k | { |
98 | 4.25k | BYTE equal = 0; |
99 | 4.25k | const BYTE* b1 = (BYTE*)buffer1; |
100 | 4.25k | const BYTE* b2 = (BYTE*)buffer2; |
101 | | // |
102 | | // Compare all bytes so that there is no leakage of information |
103 | | // due to timing differences. |
104 | 90.3k | for(; size > 0; size--) |
105 | 86.0k | equal |= (*b1++ ^ *b2++); |
106 | 4.25k | return (equal == 0); |
107 | 4.25k | } |
108 | | |
109 | | //*** MemoryCopy2B() |
110 | | // This function copies a TPM2B. This can be used when the TPM2B types are |
111 | | // the same or different. |
112 | | // |
113 | | // This function returns the number of octets in the data buffer of the TPM2B. |
114 | | LIB_EXPORT INT16 MemoryCopy2B(TPM2B* dest, // OUT: receiving TPM2B |
115 | | const TPM2B* source, // IN: source TPM2B |
116 | | unsigned int dSize // IN: size of the receiving buffer |
117 | | ) |
118 | 3.11k | { |
119 | 3.11k | pAssert(dest != NULL); |
120 | 3.11k | if(source == NULL) |
121 | 114 | dest->size = 0; |
122 | 2.99k | else |
123 | 2.99k | { |
124 | 2.99k | pAssert(source->size <= dSize); |
125 | 2.99k | MemoryCopy(dest->buffer, source->buffer, source->size); |
126 | 2.99k | dest->size = source->size; |
127 | 2.99k | } |
128 | 3.11k | return dest->size; |
129 | 3.11k | } |
130 | | |
131 | | //*** MemoryConcat2B() |
132 | | // This function will concatenate the buffer contents of a TPM2B to |
133 | | // the buffer contents of another TPM2B and adjust the size accordingly |
134 | | // ('a' := ('a' | 'b')). |
135 | | void MemoryConcat2B( |
136 | | TPM2B* aInOut, // IN/OUT: destination 2B |
137 | | TPM2B* bIn, // IN: second 2B |
138 | | unsigned int aMaxSize // IN: The size of aInOut.buffer (max values for |
139 | | // aInOut.size) |
140 | | ) |
141 | 20 | { |
142 | 20 | pAssert(bIn->size <= aMaxSize - aInOut->size); |
143 | 20 | MemoryCopy(&aInOut->buffer[aInOut->size], &bIn->buffer, bIn->size); |
144 | 20 | aInOut->size = aInOut->size + bIn->size; |
145 | 20 | return; |
146 | 20 | } |
147 | | |
148 | | //*** MemoryEqual2B() |
149 | | // This function will compare two TPM2B structures. To be equal, they |
150 | | // need to be the same size and the buffer contexts need to be the same |
151 | | // in all octets. |
152 | | // Return Type: BOOL |
153 | | // TRUE(1) size and buffer contents are the same |
154 | | // FALSE(0) size or buffer contents are not the same |
155 | | BOOL MemoryEqual2B(const TPM2B* aIn, // IN: compare value |
156 | | const TPM2B* bIn // IN: compare value |
157 | | ) |
158 | 1.97k | { |
159 | 1.97k | if(aIn->size != bIn->size) |
160 | 62 | return FALSE; |
161 | 1.91k | return MemoryEqual(aIn->buffer, bIn->buffer, aIn->size); |
162 | 1.97k | } |
163 | | |
164 | | //*** MemorySet() |
165 | | // This function will set all the octets in the specified memory range to |
166 | | // the specified octet value. |
167 | | // Note: A previous version had an additional parameter (dSize) that was |
168 | | // intended to make sure that the destination would not be overrun. The |
169 | | // problem is that, in use, all that was happening was that the value of |
170 | | // size was used for dSize so there was no benefit in the extra parameter. |
171 | | void MemorySet(void* dest, int value, size_t size) |
172 | 1.42M | { |
173 | 1.42M | memset(dest, value, size); |
174 | 1.42M | } |
175 | | |
176 | | //*** MemoryPad2B() |
177 | | // Function to pad a TPM2B with zeros and adjust the size. |
178 | | void MemoryPad2B(TPM2B* b, UINT16 newSize) |
179 | 600 | { |
180 | 600 | MemorySet(&b->buffer[b->size], 0, newSize - b->size); |
181 | 600 | b->size = newSize; |
182 | 600 | } |
183 | | |
184 | | //*** Uint16ToByteArray() |
185 | | // Function to write an integer to a byte array |
186 | | void Uint16ToByteArray(UINT16 i, BYTE* a) |
187 | 1.91k | { |
188 | 1.91k | a[1] = (BYTE)(i); |
189 | 1.91k | i >>= 8; |
190 | 1.91k | a[0] = (BYTE)(i); |
191 | 1.91k | } |
192 | | |
193 | | //*** Uint32ToByteArray() |
194 | | // Function to write an integer to a byte array |
195 | | void Uint32ToByteArray(UINT32 i, BYTE* a) |
196 | 2.72k | { |
197 | 2.72k | a[3] = (BYTE)(i); |
198 | 2.72k | i >>= 8; |
199 | 2.72k | a[2] = (BYTE)(i); |
200 | 2.72k | i >>= 8; |
201 | 2.72k | a[1] = (BYTE)(i); |
202 | 2.72k | i >>= 8; |
203 | 2.72k | a[0] = (BYTE)(i); |
204 | 2.72k | } |
205 | | |
206 | | //*** Uint64ToByteArray() |
207 | | // Function to write an integer to a byte array |
208 | | void Uint64ToByteArray(UINT64 i, BYTE* a) |
209 | 112 | { |
210 | 112 | a[7] = (BYTE)(i); |
211 | 112 | i >>= 8; |
212 | 112 | a[6] = (BYTE)(i); |
213 | 112 | i >>= 8; |
214 | 112 | a[5] = (BYTE)(i); |
215 | 112 | i >>= 8; |
216 | 112 | a[4] = (BYTE)(i); |
217 | 112 | i >>= 8; |
218 | 112 | a[3] = (BYTE)(i); |
219 | 112 | i >>= 8; |
220 | 112 | a[2] = (BYTE)(i); |
221 | 112 | i >>= 8; |
222 | 112 | a[1] = (BYTE)(i); |
223 | 112 | i >>= 8; |
224 | 112 | a[0] = (BYTE)(i); |
225 | 112 | } |
226 | | |
227 | | #if 0 // libtpms added |
228 | | //*** ByteArrayToUint8() |
229 | | // Function to write a UINT8 to a byte array. This is included for completeness |
230 | | // and to allow certain macro expansions |
231 | | UINT8 |
232 | | ByteArrayToUint8(BYTE* a) |
233 | | { |
234 | | return *a; |
235 | | } |
236 | | #endif // libtpms added |
237 | | |
238 | | //*** ByteArrayToUint16() |
239 | | // Function to write an integer to a byte array |
240 | | UINT16 |
241 | | ByteArrayToUint16(BYTE* a) |
242 | 0 | { |
243 | 0 | return ((UINT16)a[0] << 8) + a[1]; |
244 | 0 | } |
245 | | |
246 | | //*** ByteArrayToUint32() |
247 | | // Function to write an integer to a byte array |
248 | | UINT32 |
249 | | ByteArrayToUint32(BYTE* a) |
250 | 1.30k | { |
251 | 1.30k | return (UINT32)((((((UINT32)a[0] << 8) + a[1]) << 8) + (UINT32)a[2]) << 8) + a[3]; |
252 | 1.30k | } |
253 | | |
254 | | //*** ByteArrayToUint64() |
255 | | // Function to write an integer to a byte array |
256 | | UINT64 |
257 | | ByteArrayToUint64(BYTE* a) |
258 | 651 | { |
259 | 651 | return (((UINT64)BYTE_ARRAY_TO_UINT32(a)) << 32) + BYTE_ARRAY_TO_UINT32(&a[4]); |
260 | 651 | } |