/**
* \file CommLayerDataPublic.h
* \brief Public defines and types. Distributes with API.
* \author Dmytro Podgornyi (d.podgornyi@samsung.com)
* \version 0.1
* \date Created Nov 21, 2013
* \par In Samsung Ukraine R&D Center (SURC) under a contract between
* \par LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine) and
* \par "Samsung Elecrtronics Co", Ltd (Seoul, Republic of Korea)
* \par Copyright: (c) Samsung Electronics Co, Ltd 2012. All rights reserved.
**/

#ifndef __COMMLAYERDATAPUBLIC_H_INCLUDED__
#define __COMMLAYERDATAPUBLIC_H_INCLUDED__

#include <stdint.h>

//// Key Blob field tags
#define RSA_CERT_TAG         (uint8_t) 0x01
#define IV_TAG               (uint8_t) 0x02
#define KEY_TAG              (uint8_t) 0x03
#define TL_NAME_TAG          (uint8_t) 0x04
#define ATTRS_TAG            (uint8_t) 0x05

//// ERROR CODES
#define NO_ERROR 0
#define UNSUPPORTED_CMD             ( int32_t ) -1
#define WRONG_DATA                  ( int32_t ) -2
#define PLATFORM_INTERNAL_ERROR     ( int32_t ) -3
#define SHA256_ERROR                ( int32_t ) -4
#define HMAC_ERROR                  ( int32_t ) -5
#define SFS_READ_ERROR              ( int32_t ) -6
#define WRONG_RSA_CERT              ( int32_t ) -7
#define WRONG_PRIV_KEY              ( int32_t ) -8
#define NO_KEY_ERROR                ( int32_t ) -9
#define WRITE_KEY_ERROR             ( int32_t ) -10
#define READ_KEY_ERROR              ( int32_t ) -11
#define WRITE_SYMM_KEY_ERROR        ( int32_t ) -12
#define INSTALL_SYMM_KEY_ERROR      ( int32_t ) -14
#define WRONG_TAG                   ( int32_t ) -15
#define INVALID_TA_NAME_ERROR       ( int32_t ) -16
#define WRAPPING_API_ERROR          ( int32_t ) -17
#define CERT_EXPIRATION_TIME_ERROR  ( int32_t ) -18
#define NOT_IMPLEMENTED             ( int32_t ) -127
#define GET_OEM_FLAG_ERROR          ( int32_t ) -1301
#define INTEGRITY_ERROR             ( int32_t ) -1310

//// Key Info struct
/* UID can be bigger than 52 bytes */
#define MAX_UID_SIZE             (52 * 2)
#define MAX_UUID_SIZE_16         16
#define MAX_SERVICE_NAME         8
#define MAX_SERIALNO_SIZE        32
#define MAX_MODEL_SIZE           32
#define MAX_DATE_SIZE            16
#define MAX_TID_SIZE             128
#define MAX_RSA_LEN              1300
#define MAX_RSA_CERT_LEN         1800
#define APP_NAME_MAX_LEN         128
/* SKM trustlet name for key sharing in QSEE TZ */
#define SKM_TL_NAME              "skm"
#define MLDAP_TL_NAME            "mldap"

//// KEY TYPES
typedef enum
{
	/* RSA key */
	APCS_KEY = 0x10,
	RSA_KEY = 0x10,
	/* Symmetric key */
	SYMM_KEY = 0x20,
	/* Elliptic key */
	EC_SK_KEY = 0x30,
} ProvAgentKeys_t;

/* Certificate type IDs */
typedef enum
{
	CERT_DRK = 0xA0,
	CERT_ML,
	CERT_SM0,
	CERT_SM1,
	CERT_SD,
	CERT_MAX
} certType_t;

//Certificate tlv struct header
typedef struct tlvCertRSAheader
{
	uint8_t  tag;
	uint32_t length;
	uint8_t  certType;
} tlvCertRSAheader_t;

//Certificate tlv struct
typedef struct tlvCertRSA
{
	tlvCertRSAheader_t tlvHeader;
	uint8_t  certData[MAX_RSA_CERT_LEN];
} tlvCertRSA_t;

//common tlv container header
typedef struct tlvContainerHeader
{
	uint8_t  tag;
	uint32_t length;
	uint32_t tlv_num;
} tlvContainerHeader_t;

//common tlv container
typedef struct tlvContainer
{
	tlvContainerHeader_t tlvHeader;
	uint8_t  data; /* Treat as &data = address of first data byte */
} tlvContainer_t;


typedef struct KeyInfo
{
	/* 52 is for backward compatibility
	* size of KeyInfo must be equal to the old value */
	uint8_t serviceName[52];
	/* device serial number */
	uint8_t serialno[MAX_SERIALNO_SIZE];
	/* model name, ex: SGH-I337 */
	uint8_t model[MAX_MODEL_SIZE];
	/* date */
	uint16_t year;
	uint8_t mon;
	uint8_t mday;
	uint8_t hour;
	uint8_t min;
	uint8_t sec;
	/* key length in bits, default 2048 */
	uint32_t keyLen;
	/* 1 - crt rsa, 0 - without crt,  0xA0..0xFF - see certType_t*/
	uint32_t crt;
} KeyInfo_t;

typedef enum
{
	TLV_EXPONENT = 1,
	TLV_ISSUER,
	TLV_HASH_ALGO,
	TLV_SUBJECT,
	TLV_KEYUSAGE,
	TLV_EXT_KEYUSAGE,
	TLV_SIGN_DATA_BLOB,
	TLV_CERT_SM0,
	TLV_CERT_SM1,
	TLV_CERT_SD,
	TLV_PRIV_SD,
	TLV_TIMESTAMP,
	TLV_WRAPPED_PCR,
	TLV_EXTEND_PCR_DATA,
	TLV_TID,
	TLV_WRAPPED_KEY,
	TLV_KEY_INFO,
	TLV_ATTRS,
	/* Identifier of the start of a TLV buffer */
	TLV_START = 0xfe
} TlvTag_t;

#endif /* __COMMLAYERDATAPUBLIC_H_INCLUDED__ */
