Key Generation and Management

Key Generation

Methods used to generate keys.

pypkcs11.key_generator.c_copy_object(h_session, h_object, template=None)

Method to call the C_CopyObject cryptoki command.

Parameters:
  • h_session (int) – Session handle
  • h_object (int) – Handle to the object to be cloned
  • template (dict) – Template for the new object. Defaults to None
Returns:

(retcode, Handle to the new cloned object)

Return type:

tuple

pypkcs11.key_generator.c_derive_key(h_session, h_base_key, template, mechanism=None)

Derives a key from another key.

Parameters:
  • h_session (int) – Session handle
  • h_base_key (int) – The base key
  • template (dict) – A python template of attributes to set on derived key
  • mechanism – See the parse_mechanism() function for possible values.
Returns:

The result code, The derived key’s handle

pypkcs11.key_generator.c_destroy_object(h_session, h_object_value)

Deletes the object corresponsing to the passed in object handle

Parameters:
  • h_session (int) – Session handle
  • h_object_value (int) – The handle of the object to delete
Returns:

Return code

pypkcs11.key_generator.c_generate_key(h_session, mechanism=None, template=None)

Generates a symmetric key of a given flavor given the correct template.

Parameters:
  • h_session (int) – Session handle
  • template (dict) – The template to use to generate the key
  • mechanism – See the parse_mechanism() function for possible values.
Returns:

(retcode, generated key handle)

Rtype tuple:
pypkcs11.key_generator.c_generate_key_pair(h_session, mechanism=None, pbkey_template=None, prkey_template=None)

Generates a private and public key pair for a given flavor, and given public and private key templates. The return value will be the handle for the key.

Parameters:
  • h_session (int) – Session handle
  • pbkey_template (dict) – The public key template to use for key generation
  • prkey_template (dict) – The private key template to use for key generation
  • mechanism – See the parse_mechanism() function for possible values.
Returns:

(retcode, public key handle, private key handle)

Return type:

tuple

pypkcs11.key_generator.clear_keys(h_session)

Quick hacked together function that can be used to clear the first 10 000 keys.

Parameters:h_session (int) – Session handle