Home | History | Annotate | Download | only in crypto
      1 /* Copyright (c) 2014, Google Inc.
      2  *
      3  * Permission to use, copy, modify, and/or distribute this software for any
      4  * purpose with or without fee is hereby granted, provided that the above
      5  * copyright notice and this permission notice appear in all copies.
      6  *
      7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
     12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
     14 
     15 #include <openssl/crypto.h>
     16 
     17 #include <openssl/cpu.h>
     18 
     19 #include "internal.h"
     20 
     21 
     22 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \
     23     (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
     24      defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) || \
     25      defined(OPENSSL_PPC64LE))
     26 /* x86, x86_64, the ARMs and ppc64le need to record the result of a
     27  * cpuid/getauxval call for the asm to work correctly, unless compiled without
     28  * asm code. */
     29 #define NEED_CPUID
     30 
     31 #else
     32 
     33 /* Otherwise, don't emit a static initialiser. */
     34 
     35 #if !defined(BORINGSSL_NO_STATIC_INITIALIZER)
     36 #define BORINGSSL_NO_STATIC_INITIALIZER
     37 #endif
     38 
     39 #endif  /* !OPENSSL_NO_ASM && (OPENSSL_X86 || OPENSSL_X86_64 ||
     40                                OPENSSL_ARM || OPENSSL_AARCH64) */
     41 
     42 
     43 /* The capability variables are defined in this file in order to work around a
     44  * linker bug. When linking with a .a, if no symbols in a .o are referenced
     45  * then the .o is discarded, even if it has constructor functions.
     46  *
     47  * This still means that any binaries that don't include some functionality
     48  * that tests the capability values will still skip the constructor but, so
     49  * far, the init constructor function only sets the capability variables. */
     50 
     51 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
     52 
     53 /* This value must be explicitly initialised to zero in order to work around a
     54  * bug in libtool or the linker on OS X.
     55  *
     56  * If not initialised then it becomes a "common symbol". When put into an
     57  * archive, linking on OS X will fail to resolve common symbols. By
     58  * initialising it to zero, it becomes a "data symbol", which isn't so
     59  * affected. */
     60 uint32_t OPENSSL_ia32cap_P[4] = {0};
     61 
     62 #elif defined(OPENSSL_PPC64LE)
     63 
     64 unsigned long OPENSSL_ppc64le_hwcap2 = 0;
     65 
     66 #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
     67 
     68 #include <openssl/arm_arch.h>
     69 
     70 #if defined(OPENSSL_STATIC_ARMCAP)
     71 
     72 uint32_t OPENSSL_armcap_P =
     73 #if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON__)
     74     ARMV7_NEON |
     75 #endif
     76 #if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_CRYPTO)
     77     ARMV8_AES |
     78 #endif
     79 #if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_CRYPTO)
     80     ARMV8_SHA1 |
     81 #endif
     82 #if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_CRYPTO)
     83     ARMV8_SHA256 |
     84 #endif
     85 #if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_CRYPTO)
     86     ARMV8_PMULL |
     87 #endif
     88     0;
     89 
     90 #else
     91 uint32_t OPENSSL_armcap_P = 0;
     92 #endif
     93 
     94 #endif
     95 
     96 #if defined(BORINGSSL_FIPS)
     97 /* In FIPS mode, the power-on self-test function calls |CRYPTO_library_init|
     98  * because we have to ensure that CPUID detection occurs first. */
     99 #define BORINGSSL_NO_STATIC_INITIALIZER
    100 #endif
    101 
    102 #if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_NO_STATIC_INITIALIZER)
    103 #define OPENSSL_CDECL __cdecl
    104 #else
    105 #define OPENSSL_CDECL
    106 #endif
    107 
    108 #if defined(BORINGSSL_NO_STATIC_INITIALIZER)
    109 static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
    110 #elif defined(OPENSSL_WINDOWS)
    111 #pragma section(".CRT$XCU", read)
    112 static void __cdecl do_library_init(void);
    113 __declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) =
    114     do_library_init;
    115 #else
    116 static void do_library_init(void) __attribute__ ((constructor));
    117 #endif
    118 
    119 /* do_library_init is the actual initialization function. If
    120  * BORINGSSL_NO_STATIC_INITIALIZER isn't defined, this is set as a static
    121  * initializer. Otherwise, it is called by CRYPTO_library_init. */
    122 static void OPENSSL_CDECL do_library_init(void) {
    123  /* WARNING: this function may only configure the capability variables. See the
    124   * note above about the linker bug. */
    125 #if defined(NEED_CPUID)
    126   OPENSSL_cpuid_setup();
    127 #endif
    128 }
    129 
    130 void CRYPTO_library_init(void) {
    131   /* TODO(davidben): It would be tidier if this build knob could be replaced
    132    * with an internal lazy-init mechanism that would handle things correctly
    133    * in-library. https://crbug.com/542879 */
    134 #if defined(BORINGSSL_NO_STATIC_INITIALIZER)
    135   CRYPTO_once(&once, do_library_init);
    136 #endif
    137 }
    138 
    139 int CRYPTO_is_confidential_build(void) {
    140 #if defined(BORINGSSL_CONFIDENTIAL)
    141   return 1;
    142 #else
    143   return 0;
    144 #endif
    145 }
    146 
    147 int CRYPTO_has_asm(void) {
    148 #if defined(OPENSSL_NO_ASM)
    149   return 0;
    150 #else
    151   return 1;
    152 #endif
    153 }
    154 
    155 const char *SSLeay_version(int unused) {
    156   return "BoringSSL";
    157 }
    158 
    159 unsigned long SSLeay(void) {
    160   return OPENSSL_VERSION_NUMBER;
    161 }
    162 
    163 int CRYPTO_malloc_init(void) {
    164   return 1;
    165 }
    166 
    167 void ENGINE_load_builtin_engines(void) {}
    168 
    169 int ENGINE_register_all_complete(void) {
    170   return 1;
    171 }
    172 
    173 void OPENSSL_load_builtin_modules(void) {}
    174