@crossdyne/security 0.5.0-beta.1 → 1.0.0

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