summaryrefslogtreecommitdiff
path: root/main.c
blob: fe63404a4a7753447e0f2d8d122affce9909c3cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "types.h"
#include "rmd160.h"
#include "bigdeal.h"
#include "mp.h"
#include "binomial.h"
#include "output.h"
#include "os.h"
#include "collect.h"

static char rcsid[] = "$Header: /home/sater/bridge/bigdeal/RCS/main.c,v 1.26 2000/09/08 05:13:44 sater Exp $";

#define MESSLEN		100			/* Length of input buffer(s) */
#define ENTROPY_TO_COLLECT	RMDsize*4/3	/* 33% extra safety/paranoia */

progparams_t parameters;	/* Program parameters go in here */
FILE *flog;			/* Not used in safe version */

static int
readline(FILE *ifile, char *buf, int len)
/*
 * Read line into buf
 * Check for length, discard trailing \n
 */
{
	char *eol;

	if(fgets(buf, len, ifile) == NULL)
		return 0;
	eol = strchr(buf, '\n');
	if (eol)
		*eol = 0;
	return 1;
}

/*
 * Section that handles initialization file
 * Currently in use for hand format(s)
 *
 * File contains keyword=value lines
 * The init_file[] array contains default values, overwritten by file
 */

static char init_header[] =	"[BigDeal]";

char default_formats[MESSLEN] = "dup,pbn";
char default_askformats[MESSLEN] = "no";
char default_owner[MESSLEN] = "Identification of owner or tournament";

#define LONGESTKEYWORD		20	/* Extra bytes to read for keyword and = */
struct ifrecord {
	char *if_keyword;
	char *if_value;
} init_file[] = {
	{ "formats", default_formats },
	{ "askformats", default_askformats },
	{ "owner", default_owner },
	{ 0 }
};

void
write_init_file(char *ifname)
/*
 * Prompt for defaults, and write init_file
 */
{
	FILE *ifile;
	char buf1[MESSLEN], buf2[MESSLEN];
	struct ifrecord *ifrp;

	printf("This program can generate various hand formats, one or more per run:\n\n");
	output_help();
	do {
		printf("Give comma separated list of formats usually desired: [%s] ",
			default_formats);
		readline(stdin, buf1, MESSLEN);
		if (buf1[0] == 0)
			strcpy(buf1, default_formats);
		strcpy(buf2, buf1);
	} while (output_specify_formats(buf2, 0)==0);
	strcpy(default_formats, buf1);

	printf("\nNormally you will always use the format(s) just specified,\n");
	printf("but maybe you would like to change it for some runs.\n");
	printf("Do you want the program to reconfirm the format every run? [%s] ", default_askformats);
	readline(stdin, buf1, MESSLEN);
	if (buf1[0])
		strcpy(default_askformats, buf1[0] == 'y' ? "yes" : "no");

	printf("\nIf you give an identification string the program will ensure\n");
	printf("that nobody with a different identification can generate the\n");
	printf("same sets of deals as you\n");
	printf("identication? [%s]\n? ", default_owner);
	readline(stdin, buf1, MESSLEN);
	if (buf1[0])
		strcpy(default_owner, buf1);

	ifile = fopen(ifname, "w");
	fprintf(ifile, "%s\n", init_header);
	for (ifrp=init_file; ifrp->if_keyword; ifrp++) {
		fprintf(ifile, "%s=%s\n", ifrp->if_keyword, ifrp->if_value);
	}
	fclose(ifile);
}

void
read_init_file(char *ifname, int write_when_absent)
/*
 * Read init file
 */
{
	FILE *ifile;
	char buf[MESSLEN+LONGESTKEYWORD];
	char *eqptr;
	struct ifrecord *ifrp;

	ifile = fopen(ifname, "r");
	if (ifile == NULL) {
		if (write_when_absent)
			write_init_file(ifname);
		return;
	}
	while (readline(ifile, buf, MESSLEN+LONGESTKEYWORD)) {
		if (buf[0] == 0)
			continue;		/* empty line */
		if (strcmp(buf, init_header)==0)
			continue;		/* header for Windows routines */
		if (buf[0] == '[')
			break;			/* end of our stuff */
		eqptr = strchr(buf, '=');
		if (eqptr == 0) {
			fprintf(stderr, "Line '%s' does not contain =\n", buf);
			fprintf(stderr, "Suggest rerun program with -R flag\n");
			continue;
		}
		*eqptr++ = 0;
		for (ifrp=init_file; ifrp->if_keyword; ifrp++) {
			if (strcmp(ifrp->if_keyword, buf)==0) {
				strcpy(ifrp->if_value, eqptr);
				break;
			}
		}
		if (!ifrp->if_keyword) {
			fprintf(stderr, "Keyword %s in init_file unknown\n", buf);
			fprintf(stderr, "Suggest rerun program with -R flag\n");
		}
	}
	fclose(ifile);
}

#ifndef BIGDEALX
/*
 * All parameters for the program when it is running in binary or safe mode
 * In this mode the program should be as idiot proof as possible
 */
#define OPTION_STRING	"f:n:p:R"
#define USAGE_STRING	"[-n number-of-deals] [-p outputfile-prefix] [-f output-format-list] [-R(re-init)]"
#define MAXDEALS	100	/* No more than 100 deals in standard prog */
#define VERSION_COMMENT	""

#else /* BIGDEALX */
/*
 * Parameters for the other mode. This is the hacker mode where the user
 * can tinkle with entropy and other operations that increase the likelyhood
 * of the program generating something else than a random, never occurred
 * before set of deals.
 */
#define OPTION_STRING	"e:E:f:h:n:op:R"
#define USAGE_STRING	"[-n nr-of-deals] [-p ofile-prefx] [-f o-format-list] [-e entropy-str] [-E entropy-file] [-o(only entropy from command line)] [-h hash] [-R(re-init)]"
#define MAXDEALS	1000000000
#define VERSION_COMMENT "(Extended version, not recommended for tournament use)"

#define HISTNAME "dealentr.txt"
#define LOGNAME "deallog.txt"

/*
 ****************************************************************************
 * Begin section of code for other mode only
 ****************************************************************************
 */

static void
checkduphash(char *hash)
/*
 * Paranoia function: if in test we ever run into the same 160 bit number again
 * it is time to reevaluate our assumptions
 */
{
	FILE *fhist;
	char oldhash[MESSLEN];
	int hashlen;
	int counter, badentry;

	hashlen = strlen(hash);
	fhist = fopen(HISTNAME, "a+");
	if (fhist == NULL) {
		fprintf(stderr, "Couldn't open %s\n", HISTNAME);
		return;
	}
	counter = 0;
	badentry = 0;
	fseek(fhist, 0L, 0);
	while (readline(fhist, oldhash, MESSLEN)) {
		if (strlen(oldhash) != hashlen) {
			badentry++;
			continue;
		}
		if (strcmp(oldhash, hash) == 0) {
			fprintf(flog, "Panic: same hash, index %d\n", counter);
			fprintf(stderr, "Panic: same hash, index %d\n", counter);
			exit(-1);
		}
		counter++;
	}
	fprintf(fhist, "%s\n", hash);
	fclose(fhist);
	fprintf(flog, "Checked hash against %d previous hashes\n", counter);
	if (badentry) {
		fprintf(flog, "The file %s contained %d bad entries\n",
			HISTNAME, badentry);
	}
}

static void
read_entropy_from_file(char *fname)
/*
 * A frontend has generated entropy for us.
 * Let us hope it did the right thing.
 * We give it credit for 4 bits entropy per byte.
 */
{
	FILE *fentr;
	int c;

	if (fname == 0) {
		fprintf(stderr, "No entropy file supplied\n");
		return;
	}
	fentr = fopen(fname, "r");
	if (fentr == NULL) {
		fprintf(stderr, "Cannot open %s\n", fname);
		return;
	}
	while ((c = getc(fentr)) >= 0) {
		collect_more((byte *) &c, sizeof(c), 4);
	}
	fclose(fentr);
}

static int
hexval(char c)
/*
 * Convert characters '0'..'9', 'A'..'F' and 'a'..'f' to 0..15
 */
{

	if (c >= '0' && c <= '9')
		return c - '0';
	if (c >= 'a' && c <= 'f')
		return c - 'a' + 10;
	if (c >= 'A' && c <= 'F')
		return c - 'A' + 10;
	fprintf(stderr, "Character %c is not a hexchar\n", c);
	exit(-1);
}

static void
random_set(char *hashstr, byte *sr)
/*
 * hashstr is a 40 byte string of hex characters
 * we convert it into a 20 byte(160 bit) binary value
 */
{
	int i;

	if (strlen(hashstr) != 2*RMDbytes) {
		fprintf(stderr, "Argument %s should be %d characters long\n",
			hashstr, 2*RMDbytes);
		exit(-1);
	}
	for (i=0; i<RMDbytes; i++)
		sr[i] = hexval(hashstr[2*i+1]) | (hexval(hashstr[2*i])<<4);
}

/*
 ****************************************************************************
 * End section of code for other mode only
 ****************************************************************************
 */

#endif /* BIGDEALX */

static byte *
RMDhash(byte *value, int length)
/*
 * Hashes value of length bytes using RIPEMD160
 *
 * Returns pointer to 20 bytes hashcode
 */
{
	dword         MDbuf[RMDdwords];		/* contains (A, B, C, D(, E)) */
	dword	      X[16];			/* current 16 word chunk      */
	static byte   hashcode[RMDbytes];	/* for final hash-value       */
	unsigned int  i;			/* counter                    */
	int	      nbytes;			/* bytes not yet processed    */

	/* initialize */
	MDinit(MDbuf);

	/* Process value in 16-word chunks */
	for (nbytes=length; nbytes>=64; nbytes-=64) {
		for (i=0; i<16; i++) {
			X[i] = BYTES_TO_DWORD(value);
			value += 4;
		}
		compress(MDbuf, X);
	}

	/* finish: */
	MDfinish(MDbuf, value, length, 0);

	for (i=0; i<RMDbytes; i+=4) {
		hashcode[i]   =  MDbuf[i>>2];        /* implicit cast to byte */
		hashcode[i+1] = (MDbuf[i>>2] >>  8); /*  extracts the 8 least */
		hashcode[i+2] = (MDbuf[i>>2] >> 16); /*  significant bits.    */
		hashcode[i+3] = (MDbuf[i>>2] >> 24);
	}

	return (byte *)hashcode;
}

static int
goedel(dl_num *dnp)
/*
 * Checks whether the contents of dnp is a number less than the number
 * of bridge deals.
 */
{
	/*
	 * This number is precomputed
	 */
	static byte nr_bridge_deals[L] =
		{ 173,85,227,21,99,77,218,101,139,244,146,0};

#ifdef BIGDEALX
	byte a[L], b[L];

	n_over_k(52,13,a);
	n_over_k(39,13,b);
	mp96_mul(a,a,b);
	n_over_k(26,13,b);
	mp96_mul(a,a,b);

	/*
	 * This gives a the value of the number of bridge deals
	 * =( (52 over 13) times (39 over 13) times (26 over 13) )
	 * Now let us check our internal calculations
	 *
	 * If it is wrong we miscalculated somewhere
	 */

	if (mp96_cmp(a, nr_bridge_deals) != 0) {
		fprintf(stderr, "Miscalculation\n");
		/*
		 * print_goedel(stderr, (dl_num*) a);
		 * print_goedel(stderr, (dl_num*) nr_bridge_deals);
		 */
		exit(-1);
	}
#endif

	if (mp96_cmp(dnp->dn_num, nr_bridge_deals) >= 0)
		return 0; /* too big, not a hand number */
	return 1;
}

extern int nrandombits;

static void
get_entropy_from_keyboard() {
	int c, oldc;
	int nbits;

	cbreak();
	printf("Type random characters until you are told to stop ");
	oldc = 0;
	do {
		c = getchtm(&nbits);
		if (c != oldc) {
			/*
			 * Collect the character, assume 2 bits entropy
			 * plus what the timing supplied.
			 */
			collect_more( (byte*)&c, sizeof(c), 2+nbits);
			oldc = c;
		}
	} while (nrandombits < ENTROPY_TO_COLLECT);
	printf("\nThat is enough\007\n");
	cooked();
}

#ifdef BIGDEALX
char *
hexdump(byte *input, int len) {
	int i = 0;
	char *output = malloc(2*len+1);
	char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	output[len] = 0;
	for (i = 0; i < len; i++) {
		output[2*i] = hex[input[i] >> 4];
		output[2*i+1] = hex[input[i] & 0x0F];
	}
	return output;
}
#endif

/*
 * Structure with all values that get hashed for random generation
 * This includes hash of owner identication making it impossible for
 * another owner to generate the same series of deals
 * Reduces 2**-160 chance to zero
 */
static struct {
	byte	seed_sequence[4];	/* sequence number in PRNG sequence */
	byte	seed_random[RMDbytes];	/* 160 bits collected at start */
	byte	seed_owner[RMDbytes];	/* 160 bit hash of owner ident */
} seed;

int
main (int argc, char *argv[])
{
	int i;
	char message[MESSLEN];
	byte *hashcode;
	unsigned long seqno;
	dl_num dnumber;
	char filename[MESSLEN] = "";
	int c;
	extern char *optarg;
	char *formats = 0;
#ifdef BIGDEALX
	int only_arg_entropy = 0;
	char *hashstr = 0;
	int dangerous_code_used = 0;
#else
#define only_arg_entropy 0
#endif

	/*
	 * Say hi to operator
	 */
	printf("Big Deal version %d.%d%s\n\n",
		VERSION_MAJOR, VERSION_MINOR, VERSION_COMMENT);

#ifdef BIGDEALX
	flog = fopen(LOGNAME, "a");
#endif
	os_start();
	collect_start();

	while ((c = getopt(argc, argv, OPTION_STRING)) != -1) {
		switch(c) {
#ifdef BIGDEALX
		case 'E':
			read_entropy_from_file(optarg);
			dangerous_code_used = 1;
			break;
		case 'e':
			collect_more((byte *) optarg, strlen(optarg), 4*strlen(optarg));
			dangerous_code_used = 1;
			break;
		case 'h':
			hashstr = optarg;
			/* fall through */
		case 'o':
			only_arg_entropy = 1;
			dangerous_code_used = 1;
			break;
#endif
		case 'p':
			strncpy(filename, optarg, MESSLEN-1);
			break;
		case 'f':
			formats = optarg;
			break;
		case 'n':
			parameters.pp_nboards = atoi(optarg);
			break;
		case 'R':
			read_init_file(os_init_file_name(), 0);
			write_init_file(os_init_file_name());
			exit(0);
		case '?':
			fprintf(stderr, "Usage: %s %s\n", argv[0], USAGE_STRING);
			exit(-1);
		}
	}

	if (!only_arg_entropy)
		os_collect();

	read_init_file(os_init_file_name(), 1);
	binomial_start();
	/*
	 * Read number of boards to generate
	 */
	while(parameters.pp_nboards <= 0 || parameters.pp_nboards > MAXDEALS) {
		/*
		 * If we are asked to generate more than 100 boards refuse
		 * We only have 160 bits entropy, and no one plays sessions
		 * of more than 100 boards anyhow
		 */
		if (parameters.pp_nboards > MAXDEALS) {
			parameters.pp_nboards = 0;
			printf("The maximum is %d, run program again for more\n",
				MAXDEALS);
		}
		printf("Number of boards to deal(1-%d): ", MAXDEALS);
		(void) readline(stdin, message, MESSLEN);
		parameters.pp_nboards = atoi(message);
		/*
		 * Collect the string, no entropy assumed
		 */
		if (!only_arg_entropy)
			collect_more( (byte*)message, strlen(message), 0);
	}

	/*
	 * Read part of filename before the .
	 */
	while(!legal_filename_prefix(filename)) {
		printf("Output filename(without suffix): ");
		(void) readline(stdin, message, MESSLEN);
		/*
		 * Collect the string, 5 bits entropy assumed
		 */
		if (!only_arg_entropy)
			collect_more( (byte*)message, strlen(message), 5);
		strcpy(filename,message);
	}

	/*
	 * Get output formats
	 */
	if (formats == 0) {
		/*
		 * Not specified on command line
		 */
		if (strcmp(default_askformats, "yes")==0) {
			printf("Hand format(s) to generate: [%s] ",
				default_formats);
			(void) readline(stdin, message, MESSLEN);
			if (message[0]) {
				strcpy(default_formats, message);
			}
		}
		formats = default_formats;
	}

	/*
	 * If we do not have enough entropy collected (very likely)
	 * let the user supply it by rattling his keyboard
	 */
	if (!only_arg_entropy && nrandombits < ENTROPY_TO_COLLECT) {
		get_entropy_from_keyboard();
	}


	/*
	 * Extract the entropy into the random part of the seed
	 */
	collect_finish(seed.seed_random);
#ifdef BIGDEALX
	if (nrandombits < ENTROPY_TO_COLLECT) {
		/*
		 * Can only happen when only_arg_entropy is set
		 */
		printf("WARNING: entropy supplied is dangerously low!!!\n");
	}
	if (dangerous_code_used) {
		printf("You used features in this extended version of Big Deal\n");
		printf("that might defeat the purpose(generating unique sequences).\n");
		printf("Use hands for actual play only with permission from your national authority.\n\n");
	}

	/*
	 * Did someone use the -h flag ?
	 */
	if (hashstr)
		random_set(hashstr, seed.seed_random);

	/*
	 * Start checking for duplicate hashes, and log current hash
	 */
	for (i=0; i<RMDbytes; i++) {
		message[2*i  ] = "0123456789ABCDEF"[seed.seed_random[i]/16];
		message[2*i+1] = "0123456789ABCDEF"[seed.seed_random[i]%16];
	}
	message[2*RMDbytes] = 0;
	if (!hashstr)
		checkduphash(message);		/* If this fails .... */

	fprintf(flog, "Deal %d boards to file %s, hash = %s\n",
		parameters.pp_nboards, filename, message);
#endif
	(void) output_specify_formats(formats, 1);
	output_createfiles(filename, &parameters);

	/*
	 * Generate a sequence of 160 bit pseudo random numbers
	 * by running the random bits with an increasing
	 * counter through the cryptographical hash.
	 * This according to Carl Ellison's recommendation for P1363
	 *
	 * But first put hash of owner ident into seed
	 */
	hashcode = RMDhash((byte *) default_owner, strlen(default_owner));
	memcpy(seed.seed_owner, hashcode, RMDbytes);

	seqno = 0;
	for(i = 1; i <= parameters.pp_nboards; i++) {
		/*
		 * Next do loop executed as long as we have the misfortune
		 * to make 96 bit numbers above the number of hands
		 */
		do {
			seqno++;
			/*
			 * The next code guarantees the same sequence on
			 * litle endian and big endian machines, which
			 * makes it possible to reproduce sequences on each
			 */
			seed.seed_sequence[0] = seqno & 0xFF;
			seed.seed_sequence[1] = (seqno>>8) & 0xFF;
			seed.seed_sequence[2] = (seqno>>16) & 0xFF;
			seed.seed_sequence[3] = (seqno>>24) & 0xFF;
			/*
			 * Run all the bits through the hash
			 */
			hashcode = RMDhash((byte *) &seed, sizeof(seed));
			/*
			 * Take the first L bytes(96 bits) as a candidate
			 * hand number
			 */
			memcpy(dnumber.dn_num, hashcode, L);
		} while(!goedel(&dnumber));
		/*
		 * Dump the input and the output to STDERR
		 * Probably should have been done on some command line switch
		 */
#ifdef BIGDEALX
		fprintf(stderr, "%s %s\n", hexdump((byte *) &seed, sizeof(seed)), hexdump((byte *) hashcode, RMDbytes));
#endif
		/*
		 * Ok, got one
		 * Print it in all desired formats
		 */
		output_hand(i, &dnumber);
	}

	/*
	 * Finished, close output files
	 */
	output_closefiles();
#ifdef BIGDEALX
	fclose(flog);
#endif

	/*
	 * Do whatever our OS wants us to do at the end, and then exit
	 */
	os_finish();
	return 0;
}