"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.13.05/stress-crypt.c" (11 Oct 2021, 3618 Bytes) of package /linux/privat/stress-ng-0.13.05.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stress-crypt.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.13.04_vs_0.13.05.

    1 /*
    2  * Copyright (C) 2013-2021 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 static const stress_help_t help[] = {
   28     { NULL, "crypt N",  "start N workers performing password encryption" },
   29     { NULL, "crypt-ops N",  "stop after N bogo crypt operations" },
   30     { NULL, NULL,       NULL }
   31 };
   32 
   33 #if defined(HAVE_LIB_CRYPT) &&  \
   34     defined(HAVE_CRYPT_H)
   35 
   36 typedef struct {
   37     const char id;
   38     const char *method;
   39 } crypt_method_t;
   40 
   41 static const crypt_method_t crypt_methods[] = {
   42     { '1', "MD5" },
   43     { '5', "SHA-256" },
   44     { '6', "SHA-512" },
   45     { '7', "scrypt" },
   46     { '3', "NT" },
   47     { 'y', "yescrypt" },
   48 };
   49 
   50 /*
   51  *  stress_crypt_id()
   52  *  crypt a password with given seed and id
   53  */
   54 static int stress_crypt_id(
   55     const stress_args_t *args,
   56     const char id,
   57     const char *method,
   58     const char *passwd,
   59     char *salt)
   60 {
   61     salt[1] = id;
   62     char *encrypted;
   63 #if defined (HAVE_CRYPT_R)
   64     static struct crypt_data data;
   65 
   66     (void)memset(&data, 0, sizeof(data));
   67     errno = 0;
   68     encrypted = crypt_r(passwd, salt, &data);
   69 #else
   70     encrypted = crypt(passwd, salt);
   71 #endif
   72     if (!encrypted) {
   73         switch (errno) {
   74         case 0:
   75             break;
   76         case EINVAL:
   77             break;
   78 #if defined(ENOSYS)
   79         case ENOSYS:
   80             break;
   81 #endif
   82 #if defined(EOPNOTSUPP)
   83         case EOPNOTSUPP:
   84 #endif
   85             break;
   86         default:
   87             pr_fail("%s: cannot encrypt with %s, errno=%d (%s)\n",
   88                 args->name, method, errno, strerror(errno));
   89             return -1;
   90         }
   91     }
   92     return 0;
   93 }
   94 
   95 /*
   96  *  stress_crypt()
   97  *  stress libc crypt
   98  */
   99 static int stress_crypt(const stress_args_t *args)
  100 {
  101     stress_set_proc_state(args->name, STRESS_STATE_RUN);
  102 
  103     do {
  104         static const char seedchars[] =
  105             "./0123456789ABCDEFGHIJKLMNOPQRST"
  106             "UVWXYZabcdefghijklmnopqrstuvwxyz";
  107         char passwd[16];
  108         char salt[] = "$x$........";
  109         uint64_t seed[2];
  110         size_t i, failed = 0;
  111 
  112         seed[0] = stress_mwc64();
  113         seed[1] = stress_mwc64();
  114 
  115         for (i = 0; i < 8; i++)
  116             salt[i + 3] = seedchars[(seed[i / 5] >> (i % 5) * 6) & 0x3f];
  117         for (i = 0; i < sizeof(passwd) - 1; i++)
  118             passwd[i] = seedchars[stress_mwc32() % sizeof(seedchars)];
  119         passwd[i] = '\0';
  120 
  121         for (i = 0; i < SIZEOF_ARRAY(crypt_methods); i++) {
  122             int ret;
  123 
  124             ret = stress_crypt_id(args,
  125                           crypt_methods[i].id,
  126                           crypt_methods[i].method,
  127                           passwd, salt);
  128             if (ret < 0)
  129                 failed++;
  130         }
  131         if (failed)
  132             break;
  133         inc_counter(args);
  134     } while (keep_stressing(args));
  135 
  136     stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
  137 
  138     return EXIT_SUCCESS;
  139 }
  140 
  141 stressor_info_t stress_crypt_info = {
  142     .stressor = stress_crypt,
  143     .class = CLASS_CPU,
  144     .help = help
  145 };
  146 #else
  147 stressor_info_t stress_crypt_info = {
  148     .stressor = stress_not_implemented,
  149     .class = CLASS_CPU,
  150     .help = help
  151 };
  152 #endif