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