
/*
 * =====================================================================================
 *
 *       Filename:  pebble_hash.h
 *
 *    Description:  PEBBLE definitions for hash manipulation
 *
 *        Version:  1.0
 *        Created:  06/03/2020
 *       Revision:  none
 *       Compiler:  gcc
 *
 *        Company:  Samsung Electronics
 *        Copyright (c) 2020 by Samsung Electronics, All rights reserved.
 *
 * =====================================================================================
 */

#ifndef _PEBBLE_HASH_H_
#define _PEBBLE_HASH_H_

/**
 * PEBBLE includes
 */
#include "tz_pebble_interface.h"
#include "pebble_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

/**
 * @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 PEBBLE status code
*/
pebble_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_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 PEBBLE status code
*/
pebble_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 /* _PEBBLE_HASH_H_ */
