/* Copyright (c) 2008-2010, Dirk Krause All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Dirk Krause nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @file rndbytes.c The rndbytes program. */ /* rndbytes -b -n -a */ #include #include #if DK_HAVE_STDLIB_H #include #endif #if DK_HAVE_UNISTD_H #include #endif #if DK_HAVE_PROCESS_H #include #endif #if DK_HAVE_STRING_H #include #endif #if DK_HAVE_SIGNAL_H #include #endif #if DK_HAVE_CONIO_H #include #endif #include #include #include #include #include #include #include #include #include #include #include "dktools-version.h" #line 88 "rndbytes.ctr" /** Mode: Default mode (no output encoding). */ #define PROGRAM_MODE_DEFAULT 0 /** Mode: Convert output ASCII-Hex. */ #define PROGRAM_MODE_HEX 1 /** Mode: convert output ASCII-85. */ #define PROGRAM_MODE_ASCII85 2 /** Mode mask: Anything set? */ #define PROGRAM_MODE_SET 3 /** Action: Run normaly. */ #define ACTION_RUN 0 /** Action: Show help text. */ #define ACTION_HELP 1 /** Action: Show version information. */ #define ACTION_VERSION 2 /** Action: Save configuration to preferences. */ #define ACTION_CONFIGURE 4 /** Action: Unconfigure application. */ #define ACTION_UNCONFIGURE 8 /** Action: Show current configuration. */ #define ACTION_SHOWCONF 16 /** Length of buffer for preferences keys and values. */ #define DEFAULT_BUFFER_LENGTH 256 #ifndef GROUPNAME /** Application group name. */ #define GROUPNAME "dktools" #endif /** Application group name. */ static char grname[] = { GROUPNAME }; /** System configuration directory. */ static char etcdir[] = { DK_SYSCONFDIR }; /** Rndbytes job. */ typedef struct { int action; /**< Action to take. */ int randt; /**< PRNG types. */ int exval; /**< Returned by the main function. */ dk_app_t *a; /**< Application structure. */ char **msg; /**< The messages printed by the program. */ unsigned char *dbuf; /**< Default buffer (static). */ size_t s_dbuf; /**< Size of dbuf. */ size_t blksz; /**< Block size. */ unsigned long nblks; /**< Number of blocks. */ unsigned char *b; /**< Buffer to use. */ } CMD; /** Function returning void. */ #define P_void(fctname) static void fctname DK_P1(CMD *,c) /** Function returning int value. */ #define P_int(fctname) static int fctname DK_P1(CMD *,c) /** Version information. */ static char *version_text[] = { "", "rndbytes, version 2 (part of the dktools collection, version " VERSNUMB ")", "Copyright (C) 2002-2010 Dipl.-Ing. D. Krause", "http://dktools.sourceforge.net", "", NULL }; /** License terms. */ static char *license_terms[] = { "Redistribution and use in source and binary forms, with or without", "modification, are permitted provided that the following conditions are met:", "* Redistributions of source code must retain the above copyright notice, this", " list of conditions and the following disclaimer.", "* Redistributions in binary form must reproduce the above copyright notice,", " this list of conditions and the following disclaimer in the documentation", " and/or other materials provided with the distribution.", "* Neither the name of the Dirk Krause nor the names of other contributors may", " be used to endorse or promote products derived from this software without", " specific prior written permission.", "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"", "AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE", "IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE", "ARE DISCLAIMED.", "IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY", "DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES", "(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;", "LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND", "ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT", "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS", "SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", NULL }; /** The used libraries are listed together with version information. */ static char *libraries_used[]= { "", "Libraries used:", "", NULL }; #if DK_HAVE_OPENSSL_RAND_H /** Text added to version information. */ static char *ossl_txt[] = { "OpenSSL The OpenSSL cryptographic library", "\t http://www.openssl.org", NULL }; #endif /** Default help text, shown if the help text file is not found. */ static char *help_text[] = { "", "rndbytes - Create blocks of random bytes", "========================================", "", "createp -h", "createp --help", " shows this help text", "", "createp -v", "createp --version", " shows version information", "", "createp -c