/**
 * \file common.h
 * \brief Definitions common for device and host software
 */
#ifndef __CRD_UT10XX_COMMON_H
#define __CRD_UT10XX_COMMON_H

/**
 * \defgroup common_group Common definitions
 * \brief Definitions common for device and host software.
 *
 * \{
 */

 
/**
 * \name Supported ECC domains
 * \{
 */

/**
 * \brief Domain over 160-bits prime field defined in SEC2.
 *
 * \code
 *   secp160r1 OBJECT IDENTIFIER ::= { 
 *     iso(1) identified-organization(3) certicom(132) curve(0) 8 
 *   }
 * \endcode
 */
#define SECP160R1 1600

/**
 * \brief Domain over 192-bits prime field defined in SEC2.
 *
 * \code
 *   secp192r1 OBJECT IDENTIFIER ::= { 
 *     iso(1) member-body(2) us(840) 10045 curves(3) prime(1) 1
 *   }
 * \endcode
 */
#define SECP192R1 1920

/**
 * \brief Domain over 224-bits prime field defined in SEC2.
 *
 * \code
 *   secp224r1 OBJECT IDENTIFIER ::= { 
 *     iso(1) identified-organization(3) certicom(132) curve(0) 33 
 *   }
 * \endcode
 */
#define SECP224R1 2240

/**
 * \brief Domain over 256-bits prime field defined in SEC2.
 *
 * \code
 *   secp256r1 OBJECT IDENTIFIER ::= { 
 *     iso(1) member-body(2) us(840) 10045 curves(3) prime(1) 7
 *   }
 * \endcode
 */
#define SECP256R1 2560

/** \} */


/**
 * \name PIN identifiers
 * \{
 */
 
/** \brief Security Officer (SO) PIN identifier. */
#define SO_PIN_ID 0
/** \brief User PIN identified. */
#define USER_PIN_ID 1
/** \} */


/**
 * \name Commands
 * \{
 */

/** \brief Number of command bit set to 1 if it needs SO pin. */
#define SOPC 7
/** \brief Number of command bit set to 1 if it needs User pin. */
#define URPC 6
/** \brief Number of command bit set to 1 if it needs SO or User pin. */
#define SUPC 5

/**
 * \brief This command initialises device.
 *
 * It is avaliable only if device was not initialised
 * before or device was erased.
 */
#define CMD_DEV_INIT 0x00

/**
 * \brief This command configures device.
 *
 * It is obligatory if you want to use other commands.
 * During this process the following informations are set:
 *  - device information and configuration bits,
 *  - device name,
 *  - limits for wrong PIN,
 *  - PIN for user and security officer.
 */
#define CMD_DEV_CONF 0x01

/**
 * \brief This command reads information about device.
 */
#define CMD_READ_DEV_INFO 0x02

/**
 * \brief This command reads information about key and contents of public key.
 */
#define CMD_READ_KEY_INFO 0x03

/**
 * \brief This command reads object from public memory.
 */
#define CMD_READ_PUBLIC_OBJ 0x04

/**
 * \brief This command reads random bits generated by device.
 */
#define CMD_RBG_GENERATE_BITS 0x05

/**
 * \brief This command unlock device.
 */
#define CMD_DEV_UNLOCK (0x06 | (1 << SOPC))

/**
 * \brief This command cleans all protected memory.
 */
#define CMD_DEV_ERASE (0x07 | (1 << SOPC))

/**
 * \brief This command changes Security Officer PIN.
 */
#define CMD_CHANGE_SO_PIN (0x08 | (1 << SOPC))

/**
 * \brief This command generates signature in ECDSA scheme.
 */
#define CMD_ECDSA_SIGN (0x09 | (1 << URPC))

/**
 * \brief This command computes shared info in ECDH or ECIES scheme. 
 */
#define CMD_ECDH_KEY_AGREEMENT (0x0a | (1 << URPC))

/**
 * \brief This command changes User PIN.
 */
#define CMD_CHANGE_USER_PIN (0x0b | (1 << SUPC))

/**
 * \brief This command generates new User key.
 */
#define CMD_GENERATE_KEY (0x0c | (1 << SUPC))

/**
 * \brief This command removs key from protrcted memory.
 */
#define CMD_DELETE_KEY (0x0d | (1 << SUPC))

/**
 * \brief This command writes object to public memory.
 */
#define CMD_WRITE_PUBLIC_OBJ (0x0e | (1 << SUPC))

/**
 * \brief This command checks if PIN is correct.
 */
#define CMD_TRY_PIN (0x0f | (1 << SUPC))

/** \} */


/**
 * \name Device information and configuration bits
 * \{
 */

/** \brief Set to 1 if device is initialised. */
#define INIT_BIT 0
/** \brief Set to 1 if device is configured. */
#define CONF_BIT 1
/** \brief Set to 1 if device is locked. */
#define LOCK_BIT 2
/** \brief Set to 1 if erasing is in progress. */
#define EINP_BIT 3
/** \brief Set to 1 if change of user pin is not allowed. */
#define UPCNA_BIT 4
/** \} */


/**
 * \name Key information and configuration bits
 * \{
 */

/** \brief Set to 1 if key is generated. */
#define GEN_BIT 0
/** \} */


/**
 * \name Octets of EEPROM memory structures
 * \{
 */

/** \brief Octets of device bit information and configuration. */
#define DEV_INFO_OCTETS 2
/** \brief Octets of device name structure. */
#define DEV_NAME_OCTETS 10
/** \brief Octets of PIN trys number. */
#define PIN_TRYS_OCTETS 1
/** \brief Octets of PIN trys limit number. */
#define PIN_LIMIT_OCTETS 1
/** \brief Octets of PIN representation. */
#define PIN_OCTETS 8
/** \brief Octets of key information structure. */
#define KEY_INFO_OCTETS 24
/** \brief Octets of maximum public key structure. */
#define KEY_PUBLIC_OCTETS 64
/** \brief Octets of maximum private key structure. */
#define KEY_PRIVATE_OCTETS 32
/** \brief Octets of single public object. */
#define PUBLIC_OBJ_OCTETS 64
/** \} */

/**
 * \name Host frame headers
 * \{
 */

/** \brief Frame with host command. */
#define HF_CMD 0x00
/** \brief Maximum size of host command frame body. */
#define HF_CMD_BODY_MAX 128
/** \} */

/**
 * \name Device frame headers
 * \{
 */

/** \brief Frame with error. */
#define DF_ERR 0x80
/** \brief Size of device error frame body. */
#define DF_ERR_BODY 1
/** \brief Frame with device data. */
#define DF_DTE 0x83
/** \brief Maximum size of device data frame body. */
#define DF_DTE_BODY_MAX 128
/** \} */


/**
 * \name Device error codes
 * \{
 */
 
/** \brief There is no such command. */
#define ERR_R_WRONG_CMD 0
/** \brief Command is not allowed in this state of device. */
#define ERR_R_CMD_NOT_ALLOWED 1
/** \brief Wrong PIN during pin verification process. */
#define ERR_R_WRONG_PIN 2
/** \brief Point passed to ECDH shared info primitive is not on supported curve. */
#define ERR_R_WRONG_EC_POINT 3
/** \brief There is no such key. */
#define ERR_R_NO_KEY 4
/** \brief Argument passed to device is out of range. */
#define ERR_R_ARG_RANGE 5
/** \brief Wrong frame syntax. */
#define ERR_R_FRAME_SYNTAX 6
/** \brief Length of frame is out of protocol specified range. */
#define ERR_R_FRAME_RANGE 7
/** \} */

/** \} */

#endif /* __CRD_UT10XX_COMMON_H */

