David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 1 | /* Copyright (c) 2016, 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 | |
David Benjamin | 661266e | 2021-12-23 03:42:39 -0500 | [diff] [blame] | 15 | #include "internal.h" |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 16 | |
David Benjamin | 37faa93 | 2021-12-21 18:43:32 -0500 | [diff] [blame] | 17 | #if defined(OPENSSL_ARM) && defined(OPENSSL_LINUX) && \ |
| 18 | !defined(OPENSSL_STATIC_ARMCAP) |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
David Benjamin | 6ab4f0a | 2023-02-21 10:54:37 -0500 | [diff] [blame] | 21 | #include <sys/auxv.h> |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <openssl/arm_arch.h> |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 26 | #include <openssl/mem.h> |
| 27 | |
David Benjamin | 295b313 | 2021-12-21 18:54:15 -0500 | [diff] [blame] | 28 | #include "cpu_arm_linux.h" |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 29 | |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 30 | static int open_eintr(const char *path, int flags) { |
| 31 | int ret; |
| 32 | do { |
| 33 | ret = open(path, flags); |
| 34 | } while (ret < 0 && errno == EINTR); |
| 35 | return ret; |
| 36 | } |
| 37 | |
| 38 | static ssize_t read_eintr(int fd, void *out, size_t len) { |
| 39 | ssize_t ret; |
| 40 | do { |
| 41 | ret = read(fd, out, len); |
| 42 | } while (ret < 0 && errno == EINTR); |
| 43 | return ret; |
| 44 | } |
| 45 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 46 | // read_file opens |path| and reads until end-of-file. On success, it returns |
| 47 | // one and sets |*out_ptr| and |*out_len| to a newly-allocated buffer with the |
| 48 | // contents. Otherwise, it returns zero. |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 49 | static int read_file(char **out_ptr, size_t *out_len, const char *path) { |
| 50 | int fd = open_eintr(path, O_RDONLY); |
| 51 | if (fd < 0) { |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static const size_t kReadSize = 1024; |
| 56 | int ret = 0; |
| 57 | size_t cap = kReadSize, len = 0; |
| 58 | char *buf = OPENSSL_malloc(cap); |
| 59 | if (buf == NULL) { |
| 60 | goto err; |
| 61 | } |
| 62 | |
| 63 | for (;;) { |
| 64 | if (cap - len < kReadSize) { |
| 65 | size_t new_cap = cap * 2; |
| 66 | if (new_cap < cap) { |
| 67 | goto err; |
| 68 | } |
| 69 | char *new_buf = OPENSSL_realloc(buf, new_cap); |
| 70 | if (new_buf == NULL) { |
| 71 | goto err; |
| 72 | } |
| 73 | buf = new_buf; |
| 74 | cap = new_cap; |
| 75 | } |
| 76 | |
| 77 | ssize_t bytes_read = read_eintr(fd, buf + len, kReadSize); |
| 78 | if (bytes_read < 0) { |
| 79 | goto err; |
| 80 | } |
| 81 | if (bytes_read == 0) { |
| 82 | break; |
| 83 | } |
| 84 | len += bytes_read; |
| 85 | } |
| 86 | |
| 87 | *out_ptr = buf; |
| 88 | *out_len = len; |
| 89 | ret = 1; |
| 90 | buf = NULL; |
| 91 | |
| 92 | err: |
| 93 | OPENSSL_free(buf); |
| 94 | close(fd); |
| 95 | return ret; |
| 96 | } |
| 97 | |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 98 | extern uint32_t OPENSSL_armcap_P; |
| 99 | |
David Benjamin | 2c12ebd | 2023-02-01 10:39:50 -0500 | [diff] [blame] | 100 | static int g_needs_hwcap2_workaround; |
David Benjamin | 0a63b96 | 2016-04-28 12:17:55 -0400 | [diff] [blame] | 101 | |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 102 | void OPENSSL_cpuid_setup(void) { |
David Benjamin | 9cf9d3e | 2020-05-08 18:28:07 -0400 | [diff] [blame] | 103 | // We ignore the return value of |read_file| and proceed with an empty |
| 104 | // /proc/cpuinfo on error. If |getauxval| works, we will still detect |
David Benjamin | 2c12ebd | 2023-02-01 10:39:50 -0500 | [diff] [blame] | 105 | // capabilities. |
David Benjamin | 9cf9d3e | 2020-05-08 18:28:07 -0400 | [diff] [blame] | 106 | char *cpuinfo_data = NULL; |
| 107 | size_t cpuinfo_len = 0; |
| 108 | read_file(&cpuinfo_data, &cpuinfo_len, "/proc/cpuinfo"); |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 109 | STRING_PIECE cpuinfo; |
| 110 | cpuinfo.data = cpuinfo_data; |
| 111 | cpuinfo.len = cpuinfo_len; |
| 112 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 113 | // Matching OpenSSL, only report other features if NEON is present. |
David Benjamin | 6ab4f0a | 2023-02-21 10:54:37 -0500 | [diff] [blame] | 114 | unsigned long hwcap = getauxval(AT_HWCAP); |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 115 | if (hwcap & HWCAP_NEON) { |
| 116 | OPENSSL_armcap_P |= ARMV7_NEON; |
| 117 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 118 | // Some ARMv8 Android devices don't expose AT_HWCAP2. Fall back to |
David Benjamin | f9bd455 | 2021-02-09 16:13:28 -0500 | [diff] [blame] | 119 | // /proc/cpuinfo. See https://crbug.com/boringssl/46. As of February 2021, |
| 120 | // this is now rare (see Chrome's Net.NeedsHWCAP2Workaround metric), but AES |
| 121 | // and PMULL extensions are very useful, so we still carry the workaround |
| 122 | // for now. |
David Benjamin | 6ab4f0a | 2023-02-21 10:54:37 -0500 | [diff] [blame] | 123 | unsigned long hwcap2 = getauxval(AT_HWCAP2); |
David Benjamin | a2d4c0c | 2016-03-20 17:53:34 -0400 | [diff] [blame] | 124 | if (hwcap2 == 0) { |
David Benjamin | 8cd61f7 | 2018-08-31 15:37:56 -0500 | [diff] [blame] | 125 | hwcap2 = crypto_get_arm_hwcap2_from_cpuinfo(&cpuinfo); |
David Benjamin | 9a127b4 | 2017-09-15 20:30:07 -0400 | [diff] [blame] | 126 | g_needs_hwcap2_workaround = hwcap2 != 0; |
David Benjamin | a2d4c0c | 2016-03-20 17:53:34 -0400 | [diff] [blame] | 127 | } |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 128 | |
David Benjamin | a2d4c0c | 2016-03-20 17:53:34 -0400 | [diff] [blame] | 129 | if (hwcap2 & HWCAP2_AES) { |
| 130 | OPENSSL_armcap_P |= ARMV8_AES; |
| 131 | } |
| 132 | if (hwcap2 & HWCAP2_PMULL) { |
| 133 | OPENSSL_armcap_P |= ARMV8_PMULL; |
| 134 | } |
| 135 | if (hwcap2 & HWCAP2_SHA1) { |
| 136 | OPENSSL_armcap_P |= ARMV8_SHA1; |
| 137 | } |
| 138 | if (hwcap2 & HWCAP2_SHA2) { |
| 139 | OPENSSL_armcap_P |= ARMV8_SHA256; |
David Benjamin | 054e151 | 2016-03-01 17:35:47 -0500 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | OPENSSL_free(cpuinfo_data); |
| 144 | } |
| 145 | |
David Benjamin | 2c12ebd | 2023-02-01 10:39:50 -0500 | [diff] [blame] | 146 | int CRYPTO_has_broken_NEON(void) { return 0; } |
David Benjamin | 0a63b96 | 2016-04-28 12:17:55 -0400 | [diff] [blame] | 147 | |
David Benjamin | 9a127b4 | 2017-09-15 20:30:07 -0400 | [diff] [blame] | 148 | int CRYPTO_needs_hwcap2_workaround(void) { return g_needs_hwcap2_workaround; } |
| 149 | |
David Benjamin | 37faa93 | 2021-12-21 18:43:32 -0500 | [diff] [blame] | 150 | #endif // OPENSSL_ARM && OPENSSL_LINUX && !OPENSSL_STATIC_ARMCAP |