/*
 * 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)
 *
 * Created on: Jul 1, 2016
 * Author: Oleksii Kachkan <o.kachkan@samsung.com>
 * Brief: Helper functions for Secure storage.
 */
#ifndef TIGERSTORAGEUTILS_H_
#define TIGERSTORAGEUTILS_H_

#include "TzwCommon.h"

// NOTE: all 'permanent' persistent object's aliases contain "TIGER_" prefix
#define TIGER_ALIAS_PREFIX  "TIGER_"

// this persistent object contains
//  1) registration deviceId
//  2) registration/update count   (as of Nov 15, 2017 can be only 0 or 1)
//  3) per-device counter value     (new since Nov 15, 2017)
#define TIGER_REGISTRATION_INFO_ALIAS  TIGER_ALIAS_PREFIX"DEVICE_ID" // the object id alias is misleading
#define TIGER_ATTK_CERT_ALIAS          TIGER_ALIAS_PREFIX"ATTK_CERT"
#define TIGER_DRK_CERT_ALIAS           TIGER_ALIAS_PREFIX"DRK_CERT"
#define TIGER_SESSION_ALIAS            TIGER_ALIAS_PREFIX"SESSION"

// Fields that are contained in TIGER_DEVICE_ID_ALIAS object
typedef enum {
    DEVICE_ID   = 0,
    REG_COUNT   = 1,
    COUNTER     = 2
} TigerDeviceIdObjField_t;


typedef enum {
    SESSION_ID  = 0,
    ALIAS_NAME  = 1,
    UID         = 2,
    CHALLENGE   = 3,
    GENTIME     = 4
} TigerSessionObjFiled_t;

/**
 * @brief returns position of the field in TIGER_REGISTRATION_INFO_ALIAS object
 * @param[in] field id
 * @return position in bytes, or -1 if no such field id
 */
int32_t getOffsetTo(TigerDeviceIdObjField_t field);

/**
 * @brief returns position of the field in TIGER_SESSION_ALIAS object
 * @param[in] field id
 * @return position in bytes, or -1 if no such field id
 */
int32_t getSessionOffsetTo(TigerSessionObjFiled_t field);

/**
 * Identifies permanent persistent objects, e.g. registration info object
 * @param[in] objectId - object ID
 * @param[in] objectIdLen - object ID length
 * @return true if object should never be removed from TZ storage and false otherwise
 */
bool isPermanentObject(const uint8_t* objectId, uint32_t objectIdLen);

/**
 * @brief Save data as persistent object
 * @param[in] alias - persistent object alias
 * @param[in] data - data to be saved
 * @param[in] dataSize - data size
 * @return status of the operation, e.g. TEE_SUCCESS on success
 */
TEE_Result saveToPersistentObject(const char* alias, uint8_t* data, uint32_t dataSize);

/**
 * @brief Read data from persistent object
 * @param[in] alias - persistent object alias
 * @param[in] data - allocated buffer to write into
 * @param[in] dataSize - buffer size(should be more or equal to data in object)
 * @return status of the operation, e.g. TEE_SUCCESS on success
 */
TEE_Result readFromPersistentObject(IN const char* alias, OUT uint8_t* data, IN OUT uint32_t *dataSize);

/**
 * @brief Delete persistent object
 * @param[in] objectId - object ID
 * @param[in] objectIdLen - object ID length
 * @return status of the operation, e.g. TEE_SUCCESS on success
 */
TEE_Result deletePersistentObject(const uint8_t* objectId, uint32_t objectIdLen);

/**
 * @brief clearSFS for re-flash binary or Factory reset caess
 *        delete files not permanent (just delete ASK AuthKey...)
 * @return execution result. TEE_SUCCESS or not
 */
TEE_Result clearSFS();
#endif
