xref: /aosp_15_r20/external/boringssl/src/crypto/cpu_arm_linux.c (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
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 
open_eintr(const char * path,int flags)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 
read_eintr(int fd,void * out,size_t len)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 
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.
read_file(char ** out_ptr,size_t * out_len,const char * path)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 
98 static int g_needs_hwcap2_workaround;
99 
OPENSSL_cpuid_setup(void)100 void OPENSSL_cpuid_setup(void) {
101   // We ignore the return value of |read_file| and proceed with an empty
102   // /proc/cpuinfo on error. If |getauxval| works, we will still detect
103   // capabilities.
104   char *cpuinfo_data = NULL;
105   size_t cpuinfo_len = 0;
106   read_file(&cpuinfo_data, &cpuinfo_len, "/proc/cpuinfo");
107   STRING_PIECE cpuinfo;
108   cpuinfo.data = cpuinfo_data;
109   cpuinfo.len = cpuinfo_len;
110 
111   // Matching OpenSSL, only report other features if NEON is present.
112   unsigned long hwcap = getauxval(AT_HWCAP);
113   if (hwcap & HWCAP_NEON) {
114     OPENSSL_armcap_P |= ARMV7_NEON;
115 
116     // Some ARMv8 Android devices don't expose AT_HWCAP2. Fall back to
117     // /proc/cpuinfo. See https://crbug.com/boringssl/46. As of February 2021,
118     // this is now rare (see Chrome's Net.NeedsHWCAP2Workaround metric), but AES
119     // and PMULL extensions are very useful, so we still carry the workaround
120     // for now.
121     unsigned long hwcap2 = getauxval(AT_HWCAP2);
122     if (hwcap2 == 0) {
123       hwcap2 = crypto_get_arm_hwcap2_from_cpuinfo(&cpuinfo);
124       g_needs_hwcap2_workaround = hwcap2 != 0;
125     }
126 
127     if (hwcap2 & HWCAP2_AES) {
128       OPENSSL_armcap_P |= ARMV8_AES;
129     }
130     if (hwcap2 & HWCAP2_PMULL) {
131       OPENSSL_armcap_P |= ARMV8_PMULL;
132     }
133     if (hwcap2 & HWCAP2_SHA1) {
134       OPENSSL_armcap_P |= ARMV8_SHA1;
135     }
136     if (hwcap2 & HWCAP2_SHA2) {
137       OPENSSL_armcap_P |= ARMV8_SHA256;
138     }
139   }
140 
141   OPENSSL_free(cpuinfo_data);
142 }
143 
CRYPTO_has_broken_NEON(void)144 int CRYPTO_has_broken_NEON(void) { return 0; }
145 
CRYPTO_needs_hwcap2_workaround(void)146 int CRYPTO_needs_hwcap2_workaround(void) {
147   OPENSSL_init_cpuid();
148   return g_needs_hwcap2_workaround;
149 }
150 
151 #endif  // OPENSSL_ARM && OPENSSL_LINUX && !OPENSSL_STATIC_ARMCAP
152