/****************************************************************************/
/*	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>
#include <crypto.h>

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

/**
*	Initialises a block cipher.
*
*	This function initialises a block cipher. The key schedule for the
*	cipher function must have already been prepared. The initial vector,
*	or nonce for CTR mode, also must be prepaerd before this call.
*	The function prepares the block cipher structure with the parameters
*	it receives.
*
*	@param	cipher	Pointer to the block cipher structure to initialise.
*
*	@param	cfunc	The block cipher function to use. It must be one of<br>
*					BC_CIPHER_AES for AES_128, AES_192 or AES_256 or<br>
*					BC_CIPHER_DES for DES, TDEA_2 or TDEA_1<br>
*
*	@param	cmode	The chain operation of the cipher. It must be<br>
*					BC_BCMODE_ECB for electronic codebook mode,<br>
*					BC_BCMODE_CBC for cipher block chaining mode,<br>
*					BC_BCMODE_CFB for cipher feedback mode,<br>
*					BC_BCMODE_OFB for output feedback mode or<br>
*					BC_BCMODE_CTR for counter mode.
*
*	@param	sched	The key schedule for the underlying block cipher function.
*					For BC_CIPHER_AES it must have been prepared by a call
*					to BC_AesKeySched() and for BC_CIPHER_DES by a call to
*					BC_DesKeySched().  While the cipher is active, the key
*					schedule must not be changed.
*
*	@param	nonce	The initial vector or nonce. It must be a pointer to
*					as many bytes as the block size of the cipher function,
*					that is, 16 bytes for AES and 8 bytes for DES. This
*					memory location is changed by the encrypt and decrypt
*					calls and should not be modified between calls.
*					Furthermore, for CTR mode it is treated as a 16 or
*					8 byte long integer, stored in big-endian format, and
*					incremented by 1 after every encryption or decryption.
*					The only operation mode that does not use this parameter
*					is ECB, in that case this memory will never be referenced
*					and the pointer passed in this parameter will be ignored.
*/

void	BC_CipherPrepare( BC_CIPHER *cipher,
						  const struct bc_cipher *cfunc,
						  const struct bc_cblock *cmode,
						  const int *sched, int *nonce )
{
	cipher->cipher = cfunc;
	cipher->method = cmode;
	cipher->ksched = sched;
	cipher->vector = nonce;
	cipher->length = 0;
}

/**
*	Encrypts a block.
*
*	This function encrypts a block of data using the method and cipher function
*	set by the BC_CipherPrepare() function. It also updates the state.
*
*	@param	state	A pointer to a BC_CIPHER structure that holds the state
*					for the cipher. It must have been prepared by a call to
*					the BC_CipherPrepare() function.
*
*	@param	clear	Pointer to the cleartext block. The length of the block
*					is determined by the underlying cipher function; it is
*					16 bytes for AES and 8 bytes for DES/TDEA.
*
*	@param	cipher	Pointer to a memory block where the ciphertext will be
*					stored. It must have the same size as the cleartext
*					block. It can overlap the cleartext.
*/

void	BC_CipherEncrypt( BC_CIPHER *state, const void *clear, void *cipher )
{
	(*state->method->en)( state, clear, cipher );
	state->length += state->cipher->bl;
}

/**
*	Decrypts a block.
*
*	This function decrypts a block of data using the method and cipher function
*	set by the BC_CipherPrepare() function. It also updates the state.
*
*	@param	state	A pointer to a BC_CIPHER structure that holds the state
*					for the cipher. It must have been prepared by a call to
*					the BC_CipherPrepare() function.
*
*	@param	cipher	Pointer to the ciphertext block. The length of the block
*					is determined by the underlying cipher function; it is
*					16 bytes for AES and 8 bytes for DES/TDEA.
*
*	@param	clear	Pointer to a memory block where the cleartext will be
*					stored. It must have the same size as the ciphertext
*					block. It can overlap the ciphertext.
*/

void	BC_CipherDecrypt( BC_CIPHER *state, const void *cipher, void *clear )
{
	(*state->method->de)( state, cipher, clear );
	state->length += state->cipher->bl;
}

/**
*	Returns the number of bytes processed so far.
*
*	@param	state	A pointer to a BC_CIPHER structure that holds the state
*					for the cipher. It must have been prepared by a call to
*					the BC_CipherPrepare() function.
*
*	@return	The number of bytes processed since the cipher was initialised.
*/

unsigned	BC_CipherLength( const BC_CIPHER *state )
{
	return( state->length );
}

/** @} */

