Socket
Socket
Sign inDemoInstall

tssrp6a

Package Overview
Dependencies
2
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.1 to 2.0.0

LICENSE

5

dist/index.d.ts

@@ -1,6 +0,5 @@

export { SRPConfig } from "./config";
export { SRPParameters } from "./parameters";
export { SRPRoutines } from "./routines";
export { SRPClientSession, ISRPClientCredentials } from "./session-client";
export { SRPClientSession } from "./session-client";
export { SRPServerSession } from "./session-server";
export { createVerifierAndSalt, IVerifierAndSalt, bigIntegerToWordArray, wordArrayToBigInteger, generateRandomBigInteger, } from "./utils";
export { createVerifierAndSalt, IVerifierAndSalt, bigIntegerToWordArray, wordArrayToBigInt, generateRandomBigInt, } from "./utils";

19

dist/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var config_1 = require("./config");
exports.SRPConfig = config_1.SRPConfig;
exports.generateRandomBigInt = exports.wordArrayToBigInt = exports.bigIntegerToWordArray = exports.createVerifierAndSalt = exports.SRPServerSession = exports.SRPClientSession = exports.SRPRoutines = exports.SRPParameters = void 0;
var parameters_1 = require("./parameters");
exports.SRPParameters = parameters_1.SRPParameters;
Object.defineProperty(exports, "SRPParameters", { enumerable: true, get: function () { return parameters_1.SRPParameters; } });
var routines_1 = require("./routines");
exports.SRPRoutines = routines_1.SRPRoutines;
Object.defineProperty(exports, "SRPRoutines", { enumerable: true, get: function () { return routines_1.SRPRoutines; } });
var session_client_1 = require("./session-client");
exports.SRPClientSession = session_client_1.SRPClientSession;
Object.defineProperty(exports, "SRPClientSession", { enumerable: true, get: function () { return session_client_1.SRPClientSession; } });
var session_server_1 = require("./session-server");
exports.SRPServerSession = session_server_1.SRPServerSession;
Object.defineProperty(exports, "SRPServerSession", { enumerable: true, get: function () { return session_server_1.SRPServerSession; } });
var utils_1 = require("./utils");
exports.createVerifierAndSalt = utils_1.createVerifierAndSalt;
exports.bigIntegerToWordArray = utils_1.bigIntegerToWordArray;
exports.wordArrayToBigInteger = utils_1.wordArrayToBigInteger;
exports.generateRandomBigInteger = utils_1.generateRandomBigInteger;
Object.defineProperty(exports, "createVerifierAndSalt", { enumerable: true, get: function () { return utils_1.createVerifierAndSalt; } });
Object.defineProperty(exports, "bigIntegerToWordArray", { enumerable: true, get: function () { return utils_1.bigIntegerToWordArray; } });
Object.defineProperty(exports, "wordArrayToBigInt", { enumerable: true, get: function () { return utils_1.wordArrayToBigInt; } });
Object.defineProperty(exports, "generateRandomBigInt", { enumerable: true, get: function () { return utils_1.generateRandomBigInt; } });
//# sourceMappingURL=index.js.map

@@ -1,13 +0,17 @@

import { BigInteger } from "jsbn";
declare type HashingAlgorithm = keyof typeof SRPParameters.H;
import * as CryptoJS from "crypto-js";
export declare type HashWordArray = CryptoJS.LibWordArray;
export declare type HashingAlgorithm = "SHA1" | "SHA224" | "SHA256" | "SHA384" | "SHA512" | "SHA3" | "RIPEMD160";
export declare type HashingFn = (subjects: HashWordArray[]) => HashWordArray;
export declare class SRPParameters {
readonly N: bigint;
readonly g: bigint;
static N: {
"256": BigInteger;
"512": BigInteger;
"768": BigInteger;
"1024": BigInteger;
"1536": BigInteger;
"2048": BigInteger;
"256": bigint;
"512": bigint;
"768": bigint;
"1024": bigint;
"1536": bigint;
"2048": bigint;
};
static g: BigInteger;
static g: bigint;
static H: {

@@ -22,14 +26,8 @@ SHA1: HashingAlgorithm;

};
private _N;
private _NBits;
private _g;
private _H;
private _HBits;
constructor(N?: BigInteger, g?: BigInteger, H?: HashingAlgorithm);
readonly N: BigInteger;
readonly NBits: number;
readonly g: BigInteger;
readonly H: any;
readonly H: HashingFn;
readonly HBits: number;
constructor(N?: bigint, g?: bigint, H?: HashingAlgorithm | HashingFn);
hash(...array: (bigint | string)[]): bigint;
hashPadded(...array: bigint[]): bigint;
}
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SRPParameters = void 0;
const CryptoJS = require("crypto-js");
const jsbn_1 = require("jsbn");
const utils_1 = require("./utils");
// tslint:disable:variable-name
class SRPParameters {
constructor(N = SRPParameters.N[2048], g = SRPParameters.g, H = SRPParameters.H.SHA512) {
this._N = N;
this._NBits = this._N.bitLength();
this._g = g;
const hasher = CryptoJS.algo[H];
if (!hasher || !(hasher instanceof CryptoJS.lib.Hasher.init)) {
throw new Error("Unknown hash function");
this.N = N;
this.g = g;
this.NBits = this.N.toString(2).length;
if (typeof H === "function") {
this.H = H;
}
this._H = hasher.create();
else {
const hasher = CryptoJS.algo[H];
if (!hasher || !(hasher instanceof CryptoJS.lib.Hasher.init)) {
throw new Error("Unknown hash function");
}
const hasherInstance = hasher.create();
this.H = (array) => {
hasherInstance.reset();
array.forEach((hwa) => hasherInstance.update(hwa));
return hasherInstance.finalize();
};
}
// Calculate size of hash output
this._HBits = utils_1.hashBitCount(this);
this.HBits = utils_1.hashBitCount(this.H);
}
get N() {
return this._N;
hash(...array) {
return utils_1.wordArrayToBigInt(this.H(array.map(element => {
if (typeof element === "bigint") {
return utils_1.bigIntegerToWordArray(element);
}
else if (typeof element === "string") {
return utils_1.stringToWordArray(element);
}
return element;
})));
}
get NBits() {
return this._NBits;
hashPadded(...array) {
const targetLength = Math.trunc((this.NBits + 7) / 8);
return utils_1.wordArrayToBigInt(this.H(utils_1.padHashWordArray(targetLength, array.map(utils_1.bigIntegerToWordArray))));
}
get g() {
return this._g;
}
get H() {
return this._H;
}
get HBits() {
return this._HBits;
}
}
exports.SRPParameters = SRPParameters;
// tslint:disable
SRPParameters.N = {
"256": new jsbn_1.BigInteger("125617018995153554710546479714086468244499594888726646874671447258204721048803"),
"512": new jsbn_1.BigInteger("11144252439149533417835749556168991736939157778924947037200268358613863350040339017097790259154750906072491181606044774215413467851989724116331597513345603"),
"768": new jsbn_1.BigInteger("1087179135105457859072065649059069760280540086975817629066444682366896187793570736574549981488868217843627094867924800342887096064844227836735667168319981288765377499806385489913341488724152562880918438701129530606139552645689583147"),
"1024": new jsbn_1.BigInteger("167609434410335061345139523764350090260135525329813904557420930309800865859473551531551523800013916573891864789934747039010546328480848979516637673776605610374669426214776197828492691384519453218253702788022233205683635831626913357154941914129985489522629902540768368409482248290641036967659389658897350067939"),
"1536": new jsbn_1.BigInteger("1486998185923128292816507353619409521152457662596380074614818966810244974827752411420380336514078832314731499938313197533147998565301020797040787428051479639316928015998415709101293902971072960487527411068082311763171549170528008620813391411445907584912865222076100726050255271567749213905330659264908657221124284665444825474741087704974475795505492821585749417639344967192301749033325359286273431675492866492416941152646940908101472416714421046022696100064262587"),
"2048": new jsbn_1.BigInteger("21766174458617435773191008891802753781907668374255538511144643224689886235383840957210909013086056401571399717235807266581649606472148410291413364152197364477180887395655483738115072677402235101762521901569820740293149529620419333266262073471054548368736039519702486226506248861060256971802984953561121442680157668000761429988222457090413873973970171927093992114751765168063614761119615476233422096442783117971236371647333871414335895773474667308967050807005509320424799678417036867928316761272274230314067548291133582479583061439577559347101961771406173684378522703483495337037655006751328447510550299250924469288819"),
"256": BigInt("125617018995153554710546479714086468244499594888726646874671447258204721048803"),
"512": BigInt("11144252439149533417835749556168991736939157778924947037200268358613863350040339017097790259154750906072491181606044774215413467851989724116331597513345603"),
"768": BigInt("1087179135105457859072065649059069760280540086975817629066444682366896187793570736574549981488868217843627094867924800342887096064844227836735667168319981288765377499806385489913341488724152562880918438701129530606139552645689583147"),
"1024": BigInt("167609434410335061345139523764350090260135525329813904557420930309800865859473551531551523800013916573891864789934747039010546328480848979516637673776605610374669426214776197828492691384519453218253702788022233205683635831626913357154941914129985489522629902540768368409482248290641036967659389658897350067939"),
"1536": BigInt("1486998185923128292816507353619409521152457662596380074614818966810244974827752411420380336514078832314731499938313197533147998565301020797040787428051479639316928015998415709101293902971072960487527411068082311763171549170528008620813391411445907584912865222076100726050255271567749213905330659264908657221124284665444825474741087704974475795505492821585749417639344967192301749033325359286273431675492866492416941152646940908101472416714421046022696100064262587"),
"2048": BigInt("21766174458617435773191008891802753781907668374255538511144643224689886235383840957210909013086056401571399717235807266581649606472148410291413364152197364477180887395655483738115072677402235101762521901569820740293149529620419333266262073471054548368736039519702486226506248861060256971802984953561121442680157668000761429988222457090413873973970171927093992114751765168063614761119615476233422096442783117971236371647333871414335895773474667308967050807005509320424799678417036867928316761272274230314067548291133582479583061439577559347101961771406173684378522703483495337037655006751328447510550299250924469288819"),
};
// tslint:enable
SRPParameters.g = new jsbn_1.BigInteger("2");
SRPParameters.g = BigInt("2");
SRPParameters.H = {

@@ -49,0 +56,0 @@ SHA1: "SHA1",

@@ -1,4 +0,2 @@

import { BigInteger } from "jsbn";
import { SRPParameters } from "./parameters";
import { HashWordArray } from "./utils";
/**

@@ -13,22 +11,17 @@ * Default routines used for SRP calculation.

export declare class SRPRoutines {
private _parameters;
readonly parameters: SRPParameters;
constructor(parameters: SRPParameters);
readonly parameters: SRPParameters;
hash(...as: HashWordArray[]): HashWordArray;
hashPadded(...as: HashWordArray[]): HashWordArray;
hashAsBigInteger(key: BigInteger): BigInteger;
computeK(): BigInteger;
generateRandomSalt(numBytes?: number): BigInteger;
computeX(I: string, s: BigInteger, P: string): BigInteger;
computeXStep2(s: BigInteger, identityHash: HashWordArray): BigInteger;
computeIdentityHash(_: string, P: string): HashWordArray;
computeVerifier(x: BigInteger): BigInteger;
generatePrivateValue(): BigInteger;
computeClientPublicValue(a: BigInteger): BigInteger;
isValidPublicValue(value: BigInteger): boolean;
computeU(A: BigInteger, B: BigInteger): BigInteger;
computeClientEvidence(_I: string, _s: BigInteger, A: BigInteger, B: BigInteger, S: BigInteger): BigInteger;
computeServerEvidence(A: BigInteger, M1: BigInteger, S: BigInteger): BigInteger;
computeClientSessionKey(k: BigInteger, x: BigInteger, u: BigInteger, a: BigInteger, B: BigInteger): BigInteger;
computeK(): bigint;
generateRandomSalt(numBytes?: number): bigint;
computeX(I: string, s: bigint, P: string): bigint;
computeXStep2(s: bigint, identityHash: bigint): bigint;
computeIdentityHash(_: string, P: string): bigint;
computeVerifier(x: bigint): bigint;
generatePrivateValue(): bigint;
computeClientPublicValue(a: bigint): bigint;
isValidPublicValue(value: bigint): boolean;
computeU(A: bigint, B: bigint): bigint;
computeClientEvidence(_I: string, _s: bigint, A: bigint, B: bigint, S: bigint): bigint;
computeServerEvidence(A: bigint, M1: bigint, S: bigint): bigint;
computeClientSessionKey(k: bigint, x: bigint, u: bigint, a: bigint, B: bigint): bigint;
}
export declare type SRPRoutinesFactory = (params: SRPParameters) => SRPRoutines;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SRPRoutines = void 0;
const bigint_mod_arith_1 = require("bigint-mod-arith");
const utils_1 = require("./utils");
// tslint:disable:max-line-length
/**

@@ -13,23 +14,8 @@ * Default routines used for SRP calculation.

*/
// tslint:enable:max-line-length
// tslint:disable:variable-name
class SRPRoutines {
constructor(parameters) {
this._parameters = parameters;
this.parameters = parameters;
}
get parameters() {
return this._parameters;
}
hash(...as) {
return utils_1.hash(this.parameters, ...as);
}
hashPadded(...as) {
const targetLength = Math.trunc((this.parameters.NBits + 7) / 8);
return utils_1.hashPadded(this.parameters, targetLength, ...as);
}
hashAsBigInteger(key) {
return utils_1.wordArrayToBigInteger(this.hash(utils_1.bigIntegerToWordArray(key)));
}
computeK() {
return utils_1.wordArrayToBigInteger(this.hashPadded(utils_1.bigIntegerToWordArray(this.parameters.N), utils_1.bigIntegerToWordArray(this.parameters.g)));
return (this.parameters.hashPadded(this.parameters.N, this.parameters.g));
}

@@ -40,15 +26,15 @@ generateRandomSalt(numBytes) {

const saltBytes = numBytes || (2 * this.parameters.HBits) / 8;
return utils_1.generateRandomBigInteger(saltBytes);
return utils_1.generateRandomBigInt(saltBytes);
}
computeX(I, s, P) {
return utils_1.wordArrayToBigInteger(this.hash(utils_1.bigIntegerToWordArray(s), this.computeIdentityHash(I, P)));
return this.parameters.hash(s, this.computeIdentityHash(I, P));
}
computeXStep2(s, identityHash) {
return utils_1.wordArrayToBigInteger(this.hash(utils_1.bigIntegerToWordArray(s), identityHash));
return this.parameters.hash(s, identityHash);
}
computeIdentityHash(_, P) {
return this.hash(utils_1.stringToWordArray(P));
return this.parameters.hash(P);
}
computeVerifier(x) {
return this.parameters.g.modPow(x, this.parameters.N);
return bigint_mod_arith_1.modPow(this.parameters.g, x, this.parameters.N);
}

@@ -59,25 +45,25 @@ generatePrivateValue() {

do {
bi = utils_1.generateRandomBigInteger(numBits / 8).mod(this.parameters.N);
} while (bi.signum() === 0);
bi = utils_1.generateRandomBigInt(numBits / 8) % this.parameters.N;
} while (bi === BigInt(0));
return bi;
}
computeClientPublicValue(a) {
return this.parameters.g.modPow(a, this.parameters.N);
return bigint_mod_arith_1.modPow(this.parameters.g, a, this.parameters.N);
}
isValidPublicValue(value) {
return value.mod(this.parameters.N).signum() !== 0;
return value % this.parameters.N !== BigInt(0);
}
computeU(A, B) {
return utils_1.wordArrayToBigInteger(this.hashPadded(utils_1.bigIntegerToWordArray(A), utils_1.bigIntegerToWordArray(B)));
return this.parameters.hashPadded(A, B);
}
computeClientEvidence(_I, _s, A, B, S) {
return utils_1.wordArrayToBigInteger(this.hash(utils_1.bigIntegerToWordArray(A), utils_1.bigIntegerToWordArray(B), utils_1.bigIntegerToWordArray(S)));
return (this.parameters.hash((A), (B), (S)));
}
computeServerEvidence(A, M1, S) {
return utils_1.wordArrayToBigInteger(this.hash(utils_1.bigIntegerToWordArray(A), utils_1.bigIntegerToWordArray(M1), utils_1.bigIntegerToWordArray(S)));
return (this.parameters.hash((A), (M1), (S)));
}
computeClientSessionKey(k, x, u, a, B) {
const exp = u.multiply(x).add(a);
const tmp = this.parameters.g.modPow(x, this.parameters.N).multiply(k);
return B.subtract(tmp).modPow(exp, this.parameters.N);
const exp = u * x + a;
const tmp = bigint_mod_arith_1.modPow(this.parameters.g, x, this.parameters.N) * k;
return bigint_mod_arith_1.modPow(B - tmp, exp, this.parameters.N);
}

@@ -84,0 +70,0 @@ }

@@ -1,46 +0,22 @@

import { BigInteger } from "jsbn";
import { SRPConfig } from "./config";
import { SRPSession } from "./session";
import { HashWordArray } from "./utils";
export declare enum SRPClientSessionState {
INIT = "INIT",
STEP_1 = "STEP_1",
STEP_2 = "STEP_2",
STEP_3 = "STEP_3"
import { SRPRoutines } from "./routines";
export declare class SRPClientSession {
private readonly routines;
constructor(routines: SRPRoutines);
step1(userId: string, userPassword: string): SRPClientSessionStep1;
}
export interface ISRPClientCredentials {
A: BigInteger;
M1: BigInteger;
declare class SRPClientSessionStep1 {
private readonly routines;
private readonly I;
readonly IH: bigint;
constructor(routines: SRPRoutines, I: string, IH: bigint);
step2(salt: bigint, B: bigint): SRPClientSessionStep2;
}
export declare class SRPClientSession extends SRPSession {
/**
* Current client auth state
*/
protected stateStep: SRPClientSessionState;
/**
* User identity
*/
private _I?;
/**
* User identity hash
*/
private _IH?;
/**
* Client public value "A"
*/
private _A?;
/**
* Client evidence message "M1"
*/
private _M1?;
constructor(config: SRPConfig, timeoutMillis?: number);
step1(userId: string, userPassword: string): void;
step2(salt: BigInteger, B: BigInteger): ISRPClientCredentials;
step3(M2: BigInteger): void;
readonly state: SRPClientSessionState;
I: string;
identityHash: HashWordArray;
A: BigInteger;
M1: BigInteger;
private _expectState;
declare class SRPClientSessionStep2 {
private readonly routines;
readonly A: bigint;
readonly M1: bigint;
readonly S: bigint;
constructor(routines: SRPRoutines, A: bigint, M1: bigint, S: bigint);
step3(M2: bigint): void;
}
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const session_1 = require("./session");
var SRPClientSessionState;
(function (SRPClientSessionState) {
SRPClientSessionState["INIT"] = "INIT";
SRPClientSessionState["STEP_1"] = "STEP_1";
SRPClientSessionState["STEP_2"] = "STEP_2";
SRPClientSessionState["STEP_3"] = "STEP_3";
})(SRPClientSessionState = exports.SRPClientSessionState || (exports.SRPClientSessionState = {}));
class SRPClientSession extends session_1.SRPSession {
constructor(config, timeoutMillis) {
super(config, timeoutMillis);
this.stateStep = SRPClientSessionState.INIT;
exports.SRPClientSession = void 0;
class SRPClientSession {
constructor(routines) {
this.routines = routines;
}
step1(userId, userPassword) {
this._expectState(SRPClientSessionState.INIT);
if (!userId || !userId.trim()) {

@@ -24,10 +15,14 @@ throw new Error("User identity must not be null nor empty");

}
this.I = userId;
this.identityHash = this.config.routines.computeIdentityHash(userId, userPassword);
this.stateStep = SRPClientSessionState.STEP_1;
this._registerActivity();
const IH = this.routines.computeIdentityHash(userId, userPassword);
return new SRPClientSessionStep1(this.routines, userId, IH);
}
}
exports.SRPClientSession = SRPClientSession;
class SRPClientSessionStep1 {
constructor(routines, I, IH) {
this.routines = routines;
this.I = I;
this.IH = IH;
}
step2(salt, B) {
this._expectState(SRPClientSessionState.STEP_1);
this._throwOnTimeout();
if (!salt) {

@@ -39,94 +34,29 @@ throw new Error("Salt (s) must not be null");

}
const routines = this.config.routines;
const x = routines.computeXStep2(salt, this.identityHash);
const a = routines.generatePrivateValue();
this.A = routines.computeClientPublicValue(a);
const k = routines.computeK();
const u = routines.computeU(this.A, B);
this.S = routines.computeClientSessionKey(k, x, u, a, B);
this.M1 = routines.computeClientEvidence(this.I, salt, this.A, B, this.S);
this.stateStep = SRPClientSessionState.STEP_2;
this._registerActivity();
return {
A: this.A,
M1: this.M1,
};
const x = this.routines.computeXStep2(salt, this.IH);
const a = this.routines.generatePrivateValue();
const A = this.routines.computeClientPublicValue(a);
const k = this.routines.computeK();
const u = this.routines.computeU(A, B);
const S = this.routines.computeClientSessionKey(k, x, u, a, B);
const M1 = this.routines.computeClientEvidence(this.I, salt, A, B, S);
return new SRPClientSessionStep2(this.routines, A, M1, S);
}
}
class SRPClientSessionStep2 {
constructor(routines, A, M1, S) {
this.routines = routines;
this.A = A;
this.M1 = M1;
this.S = S;
}
step3(M2) {
this._expectState(SRPClientSessionState.STEP_2);
this._throwOnTimeout();
if (!M2) {
throw new Error("Server evidence (M2) must not be null");
}
const computedM2 = this.config.routines.computeServerEvidence(this.A, this.M1, this.S);
if (!computedM2.equals(M2)) {
const computedM2 = this.routines.computeServerEvidence(this.A, this.M1, this.S);
if (computedM2 !== M2) {
throw new Error("Bad server credentials");
}
this.stateStep = SRPClientSessionState.STEP_3;
this._registerActivity();
}
get state() {
return this.stateStep;
}
get I() {
if (this._I) {
return this._I;
}
throw new Error("User Identity (I) not set");
}
set I(I) {
if (this._I) {
throw new Error(`User identity (I) already set: ${this._I}`);
}
this._I = I;
}
get identityHash() {
if (this._IH) {
return this._IH;
}
throw new Error("Identity hash is not set");
}
set identityHash(identityHash) {
if (identityHash.sigBytes << 3 !== this.config.parameters.HBits) {
throw new Error(`Hash array must have correct size in bits: ${this.config.parameters.HBits}`);
}
if (this._IH) {
throw new Error(`User identity hash (_IH) already set: ${this._IH}`);
}
this._IH = identityHash;
}
get A() {
if (this._A) {
return this._A;
}
throw new Error("Public client value (A) not set");
}
set A(A) {
if (this._A) {
throw new Error(`Public client value (A) already set: ${this._A.toString(16)}`);
}
if (!this.config.routines.isValidPublicValue(A)) {
throw new Error(`Bad client public value (A): ${A.toString(16)}`);
}
this._A = A;
}
get M1() {
if (this._M1) {
return this._M1;
}
throw new Error("Client evidence (M1) not set");
}
set M1(M1) {
if (this._M1) {
throw new Error(`Client evidence (M1) already set: ${this._M1.toString(16)}`);
}
this._M1 = M1;
}
_expectState(state) {
if (this.state !== state) {
throw new Error(`State violation: Session must be in ${state} state but is in ${this.state}`);
}
}
}
exports.SRPClientSession = SRPClientSession;
//# sourceMappingURL=session-client.js.map

@@ -1,9 +0,21 @@

import { BigInteger } from "jsbn";
import { SRPConfig } from "./config";
import { SRPRoutines } from "./routines";
export declare class SRPServerSession {
private config;
private state;
constructor(config: SRPConfig);
step1(identifier: string, salt: BigInteger, verifier: BigInteger): BigInteger;
step2(A: BigInteger, M1: BigInteger): BigInteger;
private readonly routines;
constructor(routines: SRPRoutines);
step1(identifier: string, salt: bigint, verifier: bigint): SRPServerSessionStep1;
}
declare class SRPServerSessionStep1 {
readonly routines: SRPRoutines;
private readonly identifier;
private readonly salt;
private readonly verifier;
private readonly b;
readonly B: bigint;
constructor(routines: SRPRoutines, identifier: string, salt: bigint, verifier: bigint, b: bigint, B: bigint);
/**
* Compute the session key "S" without computing or checking client evidence
*/
sessionKey(A: bigint): bigint;
step2(A: bigint, M1: bigint): bigint;
}
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SRPServerSession = void 0;
const bigint_mod_arith_1 = require("bigint-mod-arith");
class SRPServerSession {
constructor(config) {
this.state = { step: "init" };
this.config = config;
constructor(routines) {
this.routines = routines;
}
step1(identifier, salt, verifier) {
if (this.state.step !== "init") {
throw new Error("step1 not from init");
}
const b = this.config.routines.generatePrivateValue();
const k = this.config.routines.computeK();
const B = computeServerPublicValue(this.config.parameters, k, verifier, b);
this.state = {
step: "1",
identifier,
salt,
verifier,
b,
B,
};
return B;
const b = this.routines.generatePrivateValue();
const k = this.routines.computeK();
const B = computeServerPublicValue(this.routines.parameters, k, verifier, b);
return new SRPServerSessionStep1(this.routines, identifier, salt, verifier, b, B);
}
step2(A, M1) {
if (this.state.step !== "1") {
throw new Error("step2 not from step1");
}
if (!A) {
}
exports.SRPServerSession = SRPServerSession;
class SRPServerSessionStep1 {
constructor(routines, identifier, salt, verifier, b, B) {
this.routines = routines;
this.identifier = identifier;
this.salt = salt;
this.verifier = verifier;
this.b = b;
this.B = B;
}
/**
* Compute the session key "S" without computing or checking client evidence
*/
sessionKey(A) {
if (A === null) {
throw new Error("Client public value (A) must not be null");
}
if (!this.config.routines.isValidPublicValue(A)) {
if (!this.routines.isValidPublicValue(A)) {
throw new Error(`Invalid Client public value (A): ${A.toString(16)}`);
}
const { identifier, salt, verifier, b, B } = this.state;
const u = this.routines.computeU(A, this.B);
const S = computeServerSessionKey(this.routines.parameters.N, this.verifier, u, A, this.b);
return S;
}
step2(A, M1) {
if (!M1) {
throw new Error("Client evidence (M1) must not be null");
}
const u = this.config.routines.computeU(A, B);
const S = computeServerSessionKey(this.config.parameters.N, verifier, u, A, b);
const computedM1 = this.config.routines.computeClientEvidence(identifier, salt, A, B, S);
if (!computedM1.equals(M1)) {
const S = this.sessionKey(A);
const computedM1 = this.routines.computeClientEvidence(this.identifier, this.salt, A, this.B, S);
if (computedM1 !== M1) {
throw new Error("Bad client credentials");
}
const M2 = this.config.routines.computeServerEvidence(A, M1, S);
this.state = { step: "2" };
const M2 = this.routines.computeServerEvidence(A, M1, S);
return M2;
}
}
exports.SRPServerSession = SRPServerSession;
const computeServerPublicValue = (parameters, k, v, b) => {
return parameters.g
.modPow(b, parameters.N)
.add(v.multiply(k))
.mod(parameters.N);
return (bigint_mod_arith_1.modPow(parameters.g, b, parameters.N) + v * k) % parameters.N;
};
const computeServerSessionKey = (N, v, u, A, b) => {
return v
.modPow(u, N)
.multiply(A)
.modPow(b, N);
return bigint_mod_arith_1.modPow(bigint_mod_arith_1.modPow(v, u, N) * A, b, N);
};
//# sourceMappingURL=session-server.js.map

@@ -1,3 +0,2 @@

import { BigInteger } from "jsbn";
import { SRPConfig } from "./config";
import { SRPRoutines } from "./routines";
export declare class SRPSession {

@@ -7,3 +6,3 @@ /**

*/
private _config;
private _routines;
/**

@@ -26,9 +25,9 @@ * Shared session key "S"

private _timedOut;
constructor(config: SRPConfig, timeoutMillis?: number);
S: BigInteger;
readonly sharedKey: BigInteger;
readonly hashedSharedKey: BigInteger;
readonly config: SRPConfig;
constructor(routines: SRPRoutines, timeoutMillis?: number);
get S(): bigint;
set S(S: bigint);
get hashedSharedKey(): bigint;
get routines(): SRPRoutines;
protected _throwOnTimeout(): void;
protected _registerActivity(): void;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable:variable-name
exports.SRPSession = void 0;
class SRPSession {
constructor(config, timeoutMillis) {
this._config = config;
constructor(routines, timeoutMillis) {
this._routines = routines;
this._timeoutMillis = timeoutMillis;

@@ -22,10 +22,7 @@ this._timedOut = false;

}
get sharedKey() {
return this.S;
}
get hashedSharedKey() {
return this.config.routines.hashAsBigInteger(this.sharedKey);
return this.routines.hashAsBigInt(this.S);
}
get config() {
return this._config;
get routines() {
return this._routines;
}

@@ -32,0 +29,0 @@ _throwOnTimeout() {

@@ -1,8 +0,5 @@

import * as CryptoJS from "crypto-js";
import { BigInteger } from "jsbn";
import { SRPConfig } from "./config";
import { SRPParameters } from "./parameters";
export declare type HashWordArray = CryptoJS.LibWordArray;
export declare const bigIntegerToWordArray: (n: BigInteger) => CryptoJS.LibWordArray;
export declare const wordArrayToBigInteger: (words: CryptoJS.LibWordArray) => BigInteger;
import { HashingFn, HashWordArray } from "./parameters";
import { SRPRoutines } from "./routines";
export declare const bigIntegerToWordArray: (n: bigint) => HashWordArray;
export declare const wordArrayToBigInt: (words: HashWordArray) => bigint;
/**

@@ -19,5 +16,4 @@ * Convert some string into HashWordArray.

*/
export declare const padWordArray: (words: CryptoJS.LibWordArray, targetLength: number) => CryptoJS.LibWordArray;
export declare function hash(parameters: SRPParameters, ...arrays: HashWordArray[]): HashWordArray;
export declare function hashPadded(parameters: SRPParameters, targetLen: number, ...arrays: HashWordArray[]): HashWordArray;
export declare const padWordArray: (words: HashWordArray, targetLength: number) => HashWordArray;
export declare function padHashWordArray(targetLen: number, arrays: HashWordArray[]): HashWordArray[];
/**

@@ -29,10 +25,10 @@ * Generates random string of ASCII characters using crypto secure random generator.

export declare function generateRandomString(characterCount?: number): string;
export declare function generateRandomBigInteger(numBytes?: number): BigInteger;
export declare function createVerifier(config: SRPConfig, I: string, s: BigInteger, P: string): BigInteger;
export declare function generateRandomBigInt(numBytes?: number): bigint;
export declare function createVerifier(routines: SRPRoutines, I: string, s: bigint, P: string): bigint;
export interface IVerifierAndSalt {
v: BigInteger;
s: BigInteger;
v: bigint;
s: bigint;
}
export declare function createVerifierAndSalt(config: SRPConfig, I: string, P: string, sBytes?: number): IVerifierAndSalt;
export declare const hashBitCount: (parameters: SRPParameters) => number;
export declare function createVerifierAndSalt(routines: SRPRoutines, I: string, P: string, sBytes?: number): IVerifierAndSalt;
export declare const hashBitCount: (hasher: HashingFn) => number;
export declare function createHashWordArray(words: number[], sigBytes: number): HashWordArray;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createHashWordArray = exports.hashBitCount = exports.createVerifierAndSalt = exports.createVerifier = exports.generateRandomBigInt = exports.generateRandomString = exports.padHashWordArray = exports.padWordArray = exports.stringToWordArray = exports.wordArrayToBigInt = exports.bigIntegerToWordArray = void 0;
const CryptoJS = require("crypto-js");
const jsbn_1 = require("jsbn");
exports.bigIntegerToWordArray = (n) => CryptoJS.enc.Hex.parse(evenLengthHex(n.toString(16)));
exports.wordArrayToBigInteger = (words) => new jsbn_1.BigInteger(CryptoJS.enc.Hex.stringify(words), 16);
const bigIntegerToWordArray = (n) => CryptoJS.enc.Hex.parse(evenLengthHex(n.toString(16)));
exports.bigIntegerToWordArray = bigIntegerToWordArray;
const wordArrayToBigInt = (words) => BigInt(`0x${CryptoJS.enc.Hex.stringify(words)}`);
exports.wordArrayToBigInt = wordArrayToBigInt;
/**

@@ -21,3 +23,3 @@ * Convert some string into HashWordArray.

*/
exports.padWordArray = (words, targetLength) => {
const padWordArray = (words, targetLength) => {
let result = words;

@@ -33,13 +35,22 @@ if (targetLength > words.sigBytes) {

};
function hash(parameters, ...arrays) {
parameters.H.reset();
arrays.forEach((hwa) => parameters.H.update(hwa));
return parameters.H.finalize();
exports.padWordArray = padWordArray;
// export function hash(
// parameters: SRPParameters,
// ...arrays: HashWordArray[]
// ): HashWordArray {
// return parameters.H(arrays);
// }
//
// export function hashPadded(
// parameters: SRPParameters,
// targetLen: number,
// ...arrays: HashWordArray[]
// ): HashWordArray {
// const arraysPadded = arrays.map((hwa) => padWordArray(hwa, targetLen));
// return hash(parameters, ...arraysPadded);
// }
function padHashWordArray(targetLen, arrays) {
return arrays.map((hwa) => exports.padWordArray(hwa, targetLen));
}
exports.hash = hash;
function hashPadded(parameters, targetLen, ...arrays) {
const arraysPadded = arrays.map((hwa) => exports.padWordArray(hwa, targetLen));
return hash(parameters, ...arraysPadded);
}
exports.hashPadded = hashPadded;
exports.padHashWordArray = padHashWordArray;
/**

@@ -65,7 +76,7 @@ * Generates random string of ASCII characters using crypto secure random generator.

exports.generateRandomString = generateRandomString;
function generateRandomBigInteger(numBytes = 16) {
return exports.wordArrayToBigInteger(generateRandom(numBytes));
function generateRandomBigInt(numBytes = 16) {
return exports.wordArrayToBigInt(generateRandom(numBytes));
}
exports.generateRandomBigInteger = generateRandomBigInteger;
function createVerifier(config, I, s, P) {
exports.generateRandomBigInt = generateRandomBigInt;
function createVerifier(routines, I, s, P) {
if (!I || !I.trim()) {

@@ -80,3 +91,2 @@ throw new Error("Identity (I) must not be null or empty.");

}
const routines = config.routines;
const x = routines.computeX(I, s, P);

@@ -86,11 +96,12 @@ return routines.computeVerifier(x);

exports.createVerifier = createVerifier;
function createVerifierAndSalt(config, I, P, sBytes) {
const s = config.routines.generateRandomSalt(sBytes);
function createVerifierAndSalt(routines, I, P, sBytes) {
const s = routines.generateRandomSalt(sBytes);
return {
s,
v: createVerifier(config, I, s, P),
v: createVerifier(routines, I, s, P),
};
}
exports.createVerifierAndSalt = createVerifierAndSalt;
exports.hashBitCount = (parameters) => hash(parameters, exports.bigIntegerToWordArray(jsbn_1.BigInteger.ONE)).sigBytes << 3;
const hashBitCount = (hasher) => hasher([exports.bigIntegerToWordArray(BigInt(1))]).sigBytes << 3;
exports.hashBitCount = hashBitCount;
// TODO: remove when constructor is exported in @types/crypto-js

@@ -97,0 +108,0 @@ function createHashWordArray(words, sigBytes) {

{
"name": "tssrp6a",
"version": "1.1.1",
"version": "2.0.0",
"main": "dist/index.js",

@@ -12,19 +12,21 @@ "files": [

"dependencies": {
"@types/crypto-js": "^3.1.43",
"@types/jsbn": "^1.2.29",
"crypto-js": "^4.0.0",
"jsbn": "^1.1.0"
"bigint-mod-arith": "^3.0.0",
"crypto-js": "^4.0.0"
},
"devDependencies": {
"@types/node": "^11.13.8",
"husky": "^2.1.0",
"lint-staged": "^8.1.5",
"nyc": "^14.0.0",
"prettier": "^1.17.0",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/crypto-js": "3.1.43",
"@types/jsbn": "1.2.29",
"@types/node": "^15.0.3",
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"eslint": "^7.26.0",
"nyc": "^15.1.0",
"prettier": "^2.3.0",
"source-map-support": "^0.5.19",
"tap-diff": "^0.1.1",
"tap-junit": "^3.1.0",
"tape": "^4.10.1",
"ts-node": "^8.1.0",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
"tap-junit": "^4.2.0",
"tape": "^5.2.2",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
},

@@ -37,18 +39,7 @@ "scripts": {

"coverage": "yarn nyc yarn test:report",
"coverage:report": "yarn nyc --reporter cobertura --reporter html --report-dir reports/unit yarn test:report"
"coverage:report": "yarn nyc --reporter cobertura --reporter html --report-dir reports/unit yarn test:report",
"lint_formatter": "prettier -c 'src/**' 'test/**'",
"lint": "yarn lint_formatter && yarn eslint src test",
"format": "prettier --write 'src/**' 'test/**'"
},
"husky": {
"hooks": {
"pre-commit": "yarn lint-staged"
}
},
"lint-staged": {
"linters": {
"*.{ts,tsx}": [
"yarn prettier --write",
"yarn tslint -c tslint.json --fix",
"git add"
]
}
},
"description": "SRP6a client and server lib",

@@ -55,0 +46,0 @@ "repository": "git@github.com:midokura/platform-frontend.git",

@@ -5,2 +5,3 @@ # Midokura TSSRP6a

[![CI](https://github.com/midonet/tssrp6a/actions/workflows/main.yml/badge.svg)](https://github.com/midonet/tssrp6a/actions/workflows/main.yml)
[![badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/bgrosse-midokura/6d88c7cb89cc67292dc093e5d7bcede3/raw/tssrp6a-coverage-badge.json)](https://midonet.github.io/tssrp6a/)

@@ -19,2 +20,10 @@ This library is a TypeScript implementation of [Secure Remote Password](http://srp.stanford.edu/) SRP6a.

## Requirements & Dependencies
The `target` of TypeScript output is `es6`.
Needs [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) native support, or a polyfill which is not included.
The only dependencies are [crypto-js](https://www.npmjs.com/package/crypto-js) and [bigint-mod-arith](https://www.npmjs.com/package/bigint-mod-arith).
## Usage

@@ -30,13 +39,10 @@ ### Signup / registration

import {
createVerifierAndSalt, SRPConfig, SRPParameters, SRPRoutines,
createVerifierAndSalt, SRPParameters, SRPRoutines,
} from "tssrp6a"
const srp6aNimbusConfig = new SRPConfig(
new SRPParameters(),
(p) => new SRPRoutines(p),
);
const srp6aNimbusRoutines = new SRPRoutines(new SRPParameters());
const userId = "hello@world.org";
const userPassword = "password";
const { s: salt, v: verifier } = createVerifierAndSalt(
srp6aNimbusConfig,
srp6aNimbusRoutines,
userId,

@@ -67,10 +73,7 @@ userPassword,

import {
createVerifierAndSalt, SRPClientSession, SRPConfig,
SRPParameters, SRPRoutines, SRPServerSession
createVerifierAndSalt, SRPClientSession, SRPParameters, SRPRoutines,
SRPServerSession
} from "tssrp6a"
const srp6aNimbusConfig = new SRPConfig(
new SRPParameters(),
(p) => new SRPRoutines(p),
);
const srp6aNimbusRoutines = new SRPRoutines(new SRPParameters());

@@ -82,3 +85,3 @@ const username = "hello@world.org";

const { s: salt, v: verifier } = createVerifierAndSalt(
srp6aNimbusConfig,
srp6aNimbusRoutines,
username,

@@ -89,3 +92,3 @@ password,

// Sign in
const srp6aNimbusClient = new SRPClientSession(srp6aNimbusConfig);
const srp6aNimbusClient = new SRPClientSession(srp6aNimbusRoutines);
srp6aNimbusClient.step1(username, password);

@@ -95,3 +98,3 @@ // erase password at this point, it is no longer stored

const server = new SRPServerSession(srp6aNimbusConfig);
const server = new SRPServerSession(srp6aNimbusRoutines);
// server gets identifier from client, salt+verifier from db (from signup)

@@ -98,0 +101,0 @@ const B = server.step1(username, salt, verifier);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc