/*
 * =====================================================================================
 *
 *       Filename:  commonConfig.h
 *
 *    Description:  Common definitions.
 *
 *        Version:  1.0
 *        Created:  03/10/2017 03:27:17 PM
 *       Compiler:  arm
 *
 *         Author:  Dongwook Shim (), dw.shim@samsung.com
 *        Company:  Samsung Electronics
 *
 *        Copyright (c) 2017 by Samsung Electronics, All rights reserved.
 *
 * =====================================================================================
 */

#ifndef __COMMON_CONFIG_H__
#define __COMMON_CONFIG_H__

#include <stdbool.h>
#include <stdint.h>
#include "commandList.h"

/*******************************************
 *
 * Endian Definitions.
 *
 *******************************************/
#define _LITTLE_ENDIAN    1234
#define _BIG_ENDIAN       4321
#ifndef _BYTE_ORDER
#define _BYTE_ORDER       _LITTLE_ENDIAN    // ARM is little-endian.
#endif  // End of __BYTE_ORDER

/*******************************************
 *
 * Macro functions.
 *
 *******************************************/
#define __SWAP16_(x)   ((uint16_t)((((uint16_t)(x) & (uint16_t)0x00FFU) << 8) | (((uint16_t)(x) & (uint16_t)0xFF00U) >> 8)))

#if _BYTE_ORDER == _BIG_ENDIAN
// The htons() function converts the unsigned short integer hostshort from host byte order to network byte order.
#define SEC_HTONS(x)      __SWAP16_(x)
// The ntohs() function converts the unsigned short integer netshort from network byte order to host byte order.
#define SEC_NTOHS(x)      __SWAP16_(x)
#else
#define SEC_HTONS(x)      (x)
#define SEC_NTOHS(x)      (x)
#endif  // End of __BYTE_ORDER == __BIG_ENDIAN

/* uint16_t <-> bytes */
#define GET_UINT16(x, y) (uint16_t)(((uint16_t)x[y+1] << 8) | (uint16_t)(x[y]))

#define SET_UINT16_FROM_U16(x, y, z) \
    do { \
        x[y+1] = (uint8_t)(((z) >> 8) & 0xFF); \
        x[y]   = (uint8_t)((z) & 0xFF); \
    } while (0);

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(arr)   (sizeof(arr) / sizeof((arr)[0]))
#endif

/*******************************************
 *
 * Definitions.
 *
 *******************************************/
// Max size of buffers.
#define MAX_SKM_BUF_SIZE                  8192
// P180110-05678 Grace-R.
// Order 4(64KB) page allocation can be failed in kernel by page allcator policy - cmlaika.kim 2018.01.16
// Request to change allocation memory size to 32KB under. - jt1217.kim in S.LSI 2018.01.18
#define MAX_PROV_BUF_SIZE                 32760  // Prov and Keymaster - 32KB include header(8B).
#define MAX_FILE_PATH_LEN                 256
#define MAX_ENCRYPTED_BLOB_SIZE           4096
#define MAX_CERT_LEN                      2048
#define MAX_KEY_LEN                       2048
#define MAX_UID_SIZE                      128
#define MAX_SERVICE_NAME                  8
#define MAX_SERIALNO_SIZE                 32
#define MAX_MODEL_SIZE                    32

// CSR related length.
#define CSR_TIMESTAMP_LEN                 14
#define CSR_BRANCH_ID_LEN                 3
#define CSR_DATA_MAX_SIZE                 4096

// Key blob fied tags.
#define KEYBLOB_TAG_CERT                  0x01
#define KEYBLOB_TAG_IV                    0x02
#define KEYBLOB_TAG_PRIVATE_KEY           0x03
#define KEYBLOB_TAG_TA_NAME               0x04
#define KEYBLOB_TAG_ATTRS                 0x05
#define KEYBLOB_TAG_GAK                   0x06

// Crypto related value.
#define IV_SIZE                           16
#define RSA_DEFAULT_EXPONENT              65537

// Certificate specific defines  time (in months) when certificate is valid.
// Note: Number of years should be rounded to 4  due to keep valid Feb 29 date in leap year.
#define CERT_VALIDATION_PERIOD            12 * 12

/*******************************************
 *
 * Error codes.
 *
 *******************************************/

#define ERR_BASE                           (int32_t)  -10000
#define NOT_ERROR                           0
#define ERR_UNSUPPORTED_CMD                ERR_BASE + (int32_t) -1
#define ERR_INVALID_ARGUMENT               ERR_BASE + (int32_t) -2
#define ERR_BUFFER_OVERFLOW                ERR_BASE + (int32_t) -3
#define ERR_NOT_ENOUGH_MEMORY              ERR_BASE + (int32_t) -4
#define ERR_PERMISSION_DENIED              ERR_BASE + (int32_t) -5
#define ERR_NOT_IMPLEMENTED                ERR_BASE + (int32_t) -6
#define ERR_FILE_READ_FAILED               ERR_BASE + (int32_t) -7
#define ERR_FILE_WRITE_FAILED              ERR_BASE + (int32_t) -8
#define ERR_INVALID_BLOB                   ERR_BASE + (int32_t) -9
#define ERR_DRK_IS_NOT_EXIST               ERR_BASE + (int32_t) -10
#define ERR_WRONG_ORDER                    ERR_BASE + (int32_t) -11
#define ERR_BASE64_CODING_FAILED           ERR_BASE + (int32_t) -12
#define ERR_HASH_VERIFICATION_FAILED       ERR_BASE + (int32_t) -13
#define ERR_UNSUPPORTED_DRKV2_UID_FORMAT   ERR_BASE + (int32_t) -14
#define ERR_RAND_BYTE_FAILED               ERR_BASE + (int32_t) -15
#define ERR_D2I_PUBKEY_FAILED              ERR_BASE + (int32_t) -16
#define ERR_WRONG_SIZE                     ERR_BASE + (int32_t) -17

#define ERR_WB_API_OPERATION_FAILED        ERR_BASE + (int32_t) -100
#define ERR_SYSCALL_FAILED                 ERR_BASE + (int32_t) -101
#define ERR_PLATFORM_API_OPERATION_FAILED  ERR_BASE + (int32_t) -102
#define ERR_DRIVER_API_OPERATION_FAILED    ERR_BASE + (int32_t) -103
#define ERR_TA_LOADING_FAILED              ERR_BASE + (int32_t) -104
#define ERR_OPENSSL_API_FAILED             ERR_BASE + (int32_t) -105

#define ERR_EVP_BASE                       ERR_BASE + (int32_t) -500
#define ERR_EVP_CIPHER_NEW_FAILED          ERR_EVP_BASE + (int32_t) -1
#define ERR_EVP_CIPHER_INIT_FAILED         ERR_EVP_BASE + (int32_t) -2
#define ERR_EVP_CIPHER_UPDATE_FAILED       ERR_EVP_BASE + (int32_t) -3
#define ERR_EVP_CIPHER_FINAL_FAILED        ERR_EVP_BASE + (int32_t) -4
#define ERR_EVP_PKEY_ENC_INIT_FAILED       ERR_EVP_BASE + (int32_t) -5
#define ERR_EVP_SET_RSA_PADDING_FAILED     ERR_EVP_BASE + (int32_t) -6
#define ERR_EVP_PKEY_ENC_FAILED            ERR_EVP_BASE + (int32_t) -7

#define ERR_RILD_BASE                      ERR_BASE + (int32_t) -1000
#define ERR_WAIT_CONDITION_ERROR           ERR_RILD_BASE + ( int32_t ) -1
#define ERR_RILD_CONNECT_ERROR             ERR_RILD_BASE + ( int32_t ) -2
#define ERR_RILD_REQUEST_HOOK_ERROR        ERR_RILD_BASE + ( int32_t ) -3
#define ERR_RILD_SET_HANDLER_ERROR         ERR_RILD_BASE + ( int32_t ) -83

#define ERR_KEYMASTER_BASE                 ERR_BASE + (int32_t) -1500
#define ERR_KEYMASTER_UNKNOWN_ERROR        ERR_KEYMASTER_BASE + ( int32_t ) -1
#define ERR_KEYMASTER_PACKING_ERROR        ERR_KEYMASTER_BASE + ( int32_t ) -2
#define ERR_KEYMASTER_UNPACKING_ERROR      ERR_KEYMASTER_BASE + ( int32_t ) -3
#define ERR_KEYMASTER_NULL_POINTER         ERR_KEYMASTER_BASE + ( int32_t ) -4
#define ERR_KEYMASTER_INVALID_OUT_SIZE     ERR_KEYMASTER_BASE + ( int32_t ) -5
#define ERR_KEYMASTER_INVALID_KEY_BLOB     ERR_KEYMASTER_BASE + ( int32_t ) -6
#define ERR_KEYMASTER_TZ_COMMUNICATE_ERROR ERR_KEYMASTER_BASE + ( int32_t ) -7

#define ERR_TA_BASE                        ERR_BASE + (int32_t) -2000
#define ERR_TA_UNSUPPORTED_CMD             ERR_TA_BASE + (int32_t) -1
#define ERR_TA_INVALID_ARGUMENT            ERR_TA_BASE + (int32_t) -2
#define ERR_TA_BUFFER_OVERFLOW             ERR_TA_BASE + (int32_t) -3
#define ERR_TA_NOT_ENOUGH_MEMORY           ERR_TA_BASE + (int32_t) -4
#define ERR_TA_PARSING_FAILED              ERR_TA_BASE + (int32_t) -5
#define ERR_TA_KEYPAIR_MISMATCHED          ERR_TA_BASE + (int32_t) -6
#define ERR_TA_VERIFICATION_FAILED         ERR_TA_BASE + (int32_t) -7
#define ERR_TA_INVALID_BLOB                ERR_TA_BASE + (int32_t) -8
#define ERR_TA_NOT_SECURE                  ERR_TA_BASE + (int32_t) -9
#define ERR_TA_REVOCATED_CERT              ERR_TA_BASE + (int32_t) -10
#define ERR_TA_GEN_RANDOM_FAILED           ERR_TA_BASE + (int32_t) -11
#define ERR_TA_GEN_HASH_FAILED             ERR_TA_BASE + (int32_t) -12
#define ERR_TA_CRYPTO_API_ERROR            ERR_TA_BASE + (int32_t) -13
#define ERR_TA_PERMISSION_DENIED           ERR_TA_BASE + (int32_t) -14
#define ERR_TA_INVALID_KEY_LENGTH          ERR_TA_BASE + (int32_t) -15
#define ERR_TA_INVALID_TLV_ATTR            ERR_TA_BASE + (int32_t) -16
#define ERR_TA_GEN_ASN1_FAILED             ERR_TA_BASE + (int32_t) -17
#define ERR_SECURE_BOOT_DISABLED           ERR_TA_BASE + (int32_t) -18
// 101 ~ 409 are reserved by TlApiError.h

#define ERR_TA_SWB_BASE                    ERR_BASE + (int32_t) -3000
#define ERR_TA_SWB_FAILURE                 ERR_TA_SWB_BASE + (int32_t) -1
#define ERR_TA_SWB_PARAM_CTX_NULL          ERR_TA_SWB_BASE + (int32_t) -2
#define ERR_TA_SWB_PARAM_KEY_NULL          ERR_TA_SWB_BASE + (int32_t) -3
#define ERR_TA_SWB_PARAM_IV_NULL           ERR_TA_SWB_BASE + (int32_t) -4
#define ERR_TA_SWB_PARAM_OUT_NULL          ERR_TA_SWB_BASE + (int32_t) -5
#define ERR_TA_SWB_PARAM_IN_NULL           ERR_TA_SWB_BASE + (int32_t) -6
#define ERR_TA_SWB_PARAM_IN_LENGTH_INVALID ERR_TA_SWB_BASE + (int32_t) -7
#define ERR_TA_SWB_PARAM_OUT_LENGTH_NULL   ERR_TA_SWB_BASE + (int32_t) -8
#define ERR_TA_SWB_FILE_OPEN_FAILED        ERR_TA_SWB_BASE + (int32_t) -9
#define ERR_TA_SWB_FILE_READ_FAILED        ERR_TA_SWB_BASE + (int32_t) -10
#define ERR_TA_SWB_RESOURCE_UNLOADED       ERR_TA_SWB_BASE + (int32_t) -11
#define ERR_TA_SWB_CIPHER_MODE_INVALID     ERR_TA_SWB_BASE + (int32_t) -12
#define ERR_TA_SWB_OUTPUT_SIZE_INVALID     ERR_TA_SWB_BASE + (int32_t) -20

#define ERR_SRKWB_BASE                     ERR_BASE + (int32_t) -3500
// -1 ~ -6 are reserved by waes_client_api.h

#define ERR_TA_SRKWB_BASE                  ERR_BASE + (int32_t) -3510
// -1 ~ -6 are reserved by waes_client_api.h

#define ERR_TA_X509_PARSE_BASE             ERR_BASE + (int32_t) -4000
// -1 ~ -15 are reserved by x509_parser_error_t at x509v3.h.

#define ERR_TA_QSEE_BASE                   ERR_BASE + (int32_t) -10000
#define ERR_TA_BF_BASE                     ERR_TA_QSEE_BASE
// -73 ~ -95 are reserved by qsee_message.h

/*******************************************
 *
 * Structure Definitions.
 *
 *******************************************/

typedef enum
{
    KEY_DRK_V1 = 0,
    KEY_GOOGLE_ATTESTATION,
    KEY_DRK_V2,
    KEY_SAMSUNG_ATTESTATION,
    KEY_MAX,
} InstallKeyType_t;

typedef struct
{
    InstallKeyType_t keyType;
    char timeStamp[CSR_TIMESTAMP_LEN + 1];
    char branchId[CSR_BRANCH_ID_LEN + 1];
} __attribute__((packed)) CsrServerInfo_t;

typedef struct
{
    uint8_t keyType;
    char serviceName[MAX_SERVICE_NAME + 1];
    char model[MAX_MODEL_SIZE];
    char serialNo[MAX_SERIALNO_SIZE];
    uint32_t keyLength;
} __attribute__((packed)) ServiceKeyInfo_t;

typedef struct
{
    uint32_t cmdId;
    uint32_t dataLen;
    uint8_t data[];
} CmdPtrReq_t;

typedef struct
{
    int32_t status;
    uint32_t dataLen;
    uint8_t data[];
} CmdPtrRsp_t;

typedef struct
{
    uint32_t cmdId;
    uint32_t dataLen;
    uint8_t *data;
} CmdReq_t;

typedef struct
{
    int32_t status;
    uint32_t dataLen;
    uint8_t *data;
} CmdRsp_t;
#endif  // End of __COMMON_CONFIG_H
