David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 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 "internal.h" |
| 18 | |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 19 | |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 20 | #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \ |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 21 | (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \ |
| 22 | defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) |
| 23 | /* x86, x86_64 and the ARMs need to record the result of a cpuid call for the |
| 24 | * asm to work correctly, unless compiled without asm code. */ |
Adam Langley | 588d252 | 2014-09-19 10:15:33 -0700 | [diff] [blame] | 25 | #define NEED_CPUID |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 26 | |
Adam Langley | 588d252 | 2014-09-19 10:15:33 -0700 | [diff] [blame] | 27 | #else |
| 28 | |
| 29 | /* Otherwise, don't emit a static initialiser. */ |
| 30 | |
| 31 | #if !defined(BORINGSSL_NO_STATIC_INITIALIZER) |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 32 | #define BORINGSSL_NO_STATIC_INITIALIZER |
| 33 | #endif |
| 34 | |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 35 | #endif /* !OPENSSL_NO_ASM && (OPENSSL_X86 || OPENSSL_X86_64 || |
| 36 | OPENSSL_ARM || OPENSSL_AARCH64) */ |
| 37 | |
| 38 | |
| 39 | /* The capability variables are defined in this file in order to work around a |
| 40 | * linker bug. When linking with a .a, if no symbols in a .o are referenced |
| 41 | * then the .o is discarded, even if it has constructor functions. |
| 42 | * |
| 43 | * This still means that any binaries that don't include some functionality |
| 44 | * that tests the capability values will still skip the constructor but, so |
| 45 | * far, the init constructor function only sets the capability variables. */ |
| 46 | |
| 47 | #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) |
| 48 | /* This value must be explicitly initialised to zero in order to work around a |
| 49 | * bug in libtool or the linker on OS X. |
| 50 | * |
| 51 | * If not initialised then it becomes a "common symbol". When put into an |
| 52 | * archive, linking on OS X will fail to resolve common symbols. By |
| 53 | * initialising it to zero, it becomes a "data symbol", which isn't so |
| 54 | * affected. */ |
| 55 | uint32_t OPENSSL_ia32cap_P[4] = {0}; |
| 56 | #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) |
| 57 | |
Adam Langley | 73415b6 | 2015-08-24 18:03:17 -0700 | [diff] [blame] | 58 | #include <openssl/arm_arch.h> |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 59 | |
Adam Langley | 6a7cfbe | 2015-10-16 15:46:46 -0700 | [diff] [blame] | 60 | #if defined(OPENSSL_STATIC_ARMCAP) |
| 61 | |
| 62 | uint32_t OPENSSL_armcap_P = |
| 63 | #if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON__) |
| 64 | ARMV7_NEON | ARMV7_NEON_FUNCTIONAL | |
| 65 | #endif |
| 66 | #if defined(OPENSSL_STATIC_ARMCAP_AES) |
| 67 | ARMV8_AES | |
| 68 | #endif |
| 69 | #if defined(OPENSSL_STATIC_ARMCAP_SHA1) |
| 70 | ARMV8_SHA1 | |
| 71 | #endif |
| 72 | #if defined(OPENSSL_STATIC_ARMCAP_SHA256) |
| 73 | ARMV8_SHA256 | |
| 74 | #endif |
| 75 | #if defined(OPENSSL_STATIC_ARMCAP_PMULL) |
| 76 | ARMV8_PMULL | |
| 77 | #endif |
| 78 | 0; |
| 79 | |
| 80 | #elif defined(__ARM_NEON__) |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 81 | uint32_t OPENSSL_armcap_P = ARMV7_NEON | ARMV7_NEON_FUNCTIONAL; |
| 82 | #else |
| 83 | uint32_t OPENSSL_armcap_P = ARMV7_NEON_FUNCTIONAL; |
| 84 | #endif |
| 85 | |
| 86 | #endif |
| 87 | |
Adam Langley | 588d252 | 2014-09-19 10:15:33 -0700 | [diff] [blame] | 88 | |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 89 | #if defined(OPENSSL_WINDOWS) |
| 90 | #define OPENSSL_CDECL __cdecl |
| 91 | #else |
| 92 | #define OPENSSL_CDECL |
| 93 | #endif |
| 94 | |
| 95 | #if !defined(BORINGSSL_NO_STATIC_INITIALIZER) |
| 96 | #if !defined(OPENSSL_WINDOWS) |
| 97 | static void do_library_init(void) __attribute__ ((constructor)); |
| 98 | #else |
| 99 | #pragma section(".CRT$XCU", read) |
| 100 | static void __cdecl do_library_init(void); |
| 101 | __declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) = |
| 102 | do_library_init; |
| 103 | #endif |
| 104 | #endif /* !BORINGSSL_NO_STATIC_INITIALIZER */ |
| 105 | |
| 106 | /* do_library_init is the actual initialization function. If |
| 107 | * BORINGSSL_NO_STATIC_INITIALIZER isn't defined, this is set as a static |
| 108 | * initializer. Otherwise, it is called by CRYPTO_library_init. */ |
| 109 | static void OPENSSL_CDECL do_library_init(void) { |
Adam Langley | 3e65265 | 2015-01-09 15:44:37 -0800 | [diff] [blame] | 110 | /* WARNING: this function may only configure the capability variables. See the |
| 111 | * note above about the linker bug. */ |
Adam Langley | 588d252 | 2014-09-19 10:15:33 -0700 | [diff] [blame] | 112 | #if defined(NEED_CPUID) |
David Benjamin | a70c75c | 2014-09-11 19:11:15 -0400 | [diff] [blame] | 113 | OPENSSL_cpuid_setup(); |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | void CRYPTO_library_init(void) { |
| 118 | /* TODO(davidben): It would be tidier if this build knob could be replaced |
| 119 | * with an internal lazy-init mechanism that would handle things correctly |
| 120 | * in-library. */ |
| 121 | #if defined(BORINGSSL_NO_STATIC_INITIALIZER) |
| 122 | do_library_init(); |
| 123 | #endif |
| 124 | } |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 125 | |
| 126 | const char *SSLeay_version(int unused) { |
Adam Langley | b3a262c | 2015-05-19 16:30:20 -0700 | [diff] [blame] | 127 | return "BoringSSL"; |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Adam Langley | b3a262c | 2015-05-19 16:30:20 -0700 | [diff] [blame] | 130 | unsigned long SSLeay(void) { |
| 131 | return OPENSSL_VERSION_NUMBER; |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 132 | } |
Adam Langley | 05ee4fd | 2015-08-02 09:35:44 -0700 | [diff] [blame^] | 133 | |
| 134 | int CRYPTO_malloc_init(void) { |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | void ENGINE_load_builtin_engines(void) {} |