@enbox/crypto 0.0.3 → 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/dist/browser.mjs +1 -1
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/algorithms/aes-ctr.js +1 -1
- package/dist/esm/algorithms/aes-gcm.js +34 -1
- 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 +110 -1
- package/dist/esm/algorithms/ecdsa.js.map +1 -1
- package/dist/esm/algorithms/eddsa.js +90 -1
- 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 +1 -1
- package/dist/esm/algorithms/x25519.js +125 -0
- package/dist/esm/algorithms/x25519.js.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/local-key-manager.js +6 -1
- package/dist/esm/local-key-manager.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/x25519.js +9 -16
- package/dist/esm/primitives/x25519.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 +1 -1
- package/dist/types/algorithms/aes-gcm.d.ts +23 -3
- 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 +48 -3
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
- package/dist/types/algorithms/eddsa.d.ts +48 -3
- 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 +1 -1
- package/dist/types/algorithms/x25519.d.ts +76 -0
- package/dist/types/algorithms/x25519.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/local-key-manager.d.ts +4 -4
- package/dist/types/local-key-manager.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/x25519.d.ts +9 -16
- package/dist/types/primitives/x25519.d.ts.map +1 -1
- package/dist/types/types/crypto-api.d.ts +52 -4
- 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 +17 -0
- 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 +3 -3
- package/package.json +10 -13
- package/src/algorithms/aes-ctr.ts +1 -1
- package/src/algorithms/aes-gcm.ts +38 -2
- package/src/algorithms/aes-kw.ts +182 -0
- package/src/algorithms/ecdsa.ts +132 -1
- package/src/algorithms/eddsa.ts +108 -1
- package/src/algorithms/hkdf.ts +54 -0
- package/src/algorithms/pbkdf2.ts +57 -0
- package/src/algorithms/sha-2.ts +1 -1
- package/src/algorithms/x25519.ts +153 -0
- package/src/index.ts +5 -0
- package/src/local-key-manager.ts +9 -4
- package/src/primitives/ecies-secp256k1.ts +113 -0
- package/src/primitives/x25519.ts +9 -16
- package/src/types/crypto-api.ts +124 -6
- 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 +21 -0
- package/src/types/params-kms.ts +67 -0
- package/src/utils.ts +53 -0
- package/dist/browser.js +0 -60
- package/dist/browser.js.map +0 -7
|
@@ -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
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -4,10 +4,14 @@ export * from './utils.js';
|
|
|
4
4
|
|
|
5
5
|
export * from './algorithms/aes-ctr.js';
|
|
6
6
|
export * from './algorithms/aes-gcm.js';
|
|
7
|
+
export * from './algorithms/aes-kw.js';
|
|
7
8
|
export * from './algorithms/crypto-algorithm.js';
|
|
8
9
|
export * from './algorithms/ecdsa.js';
|
|
9
10
|
export * from './algorithms/eddsa.js';
|
|
11
|
+
export * from './algorithms/hkdf.js';
|
|
12
|
+
export * from './algorithms/pbkdf2.js';
|
|
10
13
|
export * from './algorithms/sha-2.js';
|
|
14
|
+
export * from './algorithms/x25519.js';
|
|
11
15
|
|
|
12
16
|
export * from './jose/jwe.js';
|
|
13
17
|
export * from './jose/jwk.js';
|
|
@@ -19,6 +23,7 @@ export * from './primitives/aes-ctr.js';
|
|
|
19
23
|
export * from './primitives/aes-gcm.js';
|
|
20
24
|
export * from './primitives/aes-kw.js';
|
|
21
25
|
export * from './primitives/concat-kdf.js';
|
|
26
|
+
export * from './primitives/ecies-secp256k1.js';
|
|
22
27
|
export * from './primitives/ed25519.js';
|
|
23
28
|
export * from './primitives/hkdf.js';
|
|
24
29
|
export * from './primitives/secp256r1.js';
|
package/src/local-key-manager.ts
CHANGED
|
@@ -2,11 +2,11 @@ import type { KeyValueStore } from '@enbox/common';
|
|
|
2
2
|
import { MemoryStore } from '@enbox/common';
|
|
3
3
|
|
|
4
4
|
import type { CryptoAlgorithm } from './algorithms/crypto-algorithm.js';
|
|
5
|
-
import type { CryptoApi } from './types/crypto-api.js';
|
|
6
5
|
import type { Hasher } from './types/hasher.js';
|
|
7
6
|
import type { Jwk } from './jose/jwk.js';
|
|
8
7
|
import type { KeyIdentifier } from './types/identifier.js';
|
|
9
8
|
import type { KeyImporterExporter } from './types/key-io.js';
|
|
9
|
+
import type { KeyManager } from './types/crypto-api.js';
|
|
10
10
|
import type { Signer } from './types/signer.js';
|
|
11
11
|
import type { AsymmetricKeyGenerator, KeyGenerator } from './types/key-generator.js';
|
|
12
12
|
import type { GetPublicKeyParams, SignParams, VerifyParams } from './types/params-direct.js';
|
|
@@ -24,6 +24,7 @@ import type {
|
|
|
24
24
|
import { EcdsaAlgorithm } from './algorithms/ecdsa.js';
|
|
25
25
|
import { EdDsaAlgorithm } from './algorithms/eddsa.js';
|
|
26
26
|
import { Sha2Algorithm } from './algorithms/sha-2.js';
|
|
27
|
+
import { X25519Algorithm } from './algorithms/x25519.js';
|
|
27
28
|
import { computeJwkThumbprint, isPrivateJwk, KEY_URI_PREFIX_JWK } from './jose/jwk.js';
|
|
28
29
|
|
|
29
30
|
/**
|
|
@@ -50,6 +51,10 @@ const supportedAlgorithms = {
|
|
|
50
51
|
'SHA-256': {
|
|
51
52
|
implementation : Sha2Algorithm,
|
|
52
53
|
names : ['SHA-256']
|
|
54
|
+
},
|
|
55
|
+
'X25519': {
|
|
56
|
+
implementation : X25519Algorithm,
|
|
57
|
+
names : ['X25519']
|
|
53
58
|
}
|
|
54
59
|
} satisfies {
|
|
55
60
|
[key: string]: {
|
|
@@ -104,11 +109,11 @@ export interface LocalKeyManagerGenerateKeyParams extends KmsGenerateKeyParams {
|
|
|
104
109
|
* - `"Ed25519"`
|
|
105
110
|
* - `"secp256k1"`
|
|
106
111
|
*/
|
|
107
|
-
algorithm: 'Ed25519' | 'secp256k1' | 'secp256r1';
|
|
112
|
+
algorithm: 'Ed25519' | 'secp256k1' | 'secp256r1' | 'X25519';
|
|
108
113
|
}
|
|
109
114
|
|
|
110
115
|
export class LocalKeyManager implements
|
|
111
|
-
|
|
116
|
+
KeyManager,
|
|
112
117
|
KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams> {
|
|
113
118
|
|
|
114
119
|
/**
|
|
@@ -242,7 +247,7 @@ export class LocalKeyManager implements
|
|
|
242
247
|
* @remarks
|
|
243
248
|
* This method generates a {@link https://datatracker.ietf.org/doc/html/rfc3986 | URI}
|
|
244
249
|
* (Uniform Resource Identifier) for the given JWK, which uniquely identifies the key across all
|
|
245
|
-
* `
|
|
250
|
+
* `KeyManager` implementations. The key URI is constructed by appending the
|
|
246
251
|
* {@link https://datatracker.ietf.org/doc/html/rfc7638 | JWK thumbprint} to the prefix
|
|
247
252
|
* `urn:jwk:`. The JWK thumbprint is deterministically computed from the JWK and is consistent
|
|
248
253
|
* regardless of property order or optional property inclusion in the JWK. This ensures that the
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { concatBytes } from '@noble/ciphers/utils';
|
|
2
|
+
import { gcm } from '@noble/ciphers/aes';
|
|
3
|
+
import { hkdf } from '@noble/hashes/hkdf';
|
|
4
|
+
import { randomBytes } from '@noble/ciphers/webcrypto';
|
|
5
|
+
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
6
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* AEAD tag length for AES-256-GCM (16 bytes / 128 bits).
|
|
10
|
+
*/
|
|
11
|
+
const AEAD_TAG_LENGTH = 16;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Nonce length for AES-256-GCM encryption.
|
|
15
|
+
*/
|
|
16
|
+
const NONCE_LENGTH = 16;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Output of an ECIES-SECP256K1 encryption operation.
|
|
20
|
+
*/
|
|
21
|
+
export type EciesSecp256k1EncryptionOutput = {
|
|
22
|
+
/** The AES-GCM initialization vector. */
|
|
23
|
+
initializationVector: Uint8Array;
|
|
24
|
+
/** The ephemeral secp256k1 public key (compressed, 33 bytes). */
|
|
25
|
+
ephemeralPublicKey: Uint8Array;
|
|
26
|
+
/** The encrypted data. */
|
|
27
|
+
ciphertext: Uint8Array;
|
|
28
|
+
/** The AES-GCM authentication tag (16 bytes). */
|
|
29
|
+
messageAuthenticationCode: Uint8Array;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Input required for ECIES-SECP256K1 decryption.
|
|
34
|
+
*/
|
|
35
|
+
export type EciesSecp256k1EncryptionInput = EciesSecp256k1EncryptionOutput & {
|
|
36
|
+
/** The recipient's secp256k1 private key (raw 32 bytes). */
|
|
37
|
+
privateKey: Uint8Array;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Browser-compatible ECIES (Elliptic Curve Integrated Encryption Scheme) using secp256k1.
|
|
42
|
+
*
|
|
43
|
+
* Wire-format compatible with `eciesjs` v0.4.x configured with
|
|
44
|
+
* `isEphemeralKeyCompressed: true, isHkdfKeyCompressed: false` (the default).
|
|
45
|
+
*
|
|
46
|
+
* Protocol:
|
|
47
|
+
* 1. Generate an ephemeral secp256k1 key pair.
|
|
48
|
+
* 2. ECDH shared secret (uncompressed point).
|
|
49
|
+
* 3. HKDF-SHA-256 key derivation: `hkdf(sha256, ephemeralPubUncompressed || sharedPointUncompressed)`.
|
|
50
|
+
* 4. AES-256-GCM encryption with random 16-byte nonce.
|
|
51
|
+
*
|
|
52
|
+
* All underlying primitives (`@noble/ciphers`, `@noble/curves`, `@noble/hashes`)
|
|
53
|
+
* are pure JavaScript and work in Node, Bun, and browsers.
|
|
54
|
+
*/
|
|
55
|
+
export class EciesSecp256k1 {
|
|
56
|
+
/**
|
|
57
|
+
* Encrypt plaintext for a given secp256k1 public key.
|
|
58
|
+
* @param publicKeyBytes - Recipient's public key (compressed 33 bytes or uncompressed 65 bytes).
|
|
59
|
+
* @param plaintext - The data to encrypt.
|
|
60
|
+
*/
|
|
61
|
+
public static encrypt(publicKeyBytes: Uint8Array, plaintext: Uint8Array): EciesSecp256k1EncryptionOutput {
|
|
62
|
+
// Generate ephemeral key pair.
|
|
63
|
+
const ephemeralPrivateKey = secp256k1.utils.randomPrivateKey();
|
|
64
|
+
const ephemeralPubCompressed = secp256k1.getPublicKey(ephemeralPrivateKey, true);
|
|
65
|
+
const ephemeralPubUncompressed = secp256k1.getPublicKey(ephemeralPrivateKey, false);
|
|
66
|
+
|
|
67
|
+
// ECDH: shared point (uncompressed).
|
|
68
|
+
const sharedPointUncompressed = secp256k1.getSharedSecret(ephemeralPrivateKey, publicKeyBytes, false);
|
|
69
|
+
|
|
70
|
+
// HKDF-SHA-256: derive 32-byte symmetric key.
|
|
71
|
+
// eciesjs (isHkdfKeyCompressed=false): master = senderPubUncompressed || sharedPointUncompressed
|
|
72
|
+
const symmetricKey = hkdf(sha256, concatBytes(ephemeralPubUncompressed, sharedPointUncompressed), undefined, undefined, 32);
|
|
73
|
+
|
|
74
|
+
// AES-256-GCM encrypt.
|
|
75
|
+
const nonce = randomBytes(NONCE_LENGTH);
|
|
76
|
+
const ciphered = gcm(symmetricKey, nonce).encrypt(plaintext); // ciphertext || tag
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
ephemeralPublicKey : ephemeralPubCompressed,
|
|
80
|
+
initializationVector : nonce,
|
|
81
|
+
messageAuthenticationCode : ciphered.subarray(ciphered.length - AEAD_TAG_LENGTH),
|
|
82
|
+
ciphertext : ciphered.subarray(0, ciphered.length - AEAD_TAG_LENGTH),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Decrypt ciphertext produced by {@link EciesSecp256k1.encrypt}.
|
|
88
|
+
* @param input - The encryption output plus the recipient's private key.
|
|
89
|
+
*/
|
|
90
|
+
public static decrypt(input: EciesSecp256k1EncryptionInput): Uint8Array {
|
|
91
|
+
const { privateKey, ephemeralPublicKey, initializationVector, messageAuthenticationCode, ciphertext } = input;
|
|
92
|
+
|
|
93
|
+
// Decompress ephemeral public key (HKDF needs uncompressed form).
|
|
94
|
+
const ephemeralPubUncompressed = secp256k1.ProjectivePoint.fromHex(ephemeralPublicKey).toRawBytes(false);
|
|
95
|
+
|
|
96
|
+
// ECDH: shared point (uncompressed).
|
|
97
|
+
const sharedPointUncompressed = secp256k1.getSharedSecret(privateKey, ephemeralPublicKey, false);
|
|
98
|
+
|
|
99
|
+
// HKDF-SHA-256: derive 32-byte symmetric key (same derivation as encrypt).
|
|
100
|
+
const symmetricKey = hkdf(sha256, concatBytes(ephemeralPubUncompressed, sharedPointUncompressed), undefined, undefined, 32);
|
|
101
|
+
|
|
102
|
+
// AES-256-GCM decrypt: reconstruct the wire format (ciphertext || tag).
|
|
103
|
+
const ciphered = concatBytes(ciphertext, messageAuthenticationCode);
|
|
104
|
+
return gcm(symmetricKey, Uint8Array.from(initializationVector)).decrypt(ciphered);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Whether the ephemeral public key is compressed (always true for this implementation).
|
|
109
|
+
*/
|
|
110
|
+
public static get isEphemeralKeyCompressed(): boolean {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
}
|
package/src/primitives/x25519.ts
CHANGED
|
@@ -345,32 +345,25 @@ export class X25519 {
|
|
|
345
345
|
}
|
|
346
346
|
|
|
347
347
|
/**
|
|
348
|
-
* Computes an
|
|
349
|
-
* using
|
|
348
|
+
* Computes an X25519 Elliptic Curve Diffie-Hellman (ECDH) shared secret
|
|
349
|
+
* using X25519 private and public keys in JSON Web Key (JWK) format.
|
|
350
350
|
*
|
|
351
351
|
* @remarks
|
|
352
352
|
* This method facilitates the ECDH key agreement protocol, which is a method of securely
|
|
353
353
|
* deriving a shared secret between two parties based on their private and public keys.
|
|
354
354
|
* It takes the private key of one party (privateKeyA) and the public key of another
|
|
355
|
-
* party (publicKeyB) to compute a shared secret. The shared secret is
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
* consists of an x-coordinate and a y-coordinate. With a 256-bit curve like
|
|
362
|
-
* secp256k1, each of these coordinates is 32 bytes (256 bits) long. However,
|
|
363
|
-
* in the ECDH process, it's standard practice to use only the x-coordinate
|
|
364
|
-
* of the shared secret point as the resulting shared key. This is because
|
|
365
|
-
* the y-coordinate does not add to the entropy of the key, and both parties
|
|
366
|
-
* can independently compute the x-coordinate. Consquently, this implementation
|
|
367
|
-
* omits the y-coordinate for simplicity and standard compliance.
|
|
355
|
+
* party (publicKeyB) to compute a shared secret. The shared secret is the raw output
|
|
356
|
+
* of the X25519 function as defined in RFC 7748.
|
|
357
|
+
*
|
|
358
|
+
* Note: Unlike Weierstrass curves (e.g., secp256k1), X25519 is a Montgomery curve
|
|
359
|
+
* where the ECDH output is a single 32-byte scalar value, not an (x, y) point.
|
|
360
|
+
* The result is used directly as the shared secret.
|
|
368
361
|
*
|
|
369
362
|
* @example
|
|
370
363
|
* ```ts
|
|
371
364
|
* const privateKeyA = { ... }; // A Jwk object for party A
|
|
372
365
|
* const publicKeyB = { ... }; // A PublicKeyJwk object for party B
|
|
373
|
-
* const sharedSecret = await
|
|
366
|
+
* const sharedSecret = await X25519.sharedSecret({
|
|
374
367
|
* privateKeyA,
|
|
375
368
|
* publicKeyB
|
|
376
369
|
* });
|
package/src/types/crypto-api.ts
CHANGED
|
@@ -1,8 +1,30 @@
|
|
|
1
|
+
import type { AsymmetricKeyConverter } from './key-converter.js';
|
|
1
2
|
import type { AsymmetricKeyGenerator } from './key-generator.js';
|
|
3
|
+
import type { Cipher } from './cipher.js';
|
|
2
4
|
import type { Hasher } from './hasher.js';
|
|
5
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
3
6
|
import type { KeyIdentifier } from './identifier.js';
|
|
7
|
+
import type { KeyWrapper } from './key-wrapper.js';
|
|
4
8
|
import type { Signer } from './signer.js';
|
|
5
9
|
import type {
|
|
10
|
+
BytesToPrivateKeyParams,
|
|
11
|
+
BytesToPublicKeyParams,
|
|
12
|
+
CipherParams,
|
|
13
|
+
DeriveKeyBytesParams,
|
|
14
|
+
DeriveKeyFromBytesParams,
|
|
15
|
+
DigestParams,
|
|
16
|
+
GenerateKeyParams,
|
|
17
|
+
GetPublicKeyParams,
|
|
18
|
+
PrivateKeyToBytesParams,
|
|
19
|
+
PublicKeyToBytesParams,
|
|
20
|
+
SignParams,
|
|
21
|
+
UnwrapKeyParams,
|
|
22
|
+
VerifyParams,
|
|
23
|
+
WrapKeyParams,
|
|
24
|
+
} from './params-direct.js';
|
|
25
|
+
import type { KeyBytesDeriver, SimpleKeyDeriver } from './key-deriver.js';
|
|
26
|
+
import type {
|
|
27
|
+
KmsCipherParams,
|
|
6
28
|
KmsDigestParams,
|
|
7
29
|
KmsGenerateKeyParams,
|
|
8
30
|
KmsGetKeyUriParams,
|
|
@@ -12,7 +34,7 @@ import type {
|
|
|
12
34
|
} from './params-kms.js';
|
|
13
35
|
|
|
14
36
|
/**
|
|
15
|
-
* The `
|
|
37
|
+
* The `DsaApi` interface integrates key generation, hashing, and signing functionalities,
|
|
16
38
|
* designed for use with a Key Management System (KMS). It extends `AsymmetricKeyGenerator` for
|
|
17
39
|
* generating asymmetric keys, `Hasher` for hash digest computations, and `Signer` for signing and
|
|
18
40
|
* verifying operations.
|
|
@@ -32,25 +54,121 @@ import type {
|
|
|
32
54
|
* identifier (e.g. JWK thumbprint, UUID generated by hosted KMS, etc.).
|
|
33
55
|
* - Must support key generation, hashing, signing, and verifying operations.
|
|
34
56
|
* - May be extended to support other cryptographic operations.
|
|
35
|
-
* - Implementations of the `
|
|
57
|
+
* - Implementations of the `DsaApi` interface can be passed as an argument to the public API
|
|
36
58
|
* methods of Web5 libraries that involve key material (e.g., DID creation, VC signing, arbitrary
|
|
37
59
|
* data signing/verification, etc.).
|
|
38
60
|
*/
|
|
39
|
-
export interface
|
|
61
|
+
export interface DsaApi<
|
|
40
62
|
GenerateKeyInput = KmsGenerateKeyParams,
|
|
41
63
|
GenerateKeyOutput = KeyIdentifier,
|
|
42
64
|
GetPublicKeyInput = KmsGetPublicKeyParams,
|
|
43
65
|
DigestInput = KmsDigestParams,
|
|
44
66
|
SignInput = KmsSignParams,
|
|
45
67
|
VerifyInput = KmsVerifyParams
|
|
46
|
-
> extends AsymmetricKeyGenerator<GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput>,
|
|
68
|
+
> extends AsymmetricKeyGenerator<GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput>,
|
|
47
69
|
Hasher<DigestInput>,
|
|
48
|
-
Signer<SignInput, VerifyInput> {
|
|
70
|
+
Signer<SignInput, VerifyInput> {}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The `CryptoApi` interface extends {@link DsaApi} with encryption, key conversion,
|
|
74
|
+
* key derivation, and key wrapping capabilities.
|
|
75
|
+
*
|
|
76
|
+
* This is the full-featured cryptographic API used by agent-level code that needs direct-key
|
|
77
|
+
* cipher, key conversion, and key derivation operations beyond what the base `DsaApi` provides.
|
|
78
|
+
*/
|
|
79
|
+
export interface CryptoApi<
|
|
80
|
+
GenerateKeyInput = GenerateKeyParams,
|
|
81
|
+
GenerateKeyOutput = Jwk,
|
|
82
|
+
GetPublicKeyInput = GetPublicKeyParams,
|
|
83
|
+
DigestInput = DigestParams,
|
|
84
|
+
SignInput = SignParams,
|
|
85
|
+
VerifyInput = VerifyParams,
|
|
86
|
+
EncryptInput = CipherParams,
|
|
87
|
+
DecryptInput = CipherParams,
|
|
88
|
+
BytesToPublicKeyInput = BytesToPublicKeyParams,
|
|
89
|
+
PublicKeyToBytesInput = PublicKeyToBytesParams,
|
|
90
|
+
BytesToPrivateKeyInput = BytesToPrivateKeyParams,
|
|
91
|
+
PrivateKeyToBytesInput = PrivateKeyToBytesParams,
|
|
92
|
+
DeriveKeyInput = DeriveKeyFromBytesParams,
|
|
93
|
+
DeriveKeyOutput = Jwk,
|
|
94
|
+
DeriveKeyBytesInput = DeriveKeyBytesParams,
|
|
95
|
+
DeriveKeyBytesOutput = Uint8Array,
|
|
96
|
+
WrapKeyInput = WrapKeyParams,
|
|
97
|
+
UnwrapKeyInput = UnwrapKeyParams
|
|
98
|
+
> extends
|
|
99
|
+
DsaApi<GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput, DigestInput, SignInput, VerifyInput>,
|
|
100
|
+
Cipher<EncryptInput, DecryptInput>,
|
|
101
|
+
AsymmetricKeyConverter<BytesToPublicKeyInput, PublicKeyToBytesInput, BytesToPrivateKeyInput, PrivateKeyToBytesInput>,
|
|
102
|
+
SimpleKeyDeriver<DeriveKeyInput, DeriveKeyOutput>,
|
|
103
|
+
KeyBytesDeriver<DeriveKeyBytesInput, DeriveKeyBytesOutput>,
|
|
104
|
+
KeyWrapper<WrapKeyInput, UnwrapKeyInput> {}
|
|
105
|
+
|
|
106
|
+
/** @deprecated Use {@link CryptoApi} instead. */
|
|
107
|
+
export type ExtendedCryptoApi<
|
|
108
|
+
GenerateKeyInput = GenerateKeyParams,
|
|
109
|
+
GenerateKeyOutput = Jwk,
|
|
110
|
+
GetPublicKeyInput = GetPublicKeyParams,
|
|
111
|
+
DigestInput = DigestParams,
|
|
112
|
+
SignInput = SignParams,
|
|
113
|
+
VerifyInput = VerifyParams,
|
|
114
|
+
EncryptInput = CipherParams,
|
|
115
|
+
DecryptInput = CipherParams,
|
|
116
|
+
BytesToPublicKeyInput = BytesToPublicKeyParams,
|
|
117
|
+
PublicKeyToBytesInput = PublicKeyToBytesParams,
|
|
118
|
+
BytesToPrivateKeyInput = BytesToPrivateKeyParams,
|
|
119
|
+
PrivateKeyToBytesInput = PrivateKeyToBytesParams,
|
|
120
|
+
DeriveKeyInput = DeriveKeyFromBytesParams,
|
|
121
|
+
DeriveKeyOutput = Jwk,
|
|
122
|
+
DeriveKeyBytesInput = DeriveKeyBytesParams,
|
|
123
|
+
DeriveKeyBytesOutput = Uint8Array,
|
|
124
|
+
WrapKeyInput = WrapKeyParams,
|
|
125
|
+
UnwrapKeyInput = UnwrapKeyParams
|
|
126
|
+
> = CryptoApi<
|
|
127
|
+
GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput, DigestInput, SignInput, VerifyInput,
|
|
128
|
+
EncryptInput, DecryptInput, BytesToPublicKeyInput, PublicKeyToBytesInput,
|
|
129
|
+
BytesToPrivateKeyInput, PrivateKeyToBytesInput, DeriveKeyInput, DeriveKeyOutput,
|
|
130
|
+
DeriveKeyBytesInput, DeriveKeyBytesOutput, WrapKeyInput, UnwrapKeyInput
|
|
131
|
+
>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Parameters for configuring a {@link KeyManager} implementation.
|
|
135
|
+
*/
|
|
136
|
+
export interface KeyManagerParams {
|
|
137
|
+
CipherInput?: unknown;
|
|
138
|
+
GenerateKeyInput?: unknown;
|
|
139
|
+
GenerateKeyOutput?: unknown;
|
|
140
|
+
GetPublicKeyInput?: unknown;
|
|
141
|
+
SignInput?: unknown;
|
|
142
|
+
VerifyInput?: unknown;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Default parameter types for {@link KeyManager}, using KMS-oriented types.
|
|
147
|
+
*/
|
|
148
|
+
export interface DefaultKeyManagerParams {
|
|
149
|
+
CipherInput: KmsCipherParams;
|
|
150
|
+
GenerateKeyInput: KmsGenerateKeyParams;
|
|
151
|
+
GenerateKeyOutput: KeyIdentifier;
|
|
152
|
+
GetPublicKeyInput: KmsGetPublicKeyParams;
|
|
153
|
+
SignInput: KmsSignParams;
|
|
154
|
+
VerifyInput: KmsVerifyParams;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The `KeyManager` interface integrates key generation and signing capabilities.
|
|
159
|
+
*
|
|
160
|
+
* Concrete implementations of this interface are intended to be used as a Key Management System
|
|
161
|
+
* (KMS), which is responsible for generating and storing cryptographic keys.
|
|
162
|
+
*/
|
|
163
|
+
export interface KeyManager<T extends KeyManagerParams = DefaultKeyManagerParams>
|
|
164
|
+
extends DsaApi<T['GenerateKeyInput'], T['GenerateKeyOutput'], T['GetPublicKeyInput'], KmsDigestParams, T['SignInput'], T['VerifyInput']> {
|
|
165
|
+
|
|
49
166
|
/**
|
|
167
|
+
* Returns the Key URI for a given JWK.
|
|
50
168
|
*
|
|
51
169
|
* @param params - The parameters for getting the key URI.
|
|
52
170
|
* @param params.key - The key to get the URI for.
|
|
53
171
|
* @returns The key URI.
|
|
54
172
|
*/
|
|
55
173
|
getKeyUri(params: KmsGetKeyUriParams): Promise<KeyIdentifier>;
|
|
56
|
-
}
|
|
174
|
+
}
|
|
@@ -2,8 +2,16 @@ import type { Jwk } from '../jose/jwk.js';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* `KeyConverter` interface for converting private keys between byte array and JWK formats.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam BytesToPrivateKeyInput - The input type for `bytesToPrivateKey`. Defaults to
|
|
7
|
+
* `{ privateKeyBytes: Uint8Array }`.
|
|
8
|
+
* @typeParam PrivateKeyToBytesInput - The input type for `privateKeyToBytes`. Defaults to
|
|
9
|
+
* `{ privateKey: Jwk }`.
|
|
5
10
|
*/
|
|
6
|
-
export interface KeyConverter
|
|
11
|
+
export interface KeyConverter<
|
|
12
|
+
BytesToPrivateKeyInput = { privateKeyBytes: Uint8Array },
|
|
13
|
+
PrivateKeyToBytesInput = { privateKey: Jwk }
|
|
14
|
+
> {
|
|
7
15
|
|
|
8
16
|
/**
|
|
9
17
|
* Converts a private key from a byte array to JWK format.
|
|
@@ -13,7 +21,7 @@ export interface KeyConverter {
|
|
|
13
21
|
*
|
|
14
22
|
* @returns A Promise that resolves to the private key in JWK format.
|
|
15
23
|
*/
|
|
16
|
-
bytesToPrivateKey(params:
|
|
24
|
+
bytesToPrivateKey(params: BytesToPrivateKeyInput): Promise<Jwk>;
|
|
17
25
|
|
|
18
26
|
/**
|
|
19
27
|
* Converts a private key from JWK format to a byte array.
|
|
@@ -23,14 +31,32 @@ export interface KeyConverter {
|
|
|
23
31
|
*
|
|
24
32
|
* @returns A Promise that resolves to the private key as a Uint8Array.
|
|
25
33
|
*/
|
|
26
|
-
privateKeyToBytes(params:
|
|
34
|
+
privateKeyToBytes(params: PrivateKeyToBytesInput): Promise<Uint8Array>;
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
/**
|
|
30
|
-
* `AsymmetricKeyConverter` interface extends {@link KeyConverter
|
|
38
|
+
* `AsymmetricKeyConverter` interface extends {@link KeyConverter | `KeyConverter`}, adding support
|
|
31
39
|
* for public key conversions.
|
|
40
|
+
*
|
|
41
|
+
* When used with default type parameters, this interface includes all four conversion methods
|
|
42
|
+
* (bytes-to/from private key AND bytes-to/from public key). When used with explicit type
|
|
43
|
+
* parameters, both the private and public key conversion types can be customized.
|
|
44
|
+
*
|
|
45
|
+
* @typeParam BytesToPublicKeyInput - The input type for `bytesToPublicKey`. Defaults to
|
|
46
|
+
* `{ publicKeyBytes: Uint8Array }`.
|
|
47
|
+
* @typeParam PublicKeyToBytesInput - The input type for `publicKeyToBytes`. Defaults to
|
|
48
|
+
* `{ publicKey: Jwk }`.
|
|
49
|
+
* @typeParam BytesToPrivateKeyInput - The input type for `bytesToPrivateKey`. Defaults to
|
|
50
|
+
* `{ privateKeyBytes: Uint8Array }`.
|
|
51
|
+
* @typeParam PrivateKeyToBytesInput - The input type for `privateKeyToBytes`. Defaults to
|
|
52
|
+
* `{ privateKey: Jwk }`.
|
|
32
53
|
*/
|
|
33
|
-
export interface AsymmetricKeyConverter
|
|
54
|
+
export interface AsymmetricKeyConverter<
|
|
55
|
+
BytesToPublicKeyInput = { publicKeyBytes: Uint8Array },
|
|
56
|
+
PublicKeyToBytesInput = { publicKey: Jwk },
|
|
57
|
+
BytesToPrivateKeyInput = { privateKeyBytes: Uint8Array },
|
|
58
|
+
PrivateKeyToBytesInput = { privateKey: Jwk }
|
|
59
|
+
> extends KeyConverter<BytesToPrivateKeyInput, PrivateKeyToBytesInput> {
|
|
34
60
|
/**
|
|
35
61
|
* Converts a public key from a byte array to JWK format.
|
|
36
62
|
*
|
|
@@ -39,7 +65,7 @@ export interface AsymmetricKeyConverter extends KeyConverter {
|
|
|
39
65
|
*
|
|
40
66
|
* @returns A Promise that resolves to the public key in JWK format.
|
|
41
67
|
*/
|
|
42
|
-
bytesToPublicKey(params:
|
|
68
|
+
bytesToPublicKey(params: BytesToPublicKeyInput): Promise<Jwk>;
|
|
43
69
|
|
|
44
70
|
/**
|
|
45
71
|
* Converts a public key from JWK format to a byte array.
|
|
@@ -49,5 +75,5 @@ export interface AsymmetricKeyConverter extends KeyConverter {
|
|
|
49
75
|
*
|
|
50
76
|
* @returns A Promise that resolves to the public key as a Uint8Array.
|
|
51
77
|
*/
|
|
52
|
-
publicKeyToBytes(params:
|
|
78
|
+
publicKeyToBytes(params: PublicKeyToBytesInput): Promise<Uint8Array>;
|
|
53
79
|
}
|