X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/c218159362ff2cf9e7d947ca2867123bed7a091b..351bdbb15a6542300462a9c4676d08642dac054d:/rspit.c diff --git a/rspit.c b/rspit.c index c68c11d..e471508 100644 --- a/rspit.c +++ b/rspit.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: rspit.c,v 1.5 2000/07/01 11:27:03 mdw Exp $ + * $Id$ * * Spit out random numbers * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,40 +15,18 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * Catacomb 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 Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: rspit.c,v $ - * Revision 1.5 2000/07/01 11:27:03 mdw - * Portability fix: don't assume that `stdout' is a constant expression. - * Remove old type name `bbs_param'. - * - * Revision 1.4 2000/06/17 12:08:28 mdw - * Restructure handling of cipher-based generators. Add counter-mode - * ciphers and MGF-1 hash functions. Add FIPS 140-1 and Maurer's tests. - * - * Revision 1.3 2000/02/12 18:21:03 mdw - * Overhaul of key management (again). - * - * Revision 1.2 1999/12/22 15:59:51 mdw - * New prime-search system. Read BBS keys from key files. - * - * Revision 1.1 1999/12/10 23:29:13 mdw - * Emit random numbers for statistical tests. - * - */ - /*----- Header files ------------------------------------------------------*/ #include "config.h" @@ -93,30 +71,55 @@ #include "des3-ofb.h" #include "rc2-ofb.h" #include "rc5-ofb.h" +#include "mars-ofb.h" +#include "skipjack-ofb.h" +#include "tea-ofb.h" +#include "xtea-ofb.h" #include "blowfish-ofb.h" #include "twofish-ofb.h" #include "idea-ofb.h" #include "cast128-ofb.h" #include "cast256-ofb.h" +#include "noekeon-ofb.h" #include "rijndael-ofb.h" +#include "rijndael192-ofb.h" +#include "rijndael256-ofb.h" +#include "safer-ofb.h" +#include "safersk-ofb.h" +#include "square-ofb.h" #include "serpent-ofb.h" #include "des-counter.h" #include "des3-counter.h" #include "rc2-counter.h" #include "rc5-counter.h" +#include "mars-counter.h" +#include "skipjack-counter.h" +#include "tea-counter.h" +#include "xtea-counter.h" #include "blowfish-counter.h" #include "twofish-counter.h" #include "idea-counter.h" #include "cast128-counter.h" #include "cast256-counter.h" +#include "noekeon-counter.h" #include "rijndael-counter.h" +#include "rijndael192-counter.h" +#include "rijndael256-counter.h" +#include "safer-counter.h" +#include "safersk-counter.h" +#include "square-counter.h" #include "serpent-counter.h" +#include "md2-mgf.h" #include "md4-mgf.h" #include "md5-mgf.h" #include "sha-mgf.h" +#include "tiger-mgf.h" +#include "rmd128-mgf.h" #include "rmd160-mgf.h" +#include "rmd256-mgf.h" +#include "rmd320-mgf.h" #include "rmd160.h" @@ -129,26 +132,41 @@ typedef struct gen { const char *help; } gen; -static gen generators[]; +extern gen generators[]; #define CIPHERS \ E(DES, des) \ E(DES3, des3) \ E(RC2, rc2) \ E(RC5, rc5) \ + E(MARS, mars) \ + E(SKIPJACK, skipjack) \ + E(TEA, tea) \ + E(XTEA, xtea) \ E(BLOWFISH, blowfish) \ E(TWOFISH, twofish) \ E(IDEA, idea) \ E(CAST128, cast128) \ E(CAST256, cast256) \ + E(SQUARE, square) \ + E(SAFER, safer) \ + E(SAFERSK, safersk) \ + E(NOEKEON, noekeon) \ E(RIJNDAEL, rijndael) \ + E(RIJNDAEL192, rijndael192) \ + E(RIJNDAEL256, rijndael256) \ E(SERPENT, serpent) #define HASHES \ + E(MD2, md2) \ E(MD4, md4) \ E(MD5, md5) \ E(SHA, sha) \ - E(RMD160, rmd160) + E(TIGER, tiger) \ + E(RMD128, rmd128) \ + E(RMD160, rmd160) \ + E(RMD256, rmd256) \ + E(RMD320, rmd320) #define E(PRE, pre) CIPHER_##PRE, enum { CIPHERS CIPHER__bogus }; @@ -185,18 +203,19 @@ static struct { static FILE *outfp; static size_t outsz = 0; +static unsigned maurer_lo = 5, maurer_hi = 8; static int argc; static char **argv; static unsigned flags = 0; -enum { - f_progress = 1, - f_file = 2, - f_fips = 4, - f_maurer = 8 -}; +#define f_progress 1u +#define f_file 2u +#define f_fips 4u +#define f_maurer 8u +#define f_timer 16u +#define f_discard 32u /*----- Help options ------------------------------------------------------*/ @@ -230,10 +249,12 @@ common core set:\n\ -l, --list Show a list of the supported generators, with\n\ their options.\n\ -f, --fipstest Run the FIPS 140-1 randomness test.\n\ --m, --maurer Run Maurer's universal statistical test.\n\ +-m, --maurer[=LO-HI] Run Maurer's universal statistical test.\n\ -o, --output FILE Write output to FILE, not stdout.\n\ -z, --size SIZE Emit SIZE bytes, not an unlimited number.\n\ -p, --progress Show a little progress meter (on stderr).\n\ +-T, --timer Keep track of the CPU time used by the generator.\n\ +-d, --discard Discard the generated output.\n\ \n\ (A SIZE may be followed by `g' for gigabytes, `m' for megabytes, or\n\ `k' for kilobytes. If unqualified, an amount in bytes is assumed.)\n\ @@ -254,17 +275,19 @@ static struct option opts[] = { { "list", 0, 0, 'l' }, { "fipstest", 0, 0, 'f' }, - { "maurer", 0, 0, 'm' }, + { "maurer", OPTF_ARGOPT, 0, 'm' }, { "output", OPTF_ARGREQ, 0, 'o' }, { "size", OPTF_ARGREQ, 0, 'z' }, { "progress", 0, 0, 'p' }, + { "timer", 0, 0, 'T' }, + { "discard", 0, 0, 'd' }, /* --- End of main table --- */ { 0, 0, 0, 0 } }; -static const char *sopts = "hvu lfmo:z:p"; +static const char *sopts = "hvu lfm::o:z:pTd"; #ifndef OPTION_V DA_DECL(option_v, struct option); @@ -329,6 +352,19 @@ static int opt(void) break; case 'm': flags |= f_maurer; + if (optarg) { + char *p; + unsigned long lo, hi; + lo = strtoul(optarg, &p, 0); + if (*p == '-' || *p == ',') + hi = strtoul(p + 1, &p, 0); + else + hi = lo; + if (*p != 0 || hi < lo || lo == 0) + die(EXIT_FAILURE, "bad bit range `%s'", optarg); + maurer_lo = lo; + maurer_hi = hi; + } break; case 'o': if (flags & f_file) @@ -365,6 +401,12 @@ static int opt(void) case 'p': flags |= f_progress; break; + case 'T': + flags |= f_timer; + break; + case 'd': + flags |= f_discard; + break; default: return (i); } @@ -453,7 +495,7 @@ static grand *gen_bbs(unsigned i) * The factors of this number are * * @p = 1229936431484295969649886203367009966370895964206162032259292413@ - * @7754313537966036459299022912838407755462506416274551744201653277@ + * @7754313537966036459299022912838407755462506416274551744201653277@ * @313130311731673973886822067@ * * @q = 9798171783943489959487301695884963889684294764514008432498259742@ @@ -471,7 +513,11 @@ static grand *gen_bbs(unsigned i) */ const char *mt = - "120511284390135742513572142094334711443073194119732569353820828435640527418092392240366088035509890969913081816369160298961490135716255689660470370755013177656905237112577648090277537209936078171554274553448103698084782669252936352843649980105109850503830397166360721262431179505917248447259735253684659338653"; + "12051128439013574251357214209433471144307319411973256935382082" + "84356405274180923922403660880355098909699130818163691602989614" + "90135716255689660470370755013177656905237112577648090277537209" + "93607817155427455344810369808478266925293635284364998010510985" + "0503830397166360721262431179505917248447259735253684659338653"; /* --- Other things --- */ @@ -573,12 +619,12 @@ static grand *gen_bbs(unsigned i) /* --- Read the key data --- */ - if ((kk->k.e & KF_ENCMASK) != KENC_STRUCT) + if ((kk->k->e & KF_ENCMASK) != KENC_STRUCT) die(EXIT_FAILURE, "key is not structured"); - if ((kd = key_structfind(&kk->k, "n")) == 0) + if ((kd = key_structfind(kk->k, "n")) == 0) die(EXIT_FAILURE, "key has no subkey `n'"); if ((kd->e & KF_ENCMASK) != KENC_MP) - die(EXIT_FAILURE, "incomatible subkey encoding"); + die(EXIT_FAILURE, "incompatible subkey encoding"); m = MP_COPY(kd->u.m); key_close(&kf); } else { @@ -649,7 +695,7 @@ static grand *gen_rand(unsigned i) switch (o) { case 'k': DRESET(&d); - textkey(&d, optarg, rmd160_mackeysz); + textkey(&d, optarg, rmd160_hmackeysz); r->ops->misc(r, RAND_KEY, d.buf, d.len); break; case 't': @@ -657,7 +703,7 @@ static grand *gen_rand(unsigned i) break; case 'H': DRESET(&d); - hexkey(&d, optarg, rmd160_mackeysz); + hexkey(&d, optarg, rmd160_hmackeysz); r->ops->misc(r, GRAND_SEEDBLOCK, d.buf, d.len); break; } @@ -1007,7 +1053,7 @@ static grand *gen_opts(unsigned i) /*----- Generators table --------------------------------------------------*/ -static gen generators[] = { +gen generators[] = { { "fibonacci", gen_fib, 0, "[-s SEED]" }, { "lc", gen_lc, 0, @@ -1021,12 +1067,12 @@ static gen generators[] = { { #pre "-counter", gen_counter, CIPHER_##PRE, \ "[-k KEY-PHRASE] [-H HEX-KEY] [-i HEX-IV]" }, CIPHERS -#undef E(PRE, pre) +#undef E #define E(PRE, pre) \ { #pre "-mgf", gen_mgf, HASH_##PRE, \ "[-k KEY-PHRASE] [-H HEX-KEY] [-i INDEX]" }, HASHES -#undef E(PRE, pre) +#undef E { "rc4", gen_rc4, 0, "[-k KEY-PHRASE] [-H HEX-KEY]" }, { "seal", gen_seal, 0, @@ -1042,17 +1088,177 @@ static gen generators[] = { static gen optsg = { "options", gen_opts, 0, "This message shouldn't be printed." }; -/*----- Main code ---------------------------------------------------------*/ +/*----- Random number generation ------------------------------------------*/ -int main(int ac, char *av[]) +static int genfile(const void *buf, size_t sz, void *p) { - gen *g = &optsg; - grand *r; + FILE *fp = p; + if (fwrite(buf, 1, sz, fp) != sz) + die(EXIT_FAILURE, "error writing to file: %s", strerror(errno)); + return (0); +} + +static int genbuf(const void *buf, size_t sz, void *p) +{ + octet **pp = p; + memcpy(*pp, buf, sz); + *pp += sz; + return (0); +} + +typedef struct genmaurer_ctx { + size_t n; + maurer_ctx *m; +} genmaurer_ctx; + +static int genmaurer(const void *buf, size_t sz, void *p) +{ + genmaurer_ctx *g = p; + size_t i; + + for (i = 0; i < g->n; i++) + maurer_test(&g->m[i], buf, sz); + return (0); +} + +static int generate(grand *r, size_t outsz, + int (*func)(const void *buf, size_t sz, void *p), + void *p) +{ + static char kmg[] = { ' ', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 0 }; + unsigned percent = 0; size_t kb = 0; time_t last; static char baton[] = "-\\|/"; char *bp; + int rc; + clock_t clk = 0; + + /* --- Spit out random data --- */ + + last = time(0); + bp = baton; + if (flags & f_progress) { + char *errbuf = xmalloc(BUFSIZ); + setvbuf(stderr, errbuf, _IOLBF, BUFSIZ); + if (outsz) + fprintf(stderr, "[%*s] 0%% 0\r[/\b", 50, ""); + else + fputs("[ ] 0\r[/\b", stderr); + fflush(stderr); + } + +#ifdef SIGPIPE + signal(SIGPIPE, SIG_IGN); +#endif + + do { + octet buf[BUFSIZ]; + size_t sz = sizeof(buf); + clock_t c_start, c_stop; + + /* --- Emit a bufferful (or less) of data --- */ + + if (outsz) { + if (sz > outsz - kb) + sz = outsz - kb; + } + c_start = clock(); + r->ops->fill(r, buf, sz); + c_stop = clock(); + clk += c_stop - c_start; + if (func && (rc = func(buf, sz, p)) != 0) + return (rc); + kb += sz; + + /* --- Update the display --- */ + + if (flags & f_progress) { + time_t t = time(0); + unsigned up = 0; + + if (percent > 100) + up = 1; + + if (!outsz) { + if (difftime(t, last) > 1.0) { + up = 1; + } + if (up) + fputs(" ] ", stderr); + } else { + unsigned pc = kb * 100.0 / outsz; + if (pc > percent || percent > 100 || difftime(t, last) > 1.0) { + if (percent > 100) + percent = 0; + percent &= ~1; + for (; percent < (pc & ~1); percent += 2) + putc('.', stderr); + percent = pc; + for (; pc < 100; pc += 2) + putc(' ', stderr); + fprintf(stderr, "] %3i%% ", percent); + up = 1; + } + } + + if (up) { + size_t q = kb; + char *kk = kmg; + while (q > 8192 && kk[1]) { + q >>= 10; + kk++; + } + fprintf(stderr, "%4i%c\r[", q, *kk); + if (outsz) { + unsigned pc; + for (pc = 0; pc < (percent & ~1); pc += 2) + putc('.', stderr); + } + last = t; + } + + if (percent > 100) + percent = 0; + + if (percent < 100) { + putc(*bp++, stderr); + putc('\b', stderr); + if (!*bp) + bp = baton; + } + fflush(stderr); + } + + /* --- Terminate the loop --- */ + + } while (!outsz || kb < outsz); + + if (flags & f_progress) + fputc('\n', stderr); + if (flags & f_timer) { + fprintf(stderr, "generated %lu bytes ", (unsigned long)outsz); + if (!clk) + fputs("too quickly to measure\n", stderr); + else { + char *kk; + double sec = (double)clk/CLOCKS_PER_SEC; + double bps = (outsz << 3)/sec; + for (kk = kmg; bps > 1024 && kk[1]; kk++, bps /= 1024) + ; + fprintf(stderr, "in %g secs (%g %cb/s)\n", sec, bps, *kk); + } + } + return (0); +} + +/*----- Main code ---------------------------------------------------------*/ + +int main(int ac, char *av[]) +{ + gen *g = &optsg; + grand *r; /* --- Initialize mLib --- */ @@ -1109,8 +1315,9 @@ int main(int ac, char *av[]) if (flags & f_fips) { octet buf[FIPSTEST_BUFSZ]; unsigned rc; + octet *p = buf; - r->ops->fill(r, buf, sizeof(buf)); + generate(r, sizeof(buf), genbuf, &p); rc = fipstest(buf); if (rc & FIPSTEST_MONOBIT) moan("failed monobit test"); @@ -1121,18 +1328,17 @@ int main(int ac, char *av[]) if (rc & FIPSTEST_LONGRUNS) moan("failed long runs test"); if (!rc && (flags & f_progress)) - puts("test passed"); + fputs("test passed\n", stderr); return (rc ? EXIT_FAILURE : 0); } /* --- Do Maurer's test --- */ if (flags & f_maurer) { - octet buf[250 * 1024]; + size_t bufsz; unsigned i; unsigned rc = 0; - unsigned f = 0, jj = 0; - double maxz = 0; + genmaurer_ctx g; static struct { double x; const char *sig; } sigtab[] = { { 3.2905, "1e-3" }, @@ -1142,145 +1348,54 @@ int main(int ac, char *av[]) { 0 , 0 } }; - r->ops->fill(r, buf, sizeof(buf)); - for (i = 5; i < 8; i++) { - double z = maurer(buf, sizeof(buf), i + 1); + g.n = maurer_hi - maurer_lo + 1; + g.m = xmalloc(g.n * sizeof(maurer_ctx)); + for (i = 0; i < g.n; i++) + maurer_init(&g.m[i], i + maurer_lo); + bufsz = (100 * maurer_hi) << maurer_hi; + + generate(r, bufsz, genmaurer, &g); + + for (i = maurer_lo; i <= maurer_hi; i++) { + double z = maurer_done(&g.m[i - maurer_lo]); double zz = fabs(z); unsigned j; for (j = 0; sigtab[j].sig; j++) { if (zz > sigtab[j].x) { - if (zz > fabs(maxz)) { - maxz = z; - f = i + 1; - jj = j; - } rc = EXIT_FAILURE; moan("failed, bits = %u, sig = %s, Z_u = %g", - i + 1, sigtab[j].sig, z); + i, sigtab[j].sig, z); break; } } if (flags & f_progress) - printf("bits = %u, Z_u = %g\n", i + 1, z); + fprintf(stderr, "bits = %u, Z_u = %g\n", i, z); } + xfree(g.m); return (rc); } - /* --- Make sure we don't write to the terminal --- */ + /* --- Discard --- */ + + if (flags & f_discard) { + generate(r, outsz, 0, 0); + return (0); + } + + /* --- Write to a file --- */ #ifndef PORTABLE if (!(flags & f_file) && isatty(STDOUT_FILENO)) die(EXIT_FAILURE, "writing output to a terminal is a bad idea"); #endif - /* --- Spit out random data --- */ - - last = time(0); - bp = baton; - if (flags & f_progress) { - char *errbuf = xmalloc(BUFSIZ); - setvbuf(stderr, errbuf, _IOLBF, BUFSIZ); - if (outsz) - fprintf(stderr, "[%*s] 0%% 0\r[/\b", 50, ""); - else - fputs("[ ] 0\r[/\b", stderr); - fflush(stderr); - } - -#ifdef SIGPIPE - signal(SIGPIPE, SIG_IGN); -#endif - - for (;;) { - octet buf[BUFSIZ]; - size_t sz = sizeof(buf); - - /* --- Emit a bufferful (or less) of data --- */ - - if (outsz) { - if (sz > outsz - kb) - sz = outsz - kb; - } - r->ops->fill(r, buf, sz); - if (fwrite(buf, 1, sz, outfp) != sz) { - if (flags & f_progress) - fputc('\n', stderr); - die(EXIT_FAILURE, "error writing data: %s", strerror(errno)); - } - kb += sz; - - /* --- Update the display --- */ - - if (flags & f_progress) { - time_t t = time(0); - unsigned up = 0; - - if (percent > 100) - up = 1; - - if (!outsz) { - if (difftime(t, last) > 1.0) { - up = 1; - } - if (up) - fputs(" ] ", stderr); - } else { - unsigned pc = kb * 100.0 / outsz; - if (pc > percent || percent > 100 || difftime(t, last) > 1.0) { - if (percent > 100) - percent = 0; - percent &= ~1; - for (; percent < (pc & ~1); percent += 2) - putc('.', stderr); - percent = pc; - for (; pc < 100; pc += 2) - putc(' ', stderr); - fprintf(stderr, "] %3i%% ", percent); - up = 1; - } - } - - if (up) { - size_t q = kb; - char *suff = " KMG"; - while (q > 8192 && suff[1]) { - q >>= 10; - suff++; - } - fprintf(stderr, "%4i%c\r[", q, *suff); - if (outsz) { - unsigned pc; - for (pc = 0; pc < (percent & ~1); pc += 2) - putc('.', stderr); - } - last = t; - } - - if (percent > 100) - percent = 0; - - if (percent < 100) { - putc(*bp++, stderr); - putc('\b', stderr); - if (!*bp) - bp = baton; - } - fflush(stderr); - } - - /* --- Terminate the loop --- */ - - if (outsz && kb >= outsz) - break; - } + generate(r, outsz, genfile, outfp); /* --- Done --- */ r->ops->destroy(r); - if (flags & f_progress) - fputc('\n', stderr); return (0); }