blob: e326285f32730bbfbe59d63878a9186440495236 [file] [log] [blame]
Robert Sloan726e9d12018-09-11 11:45:04 -07001/* Copyright (c) 2018, 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#ifndef OPENSSL_HEADER_CRYPTO_CPU_ARM_LINUX_H
16#define OPENSSL_HEADER_CRYPTO_CPU_ARM_LINUX_H
17
18#include <openssl/base.h>
19
Robert Sloanc9abfe42018-11-26 12:19:07 -080020#include <string.h>
21
22#include "internal.h"
23
Robert Sloan726e9d12018-09-11 11:45:04 -070024#if defined(__cplusplus)
25extern "C" {
26#endif
27
28
Robert Sloanc9abfe42018-11-26 12:19:07 -080029// The cpuinfo parser lives in a header file so it may be accessible from
30// cross-platform fuzzers without adding code to those platforms normally.
Robert Sloan726e9d12018-09-11 11:45:04 -070031
32#define HWCAP_NEON (1 << 12)
33
34// See /usr/include/asm/hwcap.h on an ARM installation for the source of
35// these values.
36#define HWCAP2_AES (1 << 0)
37#define HWCAP2_PMULL (1 << 1)
38#define HWCAP2_SHA1 (1 << 2)
39#define HWCAP2_SHA2 (1 << 3)
40
41typedef struct {
42 const char *data;
43 size_t len;
44} STRING_PIECE;
45
Robert Sloanc9abfe42018-11-26 12:19:07 -080046static int STRING_PIECE_equals(const STRING_PIECE *a, const char *b) {
47 size_t b_len = strlen(b);
48 return a->len == b_len && OPENSSL_memcmp(a->data, b, b_len) == 0;
49}
50
51// STRING_PIECE_split finds the first occurence of |sep| in |in| and, if found,
52// sets |*out_left| and |*out_right| to |in| split before and after it. It
53// returns one if |sep| was found and zero otherwise.
54static int STRING_PIECE_split(STRING_PIECE *out_left, STRING_PIECE *out_right,
55 const STRING_PIECE *in, char sep) {
56 const char *p = (const char *)OPENSSL_memchr(in->data, sep, in->len);
57 if (p == NULL) {
58 return 0;
59 }
60 // |out_left| or |out_right| may alias |in|, so make a copy.
61 STRING_PIECE in_copy = *in;
62 out_left->data = in_copy.data;
63 out_left->len = p - in_copy.data;
64 out_right->data = in_copy.data + out_left->len + 1;
65 out_right->len = in_copy.len - out_left->len - 1;
66 return 1;
67}
68
69// STRING_PIECE_get_delimited reads a |sep|-delimited entry from |s|, writing it
70// to |out| and updating |s| to point beyond it. It returns one on success and
71// zero if |s| is empty. If |s| is has no copies of |sep| and is non-empty, it
72// reads the entire string to |out|.
73static int STRING_PIECE_get_delimited(STRING_PIECE *s, STRING_PIECE *out, char sep) {
74 if (s->len == 0) {
75 return 0;
76 }
77 if (!STRING_PIECE_split(out, s, s, sep)) {
78 // |s| had no instances of |sep|. Return the entire string.
79 *out = *s;
80 s->data += s->len;
81 s->len = 0;
82 }
83 return 1;
84}
85
86// STRING_PIECE_trim removes leading and trailing whitespace from |s|.
87static void STRING_PIECE_trim(STRING_PIECE *s) {
88 while (s->len != 0 && (s->data[0] == ' ' || s->data[0] == '\t')) {
89 s->data++;
90 s->len--;
91 }
92 while (s->len != 0 &&
93 (s->data[s->len - 1] == ' ' || s->data[s->len - 1] == '\t')) {
94 s->len--;
95 }
96}
97
98// extract_cpuinfo_field extracts a /proc/cpuinfo field named |field| from
99// |in|. If found, it sets |*out| to the value and returns one. Otherwise, it
100// returns zero.
101static int extract_cpuinfo_field(STRING_PIECE *out, const STRING_PIECE *in,
102 const char *field) {
103 // Process |in| one line at a time.
104 STRING_PIECE remaining = *in, line;
105 while (STRING_PIECE_get_delimited(&remaining, &line, '\n')) {
106 STRING_PIECE key, value;
107 if (!STRING_PIECE_split(&key, &value, &line, ':')) {
108 continue;
109 }
110 STRING_PIECE_trim(&key);
111 if (STRING_PIECE_equals(&key, field)) {
112 STRING_PIECE_trim(&value);
113 *out = value;
114 return 1;
115 }
116 }
117
118 return 0;
119}
120
121static int cpuinfo_field_equals(const STRING_PIECE *cpuinfo, const char *field,
122 const char *value) {
123 STRING_PIECE extracted;
124 return extract_cpuinfo_field(&extracted, cpuinfo, field) &&
125 STRING_PIECE_equals(&extracted, value);
126}
127
128// has_list_item treats |list| as a space-separated list of items and returns
129// one if |item| is contained in |list| and zero otherwise.
130static int has_list_item(const STRING_PIECE *list, const char *item) {
131 STRING_PIECE remaining = *list, feature;
132 while (STRING_PIECE_get_delimited(&remaining, &feature, ' ')) {
133 if (STRING_PIECE_equals(&feature, item)) {
134 return 1;
135 }
136 }
137 return 0;
138}
139
Robert Sloan726e9d12018-09-11 11:45:04 -0700140// crypto_get_arm_hwcap_from_cpuinfo returns an equivalent ARM |AT_HWCAP| value
141// from |cpuinfo|.
Robert Sloanc9abfe42018-11-26 12:19:07 -0800142static unsigned long crypto_get_arm_hwcap_from_cpuinfo(
143 const STRING_PIECE *cpuinfo) {
144 if (cpuinfo_field_equals(cpuinfo, "CPU architecture", "8")) {
145 // This is a 32-bit ARM binary running on a 64-bit kernel. NEON is always
146 // available on ARMv8. Linux omits required features, so reading the
147 // "Features" line does not work. (For simplicity, use strict equality. We
148 // assume everything running on future ARM architectures will have a
149 // working |getauxval|.)
150 return HWCAP_NEON;
151 }
152
153 STRING_PIECE features;
154 if (extract_cpuinfo_field(&features, cpuinfo, "Features") &&
155 has_list_item(&features, "neon")) {
156 return HWCAP_NEON;
157 }
158 return 0;
159}
Robert Sloan726e9d12018-09-11 11:45:04 -0700160
161// crypto_get_arm_hwcap2_from_cpuinfo returns an equivalent ARM |AT_HWCAP2|
162// value from |cpuinfo|.
Robert Sloanc9abfe42018-11-26 12:19:07 -0800163static unsigned long crypto_get_arm_hwcap2_from_cpuinfo(
164 const STRING_PIECE *cpuinfo) {
165 STRING_PIECE features;
166 if (!extract_cpuinfo_field(&features, cpuinfo, "Features")) {
167 return 0;
168 }
169
170 unsigned long ret = 0;
171 if (has_list_item(&features, "aes")) {
172 ret |= HWCAP2_AES;
173 }
174 if (has_list_item(&features, "pmull")) {
175 ret |= HWCAP2_PMULL;
176 }
177 if (has_list_item(&features, "sha1")) {
178 ret |= HWCAP2_SHA1;
179 }
180 if (has_list_item(&features, "sha2")) {
181 ret |= HWCAP2_SHA2;
182 }
183 return ret;
184}
Robert Sloan726e9d12018-09-11 11:45:04 -0700185
186// crypto_cpuinfo_has_broken_neon returns one if |cpuinfo| matches a CPU known
187// to have broken NEON unit and zero otherwise. See https://crbug.com/341598.
Robert Sloanc9abfe42018-11-26 12:19:07 -0800188static int crypto_cpuinfo_has_broken_neon(const STRING_PIECE *cpuinfo) {
189 return cpuinfo_field_equals(cpuinfo, "CPU implementer", "0x51") &&
190 cpuinfo_field_equals(cpuinfo, "CPU architecture", "7") &&
191 cpuinfo_field_equals(cpuinfo, "CPU variant", "0x1") &&
192 cpuinfo_field_equals(cpuinfo, "CPU part", "0x04d") &&
193 cpuinfo_field_equals(cpuinfo, "CPU revision", "0");
194}
195
Robert Sloan726e9d12018-09-11 11:45:04 -0700196
197#if defined(__cplusplus)
198} // extern C
199#endif
200
201#endif // OPENSSL_HEADER_CRYPTO_CPU_ARM_LINUX_H