/* psion 3a main program
 * all this does is get user input, and pass it to the md4 alogrithm
 * in the standard C library.
 * Psion users can only have 8-character passwords, hence my own
 * user input loop.
 * once I get the hang of Object-orientated programming I'll produce a 
 * proper user interface replaceing the system password input method
 * [the psion 3/3a has a fully object orientated operating system, came
 *  as wquite a shock ot a DOS/Unix hacker like myself
 
 * please send email to my home email address <richard@illuin.demon.co.uk>
 * IF urgent, to my work address: <R.J.Letts@salford.ac.uk>
 *
 * Richard Letts, Network Manager, University of Salford
 */


#include <plib.h>
#include "md4.h"

void f(char *);
char *btoe(char *);
int skey( int n, char *seed, char *passwd, char *onetime);
int keycrunch(char *key, char* seed, char *passwd);

int skey( int n, char *seed, char *passwd, char *onetime)
{
	char key[8];

	if(keycrunch(key,seed,passwd) != 0){
		p_scpy(onetime,"key crunch failed");
		return 1;
	}

	while (n--)		f(key);

	p_scpy(onetime,btoe(key));
	return 0;
}

main() 
{
	int i,j;
	char burf[100];
	int n;
	char chal[64];
	char passwd[64];
	char *cp;

	p_printf("S/Key generator for Psion Series 3/3a computers");
	p_printf("This version by Richard Letts <richard@illuin.demon.co.uk>\n\n");
		
	p_getl("Enter Challenge: ",burf,100);
	cp=burf;
	if (i=p_stoa(&cp,"%d %s",&n,chal)) {
		p_print("Invalid challenge\r\n");
		p_getch();
		return 1;
		};	

	p_print("Enter password: ");
	j=0;
	while (i=p_getch())	{

		if (i==8) {
			p_putch(8);
			p_putch(32);
			p_putch(8);
			j--;
		} else {
			p_putch(6);
			passwd[j++]=i;
			}
		if (i==13) break;
		if (j<=0)  j=0;
		if (j>=64) j=63;
		};

	passwd[--j]=0;
	p_print("\r\nGenerating response..");
	
	
//	p_getl("Enter Password: ",passwd,64);
	i=skey(n,chal,passwd,burf);
	p_print("\r\nResponse is %s\n\n\n\rPress any key to exit",burf);
	p_getch();
	return 0;
};
