2014-09-12 00:11:15 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
|
2015-11-12 05:33:18 +00:00
|
|
|
#include <openssl/cpu.h>
|
|
|
|
|
2014-09-12 00:11:15 +01:00
|
|
|
#include "internal.h"
|
|
|
|
|
2015-01-09 23:44:37 +00:00
|
|
|
|
2018-07-25 15:45:01 +01:00
|
|
|
#if defined(OPENSSL_MSAN) && !defined(OPENSSL_NO_ASM)
|
|
|
|
// MSan works by instrumenting memory accesses in the compiler. Accesses from
|
|
|
|
// uninstrumented code, such as assembly, are invisible to it. MSan will
|
|
|
|
// incorrectly report reads from assembly-initialized memory as uninitialized.
|
|
|
|
// If building BoringSSL with MSan, exclude assembly files from the build and
|
|
|
|
// define OPENSSL_NO_ASM.
|
|
|
|
//
|
|
|
|
// This is checked here rather than in a header because the consumer might not
|
|
|
|
// define OPENSSL_NO_ASM. It is only necessary for BoringSSL source files to be
|
|
|
|
// built with it.
|
|
|
|
#error "BoringSSL must be built with assembly disabled to use MSan."
|
|
|
|
#endif
|
|
|
|
|
2015-10-16 23:46:46 +01:00
|
|
|
#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \
|
2015-01-09 23:44:37 +00:00
|
|
|
(defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
|
2016-09-23 20:47:24 +01:00
|
|
|
defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) || \
|
|
|
|
defined(OPENSSL_PPC64LE))
|
2017-08-18 19:06:02 +01:00
|
|
|
// x86, x86_64, the ARMs and ppc64le need to record the result of a
|
|
|
|
// cpuid/getauxval call for the asm to work correctly, unless compiled without
|
|
|
|
// asm code.
|
2014-09-19 18:15:33 +01:00
|
|
|
#define NEED_CPUID
|
2014-09-12 00:11:15 +01:00
|
|
|
|
2014-09-19 18:15:33 +01:00
|
|
|
#else
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// Otherwise, don't emit a static initialiser.
|
2014-09-19 18:15:33 +01:00
|
|
|
|
|
|
|
#if !defined(BORINGSSL_NO_STATIC_INITIALIZER)
|
2014-09-12 00:11:15 +01:00
|
|
|
#define BORINGSSL_NO_STATIC_INITIALIZER
|
|
|
|
#endif
|
|
|
|
|
2015-01-09 23:44:37 +00:00
|
|
|
#endif /* !OPENSSL_NO_ASM && (OPENSSL_X86 || OPENSSL_X86_64 ||
|
|
|
|
OPENSSL_ARM || OPENSSL_AARCH64) */
|
|
|
|
|
|
|
|
|
2017-10-19 03:23:09 +01:00
|
|
|
// Our assembly does not use the GOT to reference symbols, which means
|
|
|
|
// references to visible symbols will often require a TEXTREL. This is
|
|
|
|
// undesirable, so all assembly-referenced symbols should be hidden. CPU
|
|
|
|
// capabilities are the only such symbols defined in C. Explicitly hide them,
|
|
|
|
// rather than rely on being built with -fvisibility=hidden.
|
|
|
|
#if defined(OPENSSL_WINDOWS)
|
|
|
|
#define HIDDEN
|
|
|
|
#else
|
|
|
|
#define HIDDEN __attribute__((visibility("hidden")))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// The capability variables are defined in this file in order to work around a
|
|
|
|
// linker bug. When linking with a .a, if no symbols in a .o are referenced
|
|
|
|
// then the .o is discarded, even if it has constructor functions.
|
|
|
|
//
|
|
|
|
// This still means that any binaries that don't include some functionality
|
|
|
|
// that tests the capability values will still skip the constructor but, so
|
|
|
|
// far, the init constructor function only sets the capability variables.
|
2015-01-09 23:44:37 +00:00
|
|
|
|
|
|
|
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
|
2017-04-04 19:08:28 +01:00
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// This value must be explicitly initialised to zero in order to work around a
|
|
|
|
// bug in libtool or the linker on OS X.
|
|
|
|
//
|
|
|
|
// If not initialised then it becomes a "common symbol". When put into an
|
|
|
|
// archive, linking on OS X will fail to resolve common symbols. By
|
|
|
|
// initialising it to zero, it becomes a "data symbol", which isn't so
|
|
|
|
// affected.
|
2017-10-19 03:23:09 +01:00
|
|
|
HIDDEN uint32_t OPENSSL_ia32cap_P[4] = {0};
|
2017-04-04 19:08:28 +01:00
|
|
|
|
|
|
|
#elif defined(OPENSSL_PPC64LE)
|
|
|
|
|
2017-10-19 03:23:09 +01:00
|
|
|
HIDDEN unsigned long OPENSSL_ppc64le_hwcap2 = 0;
|
2017-04-04 19:08:28 +01:00
|
|
|
|
2015-01-09 23:44:37 +00:00
|
|
|
#elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
|
|
|
|
|
2015-08-25 02:03:17 +01:00
|
|
|
#include <openssl/arm_arch.h>
|
2015-01-09 23:44:37 +00:00
|
|
|
|
2015-10-16 23:46:46 +01:00
|
|
|
#if defined(OPENSSL_STATIC_ARMCAP)
|
|
|
|
|
2017-10-19 03:23:09 +01:00
|
|
|
HIDDEN uint32_t OPENSSL_armcap_P =
|
2015-10-16 23:46:46 +01:00
|
|
|
#if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON__)
|
2016-02-23 23:04:15 +00:00
|
|
|
ARMV7_NEON |
|
2015-10-16 23:46:46 +01:00
|
|
|
#endif
|
2017-06-08 21:53:28 +01:00
|
|
|
#if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_CRYPTO)
|
2015-10-16 23:46:46 +01:00
|
|
|
ARMV8_AES |
|
|
|
|
#endif
|
2017-06-08 21:53:28 +01:00
|
|
|
#if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_CRYPTO)
|
2015-10-16 23:46:46 +01:00
|
|
|
ARMV8_SHA1 |
|
|
|
|
#endif
|
2017-06-08 21:53:28 +01:00
|
|
|
#if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_CRYPTO)
|
2015-10-16 23:46:46 +01:00
|
|
|
ARMV8_SHA256 |
|
|
|
|
#endif
|
2017-06-08 21:53:28 +01:00
|
|
|
#if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_CRYPTO)
|
2015-10-16 23:46:46 +01:00
|
|
|
ARMV8_PMULL |
|
|
|
|
#endif
|
|
|
|
0;
|
|
|
|
|
2015-01-09 23:44:37 +00:00
|
|
|
#else
|
2017-10-19 03:23:09 +01:00
|
|
|
HIDDEN uint32_t OPENSSL_armcap_P = 0;
|
2015-01-09 23:44:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-04 22:21:43 +01:00
|
|
|
#if defined(BORINGSSL_FIPS)
|
2017-08-18 19:06:02 +01:00
|
|
|
// In FIPS mode, the power-on self-test function calls |CRYPTO_library_init|
|
|
|
|
// because we have to ensure that CPUID detection occurs first.
|
2017-04-04 22:21:43 +01:00
|
|
|
#define BORINGSSL_NO_STATIC_INITIALIZER
|
|
|
|
#endif
|
2014-09-19 18:15:33 +01:00
|
|
|
|
2015-11-19 16:39:46 +00:00
|
|
|
#if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_NO_STATIC_INITIALIZER)
|
2014-09-12 00:11:15 +01:00
|
|
|
#define OPENSSL_CDECL __cdecl
|
|
|
|
#else
|
|
|
|
#define OPENSSL_CDECL
|
|
|
|
#endif
|
|
|
|
|
2015-11-19 16:39:46 +00:00
|
|
|
#if defined(BORINGSSL_NO_STATIC_INITIALIZER)
|
|
|
|
static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
|
2017-08-16 20:25:27 +01:00
|
|
|
#elif defined(_MSC_VER)
|
2014-09-12 00:11:15 +01:00
|
|
|
#pragma section(".CRT$XCU", read)
|
|
|
|
static void __cdecl do_library_init(void);
|
|
|
|
__declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) =
|
|
|
|
do_library_init;
|
2015-11-19 16:39:46 +00:00
|
|
|
#else
|
|
|
|
static void do_library_init(void) __attribute__ ((constructor));
|
2014-09-12 00:11:15 +01:00
|
|
|
#endif
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// do_library_init is the actual initialization function. If
|
|
|
|
// BORINGSSL_NO_STATIC_INITIALIZER isn't defined, this is set as a static
|
|
|
|
// initializer. Otherwise, it is called by CRYPTO_library_init.
|
2014-09-12 00:11:15 +01:00
|
|
|
static void OPENSSL_CDECL do_library_init(void) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// WARNING: this function may only configure the capability variables. See the
|
|
|
|
// note above about the linker bug.
|
2014-09-19 18:15:33 +01:00
|
|
|
#if defined(NEED_CPUID)
|
2014-09-12 00:11:15 +01:00
|
|
|
OPENSSL_cpuid_setup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRYPTO_library_init(void) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// TODO(davidben): It would be tidier if this build knob could be replaced
|
|
|
|
// with an internal lazy-init mechanism that would handle things correctly
|
|
|
|
// in-library. https://crbug.com/542879
|
2014-09-12 00:11:15 +01:00
|
|
|
#if defined(BORINGSSL_NO_STATIC_INITIALIZER)
|
2015-11-19 16:39:46 +00:00
|
|
|
CRYPTO_once(&once, do_library_init);
|
2014-09-12 00:11:15 +01:00
|
|
|
#endif
|
|
|
|
}
|
2015-04-13 22:34:17 +01:00
|
|
|
|
2016-05-03 17:16:21 +01:00
|
|
|
int CRYPTO_is_confidential_build(void) {
|
|
|
|
#if defined(BORINGSSL_CONFIDENTIAL)
|
|
|
|
return 1;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-16 21:44:40 +01:00
|
|
|
int CRYPTO_has_asm(void) {
|
|
|
|
#if defined(OPENSSL_NO_ASM)
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-05-08 21:07:00 +01:00
|
|
|
const char *SSLeay_version(int which) { return OpenSSL_version(which); }
|
2015-04-13 22:34:17 +01:00
|
|
|
|
2018-05-08 21:07:00 +01:00
|
|
|
const char *OpenSSL_version(int which) {
|
2018-05-15 18:37:53 +01:00
|
|
|
switch (which) {
|
|
|
|
case OPENSSL_VERSION:
|
|
|
|
return "BoringSSL";
|
|
|
|
case OPENSSL_CFLAGS:
|
|
|
|
return "compiler: n/a";
|
|
|
|
case OPENSSL_BUILT_ON:
|
|
|
|
return "built on: n/a";
|
|
|
|
case OPENSSL_PLATFORM:
|
|
|
|
return "platform: n/a";
|
|
|
|
case OPENSSL_DIR:
|
|
|
|
return "OPENSSLDIR: n/a";
|
|
|
|
default:
|
|
|
|
return "not available";
|
2018-05-08 21:07:00 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-12 19:48:19 +01:00
|
|
|
|
2018-04-12 23:31:36 +01:00
|
|
|
unsigned long SSLeay(void) { return OPENSSL_VERSION_NUMBER; }
|
2015-08-02 17:35:44 +01:00
|
|
|
|
2018-04-12 23:31:36 +01:00
|
|
|
unsigned long OpenSSL_version_num(void) { return OPENSSL_VERSION_NUMBER; }
|
2016-08-12 19:48:19 +01:00
|
|
|
|
2018-04-12 23:31:36 +01:00
|
|
|
int CRYPTO_malloc_init(void) { return 1; }
|
|
|
|
|
|
|
|
int OPENSSL_malloc_init(void) { return 1; }
|
2015-08-02 17:35:44 +01:00
|
|
|
|
|
|
|
void ENGINE_load_builtin_engines(void) {}
|
2016-01-26 06:09:19 +00:00
|
|
|
|
2018-04-12 23:31:36 +01:00
|
|
|
int ENGINE_register_all_complete(void) { return 1; }
|
2016-07-12 18:26:56 +01:00
|
|
|
|
2016-01-26 06:09:19 +00:00
|
|
|
void OPENSSL_load_builtin_modules(void) {}
|
2016-08-12 19:48:19 +01:00
|
|
|
|
|
|
|
int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
|
|
|
|
CRYPTO_library_init();
|
|
|
|
return 1;
|
|
|
|
}
|