/**
  ******************************************************************************
  * @file           : provisioning.h
  * @brief          : Header for provisioning module.
  *                   This file contains the common defines and structures for provisioning.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */

#ifndef _PROVISIONING_H_
#define _PROVISIONING_H_

#ifdef __cplusplus
extern "C" {
#endif

/** @addtogroup PROVISIONING_MNG
  * @{
  */

/* Includes ------------------------------------------------------------------*/
#include "ACWG_Security.h"
#include "stdbool.h"
#include "provisioning_info.h"

/** @addtogroup Provisioning_Exported_Constants
  * @{
  */
/* Exported constants --------------------------------------------------------*/

/**
 * @def PRIVATE_KEY_SIZE_BYTES
 * @brief Size of private key in bytes.
 */
#define PRIVATE_KEY_SIZE_BYTES          ((uint32_t)32)
/**
 * @def AUTH_PUBKEY_LEN_BYTES
 * @brief Length of authentication public key in bytes.
 */
#define AUTH_PUBKEY_LEN_BYTES           ((uint32_t)64)
/**
 * @def GROUP_RESOLVING_KEY_SIZE_BYTES
 * @brief Size of group resolving key in bytes.
 */
#define GROUP_RESOLVING_KEY_SIZE_BYTES  ((uint32_t)16)
/**
 * @def DEVICE_NAME_SIZE_BYTES
 * @brief Size of device name in bytes.
 */
#define DEVICE_NAME_SIZE_BYTES          ((uint32_t)16)
/**
 * @def B1_VENDOR_SPEC_EXT_SIZE_BYTES
 * @brief Size of vendor specific extension in bytes.
 */
#define B1_VENDOR_SPEC_EXT_SIZE_BYTES   ((uint32_t)64)
/**
 * @def RESERVED_PAIRING_KEYS_SIZE_BYTES
 * @brief Size of buffer in bytes reserved for future use .
 */
#define RESERVED_PAIRING_KEYS_SIZE_BYTES   ((uint32_t)32)
/**
 * @def CERTIFICATE_SIZE
 * @brief Size of certificate data in bytes.
 */
#define CERTIFICATE_SIZE                ((uint32_t)400)

/**
 * @def NUM_PROV_DEVICES
 * @brief Maximum number of provisioned devices.
 */
#define NUM_PROV_DEVICES                5


/**
  * @}
  */

/** @addtogroup Provisioning_Exported_Variables
  * @{
  */
/* Exported variables ------------------------------------------------------------*/

/**
  * @}
  */

/** @addtogroup Provisioning_Exported_Types
  * @{
  */
/* Exported types ------------------------------------------------------------*/

/**
 * @struct devices_t
 * @brief Structure representing a provisioned device.
 */
typedef struct devices_s
{
  uint8_t name[DEVICE_NAME_SIZE_BYTES];                /**< Device name */
  uint8_t endpoint_Keyslot[AUTH_KEYSLOT_LEN_BYTES];    /**< Endpoint keyslot */
  uint8_t endpoint_PubK[AUTH_PUBKEY_LEN_BYTES];        /**< Endpoint public key */
  uint8_t endpoint_PrivK[PRIVATE_KEY_SIZE_BYTES];      /**< Endpoint private key */
}devices_t;


/**
 * @struct provstruct_t
 * @brief Structure representing the provisioning data.
 */
typedef struct provstruct_s
{
  uint32_t isValid;                                         /**< Validity flag */
  uint8_t readerID[AUTH_READER_GROUP_ID_LEN_BYTES + AUTH_READER_GROUP_SUB_ID_LEN_BYTES]; /**< Reader ID */
  uint8_t readerPubK[AUTH_PUBKEY_LEN_BYTES];                 /**< Reader public key */
  uint8_t staticPrivateKey[PRIVATE_KEY_SIZE_BYTES];          /**< Static private key */
  uint8_t group_resolving_key[GROUP_RESOLVING_KEY_SIZE_BYTES]; /**< Group resolving key */
  uint8_t certificate_data[CERTIFICATE_SIZE];                /**< Certificate data */
  uint32_t certificate_len;                                  /**< Certificate length */
  uint8_t readerIssuerPubK[AUTH_PUBKEY_LEN_BYTES];           /**< Reader issuer public key */
  uint8_t b1VendorSpecificExtension[B1_VENDOR_SPEC_EXT_SIZE_BYTES]; /**< Vendor specific extension */
  uint32_t b1VendorSpecificExtensionLen;                     /**< Vendor specific extension length */
  uint32_t numDevices;                                      /**< Number of devices */
  devices_t devices[NUM_PROV_DEVICES];                      /**< Array of provisioned devices */
  uint32_t isValidReserved;                                   /**< RESERVED validity flag */
  uint8_t reservedPairingKeys[RESERVED_PAIRING_KEYS_SIZE_BYTES]; /**< RESERVED pairing keys */
  uint32_t struct_size;                                     /**< Structure size */
}provstruct_t;
/**
  * @}
  */

/** @addtogroup Provisioning_Exported_Functions
  * @{
  */
/* Exported functions ------------------------------------------------------- */

/**
 * @brief Shows the status of the JSON provisioning data.
 */
void showJsonStatus();

/**
 * @brief Shows the status of the provisioning data.
 */
void showProvisioningStatus();

/**
 * @brief Saves the provisioning data to flash memory.
 * @param profile_num Profile number to save.
 */
void saveProvisioningToFlash(uint32_t profile_num);

/**
 * @brief Retrieves the provisioning data from flash memory.
 * @return Pointer to the provisioning structure.
 */
provstruct_t* getProvisioningFromFlash();

/**
 * @brief Loads provisioning data from a JSON address.
 * @param jsonAddress Address of the JSON data.
 * @param provstruct Pointer to the provisioning structure to fill.
 */
void loadProvisionFromJson(uint32_t jsonAddress, provstruct_t*provstruct);

/**
 * @brief Converts provisioning data from JSON format.
 * @param jsonAddress Address of the JSON data.
 */
void convertProvisioningFromJson(uint32_t jsonAddress);

/**
 * @brief Converts default provisioning data from JSON format.
 */
void convertDefaultProvisioningFromJson();

/**
 * @brief Prints the device JSON data.
 */
void printDeviceJson();

/**
 * @brief Prints the script JSON data.
 */
void printScriptJson();


/**
 * @brief Saves the reader public key to flash memory.
 * @param pkey Pointer to the public key.
 * @param ksize Size of the key.
 */
void saveRDPubKToFlash(const uint8_t *pkey, const uint8_t ksize);

/**
 * @brief Saves the reader private key to flash memory.
 * @param pkey Pointer to the private key.
 * @param ksize Size of the key.
 */
void saveRDPrvKToFlash(const uint8_t *pkey, const uint8_t ksize);

/**
 * @brief Saves the reader identity to flash memory.
 * @param pkey Pointer to the identity data.
 * @param ksize Size of the identity data.
 */
void saveRDidenToFlash(const uint8_t *pkey, const uint8_t ksize);

/**
 * @brief Saves the reader group resolving key to flash memory.
 * @param pkey Pointer to the group resolving key.
 * @param ksize Size of the key.
 */
void saveRDgrrkToFlash(const uint8_t *pkey, const uint8_t ksize);

/**
 * @brief Saves the credential issuer public key to flash memory.
 * @param pkey Pointer to the credential issuer public key.
 * @param ksize Size of the public key.
 */
void saveCredIsPkToFlash(const uint8_t *pkey, const uint8_t ksize);

/**
 * @brief Saves the access credential public key.
 * @param last_pkey Pointer to the last public key.
 * @param ksize Size of the key.
 * @param nSlot Slot number.
 */
void saveAccessCredentialPuBK(uint8_t *pkey, const uint8_t ksize, uint8_t nSlot, uint8_t *pkeyname);

/**
 * @brief Checks if a buffer contains only zeros.
 * @param pBuf Pointer to the buffer.
 * @param nSize Size of the buffer.
 * @return 1 if all zeros, 0 otherwise.
 */
uint8_t is_zeros(uint8_t* pBuf, uint32_t nSize);

/**
 * @brief Checks the endpoint public key for provisioning.
 * @param endp_PubK Pointer to the endpoint public key.
 * @return Status code.
 */
uint8_t ProvisioningCheckEndpointPubK(uint8_t *endp_PubK);

/**
 * @brief Gets the endpoint public key from a keyslot.
 * @param endp_PubK Pointer to the endpoint public key.
 * @param endp_Keyslot Pointer to the endpoint keyslot.
 * @param slotFound Pointer to the found slot.
 * @return Status code.
 */
uint8_t ProvisioningGetEndpointPubKFromKeyslot(uint8_t *endp_PubK, uint8_t *endp_Keyslot, uint8_t*slotFound);

/**
 * @brief Converts a hex string to a hex buffer.
 * @param p Pointer to the hex string.
 * @param len Length of the string.
 * @param hexp Pointer to the hex buffer.
 */
void hexStrToHex(char *p, uint32_t len, uint8_t *hexp);

/**
  * @}
  */

/**
  * @}
  */  
 
#ifdef __cplusplus
}
#endif

#endif //_PROVISIONING_H_
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
