1 /* 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
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 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
41 typedef struct {
42 const char *data;
43 size_t len;
44 } STRING_PIECE;
45
STRING_PIECE_equals(const STRING_PIECE * a,const char * b)46 static 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.
STRING_PIECE_split(STRING_PIECE * out_left,STRING_PIECE * out_right,const STRING_PIECE * in,char sep)54 static 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|.
STRING_PIECE_get_delimited(STRING_PIECE * s,STRING_PIECE * out,char sep)73 static 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|.
STRING_PIECE_trim(STRING_PIECE * s)87 static 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.
extract_cpuinfo_field(STRING_PIECE * out,const STRING_PIECE * in,const char * field)101 static 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
121 // has_list_item treats |list| as a space-separated list of items and returns
122 // one if |item| is contained in |list| and zero otherwise.
has_list_item(const STRING_PIECE * list,const char * item)123 static int has_list_item(const STRING_PIECE *list, const char *item) {
124 STRING_PIECE remaining = *list, feature;
125 while (STRING_PIECE_get_delimited(&remaining, &feature, ' ')) {
126 if (STRING_PIECE_equals(&feature, item)) {
127 return 1;
128 }
129 }
130 return 0;
131 }
132
133 // crypto_get_arm_hwcap2_from_cpuinfo returns an equivalent ARM |AT_HWCAP2|
134 // value from |cpuinfo|.
crypto_get_arm_hwcap2_from_cpuinfo(const STRING_PIECE * cpuinfo)135 static unsigned long crypto_get_arm_hwcap2_from_cpuinfo(
136 const STRING_PIECE *cpuinfo) {
137 STRING_PIECE features;
138 if (!extract_cpuinfo_field(&features, cpuinfo, "Features")) {
139 return 0;
140 }
141
142 unsigned long ret = 0;
143 if (has_list_item(&features, "aes")) {
144 ret |= HWCAP2_AES;
145 }
146 if (has_list_item(&features, "pmull")) {
147 ret |= HWCAP2_PMULL;
148 }
149 if (has_list_item(&features, "sha1")) {
150 ret |= HWCAP2_SHA1;
151 }
152 if (has_list_item(&features, "sha2")) {
153 ret |= HWCAP2_SHA2;
154 }
155 return ret;
156 }
157
158
159 #if defined(__cplusplus)
160 } // extern C
161 #endif
162
163 #endif // OPENSSL_HEADER_CRYPTO_CPU_ARM_LINUX_H
164