@digitaldefiance/ecies-lib 1.0.24 → 1.0.25

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.
Files changed (48) hide show
  1. package/README.md +50 -1
  2. package/dist/constants.d.ts +0 -13
  3. package/dist/constants.d.ts.map +1 -1
  4. package/dist/constants.js +3 -164
  5. package/dist/constants.js.map +1 -1
  6. package/dist/defaults.d.ts +37 -0
  7. package/dist/defaults.d.ts.map +1 -0
  8. package/dist/defaults.js +323 -0
  9. package/dist/defaults.js.map +1 -0
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +1 -0
  13. package/dist/index.js.map +1 -1
  14. package/dist/interfaces/constants.d.ts +2 -29
  15. package/dist/interfaces/constants.d.ts.map +1 -1
  16. package/dist/interfaces/defaults.d.ts +35 -0
  17. package/dist/interfaces/defaults.d.ts.map +1 -0
  18. package/dist/interfaces/defaults.js +2 -0
  19. package/dist/interfaces/defaults.js.map +1 -0
  20. package/dist/interfaces/index.d.ts +1 -1
  21. package/dist/interfaces/index.d.ts.map +1 -1
  22. package/dist/interfaces/index.js +1 -1
  23. package/dist/interfaces/index.js.map +1 -1
  24. package/dist/member.js +1 -1
  25. package/dist/member.js.map +1 -1
  26. package/dist/regexes.d.ts +3 -0
  27. package/dist/regexes.d.ts.map +1 -0
  28. package/dist/regexes.js +3 -0
  29. package/dist/regexes.js.map +1 -0
  30. package/dist/services/aes-gcm.js +1 -1
  31. package/dist/services/aes-gcm.js.map +1 -1
  32. package/dist/services/ecies/crypto-core.js +1 -1
  33. package/dist/services/ecies/crypto-core.js.map +1 -1
  34. package/dist/services/ecies/multi-recipient.js +1 -1
  35. package/dist/services/ecies/multi-recipient.js.map +1 -1
  36. package/dist/services/ecies/service.js +6 -6
  37. package/dist/services/ecies/service.js.map +1 -1
  38. package/dist/services/ecies/single-recipient.js +1 -1
  39. package/dist/services/ecies/single-recipient.js.map +1 -1
  40. package/dist/services/password-login.js +1 -1
  41. package/dist/services/password-login.js.map +1 -1
  42. package/dist/services/pbkdf2.js +1 -1
  43. package/dist/services/pbkdf2.js.map +1 -1
  44. package/dist/types/deep-partial.d.ts +4 -0
  45. package/dist/types/deep-partial.d.ts.map +1 -0
  46. package/dist/types/deep-partial.js +2 -0
  47. package/dist/types/deep-partial.js.map +1 -0
  48. package/package.json +1 -1
package/README.md CHANGED
@@ -207,6 +207,50 @@ const result = await customPbkdf2Service.deriveKeyFromPasswordWithProfileAsync(
207
207
 
208
208
  This design allows for dependency injection of PBKDF2 profiles while maintaining backward compatibility with the default configurations.
209
209
 
210
+ ## Runtime configuration registry
211
+
212
+ Many applications need different cryptographic trade-offs for different surfaces—e.g., a login form that prioritizes speed versus an administrative workflow that prefers extreme iteration counts. The library ships a registry that lets you register, retrieve, and extend immutable configuration profiles without mutating the global defaults.
213
+
214
+ ```ts
215
+ import {
216
+ DefaultsRegistry,
217
+ registerRuntimeConfiguration,
218
+ getRuntimeConfiguration,
219
+ ECIESService,
220
+ Pbkdf2Service,
221
+ } from '@digitaldefiance/ecies-lib';
222
+
223
+ // 1. Register two named profiles
224
+ registerRuntimeConfiguration('security-first', {
225
+ PBKDF2: {
226
+ ITERATIONS_PER_SECOND: 3_000_000,
227
+ },
228
+ });
229
+
230
+ registerRuntimeConfiguration(
231
+ 'performance-first',
232
+ {
233
+ PBKDF2: {
234
+ ITERATIONS_PER_SECOND: 250_000,
235
+ },
236
+ },
237
+ { baseKey: DefaultsRegistry.DEFAULT_KEY },
238
+ );
239
+
240
+ // 2. Spin up services that honor those profiles
241
+ const secureDefaults = getRuntimeConfiguration('security-first');
242
+ const secureEcies = new ECIESService(undefined, secureDefaults.ECIES);
243
+ const securePbkdf2 = new Pbkdf2Service(engine, secureDefaults.PBKDF2_PROFILES, secureDefaults.ECIES, secureDefaults.PBKDF2);
244
+
245
+ const perfDefaults = getRuntimeConfiguration('performance-first');
246
+ const perfEcies = new ECIESService(undefined, perfDefaults.ECIES);
247
+
248
+ // 3. Optional: create throwaway profiles without registering them
249
+ const temporaryDefaults = DefaultsRegistry.create({ BcryptRounds: 8 });
250
+ ```
251
+
252
+ Every profile returned by the registry is deeply frozen and validated so the low-level invariants (public key sizes, checksum parameters, etc.) stay consistent. Use `clearRuntimeConfigurations()` in tests to reset to the library defaults.
253
+
210
254
  ## Secure primitives & value objects
211
255
 
212
256
  - `SecureString` / `SecureBuffer`: auto-zero, opt-in disposal, and helper methods for dealing with sensitive material.
@@ -235,7 +279,7 @@ try {
235
279
 
236
280
  ## Project structure
237
281
 
238
- ```
282
+ ```text
239
283
  packages/digitaldefiance-ecies-lib/
240
284
  ├─ src/
241
285
  │ ├─ services/ # ECIES, AES-GCM, PBKDF2, password login
@@ -301,6 +345,11 @@ MIT © Digital Defiance
301
345
 
302
346
  ## ChangeLog
303
347
 
348
+ ### v1.0.25: Rework configuration system again
349
+
350
+ - Sun Oct 12 2025 21:02:00 GMT-0700 (Pacific Daylight Time)
351
+ - Rework various services to support user-provided configurations
352
+
304
353
  ### v1.0.24: Rework pbdkf2 services, and other things and provide ways of overriding constants
305
354
 
306
355
  - Sun Oct 12 2025 18:25:00 GMT-0700 (Pacific Daylight Time)
@@ -1,8 +1,4 @@
1
- import { IChecksumConsts } from './interfaces';
2
1
  import { IConstants } from './interfaces/constants';
3
- import { IECIESConstants } from './interfaces/ecies-consts';
4
- import { IPBkdf2Consts } from './interfaces/pbkdf2-consts';
5
- import { Pbkdf2Profiles } from './pbkdf2-profiles';
6
2
  export declare const UINT8_SIZE: number;
7
3
  export declare const UINT16_SIZE: number;
8
4
  export declare const UINT16_MAX: number;
@@ -14,14 +10,5 @@ export declare const UINT64_MAX: bigint;
14
10
  * Standard size of a UUID v4 in bytes.
15
11
  */
16
12
  export declare const GUID_SIZE: number;
17
- /**
18
- * Constants for checksum operations
19
- * These values are critical for data integrity and MUST NOT be changed
20
- * in an already established system as it will break all existing checksums.
21
- */
22
- export declare const CHECKSUM: IChecksumConsts;
23
- export declare const PBKDF2: IPBkdf2Consts;
24
- export declare const PBKDF2_PROFILES: Pbkdf2Profiles;
25
- export declare const ECIES: IECIESConstants;
26
13
  export declare const Constants: IConstants;
27
14
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD,eAAO,MAAM,UAAU,EAAE,MAAmB,CAAC;AAC7C,eAAO,MAAM,WAAW,EAAE,MAAmB,CAAC;AAC9C,eAAO,MAAM,UAAU,EAAE,MAAuB,CAAC;AACjD,eAAO,MAAM,WAAW,EAAE,MAAmB,CAAC;AAC9C,eAAO,MAAM,UAAU,EAAE,MAA4B,CAAC;AACtD,eAAO,MAAM,WAAW,EAAE,MAAmB,CAAC;AAC9C,eAAO,MAAM,UAAU,EAAE,MAAuC,CAAC;AACjE;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MAAoB,CAAC;AAE7C;;;;GAIG;AACH,eAAO,MAAM,QAAQ,EAAE,eAYZ,CAAC;AAEZ,eAAO,MAAM,MAAM,EAAE,aAQV,CAAC;AAEZ,eAAO,MAAM,eAAe,EAAE,cAmBnB,CAAC;AAyBZ,eAAO,MAAM,KAAK,EAAE,eAoElB,CAAC;AAGH,eAAO,MAAM,SAAS,EAAE,UA8Cb,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD,eAAO,MAAM,UAAU,EAAE,MAAmB,CAAC;AAC7C,eAAO,MAAM,WAAW,EAAE,MAAmB,CAAC;AAC9C,eAAO,MAAM,UAAU,EAAE,MAAuB,CAAC;AACjD,eAAO,MAAM,WAAW,EAAE,MAAmB,CAAC;AAC9C,eAAO,MAAM,UAAU,EAAE,MAA4B,CAAC;AACtD,eAAO,MAAM,WAAW,EAAE,MAAmB,CAAC;AAC9C,eAAO,MAAM,UAAU,EAAE,MAAuC,CAAC;AACjE;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MAAoB,CAAC;AAI7C,eAAO,MAAM,SAAS,EAAE,UAiBb,CAAC"}
package/dist/constants.js CHANGED
@@ -1,8 +1,5 @@
1
1
  import { ObjectId } from 'bson';
2
- import { ECIESErrorTypeEnum } from './enumerations';
3
- import { Pbkdf2ProfileEnum } from './enumerations/pbkdf2-profile';
4
- import { ECIESError } from './errors/ecies';
5
- import { getCompatibleEciesEngine } from './i18n-setup';
2
+ import { MNEMONIC_REGEX, PASSWORD_REGEX } from './regexes';
6
3
  export const UINT8_SIZE = 1;
7
4
  export const UINT16_SIZE = 2;
8
5
  export const UINT16_MAX = 65535;
@@ -14,122 +11,6 @@ export const UINT64_MAX = 18446744073709551615n;
14
11
  * Standard size of a UUID v4 in bytes.
15
12
  */
16
13
  export const GUID_SIZE = 16;
17
- /**
18
- * Constants for checksum operations
19
- * These values are critical for data integrity and MUST NOT be changed
20
- * in an already established system as it will break all existing checksums.
21
- */
22
- export const CHECKSUM = Object.freeze({
23
- /** Default hash bits for SHA3 */
24
- SHA3_DEFAULT_HASH_BITS: 512,
25
- /** Length of a SHA3 checksum buffer in bytes */
26
- SHA3_BUFFER_LENGTH: 64,
27
- /** algorithm to use for checksum */
28
- ALGORITHM: 'sha3-512',
29
- /** encoding to use for checksum */
30
- ENCODING: 'hex',
31
- });
32
- export const PBKDF2 = Object.freeze({
33
- ALGORITHM: 'SHA-256',
34
- SALT_BYTES: 32,
35
- /**
36
- * Number of pbkdf2 iterations per second when hashing a password.
37
- * This is the high-security default for user login operations.
38
- */
39
- ITERATIONS_PER_SECOND: 1304000,
40
- });
41
- export const PBKDF2_PROFILES = Object.freeze({
42
- [Pbkdf2ProfileEnum.BROWSER_PASSWORD]: Object.freeze({
43
- hashBytes: 32,
44
- saltBytes: 64,
45
- iterations: 2000000,
46
- algorithm: 'SHA-512',
47
- }),
48
- [Pbkdf2ProfileEnum.HIGH_SECURITY]: Object.freeze({
49
- hashBytes: 64,
50
- saltBytes: 32,
51
- iterations: 5000000,
52
- algorithm: 'SHA-256',
53
- }),
54
- [Pbkdf2ProfileEnum.TEST_FAST]: Object.freeze({
55
- hashBytes: 32,
56
- saltBytes: 64,
57
- iterations: 1000,
58
- algorithm: 'SHA-512',
59
- }),
60
- });
61
- const ECIES_SYMMETRIC_KEY_SIZE = 32;
62
- const ECIES_PUBLIC_KEY_LENGTH = 65;
63
- const ECIES_RAW_PUBLIC_KEY_LENGTH = 64;
64
- const ECIES_IV_SIZE = 16;
65
- const ECIES_AUTH_TAG_SIZE = 16;
66
- const ECIES_MULTIPLE_RECIPIENT_ID_SIZE = 16;
67
- // Define the expected value for SIMPLE.FIXED_OVERHEAD_SIZE
68
- const expectedSimpleOverhead = UINT8_SIZE + ECIES_PUBLIC_KEY_LENGTH + ECIES_IV_SIZE + ECIES_AUTH_TAG_SIZE;
69
- // Define the expected value for MULTIPLE.FIXED_OVERHEAD_SIZE
70
- // Includes: type (1) + IV (16) + auth tag (16) = 33 (no CRC, AES-GCM provides authentication)
71
- const expectedMultipleOverhead = UINT8_SIZE + ECIES_IV_SIZE + ECIES_AUTH_TAG_SIZE;
72
- // Update ENCRYPTED_KEY_SIZE to match Simple encryption (no CRC)
73
- const expectedMultipleEncryptedKeySize = ECIES_PUBLIC_KEY_LENGTH +
74
- ECIES_IV_SIZE +
75
- ECIES_AUTH_TAG_SIZE +
76
- ECIES_SYMMETRIC_KEY_SIZE;
77
- export const ECIES = Object.freeze({
78
- /** The elliptic curve to use for all ECDSA operations */
79
- CURVE_NAME: 'secp256k1',
80
- /** The primary key derivation path for HD wallets */
81
- PRIMARY_KEY_DERIVATION_PATH: "m/44'/60'/0'/0/0",
82
- SYMMETRIC_ALGORITHM_CONFIGURATION: 'aes-256-gcm',
83
- /** Length of ECDSA signatures in bytes */
84
- SIGNATURE_SIZE: 64,
85
- /** Length of raw public keys in bytes (without 0x04 prefix) */
86
- RAW_PUBLIC_KEY_LENGTH: ECIES_RAW_PUBLIC_KEY_LENGTH,
87
- /** Length of public keys in bytes (with 0x04 prefix) */
88
- PUBLIC_KEY_LENGTH: ECIES_PUBLIC_KEY_LENGTH,
89
- PUBLIC_KEY_MAGIC: 0x04,
90
- /** Mnemonic strength in bits. This will produce a 32-bit key for ECDSA */
91
- MNEMONIC_STRENGTH: 256,
92
- /** Symmetric encryption algorithm configuration */
93
- SYMMETRIC: Object.freeze({
94
- ALGORITHM: 'aes',
95
- MODE: 'gcm',
96
- KEY_BITS: 256,
97
- KEY_SIZE: ECIES_SYMMETRIC_KEY_SIZE, // KEY_BITS / 8
98
- }),
99
- IV_SIZE: ECIES_IV_SIZE,
100
- AUTH_TAG_SIZE: ECIES_AUTH_TAG_SIZE,
101
- MAX_RAW_DATA_SIZE: 9007199254740991, // 2^53 - 1 (max safe integer for JS)
102
- /**
103
- * Message encrypts without data length or crc
104
- */
105
- SIMPLE: Object.freeze({
106
- FIXED_OVERHEAD_SIZE: expectedSimpleOverhead, // type (1) + public key (65) + IV (16) + auth tag (16)
107
- DATA_LENGTH_SIZE: 0,
108
- }),
109
- /**
110
- * Message encrypts with data length but no CRC (AES-GCM provides authentication)
111
- */
112
- SINGLE: Object.freeze({
113
- FIXED_OVERHEAD_SIZE: 106, // type (1) + public key (65) + IV (16) + auth tag (16) + data length (8)
114
- DATA_LENGTH_SIZE: 8,
115
- }),
116
- /**
117
- * Message encrypts for multiple recipients
118
- */
119
- MULTIPLE: Object.freeze({
120
- FIXED_OVERHEAD_SIZE: expectedMultipleOverhead, // type (1) + IV (16) + auth tag (16), no CRC
121
- ENCRYPTED_KEY_SIZE: expectedMultipleEncryptedKeySize, // 129
122
- MAX_RECIPIENTS: 65535,
123
- RECIPIENT_ID_SIZE: ECIES_MULTIPLE_RECIPIENT_ID_SIZE,
124
- RECIPIENT_COUNT_SIZE: 2,
125
- DATA_LENGTH_SIZE: 8,
126
- }),
127
- ENCRYPTION_TYPE: Object.freeze({
128
- SIMPLE: 33,
129
- SINGLE: 66,
130
- MULTIPLE: 99,
131
- }),
132
- });
133
14
  const objectIdLength = new ObjectId().toHexString().length / 2;
134
15
  export const Constants = Object.freeze({
135
16
  /**
@@ -145,52 +26,10 @@ export const Constants = Object.freeze({
145
26
  UINT64_MAX: UINT64_MAX,
146
27
  HEX_RADIX: 16,
147
28
  GUID_SIZE: GUID_SIZE,
148
- CHECKSUM: CHECKSUM,
149
- ECIES: ECIES,
150
- PBKDF2: PBKDF2,
151
- PBKDF2_PROFILES: PBKDF2_PROFILES,
152
- /**
153
- * Number of rounds for bcrypt hashing. Higher values increase security but also consume more CPU resources.
154
- */
155
- BcryptRounds: 10,
156
- /**
157
- * Minimum password length
158
- */
159
- PasswordMinLength: 8,
160
- /**
161
- * The regular expression for valid passwords.
162
- */
163
- PasswordRegex: /^(?=.*[A-Za-z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?])[A-Za-z\d!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]{8,}$/,
164
- /**
165
- * The regular expression for valid mnemonic phrases.
166
- * BIP39 - supports 12, 15, 18, 21, or 24 word mnemonics
167
- */
168
- MnemonicRegex: /^(?:\w+\s){11}\w+$|^(?:\w+\s){14}\w+$|^(?:\w+\s){17}\w+$|^(?:\w+\s){20}\w+$|^(?:\w+\s){23}\w+$/i,
169
- /**
170
- * Matches a 64-character hexadecimal string (SHA-256).
171
- */
172
- HmacRegex: /^[a-f0-9]{64}$/,
29
+ PasswordRegex: PASSWORD_REGEX,
30
+ MnemonicRegex: MNEMONIC_REGEX,
173
31
  });
174
- if (CHECKSUM.SHA3_BUFFER_LENGTH !== CHECKSUM.SHA3_DEFAULT_HASH_BITS / 8 ||
175
- CHECKSUM.SHA3_BUFFER_LENGTH !== CHECKSUM.SHA3_DEFAULT_HASH_BITS / 8) {
176
- throw new Error('Invalid checksum constants');
177
- }
178
32
  if (objectIdLength !== 12) {
179
33
  console.warn('ObjectID length may have changed, breaking encryption', objectIdLength);
180
34
  }
181
- if (ECIES.MULTIPLE.ENCRYPTED_KEY_SIZE !== 129) {
182
- throw new ECIESError(ECIESErrorTypeEnum.InvalidECIESMultipleEncryptedKeySize, getCompatibleEciesEngine());
183
- }
184
- if (ECIES.PUBLIC_KEY_LENGTH !== ECIES.RAW_PUBLIC_KEY_LENGTH + 1) {
185
- throw new ECIESError(ECIESErrorTypeEnum.InvalidECIESPublicKeyLength, getCompatibleEciesEngine());
186
- }
187
- if (ECIES.MULTIPLE.RECIPIENT_COUNT_SIZE !== UINT16_SIZE) {
188
- throw new ECIESError(ECIESErrorTypeEnum.InvalidECIESMultipleRecipientCountSize, getCompatibleEciesEngine());
189
- }
190
- if (ECIES.MULTIPLE.DATA_LENGTH_SIZE !== UINT64_SIZE) {
191
- throw new ECIESError(ECIESErrorTypeEnum.InvalidECIESMultipleDataLengthSize, getCompatibleEciesEngine());
192
- }
193
- if (ECIES.MULTIPLE.RECIPIENT_ID_SIZE !== GUID_SIZE) {
194
- throw new ECIESError(ECIESErrorTypeEnum.InvalidECIESMultipleRecipientIdSize, getCompatibleEciesEngine());
195
- }
196
35
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,UAAU,EA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UAAU,EAAE,UAAU;IACtB,WAAW,EAAE,WAAW;IACxB,UAAU,EAAE,UAAU;IACtB,SAAS,EAAE,EAAW;IACtB,SAAS,EAAE,SAAS;IACpB,aAAa,EAAE,cAAc;IAC7B,aAAa,EAAE,cAAc;CACrB,CAAC,CAAC;AAEZ,IAAI,cAAc,KAAK,EAAE,EAAE,CAAC;IAC1B,OAAO,CAAC,IAAI,CACV,uDAAuD,EACvD,cAAc,CACf,CAAC;AACJ,CAAC"}
@@ -0,0 +1,37 @@
1
+ import { IChecksumConsts } from './interfaces';
2
+ import { IDefaults, IDefaultsOverrides } from './interfaces/defaults';
3
+ import { IECIESConstants } from './interfaces/ecies-consts';
4
+ import { IPBkdf2Consts } from './interfaces/pbkdf2-consts';
5
+ import { Pbkdf2Profiles } from './pbkdf2-profiles';
6
+ /**
7
+ * Constants for checksum operations
8
+ * These values are critical for data integrity and MUST NOT be changed
9
+ * in an already established system as it will break all existing checksums.
10
+ */
11
+ export declare const CHECKSUM: IChecksumConsts;
12
+ export declare const PBKDF2: IPBkdf2Consts;
13
+ export declare const PBKDF2_PROFILES: Pbkdf2Profiles;
14
+ export declare const ECIES: IECIESConstants;
15
+ export declare const Defaults: IDefaults;
16
+ export type ConfigurationKey = string | symbol;
17
+ export declare function createRuntimeConfiguration(overrides?: IDefaultsOverrides, base?: IDefaults): IDefaults;
18
+ export declare class DefaultsRegistry {
19
+ static readonly DEFAULT_KEY: ConfigurationKey;
20
+ static listKeys(): ConfigurationKey[];
21
+ static has(key: ConfigurationKey): boolean;
22
+ static get(key?: ConfigurationKey): IDefaults;
23
+ static create(overrides?: IDefaultsOverrides, baseKey?: ConfigurationKey): IDefaults;
24
+ static register(key: ConfigurationKey, configOrOverrides?: IDefaultsOverrides | IDefaults, options?: {
25
+ baseKey?: ConfigurationKey;
26
+ }): IDefaults;
27
+ static unregister(key: ConfigurationKey): boolean;
28
+ static clear(): void;
29
+ }
30
+ export declare function getRuntimeConfiguration(key?: ConfigurationKey): IDefaults;
31
+ export declare function registerRuntimeConfiguration(key: ConfigurationKey, configOrOverrides?: IDefaultsOverrides | IDefaults, options?: {
32
+ baseKey?: ConfigurationKey;
33
+ }): IDefaults;
34
+ export declare function unregisterRuntimeConfiguration(key: ConfigurationKey): boolean;
35
+ export declare function clearRuntimeConfigurations(): void;
36
+ export { PASSWORD_REGEX, MNEMONIC_REGEX } from './regexes';
37
+ //# sourceMappingURL=defaults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEtE,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAGnD;;;;GAIG;AACH,eAAO,MAAM,QAAQ,EAAE,eAYZ,CAAC;AAEZ,eAAO,MAAM,MAAM,EAAE,aAQV,CAAC;AAEZ,eAAO,MAAM,eAAe,EAAE,cAmBnB,CAAC;AAyBZ,eAAO,MAAM,KAAK,EAAE,eAoElB,CAAC;AAEH,eAAO,MAAM,QAAQ,EAAE,SA0BZ,CAAC;AAEZ,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,MAAM,CAAC;AAsK/C,wBAAgB,0BAA0B,CACxC,SAAS,CAAC,EAAE,kBAAkB,EAC9B,IAAI,GAAE,SAAoB,GACzB,SAAS,CAKX;AAED,qBAAa,gBAAgB;IAC3B,gBAAuB,WAAW,mBAA6B;WAEjD,QAAQ,IAAI,gBAAgB,EAAE;WAI9B,GAAG,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO;WAInC,GAAG,CAAC,GAAG,GAAE,gBAA4C,GAAG,SAAS;WAOjE,MAAM,CAClB,SAAS,CAAC,EAAE,kBAAkB,EAC9B,OAAO,GAAE,gBAA4C,GACpD,SAAS;WAKE,QAAQ,CACpB,GAAG,EAAE,gBAAgB,EACrB,iBAAiB,CAAC,EAAE,kBAAkB,GAAG,SAAS,EAClD,OAAO,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,gBAAgB,CAAA;KAAE,GACvC,SAAS;WAgBE,UAAU,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO;WAO1C,KAAK,IAAI,IAAI;CAI5B;AAED,wBAAgB,uBAAuB,CACrC,GAAG,GAAE,gBAA4C,GAChD,SAAS,CAEX;AAED,wBAAgB,4BAA4B,CAC1C,GAAG,EAAE,gBAAgB,EACrB,iBAAiB,CAAC,EAAE,kBAAkB,GAAG,SAAS,EAClD,OAAO,CAAC,EAAE;IAAE,OAAO,CAAC,EAAE,gBAAgB,CAAA;CAAE,GACvC,SAAS,CAEX;AAED,wBAAgB,8BAA8B,CAC5C,GAAG,EAAE,gBAAgB,GACpB,OAAO,CAET;AAED,wBAAgB,0BAA0B,IAAI,IAAI,CAEjD;AAED,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC"}
@@ -0,0 +1,323 @@
1
+ import { GUID_SIZE, UINT16_SIZE, UINT64_SIZE, UINT8_SIZE } from './constants';
2
+ import { ECIESErrorTypeEnum } from './enumerations';
3
+ import { Pbkdf2ProfileEnum } from './enumerations/pbkdf2-profile';
4
+ import { ECIESError } from './errors/ecies';
5
+ import { getCompatibleEciesEngine } from './i18n-setup';
6
+ import { MNEMONIC_REGEX, PASSWORD_REGEX } from './regexes';
7
+ /**
8
+ * Constants for checksum operations
9
+ * These values are critical for data integrity and MUST NOT be changed
10
+ * in an already established system as it will break all existing checksums.
11
+ */
12
+ export const CHECKSUM = Object.freeze({
13
+ /** Default hash bits for SHA3 */
14
+ SHA3_DEFAULT_HASH_BITS: 512,
15
+ /** Length of a SHA3 checksum buffer in bytes */
16
+ SHA3_BUFFER_LENGTH: 64,
17
+ /** algorithm to use for checksum */
18
+ ALGORITHM: 'sha3-512',
19
+ /** encoding to use for checksum */
20
+ ENCODING: 'hex',
21
+ });
22
+ export const PBKDF2 = Object.freeze({
23
+ ALGORITHM: 'SHA-256',
24
+ SALT_BYTES: 32,
25
+ /**
26
+ * Number of pbkdf2 iterations per second when hashing a password.
27
+ * This is the high-security default for user login operations.
28
+ */
29
+ ITERATIONS_PER_SECOND: 1304000,
30
+ });
31
+ export const PBKDF2_PROFILES = Object.freeze({
32
+ [Pbkdf2ProfileEnum.BROWSER_PASSWORD]: Object.freeze({
33
+ hashBytes: 32,
34
+ saltBytes: 64,
35
+ iterations: 2000000,
36
+ algorithm: 'SHA-512',
37
+ }),
38
+ [Pbkdf2ProfileEnum.HIGH_SECURITY]: Object.freeze({
39
+ hashBytes: 64,
40
+ saltBytes: 32,
41
+ iterations: 5000000,
42
+ algorithm: 'SHA-256',
43
+ }),
44
+ [Pbkdf2ProfileEnum.TEST_FAST]: Object.freeze({
45
+ hashBytes: 32,
46
+ saltBytes: 64,
47
+ iterations: 1000,
48
+ algorithm: 'SHA-512',
49
+ }),
50
+ });
51
+ const ECIES_SYMMETRIC_KEY_SIZE = 32;
52
+ const ECIES_PUBLIC_KEY_LENGTH = 65;
53
+ const ECIES_RAW_PUBLIC_KEY_LENGTH = 64;
54
+ const ECIES_IV_SIZE = 16;
55
+ const ECIES_AUTH_TAG_SIZE = 16;
56
+ const ECIES_MULTIPLE_RECIPIENT_ID_SIZE = 16;
57
+ // Define the expected value for SIMPLE.FIXED_OVERHEAD_SIZE
58
+ const expectedSimpleOverhead = UINT8_SIZE + ECIES_PUBLIC_KEY_LENGTH + ECIES_IV_SIZE + ECIES_AUTH_TAG_SIZE;
59
+ // Define the expected value for MULTIPLE.FIXED_OVERHEAD_SIZE
60
+ // Includes: type (1) + IV (16) + auth tag (16) = 33 (no CRC, AES-GCM provides authentication)
61
+ const expectedMultipleOverhead = UINT8_SIZE + ECIES_IV_SIZE + ECIES_AUTH_TAG_SIZE;
62
+ // Update ENCRYPTED_KEY_SIZE to match Simple encryption (no CRC)
63
+ const expectedMultipleEncryptedKeySize = ECIES_PUBLIC_KEY_LENGTH +
64
+ ECIES_IV_SIZE +
65
+ ECIES_AUTH_TAG_SIZE +
66
+ ECIES_SYMMETRIC_KEY_SIZE;
67
+ export const ECIES = Object.freeze({
68
+ /** The elliptic curve to use for all ECDSA operations */
69
+ CURVE_NAME: 'secp256k1',
70
+ /** The primary key derivation path for HD wallets */
71
+ PRIMARY_KEY_DERIVATION_PATH: "m/44'/60'/0'/0/0",
72
+ SYMMETRIC_ALGORITHM_CONFIGURATION: 'aes-256-gcm',
73
+ /** Length of ECDSA signatures in bytes */
74
+ SIGNATURE_SIZE: 64,
75
+ /** Length of raw public keys in bytes (without 0x04 prefix) */
76
+ RAW_PUBLIC_KEY_LENGTH: ECIES_RAW_PUBLIC_KEY_LENGTH,
77
+ /** Length of public keys in bytes (with 0x04 prefix) */
78
+ PUBLIC_KEY_LENGTH: ECIES_PUBLIC_KEY_LENGTH,
79
+ PUBLIC_KEY_MAGIC: 0x04,
80
+ /** Mnemonic strength in bits. This will produce a 32-bit key for ECDSA */
81
+ MNEMONIC_STRENGTH: 256,
82
+ /** Symmetric encryption algorithm configuration */
83
+ SYMMETRIC: Object.freeze({
84
+ ALGORITHM: 'aes',
85
+ MODE: 'gcm',
86
+ KEY_BITS: 256,
87
+ KEY_SIZE: ECIES_SYMMETRIC_KEY_SIZE, // KEY_BITS / 8
88
+ }),
89
+ IV_SIZE: ECIES_IV_SIZE,
90
+ AUTH_TAG_SIZE: ECIES_AUTH_TAG_SIZE,
91
+ MAX_RAW_DATA_SIZE: 9007199254740991, // 2^53 - 1 (max safe integer for JS)
92
+ /**
93
+ * Message encrypts without data length or crc
94
+ */
95
+ SIMPLE: Object.freeze({
96
+ FIXED_OVERHEAD_SIZE: expectedSimpleOverhead, // type (1) + public key (65) + IV (16) + auth tag (16)
97
+ DATA_LENGTH_SIZE: 0,
98
+ }),
99
+ /**
100
+ * Message encrypts with data length but no CRC (AES-GCM provides authentication)
101
+ */
102
+ SINGLE: Object.freeze({
103
+ FIXED_OVERHEAD_SIZE: 106, // type (1) + public key (65) + IV (16) + auth tag (16) + data length (8)
104
+ DATA_LENGTH_SIZE: 8,
105
+ }),
106
+ /**
107
+ * Message encrypts for multiple recipients
108
+ */
109
+ MULTIPLE: Object.freeze({
110
+ FIXED_OVERHEAD_SIZE: expectedMultipleOverhead, // type (1) + IV (16) + auth tag (16), no CRC
111
+ ENCRYPTED_KEY_SIZE: expectedMultipleEncryptedKeySize, // 129
112
+ MAX_RECIPIENTS: 65535,
113
+ RECIPIENT_ID_SIZE: ECIES_MULTIPLE_RECIPIENT_ID_SIZE,
114
+ RECIPIENT_COUNT_SIZE: 2,
115
+ DATA_LENGTH_SIZE: 8,
116
+ }),
117
+ ENCRYPTION_TYPE: Object.freeze({
118
+ SIMPLE: 33,
119
+ SINGLE: 66,
120
+ MULTIPLE: 99,
121
+ }),
122
+ });
123
+ export const Defaults = Object.freeze({
124
+ CHECKSUM: CHECKSUM,
125
+ ECIES: ECIES,
126
+ PBKDF2: PBKDF2,
127
+ PBKDF2_PROFILES: PBKDF2_PROFILES,
128
+ /**
129
+ * Number of rounds for bcrypt hashing. Higher values increase security but also consume more CPU resources.
130
+ */
131
+ BcryptRounds: 10,
132
+ /**
133
+ * Minimum password length
134
+ */
135
+ PasswordMinLength: 8,
136
+ /**
137
+ * The regular expression for valid passwords.
138
+ */
139
+ PasswordRegex: PASSWORD_REGEX,
140
+ /**
141
+ * The regular expression for valid mnemonic phrases.
142
+ * BIP39 - supports 12, 15, 18, 21, or 24 word mnemonics
143
+ */
144
+ MnemonicRegex: MNEMONIC_REGEX,
145
+ /**
146
+ * Matches a 64-character hexadecimal string (SHA-256).
147
+ */
148
+ HmacRegex: /^[a-f0-9]{64}$/,
149
+ });
150
+ const DEFAULT_CONFIGURATION_KEY = Symbol.for('digitaldefiance.ecies.defaults.default');
151
+ function isPlainObject(value) {
152
+ if (value === null) {
153
+ return false;
154
+ }
155
+ if (typeof value !== 'object') {
156
+ return false;
157
+ }
158
+ if (Array.isArray(value)) {
159
+ return false;
160
+ }
161
+ if (value instanceof RegExp || value instanceof Date) {
162
+ return false;
163
+ }
164
+ return Object.getPrototypeOf(value) === Object.prototype;
165
+ }
166
+ function deepClone(input) {
167
+ if (input === null) {
168
+ return input;
169
+ }
170
+ if (Array.isArray(input)) {
171
+ return input.map((item) => deepClone(item));
172
+ }
173
+ if (input instanceof RegExp) {
174
+ return new RegExp(input.source, input.flags);
175
+ }
176
+ if (input instanceof Date) {
177
+ return new Date(input.getTime());
178
+ }
179
+ if (isPlainObject(input)) {
180
+ const result = {};
181
+ for (const [key, value] of Object.entries(input)) {
182
+ result[key] = deepClone(value);
183
+ }
184
+ return result;
185
+ }
186
+ return input;
187
+ }
188
+ function applyOverrides(target, overrides) {
189
+ if (!overrides) {
190
+ return target;
191
+ }
192
+ for (const [key, overrideValue] of Object.entries(overrides)) {
193
+ const typedKey = key;
194
+ if (overrideValue === undefined) {
195
+ continue;
196
+ }
197
+ const currentValue = target[typedKey];
198
+ if (isPlainObject(currentValue) && isPlainObject(overrideValue)) {
199
+ target[typedKey] = applyOverrides(currentValue, overrideValue);
200
+ }
201
+ else {
202
+ target[typedKey] = deepClone(overrideValue);
203
+ }
204
+ }
205
+ return target;
206
+ }
207
+ function deepFreeze(value) {
208
+ if (value === null || typeof value !== 'object') {
209
+ return value;
210
+ }
211
+ if (Object.isFrozen(value)) {
212
+ return value;
213
+ }
214
+ Object.freeze(value);
215
+ for (const property of Object.getOwnPropertyNames(value)) {
216
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
217
+ const nestedValue = value[property];
218
+ deepFreeze(nestedValue);
219
+ }
220
+ return value;
221
+ }
222
+ function computeMultipleEncryptedKeySize(ecies) {
223
+ return (ecies.PUBLIC_KEY_LENGTH +
224
+ ecies.IV_SIZE +
225
+ ecies.AUTH_TAG_SIZE +
226
+ ecies.SYMMETRIC.KEY_SIZE);
227
+ }
228
+ function validateDefaults(config) {
229
+ const checksum = config.CHECKSUM;
230
+ const ecies = config.ECIES;
231
+ if (checksum.SHA3_BUFFER_LENGTH !== checksum.SHA3_DEFAULT_HASH_BITS / 8 ||
232
+ checksum.SHA3_BUFFER_LENGTH !== checksum.SHA3_DEFAULT_HASH_BITS / 8) {
233
+ throw new Error('Invalid checksum constants');
234
+ }
235
+ const expectedEncryptedKeySize = computeMultipleEncryptedKeySize(ecies);
236
+ if (ecies.MULTIPLE.ENCRYPTED_KEY_SIZE !== expectedEncryptedKeySize) {
237
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidECIESMultipleEncryptedKeySize, getCompatibleEciesEngine());
238
+ }
239
+ if (ecies.PUBLIC_KEY_LENGTH !== ecies.RAW_PUBLIC_KEY_LENGTH + 1) {
240
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidECIESPublicKeyLength, getCompatibleEciesEngine());
241
+ }
242
+ if (ecies.MULTIPLE.RECIPIENT_COUNT_SIZE !== UINT16_SIZE) {
243
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidECIESMultipleRecipientCountSize, getCompatibleEciesEngine());
244
+ }
245
+ if (ecies.MULTIPLE.DATA_LENGTH_SIZE !== UINT64_SIZE) {
246
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidECIESMultipleDataLengthSize, getCompatibleEciesEngine());
247
+ }
248
+ if (ecies.MULTIPLE.RECIPIENT_ID_SIZE !== GUID_SIZE) {
249
+ throw new ECIESError(ECIESErrorTypeEnum.InvalidECIESMultipleRecipientIdSize, getCompatibleEciesEngine());
250
+ }
251
+ }
252
+ validateDefaults(Defaults);
253
+ const configurationRegistry = new Map();
254
+ configurationRegistry.set(DEFAULT_CONFIGURATION_KEY, Defaults);
255
+ function isFullDefaultsConfig(value) {
256
+ if (!isPlainObject(value)) {
257
+ return false;
258
+ }
259
+ const candidate = value;
260
+ return (candidate.CHECKSUM !== undefined &&
261
+ candidate.ECIES !== undefined &&
262
+ candidate.PBKDF2 !== undefined &&
263
+ candidate.PBKDF2_PROFILES !== undefined);
264
+ }
265
+ export function createRuntimeConfiguration(overrides, base = Defaults) {
266
+ const merged = deepClone(base);
267
+ applyOverrides(merged, overrides);
268
+ validateDefaults(merged);
269
+ return deepFreeze(merged);
270
+ }
271
+ export class DefaultsRegistry {
272
+ static DEFAULT_KEY = DEFAULT_CONFIGURATION_KEY;
273
+ static listKeys() {
274
+ return Array.from(configurationRegistry.keys());
275
+ }
276
+ static has(key) {
277
+ return configurationRegistry.has(key);
278
+ }
279
+ static get(key = DEFAULT_CONFIGURATION_KEY) {
280
+ return (configurationRegistry.get(key) ??
281
+ configurationRegistry.get(DEFAULT_CONFIGURATION_KEY));
282
+ }
283
+ static create(overrides, baseKey = DEFAULT_CONFIGURATION_KEY) {
284
+ const baseConfig = DefaultsRegistry.get(baseKey);
285
+ return createRuntimeConfiguration(overrides, baseConfig);
286
+ }
287
+ static register(key, configOrOverrides, options) {
288
+ if (key === DEFAULT_CONFIGURATION_KEY) {
289
+ throw new Error('Cannot overwrite the default configuration');
290
+ }
291
+ const baseKey = options?.baseKey ?? DEFAULT_CONFIGURATION_KEY;
292
+ const baseConfig = DefaultsRegistry.get(baseKey);
293
+ const configuration = isFullDefaultsConfig(configOrOverrides)
294
+ ? createRuntimeConfiguration(undefined, configOrOverrides)
295
+ : createRuntimeConfiguration(configOrOverrides, baseConfig);
296
+ configurationRegistry.set(key, configuration);
297
+ return configuration;
298
+ }
299
+ static unregister(key) {
300
+ if (key === DEFAULT_CONFIGURATION_KEY) {
301
+ return false;
302
+ }
303
+ return configurationRegistry.delete(key);
304
+ }
305
+ static clear() {
306
+ configurationRegistry.clear();
307
+ configurationRegistry.set(DEFAULT_CONFIGURATION_KEY, Defaults);
308
+ }
309
+ }
310
+ export function getRuntimeConfiguration(key = DEFAULT_CONFIGURATION_KEY) {
311
+ return DefaultsRegistry.get(key);
312
+ }
313
+ export function registerRuntimeConfiguration(key, configOrOverrides, options) {
314
+ return DefaultsRegistry.register(key, configOrOverrides, options);
315
+ }
316
+ export function unregisterRuntimeConfiguration(key) {
317
+ return DefaultsRegistry.unregister(key);
318
+ }
319
+ export function clearRuntimeConfigurations() {
320
+ DefaultsRegistry.clear();
321
+ }
322
+ export { PASSWORD_REGEX, MNEMONIC_REGEX } from './regexes';
323
+ //# sourceMappingURL=defaults.js.map