@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,156 @@
|
|
|
1
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
2
|
+
import type { Cipher } from '../types/cipher.js';
|
|
3
|
+
import type { KeyGenerator } from '../types/key-generator.js';
|
|
4
|
+
import type { DecryptParams, EncryptParams, GenerateKeyParams } from '../types/params-direct.js';
|
|
5
|
+
|
|
6
|
+
import { AesCtr } from '../primitives/aes-ctr.js';
|
|
7
|
+
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `AesCtrGenerateKeyParams` interface defines the algorithm-specific parameters that should be
|
|
11
|
+
* passed into the `generateKey()` method when using the AES-CTR algorithm.
|
|
12
|
+
*/
|
|
13
|
+
export interface AesCtrGenerateKeyParams extends GenerateKeyParams {
|
|
14
|
+
/** Specifies the algorithm variant for key generation in AES-CTR mode.
|
|
15
|
+
* The value determines the length of the key to be generated and must be one of the following:
|
|
16
|
+
* - `"A128CTR"`: Generates a 128-bit key.
|
|
17
|
+
* - `"A192CTR"`: Generates a 192-bit key.
|
|
18
|
+
* - `"A256CTR"`: Generates a 256-bit key.
|
|
19
|
+
*/
|
|
20
|
+
algorithm: 'A128CTR' | 'A192CTR' | 'A256CTR';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The `AesCtrParams` interface defines the algorithm-specific parameters that should be passed
|
|
25
|
+
* into the `encrypt()` and `decrypt()` methods when using the AES-CTR algorithm.
|
|
26
|
+
*/
|
|
27
|
+
export interface AesCtrParams {
|
|
28
|
+
/** The initial value of the counter block. */
|
|
29
|
+
counter: Uint8Array;
|
|
30
|
+
|
|
31
|
+
/** The number of bits in the counter block that are used for the actual counter. */
|
|
32
|
+
length: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The `AesCtrAlgorithm` class provides a concrete implementation for cryptographic operations using
|
|
37
|
+
* the AES algorithm in Counter (CTR) mode. This class implements both {@link Cipher | `Cipher`} and
|
|
38
|
+
* { @link KeyGenerator | `KeyGenerator`} interfaces, providing key generation, encryption, and
|
|
39
|
+
* decryption features.
|
|
40
|
+
*
|
|
41
|
+
* This class is typically accessed through implementations that extend the
|
|
42
|
+
* {@link CryptoApi | `CryptoApi`} interface.
|
|
43
|
+
*/
|
|
44
|
+
export class AesCtrAlgorithm extends CryptoAlgorithm
|
|
45
|
+
implements Cipher<EncryptParams & AesCtrParams, DecryptParams & AesCtrParams>,
|
|
46
|
+
KeyGenerator<AesCtrGenerateKeyParams, Jwk> {
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Decrypts the provided data using AES-CTR.
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* This method performs AES-CTR decryption on the given encrypted data using the specified key.
|
|
53
|
+
* Similar to the encryption process, it requires an initial counter block and the length
|
|
54
|
+
* of the counter block, along with the encrypted data and the decryption key. The method
|
|
55
|
+
* returns the decrypted data as a Uint8Array.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const aesCtr = new AesCtrAlgorithm();
|
|
60
|
+
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
61
|
+
* const counter = new Uint8Array(16); // 16-byte (128-bit) counter block used during encryption
|
|
62
|
+
* const key = { ... }; // A Jwk object representing the same AES key used for encryption
|
|
63
|
+
* const decryptedData = await aesCtr.decrypt({
|
|
64
|
+
* data: encryptedData,
|
|
65
|
+
* counter,
|
|
66
|
+
* key,
|
|
67
|
+
* length: 128 // Length of the counter in bits
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @param params - The parameters for the decryption operation.
|
|
72
|
+
*
|
|
73
|
+
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
74
|
+
*/
|
|
75
|
+
public async decrypt(params:
|
|
76
|
+
DecryptParams & AesCtrParams
|
|
77
|
+
): Promise<Uint8Array> {
|
|
78
|
+
const plaintext = AesCtr.decrypt(params);
|
|
79
|
+
|
|
80
|
+
return plaintext;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Encrypts the provided data using AES-CTR.
|
|
85
|
+
*
|
|
86
|
+
* @remarks
|
|
87
|
+
* This method performs AES-CTR encryption on the given data using the specified key.
|
|
88
|
+
* It requires the initial counter block and the length of the counter block, alongside
|
|
89
|
+
* the data and key. The method is designed to work asynchronously and returns the
|
|
90
|
+
* encrypted data as a Uint8Array.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const aesCtr = new AesCtrAlgorithm();
|
|
95
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
96
|
+
* const counter = new Uint8Array(16); // 16-byte (128-bit) counter block
|
|
97
|
+
* const key = { ... }; // A Jwk object representing an AES key
|
|
98
|
+
* const encryptedData = await aesCtr.encrypt({
|
|
99
|
+
* data,
|
|
100
|
+
* counter,
|
|
101
|
+
* key,
|
|
102
|
+
* length: 128 // Length of the counter in bits
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
106
|
+
* @param params - The parameters for the encryption operation.
|
|
107
|
+
*
|
|
108
|
+
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
109
|
+
*/
|
|
110
|
+
public async encrypt(params:
|
|
111
|
+
EncryptParams & AesCtrParams
|
|
112
|
+
): Promise<Uint8Array> {
|
|
113
|
+
const ciphertext = AesCtr.encrypt(params);
|
|
114
|
+
|
|
115
|
+
return ciphertext;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Generates a symmetric key for AES in Counter (CTR) mode in JSON Web Key (JWK) format.
|
|
120
|
+
*
|
|
121
|
+
* @remarks
|
|
122
|
+
* This method generates a symmetric AES key for use in CTR mode, based on the specified
|
|
123
|
+
* `algorithm` parameter which determines the key length. It uses cryptographically secure random
|
|
124
|
+
* number generation to ensure the uniqueness and security of the key. The key is returned in JWK
|
|
125
|
+
* format.
|
|
126
|
+
*
|
|
127
|
+
* The generated key includes the following components:
|
|
128
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
129
|
+
* - `k`: The symmetric key component, base64url-encoded.
|
|
130
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* const aesCtr = new AesCtrAlgorithm();
|
|
135
|
+
* const privateKey = await aesCtr.generateKey({ algorithm: 'A256CTR' });
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* @param params - The parameters for the key generation.
|
|
139
|
+
*
|
|
140
|
+
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
141
|
+
*/
|
|
142
|
+
public async generateKey({ algorithm }:
|
|
143
|
+
AesCtrGenerateKeyParams
|
|
144
|
+
): Promise<Jwk> {
|
|
145
|
+
// Map algorithm name to key length.
|
|
146
|
+
const length = { A128CTR: 128, A192CTR: 192, A256CTR: 256 }[algorithm] as 128 | 192 | 256;
|
|
147
|
+
|
|
148
|
+
// Generate a random private key.
|
|
149
|
+
const privateKey = await AesCtr.generateKey({ length });
|
|
150
|
+
|
|
151
|
+
// Set the `alg` property based on the specified algorithm.
|
|
152
|
+
privateKey.alg = algorithm;
|
|
153
|
+
|
|
154
|
+
return privateKey;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
2
|
+
import type { Cipher } from '../types/cipher.js';
|
|
3
|
+
import type { KeyGenerator } from '../types/key-generator.js';
|
|
4
|
+
import type { DecryptParams, EncryptParams, GenerateKeyParams } from '../types/params-direct.js';
|
|
5
|
+
|
|
6
|
+
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
7
|
+
import { AesGcm, AES_GCM_TAG_LENGTHS } from '../primitives/aes-gcm.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `AesGcmGenerateKeyParams` interface defines the algorithm-specific parameters that should be
|
|
11
|
+
* passed into the `generateKey()` method when using the AES-GCM algorithm.
|
|
12
|
+
*/
|
|
13
|
+
export interface AesGcmGenerateKeyParams extends GenerateKeyParams {
|
|
14
|
+
/** Specifies the algorithm variant for key generation in AES-GCM mode.
|
|
15
|
+
* The value determines the length of the key to be generated and must be one of the following:
|
|
16
|
+
* - `"A128GCM"`: Generates a 128-bit key.
|
|
17
|
+
* - `"A192GCM"`: Generates a 192-bit key.
|
|
18
|
+
* - `"A256GCM"`: Generates a 256-bit key.
|
|
19
|
+
*/
|
|
20
|
+
algorithm: 'A128GCM' | 'A192GCM' | 'A256GCM';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The `AesGcmParams` interface defines the algorithm-specific parameters that should be passed
|
|
25
|
+
* into the `encrypt()` and `decrypt()` methods when using the AES-GCM algorithm.
|
|
26
|
+
*/
|
|
27
|
+
export interface AesGcmParams {
|
|
28
|
+
/**
|
|
29
|
+
* The `additionalData` property is used for authentication alongside encrypted data but isn't
|
|
30
|
+
* encrypted itself. It must match in both encryption and decryption; a mismatch will cause
|
|
31
|
+
* decryption to fail. This feature allows for the authentication of data without encrypting it.
|
|
32
|
+
*
|
|
33
|
+
* The `additionalData` property is optional and omitting it does not compromise encryption
|
|
34
|
+
* security.
|
|
35
|
+
*/
|
|
36
|
+
additionalData?: Uint8Array;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The initialization vector (IV) must be unique for every encryption operation carried out with a
|
|
40
|
+
* given key. The IV need not be secret, but it must be unpredictable: that is, the IV must not be
|
|
41
|
+
* reused with the same key. The IV must be 12 bytes (96 bits) in length in accordance with the
|
|
42
|
+
* AES-GCM specification recommendedation to promote interoperability and efficiency.
|
|
43
|
+
*
|
|
44
|
+
* Note: It is OK to transmit the IV in the clear with the encrypted message.
|
|
45
|
+
*/
|
|
46
|
+
iv: Uint8Array;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* This property determines the size in bits of the authentication tag generated in the encryption
|
|
50
|
+
* operation and used for authentication in the corresponding decryption. In accordance with the
|
|
51
|
+
* AES-GCM specification, the tag length must be 96, 104, 112, 120 or 128.
|
|
52
|
+
*
|
|
53
|
+
* The `tagLength` property is optional and defaults to 128 bits if omitted.
|
|
54
|
+
*/
|
|
55
|
+
tagLength?: typeof AES_GCM_TAG_LENGTHS[number];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The `AesGcmAlgorithm` class provides a concrete implementation for cryptographic operations using
|
|
60
|
+
* the AES algorithm in Galois/Counter Mode (GCM). This class implements both
|
|
61
|
+
* {@link Cipher | `Cipher`} and { @link KeyGenerator | `KeyGenerator`} interfaces, providing
|
|
62
|
+
* key generation, encryption, and decryption features.
|
|
63
|
+
*
|
|
64
|
+
* This class is typically accessed through implementations that extend the
|
|
65
|
+
* {@link CryptoApi | `CryptoApi`} interface.
|
|
66
|
+
*/
|
|
67
|
+
export class AesGcmAlgorithm extends CryptoAlgorithm
|
|
68
|
+
implements Cipher<AesGcmParams, AesGcmParams>,
|
|
69
|
+
KeyGenerator<AesGcmGenerateKeyParams, Jwk> {
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Decrypts the provided data using AES-GCM.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* This method performs AES-GCM decryption on the given encrypted data using the specified key.
|
|
76
|
+
* It requires an initialization vector (IV), the encrypted data along with the decryption key,
|
|
77
|
+
* and optionally, additional authenticated data (AAD). The method returns the decrypted data as a
|
|
78
|
+
* Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
|
|
79
|
+
* tag used when encrypting the data. If not specified, the default tag length of 128 bits is
|
|
80
|
+
* used.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* const aesGcm = new AesGcmAlgorithm();
|
|
85
|
+
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
86
|
+
* const iv = new Uint8Array([...]); // Initialization vector used during encryption
|
|
87
|
+
* const additionalData = new Uint8Array([...]); // Optional additional authenticated data
|
|
88
|
+
* const key = { ... }; // A Jwk object representing the AES key
|
|
89
|
+
* const decryptedData = await aesGcm.decrypt({
|
|
90
|
+
* data: encryptedData,
|
|
91
|
+
* iv,
|
|
92
|
+
* additionalData,
|
|
93
|
+
* key,
|
|
94
|
+
* tagLength: 128 // Optional tag length in bits
|
|
95
|
+
* });
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @param params - The parameters for the decryption operation.
|
|
99
|
+
*
|
|
100
|
+
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
101
|
+
*/
|
|
102
|
+
public async decrypt(params:
|
|
103
|
+
DecryptParams & AesGcmParams
|
|
104
|
+
): Promise<Uint8Array> {
|
|
105
|
+
const plaintext = AesGcm.decrypt(params);
|
|
106
|
+
|
|
107
|
+
return plaintext;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Encrypts the provided data using AES-GCM.
|
|
112
|
+
*
|
|
113
|
+
* @remarks
|
|
114
|
+
* This method performs AES-GCM encryption on the given data using the specified key.
|
|
115
|
+
* It requires an initialization vector (IV), the encrypted data along with the decryption key,
|
|
116
|
+
* and optionally, additional authenticated data (AAD). The method returns the encrypted data as a
|
|
117
|
+
* Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
|
|
118
|
+
* tag generated in the encryption operation and used for authentication in the corresponding
|
|
119
|
+
* decryption. If not specified, the default tag length of 128 bits is used.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* const aesGcm = new AesGcmAlgorithm();
|
|
124
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
125
|
+
* const iv = new Uint8Array([...]); // Initialization vector
|
|
126
|
+
* const additionalData = new Uint8Array([...]); // Optional additional authenticated data
|
|
127
|
+
* const key = { ... }; // A Jwk object representing an AES key
|
|
128
|
+
* const encryptedData = await aesGcm.encrypt({
|
|
129
|
+
* data,
|
|
130
|
+
* iv,
|
|
131
|
+
* additionalData,
|
|
132
|
+
* key,
|
|
133
|
+
* tagLength: 128 // Optional tag length in bits
|
|
134
|
+
* });
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @param params - The parameters for the encryption operation.
|
|
138
|
+
*
|
|
139
|
+
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
140
|
+
*/
|
|
141
|
+
public async encrypt(params:
|
|
142
|
+
EncryptParams & AesGcmParams
|
|
143
|
+
): Promise<Uint8Array> {
|
|
144
|
+
const ciphertext = AesGcm.encrypt(params);
|
|
145
|
+
|
|
146
|
+
return ciphertext;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Generates a symmetric key for AES in Galois/Counter Mode (GCM) in JSON Web Key (JWK) format.
|
|
151
|
+
*
|
|
152
|
+
* @remarks
|
|
153
|
+
* This method generates a symmetric AES key for use in GCM mode, based on the specified
|
|
154
|
+
* `algorithm` parameter which determines the key length. It uses cryptographically secure random
|
|
155
|
+
* number generation to ensure the uniqueness and security of the key. The key is returned in JWK
|
|
156
|
+
* format.
|
|
157
|
+
*
|
|
158
|
+
* The generated key includes the following components:
|
|
159
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
160
|
+
* - `k`: The symmetric key component, base64url-encoded.
|
|
161
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* const aesGcm = new AesGcmAlgorithm();
|
|
166
|
+
* const privateKey = await aesGcm.generateKey({ algorithm: 'A256GCM' });
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* @param params - The parameters for the key generation.
|
|
170
|
+
*
|
|
171
|
+
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
172
|
+
*/
|
|
173
|
+
public async generateKey({ algorithm }:
|
|
174
|
+
AesGcmGenerateKeyParams
|
|
175
|
+
): Promise<Jwk> {
|
|
176
|
+
// Map algorithm name to key length.
|
|
177
|
+
const length = { A128GCM: 128, A192GCM: 192, A256GCM: 256 }[algorithm] as 128 | 192 | 256;
|
|
178
|
+
|
|
179
|
+
// Generate a random private key.
|
|
180
|
+
const privateKey = await AesGcm.generateKey({ length });
|
|
181
|
+
|
|
182
|
+
// Set the `alg` property based on the specified algorithm.
|
|
183
|
+
privateKey.alg = algorithm;
|
|
184
|
+
|
|
185
|
+
return privateKey;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
2
|
+
import type { Signer } from '../types/signer.js';
|
|
3
|
+
import type { AsymmetricKeyGenerator } from '../types/key-generator.js';
|
|
4
|
+
import type { ComputePublicKeyParams, GenerateKeyParams, GetPublicKeyParams, SignParams, VerifyParams } from '../types/params-direct.js';
|
|
5
|
+
|
|
6
|
+
import { Secp256k1 } from '../primitives/secp256k1.js';
|
|
7
|
+
import { Secp256r1 } from '../primitives/secp256r1.js';
|
|
8
|
+
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
9
|
+
import { isEcPrivateJwk, isEcPublicJwk } from '../jose/jwk.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The `EcdsaGenerateKeyParams` interface defines the algorithm-specific parameters that should be
|
|
13
|
+
* passed into the `generateKey()` method when using the ECDSA algorithm.
|
|
14
|
+
*/
|
|
15
|
+
export interface EcdsaGenerateKeyParams extends GenerateKeyParams {
|
|
16
|
+
/**
|
|
17
|
+
* A string defining the type of key to generate. The value must be one of the following:
|
|
18
|
+
* - `"ES256"`: ECDSA using the secp256r1 (P-256) curve and SHA-256.
|
|
19
|
+
* - `"ES256K"`: ECDSA using the secp256k1 curve and SHA-256.
|
|
20
|
+
* - `"secp256k1"`: ECDSA using the secp256k1 curve and SHA-256.
|
|
21
|
+
* - `"secp256r1"`: ECDSA using the secp256r1 (P-256) curve and SHA-256.
|
|
22
|
+
*/
|
|
23
|
+
algorithm: 'ES256' | 'ES256K' | 'secp256k1' | 'secp256r1';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The `EcdsaAlgorithm` class provides a concrete implementation for cryptographic operations using
|
|
28
|
+
* the Elliptic Curve Digital Signature Algorithm (ECDSA). This class implements both
|
|
29
|
+
* {@link Signer | `Signer`} and { @link AsymmetricKeyGenerator | `AsymmetricKeyGenerator`}
|
|
30
|
+
* interfaces, providing private key generation, public key derivation, and creation/verification
|
|
31
|
+
* of signatures.
|
|
32
|
+
*
|
|
33
|
+
* This class is typically accessed through implementations that extend the
|
|
34
|
+
* {@link CryptoApi | `CryptoApi`} interface.
|
|
35
|
+
*/
|
|
36
|
+
export class EcdsaAlgorithm extends CryptoAlgorithm
|
|
37
|
+
implements AsymmetricKeyGenerator<EcdsaGenerateKeyParams, Jwk, GetPublicKeyParams>,
|
|
38
|
+
Signer<SignParams, VerifyParams> {
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Derives the public key in JWK format from a given private key.
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* This method takes a private key in JWK format and derives its corresponding public key,
|
|
45
|
+
* also in JWK format. The process ensures that the derived public key correctly corresponds to
|
|
46
|
+
* the given private key.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const ecdsa = new EcdsaAlgorithm();
|
|
51
|
+
* const privateKey = { ... }; // A Jwk object representing a private key
|
|
52
|
+
* const publicKey = await ecdsa.computePublicKey({ key: privateKey });
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @param params - The parameters for the public key derivation.
|
|
56
|
+
* @param params.key - The private key in JWK format from which to derive the public key.
|
|
57
|
+
*
|
|
58
|
+
* @returns A Promise that resolves to the derived public key in JWK format.
|
|
59
|
+
*/
|
|
60
|
+
public async computePublicKey({ key }:
|
|
61
|
+
ComputePublicKeyParams
|
|
62
|
+
): Promise<Jwk> {
|
|
63
|
+
if (!isEcPrivateJwk(key)) throw new TypeError('Invalid key provided. Must be an elliptic curve (EC) private key.');
|
|
64
|
+
|
|
65
|
+
switch (key.crv) {
|
|
66
|
+
|
|
67
|
+
case 'secp256k1': {
|
|
68
|
+
const publicKey = await Secp256k1.computePublicKey({ key });
|
|
69
|
+
publicKey.alg = 'ES256K';
|
|
70
|
+
return publicKey;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
case 'P-256': {
|
|
74
|
+
const publicKey = await Secp256r1.computePublicKey({ key });
|
|
75
|
+
publicKey.alg = 'ES256';
|
|
76
|
+
return publicKey;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
default: {
|
|
80
|
+
throw new Error(`Unsupported curve: ${key.crv}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Generates a new private key with the specified algorithm in JSON Web Key (JWK) format.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* const ecdsa = new EcdsaAlgorithm();
|
|
91
|
+
* const privateKey = await ecdsa.generateKey({ algorithm: 'ES256K' });
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* @param params - The parameters for key generation.
|
|
95
|
+
* @param params.algorithm - The algorithm to use for key generation.
|
|
96
|
+
*
|
|
97
|
+
* @returns A Promise that resolves to the generated private key in JWK format.
|
|
98
|
+
*/
|
|
99
|
+
public async generateKey({ algorithm }:
|
|
100
|
+
EcdsaGenerateKeyParams
|
|
101
|
+
): Promise<Jwk> {
|
|
102
|
+
switch (algorithm) {
|
|
103
|
+
|
|
104
|
+
case 'ES256K':
|
|
105
|
+
case 'secp256k1': {
|
|
106
|
+
const privateKey = await Secp256k1.generateKey();
|
|
107
|
+
privateKey.alg = 'ES256K';
|
|
108
|
+
return privateKey;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
case 'ES256':
|
|
112
|
+
case 'secp256r1': {
|
|
113
|
+
const privateKey = await Secp256r1.generateKey();
|
|
114
|
+
privateKey.alg = 'ES256';
|
|
115
|
+
return privateKey;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Retrieves the public key properties from a given private key in JWK format.
|
|
122
|
+
*
|
|
123
|
+
* @remarks
|
|
124
|
+
* This method extracts the public key portion from an ECDSA private key in JWK format. It does
|
|
125
|
+
* so by removing the private key property 'd' and making a shallow copy, effectively yielding the
|
|
126
|
+
* public key.
|
|
127
|
+
*
|
|
128
|
+
* Note: This method offers a significant performance advantage, being about 200 times faster
|
|
129
|
+
* than `computePublicKey()`. However, it does not mathematically validate the private key, nor
|
|
130
|
+
* does it derive the public key from the private key. It simply extracts existing public key
|
|
131
|
+
* properties from the private key object. This makes it suitable for scenarios where speed is
|
|
132
|
+
* critical and the private key's integrity is already assured.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const ecdsa = new EcdsaAlgorithm();
|
|
137
|
+
* const privateKey = { ... }; // A Jwk object representing a private key
|
|
138
|
+
* const publicKey = await ecdsa.getPublicKey({ key: privateKey });
|
|
139
|
+
* ```
|
|
140
|
+
*
|
|
141
|
+
* @param params - The parameters for retrieving the public key properties.
|
|
142
|
+
* @param params.key - The private key in JWK format.
|
|
143
|
+
*
|
|
144
|
+
* @returns A Promise that resolves to the public key in JWK format.
|
|
145
|
+
*/
|
|
146
|
+
public async getPublicKey({ key }:
|
|
147
|
+
GetPublicKeyParams
|
|
148
|
+
): Promise<Jwk> {
|
|
149
|
+
if (!isEcPrivateJwk(key)) throw new TypeError('Invalid key provided. Must be an elliptic curve (EC) private key.');
|
|
150
|
+
|
|
151
|
+
switch (key.crv) {
|
|
152
|
+
|
|
153
|
+
case 'secp256k1': {
|
|
154
|
+
const publicKey = await Secp256k1.getPublicKey({ key });
|
|
155
|
+
publicKey.alg = 'ES256K';
|
|
156
|
+
return publicKey;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
case 'P-256': {
|
|
160
|
+
const publicKey = await Secp256r1.getPublicKey({ key });
|
|
161
|
+
publicKey.alg = 'ES256';
|
|
162
|
+
return publicKey;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
default: {
|
|
166
|
+
throw new Error(`Unsupported curve: ${key.crv}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Generates an ECDSA signature of given data using a private key.
|
|
173
|
+
*
|
|
174
|
+
* @remarks
|
|
175
|
+
* This method uses the signature algorithm determined by the given `algorithm` to sign the
|
|
176
|
+
* provided data.
|
|
177
|
+
*
|
|
178
|
+
* The signature can later be verified by parties with access to the corresponding
|
|
179
|
+
* public key, ensuring that the data has not been tampered with and was indeed signed by the
|
|
180
|
+
* holder of the private key.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* const ecdsa = new EcdsaAlgorithm();
|
|
185
|
+
* const data = new TextEncoder().encode('Message');
|
|
186
|
+
* const privateKey = { ... }; // A Jwk object representing a private key
|
|
187
|
+
* const signature = await ecdsa.sign({
|
|
188
|
+
* key: privateKey,
|
|
189
|
+
* data
|
|
190
|
+
* });
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @param params - The parameters for the signing operation.
|
|
194
|
+
* @param params.key - The private key to use for signing, represented in JWK format.
|
|
195
|
+
* @param params.data - The data to sign.
|
|
196
|
+
*
|
|
197
|
+
* @returns A Promise resolving to the digital signature as a `Uint8Array`.
|
|
198
|
+
*/
|
|
199
|
+
public async sign({ key, data }:
|
|
200
|
+
SignParams
|
|
201
|
+
): Promise<Uint8Array> {
|
|
202
|
+
if (!isEcPrivateJwk(key)) throw new TypeError('Invalid key provided. Must be an elliptic curve (EC) private key.');
|
|
203
|
+
|
|
204
|
+
switch (key.crv) {
|
|
205
|
+
|
|
206
|
+
case 'secp256k1': {
|
|
207
|
+
return await Secp256k1.sign({ key, data });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
case 'P-256': {
|
|
211
|
+
return await Secp256r1.sign({ key, data });
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
default: {
|
|
215
|
+
throw new Error(`Unsupported curve: ${key.crv}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Verifies an ECDSA signature associated with the provided data using the provided key.
|
|
222
|
+
*
|
|
223
|
+
* @remarks
|
|
224
|
+
* This method uses the signature algorithm determined by the `crv` property of the provided key
|
|
225
|
+
* to check the validity of a digital signature against the original data. It confirms whether the
|
|
226
|
+
* signature was created by the holder of the corresponding private key and that the data has not
|
|
227
|
+
* been tampered with.
|
|
228
|
+
*s
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* const ecdsa = new EcdsaAlgorithm();
|
|
232
|
+
* const publicKey = { ... }; // Public key in JWK format corresponding to the private key that signed the data
|
|
233
|
+
* const signature = new Uint8Array([...]); // Signature to verify
|
|
234
|
+
* const data = new TextEncoder().encode('Message');
|
|
235
|
+
* const isValid = await ecdsa.verify({
|
|
236
|
+
* key: publicKey,
|
|
237
|
+
* signature,
|
|
238
|
+
* data
|
|
239
|
+
* });
|
|
240
|
+
* ```
|
|
241
|
+
*
|
|
242
|
+
* @param params - The parameters for the verification operation.
|
|
243
|
+
* @param params.key - The key to use for verification.
|
|
244
|
+
* @param params.signature - The signature to verify.
|
|
245
|
+
* @param params.data - The data to verify.
|
|
246
|
+
*
|
|
247
|
+
* @returns A Promise resolving to a boolean indicating whether the signature is valid.
|
|
248
|
+
*/
|
|
249
|
+
public async verify({ key, signature, data }:
|
|
250
|
+
VerifyParams
|
|
251
|
+
): Promise<boolean> {
|
|
252
|
+
if (!isEcPublicJwk(key)) throw new TypeError('Invalid key provided. Must be an elliptic curve (EC) public key.');
|
|
253
|
+
|
|
254
|
+
switch (key.crv) {
|
|
255
|
+
|
|
256
|
+
case 'secp256k1': {
|
|
257
|
+
return await Secp256k1.verify({ key, signature, data });
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
case 'P-256': {
|
|
261
|
+
return await Secp256r1.verify({ key, signature, data });
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
default: {
|
|
265
|
+
throw new Error(`Unsupported curve: ${key.crv}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|