Rename *_realloc_clean to *_clear_realloc
[openssl.git] / crypto / buffer / buffer.c
1 /* crypto/buffer/buffer.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include "internal/cryptlib.h"
61 #include <openssl/buffer.h>
62
63 /*
64  * LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That
65  * function is applied in several functions in this file and this limit
66  * ensures that the result fits in an int.
67  */
68 #define LIMIT_BEFORE_EXPANSION 0x5ffffffc
69
70 BUF_MEM *BUF_MEM_new_ex(unsigned long flags)
71 {
72     BUF_MEM *ret;
73
74     ret = BUF_MEM_new();
75     if (ret != NULL)
76         ret->flags = flags;
77     return (ret);
78 }
79
80 BUF_MEM *BUF_MEM_new(void)
81 {
82     BUF_MEM *ret;
83
84     ret = OPENSSL_zalloc(sizeof(*ret));
85     if (ret == NULL) {
86         BUFerr(BUF_F_BUF_MEM_NEW, ERR_R_MALLOC_FAILURE);
87         return (NULL);
88     }
89     return (ret);
90 }
91
92 void BUF_MEM_free(BUF_MEM *a)
93 {
94     if (a == NULL)
95         return;
96
97     if (a->data != NULL) {
98         memset(a->data, 0, (unsigned int)a->max);
99         if (a->flags & BUF_MEM_FLAG_SECURE)
100             OPENSSL_secure_free(a->data);
101         else
102             OPENSSL_clear_free(a->data, a->max);
103     }
104     OPENSSL_free(a);
105 }
106
107 /* Allocate a block of secure memory; copy over old data if there
108  * was any, and then free it. */
109 static char *sec_alloc_realloc(BUF_MEM *str, size_t len)
110 {
111     char *ret;
112
113     ret = OPENSSL_secure_malloc(len);
114     if (str->data != NULL) {
115         if (ret != NULL)
116             memcpy(ret, str->data, str->length);
117         OPENSSL_secure_free(str->data);
118     }
119     return (ret);
120 }
121
122 size_t BUF_MEM_grow(BUF_MEM *str, size_t len)
123 {
124     char *ret;
125     size_t n;
126
127     if (str->length >= len) {
128         str->length = len;
129         return (len);
130     }
131     if (str->max >= len) {
132         memset(&str->data[str->length], 0, len - str->length);
133         str->length = len;
134         return (len);
135     }
136     /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
137     if (len > LIMIT_BEFORE_EXPANSION) {
138         BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);
139         return 0;
140     }
141     n = (len + 3) / 3 * 4;
142     if ((str->flags & BUF_MEM_FLAG_SECURE))
143         ret = sec_alloc_realloc(str, n);
144     else
145         ret = OPENSSL_realloc(str->data, n);
146     if (ret == NULL) {
147         BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);
148         len = 0;
149     } else {
150         str->data = ret;
151         str->max = n;
152         memset(&str->data[str->length], 0, len - str->length);
153         str->length = len;
154     }
155     return (len);
156 }
157
158 size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
159 {
160     char *ret;
161     size_t n;
162
163     if (str->length >= len) {
164         memset(&str->data[len], 0, str->length - len);
165         str->length = len;
166         return (len);
167     }
168     if (str->max >= len) {
169         memset(&str->data[str->length], 0, len - str->length);
170         str->length = len;
171         return (len);
172     }
173     /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
174     if (len > LIMIT_BEFORE_EXPANSION) {
175         BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);
176         return 0;
177     }
178     n = (len + 3) / 3 * 4;
179     if ((str->flags & BUF_MEM_FLAG_SECURE))
180         ret = sec_alloc_realloc(str, n);
181     else
182         ret = OPENSSL_clear_realloc(str->data, str->max, n);
183     if (ret == NULL) {
184         BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, ERR_R_MALLOC_FAILURE);
185         len = 0;
186     } else {
187         str->data = ret;
188         str->max = n;
189         memset(&str->data[str->length], 0, len - str->length);
190         str->length = len;
191     }
192     return (len);
193 }
194
195 void BUF_reverse(unsigned char *out, unsigned char *in, size_t size)
196 {
197     size_t i;
198     if (in) {
199         out += size - 1;
200         for (i = 0; i < size; i++)
201             *out-- = *in++;
202     } else {
203         unsigned char *q;
204         char c;
205         q = out + size - 1;
206         for (i = 0; i < size / 2; i++) {
207             c = *q;
208             *q-- = *out;
209             *out++ = c;
210         }
211     }
212 }