#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
int main(int argc,char *argv[])
{
CK_ULONG ulSlotCount = 0;
CK_ULONG keyLength = 0;
CK_ULONG i = 0;
int match = 0;
if (
CKR_OK != (rv = C_Initialize( &initArgs ))) {
printf( "PKCS11 example5: failed C_Initialize (error:%lx)", rv );
return -1;
}
{
printf( "PKCS11 example5: memory allocation failed" );
return -2;
}
if (
CKR_OK == (rv = C_GetSlotList(
CK_FALSE, pSlotList, &ulSlotCount )))
{
for( i=0; i < ulSlotCount; i++ )
{
{
match = strncmp( (const char*)oneSlot.slotDescription, "KMIP SLOT1 ", 64 );
if (0 == match)
{
if (
CKR_OK != (rv = C_OpenSession( pSlotList[i], flags, NULL, NULL, &hSession ))) {
printf( "PKCS11 example5: slot %ld failed to open a session (error:%lx)", pSlotList[i], rv );
break;
}
{
keyLength = 16;
strncpy( (char*)label, "AES key Generated by the token", 30 );
keyTemplate[0].
pValue = &bTFlag;
keyTemplate[1].
pValue = label;
keyTemplate[2].
pValue = &keyLength;
keyTemplate[3].
pValue = &bTFlag;
keyTemplate[4].
pValue = &bTFlag;
{
printf( "PKCS11 example5: slot %ld failed to generate key on token (error:%lx)", pSlotList[i], rv );
}
else
{
CK_ULONG decodedLength = 0;
CK_ULONG dataLength = 48;
CK_ULONG cipherLength = 0;
CK_ULONG m = 0;
CK_ULONG n = 0;
CK_ULONG q = 0;
for( m=0; m < 16; m++ ) IV[m] = (
CK_BYTE)m+5;
rv = C_EncryptInit( hSession, &cryptoMech, hKey );
for( m=0; m < 48; m++ ) data[m] = (
CK_BYTE)m+3;
dataLength = 48;
rv = C_Encrypt( hSession, data, dataLength,
NULL_PTR, &cipherLength );
for( n=0; n < cipherLength; n++ ) pCipherText[n] = 0;
if (
CKR_OK == (rv = C_Encrypt( hSession, data, dataLength, pCipherText, &cipherLength )))
{
if (
CKR_OK == (rv = C_DecryptInit( hSession, &cryptoMech, hKey )))
{
decodedLength = 70;
rv = C_Decrypt( hSession, pCipherText, cipherLength, decryptedData, &decodedLength );
for( q=0; q < 48 &&
CKR_OK == rv; q++ )
{
if (data[q] != decryptedData[q]) {
printf("\n encryption process failed at offset %ld\n", q );
break;
}
}
}
}
free( pCipherText );
}
if (
CKR_OK != (rv = C_Logout( hSession ))) {
printf( "PKCS11 example5: slot %ld failed to logout of token (error:%lx)", pSlotList[i], rv );
}
}
if (
CKR_OK != (rv = C_CloseSession( hSession ))) {
printf( "PKCS11 example5: slot %ld failed to close an active token session (error:%lx)", pSlotList[i], rv );
break;
}
}
}
}
}
}
if (
NULL_PTR != pSlotList) free( pSlotList );
printf( "PKCS11 example5: failed C_Finalize %lx", rv );
return -3;
}
return 0;
}