#include "test_common.h"

CK_RV test_crypto();
CK_RV test_gost28147(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%x\n", rc);
		return rc;
	}
	rc = test_crypto();
	if (rc != CKR_OK) {
		fprintf(stderr, "test_crypto failed: 0x%x\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%x\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%x\n", rc);
		goto out;
	}
	fprintf(stderr, "C_OpenSession 2 success\n");

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

	if( (rc = funcs->C_CloseSession(hSession2)) != CKR_OK ) {
		fprintf(stderr, 
			"Error: C_CloseSession 2 failed with 0x%x\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%x\n", ret);
		rc = ret;
	}
	else {
		fprintf(stderr, "C_CloseSession success\n");
	}

out:
	return rc;
}

CK_RV test_gost28147(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_MECHANISM mechanism_desc = {CKM_GOST28147, 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_OBJECT_CLASS param_oclass = CKO_DOMAIN_PARAMETERS;
	static CK_KEY_TYPE key_type = CKK_GOST28147;

	static CK_BYTE key[] = {
		0xc3,0x73,0x0c,0x5c,0xbc,0xca,0xcf,0x91,
		0x5a,0xc2,0x92,0x67,0x6f,0x21,0xe8,0xbd,
		0x4e,0xf7,0x53,0x31,0xd9,0x40,0x5e,0x5f,
		0x1a,0x61,0xdc,0x31,0x30,0xa6,0x50,0x11,
	};
	static CK_BYTE data[] = {
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	};
    static CK_BYTE data2[200];

	//par_cipher_0
	static CK_BYTE oid1[] = { 
		0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1f, 0x00, 
	};
	static CK_BYTE et1[] = {
		0x08,0x79,0x76,0x86,0xee,0xbb,0x99,0xe7,
		0x7f,0xa3,0x75,0xff,0x6b,0x7c,0x27,0x6f,
	};

	//par_cipher_A
	static CK_BYTE oid2[] = { 
		0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1f, 0x01, 
	};
	static CK_BYTE et2[] = {
		0x49,0x11,0x96,0x11,0xad,0x19,0x12,0x52,
		0x7c,0x49,0x5f,0x23,0xb9,0x23,0x62,0xd9,
	};

	//par_cipher_B
	static CK_BYTE oid3[] = { 
		0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1f, 0x02,
	};
	static CK_BYTE et3[] = {
		0xab,0x45,0x07,0xb4,0x96,0xc5,0x5b,0xfd,
		0x61,0x16,0x64,0x9d,0x43,0x87,0x42,0x53,
	};

	//par_cipher_C
	static CK_BYTE oid4[] = { 
		0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1f, 0x03,
	};
	static CK_BYTE et4[] = {
		0x98,0x7c,0x0c,0x18,0x3c,0x64,0xd5,0x4b,
		0x75,0x8e,0xaf,0x67,0x52,0x87,0x15,0x7b,
	};

	//par_cipher_D
	static CK_BYTE oid5[] = { 
		0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1f, 0x04,
	};
	static CK_BYTE et5[] = {
		0xa2,0x4c,0x7c,0x4e,0x91,0xed,0x19,0x8c,
		0xdc,0xf5,0x1f,0x07,0x5c,0x80,0xf6,0xa4,
	};
	static CK_ATTRIBUTE key_template[] = {
		{ CKA_CLASS, &oclass, sizeof(oclass) },
		{ CKA_KEY_TYPE, &key_type, sizeof(key_type) },
		{ CKA_GOST28147PARAMS, oid1, sizeof(oid1) },
		{ CKA_ENCRYPT, &ltrue, sizeof(CK_BBOOL)},
		{ CKA_DECRYPT, &ltrue, sizeof(CK_BBOOL)},
		{ CKA_VALUE, key, sizeof(key) }
	};
	static CK_BYTE ivec[8] = {1};

	printf("Zero Data\n");
	printf("Param Set 0\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%x\n", rc);
		goto end;
	}

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

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

	CHECK(value, len, et1);

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

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

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

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

	CHECK(value, len, et2);

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

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

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

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

	CHECK(value, len, et3);

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

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

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

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

	CHECK(value, len, et4);

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

	printf("Param Set D\n");
	key_template[2].pValue = oid5;
	key_template[2].ulValueLen = sizeof(oid5);
	rc = funcs->C_CreateObject(sess, 
		key_template, sizeof(key_template)/sizeof(CK_ATTRIBUTE), &keyh);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_CreateObject failed: 0x%x\n", rc);
		goto end;
	}
	
	rc = funcs->C_EncryptInit(sess, mechanism, keyh);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_EncryptInit failed: 0x%x\n", rc);
		goto end;
	}

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

	CHECK(value, len, et5);

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

	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%x\n", rc);
		goto end;
	}

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

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

	len = sizeof(value);
	rc = funcs->C_EncryptUpdate(sess, data2, 192, value, &len);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_EncryptUpdate failed: 0x%x\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%x\n", rc);
		goto end;
	}
	rc = funcs->C_GetOperationState(sess, state, &state_len);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_GetOperationState failed: 0x%x\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%x\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%x\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%x\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%x\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%x\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%x\n", rc);
		goto end;
	}

    // Continue operations in first session
	len = sizeof(value) - 192;
	rc = funcs->C_EncryptUpdate(sess, 
		data2 + 192, sizeof(data2) - 192, value + 192, &len);        
	if (rc != CKR_OK) {
		fprintf(stderr, "C_EncryptUpdate failed: 0x%x\n", rc);
		goto end;
	}
    
    // Continue restored operations in second session
	len = sizeof(value2) - 192;
	rc = funcs->C_EncryptUpdate(sess2, 
		data2 + 192, sizeof(data2) - 192, value2 + 192, &len);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_EncryptUpdate failed: 0x%x\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%x\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%x\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%x\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%x\n", rc);
		goto end;
	}
	// len == 0

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

	len = 192;
	rc = funcs->C_DecryptUpdate(sess2, value2, 192, value, &len);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_DecryptUpdate failed: 0x%x\n", rc);
		goto end;
	}
	len = sizeof(value2)-192;
	rc = funcs->C_DecryptUpdate(sess2, 
		value2+192, sizeof(value2)-192, value+192, &len);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_DecryptUpdate failed: 0x%x\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%x\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%x\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%x\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%x\n", rc);
		goto end;
	}
	rc = funcs->C_DecryptUpdate(sess, value2, 192, value, &len);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_DecryptUpdate failed: 0x%x\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%x\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%x\n", rc);
		goto end;
	}
	rc = funcs->C_DecryptUpdate(sess, 
		value2+192, sizeof(value2)-192, value+192, &len);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_DecryptUpdate failed: 0x%x\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%x\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%x\n", rc);
		goto end;
	}
	rc = funcs->C_DecryptFinal(sess, value+sizeof(value2), &len);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_DecryptFinal failed: 0x%x\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;
}

