The `spake2` library is a pure-Python implementation of the SPAKE2 password-authenticated key exchange (PAKE) algorithm. It enables two parties sharing a weak password to securely derive a strong shared secret over an insecure channel, preventing passive eavesdropping and limiting active attackers to a single password guess per protocol execution. The current stable version is 0.9, released in September 2024, with an infrequent release cadence.
pip install spake2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a basic SPAKE2 key exchange between two parties, Alice (role A) and Bob (role B), who share a weak password. Both parties initialize their respective SPAKE2 instances with the shared password and unique identity strings. They then exchange initial messages, process the received message, and derive a strong, shared secret key. The `idA` and `idB` strings are crucial for binding the key to specific parties and preventing replay/substitution attacks. The example uses `ParamsEd25519` for elliptic curve security.
Implement countermeasures at the application level to obscure timing differences, such as adding random delays or ensuring consistent execution paths regardless of input.
Ensure the operating system's cryptographic randomness facilities are robust and properly seeded. Consult system documentation or security guides for verifying `os.urandom()` strength.
Clearly define and enforce the roles (A and B) for the communicating parties. Alternatively, use the `SPAKE2_Symmetric` class if both sides need to operate identically without pre-assigned roles.
Always encode string passwords to bytes before passing them to `SPAKE2_A`, `SPAKE2_B`, or `SPAKE2_Symmetric`. For example, `password.encode('utf-8')`.Create new `SPAKE2_A`, `SPAKE2_B`, or `SPAKE2_Symmetric` instances for every new key exchange session. Do not reuse old messages or state objects.
Verify that both parties use the exact same password and identity strings. Confirm that one party is `SPAKE2_A` and the other is `SPAKE2_B`, or both are `SPAKE2_Symmetric`.
Convert the password string to bytes using `.encode()` before passing it to the SPAKE2 constructor, e.g., `SPAKE2_A(b'my_password', ...)` or `SPAKE2_A('my_password'.encode('utf-8'), ...)`.Ensure the import path is correct. Common parameter sets can be imported from `from spake2.parameters.all import ...` or directly from their specific submodules like `from spake2.parameters.i3072 import Params3072`.