Sun Jul 19 15:33:08 2020 UTC ()
Expand on importance of not using fpu for crypto if there's no fpu.


(riastradh)
diff -r1.17 -r1.18 src/crypto/external/bsd/openssl/dist/crypto/ppccap.c

cvs diff -r1.17 -r1.18 src/crypto/external/bsd/openssl/dist/crypto/ppccap.c (switch to unified diff)

--- src/crypto/external/bsd/openssl/dist/crypto/ppccap.c 2020/07/15 08:14:41 1.17
+++ src/crypto/external/bsd/openssl/dist/crypto/ppccap.c 2020/07/19 15:33:08 1.18
@@ -1,428 +1,431 @@ @@ -1,428 +1,431 @@
1/* 1/*
2 * Copyright 2009-2019 The OpenSSL Project Authors. All Rights Reserved. 2 * Copyright 2009-2019 The OpenSSL Project Authors. All Rights Reserved.
3 * 3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use 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 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 6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html 7 * https://www.openssl.org/source/license.html
8 */ 8 */
9 9
10#include <stdio.h> 10#include <stdio.h>
11#include <stdlib.h> 11#include <stdlib.h>
12#include <string.h> 12#include <string.h>
13#include <setjmp.h> 13#include <setjmp.h>
14#include <signal.h> 14#include <signal.h>
15#include <unistd.h> 15#include <unistd.h>
16#if defined(__linux) || defined(_AIX) 16#if defined(__linux) || defined(_AIX)
17# include <sys/utsname.h> 17# include <sys/utsname.h>
18#endif 18#endif
19#if defined(_AIX53) /* defined even on post-5.3 */ 19#if defined(_AIX53) /* defined even on post-5.3 */
20# include <sys/systemcfg.h> 20# include <sys/systemcfg.h>
21# if !defined(__power_set) 21# if !defined(__power_set)
22# define __power_set(a) (_system_configuration.implementation & (a)) 22# define __power_set(a) (_system_configuration.implementation & (a))
23# endif 23# endif
24#endif 24#endif
25#if defined(__APPLE__) && defined(__MACH__) 25#if defined(__APPLE__) && defined(__MACH__)
26# include <sys/types.h> 26# include <sys/types.h>
27# include <sys/sysctl.h> 27# include <sys/sysctl.h>
28#endif 28#endif
29#if defined(__NetBSD__) 29#if defined(__NetBSD__)
30# include <sys/param.h> 30# include <sys/param.h>
31# include <sys/sysctl.h> 31# include <sys/sysctl.h>
32#endif 32#endif
33#include <openssl/crypto.h> 33#include <openssl/crypto.h>
34#include <openssl/bn.h> 34#include <openssl/bn.h>
35#include <internal/cryptlib.h> 35#include <internal/cryptlib.h>
36#include <crypto/chacha.h> 36#include <crypto/chacha.h>
37#include "bn/bn_local.h" 37#include "bn/bn_local.h"
38 38
39#include "ppc_arch.h" 39#include "ppc_arch.h"
40 40
41unsigned int OPENSSL_ppccap_P = 0; 41unsigned int OPENSSL_ppccap_P = 0;
42 42
43static sigset_t all_masked; 43static sigset_t all_masked;
44 44
45#ifdef OPENSSL_BN_ASM_MONT 45#ifdef OPENSSL_BN_ASM_MONT
46int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 46int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
47 const BN_ULONG *np, const BN_ULONG *n0, int num) 47 const BN_ULONG *np, const BN_ULONG *n0, int num)
48{ 48{
49 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 49 int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
50 const BN_ULONG *np, const BN_ULONG *n0, int num); 50 const BN_ULONG *np, const BN_ULONG *n0, int num);
51 int bn_mul4x_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 51 int bn_mul4x_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
52 const BN_ULONG *np, const BN_ULONG *n0, int num); 52 const BN_ULONG *np, const BN_ULONG *n0, int num);
53 53
54 if (num < 4) 54 if (num < 4)
55 return 0; 55 return 0;
56 56
57 if ((num & 3) == 0) 57 if ((num & 3) == 0)
58 return bn_mul4x_mont_int(rp, ap, bp, np, n0, num); 58 return bn_mul4x_mont_int(rp, ap, bp, np, n0, num);
59 59
60 /* 60 /*
61 * There used to be [optional] call to bn_mul_mont_fpu64 here, 61 * There used to be [optional] call to bn_mul_mont_fpu64 here,
62 * but above subroutine is faster on contemporary processors. 62 * but above subroutine is faster on contemporary processors.
63 * Formulation means that there might be old processors where 63 * Formulation means that there might be old processors where
64 * FPU code path would be faster, POWER6 perhaps, but there was 64 * FPU code path would be faster, POWER6 perhaps, but there was
65 * no opportunity to figure it out... 65 * no opportunity to figure it out...
66 */ 66 */
67 67
68 return bn_mul_mont_int(rp, ap, bp, np, n0, num); 68 return bn_mul_mont_int(rp, ap, bp, np, n0, num);
69} 69}
70#endif 70#endif
71 71
72#ifdef DONT_USE_SHA256_FROM_LIBC 72#ifdef DONT_USE_SHA256_FROM_LIBC
73void sha256_block_p8(void *ctx, const void *inp, size_t len); 73void sha256_block_p8(void *ctx, const void *inp, size_t len);
74void sha256_block_ppc(void *ctx, const void *inp, size_t len); 74void sha256_block_ppc(void *ctx, const void *inp, size_t len);
75void sha256_block_data_order(void *ctx, const void *inp, size_t len); 75void sha256_block_data_order(void *ctx, const void *inp, size_t len);
76void sha256_block_data_order(void *ctx, const void *inp, size_t len) 76void sha256_block_data_order(void *ctx, const void *inp, size_t len)
77{ 77{
78 OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha256_block_p8(ctx, inp, len) : 78 OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha256_block_p8(ctx, inp, len) :
79 sha256_block_ppc(ctx, inp, len); 79 sha256_block_ppc(ctx, inp, len);
80} 80}
81#endif 81#endif
82 82
83#ifdef DONT_USE_SHA512_FROM_LIBC 83#ifdef DONT_USE_SHA512_FROM_LIBC
84void sha512_block_p8(void *ctx, const void *inp, size_t len); 84void sha512_block_p8(void *ctx, const void *inp, size_t len);
85void sha512_block_ppc(void *ctx, const void *inp, size_t len); 85void sha512_block_ppc(void *ctx, const void *inp, size_t len);
86void sha512_block_data_order(void *ctx, const void *inp, size_t len); 86void sha512_block_data_order(void *ctx, const void *inp, size_t len);
87void sha512_block_data_order(void *ctx, const void *inp, size_t len) 87void sha512_block_data_order(void *ctx, const void *inp, size_t len)
88{ 88{
89 OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha512_block_p8(ctx, inp, len) : 89 OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha512_block_p8(ctx, inp, len) :
90 sha512_block_ppc(ctx, inp, len); 90 sha512_block_ppc(ctx, inp, len);
91} 91}
92#endif 92#endif
93 93
94#ifndef OPENSSL_NO_CHACHA 94#ifndef OPENSSL_NO_CHACHA
95void ChaCha20_ctr32_int(unsigned char *out, const unsigned char *inp, 95void ChaCha20_ctr32_int(unsigned char *out, const unsigned char *inp,
96 size_t len, const unsigned int key[8], 96 size_t len, const unsigned int key[8],
97 const unsigned int counter[4]); 97 const unsigned int counter[4]);
98void ChaCha20_ctr32_vmx(unsigned char *out, const unsigned char *inp, 98void ChaCha20_ctr32_vmx(unsigned char *out, const unsigned char *inp,
99 size_t len, const unsigned int key[8], 99 size_t len, const unsigned int key[8],
100 const unsigned int counter[4]); 100 const unsigned int counter[4]);
101void ChaCha20_ctr32_vsx(unsigned char *out, const unsigned char *inp, 101void ChaCha20_ctr32_vsx(unsigned char *out, const unsigned char *inp,
102 size_t len, const unsigned int key[8], 102 size_t len, const unsigned int key[8],
103 const unsigned int counter[4]); 103 const unsigned int counter[4]);
104void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp, 104void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp,
105 size_t len, const unsigned int key[8], 105 size_t len, const unsigned int key[8],
106 const unsigned int counter[4]) 106 const unsigned int counter[4])
107{ 107{
108 OPENSSL_ppccap_P & PPC_CRYPTO207 108 OPENSSL_ppccap_P & PPC_CRYPTO207
109 ? ChaCha20_ctr32_vsx(out, inp, len, key, counter) 109 ? ChaCha20_ctr32_vsx(out, inp, len, key, counter)
110 : OPENSSL_ppccap_P & PPC_ALTIVEC 110 : OPENSSL_ppccap_P & PPC_ALTIVEC
111 ? ChaCha20_ctr32_vmx(out, inp, len, key, counter) 111 ? ChaCha20_ctr32_vmx(out, inp, len, key, counter)
112 : ChaCha20_ctr32_int(out, inp, len, key, counter); 112 : ChaCha20_ctr32_int(out, inp, len, key, counter);
113} 113}
114#endif 114#endif
115 115
116#ifndef OPENSSL_NO_POLY1305 116#ifndef OPENSSL_NO_POLY1305
117void poly1305_init_int(void *ctx, const unsigned char key[16]); 117void poly1305_init_int(void *ctx, const unsigned char key[16]);
118void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len, 118void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len,
119 unsigned int padbit); 119 unsigned int padbit);
120void poly1305_emit(void *ctx, unsigned char mac[16], 120void poly1305_emit(void *ctx, unsigned char mac[16],
121 const unsigned int nonce[4]); 121 const unsigned int nonce[4]);
122void poly1305_init_fpu(void *ctx, const unsigned char key[16]); 122void poly1305_init_fpu(void *ctx, const unsigned char key[16]);
123void poly1305_blocks_fpu(void *ctx, const unsigned char *inp, size_t len, 123void poly1305_blocks_fpu(void *ctx, const unsigned char *inp, size_t len,
124 unsigned int padbit); 124 unsigned int padbit);
125void poly1305_emit_fpu(void *ctx, unsigned char mac[16], 125void poly1305_emit_fpu(void *ctx, unsigned char mac[16],
126 const unsigned int nonce[4]); 126 const unsigned int nonce[4]);
127int poly1305_init(void *ctx, const unsigned char key[16], void *func[2]); 127int poly1305_init(void *ctx, const unsigned char key[16], void *func[2]);
128int poly1305_init(void *ctx, const unsigned char key[16], void *func[2]) 128int poly1305_init(void *ctx, const unsigned char key[16], void *func[2])
129{ 129{
130 if (sizeof(size_t) == 4 && (OPENSSL_ppccap_P & PPC_FPU)) { 130 if (sizeof(size_t) == 4 && (OPENSSL_ppccap_P & PPC_FPU)) {
131 poly1305_init_fpu(ctx, key); 131 poly1305_init_fpu(ctx, key);
132 func[0] = (void*)(uintptr_t)poly1305_blocks_fpu; 132 func[0] = (void*)(uintptr_t)poly1305_blocks_fpu;
133 func[1] = (void*)(uintptr_t)poly1305_emit_fpu; 133 func[1] = (void*)(uintptr_t)poly1305_emit_fpu;
134 } else { 134 } else {
135 poly1305_init_int(ctx, key); 135 poly1305_init_int(ctx, key);
136 func[0] = (void*)(uintptr_t)poly1305_blocks; 136 func[0] = (void*)(uintptr_t)poly1305_blocks;
137 func[1] = (void*)(uintptr_t)poly1305_emit; 137 func[1] = (void*)(uintptr_t)poly1305_emit;
138 } 138 }
139 return 1; 139 return 1;
140} 140}
141#endif 141#endif
142 142
143#ifdef ECP_NISTZ256_ASM 143#ifdef ECP_NISTZ256_ASM
144void ecp_nistz256_mul_mont(unsigned long res[4], const unsigned long a[4], 144void ecp_nistz256_mul_mont(unsigned long res[4], const unsigned long a[4],
145 const unsigned long b[4]); 145 const unsigned long b[4]);
146 146
147void ecp_nistz256_to_mont(unsigned long res[4], const unsigned long in[4]); 147void ecp_nistz256_to_mont(unsigned long res[4], const unsigned long in[4]);
148void ecp_nistz256_to_mont(unsigned long res[4], const unsigned long in[4]) 148void ecp_nistz256_to_mont(unsigned long res[4], const unsigned long in[4])
149{ 149{
150 static const unsigned long RR[] = { 0x0000000000000003U, 150 static const unsigned long RR[] = { 0x0000000000000003U,
151 0xfffffffbffffffffU, 151 0xfffffffbffffffffU,
152 0xfffffffffffffffeU, 152 0xfffffffffffffffeU,
153 0x00000004fffffffdU }; 153 0x00000004fffffffdU };
154 154
155 ecp_nistz256_mul_mont(res, in, RR); 155 ecp_nistz256_mul_mont(res, in, RR);
156} 156}
157 157
158void ecp_nistz256_from_mont(unsigned long res[4], const unsigned long in[4]); 158void ecp_nistz256_from_mont(unsigned long res[4], const unsigned long in[4]);
159void ecp_nistz256_from_mont(unsigned long res[4], const unsigned long in[4]) 159void ecp_nistz256_from_mont(unsigned long res[4], const unsigned long in[4])
160{ 160{
161 static const unsigned long one[] = { 1, 0, 0, 0 }; 161 static const unsigned long one[] = { 1, 0, 0, 0 };
162 162
163 ecp_nistz256_mul_mont(res, in, one); 163 ecp_nistz256_mul_mont(res, in, one);
164} 164}
165#endif 165#endif
166 166
167static sigjmp_buf ill_jmp; 167static sigjmp_buf ill_jmp;
168static void ill_handler(int sig) 168static void ill_handler(int sig)
169{ 169{
170 siglongjmp(ill_jmp, sig); 170 siglongjmp(ill_jmp, sig);
171} 171}
172 172
173void OPENSSL_fpu_probe(void); 173void OPENSSL_fpu_probe(void);
174void OPENSSL_ppc64_probe(void); 174void OPENSSL_ppc64_probe(void);
175void OPENSSL_altivec_probe(void); 175void OPENSSL_altivec_probe(void);
176void OPENSSL_crypto207_probe(void); 176void OPENSSL_crypto207_probe(void);
177void OPENSSL_madd300_probe(void); 177void OPENSSL_madd300_probe(void);
178 178
179long OPENSSL_rdtsc_mftb(void); 179long OPENSSL_rdtsc_mftb(void);
180long OPENSSL_rdtsc_mfspr268(void); 180long OPENSSL_rdtsc_mfspr268(void);
181 181
182uint32_t OPENSSL_rdtsc(void) 182uint32_t OPENSSL_rdtsc(void)
183{ 183{
184 if (OPENSSL_ppccap_P & PPC_MFTB) 184 if (OPENSSL_ppccap_P & PPC_MFTB)
185 return OPENSSL_rdtsc_mftb(); 185 return OPENSSL_rdtsc_mftb();
186 else if (OPENSSL_ppccap_P & PPC_MFSPR268) 186 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
187 return OPENSSL_rdtsc_mfspr268(); 187 return OPENSSL_rdtsc_mfspr268();
188 else 188 else
189 return 0; 189 return 0;
190} 190}
191 191
192size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t); 192size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
193size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t); 193size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
194 194
195size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt) 195size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
196{ 196{
197 if (OPENSSL_ppccap_P & PPC_MFTB) 197 if (OPENSSL_ppccap_P & PPC_MFTB)
198 return OPENSSL_instrument_bus_mftb(out, cnt); 198 return OPENSSL_instrument_bus_mftb(out, cnt);
199 else if (OPENSSL_ppccap_P & PPC_MFSPR268) 199 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
200 return OPENSSL_instrument_bus_mfspr268(out, cnt); 200 return OPENSSL_instrument_bus_mfspr268(out, cnt);
201 else 201 else
202 return 0; 202 return 0;
203} 203}
204 204
205size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t); 205size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
206size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t); 206size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
207 207
208size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max) 208size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
209{ 209{
210 if (OPENSSL_ppccap_P & PPC_MFTB) 210 if (OPENSSL_ppccap_P & PPC_MFTB)
211 return OPENSSL_instrument_bus2_mftb(out, cnt, max); 211 return OPENSSL_instrument_bus2_mftb(out, cnt, max);
212 else if (OPENSSL_ppccap_P & PPC_MFSPR268) 212 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
213 return OPENSSL_instrument_bus2_mfspr268(out, cnt, max); 213 return OPENSSL_instrument_bus2_mfspr268(out, cnt, max);
214 else 214 else
215 return 0; 215 return 0;
216} 216}
217 217
218#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) 218#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
219# if __GLIBC_PREREQ(2, 16) 219# if __GLIBC_PREREQ(2, 16)
220# include <sys/auxv.h> 220# include <sys/auxv.h>
221# define OSSL_IMPLEMENT_GETAUXVAL 221# define OSSL_IMPLEMENT_GETAUXVAL
222# endif 222# endif
223#endif 223#endif
224 224
225/* I wish <sys/auxv.h> was universally available */ 225/* I wish <sys/auxv.h> was universally available */
226#define HWCAP 16 /* AT_HWCAP */ 226#define HWCAP 16 /* AT_HWCAP */
227#define HWCAP_PPC64 (1U << 30) 227#define HWCAP_PPC64 (1U << 30)
228#define HWCAP_ALTIVEC (1U << 28) 228#define HWCAP_ALTIVEC (1U << 28)
229#define HWCAP_FPU (1U << 27) 229#define HWCAP_FPU (1U << 27)
230#define HWCAP_POWER6_EXT (1U << 9) 230#define HWCAP_POWER6_EXT (1U << 9)
231#define HWCAP_VSX (1U << 7) 231#define HWCAP_VSX (1U << 7)
232 232
233#define HWCAP2 26 /* AT_HWCAP2 */ 233#define HWCAP2 26 /* AT_HWCAP2 */
234#define HWCAP_VEC_CRYPTO (1U << 25) 234#define HWCAP_VEC_CRYPTO (1U << 25)
235#define HWCAP_ARCH_3_00 (1U << 23) 235#define HWCAP_ARCH_3_00 (1U << 23)
236 236
237# if defined(__GNUC__) && __GNUC__>=2 237# if defined(__GNUC__) && __GNUC__>=2
238__attribute__ ((constructor)) 238__attribute__ ((constructor))
239# endif 239# endif
240void OPENSSL_cpuid_setup(void) 240void OPENSSL_cpuid_setup(void)
241{ 241{
242 char *e; 242 char *e;
243 struct sigaction ill_oact, ill_act; 243 struct sigaction ill_oact, ill_act;
244 sigset_t oset; 244 sigset_t oset;
245 static int trigger = 0; 245 static int trigger = 0;
246 246
247 if (trigger) 247 if (trigger)
248 return; 248 return;
249 trigger = 1; 249 trigger = 1;
250 250
251 if ((e = getenv("OPENSSL_ppccap"))) { 251 if ((e = getenv("OPENSSL_ppccap"))) {
252 OPENSSL_ppccap_P = strtoul(e, NULL, 0); 252 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
253 return; 253 return;
254 } 254 }
255 255
256 OPENSSL_ppccap_P = 0; 256 OPENSSL_ppccap_P = 0;
257 257
258#if defined(_AIX) 258#if defined(_AIX)
259 OPENSSL_ppccap_P |= PPC_FPU; 259 OPENSSL_ppccap_P |= PPC_FPU;
260 260
261 if (sizeof(size_t) == 4) { 261 if (sizeof(size_t) == 4) {
262 struct utsname uts; 262 struct utsname uts;
263# if defined(_SC_AIX_KERNEL_BITMODE) 263# if defined(_SC_AIX_KERNEL_BITMODE)
264 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64) 264 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
265 return; 265 return;
266# endif 266# endif
267 if (uname(&uts) != 0 || atoi(uts.version) < 6) 267 if (uname(&uts) != 0 || atoi(uts.version) < 6)
268 return; 268 return;
269 } 269 }
270 270
271# if defined(__power_set) 271# if defined(__power_set)
272 /* 272 /*
273 * Value used in __power_set is a single-bit 1<<n one denoting 273 * Value used in __power_set is a single-bit 1<<n one denoting
274 * specific processor class. Incidentally 0xffffffff<<n can be 274 * specific processor class. Incidentally 0xffffffff<<n can be
275 * used to denote specific processor and its successors. 275 * used to denote specific processor and its successors.
276 */ 276 */
277 if (sizeof(size_t) == 4) { 277 if (sizeof(size_t) == 4) {
278 /* In 32-bit case PPC_FPU64 is always fastest [if option] */ 278 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
279 if (__power_set(0xffffffffU<<13)) /* POWER5 and later */ 279 if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
280 OPENSSL_ppccap_P |= PPC_FPU64; 280 OPENSSL_ppccap_P |= PPC_FPU64;
281 } else { 281 } else {
282 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */ 282 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
283 if (__power_set(0x1U<<14)) /* POWER6 */ 283 if (__power_set(0x1U<<14)) /* POWER6 */
284 OPENSSL_ppccap_P |= PPC_FPU64; 284 OPENSSL_ppccap_P |= PPC_FPU64;
285 } 285 }
286 286
287 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */ 287 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
288 OPENSSL_ppccap_P |= PPC_ALTIVEC; 288 OPENSSL_ppccap_P |= PPC_ALTIVEC;
289 289
290 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */ 290 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
291 OPENSSL_ppccap_P |= PPC_CRYPTO207; 291 OPENSSL_ppccap_P |= PPC_CRYPTO207;
292 292
293 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */ 293 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
294 OPENSSL_ppccap_P |= PPC_MADD300; 294 OPENSSL_ppccap_P |= PPC_MADD300;
295 295
296 return; 296 return;
297# endif 297# endif
298#endif 298#endif
299 299
300#if defined(__APPLE__) && defined(__MACH__) 300#if defined(__APPLE__) && defined(__MACH__)
301 OPENSSL_ppccap_P |= PPC_FPU; 301 OPENSSL_ppccap_P |= PPC_FPU;
302 302
303 { 303 {
304 int val; 304 int val;
305 size_t len = sizeof(val); 305 size_t len = sizeof(val);
306 306
307 if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) { 307 if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
308 if (val) 308 if (val)
309 OPENSSL_ppccap_P |= PPC_FPU64; 309 OPENSSL_ppccap_P |= PPC_FPU64;
310 } 310 }
311 311
312 len = sizeof(val); 312 len = sizeof(val);
313 if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) { 313 if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
314 if (val) 314 if (val)
315 OPENSSL_ppccap_P |= PPC_ALTIVEC; 315 OPENSSL_ppccap_P |= PPC_ALTIVEC;
316 } 316 }
317 317
318 return; 318 return;
319 } 319 }
320#endif 320#endif
321 321
322#ifdef OSSL_IMPLEMENT_GETAUXVAL 322#ifdef OSSL_IMPLEMENT_GETAUXVAL
323 { 323 {
324 unsigned long hwcap = getauxval(HWCAP); 324 unsigned long hwcap = getauxval(HWCAP);
325 unsigned long hwcap2 = getauxval(HWCAP2); 325 unsigned long hwcap2 = getauxval(HWCAP2);
326 326
327 if (hwcap & HWCAP_FPU) { 327 if (hwcap & HWCAP_FPU) {
328 OPENSSL_ppccap_P |= PPC_FPU; 328 OPENSSL_ppccap_P |= PPC_FPU;
329 329
330 if (sizeof(size_t) == 4) { 330 if (sizeof(size_t) == 4) {
331 /* In 32-bit case PPC_FPU64 is always fastest [if option] */ 331 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
332 if (hwcap & HWCAP_PPC64) 332 if (hwcap & HWCAP_PPC64)
333 OPENSSL_ppccap_P |= PPC_FPU64; 333 OPENSSL_ppccap_P |= PPC_FPU64;
334 } else { 334 } else {
335 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */ 335 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
336 if (hwcap & HWCAP_POWER6_EXT) 336 if (hwcap & HWCAP_POWER6_EXT)
337 OPENSSL_ppccap_P |= PPC_FPU64; 337 OPENSSL_ppccap_P |= PPC_FPU64;
338 } 338 }
339 } 339 }
340 340
341 if (hwcap & HWCAP_ALTIVEC) { 341 if (hwcap & HWCAP_ALTIVEC) {
342 OPENSSL_ppccap_P |= PPC_ALTIVEC; 342 OPENSSL_ppccap_P |= PPC_ALTIVEC;
343 343
344 if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO)) 344 if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
345 OPENSSL_ppccap_P |= PPC_CRYPTO207; 345 OPENSSL_ppccap_P |= PPC_CRYPTO207;
346 } 346 }
347 347
348 if (hwcap2 & HWCAP_ARCH_3_00) { 348 if (hwcap2 & HWCAP_ARCH_3_00) {
349 OPENSSL_ppccap_P |= PPC_MADD300; 349 OPENSSL_ppccap_P |= PPC_MADD300;
350 } 350 }
351 } 351 }
352#endif 352#endif
353 353
354 sigfillset(&all_masked); 354 sigfillset(&all_masked);
355 sigdelset(&all_masked, SIGILL); 355 sigdelset(&all_masked, SIGILL);
356 sigdelset(&all_masked, SIGTRAP); 356 sigdelset(&all_masked, SIGTRAP);
357#ifdef SIGEMT 357#ifdef SIGEMT
358 sigdelset(&all_masked, SIGEMT); 358 sigdelset(&all_masked, SIGEMT);
359#endif 359#endif
360 sigdelset(&all_masked, SIGFPE); 360 sigdelset(&all_masked, SIGFPE);
361 sigdelset(&all_masked, SIGBUS); 361 sigdelset(&all_masked, SIGBUS);
362 sigdelset(&all_masked, SIGSEGV); 362 sigdelset(&all_masked, SIGSEGV);
363 363
364 memset(&ill_act, 0, sizeof(ill_act)); 364 memset(&ill_act, 0, sizeof(ill_act));
365 ill_act.sa_handler = ill_handler; 365 ill_act.sa_handler = ill_handler;
366 ill_act.sa_mask = all_masked; 366 ill_act.sa_mask = all_masked;
367 367
368 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset); 368 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
369 sigaction(SIGILL, &ill_act, &ill_oact); 369 sigaction(SIGILL, &ill_act, &ill_oact);
370 370
371#ifndef OSSL_IMPLEMENT_GETAUXVAL 371#ifndef OSSL_IMPLEMENT_GETAUXVAL
372# ifdef __NetBSD__ 372# ifdef __NetBSD__
373 int error, val; 373 int error, val;
374 size_t len = sizeof(val); 374 size_t len = sizeof(val);
375 375
376 /* 376 /*
377 * If machdep.fpu_present == 0, FPU is absent and emulated by software. 377 * If machdep.fpu_present == 0, FPU is absent and emulated by
378 * Avoid using it for better performance. 378 * software. In that case, using FPU instructions hurts rather
 379 * than helps performance, and the software is unlikely to run in
 380 * constant time so it would expose us to timing side channel
 381 * attacks. So don't do it!
379 */ 382 */
380 error = sysctlbyname("machdep.fpu_present", &val, &len, NULL, 0); 383 error = sysctlbyname("machdep.fpu_present", &val, &len, NULL, 0);
381 if (error != 0 || (error == 0 && val != 0)) 384 if (error != 0 || (error == 0 && val != 0))
382# endif 385# endif
383 if (sigsetjmp(ill_jmp,1) == 0) { 386 if (sigsetjmp(ill_jmp,1) == 0) {
384 OPENSSL_fpu_probe(); 387 OPENSSL_fpu_probe();
385 OPENSSL_ppccap_P |= PPC_FPU; 388 OPENSSL_ppccap_P |= PPC_FPU;
386 389
387 if (sizeof(size_t) == 4) { 390 if (sizeof(size_t) == 4) {
388# ifdef __linux 391# ifdef __linux
389 struct utsname uts; 392 struct utsname uts;
390 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0) 393 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
391# endif 394# endif
392 if (sigsetjmp(ill_jmp, 1) == 0) { 395 if (sigsetjmp(ill_jmp, 1) == 0) {
393 OPENSSL_ppc64_probe(); 396 OPENSSL_ppc64_probe();
394 OPENSSL_ppccap_P |= PPC_FPU64; 397 OPENSSL_ppccap_P |= PPC_FPU64;
395 } 398 }
396 } else { 399 } else {
397 /* 400 /*
398 * Wanted code detecting POWER6 CPU and setting PPC_FPU64 401 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
399 */ 402 */
400 } 403 }
401 } 404 }
402 405
403 if (sigsetjmp(ill_jmp, 1) == 0) { 406 if (sigsetjmp(ill_jmp, 1) == 0) {
404 OPENSSL_altivec_probe(); 407 OPENSSL_altivec_probe();
405 OPENSSL_ppccap_P |= PPC_ALTIVEC; 408 OPENSSL_ppccap_P |= PPC_ALTIVEC;
406 if (sigsetjmp(ill_jmp, 1) == 0) { 409 if (sigsetjmp(ill_jmp, 1) == 0) {
407 OPENSSL_crypto207_probe(); 410 OPENSSL_crypto207_probe();
408 OPENSSL_ppccap_P |= PPC_CRYPTO207; 411 OPENSSL_ppccap_P |= PPC_CRYPTO207;
409 } 412 }
410 } 413 }
411 414
412 if (sigsetjmp(ill_jmp, 1) == 0) { 415 if (sigsetjmp(ill_jmp, 1) == 0) {
413 OPENSSL_madd300_probe(); 416 OPENSSL_madd300_probe();
414 OPENSSL_ppccap_P |= PPC_MADD300; 417 OPENSSL_ppccap_P |= PPC_MADD300;
415 } 418 }
416#endif 419#endif
417 420
418 if (sigsetjmp(ill_jmp, 1) == 0) { 421 if (sigsetjmp(ill_jmp, 1) == 0) {
419 OPENSSL_rdtsc_mftb(); 422 OPENSSL_rdtsc_mftb();
420 OPENSSL_ppccap_P |= PPC_MFTB; 423 OPENSSL_ppccap_P |= PPC_MFTB;
421 } else if (sigsetjmp(ill_jmp, 1) == 0) { 424 } else if (sigsetjmp(ill_jmp, 1) == 0) {
422 OPENSSL_rdtsc_mfspr268(); 425 OPENSSL_rdtsc_mfspr268();
423 OPENSSL_ppccap_P |= PPC_MFSPR268; 426 OPENSSL_ppccap_P |= PPC_MFSPR268;
424 } 427 }
425 428
426 sigaction(SIGILL, &ill_oact, NULL); 429 sigaction(SIGILL, &ill_oact, NULL);
427 sigprocmask(SIG_SETMASK, &oset, NULL); 430 sigprocmask(SIG_SETMASK, &oset, NULL);
428} 431}