Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 1x 1x 32x 32x 32x 32x 32x 1x 33x 33x 33x 33x 33x 33x 30x 2x 28x 1x 27x 27x 27x 32x 2x 30x 27x 27x 2x 25x 25x 1x 1x 1x 32x 1x 27x | import { SRPParameters } from "./parameters"; import { SRPRoutines } from "./routines"; import { modPow } from "./utils"; // Variable names match the RFC (I, IH, S, b, B, salt, b, A, M1, M2...) export class SRPServerSession { constructor(private readonly routines: SRPRoutines) {} public async step1( /** * User identity */ identifier: string, /** * User salt */ salt: bigint, /** * User verifier */ verifier: bigint, ) { const b = this.routines.generatePrivateValue(); const k = await this.routines.computeK(); const B = computeServerPublicValue( this.routines.parameters, k, verifier, b, ); return new SRPServerSessionStep1( this.routines, identifier, salt, verifier, b, B, ); } } export type SRPServerSessionStep1State = { identifier: string; salt: string; // hex representation of bigint verifier: string; b: string; B: string; }; export class SRPServerSessionStep1 { constructor( public readonly routines: SRPRoutines, /** * User identity */ private readonly identifier: string, /** * User salt */ private readonly salt: bigint, /** * User verifier */ private readonly verifier: bigint, /** * Server private key "b" */ private readonly b: bigint, /** * Serve public key "B" */ public readonly B: bigint, ) {} /** * Compute the session key "S" without computing or checking client evidence */ public async sessionKey( /** * Client public key "A" */ A: bigint, ): Promise<bigint> { if (A === null) { throw new Error("Client public value (A) must not be null"); } if (!this.routines.isValidPublicValue(A)) { throw new Error(`Invalid Client public value (A): ${A.toString(16)}`); } const u = await this.routines.computeU(A, this.B); const S = computeServerSessionKey( this.routines.parameters.primeGroup.N, this.verifier, u, A, this.b, ); return S; } public async step2( /** * Client public key "A" */ A: bigint, /** * Client message "M1" */ M1: bigint, ) { if (!M1) { throw new Error("Client evidence (M1) must not be null"); } const S = await this.sessionKey(A); const computedM1 = await this.routines.computeClientEvidence( this.identifier, this.salt, A, this.B, S, ); if (computedM1 !== M1) { throw new Error("Bad client credentials"); } const M2 = this.routines.computeServerEvidence(A, M1, S); return M2; } public toJSON(): SRPServerSessionStep1State { return { identifier: this.identifier, salt: this.salt.toString(16), verifier: this.verifier.toString(16), b: this.b.toString(16), B: this.B.toString(16), }; } public static fromState( routines: SRPRoutines, state: SRPServerSessionStep1State, ) { return new SRPServerSessionStep1( routines, state.identifier, BigInt("0x" + state.salt), BigInt("0x" + state.verifier), BigInt("0x" + state.b), BigInt("0x" + state.B), ); } } const computeServerPublicValue = ( parameters: SRPParameters, k: bigint, v: bigint, b: bigint, ): bigint => { return ( (modPow(parameters.primeGroup.g, b, parameters.primeGroup.N) + v * k) % parameters.primeGroup.N ); }; const computeServerSessionKey = ( N: bigint, v: bigint, u: bigint, A: bigint, b: bigint, ): bigint => { return modPow(modPow(v, u, N) * A, b, N); }; |