/****************************************************************************/
/*	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.																	*/
/****************************************************************************/

#ifndef _BC_RSA_H
#define _BC_RSA_H

#include <crypto/nanu.h>

#define	memrcpy(a,b,c)	memmove(a,b,c)

/** @addtogroup rsa */
/** @{ */

/**
*	Structure that holds the RSA public key.
*/

typedef struct {

	const BC_NANU_CONS	*n;			//!< Modulus
	const BC_NANU_CONS	*e;			//!< Public exponent

} BC_RSA_PUBLIC_KEY;

/**
*	Structure that holds the RSA private key.
*/

typedef struct {

	const BC_NANU_CONS	*p;			//!< First prime, P
	const BC_NANU_CONS	*q;			//!< Second prime, Q
	const BC_NANU_CONS	*P;			//!< D mod (P-1) (D:private exponent)
	const BC_NANU_CONS	*Q;			//!< D mod (Q-1)
	const BC_NANU_CONS	*I;			//!< Q<sup>-1</sup> mod P

} BC_RSA_PRIVATE_KEY;

/**
*	Additional error codes for RSA operations.
*/

enum {

	BC_RSA_KEYL = BC_NANU_NEXT-0,	//!< Key length is not valid
	BC_RSA_DLEN = BC_NANU_NEXT-1,	//!< Cleartext is too long
	BC_RSA_FMAT = BC_NANU_NEXT-2,	//!< Decoded message format error
	BC_RSA_SEED = BC_NANU_NEXT-3	//!< Random seed is 0
};

/** @} */

int	BC_RsaPrivateEncrypt( BC_NANU nanu, BC_RSA_PRIVATE_KEY *keys,
						  void *data, unsigned dlen );

int	BC_RsaPrivateDecrypt( BC_NANU nanu, BC_RSA_PRIVATE_KEY *keys, void *data );
int	BC_RsaPrivateCore( BC_NANU nanu, BC_RSA_PRIVATE_KEY *kp, void *dp );

int	BC_RsaPublicEncrypt( BC_NANU nanu, BC_RSA_PUBLIC_KEY *keys,
						 void *data, unsigned dlen, unsigned seed );

int	BC_RsaPublicDecrypt( BC_NANU nanu, BC_RSA_PUBLIC_KEY *keys, void *data );
int	BC_RsaPublicCore( BC_NANU nanu, BC_RSA_PUBLIC_KEY *kp, void *dp );

#endif

