@enbox/crypto 0.0.2 → 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.
- package/README.md +34 -102
- package/dist/browser.mjs +6 -10
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/algorithms/aes-ctr.js +1 -1
- package/dist/esm/algorithms/aes-gcm.js +35 -2
- package/dist/esm/algorithms/aes-gcm.js.map +1 -1
- package/dist/esm/algorithms/aes-kw.js +154 -0
- package/dist/esm/algorithms/aes-kw.js.map +1 -0
- package/dist/esm/algorithms/ecdsa.js +119 -6
- package/dist/esm/algorithms/ecdsa.js.map +1 -1
- package/dist/esm/algorithms/eddsa.js +99 -6
- package/dist/esm/algorithms/eddsa.js.map +1 -1
- package/dist/esm/algorithms/hkdf.js +53 -0
- package/dist/esm/algorithms/hkdf.js.map +1 -0
- package/dist/esm/algorithms/pbkdf2.js +55 -0
- package/dist/esm/algorithms/pbkdf2.js.map +1 -0
- package/dist/esm/algorithms/sha-2.js +2 -2
- package/dist/esm/algorithms/sha-2.js.map +1 -1
- package/dist/esm/algorithms/x25519.js +125 -0
- package/dist/esm/algorithms/x25519.js.map +1 -0
- package/dist/esm/crypto-error.js +41 -0
- package/dist/esm/crypto-error.js.map +1 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/jose/jwk.js +52 -26
- package/dist/esm/jose/jwk.js.map +1 -1
- package/dist/esm/local-key-manager.js +9 -3
- package/dist/esm/local-key-manager.js.map +1 -1
- package/dist/esm/primitives/aes-ctr.js.map +1 -1
- package/dist/esm/primitives/aes-gcm.js.map +1 -1
- package/dist/esm/primitives/aes-kw.js +246 -0
- package/dist/esm/primitives/aes-kw.js.map +1 -0
- package/dist/esm/primitives/concat-kdf.js +1 -1
- package/dist/esm/primitives/concat-kdf.js.map +1 -1
- package/dist/esm/primitives/ecies-secp256k1.js +79 -0
- package/dist/esm/primitives/ecies-secp256k1.js.map +1 -0
- package/dist/esm/primitives/ed25519.js +3 -3
- package/dist/esm/primitives/ed25519.js.map +1 -1
- package/dist/esm/primitives/hkdf.js +79 -0
- package/dist/esm/primitives/hkdf.js.map +1 -0
- package/dist/esm/primitives/pbkdf2.js +49 -0
- package/dist/esm/primitives/pbkdf2.js.map +1 -1
- package/dist/esm/primitives/secp256k1.js +4 -4
- package/dist/esm/primitives/secp256k1.js.map +1 -1
- package/dist/esm/primitives/secp256r1.js +4 -4
- package/dist/esm/primitives/secp256r1.js.map +1 -1
- package/dist/esm/primitives/x25519.js +10 -17
- package/dist/esm/primitives/x25519.js.map +1 -1
- package/dist/esm/primitives/xchacha20-poly1305.js +48 -3
- package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -1
- package/dist/esm/primitives/xchacha20.js +1 -1
- package/dist/esm/primitives/xchacha20.js.map +1 -1
- package/dist/esm/utils.js +30 -0
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/algorithms/aes-ctr.d.ts +2 -2
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -1
- package/dist/types/algorithms/aes-gcm.d.ts +25 -5
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
- package/dist/types/algorithms/aes-kw.d.ts +129 -0
- package/dist/types/algorithms/aes-kw.d.ts.map +1 -0
- package/dist/types/algorithms/ecdsa.d.ts +49 -4
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
- package/dist/types/algorithms/eddsa.d.ts +49 -4
- package/dist/types/algorithms/eddsa.d.ts.map +1 -1
- package/dist/types/algorithms/hkdf.d.ts +35 -0
- package/dist/types/algorithms/hkdf.d.ts.map +1 -0
- package/dist/types/algorithms/pbkdf2.d.ts +35 -0
- package/dist/types/algorithms/pbkdf2.d.ts.map +1 -0
- package/dist/types/algorithms/sha-2.d.ts +2 -2
- package/dist/types/algorithms/sha-2.d.ts.map +1 -1
- package/dist/types/algorithms/x25519.d.ts +76 -0
- package/dist/types/algorithms/x25519.d.ts.map +1 -0
- package/dist/types/crypto-error.d.ts +29 -0
- package/dist/types/crypto-error.d.ts.map +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/jose/jwk.d.ts.map +1 -1
- package/dist/types/local-key-manager.d.ts +6 -6
- package/dist/types/local-key-manager.d.ts.map +1 -1
- package/dist/types/primitives/aes-kw.d.ts +103 -0
- package/dist/types/primitives/aes-kw.d.ts.map +1 -0
- package/dist/types/primitives/concat-kdf.d.ts +1 -1
- package/dist/types/primitives/concat-kdf.d.ts.map +1 -1
- package/dist/types/primitives/ecies-secp256k1.d.ts +53 -0
- package/dist/types/primitives/ecies-secp256k1.d.ts.map +1 -0
- package/dist/types/primitives/hkdf.d.ts +90 -0
- package/dist/types/primitives/hkdf.d.ts.map +1 -0
- package/dist/types/primitives/pbkdf2.d.ts +58 -0
- package/dist/types/primitives/pbkdf2.d.ts.map +1 -1
- package/dist/types/primitives/x25519.d.ts +9 -16
- package/dist/types/primitives/x25519.d.ts.map +1 -1
- package/dist/types/primitives/xchacha20-poly1305.d.ts +47 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -1
- package/dist/types/types/cipher.d.ts +1 -1
- package/dist/types/types/crypto-api.d.ts +54 -6
- package/dist/types/types/crypto-api.d.ts.map +1 -1
- package/dist/types/types/key-converter.d.ts +37 -15
- package/dist/types/types/key-converter.d.ts.map +1 -1
- package/dist/types/types/key-deriver.d.ts +41 -0
- package/dist/types/types/key-deriver.d.ts.map +1 -1
- package/dist/types/types/key-io.d.ts +37 -0
- package/dist/types/types/key-io.d.ts.map +1 -1
- package/dist/types/types/params-direct.d.ts +96 -1
- package/dist/types/types/params-direct.d.ts.map +1 -1
- package/dist/types/types/params-kms.d.ts +55 -0
- package/dist/types/types/params-kms.d.ts.map +1 -1
- package/dist/types/utils.d.ts +19 -0
- package/dist/types/utils.d.ts.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/utils.js.map +4 -4
- package/package.json +29 -45
- package/src/algorithms/aes-ctr.ts +2 -2
- package/src/algorithms/aes-gcm.ts +41 -4
- package/src/algorithms/aes-kw.ts +182 -0
- package/src/algorithms/ecdsa.ts +145 -8
- package/src/algorithms/eddsa.ts +117 -10
- package/src/algorithms/hkdf.ts +54 -0
- package/src/algorithms/pbkdf2.ts +57 -0
- package/src/algorithms/sha-2.ts +3 -3
- package/src/algorithms/x25519.ts +153 -0
- package/src/crypto-error.ts +45 -0
- package/src/index.ts +8 -0
- package/src/jose/jwk.ts +32 -32
- package/src/local-key-manager.ts +22 -16
- package/src/primitives/aes-ctr.ts +1 -1
- package/src/primitives/aes-gcm.ts +5 -5
- package/src/primitives/aes-kw.ts +269 -0
- package/src/primitives/concat-kdf.ts +4 -2
- package/src/primitives/ecies-secp256k1.ts +113 -0
- package/src/primitives/ed25519.ts +6 -6
- package/src/primitives/hkdf.ts +121 -0
- package/src/primitives/pbkdf2.ts +91 -0
- package/src/primitives/secp256k1.ts +6 -6
- package/src/primitives/secp256r1.ts +6 -6
- package/src/primitives/x25519.ts +12 -19
- package/src/primitives/xchacha20-poly1305.ts +57 -4
- package/src/primitives/xchacha20.ts +1 -1
- package/src/types/cipher.ts +1 -1
- package/src/types/crypto-api.ts +129 -11
- package/src/types/key-converter.ts +33 -7
- package/src/types/key-deriver.ts +49 -0
- package/src/types/key-io.ts +40 -0
- package/src/types/params-direct.ts +118 -1
- package/src/types/params-kms.ts +67 -0
- package/src/utils.ts +55 -2
- package/dist/browser.js +0 -64
- package/dist/browser.js.map +0 -7
- package/dist/cjs/algorithms/aes-ctr.js +0 -188
- package/dist/cjs/algorithms/aes-ctr.js.map +0 -1
- package/dist/cjs/algorithms/aes-gcm.js +0 -196
- package/dist/cjs/algorithms/aes-gcm.js.map +0 -1
- package/dist/cjs/algorithms/crypto-algorithm.js +0 -13
- package/dist/cjs/algorithms/crypto-algorithm.js.map +0 -1
- package/dist/cjs/algorithms/ecdsa.js +0 -352
- package/dist/cjs/algorithms/ecdsa.js.map +0 -1
- package/dist/cjs/algorithms/eddsa.js +0 -325
- package/dist/cjs/algorithms/eddsa.js.map +0 -1
- package/dist/cjs/algorithms/sha-2.js +0 -119
- package/dist/cjs/algorithms/sha-2.js.map +0 -1
- package/dist/cjs/index.js +0 -41
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/jose/jwe.js +0 -3
- package/dist/cjs/jose/jwe.js.map +0 -1
- package/dist/cjs/jose/jwk.js +0 -278
- package/dist/cjs/jose/jwk.js.map +0 -1
- package/dist/cjs/jose/jws.js +0 -3
- package/dist/cjs/jose/jws.js.map +0 -1
- package/dist/cjs/jose/jwt.js +0 -3
- package/dist/cjs/jose/jwt.js.map +0 -1
- package/dist/cjs/jose/utils.js +0 -60
- package/dist/cjs/jose/utils.js.map +0 -1
- package/dist/cjs/local-key-manager.js +0 -521
- package/dist/cjs/local-key-manager.js.map +0 -1
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/primitives/aes-ctr.js +0 -398
- package/dist/cjs/primitives/aes-ctr.js.map +0 -1
- package/dist/cjs/primitives/aes-gcm.js +0 -425
- package/dist/cjs/primitives/aes-gcm.js.map +0 -1
- package/dist/cjs/primitives/concat-kdf.js +0 -215
- package/dist/cjs/primitives/concat-kdf.js.map +0 -1
- package/dist/cjs/primitives/ed25519.js +0 -651
- package/dist/cjs/primitives/ed25519.js.map +0 -1
- package/dist/cjs/primitives/pbkdf2.js +0 -120
- package/dist/cjs/primitives/pbkdf2.js.map +0 -1
- package/dist/cjs/primitives/secp256k1.js +0 -958
- package/dist/cjs/primitives/secp256k1.js.map +0 -1
- package/dist/cjs/primitives/secp256r1.js +0 -959
- package/dist/cjs/primitives/secp256r1.js.map +0 -1
- package/dist/cjs/primitives/sha256.js +0 -93
- package/dist/cjs/primitives/sha256.js.map +0 -1
- package/dist/cjs/primitives/x25519.js +0 -498
- package/dist/cjs/primitives/x25519.js.map +0 -1
- package/dist/cjs/primitives/xchacha20-poly1305.js +0 -340
- package/dist/cjs/primitives/xchacha20-poly1305.js.map +0 -1
- package/dist/cjs/primitives/xchacha20.js +0 -316
- package/dist/cjs/primitives/xchacha20.js.map +0 -1
- package/dist/cjs/types/cipher.js +0 -3
- package/dist/cjs/types/cipher.js.map +0 -1
- package/dist/cjs/types/crypto-api.js +0 -3
- package/dist/cjs/types/crypto-api.js.map +0 -1
- package/dist/cjs/types/hasher.js +0 -3
- package/dist/cjs/types/hasher.js.map +0 -1
- package/dist/cjs/types/identifier.js +0 -3
- package/dist/cjs/types/identifier.js.map +0 -1
- package/dist/cjs/types/key-compressor.js +0 -3
- package/dist/cjs/types/key-compressor.js.map +0 -1
- package/dist/cjs/types/key-converter.js +0 -3
- package/dist/cjs/types/key-converter.js.map +0 -1
- package/dist/cjs/types/key-deriver.js +0 -3
- package/dist/cjs/types/key-deriver.js.map +0 -1
- package/dist/cjs/types/key-generator.js +0 -3
- package/dist/cjs/types/key-generator.js.map +0 -1
- package/dist/cjs/types/key-io.js +0 -3
- package/dist/cjs/types/key-io.js.map +0 -1
- package/dist/cjs/types/key-wrapper.js +0 -3
- package/dist/cjs/types/key-wrapper.js.map +0 -1
- package/dist/cjs/types/params-direct.js +0 -3
- package/dist/cjs/types/params-direct.js.map +0 -1
- package/dist/cjs/types/params-enclosed.js +0 -3
- package/dist/cjs/types/params-enclosed.js.map +0 -1
- package/dist/cjs/types/params-kms.js +0 -3
- package/dist/cjs/types/params-kms.js.map +0 -1
- package/dist/cjs/types/signer.js +0 -3
- package/dist/cjs/types/signer.js.map +0 -1
- package/dist/cjs/utils.js +0 -173
- package/dist/cjs/utils.js.map +0 -1
|
@@ -1,11 +1,49 @@
|
|
|
1
|
-
import type { Jwk } from '../jose/jwk.js';
|
|
2
1
|
import type { AlgorithmIdentifier } from './identifier.js';
|
|
2
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Parameters for converting raw private key bytes to a JWK.
|
|
6
|
+
*/
|
|
7
|
+
export interface BytesToPrivateKeyParams {
|
|
8
|
+
/** The algorithm identifier. */
|
|
9
|
+
algorithm: AlgorithmIdentifier;
|
|
10
|
+
|
|
11
|
+
/** The raw private key bytes. */
|
|
12
|
+
privateKeyBytes: Uint8Array;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parameters for converting raw public key bytes to a JWK.
|
|
17
|
+
*/
|
|
18
|
+
export interface BytesToPublicKeyParams {
|
|
19
|
+
/** The algorithm identifier. */
|
|
20
|
+
algorithm: AlgorithmIdentifier;
|
|
21
|
+
|
|
22
|
+
/** The raw public key bytes. */
|
|
23
|
+
publicKeyBytes: Uint8Array;
|
|
24
|
+
}
|
|
3
25
|
|
|
4
26
|
/**
|
|
5
27
|
* Parameters for computing a public key.
|
|
6
28
|
*/
|
|
7
29
|
export interface ComputePublicKeyParams extends GetPublicKeyParams { }
|
|
8
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Parameters for encryption and decryption operations.
|
|
33
|
+
*
|
|
34
|
+
* Intended for use with a Key Management System.
|
|
35
|
+
*/
|
|
36
|
+
export interface CipherParams {
|
|
37
|
+
/** A {@link Jwk} containing the key to be used for encryption or decryption. */
|
|
38
|
+
key: Jwk;
|
|
39
|
+
|
|
40
|
+
/** Data to be encrypted or decrypted. */
|
|
41
|
+
data: Uint8Array;
|
|
42
|
+
|
|
43
|
+
/** Additional algorithm-specific parameters for encryption or decryption. */
|
|
44
|
+
[key: string]: unknown;
|
|
45
|
+
}
|
|
46
|
+
|
|
9
47
|
/**
|
|
10
48
|
* Parameters for decrypting data.
|
|
11
49
|
*/
|
|
@@ -42,6 +80,38 @@ export interface DeriveKeyParams {
|
|
|
42
80
|
derivedKeyParams: unknown
|
|
43
81
|
}
|
|
44
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
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Parameters for derivation of cryptographic byte arrays.
|
|
106
|
+
*/
|
|
107
|
+
export interface DeriveKeyBytesParams {
|
|
108
|
+
/** The base key to be used for derivation as a byte array. */
|
|
109
|
+
baseKeyBytes: Uint8Array;
|
|
110
|
+
|
|
111
|
+
/** The desired length of the derived key in bits. */
|
|
112
|
+
length: number;
|
|
113
|
+
}
|
|
114
|
+
|
|
45
115
|
/**
|
|
46
116
|
* Parameters for computing a hash digest.
|
|
47
117
|
*/
|
|
@@ -91,6 +161,39 @@ export interface SignParams {
|
|
|
91
161
|
data: Uint8Array;
|
|
92
162
|
}
|
|
93
163
|
|
|
164
|
+
/**
|
|
165
|
+
* Parameters for converting a private key JWK to raw bytes.
|
|
166
|
+
*/
|
|
167
|
+
export interface PrivateKeyToBytesParams {
|
|
168
|
+
/** The private key in JWK format. */
|
|
169
|
+
privateKey: Jwk;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Parameters for converting a public key JWK to raw bytes.
|
|
174
|
+
*/
|
|
175
|
+
export interface PublicKeyToBytesParams {
|
|
176
|
+
/** The public key in JWK format. */
|
|
177
|
+
publicKey: Jwk;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Parameters for unwrapping a key.
|
|
182
|
+
*/
|
|
183
|
+
export interface UnwrapKeyParams {
|
|
184
|
+
/** A {@link Jwk} containing the key used to decrypt the unwrapped key. */
|
|
185
|
+
decryptionKey: Jwk;
|
|
186
|
+
|
|
187
|
+
/** The wrapped private key as a byte array. */
|
|
188
|
+
wrappedKeyBytes: Uint8Array;
|
|
189
|
+
|
|
190
|
+
/** The algorithm identifier of the key encrypted in `wrappedKeyBytes`. */
|
|
191
|
+
wrappedKeyAlgorithm: string;
|
|
192
|
+
|
|
193
|
+
/** An object defining the algorithm-specific parameters for decrypting the `wrappedKeyBytes`. */
|
|
194
|
+
decryptParams?: unknown;
|
|
195
|
+
}
|
|
196
|
+
|
|
94
197
|
/**
|
|
95
198
|
* Parameters for verifying a signature.
|
|
96
199
|
*/
|
|
@@ -103,4 +206,18 @@ export interface VerifyParams {
|
|
|
103
206
|
|
|
104
207
|
/** The data associated with the signature. */
|
|
105
208
|
data: Uint8Array;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Parameters for wrapping a key.
|
|
213
|
+
*/
|
|
214
|
+
export interface WrapKeyParams {
|
|
215
|
+
/** A {@link Jwk} containing the key used to encrypt the unwrapped key. */
|
|
216
|
+
encryptionKey: Jwk;
|
|
217
|
+
|
|
218
|
+
/** A {@link Jwk} containing the private key to be wrapped. */
|
|
219
|
+
unwrappedKey: Jwk;
|
|
220
|
+
|
|
221
|
+
/** An object defining the algorithm-specific parameters for encrypting the `unwrappedKey`. */
|
|
222
|
+
encryptParams?: unknown;
|
|
106
223
|
}
|
package/src/types/params-kms.ts
CHANGED
|
@@ -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';
|
|
@@ -158,7 +161,7 @@ export class CryptoUtils {
|
|
|
158
161
|
const rejectionRange = Math.pow(10, length);
|
|
159
162
|
do {
|
|
160
163
|
// Adjust the byte generation based on length.
|
|
161
|
-
const randomBuffer = CryptoUtils.randomBytes(Math.ceil(length / 2) );
|
|
164
|
+
const randomBuffer = CryptoUtils.randomBytes(Math.ceil(length / 2) ); // 2 digits per byte.
|
|
162
165
|
const view = new DataView(randomBuffer.buffer);
|
|
163
166
|
// Convert the buffer to integer and take modulus based on length.
|
|
164
167
|
pin = view.getUint16(0, false) % rejectionRange;
|
|
@@ -172,10 +175,60 @@ export class CryptoUtils {
|
|
|
172
175
|
const view = new DataView(randomBuffer.buffer);
|
|
173
176
|
// Transform bytes to number (big endian).
|
|
174
177
|
pin = view.getUint32(0, false) % rejectionRange;
|
|
175
|
-
} while (pin > max);
|
|
178
|
+
} while (pin > max); // Reject if the number is outside the desired range.
|
|
176
179
|
}
|
|
177
180
|
|
|
178
181
|
// Pad the PIN with leading zeros to the desired length.
|
|
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
|
+
}
|