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