
/*
 * =====================================================================================
 *
 *       Filename:  hdm_jws.h
 *
 *    Description:  HDM definitions for JWS manipulation
 *
 *        Version:  1.0
 *        Created:  09/16/2019 15:26:11 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *        Company:  Samsung Electronics
 *        Copyright (c) 2015 by Samsung Electronics, All rights reserved.
 *
 * =====================================================================================
 */

#ifndef _HDM_JWS_H_
#define _HDM_JWS_H_

/**
 * HDM includes
 */
#include "tz_hdm_interface.h"
#include "hdm_defs.h"
#include "hdm_utils.h"

/**
 * JWS parser
 */
#define HEADER_TYPE   0
#define PAYLOAD_TYPE  1
#define TAG_MAX_LEN   15

/**
 * Hashmap typedef
 */
typedef hdm_return_code_t (*hdm_malloc_values_func_t)(uint8_t* value, int len);

/**
 * HDM hashmap value
 */
typedef struct {
        uint8_t tag[TAG_MAX_LEN];
        hdm_malloc_values_func_t value;
}__attribute__ ((packed)) hdm_hashmap_value_t;

/**
 * JWS Lex parser states
 */
typedef enum {
        START = 0,
        BODY,
        BEGIN_TAG,
        BUILD_TAG,
        FINAL_TAG,
        CHECKPOINT,
        BEGIN_VALUE,
        BUILD_VALUE,
        FINAL_VALUE,
        BEGIN_ARR_BODY,
        BEGIN_ARR_ITEM,
        BUILD_ARR_ITEM,
        FINAL_ARR_ITEM,
        FINAL_ARR_BODY,
        END
} jws_lex_states_t;

/**
 * @brief
 * split_b64_jws
 * Split base64 policy JWS to fill structs
 *
 * @param[in]  *tci_msg                           - tci message
 * @param[out] *jwsB64                            - JWS base64 global struct
 * @param[out] *size_header_payload_for_signature - length for signature
 *
 * @return HDM status code
 */
hdm_return_code_t split_b64_jws(tci_message_t *tci_msg, tz_hdm_jws_t *jwsB64, uint32_t *size_header_payload_for_signature);

/**
 * @brief
 * fill_jws_msg
 * Extracts information on received JSON and set related fields on the corresponding structures.
 * The lexer works as a FSM.
 *
 * @param[in] msg_decoded - JSON in string format
 * @param[in] msg_len     - JSON lenght in decimal
 * @param[in] msg_type    - HEADER_TYPE or PAYLOAD_TYPE
 *
 * @return HDM status code
 */
hdm_return_code_t fill_jws_msg(uint8_t *msg_decoded, uint32_t msg_len, uint32_t msg_type);

/**
 * @brief
 * fill_jws_msg_signature
 * Fills signature global struct signature field.
 *
 * @param[in] *signature_decoded - decoded signature
 * @param[in] signature_len      - signature length
 *
 * @return HDM status code
 */
hdm_return_code_t fill_jws_msg_signature(uint8_t *signature_decoded, uint32_t signature_len);

/**
 * @brief
 * generate_signature
 * Sign using CKM_SHA256_RSA_PKCS algorithm.
 * 
 * @param[in]     message     - the message to be signed
 * @param[in]     message_len - the length of the message
 * @param[out]    signature   - output buffer for the generated signature
 * @param[in]     sig_len     - length of the signature buffer must be of size RESPONSE_SIGNATURE_LEN
 * @param[in]     rsa_key     - the private used to sign
 *
 * @return Status Code
 */
hdm_return_code_t generate_signature(uint8_t *message, uint32_t message_len, uint8_t *signature, uint32_t sig_len, drk_rsa_private_key_t *rsa_key);

/**
 * @brief
 * check_jws_device_id
 * Checks if the device_id received is valid or not
 *
 * @param[in] *device_id - device id
 * @param[in] *nwd_info  - structure with NWd information
 */
hdm_return_code_t check_jws_device_id(uint8_t *device_id, hdm_nwd_info_t nwd_info);

#endif /* _HDM_JWS_H_ */
