/*
**	termCrypt.c
**
**	Data encryption routines.
**
**	Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
**		All Rights Reserved
*/

#include "termGlobal.h"

	/* The width of the cell ring is defined here. A word of warning: never
	 * encrypt a file and forget the access password, since it will be
	 * extremely hard to break the code (with 30 cells there are 256^30
	 * possible values each cell may be initialized to which means that
	 * you could have to try more than 1.7e+72 combinations for each single
	 * password character before actually finding the matching password).
	 * See `Random sequence generation by cellular automata' by Steven
	 * Wolfram (Institute for Advanced Study; Advances in Applied
	 * Mathematics) for more information.
	 */

#define CELL_WIDTH 30

	/* The cell ring and the ring index pointers. */

STATIC UBYTE Cell[2][CELL_WIDTH + 2],From,To;

	/* The pattern the cell ring will be filled with. The pattern
	 * may seem familiar: these are the first 32 digits of Pi.
	 */

STATIC UBYTE Pi[32] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5 };

	/* Automaton():
	 *
	 *	A cellular automaton working on a ring of celles, producing
	 *	random data in each single cell .
	 */

STATIC UBYTE
Automaton(VOID)
{
	register WORD i;

		/* Operate on the cell ring... */

	for(i = 1 ; i <= CELL_WIDTH ; i++)
		Cell[To][i] = Cell[From][i - 1] ^ (Cell[From][i] | Cell[From][i + 1]);

		/* Operate on first and last element. */

	Cell[To][0]			= Cell[From][CELL_WIDTH + 1] ^ (Cell[From][0]              | Cell[From][1]);
	Cell[To][CELL_WIDTH + 1]	= Cell[From][CELL_WIDTH]     ^ (Cell[From][CELL_WIDTH + 1] | Cell[From][0]);

		/* Swap cell rings. */

	To	 = From;
	From	^= 1;

		/* Return contents of first cell. */

	return(Cell[From][0]);
}

	/* Encrypt(UBYTE *Source,UBYTE *Destination,UBYTE *Key):
	 *
	 *	Encrypt data using cellular automaton as a random number generator.
	 */

UBYTE *
Encrypt(UBYTE *Source,WORD SourceLen,UBYTE *Destination,UBYTE *Key,WORD KeyLen,BYTE Prefill)
{
	register WORD i,j;

		/* Set up cell ring index pointers. */

	From	= 0;
	To	= 1;

	if(Prefill)
	{
		memcpy(&Cell[0][KeyLen],Pi,32 - KeyLen);

		memcpy(Cell[0],Key,KeyLen);
	}
	else
	{
			/* Initialize the cell ring with the key contents. */

		for(i = 0 , j = KeyLen - 1 ; i < CELL_WIDTH + 2 ; i++)
		{
			Cell[0][i] = Key[j];

			if(j)
				j--;
			else
				j = KeyLen - 1;
		}
	}

		/* Encrypt the source data. */

	for(i = 0 ; i < SourceLen ; i++)
		Destination[i] = ((WORD)Source[i] + Automaton()) % 256;

		/* Return result. */

	return(Destination);
}

	/* Decrypt(UBYTE *Source,UBYTE *Destination,UBYTE *Key):
	 *
	 *	Decrypt data using cellular automaton as a random number generator.
	 */

UBYTE *
Decrypt(UBYTE *Source,WORD SourceLen,UBYTE *Destination,UBYTE *Key,WORD KeyLen,BYTE Prefill)
{
	register WORD i,j,Code;

		/* Set up cell ring index pointers. */

	From	= 0;
	To	= 1;

	if(Prefill)
	{
		memcpy(&Cell[0][KeyLen],Pi,32 - KeyLen);

		memcpy(Cell[0],Key,KeyLen);
	}
	else
	{
			/* Initialize the cell ring with the key contents. */

		for(i = 0 , j = KeyLen - 1 ; i < CELL_WIDTH + 2 ; i++)
		{
			Cell[0][i] = Key[j];

			if(j)
				j--;
			else
				j = KeyLen - 1;
		}
	}

		/* Decrypt the source data. */

	for(i = 0 ; i < SourceLen ; i++)
	{
		if((Code = Source[i] - Automaton()) < 0)
			Code = 256 + Code;

		Destination[i] = Code;
	}

		/* Return result. */

	return(Destination);
}
