/*
    This file is part of MutekH.
    
    MutekH is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as
    published by the Free Software Foundation; version 2.1 of the
    License.
    
    MutekH is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with MutekH; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301 USA.

    Copyright Jeremie Brunel <jeremie.brunel@telecom-paristech.fr> (c) 2013
*/

#ifndef _LIBSSM_H
#define _LIBSSM_H

#include <hexo/types.h>
#include <libssm/hsm.h>
#include <errno.h>

/**
  Security Policy summary.
*/
struct sp_s
{
  bool_t c;                  // < confidentiality flag
  bool_t i;                  // < integrity flag
  enum hsm_prot_mode_e mode; // < protection mode
  uint16_t idx;              // < security policy index
};

/**
  Initialize SSM. Defines location of Master Block, location and size of protected region in external memory. Parameters:
  @param mb_addr Master Block byte base address (8 MSBs only, Master Block is always aligned on a 16MB boundary)
  @param ram_p_addr Byte start address of protected memory area (8 MSBs onlys, protected area is always aligned on a 16MB boundary)
  @param ram_p_size Size of protected memory area (log4 of byte size / 16MB, 1 to 4, 1=64MB, 2=256MB, 3=1GB, 4=4GB)
  @return: Zero.
*/ 
error_t sb_ssm_init(uint32_t ram_p_addr, size_t ram_p_size, uint32_t mb_addr);

/**
  Returns a Security Policy with requested confidentiality and integrity mode. Creates a new Security Policy if none is found or if @tt new flag is set. Parameters:
  @param c Confidentiality mode. Memory pages bound to returned SP are encrypted if true.
  @param i Integrity mode. Memory pages bound to returned SP have their integrity managed and checked if true.
  @param mode Page type. Specifies whether memory pages bound to returned SP are read-only or read-write @see hsm_prot_mode_e.
  @param new If set create a new Security Policy, else try to use an existing one, if any.
  @return: Zero.
*/
struct sp_s *sb_ssm_get_policy(bool_t c, bool_t i, enum hsm_prot_mode_e mode, bool_t new);

/**
  Create a protected memory area. Allocates extra memory to store MAC sets and MAC trees if needed. In current version memory area is split in 4kB pages only. Future versions will make use of larger pages if possible. Parameters:
  @param start_address Byte start address of memory area. Must be a multiple of 4kB.
  @param size Byte size of memory area. Must be a multiple of 4kB.
  @param sp Security Policy summary to apply to memory area @see sp_s.
  @return: Protected size.
*/
size_t sb_ssm_new_protected_segment(uintptr_t start_address, size_t size, struct sp_s* sp);

/**
  Invalidate a Security Policy. Parameters:
  @param sp pointer to Security Policy summary.
  @return: Zero.
 */
error_t sb_ssm_cleanup_sp(struct sp_s *sp);

/**
  Clean up a whole protected memory area. Parameters:
  @param start_address Byte address of memory area.
  @return: Zero.
*/ 
error_t sb_ssm_cleanup_segment(uintptr_t start_address, size_t size);

/**
  Copy memory to memory using HSM's atomic group read-write. Parameters:
  @param src Byte source address.
  @param dest Byte destination address.
  @param size Byte size of source and destination memory areas.
  @return: Zero.
 */
error_t sb_ssm_load_segment(uintptr_t src, uintptr_t dest, size_t);

#endif
