#include "test_common.h"

CK_RV test_crypto();
CK_RV test_kuznyechik_key_gen(CK_SESSION_HANDLE sess);

int main(int argc, char *argv[])
{
    CK_RV rc = CKR_OK;

    rc = test_main(argc, argv);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "test_main failed: 0x%lx\n", rc);
        return rc;
    }
    rc = test_crypto();
    if (rc != CKR_OK)
    {
        fprintf(stderr, "test_crypto failed: 0x%lx\n", rc);
        return rc;
    }
    return CKR_OK;
}

CK_RV test_crypto()
{
    CK_RV             rc, ret;
    CK_SESSION_HANDLE hSession;
    CK_MECHANISM_INFO minfo;

    rc = funcs->C_OpenSession(SlotId,
                              CKF_RW_SESSION | CKF_SERIAL_SESSION,
                              NULL_PTR, NULL_PTR, &hSession);
    if (rc != CKR_OK)
    {
        fprintf(stderr,
                "ERROR call to C_OpenSession failed, rc = 0x%lx\n", rc);
        goto out;
    }
    fprintf(stderr, "C_OpenSession success\n");
    rc = funcs->C_GetMechanismInfo(
        SlotId, CKM_KUZNYECHIK_KEY_GEN, &minfo);
    if (rc != CKR_OK)
    {
        fprintf(stderr,
                "\n======== Mechanism CKM_KUZNYECHIK_KEY_GEN not supported ===========\n");
    }
    else
    {
        fprintf(stderr,
                "\n==================== CKM_KUZNYECHIK_KEY_GEN test ==================\n");
        rc = test_kuznyechik_key_gen(hSession);
        if (rc != CKR_OK)
        {
            fprintf(stderr,
                    "ERROR CKM_KUZNYECHIK_KEY_GEN failed, rc = 0x%lx\n", rc);
        }
        else
        {
            fprintf(stderr, "CKM_KUZNYECHIK_KEY_GEN test passed.\n");
        }
    }

    if ((ret = funcs->C_CloseSession(hSession)) != CKR_OK)
    {
        fprintf(stderr,
                "Error: C_CloseSession failed with 0x%lx\n", ret);
        rc = ret;
    }
    else
    {
        fprintf(stderr, "C_CloseSession success\n");
    }

 out:
    return rc;
}

CK_RV test_kuznyechik_key_gen(CK_SESSION_HANDLE sess)
{
    CK_RV rc = CKR_OK;
    CK_BYTE                value[1024];
    CK_ULONG               i;
    CK_OBJECT_HANDLE       key    = CK_INVALID_HANDLE;
    CK_ATTRIBUTE           attr;
    CK_MECHANISM           mechanism_desc = { CKM_KUZNYECHIK_KEY_GEN, NULL, 0 };
    CK_MECHANISM_PTR       mechanism      = &mechanism_desc;

    static CK_BBOOL        ltrue  = CK_TRUE;
    static CK_BBOOL        lfalse = CK_FALSE;
//    static CK_OBJECT_CLASS oclass = CKO_SECRET_KEY;
//    static CK_KEY_TYPE     ktype  = CKK_KUZNYECHIK;

    static CK_ATTRIBUTE    ltemplate[] = {
//		{ CKA_CLASS, &oclass, sizeof(oclass)},
//		{ CKA_KEY_TYPE, &ktype, sizeof(ktype)},
		{ CKA_TOKEN, &ltrue, sizeof(ltrue) },
        { CKA_EXTRACTABLE, &ltrue,  sizeof(ltrue)  },
        { CKA_SENSITIVE,   &lfalse, sizeof(lfalse) }
    };

    rc = funcs->C_GenerateKey(sess, mechanism,
                              ltemplate, sizeof(ltemplate) / sizeof(CK_ATTRIBUTE), &key);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GenerateKey failed: 0x%lx\n", rc);
        return rc;
    }

    attr.type       = CKA_VALUE;
    attr.pValue     = value;
    attr.ulValueLen = sizeof(value);
    rc              = funcs->C_GetAttributeValue(sess, key, &attr, 1);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetAttributeValue failed: 0x%lx\n", rc);
        return rc;
    }

    printf("\n %%%%%% KEY: ");
    for (i = 0; i < attr.ulValueLen; i++)
        printf(" %02x", value[i]);
    printf(" %%%%%%\n\n");

    rc = funcs->C_DestroyObject(sess, key);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DestroyObject failed: 0x%lx\n", rc);
        return rc;
    }

    return rc;
}

