From: jsing Date: Sat, 31 Aug 2024 12:43:58 +0000 (+0000) Subject: Make OPENSSL_cpu_caps() machine independent. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=308329e2459d30940c2647c8743e85bc8a1e2283;p=openbsd Make OPENSSL_cpu_caps() machine independent. OPENSSL_cpu_caps() is currently machine dependent and exposes CPUID data on amd64 and i386. However, what it is really used for is to indicate whether specific algorithms are accelerated on the given hardware. Change OPENSSL_cpu_caps() so that it returns a machine indepent value, which decouples it from amd64/i386 and will allow it to be used appropriately on other platforms in the future. ok tb@ --- diff --git a/lib/libcrypto/cryptlib.c b/lib/libcrypto/cryptlib.c index b9ea39285d1..d929b0daaa8 100644 --- a/lib/libcrypto/cryptlib.c +++ b/lib/libcrypto/cryptlib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cryptlib.c,v 1.52 2024/07/09 07:16:44 beck Exp $ */ +/* $OpenBSD: cryptlib.c,v 1.53 2024/08/31 12:43:58 jsing Exp $ */ /* ==================================================================== * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. * @@ -125,6 +125,10 @@ #include #include "crypto_local.h" +#include "x86_arch.h" + +/* Machine independent capabilities. */ +uint64_t crypto_cpu_caps; static void (*locking_callback)(int mode, int type, const char *file, int line) = NULL; @@ -330,13 +334,6 @@ CRYPTO_THREADID_hash(const CRYPTO_THREADID *id) uint64_t OPENSSL_ia32cap_P; -uint64_t -OPENSSL_cpu_caps(void) -{ - return OPENSSL_ia32cap_P; -} -LCRYPTO_ALIAS(OPENSSL_cpu_caps); - #if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) #define OPENSSL_CPUID_SETUP void @@ -349,16 +346,12 @@ OPENSSL_cpuid_setup(void) return; trigger = 1; OPENSSL_ia32cap_P = OPENSSL_ia32_cpuid(); + + if ((OPENSSL_ia32cap_P & CPUCAP_MASK_AESNI) != 0) + crypto_cpu_caps |= CRYPTO_CPU_CAPS_ACCELERATED_AES; } #endif -#else -uint64_t -OPENSSL_cpu_caps(void) -{ - return 0; -} -LCRYPTO_ALIAS(OPENSSL_cpu_caps); #endif #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ) @@ -368,6 +361,13 @@ OPENSSL_cpuid_setup(void) } #endif +uint64_t +OPENSSL_cpu_caps(void) +{ + return crypto_cpu_caps; +} +LCRYPTO_ALIAS(OPENSSL_cpu_caps); + static void OPENSSL_showfatal(const char *fmta, ...) { diff --git a/lib/libcrypto/crypto.h b/lib/libcrypto/crypto.h index 40d3a43f4ff..bcca5a0ace2 100644 --- a/lib/libcrypto/crypto.h +++ b/lib/libcrypto/crypto.h @@ -1,4 +1,4 @@ -/* $OpenBSD: crypto.h,v 1.74 2024/04/10 15:13:23 beck Exp $ */ +/* $OpenBSD: crypto.h,v 1.75 2024/08/31 12:43:58 jsing Exp $ */ /* ==================================================================== * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. * @@ -373,8 +373,6 @@ __attribute__((__noreturn__)) void OpenSSLDie(const char *file, int line, const char *assertion); #define OPENSSL_assert(e) (void)((e) ? 0 : (OpenSSLDie(__FILE__, __LINE__, #e),1)) -uint64_t OPENSSL_cpu_caps(void); - int FIPS_mode(void); int FIPS_mode_set(int r); @@ -422,6 +420,13 @@ int CRYPTO_memcmp(const void *a, const void *b, size_t len); int OPENSSL_init_crypto(uint64_t opts, const void *settings); void OPENSSL_cleanup(void); +/* + * CPU capabilities. + */ +#define CRYPTO_CPU_CAPS_ACCELERATED_AES 0x00000001ULL + +uint64_t OPENSSL_cpu_caps(void); + /* * OpenSSL helpfully put OPENSSL_gmtime() here because all other time related * functions are in asn1.h.