]> git.proxmox.com Git - libtpms.git/blob - src/tpm12/tpm_memory.c
tpm2: Use EC_POINT_set/get_affine_coordinates on OpenSSL >= 1.1
[libtpms.git] / src / tpm12 / tpm_memory.c
1 /********************************************************************************/
2 /* */
3 /* TPM Memory Allocation */
4 /* Written by Ken Goldman */
5 /* IBM Thomas J. Watson Research Center */
6 /* $Id: tpm_memory.c 4609 2011-08-26 19:27:38Z kgoldman $ */
7 /* */
8 /* (c) Copyright IBM Corporation 2006, 2010. */
9 /* */
10 /* All rights reserved. */
11 /* */
12 /* Redistribution and use in source and binary forms, with or without */
13 /* modification, are permitted provided that the following conditions are */
14 /* met: */
15 /* */
16 /* Redistributions of source code must retain the above copyright notice, */
17 /* this list of conditions and the following disclaimer. */
18 /* */
19 /* Redistributions in binary form must reproduce the above copyright */
20 /* notice, this list of conditions and the following disclaimer in the */
21 /* documentation and/or other materials provided with the distribution. */
22 /* */
23 /* Neither the names of the IBM Corporation nor the names of its */
24 /* contributors may be used to endorse or promote products derived from */
25 /* this software without specific prior written permission. */
26 /* */
27 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
28 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
29 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
30 /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
31 /* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
32 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
33 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
34 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
35 /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
36 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
37 /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
38 /********************************************************************************/
39
40 #include <stdio.h>
41 #include <stdlib.h>
42
43 #include "tpm_constants.h"
44 #include "tpm_debug.h"
45 #include "tpm_error.h"
46
47 #include "tpm_memory.h"
48
49 /* TPM_Malloc() is a general purpose wrapper around malloc()
50 */
51
52 TPM_RESULT TPM_Malloc(unsigned char **buffer, uint32_t size)
53 {
54 TPM_RESULT rc = 0;
55
56 /* assertion test. The coding style requires that all allocated pointers are initialized to
57 NULL. A non-NULL value indicates either a missing initialization or a pointer reuse (a
58 memory leak). */
59 if (rc == 0) {
60 if (*buffer != NULL) {
61 printf("TPM_Malloc: Error (fatal), *buffer %p should be NULL before malloc\n", *buffer);
62 rc = TPM_FAIL;
63 }
64 }
65 /* verify that the size is not "too large" */
66 if (rc == 0) {
67 if (size > TPM_ALLOC_MAX) {
68 printf("TPM_Malloc: Error, size %u greater than maximum allowed\n", size);
69 rc = TPM_SIZE;
70 }
71 }
72 /* verify that the size is not 0, this would be implementation defined and should never occur */
73 if (rc == 0) {
74 if (size == 0) {
75 printf("TPM_Malloc: Error (fatal), size is zero\n");
76 rc = TPM_FAIL;
77 }
78 }
79 if (rc == 0) {
80 *buffer = malloc(size);
81 if (*buffer == NULL) {
82 printf("TPM_Malloc: Error allocating %u bytes\n", size);
83 rc = TPM_SIZE;
84 }
85 }
86 return rc;
87 }
88
89 /* TPM_Realloc() is a general purpose wrapper around realloc()
90 */
91
92 TPM_RESULT TPM_Realloc(unsigned char **buffer,
93 uint32_t size)
94 {
95 TPM_RESULT rc = 0;
96 unsigned char *tmpptr = NULL;
97
98 /* verify that the size is not "too large" */
99 if (rc == 0) {
100 if (size > TPM_ALLOC_MAX) {
101 printf("TPM_Realloc: Error, size %u greater than maximum allowed\n", size);
102 rc = TPM_SIZE;
103 }
104 }
105 if (rc == 0) {
106 tmpptr = realloc(*buffer, size);
107 if (tmpptr == NULL) {
108 printf("TPM_Realloc: Error reallocating %u bytes\n", size);
109 rc = TPM_SIZE;
110 }
111 }
112 if (rc == 0) {
113 *buffer = tmpptr;
114 }
115 return rc;
116 }
117
118 /* TPM_Free() is the companion to the TPM allocation functions. It is not used internally. The
119 intent is for use by an application that links directly to a TPM and wants to free memory
120 allocated by the TPM.
121
122 It avoids a potential problem if the application uses a different allocation library, perhaps one
123 that wraps the functions to detect overflows or memory leaks.
124 */
125
126 void TPM_Free(unsigned char *buffer)
127 {
128 free(buffer);
129 return;
130 }
131