@crossdyne/security 0.4.0-beta.1 → 0.5.0-beta.2

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.d.ts CHANGED
@@ -13,33 +13,13 @@ declare const SecurityConstants: {
13
13
  declare const SupportedHashAlgorithms: ReadonlyArray<HashAlgorithm>;
14
14
 
15
15
  /**
16
- * AES-GCM encryption options: nonce size, tag size, and optional AAD.
17
- * Mutable builder-style; call {@link build} to validate.
16
+ * Supported cryptographic profile versions.
17
+ * V1: baseline (PBKDF2-HMAC-SHA256, AES-256-GCM, 12-byte nonce, 16-byte tag).
18
+ * Append new members sequentially; never change existing values.
18
19
  */
19
- declare class AesGcmOptions {
20
- private _nonceSize;
21
- private _tagSize;
22
- /** Optional Additional Authenticated Data (not encrypted). */
23
- associatedData?: Uint8Array;
24
- /** Nonce size in bytes (must be 12). */
25
- get nonceSize(): number;
26
- set nonceSize(v: number);
27
- /** Tag size in bytes (12–16, default 16). */
28
- get tagSize(): number;
29
- set tagSize(v: number);
30
- /** Validates that {@link tagSize} is in the allowed range. */
31
- validate(): void;
32
- /** Default preset: nonce=12, tag=16, no AAD. */
33
- static get default(): AesGcmOptions;
34
- /** Fluent setter for {@link tagSize}. */
35
- withTagSize(s: number): this;
36
- /**
37
- * Fluent setter for {@link associatedData}.
38
- * Accepts a byte array or a UTF-8 string (encoded internally).
39
- */
40
- withAssociatedData(aad: Uint8Array | string | undefined): this;
41
- /** Validates and returns this instance. */
42
- build(): AesGcmOptions;
20
+ declare enum CryptoVersion {
21
+ /** Version 1 — initial profile. */
22
+ V1 = 1
43
23
  }
44
24
 
45
25
  /**
@@ -51,19 +31,17 @@ declare class CryptoService {
51
31
  * Encrypts a serializable object to a Base64 string.
52
32
  * @param dataModel - Object or Uint8Array to encrypt.
53
33
  * @param key - AES-256 key (32 bytes).
54
- * @param options - AES-GCM configuration; uses default if omitted.
55
34
  * @returns Base64-encoded ciphertext with prepended nonce.
56
35
  */
57
- encryptData<T>(dataModel: T, key: Uint8Array, options?: AesGcmOptions): Promise<string>;
36
+ encryptData<T>(dataModel: T, key: Uint8Array, version?: CryptoVersion): Promise<string>;
58
37
  /**
59
38
  * Decrypts a Base64-encoded ciphertext back to the original object.
60
39
  * @param encryptedBase64 - The encrypted data.
61
40
  * @param key - AES-256 key (32 bytes).
62
- * @param options - AES-GCM configuration; uses default if omitted.
63
41
  * @returns Deserialized object, or null if input is empty.
64
42
  * @throws If authentication tag mismatch or corrupted data.
65
43
  */
66
- decryptData<T>(encryptedBase64: string, key: Uint8Array, options?: AesGcmOptions, isBytes?: boolean): Promise<T | null>;
44
+ decryptData<T>(encryptedBase64: string, key: Uint8Array, isBytes?: boolean): Promise<T | null>;
67
45
  /**
68
46
  * Generates cryptographically secure random bytes.
69
47
  * @param length - Number of bytes (default 32).
@@ -72,6 +50,55 @@ declare class CryptoService {
72
50
  generateRandomBytes: (length?: number) => Uint8Array;
73
51
  }
74
52
 
53
+ /**
54
+ * Two-stage key derivation: PBKDF2 (master key) → HKDF (sub-keys).
55
+ */
56
+ declare class KeyDerivationService {
57
+ /**
58
+ * Derives KEK and Base64 AuthHash. Identity is used as-is in the
59
+ * combined salt string — caller must normalize (trim, lowercase, etc.) before calling.
60
+ * @param identity - User identity (email, username). Must be pre-normalized by caller.
61
+ * @param password - User password.
62
+ * @param salt - Random salt (minimum 16 bytes).
63
+ * @param version - Crypto version for profile selection.
64
+ * @returns Object with `kek` (Uint8Array) and `authHash` (Base64 string).
65
+ */
66
+ deriveKeysFromPassword(identity: string, password: string, salt: Uint8Array, version: CryptoVersion): Promise<{
67
+ kek: Uint8Array;
68
+ authHash: string;
69
+ }>;
70
+ }
71
+
72
+ /**
73
+ * AES-GCM encryption options: nonce size, tag size, and optional AAD.
74
+ * Mutable builder-style; call {@link build} to validate.
75
+ */
76
+ declare class AesGcmOptions {
77
+ private _nonceSize;
78
+ private _tagSize;
79
+ /** Optional Additional Authenticated Data (not encrypted). */
80
+ associatedData?: Uint8Array;
81
+ /** Nonce size in bytes (must be 12). */
82
+ get nonceSize(): number;
83
+ set nonceSize(v: number);
84
+ /** Tag size in bytes (12–16, default 16). */
85
+ get tagSize(): number;
86
+ set tagSize(v: number);
87
+ /** Validates that {@link tagSize} is in the allowed range. */
88
+ validate(): void;
89
+ /** Default preset: nonce=12, tag=16, no AAD. */
90
+ static get default(): AesGcmOptions;
91
+ /** Fluent setter for {@link tagSize}. */
92
+ withTagSize(s: number): this;
93
+ /**
94
+ * Fluent setter for {@link associatedData}.
95
+ * Accepts a byte array or a UTF-8 string (encoded internally).
96
+ */
97
+ withAssociatedData(aad: Uint8Array | string | undefined): this;
98
+ /** Validates and returns this instance. */
99
+ build(): AesGcmOptions;
100
+ }
101
+
75
102
  /**
76
103
  * PBKDF2 key derivation options: iterations count and hash algorithm.
77
104
  * Mutable builder-style; call {@link build} to validate.
@@ -97,44 +124,6 @@ declare class KdfOptions {
97
124
  build(): KdfOptions;
98
125
  }
99
126
 
100
- /**
101
- * Two-stage key derivation: PBKDF2 (master key) → HKDF (sub-keys).
102
- */
103
- declare class KeyDerivationService {
104
- /**
105
- * Derives KEK and Base64 AuthHash. Identity is normalized (trimmed, lowercase).
106
- * @param identity - User identity (email, username).
107
- * @param password - User password.
108
- * @param salt - Random salt.
109
- * @param options - KDF configuration; uses default if omitted.
110
- * @returns Object with `kek` (Uint8Array) and `authHash` (Base64 string).
111
- */
112
- deriveKeysFromPassword(identity: string, password: string, salt: Uint8Array, options?: KdfOptions): Promise<{
113
- kek: Uint8Array;
114
- authHash: string;
115
- }>;
116
- /**
117
- * Derives an SRP-compatible authentication hash (output size = hash output length).
118
- * @param identity - User identity.
119
- * @param password - User password.
120
- * @param salt - Random salt.
121
- * @param srpHashAlgorithm - SRP hash algorithm (SHA-256/384/512).
122
- * @param options - KDF configuration; uses default if omitted.
123
- * @returns Raw hash bytes for use as SRP verifier input (x).
124
- */
125
- deriveAuthHashForSrp(identity: string, password: string, salt: Uint8Array, srpHashAlgorithm: HashAlgorithm, options?: KdfOptions): Promise<Uint8Array>;
126
- }
127
-
128
- /**
129
- * Supported cryptographic profile versions.
130
- * V1: baseline (PBKDF2-HMAC-SHA256, AES-256-GCM, 12-byte nonce, 16-byte tag).
131
- * Append new members sequentially; never change existing values.
132
- */
133
- declare enum CryptoVersion {
134
- /** Version 1 — initial profile. */
135
- V1 = 1
136
- }
137
-
138
127
  /**
139
128
  * Immutable cryptographic profile bundling version, KDF, and AES-GCM settings.
140
129
  * Thread-safe after construction.
@@ -168,45 +157,51 @@ declare class CryptoProfileRegistry {
168
157
  }
169
158
 
170
159
  /**
171
- * Immutable SRP cryptographic context: modulus, generator, multiplier k, hash algorithm, and sizes.
160
+ * SRP-6a Diffie-Hellman groups (RFC 5054).
161
+ * 1024 (~80) deprecated, 1536 (~90) legacy, 2048 (~112) baseline,
162
+ * 3072+ (≥128) preferred. g=2 for ≤2048, g=5 for 3072-6144, g=19 for 8192.
163
+ * Always use {@link SrpGroupParams} to get N and g.
172
164
  */
173
- interface SrpContext {
174
- /** Prime modulus N. */
175
- readonly N: bigint;
176
- /** Generator g. */
177
- readonly g: bigint;
178
- /** Multiplier k = H(PAD(N) || PAD(g)) (RFC 5054, 2.5.3) */
179
- readonly k: bigint;
180
- /** Modulus size in bytes (ceil(bit length / 8)). */
181
- readonly modulusSize: number;
182
- /** Hash algorithm used for SRP computations. */
183
- readonly hashAlgorithmName: HashAlgorithm;
184
- /** Hash output size in bytes (e.g., 32 for SHA-256). */
185
- readonly hashSize: number;
165
+ declare enum SrpGroup {
166
+ /** 1024-bit, g=2, ~80-bit security. Deprecated, legacy only. */
167
+ Rfc5054_1024 = 1,
168
+ /** 1536-bit, g=2, ~90-bit security. Minimum for legacy systems. */
169
+ Rfc5054_1536 = 2,
170
+ /** 2048-bit, g=2, ~112-bit security. Recommended baseline. */
171
+ Rfc5054_2048 = 3,
172
+ /** 3072-bit, g=5, ~128-bit security. Preferred for long-term. */
173
+ Rfc5054_3072 = 4,
174
+ /** 4096-bit, g=5, ~156-bit security. High-security environments. */
175
+ Rfc5054_4096 = 5,
176
+ /** 6144-bit, g=5, ~192-bit security. Specialized high-assurance. */
177
+ Rfc5054_6144 = 6,
178
+ /** 8192-bit, g=19, ~256-bit security. Experimental, extremely slow. */
179
+ Rfc5054_8192 = 7,
180
+ /** User-supplied N and g. Validate safe prime and generator. */
181
+ Custom = 99
186
182
  }
187
183
 
188
184
  /**
189
185
  * Client-side SRP-6a implementation: proof generation, verifier creation, server M2 verification.
190
186
  */
191
187
  declare class SrpClientService {
192
- private readonly keyDerivation;
193
188
  /**
194
189
  * Computes SRP verifier v = g^x mod N from the authentication hash.
195
190
  * @param authHash - Auth hash (Base64).
196
- * @param ctx - SRP context (N, g, hash algorithm, etc.).
191
+ * @param group - SRP group (determines modulus N, generator g, hash).
197
192
  * @returns Verifier as Base64 string.
198
193
  */
199
- generateSrpVerifier(authHash: string, ctx: SrpContext): Promise<string>;
194
+ generateSrpVerifier(authHash: string, group: SrpGroup): Promise<string>;
200
195
  /**
201
196
  * Generates client proof (A, M1, session key S) from server challenge.
202
197
  * @param login - User login.
203
- * @param password - Plaintext password.
204
- * @param saltBase64 - Server salt (URL-safe Base64).
205
- * @param B_base64 - Server public ephemeral B (URL-safe Base64).
206
- * @param ctx - SRP context.
207
- * @returns Object with A, M1, S as Base64 strings.
198
+ * @param authHashBytes - SRP private exponent x as raw bytes (derived from auth hash).
199
+ * @param saltBase64 - Server salt (standard Base64).
200
+ * @param B_base64 - Server public ephemeral B (standard Base64).
201
+ * @param group - SRP group (determines modulus N, generator g, hash).
202
+ * @returns Object with A and M1 as standard Base64; SessionKeyK as raw bytes.
208
203
  */
209
- generateSrpProof(login: string, password: string, saltBase64: string, B_base64: string, ctx: SrpContext): Promise<{
204
+ generateSrpProof(login: string, authHashBytes: Uint8Array<ArrayBufferLike>, saltBase64: string, B_base64: string, group: SrpGroup): Promise<{
210
205
  A: string;
211
206
  M1: string;
212
207
  SessionKeyK: Uint8Array;
@@ -215,12 +210,12 @@ declare class SrpClientService {
215
210
  * Validates the server proof M2 to authenticate the server.
216
211
  * @param A_b64 - Client public A (Base64).
217
212
  * @param M1_b64 - Client proof M1 (Base64).
218
- * @param S_b64 - Session key S (Base64).
213
+ * @param sessionKeyK - Session key K as raw bytes.
219
214
  * @param serverM2_b64 - Server proof M2 (Base64).
220
- * @param ctx - SRP context.
215
+ * @param group - SRP group (determines modulus N, generator g, hash).
221
216
  * @returns True if the server proof is valid.
222
217
  */
223
- verifyServerM2(A_b64: string, M1_b64: string, sessionKeyK: Uint8Array, serverM2_b64: string, ctx: SrpContext): Promise<boolean>;
218
+ verifyServerM2(A_b64: string, M1_b64: string, sessionKeyK: Uint8Array, serverM2_b64: string, group: SrpGroup): Promise<boolean>;
224
219
  }
225
220
 
226
221
  /**
@@ -229,13 +224,13 @@ declare class SrpClientService {
229
224
  interface SrpSessionState {
230
225
  /** User login identifier. */
231
226
  login: string;
232
- /** Server private ephemeral key (Base64). */
227
+ /** Server private ephemeral key (raw bytes). */
233
228
  privateKeyB: Uint8Array;
234
- /** Password verifier (Base64). */
229
+ /** Password verifier v (raw bytes). */
235
230
  verifier: Uint8Array;
236
- /** Server public ephemeral key B (Base64). */
231
+ /** Server public ephemeral key B (raw bytes). */
237
232
  publicKeyB: Uint8Array;
238
- /** User salt s (needed for RFC 5054 M1). */
233
+ /** User salt s (raw bytes). */
239
234
  salt: Uint8Array;
240
235
  }
241
236
  /**
@@ -246,45 +241,39 @@ declare class SrpServerService {
246
241
  * Generates server challenge B and session state from verifier.
247
242
  * @param login - User login.
248
243
  * @param verifierBytes - Stored verifier v as byte array.
249
- * @param ctx - SRP context (hash, N, g, etc.).
250
- * @returns Session state with private b, verifier, and public B.
244
+ * @param salt - User salt (raw bytes).
245
+ * @param group - SRP group (determines modulus N, generator g, hash).
246
+ * @returns Session state with private b, verifier, public B, and salt.
251
247
  */
252
- getSrpChallenge(login: string, verifierBytes: Uint8Array, salt: Uint8Array, ctx: SrpContext): Promise<SrpSessionState>;
248
+ getSrpChallenge(login: string, verifierBytes: Uint8Array, salt: Uint8Array, group: SrpGroup): Promise<SrpSessionState>;
253
249
  /**
254
250
  * Verifies client M1 proof and returns server M2 proof.
255
251
  * @param sessionState - Server session state.
256
252
  * @param a - Client public A (Base64).
257
253
  * @param m1 - Client proof M1 (Base64).
258
- * @param ctx - SRP context.
254
+ * @param group - SRP group (determines modulus N, generator g, hash).
259
255
  * @returns Server proof M2 as Base64 string.
260
256
  * @throws If verification fails or input is invalid.
261
257
  */
262
- verifySrpProof(sessionState: SrpSessionState, a: string, m1: string, ctx: SrpContext): Promise<string>;
258
+ verifySrpProof(sessionState: SrpSessionState, a: string, m1: string, group: SrpGroup): Promise<string>;
263
259
  }
264
260
 
265
261
  /**
266
- * SRP-6a Diffie-Hellman groups (RFC 5054).
267
- * 1024 (~80) deprecated, 1536 (~90) legacy, 2048 (~112) baseline,
268
- * 3072+ (≥128) preferred. g=2 for ≤2048, g=5 for 3072-6144, g=19 for 8192.
269
- * Always use {@link SrpGroupParams} to get N and g.
262
+ * Service for deriving SRP authentication hashes via PBKDF2 → HKDF.
270
263
  */
271
- declare enum SrpGroup {
272
- /** 1024-bit, g=2, ~80-bit security. Deprecated, legacy only. */
273
- Rfc5054_1024 = 1,
274
- /** 1536-bit, g=2, ~90-bit security. Minimum for legacy systems. */
275
- Rfc5054_1536 = 2,
276
- /** 2048-bit, g=2, ~112-bit security. Recommended baseline. */
277
- Rfc5054_2048 = 3,
278
- /** 3072-bit, g=5, ~128-bit security. Preferred for long-term. */
279
- Rfc5054_3072 = 4,
280
- /** 4096-bit, g=5, ~156-bit security. High-security environments. */
281
- Rfc5054_4096 = 5,
282
- /** 6144-bit, g=5, ~192-bit security. Specialized high-assurance. */
283
- Rfc5054_6144 = 6,
284
- /** 8192-bit, g=19, ~256-bit security. Experimental, extremely slow. */
285
- Rfc5054_8192 = 7,
286
- /** User-supplied N and g. Validate safe prime and generator. */
287
- Custom = 99
264
+ declare class SrpKeyDerivationService {
265
+ /**
266
+ * Derives an SRP-compatible authentication hash (output size = hash output length).
267
+ * Identity is used as-is in the combined string — caller must normalize
268
+ * (trim, lowercase, etc.) before calling.
269
+ * @param identity - User identity (email, username). Must be pre-normalized by caller.
270
+ * @param password - User password.
271
+ * @param salt - Random salt (minimum 16 bytes).
272
+ * @param srpGroup - SRP group (determines hash algorithm and modulus).
273
+ * @param version - Crypto version for KDF profile selection.
274
+ * @returns Raw hash bytes for use as SRP verifier input (x).
275
+ */
276
+ deriveAuthHashForSrp(identity: string, password: string, salt: Uint8Array, srpGroup: SrpGroup, version: CryptoVersion): Promise<Uint8Array>;
288
277
  }
289
278
 
290
279
  /**
@@ -329,6 +318,24 @@ declare class SrpGroupParams {
329
318
  static getG(group: SrpGroup): bigint;
330
319
  }
331
320
 
321
+ /**
322
+ * Immutable SRP cryptographic context: modulus, generator, multiplier k, hash algorithm, and sizes.
323
+ */
324
+ interface SrpContext {
325
+ /** Prime modulus N. */
326
+ readonly N: bigint;
327
+ /** Generator g. */
328
+ readonly g: bigint;
329
+ /** Multiplier k = H(PAD(N) || PAD(g)) (RFC 5054, 2.5.3) */
330
+ readonly k: bigint;
331
+ /** Modulus size in bytes (ceil(bit length / 8)). */
332
+ readonly modulusSize: number;
333
+ /** Hash algorithm used for SRP computations. */
334
+ readonly hashAlgorithmName: HashAlgorithm;
335
+ /** Hash output size in bytes (e.g., 32 for SHA-256). */
336
+ readonly hashSize: number;
337
+ }
338
+
332
339
  /**
333
340
  * Factory for creating an {@link SrpContext} from an SRP group.
334
341
  * Automatically selects the hash algorithm (SHA-256 for ≤3072-bit, SHA-384 for 4096+).
@@ -352,12 +359,20 @@ declare class SecurityUtils {
352
359
  static fromBase64(base64: string): Uint8Array;
353
360
  /** Converts big-endian bytes to bigint. */
354
361
  static bytesToBigInt(bytes: Uint8Array): bigint;
355
- /** Converts bigint to fixed-length big-endian bytes (pads/truncates). */
362
+ /** Converts bigint to fixed-length big-endian bytes (pads only; never truncates). */
356
363
  static bigIntToFixedBytes(bn: bigint, length: number): Uint8Array;
357
364
  /** Constant-time comparison of two Uint8Arrays. */
358
365
  static fixedTimeEquals(a: Uint8Array, b: Uint8Array): boolean;
359
- /** Modular exponentiation (base^exp mod mod) using binary exponentiation. */
360
- static expMod(base: bigint, exp: bigint, mod: bigint): bigint;
366
+ /**
367
+ * Async modular exponentiation with event-loop yielding.
368
+ * @param base - The base value.
369
+ * @param exp - The exponent.
370
+ * @param mod - The modulus.
371
+ * @param yieldEvery - Number of iterations before yielding (default 64).
372
+ */
373
+ static expModAsync(base: bigint, exp: bigint, mod: bigint, yieldEvery?: number): Promise<bigint>;
374
+ /** Converts bigint to minimal-length big-endian bytes (no padding). */
375
+ static bigIntToRawBytes(bn: bigint): Uint8Array;
361
376
  }
362
377
 
363
378
  /**
@@ -376,26 +391,29 @@ declare class SrpEncoding {
376
391
  * Follows RFC 5054 / SRP-6a:
377
392
  * - H(N) and H(g) are hashed as modulus-sized values.
378
393
  * - Identity (I) is hashed as raw UTF-8 bytes.
379
- * - A and B are padded to the modulus size before hashing.
394
+ * - A and B are zero-padded to the modulus size before hashing.
380
395
  * - K is the session key (H(S) without padding).
381
396
  *
382
397
  * @param ctx - SRP context containing N, g, hash algorithm, and modulus size.
383
398
  * @param A - Client ephemeral public key.
384
399
  * @param B - Server ephemeral public key.
385
400
  * @param sessionKeyK - Session key K as raw bytes.
386
- * @param identity - User identity (login). Should already be normalized (trimmed / lowercased) by the caller.
401
+ * @param identity - User identity (login). Must be pre-normalized by the caller.
387
402
  * @param salt - User-specific salt bytes.
388
403
  * @returns The M1 proof as raw hash bytes.
389
404
  */
390
405
  static computeM1(ctx: SrpContext, A: bigint, B: bigint, sessionKeyK: Uint8Array, identity: string, salt: Uint8Array): Promise<Uint8Array>;
391
- /** Computes M2 = H(A || M1 || sessionKeyK). */
406
+ /**
407
+ * Computes M2 = H( PAD(A) || M1 || sessionKeyK ).
408
+ * A is zero-padded to the modulus size before hashing.
409
+ */
392
410
  static computeM2(ctx: SrpContext, A: bigint, m1Bytes: Uint8Array, sessionKeyK: Uint8Array): Promise<Uint8Array>;
393
411
  /** Computes session key K = H(S). */
394
412
  static computeSessionKey(ctx: SrpContext, S: bigint): Promise<Uint8Array>;
395
- /** Hashes bytes and returns raw Uint8Array (для M1/M2). */
413
+ /** Hashes concatenated byte arrays and returns raw Uint8Array. */
396
414
  private static computeHash;
397
415
  /** Concatenates byte arrays and returns the hash as bigint. */
398
416
  private static hash;
399
417
  }
400
418
 
401
- export { AesGcmOptions, CryptoProfile, CryptoProfileRegistry, CryptoService, CryptoVersion, type HashAlgorithm, KdfOptions, KeyDerivationService, SecurityConstants, SecurityUtils, SrpClientService, type SrpContext, SrpContextFactory, SrpEncoding, SrpGroup, SrpGroupParams, SrpOptions, SrpServerService, type SrpSessionState, SupportedHashAlgorithms };
419
+ export { AesGcmOptions, CryptoProfile, CryptoProfileRegistry, CryptoService, CryptoVersion, type HashAlgorithm, KdfOptions, KeyDerivationService, SecurityConstants, SecurityUtils, SrpClientService, type SrpContext, SrpContextFactory, SrpEncoding, SrpGroup, SrpGroupParams, SrpKeyDerivationService, SrpOptions, SrpServerService, type SrpSessionState, SupportedHashAlgorithms };