xref: /openbsd-src/lib/libcrypto/crypto_legacy.c (revision 08651822cfee12768eb67da85a12d25afd614624)
1*08651822Stb /* $OpenBSD: crypto_legacy.c,v 1.1 2024/11/05 10:59:42 tb Exp $ */
2*08651822Stb /* ====================================================================
3*08651822Stb  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
4*08651822Stb  *
5*08651822Stb  * Redistribution and use in source and binary forms, with or without
6*08651822Stb  * modification, are permitted provided that the following conditions
7*08651822Stb  * are met:
8*08651822Stb  *
9*08651822Stb  * 1. Redistributions of source code must retain the above copyright
10*08651822Stb  *    notice, this list of conditions and the following disclaimer.
11*08651822Stb  *
12*08651822Stb  * 2. Redistributions in binary form must reproduce the above copyright
13*08651822Stb  *    notice, this list of conditions and the following disclaimer in
14*08651822Stb  *    the documentation and/or other materials provided with the
15*08651822Stb  *    distribution.
16*08651822Stb  *
17*08651822Stb  * 3. All advertising materials mentioning features or use of this
18*08651822Stb  *    software must display the following acknowledgment:
19*08651822Stb  *    "This product includes software developed by the OpenSSL Project
20*08651822Stb  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21*08651822Stb  *
22*08651822Stb  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23*08651822Stb  *    endorse or promote products derived from this software without
24*08651822Stb  *    prior written permission. For written permission, please contact
25*08651822Stb  *    openssl-core@openssl.org.
26*08651822Stb  *
27*08651822Stb  * 5. Products derived from this software may not be called "OpenSSL"
28*08651822Stb  *    nor may "OpenSSL" appear in their names without prior written
29*08651822Stb  *    permission of the OpenSSL Project.
30*08651822Stb  *
31*08651822Stb  * 6. Redistributions of any form whatsoever must retain the following
32*08651822Stb  *    acknowledgment:
33*08651822Stb  *    "This product includes software developed by the OpenSSL Project
34*08651822Stb  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35*08651822Stb  *
36*08651822Stb  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37*08651822Stb  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38*08651822Stb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39*08651822Stb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40*08651822Stb  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41*08651822Stb  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42*08651822Stb  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43*08651822Stb  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44*08651822Stb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45*08651822Stb  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46*08651822Stb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47*08651822Stb  * OF THE POSSIBILITY OF SUCH DAMAGE.
48*08651822Stb  * ====================================================================
49*08651822Stb  *
50*08651822Stb  * This product includes cryptographic software written by Eric Young
51*08651822Stb  * (eay@cryptsoft.com).  This product includes software written by Tim
52*08651822Stb  * Hudson (tjh@cryptsoft.com).
53*08651822Stb  *
54*08651822Stb  */
55*08651822Stb /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
56*08651822Stb  * All rights reserved.
57*08651822Stb  *
58*08651822Stb  * This package is an SSL implementation written
59*08651822Stb  * by Eric Young (eay@cryptsoft.com).
60*08651822Stb  * The implementation was written so as to conform with Netscapes SSL.
61*08651822Stb  *
62*08651822Stb  * This library is free for commercial and non-commercial use as long as
63*08651822Stb  * the following conditions are aheared to.  The following conditions
64*08651822Stb  * apply to all code found in this distribution, be it the RC4, RSA,
65*08651822Stb  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
66*08651822Stb  * included with this distribution is covered by the same copyright terms
67*08651822Stb  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
68*08651822Stb  *
69*08651822Stb  * Copyright remains Eric Young's, and as such any Copyright notices in
70*08651822Stb  * the code are not to be removed.
71*08651822Stb  * If this package is used in a product, Eric Young should be given attribution
72*08651822Stb  * as the author of the parts of the library used.
73*08651822Stb  * This can be in the form of a textual message at program startup or
74*08651822Stb  * in documentation (online or textual) provided with the package.
75*08651822Stb  *
76*08651822Stb  * Redistribution and use in source and binary forms, with or without
77*08651822Stb  * modification, are permitted provided that the following conditions
78*08651822Stb  * are met:
79*08651822Stb  * 1. Redistributions of source code must retain the copyright
80*08651822Stb  *    notice, this list of conditions and the following disclaimer.
81*08651822Stb  * 2. Redistributions in binary form must reproduce the above copyright
82*08651822Stb  *    notice, this list of conditions and the following disclaimer in the
83*08651822Stb  *    documentation and/or other materials provided with the distribution.
84*08651822Stb  * 3. All advertising materials mentioning features or use of this software
85*08651822Stb  *    must display the following acknowledgement:
86*08651822Stb  *    "This product includes cryptographic software written by
87*08651822Stb  *     Eric Young (eay@cryptsoft.com)"
88*08651822Stb  *    The word 'cryptographic' can be left out if the rouines from the library
89*08651822Stb  *    being used are not cryptographic related :-).
90*08651822Stb  * 4. If you include any Windows specific code (or a derivative thereof) from
91*08651822Stb  *    the apps directory (application code) you must include an acknowledgement:
92*08651822Stb  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
93*08651822Stb  *
94*08651822Stb  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
95*08651822Stb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96*08651822Stb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97*08651822Stb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
98*08651822Stb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99*08651822Stb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100*08651822Stb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101*08651822Stb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102*08651822Stb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103*08651822Stb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104*08651822Stb  * SUCH DAMAGE.
105*08651822Stb  *
106*08651822Stb  * The licence and distribution terms for any publically available version or
107*08651822Stb  * derivative of this code cannot be changed.  i.e. this code cannot simply be
108*08651822Stb  * copied and put under another distribution licence
109*08651822Stb  * [including the GNU Public Licence.]
110*08651822Stb  */
111*08651822Stb /* ====================================================================
112*08651822Stb  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113*08651822Stb  * ECDH support in OpenSSL originally developed by
114*08651822Stb  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115*08651822Stb  */
116*08651822Stb 
117*08651822Stb #include <pthread.h>
118*08651822Stb #include <stdarg.h>
119*08651822Stb #include <stdio.h>
120*08651822Stb #include <string.h>
121*08651822Stb #include <syslog.h>
122*08651822Stb #include <unistd.h>
123*08651822Stb 
124*08651822Stb #include <openssl/opensslconf.h>
125*08651822Stb #include <openssl/crypto.h>
126*08651822Stb 
127*08651822Stb #include "crypto_internal.h"
128*08651822Stb #include "x86_arch.h"
129*08651822Stb 
130*08651822Stb /* Machine independent capabilities. */
131*08651822Stb uint64_t crypto_cpu_caps;
132*08651822Stb 
133*08651822Stb static void (*locking_callback)(int mode, int type,
134*08651822Stb     const char *file, int line) = NULL;
135*08651822Stb static int (*add_lock_callback)(int *pointer, int amount,
136*08651822Stb     int type, const char *file, int line) = NULL;
137*08651822Stb 
138*08651822Stb int
139*08651822Stb CRYPTO_num_locks(void)
140*08651822Stb {
141*08651822Stb 	return 1;
142*08651822Stb }
143*08651822Stb LCRYPTO_ALIAS(CRYPTO_num_locks);
144*08651822Stb 
145*08651822Stb unsigned long
146*08651822Stb (*CRYPTO_get_id_callback(void))(void)
147*08651822Stb {
148*08651822Stb 	return NULL;
149*08651822Stb }
150*08651822Stb LCRYPTO_ALIAS(CRYPTO_get_id_callback);
151*08651822Stb 
152*08651822Stb void
153*08651822Stb CRYPTO_set_id_callback(unsigned long (*func)(void))
154*08651822Stb {
155*08651822Stb 	return;
156*08651822Stb }
157*08651822Stb LCRYPTO_ALIAS(CRYPTO_set_id_callback);
158*08651822Stb 
159*08651822Stb unsigned long
160*08651822Stb CRYPTO_thread_id(void)
161*08651822Stb {
162*08651822Stb 	return (unsigned long)pthread_self();
163*08651822Stb }
164*08651822Stb LCRYPTO_ALIAS(CRYPTO_thread_id);
165*08651822Stb 
166*08651822Stb void
167*08651822Stb CRYPTO_set_locking_callback(void (*func)(int mode, int lock_num,
168*08651822Stb     const char *file, int line))
169*08651822Stb {
170*08651822Stb 	locking_callback = func;
171*08651822Stb }
172*08651822Stb LCRYPTO_ALIAS(CRYPTO_set_locking_callback);
173*08651822Stb 
174*08651822Stb void
175*08651822Stb (*CRYPTO_get_locking_callback(void))(int mode, int lock_num,
176*08651822Stb 	const char *file, int line)
177*08651822Stb {
178*08651822Stb 	return locking_callback;
179*08651822Stb }
180*08651822Stb LCRYPTO_ALIAS(CRYPTO_get_locking_callback);
181*08651822Stb 
182*08651822Stb void
183*08651822Stb CRYPTO_set_add_lock_callback(int (*func)(int *num, int mount, int lock_num,
184*08651822Stb 	const char *file, int line))
185*08651822Stb {
186*08651822Stb 	add_lock_callback = func;
187*08651822Stb }
188*08651822Stb LCRYPTO_ALIAS(CRYPTO_set_add_lock_callback);
189*08651822Stb 
190*08651822Stb int
191*08651822Stb (*CRYPTO_get_add_lock_callback(void))(int *num, int mount, int type,
192*08651822Stb     const char *file, int line)
193*08651822Stb {
194*08651822Stb 	return add_lock_callback;
195*08651822Stb }
196*08651822Stb LCRYPTO_ALIAS(CRYPTO_get_add_lock_callback);
197*08651822Stb 
198*08651822Stb const char *
199*08651822Stb CRYPTO_get_lock_name(int lock_num)
200*08651822Stb {
201*08651822Stb 	return "";
202*08651822Stb }
203*08651822Stb LCRYPTO_ALIAS(CRYPTO_get_lock_name);
204*08651822Stb 
205*08651822Stb struct CRYPTO_dynlock_value *
206*08651822Stb CRYPTO_get_dynlock_value(int i)
207*08651822Stb {
208*08651822Stb 	return NULL;
209*08651822Stb }
210*08651822Stb LCRYPTO_ALIAS(CRYPTO_get_dynlock_value);
211*08651822Stb 
212*08651822Stb int
213*08651822Stb CRYPTO_get_new_dynlockid(void)
214*08651822Stb {
215*08651822Stb 	return 0;
216*08651822Stb }
217*08651822Stb LCRYPTO_ALIAS(CRYPTO_get_new_dynlockid);
218*08651822Stb 
219*08651822Stb void
220*08651822Stb CRYPTO_destroy_dynlockid(int i)
221*08651822Stb {
222*08651822Stb 	return;
223*08651822Stb }
224*08651822Stb LCRYPTO_ALIAS(CRYPTO_destroy_dynlockid);
225*08651822Stb 
226*08651822Stb int CRYPTO_get_new_lockid(char *name)
227*08651822Stb {
228*08651822Stb 	return 0;
229*08651822Stb }
230*08651822Stb LCRYPTO_ALIAS(CRYPTO_get_new_lockid);
231*08651822Stb 
232*08651822Stb int
233*08651822Stb CRYPTO_THREADID_set_callback(void (*func)(CRYPTO_THREADID *))
234*08651822Stb {
235*08651822Stb 	return 1;
236*08651822Stb }
237*08651822Stb LCRYPTO_ALIAS(CRYPTO_THREADID_set_callback);
238*08651822Stb 
239*08651822Stb void
240*08651822Stb (*CRYPTO_THREADID_get_callback(void))(CRYPTO_THREADID *)
241*08651822Stb {
242*08651822Stb 	return NULL;
243*08651822Stb }
244*08651822Stb LCRYPTO_ALIAS(CRYPTO_THREADID_get_callback);
245*08651822Stb 
246*08651822Stb void
247*08651822Stb CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val)
248*08651822Stb {
249*08651822Stb 	return;
250*08651822Stb }
251*08651822Stb LCRYPTO_ALIAS(CRYPTO_THREADID_set_numeric);
252*08651822Stb 
253*08651822Stb void
254*08651822Stb CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr)
255*08651822Stb {
256*08651822Stb 	return;
257*08651822Stb }
258*08651822Stb LCRYPTO_ALIAS(CRYPTO_THREADID_set_pointer);
259*08651822Stb 
260*08651822Stb void
261*08651822Stb CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(
262*08651822Stb     *dyn_create_function)(const char *file, int line))
263*08651822Stb {
264*08651822Stb 	return;
265*08651822Stb }
266*08651822Stb LCRYPTO_ALIAS(CRYPTO_set_dynlock_create_callback);
267*08651822Stb 
268*08651822Stb void
269*08651822Stb CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)(
270*08651822Stb     int mode, struct CRYPTO_dynlock_value *l, const char *file, int line))
271*08651822Stb {
272*08651822Stb 	return;
273*08651822Stb }
274*08651822Stb LCRYPTO_ALIAS(CRYPTO_set_dynlock_lock_callback);
275*08651822Stb 
276*08651822Stb void
277*08651822Stb CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)(
278*08651822Stb     struct CRYPTO_dynlock_value *l, const char *file, int line))
279*08651822Stb {
280*08651822Stb 	return;
281*08651822Stb }
282*08651822Stb LCRYPTO_ALIAS(CRYPTO_set_dynlock_destroy_callback);
283*08651822Stb 
284*08651822Stb struct CRYPTO_dynlock_value *
285*08651822Stb (*CRYPTO_get_dynlock_create_callback(void))(const char *file, int line)
286*08651822Stb {
287*08651822Stb 	return NULL;
288*08651822Stb }
289*08651822Stb LCRYPTO_ALIAS(CRYPTO_get_dynlock_create_callback);
290*08651822Stb 
291*08651822Stb void
292*08651822Stb (*CRYPTO_get_dynlock_lock_callback(void))(int mode,
293*08651822Stb     struct CRYPTO_dynlock_value *l, const char *file, int line)
294*08651822Stb {
295*08651822Stb 	return NULL;
296*08651822Stb }
297*08651822Stb LCRYPTO_ALIAS(CRYPTO_get_dynlock_lock_callback);
298*08651822Stb 
299*08651822Stb void
300*08651822Stb (*CRYPTO_get_dynlock_destroy_callback(void))(
301*08651822Stb     struct CRYPTO_dynlock_value *l, const char *file, int line)
302*08651822Stb {
303*08651822Stb 	return NULL;
304*08651822Stb }
305*08651822Stb LCRYPTO_ALIAS(CRYPTO_get_dynlock_destroy_callback);
306*08651822Stb 
307*08651822Stb #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
308*08651822Stb void
309*08651822Stb OPENSSL_cpuid_setup(void)
310*08651822Stb {
311*08651822Stb }
312*08651822Stb #endif
313*08651822Stb 
314*08651822Stb #ifndef HAVE_CRYPTO_CPU_CAPS_INIT
315*08651822Stb void
316*08651822Stb crypto_cpu_caps_init(void)
317*08651822Stb {
318*08651822Stb 	OPENSSL_cpuid_setup();
319*08651822Stb }
320*08651822Stb #endif
321*08651822Stb 
322*08651822Stb #ifndef HAVE_CRYPTO_CPU_CAPS_IA32
323*08651822Stb uint64_t
324*08651822Stb crypto_cpu_caps_ia32(void)
325*08651822Stb {
326*08651822Stb 	return 0;
327*08651822Stb }
328*08651822Stb #endif
329*08651822Stb 
330*08651822Stb uint64_t
331*08651822Stb OPENSSL_cpu_caps(void)
332*08651822Stb {
333*08651822Stb 	return crypto_cpu_caps;
334*08651822Stb }
335*08651822Stb LCRYPTO_ALIAS(OPENSSL_cpu_caps);
336*08651822Stb 
337*08651822Stb static void
338*08651822Stb OPENSSL_showfatal(const char *fmta, ...)
339*08651822Stb {
340*08651822Stb 	struct syslog_data sdata = SYSLOG_DATA_INIT;
341*08651822Stb 	va_list ap;
342*08651822Stb 
343*08651822Stb 	va_start(ap, fmta);
344*08651822Stb 	vsyslog_r(LOG_CONS|LOG_LOCAL2, &sdata, fmta, ap);
345*08651822Stb 	va_end(ap);
346*08651822Stb }
347*08651822Stb 
348*08651822Stb void
349*08651822Stb OpenSSLDie(const char *file, int line, const char *assertion)
350*08651822Stb {
351*08651822Stb 	OPENSSL_showfatal(
352*08651822Stb 	    "uid %u cmd %s %s(%d): OpenSSL internal error, assertion failed: %s\n",
353*08651822Stb 	    getuid(), getprogname(), file, line, assertion);
354*08651822Stb 	_exit(1);
355*08651822Stb }
356*08651822Stb LCRYPTO_ALIAS(OpenSSLDie);
357*08651822Stb 
358*08651822Stb int
359*08651822Stb CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len)
360*08651822Stb {
361*08651822Stb 	size_t i;
362*08651822Stb 	const unsigned char *a = in_a;
363*08651822Stb 	const unsigned char *b = in_b;
364*08651822Stb 	unsigned char x = 0;
365*08651822Stb 
366*08651822Stb 	for (i = 0; i < len; i++)
367*08651822Stb 		x |= a[i] ^ b[i];
368*08651822Stb 
369*08651822Stb 	return x;
370*08651822Stb }
371*08651822Stb LCRYPTO_ALIAS(CRYPTO_memcmp);
372