A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
4.6 KiB

  1. /*
  2. * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include <openssl/crypto.h>
  15. #include "arm_arch.h"
  16. unsigned int OPENSSL_armcap_P = 0;
  17. #if __ARM_MAX_ARCH__<7
  18. void OPENSSL_cpuid_setup(void)
  19. {
  20. }
  21. unsigned long OPENSSL_rdtsc(void)
  22. {
  23. return 0;
  24. }
  25. #else
  26. static sigset_t all_masked;
  27. static sigjmp_buf ill_jmp;
  28. static void ill_handler(int sig)
  29. {
  30. siglongjmp(ill_jmp, sig);
  31. }
  32. /*
  33. * Following subroutines could have been inlined, but it's not all
  34. * ARM compilers support inline assembler...
  35. */
  36. void _armv7_neon_probe(void);
  37. void _armv8_aes_probe(void);
  38. void _armv8_sha1_probe(void);
  39. void _armv8_sha256_probe(void);
  40. void _armv8_pmull_probe(void);
  41. unsigned long _armv7_tick(void);
  42. unsigned long OPENSSL_rdtsc(void)
  43. {
  44. if (OPENSSL_armcap_P & ARMV7_TICK)
  45. return _armv7_tick();
  46. else
  47. return 0;
  48. }
  49. # if defined(__GNUC__) && __GNUC__>=2
  50. void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
  51. # endif
  52. /*
  53. * Use a weak reference to getauxval() so we can use it if it is available but
  54. * don't break the build if it is not.
  55. */
  56. # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
  57. extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
  58. # else
  59. static unsigned long (*getauxval) (unsigned long) = NULL;
  60. # endif
  61. /*
  62. * ARM puts the the feature bits for Crypto Extensions in AT_HWCAP2, whereas
  63. * AArch64 used AT_HWCAP.
  64. */
  65. # if defined(__arm__) || defined (__arm)
  66. # define HWCAP 16
  67. /* AT_HWCAP */
  68. # define HWCAP_NEON (1 << 12)
  69. # define HWCAP_CE 26
  70. /* AT_HWCAP2 */
  71. # define HWCAP_CE_AES (1 << 0)
  72. # define HWCAP_CE_PMULL (1 << 1)
  73. # define HWCAP_CE_SHA1 (1 << 2)
  74. # define HWCAP_CE_SHA256 (1 << 3)
  75. # elif defined(__aarch64__)
  76. # define HWCAP 16
  77. /* AT_HWCAP */
  78. # define HWCAP_NEON (1 << 1)
  79. # define HWCAP_CE HWCAP
  80. # define HWCAP_CE_AES (1 << 3)
  81. # define HWCAP_CE_PMULL (1 << 4)
  82. # define HWCAP_CE_SHA1 (1 << 5)
  83. # define HWCAP_CE_SHA256 (1 << 6)
  84. # endif
  85. void OPENSSL_cpuid_setup(void)
  86. {
  87. char *e;
  88. struct sigaction ill_oact, ill_act;
  89. sigset_t oset;
  90. static int trigger = 0;
  91. if (trigger)
  92. return;
  93. trigger = 1;
  94. if ((e = getenv("OPENSSL_armcap"))) {
  95. OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
  96. return;
  97. }
  98. sigfillset(&all_masked);
  99. sigdelset(&all_masked, SIGILL);
  100. sigdelset(&all_masked, SIGTRAP);
  101. sigdelset(&all_masked, SIGFPE);
  102. sigdelset(&all_masked, SIGBUS);
  103. sigdelset(&all_masked, SIGSEGV);
  104. OPENSSL_armcap_P = 0;
  105. memset(&ill_act, 0, sizeof(ill_act));
  106. ill_act.sa_handler = ill_handler;
  107. ill_act.sa_mask = all_masked;
  108. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  109. sigaction(SIGILL, &ill_act, &ill_oact);
  110. if (getauxval != NULL) {
  111. if (getauxval(HWCAP) & HWCAP_NEON) {
  112. unsigned long hwcap = getauxval(HWCAP_CE);
  113. OPENSSL_armcap_P |= ARMV7_NEON;
  114. if (hwcap & HWCAP_CE_AES)
  115. OPENSSL_armcap_P |= ARMV8_AES;
  116. if (hwcap & HWCAP_CE_PMULL)
  117. OPENSSL_armcap_P |= ARMV8_PMULL;
  118. if (hwcap & HWCAP_CE_SHA1)
  119. OPENSSL_armcap_P |= ARMV8_SHA1;
  120. if (hwcap & HWCAP_CE_SHA256)
  121. OPENSSL_armcap_P |= ARMV8_SHA256;
  122. }
  123. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  124. _armv7_neon_probe();
  125. OPENSSL_armcap_P |= ARMV7_NEON;
  126. if (sigsetjmp(ill_jmp, 1) == 0) {
  127. _armv8_pmull_probe();
  128. OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
  129. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  130. _armv8_aes_probe();
  131. OPENSSL_armcap_P |= ARMV8_AES;
  132. }
  133. if (sigsetjmp(ill_jmp, 1) == 0) {
  134. _armv8_sha1_probe();
  135. OPENSSL_armcap_P |= ARMV8_SHA1;
  136. }
  137. if (sigsetjmp(ill_jmp, 1) == 0) {
  138. _armv8_sha256_probe();
  139. OPENSSL_armcap_P |= ARMV8_SHA256;
  140. }
  141. }
  142. if (sigsetjmp(ill_jmp, 1) == 0) {
  143. _armv7_tick();
  144. OPENSSL_armcap_P |= ARMV7_TICK;
  145. }
  146. sigaction(SIGILL, &ill_oact, NULL);
  147. sigprocmask(SIG_SETMASK, &oset, NULL);
  148. }
  149. #endif