@enbox/crypto 0.0.1
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/LICENSE +201 -0
- package/README.md +708 -0
- package/dist/browser.js +64 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +64 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/algorithms/aes-ctr.js +188 -0
- package/dist/cjs/algorithms/aes-ctr.js.map +1 -0
- package/dist/cjs/algorithms/aes-gcm.js +196 -0
- package/dist/cjs/algorithms/aes-gcm.js.map +1 -0
- package/dist/cjs/algorithms/crypto-algorithm.js +13 -0
- package/dist/cjs/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/cjs/algorithms/ecdsa.js +352 -0
- package/dist/cjs/algorithms/ecdsa.js.map +1 -0
- package/dist/cjs/algorithms/eddsa.js +325 -0
- package/dist/cjs/algorithms/eddsa.js.map +1 -0
- package/dist/cjs/algorithms/sha-2.js +119 -0
- package/dist/cjs/algorithms/sha-2.js.map +1 -0
- package/dist/cjs/index.js +41 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/jose/jwe.js +3 -0
- package/dist/cjs/jose/jwe.js.map +1 -0
- package/dist/cjs/jose/jwk.js +278 -0
- package/dist/cjs/jose/jwk.js.map +1 -0
- package/dist/cjs/jose/jws.js +3 -0
- package/dist/cjs/jose/jws.js.map +1 -0
- package/dist/cjs/jose/jwt.js +3 -0
- package/dist/cjs/jose/jwt.js.map +1 -0
- package/dist/cjs/jose/utils.js +60 -0
- package/dist/cjs/jose/utils.js.map +1 -0
- package/dist/cjs/local-key-manager.js +521 -0
- package/dist/cjs/local-key-manager.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/primitives/aes-ctr.js +398 -0
- package/dist/cjs/primitives/aes-ctr.js.map +1 -0
- package/dist/cjs/primitives/aes-gcm.js +425 -0
- package/dist/cjs/primitives/aes-gcm.js.map +1 -0
- package/dist/cjs/primitives/concat-kdf.js +215 -0
- package/dist/cjs/primitives/concat-kdf.js.map +1 -0
- package/dist/cjs/primitives/ed25519.js +651 -0
- package/dist/cjs/primitives/ed25519.js.map +1 -0
- package/dist/cjs/primitives/pbkdf2.js +120 -0
- package/dist/cjs/primitives/pbkdf2.js.map +1 -0
- package/dist/cjs/primitives/secp256k1.js +958 -0
- package/dist/cjs/primitives/secp256k1.js.map +1 -0
- package/dist/cjs/primitives/secp256r1.js +959 -0
- package/dist/cjs/primitives/secp256r1.js.map +1 -0
- package/dist/cjs/primitives/sha256.js +93 -0
- package/dist/cjs/primitives/sha256.js.map +1 -0
- package/dist/cjs/primitives/x25519.js +498 -0
- package/dist/cjs/primitives/x25519.js.map +1 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js +340 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/cjs/primitives/xchacha20.js +316 -0
- package/dist/cjs/primitives/xchacha20.js.map +1 -0
- package/dist/cjs/types/cipher.js +3 -0
- package/dist/cjs/types/cipher.js.map +1 -0
- package/dist/cjs/types/crypto-api.js +3 -0
- package/dist/cjs/types/crypto-api.js.map +1 -0
- package/dist/cjs/types/hasher.js +3 -0
- package/dist/cjs/types/hasher.js.map +1 -0
- package/dist/cjs/types/identifier.js +3 -0
- package/dist/cjs/types/identifier.js.map +1 -0
- package/dist/cjs/types/key-compressor.js +3 -0
- package/dist/cjs/types/key-compressor.js.map +1 -0
- package/dist/cjs/types/key-converter.js +3 -0
- package/dist/cjs/types/key-converter.js.map +1 -0
- package/dist/cjs/types/key-deriver.js +3 -0
- package/dist/cjs/types/key-deriver.js.map +1 -0
- package/dist/cjs/types/key-generator.js +3 -0
- package/dist/cjs/types/key-generator.js.map +1 -0
- package/dist/cjs/types/key-io.js +3 -0
- package/dist/cjs/types/key-io.js.map +1 -0
- package/dist/cjs/types/key-wrapper.js +3 -0
- package/dist/cjs/types/key-wrapper.js.map +1 -0
- package/dist/cjs/types/params-direct.js +3 -0
- package/dist/cjs/types/params-direct.js.map +1 -0
- package/dist/cjs/types/params-enclosed.js +3 -0
- package/dist/cjs/types/params-enclosed.js.map +1 -0
- package/dist/cjs/types/params-kms.js +3 -0
- package/dist/cjs/types/params-kms.js.map +1 -0
- package/dist/cjs/types/signer.js +3 -0
- package/dist/cjs/types/signer.js.map +1 -0
- package/dist/cjs/utils.js +173 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/algorithms/aes-ctr.js +124 -0
- package/dist/esm/algorithms/aes-ctr.js.map +1 -0
- package/dist/esm/algorithms/aes-gcm.js +132 -0
- package/dist/esm/algorithms/aes-gcm.js.map +1 -0
- package/dist/esm/algorithms/crypto-algorithm.js +6 -0
- package/dist/esm/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/esm/algorithms/ecdsa.js +237 -0
- package/dist/esm/algorithms/ecdsa.js.map +1 -0
- package/dist/esm/algorithms/eddsa.js +213 -0
- package/dist/esm/algorithms/eddsa.js.map +1 -0
- package/dist/esm/algorithms/sha-2.js +57 -0
- package/dist/esm/algorithms/sha-2.js.map +1 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/jose/jwe.js +2 -0
- package/dist/esm/jose/jwe.js.map +1 -0
- package/dist/esm/jose/jwk.js +241 -0
- package/dist/esm/jose/jwk.js.map +1 -0
- package/dist/esm/jose/jws.js +2 -0
- package/dist/esm/jose/jws.js.map +1 -0
- package/dist/esm/jose/jwt.js +2 -0
- package/dist/esm/jose/jwt.js.map +1 -0
- package/dist/esm/jose/utils.js +34 -0
- package/dist/esm/jose/utils.js.map +1 -0
- package/dist/esm/local-key-manager.js +417 -0
- package/dist/esm/local-key-manager.js.map +1 -0
- package/dist/esm/primitives/aes-ctr.js +327 -0
- package/dist/esm/primitives/aes-ctr.js.map +1 -0
- package/dist/esm/primitives/aes-gcm.js +347 -0
- package/dist/esm/primitives/aes-gcm.js.map +1 -0
- package/dist/esm/primitives/concat-kdf.js +185 -0
- package/dist/esm/primitives/concat-kdf.js.map +1 -0
- package/dist/esm/primitives/ed25519.js +521 -0
- package/dist/esm/primitives/ed25519.js.map +1 -0
- package/dist/esm/primitives/pbkdf2.js +78 -0
- package/dist/esm/primitives/pbkdf2.js.map +1 -0
- package/dist/esm/primitives/secp256k1.js +805 -0
- package/dist/esm/primitives/secp256k1.js.map +1 -0
- package/dist/esm/primitives/secp256r1.js +806 -0
- package/dist/esm/primitives/secp256r1.js.map +1 -0
- package/dist/esm/primitives/sha256.js +55 -0
- package/dist/esm/primitives/sha256.js.map +1 -0
- package/dist/esm/primitives/x25519.js +392 -0
- package/dist/esm/primitives/x25519.js.map +1 -0
- package/dist/esm/primitives/xchacha20-poly1305.js +270 -0
- package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/esm/primitives/xchacha20.js +246 -0
- package/dist/esm/primitives/xchacha20.js.map +1 -0
- package/dist/esm/types/cipher.js +2 -0
- package/dist/esm/types/cipher.js.map +1 -0
- package/dist/esm/types/crypto-api.js +2 -0
- package/dist/esm/types/crypto-api.js.map +1 -0
- package/dist/esm/types/hasher.js +2 -0
- package/dist/esm/types/hasher.js.map +1 -0
- package/dist/esm/types/identifier.js +2 -0
- package/dist/esm/types/identifier.js.map +1 -0
- package/dist/esm/types/key-compressor.js +2 -0
- package/dist/esm/types/key-compressor.js.map +1 -0
- package/dist/esm/types/key-converter.js +2 -0
- package/dist/esm/types/key-converter.js.map +1 -0
- package/dist/esm/types/key-deriver.js +2 -0
- package/dist/esm/types/key-deriver.js.map +1 -0
- package/dist/esm/types/key-generator.js +2 -0
- package/dist/esm/types/key-generator.js.map +1 -0
- package/dist/esm/types/key-io.js +2 -0
- package/dist/esm/types/key-io.js.map +1 -0
- package/dist/esm/types/key-wrapper.js +2 -0
- package/dist/esm/types/key-wrapper.js.map +1 -0
- package/dist/esm/types/params-direct.js +2 -0
- package/dist/esm/types/params-direct.js.map +1 -0
- package/dist/esm/types/params-enclosed.js +2 -0
- package/dist/esm/types/params-enclosed.js.map +1 -0
- package/dist/esm/types/params-kms.js +2 -0
- package/dist/esm/types/params-kms.js.map +1 -0
- package/dist/esm/types/signer.js +2 -0
- package/dist/esm/types/signer.js.map +1 -0
- package/dist/esm/utils.js +165 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/algorithms/aes-ctr.d.ts +121 -0
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -0
- package/dist/types/algorithms/aes-gcm.d.ts +152 -0
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts +6 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts.map +1 -0
- package/dist/types/algorithms/ecdsa.d.ts +154 -0
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -0
- package/dist/types/algorithms/eddsa.d.ts +151 -0
- package/dist/types/algorithms/eddsa.d.ts.map +1 -0
- package/dist/types/algorithms/sha-2.d.ts +51 -0
- package/dist/types/algorithms/sha-2.d.ts.map +1 -0
- package/dist/types/index.d.ts +39 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/jose/jwe.d.ts +135 -0
- package/dist/types/jose/jwe.d.ts.map +1 -0
- package/dist/types/jose/jwk.d.ts +439 -0
- package/dist/types/jose/jwk.d.ts.map +1 -0
- package/dist/types/jose/jws.d.ts +67 -0
- package/dist/types/jose/jws.d.ts.map +1 -0
- package/dist/types/jose/jwt.d.ts +139 -0
- package/dist/types/jose/jwt.d.ts.map +1 -0
- package/dist/types/jose/utils.d.ts +14 -0
- package/dist/types/jose/utils.d.ts.map +1 -0
- package/dist/types/local-key-manager.d.ts +307 -0
- package/dist/types/local-key-manager.d.ts.map +1 -0
- package/dist/types/primitives/aes-ctr.d.ts +219 -0
- package/dist/types/primitives/aes-ctr.d.ts.map +1 -0
- package/dist/types/primitives/aes-gcm.d.ts +245 -0
- package/dist/types/primitives/aes-gcm.d.ts.map +1 -0
- package/dist/types/primitives/concat-kdf.d.ts +160 -0
- package/dist/types/primitives/concat-kdf.d.ts.map +1 -0
- package/dist/types/primitives/ed25519.d.ts +359 -0
- package/dist/types/primitives/ed25519.d.ts.map +1 -0
- package/dist/types/primitives/pbkdf2.d.ts +94 -0
- package/dist/types/primitives/pbkdf2.d.ts.map +1 -0
- package/dist/types/primitives/secp256k1.d.ts +598 -0
- package/dist/types/primitives/secp256k1.d.ts.map +1 -0
- package/dist/types/primitives/secp256r1.d.ts +599 -0
- package/dist/types/primitives/secp256r1.d.ts.map +1 -0
- package/dist/types/primitives/sha256.d.ts +42 -0
- package/dist/types/primitives/sha256.d.ts.map +1 -0
- package/dist/types/primitives/x25519.d.ts +283 -0
- package/dist/types/primitives/x25519.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts +210 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20.d.ts +186 -0
- package/dist/types/primitives/xchacha20.d.ts.map +1 -0
- package/dist/types/types/cipher.d.ts +49 -0
- package/dist/types/types/cipher.d.ts.map +1 -0
- package/dist/types/types/crypto-api.d.ts +40 -0
- package/dist/types/types/crypto-api.d.ts.map +1 -0
- package/dist/types/types/hasher.d.ts +33 -0
- package/dist/types/types/hasher.d.ts.map +1 -0
- package/dist/types/types/identifier.d.ts +16 -0
- package/dist/types/types/identifier.d.ts.map +1 -0
- package/dist/types/types/key-compressor.d.ts +28 -0
- package/dist/types/types/key-compressor.d.ts.map +1 -0
- package/dist/types/types/key-converter.d.ts +57 -0
- package/dist/types/types/key-converter.d.ts.map +1 -0
- package/dist/types/types/key-deriver.d.ts +39 -0
- package/dist/types/types/key-deriver.d.ts.map +1 -0
- package/dist/types/types/key-generator.d.ts +105 -0
- package/dist/types/types/key-generator.d.ts.map +1 -0
- package/dist/types/types/key-io.d.ts +37 -0
- package/dist/types/types/key-io.d.ts.map +1 -0
- package/dist/types/types/key-wrapper.d.ts +38 -0
- package/dist/types/types/key-wrapper.d.ts.map +1 -0
- package/dist/types/types/params-direct.d.ts +90 -0
- package/dist/types/types/params-direct.d.ts.map +1 -0
- package/dist/types/types/params-enclosed.d.ts +47 -0
- package/dist/types/types/params-enclosed.d.ts.map +1 -0
- package/dist/types/types/params-kms.d.ts +131 -0
- package/dist/types/types/params-kms.d.ts.map +1 -0
- package/dist/types/types/signer.d.ts +46 -0
- package/dist/types/types/signer.d.ts.map +1 -0
- package/dist/types/utils.d.ts +112 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/utils.js +7 -0
- package/dist/utils.js.map +7 -0
- package/package.json +103 -0
- package/src/algorithms/aes-ctr.ts +156 -0
- package/src/algorithms/aes-gcm.ts +187 -0
- package/src/algorithms/crypto-algorithm.ts +4 -0
- package/src/algorithms/ecdsa.ts +269 -0
- package/src/algorithms/eddsa.ts +243 -0
- package/src/algorithms/sha-2.ts +65 -0
- package/src/index.ts +42 -0
- package/src/jose/jwe.ts +196 -0
- package/src/jose/jwk.ts +632 -0
- package/src/jose/jws.ts +95 -0
- package/src/jose/jwt.ts +147 -0
- package/src/jose/utils.ts +34 -0
- package/src/local-key-manager.ts +540 -0
- package/src/primitives/aes-ctr.ts +352 -0
- package/src/primitives/aes-gcm.ts +378 -0
- package/src/primitives/concat-kdf.ts +240 -0
- package/src/primitives/ed25519.ts +548 -0
- package/src/primitives/pbkdf2.ts +122 -0
- package/src/primitives/secp256k1.ts +848 -0
- package/src/primitives/secp256r1.ts +850 -0
- package/src/primitives/sha256.ts +47 -0
- package/src/primitives/x25519.ts +403 -0
- package/src/primitives/xchacha20-poly1305.ts +274 -0
- package/src/primitives/xchacha20.ts +247 -0
- package/src/types/cipher.ts +53 -0
- package/src/types/crypto-api.ts +56 -0
- package/src/types/hasher.ts +32 -0
- package/src/types/identifier.ts +16 -0
- package/src/types/key-compressor.ts +25 -0
- package/src/types/key-converter.ts +53 -0
- package/src/types/key-deriver.ts +43 -0
- package/src/types/key-generator.ts +119 -0
- package/src/types/key-io.ts +42 -0
- package/src/types/key-wrapper.ts +42 -0
- package/src/types/params-direct.ts +106 -0
- package/src/types/params-enclosed.ts +50 -0
- package/src/types/params-kms.ts +156 -0
- package/src/types/signer.ts +50 -0
- package/src/utils.ts +181 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonicalizes a given object according to RFC 8785 (https://tools.ietf.org/html/rfc8785),
|
|
3
|
+
* which describes JSON Canonicalization Scheme (JCS). This function sorts the keys of the
|
|
4
|
+
* object and its nested objects alphabetically and then returns a stringified version of it.
|
|
5
|
+
* This method handles nested objects, array values, and null values appropriately.
|
|
6
|
+
*
|
|
7
|
+
* @param obj - The object to canonicalize.
|
|
8
|
+
* @returns The stringified version of the input object with its keys sorted alphabetically
|
|
9
|
+
* per RFC 8785.
|
|
10
|
+
*/
|
|
11
|
+
export declare function canonicalize(obj: {
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}): string;
|
|
14
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/jose/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,GAAG,MAAM,CAuBhE"}
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { KeyValueStore } from '@enbox/common';
|
|
2
|
+
import type { Jwk } from './jose/jwk.js';
|
|
3
|
+
import type { CryptoApi } from './types/crypto-api.js';
|
|
4
|
+
import type { KeyIdentifier } from './types/identifier.js';
|
|
5
|
+
import type { KeyImporterExporter } from './types/key-io.js';
|
|
6
|
+
import type { KmsSignParams, KmsDigestParams, KmsVerifyParams, KmsExportKeyParams, KmsGetKeyUriParams, KmsImportKeyParams, KmsGenerateKeyParams, KmsGetPublicKeyParams } from './types/params-kms.js';
|
|
7
|
+
/**
|
|
8
|
+
* The `LocalKeyManagerParams` interface specifies the parameters for initializing an instance of
|
|
9
|
+
* `LocalKeyManager`. It allows the optional inclusion of a `KeyValueStore` instance for key
|
|
10
|
+
* management. If not provided, a default `MemoryStore` instance will be used for storing keys in
|
|
11
|
+
* memory. Note that the `MemoryStore` is not persistent and will be cleared when the application
|
|
12
|
+
* exits.
|
|
13
|
+
*/
|
|
14
|
+
export type LocalKeyManagerParams = {
|
|
15
|
+
/**
|
|
16
|
+
* An optional property to specify a custom `KeyValueStore` instance for key management. If not
|
|
17
|
+
* provided, {@link LocalKeyManager | `LocalKeyManager`} uses a default `MemoryStore` instance.
|
|
18
|
+
* This store is responsible for managing cryptographic keys, allowing them to be retrieved,
|
|
19
|
+
* stored, and managed during cryptographic operations.
|
|
20
|
+
*/
|
|
21
|
+
keyStore?: KeyValueStore<KeyIdentifier, Jwk>;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The `LocalKeyManagerDigestParams` interface defines the algorithm-specific parameters that should
|
|
25
|
+
* be passed into the {@link LocalKeyManager.digest | `LocalKeyManager.digest()`} method.
|
|
26
|
+
*/
|
|
27
|
+
export interface LocalKeyManagerDigestParams extends KmsDigestParams {
|
|
28
|
+
/**
|
|
29
|
+
* A string defining the name of hash function to use. The value must be one of the following:
|
|
30
|
+
* - `"SHA-256"`: Generates a 256-bit digest.
|
|
31
|
+
*/
|
|
32
|
+
algorithm: 'SHA-256';
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The `LocalKeyManagerGenerateKeyParams` interface defines the algorithm-specific parameters that
|
|
36
|
+
* should be passed into the {@link LocalKeyManager.generateKey | `LocalKeyManager.generateKey()`}
|
|
37
|
+
* method when generating a key in the local KMS.
|
|
38
|
+
*/
|
|
39
|
+
export interface LocalKeyManagerGenerateKeyParams extends KmsGenerateKeyParams {
|
|
40
|
+
/**
|
|
41
|
+
* A string defining the type of key to generate. The value must be one of the following:
|
|
42
|
+
* - `"Ed25519"`
|
|
43
|
+
* - `"secp256k1"`
|
|
44
|
+
*/
|
|
45
|
+
algorithm: 'Ed25519' | 'secp256k1' | 'secp256r1';
|
|
46
|
+
}
|
|
47
|
+
export declare class LocalKeyManager implements CryptoApi, KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams> {
|
|
48
|
+
/**
|
|
49
|
+
* A private map that stores instances of cryptographic algorithm implementations. Each key in
|
|
50
|
+
* this map is an `AlgorithmConstructor`, and its corresponding value is an instance of a class
|
|
51
|
+
* that implements a specific cryptographic algorithm. This map is used to cache and reuse
|
|
52
|
+
* instances for performance optimization, ensuring that each algorithm is instantiated only once.
|
|
53
|
+
*/
|
|
54
|
+
private _algorithmInstances;
|
|
55
|
+
/**
|
|
56
|
+
* The `_keyStore` private variable in `LocalKeyManager` is a `KeyValueStore` instance used for
|
|
57
|
+
* storing and managing cryptographic keys. It allows the `LocalKeyManager` class to save,
|
|
58
|
+
* retrieve, and handle keys efficiently within the local Key Management System (KMS) context.
|
|
59
|
+
* This variable can be configured to use different storage backends, like in-memory storage or
|
|
60
|
+
* persistent storage, providing flexibility in key management according to the application's
|
|
61
|
+
* requirements.
|
|
62
|
+
*/
|
|
63
|
+
private _keyStore;
|
|
64
|
+
constructor(params?: LocalKeyManagerParams);
|
|
65
|
+
/**
|
|
66
|
+
* Generates a hash digest of the provided data.
|
|
67
|
+
*
|
|
68
|
+
* @remarks
|
|
69
|
+
* A digest is the output of the hash function. It's a fixed-size string of bytes
|
|
70
|
+
* that uniquely represents the data input into the hash function. The digest is often used for
|
|
71
|
+
* data integrity checks, as any alteration in the input data results in a significantly
|
|
72
|
+
* different digest.
|
|
73
|
+
*
|
|
74
|
+
* It takes the algorithm identifier of the hash function and data to digest as input and returns
|
|
75
|
+
* the digest of the data.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const keyManager = new LocalKeyManager();
|
|
80
|
+
* const data = new Uint8Array([...]);
|
|
81
|
+
* const digest = await keyManager.digest({ algorithm: 'SHA-256', data });
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @param params - The parameters for the digest operation.
|
|
85
|
+
* @param params.algorithm - The name of hash function to use.
|
|
86
|
+
* @param params.data - The data to digest.
|
|
87
|
+
*
|
|
88
|
+
* @returns A Promise which will be fulfilled with the hash digest.
|
|
89
|
+
*/
|
|
90
|
+
digest({ algorithm, data }: LocalKeyManagerDigestParams): Promise<Uint8Array>;
|
|
91
|
+
/**
|
|
92
|
+
* Exports a private key identified by the provided key URI from the local KMS.
|
|
93
|
+
*
|
|
94
|
+
* @remarks
|
|
95
|
+
* This method retrieves the key from the key store and returns it. It is primarily used
|
|
96
|
+
* for extracting keys for backup or transfer purposes.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const keyManager = new LocalKeyManager();
|
|
101
|
+
* const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
|
|
102
|
+
* const privateKey = await keyManager.exportKey({ keyUri });
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @param params - Parameters for exporting the key.
|
|
106
|
+
* @param params.keyUri - The key URI identifying the key to export.
|
|
107
|
+
*
|
|
108
|
+
* @returns A Promise resolving to the JWK representation of the exported key.
|
|
109
|
+
*/
|
|
110
|
+
exportKey({ keyUri }: KmsExportKeyParams): Promise<Jwk>;
|
|
111
|
+
/**
|
|
112
|
+
* Generates a new cryptographic key in the local KMS with the specified algorithm and returns a
|
|
113
|
+
* unique key URI which can be used to reference the key in subsequent operations.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* const keyManager = new LocalKeyManager();
|
|
118
|
+
* const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
|
|
119
|
+
* console.log(keyUri); // Outputs the key URI
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @param params - The parameters for key generation.
|
|
123
|
+
* @param params.algorithm - The algorithm to use for key generation, defined in `SupportedAlgorithm`.
|
|
124
|
+
*
|
|
125
|
+
* @returns A Promise that resolves to the key URI, a unique identifier for the generated key.
|
|
126
|
+
*/
|
|
127
|
+
generateKey({ algorithm }: LocalKeyManagerGenerateKeyParams): Promise<KeyIdentifier>;
|
|
128
|
+
/**
|
|
129
|
+
* Computes the Key URI for a given public JWK (JSON Web Key).
|
|
130
|
+
*
|
|
131
|
+
* @remarks
|
|
132
|
+
* This method generates a {@link https://datatracker.ietf.org/doc/html/rfc3986 | URI}
|
|
133
|
+
* (Uniform Resource Identifier) for the given JWK, which uniquely identifies the key across all
|
|
134
|
+
* `CryptoApi` implementations. The key URI is constructed by appending the
|
|
135
|
+
* {@link https://datatracker.ietf.org/doc/html/rfc7638 | JWK thumbprint} to the prefix
|
|
136
|
+
* `urn:jwk:`. The JWK thumbprint is deterministically computed from the JWK and is consistent
|
|
137
|
+
* regardless of property order or optional property inclusion in the JWK. This ensures that the
|
|
138
|
+
* same key material represented as a JWK will always yield the same thumbprint, and therefore,
|
|
139
|
+
* the same key URI.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* const keyManager = new LocalKeyManager();
|
|
144
|
+
* const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
|
|
145
|
+
* const publicKey = await keyManager.getPublicKey({ keyUri });
|
|
146
|
+
* const keyUriFromPublicKey = await keyManager.getKeyUri({ key: publicKey });
|
|
147
|
+
* console.log(keyUri === keyUriFromPublicKey); // Outputs `true`
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* @param params - The parameters for getting the key URI.
|
|
151
|
+
* @param params.key - The JWK for which to compute the key URI.
|
|
152
|
+
*
|
|
153
|
+
* @returns A Promise that resolves to the key URI as a string.
|
|
154
|
+
*/
|
|
155
|
+
getKeyUri({ key }: KmsGetKeyUriParams): Promise<KeyIdentifier>;
|
|
156
|
+
/**
|
|
157
|
+
* Retrieves the public key associated with a previously generated private key, identified by
|
|
158
|
+
* the provided key URI.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* const keyManager = new LocalKeyManager();
|
|
163
|
+
* const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
|
|
164
|
+
* const publicKey = await keyManager.getPublicKey({ keyUri });
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* @param params - The parameters for retrieving the public key.
|
|
168
|
+
* @param params.keyUri - The key URI of the private key to retrieve the public key for.
|
|
169
|
+
*
|
|
170
|
+
* @returns A Promise that resolves to the public key in JWK format.
|
|
171
|
+
*/
|
|
172
|
+
getPublicKey({ keyUri }: KmsGetPublicKeyParams): Promise<Jwk>;
|
|
173
|
+
/**
|
|
174
|
+
* Imports a private key into the local KMS.
|
|
175
|
+
*
|
|
176
|
+
* @remarks
|
|
177
|
+
* This method stores the provided JWK in the key store, making it available for subsequent
|
|
178
|
+
* cryptographic operations. It is particularly useful for initializing the KMS with pre-existing
|
|
179
|
+
* keys or for restoring keys from backups.
|
|
180
|
+
*
|
|
181
|
+
* Note that, if defined, the `kid` (key ID) property of the JWK is used as the key URI for the
|
|
182
|
+
* imported key. If the `kid` property is not provided, the key URI is computed from the JWK
|
|
183
|
+
* thumbprint of the key.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* const keyManager = new LocalKeyManager();
|
|
188
|
+
* const privateKey = { ... } // A private key in JWK format
|
|
189
|
+
* const keyUri = await keyManager.importKey({ key: privateKey });
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* @param params - Parameters for importing the key.
|
|
193
|
+
* @param params.key - The private key to import to in JWK format.
|
|
194
|
+
*
|
|
195
|
+
* @returns A Promise resolving to the key URI, uniquely identifying the imported key.
|
|
196
|
+
*/
|
|
197
|
+
importKey({ key }: KmsImportKeyParams): Promise<KeyIdentifier>;
|
|
198
|
+
/**
|
|
199
|
+
* Signs the provided data using the private key identified by the provided key URI.
|
|
200
|
+
*
|
|
201
|
+
* @remarks
|
|
202
|
+
* This method uses the signature algorithm determined by the `alg` and/or `crv` properties of the
|
|
203
|
+
* private key identified by the provided key URI to sign the provided data. The signature can
|
|
204
|
+
* later be verified by parties with access to the corresponding public key, ensuring that the
|
|
205
|
+
* data has not been tampered with and was indeed signed by the holder of the private key.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```ts
|
|
209
|
+
* const keyManager = new LocalKeyManager();
|
|
210
|
+
* const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
|
|
211
|
+
* const data = new TextEncoder().encode('Message to sign');
|
|
212
|
+
* const signature = await keyManager.sign({ keyUri, data });
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* @param params - The parameters for the signing operation.
|
|
216
|
+
* @param params.keyUri - The key URI of the private key to use for signing.
|
|
217
|
+
* @param params.data - The data to sign.
|
|
218
|
+
*
|
|
219
|
+
* @returns A Promise resolving to the digital signature as a `Uint8Array`.
|
|
220
|
+
*/
|
|
221
|
+
sign({ keyUri, data }: KmsSignParams): Promise<Uint8Array>;
|
|
222
|
+
/**
|
|
223
|
+
* Verifies a digital signature associated the provided data using the provided key.
|
|
224
|
+
*
|
|
225
|
+
* @remarks
|
|
226
|
+
* This method uses the signature algorithm determined by the `alg` and/or `crv` properties of the
|
|
227
|
+
* provided key to check the validity of a digital signature against the original data. It
|
|
228
|
+
* confirms whether the signature was created by the holder of the corresponding private key and
|
|
229
|
+
* that the data has not been tampered with.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```ts
|
|
233
|
+
* const keyManager = new LocalKeyManager();
|
|
234
|
+
* const keyUri = await keyManager.generateKey({ algorithm: 'Ed25519' });
|
|
235
|
+
* const data = new TextEncoder().encode('Message to sign');
|
|
236
|
+
* const signature = await keyManager.sign({ keyUri, data });
|
|
237
|
+
* const isSignatureValid = await keyManager.verify({ keyUri, data, signature });
|
|
238
|
+
* ```
|
|
239
|
+
*
|
|
240
|
+
* @param params - The parameters for the verification operation.
|
|
241
|
+
* @param params.key - The key to use for verification.
|
|
242
|
+
* @param params.signature - The signature to verify.
|
|
243
|
+
* @param params.data - The data to verify.
|
|
244
|
+
*
|
|
245
|
+
* @returns A Promise resolving to a boolean indicating whether the signature is valid.
|
|
246
|
+
*/
|
|
247
|
+
verify({ key, signature, data }: KmsVerifyParams): Promise<boolean>;
|
|
248
|
+
/**
|
|
249
|
+
* Retrieves an algorithm implementation instance based on the provided algorithm name.
|
|
250
|
+
*
|
|
251
|
+
* @remarks
|
|
252
|
+
* This method checks if the requested algorithm is supported and returns a cached instance
|
|
253
|
+
* if available. If an instance does not exist, it creates and caches a new one. This approach
|
|
254
|
+
* optimizes performance by reusing algorithm instances across cryptographic operations.
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* const signer = this.getAlgorithm({ algorithm: 'Ed25519' });
|
|
259
|
+
* ```
|
|
260
|
+
*
|
|
261
|
+
* @param params - The parameters for retrieving the algorithm implementation.
|
|
262
|
+
* @param params.algorithm - The name of the algorithm to retrieve.
|
|
263
|
+
*
|
|
264
|
+
* @returns An instance of the requested algorithm implementation.
|
|
265
|
+
*
|
|
266
|
+
* @throws Error if the requested algorithm is not supported.
|
|
267
|
+
*/
|
|
268
|
+
private getAlgorithm;
|
|
269
|
+
/**
|
|
270
|
+
* Determines the name of the algorithm based on the key's properties.
|
|
271
|
+
*
|
|
272
|
+
* @remarks
|
|
273
|
+
* This method facilitates the identification of the correct algorithm for cryptographic
|
|
274
|
+
* operations based on the `alg` or `crv` properties of a {@link Jwk | JWK}.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```ts
|
|
278
|
+
* const publicKey = { ... }; // Public key in JWK format
|
|
279
|
+
* const algorithm = this.getAlgorithmName({ key: publicKey });
|
|
280
|
+
* ```
|
|
281
|
+
*
|
|
282
|
+
* @param params - The parameters for determining the algorithm name.
|
|
283
|
+
* @param params.key - A JWK containing the `alg` or `crv` properties.
|
|
284
|
+
*
|
|
285
|
+
* @returns The name of the algorithm associated with the key.
|
|
286
|
+
*
|
|
287
|
+
* @throws Error if the algorithm cannot be determined from the provided input.
|
|
288
|
+
*/
|
|
289
|
+
private getAlgorithmName;
|
|
290
|
+
/**
|
|
291
|
+
* Retrieves a private key from the key store based on the provided key URI.
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* const privateKey = this.getPrivateKey({ keyUri: 'urn:jwk:...' });
|
|
296
|
+
* ```
|
|
297
|
+
*
|
|
298
|
+
* @param params - Parameters for retrieving the private key.
|
|
299
|
+
* @param params.keyUri - The key URI identifying the private key to retrieve.
|
|
300
|
+
*
|
|
301
|
+
* @returns A Promise resolving to the JWK representation of the private key.
|
|
302
|
+
*
|
|
303
|
+
* @throws Error if the key is not found in the key store.
|
|
304
|
+
*/
|
|
305
|
+
private getPrivateKey;
|
|
306
|
+
}
|
|
307
|
+
//# sourceMappingURL=local-key-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-key-manager.d.ts","sourceRoot":"","sources":["../../src/local-key-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAe,MAAM,eAAe,CAAC;AAE3D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGzC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAG7D,OAAO,KAAK,EACV,aAAa,EACb,eAAe,EACf,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,oBAAoB,EACpB,qBAAqB,EACtB,MAAM,uBAAuB,CAAC;AA8C/B;;;;;;GAMG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,aAAa,CAAC,aAAa,EAAE,GAAG,CAAC,CAAC;CAC9C,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,2BAA4B,SAAQ,eAAe;IAClE;;;OAGG;IACH,SAAS,EAAE,SAAS,CAAC;CACtB;AAED;;;;GAIG;AACH,MAAM,WAAW,gCAAiC,SAAQ,oBAAoB;IAC5E;;;;OAIG;IACH,SAAS,EAAE,SAAS,GAAG,WAAW,GAAG,WAAW,CAAC;CAClD;AAED,qBAAa,eAAgB,YACzB,SAAS,EACT,mBAAmB,CAAC,kBAAkB,EAAE,aAAa,EAAE,kBAAkB,CAAC;IAE5E;;;;;OAKG;IACH,OAAO,CAAC,mBAAmB,CAA8E;IAEzG;;;;;;;OAOG;IACH,OAAO,CAAC,SAAS,CAAoC;gBAEzC,MAAM,CAAC,EAAE,qBAAqB;IAI1C;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACU,MAAM,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,EACrC,2BAA2B,GAC1B,OAAO,CAAC,UAAU,CAAC;IAUtB;;;;;;;;;;;;;;;;;;OAkBG;IACU,SAAS,CAAC,EAAE,MAAM,EAAE,EAC/B,kBAAkB,GACjB,OAAO,CAAC,GAAG,CAAC;IAOf;;;;;;;;;;;;;;;OAeG;IACU,WAAW,CAAC,EAAE,SAAS,EAAE,EACpC,gCAAgC,GAC/B,OAAO,CAAC,aAAa,CAAC;IAoBzB;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACU,SAAS,CAAC,EAAE,GAAG,EAAE,EAC5B,kBAAkB,GACjB,OAAO,CAAC,aAAa,CAAC;IAUzB;;;;;;;;;;;;;;;OAeG;IACU,YAAY,CAAC,EAAE,MAAM,EAAE,EAClC,qBAAqB,GACpB,OAAO,CAAC,GAAG,CAAC;IAgBf;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACU,SAAS,CAAC,EAAE,GAAG,EAAE,EAC5B,kBAAkB,GACjB,OAAO,CAAC,aAAa,CAAC;IAkBzB;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACU,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,EAChC,aAAa,GACZ,OAAO,CAAC,UAAU,CAAC;IAgBtB;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACU,MAAM,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,EAC1C,eAAe,GACd,OAAO,CAAC,OAAO,CAAC;IAanB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,OAAO,CAAC,YAAY;IAmBpB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,OAAO,CAAC,gBAAgB;IAkBxB;;;;;;;;;;;;;;OAcG;YACW,aAAa;CAY5B"}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
2
|
+
/**
|
|
3
|
+
* Constant defining the AES key length values in bits.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* NIST publication FIPS 197 states:
|
|
7
|
+
* > The AES algorithm is capable of using cryptographic keys of 128, 192, and 256 bits to encrypt
|
|
8
|
+
* > and decrypt data in blocks of 128 bits.
|
|
9
|
+
*
|
|
10
|
+
* This implementation does not support key lengths that are different from the three values
|
|
11
|
+
* defined by this constant.
|
|
12
|
+
*
|
|
13
|
+
* @see {@link https://doi.org/10.6028/NIST.FIPS.197-upd1 | NIST FIPS 197}
|
|
14
|
+
*/
|
|
15
|
+
declare const AES_KEY_LENGTHS: readonly [128, 192, 256];
|
|
16
|
+
/**
|
|
17
|
+
* The `AesCtr` class provides a comprehensive set of utilities for cryptographic operations
|
|
18
|
+
* using the Advanced Encryption Standard (AES) in Counter (CTR) mode. This class includes
|
|
19
|
+
* methods for key generation, encryption, decryption, and conversions between raw byte arrays
|
|
20
|
+
* and JSON Web Key (JWK) formats. It is designed to support AES-CTR, a symmetric key algorithm
|
|
21
|
+
* that is widely used in various cryptographic applications for its efficiency and security.
|
|
22
|
+
*
|
|
23
|
+
* AES-CTR mode operates as a stream cipher using a block cipher (AES) and is well-suited for
|
|
24
|
+
* scenarios where parallel processing is beneficial or where the same key is required to
|
|
25
|
+
* encrypt multiple data blocks. The class adheres to standard cryptographic practices, ensuring
|
|
26
|
+
* compatibility and security in its implementations.
|
|
27
|
+
*
|
|
28
|
+
* Key Features:
|
|
29
|
+
* - Key Generation: Generate AES symmetric keys in JWK format.
|
|
30
|
+
* - Key Conversion: Transform keys between raw byte arrays and JWK formats.
|
|
31
|
+
* - Encryption: Encrypt data using AES-CTR with the provided symmetric key.
|
|
32
|
+
* - Decryption: Decrypt data encrypted with AES-CTR using the corresponding symmetric key.
|
|
33
|
+
*
|
|
34
|
+
* The methods in this class are asynchronous, returning Promises to accommodate various
|
|
35
|
+
* JavaScript environments.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* // Key Generation
|
|
40
|
+
* const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
|
|
41
|
+
* const privateKey = await AesCtr.generateKey({ length });
|
|
42
|
+
*
|
|
43
|
+
* // Encryption
|
|
44
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
45
|
+
* const counter = new Uint8Array(16); // 16-byte (128-bit) counter block
|
|
46
|
+
* const encryptedData = await AesCtr.encrypt({
|
|
47
|
+
* data,
|
|
48
|
+
* counter,
|
|
49
|
+
* key: privateKey,
|
|
50
|
+
* length: 64 // Length of the counter in bits
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* // Decryption
|
|
54
|
+
* const decryptedData = await AesCtr.decrypt({
|
|
55
|
+
* data: encryptedData,
|
|
56
|
+
* counter,
|
|
57
|
+
* key: privateKey,
|
|
58
|
+
* length: 64 // Length of the counter in bits
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* // Key Conversion
|
|
62
|
+
* const privateKeyBytes = await AesCtr.privateKeyToBytes({ privateKey });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare class AesCtr {
|
|
66
|
+
/**
|
|
67
|
+
* Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* This method takes a symmetric key represented as a byte array (Uint8Array) and
|
|
71
|
+
* converts it into a JWK object for use with AES (Advanced Encryption Standard)
|
|
72
|
+
* in Counter (CTR) mode. The conversion process involves encoding the key into
|
|
73
|
+
* base64url format and setting the appropriate JWK parameters.
|
|
74
|
+
*
|
|
75
|
+
* The resulting JWK object includes the following properties:
|
|
76
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
|
|
77
|
+
* - `k`: The symmetric key, base64url-encoded.
|
|
78
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
|
|
83
|
+
* const privateKey = await AesCtr.bytesToPrivateKey({ privateKeyBytes });
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @param params - The parameters for the symmetric key conversion.
|
|
87
|
+
* @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
|
|
88
|
+
*
|
|
89
|
+
* @returns A Promise that resolves to the symmetric key in JWK format.
|
|
90
|
+
*/
|
|
91
|
+
static bytesToPrivateKey({ privateKeyBytes }: {
|
|
92
|
+
privateKeyBytes: Uint8Array;
|
|
93
|
+
}): Promise<Jwk>;
|
|
94
|
+
/**
|
|
95
|
+
* Decrypts the provided data using AES in Counter (CTR) mode.
|
|
96
|
+
*
|
|
97
|
+
* @remarks
|
|
98
|
+
* This method performs AES-CTR decryption on the given encrypted data using the specified key.
|
|
99
|
+
* Similar to the encryption process, it requires an initial counter block and the length
|
|
100
|
+
* of the counter block, along with the encrypted data and the decryption key. The method
|
|
101
|
+
* returns the decrypted data as a Uint8Array.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
106
|
+
* const counter = new Uint8Array(16); // 16-byte (128-bit) counter block used during encryption
|
|
107
|
+
* const key = { ... }; // A Jwk object representing the same AES key used for encryption
|
|
108
|
+
* const decryptedData = await AesCtr.decrypt({
|
|
109
|
+
* data: encryptedData,
|
|
110
|
+
* counter,
|
|
111
|
+
* key,
|
|
112
|
+
* length: 64 // Length of the counter in bits
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* @param params - The parameters for the decryption operation.
|
|
117
|
+
* @param params.key - The key to use for decryption, represented in JWK format.
|
|
118
|
+
* @param params.data - The encrypted data to decrypt, as a Uint8Array.
|
|
119
|
+
* @param params.counter - The initial value of the counter block.
|
|
120
|
+
* @param params.length - The number of bits in the counter block that are used for the actual counter.
|
|
121
|
+
*
|
|
122
|
+
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
123
|
+
*/
|
|
124
|
+
static decrypt({ key, data, counter, length }: {
|
|
125
|
+
key: Jwk;
|
|
126
|
+
data: Uint8Array;
|
|
127
|
+
counter: Uint8Array;
|
|
128
|
+
length: number;
|
|
129
|
+
}): Promise<Uint8Array>;
|
|
130
|
+
/**
|
|
131
|
+
* Encrypts the provided data using AES in Counter (CTR) mode.
|
|
132
|
+
*
|
|
133
|
+
* @remarks
|
|
134
|
+
* This method performs AES-CTR encryption on the given data using the specified key.
|
|
135
|
+
* It requires the initial counter block and the length of the counter block, alongside
|
|
136
|
+
* the data and key. The method is designed to work asynchronously and returns the
|
|
137
|
+
* encrypted data as a Uint8Array.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
142
|
+
* const counter = new Uint8Array(16); // 16-byte (128-bit) counter block
|
|
143
|
+
* const key = { ... }; // A Jwk object representing an AES key
|
|
144
|
+
* const encryptedData = await AesCtr.encrypt({
|
|
145
|
+
* data,
|
|
146
|
+
* counter,
|
|
147
|
+
* key,
|
|
148
|
+
* length: 64 // Length of the counter in bits
|
|
149
|
+
* });
|
|
150
|
+
* ```
|
|
151
|
+
*
|
|
152
|
+
* @param params - The parameters for the encryption operation.
|
|
153
|
+
* @param params.key - The key to use for encryption, represented in JWK format.
|
|
154
|
+
* @param params.data - The data to encrypt, represented as a Uint8Array.
|
|
155
|
+
* @param params.counter - The initial value of the counter block.
|
|
156
|
+
* @param params.length - The number of bits in the counter block that are used for the actual counter.
|
|
157
|
+
*
|
|
158
|
+
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
159
|
+
*/
|
|
160
|
+
static encrypt({ key, data, counter, length }: {
|
|
161
|
+
key: Jwk;
|
|
162
|
+
data: Uint8Array;
|
|
163
|
+
counter: Uint8Array;
|
|
164
|
+
length: number;
|
|
165
|
+
}): Promise<Uint8Array>;
|
|
166
|
+
/**
|
|
167
|
+
* Generates a symmetric key for AES in Counter (CTR) mode in JSON Web Key (JWK) format.
|
|
168
|
+
*
|
|
169
|
+
* @remarks
|
|
170
|
+
* This method creates a new symmetric key of a specified length suitable for use with
|
|
171
|
+
* AES-CTR encryption. It uses cryptographically secure random number generation to
|
|
172
|
+
* ensure the uniqueness and security of the key. The generated key adheres to the JWK
|
|
173
|
+
* format, making it compatible with common cryptographic standards and easy to use in
|
|
174
|
+
* various cryptographic processes.
|
|
175
|
+
*
|
|
176
|
+
* The generated key includes the following components:
|
|
177
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
178
|
+
* - `k`: The symmetric key component, base64url-encoded.
|
|
179
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
|
|
184
|
+
* const privateKey = await AesCtr.generateKey({ length });
|
|
185
|
+
* ```
|
|
186
|
+
*
|
|
187
|
+
* @param params - The parameters for the key generation.
|
|
188
|
+
* @param params.length - The length of the key in bits. Common lengths are 128, 192, and 256 bits.
|
|
189
|
+
*
|
|
190
|
+
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
191
|
+
*/
|
|
192
|
+
static generateKey({ length }: {
|
|
193
|
+
length: typeof AES_KEY_LENGTHS[number];
|
|
194
|
+
}): Promise<Jwk>;
|
|
195
|
+
/**
|
|
196
|
+
* Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
|
|
197
|
+
*
|
|
198
|
+
* @remarks
|
|
199
|
+
* This method takes a symmetric key in JWK format and extracts its raw byte representation.
|
|
200
|
+
* It decodes the 'k' parameter of the JWK value, which represents the symmetric key in base64url
|
|
201
|
+
* encoding, into a byte array.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```ts
|
|
205
|
+
* const privateKey = { ... }; // A symmetric key in JWK format
|
|
206
|
+
* const privateKeyBytes = await AesCtr.privateKeyToBytes({ privateKey });
|
|
207
|
+
* ```
|
|
208
|
+
*
|
|
209
|
+
* @param params - The parameters for the symmetric key conversion.
|
|
210
|
+
* @param params.privateKey - The symmetric key in JWK format.
|
|
211
|
+
*
|
|
212
|
+
* @returns A Promise that resolves to the symmetric key as a Uint8Array.
|
|
213
|
+
*/
|
|
214
|
+
static privateKeyToBytes({ privateKey }: {
|
|
215
|
+
privateKey: Jwk;
|
|
216
|
+
}): Promise<Uint8Array>;
|
|
217
|
+
}
|
|
218
|
+
export {};
|
|
219
|
+
//# sourceMappingURL=aes-ctr.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-ctr.d.ts","sourceRoot":"","sources":["../../../src/primitives/aes-ctr.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAoB1C;;;;;;;;;;;;GAYG;AACH,QAAA,MAAM,eAAe,0BAA2B,CAAC;AAYjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,qBAAa,MAAM;IACjB;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;WACiB,iBAAiB,CAAC,EAAE,eAAe,EAAE,EAAE;QACzD,eAAe,EAAE,UAAU,CAAC;KAC7B,GAAG,OAAO,CAAC,GAAG,CAAC;IAahB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;WACiB,OAAO,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE;QAC1D,GAAG,EAAE,GAAG,CAAC;QACT,IAAI,EAAE,UAAU,CAAC;QACjB,OAAO,EAAE,UAAU,CAAC;QACpB,MAAM,EAAE,MAAM,CAAC;KAChB,GAAG,OAAO,CAAC,UAAU,CAAC;IA8BvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6BG;WACiB,OAAO,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE;QAC1D,GAAG,EAAE,GAAG,CAAC;QACT,IAAI,EAAE,UAAU,CAAC;QACjB,OAAO,EAAE,UAAU,CAAC;QACpB,MAAM,EAAE,MAAM,CAAC;KAChB,GAAG,OAAO,CAAC,UAAU,CAAC;IA8BvB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;WACiB,WAAW,CAAC,EAAE,MAAM,EAAE,EAAE;QAC1C,MAAM,EAAE,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;KACxC,GAAG,OAAO,CAAC,GAAG,CAAC;IAuBhB;;;;;;;;;;;;;;;;;;OAkBG;WACiB,iBAAiB,CAAC,EAAE,UAAU,EAAE,EAAE;QACpD,UAAU,EAAE,GAAG,CAAC;KACjB,GAAG,OAAO,CAAC,UAAU,CAAC;CAWxB"}
|