/*
 * Copyright (c) 2017 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: Dec 13, 2017
 * Author: Kostiantyn Volobuiev <k.volobuyev@samsung.com>
 * Brief: Module that responds for hash operations.
 */

#ifndef TZWHASH_H
#define TZWHASH_H

#include "TzwCommon.h"
#include "TzwMisc.h"

/**
 * @brief Struct to operate with cipher handle
 */
struct __TzwHashOperation;

typedef struct __TzwHashOperation* TzwHashOperation_t; /* Opaque handle like in GP */

/**
 * @brief Allocate a hash context for update and final functions
 * @param[out] object - the hash operation
 * @param[in] algorithm - the algorithm standard to use
 * @return TEE error code
 */
TzwErrorCode_t tzwAllocateHashOperation(TzwHashOperation_t* object, uint32_t algorithm);

/**
 * @brief Hash some data into the hash context structure,
 *        which must have been allocated by tzwAllocateHashOperation()
 * @param[in] object - the hash operation
 * @param[in] chunk - pointer to the msg to hash
 * @param[in] object - length of the msg to hash
 * @return TEE error code
 */
TzwErrorCode_t tzwHashUpdate(TzwHashOperation_t object, void* chunk, size_t chunkSize);

/**
 * @brief Compute the digest hash value
 * @param[in] object - the hash operation
 * @param[in] chunk - pointer to the msg to hash
 * @param[in] object - length of the msg to hash
 * @param[out] hash - pointer to output message digest hash
 * @param[out] hashLen   Length of the output message digest hash buffer
 * @return TEE error code
 */
TzwErrorCode_t tzwHashFinalize(TzwHashOperation_t object,
                                 void* chunk, size_t chunkLen,
                                 void* hash, size_t *hashLen);

/**
 * @brief Release all resources with a given hash context
 * @param[in] object - the hash operation
 */
void tzwFreeHashOperation(TzwHashOperation_t object);

/**
 * @brief Create a hash MAC
 * @param[in] keyBytes - pointer to input key to HMAC algorithm
 * @param[in] keySize - length of input key in bytes
 * @param[in] tbs - pointer to message to be authenticated
 * @param[in] tbsLen - length of message
 * @param[out] hash - pointer to message digest (memory provided by caller)
 * @return status of the operation, e.g. TEE_SUCCESS on success
 */
TEE_Result tzwHmac(const void* keyBytes, uint32_t keySize, const uint8_t* tbs, const uint32_t tbsLen, void* hash);

#endif // TZWHASH_H
