1*ead8f799Sbeck /* $OpenBSD: buffer.c,v 1.28 2023/07/08 08:26:26 beck Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker * All rights reserved.
45b37fcf3Sryker *
55b37fcf3Sryker * This package is an SSL implementation written
65b37fcf3Sryker * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker *
95b37fcf3Sryker * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker * the following conditions are aheared to. The following conditions
115b37fcf3Sryker * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker * lhash, DES, etc., code; not just the SSL code. The SSL documentation
135b37fcf3Sryker * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker *
165b37fcf3Sryker * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker * the code are not to be removed.
185b37fcf3Sryker * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker * as the author of the parts of the library used.
205b37fcf3Sryker * This can be in the form of a textual message at program startup or
215b37fcf3Sryker * in documentation (online or textual) provided with the package.
225b37fcf3Sryker *
235b37fcf3Sryker * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker * modification, are permitted provided that the following conditions
255b37fcf3Sryker * are met:
265b37fcf3Sryker * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker * notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker * notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker * documentation and/or other materials provided with the distribution.
315b37fcf3Sryker * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker * must display the following acknowledgement:
335b37fcf3Sryker * "This product includes cryptographic software written by
345b37fcf3Sryker * Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker * The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker * being used are not cryptographic related :-).
375b37fcf3Sryker * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker * the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker *
415b37fcf3Sryker * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker * SUCH DAMAGE.
525b37fcf3Sryker *
535b37fcf3Sryker * The licence and distribution terms for any publically available version or
545b37fcf3Sryker * derivative of this code cannot be changed. i.e. this code cannot simply be
555b37fcf3Sryker * copied and put under another distribution licence
565b37fcf3Sryker * [including the GNU Public Licence.]
575b37fcf3Sryker */
585b37fcf3Sryker
595b37fcf3Sryker #include <stdio.h>
60b6ab114eSjsing #include <stdlib.h>
61a8913c44Sjsing #include <string.h>
62a8913c44Sjsing
63913ec974Sbeck #include <openssl/buffer.h>
64b6ab114eSjsing #include <openssl/err.h>
655b37fcf3Sryker
667f2c4a2bSjsing /*
677f2c4a2bSjsing * LIMIT_BEFORE_EXPANSION is the maximum n such that (n + 3) / 3 * 4 < 2**31.
687f2c4a2bSjsing * That function is applied in several functions in this file and this limit
697f2c4a2bSjsing * ensures that the result fits in an int.
707f2c4a2bSjsing */
71b590e19bSdjm #define LIMIT_BEFORE_EXPANSION 0x5ffffffc
72b590e19bSdjm
7379e8bed4Sjsing BUF_MEM *
BUF_MEM_new(void)7479e8bed4Sjsing BUF_MEM_new(void)
755b37fcf3Sryker {
765b37fcf3Sryker BUF_MEM *ret;
775b37fcf3Sryker
78e21846dbSjsing if ((ret = calloc(1, sizeof(BUF_MEM))) == NULL) {
795067ae9fSbeck BUFerror(ERR_R_MALLOC_FAILURE);
805b37fcf3Sryker return (NULL);
815b37fcf3Sryker }
82e21846dbSjsing
835b37fcf3Sryker return (ret);
845b37fcf3Sryker }
85*ead8f799Sbeck LCRYPTO_ALIAS(BUF_MEM_new);
865b37fcf3Sryker
8779e8bed4Sjsing void
BUF_MEM_free(BUF_MEM * a)8879e8bed4Sjsing BUF_MEM_free(BUF_MEM *a)
895b37fcf3Sryker {
90913ec974Sbeck if (a == NULL)
91913ec974Sbeck return;
92913ec974Sbeck
937de8a684Sderaadt freezero(a->data, a->max);
946f3a6cb1Sbeck free(a);
955b37fcf3Sryker }
96*ead8f799Sbeck LCRYPTO_ALIAS(BUF_MEM_free);
975b37fcf3Sryker
9879e8bed4Sjsing int
BUF_MEM_grow(BUF_MEM * str,size_t len)9979e8bed4Sjsing BUF_MEM_grow(BUF_MEM *str, size_t len)
1005b37fcf3Sryker {
101027ccf53Sjsing return BUF_MEM_grow_clean(str, len);
1025b37fcf3Sryker }
103*ead8f799Sbeck LCRYPTO_ALIAS(BUF_MEM_grow);
1045b37fcf3Sryker
10579e8bed4Sjsing int
BUF_MEM_grow_clean(BUF_MEM * str,size_t len)10679e8bed4Sjsing BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
107767fe2ffSmarkus {
108767fe2ffSmarkus char *ret;
1090a5d6edeSdjm size_t n;
110767fe2ffSmarkus
1117f2c4a2bSjsing if (str->max >= len) {
1127f2c4a2bSjsing if (str->length >= len)
113767fe2ffSmarkus memset(&str->data[len], 0, str->length - len);
114767fe2ffSmarkus str->length = len;
115767fe2ffSmarkus return (len);
116767fe2ffSmarkus }
1177f2c4a2bSjsing
11879e8bed4Sjsing if (len > LIMIT_BEFORE_EXPANSION) {
1195067ae9fSbeck BUFerror(ERR_R_MALLOC_FAILURE);
120b590e19bSdjm return 0;
121b590e19bSdjm }
1227f2c4a2bSjsing
123767fe2ffSmarkus n = (len + 3) / 3 * 4;
1247f2c4a2bSjsing if ((ret = recallocarray(str->data, str->max, n, 1)) == NULL) {
1255067ae9fSbeck BUFerror(ERR_R_MALLOC_FAILURE);
1267f2c4a2bSjsing return (0);
1277f2c4a2bSjsing }
128767fe2ffSmarkus str->data = ret;
129767fe2ffSmarkus str->max = n;
130767fe2ffSmarkus str->length = len;
1317f2c4a2bSjsing
132767fe2ffSmarkus return (len);
133767fe2ffSmarkus }
134*ead8f799Sbeck LCRYPTO_ALIAS(BUF_MEM_grow_clean);
1350a5d6edeSdjm
13679e8bed4Sjsing void
BUF_reverse(unsigned char * out,const unsigned char * in,size_t size)13779e8bed4Sjsing BUF_reverse(unsigned char *out, const unsigned char *in, size_t size)
1380a5d6edeSdjm {
1390a5d6edeSdjm size_t i;
1401103accfSjsing
14179e8bed4Sjsing if (in) {
1420a5d6edeSdjm out += size - 1;
1430a5d6edeSdjm for (i = 0; i < size; i++)
14497222eddSmiod *out-- = *in++;
14579e8bed4Sjsing } else {
1460a5d6edeSdjm unsigned char *q;
1470a5d6edeSdjm char c;
1480a5d6edeSdjm q = out + size - 1;
14979e8bed4Sjsing for (i = 0; i < size / 2; i++) {
1500a5d6edeSdjm c = *q;
1510a5d6edeSdjm *q-- = *out;
1520a5d6edeSdjm *out++ = c;
1530a5d6edeSdjm }
1540a5d6edeSdjm }
1550a5d6edeSdjm }
156