/*
 * pnx8335_aes.h
 *
 * (C) Copyright 2007 Andre McCurdy, NXP Semiconductors
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#ifndef _PNX8335_AES_H_
#define _PNX8335_AES_H_

/****************************************************************************/

#define AES_BLOCK_SIZE	16

struct aes_op
{
	unsigned int cmd;
	unsigned char key[AES_BLOCK_SIZE];
	unsigned char iv[AES_BLOCK_SIZE];
	unsigned char *src;
	unsigned char *dst;
	unsigned int blocks;
	unsigned char ck_ct[24];	/* not required in 2-level mode */
	unsigned char sk_ct[24];
	unsigned char cw_ct[24];
};

struct kc_uid
{
	unsigned char uid_data[8];
};

#if 0
#define AES_ENCRYPT	1
#define AES_DECRYPT	0
#endif

/****************************************************************************/

#define DECRYPT				(0)
#define ENCRYPT				(1)

#define MODE_ECB			(0)
#define MODE_CBC			(1)
#define MODE_CFB			(2)	/* Not supported... */
#define MODE_OFB			(3)	/* Not supported... */
#define MODE_CTR			(4)

#define KEY_USER			(0)
#define KEY_KC_BYPASS			(1)
#define KEY_KC_2_LEVEL			(2)	/* Note: HW can be configured to support either 2-level or 3-level... not both */
#define KEY_KC_3_LEVEL			(3)

/****************************************************************************/

#define AESCMD_ED_BITS			(1)
#define AESCMD_ED_SHIFT 		(0)
#define AESCMD_DECRYPT			(DECRYPT << AESCMD_ED_SHIFT)
#define AESCMD_ENCRYPT			(ENCRYPT << AESCMD_ED_SHIFT)

#define AESCMD_MODE_BITS 		(3)
#define AESCMD_MODE_SHIFT		(AESCMD_ED_SHIFT + AESCMD_ED_BITS)
#define AESCMD_MODE_ECB			(MODE_ECB << AESCMD_MODE_SHIFT)
#define AESCMD_MODE_CBC			(MODE_CBC << AESCMD_MODE_SHIFT)
#define AESCMD_MODE_CFB			(MODE_CFB << AESCMD_MODE_SHIFT)
#define AESCMD_MODE_OFB			(MODE_OFB << AESCMD_MODE_SHIFT)
#define AESCMD_MODE_CTR			(MODE_CTR << AESCMD_MODE_SHIFT)

#define AESCMD_KEY_BITS 		(3)
#define AESCMD_KEY_SHIFT		(AESCMD_MODE_SHIFT + AESCMD_MODE_BITS)
#define AESCMD_KEY_USER 		(KEY_USER << AESCMD_KEY_SHIFT)
#define AESCMD_KEY_KC_BYPASS		(KEY_KC_BYPASS << AESCMD_KEY_SHIFT)
#define AESCMD_KEY_KC_2_LEVEL		(KEY_KC_2_LEVEL << AESCMD_KEY_SHIFT)
#define AESCMD_KEY_KC_3_LEVEL		(KEY_KC_3_LEVEL << AESCMD_KEY_SHIFT)

#define AESCMD_EXTRACT_ED(cmd)		(((cmd) >> AESCMD_ED_SHIFT) & ((1 << AESCMD_ED_BITS) -  1))
#define AESCMD_EXTRACT_MODE(cmd)	(((cmd) >> AESCMD_MODE_SHIFT) & ((1 << AESCMD_MODE_BITS) -  1))
#define AESCMD_EXTRACT_KEY(cmd) 	(((cmd) >> AESCMD_KEY_SHIFT) & ((1 << AESCMD_KEY_BITS) -  1))

/****************************************************************************/

#define AESCMD_DECRYPT_ECB_KEY_USER		(AESCMD_DECRYPT | AESCMD_MODE_ECB | AESCMD_KEY_USER)
#define AESCMD_ENCRYPT_ECB_KEY_USER		(AESCMD_ENCRYPT | AESCMD_MODE_ECB | AESCMD_KEY_USER)
#define AESCMD_DECRYPT_CBC_KEY_USER		(AESCMD_DECRYPT | AESCMD_MODE_CBC | AESCMD_KEY_USER)
#define AESCMD_ENCRYPT_CBC_KEY_USER		(AESCMD_ENCRYPT | AESCMD_MODE_CBC | AESCMD_KEY_USER)
#define AESCMD_DECRYPT_CTR_KEY_USER		(AESCMD_DECRYPT | AESCMD_MODE_CTR | AESCMD_KEY_USER)
#define AESCMD_ENCRYPT_CTR_KEY_USER		(AESCMD_ENCRYPT | AESCMD_MODE_CTR | AESCMD_KEY_USER)

#define AESCMD_DECRYPT_ECB_KEY_KC_BYPASS	(AESCMD_DECRYPT | AESCMD_MODE_ECB | AESCMD_KEY_KC_BYPASS)
#define AESCMD_ENCRYPT_ECB_KEY_KC_BYPASS	(AESCMD_ENCRYPT | AESCMD_MODE_ECB | AESCMD_KEY_KC_BYPASS)
#define AESCMD_DECRYPT_CBC_KEY_KC_BYPASS	(AESCMD_DECRYPT | AESCMD_MODE_CBC | AESCMD_KEY_KC_BYPASS)
#define AESCMD_ENCRYPT_CBC_KEY_KC_BYPASS	(AESCMD_ENCRYPT | AESCMD_MODE_CBC | AESCMD_KEY_KC_BYPASS)
#define AESCMD_DECRYPT_CTR_KEY_KC_BYPASS	(AESCMD_DECRYPT | AESCMD_MODE_CTR | AESCMD_KEY_KC_BYPASS)
#define AESCMD_ENCRYPT_CTR_KEY_KC_BYPASS	(AESCMD_ENCRYPT | AESCMD_MODE_CTR | AESCMD_KEY_KC_BYPASS)

#define AESCMD_DECRYPT_ECB_KEY_KC_2_LEVEL	(AESCMD_DECRYPT | AESCMD_MODE_ECB | AESCMD_KEY_KC_2_LEVEL)
#define AESCMD_ENCRYPT_ECB_KEY_KC_2_LEVEL	(AESCMD_ENCRYPT | AESCMD_MODE_ECB | AESCMD_KEY_KC_2_LEVEL)
#define AESCMD_DECRYPT_CBC_KEY_KC_2_LEVEL	(AESCMD_DECRYPT | AESCMD_MODE_CBC | AESCMD_KEY_KC_2_LEVEL)
#define AESCMD_ENCRYPT_CBC_KEY_KC_2_LEVEL	(AESCMD_ENCRYPT | AESCMD_MODE_CBC | AESCMD_KEY_KC_2_LEVEL)
#define AESCMD_DECRYPT_CTR_KEY_KC_2_LEVEL	(AESCMD_DECRYPT | AESCMD_MODE_CTR | AESCMD_KEY_KC_2_LEVEL)
#define AESCMD_ENCRYPT_CTR_KEY_KC_2_LEVEL	(AESCMD_ENCRYPT | AESCMD_MODE_CTR | AESCMD_KEY_KC_2_LEVEL)

#define AESCMD_DECRYPT_ECB_KEY_KC_3_LEVEL	(AESCMD_DECRYPT | AESCMD_MODE_ECB | AESCMD_KEY_KC_3_LEVEL)
#define AESCMD_ENCRYPT_ECB_KEY_KC_3_LEVEL	(AESCMD_ENCRYPT | AESCMD_MODE_ECB | AESCMD_KEY_KC_3_LEVEL)
#define AESCMD_DECRYPT_CBC_KEY_KC_3_LEVEL	(AESCMD_DECRYPT | AESCMD_MODE_CBC | AESCMD_KEY_KC_3_LEVEL)
#define AESCMD_ENCRYPT_CBC_KEY_KC_3_LEVEL	(AESCMD_ENCRYPT | AESCMD_MODE_CBC | AESCMD_KEY_KC_3_LEVEL)
#define AESCMD_DECRYPT_CTR_KEY_KC_3_LEVEL	(AESCMD_DECRYPT | AESCMD_MODE_CTR | AESCMD_KEY_KC_3_LEVEL)
#define AESCMD_ENCRYPT_CTR_KEY_KC_3_LEVEL	(AESCMD_ENCRYPT | AESCMD_MODE_CTR | AESCMD_KEY_KC_3_LEVEL)

/****************************************************************************/

#define MISC_DYNAMIC_MINOR		255
#define	NXPAES_MINOR			MISC_DYNAMIC_MINOR

/* submit operation decriptor */
#define IOCTL_NXPAES_OP 		0xC0304189	/* == _IOWR('A', 137, struct aes_op) */
#define IOCTL_NXPAES_KCUID_GET		0x40084189	/* ==  _IOR('A', 137, struct kc_uid) */

#ifdef __KERNEL__
extern int aes_process_op_in_kernel (struct aes_op *op);
#endif

#endif

