#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 dataLength = 0;
CK_ULONG cipherLength = 0;
CK_ULONG deLength = 0;
CK_ULONG i = 0;
CK_ULONG j = 0;
CK_ULONG m = 0;
CK_ULONG n = 0;
int match = 0;
if (
CKR_OK != (rv = C_Initialize( &initArgs ))) {
printf( "PKCS11 example9: failed C_Initialize (error:%lx)", rv );
return -1;
}
{
printf( "PKCS11 example9: 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, "F64B-7400-405E Rt1 slot 0 ", 64 );
if (0 == match)
{
rv = C_OpenSession( pSlotList[i], flags, NULL, NULL, &hSession );
{
CK_ULONG ulCardsRequired = 0;
CK_ULONG ulCardsInSet = 0;
CK_ULONG ulSharesLeft = 0;
CK_ULONG loginCount = 0;
rv = Thales_C_LoginBegin( hSession,
CKU_USER, &ulCardsRequired, &ulCardsInSet );
{
loginCount++;
rv = Thales_C_LoginNext( hSession,
CKU_USER, pUserPin, 6, &ulSharesLeft );
}
{
loginCount++;
rv = Thales_C_LoginNext( hSession,
CKU_USER, pUserPin, 6, &ulSharesLeft );
}
{
loginCount++;
rv = Thales_C_LoginEnd( hSession,
CKU_USER );
if (
CKR_OK == rv) loginCount++;
}
if (4 == loginCount)
{
keyLength = 32;
keyTemplate[0].
pValue = &bTFlag;
keyTemplate[1].
pValue = label;
keyTemplate[2].
pValue = &keyLength;
{
for ( j=0; j< 16; j++) buffer[j] = j+5;
keyTemplate2[0].
pValue = &objClass;
keyTemplate2[1].
pValue = &keyType;
keyTemplate2[2].
pValue = &bTFlag;
keyTemplate2[3].
pValue = label2;
keyTemplate2[4].
pValue = &bTFlag;
keyTemplate2[5].
pValue = buffer;
{
for( m=0; m < 16; m++ ) IV[m] = m+5;
if (
CKR_OK == (rv = C_EncryptInit( hSession, &cryptoMech, hKey1 )))
{
dataLength = 48;
cipherLength = 0;
for( m=0; m < dataLength; m++ ) data[m] = m+3;
for( n=0; n < 300; n++ ) cipherText[n] = 0;
dataLength = 48;
rv = C_Encrypt( hSession, data, dataLength,
NULL_PTR, &cipherLength );
if (
CKR_OK != rv && 48 != cipherLength) {
printf( "PKCS11 example9: C_Encrypt 1 CKR_OK and 48 expected, got %ld and %ld", rv, cipherLength );
}
rv = C_Encrypt( hSession, data, dataLength, cipherText, &cipherLength );
if ((
CKR_OK != rv && 48 != cipherLength) || 0 == cipherText[0] || 0 == cipherText[1]) {
printf( "PKCS11 example9: C_Encrypt 2 failed CKR_OK and 48, got %ld and %ld", rv, cipherLength );
}
if (
CKR_OK == (rv = C_DecryptInit( hSession, &cryptoMech, hKey1 )))
{
rv = C_Decrypt( hSession, cipherText, cipherLength,
NULL_PTR, &deLength );
if (
CKR_OK != rv && 48 != deLength) {
printf( "PKCS11 example9: C_Encrypt 1 CKR_OK and 48 expected, got %ld and %ld", rv, deLength );
}
rv = C_Decrypt( hSession, cipherText, cipherLength, decryptedData, &deLength );
for( j=0; j < 48; j++ )
{
if (data[j] != decryptedData[j]) {
printf( "PKCS11 example9: C_Decrypt original data mismatch Thales token, at {%ld} (%c) (%c)", j, data[j], decryptedData[j] );
}
}
}
}
}
}
rv = C_DestroyObject( hSession, hKey1 );
}
rv = C_DestroyObject( hSession, hKey2 );
}
if (
CKR_OK != (rv = C_Logout( hSession ))) {
printf( "PKCS11 example9: slot %ld failed to logout of token (error:%lx)", pSlotList[i], rv );
}
}
if (
CKR_OK != (rv = C_CloseSession( hSession ))) {
printf( "PKCS11 example9: slot %ld failed to close an active token session (error:%lx)", pSlotList[i], rv );
}
}
}
}
}
}
}
if (
NULL_PTR != pSlotList) free( pSlotList );
printf( "PKCS11 example9: failed C_Finalize %lx", rv );
return -3;
}
return 0;
}