1 // Copyright 2018 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
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 #ifndef OPENSSL_HEADER_CRYPTO_CPU_ARM_LINUX_H
16 #define OPENSSL_HEADER_CRYPTO_CPU_ARM_LINUX_H
17
18 #include <openssl/base.h>
19
20 #include <string.h>
21
22 #include "internal.h"
23
24 #if defined(__cplusplus)
25 extern "C" {
26 #endif
27
28
29 // 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.
31
32 #define CRYPTO_HWCAP_NEON (1 << 12)
33
34 // See /usr/include/asm/hwcap.h on an ARM installation for the source of
35 // these values.
36 // We add the prefix "CRYPTO_" to the definitions so as not to collide with
37 // some versions of glibc (>= 2.41) that expose them through <sys/auxv.h>.
38 #define CRYPTO_HWCAP2_AES (1 << 0)
39 #define CRYPTO_HWCAP2_PMULL (1 << 1)
40 #define CRYPTO_HWCAP2_SHA1 (1 << 2)
41 #define CRYPTO_HWCAP2_SHA2 (1 << 3)
42
43 typedef struct {
44 const char *data;
45 size_t len;
46 } STRING_PIECE;
47
STRING_PIECE_equals(const STRING_PIECE * a,const char * b)48 static int STRING_PIECE_equals(const STRING_PIECE *a, const char *b) {
49 size_t b_len = strlen(b);
50 return a->len == b_len && OPENSSL_memcmp(a->data, b, b_len) == 0;
51 }
52
53 // STRING_PIECE_split finds the first occurence of |sep| in |in| and, if found,
54 // sets |*out_left| and |*out_right| to |in| split before and after it. It
55 // returns one if |sep| was found and zero otherwise.
STRING_PIECE_split(STRING_PIECE * out_left,STRING_PIECE * out_right,const STRING_PIECE * in,char sep)56 static int STRING_PIECE_split(STRING_PIECE *out_left, STRING_PIECE *out_right,
57 const STRING_PIECE *in, char sep) {
58 const char *p = (const char *)OPENSSL_memchr(in->data, sep, in->len);
59 if (p == NULL) {
60 return 0;
61 }
62 // |out_left| or |out_right| may alias |in|, so make a copy.
63 STRING_PIECE in_copy = *in;
64 out_left->data = in_copy.data;
65 out_left->len = p - in_copy.data;
66 out_right->data = in_copy.data + out_left->len + 1;
67 out_right->len = in_copy.len - out_left->len - 1;
68 return 1;
69 }
70
71 // STRING_PIECE_get_delimited reads a |sep|-delimited entry from |s|, writing it
72 // to |out| and updating |s| to point beyond it. It returns one on success and
73 // zero if |s| is empty. If |s| is has no copies of |sep| and is non-empty, it
74 // reads the entire string to |out|.
STRING_PIECE_get_delimited(STRING_PIECE * s,STRING_PIECE * out,char sep)75 static int STRING_PIECE_get_delimited(STRING_PIECE *s, STRING_PIECE *out, char sep) {
76 if (s->len == 0) {
77 return 0;
78 }
79 if (!STRING_PIECE_split(out, s, s, sep)) {
80 // |s| had no instances of |sep|. Return the entire string.
81 *out = *s;
82 s->data += s->len;
83 s->len = 0;
84 }
85 return 1;
86 }
87
88 // STRING_PIECE_trim removes leading and trailing whitespace from |s|.
STRING_PIECE_trim(STRING_PIECE * s)89 static void STRING_PIECE_trim(STRING_PIECE *s) {
90 while (s->len != 0 && (s->data[0] == ' ' || s->data[0] == '\t')) {
91 s->data++;
92 s->len--;
93 }
94 while (s->len != 0 &&
95 (s->data[s->len - 1] == ' ' || s->data[s->len - 1] == '\t')) {
96 s->len--;
97 }
98 }
99
100 // extract_cpuinfo_field extracts a /proc/cpuinfo field named |field| from
101 // |in|. If found, it sets |*out| to the value and returns one. Otherwise, it
102 // returns zero.
extract_cpuinfo_field(STRING_PIECE * out,const STRING_PIECE * in,const char * field)103 static int extract_cpuinfo_field(STRING_PIECE *out, const STRING_PIECE *in,
104 const char *field) {
105 // Process |in| one line at a time.
106 STRING_PIECE remaining = *in, line;
107 while (STRING_PIECE_get_delimited(&remaining, &line, '\n')) {
108 STRING_PIECE key, value;
109 if (!STRING_PIECE_split(&key, &value, &line, ':')) {
110 continue;
111 }
112 STRING_PIECE_trim(&key);
113 if (STRING_PIECE_equals(&key, field)) {
114 STRING_PIECE_trim(&value);
115 *out = value;
116 return 1;
117 }
118 }
119
120 return 0;
121 }
122
123 // has_list_item treats |list| as a space-separated list of items and returns
124 // one if |item| is contained in |list| and zero otherwise.
has_list_item(const STRING_PIECE * list,const char * item)125 static int has_list_item(const STRING_PIECE *list, const char *item) {
126 STRING_PIECE remaining = *list, feature;
127 while (STRING_PIECE_get_delimited(&remaining, &feature, ' ')) {
128 if (STRING_PIECE_equals(&feature, item)) {
129 return 1;
130 }
131 }
132 return 0;
133 }
134
135 // crypto_get_arm_hwcap2_from_cpuinfo returns an equivalent ARM |AT_HWCAP2|
136 // value from |cpuinfo|.
crypto_get_arm_hwcap2_from_cpuinfo(const STRING_PIECE * cpuinfo)137 static unsigned long crypto_get_arm_hwcap2_from_cpuinfo(
138 const STRING_PIECE *cpuinfo) {
139 STRING_PIECE features;
140 if (!extract_cpuinfo_field(&features, cpuinfo, "Features")) {
141 return 0;
142 }
143
144 unsigned long ret = 0;
145 if (has_list_item(&features, "aes")) {
146 ret |= CRYPTO_HWCAP2_AES;
147 }
148 if (has_list_item(&features, "pmull")) {
149 ret |= CRYPTO_HWCAP2_PMULL;
150 }
151 if (has_list_item(&features, "sha1")) {
152 ret |= CRYPTO_HWCAP2_SHA1;
153 }
154 if (has_list_item(&features, "sha2")) {
155 ret |= CRYPTO_HWCAP2_SHA2;
156 }
157 return ret;
158 }
159
160
161 #if defined(__cplusplus)
162 } // extern C
163 #endif
164
165 #endif // OPENSSL_HEADER_CRYPTO_CPU_ARM_LINUX_H
166