190792Sgshapiro/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 294334Sgshapiro * All rights reserved. 390792Sgshapiro * 490792Sgshapiro * This package is an SSL implementation written 590792Sgshapiro * by Eric Young (eay@cryptsoft.com). 690792Sgshapiro * The implementation was written so as to conform with Netscapes SSL. 790792Sgshapiro * 890792Sgshapiro * This library is free for commercial and non-commercial use as long as 990792Sgshapiro * the following conditions are aheared to. The following conditions 1090792Sgshapiro * apply to all code found in this distribution, be it the RC4, RSA, 1190792Sgshapiro * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1290792Sgshapiro * included with this distribution is covered by the same copyright terms 1390792Sgshapiro * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14110560Sgshapiro * 1590792Sgshapiro * Copyright remains Eric Young's, and as such any Copyright notices in 1690792Sgshapiro * the code are not to be removed. 1790792Sgshapiro * If this package is used in a product, Eric Young should be given attribution 1890792Sgshapiro * as the author of the parts of the library used. 1990792Sgshapiro * This can be in the form of a textual message at program startup or 2090792Sgshapiro * in documentation (online or textual) provided with the package. 2190792Sgshapiro * 2290792Sgshapiro * Redistribution and use in source and binary forms, with or without 2390792Sgshapiro * modification, are permitted provided that the following conditions 2490792Sgshapiro * are met: 2590792Sgshapiro * 1. Redistributions of source code must retain the copyright 2690792Sgshapiro * notice, this list of conditions and the following disclaimer. 2790792Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright 2890792Sgshapiro * notice, this list of conditions and the following disclaimer in the 2990792Sgshapiro * documentation and/or other materials provided with the distribution. 3090792Sgshapiro * 3. All advertising materials mentioning features or use of this software 3190792Sgshapiro * must display the following acknowledgement: 3290792Sgshapiro * "This product includes cryptographic software written by 3390792Sgshapiro * Eric Young (eay@cryptsoft.com)" 3490792Sgshapiro * The word 'cryptographic' can be left out if the rouines from the library 3590792Sgshapiro * being used are not cryptographic related :-). 3690792Sgshapiro * 4. If you include any Windows specific code (or a derivative thereof) from 3790792Sgshapiro * the apps directory (application code) you must include an acknowledgement: 3890792Sgshapiro * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 3990792Sgshapiro * 4090792Sgshapiro * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4190792Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4290792Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4390792Sgshapiro * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4490792Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4590792Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4690792Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4790792Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4890792Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 4990792Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5090792Sgshapiro * SUCH DAMAGE. 5190792Sgshapiro * 5290792Sgshapiro * The licence and distribution terms for any publically available version or 5390792Sgshapiro * derivative of this code cannot be changed. i.e. this code cannot simply be 5490792Sgshapiro * copied and put under another distribution licence 5590792Sgshapiro * [including the GNU Public Licence.] */ 5690792Sgshapiro 5790792Sgshapiro#include <openssl/mem.h> 5890792Sgshapiro 5990792Sgshapiro#include <assert.h> 6090792Sgshapiro#include <stdarg.h> 6190792Sgshapiro#include <stdio.h> 6290792Sgshapiro 6390792Sgshapiro#if defined(OPENSSL_WINDOWS) 6490792SgshapiroOPENSSL_MSVC_PRAGMA(warning(push, 3)) 6590792Sgshapiro#include <windows.h> 6690792SgshapiroOPENSSL_MSVC_PRAGMA(warning(pop)) 6790792Sgshapiro#endif 6890792Sgshapiro 6990792Sgshapiro#include "internal.h" 7090792Sgshapiro 7190792Sgshapiro 7290792Sgshapiro#define OPENSSL_MALLOC_PREFIX 8 7390792Sgshapiro 7490792Sgshapiro 7590792Sgshapirovoid *OPENSSL_malloc(size_t size) { 7690792Sgshapiro void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX); 7790792Sgshapiro if (ptr == NULL) { 7890792Sgshapiro return NULL; 7990792Sgshapiro } 8090792Sgshapiro 8190792Sgshapiro *(size_t *)ptr = size; 8290792Sgshapiro 8390792Sgshapiro return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX; 8490792Sgshapiro} 8590792Sgshapiro 8690792Sgshapirovoid OPENSSL_free(void *orig_ptr) { 8790792Sgshapiro if (orig_ptr == NULL) { 8890792Sgshapiro return; 8990792Sgshapiro } 9090792Sgshapiro 9190792Sgshapiro void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; 9290792Sgshapiro 9390792Sgshapiro size_t size = *(size_t *)ptr; 9490792Sgshapiro OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX); 9590792Sgshapiro free(ptr); 9690792Sgshapiro} 9790792Sgshapiro 9890792Sgshapirovoid *OPENSSL_realloc(void *orig_ptr, size_t new_size) { 9990792Sgshapiro if (orig_ptr == NULL) { 10090792Sgshapiro return OPENSSL_malloc(new_size); 10190792Sgshapiro } 10290792Sgshapiro 10390792Sgshapiro void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; 10490792Sgshapiro size_t old_size = *(size_t *)ptr; 10590792Sgshapiro 10690792Sgshapiro void *ret = OPENSSL_malloc(new_size); 10790792Sgshapiro if (ret == NULL) { 10890792Sgshapiro return NULL; 10990792Sgshapiro } 11090792Sgshapiro 11190792Sgshapiro size_t to_copy = new_size; 11290792Sgshapiro if (old_size < to_copy) { 11390792Sgshapiro to_copy = old_size; 11490792Sgshapiro } 11590792Sgshapiro 11690792Sgshapiro memcpy(ret, orig_ptr, to_copy); 11790792Sgshapiro OPENSSL_free(orig_ptr); 11890792Sgshapiro 11990792Sgshapiro return ret; 12090792Sgshapiro} 12190792Sgshapiro 12290792Sgshapirovoid OPENSSL_cleanse(void *ptr, size_t len) { 12390792Sgshapiro#if defined(OPENSSL_WINDOWS) 12490792Sgshapiro SecureZeroMemory(ptr, len); 12590792Sgshapiro#else 12690792Sgshapiro OPENSSL_memset(ptr, 0, len); 12790792Sgshapiro 12890792Sgshapiro#if !defined(OPENSSL_NO_ASM) 12990792Sgshapiro /* As best as we can tell, this is sufficient to break any optimisations that 13090792Sgshapiro might try to eliminate "superfluous" memsets. If there's an easy way to 13190792Sgshapiro detect memset_s, it would be better to use that. */ 13290792Sgshapiro __asm__ __volatile__("" : : "r"(ptr) : "memory"); 13390792Sgshapiro#endif 13490792Sgshapiro#endif // !OPENSSL_NO_ASM 13590792Sgshapiro} 13690792Sgshapiro 13794334Sgshapiroint CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) { 13890792Sgshapiro const uint8_t *a = in_a; 13990792Sgshapiro const uint8_t *b = in_b; 14090792Sgshapiro uint8_t x = 0; 14190792Sgshapiro 14290792Sgshapiro for (size_t i = 0; i < len; i++) { 14390792Sgshapiro x |= a[i] ^ b[i]; 14490792Sgshapiro } 14590792Sgshapiro 14690792Sgshapiro return x; 14790792Sgshapiro} 14890792Sgshapiro 14990792Sgshapirouint32_t OPENSSL_hash32(const void *ptr, size_t len) { 15090792Sgshapiro // These are the FNV-1a parameters for 32 bits. 15190792Sgshapiro static const uint32_t kPrime = 16777619u; 15290792Sgshapiro static const uint32_t kOffsetBasis = 2166136261u; 15390792Sgshapiro 15490792Sgshapiro const uint8_t *in = ptr; 15590792Sgshapiro uint32_t h = kOffsetBasis; 15690792Sgshapiro 15790792Sgshapiro for (size_t i = 0; i < len; i++) { 15890792Sgshapiro h ^= in[i]; 15990792Sgshapiro h *= kPrime; 16090792Sgshapiro } 16190792Sgshapiro 16290792Sgshapiro return h; 16390792Sgshapiro} 16490792Sgshapiro 16590792Sgshapirosize_t OPENSSL_strnlen(const char *s, size_t len) { 16690792Sgshapiro for (size_t i = 0; i < len; i++) { 16790792Sgshapiro if (s[i] == 0) { 16890792Sgshapiro return i; 16990792Sgshapiro } 17090792Sgshapiro } 17190792Sgshapiro 17290792Sgshapiro return len; 17390792Sgshapiro} 17490792Sgshapiro 17590792Sgshapirochar *OPENSSL_strdup(const char *s) { 17690792Sgshapiro const size_t len = strlen(s) + 1; 17790792Sgshapiro char *ret = OPENSSL_malloc(len); 17890792Sgshapiro if (ret == NULL) { 17990792Sgshapiro return NULL; 18090792Sgshapiro } 18190792Sgshapiro OPENSSL_memcpy(ret, s, len); 18290792Sgshapiro return ret; 18390792Sgshapiro} 18490792Sgshapiro 18590792Sgshapiroint OPENSSL_tolower(int c) { 18690792Sgshapiro if (c >= 'A' && c <= 'Z') { 18790792Sgshapiro return c + ('a' - 'A'); 18890792Sgshapiro } 18990792Sgshapiro return c; 19090792Sgshapiro} 19190792Sgshapiro 19290792Sgshapiroint OPENSSL_strcasecmp(const char *a, const char *b) { 19390792Sgshapiro for (size_t i = 0;; i++) { 19490792Sgshapiro const int aa = OPENSSL_tolower(a[i]); 19590792Sgshapiro const int bb = OPENSSL_tolower(b[i]); 19690792Sgshapiro 19790792Sgshapiro if (aa < bb) { 19890792Sgshapiro return -1; 19990792Sgshapiro } else if (aa > bb) { 20090792Sgshapiro return 1; 20190792Sgshapiro } else if (aa == 0) { 20290792Sgshapiro return 0; 20390792Sgshapiro } 20490792Sgshapiro } 20590792Sgshapiro} 20690792Sgshapiro 20790792Sgshapiroint OPENSSL_strncasecmp(const char *a, const char *b, size_t n) { 20890792Sgshapiro for (size_t i = 0; i < n; i++) { 20990792Sgshapiro const int aa = OPENSSL_tolower(a[i]); 21090792Sgshapiro const int bb = OPENSSL_tolower(b[i]); 21190792Sgshapiro 21290792Sgshapiro if (aa < bb) { 21390792Sgshapiro return -1; 21490792Sgshapiro } else if (aa > bb) { 21590792Sgshapiro return 1; 21690792Sgshapiro } else if (aa == 0) { 21790792Sgshapiro return 0; 21890792Sgshapiro } 21990792Sgshapiro } 22090792Sgshapiro 22190792Sgshapiro return 0; 22290792Sgshapiro} 22390792Sgshapiro 22490792Sgshapiroint BIO_snprintf(char *buf, size_t n, const char *format, ...) { 22590792Sgshapiro va_list args; 22690792Sgshapiro va_start(args, format); 22790792Sgshapiro int ret = BIO_vsnprintf(buf, n, format, args); 22890792Sgshapiro va_end(args); 22990792Sgshapiro return ret; 23090792Sgshapiro} 23190792Sgshapiro 23290792Sgshapiroint BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) { 23390792Sgshapiro return vsnprintf(buf, n, format, args); 23490792Sgshapiro} 23590792Sgshapiro