LCOV - code coverage report
Current view: top level - src - hwfeatures.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 48 0.0 %
Date: 2016-12-15 12:59:22 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /* hwfeatures.c - Detect hardware features.
       2             :  * Copyright (C) 2007, 2011  Free Software Foundation, Inc.
       3             :  * Copyright (C) 2012  g10 Code GmbH
       4             :  *
       5             :  * This file is part of Libgcrypt.
       6             :  *
       7             :  * Libgcrypt is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the GNU Lesser General Public License as
       9             :  * published by the Free Software Foundation; either version 2.1 of
      10             :  * the License, or (at your option) any later version.
      11             :  *
      12             :  * Libgcrypt is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  * GNU Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
      19             :  */
      20             : 
      21             : #include <config.h>
      22             : #include <stdio.h>
      23             : #include <ctype.h>
      24             : #include <stdlib.h>
      25             : #include <string.h>
      26             : #include <stdarg.h>
      27             : #include <unistd.h>
      28             : #ifdef HAVE_SYSLOG
      29             : # include <syslog.h>
      30             : #endif /*HAVE_SYSLOG*/
      31             : 
      32             : #include "g10lib.h"
      33             : #include "hwf-common.h"
      34             : 
      35             : /* The name of a file used to globally disable selected features. */
      36             : #define HWF_DENY_FILE "/etc/gcrypt/hwf.deny"
      37             : 
      38             : /* A table to map hardware features to a string.  */
      39             : static struct
      40             : {
      41             :   unsigned int flag;
      42             :   const char *desc;
      43             : } hwflist[] =
      44             :   {
      45             :     { HWF_PADLOCK_RNG,     "padlock-rng" },
      46             :     { HWF_PADLOCK_AES,     "padlock-aes" },
      47             :     { HWF_PADLOCK_SHA,     "padlock-sha" },
      48             :     { HWF_PADLOCK_MMUL,    "padlock-mmul"},
      49             :     { HWF_INTEL_CPU,       "intel-cpu" },
      50             :     { HWF_INTEL_FAST_SHLD, "intel-fast-shld" },
      51             :     { HWF_INTEL_BMI2,      "intel-bmi2" },
      52             :     { HWF_INTEL_SSSE3,     "intel-ssse3" },
      53             :     { HWF_INTEL_SSE4_1,    "intel-sse4.1" },
      54             :     { HWF_INTEL_PCLMUL,    "intel-pclmul" },
      55             :     { HWF_INTEL_AESNI,     "intel-aesni" },
      56             :     { HWF_INTEL_RDRAND,    "intel-rdrand" },
      57             :     { HWF_INTEL_AVX,       "intel-avx" },
      58             :     { HWF_INTEL_AVX2,      "intel-avx2" },
      59             :     { HWF_ARM_NEON,        "arm-neon" },
      60             :     { HWF_ARM_AES,         "arm-aes" },
      61             :     { HWF_ARM_SHA1,        "arm-sha1" },
      62             :     { HWF_ARM_SHA2,        "arm-sha2" },
      63             :     { HWF_ARM_PMULL,       "arm-pmull" }
      64             :   };
      65             : 
      66             : /* A bit vector with the hardware features which shall not be used.
      67             :    This variable must be set prior to any initialization.  */
      68             : static unsigned int disabled_hw_features;
      69             : 
      70             : /* A bit vector describing the hardware features currently
      71             :    available. */
      72             : static unsigned int hw_features;
      73             : 
      74             : /* Convenience macros.  */
      75             : #define my_isascii(c) (!((c) & 0x80))
      76             : 
      77             : 
      78             : 
      79             : /* Disable a feature by name.  This function must be called *before*
      80             :    _gcry_detect_hw_features is called.  */
      81             : gpg_err_code_t
      82           0 : _gcry_disable_hw_feature (const char *name)
      83             : {
      84             :   int i;
      85             : 
      86           0 :   if (!strcmp(name, "all"))
      87             :     {
      88           0 :       disabled_hw_features = ~0;
      89           0 :       return 0;
      90             :     }
      91             : 
      92           0 :   for (i=0; i < DIM (hwflist); i++)
      93           0 :     if (!strcmp (hwflist[i].desc, name))
      94             :       {
      95           0 :         disabled_hw_features |= hwflist[i].flag;
      96           0 :         return 0;
      97             :       }
      98           0 :   return GPG_ERR_INV_NAME;
      99             : }
     100             : 
     101             : 
     102             : /* Return a bit vector describing the available hardware features.
     103             :    The HWF_ constants are used to test for them. */
     104             : unsigned int
     105           0 : _gcry_get_hw_features (void)
     106             : {
     107           0 :   return hw_features;
     108             : }
     109             : 
     110             : 
     111             : /* Enumerate all features.  The caller is expected to start with an
     112             :    IDX of 0 and then increment IDX until NULL is returned.  */
     113             : const char *
     114           0 : _gcry_enum_hw_features (int idx, unsigned int *r_feature)
     115             : {
     116           0 :   if (idx < 0 || idx >= DIM (hwflist))
     117           0 :     return NULL;
     118           0 :   if (r_feature)
     119           0 :     *r_feature = hwflist[idx].flag;
     120           0 :   return hwflist[idx].desc;
     121             : }
     122             : 
     123             : 
     124             : /* Read a file with features which shall not be used.  The file is a
     125             :    simple text file where empty lines and lines with the first non
     126             :    white-space character being '#' are ignored.  */
     127             : static void
     128           0 : parse_hwf_deny_file (void)
     129             : {
     130           0 :   const char *fname = HWF_DENY_FILE;
     131             :   FILE *fp;
     132             :   char buffer[256];
     133             :   char *p, *pend;
     134           0 :   int i, lnr = 0;
     135             : 
     136           0 :   fp = fopen (fname, "r");
     137           0 :   if (!fp)
     138           0 :     return;
     139             : 
     140             :   for (;;)
     141             :     {
     142           0 :       if (!fgets (buffer, sizeof buffer, fp))
     143             :         {
     144           0 :           if (!feof (fp))
     145             :             {
     146             : #ifdef HAVE_SYSLOG
     147           0 :               syslog (LOG_USER|LOG_WARNING,
     148             :                       "Libgcrypt warning: error reading '%s', line %d",
     149             :                       fname, lnr);
     150             : #endif /*HAVE_SYSLOG*/
     151             :             }
     152           0 :           fclose (fp);
     153           0 :           return;
     154             :         }
     155           0 :       lnr++;
     156           0 :       for (p=buffer; my_isascii (*p) && isspace (*p); p++)
     157             :         ;
     158           0 :       pend = strchr (p, '\n');
     159           0 :       if (pend)
     160           0 :         *pend = 0;
     161           0 :       pend = p + (*p? (strlen (p)-1):0);
     162           0 :       for ( ;pend > p; pend--)
     163           0 :         if (my_isascii (*pend) && isspace (*pend))
     164           0 :           *pend = 0;
     165           0 :       if (!*p || *p == '#')
     166           0 :         continue;
     167             : 
     168           0 :       if (_gcry_disable_hw_feature (p) == GPG_ERR_INV_NAME)
     169             :         {
     170             : #ifdef HAVE_SYSLOG
     171           0 :           syslog (LOG_USER|LOG_WARNING,
     172             :                   "Libgcrypt warning: unknown feature in '%s', line %d",
     173             :                   fname, lnr);
     174             : #endif /*HAVE_SYSLOG*/
     175             :         }
     176             :     }
     177             : }
     178             : 
     179             : 
     180             : /* Detect the available hardware features.  This function is called
     181             :    once right at startup and we assume that no other threads are
     182             :    running.  */
     183             : void
     184           0 : _gcry_detect_hw_features (void)
     185             : {
     186           0 :   hw_features = 0;
     187             : 
     188           0 :   if (fips_mode ())
     189           0 :     return; /* Hardware support is not to be evaluated.  */
     190             : 
     191           0 :   parse_hwf_deny_file ();
     192             : 
     193             : #if defined (HAVE_CPU_ARCH_X86)
     194             :   {
     195           0 :     hw_features = _gcry_hwf_detect_x86 ();
     196             :   }
     197             : #endif /* HAVE_CPU_ARCH_X86 */
     198             : #if defined (HAVE_CPU_ARCH_ARM)
     199             :   {
     200             :     hw_features = _gcry_hwf_detect_arm ();
     201             :   }
     202             : #endif /* HAVE_CPU_ARCH_ARM */
     203             : 
     204           0 :   hw_features &= ~disabled_hw_features;
     205             : }

Generated by: LCOV version 1.12