/*
 *	lokibench.c - run LOKI in a loop consuming CPU time for lokibenching
 *
 *	Usage:	lokibench [-e|-d] [-k key] [-w work_value]  num_iterations
 *		Flags:
 *		    -e		lokibench encryption (default)
 *		    -d		lokibench decryption
 *		    -k key	specifies key as 16 hex digits
 *		    -w work	specifies work value as 16 hex digits
 *
 *  Author:	Lawrence Brown <lpb@csadfa.oz>	Apr 1990
 *		Computer Science, UC UNSW, ADFA, Canberra, ACT 2600, Australia.
 *
 *		Based on PD program by Phil Karn, KA9Q <karn@flash.bellcore.com>
 *
 *  Copyright 1990 by Lawrence Brown and ITRACE. All rights reserved.
 *      This program may not be sold or used as inducement to buy a
 *      product without the written permission of the author.
 */
#include "loki.h"
#include <stdio.h>

char	key[8]  = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},	/* 64-bit key */
	work[8] = {0,0,0,0,0,0,0,0};		/* 64-bit value to encrypt   */
char	*Name;					/* Name of this program      */
char	*usage = "lokibench [-e|-d] [-k key] [-w work_value]  num_iterations";
long	count = 100;				/* number of iterations      */
int	encrypting = 1;				/* test encrypt fn flag      */

main(argc, argv)
int	argc;
char	**argv;
{
	long	i;		/* loop counter                              */
	int	errflag = 0;	/* Error detected in command line flags ?    */
	int	c;		/* current flag found                        */
	extern char	*optarg;	/* current getopt argument pointer   */
	extern int	optind;	/* next arg in list after getopt     */
	extern long	atol();

	Name = argv[0];		/* save name of program for error messages   */
				/* scan command line flags                   */

	while ((c = getopt(argc, argv, "edk:w:")) != EOF) {
		switch (c) {
		  case 'e':	encrypting = 1;		break;
		  case 'd':	encrypting = 0;		break;
		  case 'k':	get8(key, optarg);	break;
		  case 'w':	get8(work, optarg);	break;
		  default:	errflag++;		break;
		}
	}
	if (errflag) {		/* error in flags                            */
		fprintf(stderr, "%s\n", usage);
		exit(1);
	}
	if (optind < argc)
		count = atol(argv[optind++]);	/* extract iteration count   */

	printf("Setting key: "); put8(key); printf(", "); fflush(stdout);
	setlokikey(key);	/* initialize LOKI key                        */

	printf("%s ", (encrypting ? "encrypting" : "decrypting"));
	put8(work); printf(" %ld times ... ",count); fflush(stdout);

	if (encrypting)		/* perform required number of loops          */
	    for(i = 0;i < count; i++)
		enloki(work);
	else
	    for(i = 0;i < count; i++)
	    	deloki(work);
	printf("done\n");
	exit(0);
}


get8(cp, inp)
char *cp, *inp;
{
	int i,t[8];

	sscanf(inp, "%2x%2x%2x%2x%2x%2x%2x%2x",
		&t[0],&t[1],&t[2],&t[3],&t[4],&t[5],&t[6],&t[7]);
	for(i=0;i<8;i++){
		*cp++ = t[i];
	}
}

put8(cp)
char *cp;
{
	int i;

	for(i=0;i<8;i++){
		printf("%02X",*cp++ & 0xff);
	}
}
