/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Created in Samsung Ukraine R&D Center (SRK) under a contract between
 * LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine)
 * and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
 */

/**
 * @file TigerSskds.h
 * @brief TigerTa
 * @author Viktor Kopp (v.kopp@samsung.com)
 * @author Andrey Orlenko (a.orlenko@samsung.com)
 * @date Created Aug 23, 2016
 */

#ifndef TIGERSSKDS_H_
#define TIGERSSKDS_H_

#include "TigerTci.h"
#include "TzwCommon.h"

/**
* unique_id The unique id for each device, format as below:
* 1.bytes 0-3: Identify each silicon provider id, defined by WeChat
* 2.bytes 4-7: SoC model ID, defined by each silicon provider（like Qualcomm and Trustonic）
* -3.bytes 8-15: Public Chip Serial *Number of SoC, defined by each silicon provider（like Qualcomm and Trustonic）
* +3.bytes 8 - 24: UUID
*/
//#define UNIQUE_ID_SIZE               (4 + 4 + 8)
#ifdef __SSKDS_DEBUG__
#define UNIQUE_ID_SIZE               (4 + 4 + 16 + 4)
#else
//Add more 4 bytes random data due to Wechat update limitation.
#define UNIQUE_ID_SIZE               (4 + 4 + 16 + 4)
#endif

#define DEVICE_ID_SIZE               (UNIQUE_ID_SIZE * 2) // hex string: UNIQUE_ID_SIZE * 2
#define BYTE_IN_HEX_LEN (2) // any data in hex-presentation will be length * 2.

/**
 * @brief Check that ATTK and Device-key-pair generated and Device key regirested on SSKDS.
 * @param[out] status DeviceKey status.
 * @return status of the operation, e.g. TEE_SUCCESS on success.
 */
TEE_Result getRegistrationStatus(TciDeviceStatus_t* deviceStatus);

/**
 * @brief Set device registation status.
 * @param[io] registrationStatus - 0 if success, 1 in fail.
 * @return status of the operation, e.g. TEE_SUCCESS on success.
 */
TEE_Result setRegistrationStatus(uint8_t registrationStatus);

/**
 * @brief Generate unique device id
 * @param [in] chipId - chip Id (i.e. 8998, 6757 etc)
 * @param [out] device Id array
 * @param [in] device Id array length
 * @return status of the operation, e.g. TEE_SUCCESS on success.
 */
TEE_Result generateDeviceId(const uint32_t chipId, uint8_t* deviceId, const uint32_t deviceIdLen);

/**
 * @brief Store device id in TEE storage
 * @param [in] device Id array
 * @param [in] device Id array length
 * @return status of the operation, e.g. TEE_SUCCESS on success.
 */
TEE_Result storeDeviceId(uint8_t* deviceId, const size_t devIdSize);

/**
 * @brief Loads device id data from TEE storage
 * @param [out] device Id array
 * @param [in/out] array size on input, loaded array size on output
 */
TEE_Result loadDeviceId(uint8_t* deviceId, const uint32_t deviceIdLen);

/**
 * Signs data with ATTK private part
 * @param[in] Pointer to array with data to sign
 * @param[in] Length of data to sign
 * @param[out] Output signature
 * @param[in/out] On input max size of sig array, on output actual size of sig array
 */
TEE_Result signSskdsData(const uint8_t* data, size_t datalen, uint8_t* sig, uint32_t* siglen);

/**
 * Gets requested data for a SSKDS request
 * @param apRequest Pointer on Requested data type
 * @param apResponse Response data
 * @return TEE_SUCCESS if success and TEE_Result code otherwise
 */
TEE_Result getSskdsData(const TciSskdsGetData_t* const apRequest, TciExportedData_t* apResponse);

/**
 * Helper function for converting byte-array to hex.
 * Out buffer size should be sizeIn * 2.
 * @param bytesIn - buffer in;
 * @param sizeIn - buffer in size;
 * @param bytesOut - buffer out;
 * @param sizeOut - buffer out size;
 * @return TEE_SUCCESS if success and TEE_Result code otherwise
 */
TEE_Result convertBytesToHex(const uint8_t* bytesIn, const uint32_t sizeIn, uint8_t* bytesOut, const uint32_t sizeOut);

#if defined(__USE_TESTBED__)
DECLARE_TESTBED_FUNC(storeDeviceId);
#endif /*__USE_TESTBED__*/

#endif /* TIGERSSKDS_H_ */
