"Fossies" - the Fresh Open Source Software Archive

Member "pidentd-3.0.19/src/ikeygen.c" (21 May 2000, 4709 Bytes) of package /linux/misc/old/pidentd-3.0.19.tar.gz:


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 "ikeygen.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** ikeygen.c - Generate a random DES key
    3 **
    4 ** Copyright (c) 1999 Peter Eriksson <pen@lysator.liu.se>
    5 **
    6 ** This program is free software; you can redistribute it and/or
    7 ** modify it as you wish - as long as you don't claim that you wrote
    8 ** it.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   13 */
   14 
   15 #include "config.h"
   16 
   17 #ifdef HAVE_LIBDES
   18 
   19 #include <stdio.h>
   20 #include <stdlib.h>
   21 #include <fcntl.h>
   22 
   23 #ifdef HAVE_UNISTD_H
   24 #include <unistd.h>
   25 #endif
   26 
   27 #ifdef HAVE_RAND_H
   28 #include <rand.h>
   29 #elif HAVE_OPENSSL_RAND_H
   30 #include <openssl/rand.h>
   31 #endif
   32 
   33 #include "pidentd.h"
   34 
   35 static int verbose = 0;
   36 static char *keyfile_path = PATH_KEYFILE;
   37 static int crypto_v0_bugfix = 1;
   38 
   39 static void
   40 read_dev_random(unsigned char *keybuf, int length)
   41 {
   42   int dev_random;
   43   int bytes_read = 0;
   44   int total_bytes_read = 0;
   45   int bytes_to_read = length;
   46   unsigned char *index;
   47 
   48   if ( (dev_random = open("/dev/random", O_RDONLY)) == -1 ) {
   49     perror("unable to read /dev/random");
   50     exit(1);
   51   }
   52   while (index < keybuf + length) {
   53     bytes_read = read(dev_random, keybuf, bytes_to_read);
   54     if (bytes_read < 0) {
   55       perror("error reading /dev/random");
   56       exit(1);
   57     } else {
   58       index += bytes_read;
   59       bytes_to_read -= bytes_read;
   60       total_bytes_read += bytes_read;
   61       if (verbose) 
   62     printf("Got %d bytes from /dev/random (%d out of %d done).\n",
   63            bytes_read, total_bytes_read, length);
   64     }
   65   }
   66 }
   67 
   68 static void
   69 make_random_key(unsigned char *keybuf)
   70 {
   71     int i;
   72     
   73 #ifdef HAVE_RAND_BYTES
   74     RAND_bytes(keybuf, 1024);
   75 #elif HAVE_DEV_RANDOM
   76     read_dev_random(keybuf, 1024);
   77 #else
   78 
   79     /* This is a BAD BAD BAD key generator */
   80     for (i = 0; i < 1024; i++)
   81     keybuf[i] = random() & 0xFF;
   82 #endif
   83 
   84     if (crypto_v0_bugfix)
   85     {
   86     /* Yuk! The code assumes that the key buf contains a NUL
   87        terminated string! */
   88     for (i = 0; i < 1024; i++)
   89         while (keybuf[i] == 0)
   90         keybuf[i] = random() & 0xFF;
   91     }
   92 }
   93 
   94 
   95 static void
   96 usage(FILE *fp)
   97 {
   98     fputs("Usage: ikeygen [-h] [-V] [-v] [-iFILE] [KEYFILE]\n", fp);
   99     fputs("\t-h\tDisplay this information.\n", fp);
  100     fputs("\t-V\tPrint version and build information.\n", fp);
  101     fputs("\t-v\tBe verbose.\n", fp);
  102     fputs("\t-iFILE\tFile to copy DES key from.\n", fp);
  103 }
  104 
  105 void
  106 program_header(FILE *fp)
  107 {
  108     fprintf(fp, "[Ikeygen, version %s - %s %s]\n",
  109         server_version, __DATE__, __TIME__);
  110 }
  111     
  112 
  113 int
  114 main(int argc,
  115      char *argv[])
  116 {
  117     int i, res;
  118     int keyfile_fd, fd;
  119     unsigned char key[1024];
  120     char *input_key_file = NULL;
  121     off_t keyfile_end_pos = 0;
  122     
  123     
  124     for (i = 1; i < argc && argv[i][0] == '-'; i++)
  125     switch (argv[i][1])
  126     {
  127       case 'V':
  128         program_header(stdout);
  129         exit(EXIT_SUCCESS);
  130         
  131       case 'i':
  132         input_key_file = argv[i]+2;
  133         break;
  134         
  135       case 'v':
  136         program_header(stdout);
  137         verbose = 1;
  138         break;
  139         
  140       case 'h':
  141         usage(stdout);
  142         exit(0);
  143 
  144       default:
  145         usage(stderr);
  146         exit(1);
  147     }
  148 
  149     if (i < argc)
  150     keyfile_path = argv[i++];
  151     
  152     if (verbose)
  153     printf("Using key file: %s\n", keyfile_path);
  154     
  155     if (input_key_file)
  156     {
  157     if (verbose)
  158         printf("Copying 1024 byte key from: %s\n", input_key_file);
  159     
  160     fd = open(input_key_file, O_RDONLY);
  161     if (fd < 0)
  162     {
  163         fprintf(stderr, "open: ");
  164         perror(input_key_file);
  165         exit(1);
  166     }
  167     
  168     res = read(fd, key, sizeof(key));
  169     if (res < 0)
  170     {
  171         perror("read(input_key_file)");
  172         exit(1);
  173     }
  174     if (res != sizeof(key))
  175     {
  176         fprintf(stderr, "read(input_key_file): too short\n");
  177         exit(1);
  178     }
  179     close(fd);
  180     }
  181     else
  182     {
  183     if (verbose)
  184         printf("Making random input key\n");
  185     
  186     make_random_key(key);
  187     }
  188 
  189     keyfile_fd = open(keyfile_path, O_RDWR|O_CREAT, 0600);
  190     if (keyfile_fd < 0)
  191     {
  192     fprintf(stderr, "ikeygen: open: ");
  193     perror(keyfile_path);
  194     exit(1);
  195     }
  196 
  197     keyfile_end_pos = lseek(keyfile_fd, 0, SEEK_END);
  198     if (keyfile_end_pos % 1024 != 0)
  199     {
  200     fprintf(stderr, "ikeygen: key file is corrupt\n");
  201     exit(1);
  202     }
  203     
  204     res = write(keyfile_fd, key, sizeof(key));
  205     if (res < 0)
  206     {
  207     perror("ikeygen: write(keyfile)");
  208     close(keyfile_fd);
  209     exit(1);
  210     }
  211     if (res != sizeof(key))
  212     {
  213 #ifdef HAVE_FTRUNCATE
  214     ftruncate(keyfile_fd, keyfile_end_pos);
  215 #endif
  216     fprintf(stderr,
  217         "ikeygen: write(keyfile): could not write a complete key\n");
  218     
  219     close(keyfile_fd);
  220     exit(1);
  221     }
  222     
  223     close(keyfile_fd);
  224     
  225     if (verbose)
  226     printf("Key file now contains %lu keys\n",
  227            ((unsigned long) keyfile_end_pos / 1024)+1);
  228     exit(0);
  229 }
  230 
  231 #else
  232 
  233 #error This program needs a usable DES library
  234 
  235 #endif