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
15#include "internal.h"
16
17#if defined(OPENSSL_ARM) && defined(OPENSSL_LINUX) && \
18 !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/arm_arch.h>
26#include <openssl/mem.h>
27
28#include "cpu_arm_linux.h"
29
30static 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
38static 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
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.
49static 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
92err:
93 OPENSSL_free(buf);
94 close(fd);
95 return ret;
96}
97
98extern uint32_t OPENSSL_armcap_P;
99
100static int g_needs_hwcap2_workaround;
101
102void OPENSSL_cpuid_setup(void) {
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
105 // capabilities.
106 char *cpuinfo_data = NULL;
107 size_t cpuinfo_len = 0;
108 read_file(&cpuinfo_data, &cpuinfo_len, "/proc/cpuinfo");
109 STRING_PIECE cpuinfo;
110 cpuinfo.data = cpuinfo_data;
111 cpuinfo.len = cpuinfo_len;
112
113 // Matching OpenSSL, only report other features if NEON is present.
114 unsigned long hwcap = getauxval(AT_HWCAP);
115 if (hwcap & HWCAP_NEON) {
116 OPENSSL_armcap_P |= ARMV7_NEON;
117
118 // Some ARMv8 Android devices don't expose AT_HWCAP2. Fall back to
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.
123 unsigned long hwcap2 = getauxval(AT_HWCAP2);
124 if (hwcap2 == 0) {
125 hwcap2 = crypto_get_arm_hwcap2_from_cpuinfo(&cpuinfo);
126 g_needs_hwcap2_workaround = hwcap2 != 0;
127 }
128
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;
140 }
141 }
142
143 OPENSSL_free(cpuinfo_data);
144}
145
146int CRYPTO_has_broken_NEON(void) { return 0; }
147
148int CRYPTO_needs_hwcap2_workaround(void) { return g_needs_hwcap2_workaround; }
149
150#endif // OPENSSL_ARM && OPENSSL_LINUX && !OPENSSL_STATIC_ARMCAP
151

source code of dart_sdk/third_party/boringssl/src/crypto/cpu_arm_linux.c