/**
 * Copyright (C) 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Mobile Communication Division,
 * Digital Media & Communications Business, Samsung Electronics Co., Ltd.
 *
 * This software and its documentation are confidential and proprietary
 * information of Samsung Electronics Co., Ltd.  No part of the software and
 * documents may be copied, reproduced, transmitted, translated, or reduced to
 * any electronic medium or machine-readable form without the prior written
 * consent of Samsung Electronics.
 *
 * Samsung Electronics makes no representations with respect to the contents,
 * and assumes no responsibility for any errors that might appear in the
 * software and documents. This publication and the contents hereof are subject
 * to change without notice.
 *
 */

/**
 * @file tz_hdcp2.h
 * @author
 * @date
 * @brief This file contains definition of all the functions used in SWD
 */
#ifndef _TZ_HDCP2_H_
#define _TZ_HDCP2_H_

#ifdef __cplusplus
extern "C"{
#endif

#include "tz_hdcp2_common.h"
#include <time.h>
#include <stdio.h>
#include "qsee_core.h"
#include "qsee_sfs.h"
#include "qsee_fs.h"
#include "qsee_heap.h"
#include "qsee_prng.h"
#include "qsee_comstr.h"
#include "qsee_cipher.h"
#include "qsee_hash.h"
#include "qsee_hmac.h"
#include "qsee_log.h"

/**
 * @def HDCP2_OK
 *
 * This macro is assigned a value of 0, which specifies the operation is successful.
 *
 */
#define HDCP2_OK	0

#ifdef DEBUG
#define HDCP_TZ_LOG	QSEE_LOG
#else
#define HDCP_TZ_LOG
#endif /* DEBUG */

#ifdef CONFIG_MSM8996
#define boolean bool
#define uint32 uint32_t
#define uint8 uint8_t
#define uint16 uint16_t
#define uint64 uint64_t
#else
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
#endif /* CONFIG_MSM8996 */

/**
 * @struct timeval_nwd_
 * This structure contains information of timestamps.
 */
typedef struct timeval_nwd_ {
	time_t tv_sec;
	// __suseconds_t tv_usec;
	// test .. change TODO:
} timestamp_t;

/**
 * @def HDCP2_VERSION22
 * This macro is assigned a value of 22 to be maintained everywhere.
 */
#define HDCP2_VERSION22	22

// size = 160 bytes
/**
 * @struct Pairing_Info_
 * This structure contains information regarding Pairing info.
 */
typedef struct Pairing_Info_
{
	unsigned char m[16];
	unsigned char km[16];
	unsigned char Ekm[16];
	unsigned char receiver_id[5];
	unsigned char padding[107]; // for encryption
} HDCP2_PAIRING_INFO;

/**
 * @struct Pairing_Info_Box
 * This structure contains information regarding msg_id and Pairing info.
 */
typedef struct Pairing_Info_Box
{
	unsigned char msg_id;
	HDCP2_PAIRING_INFO pairing_info;
} HDCP2_PAIRING_INFO_BOX;

/**
 * @struct TZ_HDCP2_CTX_
 * This structure contains information regarding HDCP2 context.
 */
typedef struct TZ_HDCP2_CTX_ {
	AKE_RECEIVER_INFO receiver_info;
	AKE_TRANSMITTER_INFO transmitter_info;
	HDCP2_PAIRING_INFO pairing_info;
	uint8_t dcp_llc_kpub[384];
	uint8_t REPEATER;
	uint8_t repeater_ack[16];
	uint8_t M[32];
	uint8_t seq_num_M[3];
	uint8_t seq_num_V[3];
	uint8_t no_of_streams[2];
	Downstream_Params multi_stream[16];
	uint8_t rtx[8];
	uint8_t rrx[8];
	uint8_t ctr[8];
	uint8_t rn[8];
	uint8_t kd[32];
	uint8_t dkey[16];
	uint8_t ks[16];
	uint8_t riv[8];
	int is_paired;
	int version;
	uint8_t hdcp_version;
	timestamp_t time_ake_init;
	timestamp_t time_ske_done;
	timestamp_t time_rep_stream_manage_done;
	timestamp_t ts;
	uint8_t L_msb[16];
	uint8_t L_lsb[16];
	int reauth_request;
	uint8_t Tx_LC_Precompute;
	uint8_t Rx_LC_Precompute;
	uint8_t Precompute_flag;
	uint8_t streamCtrj[4];
	uint8_t type1_stream;
	uint8_t type1_set;
	uint8_t ContentStreamIDj[2];
	uint8_t Type;
} TZ_HDCP2_CTX;

/**
 * @fn int TZ_AKE_SET_PAIRING_INFO_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size)
 * @brief This function receives the pairing info from receiver
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing the HDCP2_PAIRING_INFO message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_SET_PAIRING_INFO_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_HDCP2_LOADKEY_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function loads the key provisioning key
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_HDCP2_LOADKEY_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Init_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function initiates the authentication from transmitter by sending the initiation message which contains 64 bit pseudo random value
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing AKE_INIT message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Init_T(const unsigned char command, unsigned char *request, const u32 req_size,
			unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Transmitter_Info_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function sends the transmitter info to the receiver
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing AKE_TRANSMITTER_INFO message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Transmitter_Info_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Send_Cert_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief A certificate is  received from the receiver which indicates whether the connected receiver is an HDCP receiver and also extracts Receiver ID
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing AKE_SEND_CERT message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Send_Cert_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Receiver_Info_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function gets the information regarding compliance of receiver with various hdcp versions
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing AKE_RECEIVER_INFO message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Receiver_Info_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_No_Store_km_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function is called when transmitter does not have 128 bit master key stored corresponding to the receiver ID.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing AKE_NO_STORED_KM message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_No_Store_km_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Store_km_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function is called when transmitter has 128 bit master key corresponding to the receiver ID.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing AKE_STORED_KM message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Store_km_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Send_rrx_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function receives AKE_Send_rrx message from receiver containing the 64 bit pseudo random value
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing AKE_SEND_RRX message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Send_rrx_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Send_h_prime_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function receives AKE_SEND_H_PRIME message from the receiver which contains 256-bit H-prime. This message has to be received within 200ms in case of stored_km and within 1s in case of no_stored_km.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing AKE_SEND_H_PRIME message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Send_h_prime_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Send_Pairing_Info_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function receives AKE_SEND_PAIRING_INFO message from receiver which contains 128 bit Ekh(km).
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing AKE_SEND_PAIRING_INFO message
 * @param req_size - size of request
 * @param response- pointer to response containing HDCP2_PAIRING_INFO message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Send_Pairing_Info_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_LC_Init_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function initiates locality check by sending a message containing 64 bit pseudo random number to hdcp receiver
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing LC_INIT message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_LC_Init_T(const unsigned char command, unsigned char *request, const u32 req_size,
			unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_RTT_Challenge_T(const unsigned char command, unsigned char *request,const u32 req_size, unsigned char *response, uint32_t *res_size);
 * @brief This function sends the message containing the least significant 128 bits of L to the receiver
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing RTT_CHALLENGE message
 * @param req_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_RTT_Challenge_T(const unsigned char  command, unsigned char *request, const u32 req_size,
			unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_LC_Send_L_prime_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function receives a message containing most significant bits of L- prime from receiver. This is compared with L. The time elapsed between sending RTT_Challenge_T and receiving L-prime must be less than 7ms.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing LC_SEND_L_PRIME_PC message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_LC_Send_L_prime_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_SKE_Send_Eks_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function sends a message containing Edkey(ks) and a 64 bit pseudo random number to the hdcp receiver
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing SKE_SEND_EKS message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_SKE_Send_Eks_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int int TZ_ENC_Data(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function encrypts the plain text sent to it.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing the data stream to be encrypted
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_ENC_Data(const u8 command, u8 *request, const u32 req_size,
			u8 *response, u32 *res_size);

/**
 * @fn int TZ_HW_Init_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function gets crypto device lock, initializes the secure memory,performs the integrity check and sets the hdcp2 flag by initializing the trustzone
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing SECMEM_INIT_CMD message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_HW_Init_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_HW_Close_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function closes the trustzone, releases the secure memory and  the crypto device lock
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_HW_Close_T(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_OEM_SET_FLAG(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function closes the trustzone, releases the secure memory and  the crypto device lock
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_OEM_SET_FLAG(const u8 command, u8 *request, const u32 req_size,
			u8 *response, u32 *res_size);

/**
 * @fn int TZ_OEM_GET_FLAG(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This functions checks for OEM information
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_OEM_GET_FLAG(const u8 command, u8 *request, const u32 req_size,
			u8 *response, u32 *res_size);

/**
 * @fn int TZ_DRM_Set_HDCP2_MagicKey(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function is sets the Magic Key
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_DRM_Set_HDCP2_MagicKey(const u8 command, u8 *request, const u32 req_size,
			u8 *response, u32 *res_size);

/**
 * @fn int TZ_HDCP2_LOADKEY_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function loads the key provisioning key.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_HDCP2_LOADKEY_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_SET_PAIRING_INFO_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function Generates Ekh_km and send it to the transmitter.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_SET_PAIRING_INFO_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Init_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function Copies rtx sent by transmitter to the TZ HDCP Context.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing AKE_INIT message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Init_R(const unsigned char command, unsigned char *request, const u32 req_size,
			unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Transmitter_Info_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief  This function Copies transmitter info received into TZ HDCP Context.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing AKE_TRANSMITTER_INFO message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Transmitter_Info_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Send_Cert_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function Copies and sends receiver certificate to transmitter.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing AKE_SEND_CERT message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Send_Cert_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Receiver_Info_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function Forms receiver information and sends it to transmitter.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing AKE_RECEIVER_INFO message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Receiver_Info_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_No_Store_km_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function Receives Ekpub_km from transmitter and Decrypts it to get km using RSA_OAEP_decrypt and copies it to the TZ HDCP Context
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing AKE_NO_STORED_KM message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_No_Store_km_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Store_km_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function Receives Ekh_km from transmitter and Decrypts it to get km and copies it to the TZ HDCP Context
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing AKE_STORED_KM message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Store_km_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Send_rrx_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function Generates and sends 8bytes of random number[rrx] to transmitter.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing AKE_SEND_RRX message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Send_rrx_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Send_h_prime_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function Generates H prime and send it to the transmitter as below eqn.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing AKE_SEND_H_PRIME message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Send_h_prime_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_AKE_Send_Pairing_Info_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function Generates Ekh_km and send it to the transmitter.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing AKE_SEND_PAIRING_INFO message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_AKE_Send_Pairing_Info_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_LC_Init_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function Copies received random value [rn] from transmitter
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing LC_INIT message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_LC_Init_R(const unsigned char command, unsigned char *request, const u32 req_size,
			unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_RTT_CHALLENGE_R(const unsigned char command, unsigned char *request,const u32 req_size, unsigned char *response, uint32_t *res_size);
 * @brief This function copies received least significant 128 bits of L from transmitter.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request RTT_CHALLENGE message
 * @param req_size - size of request
 * @param response- pointer to response containing
 * @param req_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_RTT_CHALLENGE_R(const unsigned char command, unsigned char *request, const u32 req_size,
			unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_LC_Send_L_prime_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function calaculates L by equation : HMAC_SHA256(rn, (kd ^ rrx) )
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing LC_SEND_L_PRIME_PC message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_LC_Send_L_prime_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_RTT_READY_R(const unsigned char command, unsigned char *request,const u32 req_size, unsigned char *response, uint32_t *res_size);
 * @brief This function sends RTT_Ready message to transmitter when L' computation is complete.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing RTT_READY message
 * @param req_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_RTT_READY_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_SKE_Send_Eks_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function decrypts Eks using riv
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing SKE_SEND_EKS message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_SKE_Send_Eks_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_SET_HDCP_VERSION_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function sets the HDCP version on receiver side.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param req_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_SET_HDCP_VERSION_R(const u8 command, u8 *request, const u32 req_size,
			u8 *response, u32 *res_size);

/**
 * @fn int TZ_DEC_Data(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function decrypts the encrypted data stream.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing CIP_DATA_INFO_RX message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_DEC_Data(unsigned char command, unsigned char *request, const u32 req_size,
			unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_HW_Init_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function gets crypto device lock, initializes the secure memory and sets the hdcp2 flag by initializing the trustzone
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_HW_Init_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_HW_Close_R(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function closes the trustzone, releases the secure memory and  the crypto device lock
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_HW_Close_R(const unsigned char command, unsigned char *request,
			const u32 req_size, unsigned char *response, uint32_t *res_size);

/**
 * @fn int TZ_SRM_Check(uint8_t *receiverID)
 * @brief This function checks if the receiver ID is blacklisted
 * @param receiverID - pointer to receiver ID
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_SRM_Check(unsigned char *receiverID);

/**
 * @fn int TZ_RepeaterAuth_Send_ReceiverId_List_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function gets the message containing the information regarding the receivers connected to the repeater
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_RepeaterAuth_Send_ReceiverId_List_T(const u8 command, u8 *request,
			const u32 req_size, u8 *response, u32 *res_size);

/**
 * @fn int TZ_RepeaterAuth_Send_ReceiverId_List20_T(uint8_t *request);
 * @brief This function gets the message containing the information regarding the hdcp2.0 compliant receivers connected to the repeater
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing REPEATERAUTH_SEND_RECEIVER_ID_LIST20 message
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_RepeaterAuth_Send_ReceiverId_List20_T(unsigned char *request);

/**
 * @fn int TZ_RepeaterAuth_Send_ReceiverId_List21_T(uint8_t *request);
 * @brief This function gets the message containing the information regarding the hdcp2.1 compliant receivers connected to the repeater
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing REPEATERAUTH_SEND_RECEIVER_ID_LIST21 message
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_RepeaterAuth_Send_ReceiverId_List21_T(unsigned char *request);

/**
 * @fn int TZ_RepeaterAuth_Send_Ack_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function sends an acknowledgment to the receiver
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response  containing REPEATERAUTH_SEND_ACK message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_RepeaterAuth_Send_Ack_T(const u8 command, u8 *request, const u32 req_size,
			u8 *response, u32 *res_size);

/**
 * @fn int TZ_Receiver_AuthStatus_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function lets the transmitter know about the authentication status of receiver with the repeater
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing REPEATERAUTH_SEND_ACK message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_Receiver_AuthStatus_T(const u8 command, u8 *request, const u32 req_size,
			u8 *response, u32 *res_size);

/**
 * @fn int TZ_RepeaterAuth_Stream_Manage_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function propagates content stream management information, which includes Type values assigned to the content streams
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response containing REPEATERAUTH_STREAM_MANAGE message
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_RepeaterAuth_Stream_Manage_T(const u8 command, u8 *request, const u32 req_size,
			u8 *response, u32 *res_size);

/**
 * @fn int TZ_RepeaterAuth_Stream_Ready_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function obtains M- prime as sent by repeater
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request containing REPEATERAUTH_STREAM_READY message
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_RepeaterAuth_Stream_Ready_T(const u8 command, u8 *request, const u32 req_size,
			u8 *response, u32 *res_size);

/**
 * @fn int TZ_SET_HDCP_VERSION_T(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief This function sets the HDCP version on transmitter side.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param req_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_SET_HDCP_VERSION_T(const u8 command, u8 *request, const u32 req_size,
			u8 *response, u32 *res_size);

/**
 * @fn int TZ_COMMAND(const uint8_t command, uint8_t *request,const u32 req_size, uint8_t *response, uint32_t *res_size);
 * @brief The control comes to this function when secure world is entered and the command to be executed next is decided here only.
 * @param command - The command to be executed, sent from NWD
 * @param request- pointer to request
 * @param req_size - size of request
 * @param response- pointer to response
 * @param res_size - size of response
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int TZ_COMMAND(const unsigned char command, unsigned char *request, const u32 req_size,
			unsigned char *response, uint32_t *res_size);

/**
 * @fn void GetTimeStamp(timestamp_t *ts);
 * @brief This function gets the current time stamp
 * @param ts - pointer to the timestamp_t that is to be populated here
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
void GetTimeStamp(timestamp_t *ts);

#ifdef __cplusplus
}
#endif

#endif /* !_TZ_HDCP2_H_ */
