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