Module: base_state
- exception x3dh.base_state.KeyAgreementException[source]
Bases:
ExceptionException raised by
BaseState.get_shared_secret_active()andBaseState.get_shared_secret_passive()in case of an error related to the key agreement operation.
- class x3dh.base_state.BaseState[source]
Bases:
ABCThis class is the core of this X3DH implementation. It offers methods to manually manage the X3DH state and perform key agreements with other parties.
Warning
This class requires manual state management, including e.g. signed pre key rotation, pre key hiding/deletion and refills. The subclass
Stateautomates those management/maintenance tasks and should be preferred if external/manual management is not explicitly wanted.- classmethod create(identity_key_format, hash_function, info, identity_key_pair=None)[source]
- Parameters:
identity_key_format (
IdentityKeyFormat) – The format in which the identity public key is included in bundles/headers.hash_function (
HashFunction) – A 256 or 512-bit hash function.info (
bytes) – A (byte) string identifying the application.identity_key_pair (
IdentityKeyPair|None) – If set, use the given identity key pair instead of generating a new one.
- Return type:
TypeVar(BaseStateTypeT, bound= BaseState)- Returns:
A configured instance of
BaseState. Note that an identity key pair and a signed pre key are generated, but no pre keys. Usegenerate_pre_keys()to generate some.
- abstractmethod static _encode_public_key(key_format, pub)[source]
- Parameters:
key_format (
IdentityKeyFormat) – The format in which this public key is serialized.pub (
bytes) – The public key.
- Return type:
- Returns:
An encoding of the public key, possibly including information about the curve and type of key, though this is application defined. Note that two different public keys must never result in the same byte sequence, uniqueness of the public keys must be preserved.
- property model: BaseStateModel
Returns: The internal state of this
BaseStateas a pydantic model. Note that pre keys hidden usinghide_pre_key()are not considered part of the state.
- property json: JSONObject
Returns: The internal state of this
BaseStateas a JSON-serializable Python object. Note that pre keys hidden usinghide_pre_key()are not considered part of the state.
- classmethod from_model(model, identity_key_format, hash_function, info)[source]
- Parameters:
model (
BaseStateModel) – The pydantic model holding the internal state of aBaseState, as produced bymodel.identity_key_format (
IdentityKeyFormat) – The format in which the identity public key is included in bundles/headers.hash_function (
HashFunction) – A 256 or 512-bit hash function.info (
bytes) – A (byte) string identifying the application.
- Return type:
TypeVar(BaseStateTypeT, bound= BaseState)- Returns:
A configured instance of
BaseState, with internal state restored from the model.
Warning
Migrations are not provided via the
model/from_model()API. Usejson/from_json()instead. Refer to Serialization and Migration in the documentation for details.
- classmethod from_json(serialized, identity_key_format, hash_function, info)[source]
- Parameters:
serialized (JSONObject) – A JSON-serializable Python object holding the internal state of a
BaseState, as produced byjson.identity_key_format (
IdentityKeyFormat) – The format in which the identity public key is included in bundles/headers.hash_function (
HashFunction) – A 256 or 512-bit hash function.info (
bytes) – A (byte) string identifying the application.
- Return type:
- Returns:
A configured instance of
BaseState, with internal state restored from the serialized data, and a flag that indicates whether the bundle needs to be published. The latter was part of the pre-stable serialization format.
- signed_pre_key_age()[source]
- Return type:
- Returns:
The age of the signed pre key, i.e. the time elapsed since it was last rotated, in seconds.
- rotate_signed_pre_key()[source]
Rotate the signed pre key. Keep the old signed pre key around for one additional rotation period, i.e. until this method is called again.
- Return type:
Returns: The currently hidden pre keys.
- hide_pre_key(pre_key_pub)[source]
Hide a pre key from the bundle returned by
bundleand pre key count returned byget_num_visible_pre_keys(), but keep the pre key for cryptographic operations. Hidden pre keys are not included in the serialized state as returned bymodelandjson.
Delete all pre keys that were previously hidden using
hide_pre_key().- Return type:
Perform an X3DH key agreement, actively.
- Parameters:
bundle (
Bundle) – The bundle of the passive party.associated_data_appendix (
bytes) – Additional information to append to the associated data, like usernames, certificates or other identifying information.require_pre_key (
bool) – Use this flag to abort the key agreement if the bundle does not contain a pre key.
- Return type:
- Returns:
The shared secret and associated data shared between both parties, and the header required by the other party to complete the passive part of the key agreement.
- Raises:
KeyAgreementException – If an error occurs during the key agreement. The exception message will contain (human-readable) details.
Perform an X3DH key agreement, passively.
- Parameters:
header (
Header) – The header received from the active party.associated_data_appendix (
bytes) – Additional information to append to the associated data, like usernames, certificates or other identifying information.require_pre_key (
bool) – Use this flag to abort the key agreement if the active party did not use a pre key.
- Return type:
- Returns:
The shared secret and the associated data shared between both parties, and the signed pre key pair that was used during the key exchange, for use by follow-up protocols.
- Raises:
KeyAgreementException – If an error occurs during the key agreement. The exception message will contain (human-readable) details.