aboutsummaryrefslogtreecommitdiffstats
path: root/programs/ranbits
diff options
context:
space:
mode:
Diffstat (limited to 'programs/ranbits')
-rw-r--r--programs/ranbits/.cvsignore1
-rw-r--r--programs/ranbits/Makefile39
-rw-r--r--programs/ranbits/ranbits.877
-rw-r--r--programs/ranbits/ranbits.c146
4 files changed, 263 insertions, 0 deletions
diff --git a/programs/ranbits/.cvsignore b/programs/ranbits/.cvsignore
new file mode 100644
index 000000000..910103faa
--- /dev/null
+++ b/programs/ranbits/.cvsignore
@@ -0,0 +1 @@
+ranbits
diff --git a/programs/ranbits/Makefile b/programs/ranbits/Makefile
new file mode 100644
index 000000000..558318e8e
--- /dev/null
+++ b/programs/ranbits/Makefile
@@ -0,0 +1,39 @@
+# Makefile for miscelaneous programs
+# Copyright (C) 2002 Michael Richardson <mcr@freeswan.org>
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+#
+# 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. See the GNU General Public License
+# for more details.
+#
+# RCSID $Id: Makefile,v 1.1 2004/03/15 20:35:30 as Exp $
+
+FREESWANSRCDIR=../..
+include ${FREESWANSRCDIR}/Makefile.inc
+
+PROGRAM=ranbits
+LIBS=${FREESWANLIB}
+
+include ../Makefile.program
+
+#
+# $Log: Makefile,v $
+# Revision 1.1 2004/03/15 20:35:30 as
+# added files from freeswan-2.04-x509-1.5.3
+#
+# Revision 1.2 2002/06/02 21:51:41 mcr
+# changed TOPDIR->FREESWANSRCDIR in all Makefiles.
+# (note that linux/net/ipsec/Makefile uses TOPDIR because this is the
+# kernel sense.)
+#
+# Revision 1.1 2002/04/24 07:55:32 mcr
+# #include patches and Makefiles for post-reorg compilation.
+#
+#
+#
+
diff --git a/programs/ranbits/ranbits.8 b/programs/ranbits/ranbits.8
new file mode 100644
index 000000000..5a99a088f
--- /dev/null
+++ b/programs/ranbits/ranbits.8
@@ -0,0 +1,77 @@
+.TH IPSEC_RANBITS 8 "22 Aug 2000"
+.\" RCSID $Id: ranbits.8,v 1.1 2004/03/15 20:35:30 as Exp $
+.SH NAME
+ipsec ranbits \- generate random bits in ASCII form
+.SH SYNOPSIS
+.B ipsec
+.B ranbits
+[
+.B \-\-quick
+] [
+.B \-\-continuous
+] [
+.B \-\-bytes
+] nbits
+.SH DESCRIPTION
+.I Ranbits
+obtains
+.I nbits
+(rounded up to the nearest byte)
+high-quality random bits from
+.IR random (4),
+and emits them on standard output as an ASCII string.
+The default output format is
+.IR datatot (3)
+.B h
+format:
+lowercase hexadecimal with a
+.B 0x
+prefix and an underscore every 32 bits.
+.PP
+The
+.B \-\-quick
+option produces quick-and-dirty random bits:
+instead of using the high-quality random bits from
+.IR /dev/random ,
+which may take some time to supply the necessary bits if
+.I nbits
+is large,
+.I ranbits
+uses
+.IR /dev/urandom ,
+which yields prompt results but lower-quality randomness.
+.PP
+The
+.B \-\-continuous
+option uses
+.IR datatot (3)
+.B x
+output format, like
+.B h
+but without the underscores.
+.PP
+The
+.B \-\-bytes
+option causes
+.I nbits
+to be interpreted as a byte count rather than a bit count.
+.SH FILES
+/dev/random, /dev/urandom
+.SH SEE ALSO
+ipsec_datatot(3), random(4)
+.SH HISTORY
+Written for the Linux FreeS/WAN project
+<http://www.freeswan.org>
+by Henry Spencer.
+.SH BUGS
+There is an internal limit on
+.IR nbits ,
+currently 20000.
+.PP
+Without
+.BR \-\-quick ,
+.IR ranbits 's
+run time is difficult to predict.
+A request for a large number of bits,
+at a time when the system's entropy pool is low on randomness,
+may take quite a while to satisfy.
diff --git a/programs/ranbits/ranbits.c b/programs/ranbits/ranbits.c
new file mode 100644
index 000000000..7b9a0f76e
--- /dev/null
+++ b/programs/ranbits/ranbits.c
@@ -0,0 +1,146 @@
+/*
+ * random bit generation for scripts, control files, etc.
+ * Copyright (C) 1998, 1999, 2000 Henry Spencer.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * 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. See the GNU General Public License
+ * for more details.
+ *
+ * RCSID $Id: ranbits.c,v 1.1 2004/03/15 20:35:30 as Exp $
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <limits.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <freeswan.h>
+
+#ifndef DEVICE
+#define DEVICE "/dev/random"
+#endif
+#ifndef QDEVICE
+#define QDEVICE "/dev/urandom"
+#endif
+#ifndef MAXBITS
+#define MAXBITS 20000
+#endif
+
+char usage[] = "Usage: ranbits [--quick] [--continuous] [--bytes] nbits";
+struct option opts[] = {
+ {"quick", 0, NULL, 'q',},
+ {"continuous", 0, NULL, 'c',},
+ {"bytes", 0, NULL, 'b',},
+ {"help", 0, NULL, 'h',},
+ {"version", 0, NULL, 'v',},
+ {0, 0, NULL, 0,}
+};
+int quick = 0; /* quick and dirty? */
+char format = 'h'; /* datatot() format code */
+int isbytes = 0; /* byte count rather than bits? */
+
+char me[] = "ipsec ranbits"; /* for messages */
+
+char buf[MAXBITS/CHAR_BIT];
+char outbuf[3*sizeof(buf)];
+
+int main(int argc, char *argv[])
+{
+ int opt;
+ extern int optind;
+ int errflg = 0;
+ int nbits;
+ size_t nbytes;
+ char *devname;
+ int dev;
+ size_t ndone;
+ size_t nneeded;
+ ssize_t got;
+
+ while ((opt = getopt_long(argc, argv, "", opts, NULL)) != EOF)
+ switch (opt) {
+ case 'q': /* quick and dirty randomness */
+ quick = 1;
+ break;
+ case 'c': /* continuous hex, no underscores */
+ format = 'x';
+ break;
+ case 'b': /* byte count, not bit count */
+ isbytes = 1;
+ break;
+ case 'h': /* help */
+ printf("%s\n", usage);
+ exit(0);
+ break;
+ case 'v': /* version */
+ printf("%s %s\n", me, ipsec_version_code());
+ exit(0);
+ break;
+ case '?':
+ default:
+ errflg = 1;
+ break;
+ }
+ if (errflg || optind != argc-1) {
+ fprintf(stderr, "%s\n", usage);
+ exit(2);
+ }
+
+ nbits = atoi(argv[optind]);
+ if (isbytes)
+ nbits *= CHAR_BIT;
+ if (nbits <= 0) {
+ fprintf(stderr, "%s: invalid bit count (%d)\n", me, nbits);
+ exit(1);
+ }
+ if (nbits > MAXBITS) {
+ fprintf(stderr, "%s: overlarge bit count (max %d)\n", me,
+ MAXBITS);
+ exit(1);
+ }
+ nbytes = (size_t)(nbits + CHAR_BIT - 1) / CHAR_BIT;
+
+ devname = (quick) ? QDEVICE : DEVICE;
+ dev = open(devname, 0);
+ if (dev < 0) {
+ fprintf(stderr, "%s: could not open %s (%s)\n", me,
+ devname, strerror(errno));
+ exit(1);
+ }
+
+ ndone = 0;
+ while (ndone < nbytes) {
+ got = read(dev, buf + ndone, nbytes - ndone);
+ if (got < 0) {
+ fprintf(stderr, "%s: read error on %s (%s)\n", me,
+ devname, strerror(errno));
+ exit(1);
+ }
+ if (got == 0) {
+ fprintf(stderr, "%s: eof on %s!?!\n", me, devname);
+ exit(1);
+ }
+ ndone += got;
+ }
+
+ nneeded = datatot(buf, nbytes, format, outbuf, sizeof(outbuf));
+ if (nneeded > sizeof(outbuf)) {
+ fprintf(stderr, "%s: buffer overflow (need %ld bytes)?!?\n",
+ me, (long)nneeded);
+ exit(1);
+ }
+ printf("%s\n", outbuf);
+ exit(0);
+}