#include "test_common.h"

CK_RV test_crypto();
CK_RV test_gostr3410_512(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, (CK_ULONG)strlen((char *)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_GOSTR3410_512, &minfo);
    if (rc != CKR_OK)
    {
        fprintf(stderr,
                "\n======== Mechanism CKM_GOSTR3410_512 not supported ===========\n");
    }
    else
    {
        fprintf(stderr,
                "\n==================== CKM_GOSTR3410_512 test ==================\n");
        rc = test_gostr3410_512(hSession);
        if (rc != CKR_OK)
        {
            fprintf(stderr,
                    "ERROR CKM_GOSTR3410_512 failed, rc = 0x%lx\n", rc);
        }
        else
        {
            fprintf(stderr, "CKM_GOSTR3410_512 test SUCCESS.\n");
        }
    }
 out_close:
    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_gostr3410_512(CK_SESSION_HANDLE sess)
{
    CK_RV                  rc = CKR_OK;
    CK_BYTE                value[256];
    CK_ULONG               len;
    CK_OBJECT_HANDLE       pub_key            = CK_INVALID_HANDLE;
    CK_OBJECT_HANDLE       priv_key           = CK_INVALID_HANDLE;
    CK_MECHANISM           mechanism_desc     = { CKM_GOSTR3410_512, NULL, 0 };
    CK_MECHANISM_PTR       mechanism          = &mechanism_desc;
    CK_MECHANISM           mechanism_gen_desc = { CKM_GOSTR3410_512_KEY_PAIR_GEN, NULL, 0 };
    CK_MECHANISM_PTR       mechanism_gen      = &mechanism_gen_desc;

    static CK_BBOOL        ltrue       = CK_TRUE;
    static CK_OBJECT_CLASS oclass_pub  = CKO_PUBLIC_KEY;
    static CK_OBJECT_CLASS oclass_priv = CKO_PRIVATE_KEY;
    static CK_BYTE         tc26_decc_A_der_oid[] =
    { 0x06, 0x09, 0x2a, 0x85, 0x03, 0x07, 0x01, 0x02, 0x01, 0x02, 0x01 };
    static CK_BYTE         tc26_gost3411_2012_512_der_oid[] =
    { 0x06, 0x08, 0x2a, 0x85, 0x03, 0x07, 0x01, 0x01, 0x02, 0x03 };
    static CK_BYTE         gost28147params_Z[] = {
        0x06, 0x09, 0x2a, 0x85, 0x03, 0x07, 0x01, 0x02, 0x05, 0x01, 0x01
    };


    static CK_BYTE     digest[] = {
        0x27, 0x55, 0x57, 0xa4, 0x7d, 0xcf, 0xcb, 0x82,
        0x35, 0xff, 0x02, 0x9b, 0x74, 0x83, 0x7f, 0x04,
        0x41, 0xef, 0xe4, 0x1a, 0xed, 0x12, 0xc2, 0x07,
        0x31, 0x3e, 0x83, 0xb2, 0x7a, 0xbd, 0x1e, 0x6a,
        0x9d, 0x89, 0x27, 0x13, 0xbc, 0x30, 0xa1, 0x6b,
        0xf9, 0x47, 0xd4, 0x6a, 0x59, 0xbb, 0xbb, 0x3a,
        0x33, 0xee, 0x33, 0x38, 0x53, 0x91, 0xc7, 0x36,
        0x75, 0xe7, 0xd0, 0xc3, 0x60, 0x21, 0x35, 0x40,
    };
    CK_ATTRIBUTE       pub_template[] = {
        { CKA_CLASS,           &oclass_pub, sizeof(oclass_pub) },
        { CKA_TOKEN,           &ltrue,      sizeof(ltrue)      },
        { CKA_GOSTR3410PARAMS,
          tc26_decc_A_der_oid, sizeof(tc26_decc_A_der_oid) },
        { CKA_GOSTR3411PARAMS,
          tc26_gost3411_2012_512_der_oid,
          sizeof(tc26_gost3411_2012_512_der_oid) },
        { CKA_VERIFY,          &ltrue,      sizeof(CK_BBOOL)   },
        { CKA_GOST28147PARAMS,
          gost28147params_Z, sizeof(gost28147params_Z) },
    };
    CK_ATTRIBUTE       priv_template[] = {
        { CKA_CLASS,   &oclass_priv, sizeof(oclass_priv) },
        { CKA_TOKEN,   &ltrue,       sizeof(ltrue)       },
        { CKA_PRIVATE, &ltrue,       sizeof(ltrue)       },
        { CKA_SIGN,    &ltrue,       sizeof(CK_BBOOL)    },
    };

    rc = funcs->C_GenerateKeyPair(sess, mechanism_gen,
                                  pub_template, sizeof(pub_template) / sizeof(CK_ATTRIBUTE),
                                  priv_template, sizeof(priv_template) / sizeof(CK_ATTRIBUTE),
                                  &pub_key, &priv_key);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GenerateKeyPair failed: 0x%lx\n", rc);
        return rc;
    }
    fprintf(stderr, "C_GenerateKeyPair OK\n");

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

    len = sizeof(value);
    rc  = funcs->C_Sign(sess, digest, sizeof(digest), value, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_Sign failed: 0x%lx\n", rc);
        return rc;
    }
    fprintf(stderr, "C_Sign OK\n");

    printf("SIGNATURE:\n");
    print_bytes(value, len);

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

    rc = funcs->C_Verify(sess, digest, sizeof(digest), value, len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_Verify failed: 0x%lx\n", rc);
        return rc;
    }
    fprintf(stderr, "C_Verify OK\n");

    value[5] ^= 0x08;
    fprintf(stderr, "Signature changed for testing\n");
    // Signature changed and not valid
    rc = funcs->C_VerifyInit(sess, mechanism, pub_key);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_VerifyInit failed: 0x%lx\n", rc);
        return rc;
    }
    rc = funcs->C_Verify(sess, digest, sizeof(digest), value, len);
    if (rc == CKR_OK)
    {
        fprintf(stderr,
                "C_Verify not failed for bad signature: 0x%lx\n", rc);
        return CKR_FUNCTION_FAILED;
    }
    fprintf(stderr,
            "C_Verify failed for bad signature (0x%lx) - it's OK\n", rc);

    // Restore valid signature
    value[5] ^= 0x08;
    // Change data
    digest[7] ^= 0x3A;
    fprintf(stderr, "Data changed for testing\n");
    rc = funcs->C_VerifyInit(sess, mechanism, pub_key);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_VerifyInit failed: 0x%lx\n", rc);
        return rc;
    }
    rc = funcs->C_Verify(sess, digest, sizeof(digest), value, len);
    if (rc == CKR_OK)
    {
        fprintf(stderr,
                "C_Verify not failed for bad data: 0x%lx\n", rc);
        return CKR_FUNCTION_FAILED;
    }
    fprintf(stderr,
            "C_Verify failed for bad data (0x%lx) - it's OK\n", rc);

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

    return rc;
}



