verifyEphemeralKey function
Verifies that an ephemeral public key is valid.
An ephemeral public key (A for user, B for server) must not be zero modulo the safe prime (i.e., key % N ≠ 0). This prevents certain attacks where an attacker can force the session key to a known value.
Parameters:
publicKey
: The ephemeral public key to verify (A for user, B for server).safePrime
: The safe prime N used in the SRP exchange.keyName
: Descriptive name for error messages (e.g., 'A (user)' or 'B (server)').
Throws:
- InvalidParameterException if the key is invalid (key % N == 0), which may indicate an attack attempt.
Implementation
void verifyEphemeralKey(BigInt publicKey, BigInt safePrime, String keyName) {
if (publicKey % safePrime == BigInt.zero) {
throw InvalidParameterException(
'Ephemeral public key $keyName is invalid ($keyName % N == 0). '
'This may indicate an attack attempt.'
);
}
}