#include "test_common.h"

CK_RV test_crypto();
CK_RV test_tls_prf(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");
/*
        // log in as normal user
        rc = funcs->C_Login(hSession, CKU_USER, user_pin, strlen(user_pin));
        if (rc != CKR_OK) {
                fprintf(stderr, "ERROR call to C_Login failed, rc = 0x%lx\n", rc);
                goto out_close;
        }
        fprintf(stderr, "C_Login success\n");
 */

    rc = funcs->C_GetMechanismInfo(SlotId, CKM_TLS_GOST_PRF, &minfo);
    if (rc != CKR_OK)
    {
        fprintf(stderr,
                "\n======== Mechanism CKM_TLS_GOST_PRF not supported ===========\n");
    }
    else
    {
        fprintf(stderr,
                "\n==================== CKM_TLS_GOST_PRF test ==================\n");
        rc = test_tls_prf(hSession);
        if (rc != CKR_OK)
        {
            fprintf(stderr,
                    "ERROR CKM_TLS_GOST_PRF failed, rc = 0x%lx\n", rc);
        }
        else
        {
            fprintf(stderr, "CKM_TLS_GOST_PRF 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_tls_prf(CK_SESSION_HANDLE sess)
{
    CK_RV rc = CKR_OK;
    static CK_BYTE         value[1024];
    CK_MECHANISM           mechanism_desc = { CKM_TLS_GOST_PRF, NULL, 0 };
    CK_MECHANISM_PTR       mechanism      = &mechanism_desc;
    CK_OBJECT_HANDLE       keyh           = CK_INVALID_HANDLE;
    static CK_OBJECT_CLASS oclass         = CKO_SECRET_KEY;
    static CK_KEY_TYPE     key_type       = CKK_GENERIC_SECRET;
    static CK_BBOOL        ltrue          = CK_TRUE;
    // Using master secret (48 bytes) from LCJSSE test utility.
    static CK_BYTE         keyval[] = {
        0x76, 0x22, 0x41, 0x3d, 0x5a, 0xb6, 0x7f, 0x5b,
        0x86, 0x2e, 0x75, 0x97, 0xe8, 0xf9, 0x31, 0xe4,
        0x2f, 0x13, 0x13, 0xd5, 0x61, 0xed, 0xab, 0xed,
        0xa5, 0x4b, 0xd8, 0x5d, 0x60, 0x0a, 0xec, 0x99,
        0x45, 0xfe, 0x39, 0xa1, 0xb7, 0x62, 0x0d, 0x66,
        0x3c, 0xdd, 0xb3, 0x71, 0x90, 0x0f, 0xc3, 0xdd
    };
    // Using real TLS message label from LirJSSE test utility.
    static CK_UTF8CHAR            *label = (CK_UTF8CHAR *)"client finished";
	CK_ULONG label_len = (CK_ULONG)strlen((char *)label);
    // seed (64 bytes) = client_random (32 bytes) + server_random (32 bytes)
    // Using concatenated client_random and server_random data (32+32 bytes)
    // from LCJSSE test utility.
    static CK_BYTE seed[] = {
        0x87, 0x5a, 0x38, 0x94, 0xa4, 0xf9, 0x3c, 0x30,
        0x65, 0xfc, 0x66, 0xbe, 0xf2, 0xc0, 0x09, 0xb9,
        0xc3, 0x26, 0x3e, 0x16, 0xc7, 0x28, 0x14, 0x27,
        0x98, 0x26, 0xe4, 0xd5, 0x30, 0x7f, 0x9a, 0x2d,
        0x87, 0x0e, 0x1d, 0x34, 0xaf, 0x28, 0x1c, 0x60,
        0xae, 0x8d, 0x26, 0xe4, 0xd5, 0x30, 0x7f, 0x9a,
        0x2d, 0x87, 0x0e, 0x1d, 0x34, 0xaf, 0x28, 0x1c,
        0x60, 0xae, 0x8d, 0x21, 0xee, 0x7d, 0x52, 0x44
    };
    // Using resulting TLS PRF value (12 bytes) from LCJSSE test utility.
    static CK_BYTE et_94[] = {
        0x2a, 0x6b, 0x4c, 0x14, 0x61, 0xb1, 0x39, 0x2d,
        0x18, 0xdf, 0x8a, 0x8d,
    };
/*
   Тестовые данные ТК 26 для алгоритма PRF TLS 2012

   3) PRF_TLS_GOSTR3411_2012_256
   Ключ K:
   00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
   10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
   Данные seed:
   18 47 1d 62 2d c6 55 c4 d2 d2 26 96 91 ca 4a 56
   0b 50 ab a6 63 55 3a f2 41 f1 ad a8 82 c9 f2 9a
   Данные label:
   11 22 33 44 55
   Выход T1:
   ff 09 66 4a 44 74 58 65 94 4f 83 9e bb 48 96 5f
   15 44 ff 1c c8 e8 f1 6f 24 7e e5 f8 a9 eb e9 7f
   Выход T2:
   c4 e3 c7 90 0e 46 ca d3 db 6a 01 64 30 63 04 0e
   c6 7f c0 fd 5c d9 f9 04 65 23 52 37 bd ff 2c 02

   4) PRF_TLS_GOSTR3411_2012_512
   Ключ K:
   00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
   10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
   Данные seed:
   18 47 1d 62 2d c6 55 c4 d2 d2 26 96 91 ca 4a 56
   0b 50 ab a6 63 55 3a f2 41 f1 ad a8 82 c9 f2 9a
   Данные label:
   11 22 33 44 55
   Выход T1:
   f3 51 87 a3 dc 96 55 11 3a 0e 84 d0 6f d7 52 6c
   5f c1 fb de c1 a0 e4 67 3d d6 d7 9d 0b 92 0e 65
   ad 1b c4 7b b0 83 b3 85 1c b7 cd 8e 7e 6a 91 1a
   62 6c f0 2b 29 e9 e4 a5 8e d7 66 a4 49 a7 29 6d
   Выход T2:
   e6 1a 7a 26 c4 d1 ca ee cf d8 0c ca 65 c7 1f 0f
   88 c1 f8 22 c0 e8 c0 ad 94 9d 03 fe e1 39 57 9f
   72 ba 0c 3d 32 c5 f9 54 f1 cc cd 54 08 1f c7 44
   02 78 cb a1 fe 7b 7a 17 a9 86 fd ff 5b d1 5d 1f
 */
    static CK_BYTE      secret_2012[] = {
        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,
    };
    static CK_UTF8CHAR  label_2012[] = {
        0x11, 0x22, 0x33, 0x44, 0x55,
    };
    static CK_BYTE      seed_2012[] = {
        0x18, 0x47, 0x1d, 0x62, 0x2d, 0xc6, 0x55, 0xc4,
        0xd2, 0xd2, 0x26, 0x96, 0x91, 0xca, 0x4a, 0x56,
        0x0b, 0x50, 0xab, 0xa6, 0x63, 0x55, 0x3a, 0xf2,
        0x41, 0xf1, 0xad, 0xa8, 0x82, 0xc9, 0xf2, 0x9a,
    };
    static CK_BYTE      T_256[] = {
        // T1
        0xff, 0x09, 0x66, 0x4a, 0x44, 0x74, 0x58, 0x65,
        0x94, 0x4f, 0x83, 0x9e, 0xbb, 0x48, 0x96, 0x5f,
        0x15, 0x44, 0xff, 0x1c, 0xc8, 0xe8, 0xf1, 0x6f,
        0x24, 0x7e, 0xe5, 0xf8, 0xa9, 0xeb, 0xe9, 0x7f,
        // T2
        0xc4, 0xe3, 0xc7, 0x90, 0x0e, 0x46, 0xca, 0xd3,
        0xdb, 0x6a, 0x01, 0x64, 0x30, 0x63, 0x04, 0x0e,
        0xc6, 0x7f, 0xc0, 0xfd, 0x5c, 0xd9, 0xf9, 0x04,
        0x65, 0x23, 0x52, 0x37, 0xbd, 0xff, 0x2c, 0x02,
    };
    static CK_BYTE      T_512[] = {
        // T1
        0xf3, 0x51, 0x87, 0xa3, 0xdc, 0x96, 0x55, 0x11,
        0x3a, 0x0e, 0x84, 0xd0, 0x6f, 0xd7, 0x52, 0x6c,
        0x5f, 0xc1, 0xfb, 0xde, 0xc1, 0xa0, 0xe4, 0x67,
        0x3d, 0xd6, 0xd7, 0x9d, 0x0b, 0x92, 0x0e, 0x65,
        0xad, 0x1b, 0xc4, 0x7b, 0xb0, 0x83, 0xb3, 0x85,
        0x1c, 0xb7, 0xcd, 0x8e, 0x7e, 0x6a, 0x91, 0x1a,
        0x62, 0x6c, 0xf0, 0x2b, 0x29, 0xe9, 0xe4, 0xa5,
        0x8e, 0xd7, 0x66, 0xa4, 0x49, 0xa7, 0x29, 0x6d,
        // T2
        0xe6, 0x1a, 0x7a, 0x26, 0xc4, 0xd1, 0xca, 0xee,
        0xcf, 0xd8, 0x0c, 0xca, 0x65, 0xc7, 0x1f, 0x0f,
        0x88, 0xc1, 0xf8, 0x22, 0xc0, 0xe8, 0xc0, 0xad,
        0x94, 0x9d, 0x03, 0xfe, 0xe1, 0x39, 0x57, 0x9f,
        0x72, 0xba, 0x0c, 0x3d, 0x32, 0xc5, 0xf9, 0x54,
        0xf1, 0xcc, 0xcd, 0x54, 0x08, 0x1f, 0xc7, 0x44,
        0x02, 0x78, 0xcb, 0xa1, 0xfe, 0x7b, 0x7a, 0x17,
        0xa9, 0x86, 0xfd, 0xff, 0x5b, 0xd1, 0x5d, 0x1f,
    };
    CK_ATTRIBUTE key_template[] = {
        { CKA_VALUE,    keyval,    sizeof(keyval)   },
        { CKA_CLASS,    &oclass,   sizeof(oclass)   },
        { CKA_KEY_TYPE, &key_type, sizeof(key_type) },
        { CKA_DERIVE,   &ltrue,    sizeof(ltrue)    }
    };
    static CK_ULONG     len = sizeof(value);

    // В зависимости от значения OID для ГОСТ Р 34.11
    // будут использоваться алгоритмы ГОСТ Р 34.11-94,
    // ГОСТ Р 34.11-2012 (256 бит) или ГОСТ Р 34.11-2012 (512 бит).

    static CK_BYTE oid_3411_94[] = {
        0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1e, 0x01
    };
    static CK_BYTE oid_3411_2012_256[] = {
        0x06, 0x08, 0x2a, 0x85, 0x03, 0x07, 0x01, 0x01, 0x02, 0x02
    };
    static CK_BYTE oid_3411_2012_512[] = {
        0x06, 0x08, 0x2a, 0x85, 0x03, 0x07, 0x01, 0x01, 0x02, 0x03
    };
/*
   typedef struct CK_TLS_GOST_PRF_PARAMS {
        CK_TLS_PRF_PARAMS  TlsPrfParams;
        CK_BYTE_PTR        pHashParamsOid;
        CK_ULONG           ulHashParamsOidLen;
   } CK_TLS_GOST_PRF_PARAMS;
 */
    CK_TLS_GOST_PRF_PARAMS params_gost = {
        {
            seed, sizeof(seed),
            NULL, 0,
            value, &len
        },
        oid_3411_94,
        sizeof(oid_3411_94)
    };

	params_gost.TlsPrfParams.pLabel = label;
	params_gost.TlsPrfParams.ulLabelLen = label_len;

    printf("TLS PRF 3411-94 test\n");
    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);
        return rc;
    }
    len                       = 12;
    mechanism->pParameter     = &params_gost;
    mechanism->ulParameterLen = sizeof(params_gost);
    rc                        = funcs->C_DeriveKey(sess, mechanism,
                                                   keyh, NULL_PTR, 0, NULL_PTR);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DeriveKey failed: 0x%lx\n", rc);
        return rc;
    }
    funcs->C_DestroyObject(sess, keyh);

    printf("TLS PRF 3411-94:\n");
    print_bytes(value, len);
    CHECK(value, len, et_94);

    printf("TLS PRF 3411-2012-256 test\n");
    key_template[0].pValue     = secret_2012;
    key_template[0].ulValueLen = sizeof(secret_2012);
    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);
        return rc;
    }
    len                                 = sizeof(T_256);
    params_gost.TlsPrfParams.pSeed      = seed_2012;
    params_gost.TlsPrfParams.ulSeedLen  = sizeof(seed_2012);
    params_gost.TlsPrfParams.pLabel     = label_2012;
    params_gost.TlsPrfParams.ulLabelLen = sizeof(label_2012);
    params_gost.pHashParamsOid          = oid_3411_2012_256;
    params_gost.ulHashParamsOidLen      = sizeof(oid_3411_2012_256);
    rc                                  = funcs->C_DeriveKey(sess, mechanism,
                                                             keyh, NULL_PTR, 0, NULL_PTR);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DeriveKey failed: 0x%lx\n", rc);
        return rc;
    }

    printf("TLS PRF 3411-2012 (256):\n");
    print_bytes(value, len);
    CHECK(value, len, T_256);

    printf("TLS PRF 3411-2012-512 test\n");
    len                                 = sizeof(T_512);
    params_gost.TlsPrfParams.pSeed      = seed_2012;
    params_gost.TlsPrfParams.ulSeedLen  = sizeof(seed_2012);
    params_gost.TlsPrfParams.pLabel     = label_2012;
    params_gost.TlsPrfParams.ulLabelLen = sizeof(label_2012);
    params_gost.pHashParamsOid          = oid_3411_2012_512;
    params_gost.ulHashParamsOidLen      = sizeof(oid_3411_2012_512);
    rc                                  = funcs->C_DeriveKey(sess, mechanism,
                                                             keyh, NULL_PTR, 0, NULL_PTR);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DeriveKey failed: 0x%lx\n", rc);
        return rc;
    }

    printf("TLS PRF 3411-2012 (512):\n");
    print_bytes(value, len);
    CHECK(value, len, T_512);
    rc = funcs->C_DestroyObject(sess, keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DestroyObject failed: 0x%lx\n", rc);
        return rc;
    }

    return rc;
}
