/*
@file ipprotector_api.h
@brief This file contains all definitions used in ipprotector
*/
/*===========================================================================
  Copyright (c) 2018, 2021 by Qualcomm Technologies, Incorporated.
  All Rights Reserved.
  Confidential and Proprietary - Qualcomm Technologies, Inc.
===========================================================================*/

/*===========================================================================

                            EDIT HISTORY FOR FILE

# when       who     what, where, why
# --------   ---     ---------------------------------------------------------
===========================================================================*/

#ifndef __IPPROTECTOR_API_H__
#define __IPPROTECTOR_API_H__

enum error_codes {
    /* IP Protector Lib error codes */
    IP_OUT_OF_MEMORY = 100,
    IP_MISSING_BUFFER,
    IP_INVALID_PARAMETER,
    IP_OBJECT_ERROR,
    /* Secure Image Kernel Service error codes are between 200 and 300 */
    IP_KERNEL_ERROR = 200,
    /* ELF Parser error codes */
    IP_MISSING_LOAD_SEGMENT = 300,
    IP_INVALID_NUMBER_OF_LOAD_SEGMENTS,
    IP_INVALID_SEGMENT_OFFSET
};

/*
 * Description: Decrypt ELF. No signature verification will be performed.
 *              In order for decryption to be successful:
 *              1) ELF must have been encrypted via Sectools using a
 *                 Feature ID value matching feat_id.
 *              2) Sectools must have encrypted the ELF using the
 *                 public key retrieved via ipprotector_get_public_key
 *                 on a device with the same configured values for MSA,
 *                 APPS Secure, OEM ID, OEM RCH, OTP UIE L1 Key, and
 *                 Feature ID.
 *
 * In:          feat_id: Feature ID to use during key derivation.
 *              image_addr: Address of buffer containing encrypted ELF.
 *              image_size: Size of buffer containing encrypted ELF.
 *              image_out_size: Size of buffer containing decrypted ELF.
 * Out:         image_out_addr: Buffer containing decrypted ELF. ELF
                                will be decrypted in place if provided
                                image_out_addr is NULL.
 * Return:      SUCCESS on success.
 *              Any other error code on failure.
 */
int32_t ipprotector_decrypt(uint32_t feat_id, uint8_t *image_addr,
                            uint32_t image_size, uint8_t *image_out_addr,
                            uint32_t image_out_size);

/*
 * Description: Verify ELF. If ELF is encrypted, decryption will be implicitly
 *              performed.
 *              In order for verification to be successful:
 *              1) ELF must have been signed via Sectools using
 *                 Feature ID, Anti-Rollback Version, and SW ID values
 *                 matching those of feat_id, anti_rollback_version, and
 *                 0x2A respectively.
 *
 * In:          feat_id: Feature ID to use during decryption and verification.
 *              anti_rollback_version: Anti-Rollback Version to use during
 *                                     signature verification.
 *              image_addr: Address of buffer containing signed ELF.
 *              image_size: Size of buffer containing signed ELF.
 * Out:         void.
 * Return:      SUCCESS on success.
 *              Any other error code on failure.
 */
int32_t ipprotector_verify_sig(uint32_t feat_id, uint32_t anti_rollback_version,
                               uint8_t *image_addr, uint32_t image_size);
/*
 * Description: Verify ELF. If ELF is encrypted, decryption will be implicitly
 *              performed.
 *              In order for verification to be successful:
 *              1) ELF must have been signed via Sectools using
 *                 Feature ID, Anti-Rollback Version, and SW ID values
 *                 matching those of feat_id, anti_rollback_version, and
 *                 0x2A respectively.
 *
 * In:          feat_id: Feature ID to use during decryption and verification.
 *              anti_rollback_version: Anti-Rollback Version to use during
 *                                     signature verification.
 *              image_addr: Address of buffer containing signed ELF.
 *              image_size: Size of buffer containing signed ELF.
 * Out:         verified_sw_version: The Anti-Rollback Version embedded in
 *                                   the verified ELF.
 * Return:      SUCCESS on success.
 *              Any other error code on failure.
 */
int32_t ipprotector_verify_sig2(uint32_t feat_id,
                                uint32_t anti_rollback_version,
                                uint8_t *image_addr, uint32_t image_size,
                                uint32_t *verified_sw_version);

/*
 * Description: Get L1 public key derived from provided Feature ID. The
 *              returned public key must be provided to Sectools for
 *              encrypting the ELF.
 *
 * In:          feat_id: Feature ID to use during key derivation.
 *              key_out_size: Size of buffer containing public key.
 * Out:         key_out_addr: Buffer containing public key.
 *
 * Return:      SUCCESS on success.
 *              Any other error code on failure.
 */
int32_t ipprotector_get_public_key(uint32_t feat_id, uint8_t *key_out_addr,
                                   uint32_t key_out_size);

/*
 * Description: Get the offset and file size of ELF's single LOAD segment.
 *              Assumes exactly 1 LOAD segment is present in ELF.
 *
 * In:          image_addr: Address of buffer containing ELF.
 *              image_size: Size of buffer containing ELF.
 * Out:         segment_offset: Offset of LOAD segment within ELF buffer.
 *              segment_size: Size of LOAD segment.
 *
 * Return:      SUCCESS on success.
 *              Any other error code on failure.
 */
int32_t ipprotector_find_seg(uint8_t *image_addr, uint32_t image_size,
                             uint32_t *segment_offset, size_t *segment_size);

#endif  //__IPPROTECTOR_API_H__
