VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.1j/crypto/ppccap.c@ 90248

最後變更 在這個檔案從90248是 87984,由 vboxsync 提交於 4 年 前

openssl-1.1.1j: Applied and adjusted our OpenSSL changes to 1.1.1j. bugref:9963

檔案大小: 12.9 KB
 
1/*
2 * Copyright 2009-2021 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#include <internal/cryptlib.h>
32#include <crypto/chacha.h>
33#include "bn/bn_local.h"
34
35#include "ppc_arch.h"
36
37unsigned int OPENSSL_ppccap_P = 0;
38
39static sigset_t all_masked;
40
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);
70void sha256_block_data_order(void *ctx, const void *inp, size_t len);
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);
79void sha512_block_data_order(void *ctx, const void *inp, size_t len);
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]);
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]);
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{
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);
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]);
119int poly1305_init(void *ctx, const unsigned char key[16], void *func[2]);
120int poly1305_init(void *ctx, const unsigned char key[16], void *func[2])
121{
122 if (sizeof(size_t) == 4 && (OPENSSL_ppccap_P & PPC_FPU)) {
123 poly1305_init_fpu(ctx, key);
124 func[0] = (void*)(uintptr_t)poly1305_blocks_fpu;
125 func[1] = (void*)(uintptr_t)poly1305_emit_fpu;
126 } else {
127 poly1305_init_int(ctx, key);
128 func[0] = (void*)(uintptr_t)poly1305_blocks;
129 func[1] = (void*)(uintptr_t)poly1305_emit;
130 }
131 return 1;
132}
133#endif
134
135#ifdef ECP_NISTZ256_ASM
136void ecp_nistz256_mul_mont(unsigned long res[4], const unsigned long a[4],
137 const unsigned long b[4]);
138
139void ecp_nistz256_to_mont(unsigned long res[4], const unsigned long in[4]);
140void ecp_nistz256_to_mont(unsigned long res[4], const unsigned long in[4])
141{
142 static const unsigned long RR[] = { 0x0000000000000003U,
143 0xfffffffbffffffffU,
144 0xfffffffffffffffeU,
145 0x00000004fffffffdU };
146
147 ecp_nistz256_mul_mont(res, in, RR);
148}
149
150void ecp_nistz256_from_mont(unsigned long res[4], const unsigned long in[4]);
151void ecp_nistz256_from_mont(unsigned long res[4], const unsigned long in[4])
152{
153 static const unsigned long one[] = { 1, 0, 0, 0 };
154
155 ecp_nistz256_mul_mont(res, in, one);
156}
157#endif
158
159static sigjmp_buf ill_jmp;
160static void ill_handler(int sig)
161{
162 siglongjmp(ill_jmp, sig);
163}
164
165void OPENSSL_fpu_probe(void);
166void OPENSSL_ppc64_probe(void);
167void OPENSSL_altivec_probe(void);
168void OPENSSL_crypto207_probe(void);
169void OPENSSL_madd300_probe(void);
170
171long OPENSSL_rdtsc_mftb(void);
172long OPENSSL_rdtsc_mfspr268(void);
173
174uint32_t OPENSSL_rdtsc(void)
175{
176 if (OPENSSL_ppccap_P & PPC_MFTB)
177 return OPENSSL_rdtsc_mftb();
178 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
179 return OPENSSL_rdtsc_mfspr268();
180 else
181 return 0;
182}
183
184size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
185size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
186
187size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
188{
189 if (OPENSSL_ppccap_P & PPC_MFTB)
190 return OPENSSL_instrument_bus_mftb(out, cnt);
191 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
192 return OPENSSL_instrument_bus_mfspr268(out, cnt);
193 else
194 return 0;
195}
196
197size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
198size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
199
200size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
201{
202 if (OPENSSL_ppccap_P & PPC_MFTB)
203 return OPENSSL_instrument_bus2_mftb(out, cnt, max);
204 else if (OPENSSL_ppccap_P & PPC_MFSPR268)
205 return OPENSSL_instrument_bus2_mfspr268(out, cnt, max);
206 else
207 return 0;
208}
209
210#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
211# if __GLIBC_PREREQ(2, 16)
212# include <sys/auxv.h>
213# define OSSL_IMPLEMENT_GETAUXVAL
214# endif
215#endif
216
217#if defined(__FreeBSD__)
218# include <sys/param.h>
219# if __FreeBSD_version >= 1200000
220# include <sys/auxv.h>
221# define OSSL_IMPLEMENT_GETAUXVAL
222
223static unsigned long getauxval(unsigned long key)
224{
225 unsigned long val = 0ul;
226
227 if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
228 return 0ul;
229
230 return val;
231}
232# endif
233#endif
234
235/* I wish <sys/auxv.h> was universally available */
236#define HWCAP 16 /* AT_HWCAP */
237#define HWCAP_PPC64 (1U << 30)
238#define HWCAP_ALTIVEC (1U << 28)
239#define HWCAP_FPU (1U << 27)
240#define HWCAP_POWER6_EXT (1U << 9)
241#define HWCAP_VSX (1U << 7)
242
243#define HWCAP2 26 /* AT_HWCAP2 */
244#define HWCAP_VEC_CRYPTO (1U << 25)
245#define HWCAP_ARCH_3_00 (1U << 23)
246
247# if defined(__GNUC__) && __GNUC__>=2
248__attribute__ ((constructor))
249# endif
250void OPENSSL_cpuid_setup(void)
251{
252 char *e;
253 struct sigaction ill_oact, ill_act;
254 sigset_t oset;
255 static int trigger = 0;
256
257 if (trigger)
258 return;
259 trigger = 1;
260
261 if ((e = getenv("OPENSSL_ppccap"))) {
262 OPENSSL_ppccap_P = strtoul(e, NULL, 0);
263 return;
264 }
265
266 OPENSSL_ppccap_P = 0;
267
268#if defined(_AIX)
269 OPENSSL_ppccap_P |= PPC_FPU;
270
271 if (sizeof(size_t) == 4) {
272 struct utsname uts;
273# if defined(_SC_AIX_KERNEL_BITMODE)
274 if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
275 return;
276# endif
277 if (uname(&uts) != 0 || atoi(uts.version) < 6)
278 return;
279 }
280
281# if defined(__power_set)
282 /*
283 * Value used in __power_set is a single-bit 1<<n one denoting
284 * specific processor class. Incidentally 0xffffffff<<n can be
285 * used to denote specific processor and its successors.
286 */
287 if (sizeof(size_t) == 4) {
288 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
289 if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
290 OPENSSL_ppccap_P |= PPC_FPU64;
291 } else {
292 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
293 if (__power_set(0x1U<<14)) /* POWER6 */
294 OPENSSL_ppccap_P |= PPC_FPU64;
295 }
296
297 if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
298 OPENSSL_ppccap_P |= PPC_ALTIVEC;
299
300 if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
301 OPENSSL_ppccap_P |= PPC_CRYPTO207;
302
303 if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
304 OPENSSL_ppccap_P |= PPC_MADD300;
305
306 return;
307# endif
308#endif
309
310#if defined(__APPLE__) && defined(__MACH__)
311 OPENSSL_ppccap_P |= PPC_FPU;
312
313 {
314 int val;
315 size_t len = sizeof(val);
316
317 if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
318 if (val)
319 OPENSSL_ppccap_P |= PPC_FPU64;
320 }
321
322 len = sizeof(val);
323 if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
324 if (val)
325 OPENSSL_ppccap_P |= PPC_ALTIVEC;
326 }
327
328 return;
329 }
330#endif
331
332#ifdef OSSL_IMPLEMENT_GETAUXVAL
333 {
334 unsigned long hwcap = getauxval(HWCAP);
335 unsigned long hwcap2 = getauxval(HWCAP2);
336
337 if (hwcap & HWCAP_FPU) {
338 OPENSSL_ppccap_P |= PPC_FPU;
339
340 if (sizeof(size_t) == 4) {
341 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
342 if (hwcap & HWCAP_PPC64)
343 OPENSSL_ppccap_P |= PPC_FPU64;
344 } else {
345 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
346 if (hwcap & HWCAP_POWER6_EXT)
347 OPENSSL_ppccap_P |= PPC_FPU64;
348 }
349 }
350
351 if (hwcap & HWCAP_ALTIVEC) {
352 OPENSSL_ppccap_P |= PPC_ALTIVEC;
353
354 if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
355 OPENSSL_ppccap_P |= PPC_CRYPTO207;
356 }
357
358 if (hwcap2 & HWCAP_ARCH_3_00) {
359 OPENSSL_ppccap_P |= PPC_MADD300;
360 }
361 }
362#endif
363
364 sigfillset(&all_masked);
365 sigdelset(&all_masked, SIGILL);
366 sigdelset(&all_masked, SIGTRAP);
367#ifdef SIGEMT
368 sigdelset(&all_masked, SIGEMT);
369#endif
370 sigdelset(&all_masked, SIGFPE);
371 sigdelset(&all_masked, SIGBUS);
372 sigdelset(&all_masked, SIGSEGV);
373
374 memset(&ill_act, 0, sizeof(ill_act));
375 ill_act.sa_handler = ill_handler;
376 ill_act.sa_mask = all_masked;
377
378 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
379 sigaction(SIGILL, &ill_act, &ill_oact);
380
381#ifndef OSSL_IMPLEMENT_GETAUXVAL
382 if (sigsetjmp(ill_jmp,1) == 0) {
383 OPENSSL_fpu_probe();
384 OPENSSL_ppccap_P |= PPC_FPU;
385
386 if (sizeof(size_t) == 4) {
387# ifdef __linux
388 struct utsname uts;
389 if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
390# endif
391 if (sigsetjmp(ill_jmp, 1) == 0) {
392 OPENSSL_ppc64_probe();
393 OPENSSL_ppccap_P |= PPC_FPU64;
394 }
395 } else {
396 /*
397 * Wanted code detecting POWER6 CPU and setting PPC_FPU64
398 */
399 }
400 }
401
402 if (sigsetjmp(ill_jmp, 1) == 0) {
403 OPENSSL_altivec_probe();
404 OPENSSL_ppccap_P |= PPC_ALTIVEC;
405 if (sigsetjmp(ill_jmp, 1) == 0) {
406 OPENSSL_crypto207_probe();
407 OPENSSL_ppccap_P |= PPC_CRYPTO207;
408 }
409 }
410
411 if (sigsetjmp(ill_jmp, 1) == 0) {
412 OPENSSL_madd300_probe();
413 OPENSSL_ppccap_P |= PPC_MADD300;
414 }
415#endif
416
417 if (sigsetjmp(ill_jmp, 1) == 0) {
418 OPENSSL_rdtsc_mftb();
419 OPENSSL_ppccap_P |= PPC_MFTB;
420 } else if (sigsetjmp(ill_jmp, 1) == 0) {
421 OPENSSL_rdtsc_mfspr268();
422 OPENSSL_ppccap_P |= PPC_MFSPR268;
423 }
424
425 sigaction(SIGILL, &ill_oact, NULL);
426 sigprocmask(SIG_SETMASK, &oset, NULL);
427}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette