2
0

ppccap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright 2009-2021 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. #if defined(__APPLE__) && defined(__MACH__)
  25. # include <sys/types.h>
  26. # include <sys/sysctl.h>
  27. #endif
  28. #include <openssl/crypto.h>
  29. #include <openssl/bn.h>
  30. #include <internal/cryptlib.h>
  31. #include <crypto/chacha.h>
  32. #include "bn/bn_local.h"
  33. #include "ppc_arch.h"
  34. unsigned int OPENSSL_ppccap_P = 0;
  35. static sigset_t all_masked;
  36. #ifdef OPENSSL_BN_ASM_MONT
  37. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  38. const BN_ULONG *np, const BN_ULONG *n0, int num)
  39. {
  40. int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  41. const BN_ULONG *np, const BN_ULONG *n0, int num);
  42. int bn_mul4x_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  43. const BN_ULONG *np, const BN_ULONG *n0, int num);
  44. if (num < 4)
  45. return 0;
  46. if ((num & 3) == 0)
  47. return bn_mul4x_mont_int(rp, ap, bp, np, n0, num);
  48. /*
  49. * There used to be [optional] call to bn_mul_mont_fpu64 here,
  50. * but above subroutine is faster on contemporary processors.
  51. * Formulation means that there might be old processors where
  52. * FPU code path would be faster, POWER6 perhaps, but there was
  53. * no opportunity to figure it out...
  54. */
  55. return bn_mul_mont_int(rp, ap, bp, np, n0, num);
  56. }
  57. #endif
  58. void sha256_block_p8(void *ctx, const void *inp, size_t len);
  59. void sha256_block_ppc(void *ctx, const void *inp, size_t len);
  60. void sha256_block_data_order(void *ctx, const void *inp, size_t len);
  61. void sha256_block_data_order(void *ctx, const void *inp, size_t len)
  62. {
  63. OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha256_block_p8(ctx, inp, len) :
  64. sha256_block_ppc(ctx, inp, len);
  65. }
  66. void sha512_block_p8(void *ctx, const void *inp, size_t len);
  67. void sha512_block_ppc(void *ctx, const void *inp, size_t len);
  68. void sha512_block_data_order(void *ctx, const void *inp, size_t len);
  69. void sha512_block_data_order(void *ctx, const void *inp, size_t len)
  70. {
  71. OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha512_block_p8(ctx, inp, len) :
  72. sha512_block_ppc(ctx, inp, len);
  73. }
  74. #ifndef OPENSSL_NO_CHACHA
  75. void ChaCha20_ctr32_int(unsigned char *out, const unsigned char *inp,
  76. size_t len, const unsigned int key[8],
  77. const unsigned int counter[4]);
  78. void ChaCha20_ctr32_vmx(unsigned char *out, const unsigned char *inp,
  79. size_t len, const unsigned int key[8],
  80. const unsigned int counter[4]);
  81. void ChaCha20_ctr32_vsx(unsigned char *out, const unsigned char *inp,
  82. size_t len, const unsigned int key[8],
  83. const unsigned int counter[4]);
  84. void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp,
  85. size_t len, const unsigned int key[8],
  86. const unsigned int counter[4])
  87. {
  88. OPENSSL_ppccap_P & PPC_CRYPTO207
  89. ? ChaCha20_ctr32_vsx(out, inp, len, key, counter)
  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. int poly1305_init(void *ctx, const unsigned char key[16], void *func[2])
  108. {
  109. if (sizeof(size_t) == 4 && (OPENSSL_ppccap_P & PPC_FPU)) {
  110. poly1305_init_fpu(ctx, key);
  111. func[0] = (void*)(uintptr_t)poly1305_blocks_fpu;
  112. func[1] = (void*)(uintptr_t)poly1305_emit_fpu;
  113. } else {
  114. poly1305_init_int(ctx, key);
  115. func[0] = (void*)(uintptr_t)poly1305_blocks;
  116. func[1] = (void*)(uintptr_t)poly1305_emit;
  117. }
  118. return 1;
  119. }
  120. #endif
  121. #ifdef ECP_NISTZ256_ASM
  122. void ecp_nistz256_mul_mont(unsigned long res[4], const unsigned long a[4],
  123. const unsigned long b[4]);
  124. void ecp_nistz256_to_mont(unsigned long res[4], const unsigned long in[4]);
  125. void ecp_nistz256_to_mont(unsigned long res[4], const unsigned long in[4])
  126. {
  127. static const unsigned long RR[] = { 0x0000000000000003U,
  128. 0xfffffffbffffffffU,
  129. 0xfffffffffffffffeU,
  130. 0x00000004fffffffdU };
  131. ecp_nistz256_mul_mont(res, in, RR);
  132. }
  133. void ecp_nistz256_from_mont(unsigned long res[4], const unsigned long in[4]);
  134. void ecp_nistz256_from_mont(unsigned long res[4], const unsigned long in[4])
  135. {
  136. static const unsigned long one[] = { 1, 0, 0, 0 };
  137. ecp_nistz256_mul_mont(res, in, one);
  138. }
  139. #endif
  140. static sigjmp_buf ill_jmp;
  141. static void ill_handler(int sig)
  142. {
  143. siglongjmp(ill_jmp, sig);
  144. }
  145. void OPENSSL_fpu_probe(void);
  146. void OPENSSL_ppc64_probe(void);
  147. void OPENSSL_altivec_probe(void);
  148. void OPENSSL_crypto207_probe(void);
  149. void OPENSSL_madd300_probe(void);
  150. long OPENSSL_rdtsc_mftb(void);
  151. long OPENSSL_rdtsc_mfspr268(void);
  152. uint32_t OPENSSL_rdtsc(void)
  153. {
  154. if (OPENSSL_ppccap_P & PPC_MFTB)
  155. return OPENSSL_rdtsc_mftb();
  156. else if (OPENSSL_ppccap_P & PPC_MFSPR268)
  157. return OPENSSL_rdtsc_mfspr268();
  158. else
  159. return 0;
  160. }
  161. size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
  162. size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
  163. size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
  164. {
  165. if (OPENSSL_ppccap_P & PPC_MFTB)
  166. return OPENSSL_instrument_bus_mftb(out, cnt);
  167. else if (OPENSSL_ppccap_P & PPC_MFSPR268)
  168. return OPENSSL_instrument_bus_mfspr268(out, cnt);
  169. else
  170. return 0;
  171. }
  172. size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
  173. size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
  174. size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
  175. {
  176. if (OPENSSL_ppccap_P & PPC_MFTB)
  177. return OPENSSL_instrument_bus2_mftb(out, cnt, max);
  178. else if (OPENSSL_ppccap_P & PPC_MFSPR268)
  179. return OPENSSL_instrument_bus2_mfspr268(out, cnt, max);
  180. else
  181. return 0;
  182. }
  183. #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
  184. # if __GLIBC_PREREQ(2, 16)
  185. # include <sys/auxv.h>
  186. # define OSSL_IMPLEMENT_GETAUXVAL
  187. # elif defined(__ANDROID_API__)
  188. /* see https://developer.android.google.cn/ndk/guides/cpu-features */
  189. # if __ANDROID_API__ >= 18
  190. # include <sys/auxv.h>
  191. # define OSSL_IMPLEMENT_GETAUXVAL
  192. # endif
  193. # endif
  194. #endif
  195. #if defined(__FreeBSD__)
  196. # include <sys/param.h>
  197. # if __FreeBSD_version >= 1200000
  198. # include <sys/auxv.h>
  199. # define OSSL_IMPLEMENT_GETAUXVAL
  200. static unsigned long getauxval(unsigned long key)
  201. {
  202. unsigned long val = 0ul;
  203. if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
  204. return 0ul;
  205. return val;
  206. }
  207. # endif
  208. #endif
  209. /* I wish <sys/auxv.h> was universally available */
  210. #define HWCAP 16 /* AT_HWCAP */
  211. #define HWCAP_PPC64 (1U << 30)
  212. #define HWCAP_ALTIVEC (1U << 28)
  213. #define HWCAP_FPU (1U << 27)
  214. #define HWCAP_POWER6_EXT (1U << 9)
  215. #define HWCAP_VSX (1U << 7)
  216. #define HWCAP2 26 /* AT_HWCAP2 */
  217. #define HWCAP_VEC_CRYPTO (1U << 25)
  218. #define HWCAP_ARCH_3_00 (1U << 23)
  219. # if defined(__GNUC__) && __GNUC__>=2
  220. __attribute__ ((constructor))
  221. # endif
  222. void OPENSSL_cpuid_setup(void)
  223. {
  224. char *e;
  225. struct sigaction ill_oact, ill_act;
  226. sigset_t oset;
  227. static int trigger = 0;
  228. if (trigger)
  229. return;
  230. trigger = 1;
  231. if ((e = getenv("OPENSSL_ppccap"))) {
  232. OPENSSL_ppccap_P = strtoul(e, NULL, 0);
  233. return;
  234. }
  235. OPENSSL_ppccap_P = 0;
  236. #if defined(_AIX)
  237. OPENSSL_ppccap_P |= PPC_FPU;
  238. if (sizeof(size_t) == 4) {
  239. struct utsname uts;
  240. # if defined(_SC_AIX_KERNEL_BITMODE)
  241. if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
  242. return;
  243. # endif
  244. if (uname(&uts) != 0 || atoi(uts.version) < 6)
  245. return;
  246. }
  247. # if defined(__power_set)
  248. /*
  249. * Value used in __power_set is a single-bit 1<<n one denoting
  250. * specific processor class. Incidentally 0xffffffff<<n can be
  251. * used to denote specific processor and its successors.
  252. */
  253. if (sizeof(size_t) == 4) {
  254. /* In 32-bit case PPC_FPU64 is always fastest [if option] */
  255. if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
  256. OPENSSL_ppccap_P |= PPC_FPU64;
  257. } else {
  258. /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
  259. if (__power_set(0x1U<<14)) /* POWER6 */
  260. OPENSSL_ppccap_P |= PPC_FPU64;
  261. }
  262. if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
  263. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  264. if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
  265. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  266. if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
  267. OPENSSL_ppccap_P |= PPC_MADD300;
  268. return;
  269. # endif
  270. #endif
  271. #if defined(__APPLE__) && defined(__MACH__)
  272. OPENSSL_ppccap_P |= PPC_FPU;
  273. {
  274. int val;
  275. size_t len = sizeof(val);
  276. if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
  277. if (val)
  278. OPENSSL_ppccap_P |= PPC_FPU64;
  279. }
  280. len = sizeof(val);
  281. if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
  282. if (val)
  283. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  284. }
  285. return;
  286. }
  287. #endif
  288. #ifdef OSSL_IMPLEMENT_GETAUXVAL
  289. {
  290. unsigned long hwcap = getauxval(HWCAP);
  291. unsigned long hwcap2 = getauxval(HWCAP2);
  292. if (hwcap & HWCAP_FPU) {
  293. OPENSSL_ppccap_P |= PPC_FPU;
  294. if (sizeof(size_t) == 4) {
  295. /* In 32-bit case PPC_FPU64 is always fastest [if option] */
  296. if (hwcap & HWCAP_PPC64)
  297. OPENSSL_ppccap_P |= PPC_FPU64;
  298. } else {
  299. /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
  300. if (hwcap & HWCAP_POWER6_EXT)
  301. OPENSSL_ppccap_P |= PPC_FPU64;
  302. }
  303. }
  304. if (hwcap & HWCAP_ALTIVEC) {
  305. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  306. if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
  307. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  308. }
  309. if (hwcap2 & HWCAP_ARCH_3_00) {
  310. OPENSSL_ppccap_P |= PPC_MADD300;
  311. }
  312. }
  313. #endif
  314. sigfillset(&all_masked);
  315. sigdelset(&all_masked, SIGILL);
  316. sigdelset(&all_masked, SIGTRAP);
  317. #ifdef SIGEMT
  318. sigdelset(&all_masked, SIGEMT);
  319. #endif
  320. sigdelset(&all_masked, SIGFPE);
  321. sigdelset(&all_masked, SIGBUS);
  322. sigdelset(&all_masked, SIGSEGV);
  323. memset(&ill_act, 0, sizeof(ill_act));
  324. ill_act.sa_handler = ill_handler;
  325. ill_act.sa_mask = all_masked;
  326. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  327. sigaction(SIGILL, &ill_act, &ill_oact);
  328. #ifndef OSSL_IMPLEMENT_GETAUXVAL
  329. if (sigsetjmp(ill_jmp,1) == 0) {
  330. OPENSSL_fpu_probe();
  331. OPENSSL_ppccap_P |= PPC_FPU;
  332. if (sizeof(size_t) == 4) {
  333. # ifdef __linux
  334. struct utsname uts;
  335. if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
  336. # endif
  337. if (sigsetjmp(ill_jmp, 1) == 0) {
  338. OPENSSL_ppc64_probe();
  339. OPENSSL_ppccap_P |= PPC_FPU64;
  340. }
  341. } else {
  342. /*
  343. * Wanted code detecting POWER6 CPU and setting PPC_FPU64
  344. */
  345. }
  346. }
  347. if (sigsetjmp(ill_jmp, 1) == 0) {
  348. OPENSSL_altivec_probe();
  349. OPENSSL_ppccap_P |= PPC_ALTIVEC;
  350. if (sigsetjmp(ill_jmp, 1) == 0) {
  351. OPENSSL_crypto207_probe();
  352. OPENSSL_ppccap_P |= PPC_CRYPTO207;
  353. }
  354. }
  355. if (sigsetjmp(ill_jmp, 1) == 0) {
  356. OPENSSL_madd300_probe();
  357. OPENSSL_ppccap_P |= PPC_MADD300;
  358. }
  359. #endif
  360. if (sigsetjmp(ill_jmp, 1) == 0) {
  361. OPENSSL_rdtsc_mftb();
  362. OPENSSL_ppccap_P |= PPC_MFTB;
  363. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  364. OPENSSL_rdtsc_mfspr268();
  365. OPENSSL_ppccap_P |= PPC_MFSPR268;
  366. }
  367. sigaction(SIGILL, &ill_oact, NULL);
  368. sigprocmask(SIG_SETMASK, &oset, NULL);
  369. }