-
Notifications
You must be signed in to change notification settings - Fork 9
/
routines.ts
153 lines (133 loc) · 3.87 KB
/
routines.ts
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
import { SRPParameters } from "./parameters";
import {
bigIntToArrayBuffer,
generateRandomBigInt,
hash,
hashPadded,
stringToArrayBuffer,
arrayBufferToBigInt,
hashBitCount,
modPow,
} from "./utils";
/**
* Default routines used for SRP calculation.
*
* These routines were implemented based on the Java Nimbus-SRP implementation.
* This project can be found at https://bitbucket.org/connect2id/nimbus-srp
* and the reference routine implementation at:
* https://bitbucket.org/connect2id/nimbus-srp/src/c88fec8a6dcd46dacf1e031b52f9bffca902acf4/src/main/java/com/nimbusds/srp6/SRP6Routines.java
*/
export class SRPRoutines {
constructor(public readonly parameters: SRPParameters) {}
public hash(...as: ArrayBuffer[]): Promise<ArrayBuffer> {
return hash(this.parameters, ...as);
}
public hashPadded(...as: ArrayBuffer[]): Promise<ArrayBuffer> {
const targetLength = Math.trunc((this.parameters.NBits + 7) / 8);
return hashPadded(this.parameters, targetLength, ...as);
}
public async computeK(): Promise<bigint> {
return arrayBufferToBigInt(
await this.hashPadded(
bigIntToArrayBuffer(this.parameters.primeGroup.N),
bigIntToArrayBuffer(this.parameters.primeGroup.g),
),
);
}
public async generateRandomSalt(numBytes?: number): Promise<bigint> {
const HBits = await hashBitCount(this.parameters);
// Recommended salt bytes is > than Hash output bytes. We default to twice
// the bytes used by the hash
const saltBytes = numBytes || (2 * HBits) / 8;
return generateRandomBigInt(saltBytes);
}
public async computeX(I: string, s: bigint, P: string): Promise<bigint> {
return arrayBufferToBigInt(
await this.hash(
bigIntToArrayBuffer(s),
await this.computeIdentityHash(I, P),
),
);
}
public async computeXStep2(
s: bigint,
identityHash: ArrayBuffer,
): Promise<bigint> {
return arrayBufferToBigInt(
await this.hash(bigIntToArrayBuffer(s), identityHash),
);
}
public async computeIdentityHash(_: string, P: string): Promise<ArrayBuffer> {
return await this.hash(stringToArrayBuffer(P));
}
public computeVerifier(x: bigint): bigint {
return modPow(
this.parameters.primeGroup.g,
x,
this.parameters.primeGroup.N,
);
}
public generatePrivateValue(): bigint {
const numBits = Math.max(256, this.parameters.NBits);
let bi: bigint;
do {
bi = generateRandomBigInt(numBits / 8) % this.parameters.primeGroup.N;
} while (bi === BigInt(0));
return bi;
}
public computeClientPublicValue(a: bigint): bigint {
return modPow(
this.parameters.primeGroup.g,
a,
this.parameters.primeGroup.N,
);
}
public isValidPublicValue(value: bigint): boolean {
return value % this.parameters.primeGroup.N !== BigInt(0);
}
public async computeU(A: bigint, B: bigint): Promise<bigint> {
return arrayBufferToBigInt(
await this.hashPadded(bigIntToArrayBuffer(A), bigIntToArrayBuffer(B)),
);
}
public async computeClientEvidence(
_I: string,
_s: bigint,
A: bigint,
B: bigint,
S: bigint,
): Promise<bigint> {
return arrayBufferToBigInt(
await this.hash(
bigIntToArrayBuffer(A),
bigIntToArrayBuffer(B),
bigIntToArrayBuffer(S),
),
);
}
public async computeServerEvidence(
A: bigint,
M1: bigint,
S: bigint,
): Promise<bigint> {
return arrayBufferToBigInt(
await this.hash(
bigIntToArrayBuffer(A),
bigIntToArrayBuffer(M1),
bigIntToArrayBuffer(S),
),
);
}
public computeClientSessionKey(
k: bigint,
x: bigint,
u: bigint,
a: bigint,
B: bigint,
): bigint {
const N = this.parameters.primeGroup.N;
const exp = u * x + a;
const tmp = (modPow(this.parameters.primeGroup.g, x, N) * k) % N;
return modPow(B + N - tmp, exp, N);
}
}