first commit
This commit is contained in:
74
docs/security-stack/security/srp/src/server/server-finish.md
Normal file
74
docs/security-stack/security/srp/src/server/server-finish.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# `server-finish`
|
||||
|
||||
## Purpose
|
||||
|
||||
Complete the SRP-6a handshake on the server side after receiving the client's public value `A` and proof `M1`. Validates `A mod N != 0` (RFC 5054 §2.5.4 abort condition), derives the server-side shared secret `S = (A * v^u)^b mod N`, hashes it into the session key `K = H(S)`, recomputes the expected `M1`, and constant-time-compares against the client's `M1`. On match emits `M2 = H(A | M1 | K)` per RFC 2945 §3. On mismatch throws `SrpError(SRP.AUTH_FAILED)`.
|
||||
|
||||
## Features
|
||||
|
||||
- `A mod N == 0` gate per RFC 5054 §2.5.4.
|
||||
- `u ≡ 0 mod N` gate inherited from `computeU`.
|
||||
- Constant-time `M1` comparison via `@suaveplan/crypto-utils.constantTimeEqual`.
|
||||
- Returns `K`, `M2`, and `S` so consumers can chain into a KDF on `S` directly.
|
||||
- Emits `srp.handshake_duration_ms{role=server,phase=finish}` plus the appropriate outcome counter (`ok` / `auth_failed` / `public_key_invalid`).
|
||||
|
||||
## Usage: Basic Example
|
||||
|
||||
```ts
|
||||
import { serverFinishHandshake } from "@suaveplan/srp/server";
|
||||
import { params2048Sha256 } from "@suaveplan/srp/params";
|
||||
import { nobleHashBridge } from "@suaveplan/srp/hash";
|
||||
|
||||
const params = params2048Sha256(nobleHashBridge("SHA256"));
|
||||
const salt = new Uint8Array(16);
|
||||
const A = new Uint8Array(256);
|
||||
A[255] = 1;
|
||||
const B = new Uint8Array(256);
|
||||
B[255] = 2;
|
||||
const b = new Uint8Array(32);
|
||||
b[31] = 1;
|
||||
const v = new Uint8Array(256);
|
||||
v[255] = 1;
|
||||
const M1Client = new Uint8Array(32); // server expects to recompute this and compare
|
||||
try {
|
||||
serverFinishHandshake("alice", salt, A, B, b, v, M1Client, params);
|
||||
} catch (err) {
|
||||
console.log("auth failed:", (err as Error).message);
|
||||
}
|
||||
```
|
||||
|
||||
## Usage: Advanced Example
|
||||
|
||||
```ts
|
||||
import { serverFinishHandshake, serverStartHandshake } from "@suaveplan/srp/server";
|
||||
import { clientFinishHandshake, clientStartHandshake } from "@suaveplan/srp/client";
|
||||
import { computeVerifier } from "@suaveplan/srp/verifier";
|
||||
import { params2048Sha256 } from "@suaveplan/srp/params";
|
||||
import { nobleHashBridge } from "@suaveplan/srp/hash";
|
||||
|
||||
const params = params2048Sha256(nobleHashBridge("SHA256"));
|
||||
const salt = new Uint8Array(16);
|
||||
const rng = {
|
||||
nextUint32: () => 0,
|
||||
nextFloat: () => 0,
|
||||
nextBytes: (n: number) => crypto.getRandomValues(new Uint8Array(n)),
|
||||
};
|
||||
const { v } = computeVerifier("alice", "password123", salt, params);
|
||||
const { a, A } = clientStartHandshake(rng, params);
|
||||
const { b, B } = serverStartHandshake(v, rng, params);
|
||||
const client = clientFinishHandshake("alice", "password123", salt, A, B, a, params);
|
||||
const server = serverFinishHandshake("alice", salt, A, B, b, v, client.M1, params);
|
||||
console.log(Buffer.from(client.K).equals(Buffer.from(server.K))); // true
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### `serverFinishHandshake(I, salt, A, B, b, v, M1Client, params): { K, M2, S }`
|
||||
|
||||
Throws `SrpError(SRP.PUBLIC_KEY_INVALID)` when `A mod N == 0` or `u ≡ 0 mod N`. Throws `SrpError(SRP.AUTH_FAILED)` when the recomputed `M1` does not constant-time-match `M1Client`. On success returns the session key `K`, the server proof `M2`, and the raw shared secret `S`.
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
The order `validate A mod N → compute u → compute S → compute K → compute expectedM1 → compare M1Client` is intentional: the cheap modular check (`A mod N == 0`) happens first so an adversary cannot force the server to spend a `modPow` on garbage; `computeU` runs next because it has its own abort gate (`u ≡ 0`); only then do we pay the full `modPow` chain. This ordering minimises wasted server CPU on adversarial inputs without leaking any new timing channel (every legitimate handshake takes the same path).
|
||||
|
||||
The constant-time `M1` compare is the last gate before emitting `M2`. We use `expectedM1` (the server's locally-computed proof) inside `computeM2` rather than `M1Client` — even though they are byte-identical when the compare succeeds — because reusing the server-side computation eliminates a class of subtle bugs where a malicious client substitutes a near-miss `M1` that happens to match by the time the compare runs.
|
||||
65
docs/security-stack/security/srp/src/server/server-m2.md
Normal file
65
docs/security-stack/security/srp/src/server/server-m2.md
Normal file
@@ -0,0 +1,65 @@
|
||||
# `server-m2`
|
||||
|
||||
## Purpose
|
||||
|
||||
Split-roundtrip helper that computes the server proof `M2 = H(A | M1 | K)` independently of `serverFinishHandshake`. Useful for transports that ack `M1` over one HTTP request (server validates client proof, persists the session) and emit `M2` from a different worker or route (perhaps a follow-up acknowledgement endpoint, or a fan-out broadcast confirming successful authentication). The second-half route does not need to redo the `modPow` + KDF chain; it just needs `(A, M1, K)` (which it can fetch from a session store) and this helper.
|
||||
|
||||
## Features
|
||||
|
||||
- Single function call returning `H(A | M1 | K)`.
|
||||
- Byte-identical to the `M2` field returned by `serverFinishHandshake` for the same `(A, M1, K)` triple.
|
||||
- Deterministic — same inputs always produce the same bytes.
|
||||
- Wraps the shared `computeM2` helper in `math/proof.ts` so the proof formula lives in exactly one place.
|
||||
|
||||
## Usage: Basic Example
|
||||
|
||||
```ts
|
||||
import { serverComputeM2 } from "@suaveplan/srp/server";
|
||||
import { params2048Sha256 } from "@suaveplan/srp/params";
|
||||
import { nobleHashBridge } from "@suaveplan/srp/hash";
|
||||
|
||||
const params = params2048Sha256(nobleHashBridge("SHA256"));
|
||||
const A = new Uint8Array(256);
|
||||
const M1 = new Uint8Array(32);
|
||||
const K = new Uint8Array(32);
|
||||
const M2 = serverComputeM2(A, M1, K, params);
|
||||
console.log(M2.byteLength); // 32
|
||||
```
|
||||
|
||||
## Usage: Advanced Example
|
||||
|
||||
```ts
|
||||
import { serverComputeM2, serverFinishHandshake } from "@suaveplan/srp/server";
|
||||
import { clientFinishHandshake, clientStartHandshake } from "@suaveplan/srp/client";
|
||||
import { serverStartHandshake } from "@suaveplan/srp/server";
|
||||
import { computeVerifier } from "@suaveplan/srp/verifier";
|
||||
import { params2048Sha256 } from "@suaveplan/srp/params";
|
||||
import { nobleHashBridge } from "@suaveplan/srp/hash";
|
||||
|
||||
const params = params2048Sha256(nobleHashBridge("SHA256"));
|
||||
const salt = new Uint8Array(16);
|
||||
const rng = {
|
||||
nextUint32: () => 0,
|
||||
nextFloat: () => 0,
|
||||
nextBytes: (n: number) => crypto.getRandomValues(new Uint8Array(n)),
|
||||
};
|
||||
const { v } = computeVerifier("alice", "password123", salt, params);
|
||||
const { a, A } = clientStartHandshake(rng, params);
|
||||
const { b, B } = serverStartHandshake(v, rng, params);
|
||||
const client = clientFinishHandshake("alice", "password123", salt, A, B, a, params);
|
||||
const inline = serverFinishHandshake("alice", salt, A, B, b, v, client.M1, params);
|
||||
const split = serverComputeM2(A, client.M1, inline.K, params);
|
||||
console.log(Buffer.from(inline.M2).equals(Buffer.from(split)));
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### `serverComputeM2(A, M1, K, params): Uint8Array`
|
||||
|
||||
Returns `H(A | M1 | K)` of length `params.hash.digestSize`.
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
The split-roundtrip pattern matters operationally: many real deployments separate the "validate client proof" step (which needs the full SRP state machine — `v`, `b`, `B`) from the "ack mutual authentication" step (which only needs `A`, `M1`, and the session key `K`). The first step can run on a stateful auth node with the verifier DB; the second can run on any edge that has been handed the session-key blob. `serverComputeM2` is the API surface that supports that split without forcing the second step to re-derive `S` and `K`.
|
||||
|
||||
The function is intentionally trivial — a single forward to `computeM2`. The wrapper exists for API stability: if RFC 2945 ever extends `M2` (e.g. adds a session-binding field), this entry point is where the change lands, and `serverFinishHandshake` is updated to call into the same helper.
|
||||
74
docs/security-stack/security/srp/src/server/server-start.md
Normal file
74
docs/security-stack/security/srp/src/server/server-start.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# `server-start`
|
||||
|
||||
## Purpose
|
||||
|
||||
Begin the SRP-6a handshake on the server side. Loads the stored verifier `v` (alongside the user's `(I, salt)`), draws a `≥ 256`-bit private exponent `b`, and emits the server public value `B = (k * v + g^b) mod N` per RFC 5054 §2.5.4. The augmented form (multiplying `k` into the verifier) is what distinguishes SRP-6a from SRP-6. The function re-draws `b` if the resulting `B mod N == 0`, up to 16 attempts.
|
||||
|
||||
## Features
|
||||
|
||||
- 256-bit private exponent — matches RFC 5054 §2.5.4 minimum.
|
||||
- `k = H(N | PAD(g))` precomputed once per call (could be memoized externally for further savings).
|
||||
- `B ≡ 0 mod N` re-draw with 16-attempt safety cap.
|
||||
- Returns `B` zero-padded to `byteLength(N)`.
|
||||
- Emits `srp.handshake_duration_ms{role=server,phase=start}` and on abort `srp.outcome_total{role=server,outcome=public_key_invalid}` + `srp.errors_total{code=SRP.PUBLIC_KEY_INVALID}`.
|
||||
|
||||
## Usage: Basic Example
|
||||
|
||||
```ts
|
||||
import { serverStartHandshake } from "@suaveplan/srp/server";
|
||||
import { params2048Sha256 } from "@suaveplan/srp/params";
|
||||
import { nobleHashBridge } from "@suaveplan/srp/hash";
|
||||
import { computeVerifier } from "@suaveplan/srp/verifier";
|
||||
|
||||
const params = params2048Sha256(nobleHashBridge("SHA256"));
|
||||
const salt = new Uint8Array(16);
|
||||
const { v } = computeVerifier("alice", "password123", salt, params);
|
||||
const rng = {
|
||||
nextUint32: () => 0,
|
||||
nextFloat: () => 0,
|
||||
nextBytes: (n: number) => crypto.getRandomValues(new Uint8Array(n)),
|
||||
};
|
||||
const { b, B } = serverStartHandshake(v, rng, params);
|
||||
console.log(b.byteLength, B.byteLength); // 32 256
|
||||
```
|
||||
|
||||
## Usage: Advanced Example
|
||||
|
||||
```ts
|
||||
import { serverStartHandshake } from "@suaveplan/srp/server";
|
||||
import { params2048Sha256 } from "@suaveplan/srp/params";
|
||||
import { nobleHashBridge } from "@suaveplan/srp/hash";
|
||||
import { computeVerifier } from "@suaveplan/srp/verifier";
|
||||
import type { Rng } from "@suaveplan/types";
|
||||
|
||||
const params = params2048Sha256(nobleHashBridge("SHA256"));
|
||||
const salt = new Uint8Array(16);
|
||||
const { v } = computeVerifier("alice", "password123", salt, params);
|
||||
|
||||
// Synthetic deterministic RNG to demonstrate reproducibility.
|
||||
function seededRng(seed: number): Rng {
|
||||
let state = seed >>> 0;
|
||||
return {
|
||||
nextUint32: () => {
|
||||
state = (state * 1664525 + 1013904223) >>> 0;
|
||||
return state;
|
||||
},
|
||||
nextFloat: () => 0.5,
|
||||
};
|
||||
}
|
||||
const r1 = serverStartHandshake(v, seededRng(42), params);
|
||||
const r2 = serverStartHandshake(v, seededRng(42), params);
|
||||
console.log(Buffer.from(r1.B).equals(Buffer.from(r2.B))); // true
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### `serverStartHandshake(v, rng, params): { b, B }`
|
||||
|
||||
Returns `{ b, B }`. `b` is 32 bytes of fresh entropy from `rng`; `B` is `params.N.byteLength` bytes, zero-padded.
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
`kvModN = (k * v) mod N` is computed once at the top of the function and reused inside the re-draw loop because it does not depend on `b`. The only loop-variant work is `g^b mod N` (the `modPow` call) and the final `((kvModN + gb) mod N)`. This matters because the dominant cost is `modPow`; pulling `k*v` out of the loop saves wall time on the (vanishingly unlikely) re-draw path.
|
||||
|
||||
The re-draw cap is the same `16` as the client side. Per RFC 5054 §2.5.4 the requirement is "MUST abort if `B ≡ 0 mod N`"; the cap converts that into a finite-bound failure rather than an unbounded loop on a pathological RNG.
|
||||
Reference in New Issue
Block a user