@crossdyne/security 0.2.0-beta.1 → 0.3.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +607 -109
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +362 -28
- package/dist/index.d.ts +362 -28
- package/dist/index.js +594 -109
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,82 +1,150 @@
|
|
|
1
|
+
// src/configurations/security-constants.ts
|
|
2
|
+
var SecurityConstants = {
|
|
3
|
+
AesGcmNonceSize: 12,
|
|
4
|
+
AesGcmTagSize: 16,
|
|
5
|
+
AesGcmTagSizeMin: 12,
|
|
6
|
+
AesGcmTagSizeMax: 16,
|
|
7
|
+
KeySizeBytes: 32,
|
|
8
|
+
// 256 bits
|
|
9
|
+
Pbkdf2IterationsDefault: 6e5,
|
|
10
|
+
Pbkdf2IterationsMinimum: 1e5
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// src/configurations/supported-hash-algorithms.ts
|
|
14
|
+
var SupportedHashAlgorithms = ["SHA-256", "SHA-384", "SHA-512"];
|
|
15
|
+
|
|
1
16
|
// src/utils/security.utils.ts
|
|
2
17
|
var SecurityUtils = class {
|
|
3
|
-
|
|
4
|
-
Base64 helpers
|
|
5
|
-
*/
|
|
18
|
+
/** Encodes Uint8Array to standard Base64. */
|
|
6
19
|
static toBase64(bytes) {
|
|
7
20
|
let binary = "";
|
|
8
|
-
for (let i = 0; i < bytes.
|
|
21
|
+
for (let i = 0; i < bytes.length; i++)
|
|
9
22
|
binary += String.fromCharCode(bytes[i]);
|
|
10
|
-
return
|
|
23
|
+
return btoa(binary);
|
|
11
24
|
}
|
|
25
|
+
/** Decodes URL-safe or standard Base64 to Uint8Array. */
|
|
12
26
|
static fromBase64(base64) {
|
|
13
|
-
|
|
14
|
-
const
|
|
27
|
+
let cleaned = base64.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
|
|
28
|
+
const mod = cleaned.length % 4;
|
|
29
|
+
if (mod !== 0) {
|
|
30
|
+
cleaned += "=".repeat(4 - mod);
|
|
31
|
+
}
|
|
32
|
+
const binary = atob(cleaned);
|
|
15
33
|
const bytes = new Uint8Array(binary.length);
|
|
16
34
|
for (let i = 0; i < binary.length; i++)
|
|
17
35
|
bytes[i] = binary.charCodeAt(i);
|
|
18
36
|
return bytes;
|
|
19
37
|
}
|
|
38
|
+
/** Converts big-endian bytes to bigint. */
|
|
39
|
+
static bytesToBigInt(bytes) {
|
|
40
|
+
if (bytes.length === 0)
|
|
41
|
+
return 0n;
|
|
42
|
+
return BigInt("0x" + Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join(""));
|
|
43
|
+
}
|
|
44
|
+
/** Converts bigint to fixed-length big-endian bytes (pads/truncates). */
|
|
20
45
|
static bigIntToFixedBytes(bn, length) {
|
|
21
46
|
let hex = bn.toString(16);
|
|
22
47
|
if (hex.length % 2 !== 0)
|
|
23
48
|
hex = "0" + hex;
|
|
24
49
|
if (hex.length > length * 2)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
50
|
+
hex = hex.slice(hex.length - length * 2);
|
|
51
|
+
else
|
|
52
|
+
hex = hex.padStart(length * 2, "0");
|
|
53
|
+
return new Uint8Array(hex.match(/.{1,2}/g)?.map((b) => parseInt(b, 16)) || []);
|
|
28
54
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
55
|
+
/** Constant-time comparison of two Uint8Arrays. */
|
|
56
|
+
static fixedTimeEquals(a, b) {
|
|
57
|
+
if (a.length !== b.length)
|
|
58
|
+
return false;
|
|
59
|
+
let diff = 0;
|
|
60
|
+
for (let i = 0; i < a.length; i++)
|
|
61
|
+
diff |= a[i] ^ b[i];
|
|
62
|
+
return diff === 0;
|
|
32
63
|
}
|
|
33
|
-
|
|
34
|
-
Math
|
|
35
|
-
*/
|
|
64
|
+
/** Modular exponentiation (base^exp mod mod) using binary exponentiation. */
|
|
36
65
|
static expMod(base, exp, mod) {
|
|
37
66
|
let res = BigInt(1);
|
|
38
67
|
base = base % mod;
|
|
39
|
-
while (exp >
|
|
40
|
-
if (exp %
|
|
68
|
+
while (exp > 0n) {
|
|
69
|
+
if (exp % 2n === 1n)
|
|
70
|
+
res = res * base % mod;
|
|
41
71
|
base = base * base % mod;
|
|
42
|
-
exp = exp /
|
|
72
|
+
exp = exp / 2n;
|
|
43
73
|
}
|
|
44
74
|
return res;
|
|
45
75
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// src/crypto/aes-gcm-options.ts
|
|
79
|
+
var AesGcmOptions = class _AesGcmOptions {
|
|
80
|
+
constructor() {
|
|
81
|
+
this._nonceSize = SecurityConstants.AesGcmNonceSize;
|
|
82
|
+
this._tagSize = SecurityConstants.AesGcmTagSize;
|
|
83
|
+
}
|
|
84
|
+
/** Nonce size in bytes (must be 12). */
|
|
85
|
+
get nonceSize() {
|
|
86
|
+
return this._nonceSize;
|
|
87
|
+
}
|
|
88
|
+
set nonceSize(v) {
|
|
89
|
+
if (v !== SecurityConstants.AesGcmNonceSize) throw new RangeError(`AES-GCM requires ${SecurityConstants.AesGcmNonceSize}-byte nonce`);
|
|
90
|
+
this._nonceSize = v;
|
|
91
|
+
}
|
|
92
|
+
/** Tag size in bytes (12–16, default 16). */
|
|
93
|
+
get tagSize() {
|
|
94
|
+
return this._tagSize;
|
|
95
|
+
}
|
|
96
|
+
set tagSize(v) {
|
|
97
|
+
if (v < SecurityConstants.AesGcmTagSizeMin || v > SecurityConstants.AesGcmTagSizeMax) throw new RangeError(`Tag size must be between ${SecurityConstants.AesGcmTagSizeMin} and ${SecurityConstants.AesGcmTagSizeMax}`);
|
|
98
|
+
this._tagSize = v;
|
|
99
|
+
}
|
|
100
|
+
/** Validates that {@link tagSize} is in the allowed range. */
|
|
101
|
+
validate() {
|
|
102
|
+
if (this.tagSize < SecurityConstants.AesGcmTagSizeMin || this.tagSize > SecurityConstants.AesGcmTagSizeMax) throw new Error(`Invalid Tag Size: ${this.tagSize}`);
|
|
103
|
+
}
|
|
104
|
+
/** Default preset: nonce=12, tag=16, no AAD. */
|
|
105
|
+
static get default() {
|
|
106
|
+
return new _AesGcmOptions();
|
|
107
|
+
}
|
|
108
|
+
/** Fluent setter for {@link tagSize}. */
|
|
109
|
+
withTagSize(s) {
|
|
110
|
+
this.tagSize = s;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Fluent setter for {@link associatedData}.
|
|
115
|
+
* Accepts a byte array or a UTF-8 string (encoded internally).
|
|
116
|
+
*/
|
|
117
|
+
withAssociatedData(aad) {
|
|
118
|
+
this.associatedData = typeof aad === "string" ? new TextEncoder().encode(aad) : aad;
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
/** Validates and returns this instance. */
|
|
122
|
+
build() {
|
|
123
|
+
this.validate();
|
|
124
|
+
return this;
|
|
60
125
|
}
|
|
61
126
|
};
|
|
62
|
-
SecurityUtils.MODULUS_SIZE = 384;
|
|
63
|
-
SecurityUtils.NONCE_SIZE = 12;
|
|
64
|
-
SecurityUtils.TAG_SIZE = 16;
|
|
65
|
-
SecurityUtils.KEY_SIZE = 32;
|
|
66
|
-
SecurityUtils.N = BigInt("0xAC6BDB41324A9A9BF166DE5E1F403D434A6E1B3B94A7E62AC1211858E002C75AD4455C9D19C0A3180296917A376205164043E20144FF485719D181A99EB574671AC58054457ED444A67032EA17D03AD43464D2397449CA593630A670D90D95A78E846A3C8AF80862098D80F33C42ED7059E75225E0A52718E2379369F65B79680A6560B080092EE71986066735A96A7D42E7597116742B02D3A154471B6A23D84E0D642C790D597A2BB7F5A48F734898BDD138C69493E723491959C1B4BD40C91C1C7924F88D046467A006507E781220A80C55A927906A7C6C9C227E674686DD5D1B855D28F0D604E24586C608630B9A34C4808381A54F0D9080A5F90B60187F");
|
|
67
|
-
SecurityUtils.g = BigInt(2);
|
|
68
|
-
SecurityUtils.k = BigInt("0xD55AE1AEC9F9115621E93E16E5DE4517DF8450D0957024D1256AA32B71E4E412");
|
|
69
|
-
/*
|
|
70
|
-
BigInt helpers
|
|
71
|
-
*/
|
|
72
|
-
SecurityUtils.bytesToBigInt = (bytes) => BigInt("0x" + Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join(""));
|
|
73
127
|
|
|
74
128
|
// src/crypto/crypto.service.ts
|
|
75
129
|
var CryptoService = class {
|
|
76
130
|
constructor() {
|
|
131
|
+
/**
|
|
132
|
+
* Generates cryptographically secure random bytes.
|
|
133
|
+
* @param length - Number of bytes (default 32).
|
|
134
|
+
* @returns Uint8Array of random bytes.
|
|
135
|
+
*/
|
|
77
136
|
this.generateRandomBytes = (length = 32) => crypto.getRandomValues(new Uint8Array(length));
|
|
78
137
|
}
|
|
79
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Encrypts a serializable object to a Base64 string.
|
|
140
|
+
* @param dataModel - Object or Uint8Array to encrypt.
|
|
141
|
+
* @param key - AES-256 key (32 bytes).
|
|
142
|
+
* @param options - AES-GCM configuration; uses default if omitted.
|
|
143
|
+
* @returns Base64-encoded ciphertext with prepended nonce.
|
|
144
|
+
*/
|
|
145
|
+
async encryptData(dataModel, key, options) {
|
|
146
|
+
const opts = options ?? AesGcmOptions.default;
|
|
147
|
+
opts.validate();
|
|
80
148
|
const encoder = new TextEncoder();
|
|
81
149
|
let jsonString;
|
|
82
150
|
if (dataModel instanceof Uint8Array) {
|
|
@@ -85,7 +153,7 @@ var CryptoService = class {
|
|
|
85
153
|
jsonString = JSON.stringify(dataModel);
|
|
86
154
|
}
|
|
87
155
|
const plainBytes = encoder.encode(jsonString);
|
|
88
|
-
const nonce = crypto.getRandomValues(new Uint8Array(
|
|
156
|
+
const nonce = crypto.getRandomValues(new Uint8Array(opts.nonceSize));
|
|
89
157
|
const cryptoKey = await crypto.subtle.importKey(
|
|
90
158
|
"raw",
|
|
91
159
|
key,
|
|
@@ -93,28 +161,43 @@ var CryptoService = class {
|
|
|
93
161
|
false,
|
|
94
162
|
["encrypt"]
|
|
95
163
|
);
|
|
164
|
+
let associatedData = new Uint8Array(0);
|
|
165
|
+
if (opts.associatedData != null) {
|
|
166
|
+
associatedData = opts.associatedData;
|
|
167
|
+
}
|
|
96
168
|
const encryptedContent = await crypto.subtle.encrypt(
|
|
97
169
|
{
|
|
98
170
|
name: "AES-GCM",
|
|
99
171
|
iv: nonce,
|
|
100
|
-
tagLength:
|
|
172
|
+
tagLength: opts.tagSize * 8,
|
|
173
|
+
additionalData: associatedData
|
|
101
174
|
},
|
|
102
175
|
cryptoKey,
|
|
103
176
|
plainBytes
|
|
104
177
|
);
|
|
105
|
-
const result = new Uint8Array(
|
|
178
|
+
const result = new Uint8Array(opts.nonceSize + encryptedContent.byteLength);
|
|
106
179
|
result.set(nonce, 0);
|
|
107
|
-
result.set(new Uint8Array(encryptedContent),
|
|
180
|
+
result.set(new Uint8Array(encryptedContent), opts.nonceSize);
|
|
108
181
|
return SecurityUtils.toBase64(result);
|
|
109
182
|
}
|
|
110
|
-
|
|
183
|
+
/**
|
|
184
|
+
* Decrypts a Base64-encoded ciphertext back to the original object.
|
|
185
|
+
* @param encryptedBase64 - The encrypted data.
|
|
186
|
+
* @param key - AES-256 key (32 bytes).
|
|
187
|
+
* @param options - AES-GCM configuration; uses default if omitted.
|
|
188
|
+
* @returns Deserialized object, or null if input is empty.
|
|
189
|
+
* @throws If authentication tag mismatch or corrupted data.
|
|
190
|
+
*/
|
|
191
|
+
async decryptData(encryptedBase64, key, options, isBytes = false) {
|
|
111
192
|
if (!encryptedBase64)
|
|
112
193
|
return null;
|
|
194
|
+
const opts = options ?? AesGcmOptions.default;
|
|
195
|
+
opts.validate();
|
|
113
196
|
const encryptedBytes = SecurityUtils.fromBase64(encryptedBase64);
|
|
114
|
-
if (encryptedBytes.length <
|
|
115
|
-
throw new Error(
|
|
116
|
-
const nonce = encryptedBytes.slice(0,
|
|
117
|
-
const ciphertextWithTag = encryptedBytes.slice(
|
|
197
|
+
if (encryptedBytes.length < opts.nonceSize + opts.tagSize)
|
|
198
|
+
throw new Error(`Invalid format: minimum expected ${opts.nonceSize + opts.tagSize} byte.`);
|
|
199
|
+
const nonce = encryptedBytes.slice(0, opts.nonceSize);
|
|
200
|
+
const ciphertextWithTag = encryptedBytes.slice(opts.nonceSize);
|
|
118
201
|
const cryptoKey = await crypto.subtle.importKey(
|
|
119
202
|
"raw",
|
|
120
203
|
key,
|
|
@@ -123,122 +206,524 @@ var CryptoService = class {
|
|
|
123
206
|
["decrypt"]
|
|
124
207
|
);
|
|
125
208
|
try {
|
|
209
|
+
let associatedData = new Uint8Array(0);
|
|
210
|
+
if (opts.associatedData != null) {
|
|
211
|
+
associatedData = opts.associatedData;
|
|
212
|
+
}
|
|
126
213
|
const decryptedBuffer = await crypto.subtle.decrypt(
|
|
127
214
|
{
|
|
128
215
|
name: "AES-GCM",
|
|
129
216
|
iv: nonce,
|
|
130
|
-
tagLength:
|
|
217
|
+
tagLength: opts.tagSize * 8,
|
|
218
|
+
additionalData: associatedData
|
|
131
219
|
},
|
|
132
220
|
cryptoKey,
|
|
133
221
|
ciphertextWithTag
|
|
134
222
|
);
|
|
135
223
|
const decoder = new TextDecoder();
|
|
136
224
|
const jsonString = decoder.decode(decryptedBuffer);
|
|
225
|
+
const parsed = JSON.parse(jsonString);
|
|
226
|
+
if (isBytes && typeof parsed === "string") {
|
|
227
|
+
return SecurityUtils.fromBase64(parsed);
|
|
228
|
+
}
|
|
137
229
|
return JSON.parse(jsonString);
|
|
138
230
|
} catch (e) {
|
|
139
|
-
console.error("
|
|
140
|
-
|
|
231
|
+
console.error("Decryption error:", e);
|
|
232
|
+
throw new Error("Decryption failed: authentication tag mismatch or corrupted data.");
|
|
141
233
|
}
|
|
142
234
|
}
|
|
143
235
|
};
|
|
144
236
|
|
|
145
|
-
// src/crypto/
|
|
146
|
-
var
|
|
237
|
+
// src/crypto/kdf-options.ts
|
|
238
|
+
var KdfOptions = class _KdfOptions {
|
|
147
239
|
constructor() {
|
|
148
|
-
this.
|
|
149
|
-
this.
|
|
240
|
+
this._pbkdf2Iterations = SecurityConstants.Pbkdf2IterationsDefault;
|
|
241
|
+
this._hashAlgorithm = "SHA-256";
|
|
242
|
+
}
|
|
243
|
+
/** PBKDF2 iteration count (minimum 100_000). */
|
|
244
|
+
get pbkdf2Iterations() {
|
|
245
|
+
return this._pbkdf2Iterations;
|
|
246
|
+
}
|
|
247
|
+
set pbkdf2Iterations(v) {
|
|
248
|
+
if (v < SecurityConstants.Pbkdf2IterationsMinimum)
|
|
249
|
+
throw new RangeError(`PBKDF2 iterations must be \u2265 ${SecurityConstants.Pbkdf2IterationsMinimum}`);
|
|
250
|
+
this._pbkdf2Iterations = v;
|
|
251
|
+
}
|
|
252
|
+
/** Hash algorithm used by PBKDF2. Must be one of {@link SupportedHashAlgorithms}. */
|
|
253
|
+
get hashAlgorithm() {
|
|
254
|
+
return this._hashAlgorithm;
|
|
255
|
+
}
|
|
256
|
+
set hashAlgorithm(v) {
|
|
257
|
+
if (!SupportedHashAlgorithms.includes(v))
|
|
258
|
+
throw new RangeError(`Unsupported hash: ${v}`);
|
|
259
|
+
this._hashAlgorithm = v;
|
|
260
|
+
}
|
|
261
|
+
/** Validates iterations and hash algorithm. */
|
|
262
|
+
validate() {
|
|
263
|
+
if (this.pbkdf2Iterations < SecurityConstants.Pbkdf2IterationsMinimum)
|
|
264
|
+
throw new Error(`Pbkdf2Iterations (${this.pbkdf2Iterations}) below minimum`);
|
|
265
|
+
if (!SupportedHashAlgorithms.includes(this.hashAlgorithm))
|
|
266
|
+
throw new Error(`Invalid hash algorithm: ${this.hashAlgorithm}`);
|
|
150
267
|
}
|
|
151
|
-
|
|
268
|
+
/** Default preset: SHA-256, 600_000 iterations. */
|
|
269
|
+
static get default() {
|
|
270
|
+
return new _KdfOptions();
|
|
271
|
+
}
|
|
272
|
+
/** Fluent setter for {@link pbkdf2Iterations}. */
|
|
273
|
+
withPbkdf2Iterations(i) {
|
|
274
|
+
this.pbkdf2Iterations = i;
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
/** Fluent setter for {@link hashAlgorithm}. */
|
|
278
|
+
withHashAlgorithm(h) {
|
|
279
|
+
this.hashAlgorithm = h;
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
/** Validates and returns this instance. */
|
|
283
|
+
build() {
|
|
284
|
+
this.validate();
|
|
285
|
+
return this;
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// src/crypto/key-derivation.service.ts
|
|
290
|
+
var KeyDerivationService = class {
|
|
291
|
+
/**
|
|
292
|
+
* Derives KEK and Base64 AuthHash. Identity is normalized (trimmed, lowercase).
|
|
293
|
+
* @param identity - User identity (email, username).
|
|
294
|
+
* @param password - User password.
|
|
295
|
+
* @param salt - Random salt.
|
|
296
|
+
* @param options - KDF configuration; uses default if omitted.
|
|
297
|
+
* @returns Object with `kek` (Uint8Array) and `authHash` (Base64 string).
|
|
298
|
+
*/
|
|
299
|
+
async deriveKeysFromPassword(identity, password, salt, options) {
|
|
300
|
+
const opts = options ?? KdfOptions.default;
|
|
301
|
+
opts.validate();
|
|
152
302
|
const safeSalt = new Uint8Array(salt);
|
|
153
303
|
const encoder = new TextEncoder();
|
|
154
|
-
const normalizedLogin =
|
|
304
|
+
const normalizedLogin = identity.trim().toLowerCase();
|
|
155
305
|
const combinedPassword = `${normalizedLogin}:${password}`;
|
|
156
306
|
const passwordBytes = encoder.encode(combinedPassword);
|
|
157
307
|
const baseKey = await crypto.subtle.importKey("raw", passwordBytes, "PBKDF2", false, ["deriveBits", "deriveKey"]);
|
|
158
308
|
const masterKeyBits = await crypto.subtle.deriveBits({
|
|
159
309
|
name: "PBKDF2",
|
|
160
310
|
salt: safeSalt,
|
|
161
|
-
iterations:
|
|
162
|
-
hash:
|
|
163
|
-
}, baseKey,
|
|
311
|
+
iterations: opts.pbkdf2Iterations,
|
|
312
|
+
hash: opts.hashAlgorithm
|
|
313
|
+
}, baseKey, SecurityConstants.KeySizeBytes * 8);
|
|
164
314
|
const masterKey = await crypto.subtle.importKey("raw", masterKeyBits, "HKDF", false, ["deriveBits"]);
|
|
165
315
|
const kek = await crypto.subtle.deriveBits(
|
|
166
|
-
{ name: "HKDF", hash:
|
|
316
|
+
{ name: "HKDF", hash: opts.hashAlgorithm, salt: new Uint8Array(0), info: encoder.encode("AES-GCM-KEK-v1") },
|
|
167
317
|
masterKey,
|
|
168
|
-
|
|
318
|
+
SecurityConstants.KeySizeBytes * 8
|
|
169
319
|
);
|
|
170
320
|
const authBytes = await crypto.subtle.deriveBits(
|
|
171
|
-
{ name: "HKDF", hash:
|
|
321
|
+
{ name: "HKDF", hash: opts.hashAlgorithm, salt: new Uint8Array(0), info: encoder.encode("SERVER-AUTH-HASH-v1") },
|
|
172
322
|
masterKey,
|
|
173
|
-
|
|
323
|
+
SecurityConstants.KeySizeBytes * 8
|
|
174
324
|
);
|
|
175
325
|
return {
|
|
176
326
|
kek: new Uint8Array(kek),
|
|
177
327
|
authHash: SecurityUtils.toBase64(new Uint8Array(authBytes))
|
|
178
328
|
};
|
|
179
329
|
}
|
|
330
|
+
/**
|
|
331
|
+
* Derives an SRP-compatible authentication hash (output size = hash output length).
|
|
332
|
+
* @param identity - User identity.
|
|
333
|
+
* @param password - User password.
|
|
334
|
+
* @param salt - Random salt.
|
|
335
|
+
* @param srpHashAlgorithm - SRP hash algorithm (SHA-256/384/512).
|
|
336
|
+
* @param options - KDF configuration; uses default if omitted.
|
|
337
|
+
* @returns Raw hash bytes for use as SRP verifier input (x).
|
|
338
|
+
*/
|
|
339
|
+
async deriveAuthHashForSrp(identity, password, salt, srpHashAlgorithm, options) {
|
|
340
|
+
const opts = options ?? KdfOptions.default;
|
|
341
|
+
opts.validate();
|
|
342
|
+
const srpHashSize = srpHashAlgorithm === "SHA-256" ? 32 : srpHashAlgorithm === "SHA-384" ? 48 : 64;
|
|
343
|
+
const normalizedLogin = identity.trim().toLowerCase();
|
|
344
|
+
const combinedPassword = `${normalizedLogin}:${password}`;
|
|
345
|
+
const passwordBytes = new TextEncoder().encode(combinedPassword);
|
|
346
|
+
const baseKey = await crypto.subtle.importKey(
|
|
347
|
+
"raw",
|
|
348
|
+
passwordBytes,
|
|
349
|
+
"PBKDF2",
|
|
350
|
+
false,
|
|
351
|
+
["deriveBits"]
|
|
352
|
+
);
|
|
353
|
+
const masterKeyBits = await crypto.subtle.deriveBits({
|
|
354
|
+
name: "PBKDF2",
|
|
355
|
+
salt,
|
|
356
|
+
iterations: opts.pbkdf2Iterations,
|
|
357
|
+
hash: srpHashAlgorithm
|
|
358
|
+
}, baseKey, SecurityConstants.KeySizeBytes * 8);
|
|
359
|
+
const masterKey = await crypto.subtle.importKey(
|
|
360
|
+
"raw",
|
|
361
|
+
masterKeyBits,
|
|
362
|
+
"HKDF",
|
|
363
|
+
false,
|
|
364
|
+
["deriveBits"]
|
|
365
|
+
);
|
|
366
|
+
const info = new TextEncoder().encode("SRP-AUTH-HASH-v1");
|
|
367
|
+
const authBytes = await crypto.subtle.deriveBits(
|
|
368
|
+
{
|
|
369
|
+
name: "HKDF",
|
|
370
|
+
hash: srpHashAlgorithm,
|
|
371
|
+
salt: new Uint8Array(0),
|
|
372
|
+
info
|
|
373
|
+
},
|
|
374
|
+
masterKey,
|
|
375
|
+
srpHashSize * 8
|
|
376
|
+
);
|
|
377
|
+
return new Uint8Array(authBytes);
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
// src/crypto/crypto-version.ts
|
|
382
|
+
var CryptoVersion = /* @__PURE__ */ ((CryptoVersion2) => {
|
|
383
|
+
CryptoVersion2[CryptoVersion2["V1"] = 1] = "V1";
|
|
384
|
+
return CryptoVersion2;
|
|
385
|
+
})(CryptoVersion || {});
|
|
386
|
+
|
|
387
|
+
// src/crypto/crypto-profile.ts
|
|
388
|
+
var CryptoProfile = class {
|
|
389
|
+
/**
|
|
390
|
+
* Creates a new CryptoProfile.
|
|
391
|
+
* @param params - Object containing version, kdfOptions, aesGcmOptions.
|
|
392
|
+
*/
|
|
393
|
+
constructor(params) {
|
|
394
|
+
this.version = params.version;
|
|
395
|
+
this.kdfOptions = params.kdfOptions;
|
|
396
|
+
this.aesGcmOptions = params.aesGcmOptions;
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
// src/crypto/crypto-profile-registry.ts
|
|
401
|
+
var CryptoProfileRegistry = class {
|
|
402
|
+
static getProfile(version) {
|
|
403
|
+
switch (version) {
|
|
404
|
+
case 1 /* V1 */:
|
|
405
|
+
return new CryptoProfile({
|
|
406
|
+
version: 1 /* V1 */,
|
|
407
|
+
kdfOptions: KdfOptions.default,
|
|
408
|
+
aesGcmOptions: AesGcmOptions.default
|
|
409
|
+
});
|
|
410
|
+
default:
|
|
411
|
+
throw new Error(`Unsupported crypto version: ${version}`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
/** Latest supported profile (currently V1). */
|
|
415
|
+
static get latest() {
|
|
416
|
+
return this.getProfile(1 /* V1 */);
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
// src/utils/srp-encoding.ts
|
|
421
|
+
var SrpEncoding = class {
|
|
422
|
+
/** Serializes a bigint to modulus-sized big-endian bytes. */
|
|
423
|
+
static toModulusBytes(ctx, value) {
|
|
424
|
+
return SecurityUtils.bigIntToFixedBytes(value, ctx.modulusSize);
|
|
425
|
+
}
|
|
426
|
+
/** Serializes a bigint to hash-sized big-endian bytes. */
|
|
427
|
+
static toHashBytes(ctx, value) {
|
|
428
|
+
return SecurityUtils.bigIntToFixedBytes(value, ctx.hashSize);
|
|
429
|
+
}
|
|
430
|
+
/** Hashes modulus-sized values (e.g., u = H(A, B)). */
|
|
431
|
+
static async hashModuli(ctx, ...values) {
|
|
432
|
+
const buffers = values.map((v) => this.toModulusBytes(ctx, v));
|
|
433
|
+
return this.hash(ctx.hashAlgorithmName, ...buffers);
|
|
434
|
+
}
|
|
435
|
+
/** Computes M1 = H(A || B || sessionKeyK). */
|
|
436
|
+
static async computeM1(ctx, A, B, sessionKeyK) {
|
|
437
|
+
return this.hash(ctx.hashAlgorithmName, this.toModulusBytes(ctx, A), this.toModulusBytes(ctx, B), sessionKeyK);
|
|
438
|
+
}
|
|
439
|
+
/** Computes M2 = H(A || M1 || sessionKeyK). */
|
|
440
|
+
static async computeM2(ctx, A, M1, sessionKeyK) {
|
|
441
|
+
return this.hash(ctx.hashAlgorithmName, this.toModulusBytes(ctx, A), this.toHashBytes(ctx, M1), sessionKeyK);
|
|
442
|
+
}
|
|
443
|
+
/** Computes session key K = H(S). */
|
|
444
|
+
static async computeSessionKey(ctx, S) {
|
|
445
|
+
const sBytes = this.toModulusBytes(ctx, S);
|
|
446
|
+
const hashBuffer = await crypto.subtle.digest(
|
|
447
|
+
ctx.hashAlgorithmName,
|
|
448
|
+
sBytes
|
|
449
|
+
);
|
|
450
|
+
return new Uint8Array(hashBuffer);
|
|
451
|
+
}
|
|
452
|
+
/** Concatenates byte arrays and returns the hash as bigint. */
|
|
453
|
+
static async hash(algo, ...buffers) {
|
|
454
|
+
const totalLen = buffers.reduce((sum, b) => sum + b.length, 0);
|
|
455
|
+
const combined = new Uint8Array(totalLen);
|
|
456
|
+
let offset = 0;
|
|
457
|
+
for (const buf of buffers) {
|
|
458
|
+
combined.set(buf, offset);
|
|
459
|
+
offset += buf.length;
|
|
460
|
+
}
|
|
461
|
+
const hashBuffer = await crypto.subtle.digest(algo, combined);
|
|
462
|
+
return SecurityUtils.bytesToBigInt(new Uint8Array(hashBuffer));
|
|
463
|
+
}
|
|
180
464
|
};
|
|
181
465
|
|
|
182
|
-
// src/srp/srp
|
|
183
|
-
var
|
|
466
|
+
// src/srp/srp-client-service.ts
|
|
467
|
+
var SrpClientService = class {
|
|
184
468
|
constructor() {
|
|
185
469
|
this.keyDerivation = new KeyDerivationService();
|
|
186
470
|
}
|
|
187
|
-
|
|
471
|
+
/**
|
|
472
|
+
* Computes SRP verifier v = g^x mod N from the authentication hash.
|
|
473
|
+
* @param authHash - Auth hash (Base64).
|
|
474
|
+
* @param ctx - SRP context (N, g, hash algorithm, etc.).
|
|
475
|
+
* @returns Verifier as Base64 string.
|
|
476
|
+
*/
|
|
477
|
+
async generateSrpVerifier(authHash, ctx) {
|
|
188
478
|
const x = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(authHash));
|
|
189
|
-
const v = SecurityUtils.expMod(
|
|
190
|
-
return SecurityUtils.toBase64(
|
|
479
|
+
const v = SecurityUtils.expMod(ctx.g, x, ctx.N);
|
|
480
|
+
return SecurityUtils.toBase64(SrpEncoding.toModulusBytes(ctx, v));
|
|
191
481
|
}
|
|
192
|
-
|
|
482
|
+
/**
|
|
483
|
+
* Generates client proof (A, M1, session key S) from server challenge.
|
|
484
|
+
* @param login - User login.
|
|
485
|
+
* @param password - Plaintext password.
|
|
486
|
+
* @param saltBase64 - Server salt (URL-safe Base64).
|
|
487
|
+
* @param B_base64 - Server public ephemeral B (URL-safe Base64).
|
|
488
|
+
* @param ctx - SRP context.
|
|
489
|
+
* @returns Object with A, M1, S as Base64 strings.
|
|
490
|
+
*/
|
|
491
|
+
async generateSrpProof(login, password, saltBase64, B_base64, ctx) {
|
|
193
492
|
const salt = SecurityUtils.fromBase64(saltBase64);
|
|
194
|
-
const
|
|
195
|
-
const x = SecurityUtils.bytesToBigInt(
|
|
493
|
+
const authHash = await this.keyDerivation.deriveAuthHashForSrp(login, password, salt, ctx.hashAlgorithmName);
|
|
494
|
+
const x = SecurityUtils.bytesToBigInt(authHash);
|
|
196
495
|
const aBytes = crypto.getRandomValues(new Uint8Array(32));
|
|
197
496
|
const a = SecurityUtils.bytesToBigInt(aBytes);
|
|
198
|
-
const A = SecurityUtils.expMod(
|
|
199
|
-
if (A %
|
|
200
|
-
throw new Error("
|
|
497
|
+
const A = SecurityUtils.expMod(ctx.g, a, ctx.N);
|
|
498
|
+
if (A % ctx.N === 0n)
|
|
499
|
+
throw new Error("Critical error: A % N === 0");
|
|
201
500
|
const B = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(B_base64));
|
|
202
|
-
if (B %
|
|
203
|
-
throw new Error("
|
|
204
|
-
const u = await
|
|
205
|
-
{ value: A, length: SecurityUtils.MODULUS_SIZE },
|
|
206
|
-
{ value: B, length: SecurityUtils.MODULUS_SIZE }
|
|
207
|
-
]);
|
|
501
|
+
if (B % ctx.N === 0n)
|
|
502
|
+
throw new Error("Critical error: B % N === 0");
|
|
503
|
+
const u = await SrpEncoding.hashModuli(ctx, A, B);
|
|
208
504
|
if (u === 0n)
|
|
209
505
|
throw new Error("\u041D\u0435\u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043C\u043E\u0435 \u0437\u043D\u0430\u0447\u0435\u043D\u0438\u0435 u");
|
|
210
|
-
const gX = SecurityUtils.expMod(
|
|
211
|
-
const term =
|
|
212
|
-
const base = (B - term +
|
|
506
|
+
const gX = SecurityUtils.expMod(ctx.g, x, ctx.N);
|
|
507
|
+
const term = ctx.k * gX % ctx.N;
|
|
508
|
+
const base = (B - term + ctx.N) % ctx.N;
|
|
213
509
|
const exponent = a + u * x;
|
|
214
|
-
const S = SecurityUtils.expMod(base, exponent,
|
|
510
|
+
const S = SecurityUtils.expMod(base, exponent, ctx.N);
|
|
215
511
|
if (S === 0n)
|
|
216
|
-
throw new Error("
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
{ value: B, length: SecurityUtils.MODULUS_SIZE },
|
|
220
|
-
{ value: S, length: SecurityUtils.MODULUS_SIZE }
|
|
221
|
-
]);
|
|
512
|
+
throw new Error("Critical error: S === 0");
|
|
513
|
+
const sessionKeyK = await SrpEncoding.computeSessionKey(ctx, S);
|
|
514
|
+
const M1 = await SrpEncoding.computeM1(ctx, A, B, sessionKeyK);
|
|
222
515
|
return {
|
|
223
|
-
A: SecurityUtils.toBase64(
|
|
224
|
-
M1: SecurityUtils.toBase64(
|
|
225
|
-
S: SecurityUtils.toBase64(
|
|
516
|
+
A: SecurityUtils.toBase64(SrpEncoding.toModulusBytes(ctx, A)),
|
|
517
|
+
M1: SecurityUtils.toBase64(SrpEncoding.toHashBytes(ctx, M1)),
|
|
518
|
+
S: SecurityUtils.toBase64(SrpEncoding.toModulusBytes(ctx, S))
|
|
226
519
|
};
|
|
227
520
|
}
|
|
228
|
-
|
|
521
|
+
/**
|
|
522
|
+
* Validates the server proof M2 to authenticate the server.
|
|
523
|
+
* @param A_b64 - Client public A (Base64).
|
|
524
|
+
* @param M1_b64 - Client proof M1 (Base64).
|
|
525
|
+
* @param S_b64 - Session key S (Base64).
|
|
526
|
+
* @param serverM2_b64 - Server proof M2 (Base64).
|
|
527
|
+
* @param ctx - SRP context.
|
|
528
|
+
* @returns True if the server proof is valid.
|
|
529
|
+
*/
|
|
530
|
+
async verifyServerM2(A_b64, M1_b64, S_b64, serverM2_b64, ctx) {
|
|
229
531
|
const A = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(A_b64));
|
|
230
532
|
const M1 = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(M1_b64));
|
|
231
533
|
const S = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(S_b64));
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
534
|
+
const sessionKeyK = await SrpEncoding.computeSessionKey(ctx, S);
|
|
535
|
+
const computedM2 = await SrpEncoding.computeM2(ctx, A, M1, sessionKeyK);
|
|
536
|
+
const computedM2Bytes = SrpEncoding.toHashBytes(ctx, computedM2);
|
|
537
|
+
const serverM2Bytes = SecurityUtils.fromBase64(serverM2_b64);
|
|
538
|
+
return SecurityUtils.fixedTimeEquals(computedM2Bytes, serverM2Bytes);
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
// src/srp/srp-server-service.ts
|
|
543
|
+
var SrpServerService = class {
|
|
544
|
+
/**
|
|
545
|
+
* Generates server challenge B and session state from verifier.
|
|
546
|
+
* @param login - User login.
|
|
547
|
+
* @param verifierBytes - Stored verifier v as byte array.
|
|
548
|
+
* @param ctx - SRP context (hash, N, g, etc.).
|
|
549
|
+
* @returns Session state with private b, verifier, and public B.
|
|
550
|
+
*/
|
|
551
|
+
async getSrpChallenge(login, verifierBytes, ctx) {
|
|
552
|
+
const v = SecurityUtils.bytesToBigInt(verifierBytes);
|
|
553
|
+
const bBytes = crypto.getRandomValues(new Uint8Array(32));
|
|
554
|
+
const b = SecurityUtils.bytesToBigInt(bBytes);
|
|
555
|
+
const gB = SecurityUtils.expMod(ctx.g, b, ctx.N);
|
|
556
|
+
const B = (ctx.k * v + gB) % ctx.N;
|
|
557
|
+
return {
|
|
558
|
+
login,
|
|
559
|
+
privateKeyB: SecurityUtils.toBase64(bBytes),
|
|
560
|
+
verifier: SecurityUtils.toBase64(verifierBytes),
|
|
561
|
+
publicKeyB: SecurityUtils.toBase64(SrpEncoding.toModulusBytes(ctx, B))
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Verifies client M1 proof and returns server M2 proof.
|
|
566
|
+
* @param sessionState - Server session state.
|
|
567
|
+
* @param a - Client public A (Base64).
|
|
568
|
+
* @param m1 - Client proof M1 (Base64).
|
|
569
|
+
* @param ctx - SRP context.
|
|
570
|
+
* @returns Server proof M2 as Base64 string.
|
|
571
|
+
* @throws If verification fails or input is invalid.
|
|
572
|
+
*/
|
|
573
|
+
async verifySrpProof(sessionState, a, m1, ctx) {
|
|
574
|
+
const A = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(a));
|
|
575
|
+
const M1_client = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(m1));
|
|
576
|
+
const b = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(sessionState.privateKeyB));
|
|
577
|
+
const v = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(sessionState.verifier));
|
|
578
|
+
const B = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(sessionState.publicKeyB));
|
|
579
|
+
if (v <= 0n)
|
|
580
|
+
throw new Error("The verifier is corrupted");
|
|
581
|
+
if (A % ctx.N === 0n)
|
|
582
|
+
throw new Error("Incorrect value of A");
|
|
583
|
+
if (A <= 0n || A >= ctx.N)
|
|
584
|
+
throw new Error("Invalid A (out of range)");
|
|
585
|
+
const u = await SrpEncoding.hashModuli(ctx, A, B);
|
|
586
|
+
if (u === 0n)
|
|
587
|
+
throw new Error("Error in calculating the parameter u");
|
|
588
|
+
const vU = SecurityUtils.expMod(v, u, ctx.N);
|
|
589
|
+
const S = SecurityUtils.expMod(A * vU % ctx.N, b, ctx.N);
|
|
590
|
+
const sessionKeyK = await SrpEncoding.computeSessionKey(ctx, S);
|
|
591
|
+
const M1_server = await SrpEncoding.computeM1(ctx, A, B, sessionKeyK);
|
|
592
|
+
const m1ServerBytes = SrpEncoding.toHashBytes(ctx, M1_server);
|
|
593
|
+
const m1ClientBytes = SrpEncoding.toHashBytes(ctx, M1_client);
|
|
594
|
+
if (!SecurityUtils.fixedTimeEquals(m1ServerBytes, m1ClientBytes))
|
|
595
|
+
throw new Error("Invalid password");
|
|
596
|
+
const M2_server = await SrpEncoding.computeM2(ctx, A, M1_client, sessionKeyK);
|
|
597
|
+
return SecurityUtils.toBase64(SrpEncoding.toHashBytes(ctx, M2_server));
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
// src/srp/srp-group.ts
|
|
602
|
+
var SrpGroup = /* @__PURE__ */ ((SrpGroup2) => {
|
|
603
|
+
SrpGroup2[SrpGroup2["Rfc5054_1024"] = 1] = "Rfc5054_1024";
|
|
604
|
+
SrpGroup2[SrpGroup2["Rfc5054_1536"] = 2] = "Rfc5054_1536";
|
|
605
|
+
SrpGroup2[SrpGroup2["Rfc5054_2048"] = 3] = "Rfc5054_2048";
|
|
606
|
+
SrpGroup2[SrpGroup2["Rfc5054_3072"] = 4] = "Rfc5054_3072";
|
|
607
|
+
SrpGroup2[SrpGroup2["Rfc5054_4096"] = 5] = "Rfc5054_4096";
|
|
608
|
+
SrpGroup2[SrpGroup2["Rfc5054_6144"] = 6] = "Rfc5054_6144";
|
|
609
|
+
SrpGroup2[SrpGroup2["Rfc5054_8192"] = 7] = "Rfc5054_8192";
|
|
610
|
+
SrpGroup2[SrpGroup2["Custom"] = 99] = "Custom";
|
|
611
|
+
return SrpGroup2;
|
|
612
|
+
})(SrpGroup || {});
|
|
613
|
+
|
|
614
|
+
// src/srp/srp-group-params.ts
|
|
615
|
+
var SrpGroupParams = class {
|
|
616
|
+
/**
|
|
617
|
+
* Returns the prime modulus N for the given SRP group.
|
|
618
|
+
* @param group - SRP group identifier.
|
|
619
|
+
* @returns N as a bigint.
|
|
620
|
+
*/
|
|
621
|
+
static getN(group) {
|
|
622
|
+
switch (group) {
|
|
623
|
+
case 1 /* Rfc5054_1024 */:
|
|
624
|
+
return BigInt("0xEEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3");
|
|
625
|
+
case 2 /* Rfc5054_1536 */:
|
|
626
|
+
return BigInt("0x9DEF3CAFB939277AB1F12A8617A47BBBDBA51DF499AC4C80BEEEA9614B19CC4D5F4F5F556E27CBDE51C6A94BE4607A291558903BA0D0F84380B655BB9A22E8DCDF028A7CEC67F0D08134B1C8B97989149B609E0BE3BAB63D47548381DBC5B1FC764E3F4B53DD9DA1158BFD3E2B9C8CF56EDF019539349627DB2FD53D24B7C48665772E437D6C7F8CE442734AF7CCB7AE837C264AE3A9BEB87F8A2FE9B8B5292E5A021FFF5E91479E8CE7A28C2442C6F315180F93499A234DCF76E3FED135F9BB");
|
|
627
|
+
case 3 /* Rfc5054_2048 */:
|
|
628
|
+
return BigInt("0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73");
|
|
629
|
+
case 4 /* Rfc5054_3072 */:
|
|
630
|
+
return BigInt("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF");
|
|
631
|
+
case 5 /* Rfc5054_4096 */:
|
|
632
|
+
return BigInt("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199FFFFFFFFFFFFFFFF");
|
|
633
|
+
case 6 /* Rfc5054_6144 */:
|
|
634
|
+
return BigInt("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C93402849236C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AACC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF");
|
|
635
|
+
case 7 /* Rfc5054_8192 */:
|
|
636
|
+
return BigInt("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C93402849236C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AACC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E438777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F5683423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD922222E04A4037C0713EB57A81A23F0C73473FC646CEA306B4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A364597E899A0255DC164F31CC50846851DF9AB48195DED7EA1B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E7160C980DD98EDD3DFFFFFFFFFFFFFFFFF");
|
|
637
|
+
default:
|
|
638
|
+
throw new Error("Unsupported group");
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Returns the generator g for the given SRP group.
|
|
643
|
+
* @param group - SRP group identifier.
|
|
644
|
+
* @returns g as a bigint (2, 5, or 19).
|
|
645
|
+
*/
|
|
646
|
+
static getG(group) {
|
|
647
|
+
switch (group) {
|
|
648
|
+
case 1 /* Rfc5054_1024 */:
|
|
649
|
+
return BigInt(2);
|
|
650
|
+
case 2 /* Rfc5054_1536 */:
|
|
651
|
+
return BigInt(2);
|
|
652
|
+
case 3 /* Rfc5054_2048 */:
|
|
653
|
+
return BigInt(2);
|
|
654
|
+
case 4 /* Rfc5054_3072 */:
|
|
655
|
+
return BigInt(5);
|
|
656
|
+
case 5 /* Rfc5054_4096 */:
|
|
657
|
+
return BigInt(5);
|
|
658
|
+
case 6 /* Rfc5054_6144 */:
|
|
659
|
+
return BigInt(5);
|
|
660
|
+
case 7 /* Rfc5054_8192 */:
|
|
661
|
+
return BigInt(19);
|
|
662
|
+
default:
|
|
663
|
+
throw new Error("Unsupported group");
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
// src/srp/srp-options.ts
|
|
669
|
+
var SrpOptions = class {
|
|
670
|
+
constructor() {
|
|
671
|
+
/** Diffie-Hellman group. Default Rfc5054_3072 (g=5). */
|
|
672
|
+
this.group = 4 /* Rfc5054_3072 */;
|
|
673
|
+
/** Hash algorithm for SRP computations. Default SHA-256. */
|
|
674
|
+
this.hashAlgorithmName = "SHA-256";
|
|
675
|
+
/** Salt size in bytes. Default 32. */
|
|
676
|
+
this.saltSize = 32;
|
|
677
|
+
}
|
|
678
|
+
/** Prime modulus N for the selected group. */
|
|
679
|
+
get N() {
|
|
680
|
+
return SrpGroupParams.getN(this.group);
|
|
681
|
+
}
|
|
682
|
+
/** Generator g for the selected group (2, 5, or 19). */
|
|
683
|
+
get g() {
|
|
684
|
+
return SrpGroupParams.getG(this.group);
|
|
685
|
+
}
|
|
686
|
+
/** Byte length of N (ceil(bitLength / 8)). */
|
|
687
|
+
get modulusSize() {
|
|
688
|
+
return Math.floor((this.N.toString(2).length + 7) / 8);
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* Computes the multiplier parameter k = H(PAD(N) || PAD(g)) (RFC 5054, 2.5.3).
|
|
692
|
+
* @returns k as a bigint.
|
|
693
|
+
*/
|
|
694
|
+
async computeK() {
|
|
695
|
+
const nBytes = SecurityUtils.bigIntToFixedBytes(this.N, this.modulusSize);
|
|
696
|
+
const gBytes = SecurityUtils.bigIntToFixedBytes(this.g, this.modulusSize);
|
|
697
|
+
const combined = new Uint8Array(nBytes.length + gBytes.length);
|
|
698
|
+
combined.set(nBytes, 0);
|
|
699
|
+
combined.set(gBytes, nBytes.length);
|
|
700
|
+
const hash = await crypto.subtle.digest(this.hashAlgorithmName, combined);
|
|
701
|
+
return SecurityUtils.bytesToBigInt(new Uint8Array(hash));
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
// src/srp/srp-context-factory.ts
|
|
706
|
+
var SrpContextFactory = class {
|
|
707
|
+
/**
|
|
708
|
+
* Creates an SRP context with the specified group.
|
|
709
|
+
* @param group - SRP group (default Rfc5054_3072).
|
|
710
|
+
* @returns A ready-to-use SrpContext.
|
|
711
|
+
*/
|
|
712
|
+
static async create(group = 4 /* Rfc5054_3072 */) {
|
|
713
|
+
const opts = new SrpOptions();
|
|
714
|
+
opts.group = group;
|
|
715
|
+
opts.hashAlgorithmName = group === 5 /* Rfc5054_4096 */ || group === 6 /* Rfc5054_6144 */ || group === 7 /* Rfc5054_8192 */ ? "SHA-384" : "SHA-256";
|
|
716
|
+
return {
|
|
717
|
+
N: opts.N,
|
|
718
|
+
g: opts.g,
|
|
719
|
+
k: await opts.computeK(),
|
|
720
|
+
modulusSize: opts.modulusSize,
|
|
721
|
+
hashAlgorithmName: opts.hashAlgorithmName,
|
|
722
|
+
hashSize: opts.hashAlgorithmName === "SHA-256" ? 32 : opts.hashAlgorithmName === "SHA-384" ? 48 : 64
|
|
723
|
+
};
|
|
239
724
|
}
|
|
240
725
|
};
|
|
241
726
|
|
|
242
|
-
export { CryptoService, KeyDerivationService, SecurityUtils,
|
|
727
|
+
export { AesGcmOptions, CryptoProfile, CryptoProfileRegistry, CryptoService, CryptoVersion, KdfOptions, KeyDerivationService, SecurityConstants, SecurityUtils, SrpClientService, SrpContextFactory, SrpEncoding, SrpGroup, SrpGroupParams, SrpOptions, SrpServerService, SupportedHashAlgorithms };
|
|
243
728
|
//# sourceMappingURL=index.js.map
|
|
244
729
|
//# sourceMappingURL=index.js.map
|