@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
package/src/algorithms/sha-2.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { Hasher } from '../types/hasher.js';
|
|
2
1
|
import type { DigestParams } from '../types/params-direct.js';
|
|
2
|
+
import type { Hasher } from '../types/hasher.js';
|
|
3
3
|
|
|
4
|
-
import { Sha256 } from '../primitives/sha256.js';
|
|
5
4
|
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
5
|
+
import { Sha256 } from '../primitives/sha256.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* The `Sha2DigestParams` interface defines the algorithm-specific parameters that should be
|
|
@@ -22,7 +22,7 @@ export interface Sha2DigestParams extends DigestParams {
|
|
|
22
22
|
* of the hash function and arbitrary data as input and returns the hash digest of the data.
|
|
23
23
|
*
|
|
24
24
|
* This class is typically accessed through implementations that extend the
|
|
25
|
-
* {@link
|
|
25
|
+
* {@link DsaApi | `DsaApi`} interface.
|
|
26
26
|
*/
|
|
27
27
|
export class Sha2Algorithm extends CryptoAlgorithm
|
|
28
28
|
implements Hasher<Sha2DigestParams> {
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { AsymmetricKeyGenerator } from '../types/key-generator.js';
|
|
2
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
3
|
+
import type { KeyConverter } from '../types/key-converter.js';
|
|
4
|
+
import type {
|
|
5
|
+
BytesToPrivateKeyParams,
|
|
6
|
+
ComputePublicKeyParams,
|
|
7
|
+
GenerateKeyParams,
|
|
8
|
+
GetPublicKeyParams,
|
|
9
|
+
PrivateKeyToBytesParams,
|
|
10
|
+
} from '../types/params-direct.js';
|
|
11
|
+
|
|
12
|
+
import { CryptoAlgorithm } from './crypto-algorithm.js';
|
|
13
|
+
import { isOkpPrivateJwk } from '../jose/jwk.js';
|
|
14
|
+
import { X25519 } from '../primitives/x25519.js';
|
|
15
|
+
|
|
16
|
+
import { CryptoError, CryptoErrorCode } from '../crypto-error.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The `X25519GenerateKeyParams` interface defines the algorithm-specific parameters that should be
|
|
20
|
+
* passed into the `generateKey()` method when using the X25519 key agreement algorithm.
|
|
21
|
+
*/
|
|
22
|
+
export interface X25519GenerateKeyParams extends GenerateKeyParams {
|
|
23
|
+
/**
|
|
24
|
+
* A string defining the type of key to generate. The value must be:
|
|
25
|
+
* - `"X25519"`: Elliptic-curve Diffie-Hellman (ECDH) using Curve25519.
|
|
26
|
+
*/
|
|
27
|
+
algorithm: 'X25519';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The `X25519Algorithm` class provides a concrete implementation for key generation,
|
|
32
|
+
* public key derivation, and key conversion using the X25519 elliptic curve. X25519 is a
|
|
33
|
+
* key agreement curve (not a signature curve) used for ECDH key exchange in JWE encryption.
|
|
34
|
+
*
|
|
35
|
+
* This class implements the {@link AsymmetricKeyGenerator | `AsymmetricKeyGenerator`} and
|
|
36
|
+
* {@link KeyConverter | `KeyConverter`} interfaces, providing private key generation,
|
|
37
|
+
* public key derivation, and byte/JWK conversion.
|
|
38
|
+
*/
|
|
39
|
+
export class X25519Algorithm extends CryptoAlgorithm
|
|
40
|
+
implements AsymmetricKeyGenerator<X25519GenerateKeyParams, Jwk, GetPublicKeyParams>,
|
|
41
|
+
KeyConverter {
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Converts a raw private key in bytes to its corresponding JWK format.
|
|
45
|
+
*
|
|
46
|
+
* @param params - The parameters for the private key conversion.
|
|
47
|
+
* @param params.algorithm - Must be `'X25519'`.
|
|
48
|
+
* @param params.privateKeyBytes - The raw private key as a Uint8Array.
|
|
49
|
+
*
|
|
50
|
+
* @returns A Promise that resolves to the private key in JWK format.
|
|
51
|
+
*/
|
|
52
|
+
public async bytesToPrivateKey({ algorithm, privateKeyBytes }:
|
|
53
|
+
BytesToPrivateKeyParams & { algorithm: 'X25519' }
|
|
54
|
+
): Promise<Jwk> {
|
|
55
|
+
switch (algorithm) {
|
|
56
|
+
|
|
57
|
+
case 'X25519': {
|
|
58
|
+
return X25519.bytesToPrivateKey({ privateKeyBytes });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
default: {
|
|
62
|
+
throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Algorithm not supported: ${algorithm}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Derives the public key in JWK format from a given X25519 private key.
|
|
69
|
+
*
|
|
70
|
+
* @param params - The parameters for the public key derivation.
|
|
71
|
+
* @param params.key - The private key in JWK format from which to derive the public key.
|
|
72
|
+
*
|
|
73
|
+
* @returns A Promise that resolves to the derived public key in JWK format.
|
|
74
|
+
*/
|
|
75
|
+
public async computePublicKey({ key }:
|
|
76
|
+
ComputePublicKeyParams
|
|
77
|
+
): Promise<Jwk> {
|
|
78
|
+
if (!isOkpPrivateJwk(key)) {throw new TypeError('Invalid key provided. Must be an octet key pair (OKP) private key.');}
|
|
79
|
+
|
|
80
|
+
switch (key.crv) {
|
|
81
|
+
|
|
82
|
+
case 'X25519': {
|
|
83
|
+
return X25519.computePublicKey({ key });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
default: {
|
|
87
|
+
throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Unsupported curve: ${key.crv}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Generates a new X25519 private key in JWK format.
|
|
94
|
+
*
|
|
95
|
+
* @param params - The parameters for key generation.
|
|
96
|
+
* @param params.algorithm - Must be `'X25519'`.
|
|
97
|
+
*
|
|
98
|
+
* @returns A Promise that resolves to the generated private key in JWK format.
|
|
99
|
+
*/
|
|
100
|
+
async generateKey({ algorithm }:
|
|
101
|
+
X25519GenerateKeyParams
|
|
102
|
+
): Promise<Jwk> {
|
|
103
|
+
switch (algorithm) {
|
|
104
|
+
|
|
105
|
+
case 'X25519': {
|
|
106
|
+
return X25519.generateKey();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
default: {
|
|
110
|
+
throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Algorithm not supported: ${algorithm}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Retrieves the public key properties from a given X25519 private key in JWK format.
|
|
117
|
+
*
|
|
118
|
+
* @param params - The parameters for retrieving the public key properties.
|
|
119
|
+
* @param params.key - The private key in JWK format.
|
|
120
|
+
*
|
|
121
|
+
* @returns A Promise that resolves to the public key in JWK format.
|
|
122
|
+
*/
|
|
123
|
+
public async getPublicKey({ key }:
|
|
124
|
+
GetPublicKeyParams
|
|
125
|
+
): Promise<Jwk> {
|
|
126
|
+
if (!isOkpPrivateJwk(key)) {throw new TypeError('Invalid key provided. Must be an octet key pair (OKP) private key.');}
|
|
127
|
+
|
|
128
|
+
switch (key.crv) {
|
|
129
|
+
|
|
130
|
+
case 'X25519': {
|
|
131
|
+
return X25519.getPublicKey({ key });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
default: {
|
|
135
|
+
throw new CryptoError(CryptoErrorCode.AlgorithmNotSupported, `Unsupported curve: ${key.crv}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Converts a private key from JWK format to a byte array.
|
|
142
|
+
*
|
|
143
|
+
* @param params - The parameters for the private key conversion.
|
|
144
|
+
* @param params.privateKey - The private key in JWK format.
|
|
145
|
+
*
|
|
146
|
+
* @returns A Promise that resolves to the private key as a Uint8Array.
|
|
147
|
+
*/
|
|
148
|
+
public async privateKeyToBytes({ privateKey }:
|
|
149
|
+
PrivateKeyToBytesParams
|
|
150
|
+
): Promise<Uint8Array> {
|
|
151
|
+
return X25519.privateKeyToBytes({ privateKey });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A custom error class for Crypto-related errors.
|
|
3
|
+
*/
|
|
4
|
+
export class CryptoError extends Error {
|
|
5
|
+
/**
|
|
6
|
+
* Constructs an instance of CryptoError, a custom error class for handling Crypto-related errors.
|
|
7
|
+
*
|
|
8
|
+
* @param code - A {@link CryptoErrorCode} representing the specific type of error encountered.
|
|
9
|
+
* @param message - A human-readable description of the error.
|
|
10
|
+
*/
|
|
11
|
+
constructor(public code: CryptoErrorCode, message: string) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'CryptoError';
|
|
14
|
+
|
|
15
|
+
// Ensures that instanceof works properly, the correct prototype chain when using inheritance,
|
|
16
|
+
// and that V8 stack traces (like Chrome, Edge, and Node.js) are more readable and relevant.
|
|
17
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
18
|
+
|
|
19
|
+
// Captures the stack trace in V8 engines (like Chrome, Edge, and Node.js).
|
|
20
|
+
// In non-V8 environments, the stack trace will still be captured.
|
|
21
|
+
if (Error.captureStackTrace) {
|
|
22
|
+
Error.captureStackTrace(this, CryptoError);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* An enumeration of possible Crypto error codes.
|
|
29
|
+
*/
|
|
30
|
+
export enum CryptoErrorCode {
|
|
31
|
+
/** The supplied algorithm identifier is not supported by the implementation. */
|
|
32
|
+
AlgorithmNotSupported = 'algorithmNotSupported',
|
|
33
|
+
|
|
34
|
+
/** The encoding operation (either encoding or decoding) failed. */
|
|
35
|
+
EncodingError = 'encodingError',
|
|
36
|
+
|
|
37
|
+
/** The JWE supplied does not conform to valid syntax. */
|
|
38
|
+
InvalidJwe = 'invalidJwe',
|
|
39
|
+
|
|
40
|
+
/** The JWK supplied does not conform to valid syntax. */
|
|
41
|
+
InvalidJwk = 'invalidJwk',
|
|
42
|
+
|
|
43
|
+
/** The requested operation is not supported by the implementation. */
|
|
44
|
+
OperationNotSupported = 'operationNotSupported',
|
|
45
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
+
export * from './crypto-error.js';
|
|
1
2
|
export * from './local-key-manager.js';
|
|
2
3
|
export * from './utils.js';
|
|
3
4
|
|
|
4
5
|
export * from './algorithms/aes-ctr.js';
|
|
5
6
|
export * from './algorithms/aes-gcm.js';
|
|
7
|
+
export * from './algorithms/aes-kw.js';
|
|
6
8
|
export * from './algorithms/crypto-algorithm.js';
|
|
7
9
|
export * from './algorithms/ecdsa.js';
|
|
8
10
|
export * from './algorithms/eddsa.js';
|
|
11
|
+
export * from './algorithms/hkdf.js';
|
|
12
|
+
export * from './algorithms/pbkdf2.js';
|
|
9
13
|
export * from './algorithms/sha-2.js';
|
|
14
|
+
export * from './algorithms/x25519.js';
|
|
10
15
|
|
|
11
16
|
export * from './jose/jwe.js';
|
|
12
17
|
export * from './jose/jwk.js';
|
|
@@ -16,8 +21,11 @@ export * from './jose/utils.js';
|
|
|
16
21
|
|
|
17
22
|
export * from './primitives/aes-ctr.js';
|
|
18
23
|
export * from './primitives/aes-gcm.js';
|
|
24
|
+
export * from './primitives/aes-kw.js';
|
|
19
25
|
export * from './primitives/concat-kdf.js';
|
|
26
|
+
export * from './primitives/ecies-secp256k1.js';
|
|
20
27
|
export * from './primitives/ed25519.js';
|
|
28
|
+
export * from './primitives/hkdf.js';
|
|
21
29
|
export * from './primitives/secp256r1.js';
|
|
22
30
|
export * from './primitives/pbkdf2.js';
|
|
23
31
|
export * from './primitives/secp256k1.js';
|
package/src/jose/jwk.ts
CHANGED
|
@@ -106,7 +106,7 @@ export type JwkType =
|
|
|
106
106
|
* A type of public key that is used with algorithms such as EdDSA (Ed25519 and
|
|
107
107
|
* Ed448 curves) and ECDH (X25519 and X448 curves).
|
|
108
108
|
*/
|
|
109
|
-
| 'OKP'
|
|
109
|
+
| 'OKP';
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
112
|
* JSON Web Key Elliptic Curve
|
|
@@ -155,7 +155,7 @@ export type JwkParamsAnyKeyType = {
|
|
|
155
155
|
'x5t#S256'?: string;
|
|
156
156
|
/** JWK X.509 URL Parameter */
|
|
157
157
|
x5u?: string;
|
|
158
|
-
}
|
|
158
|
+
};
|
|
159
159
|
|
|
160
160
|
/** Parameters used with "EC" (elliptic curve) public keys. */
|
|
161
161
|
export type JwkParamsEcPublic = Omit<JwkParamsAnyKeyType, 'alg' | 'kty'> & {
|
|
@@ -194,7 +194,7 @@ export type JwkParamsEcPublic = Omit<JwkParamsAnyKeyType, 'alg' | 'kty'> & {
|
|
|
194
194
|
* MUST be present only for secp256k1 public keys.
|
|
195
195
|
*/
|
|
196
196
|
y?: string;
|
|
197
|
-
}
|
|
197
|
+
};
|
|
198
198
|
|
|
199
199
|
/** Parameters used with "EC" (elliptic curve) private keys. */
|
|
200
200
|
export type JwkParamsEcPrivate = JwkParamsEcPublic & {
|
|
@@ -205,7 +205,7 @@ export type JwkParamsEcPrivate = JwkParamsEcPublic & {
|
|
|
205
205
|
* MUST be present for all EC private keys.
|
|
206
206
|
*/
|
|
207
207
|
d: string;
|
|
208
|
-
}
|
|
208
|
+
};
|
|
209
209
|
|
|
210
210
|
/** Parameters used with "OKP" (octet key pair) public keys. */
|
|
211
211
|
export type JwkParamsOkpPublic =
|
|
@@ -229,7 +229,7 @@ export type JwkParamsOkpPublic =
|
|
|
229
229
|
* strings as private and public keys.
|
|
230
230
|
*/
|
|
231
231
|
kty: 'OKP';
|
|
232
|
-
}
|
|
232
|
+
};
|
|
233
233
|
|
|
234
234
|
/** Parameters used with "OKP" (octet key pair) private keys. */
|
|
235
235
|
export type JwkParamsOkpPrivate = JwkParamsOkpPublic & {
|
|
@@ -288,7 +288,7 @@ export type JwkParamsOctPrivate = Omit<JwkParamsAnyKeyType, 'alg' | 'kty'> & {
|
|
|
288
288
|
* symmetric signature algorithms.
|
|
289
289
|
*/
|
|
290
290
|
kty: 'oct';
|
|
291
|
-
}
|
|
291
|
+
};
|
|
292
292
|
|
|
293
293
|
/** Parameters Used with "RSA" public keys. */
|
|
294
294
|
export type JwkParamsRsaPublic = Omit<JwkParamsAnyKeyType, 'kty'> & {
|
|
@@ -519,11 +519,11 @@ export async function computeJwkThumbprint({ jwk }: {
|
|
|
519
519
|
* @returns True if the object is a valid EC private JWK; otherwise, false.
|
|
520
520
|
*/
|
|
521
521
|
export function isEcPrivateJwk(obj: unknown): obj is JwkParamsEcPrivate {
|
|
522
|
-
if (!obj || typeof obj !== 'object') return false;
|
|
523
|
-
if (!('kty' in obj && 'crv' in obj && 'x' in obj && 'd' in obj)) return false;
|
|
524
|
-
if (obj.kty !== 'EC') return false;
|
|
525
|
-
if (typeof obj.d !== 'string') return false;
|
|
526
|
-
if (typeof obj.x !== 'string') return false;
|
|
522
|
+
if (!obj || typeof obj !== 'object') {return false;}
|
|
523
|
+
if (!('kty' in obj && 'crv' in obj && 'x' in obj && 'd' in obj)) {return false;}
|
|
524
|
+
if (obj.kty !== 'EC') {return false;}
|
|
525
|
+
if (typeof obj.d !== 'string') {return false;}
|
|
526
|
+
if (typeof obj.x !== 'string') {return false;}
|
|
527
527
|
return true;
|
|
528
528
|
}
|
|
529
529
|
|
|
@@ -534,11 +534,11 @@ export function isEcPrivateJwk(obj: unknown): obj is JwkParamsEcPrivate {
|
|
|
534
534
|
* @returns True if the object is a valid EC public JWK; otherwise, false.
|
|
535
535
|
*/
|
|
536
536
|
export function isEcPublicJwk(obj: unknown): obj is JwkParamsEcPublic {
|
|
537
|
-
if (!obj || typeof obj !== 'object') return false;
|
|
538
|
-
if (!('kty' in obj && 'crv' in obj && 'x' in obj)) return false;
|
|
539
|
-
if ('d' in obj) return false;
|
|
540
|
-
if (obj.kty !== 'EC') return false;
|
|
541
|
-
if (typeof obj.x !== 'string') return false;
|
|
537
|
+
if (!obj || typeof obj !== 'object') {return false;}
|
|
538
|
+
if (!('kty' in obj && 'crv' in obj && 'x' in obj)) {return false;}
|
|
539
|
+
if ('d' in obj) {return false;}
|
|
540
|
+
if (obj.kty !== 'EC') {return false;}
|
|
541
|
+
if (typeof obj.x !== 'string') {return false;}
|
|
542
542
|
return true;
|
|
543
543
|
}
|
|
544
544
|
|
|
@@ -549,10 +549,10 @@ export function isEcPublicJwk(obj: unknown): obj is JwkParamsEcPublic {
|
|
|
549
549
|
* @returns True if the object is a valid oct private JWK; otherwise, false.
|
|
550
550
|
*/
|
|
551
551
|
export function isOctPrivateJwk(obj: unknown): obj is JwkParamsOctPrivate {
|
|
552
|
-
if (!obj || typeof obj !== 'object') return false;
|
|
553
|
-
if (!('kty' in obj && 'k' in obj)) return false;
|
|
554
|
-
if (obj.kty !== 'oct') return false;
|
|
555
|
-
if (typeof obj.k !== 'string') return false;
|
|
552
|
+
if (!obj || typeof obj !== 'object') {return false;}
|
|
553
|
+
if (!('kty' in obj && 'k' in obj)) {return false;}
|
|
554
|
+
if (obj.kty !== 'oct') {return false;}
|
|
555
|
+
if (typeof obj.k !== 'string') {return false;}
|
|
556
556
|
return true;
|
|
557
557
|
}
|
|
558
558
|
|
|
@@ -563,11 +563,11 @@ export function isOctPrivateJwk(obj: unknown): obj is JwkParamsOctPrivate {
|
|
|
563
563
|
* @returns True if the object is a valid OKP private JWK; otherwise, false.
|
|
564
564
|
*/
|
|
565
565
|
export function isOkpPrivateJwk(obj: unknown): obj is JwkParamsOkpPrivate {
|
|
566
|
-
if (!obj || typeof obj !== 'object') return false;
|
|
567
|
-
if (!('kty' in obj && 'crv' in obj && 'x' in obj && 'd' in obj)) return false;
|
|
568
|
-
if (obj.kty !== 'OKP') return false;
|
|
569
|
-
if (typeof obj.d !== 'string') return false;
|
|
570
|
-
if (typeof obj.x !== 'string') return false;
|
|
566
|
+
if (!obj || typeof obj !== 'object') {return false;}
|
|
567
|
+
if (!('kty' in obj && 'crv' in obj && 'x' in obj && 'd' in obj)) {return false;}
|
|
568
|
+
if (obj.kty !== 'OKP') {return false;}
|
|
569
|
+
if (typeof obj.d !== 'string') {return false;}
|
|
570
|
+
if (typeof obj.x !== 'string') {return false;}
|
|
571
571
|
return true;
|
|
572
572
|
}
|
|
573
573
|
|
|
@@ -578,11 +578,11 @@ export function isOkpPrivateJwk(obj: unknown): obj is JwkParamsOkpPrivate {
|
|
|
578
578
|
* @returns True if the object is a valid OKP public JWK; otherwise, false.
|
|
579
579
|
*/
|
|
580
580
|
export function isOkpPublicJwk(obj: unknown): obj is JwkParamsOkpPublic {
|
|
581
|
-
if (!obj || typeof obj !== 'object') return false;
|
|
582
|
-
if ('d' in obj) return false;
|
|
583
|
-
if (!('kty' in obj && 'crv' in obj && 'x' in obj)) return false;
|
|
584
|
-
if (obj.kty !== 'OKP') return false;
|
|
585
|
-
if (typeof obj.x !== 'string') return false;
|
|
581
|
+
if (!obj || typeof obj !== 'object') {return false;}
|
|
582
|
+
if ('d' in obj) {return false;}
|
|
583
|
+
if (!('kty' in obj && 'crv' in obj && 'x' in obj)) {return false;}
|
|
584
|
+
if (obj.kty !== 'OKP') {return false;}
|
|
585
|
+
if (typeof obj.x !== 'string') {return false;}
|
|
586
586
|
return true;
|
|
587
587
|
}
|
|
588
588
|
|
|
@@ -593,7 +593,7 @@ export function isOkpPublicJwk(obj: unknown): obj is JwkParamsOkpPublic {
|
|
|
593
593
|
* @returns True if the object is a valid private JWK; otherwise, false.
|
|
594
594
|
*/
|
|
595
595
|
export function isPrivateJwk(obj: unknown): obj is PrivateKeyJwk {
|
|
596
|
-
if (!obj || typeof obj !== 'object') return false;
|
|
596
|
+
if (!obj || typeof obj !== 'object') {return false;}
|
|
597
597
|
|
|
598
598
|
const kty = (obj as { kty: string }).kty;
|
|
599
599
|
|
|
@@ -616,7 +616,7 @@ export function isPrivateJwk(obj: unknown): obj is PrivateKeyJwk {
|
|
|
616
616
|
* @returns True if the object is a valid public JWK; otherwise, false.
|
|
617
617
|
*/
|
|
618
618
|
export function isPublicJwk(obj: unknown): obj is PublicKeyJwk {
|
|
619
|
-
if (!obj || typeof obj !== 'object') return false;
|
|
619
|
+
if (!obj || typeof obj !== 'object') {return false;}
|
|
620
620
|
|
|
621
621
|
const kty = (obj as { kty: string }).kty;
|
|
622
622
|
|
package/src/local-key-manager.ts
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
1
|
-
import { KeyValueStore
|
|
1
|
+
import type { KeyValueStore } from '@enbox/common';
|
|
2
|
+
import { MemoryStore } from '@enbox/common';
|
|
2
3
|
|
|
3
|
-
import type {
|
|
4
|
+
import type { CryptoAlgorithm } from './algorithms/crypto-algorithm.js';
|
|
4
5
|
import type { Hasher } from './types/hasher.js';
|
|
5
|
-
import type {
|
|
6
|
-
import type { CryptoApi } from './types/crypto-api.js';
|
|
6
|
+
import type { Jwk } from './jose/jwk.js';
|
|
7
7
|
import type { KeyIdentifier } from './types/identifier.js';
|
|
8
8
|
import type { KeyImporterExporter } from './types/key-io.js';
|
|
9
|
-
import type {
|
|
9
|
+
import type { KeyManager } from './types/crypto-api.js';
|
|
10
|
+
import type { Signer } from './types/signer.js';
|
|
11
|
+
import type { AsymmetricKeyGenerator, KeyGenerator } from './types/key-generator.js';
|
|
10
12
|
import type { GetPublicKeyParams, SignParams, VerifyParams } from './types/params-direct.js';
|
|
11
13
|
import type {
|
|
12
|
-
KmsSignParams,
|
|
13
14
|
KmsDigestParams,
|
|
14
|
-
KmsVerifyParams,
|
|
15
15
|
KmsExportKeyParams,
|
|
16
|
-
KmsGetKeyUriParams,
|
|
17
|
-
KmsImportKeyParams,
|
|
18
16
|
KmsGenerateKeyParams,
|
|
17
|
+
KmsGetKeyUriParams,
|
|
19
18
|
KmsGetPublicKeyParams,
|
|
19
|
+
KmsImportKeyParams,
|
|
20
|
+
KmsSignParams,
|
|
21
|
+
KmsVerifyParams,
|
|
20
22
|
} from './types/params-kms.js';
|
|
21
23
|
|
|
22
|
-
import { Sha2Algorithm } from './algorithms/sha-2.js';
|
|
23
24
|
import { EcdsaAlgorithm } from './algorithms/ecdsa.js';
|
|
24
25
|
import { EdDsaAlgorithm } from './algorithms/eddsa.js';
|
|
25
|
-
import {
|
|
26
|
+
import { Sha2Algorithm } from './algorithms/sha-2.js';
|
|
27
|
+
import { X25519Algorithm } from './algorithms/x25519.js';
|
|
26
28
|
import { computeJwkThumbprint, isPrivateJwk, KEY_URI_PREFIX_JWK } from './jose/jwk.js';
|
|
27
29
|
|
|
28
30
|
/**
|
|
@@ -49,11 +51,15 @@ const supportedAlgorithms = {
|
|
|
49
51
|
'SHA-256': {
|
|
50
52
|
implementation : Sha2Algorithm,
|
|
51
53
|
names : ['SHA-256']
|
|
54
|
+
},
|
|
55
|
+
'X25519': {
|
|
56
|
+
implementation : X25519Algorithm,
|
|
57
|
+
names : ['X25519']
|
|
52
58
|
}
|
|
53
59
|
} satisfies {
|
|
54
60
|
[key: string]: {
|
|
55
61
|
implementation : typeof CryptoAlgorithm;
|
|
56
|
-
names
|
|
62
|
+
names : string[];
|
|
57
63
|
}
|
|
58
64
|
};
|
|
59
65
|
|
|
@@ -103,11 +109,11 @@ export interface LocalKeyManagerGenerateKeyParams extends KmsGenerateKeyParams {
|
|
|
103
109
|
* - `"Ed25519"`
|
|
104
110
|
* - `"secp256k1"`
|
|
105
111
|
*/
|
|
106
|
-
algorithm: 'Ed25519' | 'secp256k1' | 'secp256r1';
|
|
112
|
+
algorithm: 'Ed25519' | 'secp256k1' | 'secp256r1' | 'X25519';
|
|
107
113
|
}
|
|
108
114
|
|
|
109
115
|
export class LocalKeyManager implements
|
|
110
|
-
|
|
116
|
+
KeyManager,
|
|
111
117
|
KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams> {
|
|
112
118
|
|
|
113
119
|
/**
|
|
@@ -241,7 +247,7 @@ export class LocalKeyManager implements
|
|
|
241
247
|
* @remarks
|
|
242
248
|
* This method generates a {@link https://datatracker.ietf.org/doc/html/rfc3986 | URI}
|
|
243
249
|
* (Uniform Resource Identifier) for the given JWK, which uniquely identifies the key across all
|
|
244
|
-
* `
|
|
250
|
+
* `KeyManager` implementations. The key URI is constructed by appending the
|
|
245
251
|
* {@link https://datatracker.ietf.org/doc/html/rfc7638 | JWK thumbprint} to the prefix
|
|
246
252
|
* `urn:jwk:`. The JWK thumbprint is deterministically computed from the JWK and is consistent
|
|
247
253
|
* regardless of property order or optional property inclusion in the JWK. This ensures that the
|
|
@@ -335,7 +341,7 @@ export class LocalKeyManager implements
|
|
|
335
341
|
public async importKey({ key }:
|
|
336
342
|
KmsImportKeyParams
|
|
337
343
|
): Promise<KeyIdentifier> {
|
|
338
|
-
if (!isPrivateJwk(key)) throw new TypeError('Invalid key provided. Must be a private key in JWK format.');
|
|
344
|
+
if (!isPrivateJwk(key)) {throw new TypeError('Invalid key provided. Must be a private key in JWK format.');}
|
|
339
345
|
|
|
340
346
|
// Make a deep copy of the key to avoid mutating the original.
|
|
341
347
|
const privateKey = structuredClone(key);
|
|
@@ -296,7 +296,7 @@ export class AesCtr {
|
|
|
296
296
|
length: typeof AES_KEY_LENGTHS[number];
|
|
297
297
|
}): Promise<Jwk> {
|
|
298
298
|
// Validate the key length.
|
|
299
|
-
if (!AES_KEY_LENGTHS.includes(length
|
|
299
|
+
if (!(AES_KEY_LENGTHS as readonly number[]).includes(length)) {
|
|
300
300
|
throw new RangeError(`The key length is invalid: Must be ${AES_KEY_LENGTHS.join(', ')} bits`);
|
|
301
301
|
}
|
|
302
302
|
|
|
@@ -187,7 +187,7 @@ export class AesGcm {
|
|
|
187
187
|
}
|
|
188
188
|
|
|
189
189
|
// Validate the tag length.
|
|
190
|
-
if (tagLength && !AES_GCM_TAG_LENGTHS.includes(tagLength
|
|
190
|
+
if (tagLength && !(AES_GCM_TAG_LENGTHS as readonly number[]).includes(tagLength)) {
|
|
191
191
|
throw new RangeError(`The tag length is invalid: Must be ${AES_GCM_TAG_LENGTHS.join(', ')} bits`);
|
|
192
192
|
}
|
|
193
193
|
|
|
@@ -203,7 +203,7 @@ export class AesGcm {
|
|
|
203
203
|
name: 'AES-GCM',
|
|
204
204
|
iv,
|
|
205
205
|
...(tagLength && { tagLength }),
|
|
206
|
-
...(additionalData && { additionalData})
|
|
206
|
+
...(additionalData && { additionalData })
|
|
207
207
|
};
|
|
208
208
|
|
|
209
209
|
// Decrypt the data.
|
|
@@ -263,7 +263,7 @@ export class AesGcm {
|
|
|
263
263
|
}
|
|
264
264
|
|
|
265
265
|
// Validate the tag length.
|
|
266
|
-
if (tagLength && !AES_GCM_TAG_LENGTHS.includes(tagLength
|
|
266
|
+
if (tagLength && !(AES_GCM_TAG_LENGTHS as readonly number[]).includes(tagLength)) {
|
|
267
267
|
throw new RangeError(`The tag length is invalid: Must be ${AES_GCM_TAG_LENGTHS.join(', ')} bits`);
|
|
268
268
|
}
|
|
269
269
|
|
|
@@ -279,7 +279,7 @@ export class AesGcm {
|
|
|
279
279
|
name: 'AES-GCM',
|
|
280
280
|
iv,
|
|
281
281
|
...(tagLength && { tagLength }),
|
|
282
|
-
...(additionalData && { additionalData})
|
|
282
|
+
...(additionalData && { additionalData })
|
|
283
283
|
};
|
|
284
284
|
|
|
285
285
|
// Encrypt the data.
|
|
@@ -321,7 +321,7 @@ export class AesGcm {
|
|
|
321
321
|
length: typeof AES_KEY_LENGTHS[number];
|
|
322
322
|
}): Promise<Jwk> {
|
|
323
323
|
// Validate the key length.
|
|
324
|
-
if (!AES_KEY_LENGTHS.includes(length
|
|
324
|
+
if (!(AES_KEY_LENGTHS as readonly number[]).includes(length)) {
|
|
325
325
|
throw new RangeError(`The key length is invalid: Must be ${AES_KEY_LENGTHS.join(', ')} bits`);
|
|
326
326
|
}
|
|
327
327
|
|