#include "test_common.h"

CK_RV test_crypto();
CK_RV test_gost28147_key_gen(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%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;
	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_GetMechanismInfo(SlotId, CKM_GOST28147_KEY_GEN, &minfo);
	if (rc != CKR_OK) {
		fprintf(stderr, 
"\n======== Mechanism CKM_GOST28147_KEY_GEN not supported ===========\n");
	} else {
		fprintf(stderr, 
"\n==================== CKM_GOST28147_KEY_GEN test ==================\n");
		rc = test_gost28147_key_gen(hSession);
		if (rc != CKR_OK) {
			fprintf(stderr, 
				"ERROR CKM_GOST28147_KEY_GEN failed, rc = 0x%x\n", rc);
		} else {
			fprintf(stderr, "CKM_GOST28147_KEY_GEN test passed.\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_key_gen(CK_SESSION_HANDLE sess)
{
	int rc = 0;
	CK_BYTE value[1024];
	CK_ULONG i;
	CK_OBJECT_HANDLE key = CK_INVALID_HANDLE;
	CK_OBJECT_HANDLE des_key = CK_INVALID_HANDLE;
	CK_BYTE_PTR base64 = NULL_PTR;
	FILE *fp = NULL;
	CK_ATTRIBUTE attr;
   	CK_MECHANISM mechanism_desc = {CKM_GOST28147_KEY_GEN, 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 ktype = CKK_GOST28147;
	static CK_BYTE oid[] = { 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x1f, 0x01 };
	static CK_BYTE ivec[] = {0x0f, 0xed, 0xcb, 0xa9, 0x87, 0x65, 0x43, 0x21};

	static CK_ATTRIBUTE ltemplate[] = {
		{ CKA_GOST28147PARAMS, oid, sizeof(oid) },
		{ CKA_EXTRACTABLE, &ltrue, sizeof(ltrue) },
		{ CKA_SENSITIVE, &lfalse, sizeof(lfalse) }
	};

	rc = funcs->C_GenerateKey(sess, mechanism,
			ltemplate, sizeof(ltemplate)/sizeof(CK_ATTRIBUTE), &key);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_GenerateKey failed: 0x%x\n", rc);
		return rc;
	}

	attr.type = CKA_VALUE;
	attr.pValue = value; 
	attr.ulValueLen = sizeof(value);
	rc = funcs->C_GetAttributeValue(sess, key, &attr, 1);
	if (rc != CKR_OK) {
		fprintf(stderr, "C_GetAttributeValue failed: 0x%x\n", rc);
		return rc;
	}

	printf("\n %%%%%% KEY: ");
	for (i = 0; i < attr.ulValueLen; i++) printf(" %02x", value[i]);
	printf(" %%%%%%\n\n");

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

	return rc;
}

