/* * TEA Total Copyright (c) Alex Holden 2000, 2001. * * This code is public domain; you can do whatever you want with it, though I * would prefer you to contribute any bug fixes back to me if possible. * This software comes with NO WARRANTY WHATSOEVER, not even the implied * warranties of merchantability and fitness for a particular purpose. * * randdev.c: Generates a random key using /dev/random or equivalent. */ #include #include #include "teatotal.h" #include "tea-kgen.h" #define TEA_KGEN_MSG "If tea-kgen pauses, mash keyboard or move " \ "mouse to generate more randomness.\n" /* * Generate the key data by simply reading 16 bytes from the system's * /dev/random or equivalent. */ void generate_key(teastate *state) { int i, fd; /* Open the random number generator device */ if(!(fd = open(RANDOM_DEVICE, O_RDONLY))) die("Couldn't open random device\n"); /* Print a warning message about generating randomness if necessary */ safe_write(STDERR_FILENO, TEA_KGEN_MSG, sizeof(TEA_KGEN_MSG)); /* Read 16 bytes (128 bits) of data from it */ if((i = safe_read(fd, state->k, 16)) == -1) die("Couldn't read from random device\n"); if(i != 16) die("Failed to read enough data from random device\n"); /* Close the random device */ close(fd); }