@enbox/crypto 0.0.3 → 0.0.4

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 (91) hide show
  1. package/dist/browser.mjs +1 -1
  2. package/dist/browser.mjs.map +4 -4
  3. package/dist/esm/algorithms/aes-ctr.js +1 -1
  4. package/dist/esm/algorithms/aes-gcm.js +34 -1
  5. package/dist/esm/algorithms/aes-gcm.js.map +1 -1
  6. package/dist/esm/algorithms/aes-kw.js +154 -0
  7. package/dist/esm/algorithms/aes-kw.js.map +1 -0
  8. package/dist/esm/algorithms/ecdsa.js +110 -1
  9. package/dist/esm/algorithms/ecdsa.js.map +1 -1
  10. package/dist/esm/algorithms/eddsa.js +90 -1
  11. package/dist/esm/algorithms/eddsa.js.map +1 -1
  12. package/dist/esm/algorithms/hkdf.js +53 -0
  13. package/dist/esm/algorithms/hkdf.js.map +1 -0
  14. package/dist/esm/algorithms/pbkdf2.js +55 -0
  15. package/dist/esm/algorithms/pbkdf2.js.map +1 -0
  16. package/dist/esm/algorithms/sha-2.js +1 -1
  17. package/dist/esm/algorithms/x25519.js +125 -0
  18. package/dist/esm/algorithms/x25519.js.map +1 -0
  19. package/dist/esm/index.js +5 -0
  20. package/dist/esm/index.js.map +1 -1
  21. package/dist/esm/local-key-manager.js +6 -1
  22. package/dist/esm/local-key-manager.js.map +1 -1
  23. package/dist/esm/primitives/ecies-secp256k1.js +79 -0
  24. package/dist/esm/primitives/ecies-secp256k1.js.map +1 -0
  25. package/dist/esm/primitives/x25519.js +9 -16
  26. package/dist/esm/primitives/x25519.js.map +1 -1
  27. package/dist/esm/utils.js +30 -0
  28. package/dist/esm/utils.js.map +1 -1
  29. package/dist/types/algorithms/aes-ctr.d.ts +1 -1
  30. package/dist/types/algorithms/aes-gcm.d.ts +23 -3
  31. package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
  32. package/dist/types/algorithms/aes-kw.d.ts +129 -0
  33. package/dist/types/algorithms/aes-kw.d.ts.map +1 -0
  34. package/dist/types/algorithms/ecdsa.d.ts +48 -3
  35. package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
  36. package/dist/types/algorithms/eddsa.d.ts +48 -3
  37. package/dist/types/algorithms/eddsa.d.ts.map +1 -1
  38. package/dist/types/algorithms/hkdf.d.ts +35 -0
  39. package/dist/types/algorithms/hkdf.d.ts.map +1 -0
  40. package/dist/types/algorithms/pbkdf2.d.ts +35 -0
  41. package/dist/types/algorithms/pbkdf2.d.ts.map +1 -0
  42. package/dist/types/algorithms/sha-2.d.ts +1 -1
  43. package/dist/types/algorithms/x25519.d.ts +76 -0
  44. package/dist/types/algorithms/x25519.d.ts.map +1 -0
  45. package/dist/types/index.d.ts +5 -0
  46. package/dist/types/index.d.ts.map +1 -1
  47. package/dist/types/local-key-manager.d.ts +4 -4
  48. package/dist/types/local-key-manager.d.ts.map +1 -1
  49. package/dist/types/primitives/ecies-secp256k1.d.ts +53 -0
  50. package/dist/types/primitives/ecies-secp256k1.d.ts.map +1 -0
  51. package/dist/types/primitives/x25519.d.ts +9 -16
  52. package/dist/types/primitives/x25519.d.ts.map +1 -1
  53. package/dist/types/types/crypto-api.d.ts +52 -4
  54. package/dist/types/types/crypto-api.d.ts.map +1 -1
  55. package/dist/types/types/key-converter.d.ts +37 -15
  56. package/dist/types/types/key-converter.d.ts.map +1 -1
  57. package/dist/types/types/key-deriver.d.ts +41 -0
  58. package/dist/types/types/key-deriver.d.ts.map +1 -1
  59. package/dist/types/types/key-io.d.ts +37 -0
  60. package/dist/types/types/key-io.d.ts.map +1 -1
  61. package/dist/types/types/params-direct.d.ts +17 -0
  62. package/dist/types/types/params-direct.d.ts.map +1 -1
  63. package/dist/types/types/params-kms.d.ts +55 -0
  64. package/dist/types/types/params-kms.d.ts.map +1 -1
  65. package/dist/types/utils.d.ts +19 -0
  66. package/dist/types/utils.d.ts.map +1 -1
  67. package/dist/utils.js +1 -1
  68. package/dist/utils.js.map +3 -3
  69. package/package.json +10 -13
  70. package/src/algorithms/aes-ctr.ts +1 -1
  71. package/src/algorithms/aes-gcm.ts +38 -2
  72. package/src/algorithms/aes-kw.ts +182 -0
  73. package/src/algorithms/ecdsa.ts +132 -1
  74. package/src/algorithms/eddsa.ts +108 -1
  75. package/src/algorithms/hkdf.ts +54 -0
  76. package/src/algorithms/pbkdf2.ts +57 -0
  77. package/src/algorithms/sha-2.ts +1 -1
  78. package/src/algorithms/x25519.ts +153 -0
  79. package/src/index.ts +5 -0
  80. package/src/local-key-manager.ts +9 -4
  81. package/src/primitives/ecies-secp256k1.ts +113 -0
  82. package/src/primitives/x25519.ts +9 -16
  83. package/src/types/crypto-api.ts +124 -6
  84. package/src/types/key-converter.ts +33 -7
  85. package/src/types/key-deriver.ts +49 -0
  86. package/src/types/key-io.ts +40 -0
  87. package/src/types/params-direct.ts +21 -0
  88. package/src/types/params-kms.ts +67 -0
  89. package/src/utils.ts +53 -0
  90. package/dist/browser.js +0 -60
  91. package/dist/browser.js.map +0 -7
@@ -40,4 +40,53 @@ export interface KeyDeriver<
40
40
  * @returns A Promise resolving to the derived key in the specified output format.
41
41
  */
42
42
  deriveKey(params: DeriveKeyInput): Promise<DeriveKeyOutput>;
43
+ }
44
+
45
+ /**
46
+ * The `SimpleKeyDeriver` interface provides a single `deriveKey()` method for key derivation,
47
+ * without the `deriveBits()` method that {@link KeyDeriver} includes.
48
+ *
49
+ * This is useful for implementations that only need key derivation (not raw bit derivation).
50
+ */
51
+ export interface SimpleKeyDeriver<
52
+ DeriveKeyInput,
53
+ DeriveKeyOutput,
54
+ > {
55
+ /**
56
+ * Derives a cryptographic key based on the provided input parameters.
57
+ *
58
+ * @param params - The parameters for the key derivation process.
59
+ *
60
+ * @returns A Promise resolving to the derived key in the specified output format.
61
+ */
62
+ deriveKey(params: DeriveKeyInput): Promise<DeriveKeyOutput>;
63
+ }
64
+
65
+ /**
66
+ * The `KeyBytesDeriver` interface provides a method for deriving a byte array using a key
67
+ * derivation algorithm.
68
+ *
69
+ * The `deriveKeyBytes()` method derives cryptographic bits from input data using the specified
70
+ * key derivation algorithm. This interface is designed to support various key derivation
71
+ * algorithms, accommodating different input and output types.
72
+ */
73
+ export interface KeyBytesDeriver<
74
+ DeriveKeyBytesInput,
75
+ DeriveKeyBytesOutput
76
+ > {
77
+ /**
78
+ * Generates a specified number of cryptographic bits from given input parameters.
79
+ *
80
+ * @remarks
81
+ * The `deriveKeyBytes()` method of the {@link KeyBytesDeriver | `KeyBytesDeriver`} interface is
82
+ * used to create cryptographic material such as initialization vectors or keys from various
83
+ * sources. The method takes in parameters specific to the chosen key derivation algorithm and
84
+ * outputs a promise that resolves to a `Uint8Array` containing the derived bits.
85
+ *
86
+ * @param params - The parameters for the key derivation process, specific to the chosen
87
+ * algorithm.
88
+ *
89
+ * @returns A Promise resolving to the derived bits in the specified format.
90
+ */
91
+ deriveKeyBytes(params: DeriveKeyBytesInput): Promise<DeriveKeyBytesOutput>;
43
92
  }
@@ -39,4 +39,44 @@ export interface KeyImporterExporter<
39
39
  * @returns A Promise resolving to the key identifier of the imported key.
40
40
  */
41
41
  importKey(params: ImportKeyInput): Promise<ImportKeyOutput>;
42
+ }
43
+
44
+ /**
45
+ * The `KeyExporter` interface provides a method for exporting cryptographic keys.
46
+ */
47
+ export interface KeyExporter<ExportKeyInput, ExportKeyOutput = Jwk> {
48
+ /**
49
+ * Exports a cryptographic key to an external JWK object.
50
+ *
51
+ * @param params - The parameters for the key export operation.
52
+ *
53
+ * @returns A Promise resolving to the exported key in JWK format.
54
+ */
55
+ exportKey(params: ExportKeyInput): Promise<ExportKeyOutput>;
56
+ }
57
+
58
+ /**
59
+ * The `KeyImporter` interface provides a method for importing cryptographic keys.
60
+ */
61
+ export interface KeyImporter<ImportKeyInput, ImportKeyOutput = void> {
62
+ /**
63
+ * Imports an external key in JWK format.
64
+ *
65
+ * @param params - The parameters for the key import operation.
66
+ *
67
+ * @returns A Promise resolving to the key identifier of the imported key.
68
+ */
69
+ importKey(params: ImportKeyInput): Promise<ImportKeyOutput>;
70
+ }
71
+
72
+ /**
73
+ * The `KeyDeleter` interface provides a method for deleting cryptographic keys.
74
+ */
75
+ export interface KeyDeleter<DeleteKeyInput> {
76
+ /**
77
+ * Deletes a cryptographic key from the key store.
78
+ *
79
+ * @param params - The parameters for the key deletion operation.
80
+ */
81
+ deleteKey(params: DeleteKeyInput): Promise<void>;
42
82
  }
@@ -80,6 +80,27 @@ export interface DeriveKeyParams {
80
80
  derivedKeyParams: unknown
81
81
  }
82
82
 
83
+ /**
84
+ * Parameters for deriving a key from raw byte-based key material.
85
+ *
86
+ * Unlike {@link DeriveKeyParams} which operates on JWK keys, this interface works with raw
87
+ * byte arrays as the base key input, making it suitable for agent-level key derivation where
88
+ * keys originate from passphrases, seed phrases, or other byte-oriented sources.
89
+ */
90
+ export interface DeriveKeyFromBytesParams {
91
+ /** The algorithm identifier. */
92
+ algorithm: string;
93
+
94
+ /** The base key to be used for derivation as a byte array. */
95
+ baseKeyBytes: Uint8Array;
96
+
97
+ /** The algorithm identifier for the derived key. */
98
+ derivedKeyAlgorithm?: string;
99
+
100
+ /** Additional algorithm-specific parameters for key derivation. */
101
+ [key: string]: unknown;
102
+ }
103
+
83
104
  /**
84
105
  * Parameters for derivation of cryptographic byte arrays.
85
106
  */
@@ -153,4 +153,71 @@ export interface KmsUnwrapKeyParams {
153
153
 
154
154
  /** Algorithm to be used for unwrapping. */
155
155
  unwrapAlgorithm: AlgorithmIdentifier;
156
+ }
157
+
158
+ /**
159
+ * Parameters for KMS-based encryption and decryption operations.
160
+ *
161
+ * Intended for use with a Key Management System where the key is referenced by URI.
162
+ */
163
+ export interface KmsCipherParams {
164
+ /** Identifier for the private key in the KMS. */
165
+ keyUri: KeyIdentifier;
166
+
167
+ /** Data to be encrypted or decrypted. */
168
+ data: Uint8Array;
169
+ }
170
+
171
+ /**
172
+ * Parameters for KMS-based derivation of a byte array from a given base key.
173
+ *
174
+ * Intended for use with a Key Management System.
175
+ */
176
+ export interface KmsDeriveKeyBytesParams {
177
+ /** Identifier for the base key used in derivation in the KMS. */
178
+ baseKeyUri: KeyIdentifier;
179
+
180
+ /** The desired length of the derived key in bits. */
181
+ length: number;
182
+ }
183
+
184
+ /**
185
+ * Parameters for KMS-based key unwrapping. Intended for use with a Key Management System where
186
+ * the decryption key is referenced by URI.
187
+ */
188
+ export interface KmsUriUnwrapKeyParams {
189
+ /** Identifier for the private key in the KMS used for decrypting the wrapped key. */
190
+ decryptionKeyUri: KeyIdentifier;
191
+
192
+ /** The wrapped private key as a byte array. */
193
+ wrappedKeyBytes: Uint8Array;
194
+
195
+ /** The algorithm identifier of the key encrypted in `wrappedKeyBytes`. */
196
+ wrappedKeyAlgorithm: string;
197
+
198
+ /** An object defining the algorithm-specific parameters for decrypting the `wrappedKeyBytes`. */
199
+ decryptParams?: unknown;
200
+ }
201
+
202
+ /**
203
+ * Parameters for KMS-based key wrapping. Intended for use with a Key Management System where
204
+ * the encryption key is referenced by URI.
205
+ */
206
+ export interface KmsUriWrapKeyParams {
207
+ /** Identifier for the private key in the KMS used for encrypting the unwrapped key. */
208
+ encryptionKeyUri: KeyIdentifier;
209
+
210
+ /** A {@link Jwk} containing the private key to be wrapped. */
211
+ unwrappedKey: Jwk;
212
+
213
+ /** An object defining the algorithm-specific parameters for encrypting the `unwrappedKey`. */
214
+ encryptParams?: unknown;
215
+ }
216
+
217
+ /**
218
+ * Parameters for KMS-based key deletion. Intended for use with a Key Management System.
219
+ */
220
+ export interface KmsDeleteKeyParams {
221
+ /** Identifier for the key to be deleted in the KMS. */
222
+ keyUri: KeyIdentifier;
156
223
  }
package/src/utils.ts CHANGED
@@ -1,4 +1,7 @@
1
+ import type { Cipher } from './types/cipher.js';
1
2
  import type { Jwk } from './jose/jwk.js';
3
+ import type { KeyWrapper } from './types/key-wrapper.js';
4
+ import type { KeyExporter, KeyImporter } from './types/key-io.js';
2
5
 
3
6
  import { crypto } from '@noble/hashes/crypto';
4
7
  import { randomBytes as nobleRandomBytes } from '@noble/hashes/utils';
@@ -179,3 +182,53 @@ export class CryptoUtils {
179
182
  return pin.toString().padStart(length, '0');
180
183
  }
181
184
  }
185
+
186
+ /**
187
+ * Type guard that checks whether the given object implements the {@link Cipher} interface.
188
+ */
189
+ export function isCipher<EncryptInput, DecryptInput>(
190
+ obj: unknown
191
+ ): obj is Cipher<EncryptInput, DecryptInput> {
192
+ return (
193
+ obj !== null && typeof obj === 'object'
194
+ && 'encrypt' in obj && typeof obj.encrypt === 'function'
195
+ && 'decrypt' in obj && typeof obj.decrypt === 'function'
196
+ );
197
+ }
198
+
199
+ /**
200
+ * Type guard that checks whether the given object implements the {@link KeyExporter} interface.
201
+ */
202
+ export function isKeyExporter<ExportKeyInput, ExportKeyOutput>(
203
+ obj: unknown
204
+ ): obj is KeyExporter<ExportKeyInput, ExportKeyOutput> {
205
+ return (
206
+ obj !== null && typeof obj === 'object'
207
+ && 'exportKey' in obj && typeof obj.exportKey === 'function'
208
+ );
209
+ }
210
+
211
+ /**
212
+ * Type guard that checks whether the given object implements the {@link KeyImporter} interface.
213
+ */
214
+ export function isKeyImporter<ImportKeyInput, ImportKeyOutput>(
215
+ obj: unknown
216
+ ): obj is KeyImporter<ImportKeyInput, ImportKeyOutput> {
217
+ return (
218
+ obj !== null && typeof obj === 'object'
219
+ && 'importKey' in obj && typeof obj.importKey === 'function'
220
+ );
221
+ }
222
+
223
+ /**
224
+ * Type guard that checks whether the given object implements the {@link KeyWrapper} interface.
225
+ */
226
+ export function isKeyWrapper<WrapKeyInput, UnwrapKeyInput>(
227
+ obj: unknown
228
+ ): obj is KeyWrapper<WrapKeyInput, UnwrapKeyInput> {
229
+ return (
230
+ obj !== null && typeof obj === 'object'
231
+ && 'wrapKey' in obj && typeof obj.wrapKey === 'function'
232
+ && 'unwrapKey' in obj && typeof obj.unwrapKey === 'function'
233
+ );
234
+ }