Base procedures

View some openCryptoki code samples for base procedures, such as a main program, an initialization procedure, and finalize information.

Main program

/* Example program to test opencryptoki
 * build: gcc test_ock.c -g -O0 -o test_ock -lopencryptoki -ldl 
                         -I /root/opencryptoki/usr/include/pkcs11/
 * execute: ./test_ock -c <slot> -p <PIN> */
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <dlfcn.h>
#include <pkcs11types.h>
#include <string.h>
#include <unistd.h>
#define OCKSHAREDLIB "libopencryptoki.so"

void *lib_ock;
char *pin = NULL;
int count, arg;
CK_SLOT_ID  slotID = 0;
CK_ULONG rsaKeyLen = 2048, cipherTextLen = 0, clearTextLen = 0;
CK_BYTE *pCipherText = NULL, *pClearText = NULL;
CK_BYTE *pRSACipher = NULL, *pRSAClear = NULL;
CK_FLAGS rw_sessionFlags = CKF_RW_SESSION | CKF_SERIAL_SESSION;
CK_SESSION_HANDLE hSession;
CK_BYTE keyValue[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
                      0xCA,0xFE,0xBE,0xEF,0xCA,0xFE,0xBE,0xEF};
CK_BYTE msg[] = "The quick brown fox jumps over the lazy dog";
CK_ULONG msgLen = sizeof(msg);
CK_OBJECT_HANDLE hPublicKey, hPrivateKey;

/*** <insert helper functions (provided below) here> ***/
/*** usage / help ***/
void usage(void)
{
  printf("Usage:\n");
  printf(" -s <slot number> \n");
  printf(" -p <user PIN>\n");
  printf("\n");
  exit (8);  }

int main(int argc, char *argv[]) {
   while ((arg = getopt (argc, argv, "s:p:")) != -1) {
	switch (arg) {
	case 's':   slotID = atoi(optarg);
		     break;
	case 'p':   pin = malloc(strlen(optarg));
            	strcpy(pin,optarg);
		     break;
	default:    printf("wrong option %c", arg);
		     usage();
	}  }

  if ((!pin) || (!slotID)) {
	printf("Incorrect parameter given!\n");
	usage();
	exit (8);   }

  init();
  openSession(slotID, rw_sessionFlags, &hSession);
  loginSession(CKU_USER, pin, 8, hSession);
  createKeyObject(hSession, (CK_BYTE_PTR)&keyValue, sizeof(keyValue));
  AESencrypt(hSession, (CK_BYTE_PTR)&msg, msgLen, &pCipherText, &cipherTextLen);
  AESdecrypt(hSession, pCipherText, cipherTextLen, &pClearText, &clearTextLen);
  generateRSAKeyPair(hSession, rsaKeyLen, &hPublicKey, &hPrivateKey);
  RSAencrypt(hSession, hPublicKey, (CK_BYTE_PTR)&msg, msgLen, &pRSACipher, &rsaKeyLen);
  RSAdecrypt(hSession, hPrivateKey, pRSACipher, rsaKeyLen, &pRSAClear, &rsaKeyLen);
  logoutSession(hSession); closeSession(hSession);
  finalize();
  return 0;
}

C_Initialize

/*
 * initialize
 */
CK_RV init(void){
  CK_RV rc;
  lib_ock = dlopen(OCKSHAREDLIB, RTLD_GLOBAL | RTLD_NOW);
  if (!lib_ock) {
	printf("Error loading shared lib '%s' [%s]", OCKSHAREDLIB, dlerror());
	return 1;
  }
  rc = C_Initialize(NULL);
  if (rc != CKR_OK) {
     printf("Error initializing the opencryptoki library: 0x%X\n", rc);
  }
  return CKR_OK;
}

C_Finalize

/*
 * finalize
 */
CK_RV finalize(void) {
  CK_RV rc;
  rc = C_Finalize(NULL);
  if (rc != CKR_OK) {
		 printf("Error during finalize: %x\n", rc);
  }
  if (pCipherText) free(pCipherText);
  if (pClearText)  free(pClearText);
  if (pRSACipher)  free(pRSACipher);
  if (pRSAClear)   free(pRSAClear);
  return rc;
}