summaryrefslogtreecommitdiff
path: root/ssh-rand-helper.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2002-04-14 19:27:12 +1000
committerDamien Miller <djm@mindrot.org>2002-04-14 19:27:12 +1000
commit32e48180154a9d03fab7288fc18080acee29c7a8 (patch)
tree8c4fac41c9700f4905d343b5b7542759b61e72cc /ssh-rand-helper.c
parentfd4c9eee25e4e796b714477c3fbb0286ebe50fb7 (diff)
- (djm) ssh-rand-helper improvements
- Add commandline debugging options - Don't write binary data if stdout is a tty (use hex instead) - Give it a manpage
Diffstat (limited to 'ssh-rand-helper.c')
-rw-r--r--ssh-rand-helper.c82
1 files changed, 69 insertions, 13 deletions
diff --git a/ssh-rand-helper.c b/ssh-rand-helper.c
index 07856e5c3..8f2283ead 100644
--- a/ssh-rand-helper.c
+++ b/ssh-rand-helper.c
@@ -39,7 +39,7 @@
39#include "pathnames.h" 39#include "pathnames.h"
40#include "log.h" 40#include "log.h"
41 41
42RCSID("$Id: ssh-rand-helper.c,v 1.5 2002/02/10 07:32:30 djm Exp $"); 42RCSID("$Id: ssh-rand-helper.c,v 1.6 2002/04/14 09:27:13 djm Exp $");
43 43
44/* Number of bytes we write out */ 44/* Number of bytes we write out */
45#define OUTPUT_SEED_SIZE 48 45#define OUTPUT_SEED_SIZE 48
@@ -747,38 +747,88 @@ prng_read_commands(char *cmdfilename)
747 return cur_cmd < MIN_ENTROPY_SOURCES ? -1 : 0; 747 return cur_cmd < MIN_ENTROPY_SOURCES ? -1 : 0;
748} 748}
749 749
750void
751usage(void)
752{
753 fprintf(stderr, "Usage: %s [options]\n", __progname);
754 fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
755 fprintf(stderr, " Multiple -v increases verbosity.\n");
756 fprintf(stderr, " -x Force output in hexidecimal (for debugging)\n");
757 fprintf(stderr, " -X Force output in binary\n");
758 fprintf(stderr, " -b bytes Number of bytes to output (default %d)\n",
759 OUTPUT_SEED_SIZE);
760}
761
750int 762int
751main(int argc, char **argv) 763main(int argc, char **argv)
752{ 764{
753 unsigned char buf[OUTPUT_SEED_SIZE]; 765 unsigned char *buf;
754 int ret; 766 int ret, ch, debug_level, output_hex, bytes;
767 extern char *optarg;
768 LogLevel ll;
755 769
756 __progname = get_progname(argv[0]); 770 __progname = get_progname(argv[0]);
757 /* XXX: need some debugging mode */
758 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1); 771 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
759 772
773 ll = SYSLOG_LEVEL_INFO;
774 debug_level = output_hex = 0;
775 bytes = OUTPUT_SEED_SIZE;
776
777 /* Don't write binary data to a tty, unless we are forced to */
778 if (isatty(STDOUT_FILENO))
779 output_hex = 1;
780
781 while ((ch = getopt(argc, argv, "vxXhb:")) != -1) {
782 switch (ch) {
783 case 'v':
784 if (debug_level < 3)
785 ll = SYSLOG_LEVEL_DEBUG1 + debug_level++;
786 break;
787 case 'x':
788 output_hex = 1;
789 break;
790 case 'X':
791 output_hex = 0;
792 break;
793 case 'b':
794 if ((bytes = atoi(optarg)) <= 0)
795 fatal("Invalid number of output bytes");
796 break;
797 case 'h':
798 usage();
799 exit(0);
800 default:
801 error("Invalid commandline option");
802 usage();
803 }
804 }
805
806 log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
807
760#ifdef USE_SEED_FILES 808#ifdef USE_SEED_FILES
761 prng_read_seedfile(); 809 prng_read_seedfile();
762#endif 810#endif
763 811
812 buf = xmalloc(bytes);
813
764 /* 814 /*
765 * Seed the RNG from wherever we can 815 * Seed the RNG from wherever we can
766 */ 816 */
767 817
768 /* Take whatever is on the stack, but don't credit it */ 818 /* Take whatever is on the stack, but don't credit it */
769 RAND_add(buf, sizeof(buf), 0); 819 RAND_add(buf, bytes, 0);
770 820
771 debug("Seeded RNG with %i bytes from system calls", 821 debug("Seeded RNG with %i bytes from system calls",
772 (int)stir_from_system()); 822 (int)stir_from_system());
773 823
774#ifdef PRNGD_PORT 824#ifdef PRNGD_PORT
775 if (get_random_bytes_prngd(buf, sizeof(buf), PRNGD_PORT, NULL) == -1) 825 if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == -1)
776 fatal("Entropy collection failed"); 826 fatal("Entropy collection failed");
777 RAND_add(buf, sizeof(buf), sizeof(buf)); 827 RAND_add(buf, bytes, bytes);
778#elif defined(PRNGD_SOCKET) 828#elif defined(PRNGD_SOCKET)
779 if (get_random_bytes_prngd(buf, sizeof(buf), 0, PRNGD_SOCKET) == -1) 829 if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == -1)
780 fatal("Entropy collection failed"); 830 fatal("Entropy collection failed");
781 RAND_add(buf, sizeof(buf), sizeof(buf)); 831 RAND_add(buf, bytes, bytes);
782#else 832#else
783 /* Read in collection commands */ 833 /* Read in collection commands */
784 if (prng_read_commands(SSH_PRNG_COMMAND_FILE) == -1) 834 if (prng_read_commands(SSH_PRNG_COMMAND_FILE) == -1)
@@ -798,12 +848,18 @@ main(int argc, char **argv)
798 if (!RAND_status()) 848 if (!RAND_status())
799 fatal("Not enough entropy in RNG"); 849 fatal("Not enough entropy in RNG");
800 850
801 RAND_bytes(buf, sizeof(buf)); 851 RAND_bytes(buf, bytes);
802 852
803 ret = atomicio(write, STDOUT_FILENO, buf, sizeof(buf)); 853 if (output_hex) {
854 for(ret = 0; ret < bytes; ret++)
855 printf("%02x", (unsigned char)(buf[ret]));
856 printf("\n");
857 } else
858 ret = atomicio(write, STDOUT_FILENO, buf, bytes);
804 859
805 memset(buf, '\0', sizeof(buf)); 860 memset(buf, '\0', bytes);
861 xfree(buf);
806 862
807 return ret == sizeof(buf) ? 0 : 1; 863 return ret == bytes ? 0 : 1;
808} 864}
809 865