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