#include "test_common.h"

CK_RV test_crypto();
CK_RV test_kdf_gost3411_2012_256(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_KDF_GOSTR3411_2012_256, &minfo);
    if (rc != CKR_OK)
    {
        fprintf(stderr,
                "\n======== Mechanism CKM_KDF_GOSTR3411_2012_256 "
                "not supported ===========\n");
    }
    else
    {
        fprintf(stderr,
                "\n==================== CKM_KDF_GOSTR3411_2012_256 "
                "test ==================\n");
        rc = test_kdf_gost3411_2012_256(hSession);
        if (rc != CKR_OK)
        {
            fprintf(stderr,
                    "ERROR CKM_KDF_GOSTR3411_2012_256 failed, "
                    "rc = 0x%lx\n", rc);
        }
        else
        {
            fprintf(stderr,
                    "CKM_KDF_GOSTR3411_2012_256 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_kdf_gost3411_2012_256(CK_SESSION_HANDLE sess)
{
    CK_RV rc = CKR_OK;
    static CK_BYTE                value[256];
    CK_ULONG               len;
    CK_MECHANISM           mechanism_desc =
    { CKM_KDF_GOSTR3411_2012_256, NULL, 0 };
    CK_MECHANISM_PTR       mechanism = &mechanism_desc;
	CK_TC26_KDF_GOSTR3411_2012_256_PARAMS params;

    CK_OBJECT_HANDLE       keyh          = CK_INVALID_HANDLE;
    CK_OBJECT_HANDLE       kdfkeyh       = CK_INVALID_HANDLE;
    static CK_OBJECT_CLASS oclass        = CKO_SECRET_KEY;
    static CK_KEY_TYPE     key_type      = CKK_GOST28147;
    static CK_BBOOL        ltrue         = CK_TRUE;
    static CK_BYTE         gost28147_Z[] = {
        0x06, 0x09, 0x2a, 0x85, 0x03, 0x07, 0x01, 0x02, 0x05, 0x01, 0x01
    };
	CK_BYTE label[] = {
		0x26, 0xBD, 0xB8, 0x78,
	};
    CK_BYTE                keyval1[] = {
        0x79, 0x8b, 0xd0, 0x9e, 0xf7, 0xad, 0x67, 0x64,
        0x6a, 0x3b, 0x74, 0xfe, 0x48, 0xd7, 0x31, 0x73,
        0x50, 0xa8, 0x6f, 0x07, 0x7d, 0x05, 0xef, 0x78,
        0xcb, 0xe9, 0xba, 0x3c, 0x9e, 0xf3, 0xa4, 0x11,
    };
    CK_BYTE                ukm1[] = {
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    };
    static CK_BYTE         et1[] = {
        0xf1, 0x64, 0x90, 0x88, 0x2e, 0xe5, 0xd5, 0x23,
        0x1e, 0xbb, 0xc3, 0xbc, 0x7e, 0xb6, 0x4c, 0x85,
        0x96, 0x5c, 0xa5, 0xb1, 0x07, 0x62, 0xd6, 0x55,
        0x15, 0xe3, 0x6b, 0x04, 0x28, 0x99, 0xa2, 0xa2,
    };

    CK_BYTE                keyval2[] =
    {
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
    };
    CK_BYTE                ukm2[] =
    {
        0xaf, 0x21, 0x43, 0x41, 0x45, 0x65, 0x63, 0x78
    };
    static CK_BYTE         et2[] =
    {
        0xa1, 0xaa, 0x5f, 0x7d, 0xe4, 0x02, 0xd7, 0xb3,
        0xd3, 0x23, 0xf2, 0x99, 0x1c, 0x8d, 0x45, 0x34,
        0x01, 0x31, 0x37, 0x01, 0x0a, 0x83, 0x75, 0x4f,
        0xd0, 0xaf, 0x6d, 0x7c, 0xd4, 0x92, 0x2e, 0xd9
    };

    CK_ATTRIBUTE key_template[] = {
        { CKA_VALUE,            NULL_PTR,                 0 },
        { CKA_CLASS,            &oclass,   sizeof(oclass)   },
        { CKA_KEY_TYPE,         &key_type, sizeof(key_type) },
        { CKA_GOST28147_PARAMS,
          gost28147_Z, sizeof(gost28147_Z) },
        { CKA_ENCRYPT,          &ltrue,    sizeof(ltrue)    },
        { CKA_DECRYPT,          &ltrue,    sizeof(ltrue)    }
    };

    CK_ATTRIBUTE key_derive_template[] = {
        { CKA_CLASS,            &oclass,   sizeof(oclass)   },
        { CKA_KEY_TYPE,         &key_type, sizeof(key_type) },
        { CKA_GOST28147_PARAMS,
          gost28147_Z, sizeof(gost28147_Z) },
        { CKA_ENCRYPT,          &ltrue,    sizeof(ltrue)    },
        { CKA_DECRYPT,          &ltrue,    sizeof(ltrue)    }
    };
    CK_ATTRIBUTE value_template[] = {
        { CKA_VALUE, value, sizeof(value) },
    };

    key_template[0].pValue     = keyval1;
    key_template[0].ulValueLen = sizeof(keyval1);
    rc                         = funcs->C_CreateObject(sess,
                                                       key_template, sizeof(key_template) / sizeof(CK_ATTRIBUTE),
                                                       &keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_CreateObject failed: 0x%lx\n", rc);
        goto end;
    }

    mechanism->pParameter     = &params;
    mechanism->ulParameterLen = sizeof(params);

	params.pLabel = label;
	params.ulLabelLen = sizeof(label);

	params.pSeed = ukm1;
	params.ulSeedLen = sizeof(ukm1);
    rc                        = funcs->C_DeriveKey(sess, mechanism, keyh,
                                                   key_derive_template,
                                                   sizeof(key_derive_template) / sizeof(CK_ATTRIBUTE),
                                                   &kdfkeyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DeriveKey failed: 0x%lx\n", rc);
        goto end;
    }
    rc = funcs->C_GetAttributeValue(sess, kdfkeyh,
                                    value_template,
                                    sizeof(value_template) / sizeof(CK_ATTRIBUTE));
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetAttributeValue failed: 0x%lx\n", rc);
        goto end;
    }
    len = value_template[0].ulValueLen;
    if (len != sizeof(et1))
    {
        fprintf(stderr, "Invalid length: %ld\n", len);
        rc = CKR_FUNCTION_FAILED;
        goto end;
    }
    print_bytes(value, len);
    if (memcmp(value, et1, len) != 0)
    {
        fprintf(stderr, "Invalid value\n");
        rc = CKR_FUNCTION_FAILED;
        goto end;
    }
    printf("KDF1 OK\n");

    funcs->C_DestroyObject(sess, keyh);

    key_template[0].pValue     = keyval2;
    key_template[0].ulValueLen = sizeof(keyval2);
    rc                         = funcs->C_CreateObject(sess,
                                                       key_template,
                                                       sizeof(key_template) / sizeof(CK_ATTRIBUTE),
                                                       &keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_CreateObject failed: 0x%lx\n", rc);
        goto end;
    }

	params.pSeed = ukm2;
	params.ulSeedLen = sizeof(ukm2);
    rc                        = funcs->C_DeriveKey(sess, mechanism, keyh,
                                                   key_derive_template,
                                                   sizeof(key_derive_template) / sizeof(CK_ATTRIBUTE),
                                                   &kdfkeyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DeriveKey failed: 0x%lx\n", rc);
        goto end;
    }
    rc = funcs->C_GetAttributeValue(sess, kdfkeyh,
                                    value_template,
                                    sizeof(value_template) / sizeof(CK_ATTRIBUTE));
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetAttributeValue failed: 0x%lx\n", rc);
        goto end;
    }
    len = value_template[0].ulValueLen;
    if (len != sizeof(et2))
    {
        fprintf(stderr, "Invalid length: %ld\n", len);
        rc = CKR_FUNCTION_FAILED;
        goto end;
    }
    print_bytes(value, len);
    if (memcmp(value, et2, len) != 0)
    {
        fprintf(stderr, "Invalid value\n");
        rc = CKR_FUNCTION_FAILED;
        goto end;
    }
    printf("KDF2 OK\n");

    printf("SUCCESS\n");
    rc = CKR_OK;
 end:
    if (keyh != CK_INVALID_HANDLE)
    {
        funcs->C_DestroyObject(sess, keyh);
    }

    return rc;
}

