Additional C Sample Programs

There are also a number of additional C sample programs provided. For more information about the functionality of these programs refer to the description provided at the top of the source file for each of them.

Java Samples

Compiling and Running the Sample Programs

The binaries for the sample programs are included in jcprovsamples.jar file. However, in order to use the sources provided, you must compile them first. 

NOTE   JDK 7 or newer is required to compile these samples.

For best results, ensure that jcprov.jar is in your CLASSPATH environment variable before compiling the applications. Since all the applications are registered under the name space SafeNet_tech.jcprov.samples, a path that allows this namespace to be used must also be added to the CLASSPATH. If the samples are compiled in their installed locations, the path leading to the “samples” directory in the installation location will allow them to be executed as documented below.

For compiling and running under Windows

1.Set the CLASSPATH environment variable to point to jcprov.jar and sample programs’ root path.

C:\> set “CLASSPATH=C:\program files\safenet\cprovsdk\bin\jcprov.jar; C:\program files\safenet\cprovsdk\samples”  

2.Use javac program to compile the examples.

C:\Program Files\Safenet\CprovSDK\samples\SafeNet_tech\jcprov\samples> javac GetInfo.java  

3.Use java program to run samples.

C:\Program files\safenet\CprovSDK\samples\SafeNet_tech\jcprov\samples> javaSafeNet_tech.jcprov.samples.GetInfo -info  
For compiling and running under UNIX

1.Create a temporary compile directory.

% mkdir -p SafeNet_tech/jcprov/samples  

2.Copy the sample program and Makefile into that directory.

% cp /opt/safenet/protecttoolkit5/ptk/src/SafeNet_tech/jcprov/samples/* SafeNet_tech/jcprov/samples

3.Set the CLASSPATH environment variable to point to jcprov.jar and sample programs’ root path.

% export CLASSPATH=/opt/safenet/protecttoolkit5/ptk/lib/jcprov.jar:`pwd`

4.Change directory to sample programs’ path.

% cd SafeNet_tech/jcprov/samples 

5.Use javac program to compile the examples.

% javac GetInfo.java 

6.Use java program to run samples.

% java SafeNet_tech.jcprov.samples.GetInfo -info 

The Java Classes

DeleteKey

This class demonstrates the deletion of keys.

java SafeNet_tech.jcprov.samples.DeleteKey -keyType <keytype> -keyName <keyname> [-slot <slotId>] [-password <password>]

Option Description
keytype

One of (des, des2, des3, rsa). The types of keys supported are:

>des — single DES key

>des2 — double length Triple DES key

>des3 — triple length Triple DES key

>rsa — RSA Key Pair

keyname The name (label) of the key to delete.
slotId The slot containing the token to delete the key from. The default is (0).
password The user password of the slot. If specified, a private key is deleted.

EccDemo

This class demonstrates the generation of EC keys (prime192v1) and optionally performs sign/verify option with generated keys

java SafeNet_tech.jcprov.samples.EccDemo [-g] -n<keylabel>

Option Description
-g Generate Key Pair only (do not perform sign/verify)
-n<keylabel> Labels for key pair

EncDec

This class demonstrates the encryption and decryption operations.

java SafeNet_tech.jcprov.samples.EncDec -keyType <keytype> -keyName <keyname> [-slot <slotId>] [-password <password>]

Option Description
keytype

One of (des, des2, des3, rsa). The types of keys supported are:

>des — single DES key

>des2 — double length Triple DES key

>des3 — triple length Triple DES key

>rsa — RSA Key Pair

keyname The name (label) of the key to delete.
slotId The slot containing the token to delete the key from. The default is (0).
password The user password of the slot. If specified, a private key is used.

EnumAttributes

This class demonstrates the SafeNet extension to enumerate all attributes of an object.

java SafeNet_tech.jcprov.samples.EnumAttributes -name <objectname> [-slot <slotId>] [-password <password>]

Option Description
objectName The name (label) of the object to enumerate over.
slotId The slot containing the object. The default is (0).
password The user password of the slot. If specified, a private object is used.

GenerateKey

This class demonstrates the generation of keys.

java SafeNet_tech.jcprov.samples.GenerateKey -keyType <keytype> -keyName <keyname> [-slot <slotId>] [-password <password>]

Option Description
keytype

One of (des, des2, des3, rsa). The types of keys supported are:

>des — single DES key

>des2 — double length Triple DES key

>des3 — triple length Triple DES key

>rsa — RSA Key Pair

>ec — EC Key Pair

keyname The name (label) of the key to delete.
slotId The slot containing the token to delete the key from. The default is (0).
password The user password of the slot. If specified, a private key is created.

GetInfo

The class demonstrates the retrieval of Slot and Token Information.

java SafeNet_tech.jcprov.samples.GetInfo (-info, -slot, -token) [<slotId>]

Option Description
info Retrieve the General information.
slot Retrieve the Slot Information of the specified slot.
token Retrieve the Token Information of the token in the specified slot.
slotId The related slot ID of the slot or token information to retrieve. The default is (all).

ListObjects

This class demonstrates the listing of Token objects.

java SafeNet_tech.jcprov.samples.ListObjects [-slot <slotId>] [-password <password>]

Option Description
slotId The slot containing the token objects to list. The default is (0).
password The user password of the slot. If specified, private objects are also listed.

ReEncrypt

This class demonstrates re-encryption of variable length data.

Re-encryption is where cipher text (encrypted key or data) is decrypted with one key, and then the resulting plain text is encrypted with another key. Typically you want this operation to occur in such a way as to avoid having the intermediate plain text leaving the security of the adapter.

This is achieved in PKCS#11 via the C_UnwrapKey and C_WrapKey functions. By specifying the intermediate plain text data as a GENERIC_SECRET, SENSITIVE, Session object, you can keep variable length data securely in the adapter. This program assumes that slot 0 exists. All objects generated during program execution are session objects, and as such the contents of the token in slot 0 are not modified.

java SafeNet_tech.jcprov.samples.ReEncrypt

Threading

Sample program to show use of different ways to handle multi-threading.

This program initializes the Cryptoki library according to the specified locking model. Then a shared handle to the specified key is created. The specified number of threads is started, where each thread opens a session and then enters a loop which does a triple DES encryption operation using the shared key handle.

It is assumed that the key exists in slot 0, and is a Public Token object.

java ...Threading -numThreads <numthreads> -keyName <keyname> -locking <lockingmodel> [-v]

Option Description
numthreads The number of threads to start.
keyname The name of the Triple DES key to use for encryption operation. 
lockingmodel

The locking model, one of:

>None — No locking performed. Some of the threads should report failures.

>OS — Use native OS mechanisms to perform locking.

>Functions — Use Java functions to perform locking.