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