
/*
 * =====================================================================================
 *
 *       Filename:  pebble_utils.h
 *
 *    Description:  PEBBLE utils functions
 *
 *        Version:  1.0
 *        Created:  06/03/2020
 *       Revision:  none
 *       Compiler:  gcc
 *
 *        Company:  Samsung Electronics
 *        Copyright (c) 2020 by Samsung Electronics, All rights reserved.
 *
 * =====================================================================================
 */

#ifndef _PEBBLE_UTILS_H_
#define _PEBBLE_UTILS_H_

/**
 * Conditional includes
 */
#ifdef CONFIG_QSEE
#else
#include "tees_secure_object.h"
#endif

/**
 * PEBBLE includes
 */
#include "tz_pebble_interface.h"
#include "pebble_defs.h"
#include "pebble_drk.h"
#include "pebble_x509.h"
#include "pebble_core.h"

/**
 * External includes
 */
#include "TZ_Vendor_tl.h"
#include <openssl/x509v3.h>
#include <openssl/crypto.h>

/**
 * Fit to uint32_t macro
 */
#define FIT_TO_UINT32(X) (X >= 0 && X <= 0xFFFFFFFF) ? (uint32_t)X : 0;

/**
 * @brief
 * hex2int
 * Converts hexadecimal to integer
 *
 * @param[in] *hex - hexadecimal number
 * @return Integer
 */
int64_t hex2int(uint8_t *hex);

/**
 * @brief
 * get_error_string
 * Get error string from error code
 *
 * @param[in] error_code - Error code
 *
 * @return The error string
 */
uint8_t* get_error_string(uint32_t error_code);

/**
 * @brief
 * hex_to_byte_stream
 * Transforms given HEX input string into a
 * byte formatted stream.
 *
 * @param[in]  hex_string - representation of HEX number
 * @param[in]  str_len    - length of the HEX string
 * @param[out] buff       - output pointer to the byte stream
 *
 * @return nothing
 */
void hex_to_byte_stream(uint8_t hex_string[], uint32_t str_len,
                        uint8_t **buff);

/**
 * @brief
 * change string "\x5cn" to "\n".
 *
 * @param[in/out]  certificate - pointer to certificate
 *
 * @return nothing
 */
void settle_certificate_to_openssl(uint8_t *certificate);

/**
 * Debug functions prototypes
 */
#ifdef DEBUG_PEBBLE
void dump_jws_header(tz_pebble_header_t *header);
void dump_jws_payload(tz_pebble_payload_t *payload);
void dump_jws_sinature(tz_pebble_signature_t *signature);
void printRange(uint8_t *buff, int len, char *tag);
void printLongRange(uint8_t *buff, int len, char *tag);
void printRangeHex(uint8_t *buff, int len, char *tag);
void printLongRangeHex(uint8_t *buff, int len, char *tag);
#endif /* DEBUG_PEBBLE */

#endif /* _PEBBLE_UTILS_H_ */
