| 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 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 |
| 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. |
| 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|. |
| 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|. |
| 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. |
| 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. |
| 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|. |
| 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 |