@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,274 @@
|
|
|
1
|
+
import { Convert } from '@enbox/common';
|
|
2
|
+
import { xchacha20poly1305 } from '@noble/ciphers/chacha';
|
|
3
|
+
import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
|
|
4
|
+
|
|
5
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
6
|
+
|
|
7
|
+
import { computeJwkThumbprint, isOctPrivateJwk } from '../jose/jwk.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Constant defining the length of the authentication tag in bytes for XChaCha20-Poly1305.
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* The `POLY1305_TAG_LENGTH` is set to 16 bytes (128 bits), which is the standard size for the
|
|
14
|
+
* Poly1305 authentication tag in XChaCha20-Poly1305 encryption. This tag length ensures
|
|
15
|
+
* a strong level of security for message authentication, verifying the integrity and
|
|
16
|
+
* authenticity of the data during decryption.
|
|
17
|
+
*/
|
|
18
|
+
export const POLY1305_TAG_LENGTH = 16;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The `XChaCha20Poly1305` class provides a suite of utilities for cryptographic operations
|
|
22
|
+
* using the XChaCha20-Poly1305 algorithm, a combination of the XChaCha20 stream cipher and the
|
|
23
|
+
* Poly1305 message authentication code (MAC). This class encompasses methods for key generation,
|
|
24
|
+
* encryption, decryption, and conversions between raw byte arrays and JSON Web Key (JWK) formats.
|
|
25
|
+
*
|
|
26
|
+
* XChaCha20-Poly1305 is renowned for its high security and efficiency, especially in scenarios
|
|
27
|
+
* involving large data volumes or where data integrity and confidentiality are paramount. The
|
|
28
|
+
* extended nonce size of XChaCha20 reduces the risks of nonce reuse, while Poly1305 provides
|
|
29
|
+
* a strong MAC ensuring data integrity.
|
|
30
|
+
*
|
|
31
|
+
* Key Features:
|
|
32
|
+
* - Key Generation: Generate XChaCha20-Poly1305 symmetric keys in JWK format.
|
|
33
|
+
* - Key Conversion: Transform keys between raw byte arrays and JWK formats.
|
|
34
|
+
* - Encryption: Encrypt data using XChaCha20-Poly1305, returning both ciphertext and MAC tag.
|
|
35
|
+
* - Decryption: Decrypt data and verify integrity using the XChaCha20-Poly1305 algorithm.
|
|
36
|
+
*
|
|
37
|
+
* The methods in this class are asynchronous, returning Promises to accommodate various
|
|
38
|
+
* JavaScript environments.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* // Key Generation
|
|
43
|
+
* const privateKey = await XChaCha20Poly1305.generateKey();
|
|
44
|
+
*
|
|
45
|
+
* // Encryption
|
|
46
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
47
|
+
* const nonce = utils.randomBytes(24); // 24-byte nonce
|
|
48
|
+
* const additionalData = new TextEncoder().encode('Associated data');
|
|
49
|
+
* const { ciphertext, tag } = await XChaCha20Poly1305.encrypt({
|
|
50
|
+
* data,
|
|
51
|
+
* nonce,
|
|
52
|
+
* additionalData,
|
|
53
|
+
* key: privateKey
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* // Decryption
|
|
57
|
+
* const decryptedData = await XChaCha20Poly1305.decrypt({
|
|
58
|
+
* data: ciphertext,
|
|
59
|
+
* nonce,
|
|
60
|
+
* tag,
|
|
61
|
+
* additionalData,
|
|
62
|
+
* key: privateKey
|
|
63
|
+
* });
|
|
64
|
+
*
|
|
65
|
+
* // Key Conversion
|
|
66
|
+
* const privateKeyBytes = await XChaCha20Poly1305.privateKeyToBytes({ privateKey });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export class XChaCha20Poly1305 {
|
|
70
|
+
/**
|
|
71
|
+
* Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
|
|
72
|
+
*
|
|
73
|
+
* @remarks
|
|
74
|
+
* This method takes a symmetric key represented as a byte array (Uint8Array) and converts it into
|
|
75
|
+
* a JWK object for use with the XChaCha20-Poly1305 algorithm. The process involves encoding the
|
|
76
|
+
* key into base64url format and setting the appropriate JWK parameters.
|
|
77
|
+
*
|
|
78
|
+
* The resulting JWK object includes the following properties:
|
|
79
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
|
|
80
|
+
* - `k`: The symmetric key, base64url-encoded.
|
|
81
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
|
|
86
|
+
* const privateKey = await XChaCha20Poly1305.bytesToPrivateKey({ privateKeyBytes });
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @param params - The parameters for the symmetric key conversion.
|
|
90
|
+
* @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
|
|
91
|
+
*
|
|
92
|
+
* @returns A Promise that resolves to the symmetric key in JWK format.
|
|
93
|
+
*/
|
|
94
|
+
public static async bytesToPrivateKey({ privateKeyBytes }: {
|
|
95
|
+
privateKeyBytes: Uint8Array;
|
|
96
|
+
}): Promise<Jwk> {
|
|
97
|
+
// Construct the private key in JWK format.
|
|
98
|
+
const privateKey: Jwk = {
|
|
99
|
+
k : Convert.uint8Array(privateKeyBytes).toBase64Url(),
|
|
100
|
+
kty : 'oct'
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// Compute the JWK thumbprint and set as the key ID.
|
|
104
|
+
privateKey.kid = await computeJwkThumbprint({ jwk: privateKey });
|
|
105
|
+
|
|
106
|
+
return privateKey;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Decrypts the provided data using XChaCha20-Poly1305.
|
|
111
|
+
*
|
|
112
|
+
* @remarks
|
|
113
|
+
* This method performs XChaCha20-Poly1305 decryption on the given encrypted data using the
|
|
114
|
+
* specified key, nonce, and authentication tag. It supports optional additional authenticated
|
|
115
|
+
* data (AAD) for enhanced security. The nonce must be 24 bytes long, consistent with XChaCha20's
|
|
116
|
+
* specifications.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
121
|
+
* const nonce = new Uint8Array(24); // 24-byte nonce
|
|
122
|
+
* const additionalData = new Uint8Array([...]); // Optional AAD
|
|
123
|
+
* const key = { ... }; // A Jwk object representing the XChaCha20-Poly1305 key
|
|
124
|
+
* const decryptedData = await XChaCha20Poly1305.decrypt({
|
|
125
|
+
* data: encryptedData,
|
|
126
|
+
* nonce,
|
|
127
|
+
* additionalData,
|
|
128
|
+
* key
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @param params - The parameters for the decryption operation.
|
|
133
|
+
* @param params.data - The encrypted data to decrypt including the authentication tag,
|
|
134
|
+
* represented as a Uint8Array.
|
|
135
|
+
* @param params.key - The key to use for decryption, represented in JWK format.
|
|
136
|
+
* @param params.nonce - The nonce used during the encryption process.
|
|
137
|
+
* @param params.additionalData - Optional additional authenticated data.
|
|
138
|
+
*
|
|
139
|
+
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
140
|
+
*/
|
|
141
|
+
public static async decrypt({ data, key, nonce, additionalData }: {
|
|
142
|
+
additionalData?: Uint8Array;
|
|
143
|
+
data: Uint8Array;
|
|
144
|
+
key: Jwk;
|
|
145
|
+
nonce: Uint8Array;
|
|
146
|
+
}): Promise<Uint8Array> {
|
|
147
|
+
// Convert the private key from JWK format to bytes.
|
|
148
|
+
const privateKeyBytes = await XChaCha20Poly1305.privateKeyToBytes({ privateKey: key });
|
|
149
|
+
|
|
150
|
+
const xc20p = xchacha20poly1305(privateKeyBytes, nonce, additionalData);
|
|
151
|
+
const plaintext = xc20p.decrypt(data);
|
|
152
|
+
|
|
153
|
+
return plaintext;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Encrypts the provided data using XChaCha20-Poly1305.
|
|
158
|
+
*
|
|
159
|
+
* @remarks
|
|
160
|
+
* This method performs XChaCha20-Poly1305 encryption on the given data using the specified key
|
|
161
|
+
* and nonce. It supports optional additional authenticated data (AAD) for enhanced security. The
|
|
162
|
+
* nonce must be 24 bytes long, as per XChaCha20's specifications. The method returns the
|
|
163
|
+
* encrypted data along with an authentication tag as a Uint8Array, ensuring both confidentiality
|
|
164
|
+
* and integrity of the data.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
169
|
+
* const nonce = utils.randomBytes(24); // 24-byte nonce
|
|
170
|
+
* const additionalData = new TextEncoder().encode('Associated data'); // Optional AAD
|
|
171
|
+
* const key = { ... }; // A Jwk object representing an XChaCha20-Poly1305 key
|
|
172
|
+
* const encryptedData = await XChaCha20Poly1305.encrypt({
|
|
173
|
+
* data,
|
|
174
|
+
* nonce,
|
|
175
|
+
* additionalData,
|
|
176
|
+
* key
|
|
177
|
+
* });
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @param params - The parameters for the encryption operation.
|
|
181
|
+
* @param params.data - The data to encrypt, represented as a Uint8Array.
|
|
182
|
+
* @param params.key - The key to use for encryption, represented in JWK format.
|
|
183
|
+
* @param params.nonce - A 24-byte nonce for the encryption process.
|
|
184
|
+
* @param params.additionalData - Optional additional authenticated data.
|
|
185
|
+
*
|
|
186
|
+
* @returns A Promise that resolves to a byte array containing the encrypted data and the
|
|
187
|
+
* authentication tag.
|
|
188
|
+
*/
|
|
189
|
+
public static async encrypt({ data, key, nonce, additionalData}: {
|
|
190
|
+
additionalData?: Uint8Array;
|
|
191
|
+
data: Uint8Array;
|
|
192
|
+
key: Jwk;
|
|
193
|
+
nonce: Uint8Array;
|
|
194
|
+
}): Promise<Uint8Array> {
|
|
195
|
+
// Convert the private key from JWK format to bytes.
|
|
196
|
+
const privateKeyBytes = await XChaCha20Poly1305.privateKeyToBytes({ privateKey: key });
|
|
197
|
+
|
|
198
|
+
const xc20p = xchacha20poly1305(privateKeyBytes, nonce, additionalData);
|
|
199
|
+
const ciphertext = xc20p.encrypt(data);
|
|
200
|
+
|
|
201
|
+
return ciphertext;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Generates a symmetric key for XChaCha20-Poly1305 in JSON Web Key (JWK) format.
|
|
206
|
+
*
|
|
207
|
+
* @remarks
|
|
208
|
+
* This method creates a new symmetric key suitable for use with the XChaCha20-Poly1305 algorithm.
|
|
209
|
+
* The key is generated using cryptographically secure random number generation to ensure its
|
|
210
|
+
* uniqueness and security. The XChaCha20-Poly1305 algorithm requires a 256-bit key (32 bytes),
|
|
211
|
+
* and this method adheres to that specification.
|
|
212
|
+
*
|
|
213
|
+
* Key components included in the JWK:
|
|
214
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
215
|
+
* - `k`: The symmetric key component, base64url-encoded.
|
|
216
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* const privateKey = await XChaCha20Poly1305.generateKey();
|
|
221
|
+
* ```
|
|
222
|
+
*
|
|
223
|
+
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
224
|
+
*/
|
|
225
|
+
public static async generateKey(): Promise<Jwk> {
|
|
226
|
+
// Get the Web Crypto API interface.
|
|
227
|
+
const webCrypto = getWebcryptoSubtle();
|
|
228
|
+
|
|
229
|
+
// Generate a random private key.
|
|
230
|
+
// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#usage_notes for
|
|
231
|
+
// an explanation for why Web Crypto generateKey() is used instead of getRandomValues().
|
|
232
|
+
const webCryptoKey = await webCrypto.generateKey( { name: 'AES-CTR', length: 256 }, true, ['encrypt']);
|
|
233
|
+
|
|
234
|
+
// Export the private key in JWK format.
|
|
235
|
+
const { alg, ext, key_ops, ...privateKey } = await webCrypto.exportKey('jwk', webCryptoKey);
|
|
236
|
+
|
|
237
|
+
// Compute the JWK thumbprint and set as the key ID.
|
|
238
|
+
privateKey.kid = await computeJwkThumbprint({ jwk: privateKey });
|
|
239
|
+
|
|
240
|
+
return privateKey;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
|
|
245
|
+
*
|
|
246
|
+
* This method takes a symmetric key in JWK format and extracts its raw byte representation.
|
|
247
|
+
* It decodes the 'k' parameter of the JWK value, which represents the symmetric key in base64url
|
|
248
|
+
* encoding, into a byte array.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* const privateKey = { ... }; // A symmetric key in JWK format
|
|
253
|
+
* const privateKeyBytes = await XChaCha20Poly1305.privateKeyToBytes({ privateKey });
|
|
254
|
+
* ```
|
|
255
|
+
*
|
|
256
|
+
* @param params - The parameters for the symmetric key conversion.
|
|
257
|
+
* @param params.privateKey - The symmetric key in JWK format.
|
|
258
|
+
*
|
|
259
|
+
* @returns A Promise that resolves to the symmetric key as a Uint8Array.
|
|
260
|
+
*/
|
|
261
|
+
public static async privateKeyToBytes({ privateKey }: {
|
|
262
|
+
privateKey: Jwk;
|
|
263
|
+
}): Promise<Uint8Array> {
|
|
264
|
+
// Verify the provided JWK represents a valid oct private key.
|
|
265
|
+
if (!isOctPrivateJwk(privateKey)) {
|
|
266
|
+
throw new Error(`XChaCha20Poly1305: The provided key is not a valid oct private key.`);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Decode the provided private key to bytes.
|
|
270
|
+
const privateKeyBytes = Convert.base64Url(privateKey.k).toUint8Array();
|
|
271
|
+
|
|
272
|
+
return privateKeyBytes;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { Convert } from '@enbox/common';
|
|
2
|
+
import { xchacha20 } from '@noble/ciphers/chacha';
|
|
3
|
+
import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
|
|
4
|
+
|
|
5
|
+
import type { Jwk } from '../jose/jwk.js';
|
|
6
|
+
|
|
7
|
+
import { computeJwkThumbprint, isOctPrivateJwk } from '../jose/jwk.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `XChaCha20` class provides a comprehensive suite of utilities for cryptographic operations
|
|
11
|
+
* using the XChaCha20 symmetric encryption algorithm. This class includes methods for key
|
|
12
|
+
* generation, encryption, decryption, and conversions between raw byte arrays and JSON Web Key
|
|
13
|
+
* (JWK) formats. XChaCha20 is an extended nonce variant of ChaCha20, a stream cipher designed for
|
|
14
|
+
* high-speed encryption with substantial security margins.
|
|
15
|
+
*
|
|
16
|
+
* The XChaCha20 algorithm is particularly well-suited for encrypting large volumes of data or
|
|
17
|
+
* data streams, especially where random access is required. The class adheres to standard
|
|
18
|
+
* cryptographic practices, ensuring robustness and security in its implementations.
|
|
19
|
+
*
|
|
20
|
+
* Key Features:
|
|
21
|
+
* - Key Generation: Generate XChaCha20 symmetric keys in JWK format.
|
|
22
|
+
* - Key Conversion: Transform keys between raw byte arrays and JWK formats.
|
|
23
|
+
* - Encryption: Encrypt data using XChaCha20 with the provided symmetric key.
|
|
24
|
+
* - Decryption: Decrypt data encrypted with XChaCha20 using the corresponding symmetric key.
|
|
25
|
+
*
|
|
26
|
+
* The methods in this class are asynchronous, returning Promises to accommodate various
|
|
27
|
+
* JavaScript environments.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* // Key Generation
|
|
32
|
+
* const privateKey = await XChaCha20.generateKey();
|
|
33
|
+
*
|
|
34
|
+
* // Encryption
|
|
35
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
36
|
+
* const nonce = utils.randomBytes(24); // 24-byte nonce for XChaCha20
|
|
37
|
+
* const encryptedData = await XChaCha20.encrypt({
|
|
38
|
+
* data,
|
|
39
|
+
* nonce,
|
|
40
|
+
* key: privateKey
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // Decryption
|
|
44
|
+
* const decryptedData = await XChaCha20.decrypt({
|
|
45
|
+
* data: encryptedData,
|
|
46
|
+
* nonce,
|
|
47
|
+
* key: privateKey
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // Key Conversion
|
|
51
|
+
* const privateKeyBytes = await XChaCha20.privateKeyToBytes({ privateKey });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export class XChaCha20 {
|
|
55
|
+
/**
|
|
56
|
+
* Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* This method takes a symmetric key represented as a byte array (Uint8Array) and
|
|
60
|
+
* converts it into a JWK object for use with the XChaCha20 symmetric encryption algorithm. The
|
|
61
|
+
* conversion process involves encoding the key into base64url format and setting the appropriate
|
|
62
|
+
* JWK parameters.
|
|
63
|
+
*
|
|
64
|
+
* The resulting JWK object includes the following properties:
|
|
65
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
|
|
66
|
+
* - `k`: The symmetric key, base64url-encoded.
|
|
67
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
|
|
72
|
+
* const privateKey = await XChaCha20.bytesToPrivateKey({ privateKeyBytes });
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @param params - The parameters for the symmetric key conversion.
|
|
76
|
+
* @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
|
|
77
|
+
*
|
|
78
|
+
* @returns A Promise that resolves to the symmetric key in JWK format.
|
|
79
|
+
*/
|
|
80
|
+
public static async bytesToPrivateKey({ privateKeyBytes }: {
|
|
81
|
+
privateKeyBytes: Uint8Array;
|
|
82
|
+
}): Promise<Jwk> {
|
|
83
|
+
// Construct the private key in JWK format.
|
|
84
|
+
const privateKey: Jwk = {
|
|
85
|
+
k : Convert.uint8Array(privateKeyBytes).toBase64Url(),
|
|
86
|
+
kty : 'oct'
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// Compute the JWK thumbprint and set as the key ID.
|
|
90
|
+
privateKey.kid = await computeJwkThumbprint({ jwk: privateKey });
|
|
91
|
+
|
|
92
|
+
return privateKey;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Decrypts the provided data using XChaCha20.
|
|
97
|
+
*
|
|
98
|
+
* @remarks
|
|
99
|
+
* This method performs XChaCha20 decryption on the given encrypted data using the specified key
|
|
100
|
+
* and nonce. The nonce should be the same as used in the encryption process and must be 24 bytes
|
|
101
|
+
* long. The method returns the decrypted data as a Uint8Array.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
106
|
+
* const nonce = new Uint8Array(24); // 24-byte nonce used during encryption
|
|
107
|
+
* const key = { ... }; // A Jwk object representing the XChaCha20 key
|
|
108
|
+
* const decryptedData = await XChaCha20.decrypt({
|
|
109
|
+
* data: encryptedData,
|
|
110
|
+
* nonce,
|
|
111
|
+
* key
|
|
112
|
+
* });
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @param params - The parameters for the decryption operation.
|
|
116
|
+
* @param params.data - The encrypted data to decrypt, represented as a Uint8Array.
|
|
117
|
+
* @param params.key - The key to use for decryption, represented in JWK format.
|
|
118
|
+
* @param params.nonce - The nonce used during the encryption process.
|
|
119
|
+
*
|
|
120
|
+
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
121
|
+
*/
|
|
122
|
+
public static async decrypt({ data, key, nonce }: {
|
|
123
|
+
data: Uint8Array;
|
|
124
|
+
key: Jwk;
|
|
125
|
+
nonce: Uint8Array;
|
|
126
|
+
}): Promise<Uint8Array> {
|
|
127
|
+
// Convert the private key from JWK format to bytes.
|
|
128
|
+
const privateKeyBytes = await XChaCha20.privateKeyToBytes({ privateKey: key });
|
|
129
|
+
|
|
130
|
+
const ciphertext = xchacha20(privateKeyBytes, nonce, data);
|
|
131
|
+
|
|
132
|
+
return ciphertext;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Encrypts the provided data using XChaCha20.
|
|
137
|
+
*
|
|
138
|
+
* @remarks
|
|
139
|
+
* This method performs XChaCha20 encryption on the given data using the specified key and nonce.
|
|
140
|
+
* The nonce must be 24 bytes long, ensuring a high level of security through a vast nonce space,
|
|
141
|
+
* reducing the risks associated with nonce reuse. The method returns the encrypted data as a
|
|
142
|
+
* Uint8Array.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
147
|
+
* const nonce = utils.randomBytes(24); // 24-byte nonce for XChaCha20
|
|
148
|
+
* const key = { ... }; // A Jwk object representing an XChaCha20 key
|
|
149
|
+
* const encryptedData = await XChaCha20.encrypt({
|
|
150
|
+
* data,
|
|
151
|
+
* nonce,
|
|
152
|
+
* key
|
|
153
|
+
* });
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @param params - The parameters for the encryption operation.
|
|
157
|
+
* @param params.data - The data to encrypt, represented as a Uint8Array.
|
|
158
|
+
* @param params.key - The key to use for encryption, represented in JWK format.
|
|
159
|
+
* @param params.nonce - A 24-byte nonce for the encryption process.
|
|
160
|
+
*
|
|
161
|
+
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
162
|
+
*/
|
|
163
|
+
public static async encrypt({ data, key, nonce }: {
|
|
164
|
+
data: Uint8Array;
|
|
165
|
+
key: Jwk;
|
|
166
|
+
nonce: Uint8Array;
|
|
167
|
+
}): Promise<Uint8Array> {
|
|
168
|
+
// Convert the private key from JWK format to bytes.
|
|
169
|
+
const privateKeyBytes = await XChaCha20.privateKeyToBytes({ privateKey: key });
|
|
170
|
+
|
|
171
|
+
const plaintext = xchacha20(privateKeyBytes, nonce, data);
|
|
172
|
+
|
|
173
|
+
return plaintext;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Generates a symmetric key for XChaCha20 in JSON Web Key (JWK) format.
|
|
178
|
+
*
|
|
179
|
+
* @remarks
|
|
180
|
+
* This method creates a new symmetric key suitable for use with the XChaCha20 encryption
|
|
181
|
+
* algorithm. The key is generated using cryptographically secure random number generation
|
|
182
|
+
* to ensure its uniqueness and security. The XChaCha20 algorithm requires a 256-bit key
|
|
183
|
+
* (32 bytes), and this method adheres to that specification.
|
|
184
|
+
*
|
|
185
|
+
* Key components included in the JWK:
|
|
186
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
187
|
+
* - `k`: The symmetric key component, base64url-encoded.
|
|
188
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* const privateKey = await XChaCha20.generateKey();
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
195
|
+
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
196
|
+
*/
|
|
197
|
+
public static async generateKey(): Promise<Jwk> {
|
|
198
|
+
// Get the Web Crypto API interface.
|
|
199
|
+
const webCrypto = getWebcryptoSubtle();
|
|
200
|
+
|
|
201
|
+
// Generate a random private key.
|
|
202
|
+
// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#usage_notes for
|
|
203
|
+
// an explanation for why Web Crypto generateKey() is used instead of getRandomValues().
|
|
204
|
+
const webCryptoKey = await webCrypto.generateKey( { name: 'AES-CTR', length: 256 }, true, ['encrypt']);
|
|
205
|
+
|
|
206
|
+
// Export the private key in JWK format.
|
|
207
|
+
const { alg, ext, key_ops, ...privateKey } = await webCrypto.exportKey('jwk', webCryptoKey);
|
|
208
|
+
|
|
209
|
+
// Compute the JWK thumbprint and set as the key ID.
|
|
210
|
+
privateKey.kid = await computeJwkThumbprint({ jwk: privateKey });
|
|
211
|
+
|
|
212
|
+
return privateKey;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
|
|
217
|
+
*
|
|
218
|
+
* @remarks
|
|
219
|
+
* This method takes a symmetric key in JWK format and extracts its raw byte representation.
|
|
220
|
+
* It decodes the 'k' parameter of the JWK value, which represents the symmetric key in base64url
|
|
221
|
+
* encoding, into a byte array.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* const privateKey = { ... }; // A symmetric key in JWK format
|
|
226
|
+
* const privateKeyBytes = await XChaCha20.privateKeyToBytes({ privateKey });
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* @param params - The parameters for the symmetric key conversion.
|
|
230
|
+
* @param params.privateKey - The symmetric key in JWK format.
|
|
231
|
+
*
|
|
232
|
+
* @returns A Promise that resolves to the symmetric key as a Uint8Array.
|
|
233
|
+
*/
|
|
234
|
+
public static async privateKeyToBytes({ privateKey }: {
|
|
235
|
+
privateKey: Jwk;
|
|
236
|
+
}): Promise<Uint8Array> {
|
|
237
|
+
// Verify the provided JWK represents a valid oct private key.
|
|
238
|
+
if (!isOctPrivateJwk(privateKey)) {
|
|
239
|
+
throw new Error(`XChaCha20: The provided key is not a valid oct private key.`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Decode the provided private key to bytes.
|
|
243
|
+
const privateKeyBytes = Convert.base64Url(privateKey.k).toUint8Array();
|
|
244
|
+
|
|
245
|
+
return privateKeyBytes;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { EnclosedEncryptParams, EnclosedDecryptParams } from './params-enclosed.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The `Cipher` interface provides methods for encrypting and decrypting data.
|
|
5
|
+
*
|
|
6
|
+
* It defines two core functions: `encrypt()` for converting plaintext to ciphertext using specific
|
|
7
|
+
* algorithm parameters, and `decrypt()` for the reverse process, turning ciphertext back into
|
|
8
|
+
* plaintext. These methods operate asynchronously and return promises that resolve to the processed
|
|
9
|
+
* data, whether encrypted or decrypted.
|
|
10
|
+
*
|
|
11
|
+
* The interface is designed to be flexible, accommodating various encryption algorithms and their
|
|
12
|
+
* unique parameters. It defaults to using {@link EnclosedEncryptParams | `EnclosedEncryptParams`}
|
|
13
|
+
* and {@link EnclosedDecryptParams | `EnclosedDecryptParams`}, which are intended to be used with
|
|
14
|
+
* a closure that captures the key and algorithm-specific parameters so that arbitrary data can be
|
|
15
|
+
* encrypted and decrypted without exposing the key or parameters to the caller. However, the
|
|
16
|
+
* interface can be extended to support other parameter types, such as {@link EncryptParams |
|
|
17
|
+
* `EncryptParams`} and {@link DecryptParams | `DecryptParams`}, which are intended to be used when
|
|
18
|
+
* the key and algorithm-specific parameters are known to the caller.
|
|
19
|
+
*/
|
|
20
|
+
export interface Cipher<
|
|
21
|
+
EncryptInput = EnclosedEncryptParams,
|
|
22
|
+
DecryptInput = EnclosedDecryptParams
|
|
23
|
+
> {
|
|
24
|
+
/**
|
|
25
|
+
* Encrypts the provided data.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* The `encrypt()` method of the {@link Cipher |`Cipher`} interface encrypts data.
|
|
29
|
+
*
|
|
30
|
+
* It typically takes the data to encrypt (also known as "plaintext") and algorithm-specific
|
|
31
|
+
* parameters as input and returns the encrypted data (also known as "ciphertext").
|
|
32
|
+
*
|
|
33
|
+
* @param params - The parameters for the encryption operation.
|
|
34
|
+
*
|
|
35
|
+
* @returns A Promise which will be fulfilled with the encrypted data.
|
|
36
|
+
*/
|
|
37
|
+
encrypt(params: EncryptInput): Promise<Uint8Array>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Decrypts the provided data.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* The `decrypt()` method of the {@link Cipher |`Cipher`} interface decrypts encrypted data.
|
|
44
|
+
*
|
|
45
|
+
* It typically takes the data to decrypt (also known as "ciphertext") and algorithm-specific
|
|
46
|
+
* parameters as input and returns the decrypted data (also known as "plaintext").
|
|
47
|
+
*
|
|
48
|
+
* @param params - The parameters for the decryption operation.
|
|
49
|
+
*
|
|
50
|
+
* @returns A Promise which will be fulfilled with the decrypted data.
|
|
51
|
+
*/
|
|
52
|
+
decrypt(params: DecryptInput): Promise<Uint8Array>;
|
|
53
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Hasher } from './hasher.js';
|
|
2
|
+
import type { Signer } from './signer.js';
|
|
3
|
+
import type { KeyIdentifier } from './identifier.js';
|
|
4
|
+
import type { AsymmetricKeyGenerator } from './key-generator.js';
|
|
5
|
+
import type {
|
|
6
|
+
KmsSignParams,
|
|
7
|
+
KmsDigestParams,
|
|
8
|
+
KmsVerifyParams,
|
|
9
|
+
KmsGetKeyUriParams,
|
|
10
|
+
KmsGenerateKeyParams,
|
|
11
|
+
KmsGetPublicKeyParams,
|
|
12
|
+
} from './params-kms.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The `CryptoApi` interface integrates key generation, hashing, and signing functionalities,
|
|
16
|
+
* designed for use with a Key Management System (KMS). It extends `AsymmetricKeyGenerator` for
|
|
17
|
+
* generating asymmetric keys, `Hasher` for hash digest computations, and `Signer` for signing and
|
|
18
|
+
* verifying operations.
|
|
19
|
+
*
|
|
20
|
+
* Concrete implementations of this interface are intended to be used with a KMS, which is
|
|
21
|
+
* responsible for generating and storing cryptographic keys. The KMS is also responsible for
|
|
22
|
+
* performing cryptographic operations using the keys it manages. The KMS is typically a cloud
|
|
23
|
+
* service, but it can also be a hardware device or software application.
|
|
24
|
+
*
|
|
25
|
+
* Guidelines for implementing this interface:
|
|
26
|
+
* - Must use JSON Web Keys ({@link Jwk | JWK}) as the key format.
|
|
27
|
+
* - Must IANA registered JSON Object Signing and Encryption
|
|
28
|
+
* {@ link https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms | (JOSE)}
|
|
29
|
+
* names for algorithm, curves, etc. whenever possible.
|
|
30
|
+
* - All I/O that interacts with private or secret keys must be done via reference using a
|
|
31
|
+
* {@link KeyIdentifier | `KeyIdentifier`}. Implementations can use any string as the key
|
|
32
|
+
* identifier (e.g. JWK thumbprint, UUID generated by hosted KMS, etc.).
|
|
33
|
+
* - Must support key generation, hashing, signing, and verifying operations.
|
|
34
|
+
* - May be extended to support other cryptographic operations.
|
|
35
|
+
* - Implementations of the `CryptoApi` interface can be passed as an argument to the public API
|
|
36
|
+
* methods of Web5 libraries that involve key material (e.g., DID creation, VC signing, arbitrary
|
|
37
|
+
* data signing/verification, etc.).
|
|
38
|
+
*/
|
|
39
|
+
export interface CryptoApi<
|
|
40
|
+
GenerateKeyInput = KmsGenerateKeyParams,
|
|
41
|
+
GenerateKeyOutput = KeyIdentifier,
|
|
42
|
+
GetPublicKeyInput = KmsGetPublicKeyParams,
|
|
43
|
+
DigestInput = KmsDigestParams,
|
|
44
|
+
SignInput = KmsSignParams,
|
|
45
|
+
VerifyInput = KmsVerifyParams
|
|
46
|
+
> extends AsymmetricKeyGenerator<GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput>,
|
|
47
|
+
Hasher<DigestInput>,
|
|
48
|
+
Signer<SignInput, VerifyInput> {
|
|
49
|
+
/**
|
|
50
|
+
*
|
|
51
|
+
* @param params - The parameters for getting the key URI.
|
|
52
|
+
* @param params.key - The key to get the URI for.
|
|
53
|
+
* @returns The key URI.
|
|
54
|
+
*/
|
|
55
|
+
getKeyUri(params: KmsGetKeyUriParams): Promise<KeyIdentifier>;
|
|
56
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `Hasher` interface provides a method for generating digests of data using hash functions.
|
|
3
|
+
* It defines the core function `digest()` for processing variable-size input data to produce a
|
|
4
|
+
* fixed-size output (the "digest") which uniquely represents the input data.
|
|
5
|
+
*
|
|
6
|
+
* This method operates asynchronously and returns a digest, often used for data integrity checks.
|
|
7
|
+
* The interface can be implemented with various hash algorithms and their unique parameters.
|
|
8
|
+
*
|
|
9
|
+
* The interface is designed to accommodate byte array input data but can be extended to support
|
|
10
|
+
* other input types as needed.
|
|
11
|
+
*/
|
|
12
|
+
export interface Hasher<DigestInput> {
|
|
13
|
+
/**
|
|
14
|
+
* Generates a hash digest of the provided data.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* The `digest()` method of the {@link Hasher | `Hasher`} interface generates a hash digest of the
|
|
18
|
+
* input data.
|
|
19
|
+
*
|
|
20
|
+
* A digest is the output of the hash function. It's a fixed-size string of bytes
|
|
21
|
+
* that uniquely represents the data input into the hash function. The digest is often used for
|
|
22
|
+
* data integrity checks, as any alteration in the input data results in a significantly
|
|
23
|
+
* different digest.
|
|
24
|
+
*
|
|
25
|
+
* It typically takes the data to digest as input and returns the digest of the data.
|
|
26
|
+
*
|
|
27
|
+
* @param params - The parameters for the digest operation.
|
|
28
|
+
*
|
|
29
|
+
* @returns A Promise which will be fulfilled with the hash digest.
|
|
30
|
+
*/
|
|
31
|
+
digest(params: DigestInput): Promise<Uint8Array>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents an identifier for a cryptographic algorithm.
|
|
3
|
+
*
|
|
4
|
+
* This is typically a string that specifies the name of the algorithm, such as 'ES256K', 'A256GCM',
|
|
5
|
+
* 'SHA-256', etc. It's used to refer to the specific algorithm in cryptographic operations.
|
|
6
|
+
*/
|
|
7
|
+
export type AlgorithmIdentifier = string;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Denotes an identifier for a cryptographic key.
|
|
11
|
+
*
|
|
12
|
+
* It's a string used to uniquely identify a key within a given context, such as in key management
|
|
13
|
+
* systems (KMS) or cryptographic protocols. This identifier can be a simple string, a UUID, a URI,
|
|
14
|
+
* or any format chosen to suit the application's needs.
|
|
15
|
+
*/
|
|
16
|
+
export type KeyIdentifier = string;
|