@enbox/crypto 0.0.3 → 0.0.5

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 (116) 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/cose/cbor.js +35 -0
  20. package/dist/esm/cose/cbor.js.map +1 -0
  21. package/dist/esm/cose/cose-key.js +312 -0
  22. package/dist/esm/cose/cose-key.js.map +1 -0
  23. package/dist/esm/cose/cose-sign1.js +283 -0
  24. package/dist/esm/cose/cose-sign1.js.map +1 -0
  25. package/dist/esm/cose/eat.js +254 -0
  26. package/dist/esm/cose/eat.js.map +1 -0
  27. package/dist/esm/crypto-error.js +4 -0
  28. package/dist/esm/crypto-error.js.map +1 -1
  29. package/dist/esm/index.js +9 -0
  30. package/dist/esm/index.js.map +1 -1
  31. package/dist/esm/local-key-manager.js +6 -1
  32. package/dist/esm/local-key-manager.js.map +1 -1
  33. package/dist/esm/primitives/ecies-secp256k1.js +79 -0
  34. package/dist/esm/primitives/ecies-secp256k1.js.map +1 -0
  35. package/dist/esm/primitives/x25519.js +9 -16
  36. package/dist/esm/primitives/x25519.js.map +1 -1
  37. package/dist/esm/utils.js +30 -0
  38. package/dist/esm/utils.js.map +1 -1
  39. package/dist/types/algorithms/aes-ctr.d.ts +1 -1
  40. package/dist/types/algorithms/aes-gcm.d.ts +23 -3
  41. package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
  42. package/dist/types/algorithms/aes-kw.d.ts +129 -0
  43. package/dist/types/algorithms/aes-kw.d.ts.map +1 -0
  44. package/dist/types/algorithms/ecdsa.d.ts +48 -3
  45. package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
  46. package/dist/types/algorithms/eddsa.d.ts +48 -3
  47. package/dist/types/algorithms/eddsa.d.ts.map +1 -1
  48. package/dist/types/algorithms/hkdf.d.ts +35 -0
  49. package/dist/types/algorithms/hkdf.d.ts.map +1 -0
  50. package/dist/types/algorithms/pbkdf2.d.ts +35 -0
  51. package/dist/types/algorithms/pbkdf2.d.ts.map +1 -0
  52. package/dist/types/algorithms/sha-2.d.ts +1 -1
  53. package/dist/types/algorithms/x25519.d.ts +76 -0
  54. package/dist/types/algorithms/x25519.d.ts.map +1 -0
  55. package/dist/types/cose/cbor.d.ts +30 -0
  56. package/dist/types/cose/cbor.d.ts.map +1 -0
  57. package/dist/types/cose/cose-key.d.ts +106 -0
  58. package/dist/types/cose/cose-key.d.ts.map +1 -0
  59. package/dist/types/cose/cose-sign1.d.ts +195 -0
  60. package/dist/types/cose/cose-sign1.d.ts.map +1 -0
  61. package/dist/types/cose/eat.d.ts +203 -0
  62. package/dist/types/cose/eat.d.ts.map +1 -0
  63. package/dist/types/crypto-error.d.ts +4 -0
  64. package/dist/types/crypto-error.d.ts.map +1 -1
  65. package/dist/types/index.d.ts +9 -0
  66. package/dist/types/index.d.ts.map +1 -1
  67. package/dist/types/local-key-manager.d.ts +4 -4
  68. package/dist/types/local-key-manager.d.ts.map +1 -1
  69. package/dist/types/primitives/ecies-secp256k1.d.ts +53 -0
  70. package/dist/types/primitives/ecies-secp256k1.d.ts.map +1 -0
  71. package/dist/types/primitives/x25519.d.ts +9 -16
  72. package/dist/types/primitives/x25519.d.ts.map +1 -1
  73. package/dist/types/types/crypto-api.d.ts +52 -4
  74. package/dist/types/types/crypto-api.d.ts.map +1 -1
  75. package/dist/types/types/key-converter.d.ts +37 -15
  76. package/dist/types/types/key-converter.d.ts.map +1 -1
  77. package/dist/types/types/key-deriver.d.ts +41 -0
  78. package/dist/types/types/key-deriver.d.ts.map +1 -1
  79. package/dist/types/types/key-io.d.ts +37 -0
  80. package/dist/types/types/key-io.d.ts.map +1 -1
  81. package/dist/types/types/params-direct.d.ts +17 -0
  82. package/dist/types/types/params-direct.d.ts.map +1 -1
  83. package/dist/types/types/params-kms.d.ts +55 -0
  84. package/dist/types/types/params-kms.d.ts.map +1 -1
  85. package/dist/types/utils.d.ts +19 -0
  86. package/dist/types/utils.d.ts.map +1 -1
  87. package/dist/utils.js +1 -1
  88. package/dist/utils.js.map +3 -3
  89. package/package.json +12 -14
  90. package/src/algorithms/aes-ctr.ts +1 -1
  91. package/src/algorithms/aes-gcm.ts +38 -2
  92. package/src/algorithms/aes-kw.ts +182 -0
  93. package/src/algorithms/ecdsa.ts +132 -1
  94. package/src/algorithms/eddsa.ts +108 -1
  95. package/src/algorithms/hkdf.ts +54 -0
  96. package/src/algorithms/pbkdf2.ts +57 -0
  97. package/src/algorithms/sha-2.ts +1 -1
  98. package/src/algorithms/x25519.ts +153 -0
  99. package/src/cose/cbor.ts +36 -0
  100. package/src/cose/cose-key.ts +344 -0
  101. package/src/cose/cose-sign1.ts +473 -0
  102. package/src/cose/eat.ts +368 -0
  103. package/src/crypto-error.ts +6 -0
  104. package/src/index.ts +10 -0
  105. package/src/local-key-manager.ts +9 -4
  106. package/src/primitives/ecies-secp256k1.ts +113 -0
  107. package/src/primitives/x25519.ts +9 -16
  108. package/src/types/crypto-api.ts +124 -6
  109. package/src/types/key-converter.ts +33 -7
  110. package/src/types/key-deriver.ts +49 -0
  111. package/src/types/key-io.ts +40 -0
  112. package/src/types/params-direct.ts +21 -0
  113. package/src/types/params-kms.ts +67 -0
  114. package/src/utils.ts +53 -0
  115. package/dist/browser.js +0 -60
  116. package/dist/browser.js.map +0 -7
@@ -1,8 +1,30 @@
1
+ import type { AsymmetricKeyConverter } from './key-converter.js';
1
2
  import type { AsymmetricKeyGenerator } from './key-generator.js';
3
+ import type { Cipher } from './cipher.js';
2
4
  import type { Hasher } from './hasher.js';
5
+ import type { Jwk } from '../jose/jwk.js';
3
6
  import type { KeyIdentifier } from './identifier.js';
7
+ import type { KeyWrapper } from './key-wrapper.js';
4
8
  import type { Signer } from './signer.js';
5
9
  import type {
10
+ BytesToPrivateKeyParams,
11
+ BytesToPublicKeyParams,
12
+ CipherParams,
13
+ DeriveKeyBytesParams,
14
+ DeriveKeyFromBytesParams,
15
+ DigestParams,
16
+ GenerateKeyParams,
17
+ GetPublicKeyParams,
18
+ PrivateKeyToBytesParams,
19
+ PublicKeyToBytesParams,
20
+ SignParams,
21
+ UnwrapKeyParams,
22
+ VerifyParams,
23
+ WrapKeyParams,
24
+ } from './params-direct.js';
25
+ import type { KeyBytesDeriver, SimpleKeyDeriver } from './key-deriver.js';
26
+ import type {
27
+ KmsCipherParams,
6
28
  KmsDigestParams,
7
29
  KmsGenerateKeyParams,
8
30
  KmsGetKeyUriParams,
@@ -12,7 +34,7 @@ import type {
12
34
  } from './params-kms.js';
13
35
 
14
36
  /**
15
- * The `CryptoApi` interface integrates key generation, hashing, and signing functionalities,
37
+ * The `DsaApi` interface integrates key generation, hashing, and signing functionalities,
16
38
  * designed for use with a Key Management System (KMS). It extends `AsymmetricKeyGenerator` for
17
39
  * generating asymmetric keys, `Hasher` for hash digest computations, and `Signer` for signing and
18
40
  * verifying operations.
@@ -32,25 +54,121 @@ import type {
32
54
  * identifier (e.g. JWK thumbprint, UUID generated by hosted KMS, etc.).
33
55
  * - Must support key generation, hashing, signing, and verifying operations.
34
56
  * - May be extended to support other cryptographic operations.
35
- * - Implementations of the `CryptoApi` interface can be passed as an argument to the public API
57
+ * - Implementations of the `DsaApi` interface can be passed as an argument to the public API
36
58
  * methods of Web5 libraries that involve key material (e.g., DID creation, VC signing, arbitrary
37
59
  * data signing/verification, etc.).
38
60
  */
39
- export interface CryptoApi<
61
+ export interface DsaApi<
40
62
  GenerateKeyInput = KmsGenerateKeyParams,
41
63
  GenerateKeyOutput = KeyIdentifier,
42
64
  GetPublicKeyInput = KmsGetPublicKeyParams,
43
65
  DigestInput = KmsDigestParams,
44
66
  SignInput = KmsSignParams,
45
67
  VerifyInput = KmsVerifyParams
46
- > extends AsymmetricKeyGenerator<GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput>,
68
+ > extends AsymmetricKeyGenerator<GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput>,
47
69
  Hasher<DigestInput>,
48
- Signer<SignInput, VerifyInput> {
70
+ Signer<SignInput, VerifyInput> {}
71
+
72
+ /**
73
+ * The `CryptoApi` interface extends {@link DsaApi} with encryption, key conversion,
74
+ * key derivation, and key wrapping capabilities.
75
+ *
76
+ * This is the full-featured cryptographic API used by agent-level code that needs direct-key
77
+ * cipher, key conversion, and key derivation operations beyond what the base `DsaApi` provides.
78
+ */
79
+ export interface CryptoApi<
80
+ GenerateKeyInput = GenerateKeyParams,
81
+ GenerateKeyOutput = Jwk,
82
+ GetPublicKeyInput = GetPublicKeyParams,
83
+ DigestInput = DigestParams,
84
+ SignInput = SignParams,
85
+ VerifyInput = VerifyParams,
86
+ EncryptInput = CipherParams,
87
+ DecryptInput = CipherParams,
88
+ BytesToPublicKeyInput = BytesToPublicKeyParams,
89
+ PublicKeyToBytesInput = PublicKeyToBytesParams,
90
+ BytesToPrivateKeyInput = BytesToPrivateKeyParams,
91
+ PrivateKeyToBytesInput = PrivateKeyToBytesParams,
92
+ DeriveKeyInput = DeriveKeyFromBytesParams,
93
+ DeriveKeyOutput = Jwk,
94
+ DeriveKeyBytesInput = DeriveKeyBytesParams,
95
+ DeriveKeyBytesOutput = Uint8Array,
96
+ WrapKeyInput = WrapKeyParams,
97
+ UnwrapKeyInput = UnwrapKeyParams
98
+ > extends
99
+ DsaApi<GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput, DigestInput, SignInput, VerifyInput>,
100
+ Cipher<EncryptInput, DecryptInput>,
101
+ AsymmetricKeyConverter<BytesToPublicKeyInput, PublicKeyToBytesInput, BytesToPrivateKeyInput, PrivateKeyToBytesInput>,
102
+ SimpleKeyDeriver<DeriveKeyInput, DeriveKeyOutput>,
103
+ KeyBytesDeriver<DeriveKeyBytesInput, DeriveKeyBytesOutput>,
104
+ KeyWrapper<WrapKeyInput, UnwrapKeyInput> {}
105
+
106
+ /** @deprecated Use {@link CryptoApi} instead. */
107
+ export type ExtendedCryptoApi<
108
+ GenerateKeyInput = GenerateKeyParams,
109
+ GenerateKeyOutput = Jwk,
110
+ GetPublicKeyInput = GetPublicKeyParams,
111
+ DigestInput = DigestParams,
112
+ SignInput = SignParams,
113
+ VerifyInput = VerifyParams,
114
+ EncryptInput = CipherParams,
115
+ DecryptInput = CipherParams,
116
+ BytesToPublicKeyInput = BytesToPublicKeyParams,
117
+ PublicKeyToBytesInput = PublicKeyToBytesParams,
118
+ BytesToPrivateKeyInput = BytesToPrivateKeyParams,
119
+ PrivateKeyToBytesInput = PrivateKeyToBytesParams,
120
+ DeriveKeyInput = DeriveKeyFromBytesParams,
121
+ DeriveKeyOutput = Jwk,
122
+ DeriveKeyBytesInput = DeriveKeyBytesParams,
123
+ DeriveKeyBytesOutput = Uint8Array,
124
+ WrapKeyInput = WrapKeyParams,
125
+ UnwrapKeyInput = UnwrapKeyParams
126
+ > = CryptoApi<
127
+ GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput, DigestInput, SignInput, VerifyInput,
128
+ EncryptInput, DecryptInput, BytesToPublicKeyInput, PublicKeyToBytesInput,
129
+ BytesToPrivateKeyInput, PrivateKeyToBytesInput, DeriveKeyInput, DeriveKeyOutput,
130
+ DeriveKeyBytesInput, DeriveKeyBytesOutput, WrapKeyInput, UnwrapKeyInput
131
+ >;
132
+
133
+ /**
134
+ * Parameters for configuring a {@link KeyManager} implementation.
135
+ */
136
+ export interface KeyManagerParams {
137
+ CipherInput?: unknown;
138
+ GenerateKeyInput?: unknown;
139
+ GenerateKeyOutput?: unknown;
140
+ GetPublicKeyInput?: unknown;
141
+ SignInput?: unknown;
142
+ VerifyInput?: unknown;
143
+ }
144
+
145
+ /**
146
+ * Default parameter types for {@link KeyManager}, using KMS-oriented types.
147
+ */
148
+ export interface DefaultKeyManagerParams {
149
+ CipherInput: KmsCipherParams;
150
+ GenerateKeyInput: KmsGenerateKeyParams;
151
+ GenerateKeyOutput: KeyIdentifier;
152
+ GetPublicKeyInput: KmsGetPublicKeyParams;
153
+ SignInput: KmsSignParams;
154
+ VerifyInput: KmsVerifyParams;
155
+ }
156
+
157
+ /**
158
+ * The `KeyManager` interface integrates key generation and signing capabilities.
159
+ *
160
+ * Concrete implementations of this interface are intended to be used as a Key Management System
161
+ * (KMS), which is responsible for generating and storing cryptographic keys.
162
+ */
163
+ export interface KeyManager<T extends KeyManagerParams = DefaultKeyManagerParams>
164
+ extends DsaApi<T['GenerateKeyInput'], T['GenerateKeyOutput'], T['GetPublicKeyInput'], KmsDigestParams, T['SignInput'], T['VerifyInput']> {
165
+
49
166
  /**
167
+ * Returns the Key URI for a given JWK.
50
168
  *
51
169
  * @param params - The parameters for getting the key URI.
52
170
  * @param params.key - The key to get the URI for.
53
171
  * @returns The key URI.
54
172
  */
55
173
  getKeyUri(params: KmsGetKeyUriParams): Promise<KeyIdentifier>;
56
- }
174
+ }
@@ -2,8 +2,16 @@ import type { Jwk } from '../jose/jwk.js';
2
2
 
3
3
  /**
4
4
  * `KeyConverter` interface for converting private keys between byte array and JWK formats.
5
+ *
6
+ * @typeParam BytesToPrivateKeyInput - The input type for `bytesToPrivateKey`. Defaults to
7
+ * `{ privateKeyBytes: Uint8Array }`.
8
+ * @typeParam PrivateKeyToBytesInput - The input type for `privateKeyToBytes`. Defaults to
9
+ * `{ privateKey: Jwk }`.
5
10
  */
6
- export interface KeyConverter {
11
+ export interface KeyConverter<
12
+ BytesToPrivateKeyInput = { privateKeyBytes: Uint8Array },
13
+ PrivateKeyToBytesInput = { privateKey: Jwk }
14
+ > {
7
15
 
8
16
  /**
9
17
  * Converts a private key from a byte array to JWK format.
@@ -13,7 +21,7 @@ export interface KeyConverter {
13
21
  *
14
22
  * @returns A Promise that resolves to the private key in JWK format.
15
23
  */
16
- bytesToPrivateKey(params: { privateKeyBytes: Uint8Array }): Promise<Jwk>;
24
+ bytesToPrivateKey(params: BytesToPrivateKeyInput): Promise<Jwk>;
17
25
 
18
26
  /**
19
27
  * Converts a private key from JWK format to a byte array.
@@ -23,14 +31,32 @@ export interface KeyConverter {
23
31
  *
24
32
  * @returns A Promise that resolves to the private key as a Uint8Array.
25
33
  */
26
- privateKeyToBytes(params: { privateKey: Jwk }): Promise<Uint8Array>;
34
+ privateKeyToBytes(params: PrivateKeyToBytesInput): Promise<Uint8Array>;
27
35
  }
28
36
 
29
37
  /**
30
- * `AsymmetricKeyConverter` interface extends {@link KeyConverter |`KeyConverter`}, adding support
38
+ * `AsymmetricKeyConverter` interface extends {@link KeyConverter | `KeyConverter`}, adding support
31
39
  * for public key conversions.
40
+ *
41
+ * When used with default type parameters, this interface includes all four conversion methods
42
+ * (bytes-to/from private key AND bytes-to/from public key). When used with explicit type
43
+ * parameters, both the private and public key conversion types can be customized.
44
+ *
45
+ * @typeParam BytesToPublicKeyInput - The input type for `bytesToPublicKey`. Defaults to
46
+ * `{ publicKeyBytes: Uint8Array }`.
47
+ * @typeParam PublicKeyToBytesInput - The input type for `publicKeyToBytes`. Defaults to
48
+ * `{ publicKey: Jwk }`.
49
+ * @typeParam BytesToPrivateKeyInput - The input type for `bytesToPrivateKey`. Defaults to
50
+ * `{ privateKeyBytes: Uint8Array }`.
51
+ * @typeParam PrivateKeyToBytesInput - The input type for `privateKeyToBytes`. Defaults to
52
+ * `{ privateKey: Jwk }`.
32
53
  */
33
- export interface AsymmetricKeyConverter extends KeyConverter {
54
+ export interface AsymmetricKeyConverter<
55
+ BytesToPublicKeyInput = { publicKeyBytes: Uint8Array },
56
+ PublicKeyToBytesInput = { publicKey: Jwk },
57
+ BytesToPrivateKeyInput = { privateKeyBytes: Uint8Array },
58
+ PrivateKeyToBytesInput = { privateKey: Jwk }
59
+ > extends KeyConverter<BytesToPrivateKeyInput, PrivateKeyToBytesInput> {
34
60
  /**
35
61
  * Converts a public key from a byte array to JWK format.
36
62
  *
@@ -39,7 +65,7 @@ export interface AsymmetricKeyConverter extends KeyConverter {
39
65
  *
40
66
  * @returns A Promise that resolves to the public key in JWK format.
41
67
  */
42
- bytesToPublicKey(params: { publicKeyBytes: Uint8Array }): Promise<Jwk>;
68
+ bytesToPublicKey(params: BytesToPublicKeyInput): Promise<Jwk>;
43
69
 
44
70
  /**
45
71
  * Converts a public key from JWK format to a byte array.
@@ -49,5 +75,5 @@ export interface AsymmetricKeyConverter extends KeyConverter {
49
75
  *
50
76
  * @returns A Promise that resolves to the public key as a Uint8Array.
51
77
  */
52
- publicKeyToBytes(params: { publicKey: Jwk }): Promise<Uint8Array>;
78
+ publicKeyToBytes(params: PublicKeyToBytesInput): Promise<Uint8Array>;
53
79
  }
@@ -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
+ }