yubihsm package
Subpackages
Submodules
yubihsm.core module
Core classes for YubiHSM communication.
- class yubihsm.core.AsymmetricAuth(hsm, sid, context, receipt)[source]
Bases:
object
A negotiation of an authenticated Session with a YubiHSM.
This class is used to begin the mutual authentication process for establishing an authenticated session with the YubiHSM, using asymmetric authentication. Typically you get an instance of this class by calling
init_session_asymmetric()
.- authenticate(key_senc, key_smac, key_srmac)[source]
Construct an authenticated session.
- Parameters:
- Return type:
- Returns:
An authenticated session.
- classmethod create_session(hsm, auth_key_id, private_key, public_key)[source]
Construct an authenticated session.
- Parameters:
hsm (
YubiHsm
) – The YubiHSM connection.auth_key_id (
int
) – The ID of the Authentication key used to authenticate the session.private_key (
EllipticCurvePrivateKey
) – Private key corresponding to the public authentication key object.public_key (
EllipticCurvePublicKey
) – The device’s public key.
- Return type:
- class yubihsm.core.AuthSession(hsm, sid, key_enc, key_mac, key_rmac, mac_chain)[source]
Bases:
object
An authenticated secure session with a YubiHSM.
Typically you get an instance of this class by calling
create_session()
,create_session_derived()
, orcreate_session_asymmetric()
.- close()[source]
Close this session with the YubiHSM.
Once closed, this session object can no longer be used, unless re-connected.
- Return type:
- get_enabled_algorithms()[source]
Get the algorithms available, and whether or not they are enabled.
- get_fips_mode()[source]
Get the current setting for FIPS mode.
YubiHSM2 FIPS only.
- Return type:
- Returns:
True if in FIPS mode, False if not.
- get_fips_status()[source]
Get the current FIPS status.
YubiHSM2 FIPS only.
- Return type:
- Returns:
The FipsStatus value.
- get_force_audit()[source]
Get the current setting for forced audit mode.
- Return type:
- Returns:
The AUDIT setting for FORCE_AUDIT.
- get_log_entries(previous_entry=None)[source]
Get logs from the YubiHSM.
This returns a tuple of the number of unlogged boot events, the number of unlogged authentication events, and the log entries from the YubiHSM. The chain of entry digests will be validated, starting from the first entry returned, or the one supplied as previous_entry.
- get_object(object_id, object_type)[source]
Get a reference to a YhsmObject with the given id and type.
The object returned will be a subclass of YhsmObject corresponding to the given object_type.
- Parameters:
- Return type:
- Returns:
An object reference.
- list_objects(object_id=None, object_type=None, domains=None, capabilities=None, algorithm=None, label=None)[source]
List objects from the YubiHSM.
This returns a list of all objects currently stored on the YubiHSM, which are accessible by this session. The arguments to this method can be used to filter the results returned.
- Parameters:
object_id (
Optional
[int
]) – Return only objects with this ID.object_type (
Optional
[OBJECT
]) – Return only objects of this type.domains (
Optional
[int
]) – Return only objects belonging to one or more of these domains.capabilities (
Optional
[int
]) – Return only objects with one or more of these capabilities.algorithm (
Optional
[ALGORITHM
]) – Return only objects with this algorithm.label (
Optional
[str
]) – Return only objects with this label.
- Return type:
- Returns:
A list of matched objects.
- reset_device()[source]
Perform a factory reset of the YubiHSM.
Resets and reboots the YubiHSM, deletes all Objects and restores the default Authkey.
- Return type:
- set_command_audit(commands)[source]
Set audit mode of commands.
Takes a dict of COMMAND -> AUDIT pairs and updates the audit settings for the commands given.
>>> session.set_comment_audit({ :rtype: :sphinx_autodoc_typehints_type:`\:py\:obj\:\`None\`` ... COMMAND.ECHO: AUDIT.OFF, ... COMMAND.LIST_OBJECTS: AUDIT.ON ... })
- set_enabled_algorithms(algorithms)[source]
Set audit mode of commands.
New in YubiHSM 2.2.0.
Algorithms can only be toggled on a “fresh” device (after reset, before adding objects).
Takes a dict of ALGORITHM -> bool pairs and updates the enabled algorithm settings for the algorithms given.
>>> session.set_enabled_algorithms({ :rtype: :sphinx_autodoc_typehints_type:`\:py\:obj\:\`None\`` ... ALGORITHM.RSA_2048: False, ... ALGORITHM.RSA_OAEP_SHA256_: True, ... })
- set_fips_mode(mode)[source]
Set the FIPS mode of the YubiHSM.
YubiHSM2 FIPS only.
This can only be toggled on a “fresh” device (after reset, before adding objects).
- class yubihsm.core.DeviceInfo(version, serial, log_size, log_used, supported_algorithms, part_number)[source]
Bases:
object
Data class holding various information about the YubiHSM.
- Variables:
version – YubiHSM version tuple.
serial – YubiHSM serial number.
log_size – Log entry storage capacity.
log_used – Log entries currently stored.
supported_algorithms – List of supported algorithms.
part_number – Chip designator.
- class yubihsm.core.LogData(n_boot: int, n_auth: int, entries: Sequence[LogEntry])[source]
Bases:
NamedTuple
Data class holding response data from a GET_LOGS command.
- Parameters:
n_boot – Number of unlogged boot events.
n_auth – Number of unlogged authentication events.
entries – List of LogEntry items.
- class yubihsm.core.LogEntry(number, command, length, session_key, target_key, second_key, result, tick, digest)[source]
Bases:
object
YubiHSM log entry.
- Parameters:
number (int) – The sequence number of the entry.
command (int) – The COMMAND executed.
length (int) – The length of the command.
session_key (int) – The ID of the Authentication Key for the session.
target_key (int) – The ID of the key used by the command.
second_key (int) – The ID of the secondary key used by the command, if applicable.
result (int) – The result byte of the response.
tick (int) – The YubiHSM system tick value when the command was run.
digest (bytes) – A truncated hash of the entry and previous digest.
- property data: bytes
Get log entry binary data.
- Returns:
The binary LogEntry data, excluding the digest.
- class yubihsm.core.SymmetricAuth(hsm, sid, context, card_crypto)[source]
Bases:
object
A negotiation of an authenticated Session with a YubiHSM.
This class is used to begin the mutual authentication process for establishing an authenticated session with the YubiHSM, using symmetric authentication. Typically you get an instance of this class by calling
init_session()
.- authenticate(key_senc, key_smac, key_srmac)[source]
Construct an authenticated session.
- Parameters:
- Return type:
- Returns:
An authenticated session.
- classmethod create_session(hsm, auth_key_id, key_enc, key_mac)[source]
Construct an authenticated session.
- Parameters:
- Return type:
- class yubihsm.core.YubiHsm(backend)[source]
Bases:
object
An unauthenticated connection to a YubiHSM.
- classmethod connect(url=None)[source]
Return a YubiHsm connected to the backend specified by the URL.
If no URL is given this will attempt to connect to a YubiHSM connector running on localhost, using the default port.
- create_session(auth_key_id, key_enc, key_mac)[source]
Create an authenticated session with the YubiHSM.
See also create_session_derived, which derives K-ENC and K-MAC from a password.
- Parameters:
- Return type:
- Returns:
An authenticated session.
- create_session_asymmetric(auth_key_id, private_key, public_key=None)[source]
Create an authenticated session with the YubiHSM.
- Parameters:
auth_key_id (
int
) – The ID of the Authentication key used to authenticate the session.private_key (
EllipticCurvePrivateKey
) – Private key corresponding to the public authentication key object.public_key (
Optional
[EllipticCurvePublicKey
]) – The device’s public key. If omitted, the public key is fetched from the YubiHSM.
- Return type:
- Returns:
An authenticated session.
- create_session_derived(auth_key_id, password)[source]
Create an authenticated session with the YubiHSM.
Uses a supplied password to derive the keys K-ENC and K-MAC.
- Parameters:
- Return type:
- Returns:
An authenticated session.
- get_device_info()[source]
Get general device information from the YubiHSM.
- Return type:
- Returns:
Device information.
- get_device_public_key()[source]
Retrieve the device’s public key.
- Return type:
- Returns:
The device public key.
- init_session(auth_key_id)[source]
Initiate the symmetric authentication process for establishing an authenticated session with the YubiHSM.
- Parameters:
auth_key_id (
int
) – The ID of the Authentication key used to authenticate the session.- Return type:
- Returns:
A negotiation of an authenticated Session with a YubiHSM.
yubihsm.defs module
Named constants used in YubiHSM commands.
- class yubihsm.defs.ALGORITHM(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnum
Various algorithm constants
- AES128 = 50
- AES128_CCM_WRAP = 29
- AES128_YUBICO_AUTHENTICATION = 38
- AES128_YUBICO_OTP = 37
- AES192 = 51
- AES192_CCM_WRAP = 41
- AES192_YUBICO_OTP = 39
- AES256 = 52
- AES256_CCM_WRAP = 42
- AES256_YUBICO_OTP = 40
- AES_CBC = 54
- AES_ECB = 53
- AES_KWP = 55
- EC_BP256 = 16
- EC_BP384 = 17
- EC_BP512 = 18
- EC_ECDH = 24
- EC_ECDSA_SHA1 = 23
- EC_ECDSA_SHA256 = 43
- EC_ECDSA_SHA384 = 44
- EC_ECDSA_SHA512 = 45
- EC_ED25519 = 46
- EC_K256 = 15
- EC_P224 = 47
- EC_P256 = 12
- EC_P256_YUBICO_AUTHENTICATION = 49
- EC_P384 = 13
- EC_P521 = 14
- HMAC_SHA1 = 19
- HMAC_SHA256 = 20
- HMAC_SHA384 = 21
- HMAC_SHA512 = 22
- OPAQUE_DATA = 30
- OPAQUE_X509_CERTIFICATE = 31
- RSA_2048 = 9
- RSA_3072 = 10
- RSA_4096 = 11
- RSA_MGF1_SHA1 = 32
- RSA_MGF1_SHA256 = 33
- RSA_MGF1_SHA384 = 34
- RSA_MGF1_SHA512 = 35
- RSA_OAEP_SHA1 = 25
- RSA_OAEP_SHA256 = 26
- RSA_OAEP_SHA384 = 27
- RSA_OAEP_SHA512 = 28
- RSA_PKCS1_DECRYPT = 48
- RSA_PKCS1_SHA1 = 1
- RSA_PKCS1_SHA256 = 2
- RSA_PKCS1_SHA384 = 3
- RSA_PKCS1_SHA512 = 4
- RSA_PSS_SHA1 = 5
- RSA_PSS_SHA256 = 6
- RSA_PSS_SHA384 = 7
- RSA_PSS_SHA512 = 8
- TEMPLATE_SSH = 36
- static for_curve(curve)[source]
Returns a member corresponding to a Cryptography curve instance.
- Example:
- Return type:
>>> ALGORITHM.for_curve(ec.SECP256R1()) == ALGORITHM.EC_P256 True
- to_curve()[source]
Return a Cryptography EC curve instance for a given member.
- Returns:
The corresponding curve.
- Return type:
cryptography.hazmat.primitives.ec.
- Example:
>>> isinstance(ALGORITHM.EC_P256.to_curve(), ec.SECP256R1) True
- class yubihsm.defs.AUDIT(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnum
Values for audit options
- FIXED = 2
- OFF = 0
- ON = 1
- class yubihsm.defs.CAPABILITY(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntFlag
YubiHSM object capability flags
- CHANGE_AUTHENTICATION_KEY = 70368744177664
- CREATE_OTP_AEAD = 1073741824
- DECRYPT_CBC = 4503599627370496
- DECRYPT_ECB = 1125899906842624
- DECRYPT_OAEP = 1024
- DECRYPT_OTP = 536870912
- DECRYPT_PKCS = 512
- DELETE_ASYMMETRIC_KEY = 2199023255552
- DELETE_AUTHENTICATION_KEY = 1099511627776
- DELETE_HMAC_KEY = 8796093022208
- DELETE_OPAQUE = 549755813888
- DELETE_OTP_AEAD_KEY = 35184372088832
- DELETE_SYMMETRIC_KEY = 562949953421312
- DELETE_TEMPLATE = 17592186044416
- DELETE_WRAP_KEY = 4398046511104
- DERIVE_ECDH = 2048
- ENCRYPT_CBC = 9007199254740992
- ENCRYPT_ECB = 2251799813685248
- EXPORTABLE_UNDER_WRAP = 65536
- EXPORT_WRAPPED = 4096
- GENERATE_ASYMMETRIC_KEY = 16
- GENERATE_HMAC_KEY = 2097152
- GENERATE_OTP_AEAD_KEY = 68719476736
- GENERATE_SYMMETRIC_KEY = 281474976710656
- GENERATE_WRAP_KEY = 32768
- GET_LOG_ENTRIES = 16777216
- GET_OPAQUE = 1
- GET_OPTION = 262144
- GET_PSEUDO_RANDOM = 524288
- GET_TEMPLATE = 67108864
- IMPORT_WRAPPED = 8192
- PUBLIC_WRAP_KEY_DELETE = 36028797018963968
- PUBLIC_WRAP_KEY_WRITE = 18014398509481984
- PUT_ASYMMETRIC = 8
- PUT_AUTHENTICATION_KEY = 4
- PUT_HMAC_KEY = 1048576
- PUT_OPAQUE = 2
- PUT_OTP_AEAD_KEY = 34359738368
- PUT_SYMMETRIC_KEY = 140737488355328
- PUT_TEMPLATE = 134217728
- PUT_WRAP_KEY = 16384
- RANDOMIZE_OTP_AEAD = 2147483648
- RESET_DEVICE = 268435456
- REWRAP_FROM_OTP_AEAD_KEY = 4294967296
- REWRAP_TO_OTP_AEAD_KEY = 8589934592
- SET_OPTION = 131072
- SIGN_ATTESTATION_CERTIFICATE = 17179869184
- SIGN_ECDSA = 128
- SIGN_EDDSA = 256
- SIGN_HMAC = 4194304
- SIGN_PKCS = 32
- SIGN_PSS = 64
- SIGN_SSH_CERTIFICATE = 33554432
- UNWRAP_DATA = 274877906944
- VERIFY_HMAC = 8388608
- WRAP_DATA = 137438953472
- class yubihsm.defs.COMMAND(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnum
Commands available to send to the YubiHSM
- AUTHENTICATE_SESSION = 4
- BLINK_DEVICE = 107
- CHANGE_AUTHENTICATION_KEY = 108
- CLOSE_SESSION = 64
- CREATE_OTP_AEAD = 97
- CREATE_SESSION = 3
- DECRYPT_CBC = 113
- DECRYPT_ECB = 111
- DECRYPT_OAEP = 89
- DECRYPT_OTP = 96
- DECRYPT_PKCS1 = 73
- DELETE_OBJECT = 88
- DERIVE_ECDH = 87
- DEVICE_INFO = 6
- ECHO = 1
- ENCRYPT_CBC = 114
- ENCRYPT_ECB = 112
- ERROR = 127
- EXPORT_WRAPPED = 74
- EXPORT_WRAPPED_RSA = 118
- GENERATE_ASYMMETRIC_KEY = 70
- GENERATE_HMAC_KEY = 90
- GENERATE_OTP_AEAD_KEY = 102
- GENERATE_SYMMETRIC_KEY = 110
- GENERATE_WRAP_KEY = 91
- GET_DEVICE_PUBLIC_KEY = 10
- GET_LOG_ENTRIES = 77
- GET_OBJECT_INFO = 78
- GET_OPAQUE = 67
- GET_OPTION = 80
- GET_PSEUDO_RANDOM = 81
- GET_PUBLIC_KEY = 84
- GET_STORAGE_INFO = 65
- GET_TEMPLATE = 95
- IMPORT_WRAPPED = 75
- IMPORT_WRAPPED_RSA = 119
- LIST_OBJECTS = 72
- PUT_ASYMMETRIC_KEY = 69
- PUT_AUTHENTICATION_KEY = 68
- PUT_HMAC_KEY = 82
- PUT_OPAQUE = 66
- PUT_OTP_AEAD_KEY = 101
- PUT_PUBLIC_WRAP_KEY = 115
- PUT_SYMMETRIC_KEY = 109
- PUT_TEMPLATE = 94
- PUT_WRAP_KEY = 76
- RANDOMIZE_OTP_AEAD = 98
- RESET_DEVICE = 8
- REWRAP_OTP_AEAD = 99
- SESSION_MESSAGE = 5
- SET_LOG_INDEX = 103
- SET_OPTION = 79
- SIGN_ATTESTATION_CERTIFICATE = 100
- SIGN_ECDSA = 86
- SIGN_EDDSA = 106
- SIGN_HMAC = 83
- SIGN_PKCS1 = 71
- SIGN_PSS = 85
- SIGN_SSH_CERTIFICATE = 93
- UNWRAP_DATA = 105
- UNWRAP_KEY_RSA = 117
- VERIFY_HMAC = 92
- WRAP_DATA = 104
- WRAP_KEY_RSA = 116
- class yubihsm.defs.ERROR(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnum
Error codes returned by the YubiHSM
- ALGORITHM_DISABLED = 18
- AUTHENTICATION_FAILED = 4
- COMMAND_UNEXECUTED = 255
- DEMO_MODE = 16
- INSUFFICIENT_PERMISSIONS = 9
- INVALID_COMMAND = 1
- INVALID_DATA = 2
- INVALID_ID = 12
- INVALID_OTP = 15
- INVALID_SESSION = 3
- LOG_FULL = 10
- OBJECT_EXISTS = 17
- OBJECT_NOT_FOUND = 11
- OK = 0
- SESSIONS_FULL = 5
- SESSION_FAILED = 6
- SSH_CA_CONSTRAINT_VIOLATION = 14
- STORAGE_FAILED = 7
- WRONG_LENGTH = 8
- class yubihsm.defs.FIPS_STATUS(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnum
Values for FIPS status
- OFF = 0
- ON = 1
- PENDING = 3
- class yubihsm.defs.LIST_FILTER(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnum
Keys for use to filter on in list_objects
- ALGORITHM = 5
- CAPABILITIES = 4
- DOMAINS = 3
- ID = 1
- LABEL = 6
- TYPE = 2
- class yubihsm.defs.OBJECT(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnum
YubiHSM object types
- ASYMMETRIC_KEY = 3
- AUTHENTICATION_KEY = 2
- HMAC_KEY = 5
- OPAQUE = 1
- OTP_AEAD_KEY = 7
- PUBLIC_WRAP_KEY = 9
- SYMMETRIC_KEY = 8
- TEMPLATE = 6
- WRAP_KEY = 4
yubihsm.exceptions module
Exceptions thrown by this library.
- exception yubihsm.exceptions.YubiHsmAuthenticationError[source]
Bases:
YubiHsmError
Authentication failed.
- exception yubihsm.exceptions.YubiHsmConnectionError[source]
Bases:
YubiHsmError
The connection to the YubiHSM failed.
- exception yubihsm.exceptions.YubiHsmDeviceError(code)[source]
Bases:
YubiHsmError
The YubiHSM returned an error code.
- Parameters:
code (int) – The device error code.
- exception yubihsm.exceptions.YubiHsmInvalidRequestError[source]
Bases:
YubiHsmError
The request was not able to be sent to the YubiHSM.
- exception yubihsm.exceptions.YubiHsmInvalidResponseError[source]
Bases:
YubiHsmError
The YubiHSM returned an unexpected response.
yubihsm.objects module
Classes for interacting with objects on a YubiHSM.
- class yubihsm.objects.AsymmetricKey(session, object_id, seq=None)[source]
Bases:
YhsmObject
Used to sign/decrypt data with the private key of an asymmetric key pair.
- Supported algorithms:
- attest(attesting_key_id=0)[source]
Attest this asymmetric key.
Creates an X509 certificate containing this key pair’s public key, signed by the asymmetric key identified by the given ID. You also need a X509 certificate stored with the same ID as the attesting key in the YubiHSM, to be used as a template.
- Parameters:
attesting_key_id (
int
) – (optional) The ID of the asymmetric key used to attest. If omitted, the built-in Yubico attestation key is used.- Return type:
- Returns:
The attestation certificate.
- decrypt_oaep(data, label=b'', hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, mgf_hash=<cryptography.hazmat.primitives.hashes.SHA256 object>)[source]
Decrypt data encrypted with RSAES-OAEP.
- Parameters:
data (
bytes
) – The ciphertext to decrypt.label (
bytes
) – (optional) OAEP label.hash (
HashAlgorithm
) – (optional) The algorithm to use when hashing the data.mgf_hash (
HashAlgorithm
) – (optional) The algorithm to use for MGF1.
- Return type:
- Returns:
The decrypted plaintext.
- derive_ecdh(public_key)[source]
Perform an ECDH key exchange as specified in SP 800-56A.
- Parameters:
public_key (
EllipticCurvePublicKey
) – The public key to use for the key exchange.- Return type:
- Returns:
The resulting shared key.
- classmethod generate(session, object_id, label, domains, capabilities, algorithm)[source]
Generate a new private key in the YubiHSM.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – The algorithm to use for the private key.
- Return type:
- Returns:
A reference to the newly created object.
- get_certificate()[source]
Get the X509 certificate associated with the key.
An X509 certificate is associated with an asymmetric key if it is stored as an Opaque object with the same object ID as the key, and it has the
OPAQUE_X509_CERTIFICATE
algorithm set.Equivalent to calling Opaque(session, key_id).get_certificate().
- Return type:
- Returns:
The certificate stored for the object.
- get_public_key()[source]
Get the public key of the key pair.
This will return either a
RSAPublicKey
or aEllipticCurvePublicKey
depending on the algorithm of the key.Ed25519 keys will be returned as a Cryptography
Ed25519PublicKey
object if possible (requires Cryptography 2.6 or later), or an internal representation if not, either which can be serialized using theserialize_ed25519_public_key()
function.- Returns:
The public key of the key pair.
- classmethod put(session, object_id, label, domains, capabilities, key)[source]
Import a private key into the YubiHSM.
RSA and EC keys can be created by using the cryptography APIs. You can then pass either a
RSAPrivateKey
, aEllipticCurvePrivateKey
, or aEd25519PrivateKey
as key.- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.key – The private key to import.
- Return type:
- Returns:
A reference to the newly created object.
- put_certificate(label, domains, capabilities, certificate)[source]
Store an X509 certificate associated with this key.
Equivalent to calling Opaque.put_certificate(session, key_id, …).
- Parameters:
label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.certificate (
Certificate
) – A certificate to import.
- Return type:
- Returns:
A reference to the newly created object.
- sign_ecdsa(data, hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, length=0)[source]
Sign data using ECDSA.
- Parameters:
data (
bytes
) – The data to sign.hash (
HashAlgorithm
) – (optional) The algorithm to use when hashing the data.length (
int
) – (optional) length to pad/truncate the hash to.
- Return type:
- Returns:
The resulting signature.
- sign_pkcs1v1_5(data, hash=<cryptography.hazmat.primitives.hashes.SHA256 object>)[source]
Sign data using RSASSA-PKCS1-v1_5.
- Parameters:
data (
bytes
) – The data to sign.hash (
HashAlgorithm
) – (optional) The algorithm to use when hashing the data.
- Return type:
- Returns:
The resulting signature.
- sign_pss(data, salt_len, hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, mgf_hash=<cryptography.hazmat.primitives.hashes.SHA256 object>)[source]
Sign data using RSASSA-PSS with MGF1.
- Parameters:
data (
bytes
) – The data to sign.salt_len (
int
) – The length of the salt to use.hash (
HashAlgorithm
) – (optional) The algorithm to use when hashing the data.mgf_hash (
HashAlgorithm
) – (optional) The algorithm to use for MGF1.
- Return type:
- Returns:
The resulting signature.
- class yubihsm.objects.AuthenticationKey(session, object_id, seq=None)[source]
Bases:
YhsmObject
Used to authenticate a session with the YubiHSM.
AuthenticationKeys use two separate keys to mutually authenticate and set up a secure session with a YubiHSM. These two keys can either be given explicitly, or be derived from a password.
- change_password(password)[source]
Change the password used to authenticate a session.
Changes the raw keys used for authentication, by deriving them from a password.
- change_public_key(public_key)[source]
Change an asymmetric AuthenticationKey’s public key
- Parameters:
public_key (
EllipticCurvePublicKey
) – The new public key.- Return type:
- classmethod put(session, object_id, label, domains, capabilities, delegated_capabilities, key_enc, key_mac)[source]
Create an AuthenticationKey by providing raw keys.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.delegated_capabilities (
CAPABILITY
) – The set of capabilities that the AuthenticationKey can give to objects created when authenticated using it.key_enc (
bytes
) – The raw encryption key.key_mac (
bytes
) – The raw MAC key.
- Return type:
- Returns:
A reference to the newly created object.
- classmethod put_derived(session, object_id, label, domains, capabilities, delegated_capabilities, password)[source]
Create an AuthenticationKey derived from a password.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.delegated_capabilities (
CAPABILITY
) – The set of capabilities that the AuthenticationKey can give to objects created when authenticated using it.password (
str
) – The password to derive raw keys from.
- Return type:
- Returns:
A reference to the newly created object.
- classmethod put_public_key(session, object_id, label, domains, capabilities, delegated_capabilities, public_key)[source]
Create an asymmetric AuthenticationKey by providing a public key
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.delegated_capabilities (
CAPABILITY
) – The set of capabilities that the AuthenticationKey can give to objects created when authenticated using it.public_key (
EllipticCurvePublicKey
) – The public key to import.
- Return type:
- Returns:
A reference to the newly created object.
- class yubihsm.objects.HmacKey(session, object_id, seq=None)[source]
Bases:
YhsmObject
Used to calculate and verify HMAC signatures.
- Supported algorithms:
- classmethod generate(session, object_id, label, domains, capabilities, algorithm=ALGORITHM.HMAC_SHA256)[source]
Generate a new HMAC key in the YubiHSM.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – (optional) The algorithm to use for the HMAC key.
- Return type:
- Returns:
A reference to the newly created object.
- classmethod put(session, object_id, label, domains, capabilities, key, algorithm=ALGORITHM.HMAC_SHA256)[source]
Import an HMAC key into the YubiHSM.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.key (
bytes
) – The raw key corresponding to the algorithm.algorithm – (optional) The algorithm to use for the HMAC key.
- Return type:
- Returns:
A reference to the newly created object.
- class yubihsm.objects.ObjectInfo(capabilities, id, size, domains, object_type, algorithm, sequence, origin, label, delegated_capabilities)[source]
Bases:
object
Data structure holding various information about an object.
- Variables:
capabilities – The capabilities of the object.
id – The ID of the object.
size – The size of the object.
domains – The set of domains the object belongs to.
object_type – The type of the object.
algorithm – The algorithm of the object.
sequence – The sequence number of the object.
origin – How the object was created/imported.
label – The label of the object.
delegated_capabilities – The set of delegated capabilities for the object.
-
capabilities:
CAPABILITY
-
delegated_capabilities:
CAPABILITY
- class yubihsm.objects.Opaque(session, object_id, seq=None)[source]
Bases:
YhsmObject
Object used to store arbitrary data on the YubiHSM.
- Supported algorithms:
- get()[source]
Read the data of an Opaque object from the YubiHSM.
- Return type:
- Returns:
The data stored for the object.
- get_certificate()[source]
Read an Opaque object from the YubiHSM, parsed as a certificate.
- Return type:
- Returns:
The certificate stored for the object.
- classmethod put(session, object_id, label, domains, capabilities, algorithm, data)[source]
Import an Opaque object into the YubiHSM.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – The algorithm to use for the object.data (
bytes
) – The binary data to store.
- Return type:
- Returns:
A reference to the newly created object.
- classmethod put_certificate(session, object_id, label, domains, capabilities, certificate)[source]
Import an X509 certificate into the YubiHSM as an Opaque.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.certificate (
Certificate
) – A certificate to import.
- Return type:
- Returns:
A reference to the newly created object.
- class yubihsm.objects.OtpAeadKey(session, object_id, seq=None)[source]
Bases:
YhsmObject
Used to decrypt and use a Yubico OTP AEAD for OTP decryption.
- Supported algorithms:
- classmethod generate(session, object_id, label, domains, capabilities, algorithm, nonce_id)[source]
Generate a new OTP AEAD key in the YubiHSM.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – The algorithm to use for the key.nonce_id (
int
) – The nonce ID used for AEADs.
- Return type:
- Returns:
A reference to the newly created object.
- classmethod put(session, object_id, label, domains, capabilities, algorithm, nonce_id, key)[source]
Import an OTP AEAD key into the YubiHSM.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – The algorithm to use for the key.nonce_id (
int
) – The nonce ID used for AEADs.key (
bytes
) – The key to import, corresponding to the algorithm.
- Return type:
- Returns:
A reference to the newly created object.
- class yubihsm.objects.OtpData(use_counter: int, session_counter: int, timestamp_high: int, timestamp_low: int)[source]
Bases:
NamedTuple
Decrypted OTP counter values.
- Parameters:
use_counter – 16 bit counter incremented on each power cycle.
session_counter – 8 bit counter incremented on each touch.
timestamp_high – 8 bit high part of the timestamp.
timestamp_low – 16 bit low part of the timestamp.
- class yubihsm.objects.PublicWrapKey(session, object_id, seq=None)[source]
Bases:
YhsmObject
Used to export other objects under wrap using the public key of an asymmetric key pair.
The algorithm used for wrapping is CKM_RSA_AES_KEY_WRAP, as specified in PKCS#11.
Supported algorithms: -
RSA_2048
-RSA_3072
-RSA_4096
- export_raw_key(key, algorithm=ALGORITHM.AES256, oaep_hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, mgf_hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, oaep_label=b'')[source]
Export an (a)symmetric key object under asymmetric wrap.
This command wraps only the raw key material of the key object. Asymmetric keys are serialized as PKCS#8.
- Parameters:
key (
Union
[AsymmetricKey
,SymmetricKey
]) – The (a)symmetric key object to wrap.algorithm (
ALGORITHM
) – (optional) The algorithm for the ephemeral key.oaep_hash (
HashAlgorithm
) – (optional) The hash algorithm to use for OAEP label.mgf_hash (
HashAlgorithm
) – (optional) The hash algorithm to use for MGF1.oaep_label (
bytes
) – (optional) OAEP label.
- Return type:
- Returns:
The encrypted key.
- export_wrapped_rsa(obj, algorithm=ALGORITHM.AES256, oaep_hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, mgf_hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, oaep_label=b'')[source]
Export an object under asymmetric wrap.
- Parameters:
obj (
YhsmObject
) – The object to export.algorithm (
ALGORITHM
) – (optional) The algorithm to use for the ephemeral key.oaep_hash (
HashAlgorithm
) – (optional) The hash algorithm to use for OAEP label.mgf_hash (
HashAlgorithm
) – (optional) The hash algorithm to use for MGF1.oaep_label (
bytes
) – (optional) OAEP label.
- Return type:
- Returns:
The encrypted object data.
- classmethod put(session, object_id, label, domains, capabilities, delegated_capabilities, public_key)[source]
Import a public RSA wrapkey into the YubiHSM.
The RSA public key can be supplied using the cryptography API. You can then pass a
RSAPublicKey
as public_key.- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.delegated_capabilities (
CAPABILITY
) – The set of capabilities that the WrapKey can give to objects that it imports.public_key (
RSAPublicKey
) – The public key to import.
- Return type:
- Returns:
A reference to the newly created object.
- class yubihsm.objects.SymmetricKey(session, object_id, seq=None)[source]
Bases:
YhsmObject
Used to encrypt/decrypt data using a symmetric key.
- classmethod generate(session, object_id, label, domains, capabilities, algorithm)[source]
Generate a new symmetric key in the YubiHSM.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – The algorithm to use for the symmetric key.
- Return type:
- Returns:
A reference to the newly created object.
- classmethod put(session, object_id, label, domains, capabilities, algorithm, key)[source]
Import a symmetric key into the YubiHSM.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – The algorithm to use for the symmetric key.key (
bytes
) – The raw encryption key corresponding to the algorithm.
- Return type:
- Returns:
A reference to the newly created object.
- class yubihsm.objects.Template(session, object_id, seq=None)[source]
Bases:
YhsmObject
Binary template used to validate SSH certificate requests.
- Supported algorithms:
- classmethod put(session, object_id, label, domains, capabilities, algorithm, data)[source]
Import a Template into the YubiHSM.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – The algorithm to use for the template.data (
bytes
) – The template data.
- Return type:
- Returns:
A reference to the newly created object.
- class yubihsm.objects.WrapKey(session, object_id, seq=None)[source]
Bases:
YhsmObject
Used to import and export other objects under wrap.
Asymmetric wrapkeys are only used for importing wrapped objects. To export objects under asymmetric wrap, use
PublicWrapKey
.- Supported algorithms:
- export_wrapped(obj, seed=False)[source]
Export an object under wrap.
- Parameters:
obj (
YhsmObject
) – The object to export.seed (
bool
) – (optional) Export key with seed. Only applicable for ed25519 key objects.
- Return type:
- Returns:
The encrypted object data.
- classmethod generate(session, object_id, label, domains, capabilities, algorithm, delegated_capabilities)[source]
Generate a new wrap key in the YubiHSM.
- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – The algorithm to use for the wrap key.
- Return type:
- Returns:
A reference to the newly created object.
- import_raw_key(object_id, object_type, label, domains, capabilities, algorithm, wrapped, oaep_hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, mgf_hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, oaep_label=b'')[source]
Import an (a)symmetric key previously exported under asymmetric wrap.
Asymmetric keys are expected to have been serialized as PKCS#8.
- Parameters:
object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.object_type (
OBJECT
) – The key object type (OBJECT.ASYMMETRIC_KEY or OBJECT.SYMMETRIC_KEY).label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – The algorithm of the key.wrapped (
bytes
) – The wrapped key object.oaep_hash (
HashAlgorithm
) – (optional) The hash algorithm to use for OAEP label.mgf_hash (
HashAlgorithm
) – (optional) The hash algorithm to use for MGF1.oaep_label (
bytes
) – (optional) OAEP label.
- Return type:
- Returns:
A reference to the imported key object.
- import_wrapped(wrapped_obj)[source]
Import an object previously exported under wrap.
- Parameters:
wraped_obj – The encrypted object data.
- Return type:
- Returns:
A reference to the imported object.
- import_wrapped_rsa(wrapped_obj, oaep_hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, mgf_hash=<cryptography.hazmat.primitives.hashes.SHA256 object>, oaep_label=b'')[source]
Import an object previously exported under asymmetric wrap.
- Parameters:
wrapped_obj (
bytes
) – The encrypted object data.oaep_hash (
HashAlgorithm
) – (optional) The hash algorithm to use for OAEP label.mgf_hash (
HashAlgorithm
) – (optional) The hash algorithm to use for MGF1.oaep_label (
bytes
) – (optional) OAEP label.
- Return type:
- Returns:
A reference to the imported object.
- classmethod put(session, object_id, label, domains, capabilities, algorithm, delegated_capabilities, key)[source]
Import a wrap key into the YubiHSM.
Asymmetric keys can be imported using the cryptography API. You can then pass a
RSAPrivateKey
as key.- Parameters:
session (
AuthSession
) – The session to import via.object_id (
int
) – The ID to set for the object. Set to 0 to let the YubiHSM designate an ID.label (
str
) – A text label to give the object.domains (
int
) – The set of domains to assign the object to.capabilities (
CAPABILITY
) – The set of capabilities to give the object.algorithm (
ALGORITHM
) – The algorithm to use for the wrap key.delegated_capabilities (
CAPABILITY
) – The set of capabilities that the WrapKey can give to objects that it imports.key (
Union
[bytes
,RSAPrivateKey
]) – The encryption key corresponding to the algorithm.
- Return type:
- Returns:
A reference to the newly created object.
- class yubihsm.objects.YhsmObject(session, object_id, seq=None)[source]
Bases:
object
A reference to an object stored in a YubiHSM.
YubiHSM objects are uniquely identified by their type and ID combined.
- Variables:
session – The session to use for YubiHSM communication.
id – The ID of the object.
object_type – The type of the object.
- delete()[source]
Delete the object from the YubiHSM. :rtype:
None
Warning
This action in irreversible.
- get_info()[source]
Read extended information about the object from the YubiHSM.
- Return type:
- Returns:
Information about the object.
- with_session(session)[source]
Get a copy of the object reference, using the given session.
- Parameters:
session (
AuthSession
) – The session to use for the created reference.- Return type:
TypeVar
(T_Object
, bound= YhsmObject)- Returns:
A new reference to the object, associated wth the given session.
yubihsm.utils module
Various utility functions used throughout the library.
Module contents
Contains the main YubiHsm class used to connect to a YubiHSM device.
See YubiHsm
.
- Example:
>>> from yubihsm import YubiHsm
... hsm = YubiHsm.connect('http://localhost:12345')
... session = hsm.create_session_derived(1, 'password')