Skip to main content

PKCS#11 (Cryptoki) support for Python

Project description

A high level, “more Pythonic” interface to the PKCS#11 (Cryptoki) standard to support HSM and Smartcard devices in Python.

The interface is designed to follow the logical structure of a HSM, with useful defaults for obscurely documented parameters. Many APIs will optionally accept iterables and act as generators, allowing you to stream large data blocks in a straightforward way.

Source: https://github.com/danni/python-pkcs11

Documentation: http://python-pkcs11.readthedocs.io/en/latest/

Getting Started

Install from Pip:

pip install python-pkcs11

Or build from source:

python setup.py build

Assuming your PKCS#11 library is set as PKCS_MODULE and contains a token named DEMO:

import pkcs11

# Initialise our PKCS#11 library
lib = pkcs11.lib(os.environ['PKCS11_MODULE'])
# Get the first token that matches our criteria
token = next(lib.get_tokens(token_label='DEMO'))

data = b'INPUT DATA'
iv = b'0' * 16  # Never use a fixed IV for real crypto

# Open a session on our token
with token.open(user_pin='1234') as session:
    # Generate an AES key in this session
    key = session.generate_key(pkcs11.KeyType.AES, 128, store=False)

    # Encrypt our data
    crypttext = key.encrypt(data, mechanism_param=iv)

Tested Compatibility

Things that should almost certainly work.

PKCS#11 version:

  • 2.4

Libraries:

  • SoftHSMv2

  • Thales nCipher (Security World)

Mechanisms:

  • AES

  • RSA

Operations:

  • Encrypt

  • Decrypt

  • Generate Key

  • Generate Keypair

Feel free to send pull requests for any functionality that’s not exposed. The code is designed to be readable and expose the PKCS#11 spec in a straight-forward way.

More info on PKCS#11

The latest version of the PKCS#11 spec is available from OASIS:

http://docs.oasis-open.org/pkcs11/pkcs11-base/v2.40/pkcs11-base-v2.40.html

You should also consult the documentation for your PKCS#11 implementation. Many implementations expose additional vendor options configurable in your environment, including alternative features, modes and debugging information.

License

MIT License

Copyright (c) 2017 Danielle Madeley

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-pkcs11-0.0.2.tar.gz (269.6 kB view details)

Uploaded Source

File details

Details for the file python-pkcs11-0.0.2.tar.gz.

File metadata

File hashes

Hashes for python-pkcs11-0.0.2.tar.gz
Algorithm Hash digest
SHA256 42cb289c13fda1d5b590e8fd59c294af64f6c7f8971c94fadb088bfccaf29b1c
MD5 d7a507452e7c054dbf9b0ba1896ab28c
BLAKE2b-256 8e30498ed2f4035e935e8f08475456b3fb6313308a8047448bea9fda9a921229

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page