/*
 * testcrypt - test UNIX password encryption function
 *
 * (c) Copyright 1987 by Matt Bishop and the Universities Space Research
 *			 Association.
 * This work was funded by grant NCC 2-398 from the National Aeronautics
 * and Space Administration to the Universities Space Research Association.
 *
 * Author's Address:
 *		Matt Bishop
 *		Research Institute for Advanced Computer Science
 *		NASA Ames Research Center
 *		Moffett Field, CA  94035
 * ARPANET:	mab@riacs.edu, mab@icarus.riacs.edu
 * CSNET:	mab@riacs.edu
 * UUCP:	...!decvax!decwrl!riacs!mab
 *		...!ihnp4!ames!riacs!mab
 *		...!ucbvax!ames!riacs!mab
 */
#include <stdio.h>
#include "des.h"
#define MAXKEYS	256
/*
 * the useful variables
 */
char *progname;		/* program name */
char encpasswd[MAXKEYS][14];	/* encrypted password */
char salt[MAXKEYS][3];		/* salt */
char passwd[MAXKEYS][9];		/* password */
int ntests;		/* number of test cases */
int shortcrypt = 0;	/* use standard crypt function */

/*
 * library functions
 */
long **shc_crypt(), **decrypt();

typedef long name[2];

main(argc, argv)
int argc;
char *argv[];
{
	register int failed;		/* number of rounds that failed */
	register char *p;		/* points to result of encryption */
	register int i;			/* counter for result comparison */
	register name *q, *r;		/* points to results of "decryption" */
	int oops;			/* 1 if given test failed */
	long t;

	/*
	 * process argument list
	 */
	progname = argv[0];
	for (i = 1; i < argc; i++)
		if (strcmp(argv[i], "-s") == 0)
			shortcrypt = 1;
		else{
			fprintf(stderr, "%s: bad option %s\n",
						progname, argv[i]);
			exit(1);
		}

	/*
	 * assume it will encrypt everything correctly
	 */
	failed = 0;

	/*
	 * announce what this is
	 */
	printf("(short) CRYPT Testing: %d bit base, %d bit path, %d permutation key computation\n",
			shc_bits(), shc_path(), shc_permkey());

	/*
	 * do the encryption and count the number of errors
	 */
	ntests = 0;
	while(scanf("%s%s%s", encpasswd[ntests], salt[ntests], passwd[ntests]) == 3) 
	  ntests++;

	q = shc_crypt(passwd, salt, ntests);
	r = decrypt(encpasswd, ntests);
	
	for(t = 0; t < ntests; t++) {
	  for(i = 0; i < 2; i++)
	    if (q[t][i] != r[t][i])
	      break;
	  oops = (i != 2);
	  /*
	   * print the results
	   */
	  if (oops){
	    printf("password test #%2d: password %-8.8s salt %2s ",
		   t, passwd[t], salt[t]);
	    printf("encrypted %s fails\n", encpasswd[t]);
	    printf("q1 = %d, q2 = %d, r1 = %d, r2 = %d=\n", q[t][0],q[t][1],
		   r[t][0], r[t][1]);
	    failed++;
	  }
	}


	/*
	 * announce the results
	 */
	printf("password encryption summary: %d successes, %d failures\n",
						ntests - failed, failed);

	/*
	 * bye!
	 */
	exit(failed);
}


