/**
* \file CommLayerData.h
* \brief Private defines and types. This file is shared between SWd and NWd.
* \author Dmytro Podgornyi (d.podgornyi@samsung.com)
* \version 0.1
* \date Created May 28, 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 __COMMLAYERDATA_H_INCLUDED__
#define __COMMLAYERDATA_H_INCLUDED__

#include <stdint.h>

#include "CommLayerDataPublic.h"

//// Sizes of data and limits
#ifdef USE_DRKV2
#define MAX_TRANSFER_SIZE 12288
#else
#define MAX_TRANSFER_SIZE 8192
#endif
#define MAX_FILE_PATH_LEN 256
#define IV_SIZE           16
#define KEK_LENGTH        32
#define MAX_SYM_KEY_LEN   32
#define RSA_EXPONENT      65537
#define RSA_SHA1          5
#define RSA_SHA256        6
#define RSA_SHA512        7
#define SHA1_SIZE         20

#define TAG_FIELD_SIZE     0x01
#define LENGTH_FIELD_SIZE  0x02
#define TAGLENGTH_FIELD_SIZE (TAG_FIELD_SIZE + LENGTH_FIELD_SIZE)
#define SIZE_KEY_BLOB 2 * MAX_RSA_CERT_LEN + MAX_RSA_LEN + MAX_TID_SIZE + 4 * TAGLENGTH_FIELD_SIZE

//// UUID of the MobiCore trustlets
#define MC_UUID_MLDAP {{0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x17}}
#define MC_UUID_PROV {{0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x0c}}
#define MC_UUID_SKM {{0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x0d}}
#ifndef MC_UUID_KNOX
#define MC_UUID_KNOX {{0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x0f}}
#endif /* MC_UUID_KNOX */

//// COMMANDS
#define INSTALL_APCS_KEY_CMD            0xB0
#define INSTALL_SYMM_KEY_CMD            0xB1
#define CHECK_APCS_KEY_CMD              0xB2
#define CHECK_SYMM_KEY_CMD              0xB3
/* for compatibility with static-linked old binaries */
#define GEN_RSA_SERVICE_KEY_CMD         0xB4
#define GEN_RSA_SERVICE_KEY_EX_CMD      0x1B4
/* generation with additional x509v3 attributes */
#define GEN_RSA_SERVICE_KEY_ATTR_CMD    0x2B4
#define GEN_SYMM_SERVICE_KEY_EX_CMD     0x1B5
#define VERIFY_RSA_SERVICE_KEY_CMD      0xB6
#define VERIFY_SYMM_SERVICE_KEY_CMD     0xB7
#define GET_UID_DRK_CMD                 0xB8
#ifdef USE_QSEE
#define GET_ENCAPSULATED_KEY_SIZE       0xB9
#define GET_ENCAPSULATED_KEY            0XBA
#define SHARE_SERVICE_KEY_CMD           0xBB
#endif
#define GET_ROOT_PK_CMD                 0xBC
#define GET_ENCRYPTED_TID               0xBD
/* MLDAP OTA Commands */
#define OTA_GET_SD_PUB_KEY_CMD          0xC0
#define SIGN_DATA_CMD                   0xC1
#define OTA_STORE_ML_AND_SD_CERT_CMD    0xC2
#define OTA_VERIFY_SD_CERT_CMD          0xC3
#define OTA_GET_ML_CERT_CMD             0xC4
#define OTA_GET_DRK_CERT_CMD            0xC5
#define OTA_DELETE_SD_FILE              0xC6
/* MLDAP DAP Commands */
#define DAP_GET_SD_CERT_CMD             0xC9
#define DAP_GET_SM0_CERT_CMD            0xCA
#define DAP_GET_SM1_CERT_CMD            0xCB
#define DAP_GET_OEM_FLAG                0xCC
#define DAP_PCR_READ                    0xCD
#define DAP_PCR_EXTEND                  0xCE
/* Using DRK service */
#define STORE_SERVICE_KEY_CMD       0xD0
#define VERIFY_SERVICE_KEY_CMD      0xD1
/* elliptic curves */
#define VERIFY_EC_SERVICE_KEY_CMD       0xD6
#define GEN_EC_SERVICE_KEY_EX_CMD       0x1D4
/* tests */
#define GET_RAW_CERT_DEV_CMD            0xDA
#define GET_RAW_PRIV_DEV_CMD            0xDB
#define GET_RAW_CERT_SERVICE_CMD        0xDC
#define GET_RAW_PRIV_SERVICE_CMD        0xDD

//// NWD specific defines
/* timeout in milliseconds: how long to wait for SWD access before reportig
 * error */
#define NWD_SYNC_TIMEOUT 3000L

//// Certificate specific defines
/* time (in months) when certificate is valid */
#define TIME_OF_CERT_USE (10 * 12)

/* XXX: sizeof(cmd_req_t) must equal to sizeof(cmd_rsp_t) */
//// Request struct
typedef struct cmd_req
{
	uint32_t cmd_id;
	uint32_t dataLen;
#ifdef USE_BLOWFISH
	uint8_t *data;
#else
	uint8_t data[MAX_TRANSFER_SIZE];
#endif
#ifdef USE_QSEE
    uint8_t mldap_flag;
#endif
} cmd_req_t;

//// Response struct
typedef struct cmd_rsp
{
	int32_t status;
	uint32_t dataLen;
	uint8_t data[MAX_TRANSFER_SIZE];
} cmd_rsp_t;

#define GET_UINT16T_LE(N, ARR) (uint16_t) ( ((uint16_t)ARR[N]) | ((uint16_t)ARR[N + 1] << 8) )
#define SET_UINT16T_LE(VAL, N, ARR) \
	do { \
		ARR[N] = (uint8_t)((VAL) & 0xff); \
		ARR[(N) + 1] = (uint8_t)(((VAL) >> 8) & 0xff); \
	} while (0)

//// Standard values if they aren't defined

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef NULL
#define NULL ((void*)0)
#endif

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif

//// TESTS

/* Functional tests for SWd. If defined tests will be performed */
#if defined(SWD_DEBUG) || defined(NWD_DEBUG)
#define RUN_FUNC_TESTS
#endif

/* Build API for access to raw certificates and private keys.
 * It's used for certificate validation tests in NWd. Must be removed for
 * relese buidls */
/* XXX: Important: don't define it for release builds, may cause backdoors */
//#if defined(SWD_DEBUG) || defined(NWD_DEBUG)
//#define INCLUDE_RAW_ACCESSORS
//#endif

#endif /* __COMMLAYERDATA_H_INCLUDED__ */