| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/crypto.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <stdio.h> |
| 19 |
| 20 | #include "bcm_support.h" |
| 21 | #include "fipsmodule/rand/internal.h" |
| 22 | #include "internal.h" |
| 23 |
| 24 |
| 25 | static_assert(sizeof(ossl_ssize_t) == sizeof(size_t), |
| 26 | "ossl_ssize_t should be the same size as size_t"); |
| 27 |
| 28 |
| 29 | // Our assembly does not use the GOT to reference symbols, which means |
| 30 | // references to visible symbols will often require a TEXTREL. This is |
| 31 | // undesirable, so all assembly-referenced symbols should be hidden. CPU |
| 32 | // capabilities are the only such symbols defined in C. Explicitly hide them, |
| 33 | // rather than rely on being built with -fvisibility=hidden. |
| 34 | #if defined(OPENSSL_WINDOWS) |
| 35 | #define HIDDEN |
| 36 | #else |
| 37 | #define HIDDEN __attribute__((visibility("hidden"))) |
| 38 | #endif |
| 39 |
| 40 |
| 41 | // The capability variables are defined in this file in order to work around a |
| 42 | // linker bug. When linking with a .a, if no symbols in a .o are referenced |
| 43 | // then the .o is discarded, even if it has constructor functions. |
| 44 | // |
| 45 | // This still means that any binaries that don't include some functionality |
| 46 | // that tests the capability values will still skip the constructor but, so |
| 47 | // far, the init constructor function only sets the capability variables. |
| 48 |
| 49 | #if defined(BORINGSSL_DISPATCH_TEST) |
| 50 | // This value must be explicitly initialised to zero in order to work around a |
| 51 | // bug in libtool or the linker on OS X. |
| 52 | // |
| 53 | // If not initialised then it becomes a "common symbol". When put into an |
| 54 | // archive, linking on OS X will fail to resolve common symbols. By |
| 55 | // initialising it to zero, it becomes a "data symbol", which isn't so |
| 56 | // affected. |
| 57 | HIDDEN uint8_t BORINGSSL_function_hit[8] = {0}; |
| 58 | #endif |
| 59 |
| 60 | #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) |
| 61 |
| 62 | // This value must be explicitly initialized to zero. See similar comment above. |
| 63 | HIDDEN uint32_t OPENSSL_ia32cap_P[4] = {0}; |
| 64 |
| 65 | uint32_t OPENSSL_get_ia32cap(int idx) { |
| 66 | OPENSSL_init_cpuid(); |
| 67 | return OPENSSL_ia32cap_P[idx]; |
| 68 | } |
| 69 |
| 70 | #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) |
| 71 |
| 72 | #if defined(OPENSSL_STATIC_ARMCAP) |
| 73 |
| 74 | // See ARM ACLE for the definitions of these macros. Note |__ARM_FEATURE_AES| |
| 75 | // covers both AES and PMULL and |__ARM_FEATURE_SHA2| covers SHA-1 and SHA-256. |
| 76 | // https://developer.arm.com/architectures/system-architectures/software-standards/acle |
| 77 | // https://github.com/ARM-software/acle/issues/152 |
| 78 | // |
| 79 | // TODO(davidben): Do we still need |OPENSSL_STATIC_ARMCAP_*| or are the |
| 80 | // standard flags and -march sufficient? |
| 81 | HIDDEN uint32_t OPENSSL_armcap_P = |
| 82 | #if defined(OPENSSL_STATIC_ARMCAP_NEON) || defined(__ARM_NEON) |
| 83 | ARMV7_NEON | |
| 84 | #endif |
| 85 | #if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_AES) |
| 86 | ARMV8_AES | |
| 87 | #endif |
| 88 | #if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_AES) |
| 89 | ARMV8_PMULL | |
| 90 | #endif |
| 91 | #if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_SHA2) |
| 92 | ARMV8_SHA1 | |
| 93 | #endif |
| 94 | #if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_SHA2) |
| 95 | ARMV8_SHA256 | |
| 96 | #endif |
| 97 | #if defined(__ARM_FEATURE_SHA512) |
| 98 | ARMV8_SHA512 | |
| 99 | #endif |
| 100 | 0; |
| 101 |
| 102 | #else |
| 103 | HIDDEN uint32_t OPENSSL_armcap_P = 0; |
| 104 |
| 105 | uint32_t *OPENSSL_get_armcap_pointer_for_test(void) { |
| 106 | OPENSSL_init_cpuid(); |
| 107 | return &OPENSSL_armcap_P; |
| 108 | } |
| 109 | #endif |
| 110 |
| 111 | uint32_t OPENSSL_get_armcap(void) { |
| 112 | OPENSSL_init_cpuid(); |
| 113 | return OPENSSL_armcap_P; |
| 114 | } |
| 115 |
| 116 | #endif |
| 117 |
| 118 | #if defined(NEED_CPUID) |
| 119 | static CRYPTO_once_t once = CRYPTO_ONCE_INIT; |
| 120 | void OPENSSL_init_cpuid(void) { CRYPTO_once(&once, OPENSSL_cpuid_setup); } |
| 121 | #endif |
| 122 |
| 123 | void CRYPTO_library_init(void) {} |
| 124 |
| 125 | int CRYPTO_is_confidential_build(void) { |
| 126 | #if defined(BORINGSSL_CONFIDENTIAL) |
| 127 | return 1; |
| 128 | #else |
| 129 | return 0; |
| 130 | #endif |
| 131 | } |
| 132 |
| 133 | void CRYPTO_pre_sandbox_init(void) { |
| 134 | // Read from /proc/cpuinfo if needed. |
| 135 | OPENSSL_init_cpuid(); |
| 136 | // Open /dev/urandom if needed. |
| 137 | CRYPTO_init_sysrand(); |
| 138 | // Set up MADV_WIPEONFORK state if needed. |
| 139 | CRYPTO_get_fork_generation(); |
| 140 | } |
| 141 |
| 142 | const char *SSLeay_version(int which) { return OpenSSL_version(which); } |
| 143 |
| 144 | const char *OpenSSL_version(int which) { |
| 145 | switch (which) { |
| 146 | case OPENSSL_VERSION: |
| 147 | return "BoringSSL"; |
| 148 | case OPENSSL_CFLAGS: |
| 149 | return "compiler: n/a"; |
| 150 | case OPENSSL_BUILT_ON: |
| 151 | return "built on: n/a"; |
| 152 | case OPENSSL_PLATFORM: |
| 153 | return "platform: n/a"; |
| 154 | case OPENSSL_DIR: |
| 155 | return "OPENSSLDIR: n/a"; |
| 156 | default: |
| 157 | return "not available"; |
| 158 | } |
| 159 | } |
| 160 |
| 161 | unsigned long SSLeay(void) { return OPENSSL_VERSION_NUMBER; } |
| 162 |
| 163 | unsigned long OpenSSL_version_num(void) { return OPENSSL_VERSION_NUMBER; } |
| 164 |
| 165 | int CRYPTO_malloc_init(void) { return 1; } |
| 166 |
| 167 | int OPENSSL_malloc_init(void) { return 1; } |
| 168 |
| 169 | void ENGINE_load_builtin_engines(void) {} |
| 170 |
| 171 | int ENGINE_register_all_complete(void) { return 1; } |
| 172 |
| 173 | void OPENSSL_load_builtin_modules(void) {} |
| 174 |
| 175 | int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) { |
| 176 | return 1; |
| 177 | } |
| 178 |
| 179 | void OPENSSL_cleanup(void) {} |
| 180 |
| 181 | FILE *CRYPTO_get_stderr(void) { return stderr; } |
| 182 |