diff --git a/src/g10lib.h b/src/g10lib.h index 22c0f0c2..985e75c6 100644 --- a/src/g10lib.h +++ b/src/g10lib.h @@ -1,480 +1,484 @@ /* g10lib.h - Internal definitions for libgcrypt * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005 * 2007, 2011 Free Software Foundation, Inc. * * This file is part of Libgcrypt. * * Libgcrypt is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser general Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Libgcrypt is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . */ /* This header is to be used inside of libgcrypt in place of gcrypt.h. This way we can better distinguish between internal and external usage of gcrypt.h. */ #ifndef G10LIB_H #define G10LIB_H 1 #ifdef _GCRYPT_H #error gcrypt.h already included #endif #ifndef _GCRYPT_IN_LIBGCRYPT #error something is wrong with config.h #endif #include #include #include "visibility.h" #include "types.h" /* Attribute handling macros. */ #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) #define JNLIB_GCC_M_FUNCTION 1 #define JNLIB_GCC_A_NR __attribute__ ((noreturn)) #define JNLIB_GCC_A_PRINTF( f, a ) __attribute__ ((format (printf,f,a))) #define JNLIB_GCC_A_NR_PRINTF( f, a ) \ __attribute__ ((noreturn, format (printf,f,a))) #define GCC_ATTR_NORETURN __attribute__ ((__noreturn__)) #else #define JNLIB_GCC_A_NR #define JNLIB_GCC_A_PRINTF( f, a ) #define JNLIB_GCC_A_NR_PRINTF( f, a ) #define GCC_ATTR_NORETURN #endif #if __GNUC__ >= 3 /* According to glibc this attribute is available since 2.8 however we better play safe and use it only with gcc 3 or newer. */ #define GCC_ATTR_FORMAT_ARG(a) __attribute__ ((format_arg (a))) #else #define GCC_ATTR_FORMAT_ARG(a) #endif /* I am not sure since when the unused attribute is really supported. In any case it it only needed for gcc versions which print a warning. Thus let us require gcc >= 3.5. */ #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 5 ) #define GCC_ATTR_UNUSED __attribute__ ((unused)) #else #define GCC_ATTR_UNUSED #endif #if __GNUC__ > 3 #define NOINLINE_FUNC __attribute__((noinline)) #else #define NOINLINE_FUNC #endif #if __GNUC__ >= 3 #define LIKELY(expr) __builtin_expect( !!(expr), 1 ) #define UNLIKELY(expr) __builtin_expect( !!(expr), 0 ) #define CONSTANT_P(expr) __builtin_constant_p( expr ) #else #define LIKELY(expr) (!!(expr)) #define UNLIKELY(expr) (!!(expr)) #define CONSTANT_P(expr) (0) #endif /* Gettext macros. */ #define _(a) _gcry_gettext(a) #define N_(a) (a) /* Some handy macros */ #ifndef STR #define STR(v) #v #endif #define STR2(v) STR(v) #define DIM(v) (sizeof(v)/sizeof((v)[0])) #define DIMof(type,member) DIM(((type *)0)->member) #define my_isascii(c) (!((c) & 0x80)) /*-- src/global.c -*/ extern int _gcry_global_any_init_done; int _gcry_global_is_operational (void); gcry_err_code_t _gcry_vcontrol (enum gcry_ctl_cmds cmd, va_list arg_ptr); void _gcry_pre_syscall (void); void _gcry_post_syscall (void); int _gcry_get_debug_flag (unsigned int mask); char *_gcry_get_config (int mode, const char *what); /* Malloc functions and common wrapper macros. */ void *_gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC; void *_gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *_gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; void *_gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *_gcry_realloc (void *a, size_t n); char *_gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC; void *_gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC; void *_gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *_gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC; void *_gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC; void *_gcry_xrealloc (void *a, size_t n); char *_gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC; void _gcry_free (void *a); int _gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE; #define xtrymalloc(a) _gcry_malloc ((a)) #define xtrycalloc(a,b) _gcry_calloc ((a),(b)) #define xtrymalloc_secure(a) _gcry_malloc_secure ((a)) #define xtrycalloc_secure(a,b) _gcry_calloc_secure ((a),(b)) #define xtryrealloc(a,b) _gcry_realloc ((a),(b)) #define xtrystrdup(a) _gcry_strdup ((a)) #define xmalloc(a) _gcry_xmalloc ((a)) #define xcalloc(a,b) _gcry_xcalloc ((a),(b)) #define xmalloc_secure(a) _gcry_xmalloc_secure ((a)) #define xcalloc_secure(a,b) _gcry_xcalloc_secure ((a),(b)) #define xrealloc(a,b) _gcry_xrealloc ((a),(b)) #define xstrdup(a) _gcry_xstrdup ((a)) #define xfree(a) _gcry_free ((a)) /*-- src/misc.c --*/ #if defined(JNLIB_GCC_M_FUNCTION) || __STDC_VERSION__ >= 199901L void _gcry_bug (const char *file, int line, const char *func) GCC_ATTR_NORETURN; void _gcry_assert_failed (const char *expr, const char *file, int line, const char *func) GCC_ATTR_NORETURN; #else void _gcry_bug (const char *file, int line); void _gcry_assert_failed (const char *expr, const char *file, int line); #endif void _gcry_divide_by_zero (void) JNLIB_GCC_A_NR; const char *_gcry_gettext (const char *key) GCC_ATTR_FORMAT_ARG(1); void _gcry_fatal_error(int rc, const char *text ) JNLIB_GCC_A_NR; void _gcry_logv (int level, const char *fmt, va_list arg_ptr) JNLIB_GCC_A_PRINTF(2,0); void _gcry_log( int level, const char *fmt, ... ) JNLIB_GCC_A_PRINTF(2,3); void _gcry_log_bug( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2); void _gcry_log_fatal( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2); void _gcry_log_error( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); void _gcry_log_info( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); void _gcry_log_debug( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); void _gcry_log_printf ( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); void _gcry_log_printhex (const char *text, const void *buffer, size_t length); void _gcry_log_printmpi (const char *text, gcry_mpi_t mpi); void _gcry_log_printsxp (const char *text, gcry_sexp_t sexp); void _gcry_set_log_verbosity( int level ); int _gcry_log_verbosity( int level ); #ifdef JNLIB_GCC_M_FUNCTION #define BUG() _gcry_bug( __FILE__ , __LINE__, __FUNCTION__ ) #define gcry_assert(expr) (LIKELY(expr)? (void)0 \ : _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __FUNCTION__)) #elif __STDC_VERSION__ >= 199901L #define BUG() _gcry_bug( __FILE__ , __LINE__, __func__ ) #define gcry_assert(expr) (LIKELY(expr)? (void)0 \ : _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __func__)) #else #define BUG() _gcry_bug( __FILE__ , __LINE__ ) #define gcry_assert(expr) (LIKELY(expr)? (void)0 \ : _gcry_assert_failed (STR(expr), __FILE__, __LINE__)) #endif #define log_bug _gcry_log_bug #define log_fatal _gcry_log_fatal #define log_error _gcry_log_error #define log_info _gcry_log_info #define log_debug _gcry_log_debug #define log_printf _gcry_log_printf #define log_printhex _gcry_log_printhex #define log_printmpi _gcry_log_printmpi #define log_printsxp _gcry_log_printsxp /* Compatibility macro. */ #define log_mpidump _gcry_log_printmpi /* Tokeninze STRING and return a malloced array. */ char **_gcry_strtokenize (const char *string, const char *delim); /*-- src/hwfeatures.c --*/ #if defined(HAVE_CPU_ARCH_X86) #define HWF_PADLOCK_RNG (1 << 0) #define HWF_PADLOCK_AES (1 << 1) #define HWF_PADLOCK_SHA (1 << 2) #define HWF_PADLOCK_MMUL (1 << 3) #define HWF_INTEL_CPU (1 << 4) #define HWF_INTEL_FAST_SHLD (1 << 5) #define HWF_INTEL_BMI2 (1 << 6) #define HWF_INTEL_SSSE3 (1 << 7) #define HWF_INTEL_SSE4_1 (1 << 8) #define HWF_INTEL_PCLMUL (1 << 9) #define HWF_INTEL_AESNI (1 << 10) #define HWF_INTEL_RDRAND (1 << 11) #define HWF_INTEL_AVX (1 << 12) #define HWF_INTEL_AVX2 (1 << 13) #define HWF_INTEL_FAST_VPGATHER (1 << 14) #define HWF_INTEL_RDTSC (1 << 15) #define HWF_INTEL_SHAEXT (1 << 16) #define HWF_INTEL_VAES_VPCLMUL (1 << 17) #elif defined(HAVE_CPU_ARCH_ARM) #define HWF_ARM_NEON (1 << 0) #define HWF_ARM_AES (1 << 1) #define HWF_ARM_SHA1 (1 << 2) #define HWF_ARM_SHA2 (1 << 3) #define HWF_ARM_PMULL (1 << 4) +#define HWF_ARM_SHA3 (1 << 5) +#define HWF_ARM_SM3 (1 << 6) +#define HWF_ARM_SM4 (1 << 7) +#define HWF_ARM_SHA512 (1 << 8) #elif defined(HAVE_CPU_ARCH_PPC) #define HWF_PPC_VCRYPTO (1 << 0) #define HWF_PPC_ARCH_3_00 (1 << 1) #define HWF_PPC_ARCH_2_07 (1 << 2) #define HWF_PPC_ARCH_3_10 (1 << 3) #elif defined(HAVE_CPU_ARCH_S390X) #define HWF_S390X_MSA (1 << 0) #define HWF_S390X_MSA_4 (1 << 1) #define HWF_S390X_MSA_8 (1 << 2) #define HWF_S390X_MSA_9 (1 << 3) #define HWF_S390X_VX (1 << 4) #endif gpg_err_code_t _gcry_disable_hw_feature (const char *name); void _gcry_detect_hw_features (void); unsigned int _gcry_get_hw_features (void); const char *_gcry_enum_hw_features (int idx, unsigned int *r_feature); /*-- mpi/mpiutil.c --*/ const char *_gcry_mpi_get_hw_config (void); /*-- cipher/pubkey.c --*/ /* FIXME: shouldn't this go into mpi.h? */ #ifndef mpi_powm #define mpi_powm(w,b,e,m) gcry_mpi_powm( (w), (b), (e), (m) ) #endif /*-- primegen.c --*/ gcry_err_code_t _gcry_primegen_init (void); gcry_mpi_t _gcry_generate_secret_prime (unsigned int nbits, gcry_random_level_t random_level, int (*extra_check)(void*, gcry_mpi_t), void *extra_check_arg); gcry_mpi_t _gcry_generate_public_prime (unsigned int nbits, gcry_random_level_t random_level, int (*extra_check)(void*, gcry_mpi_t), void *extra_check_arg); gcry_err_code_t _gcry_generate_elg_prime (int mode, unsigned int pbits, unsigned int qbits, gcry_mpi_t g, gcry_mpi_t *r_prime, gcry_mpi_t **factors); gcry_mpi_t _gcry_derive_x931_prime (const gcry_mpi_t xp, const gcry_mpi_t xp1, const gcry_mpi_t xp2, const gcry_mpi_t e, gcry_mpi_t *r_p1, gcry_mpi_t *r_p2); gpg_err_code_t _gcry_generate_fips186_2_prime (unsigned int pbits, unsigned int qbits, const void *seed, size_t seedlen, gcry_mpi_t *r_q, gcry_mpi_t *r_p, int *r_counter, void **r_seed, size_t *r_seedlen); gpg_err_code_t _gcry_generate_fips186_3_prime (unsigned int pbits, unsigned int qbits, const void *seed, size_t seedlen, gcry_mpi_t *r_q, gcry_mpi_t *r_p, int *r_counter, void **r_seed, size_t *r_seedlen, int *r_hashalgo); gpg_err_code_t _gcry_fips186_4_prime_check (const gcry_mpi_t x, unsigned int bits); /* Replacements of missing functions (missing-string.c). */ #ifndef HAVE_STPCPY char *stpcpy (char *a, const char *b); #endif #ifndef HAVE_STRCASECMP int strcasecmp (const char *a, const char *b) _GCRY_GCC_ATTR_PURE; #endif #include "../compat/libcompat.h" /* Macros used to rename missing functions. */ #ifndef HAVE_STRTOUL #define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c))) #endif #ifndef HAVE_MEMMOVE #define memmove(d, s, n) bcopy((s), (d), (n)) #endif #ifndef HAVE_STRICMP #define stricmp(a,b) strcasecmp( (a), (b) ) #endif #ifndef HAVE_ATEXIT #define atexit(a) (on_exit((a),0)) #endif #ifndef HAVE_RAISE #define raise(a) kill(getpid(), (a)) #endif /* Stack burning. */ #ifdef HAVE_GCC_ASM_VOLATILE_MEMORY #define __gcry_burn_stack_dummy() asm volatile ("":::"memory") #else void __gcry_burn_stack_dummy (void); #endif void __gcry_burn_stack (unsigned int bytes); #define _gcry_burn_stack(bytes) \ do { __gcry_burn_stack (bytes); \ __gcry_burn_stack_dummy (); } while(0) /* To avoid that a compiler optimizes certain memset calls away, this macro may be used instead. For constant length buffers, memory wiping is inlined. Dead store elimination of inlined memset is avoided here by using assembly block after memset. For non-constant length buffers, memory is wiped through _gcry_fast_wipememory. */ #ifdef HAVE_GCC_ASM_VOLATILE_MEMORY #define fast_wipememory2_inline(_ptr,_set,_len) do { \ memset((_ptr), (_set), (_len)); \ asm volatile ("\n" :: "r" (_ptr) : "memory"); \ } while(0) #else #define fast_wipememory2_inline(_ptr,_set,_len) \ _gcry_fast_wipememory2((void *)_ptr, _set, _len) #endif #define wipememory2(_ptr,_set,_len) do { \ if (!CONSTANT_P(_len) || !CONSTANT_P(_set)) { \ if (CONSTANT_P(_set) && (_set) == 0) \ _gcry_fast_wipememory((void *)(_ptr), (_len)); \ else \ _gcry_fast_wipememory2((void *)(_ptr), (_set), (_len)); \ } else { \ fast_wipememory2_inline((void *)(_ptr), (_set), (_len)); \ } \ } while(0) #define wipememory(_ptr,_len) wipememory2((_ptr),0,(_len)) void _gcry_fast_wipememory(void *ptr, size_t len); void _gcry_fast_wipememory2(void *ptr, int set, size_t len); /* Digit predicates. */ #define digitp(p) (*(p) >= '0' && *(p) <= '9') #define octdigitp(p) (*(p) >= '0' && *(p) <= '7') #define alphap(a) ( (*(a) >= 'A' && *(a) <= 'Z') \ || (*(a) >= 'a' && *(a) <= 'z')) #define hexdigitp(a) (digitp (a) \ || (*(a) >= 'A' && *(a) <= 'F') \ || (*(a) >= 'a' && *(a) <= 'f')) /* Init functions. */ gcry_err_code_t _gcry_cipher_init (void); gcry_err_code_t _gcry_md_init (void); gcry_err_code_t _gcry_mac_init (void); gcry_err_code_t _gcry_pk_init (void); gcry_err_code_t _gcry_secmem_module_init (void); gcry_err_code_t _gcry_mpi_init (void); /* Memory management. */ #define GCRY_ALLOC_FLAG_SECURE (1 << 0) #define GCRY_ALLOC_FLAG_XHINT (1 << 1) /* Called from xmalloc. */ /*-- sexp.c --*/ gcry_err_code_t _gcry_sexp_vbuild (gcry_sexp_t *retsexp, size_t *erroff, const char *format, va_list arg_ptr); char *_gcry_sexp_nth_string (const gcry_sexp_t list, int number); gpg_err_code_t _gcry_sexp_vextract_param (gcry_sexp_t sexp, const char *path, const char *list, va_list arg_ptr); /*-- fips.c --*/ extern int _gcry_no_fips_mode_required; void _gcry_initialize_fips_mode (int force); int _gcry_fips_to_activate (void); /* This macro returns true if fips mode is enabled. This is independent of the fips required finite state machine and only used to enable fips specific code. No locking is required because we have the requirement that this variable is only initialized once with no other threads existing. */ #define fips_mode() (!_gcry_no_fips_mode_required) void _gcry_fips_signal_error (const char *srcfile, int srcline, const char *srcfunc, int is_fatal, const char *description); #ifdef JNLIB_GCC_M_FUNCTION # define fips_signal_error(a) \ _gcry_fips_signal_error (__FILE__, __LINE__, __FUNCTION__, 0, (a)) # define fips_signal_fatal_error(a) \ _gcry_fips_signal_error (__FILE__, __LINE__, __FUNCTION__, 1, (a)) #else # define fips_signal_error(a) \ _gcry_fips_signal_error (__FILE__, __LINE__, NULL, 0, (a)) # define fips_signal_fatal_error(a) \ _gcry_fips_signal_error (__FILE__, __LINE__, NULL, 1, (a)) #endif int _gcry_fips_indicator_cipher (va_list arg_ptr); int _gcry_fips_indicator_kdf (va_list arg_ptr); int _gcry_fips_is_operational (void); /* Return true if the library is in the operational state. */ #define fips_is_operational() \ (!_gcry_global_any_init_done ? \ _gcry_global_is_operational() : \ (!fips_mode () || _gcry_global_is_operational ())) #define fips_not_operational() (GPG_ERR_NOT_OPERATIONAL) int _gcry_fips_test_operational (void); int _gcry_fips_test_error_or_operational (void); gpg_err_code_t _gcry_fips_run_selftests (int extended); void _gcry_fips_noreturn (void); #define fips_noreturn() (_gcry_fips_noreturn ()) #endif /* G10LIB_H */ diff --git a/src/hwf-arm.c b/src/hwf-arm.c index 60107f36..70d375b2 100644 --- a/src/hwf-arm.c +++ b/src/hwf-arm.c @@ -1,456 +1,472 @@ /* hwf-arm.c - Detect hardware features - ARM part * Copyright (C) 2013,2019 Jussi Kivilinna * * This file is part of Libgcrypt. * * Libgcrypt is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Libgcrypt is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . */ #include #include #include #include #include #include #include #if defined(HAVE_SYS_AUXV_H) && (defined(HAVE_GETAUXVAL) || \ defined(HAVE_ELF_AUX_INFO)) #include #endif #include "g10lib.h" #include "hwf-common.h" #if !defined (__arm__) && !defined (__aarch64__) # error Module build for wrong CPU. #endif #if defined(HAVE_SYS_AUXV_H) && defined(HAVE_ELF_AUX_INFO) && \ !defined(HAVE_GETAUXVAL) && defined(AT_HWCAP) #define HAVE_GETAUXVAL static unsigned long getauxval(unsigned long type) { unsigned long auxval = 0; int err; /* FreeBSD provides 'elf_aux_info' function that does the same as * 'getauxval' on Linux. */ err = elf_aux_info (type, &auxval, sizeof(auxval)); if (err) { errno = err; auxval = 0; } return auxval; } #endif #undef HAS_SYS_AT_HWCAP #if defined(__linux__) || \ (defined(HAVE_SYS_AUXV_H) && defined(HAVE_GETAUXVAL)) #define HAS_SYS_AT_HWCAP 1 struct feature_map_s { unsigned int hwcap_flag; unsigned int hwcap2_flag; const char *feature_match; unsigned int hwf_flag; }; #ifdef __arm__ /* Note: These macros have same values on Linux and FreeBSD. */ #ifndef AT_HWCAP # define AT_HWCAP 16 #endif #ifndef AT_HWCAP2 # define AT_HWCAP2 26 #endif #ifndef HWCAP_NEON # define HWCAP_NEON 4096 #endif #ifndef HWCAP2_AES # define HWCAP2_AES 1 #endif #ifndef HWCAP2_PMULL # define HWCAP2_PMULL 2 #endif #ifndef HWCAP2_SHA1 # define HWCAP2_SHA1 4 #endif #ifndef HWCAP2_SHA2 # define HWCAP2_SHA2 8 #endif static const struct feature_map_s arm_features[] = { #ifdef ENABLE_NEON_SUPPORT { HWCAP_NEON, 0, " neon", HWF_ARM_NEON }, #endif #ifdef ENABLE_ARM_CRYPTO_SUPPORT { 0, HWCAP2_AES, " aes", HWF_ARM_AES }, { 0, HWCAP2_SHA1," sha1", HWF_ARM_SHA1 }, { 0, HWCAP2_SHA2, " sha2", HWF_ARM_SHA2 }, { 0, HWCAP2_PMULL, " pmull", HWF_ARM_PMULL }, #endif }; #elif defined(__aarch64__) /* Note: These macros have same values on Linux and FreeBSD. */ #ifndef AT_HWCAP # define AT_HWCAP 16 #endif #ifndef AT_HWCAP2 # define AT_HWCAP2 -1 #endif #ifndef HWCAP_ASIMD # define HWCAP_ASIMD 2 #endif #ifndef HWCAP_AES # define HWCAP_AES 8 #endif #ifndef HWCAP_PMULL # define HWCAP_PMULL 16 #endif #ifndef HWCAP_SHA1 # define HWCAP_SHA1 32 #endif #ifndef HWCAP_SHA2 # define HWCAP_SHA2 64 #endif +#ifndef HWCAP_SHA3 +# define HWCAP_SHA3 (1 << 17) +#endif +#ifndef HWCAP_SM3 +# define HWCAP_SM3 (1 << 18) +#endif +#ifndef HWCAP_SM4 +# define HWCAP_SM4 (1 << 19) +#endif +#ifndef HWCAP_SHA512 +# define HWCAP_SHA512 (1 << 21) +#endif static const struct feature_map_s arm_features[] = { #ifdef ENABLE_NEON_SUPPORT { HWCAP_ASIMD, 0, " asimd", HWF_ARM_NEON }, #endif #ifdef ENABLE_ARM_CRYPTO_SUPPORT { HWCAP_AES, 0, " aes", HWF_ARM_AES }, { HWCAP_SHA1, 0, " sha1", HWF_ARM_SHA1 }, { HWCAP_SHA2, 0, " sha2", HWF_ARM_SHA2 }, { HWCAP_PMULL, 0, " pmull", HWF_ARM_PMULL }, + { HWCAP_SHA3, 0, " sha3", HWF_ARM_SHA3 }, + { HWCAP_SM3, 0, " sm3", HWF_ARM_SM3 }, + { HWCAP_SM4, 0, " sm4", HWF_ARM_SM4 }, + { HWCAP_SHA512, 0, " sha512", HWF_ARM_SHA512 }, #endif }; #endif static int get_hwcap(unsigned int *hwcap, unsigned int *hwcap2) { struct { unsigned long a_type; unsigned long a_val; } auxv; FILE *f; int err = -1; static int hwcap_initialized = 0; static unsigned int stored_hwcap = 0; static unsigned int stored_hwcap2 = 0; if (hwcap_initialized) { *hwcap = stored_hwcap; *hwcap2 = stored_hwcap2; return 0; } #if defined(HAVE_SYS_AUXV_H) && defined(HAVE_GETAUXVAL) errno = 0; auxv.a_val = getauxval (AT_HWCAP); if (errno == 0) { stored_hwcap |= auxv.a_val; hwcap_initialized = 1; } if (AT_HWCAP2 >= 0) { errno = 0; auxv.a_val = getauxval (AT_HWCAP2); if (errno == 0) { stored_hwcap2 |= auxv.a_val; hwcap_initialized = 1; } } if (hwcap_initialized && (stored_hwcap || stored_hwcap2)) { *hwcap = stored_hwcap; *hwcap2 = stored_hwcap2; return 0; } #endif f = fopen("/proc/self/auxv", "r"); if (!f) { *hwcap = stored_hwcap; *hwcap2 = stored_hwcap2; return -1; } while (fread(&auxv, sizeof(auxv), 1, f) > 0) { if (auxv.a_type == AT_HWCAP) { stored_hwcap |= auxv.a_val; hwcap_initialized = 1; } if (auxv.a_type == AT_HWCAP2) { stored_hwcap2 |= auxv.a_val; hwcap_initialized = 1; } } if (hwcap_initialized) err = 0; fclose(f); *hwcap = stored_hwcap; *hwcap2 = stored_hwcap2; return err; } static unsigned int detect_arm_at_hwcap(void) { unsigned int hwcap; unsigned int hwcap2; unsigned int features = 0; unsigned int i; if (get_hwcap(&hwcap, &hwcap2) < 0) return features; for (i = 0; i < DIM(arm_features); i++) { if (hwcap & arm_features[i].hwcap_flag) features |= arm_features[i].hwf_flag; if (hwcap2 & arm_features[i].hwcap2_flag) features |= arm_features[i].hwf_flag; } return features; } #endif #undef HAS_PROC_CPUINFO #ifdef __linux__ #define HAS_PROC_CPUINFO 1 static unsigned int detect_arm_proc_cpuinfo(unsigned int *broken_hwfs) { char buf[1024]; /* large enough */ char *str_features, *str_feat; int cpu_implementer, cpu_arch, cpu_variant, cpu_part, cpu_revision; FILE *f; int readlen, i; size_t mlen; static int cpuinfo_initialized = 0; static unsigned int stored_cpuinfo_features; static unsigned int stored_broken_hwfs; struct { const char *name; int *value; } cpu_entries[5] = { { "CPU implementer", &cpu_implementer }, { "CPU architecture", &cpu_arch }, { "CPU variant", &cpu_variant }, { "CPU part", &cpu_part }, { "CPU revision", &cpu_revision }, }; if (cpuinfo_initialized) { *broken_hwfs |= stored_broken_hwfs; return stored_cpuinfo_features; } f = fopen("/proc/cpuinfo", "r"); if (!f) return 0; memset (buf, 0, sizeof(buf)); readlen = fread (buf, 1, sizeof(buf), f); fclose (f); if (readlen <= 0 || readlen > sizeof(buf)) return 0; buf[sizeof(buf) - 1] = '\0'; cpuinfo_initialized = 1; stored_cpuinfo_features = 0; stored_broken_hwfs = 0; /* Find features line. */ str_features = strstr(buf, "Features"); if (!str_features) return stored_cpuinfo_features; /* Find CPU version information. */ for (i = 0; i < DIM(cpu_entries); i++) { char *str; *cpu_entries[i].value = -1; str = strstr(buf, cpu_entries[i].name); if (!str) continue; str = strstr(str, ": "); if (!str) continue; str += 2; if (strcmp(cpu_entries[i].name, "CPU architecture") == 0 && strcmp(str, "AArch64") == 0) *cpu_entries[i].value = 8; else *cpu_entries[i].value = strtoul(str, NULL, 0); } /* Lines to strings. */ for (i = 0; i < sizeof(buf); i++) if (buf[i] == '\n') buf[i] = '\0'; /* Check features. */ for (i = 0; i < DIM(arm_features); i++) { str_feat = strstr(str_features, arm_features[i].feature_match); if (str_feat) { mlen = strlen(arm_features[i].feature_match); if (str_feat[mlen] == ' ' || str_feat[mlen] == '\0') { stored_cpuinfo_features |= arm_features[i].hwf_flag; } } } /* Check for CPUs with broken NEON implementation. See * https://code.google.com/p/chromium/issues/detail?id=341598 */ if (cpu_implementer == 0x51 && cpu_arch == 7 && cpu_variant == 1 && cpu_part == 0x4d && cpu_revision == 0) { stored_broken_hwfs = HWF_ARM_NEON; } *broken_hwfs |= stored_broken_hwfs; return stored_cpuinfo_features; } #endif /* __linux__ */ static unsigned int detect_arm_hwf_by_toolchain (void) { unsigned int ret = 0; /* Detect CPU features required by toolchain. * This allows detection of ARMv8 crypto extension support, * for example, on macOS/aarch64. */ #if __GNUC__ >= 4 #if defined(__ARM_NEON) && defined(ENABLE_NEON_SUPPORT) ret |= HWF_ARM_NEON; #ifdef HAVE_GCC_INLINE_ASM_NEON /* Early test for NEON instruction to detect faulty toolchain * configuration. */ asm volatile ("veor q15, q15, q15":::"q15"); #endif #ifdef HAVE_GCC_INLINE_ASM_AARCH64_NEON /* Early test for NEON instruction to detect faulty toolchain * configuration. */ asm volatile ("eor v31.16b, v31.16b, v31.16b":::"v31"); #endif #endif /* __ARM_NEON */ #if defined(__ARM_FEATURE_CRYPTO) /* ARMv8 crypto extensions include support for PMULL, AES, SHA1 and SHA2 * instructions. */ ret |= HWF_ARM_PMULL; ret |= HWF_ARM_AES; ret |= HWF_ARM_SHA1; ret |= HWF_ARM_SHA2; #ifdef HAVE_GCC_INLINE_ASM_AARCH32_CRYPTO /* Early test for CE instructions to detect faulty toolchain * configuration. */ asm volatile ("vmull.p64 q0, d0, d0;\n\t" "aesimc.8 q7, q0;\n\t" "sha1su1.32 q0, q0;\n\t" "sha256su1.32 q0, q7, q15;\n\t" ::: "q0", "q7", "q15"); #endif #ifdef HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO /* Early test for CE instructions to detect faulty toolchain * configuration. */ asm volatile ("pmull2 v0.1q, v0.2d, v31.2d;\n\t" "aesimc v15.16b, v0.16b;\n\t" "sha1su1 v0.4s, v0.4s;\n\t" "sha256su1 v0.4s, v15.4s, v31.4s;\n\t" ::: "v0", "v15", "v31"); #endif #endif #endif return ret; } unsigned int _gcry_hwf_detect_arm (void) { unsigned int ret = 0; unsigned int broken_hwfs = 0; #if defined (HAS_SYS_AT_HWCAP) ret |= detect_arm_at_hwcap (); #endif #if defined (HAS_PROC_CPUINFO) ret |= detect_arm_proc_cpuinfo (&broken_hwfs); #endif ret |= detect_arm_hwf_by_toolchain (); ret &= ~broken_hwfs; return ret; } diff --git a/src/hwfeatures.c b/src/hwfeatures.c index 97e67b3c..7060d995 100644 --- a/src/hwfeatures.c +++ b/src/hwfeatures.c @@ -1,238 +1,242 @@ /* hwfeatures.c - Detect hardware features. * Copyright (C) 2007, 2011 Free Software Foundation, Inc. * Copyright (C) 2012 g10 Code GmbH * * This file is part of Libgcrypt. * * Libgcrypt is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Libgcrypt is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . */ #include #include #include #include #include #include #include #ifdef HAVE_SYSLOG # include #endif /*HAVE_SYSLOG*/ #include "g10lib.h" #include "hwf-common.h" /* The name of a file used to globally disable selected features. */ #define HWF_DENY_FILE "/etc/gcrypt/hwf.deny" /* A table to map hardware features to a string. * Note: Remember to add new HW features to 'doc/gcrypt.texi'. */ static struct { unsigned int flag; const char *desc; } hwflist[] = { #if defined(HAVE_CPU_ARCH_X86) { HWF_PADLOCK_RNG, "padlock-rng" }, { HWF_PADLOCK_AES, "padlock-aes" }, { HWF_PADLOCK_SHA, "padlock-sha" }, { HWF_PADLOCK_MMUL, "padlock-mmul"}, { HWF_INTEL_CPU, "intel-cpu" }, { HWF_INTEL_FAST_SHLD, "intel-fast-shld" }, { HWF_INTEL_BMI2, "intel-bmi2" }, { HWF_INTEL_SSSE3, "intel-ssse3" }, { HWF_INTEL_SSE4_1, "intel-sse4.1" }, { HWF_INTEL_PCLMUL, "intel-pclmul" }, { HWF_INTEL_AESNI, "intel-aesni" }, { HWF_INTEL_RDRAND, "intel-rdrand" }, { HWF_INTEL_AVX, "intel-avx" }, { HWF_INTEL_AVX2, "intel-avx2" }, { HWF_INTEL_FAST_VPGATHER, "intel-fast-vpgather" }, { HWF_INTEL_RDTSC, "intel-rdtsc" }, { HWF_INTEL_SHAEXT, "intel-shaext" }, { HWF_INTEL_VAES_VPCLMUL, "intel-vaes-vpclmul" }, #elif defined(HAVE_CPU_ARCH_ARM) { HWF_ARM_NEON, "arm-neon" }, { HWF_ARM_AES, "arm-aes" }, { HWF_ARM_SHA1, "arm-sha1" }, { HWF_ARM_SHA2, "arm-sha2" }, { HWF_ARM_PMULL, "arm-pmull" }, + { HWF_ARM_SHA3, "arm-sha3" }, + { HWF_ARM_SM3, "arm-sm3" }, + { HWF_ARM_SM4, "arm-sm4" }, + { HWF_ARM_SHA512, "arm-sha512" }, #elif defined(HAVE_CPU_ARCH_PPC) { HWF_PPC_VCRYPTO, "ppc-vcrypto" }, { HWF_PPC_ARCH_3_00, "ppc-arch_3_00" }, { HWF_PPC_ARCH_2_07, "ppc-arch_2_07" }, { HWF_PPC_ARCH_3_10, "ppc-arch_3_10" }, #elif defined(HAVE_CPU_ARCH_S390X) { HWF_S390X_MSA, "s390x-msa" }, { HWF_S390X_MSA_4, "s390x-msa-4" }, { HWF_S390X_MSA_8, "s390x-msa-8" }, { HWF_S390X_MSA_9, "s390x-msa-9" }, { HWF_S390X_VX, "s390x-vx" }, #endif }; /* A bit vector with the hardware features which shall not be used. This variable must be set prior to any initialization. */ static unsigned int disabled_hw_features; /* A bit vector describing the hardware features currently available. */ static unsigned int hw_features; /* Disable a feature by name. This function must be called *before* _gcry_detect_hw_features is called. */ gpg_err_code_t _gcry_disable_hw_feature (const char *name) { int i; size_t n1, n2; while (name && *name) { n1 = strcspn (name, ":,"); if (!n1) ; else if (n1 == 3 && !strncmp (name, "all", 3)) disabled_hw_features = ~0; else { for (i=0; i < DIM (hwflist); i++) { n2 = strlen (hwflist[i].desc); if (n1 == n2 && !strncmp (hwflist[i].desc, name, n2)) { disabled_hw_features |= hwflist[i].flag; break; } } if (!(i < DIM (hwflist))) return GPG_ERR_INV_NAME; } name += n1; if (*name) name++; /* Skip delimiter ':' or ','. */ } return 0; } /* Return a bit vector describing the available hardware features. The HWF_ constants are used to test for them. */ unsigned int _gcry_get_hw_features (void) { return hw_features; } /* Enumerate all features. The caller is expected to start with an IDX of 0 and then increment IDX until NULL is returned. */ const char * _gcry_enum_hw_features (int idx, unsigned int *r_feature) { if (idx < 0 || idx >= DIM (hwflist)) return NULL; if (r_feature) *r_feature = hwflist[idx].flag; return hwflist[idx].desc; } /* Read a file with features which shall not be used. The file is a simple text file where empty lines and lines with the first non white-space character being '#' are ignored. */ static void parse_hwf_deny_file (void) { const char *fname = HWF_DENY_FILE; FILE *fp; char buffer[256]; char *p, *pend; int lnr = 0; fp = fopen (fname, "r"); if (!fp) return; for (;;) { if (!fgets (buffer, sizeof buffer, fp)) { if (!feof (fp)) { #ifdef HAVE_SYSLOG syslog (LOG_USER|LOG_WARNING, "Libgcrypt warning: error reading '%s', line %d", fname, lnr); #endif /*HAVE_SYSLOG*/ } fclose (fp); return; } lnr++; for (p=buffer; my_isascii (*p) && isspace (*p); p++) ; pend = strchr (p, '\n'); if (pend) *pend = 0; pend = p + (*p? (strlen (p)-1):0); for ( ;pend > p; pend--) if (my_isascii (*pend) && isspace (*pend)) *pend = 0; if (!*p || *p == '#') continue; if (_gcry_disable_hw_feature (p) == GPG_ERR_INV_NAME) { #ifdef HAVE_SYSLOG syslog (LOG_USER|LOG_WARNING, "Libgcrypt warning: unknown feature in '%s', line %d", fname, lnr); #endif /*HAVE_SYSLOG*/ } } } /* Detect the available hardware features. This function is called once right at startup and we assume that no other threads are running. */ void _gcry_detect_hw_features (void) { hw_features = 0; parse_hwf_deny_file (); #if defined (HAVE_CPU_ARCH_X86) { hw_features = _gcry_hwf_detect_x86 (); } #elif defined (HAVE_CPU_ARCH_ARM) { hw_features = _gcry_hwf_detect_arm (); } #elif defined (HAVE_CPU_ARCH_PPC) { hw_features = _gcry_hwf_detect_ppc (); } #elif defined (HAVE_CPU_ARCH_S390X) { hw_features = _gcry_hwf_detect_s390x (); } #endif hw_features &= ~disabled_hw_features; }