Blame SOURCES/keyrand.c

b76ddc
/* 
b76ddc
   keyrand implementation using /dev/random
b76ddc
   Copyright (C) 2006 Red Hat, Inc.
b76ddc
b76ddc
   This program is free software; you can redistribute it and/or modify
b76ddc
   it under the terms of the GNU General Public License as published by
b76ddc
   the Free Software Foundation; either version 2 of the License, or
b76ddc
   (at your option) any later version.
b76ddc
  
b76ddc
   This program is distributed in the hope that it will be useful,
b76ddc
   but WITHOUT ANY WARRANTY; without even the implied warranty of
b76ddc
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b76ddc
   GNU General Public License for more details.
b76ddc
  
b76ddc
   You should have received a copy of the GNU General Public License
b76ddc
   along with this program; if not, write to the Free Software
b76ddc
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
b76ddc
b76ddc
*/
b76ddc
b76ddc
#include <sys/types.h>
b76ddc
b76ddc
#include <unistd.h>
b76ddc
#include <string.h>
b76ddc
#include <stdio.h>
b76ddc
#include <fcntl.h>
b76ddc
#include <errno.h>
b76ddc
#include <stdlib.h>
b76ddc
b76ddc
#include <slang/slang.h>
b76ddc
#include <newt.h>
b76ddc
b76ddc
static void collect_bytes(int fd, char *buffer, int total)
b76ddc
{
b76ddc
    int count;
b76ddc
    newtComponent title, form, scale;
b76ddc
    char message[1024];
b76ddc
    newtGrid box;
b76ddc
b76ddc
    box = newtCreateGrid(1, 3);
b76ddc
b76ddc
    snprintf(message, sizeof message,
b76ddc
             "To generate %u random bits from the "
b76ddc
             "kernel random number generator, some "
b76ddc
             "keyboard or mouse input may be necessary at the "
b76ddc
             "console for this host.  Please try entering "
b76ddc
             "some random text or moving the mouse, if "
b76ddc
             "running this program locally.", total * 8);
b76ddc
    
b76ddc
    title = newtTextboxReflowed(1, 1, message, 60, 10, 0, 0);
b76ddc
    
b76ddc
    newtGridSetField(box, 0, 0, NEWT_GRID_COMPONENT, title,
b76ddc
		     0, 0, 0, 0, 0, 0);
b76ddc
    
b76ddc
    /* The progress bar */
b76ddc
    scale = newtScale(0, 0, 30, total);
b76ddc
    newtScaleSet(scale, 0);
b76ddc
    
b76ddc
    newtGridSetField(box, 0, 1, NEWT_GRID_COMPONENT, scale,
b76ddc
		     0, 1, 0, 0, 0, 0);
b76ddc
b76ddc
    form = newtForm(NULL, NULL, 0);
b76ddc
    newtGridAddComponentsToForm(box, form, 1);
b76ddc
    
b76ddc
    newtGridWrappedWindow(box, "Collecting random data");
b76ddc
    
b76ddc
    newtDrawForm(form);
b76ddc
b76ddc
    count = 0;
b76ddc
b76ddc
    do {
b76ddc
        ssize_t rv;
b76ddc
        
b76ddc
        newtScaleSet(scale, count);
b76ddc
        newtRefresh();
b76ddc
b76ddc
        rv = read(fd, buffer + count, total - count);
b76ddc
        if (rv == -1 && errno == EINTR) continue;
b76ddc
        else if (rv < 0) {
b76ddc
            newtWinMessage("Error", "Exit", 
b76ddc
                           "Error reading from /dev/random");
b76ddc
            newtFinished();
b76ddc
            exit(1);
b76ddc
        }
b76ddc
b76ddc
	SLang_flush_input();
b76ddc
        count += rv;
b76ddc
    } while (count < total);
b76ddc
b76ddc
    newtFormDestroy(form);
b76ddc
}
b76ddc
    
b76ddc
b76ddc
int main(int argc, char **argv)
b76ddc
{
b76ddc
    const char *output;
b76ddc
    int bits, bytes, fd, rfd;
b76ddc
    char *buffer;
b76ddc
b76ddc
    if (argc < 3) {
b76ddc
        fprintf(stderr, "Usage: keyrand <number-of-bits> <output-file>\n");
b76ddc
        exit(1);
b76ddc
    }
b76ddc
    
b76ddc
    bits = atoi(argv[1]);
b76ddc
    output = argv[2];
b76ddc
    fd = open(output, O_APPEND|O_WRONLY);
b76ddc
    rfd = open("/dev/random", O_RDONLY);
b76ddc
    
b76ddc
    newtInit();
b76ddc
    newtCls();
b76ddc
    
b76ddc
    newtDrawRootText(0, 0, 
b76ddc
                     "Red Hat Keypair Generation (c) 2006 Red Hat, Inc.");
b76ddc
    
b76ddc
    if (fd < 0) {
b76ddc
        newtWinMessage("Error", "Exit", "Could not open output file");
b76ddc
        newtFinished();
b76ddc
        exit(1);
b76ddc
    }
b76ddc
    else if (rfd < 0) {
b76ddc
        newtWinMessage("Error", "Exit", "Could not open /dev/random");
b76ddc
        newtFinished();
b76ddc
        exit(1);
b76ddc
    }
b76ddc
    else if (bits < 8 || bits > 800 * 1024) {
b76ddc
        newtWinMessage("Error", "Exit", "More than 8 bits must be requested");
b76ddc
        newtFinished();
b76ddc
        exit(1);
b76ddc
    }
b76ddc
    
b76ddc
    bytes = bits / 8;
b76ddc
    buffer = malloc(bytes);
b76ddc
    sleep(1);
b76ddc
b76ddc
    collect_bytes(rfd, buffer, bytes);
b76ddc
    
b76ddc
    if (write(fd, buffer, bytes) != bytes || close(fd)) {
b76ddc
        newtWinMessage("Error", "Exit", "Error writing to random file");
b76ddc
        newtFinished();
b76ddc
        exit(1);
b76ddc
    }
b76ddc
b76ddc
    newtFinished();
b76ddc
    
b76ddc
    newtRefresh();
b76ddc
b76ddc
    sleep(1);
b76ddc
    newtPopWindow();
b76ddc
    SLang_flush_input();
b76ddc
    newtClearKeyBuffer();
b76ddc
b76ddc
    return 0;
b76ddc
}
b76ddc