
/*
 * =====================================================================================
 *
 *       Filename:  hdm_hash.h
 *
 *    Description:  HDM definitions for hash manipulation
 *
 *        Version:  1.0
 *        Created:  09/16/2019 15:26:11 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *        Company:  Samsung Electronics
 *        Copyright (c) 2015 by Samsung Electronics, All rights reserved.
 *
 * =====================================================================================
 */

#ifndef _HDM_HASH_H_
#define _HDM_HASH_H_

/**
 * HDM includes
 */
#include "tz_hdm_interface.h"
#include "hdm_defs.h"

/**
 * External includes
 */
#include "base64.h"
#include <openssl/crypto.h>
#include <openssl/sha.h>
#include <openssl/evp.h>

/**
 * Lengths
 */
#define IMEI_LEN           15
#define SERIAL_NUMBER_LEN  20
#define MAC_LEN            17

/**
 * @brief
 * compute_hash
 * Computes hash of given type for the given input string.
 *
 * @param[in]  msg     - message to hash
 * @param[in]  msg_len - length of the message
 * @param[out] out     - hashed message
 * @param[out] out_len - length of the hashed message
 * @param[in]  evp_md  - hash function to use
 *
 * @return HDM status code
*/
hdm_return_code_t compute_hash(uint8_t *msg, uint32_t msg_len, uint8_t *out, uint32_t *out_len, const EVP_MD *evp_md);

/**
 * @brief
 * gen_rpmb_hash
 * Receives an empty buffer and fills it with the SHA256 of the concatenation of MAGIC, POLICY counter and DEVICE block.
 *
 * @param[in] payload    - payload with device_block and policy_version info
 * @param[out] rpmb_hash - buffer to be filled with the RPMB hash
 * @param[in] version    - rpmb data version
 * @param[out] len       - rpmb_hash lenght
 *
 * @return HDM status code
*/
hdm_return_code_t gen_rpmb_hash(void *payload, uint8_t *rpmb_hash, uint32_t *len, uint32_t version);

/**
 * @brief
 * compare_rpmb_hash
 * Generates the hash for the stored payload values and compares it against the hash value stored.
 *
 * @param[in] rpmb_data  - data from the rpmb
 * @param[in] version    - rpmb data verrsion
 * @return HDM status code
*/
hdm_return_code_t compare_rpmb_hash(void *rpmb_data, uint32_t version);

/**
 * @brief
 * gen_device_id
 * Compare and generates hash value of device ID
 *
 * @param[in]   *nwd_device_id      - Device ID acquired from normal world
 * @param[in]   *rpmb_device_id     - Device ID acquired from RPMB
 * @param[out]  *device_id_hash     - B64( H(device_id) )
 * @param[out]  *device_id_hash_len - B64 device_id length is fixed value 44 (DEVICE_ID_B64_LEN)
 *
 * @return HDM status code
 */
hdm_return_code_t gen_device_id(uint8_t *nwd_device_id, uint8_t *rpmb_device_id, uint8_t *device_id_hash, uint32_t *device_id_hash_len);

/**
 * @brief
 * gen_drk_device_id
 * Generates Device ID to match with DRKv2 Certificate
 *
 * @param[in]     hash_imei     - Hashed IMEI/MAC Address
 * @param[in]     serial_number - Serial Number
 * @param[out]    device_id     - B64(H(H(ID) | H(SN))), ID = ((IMEI_1 | IMEI_2) or MAC_ADDR)
 * @param[in|out] device_id_len - B64 device_id length is fixed value 44 (DEVICE_ID_B64_LEN)
 *
 * @return HDM status code
*/
hdm_return_code_t gen_drk_device_id(uint8_t *hash_imei, uint8_t *serial_number, uint8_t *device_id, uint32_t *device_id_len);

#endif /* _HDM_HASH_H_ */
