#include "test_common.h"
#ifdef WIN32
#include "getopt.h"
#else 
extern char *optarg;
#endif

void usage(void);
CK_RV test_crypto();
int test_gost28147(CK_SESSION_HANDLE sess, CK_SESSION_HANDLE sess2);

CK_SLOT_ID SlotId;

CK_SLOT_ID_PTR SlotList = NULL;
CK_ULONG SlotCount = 0;

int main(int argc, char *argv[]) {
   CK_RV rc = 1;
   CK_FLAGS flags = 0;
   unsigned long slot_num = 0;  
   int i;

   // parse the command line parameters
	for (i=1; i<argc; i++) {
		if (strcmp("-api", argv[i]) == 0) {
			i++;
			api_path = argv[i];
		} else if (strcmp("-user_pin", argv[i]) == 0) {
			i++;
			user_pin = argv[i];
		}
	}

   // load the PKCS11 library 
	rc = init(api_path, &funcs, NULL);
	if (rc != CKR_OK) {
		printf("ERROR calling init, rc = %p.\n", (void *)rc);
		return rc;
	}
	rc = get_slot_list(funcs, 
                    &SlotList, 
                    &SlotCount);
	if (rc != CKR_OK) {
		printf("get_slot_list failed, rc = 0x%x\n", rc );
		return rc;
	}
	rc = find_slot_with_token(funcs, 
                           SlotList, 
                           SlotCount, 
                           &SlotId);
	if (rc != CKR_OK) {
		printf("find_slot_with_token failed, rc = 0x%x\n", rc );
		return rc;
	}
//	for (i=0; i<20; i++) {
		// test the crypto functions 
		rc = test_crypto();
		if (rc != CKR_OK) {
			printf("ERROR call to test_crypto failed.\n");
			return rc;
		}
		printf("test_crypto OK\n");
//	}
	return CKR_OK;
}


void usage(void)
{
   fprintf(stderr, "Usage: lissi [-c <slotId>]\n"
      "Default slotID is 0\n"
//      "To get a list of slotIDs, call %s/pkcs11_cfg -s\n", SBIN_PATH);
      "To get a list of slotIDs, call pkcs11_cfg -s\n");
   exit(-1);
}



CK_RV test_crypto() 
{
	CK_RV rc, ret;
	CK_SESSION_HANDLE hSession, hSession2;
	CK_MECHANISM_INFO minfo;

   // open a R/W cryptoki session, CKR_SERIAL_SESSION is a legacy bit we have to set
   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 = %p\n", (void *)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 = %p\n", (void *)rc);
      goto out_close;
   }
  fprintf(stderr, "C_Login success\n");
*/
  // open a R/W cryptoki session 2, CKR_SERIAL_SESSION is a legacy bit we have to set
   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 = %p\n", (void *)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 = %p\n", (void *)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 %p\n", (void *)rc);
   }
   else {
      fprintf(stderr, "C_CloseSession 2 success\n");
   }


//out_close:
   if( (ret = funcs->C_CloseSession(hSession)) != CKR_OK ) {
      fprintf(stderr, "Error: C_CloseSession failed with %p\n", (void *)ret);
	  rc = ret;
   }
   else {
      fprintf(stderr, "C_CloseSession success\n");
   }

out:
   return rc;
}

int 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;

	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,
	};
	CK_BYTE data[] = {
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	};
    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,
	};
	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) }
	};
	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;
	}

	if (len != sizeof(et1)) {
		fprintf(stderr, "Invalid ciphertext length: %d\n", len);
		rc = -1;
		goto end;
	}
	if (memcmp(value, et1, len) != 0) {
		fprintf(stderr, "Invalid ciphertext\n");
		rc = -2;
		goto end;
	}

	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;
	}

	if (len != sizeof(et2)) {
		fprintf(stderr, "Invalid ciphertext length: %d\n", len);
		rc = -3;
		goto end;
	}
	if (memcmp(value, et2, len) != 0) {
		fprintf(stderr, "Invalid ciphertext\n");
		rc = -4;
		goto end;
	}
	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;
	}

	if (len != sizeof(et3)) {
		fprintf(stderr, "Invalid ciphertext length: %d\n", len);
		rc = -5;
		goto end;
	}
	if (memcmp(value, et3, len) != 0) {
		fprintf(stderr, "Invalid ciphertext\n");
		rc = -6;
		goto end;
	}
	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;
	}

	if (len != sizeof(et4)) {
		fprintf(stderr, "Invalid ciphertext length: %d\n", len);
		rc = -7;
		goto end;
	}
	if (memcmp(value, et4, len) != 0) {
		fprintf(stderr, "Invalid ciphertext\n");
		rc = -8;
		goto end;
	}
	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;
	}

	if (len != sizeof(et5)) {
		fprintf(stderr, "Invalid ciphertext length: %d\n", len);
		rc = -9;
		goto end;
	}
	if (memcmp(value, et5, len) != 0) {
		fprintf(stderr, "Invalid ciphertext\n");
		rc = -10;
		goto end;
	}
	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;
	}
	if (len != sizeof(data2)) {
		fprintf(stderr, "Invalid decrypted text length: %d\n", len);
		rc = -13;
		goto end;
	}
	if (memcmp(value, data2, len) != 0) {
		fprintf(stderr, "Invalid decrypted text\n");
		rc = -14;
		goto end;
	}

	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;
	}

	if (memcmp(value, data2, sizeof(data2)) != 0) {
		fprintf(stderr, "Invalid decrypted text\n");
		rc = -16;
		goto end;
	}
	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);

	if (memcmp(value, data2, sizeof(data2)) != 0) {
		fprintf(stderr, "Invalid decrypted text\n");
		rc = -18;
		goto end;
	}
	printf("SUCCESS\n");
	rc = CKR_OK;
end:
	if (keyh != CK_INVALID_HANDLE) {
		funcs->C_DestroyObject(sess, keyh);
	}

	return rc;
}





