synapse.lib.crypto package
Submodules
synapse.lib.crypto.coin module
- synapse.lib.crypto.coin.logger = <Logger synapse.lib.crypto.coin (WARNING)>
synapse.lib.crypto.coin contains functions for verifying whether or not a given regex match containing a valu is valid for a given type of coin.
these functions are intended to be used with synapse.lib.scrape.
synapse.lib.crypto.ecc module
- class synapse.lib.crypto.ecc.PriKey(priv)[source]
Bases:
objectA helper class for using ECC private keys.
- dump(fmt='der')[source]
Get the private key bytes in PKCS8 format.
- Parameters:
fmt (str) – The encoding format, “der” (default) or “pem”.
- Returns:
The encoded PKCS8 private key.
- Return type:
bytes
- exchange(pubkey)[source]
Perform a ECDH key exchange with a public key.
- Parameters:
pubkey (PubKey) – A PubKey to perform the ECDH with.
- Returns:
The ECDH bytes. This is deterministic for a given pubkey and private key.
- Return type:
bytes
- static generate(curve='SECP384R1')[source]
Generate a new ECC PriKey instance.
- Parameters:
curve (str) – The named curve to use. Defaults to SECP384R1.
- Returns:
A new PriKey instance.
- Return type:
- iden()[source]
Return a SHA256 hash for the public key (to be used as a GUID).
- Returns:
The SHA256 hash of the public key bytes.
- Return type:
str
- static load(byts, fmt='der')[source]
Create a PriKey instance from PKCS8 encoded bytes.
- Parameters:
byts (bytes) – Bytes to load.
fmt (str) – The encoding format, “der” (default) or “pem”.
- Returns:
A new PriKey instance.
- Return type:
- class synapse.lib.crypto.ecc.PubKey(publ)[source]
Bases:
objectA helper class for using ECC public keys.
- dump(fmt='der')[source]
Get the public key bytes in SubjectPublicKeyInfo format.
- Parameters:
fmt (str) – The encoding format, “der” (default) or “pem”.
- Returns:
The encoded SubjectPublicKeyInfo public key.
- Return type:
bytes
- iden()[source]
Return a SHA256 hash for the public key (to be used as a GUID).
- Returns:
The SHA256 hash of the public key bytes.
- Return type:
str
- static load(byts, fmt='der')[source]
Create a PubKey instance from SubjectPublicKeyInfo encoded bytes.
- Parameters:
byts (bytes) – Bytes to load.
fmt (str) – The encoding format, “der” (default) or “pem”.
- Returns:
A new PubKey instance.
- Return type:
- verify(byts, sign, hashalgo='sha256')[source]
Verify the signature for the given bytes using the ECC public key.
- Parameters:
byts (bytes) – The data bytes.
sign (bytes) – The DER encoded signature bytes.
hashalgo (str) – The hash algorithm to use (sha256, sha384, or sha512).
- Returns:
True if the data was verified, False otherwise.
- Return type:
bool
- synapse.lib.crypto.ecc.doECDHE(statprv_u, statpub_v, ephmprv_u, ephmpub_v, length=64, salt=None, info=None)[source]
Perform one side of an Ecliptic Curve Diffie Hellman Ephemeral key exchange.
- Parameters:
statprv_u (PriKey) – Static Private Key for U
(PubKey (statpub_v) – Static Public Key for V
ephmprv_u (PriKey) – Ephemeral Private Key for U
ephmpub_v (PubKey) – Ephemeral Public Key for V
length (int) – Number of bytes to return
salt (bytes) – Salt to use when computing the key.
info (bytes) – Additional information to use when computing the key.
Notes
This makes no assumption about the reuse of the Ephemeral keys passed to the function. It is the caller’s responsibility to destroy the keys after they are used for doing key generation. This implementation is the dhHybrid1 scheme described in NIST 800-56A Revision 2.
- Returns:
The derived key.
- Return type:
bytes
synapse.lib.crypto.jwk module
- synapse.lib.crypto.jwk.jwkThumbprint(jwk)[source]
Compute the RFC 7638 JWK SHA-256 thumbprint and return it as a base64url string.
- synapse.lib.crypto.jwk.jwkToKey(jwk)[source]
Convert a JWK (RFC 7517) into a Synapse backend key wrapper.
- Parameters:
jwk (dict) – The JWK members.
- Returns:
An s_rsa.PriKey/PubKey or s_ecc.PriKey/PubKey for an RSA or EC key. Raises s_exc.BadArg for an unsupported key type, a malformed member, or an off-curve EC point.
synapse.lib.crypto.passwd module
- async synapse.lib.crypto.passwd.checkShadowV2(passwd: AnyStr, shadow: Dict) bool[source]
Check a password against a shadow dictionary.
- Parameters:
passwd (str) – Password to check.
shadow (dict) – Data to check the password against.
- Returns:
True if the password is valid, false otherwise.
- Return type:
bool
synapse.lib.crypto.rsa module
- class synapse.lib.crypto.rsa.PriKey(priv)[source]
Bases:
objectA helper class for using RSA private keys.
Signing methods use RSA-PSS and MFG1 with sha256 hashing.
- dump(fmt='der')[source]
Get the private key bytes in PKCS8 format.
- Parameters:
fmt (str) – The encoding format, “der” (default) or “pem”.
- Returns:
The encoded PKCS8 private key.
- Return type:
bytes
- static generate(bits=2048)[source]
Generate a new RSA PriKey instance.
- Parameters:
bits (int) – The size of the RSA key in bits.
- Returns:
A new PriKey instance.
- Return type:
- iden() str[source]
Return a SHA256 hash for the public key (to be used as a GUID).
- Returns:
The SHA256 hash of the public key bytes.
- Return type:
str
- static load(byts, fmt='der')[source]
Create a PriKey instance from PKCS8 encoded bytes.
- Parameters:
byts (bytes) – Bytes to load.
fmt (str) – The encoding format, “der” (default) or “pem”.
- Returns:
A new PriKey instance.
- Return type:
- public()[source]
Get the PubKey which corresponds to the RSA PriKey.
- Returns:
A new PubKey object whose key corresponds to the private key.
- Return type:
- sign(byts, padding='pss', hashalgo='sha256', saltlen=None)[source]
Compute the RSA signature for the given bytestream.
- Parameters:
byts (bytes) – The bytes to sign.
padding (str) – The padding scheme, “pss” (default) or “pkcs1v15”.
hashalgo (str) – The hash algorithm name (sha256, sha384, or sha512).
saltlen – The PSS salt length in bytes, or None for the maximum length.
- Returns:
The RSA signature bytes.
- Return type:
bytes
- class synapse.lib.crypto.rsa.PubKey(publ)[source]
Bases:
objectA helper class for using RSA public keys.
- dump(fmt='der')[source]
Get the public key bytes in SubjectPublicKeyInfo format.
- Parameters:
fmt (str) – The encoding format, “der” (default) or “pem”.
- Returns:
The encoded SubjectPublicKeyInfo public key.
- Return type:
bytes
- iden()[source]
Return a SHA256 hash for the public key (to be used as a GUID).
- Returns:
The SHA256 hash of the public key bytes.
- Return type:
str
- static load(byts, fmt='der')[source]
Create a PubKey instance from SubjectPublicKeyInfo encoded bytes.
- Parameters:
byts (bytes) – Bytes to load.
fmt (str) – The encoding format, “der” (default) or “pem”.
- Returns:
A new PubKey instance.
- Return type:
- verify(byts, sign, padding='pss', hashalgo='sha256', saltlen=None)[source]
Verify the signature for the given bytes using the RSA public key.
- Parameters:
byts (bytes) – The data bytes.
sign (bytes) – The signature bytes.
padding (str) – The padding scheme, “pss” (default) or “pkcs1v15”.
hashalgo (str) – The hash algorithm name (sha256, sha384, or sha512).
saltlen – The PSS salt length in bytes, or None for the maximum length.
- Returns:
True if the data was verified, False otherwise.
- Return type:
bool
synapse.lib.crypto.tinfoil module
- class synapse.lib.crypto.tinfoil.CryptSeq(rx_key, tx_key, initial_rx_seq=0, initial_tx_seq=0)[source]
Bases:
objectApplies and verifies sequence numbers of encrypted messages coming and going
- Parameters:
rx_key (bytes) – TX key (used with TinFoilHat).
tx_key (bytes) – RX key (used with TinFoilHat).
initial_rx_seq (int) – Starting rx sequence number.
initial_tx_seq (int) – Starting tx sequence number.
- class synapse.lib.crypto.tinfoil.TinFoilHat(ekey)[source]
Bases:
objectThe TinFoilHat class implements a GCM-AES encryption/decryption class.
- Parameters:
ekey (bytes) – A 32 byte key used for doing encryption & decryption. It
manner. (is assumed the caller has generated the key in a safe)
- dec(byts)[source]
Decode an envelope dict and decrypt the given bytes.
- Parameters:
byts (bytes) – Bytes to decrypt.
- Returns:
Decrypted message.
- Return type:
bytes
- enc(byts, asscd=None)[source]
Encrypt the given bytes and return an envelope dict in msgpack form.
- Parameters:
byts (bytes) – The message to be encrypted.
asscd (bytes) – Extra data that needs to be authenticated (but not encrypted).
- Returns:
The encrypted message. This is a msgpacked dictionary containing the IV, ciphertext, and associated data.
- Return type:
bytes
synapse.lib.crypto.utils module
- synapse.lib.crypto.utils.getCurveByName(name)[source]
Get an instantiated cryptography elliptic curve for the given curve name.
- Parameters:
name (str) – The named curve (P-256, P-384, or P-521).
- Returns:
The instantiated elliptic curve.
- synapse.lib.crypto.utils.getEncodingByName(fmt)[source]
Get the cryptography serialization Encoding for the given format name.
- Parameters:
fmt (str) – The encoding format, “der” or “pem”.
- Returns:
The cryptography Encoding.
- synapse.lib.crypto.utils.getHashByName(hashalgo)[source]
Get an instantiated cryptography hash for the given algorithm name.
- Parameters:
hashalgo (str) – The hash algorithm name (sha256, sha384, or sha512).
- Returns:
The instantiated hash algorithm.
- synapse.lib.crypto.utils.getPaddingByName(padding, hashobj, saltlen=None)[source]
Get the cryptography asymmetric padding for the given scheme name.
- Parameters:
padding (str) – The padding scheme, “pss” or “pkcs1v15”.
hashobj – The instantiated hash used to construct MGF1 for PSS padding.
saltlen – The PSS salt length in bytes. Defaults to the maximum length; callers that require a specific salt length (e.g. the JWS PS* algorithms, which mandate salt length equal to the digest length) may pass an integer.
- Returns:
The cryptography padding object.
- synapse.lib.crypto.utils.loadKey(byts)[source]
Load a single public or private key, auto-detecting the PEM vs DER encoding and whether the key is a public or private key.
- Parameters:
byts (bytes) – The DER or PEM encoded key bytes.
- Returns:
A
(isprivate, key)tuple wherekeyis the loaded cryptography key object.