"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-crypt.c" (15 Mar 2019, 2758 Bytes) of package /linux/privat/stress-ng-0.09.56.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 last Fossies "Diffs" side-by-side code changes report: 0.09.50_vs_0.09.51.

    1 /*
    2  * Copyright (C) 2013-2019 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 #undef _XOPEN_SOURCE
   28 #define _XOPEN_SOURCE   600
   29 
   30 #if defined(HAVE_LIB_CRYPT) &&  \
   31     defined(HAVE_CRYPT_H)
   32 
   33 /*
   34  *  stress_crypt_id()
   35  *  crypt a password with given seed and id
   36  */
   37 static int stress_crypt_id(
   38     const args_t *args,
   39     const char id,
   40     const char *method,
   41     const char *passwd,
   42     char *salt)
   43 {
   44     salt[1] = id;
   45     char *crypted;
   46 #if defined (HAVE_CRYPT_R)
   47     struct crypt_data data;
   48 
   49     (void)memset(&data, 0, sizeof(data));
   50     crypted = crypt_r(passwd, salt, &data);
   51 #else
   52     crypted = crypt(passwd, salt);
   53 #endif
   54     if (!crypted) {
   55         pr_fail("%s: cannot encrypt with %s", args->name, method);
   56         return -1;
   57     }
   58     return 0;
   59 }
   60 
   61 /*
   62  *  stress_crypt()
   63  *  stress libc crypt
   64  */
   65 static int stress_crypt(const args_t *args)
   66 {
   67     do {
   68         static const char seedchars[] =
   69             "./0123456789ABCDEFGHIJKLMNOPQRST"
   70             "UVWXYZabcdefghijklmnopqrstuvwxyz";
   71         char passwd[16];
   72         char salt[] = "$x$........";
   73         uint64_t seed[2];
   74         size_t i;
   75 
   76         seed[0] = mwc64();
   77         seed[1] = mwc64();
   78 
   79         for (i = 0; i < 8; i++)
   80             salt[i + 3] = seedchars[(seed[i / 5] >> (i % 5) * 6) & 0x3f];
   81         for (i = 0; i < sizeof(passwd) - 1; i++)
   82             passwd[i] = seedchars[mwc32() % sizeof(seedchars)];
   83         passwd[i] = '\0';
   84 
   85         if (stress_crypt_id(args, '1', "MD5", passwd, salt) < 0)
   86             break;
   87 #if NEED_GLIBC(2,7,0)
   88         if (stress_crypt_id(args, '5', "SHA-256", passwd, salt) < 0)
   89             break;
   90         if (stress_crypt_id(args, '6', "SHA-512", passwd, salt) < 0)
   91             break;
   92 #endif
   93         inc_counter(args);
   94     } while (keep_stressing());
   95 
   96     return EXIT_SUCCESS;
   97 }
   98 
   99 stressor_info_t stress_crypt_info = {
  100     .stressor = stress_crypt,
  101     .class = CLASS_CPU
  102 };
  103 #else
  104 stressor_info_t stress_crypt_info = {
  105     .stressor = stress_not_implemented,
  106     .class = CLASS_CPU
  107 };
  108 #endif