Module: kdf_chain

class doubleratchet.kdf_chain.KDFChain[source]

Bases: object

The term KDF chain is used when some of the output from a KDF is used as an output key and some is used to replace the KDF key, which can then be used with another input.

https://signal.org/docs/specifications/doubleratchet/#kdf-chains

classmethod create(kdf, key)[source]
Parameters
  • kdf (Type[KDF]) – The KDF to use for the derivation step.

  • key (bytes) – The initial chain key.

Return type

TypeVar(KDFChainTypeT, bound= KDFChain)

Returns

A configured instance of KDFChain.

property model: KDFChainModel

Returns: The internal state of this KDFChain as a pydantic model.

property json: JSONObject

Returns: The internal state of this KDFChain as a JSON-serializable Python object.

classmethod from_model(model, kdf)[source]
Parameters
  • model (KDFChainModel) – The pydantic model holding the internal state of a KDFChain, as produced by model.

  • kdf (Type[KDF]) – The KDF to use for the derivation step.

Return type

TypeVar(KDFChainTypeT, bound= KDFChain)

Returns

A configured instance of KDFChain, with internal state restored from the model.

Warning

Migrations are not provided via the model/from_model() API. Use json/from_json() instead. Refer to Serialization and Migration in the documentation for details.

classmethod from_json(serialized, kdf)[source]
Parameters
  • serialized (Mapping[str, Union[None, float, int, str, bool, List[Union[None, float, int, str, bool, List[Union[None, float, int, str, bool]], Mapping[str, Union[None, float, int, str, bool]]]], Mapping[str, Union[None, float, int, str, bool, List[Union[None, float, int, str, bool]], Mapping[str, Union[None, float, int, str, bool]]]]]]) – A JSON-serializable Python object holding the internal state of a KDFChain, as produced by json.

  • kdf (Type[KDF]) – The KDF to use for the derivation step.

Return type

TypeVar(KDFChainTypeT, bound= KDFChain)

Returns

A configured instance of KDFChain, with internal state restored from the serialized data.

async step(data, length)[source]

Perform a ratchet step of this KDF chain.

Parameters
  • data (bytes) – The input data.

  • length (int) – The desired size of the output data, in bytes.

Return type

bytes

Returns

length bytes of output data, derived from the internal KDF key and the input data.

property length: int

Returns: The length of this KDF chain, i.e. the number of steps that have been performed.