/**
  ******************************************************************************
  * @file    se_interface_application.c
  * @author  MCD Application Team
  * @brief   Secure Engine Interface module.
  *          This file provides set of firmware functions to manage SE Interface
  *          functionalities. These services are used by the application.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright(c) 2017 STMicroelectronics International N.V.
  * All rights reserved.</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted, provided that the following conditions are met:
  *
  * 1. Redistribution of source code must retain the above copyright notice,
  *    this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright notice,
  *    this list of conditions and the following disclaimer in the documentation
  *    and/or other materials provided with the distribution.
  * 3. Neither the name of STMicroelectronics nor the names of other
  *    contributors to this software may be used to endorse or promote products
  *    derived from this software without specific written permission.
  * 4. This software, including modifications and/or derivative works of this
  *    software, must execute solely and exclusively on microcontroller or
  *    microprocessor devices manufactured by or for STMicroelectronics.
  * 5. Redistribution and use of this software other than as permitted under
  *    this license is void and will automatically terminate your rights under
  *    this license.
  *
  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  *
  ******************************************************************************
  */
/* Place code in a specific section*/
#if defined(__ICCARM__)
#pragma default_function_attributes = @ ".SE_IF_Code"
#elif defined(__CC_ARM)
#pragma arm section code = ".SE_IF_Code"
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"                       /* se_interface_application.c is compiled in SBSFU project using main.h from this project */
#include "se_low_level.h"               /* This file is part of SE_CoreBin and adapts the Secure Engine (and its interface) to the STM32 board specificities */
#include "se_interface_common.h"
#include "se_callgate.h"
#include "se_interface_application.h"
#include "se_intrinsics.h"
/*
*/
/** @addtogroup SE Secure Engine
  * @{
  */

/** @defgroup  SE_INTERFACE Secure Engine Interface
  * @brief APIs the SE users (bootloader, user app) can call.
  * @note Please note that this code is compiled in the SB_SFU project (not in SE_CoreBin).
  *        Nevertheless, this file also includes "se_low_level.h" from SE_CoreBin to make sure the SE interface is in sync with the SE code.
  * @{
  */

/** @defgroup  SE_INTERFACE_APPLICATION Secure Engine Interface for Application
  * @brief Interface functions the User Application calls to use SE services.
  * @{
  *
  * "se_interface" is built and set in the context of the SB_SFU project.
  * Then some symbols are exported to the User Application for the User Application
  * to call some Secure Engine services through "se_interface"
  * (SB_SFU and the User Application are exclusive, they do not run in parallel).
  * As "se_interface" is set by SB_SFU but can be used by the User Application, it is important
  * not to introduce global variables if you do not want to create dependencies between the SB_SFU RAM mapping (SRAM1)
  * and the User Application RAM mapping.
  */

/* DO NOT ADD ANY VARIABLE HERE, SEE EXPLANATIONS ABOVE */

/** @defgroup SE_INTERFACE_APPLICATION_Exported_Functions Exported Functions
 *  @ Functions the User Application calls to run the SE services.
  * @{
  */

/**
  * @brief Service called by the User Application to retrieve the Active Firmware Info.
  * @param peSE_Status Secure Engine Status.
  * @param p_FwInfo Active Firmware Info structure that will be filled.
  * @retval SE_ErrorStatus SE_SUCCESS if successful, SE_ERROR otherwise.
  */
__root SE_ErrorStatus SE_APP_GetActiveFwInfo(SE_StatusTypeDef *peSE_Status, SE_APP_ActiveFwInfo *p_FwInfo)
{
  SE_ErrorStatus e_ret_status;

  /* Check the pointers allocation */
  if ((NULL == peSE_Status) || (NULL == p_FwInfo))
  {
    return SE_ERROR;
  }

  /* Set the CallGate function pointer */
  SET_CALLGATE();

  /*Enter Secure Mode*/
  SE_EnterSecureMode();

  /*Secure Engine Call*/
  e_ret_status = (*SE_CallGatePtr)(SE_APP_GET_ACTIVE_FW_INFO, peSE_Status, p_FwInfo);

  /*Exit Secure Mode*/
  SE_ExitSecureMode();

  return e_ret_status;
}


/**
  * @brief Service called by the User Application to .....
  * @param peSE_Status Secure Engine Status.
  * @param p_DataIn data to be processed
  * @param p_DataOut data processed
  * @param dataLen data length in bytes
  * @retval SE_ErrorStatus SE_SUCCESS if successful, SE_ERROR otherwise.
  */
__root SE_ErrorStatus SE_APP_ProcessMyData1(SE_StatusTypeDef *peSE_Status, uint8_t *p_DataIn, uint8_t *p_DataOut, uint32_t dataLen, uint8_t *p_Nonce, uint8_t *p_Tag) /* step 2 */
{
  SE_ErrorStatus e_ret_status;

  /* Check the pointers allocation */
  if ((NULL == peSE_Status) || (NULL == p_DataIn) || (NULL == p_DataOut))
  {
    return SE_ERROR;
  }

  /* step 2 */
  if ((NULL == p_Nonce) || (NULL == p_Tag))
  {
    return SE_ERROR;
  }

  /* Set the CallGate function pointer */
  SET_CALLGATE();

  /*Enter Secure Mode*/
  SE_EnterSecureMode();

  /*Secure Engine Call*/
  e_ret_status = (*SE_CallGatePtr)(SE_APP_USER_FUNC1, peSE_Status, p_DataIn, p_DataOut, dataLen, p_Nonce, p_Tag);

  /*Exit Secure Mode*/
  SE_ExitSecureMode();

  return e_ret_status;
}


/**
  * @brief Service called by the User Application to .....
  * @param peSE_Status Secure Engine Status.
  * @param p_DataIn data to be processed
  * @param p_DataOut data processed
  * @param dataLen data length in bytes
  * @retval SE_ErrorStatus SE_SUCCESS if successful, SE_ERROR otherwise.
  */
__root SE_ErrorStatus SE_APP_ProcessMyData2(SE_StatusTypeDef *peSE_Status, uint8_t *p_DataIn, uint8_t *p_DataOut, uint32_t dataLen, uint8_t *p_Nonce, uint8_t *p_Tag) /* step 2 */
{
  SE_ErrorStatus e_ret_status;

  /* Check the pointers allocation */
  if ((NULL == peSE_Status) || (NULL == p_DataIn) || (NULL == p_DataOut))
  {
    return SE_ERROR;
  }

  /* step 2 */
  if ((NULL == p_Nonce) || (NULL == p_Tag))
  {
    return SE_ERROR;
  }

  /* Set the CallGate function pointer */
  SET_CALLGATE();

  /*Enter Secure Mode*/
  SE_EnterSecureMode();

  /*Secure Engine Call*/
  e_ret_status = (*SE_CallGatePtr)(SE_APP_USER_FUNC2, peSE_Status, p_DataIn, p_DataOut, dataLen, p_Nonce, p_Tag);

  /*Exit Secure Mode*/
  SE_ExitSecureMode();

  return e_ret_status;
}


/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/* Stop placing data in specified section*/
#if defined(__ICCARM__)
#pragma default_function_attributes =
#elif defined(__CC_ARM)
#pragma arm section code
#endif


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
