summaryrefslogtreecommitdiffstats
path: root/src/crypto/cpu-arm.c
diff options
context:
space:
mode:
authorAdam Langley <agl@google.com>2015-01-22 14:27:53 -0800
committerAdam Langley <agl@google.com>2015-01-30 16:52:14 -0800
commitd9e397b599b13d642138480a28c14db7a136bf05 (patch)
tree34bab61dc4ce323b123ad4614dbc07e86ea2f9ef /src/crypto/cpu-arm.c
downloadexternal_boringssl-d9e397b599b13d642138480a28c14db7a136bf05.zip
external_boringssl-d9e397b599b13d642138480a28c14db7a136bf05.tar.gz
external_boringssl-d9e397b599b13d642138480a28c14db7a136bf05.tar.bz2
Initial commit of BoringSSL for Android.
Diffstat (limited to 'src/crypto/cpu-arm.c')
-rw-r--r--src/crypto/cpu-arm.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/crypto/cpu-arm.c b/src/crypto/cpu-arm.c
new file mode 100644
index 0000000..96392d8
--- /dev/null
+++ b/src/crypto/cpu-arm.c
@@ -0,0 +1,112 @@
+/* Copyright (c) 2014, Google Inc.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+
+#include <openssl/cpu.h>
+
+#if defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
+
+#include <inttypes.h>
+#include <stdio.h>
+
+#include "arm_arch.h"
+
+
+/* We can't include <sys/auxv.h> because the Android SDK version against which
+ * Chromium builds is too old to have it. Instead we define all the constants
+ * that we need and have a weak pointer to getauxval. */
+
+unsigned long getauxval(unsigned long type) __attribute__((weak));
+
+static const unsigned long AT_HWCAP = 16;
+static const unsigned long AT_HWCAP2 = 26;
+
+char CRYPTO_is_NEON_capable(void) {
+ return (OPENSSL_armcap_P & ARMV7_NEON) != 0;
+}
+
+void CRYPTO_set_NEON_capable(char neon_capable) {
+ if (neon_capable) {
+ OPENSSL_armcap_P |= ARMV7_NEON;
+ } else {
+ OPENSSL_armcap_P &= ~ARMV7_NEON;
+ }
+}
+
+char CRYPTO_is_NEON_functional(void) {
+ static const uint32_t kWantFlags = ARMV7_NEON | ARMV7_NEON_FUNCTIONAL;
+ return (OPENSSL_armcap_P & kWantFlags) == kWantFlags;
+}
+
+void CRYPTO_set_NEON_functional(char neon_functional) {
+ if (neon_functional) {
+ OPENSSL_armcap_P |= ARMV7_NEON_FUNCTIONAL;
+ } else {
+ OPENSSL_armcap_P &= ~ARMV7_NEON_FUNCTIONAL;
+ }
+}
+
+void OPENSSL_cpuid_setup(void) {
+ if (getauxval == NULL) {
+ return;
+ }
+
+ unsigned long hwcap = getauxval(AT_HWCAP);
+
+#if defined(OPENSSL_ARM)
+ static const unsigned long kNEON = 1 << 12;
+ if ((hwcap & kNEON) == 0) {
+ return;
+ }
+
+ /* In 32-bit mode, the ARMv8 feature bits are in a different aux vector
+ * value. */
+ hwcap = getauxval(AT_HWCAP2);
+
+ /* See /usr/include/asm/hwcap.h on an ARM installation for the source of
+ * these values. */
+ static const unsigned long kAES = 1 << 0;
+ static const unsigned long kPMULL = 1 << 1;
+ static const unsigned long kSHA1 = 1 << 2;
+ static const unsigned long kSHA256 = 1 << 3;
+#elif defined(OPENSSL_AARCH64)
+ /* See /usr/include/asm/hwcap.h on an aarch64 installation for the source of
+ * these values. */
+ static const unsigned long kNEON = 1 << 1;
+ static const unsigned long kAES = 1 << 3;
+ static const unsigned long kPMULL = 1 << 4;
+ static const unsigned long kSHA1 = 1 << 5;
+ static const unsigned long kSHA256 = 1 << 6;
+
+ if ((hwcap & kNEON) == 0) {
+ return;
+ }
+#endif
+
+ OPENSSL_armcap_P |= ARMV7_NEON | ARMV7_NEON_FUNCTIONAL;
+
+ if (hwcap & kAES) {
+ OPENSSL_armcap_P |= ARMV8_AES;
+ }
+ if (hwcap & kPMULL) {
+ OPENSSL_armcap_P |= ARMV8_PMULL;
+ }
+ if (hwcap & kSHA1) {
+ OPENSSL_armcap_P |= ARMV8_SHA1;
+ }
+ if (hwcap & kSHA256) {
+ OPENSSL_armcap_P |= ARMV8_SHA256;
+ }
+}
+
+#endif /* defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) */