Add IDEA ciphers to default provider
[openssl.git] / crypto / sparcv9cap.c
1 /*
2  * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <sys/time.h>
16 #include <unistd.h>
17 #include <openssl/bn.h>
18 #include "internal/cryptlib.h"
19
20 #include "sparc_arch.h"
21
22 #if defined(__GNUC__) && defined(__linux)
23 __attribute__ ((visibility("hidden")))
24 #endif
25 unsigned int OPENSSL_sparcv9cap_P[2] = { SPARCV9_TICK_PRIVILEGED, 0 };
26
27 /*
28  * TODO(3.0): Temporarily disabled some assembler that hasn't been brought into
29  * the FIPS module yet.
30  */
31 #ifndef FIPS_MODE
32 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
33                 const BN_ULONG *np, const BN_ULONG *n0, int num)
34 {
35     int bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
36                          const BN_ULONG *np, const BN_ULONG *n0, int num);
37     int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
38                         const BN_ULONG *np, const BN_ULONG *n0, int num);
39     int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
40                         const BN_ULONG *np, const BN_ULONG *n0, int num);
41
42     if (!(num & 1) && num >= 6) {
43         if ((num & 15) == 0 && num <= 64 &&
44             (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
45             (CFR_MONTMUL | CFR_MONTSQR)) {
46             typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
47                                           const BN_ULONG *bp,
48                                           const BN_ULONG *np,
49                                           const BN_ULONG *n0);
50             int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap,
51                                  const BN_ULONG *bp, const BN_ULONG *np,
52                                  const BN_ULONG *n0);
53             int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
54                                   const BN_ULONG *bp, const BN_ULONG *np,
55                                   const BN_ULONG *n0);
56             int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
57                                   const BN_ULONG *bp, const BN_ULONG *np,
58                                   const BN_ULONG *n0);
59             int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
60                                   const BN_ULONG *bp, const BN_ULONG *np,
61                                   const BN_ULONG *n0);
62             static const bn_mul_mont_f funcs[4] = {
63                 bn_mul_mont_t4_8, bn_mul_mont_t4_16,
64                 bn_mul_mont_t4_24, bn_mul_mont_t4_32
65             };
66             bn_mul_mont_f worker = funcs[num / 16 - 1];
67
68             if ((*worker) (rp, ap, bp, np, n0))
69                 return 1;
70             /* retry once and fall back */
71             if ((*worker) (rp, ap, bp, np, n0))
72                 return 1;
73             return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
74         }
75         if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3))
76             return bn_mul_mont_vis3(rp, ap, bp, np, n0, num);
77         else if (num >= 8 &&
78                  /*
79                   * bn_mul_mont_fpu doesn't use FMADD, we just use the
80                   * flag to detect when FPU path is preferable in cases
81                   * when current heuristics is unreliable. [it works
82                   * out because FMADD-capable processors where FPU
83                   * code path is undesirable are also VIS3-capable and
84                   * VIS3 code path takes precedence.]
85                   */
86                  ( (OPENSSL_sparcv9cap_P[0] & SPARCV9_FMADD) ||
87                    (OPENSSL_sparcv9cap_P[0] &
88                     (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) ==
89                    (SPARCV9_PREFER_FPU | SPARCV9_VIS1) ))
90             return bn_mul_mont_fpu(rp, ap, bp, np, n0, num);
91     }
92     return bn_mul_mont_int(rp, ap, bp, np, n0, num);
93 }
94 #endif /* FIPS_MODE */
95
96 unsigned long _sparcv9_rdtick(void);
97 void _sparcv9_vis1_probe(void);
98 unsigned long _sparcv9_vis1_instrument(void);
99 void _sparcv9_vis2_probe(void);
100 void _sparcv9_fmadd_probe(void);
101 unsigned long _sparcv9_rdcfr(void);
102 void _sparcv9_vis3_probe(void);
103 void _sparcv9_fjaesx_probe(void);
104 unsigned long _sparcv9_random(void);
105 size_t _sparcv9_vis1_instrument_bus(unsigned int *, size_t);
106 size_t _sparcv9_vis1_instrument_bus2(unsigned int *, size_t, size_t);
107
108 uint32_t OPENSSL_rdtsc(void)
109 {
110     if (OPENSSL_sparcv9cap_P[0] & SPARCV9_TICK_PRIVILEGED)
111 #if defined(__sun) && defined(__SVR4)
112         return gethrtime();
113 #else
114         return 0;
115 #endif
116     else
117         return _sparcv9_rdtick();
118 }
119
120 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
121 {
122     if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
123         SPARCV9_BLK)
124         return _sparcv9_vis1_instrument_bus(out, cnt);
125     else
126         return 0;
127 }
128
129 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
130 {
131     if ((OPENSSL_sparcv9cap_P[0] & (SPARCV9_TICK_PRIVILEGED | SPARCV9_BLK)) ==
132         SPARCV9_BLK)
133         return _sparcv9_vis1_instrument_bus2(out, cnt, max);
134     else
135         return 0;
136 }
137
138 static sigjmp_buf common_jmp;
139 static void common_handler(int sig)
140 {
141     siglongjmp(common_jmp, sig);
142 }
143
144 #if defined(__sun) && defined(__SVR4)
145 # if defined(__GNUC__) && __GNUC__>=2
146 extern unsigned int getisax(unsigned int vec[], unsigned int sz) __attribute__ ((weak));
147 # elif defined(__SUNPRO_C)
148 #pragma weak getisax
149 extern unsigned int getisax(unsigned int vec[], unsigned int sz);
150 # else
151 static unsigned int (*getisax) (unsigned int vec[], unsigned int sz) = NULL;
152 # endif
153 #endif
154
155 void OPENSSL_cpuid_setup(void)
156 {
157     char *e;
158     struct sigaction common_act, ill_oact, bus_oact;
159     sigset_t all_masked, oset;
160     static int trigger = 0;
161
162     if (trigger)
163         return;
164     trigger = 1;
165
166     if ((e = getenv("OPENSSL_sparcv9cap"))) {
167         OPENSSL_sparcv9cap_P[0] = strtoul(e, NULL, 0);
168         if ((e = strchr(e, ':')))
169             OPENSSL_sparcv9cap_P[1] = strtoul(e + 1, NULL, 0);
170         return;
171     }
172
173 #if defined(__sun) && defined(__SVR4)
174     if (getisax != NULL) {
175         unsigned int vec[2] = { 0, 0 };
176
177         if (getisax (vec,2)) {
178             if (vec[0]&0x00020) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1;
179             if (vec[0]&0x00040) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
180             if (vec[0]&0x00080) OPENSSL_sparcv9cap_P[0] |= SPARCV9_BLK;
181             if (vec[0]&0x00100) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
182             if (vec[0]&0x00400) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
183             if (vec[0]&0x01000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJHPCACE;
184             if (vec[0]&0x02000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJDESX;
185             if (vec[0]&0x08000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_IMA;
186             if (vec[0]&0x10000) OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
187             if (vec[1]&0x00008) OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS4;
188
189             /* reconstruct %cfr copy */
190             OPENSSL_sparcv9cap_P[1] = (vec[0]>>17)&0x3ff;
191             OPENSSL_sparcv9cap_P[1] |= (OPENSSL_sparcv9cap_P[1]&CFR_MONTMUL)<<1;
192             if (vec[0]&0x20000000) OPENSSL_sparcv9cap_P[1] |= CFR_CRC32C;
193             if (vec[1]&0x00000020) OPENSSL_sparcv9cap_P[1] |= CFR_XMPMUL;
194             if (vec[1]&0x00000040)
195                 OPENSSL_sparcv9cap_P[1] |= CFR_XMONTMUL|CFR_XMONTSQR;
196
197             /* Some heuristics */
198             /* all known VIS2-capable CPUs have unprivileged tick counter */
199             if (OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS2)
200                 OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
201
202             OPENSSL_sparcv9cap_P[0] |= SPARCV9_PREFER_FPU;
203
204             /* detect UltraSPARC-Tx, see sparccpud.S for details... */
205             if ((OPENSSL_sparcv9cap_P[0]&SPARCV9_VIS1) &&
206                 _sparcv9_vis1_instrument() >= 12)
207                 OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
208         }
209
210         if (sizeof(size_t) == 8)
211             OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
212
213         return;
214     }
215 #endif
216
217     /* Initial value, fits UltraSPARC-I&II... */
218     OPENSSL_sparcv9cap_P[0] = SPARCV9_PREFER_FPU | SPARCV9_TICK_PRIVILEGED;
219
220     sigfillset(&all_masked);
221     sigdelset(&all_masked, SIGILL);
222     sigdelset(&all_masked, SIGTRAP);
223 # ifdef SIGEMT
224     sigdelset(&all_masked, SIGEMT);
225 # endif
226     sigdelset(&all_masked, SIGFPE);
227     sigdelset(&all_masked, SIGBUS);
228     sigdelset(&all_masked, SIGSEGV);
229     sigprocmask(SIG_SETMASK, &all_masked, &oset);
230
231     memset(&common_act, 0, sizeof(common_act));
232     common_act.sa_handler = common_handler;
233     common_act.sa_mask = all_masked;
234
235     sigaction(SIGILL, &common_act, &ill_oact);
236     sigaction(SIGBUS, &common_act, &bus_oact); /* T1 fails 16-bit ldda [on
237                                                 * Linux] */
238
239     if (sigsetjmp(common_jmp, 1) == 0) {
240         _sparcv9_rdtick();
241         OPENSSL_sparcv9cap_P[0] &= ~SPARCV9_TICK_PRIVILEGED;
242     }
243
244     if (sigsetjmp(common_jmp, 1) == 0) {
245         _sparcv9_vis1_probe();
246         OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS1 | SPARCV9_BLK;
247         /* detect UltraSPARC-Tx, see sparccpud.S for details... */
248         if (_sparcv9_vis1_instrument() >= 12)
249             OPENSSL_sparcv9cap_P[0] &= ~(SPARCV9_VIS1 | SPARCV9_PREFER_FPU);
250         else {
251             _sparcv9_vis2_probe();
252             OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS2;
253         }
254     }
255
256     if (sigsetjmp(common_jmp, 1) == 0) {
257         _sparcv9_fmadd_probe();
258         OPENSSL_sparcv9cap_P[0] |= SPARCV9_FMADD;
259     }
260
261     /*
262      * VIS3 flag is tested independently from VIS1, unlike VIS2 that is,
263      * because VIS3 defines even integer instructions.
264      */
265     if (sigsetjmp(common_jmp, 1) == 0) {
266         _sparcv9_vis3_probe();
267         OPENSSL_sparcv9cap_P[0] |= SPARCV9_VIS3;
268     }
269
270     if (sigsetjmp(common_jmp, 1) == 0) {
271         _sparcv9_fjaesx_probe();
272         OPENSSL_sparcv9cap_P[0] |= SPARCV9_FJAESX;
273     }
274
275     /*
276      * In wait for better solution _sparcv9_rdcfr is masked by
277      * VIS3 flag, because it goes to uninterruptable endless
278      * loop on UltraSPARC II running Solaris. Things might be
279      * different on Linux...
280      */
281     if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) &&
282         sigsetjmp(common_jmp, 1) == 0) {
283         OPENSSL_sparcv9cap_P[1] = (unsigned int)_sparcv9_rdcfr();
284     }
285
286     sigaction(SIGBUS, &bus_oact, NULL);
287     sigaction(SIGILL, &ill_oact, NULL);
288
289     sigprocmask(SIG_SETMASK, &oset, NULL);
290
291     if (sizeof(size_t) == 8)
292         OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
293 # ifdef __linux
294     else {
295         int ret = syscall(340);
296
297         if (ret >= 0 && ret & 1)
298             OPENSSL_sparcv9cap_P[0] |= SPARCV9_64BIT_STACK;
299     }
300 # endif
301 }