/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss or reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include <crypto/cipher.h>

/****************************************************************************/
/*						User API											*/
/****************************************************************************/

/** @addtogroup cipher */
/** @{ */

/**
*	Initialises an ARC4 state with a given key.
*
*	@param	crypt	Pointer to the ARC4 state descriptor
*	@param	key		Pointer to the encryption key
*	@param	klen	The length of the key in bytes, between 1 and 255.
*					Values outside of the range will be clipped.
*/

void	BC_Arc4Start( BC_ARC4 crypt, const char *key, int klen )
{
int				i, j, k;
unsigned int	t;
unsigned char	*kpt;

	kpt = (unsigned char *) key;

	if ( klen < 1 ) klen = 1;
	if ( klen > 255 ) klen = 255;

	for ( i = 255 ; i >= 0 ; i-- ) crypt[ i ] = i;

	for ( i = j = k = 0 ; i < 256 ; i++ )
	{
		j = ( j + crypt[ i ] + kpt[ k ] ) & 255;
		t = crypt[ i ];
		crypt[ i ] = crypt[ j ];
		crypt[ j ] = t;
		if ( ++k >= klen ) k = 0;
	}

	crypt[ 256 ] = 0;
	crypt[ 257 ] = 0;
}

/**
*	ARC4 encrytion/decryption.
*
*	ARC4 is symmetrical, therefore you can use the same routine for
*	encryption and decryption. The input data is pointed by \p in, the
*	outgoing buffer is pointed by \p out. If \p out is \p NULL, then
*	no output is generated and no input is read, but the internal state
*	is advanced by \p dlen steps. If \p out is not NULL, but \p in is,
*	then no input will be used but the output buffer will be filled with
*	\p dlen number of presudorandom bytes (as if an all-0 input was encrypted).
*	If both pointers are set, then \p dlen input bytes will be processed and
*	the result will be put into the output buffer. The two pointers can point
*	to the same area for in-place transformation but other than that special
*	case the two areas should not overlap.
*
*	@param	crypt	The ARC4 state descriptor
*	@param	in		Pointer to the input text (clear or cipher)
*	@param	out		Pointer to the result text (cipher or clear)
*	@param	dlen	The number of bytes to process
*/

void	BC_Arc4Crypt( BC_ARC4 crypt, const char *in, char *out, int dlen )
{
unsigned int	i, j, t, x;
unsigned char	*p;

	if ( dlen <= 0 ) return;

	i = crypt[ 256 ];
	j = crypt[ 257 ];
	x = 0;
	p = (unsigned char *) in;

	while ( dlen-- )
	{
		i = ( i + 1 ) & 255;
		j = ( j + crypt[ i ] ) & 255;
		t = crypt[ i ];
		crypt[ i ] = crypt[ j ];
		crypt[ j ] = t;

		if ( out ) {

			t = ( t + crypt[ i ] ) & 255;
			if ( p ) x = *p++;
			*out++ = x ^ crypt[ t ];
		}
	}

	crypt[ 256 ] = i;
	crypt[ 257 ] = j;
}

/** @} */

