xref: /openbsd-src/lib/libcrypto/crypto_legacy.c (revision 5f24b0e3ad53ff02bc8fc039393e77b3b27c78f4)
1*5f24b0e3Stb /* $OpenBSD: crypto_legacy.c,v 1.6 2024/11/06 04:18:42 tb Exp $ */
208651822Stb /* ====================================================================
308651822Stb  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
408651822Stb  *
508651822Stb  * Redistribution and use in source and binary forms, with or without
608651822Stb  * modification, are permitted provided that the following conditions
708651822Stb  * are met:
808651822Stb  *
908651822Stb  * 1. Redistributions of source code must retain the above copyright
1008651822Stb  *    notice, this list of conditions and the following disclaimer.
1108651822Stb  *
1208651822Stb  * 2. Redistributions in binary form must reproduce the above copyright
1308651822Stb  *    notice, this list of conditions and the following disclaimer in
1408651822Stb  *    the documentation and/or other materials provided with the
1508651822Stb  *    distribution.
1608651822Stb  *
1708651822Stb  * 3. All advertising materials mentioning features or use of this
1808651822Stb  *    software must display the following acknowledgment:
1908651822Stb  *    "This product includes software developed by the OpenSSL Project
2008651822Stb  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
2108651822Stb  *
2208651822Stb  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2308651822Stb  *    endorse or promote products derived from this software without
2408651822Stb  *    prior written permission. For written permission, please contact
2508651822Stb  *    openssl-core@openssl.org.
2608651822Stb  *
2708651822Stb  * 5. Products derived from this software may not be called "OpenSSL"
2808651822Stb  *    nor may "OpenSSL" appear in their names without prior written
2908651822Stb  *    permission of the OpenSSL Project.
3008651822Stb  *
3108651822Stb  * 6. Redistributions of any form whatsoever must retain the following
3208651822Stb  *    acknowledgment:
3308651822Stb  *    "This product includes software developed by the OpenSSL Project
3408651822Stb  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
3508651822Stb  *
3608651822Stb  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
3708651822Stb  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3808651822Stb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3908651822Stb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4008651822Stb  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4108651822Stb  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4208651822Stb  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4308651822Stb  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4408651822Stb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4508651822Stb  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4608651822Stb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4708651822Stb  * OF THE POSSIBILITY OF SUCH DAMAGE.
4808651822Stb  * ====================================================================
4908651822Stb  *
5008651822Stb  * This product includes cryptographic software written by Eric Young
5108651822Stb  * (eay@cryptsoft.com).  This product includes software written by Tim
5208651822Stb  * Hudson (tjh@cryptsoft.com).
5308651822Stb  *
5408651822Stb  */
5508651822Stb /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5608651822Stb  * All rights reserved.
5708651822Stb  *
5808651822Stb  * This package is an SSL implementation written
5908651822Stb  * by Eric Young (eay@cryptsoft.com).
6008651822Stb  * The implementation was written so as to conform with Netscapes SSL.
6108651822Stb  *
6208651822Stb  * This library is free for commercial and non-commercial use as long as
6308651822Stb  * the following conditions are aheared to.  The following conditions
6408651822Stb  * apply to all code found in this distribution, be it the RC4, RSA,
6508651822Stb  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
6608651822Stb  * included with this distribution is covered by the same copyright terms
6708651822Stb  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
6808651822Stb  *
6908651822Stb  * Copyright remains Eric Young's, and as such any Copyright notices in
7008651822Stb  * the code are not to be removed.
7108651822Stb  * If this package is used in a product, Eric Young should be given attribution
7208651822Stb  * as the author of the parts of the library used.
7308651822Stb  * This can be in the form of a textual message at program startup or
7408651822Stb  * in documentation (online or textual) provided with the package.
7508651822Stb  *
7608651822Stb  * Redistribution and use in source and binary forms, with or without
7708651822Stb  * modification, are permitted provided that the following conditions
7808651822Stb  * are met:
7908651822Stb  * 1. Redistributions of source code must retain the copyright
8008651822Stb  *    notice, this list of conditions and the following disclaimer.
8108651822Stb  * 2. Redistributions in binary form must reproduce the above copyright
8208651822Stb  *    notice, this list of conditions and the following disclaimer in the
8308651822Stb  *    documentation and/or other materials provided with the distribution.
8408651822Stb  * 3. All advertising materials mentioning features or use of this software
8508651822Stb  *    must display the following acknowledgement:
8608651822Stb  *    "This product includes cryptographic software written by
8708651822Stb  *     Eric Young (eay@cryptsoft.com)"
8808651822Stb  *    The word 'cryptographic' can be left out if the rouines from the library
8908651822Stb  *    being used are not cryptographic related :-).
9008651822Stb  * 4. If you include any Windows specific code (or a derivative thereof) from
9108651822Stb  *    the apps directory (application code) you must include an acknowledgement:
9208651822Stb  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
9308651822Stb  *
9408651822Stb  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
9508651822Stb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9608651822Stb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9708651822Stb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
9808651822Stb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
9908651822Stb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
10008651822Stb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10108651822Stb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
10208651822Stb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
10308651822Stb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
10408651822Stb  * SUCH DAMAGE.
10508651822Stb  *
10608651822Stb  * The licence and distribution terms for any publically available version or
10708651822Stb  * derivative of this code cannot be changed.  i.e. this code cannot simply be
10808651822Stb  * copied and put under another distribution licence
10908651822Stb  * [including the GNU Public Licence.]
11008651822Stb  */
11108651822Stb /* ====================================================================
11208651822Stb  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
11308651822Stb  * ECDH support in OpenSSL originally developed by
11408651822Stb  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
11508651822Stb  */
11608651822Stb 
11708651822Stb #include <pthread.h>
11808651822Stb #include <stdarg.h>
11908651822Stb #include <stdio.h>
12008651822Stb #include <string.h>
12108651822Stb #include <syslog.h>
12208651822Stb #include <unistd.h>
12308651822Stb 
12408651822Stb #include <openssl/opensslconf.h>
12508651822Stb #include <openssl/crypto.h>
1267007099fStb #include <openssl/err.h>
12708651822Stb 
12808651822Stb #include "crypto_internal.h"
129f5906b22Stb #include "crypto_local.h"
13008651822Stb #include "x86_arch.h"
13108651822Stb 
13208651822Stb /* Machine independent capabilities. */
13308651822Stb uint64_t crypto_cpu_caps;
13408651822Stb 
13508651822Stb static void (*locking_callback)(int mode, int type,
13608651822Stb     const char *file, int line) = NULL;
13708651822Stb static int (*add_lock_callback)(int *pointer, int amount,
13808651822Stb     int type, const char *file, int line) = NULL;
13908651822Stb 
14008651822Stb int
14108651822Stb CRYPTO_num_locks(void)
14208651822Stb {
14308651822Stb 	return 1;
14408651822Stb }
14508651822Stb LCRYPTO_ALIAS(CRYPTO_num_locks);
14608651822Stb 
14708651822Stb unsigned long
14808651822Stb (*CRYPTO_get_id_callback(void))(void)
14908651822Stb {
15008651822Stb 	return NULL;
15108651822Stb }
15208651822Stb LCRYPTO_ALIAS(CRYPTO_get_id_callback);
15308651822Stb 
15408651822Stb void
15508651822Stb CRYPTO_set_id_callback(unsigned long (*func)(void))
15608651822Stb {
15708651822Stb 	return;
15808651822Stb }
15908651822Stb LCRYPTO_ALIAS(CRYPTO_set_id_callback);
16008651822Stb 
16108651822Stb unsigned long
16208651822Stb CRYPTO_thread_id(void)
16308651822Stb {
16408651822Stb 	return (unsigned long)pthread_self();
16508651822Stb }
16608651822Stb LCRYPTO_ALIAS(CRYPTO_thread_id);
16708651822Stb 
16808651822Stb void
16908651822Stb CRYPTO_set_locking_callback(void (*func)(int mode, int lock_num,
17008651822Stb     const char *file, int line))
17108651822Stb {
17208651822Stb 	locking_callback = func;
17308651822Stb }
17408651822Stb LCRYPTO_ALIAS(CRYPTO_set_locking_callback);
17508651822Stb 
17608651822Stb void
17708651822Stb (*CRYPTO_get_locking_callback(void))(int mode, int lock_num,
17808651822Stb 	const char *file, int line)
17908651822Stb {
18008651822Stb 	return locking_callback;
18108651822Stb }
18208651822Stb LCRYPTO_ALIAS(CRYPTO_get_locking_callback);
18308651822Stb 
18408651822Stb void
18508651822Stb CRYPTO_set_add_lock_callback(int (*func)(int *num, int mount, int lock_num,
18608651822Stb 	const char *file, int line))
18708651822Stb {
18808651822Stb 	add_lock_callback = func;
18908651822Stb }
19008651822Stb LCRYPTO_ALIAS(CRYPTO_set_add_lock_callback);
19108651822Stb 
19208651822Stb int
19308651822Stb (*CRYPTO_get_add_lock_callback(void))(int *num, int mount, int type,
19408651822Stb     const char *file, int line)
19508651822Stb {
19608651822Stb 	return add_lock_callback;
19708651822Stb }
19808651822Stb LCRYPTO_ALIAS(CRYPTO_get_add_lock_callback);
19908651822Stb 
20008651822Stb const char *
20108651822Stb CRYPTO_get_lock_name(int lock_num)
20208651822Stb {
20308651822Stb 	return "";
20408651822Stb }
20508651822Stb LCRYPTO_ALIAS(CRYPTO_get_lock_name);
20608651822Stb 
20708651822Stb struct CRYPTO_dynlock_value *
20808651822Stb CRYPTO_get_dynlock_value(int i)
20908651822Stb {
21008651822Stb 	return NULL;
21108651822Stb }
21208651822Stb LCRYPTO_ALIAS(CRYPTO_get_dynlock_value);
21308651822Stb 
21408651822Stb int
21508651822Stb CRYPTO_get_new_dynlockid(void)
21608651822Stb {
21708651822Stb 	return 0;
21808651822Stb }
21908651822Stb LCRYPTO_ALIAS(CRYPTO_get_new_dynlockid);
22008651822Stb 
22108651822Stb void
22208651822Stb CRYPTO_destroy_dynlockid(int i)
22308651822Stb {
22408651822Stb 	return;
22508651822Stb }
22608651822Stb LCRYPTO_ALIAS(CRYPTO_destroy_dynlockid);
22708651822Stb 
22808651822Stb int CRYPTO_get_new_lockid(char *name)
22908651822Stb {
23008651822Stb 	return 0;
23108651822Stb }
23208651822Stb LCRYPTO_ALIAS(CRYPTO_get_new_lockid);
23308651822Stb 
23408651822Stb int
23508651822Stb CRYPTO_THREADID_set_callback(void (*func)(CRYPTO_THREADID *))
23608651822Stb {
23708651822Stb 	return 1;
23808651822Stb }
23908651822Stb LCRYPTO_ALIAS(CRYPTO_THREADID_set_callback);
24008651822Stb 
24108651822Stb void
24208651822Stb (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *)
24308651822Stb {
24408651822Stb 	return NULL;
24508651822Stb }
24608651822Stb LCRYPTO_ALIAS(CRYPTO_THREADID_get_callback);
24708651822Stb 
24808651822Stb void
24908651822Stb CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val)
25008651822Stb {
25108651822Stb 	return;
25208651822Stb }
25308651822Stb LCRYPTO_ALIAS(CRYPTO_THREADID_set_numeric);
25408651822Stb 
25508651822Stb void
25608651822Stb CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr)
25708651822Stb {
25808651822Stb 	return;
25908651822Stb }
26008651822Stb LCRYPTO_ALIAS(CRYPTO_THREADID_set_pointer);
26108651822Stb 
26208651822Stb void
26308651822Stb CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(
26408651822Stb     *dyn_create_function)(const char *file, int line))
26508651822Stb {
26608651822Stb 	return;
26708651822Stb }
26808651822Stb LCRYPTO_ALIAS(CRYPTO_set_dynlock_create_callback);
26908651822Stb 
27008651822Stb void
27108651822Stb CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)(
27208651822Stb     int mode, struct CRYPTO_dynlock_value *l, const char *file, int line))
27308651822Stb {
27408651822Stb 	return;
27508651822Stb }
27608651822Stb LCRYPTO_ALIAS(CRYPTO_set_dynlock_lock_callback);
27708651822Stb 
27808651822Stb void
27908651822Stb CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)(
28008651822Stb     struct CRYPTO_dynlock_value *l, const char *file, int line))
28108651822Stb {
28208651822Stb 	return;
28308651822Stb }
28408651822Stb LCRYPTO_ALIAS(CRYPTO_set_dynlock_destroy_callback);
28508651822Stb 
28608651822Stb struct CRYPTO_dynlock_value *
28708651822Stb (*CRYPTO_get_dynlock_create_callback(void))(const char *file, int line)
28808651822Stb {
28908651822Stb 	return NULL;
29008651822Stb }
29108651822Stb LCRYPTO_ALIAS(CRYPTO_get_dynlock_create_callback);
29208651822Stb 
29308651822Stb void
29408651822Stb (*CRYPTO_get_dynlock_lock_callback(void))(int mode,
29508651822Stb     struct CRYPTO_dynlock_value *l, const char *file, int line)
29608651822Stb {
29708651822Stb 	return NULL;
29808651822Stb }
29908651822Stb LCRYPTO_ALIAS(CRYPTO_get_dynlock_lock_callback);
30008651822Stb 
30108651822Stb void
30208651822Stb (*CRYPTO_get_dynlock_destroy_callback(void))(
30308651822Stb     struct CRYPTO_dynlock_value *l, const char *file, int line)
30408651822Stb {
30508651822Stb 	return NULL;
30608651822Stb }
30708651822Stb LCRYPTO_ALIAS(CRYPTO_get_dynlock_destroy_callback);
30808651822Stb 
30908651822Stb #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
31008651822Stb void
31108651822Stb OPENSSL_cpuid_setup(void)
31208651822Stb {
31308651822Stb }
31408651822Stb #endif
31508651822Stb 
31608651822Stb #ifndef HAVE_CRYPTO_CPU_CAPS_INIT
31708651822Stb void
31808651822Stb crypto_cpu_caps_init(void)
31908651822Stb {
32008651822Stb 	OPENSSL_cpuid_setup();
32108651822Stb }
32208651822Stb #endif
32308651822Stb 
32408651822Stb #ifndef HAVE_CRYPTO_CPU_CAPS_IA32
32508651822Stb uint64_t
32608651822Stb crypto_cpu_caps_ia32(void)
32708651822Stb {
32808651822Stb 	return 0;
32908651822Stb }
33008651822Stb #endif
33108651822Stb 
33208651822Stb uint64_t
33308651822Stb OPENSSL_cpu_caps(void)
33408651822Stb {
33508651822Stb 	return crypto_cpu_caps;
33608651822Stb }
33708651822Stb LCRYPTO_ALIAS(OPENSSL_cpu_caps);
33808651822Stb 
33908651822Stb static void
34008651822Stb OPENSSL_showfatal(const char *fmta, ...)
34108651822Stb {
34208651822Stb 	struct syslog_data sdata = SYSLOG_DATA_INIT;
34308651822Stb 	va_list ap;
34408651822Stb 
34508651822Stb 	va_start(ap, fmta);
34608651822Stb 	vsyslog_r(LOG_CONS|LOG_LOCAL2, &sdata, fmta, ap);
34708651822Stb 	va_end(ap);
34808651822Stb }
34908651822Stb 
35008651822Stb void
35108651822Stb OpenSSLDie(const char *file, int line, const char *assertion)
35208651822Stb {
35308651822Stb 	OPENSSL_showfatal(
35408651822Stb 	    "uid %u cmd %s %s(%d): OpenSSL internal error, assertion failed: %s\n",
35508651822Stb 	    getuid(), getprogname(), file, line, assertion);
35608651822Stb 	_exit(1);
35708651822Stb }
35808651822Stb LCRYPTO_ALIAS(OpenSSLDie);
35908651822Stb 
36008651822Stb int
3619eae0f87Stb CRYPTO_mem_ctrl(int mode)
3629eae0f87Stb {
3639eae0f87Stb 	return CRYPTO_MEM_CHECK_OFF;
3649eae0f87Stb }
3659eae0f87Stb LCRYPTO_ALIAS(CRYPTO_mem_ctrl);
3669eae0f87Stb 
3679eae0f87Stb int
36808651822Stb CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len)
36908651822Stb {
37008651822Stb 	size_t i;
37108651822Stb 	const unsigned char *a = in_a;
37208651822Stb 	const unsigned char *b = in_b;
37308651822Stb 	unsigned char x = 0;
37408651822Stb 
37508651822Stb 	for (i = 0; i < len; i++)
37608651822Stb 		x |= a[i] ^ b[i];
37708651822Stb 
37808651822Stb 	return x;
37908651822Stb }
38008651822Stb LCRYPTO_ALIAS(CRYPTO_memcmp);
381f5906b22Stb 
3827007099fStb int
3837007099fStb FIPS_mode(void)
3847007099fStb {
3857007099fStb 	return 0;
3867007099fStb }
3877007099fStb LCRYPTO_ALIAS(FIPS_mode);
3887007099fStb 
3897007099fStb int
3907007099fStb FIPS_mode_set(int r)
3917007099fStb {
3927007099fStb 	if (r == 0)
3937007099fStb 		return 1;
3947007099fStb 	CRYPTOerror(CRYPTO_R_FIPS_MODE_NOT_SUPPORTED);
3957007099fStb 	return 0;
3967007099fStb }
3977007099fStb LCRYPTO_ALIAS(FIPS_mode_set);
3987007099fStb 
399f5906b22Stb const char *
400f5906b22Stb SSLeay_version(int t)
401f5906b22Stb {
402f5906b22Stb 	switch (t) {
403f5906b22Stb 	case SSLEAY_VERSION:
404f5906b22Stb 		return OPENSSL_VERSION_TEXT;
405f5906b22Stb 	case SSLEAY_BUILT_ON:
406f5906b22Stb 		return "built on: date not available";
407f5906b22Stb 	case SSLEAY_CFLAGS:
408f5906b22Stb 		return "compiler: information not available";
409f5906b22Stb 	case SSLEAY_PLATFORM:
410f5906b22Stb 		return "platform: information not available";
411f5906b22Stb 	case SSLEAY_DIR:
412f5906b22Stb 		return "OPENSSLDIR: \"" OPENSSLDIR "\"";
413f5906b22Stb 	}
414f5906b22Stb 	return "not available";
415f5906b22Stb }
416f5906b22Stb LCRYPTO_ALIAS(SSLeay_version);
417f5906b22Stb 
418f5906b22Stb unsigned long
419f5906b22Stb SSLeay(void)
420f5906b22Stb {
421f5906b22Stb 	return SSLEAY_VERSION_NUMBER;
422f5906b22Stb }
423f5906b22Stb LCRYPTO_ALIAS(SSLeay);
424f5906b22Stb 
425f5906b22Stb const char *
426f5906b22Stb OpenSSL_version(int t)
427f5906b22Stb {
428f5906b22Stb 	switch (t) {
429f5906b22Stb 	case OPENSSL_VERSION:
430f5906b22Stb 		return OPENSSL_VERSION_TEXT;
431f5906b22Stb 	case OPENSSL_BUILT_ON:
432f5906b22Stb 		return "built on: date not available";
433f5906b22Stb 	case OPENSSL_CFLAGS:
434f5906b22Stb 		return "compiler: information not available";
435f5906b22Stb 	case OPENSSL_PLATFORM:
436f5906b22Stb 		return "platform: information not available";
437f5906b22Stb 	case OPENSSL_DIR:
438f5906b22Stb 		return "OPENSSLDIR: \"" OPENSSLDIR "\"";
439f5906b22Stb 	case OPENSSL_ENGINES_DIR:
440f5906b22Stb 		return "ENGINESDIR: N/A";
441f5906b22Stb 	}
442f5906b22Stb 	return "not available";
443f5906b22Stb }
444f5906b22Stb LCRYPTO_ALIAS(OpenSSL_version);
445f5906b22Stb 
446f5906b22Stb unsigned long
447f5906b22Stb OpenSSL_version_num(void)
448f5906b22Stb {
449f5906b22Stb 	return SSLeay();
450f5906b22Stb }
451f5906b22Stb LCRYPTO_ALIAS(OpenSSL_version_num);
452