Skip to content

Commit

Permalink
refactor: move multipler derivation to the SRP parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
zolbooo committed Jun 2, 2024
1 parent b8d35d9 commit 6436c36
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 28 deletions.
14 changes: 1 addition & 13 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import * as constants from "./constants";

import { SRPParameterSet } from "./constants";
import {
byteArrayToHexString,
concatByteArrays,
safeByteArrayEquals,
} from "./utils";
import { concatByteArrays, safeByteArrayEquals } from "./utils";

import { DeriveMultiplierFn } from "./srp/multiplier";
import { DigestFn, deriveVerifier } from "./srp/verifier";
import {
deriveSessionKey,
Expand All @@ -21,25 +16,19 @@ export class ClientSession {
private parameters: SRPParameterSet = constants.SRP_PARAMETERS_RFC5054_2048;

private digest?: DigestFn;
private deriveMultiplier?: DeriveMultiplierFn;
constructor({
parameters,
digest,
deriveMultiplier,
}: {
parameters?: SRPParameterSet;
digest?: DigestFn;
deriveMultiplier?: DeriveMultiplierFn;
} = {}) {
if (parameters) {
this.parameters = parameters;
}
if (digest) {
this.digest = digest;
}
if (deriveMultiplier) {
this.deriveMultiplier = deriveMultiplier;
}
}

private clientPrivateEphemeral?: Uint8Array;
Expand Down Expand Up @@ -82,7 +71,6 @@ export class ClientSession {
serverPublicEphemeral: serverPublicEphemeral,
parameters: this.parameters,
digest: this.digest,
deriveMultiplier: this.deriveMultiplier,
});
this.sessionKey = sessionKey;
const clientProof = await deriveClientProof({
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { DeriveMultiplierFn } from "./srp/multiplier";

import { hexStringToByteArray } from "./utils";

export type SRPHashAlgorithm = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";
export interface SRPParameterSet {
N: Uint8Array;
G: Uint8Array;
algorithm: SRPHashAlgorithm;
deriveMultiplier?: DeriveMultiplierFn;
}

export const SRP_PARAMETERS_RFC5054_2048: SRPParameterSet = {
Expand Down
8 changes: 0 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,18 @@ import {
generateServerEphemeral,
} from "./srp/server";
import { deriveClientProof } from "./srp/client";
import { DeriveMultiplierFn } from "./srp/multiplier";

export class ServerSession {
private parameters: SRPParameterSet = constants.SRP_PARAMETERS_RFC5054_2048;

private deriveMultiplier?: DeriveMultiplierFn;
constructor({
parameters,
deriveMultiplier,
}: {
parameters?: SRPParameterSet;
deriveMultiplier?: DeriveMultiplierFn;
} = {}) {
if (parameters) {
this.parameters = parameters;
}
if (deriveMultiplier) {
this.deriveMultiplier = deriveMultiplier;
}
}

private clientVerifier?: Uint8Array;
Expand All @@ -45,7 +38,6 @@ export class ServerSession {
await generateServerEphemeral({
verifier,
parameters: this.parameters,
deriveMultiplier: this.deriveMultiplier,
});
this.clientVerifier = verifier;
this.clientPublicEphemeral = clientPublicEphemeral;
Expand Down
6 changes: 3 additions & 3 deletions src/srp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
} from "../utils";

import { deriveSharedHash } from "./common";
import { deriveMultiplierSRP6a } from "./multiplier";
import { DigestFn, deriveVerifier, digestPBKDF2 } from "./verifier";
import { DeriveMultiplierFn, deriveMultiplierSRP6a } from "./multiplier";

export function generateClientEphemeral(
parameters: SRPParameterSet = defaultParameters
Expand Down Expand Up @@ -57,7 +57,6 @@ export async function deriveSessionKey({
salt,
clientPrivateEphemeral,
serverPublicEphemeral,
deriveMultiplier = deriveMultiplierSRP6a,
digest = digestPBKDF2,
parameters = defaultParameters,
unsafe_skipOutputHashing = false,
Expand All @@ -68,7 +67,6 @@ export async function deriveSessionKey({
salt: Uint8Array;
clientPrivateEphemeral: Uint8Array;
serverPublicEphemeral: Uint8Array;
deriveMultiplier?: DeriveMultiplierFn;
digest?: DigestFn;
parameters?: SRPParameterSet;
unsafe_skipOutputHashing?: boolean;
Expand All @@ -82,7 +80,9 @@ export async function deriveSessionKey({
parameters,
})
);
const { deriveMultiplier = deriveMultiplierSRP6a } = parameters;
const k = byteArrayToBigInt(await deriveMultiplier(parameters));

const N = byteArrayToBigInt(parameters.N);
const { x: xBytes, verifier } = await deriveVerifier(
{ username, password },
Expand Down
2 changes: 1 addition & 1 deletion src/srp/multiplier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SRPParameterSet } from "../constants";
import { concatByteArrays, padData } from "../utils";

export type DeriveMultiplierFn = (
parameters: SRPParameterSet
parameters: Pick<SRPParameterSet, "N" | "G" | "algorithm">
) => Uint8Array | Promise<Uint8Array>;

export const deriveMultiplierSRP6a: DeriveMultiplierFn = async (parameters) =>
Expand Down
5 changes: 2 additions & 3 deletions src/srp/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ import {
} from "../utils";

import { deriveSharedHash } from "./common";
import { DeriveMultiplierFn, deriveMultiplierSRP6a } from "./multiplier";
import { deriveMultiplierSRP6a } from "./multiplier";

export async function generateServerEphemeral({
verifier,
deriveMultiplier = deriveMultiplierSRP6a,
parameters = defaultParameters,
unsafe_staticPrivateEphemeral,
}: {
verifier: Uint8Array;
deriveMultiplier?: DeriveMultiplierFn;
parameters?: SRPParameterSet;
unsafe_staticPrivateEphemeral?: Uint8Array;
}): Promise<{
serverPrivateEphemeral: Uint8Array;
serverPublicEphemeral: Uint8Array;
}> {
const { deriveMultiplier = deriveMultiplierSRP6a } = parameters;
const multiplier = BigInt(
"0x" + byteArrayToHexString(await deriveMultiplier(parameters))
);
Expand Down

0 comments on commit 6436c36

Please sign in to comment.