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.

317 lines
9.9 KiB

  1. /*
  2. * Copyright 2009-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 <unistd.h>
  15. #if defined(__linux) || defined(_AIX)
  16. # include <sys/utsname.h>
  17. #endif
  18. #if defined(_AIX53) /* defined even on post-5.3 */
  19. # include <sys/systemcfg.h>
  20. # if !defined(__power_set)
  21. # define __power_set(a) (_system_configuration.implementation & (a))
  22. # endif
  23. #endif
  24. #include <openssl/crypto.h>
  25. #include <openssl/bn.h>
  26. #include "ppc_arch.h"
  27. unsigned int OPENSSL_ppccap_P = 0;
  28. static sigset_t all_masked;
  29. #ifdef OPENSSL_BN_ASM_MONT
  30. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  31. const BN_ULONG *np, const BN_ULONG *n0, int num)
  32. {
  33. int bn_mul_mont_fpu64(BN_ULONG *rp, const BN_ULONG *ap,
  34. const BN_ULONG *bp, const BN_ULONG *np,
  35. const BN_ULONG *n0, int num);
  36. int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  37. const BN_ULONG *np, const BN_ULONG *n0, int num);
  38. if (sizeof(size_t) == 4) {
  39. # if 1 || (defined(__APPLE__) && defined(__MACH__))
  40. if (num >= 8 && (num & 3) == 0 && (OPENSSL_ppccap_P & PPC_FPU64))
  41. return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
  42. # else
  43. /*
  44. * boundary of 32 was experimentally determined on Linux 2.6.22,
  45. * might have to be adjusted on AIX...
  46. */
  47. if (num >= 32 && (num & 3) == 0 && (OPENSSL_ppccap_P & PPC_FPU64)) {
  48. sigset_t oset;
  49. int ret;
  50. sigprocmask(SIG_SETMASK, &all_masked, &oset);
  51. ret = bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
  52. sigprocmask(SIG_SETMASK, &oset, NULL);
  53. return ret;
  54. }
  55. # endif
  56. } else if ((OPENSSL_ppccap_P & PPC_FPU64))
  57. /*
  58. * this is a "must" on POWER6, but run-time detection is not
  59. * implemented yet...
  60. */
  61. return bn_mul_mont_fpu64(rp, ap, bp, np, n0, num);
  62. return bn_mul_mont_int(rp, ap, bp, np, n0, num);
  63. }
  64. #endif
  65. void sha256_block_p8(void *ctx, const void *inp, size_t len);
  66. void sha256_block_ppc(void *ctx, const void *inp, size_t len);
  67. void sha256_block_data_order(void *ctx, const void *inp, size_t len)
  68. {
  69. OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha256_block_p8(ctx, inp, len) :
  70. sha256_block_ppc(ctx, inp, len);
  71. }
  72. void sha512_block_p8(void *ctx, const void *inp, size_t len);
  73. void sha512_block_ppc(void *ctx, const void *inp, size_t len);
  74. void sha512_block_data_order(void *ctx, const void *inp, size_t len)
  75. {
  76. OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha512_block_p8(ctx, inp, len) :
  77. sha512_block_ppc(ctx, inp, len);
  78. }
  79. #ifndef OPENSSL_NO_CHACHA
  80. void ChaCha20_ctr32_int(unsigned char *out, const unsigned char *inp,
  81. size_t len, const unsigned int key[8],
  82. const unsigned int counter[4]);
  83. void ChaCha20_ctr32_vmx(unsigned char *out, const unsigned char *inp,
  84. size_t len, const unsigned int key[8],
  85. const unsigned int counter[4]);
  86. void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp,
  87. size_t len, const unsigned int key[8],
  88. const unsigned int counter[4])
  89. {
  90. OPENSSL_ppccap_P & PPC_ALTIVEC
  91. ? ChaCha20_ctr32_vmx(out, inp, len, key, counter)
  92. : ChaCha20_ctr32_int(out, inp, len, key, counter);
  93. }
  94. #endif
  95. #ifndef OPENSSL_NO_POLY1305
  96. void poly1305_init_int(void *ctx, const unsigned char key[16]);
  97. void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len,
  98. unsigned int padbit);
  99. void poly1305_emit(void *ctx, unsigned char mac[16],
  100. const unsigned int nonce[4]);
  101. void poly1305_init_fpu(void *ctx, const unsigned char key[16]);
  102. void poly1305_blocks_fpu(void *ctx, const unsigned char *inp, size_t len,
  103. unsigned int padbit);
  104. void poly1305_emit_fpu(void *ctx, unsigned char mac[16],
  105. const unsigned int nonce[4]);
  106. int poly1305_init(void *ctx, const unsigned char key[16], void *func[2])
  107. {
  108. if (sizeof(size_t) == 4 && (OPENSSL_ppccap_P & PPC_FPU)) {
  109. poly1305_init_fpu(ctx,key);
  110. func[0] = poly1305_blocks_fpu;
  111. func[1] = poly1305_emit_fpu;
  112. } else {
  113. poly1305_init_int(ctx,key);
  114. func[0] = poly1305_blocks;
  115. func[1] = poly1305_emit;
  116. }
  117. return 1;
  118. }
  119. #endif
  120. static sigjmp_buf ill_jmp;
  121. static void ill_handler(int sig)
  122. {
  123. siglongjmp(ill_jmp, sig);
  124. }
  125. void OPENSSL_fpu_probe(void);
  126. void OPENSSL_ppc64_probe(void);
  127. void OPENSSL_altivec_probe(void);
  128. void OPENSSL_crypto207_probe(void);
  129. void OPENSSL_madd300_probe(void);
  130. /*
  131. * Use a weak reference to getauxval() so we can use it if it is available
  132. * but don't break the build if it is not. Note that this is *link-time*
  133. * feature detection, not *run-time*. In other words if we link with
  134. * symbol present, it's expected to be present even at run-time.
  135. */
  136. #if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
  137. extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
  138. #else
  139. static unsigned long (*getauxval) (unsigned long) = NULL;
  140. #endif
  141. /* I wish <sys/auxv.h> was universally available */
  142. #define HWCAP 16 /* AT_HWCAP */
  143. #define HWCAP_PPC64 (1U << 30)
  144. #define HWCAP_ALTIVEC (1U << 28)
  145. #define HWCAP_FPU (1U << 27)
  146. #define HWCAP_POWER6_EXT (1U << 9)
  147. #define HWCAP_VSX (1U << 7)
  148. #define HWCAP2 26 /* AT_HWCAP2 */
  149. #define HWCAP_VEC_CRYPTO (1U << 25)
  150. #define HWCAP_ARCH_3_00 (1U << 23)
  151. # if defined(__GNUC__) && __GNUC__>=2
  152. __attribute__ ((constructor))
  153. # endif
  154. void OPENSSL_cpuid_setup(void)
  155. {
  156. char *e;
  157. struct sigaction ill_oact, ill_act;
  158. sigset_t oset;
  159. static int trigger = 0;
  160. if (trigger)
  161. return;
  162. trigger = 1;
  163. if ((e = getenv("OPENSSL_ppccap"))) {
  164. OPENSSL_ppccap_P = strtoul(e, NULL, 0);
  165. return;
  166. }
  167. OPENSSL_ppccap_P = 0;
  168. #if defined(_AIX)
  169. OPENSSL_ppccap_P |= PPC_FPU;
  170. if (sizeof(size_t) == 4) {
  171. struct utsname uts;
  172. # if defined(_SC_AIX_KERNEL_BITMODE)
  173. if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
  174. return;
  175. # endif
  176. if (uname(&uts) != 0 || atoi(uts.version) < 6)
  177. return;
  178. }
  179. # if defined(__power_set)
  180. /*
  181. * Value used in __power_set is a single-bit 1<<n one denoting
  182. * specific processor class. Incidentally 0xffffffff<<n can be
  183. * used to denote specific processor and its successors.
  184. */
  185. if (sizeof(size_t) == 4) {
  186. /* In 32-bit case PPC_FPU64 is always fastest [if option] */
  187. if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
  188. OPENSSL_ppccap_P |= PPC_FPU64;
  189. } else {
  190. /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
  191. if (__power_set(0x1U<<14)) /* POWER6 */
  192. OPENSSL_ppccap_P |= PPC_FPU64;
  193. }
  194. if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
  195. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  196. if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
  197. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  198. if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
  199. OPENSSL_ppccap_P |= PPC_MADD300;
  200. return;
  201. # endif
  202. #endif
  203. if (getauxval != NULL) {
  204. unsigned long hwcap = getauxval(HWCAP);
  205. if (hwcap & HWCAP_FPU) {
  206. OPENSSL_ppccap_P |= PPC_FPU;
  207. if (sizeof(size_t) == 4) {
  208. /* In 32-bit case PPC_FPU64 is always fastest [if option] */
  209. if (hwcap & HWCAP_PPC64)
  210. OPENSSL_ppccap_P |= PPC_FPU64;
  211. } else {
  212. /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
  213. if (hwcap & HWCAP_POWER6_EXT)
  214. OPENSSL_ppccap_P |= PPC_FPU64;
  215. }
  216. }
  217. if (hwcap & HWCAP_ALTIVEC) {
  218. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  219. if ((hwcap & HWCAP_VSX) && (getauxval(HWCAP2) & HWCAP_VEC_CRYPTO))
  220. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  221. }
  222. if (hwcap & HWCAP_ARCH_3_00) {
  223. OPENSSL_ppccap_P |= PPC_MADD300;
  224. }
  225. return;
  226. }
  227. sigfillset(&all_masked);
  228. sigdelset(&all_masked, SIGILL);
  229. sigdelset(&all_masked, SIGTRAP);
  230. #ifdef SIGEMT
  231. sigdelset(&all_masked, SIGEMT);
  232. #endif
  233. sigdelset(&all_masked, SIGFPE);
  234. sigdelset(&all_masked, SIGBUS);
  235. sigdelset(&all_masked, SIGSEGV);
  236. memset(&ill_act, 0, sizeof(ill_act));
  237. ill_act.sa_handler = ill_handler;
  238. ill_act.sa_mask = all_masked;
  239. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  240. sigaction(SIGILL, &ill_act, &ill_oact);
  241. if (sigsetjmp(ill_jmp,1) == 0) {
  242. OPENSSL_fpu_probe();
  243. OPENSSL_ppccap_P |= PPC_FPU;
  244. if (sizeof(size_t) == 4) {
  245. #ifdef __linux
  246. struct utsname uts;
  247. if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
  248. #endif
  249. if (sigsetjmp(ill_jmp, 1) == 0) {
  250. OPENSSL_ppc64_probe();
  251. OPENSSL_ppccap_P |= PPC_FPU64;
  252. }
  253. } else {
  254. /*
  255. * Wanted code detecting POWER6 CPU and setting PPC_FPU64
  256. */
  257. }
  258. }
  259. if (sigsetjmp(ill_jmp, 1) == 0) {
  260. OPENSSL_altivec_probe();
  261. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  262. if (sigsetjmp(ill_jmp, 1) == 0) {
  263. OPENSSL_crypto207_probe();
  264. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  265. }
  266. }
  267. if (sigsetjmp(ill_jmp, 1) == 0) {
  268. OPENSSL_madd300_probe();
  269. OPENSSL_ppccap_P |= PPC_MADD300;
  270. }
  271. sigaction(SIGILL, &ill_oact, NULL);
  272. sigprocmask(SIG_SETMASK, &oset, NULL);
  273. }