#include <stdio.h>

#include "app_main.h"
#include "knoxai_io_datatypes.h"
#include "knoxai_factory_model_key.h"
#include "knoxai_prov.h"
#include "knoxai_logger.h"

int checkIccc();
uint32_t process_cmd(uint32_t commandId, tciMessage_t *sendmsg, tciMessage_t *respmsg);
// include vendor specific function
#ifdef CONFIG_TEEGRIS
    #include "icccOperations.h"
    #include "vendor_tmain_c"
#endif

#ifdef CONFIG_QSEE
    #include "tz_iccc_comdef.h"
    #include "vendor_qmain_c"
#endif

#ifdef CONFIG_KINIBI
    #include "icccOperations.h"
    #include "vendor_kmain_c"
#endif

int checkIccc() {
    uint32_t type = ICCC_STATUS_COMP_TYPE_SOFT_INTEGRITY;
    uint8_t resp_msg_buf[ICCC_STATUS_MAX_RESULT_MESSAGE] = {0, };
    uint32_t resp_msg_len = 0;
    uint32_t result_iccc = 1;
    int result_code = 1;

    if (ICCC_SUCCESS == Iccc_DeviceStatus_TA(type, resp_msg_buf, sizeof(resp_msg_buf), &resp_msg_len, &result_iccc)) {
        if (result_iccc == 0) { // ICCC - Device secure
            KNOXAI_LOG("Device integrity is normal - (%s - %d), (0x%08x)", resp_msg_buf, resp_msg_len, result_iccc);
            result_code = 0x02;
        } else {
            KNOXAI_LOG("Device integrity is abnormal - (%s - %d), (0x%08x)", resp_msg_buf, resp_msg_len, result_iccc);
            result_code = -2;
        }
    } else {
        KNOXAI_LOG("ICCC integrity check failure with Iccc_DeviceStatus_TA");
        result_code = -1;
    }
    return result_code;
}
uint32_t process_cmd(uint32_t commandId, tciMessage_t *sendmsg, tciMessage_t *respmsg)
{
    uint32_t ret = KNOXAI_SUCCESS;

    KNOXAI_LOG("process_cmd : received command - %u", commandId);

    switch (commandId) {
        case CMD_KNOXAI_RETURN_PLAIN_FACTORY_MODEL_KEY:
            ret = knoxai_factory_model_key(&sendmsg->payload.return_plain_factory_model_key, &respmsg->payload.return_plain_factory_model_key);
            respmsg->header.len = sizeof(respmsg->payload.return_plain_factory_model_key);
            break;
        case CMD_KNOXAI_GET_DEVICE_CERTIFICATES:
            ret = knoxai_get_device_certificates(&sendmsg->payload.get_device_certificates, &respmsg->payload.get_device_certificates);
            respmsg->header.len = sizeof(respmsg->payload.get_device_certificates);
            break;
        case CMD_KNOXAI_SET_PROVISION:
            ret = knoxai_set_prov_data(&sendmsg->payload.set_provisioning, &respmsg->payload.set_provisioning);
            respmsg->header.len = sizeof(respmsg->payload.set_provisioning);
            break;
#ifdef DEBUG_KNOXAI
        case 0x77: // for self test
            {
                void self_test();
                self_test();
                respmsg->payload.set_provisioning.content.knoxai_rsp.result_code = KNOXAI_SUCCESS;
            }
            break;
#endif
        default:
            ret = CMD_KNOXAI_UNKNOWN;
            break;
    }

    return ret;
}

#ifdef DEBUG_KNOXAI
#include "knoxai_ec.h"
#include "knoxai_rsa.h"
#include "knoxai_vendor_utils.h"
void self_test() {
    uint32_t ret = KNOXAI_FAILURE;
    uint8_t  blob_ptr[] = {
              0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01
            , 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01};
    uint32_t blob_len = 32;
    uint8_t  dest_ptr[256];
    uint32_t dest_len = 256;
    uint8_t  result_ptr[256];
    uint32_t result_len = 256;
    uint8_t  plainDekKey[] = {
              0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0x11
            , 0x01, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0x10};
    uint32_t plainDekKeyLen = 32;
    int      nPass = 0;
    KNOXAI_LOG("$$$$$$$ self test start");
    // knoxai_ec
    {
        uint8_t  ec_in[] = {
                0xbe, 0x6e, 0x29, 0x7b, 0x58, 0xe4, 0x43, 0x16, 0x49, 0x9e, 0x41, 0xa7, 0xdc, 0xf5, 0x53, 0x32
                , 0x7d, 0xe5, 0x91, 0x9a, 0x2e, 0x54, 0xe8, 0xb5, 0xe5, 0x2e, 0xca, 0xbc, 0x6e, 0x16, 0xfa, 0x9a
                , 0x3a, 0x52, 0x0d, 0x43, 0x9d, 0x66, 0x7b, 0x5c, 0x87, 0xe5, 0x44, 0x52, 0xcd, 0xe5, 0x01, 0x4b
                , 0x4a, 0xb4, 0x86, 0x95, 0x18, 0xce, 0xd2, 0xdd, 0xc4, 0xc2, 0x1f, 0xc0, 0xf2, 0xa5, 0xe4, 0x6e
                , 0x00, 0x00, 0x00, 0x30
                , 0xea, 0xd7, 0x34, 0x47, 0xf7, 0x52, 0xe4, 0x07, 0x28, 0x1a, 0x9a, 0x03, 0xc1, 0x35, 0x8b, 0xab
                , 0x4b, 0x01, 0xbd, 0x49, 0x58, 0xd9, 0x4f, 0x95, 0x57, 0x23, 0xf4, 0x73, 0x8b, 0xce, 0x91, 0xc2
                , 0x11, 0xd9, 0xe4, 0x89, 0x32, 0x97, 0xc2, 0x32, 0xfc, 0x6e, 0x52, 0xfb, 0xad, 0xaa, 0x4e, 0x2c
                , 0xd4, 0xac, 0x3f, 0x26, 0x2e, 0x7e, 0x9b, 0x66, 0xae, 0xc9, 0xdd, 0x04, 0x0c, 0x04, 0x16, 0x1c
                , 0x36, 0x3f, 0xdc, 0x84, 0xe6, 0xd7, 0x58, 0x00, 0x1a, 0xc8, 0x12, 0xd9, 0xc3, 0x94, 0xfe, 0x49
                , 0xec, 0x46, 0xfd, 0x30, 0x31, 0x8e, 0x58, 0xca, 0xdc, 0x2c, 0x81, 0xb5, 0x23, 0x2f, 0xf8, 0x79
                , 0xb8, 0x11, 0x9c, 0x73, 0x09, 0x06, 0x46, 0x18, 0xd0, 0xba, 0x1c, 0xaa, 0xb6, 0xdc, 0x05, 0xb5};
        uint32_t  ec_in_len = 164;
    
        ret = knoxai_ecies(plainDekKey, plainDekKeyLen, ec_in, ec_in_len, result_ptr, &result_len);
        if ( ret == KNOXAI_SUCCESS  ) {
            if ( TEE_MemCompare((void*)result_ptr, (void*)blob_ptr , blob_len) == 0 ) {
                KNOXAI_LOG("$$$$$$$ knoxai_ecies matched");
                nPass++;
            } else {
                KNOXAI_LOG("knoxai_ecies NOT matched");
                KNOXAI_DBG_DUMP("dec data", result_ptr, result_len);
            }
        } else {
            KNOXAI_LOG("knoxai_ecies self test failed[%d]", ret);
        }
    }
    // knoxai_rsa
    {
        // openssl genrsa -des -out ss.pem 2048 ; key gen
        // openssl rsa -in ss.pem -text         ; key print out
        uint8_t  rsa_modulus[] = {
                0xb8, 0xfd, 0x00, 0xce, 0x09, 0x88, 0xa8, 0x32, 0x65, 0x64, 0xaf, 0xc3, 0xea, 0x68, 0x3b, 0x88
                , 0x4e, 0x07, 0x63, 0x8c, 0xf9, 0x5d, 0x3e, 0x14, 0x40, 0x82, 0xb8, 0xbd, 0xd6, 0x56, 0x46, 0xba
                , 0x0b, 0x92, 0x7e, 0x01, 0xe3, 0x35, 0x7a, 0x94, 0x67, 0x19, 0x9e, 0x8c, 0xf7, 0x32, 0x71, 0xe3
                , 0xa8, 0x20, 0xcf, 0xd2, 0xd7, 0x1e, 0xd7, 0xe7, 0x1d, 0xfb, 0xdd, 0x29, 0xea, 0xef, 0x39, 0x10
                , 0x74, 0xae, 0x07, 0x42, 0xe9, 0xea, 0x11, 0x9d, 0x90, 0x47, 0x99, 0x2c, 0x08, 0x47, 0xaf, 0x4e
                , 0xd9, 0x14, 0x65, 0x2d, 0x2c, 0x09, 0x9c, 0xed, 0x5e, 0x88, 0x89, 0x8b, 0xa5, 0x4a, 0x3e, 0x08
                , 0x64, 0x15, 0x81, 0x42, 0x35, 0x64, 0x53, 0x5e, 0xdb, 0x9a, 0x22, 0xa5, 0x76, 0x5e, 0x65, 0xfb
                , 0xf4, 0xf1, 0xef, 0x41, 0xca, 0xe9, 0xb8, 0xa5, 0xfd, 0xf3, 0x72, 0x12, 0x32, 0xf7, 0xb9, 0x42
                , 0xd1, 0x9b, 0x43, 0x5d, 0x63, 0x09, 0xc5, 0xf0, 0x47, 0x27, 0x80, 0xa6, 0x02, 0x44, 0x0e, 0x0d
                , 0xbd, 0xd3, 0xde, 0x4e, 0xe4, 0x9a, 0xd6, 0x0e, 0x1d, 0x33, 0x7f, 0x1f, 0x00, 0x0a, 0x88, 0xf7
                , 0x00, 0xd2, 0x9d, 0xbd, 0x73, 0x1c, 0xc7, 0xba, 0x12, 0xcd, 0x72, 0x77, 0x6a, 0x4b, 0x4c, 0x64
                , 0xd3, 0x92, 0x9c, 0xe5, 0x29, 0x81, 0xc2, 0xf4, 0xbd, 0xd2, 0xb3, 0x34, 0xf7, 0x5e, 0x12, 0x83
                , 0x15, 0x03, 0x13, 0x05, 0xba, 0xcf, 0x61, 0x22, 0xe9, 0x46, 0x2e, 0x10, 0x17, 0xf2, 0x95, 0x3b
                , 0x20, 0x8b, 0xa3, 0x6d, 0x02, 0x19, 0x61, 0x42, 0x97, 0x36, 0x60, 0x44, 0x04, 0x29, 0x78, 0xd1
                , 0x62, 0xb0, 0xa3, 0x7f, 0x3d, 0x10, 0x7c, 0x39, 0xf8, 0x9f, 0x0d, 0x59, 0x23, 0xeb, 0xce, 0x8a
                , 0xa9, 0x58, 0x44, 0x9e, 0x15, 0x11, 0x2c, 0x54, 0xc3, 0x4f, 0x56, 0xe7, 0xa7, 0x33, 0x40, 0x8d};
        uint8_t  rsa_pubExp[] = {0x01, 0x00, 0x01};
        uint8_t  rsa_priExp[] = {
                0x6b, 0x7c, 0x96, 0xbb, 0x21, 0x4f, 0x06, 0x6c, 0x41, 0xfd, 0xff, 0x23, 0x17, 0x50, 0xf9, 0x46
                , 0x7a, 0x10, 0x51, 0x07, 0xd8, 0xdd, 0x14, 0xf5, 0x2f, 0x86, 0xc5, 0x51, 0x88, 0xf9, 0x18, 0x9f
                , 0x81, 0x99, 0xdd, 0x1d, 0xbb, 0xa7, 0x97, 0x52, 0x23, 0x95, 0xff, 0x4a, 0x69, 0x58, 0x26, 0x54
                , 0xe1, 0x9c, 0x2f, 0x25, 0x93, 0x59, 0x98, 0x99, 0xf2, 0x12, 0x26, 0xaf, 0xf4, 0x92, 0x8b, 0xd5
                , 0xc3, 0xf6, 0x5a, 0xc1, 0xc6, 0xa8, 0xfb, 0x13, 0x52, 0x82, 0x47, 0xbe, 0x0e, 0x11, 0x99, 0x0b
                , 0x77, 0x97, 0x22, 0x32, 0xdd, 0x23, 0xb8, 0xcb, 0x33, 0xe2, 0xa2, 0x09, 0x42, 0x41, 0xa1, 0x49
                , 0xbe, 0xdd, 0xbf, 0xfd, 0x78, 0x88, 0x92, 0xe8, 0xcb, 0x31, 0x04, 0x70, 0xa0, 0x16, 0xb9, 0xab
                , 0x6c, 0xe8, 0x26, 0xdd, 0x53, 0xc9, 0xfa, 0x40, 0x79, 0x0e, 0xbb, 0x79, 0x57, 0xa4, 0x19, 0x40
                , 0x7d, 0x22, 0xae, 0x20, 0xd7, 0xb8, 0x78, 0x07, 0x61, 0x99, 0x62, 0x8a, 0xb7, 0xa7, 0xb3, 0xe2
                , 0x97, 0x2a, 0x9b, 0xb9, 0x56, 0x0e, 0x68, 0x85, 0x38, 0x33, 0x1c, 0x18, 0x30, 0x4a, 0xeb, 0x51
                , 0xfa, 0x32, 0x7a, 0x56, 0xbe, 0x43, 0x7e, 0xcc, 0xbb, 0x8c, 0xbe, 0x63, 0xbe, 0xc0, 0xdb, 0x86
                , 0x7f, 0x06, 0x29, 0x3c, 0xb7, 0x93, 0x25, 0xa1, 0x41, 0x94, 0x4f, 0x63, 0xb0, 0x7d, 0x07, 0xac
                , 0x9b, 0xfa, 0x6f, 0x42, 0x8f, 0xa0, 0xe7, 0x78, 0x06, 0x1b, 0x90, 0x3c, 0x5d, 0x9a, 0x2f, 0xe0
                , 0x3d, 0xbd, 0xe6, 0x83, 0x59, 0x8a, 0x02, 0x9a, 0x0c, 0xba, 0xc0, 0x7c, 0xfc, 0x5f, 0xe4, 0x41
                , 0x84, 0x40, 0x3e, 0xbd, 0xb8, 0xd9, 0x35, 0xe9, 0xfe, 0xc4, 0x9a, 0x41, 0x54, 0x59, 0x02, 0xdf
                , 0xd9, 0xff, 0x22, 0x2f, 0x37, 0xdb, 0xc5, 0x57, 0x47, 0x9a, 0x48, 0x6e, 0xc5, 0x82, 0x95, 0x01};
        uint32_t  rsa_pubExp_len = 3;
        uint32_t  rsa_len = 256;

        ret = knoxai_rsa_encrypt(rsa_modulus, rsa_len, rsa_pubExp, rsa_pubExp_len, blob_ptr, blob_len, dest_ptr, &dest_len);
        if ( ret == KNOXAI_SUCCESS ) {
            KNOXAI_DBG_DUMP("enc data", dest_ptr, dest_len);
            ret = knoxai_rsa_decrypt(rsa_modulus, rsa_len, rsa_pubExp, rsa_pubExp_len, rsa_priExp, rsa_len, 
                                dest_ptr, dest_len, result_ptr, &result_len);
            if ( ret == KNOXAI_SUCCESS ) {
                if ( TEE_MemCompare((void*)result_ptr, (void*)blob_ptr , blob_len) == 0 ) {
                    KNOXAI_LOG("$$$$$$$ knoxai_rsa matched");
                    nPass++;
                } else {
                    KNOXAI_LOG("knoxai_rsa NOT matched");
                }
            }
        } else {
            KNOXAI_LOG("knoxai_rsa self test failed[%d]", ret);
        }
    }
    // knoxai_vendor_utils
    ret = knoxai_wrap((TEE_UUID)SELF_UUID, blob_ptr, blob_len, dest_ptr, &dest_len);
    if ( ret == KNOXAI_SUCCESS) {
        KNOXAI_DBG_DUMP("wrap data", dest_ptr, dest_len);
        result_len = 256;
        TEE_MemFill(result_ptr, 0, result_len);
        ret = knoxai_unwrap((TEE_UUID)SELF_UUID, dest_ptr, dest_len, result_ptr, &result_len);
        if ( ret == KNOXAI_SUCCESS && TEE_MemCompare((void*)result_ptr, (void*)blob_ptr , blob_len) == 0 ) {
            KNOXAI_LOG("$$$$$$$ wrap - unwrap matched");
            nPass++;
        } else {
            KNOXAI_LOG("wrap - unwrap NOT matched unwrap ret = [%d]", ret);
            KNOXAI_DBG_DUMP("unwrap data", result_ptr, result_len);
        }
    } else {
        KNOXAI_LOG("wrap self test failed [%d]", ret);
    }
    KNOXAI_LOG("$$$$$$$ self test done");
    KNOXAI_LOG("$$$$$$$ success [%d] / test case [3]", nPass);
    return;
}
#endif