@crossdyne/security 0.5.0-beta.2 → 2.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.cts CHANGED
@@ -1,17 +1,26 @@
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
 
13
20
  declare const SupportedHashAlgorithms: ReadonlyArray<HashAlgorithm>;
14
21
 
22
+ type SupportedAlgorithm = 'AES-GCM';
23
+
15
24
  /**
16
25
  * Supported cryptographic profile versions.
17
26
  * V1: baseline (PBKDF2-HMAC-SHA256, AES-256-GCM, 12-byte nonce, 16-byte tag).
@@ -30,24 +39,37 @@ declare class CryptoService {
30
39
  /**
31
40
  * Encrypts a serializable object to a Base64 string.
32
41
  * @param dataModel - Object or Uint8Array to encrypt.
33
- * @param key - AES-256 key (32 bytes).
42
+ * @param key - A non-extractable CryptoKey obtained via {@link CryptoService.importKey}.
34
43
  * @returns Base64-encoded ciphertext with prepended nonce.
35
44
  */
36
- encryptData<T>(dataModel: T, key: Uint8Array, version?: CryptoVersion): Promise<string>;
45
+ encryptData<T>(dataModel: T, key: CryptoKey, version?: CryptoVersion): Promise<string>;
37
46
  /**
38
47
  * Decrypts a Base64-encoded ciphertext back to the original object.
39
48
  * @param encryptedBase64 - The encrypted data.
40
- * @param key - AES-256 key (32 bytes).
49
+ * @param key - A non-extractable CryptoKey obtained via {@link CryptoService.importKey}.
41
50
  * @returns Deserialized object, or null if input is empty.
42
51
  * @throws If authentication tag mismatch or corrupted data.
43
52
  */
44
- decryptData<T>(encryptedBase64: string, key: Uint8Array, isBytes?: boolean): Promise<T | null>;
53
+ decryptData<T>(encryptedBase64: string, key: CryptoKey, isBytes?: boolean): Promise<T | null>;
45
54
  /**
46
55
  * Generates cryptographically secure random bytes.
47
56
  * @param length - Number of bytes (default 32).
48
57
  * @returns Uint8Array of random bytes.
49
58
  */
50
59
  generateRandomBytes: (length?: number) => Uint8Array;
60
+ /**
61
+ * Imports raw key bytes into a non-extractable CryptoKey based on the crypto profile version.
62
+ *
63
+ * The resulting key cannot be exported back to raw bytes (extractable: false),
64
+ * ensuring that sensitive key material does not persist in JavaScript-accessible memory.
65
+ * Callers should securely wipe the original raw key buffer immediately after import.
66
+ *
67
+ * @param rawKey - Raw key bytes (e.g. a Data Encryption Key).
68
+ * @param version - Crypto profile version that determines the algorithm and parameters.
69
+ * @param usages - Allowed key operations (e.g. ['encrypt', 'decrypt']).
70
+ * @returns A non-extractable CryptoKey bound to the algorithm defined by the profile.
71
+ */
72
+ importKey(rawKey: Uint8Array, version: CryptoVersion, usages: KeyUsage[]): Promise<CryptoKey>;
51
73
  }
52
74
 
53
75
  /**
@@ -70,58 +92,47 @@ declare class KeyDerivationService {
70
92
  }
71
93
 
72
94
  /**
73
- * AES-GCM encryption options: nonce size, tag size, and optional AAD.
74
- * Mutable builder-style; call {@link build} to validate.
95
+ * Immutable AES-GCM configuration. All parameters are validated at creation.
96
+ *
97
+ * @remarks
98
+ * Do not construct manually. Use versioned presets such as {@link AesGcmOptions.V1}
99
+ * or the {@link AesGcmOptions.create} factory for custom (non-standard) configurations.
75
100
  */
76
101
  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);
102
+ /** Nonce size in bytes. Must equal 12 (NIST SP 800-38D). */
103
+ readonly nonceSize: number;
104
+ /** Authentication tag size in bytes. Allowed range: 12–16. */
105
+ readonly tagSize: number;
106
+ private constructor();
87
107
  /** Validates that {@link tagSize} is in the allowed range. */
88
108
  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
109
  /**
94
- * Fluent setter for {@link associatedData}.
95
- * Accepts a byte array or a UTF-8 string (encoded internally).
110
+ * V1 preset: nonce=12, tag=16, no AAD.
111
+ * These exact values are frozen for all V1-encrypted payloads.
96
112
  */
97
- withAssociatedData(aad: Uint8Array | string | undefined): this;
98
- /** Validates and returns this instance. */
99
- build(): AesGcmOptions;
113
+ static readonly V1: AesGcmOptions;
100
114
  }
101
115
 
102
116
  /**
103
- * PBKDF2 key derivation options: iterations count and hash algorithm.
104
- * Mutable builder-style; call {@link build} to validate.
117
+ * Immutable PBKDF2/HKDF configuration. All parameters are validated at creation.
118
+ *
119
+ * @remarks
120
+ * Do not construct manually. Use versioned presets such as {@link KdfOptions.V1}
121
+ * or the {@link KdfOptions.create} factory for custom (non-standard) configurations.
105
122
  */
106
123
  declare class KdfOptions {
107
- private _pbkdf2Iterations;
108
- private _hashAlgorithm;
109
- /** PBKDF2 iteration count (minimum 100_000). */
110
- get pbkdf2Iterations(): number;
111
- set pbkdf2Iterations(v: number);
112
- /** Hash algorithm used by PBKDF2. Must be one of {@link SupportedHashAlgorithms}. */
113
- get hashAlgorithm(): HashAlgorithm;
114
- set hashAlgorithm(v: HashAlgorithm);
124
+ /** PBKDF2 iteration count. Must be at least 100_000. */
125
+ readonly pbkdf2Iterations: number;
126
+ /** Hash algorithm used by PBKDF2 and HKDF. Supported: SHA-256, SHA-384, SHA-512. */
127
+ readonly hashAlgorithm: HashAlgorithm;
128
+ private constructor();
115
129
  /** Validates iterations and hash algorithm. */
116
130
  validate(): void;
117
- /** Default preset: SHA-256, 600_000 iterations. */
118
- static get default(): KdfOptions;
119
- /** Fluent setter for {@link pbkdf2Iterations}. */
120
- withPbkdf2Iterations(i: number): this;
121
- /** Fluent setter for {@link hashAlgorithm}. */
122
- withHashAlgorithm(h: HashAlgorithm): this;
123
- /** Validates and returns this instance. */
124
- build(): KdfOptions;
131
+ /**
132
+ * V1 preset: SHA-256, 600_000 iterations.
133
+ * These exact values are frozen for all V1-derived keys.
134
+ */
135
+ static readonly V1: KdfOptions;
125
136
  }
126
137
 
127
138
  /**
@@ -131,6 +142,7 @@ declare class KdfOptions {
131
142
  declare class CryptoProfile {
132
143
  /** Protocol version, influencing KDF defaults, cipher modes, and serialization. */
133
144
  readonly version: CryptoVersion;
145
+ readonly algorithmName: SupportedAlgorithm;
134
146
  /** Key derivation parameters (iterations, hash algorithm). */
135
147
  readonly kdfOptions: KdfOptions;
136
148
  /** AES-GCM encryption parameters (nonce size, tag size, AAD). */
@@ -141,6 +153,7 @@ declare class CryptoProfile {
141
153
  */
142
154
  constructor(params: {
143
155
  version: CryptoVersion;
156
+ algorithmName: SupportedAlgorithm;
144
157
  kdfOptions: KdfOptions;
145
158
  aesGcmOptions: AesGcmOptions;
146
159
  });
@@ -151,9 +164,8 @@ declare class CryptoProfile {
151
164
  * Returns a fresh profile per call.
152
165
  */
153
166
  declare class CryptoProfileRegistry {
167
+ private static readonly V1_PROFILE;
154
168
  static getProfile(version: CryptoVersion): CryptoProfile;
155
- /** Latest supported profile (currently V1). */
156
- static get latest(): CryptoProfile;
157
169
  }
158
170
 
159
171
  /**
@@ -416,4 +428,4 @@ declare class SrpEncoding {
416
428
  private static hash;
417
429
  }
418
430
 
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 };
431
+ export { AesGcmOptions, CryptoProfile, CryptoProfileRegistry, CryptoService, CryptoVersion, type HashAlgorithm, KdfOptions, KeyDerivationService, SecurityConstants, SecurityUtils, SrpClientService, type SrpContext, SrpContextFactory, SrpEncoding, SrpGroup, SrpGroupParams, SrpKeyDerivationService, SrpOptions, SrpServerService, type SrpSessionState, type SupportedAlgorithm, SupportedHashAlgorithms };
package/dist/index.d.ts CHANGED
@@ -1,17 +1,26 @@
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
 
13
20
  declare const SupportedHashAlgorithms: ReadonlyArray<HashAlgorithm>;
14
21
 
22
+ type SupportedAlgorithm = 'AES-GCM';
23
+
15
24
  /**
16
25
  * Supported cryptographic profile versions.
17
26
  * V1: baseline (PBKDF2-HMAC-SHA256, AES-256-GCM, 12-byte nonce, 16-byte tag).
@@ -30,24 +39,37 @@ declare class CryptoService {
30
39
  /**
31
40
  * Encrypts a serializable object to a Base64 string.
32
41
  * @param dataModel - Object or Uint8Array to encrypt.
33
- * @param key - AES-256 key (32 bytes).
42
+ * @param key - A non-extractable CryptoKey obtained via {@link CryptoService.importKey}.
34
43
  * @returns Base64-encoded ciphertext with prepended nonce.
35
44
  */
36
- encryptData<T>(dataModel: T, key: Uint8Array, version?: CryptoVersion): Promise<string>;
45
+ encryptData<T>(dataModel: T, key: CryptoKey, version?: CryptoVersion): Promise<string>;
37
46
  /**
38
47
  * Decrypts a Base64-encoded ciphertext back to the original object.
39
48
  * @param encryptedBase64 - The encrypted data.
40
- * @param key - AES-256 key (32 bytes).
49
+ * @param key - A non-extractable CryptoKey obtained via {@link CryptoService.importKey}.
41
50
  * @returns Deserialized object, or null if input is empty.
42
51
  * @throws If authentication tag mismatch or corrupted data.
43
52
  */
44
- decryptData<T>(encryptedBase64: string, key: Uint8Array, isBytes?: boolean): Promise<T | null>;
53
+ decryptData<T>(encryptedBase64: string, key: CryptoKey, isBytes?: boolean): Promise<T | null>;
45
54
  /**
46
55
  * Generates cryptographically secure random bytes.
47
56
  * @param length - Number of bytes (default 32).
48
57
  * @returns Uint8Array of random bytes.
49
58
  */
50
59
  generateRandomBytes: (length?: number) => Uint8Array;
60
+ /**
61
+ * Imports raw key bytes into a non-extractable CryptoKey based on the crypto profile version.
62
+ *
63
+ * The resulting key cannot be exported back to raw bytes (extractable: false),
64
+ * ensuring that sensitive key material does not persist in JavaScript-accessible memory.
65
+ * Callers should securely wipe the original raw key buffer immediately after import.
66
+ *
67
+ * @param rawKey - Raw key bytes (e.g. a Data Encryption Key).
68
+ * @param version - Crypto profile version that determines the algorithm and parameters.
69
+ * @param usages - Allowed key operations (e.g. ['encrypt', 'decrypt']).
70
+ * @returns A non-extractable CryptoKey bound to the algorithm defined by the profile.
71
+ */
72
+ importKey(rawKey: Uint8Array, version: CryptoVersion, usages: KeyUsage[]): Promise<CryptoKey>;
51
73
  }
52
74
 
53
75
  /**
@@ -70,58 +92,47 @@ declare class KeyDerivationService {
70
92
  }
71
93
 
72
94
  /**
73
- * AES-GCM encryption options: nonce size, tag size, and optional AAD.
74
- * Mutable builder-style; call {@link build} to validate.
95
+ * Immutable AES-GCM configuration. All parameters are validated at creation.
96
+ *
97
+ * @remarks
98
+ * Do not construct manually. Use versioned presets such as {@link AesGcmOptions.V1}
99
+ * or the {@link AesGcmOptions.create} factory for custom (non-standard) configurations.
75
100
  */
76
101
  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);
102
+ /** Nonce size in bytes. Must equal 12 (NIST SP 800-38D). */
103
+ readonly nonceSize: number;
104
+ /** Authentication tag size in bytes. Allowed range: 12–16. */
105
+ readonly tagSize: number;
106
+ private constructor();
87
107
  /** Validates that {@link tagSize} is in the allowed range. */
88
108
  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
109
  /**
94
- * Fluent setter for {@link associatedData}.
95
- * Accepts a byte array or a UTF-8 string (encoded internally).
110
+ * V1 preset: nonce=12, tag=16, no AAD.
111
+ * These exact values are frozen for all V1-encrypted payloads.
96
112
  */
97
- withAssociatedData(aad: Uint8Array | string | undefined): this;
98
- /** Validates and returns this instance. */
99
- build(): AesGcmOptions;
113
+ static readonly V1: AesGcmOptions;
100
114
  }
101
115
 
102
116
  /**
103
- * PBKDF2 key derivation options: iterations count and hash algorithm.
104
- * Mutable builder-style; call {@link build} to validate.
117
+ * Immutable PBKDF2/HKDF configuration. All parameters are validated at creation.
118
+ *
119
+ * @remarks
120
+ * Do not construct manually. Use versioned presets such as {@link KdfOptions.V1}
121
+ * or the {@link KdfOptions.create} factory for custom (non-standard) configurations.
105
122
  */
106
123
  declare class KdfOptions {
107
- private _pbkdf2Iterations;
108
- private _hashAlgorithm;
109
- /** PBKDF2 iteration count (minimum 100_000). */
110
- get pbkdf2Iterations(): number;
111
- set pbkdf2Iterations(v: number);
112
- /** Hash algorithm used by PBKDF2. Must be one of {@link SupportedHashAlgorithms}. */
113
- get hashAlgorithm(): HashAlgorithm;
114
- set hashAlgorithm(v: HashAlgorithm);
124
+ /** PBKDF2 iteration count. Must be at least 100_000. */
125
+ readonly pbkdf2Iterations: number;
126
+ /** Hash algorithm used by PBKDF2 and HKDF. Supported: SHA-256, SHA-384, SHA-512. */
127
+ readonly hashAlgorithm: HashAlgorithm;
128
+ private constructor();
115
129
  /** Validates iterations and hash algorithm. */
116
130
  validate(): void;
117
- /** Default preset: SHA-256, 600_000 iterations. */
118
- static get default(): KdfOptions;
119
- /** Fluent setter for {@link pbkdf2Iterations}. */
120
- withPbkdf2Iterations(i: number): this;
121
- /** Fluent setter for {@link hashAlgorithm}. */
122
- withHashAlgorithm(h: HashAlgorithm): this;
123
- /** Validates and returns this instance. */
124
- build(): KdfOptions;
131
+ /**
132
+ * V1 preset: SHA-256, 600_000 iterations.
133
+ * These exact values are frozen for all V1-derived keys.
134
+ */
135
+ static readonly V1: KdfOptions;
125
136
  }
126
137
 
127
138
  /**
@@ -131,6 +142,7 @@ declare class KdfOptions {
131
142
  declare class CryptoProfile {
132
143
  /** Protocol version, influencing KDF defaults, cipher modes, and serialization. */
133
144
  readonly version: CryptoVersion;
145
+ readonly algorithmName: SupportedAlgorithm;
134
146
  /** Key derivation parameters (iterations, hash algorithm). */
135
147
  readonly kdfOptions: KdfOptions;
136
148
  /** AES-GCM encryption parameters (nonce size, tag size, AAD). */
@@ -141,6 +153,7 @@ declare class CryptoProfile {
141
153
  */
142
154
  constructor(params: {
143
155
  version: CryptoVersion;
156
+ algorithmName: SupportedAlgorithm;
144
157
  kdfOptions: KdfOptions;
145
158
  aesGcmOptions: AesGcmOptions;
146
159
  });
@@ -151,9 +164,8 @@ declare class CryptoProfile {
151
164
  * Returns a fresh profile per call.
152
165
  */
153
166
  declare class CryptoProfileRegistry {
167
+ private static readonly V1_PROFILE;
154
168
  static getProfile(version: CryptoVersion): CryptoProfile;
155
- /** Latest supported profile (currently V1). */
156
- static get latest(): CryptoProfile;
157
169
  }
158
170
 
159
171
  /**
@@ -416,4 +428,4 @@ declare class SrpEncoding {
416
428
  private static hash;
417
429
  }
418
430
 
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 };
431
+ export { AesGcmOptions, CryptoProfile, CryptoProfileRegistry, CryptoService, CryptoVersion, type HashAlgorithm, KdfOptions, KeyDerivationService, SecurityConstants, SecurityUtils, SrpClientService, type SrpContext, SrpContextFactory, SrpEncoding, SrpGroup, SrpGroupParams, SrpKeyDerivationService, SrpOptions, SrpServerService, type SrpSessionState, type SupportedAlgorithm, SupportedHashAlgorithms };