#include "test_common.h"

CK_RV test_crypto();
CK_RV test_magma_ctr(CK_SESSION_HANDLE sess, CK_SESSION_HANDLE sess2);

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, hSession2;
    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_OpenSession(SlotId,
                              CKF_RW_SESSION | CKF_SERIAL_SESSION,
                              NULL_PTR, NULL_PTR, &hSession2);
    if (rc != CKR_OK)
    {
        fprintf(stderr,
                "ERROR call to C_OpenSession failed, rc = 0x%lx\n", rc);
        goto out;
    }
    fprintf(stderr, "C_OpenSession 2 success\n");

    rc = funcs->C_GetMechanismInfo(SlotId, CKM_MAGMA_CTR, &minfo);
    if (rc != CKR_OK)
    {
        fprintf(stderr,
                "\n======== Mechanism CKM_MAGMA_CTR not supported ===========\n");
    }
    else
    {
        fprintf(stderr,
                "\n==================== CKM_MAGMA_CTR test ==================\n");
        rc = test_magma_ctr(hSession, hSession2);
        if (rc != CKR_OK)
        {
            fprintf(stderr,
                    "ERROR CKM_MAGMA_CTR failed, rc = 0x%lx\n", rc);
        }
        else
        {
            fprintf(stderr, "CKM_MAGMA_CTR test passed.\n");
        }
    }

    if ((rc = funcs->C_CloseSession(hSession2)) != CKR_OK)
    {
        fprintf(stderr,
                "Error: C_CloseSession 2 failed with 0x%lx\n", rc);
    }
    else
    {
        fprintf(stderr, "C_CloseSession 2 success\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_magma_ctr(CK_SESSION_HANDLE sess, CK_SESSION_HANDLE sess2)
{
    CK_RV                  rc = CKR_OK;
    CK_ULONG               len, state_len;
    CK_BYTE                value[1024], value2[1024];
    CK_OBJECT_HANDLE       keyh = CK_INVALID_HANDLE;
    CK_BYTE                *state;
    CK_BYTE                data2[256];
    CK_MECHANISM           mechanism_desc = { CKM_MAGMA_CTR, 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     key_type = CKK_GOST28147;	// Тоже работает
    static CK_KEY_TYPE     key_type = CKK_MAGMA;

    //par_cipher_Z (TC 26 Z)
//    static CK_BYTE       oid6[] = {
//        0x06, 0x09, 0x2a, 0x85, 0x03, 0x07, 0x01, 0x02, 0x05, 0x01, 0x01
//    };
/*
Magma CTR mode test
-------------------------------------------------
Standard Master Key:
0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
Standard IV:
0x12, 0x34, 0x56, 0x78,
Standard Input Value:
0x92, 0xde, 0xf0, 0x6b, 0x3c, 0x13, 0x0a, 0x59,
0xdb, 0x54, 0xc7, 0x04, 0xf8, 0x18, 0x9d, 0x20,
0x4a, 0x98, 0xfb, 0x2e, 0x67, 0xa8, 0x02, 0x4c,
0x89, 0x12, 0x40, 0x9b, 0x17, 0xb5, 0x7e, 0x41,
Standard Control value:
0x4e, 0x98, 0x11, 0x0c, 0x97, 0xb7, 0xb9, 0x3c,
0x3e, 0x25, 0x0d, 0x93, 0xd6, 0xe8, 0x5d, 0x69,
0x13, 0x6d, 0x86, 0x88, 0x07, 0xb2, 0xdb, 0xef,
0x56, 0x8e, 0xb6, 0x80, 0xab, 0x52, 0xa1, 0x2d,
Reverted by 4 bytes Master Key:
0xcc, 0xdd, 0xee, 0xff, 0x88, 0x99, 0xaa, 0xbb,
0x44, 0x55, 0x66, 0x77, 0x00, 0x11, 0x22, 0x33,
0xf3, 0xf2, 0xf1, 0xf0, 0xf7, 0xf6, 0xf5, 0xf4,
0xfb, 0xfa, 0xf9, 0xf8, 0xff, 0xfe, 0xfd, 0xfc,
Reverted by 4 bytes IV:
0x78, 0x56, 0x34, 0x12,
Reverted by 8 bytes Input Value:
0x59, 0x0a, 0x13, 0x3c, 0x6b, 0xf0, 0xde, 0x92,
0x20, 0x9d, 0x18, 0xf8, 0x04, 0xc7, 0x54, 0xdb,
0x4c, 0x02, 0xa8, 0x67, 0x2e, 0xfb, 0x98, 0x4a,
0x41, 0x7e, 0xb5, 0x17, 0x9b, 0x40, 0x12, 0x89,
Reverted by 8 bytes Control value:
0x3c, 0xb9, 0xb7, 0x97, 0x0c, 0x11, 0x98, 0x4e,
0x69, 0x5d, 0xe8, 0xd6, 0x93, 0x0d, 0x25, 0x3e,
0xef, 0xdb, 0xb2, 0x07, 0x88, 0x86, 0x6d, 0x13,
0x2d, 0xa1, 0x52, 0xab, 0x80, 0xb6, 0x8e, 0x56,
Output Value:
0x3c, 0xb9, 0xb7, 0x97, 0x0c, 0x11, 0x98, 0x4e,
0x69, 0x5d, 0xe8, 0xd6, 0x93, 0x0d, 0x25, 0x3e,
0xef, 0xdb, 0xb2, 0x07, 0x88, 0x86, 0x6d, 0x13,
0x2d, 0xa1, 0x52, 0xab, 0x80, 0xb6, 0x8e, 0x56,
Reverted Control value:
0x3c, 0xb9, 0xb7, 0x97, 0x0c, 0x11, 0x98, 0x4e,
0x69, 0x5d, 0xe8, 0xd6, 0x93, 0x0d, 0x25, 0x3e,
0xef, 0xdb, 0xb2, 0x07, 0x88, 0x86, 0x6d, 0x13,
0x2d, 0xa1, 0x52, 0xab, 0x80, 0xb6, 0x8e, 0x56,
Reverted by 8 bytes Output Value:
0x4e, 0x98, 0x11, 0x0c, 0x97, 0xb7, 0xb9, 0x3c,
0x3e, 0x25, 0x0d, 0x93, 0xd6, 0xe8, 0x5d, 0x69,
0x13, 0x6d, 0x86, 0x88, 0x07, 0xb2, 0xdb, 0xef,
0x56, 0x8e, 0xb6, 0x80, 0xab, 0x52, 0xa1, 0x2d,
Standard Control Value:
0x4e, 0x98, 0x11, 0x0c, 0x97, 0xb7, 0xb9, 0x3c,
0x3e, 0x25, 0x0d, 0x93, 0xd6, 0xe8, 0x5d, 0x69,
0x13, 0x6d, 0x86, 0x88, 0x07, 0xb2, 0xdb, 0xef,
0x56, 0x8e, 0xb6, 0x80, 0xab, 0x52, 0xa1, 0x2d,
------------------------------------------------- OK
*/
    static unsigned char plaintext_ctr_Z[] = {
		0x59, 0x0a, 0x13, 0x3c, 0x6b, 0xf0, 0xde, 0x92,
		0x20, 0x9d, 0x18, 0xf8, 0x04, 0xc7, 0x54, 0xdb,
		0x4c, 0x02, 0xa8, 0x67, 0x2e, 0xfb, 0x98, 0x4a,
		0x41, 0x7e, 0xb5, 0x17, 0x9b, 0x40, 0x12, 0x89,
    };
    static unsigned char iv_ctr_Z[] = {
		0x78, 0x56, 0x34, 0x12,
    };
    static unsigned char key_ctr_Z[] = {
		0xcc, 0xdd, 0xee, 0xff, 0x88, 0x99, 0xaa, 0xbb,
		0x44, 0x55, 0x66, 0x77, 0x00, 0x11, 0x22, 0x33,
		0xf3, 0xf2, 0xf1, 0xf0, 0xf7, 0xf6, 0xf5, 0xf4,
		0xfb, 0xfa, 0xf9, 0xf8, 0xff, 0xfe, 0xfd, 0xfc,
    };
    // id-tc26-gost-28147-param-Z (1.2.643.7.1.2.5.1.1)
    static unsigned char encr_ctr_tc26_Z[] = {
		0x11, 0xf3, 0x2d, 0xf1, 0xac, 0xce, 0x66, 0xb0,
		0x5a, 0xcf, 0x02, 0x86, 0xe1, 0x9b, 0x6f, 0x64,
		0x12, 0xeb, 0x70, 0xe6, 0xc6, 0xc9, 0xee, 0x74,
		0xc9, 0x61, 0xd9, 0x66, 0x4d, 0x48, 0x0f, 0xb5,
    };

    static CK_ATTRIBUTE  key_template[] = {
        { CKA_CLASS,           &oclass,   sizeof(oclass)    },
        { CKA_KEY_TYPE,        &key_type, sizeof(key_type)  },
//        { CKA_GOST28147PARAMS, oid6,      sizeof(oid6)      },
        { CKA_ENCRYPT,         &ltrue,    sizeof(CK_BBOOL)  },
        { CKA_DECRYPT,         &ltrue,    sizeof(CK_BBOOL)  },
        { CKA_VALUE,           key_ctr_Z, sizeof(key_ctr_Z) }
    };

//    printf("Param Set Z\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);
        goto end;
    }

    mechanism->pParameter     = iv_ctr_Z;
    mechanism->ulParameterLen = sizeof(iv_ctr_Z);
    rc                        = funcs->C_EncryptInit(sess, mechanism, keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_EncryptInit failed: 0x%lx\n", rc);
        goto end;
    }

    len = sizeof(plaintext_ctr_Z);
    rc  = funcs->C_Encrypt(sess, plaintext_ctr_Z, len, value, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_Encrypt failed: 0x%lx\n", rc);
        goto end;
    }

    CHECK(value, len, encr_ctr_tc26_Z);

    rc = funcs->C_DecryptInit(sess, mechanism, keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptInit failed: 0x%lx\n", rc);
        goto end;
    }

    len = sizeof(encr_ctr_tc26_Z);
    rc = funcs->C_Decrypt(sess, value, len, value2, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_Decrypt failed: 0x%lx\n", rc);
        goto end;
    }

    CHECK(value2, len, plaintext_ctr_Z);

    rc = funcs->C_EncryptInit(sess, mechanism, keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_EncryptInit failed: 0x%lx\n", rc);
        goto end;
    }

    len = 5;
    rc  = funcs->C_EncryptUpdate(sess, plaintext_ctr_Z, len, value, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_EncryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }

    if (memcmp(value, encr_ctr_tc26_Z, 5))
    {
        fprintf(stderr, "Test value invalid\n");
        goto end;
    }
    ;

    len = sizeof(plaintext_ctr_Z) - 5;
    rc  = funcs->C_EncryptUpdate(sess, plaintext_ctr_Z + 5, len, value + 5, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_EncryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }

    rc = funcs->C_EncryptFinal(sess, value2, &len);

    CHECK(value, sizeof(encr_ctr_tc26_Z), encr_ctr_tc26_Z);

    rc = funcs->C_DecryptInit(sess, mechanism, keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptInit failed: 0x%lx\n", rc);
        goto end;
    }

    len = 9;
    rc  = funcs->C_DecryptUpdate(sess, encr_ctr_tc26_Z, len, value, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }

    if (memcmp(value, plaintext_ctr_Z, 9))
    {
        fprintf(stderr, "Test value invalid\n");
        goto end;
    }
    ;

    len = sizeof(encr_ctr_tc26_Z) - 9;
    rc  = funcs->C_DecryptUpdate(sess, encr_ctr_tc26_Z + 9, len, value + 9, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }

    rc = funcs->C_DecryptFinal(sess, value2, &len);

    CHECK(value, sizeof(plaintext_ctr_Z), plaintext_ctr_Z);

    printf("Two Sessions With Get/SetOperationState\n");

    printf("Random Data\n");
    rc = funcs->C_GenerateRandom(sess, data2, sizeof(data2));
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GenerateRandom failed: 0x%lx\n", rc);
        goto end;
    }

    mechanism->pParameter     = iv_ctr_Z;
    mechanism->ulParameterLen = sizeof(iv_ctr_Z);
    rc                        = funcs->C_EncryptInit(sess, mechanism, keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_EncryptInit failed: 0x%lx\n", rc);
        goto end;
    }

    len = 191;
    rc  = funcs->C_EncryptUpdate(sess, data2, len, value, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_EncryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }

    // Save session operation state
    state_len = 0;
    rc        = funcs->C_GetOperationState(sess, NULL, &state_len);
    state     = malloc(state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
    rc = funcs->C_GetOperationState(sess, state, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }

    // Restore saved operation state to other session
    rc = funcs->C_SetOperationState(sess2, state, state_len,
                                    keyh, CK_INVALID_HANDLE);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_SetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
    memcpy(value2, value, sizeof(value));

    free(state);
    state_len = 0;
    rc        = funcs->C_GetOperationState(sess2, NULL, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
    state = malloc(state_len);
    rc    = funcs->C_GetOperationState(sess2, state, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }

    free(state);
    state_len = 0;
    rc        = funcs->C_GetOperationState(sess, NULL, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
    state = malloc(state_len);
    rc    = funcs->C_GetOperationState(sess, state, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
    rc = funcs->C_SetOperationState(sess, state, state_len,
                                    keyh, CK_INVALID_HANDLE);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_SetOperationState failed: 0x%lx\n", rc);
        goto end;
    }

    // Continue operations in first session
    len = sizeof(data2) - 191;
    rc  = funcs->C_EncryptUpdate(sess,
                                 data2 + 191, len, value + 191, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_EncryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }

    // Continue restored operations in second session
    len = sizeof(data2) - 191;
    rc  = funcs->C_EncryptUpdate(sess2,
                                 data2 + 191, len, value2 + 191, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_EncryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }

    // Finalize operations in first session
    len = sizeof(value) - sizeof(data2);
    rc  = funcs->C_EncryptFinal(sess, value + sizeof(data2), &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_EncryptFinal failed: 0x%lx\n", rc);
        goto end;
    }
    memcpy(value2, value, sizeof(value));

    free(state);
    state_len = 0;
    rc        = funcs->C_GetOperationState(sess2, NULL, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
    state = malloc(state_len);
    rc    = funcs->C_GetOperationState(sess2, state, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }

    // Finalize operations in second session
    len = sizeof(value2) - sizeof(data2);
    rc  = funcs->C_EncryptFinal(sess2, value2 + sizeof(data2), &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_EncryptFinal failed: 0x%lx\n", rc);
        goto end;
    }
    // len == 0

    rc = funcs->C_DecryptInit(sess2, mechanism, keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptInit failed: 0x%lx\n", rc);
        goto end;
    }

    len = sizeof(data2);
    rc  = funcs->C_Decrypt(sess2, value2, len, value, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_Decrypt failed: 0x%lx\n", rc);
        goto end;
    }

    CHECK(value, len, data2);

    rc = funcs->C_DecryptInit(sess2, mechanism, keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptInit failed: 0x%lx\n", rc);
        goto end;
    }

    len = 191;
    rc  = funcs->C_DecryptUpdate(sess2, value2, len, value, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }
    len = sizeof(data2) - 191;
    rc  = funcs->C_DecryptUpdate(sess2,
                                 value2 + 191, len, value + 191, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }
    len = sizeof(value2);
    rc  = funcs->C_DecryptFinal(sess2, value + sizeof(value2), &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptFinal failed: 0x%lx\n", rc);
        goto end;
    }

    // len == 0
    len = sizeof(data2);
    CHECK(value, len, data2);

    rc = funcs->C_DecryptInit(sess, mechanism, keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptInit failed: 0x%lx\n", rc);
        goto end;
    }
    free(state);
    state_len = 0;
    rc        = funcs->C_GetOperationState(sess, NULL, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
    state = malloc(state_len);
    rc    = funcs->C_GetOperationState(sess, state, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
	len = 191;
    rc = funcs->C_DecryptUpdate(sess, value2, len, value, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }
    free(state);
    state_len = 0;
    rc        = funcs->C_GetOperationState(sess, NULL, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
    state = malloc(state_len);
    rc    = funcs->C_GetOperationState(sess, state, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
	len = sizeof(data2) - 191;
    rc = funcs->C_DecryptUpdate(sess,
                                value2 + 191, len, value + 191, &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptUpdate failed: 0x%lx\n", rc);
        goto end;
    }
    free(state);
    state_len = 0;
    rc        = funcs->C_GetOperationState(sess, NULL, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
    state = malloc(state_len);
    rc    = funcs->C_GetOperationState(sess, state, &state_len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetOperationState failed: 0x%lx\n", rc);
        goto end;
    }
    rc = funcs->C_DecryptFinal(sess, value + sizeof(value2), &len);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DecryptFinal failed: 0x%lx\n", rc);
        goto end;
    }
    free(state);

    // len == 0
    len = sizeof(data2);
    CHECK(value, len, data2);

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

    return rc;
}

