@enbox/crypto 0.0.1 → 0.0.3
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.js +6 -10
- package/dist/browser.js.map +4 -4
- package/dist/browser.mjs +6 -10
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/algorithms/aes-gcm.js +1 -1
- package/dist/esm/algorithms/aes-gcm.js.map +1 -1
- package/dist/esm/algorithms/ecdsa.js +9 -5
- package/dist/esm/algorithms/ecdsa.js.map +1 -1
- package/dist/esm/algorithms/eddsa.js +9 -5
- package/dist/esm/algorithms/eddsa.js.map +1 -1
- package/dist/esm/algorithms/sha-2.js +1 -1
- package/dist/esm/algorithms/sha-2.js.map +1 -1
- package/dist/esm/crypto-error.js +41 -0
- package/dist/esm/crypto-error.js.map +1 -0
- package/dist/esm/index.js +3 -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 +3 -2
- 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/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 +1 -1
- 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.map +1 -1
- package/dist/types/algorithms/aes-ctr.d.ts +1 -1
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -1
- package/dist/types/algorithms/aes-gcm.d.ts +2 -2
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
- package/dist/types/algorithms/ecdsa.d.ts +1 -1
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
- package/dist/types/algorithms/eddsa.d.ts +2 -2
- package/dist/types/algorithms/eddsa.d.ts.map +1 -1
- package/dist/types/algorithms/sha-2.d.ts +1 -1
- package/dist/types/algorithms/sha-2.d.ts.map +1 -1
- 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 +3 -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 +3 -3
- 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/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/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 +3 -3
- package/dist/types/types/crypto-api.d.ts.map +1 -1
- package/dist/types/types/params-direct.d.ts +79 -1
- package/dist/types/types/params-direct.d.ts.map +1 -1
- package/dist/utils.js.map +2 -2
- package/package.json +21 -34
- package/src/algorithms/aes-ctr.ts +1 -1
- package/src/algorithms/aes-gcm.ts +3 -2
- package/src/algorithms/ecdsa.ts +13 -7
- package/src/algorithms/eddsa.ts +9 -9
- package/src/algorithms/sha-2.ts +2 -2
- package/src/crypto-error.ts +45 -0
- package/src/index.ts +3 -0
- package/src/jose/jwk.ts +32 -32
- package/src/local-key-manager.ts +14 -13
- 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/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 +3 -3
- 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 +5 -5
- package/src/types/params-direct.ts +97 -1
- package/src/utils.ts +2 -2
- 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
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
39
|
-
var t = {};
|
|
40
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
41
|
-
t[p] = s[p];
|
|
42
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
43
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
44
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
45
|
-
t[p[i]] = s[p[i]];
|
|
46
|
-
}
|
|
47
|
-
return t;
|
|
48
|
-
};
|
|
49
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.XChaCha20Poly1305 = exports.POLY1305_TAG_LENGTH = void 0;
|
|
51
|
-
var common_1 = require("@enbox/common");
|
|
52
|
-
var chacha_1 = require("@noble/ciphers/chacha");
|
|
53
|
-
var webcrypto_1 = require("@noble/ciphers/webcrypto");
|
|
54
|
-
var jwk_js_1 = require("../jose/jwk.js");
|
|
55
|
-
/**
|
|
56
|
-
* Constant defining the length of the authentication tag in bytes for XChaCha20-Poly1305.
|
|
57
|
-
*
|
|
58
|
-
* @remarks
|
|
59
|
-
* The `POLY1305_TAG_LENGTH` is set to 16 bytes (128 bits), which is the standard size for the
|
|
60
|
-
* Poly1305 authentication tag in XChaCha20-Poly1305 encryption. This tag length ensures
|
|
61
|
-
* a strong level of security for message authentication, verifying the integrity and
|
|
62
|
-
* authenticity of the data during decryption.
|
|
63
|
-
*/
|
|
64
|
-
exports.POLY1305_TAG_LENGTH = 16;
|
|
65
|
-
/**
|
|
66
|
-
* The `XChaCha20Poly1305` class provides a suite of utilities for cryptographic operations
|
|
67
|
-
* using the XChaCha20-Poly1305 algorithm, a combination of the XChaCha20 stream cipher and the
|
|
68
|
-
* Poly1305 message authentication code (MAC). This class encompasses methods for key generation,
|
|
69
|
-
* encryption, decryption, and conversions between raw byte arrays and JSON Web Key (JWK) formats.
|
|
70
|
-
*
|
|
71
|
-
* XChaCha20-Poly1305 is renowned for its high security and efficiency, especially in scenarios
|
|
72
|
-
* involving large data volumes or where data integrity and confidentiality are paramount. The
|
|
73
|
-
* extended nonce size of XChaCha20 reduces the risks of nonce reuse, while Poly1305 provides
|
|
74
|
-
* a strong MAC ensuring data integrity.
|
|
75
|
-
*
|
|
76
|
-
* Key Features:
|
|
77
|
-
* - Key Generation: Generate XChaCha20-Poly1305 symmetric keys in JWK format.
|
|
78
|
-
* - Key Conversion: Transform keys between raw byte arrays and JWK formats.
|
|
79
|
-
* - Encryption: Encrypt data using XChaCha20-Poly1305, returning both ciphertext and MAC tag.
|
|
80
|
-
* - Decryption: Decrypt data and verify integrity using the XChaCha20-Poly1305 algorithm.
|
|
81
|
-
*
|
|
82
|
-
* The methods in this class are asynchronous, returning Promises to accommodate various
|
|
83
|
-
* JavaScript environments.
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```ts
|
|
87
|
-
* // Key Generation
|
|
88
|
-
* const privateKey = await XChaCha20Poly1305.generateKey();
|
|
89
|
-
*
|
|
90
|
-
* // Encryption
|
|
91
|
-
* const data = new TextEncoder().encode('Messsage');
|
|
92
|
-
* const nonce = utils.randomBytes(24); // 24-byte nonce
|
|
93
|
-
* const additionalData = new TextEncoder().encode('Associated data');
|
|
94
|
-
* const { ciphertext, tag } = await XChaCha20Poly1305.encrypt({
|
|
95
|
-
* data,
|
|
96
|
-
* nonce,
|
|
97
|
-
* additionalData,
|
|
98
|
-
* key: privateKey
|
|
99
|
-
* });
|
|
100
|
-
*
|
|
101
|
-
* // Decryption
|
|
102
|
-
* const decryptedData = await XChaCha20Poly1305.decrypt({
|
|
103
|
-
* data: ciphertext,
|
|
104
|
-
* nonce,
|
|
105
|
-
* tag,
|
|
106
|
-
* additionalData,
|
|
107
|
-
* key: privateKey
|
|
108
|
-
* });
|
|
109
|
-
*
|
|
110
|
-
* // Key Conversion
|
|
111
|
-
* const privateKeyBytes = await XChaCha20Poly1305.privateKeyToBytes({ privateKey });
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
|
-
var XChaCha20Poly1305 = /** @class */ (function () {
|
|
115
|
-
function XChaCha20Poly1305() {
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
|
|
119
|
-
*
|
|
120
|
-
* @remarks
|
|
121
|
-
* This method takes a symmetric key represented as a byte array (Uint8Array) and converts it into
|
|
122
|
-
* a JWK object for use with the XChaCha20-Poly1305 algorithm. The process involves encoding the
|
|
123
|
-
* key into base64url format and setting the appropriate JWK parameters.
|
|
124
|
-
*
|
|
125
|
-
* The resulting JWK object includes the following properties:
|
|
126
|
-
* - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
|
|
127
|
-
* - `k`: The symmetric key, base64url-encoded.
|
|
128
|
-
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
-
* ```ts
|
|
132
|
-
* const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
|
|
133
|
-
* const privateKey = await XChaCha20Poly1305.bytesToPrivateKey({ privateKeyBytes });
|
|
134
|
-
* ```
|
|
135
|
-
*
|
|
136
|
-
* @param params - The parameters for the symmetric key conversion.
|
|
137
|
-
* @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
|
|
138
|
-
*
|
|
139
|
-
* @returns A Promise that resolves to the symmetric key in JWK format.
|
|
140
|
-
*/
|
|
141
|
-
XChaCha20Poly1305.bytesToPrivateKey = function (_a) {
|
|
142
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
143
|
-
var privateKey, _c;
|
|
144
|
-
var privateKeyBytes = _b.privateKeyBytes;
|
|
145
|
-
return __generator(this, function (_d) {
|
|
146
|
-
switch (_d.label) {
|
|
147
|
-
case 0:
|
|
148
|
-
privateKey = {
|
|
149
|
-
k: common_1.Convert.uint8Array(privateKeyBytes).toBase64Url(),
|
|
150
|
-
kty: 'oct'
|
|
151
|
-
};
|
|
152
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
153
|
-
_c = privateKey;
|
|
154
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
|
|
155
|
-
case 1:
|
|
156
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
157
|
-
_c.kid = _d.sent();
|
|
158
|
-
return [2 /*return*/, privateKey];
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
/**
|
|
164
|
-
* Decrypts the provided data using XChaCha20-Poly1305.
|
|
165
|
-
*
|
|
166
|
-
* @remarks
|
|
167
|
-
* This method performs XChaCha20-Poly1305 decryption on the given encrypted data using the
|
|
168
|
-
* specified key, nonce, and authentication tag. It supports optional additional authenticated
|
|
169
|
-
* data (AAD) for enhanced security. The nonce must be 24 bytes long, consistent with XChaCha20's
|
|
170
|
-
* specifications.
|
|
171
|
-
*
|
|
172
|
-
* @example
|
|
173
|
-
* ```ts
|
|
174
|
-
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
175
|
-
* const nonce = new Uint8Array(24); // 24-byte nonce
|
|
176
|
-
* const additionalData = new Uint8Array([...]); // Optional AAD
|
|
177
|
-
* const key = { ... }; // A Jwk object representing the XChaCha20-Poly1305 key
|
|
178
|
-
* const decryptedData = await XChaCha20Poly1305.decrypt({
|
|
179
|
-
* data: encryptedData,
|
|
180
|
-
* nonce,
|
|
181
|
-
* additionalData,
|
|
182
|
-
* key
|
|
183
|
-
* });
|
|
184
|
-
* ```
|
|
185
|
-
*
|
|
186
|
-
* @param params - The parameters for the decryption operation.
|
|
187
|
-
* @param params.data - The encrypted data to decrypt including the authentication tag,
|
|
188
|
-
* represented as a Uint8Array.
|
|
189
|
-
* @param params.key - The key to use for decryption, represented in JWK format.
|
|
190
|
-
* @param params.nonce - The nonce used during the encryption process.
|
|
191
|
-
* @param params.additionalData - Optional additional authenticated data.
|
|
192
|
-
*
|
|
193
|
-
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
194
|
-
*/
|
|
195
|
-
XChaCha20Poly1305.decrypt = function (_a) {
|
|
196
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
197
|
-
var privateKeyBytes, xc20p, plaintext;
|
|
198
|
-
var data = _b.data, key = _b.key, nonce = _b.nonce, additionalData = _b.additionalData;
|
|
199
|
-
return __generator(this, function (_c) {
|
|
200
|
-
switch (_c.label) {
|
|
201
|
-
case 0: return [4 /*yield*/, XChaCha20Poly1305.privateKeyToBytes({ privateKey: key })];
|
|
202
|
-
case 1:
|
|
203
|
-
privateKeyBytes = _c.sent();
|
|
204
|
-
xc20p = (0, chacha_1.xchacha20poly1305)(privateKeyBytes, nonce, additionalData);
|
|
205
|
-
plaintext = xc20p.decrypt(data);
|
|
206
|
-
return [2 /*return*/, plaintext];
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
};
|
|
211
|
-
/**
|
|
212
|
-
* Encrypts the provided data using XChaCha20-Poly1305.
|
|
213
|
-
*
|
|
214
|
-
* @remarks
|
|
215
|
-
* This method performs XChaCha20-Poly1305 encryption on the given data using the specified key
|
|
216
|
-
* and nonce. It supports optional additional authenticated data (AAD) for enhanced security. The
|
|
217
|
-
* nonce must be 24 bytes long, as per XChaCha20's specifications. The method returns the
|
|
218
|
-
* encrypted data along with an authentication tag as a Uint8Array, ensuring both confidentiality
|
|
219
|
-
* and integrity of the data.
|
|
220
|
-
*
|
|
221
|
-
* @example
|
|
222
|
-
* ```ts
|
|
223
|
-
* const data = new TextEncoder().encode('Messsage');
|
|
224
|
-
* const nonce = utils.randomBytes(24); // 24-byte nonce
|
|
225
|
-
* const additionalData = new TextEncoder().encode('Associated data'); // Optional AAD
|
|
226
|
-
* const key = { ... }; // A Jwk object representing an XChaCha20-Poly1305 key
|
|
227
|
-
* const encryptedData = await XChaCha20Poly1305.encrypt({
|
|
228
|
-
* data,
|
|
229
|
-
* nonce,
|
|
230
|
-
* additionalData,
|
|
231
|
-
* key
|
|
232
|
-
* });
|
|
233
|
-
* ```
|
|
234
|
-
*
|
|
235
|
-
* @param params - The parameters for the encryption operation.
|
|
236
|
-
* @param params.data - The data to encrypt, represented as a Uint8Array.
|
|
237
|
-
* @param params.key - The key to use for encryption, represented in JWK format.
|
|
238
|
-
* @param params.nonce - A 24-byte nonce for the encryption process.
|
|
239
|
-
* @param params.additionalData - Optional additional authenticated data.
|
|
240
|
-
*
|
|
241
|
-
* @returns A Promise that resolves to a byte array containing the encrypted data and the
|
|
242
|
-
* authentication tag.
|
|
243
|
-
*/
|
|
244
|
-
XChaCha20Poly1305.encrypt = function (_a) {
|
|
245
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
246
|
-
var privateKeyBytes, xc20p, ciphertext;
|
|
247
|
-
var data = _b.data, key = _b.key, nonce = _b.nonce, additionalData = _b.additionalData;
|
|
248
|
-
return __generator(this, function (_c) {
|
|
249
|
-
switch (_c.label) {
|
|
250
|
-
case 0: return [4 /*yield*/, XChaCha20Poly1305.privateKeyToBytes({ privateKey: key })];
|
|
251
|
-
case 1:
|
|
252
|
-
privateKeyBytes = _c.sent();
|
|
253
|
-
xc20p = (0, chacha_1.xchacha20poly1305)(privateKeyBytes, nonce, additionalData);
|
|
254
|
-
ciphertext = xc20p.encrypt(data);
|
|
255
|
-
return [2 /*return*/, ciphertext];
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
});
|
|
259
|
-
};
|
|
260
|
-
/**
|
|
261
|
-
* Generates a symmetric key for XChaCha20-Poly1305 in JSON Web Key (JWK) format.
|
|
262
|
-
*
|
|
263
|
-
* @remarks
|
|
264
|
-
* This method creates a new symmetric key suitable for use with the XChaCha20-Poly1305 algorithm.
|
|
265
|
-
* The key is generated using cryptographically secure random number generation to ensure its
|
|
266
|
-
* uniqueness and security. The XChaCha20-Poly1305 algorithm requires a 256-bit key (32 bytes),
|
|
267
|
-
* and this method adheres to that specification.
|
|
268
|
-
*
|
|
269
|
-
* Key components included in the JWK:
|
|
270
|
-
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
271
|
-
* - `k`: The symmetric key component, base64url-encoded.
|
|
272
|
-
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
273
|
-
*
|
|
274
|
-
* @example
|
|
275
|
-
* ```ts
|
|
276
|
-
* const privateKey = await XChaCha20Poly1305.generateKey();
|
|
277
|
-
* ```
|
|
278
|
-
*
|
|
279
|
-
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
280
|
-
*/
|
|
281
|
-
XChaCha20Poly1305.generateKey = function () {
|
|
282
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
283
|
-
var webCrypto, webCryptoKey, _a, alg, ext, key_ops, privateKey, _b;
|
|
284
|
-
return __generator(this, function (_c) {
|
|
285
|
-
switch (_c.label) {
|
|
286
|
-
case 0:
|
|
287
|
-
webCrypto = (0, webcrypto_1.getWebcryptoSubtle)();
|
|
288
|
-
return [4 /*yield*/, webCrypto.generateKey({ name: 'AES-CTR', length: 256 }, true, ['encrypt'])];
|
|
289
|
-
case 1:
|
|
290
|
-
webCryptoKey = _c.sent();
|
|
291
|
-
return [4 /*yield*/, webCrypto.exportKey('jwk', webCryptoKey)];
|
|
292
|
-
case 2:
|
|
293
|
-
_a = _c.sent(), alg = _a.alg, ext = _a.ext, key_ops = _a.key_ops, privateKey = __rest(_a, ["alg", "ext", "key_ops"]);
|
|
294
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
295
|
-
_b = privateKey;
|
|
296
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
|
|
297
|
-
case 3:
|
|
298
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
299
|
-
_b.kid = _c.sent();
|
|
300
|
-
return [2 /*return*/, privateKey];
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
};
|
|
305
|
-
/**
|
|
306
|
-
* Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
|
|
307
|
-
*
|
|
308
|
-
* This method takes a symmetric key in JWK format and extracts its raw byte representation.
|
|
309
|
-
* It decodes the 'k' parameter of the JWK value, which represents the symmetric key in base64url
|
|
310
|
-
* encoding, into a byte array.
|
|
311
|
-
*
|
|
312
|
-
* @example
|
|
313
|
-
* ```ts
|
|
314
|
-
* const privateKey = { ... }; // A symmetric key in JWK format
|
|
315
|
-
* const privateKeyBytes = await XChaCha20Poly1305.privateKeyToBytes({ privateKey });
|
|
316
|
-
* ```
|
|
317
|
-
*
|
|
318
|
-
* @param params - The parameters for the symmetric key conversion.
|
|
319
|
-
* @param params.privateKey - The symmetric key in JWK format.
|
|
320
|
-
*
|
|
321
|
-
* @returns A Promise that resolves to the symmetric key as a Uint8Array.
|
|
322
|
-
*/
|
|
323
|
-
XChaCha20Poly1305.privateKeyToBytes = function (_a) {
|
|
324
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
325
|
-
var privateKeyBytes;
|
|
326
|
-
var privateKey = _b.privateKey;
|
|
327
|
-
return __generator(this, function (_c) {
|
|
328
|
-
// Verify the provided JWK represents a valid oct private key.
|
|
329
|
-
if (!(0, jwk_js_1.isOctPrivateJwk)(privateKey)) {
|
|
330
|
-
throw new Error("XChaCha20Poly1305: The provided key is not a valid oct private key.");
|
|
331
|
-
}
|
|
332
|
-
privateKeyBytes = common_1.Convert.base64Url(privateKey.k).toUint8Array();
|
|
333
|
-
return [2 /*return*/, privateKeyBytes];
|
|
334
|
-
});
|
|
335
|
-
});
|
|
336
|
-
};
|
|
337
|
-
return XChaCha20Poly1305;
|
|
338
|
-
}());
|
|
339
|
-
exports.XChaCha20Poly1305 = XChaCha20Poly1305;
|
|
340
|
-
//# sourceMappingURL=xchacha20-poly1305.js.map
|
|
@@ -1 +0,0 @@
|
|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DAAsC,EAErC;;gBAFuC,UAAU,gBAAA;;gBAGhD,8DAA8D;gBAC9D,IAAI,CAAC,IAAA,wBAAe,EAAC,UAAU,CAAC,EAAE,CAAC;oBACjC,MAAM,IAAI,KAAK,CAAC,qEAAqE,CAAC,CAAC;gBACzF,CAAC;gBAGK,eAAe,GAAG,gBAAO,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC;gBAEvE,sBAAO,eAAe,EAAC;;;KACxB;IACH,wBAAC;AAAD,CAAC,AA7MD,IA6MC;AA7MY,8CAAiB"}
|
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
39
|
-
var t = {};
|
|
40
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
41
|
-
t[p] = s[p];
|
|
42
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
43
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
44
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
45
|
-
t[p[i]] = s[p[i]];
|
|
46
|
-
}
|
|
47
|
-
return t;
|
|
48
|
-
};
|
|
49
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.XChaCha20 = void 0;
|
|
51
|
-
var common_1 = require("@enbox/common");
|
|
52
|
-
var chacha_1 = require("@noble/ciphers/chacha");
|
|
53
|
-
var webcrypto_1 = require("@noble/ciphers/webcrypto");
|
|
54
|
-
var jwk_js_1 = require("../jose/jwk.js");
|
|
55
|
-
/**
|
|
56
|
-
* The `XChaCha20` class provides a comprehensive suite of utilities for cryptographic operations
|
|
57
|
-
* using the XChaCha20 symmetric encryption algorithm. This class includes methods for key
|
|
58
|
-
* generation, encryption, decryption, and conversions between raw byte arrays and JSON Web Key
|
|
59
|
-
* (JWK) formats. XChaCha20 is an extended nonce variant of ChaCha20, a stream cipher designed for
|
|
60
|
-
* high-speed encryption with substantial security margins.
|
|
61
|
-
*
|
|
62
|
-
* The XChaCha20 algorithm is particularly well-suited for encrypting large volumes of data or
|
|
63
|
-
* data streams, especially where random access is required. The class adheres to standard
|
|
64
|
-
* cryptographic practices, ensuring robustness and security in its implementations.
|
|
65
|
-
*
|
|
66
|
-
* Key Features:
|
|
67
|
-
* - Key Generation: Generate XChaCha20 symmetric keys in JWK format.
|
|
68
|
-
* - Key Conversion: Transform keys between raw byte arrays and JWK formats.
|
|
69
|
-
* - Encryption: Encrypt data using XChaCha20 with the provided symmetric key.
|
|
70
|
-
* - Decryption: Decrypt data encrypted with XChaCha20 using the corresponding symmetric key.
|
|
71
|
-
*
|
|
72
|
-
* The methods in this class are asynchronous, returning Promises to accommodate various
|
|
73
|
-
* JavaScript environments.
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* ```ts
|
|
77
|
-
* // Key Generation
|
|
78
|
-
* const privateKey = await XChaCha20.generateKey();
|
|
79
|
-
*
|
|
80
|
-
* // Encryption
|
|
81
|
-
* const data = new TextEncoder().encode('Messsage');
|
|
82
|
-
* const nonce = utils.randomBytes(24); // 24-byte nonce for XChaCha20
|
|
83
|
-
* const encryptedData = await XChaCha20.encrypt({
|
|
84
|
-
* data,
|
|
85
|
-
* nonce,
|
|
86
|
-
* key: privateKey
|
|
87
|
-
* });
|
|
88
|
-
*
|
|
89
|
-
* // Decryption
|
|
90
|
-
* const decryptedData = await XChaCha20.decrypt({
|
|
91
|
-
* data: encryptedData,
|
|
92
|
-
* nonce,
|
|
93
|
-
* key: privateKey
|
|
94
|
-
* });
|
|
95
|
-
*
|
|
96
|
-
* // Key Conversion
|
|
97
|
-
* const privateKeyBytes = await XChaCha20.privateKeyToBytes({ privateKey });
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
var XChaCha20 = /** @class */ (function () {
|
|
101
|
-
function XChaCha20() {
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
|
|
105
|
-
*
|
|
106
|
-
* @remarks
|
|
107
|
-
* This method takes a symmetric key represented as a byte array (Uint8Array) and
|
|
108
|
-
* converts it into a JWK object for use with the XChaCha20 symmetric encryption algorithm. The
|
|
109
|
-
* conversion process involves encoding the key into base64url format and setting the appropriate
|
|
110
|
-
* JWK parameters.
|
|
111
|
-
*
|
|
112
|
-
* The resulting JWK object includes the following properties:
|
|
113
|
-
* - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
|
|
114
|
-
* - `k`: The symmetric key, base64url-encoded.
|
|
115
|
-
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
116
|
-
*
|
|
117
|
-
* @example
|
|
118
|
-
* ```ts
|
|
119
|
-
* const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
|
|
120
|
-
* const privateKey = await XChaCha20.bytesToPrivateKey({ privateKeyBytes });
|
|
121
|
-
* ```
|
|
122
|
-
*
|
|
123
|
-
* @param params - The parameters for the symmetric key conversion.
|
|
124
|
-
* @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
|
|
125
|
-
*
|
|
126
|
-
* @returns A Promise that resolves to the symmetric key in JWK format.
|
|
127
|
-
*/
|
|
128
|
-
XChaCha20.bytesToPrivateKey = function (_a) {
|
|
129
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
130
|
-
var privateKey, _c;
|
|
131
|
-
var privateKeyBytes = _b.privateKeyBytes;
|
|
132
|
-
return __generator(this, function (_d) {
|
|
133
|
-
switch (_d.label) {
|
|
134
|
-
case 0:
|
|
135
|
-
privateKey = {
|
|
136
|
-
k: common_1.Convert.uint8Array(privateKeyBytes).toBase64Url(),
|
|
137
|
-
kty: 'oct'
|
|
138
|
-
};
|
|
139
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
140
|
-
_c = privateKey;
|
|
141
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
|
|
142
|
-
case 1:
|
|
143
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
144
|
-
_c.kid = _d.sent();
|
|
145
|
-
return [2 /*return*/, privateKey];
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
};
|
|
150
|
-
/**
|
|
151
|
-
* Decrypts the provided data using XChaCha20.
|
|
152
|
-
*
|
|
153
|
-
* @remarks
|
|
154
|
-
* This method performs XChaCha20 decryption on the given encrypted data using the specified key
|
|
155
|
-
* and nonce. The nonce should be the same as used in the encryption process and must be 24 bytes
|
|
156
|
-
* long. The method returns the decrypted data as a Uint8Array.
|
|
157
|
-
*
|
|
158
|
-
* @example
|
|
159
|
-
* ```ts
|
|
160
|
-
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
161
|
-
* const nonce = new Uint8Array(24); // 24-byte nonce used during encryption
|
|
162
|
-
* const key = { ... }; // A Jwk object representing the XChaCha20 key
|
|
163
|
-
* const decryptedData = await XChaCha20.decrypt({
|
|
164
|
-
* data: encryptedData,
|
|
165
|
-
* nonce,
|
|
166
|
-
* key
|
|
167
|
-
* });
|
|
168
|
-
* ```
|
|
169
|
-
*
|
|
170
|
-
* @param params - The parameters for the decryption operation.
|
|
171
|
-
* @param params.data - The encrypted data to decrypt, represented as a Uint8Array.
|
|
172
|
-
* @param params.key - The key to use for decryption, represented in JWK format.
|
|
173
|
-
* @param params.nonce - The nonce used during the encryption process.
|
|
174
|
-
*
|
|
175
|
-
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
176
|
-
*/
|
|
177
|
-
XChaCha20.decrypt = function (_a) {
|
|
178
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
179
|
-
var privateKeyBytes, ciphertext;
|
|
180
|
-
var data = _b.data, key = _b.key, nonce = _b.nonce;
|
|
181
|
-
return __generator(this, function (_c) {
|
|
182
|
-
switch (_c.label) {
|
|
183
|
-
case 0: return [4 /*yield*/, XChaCha20.privateKeyToBytes({ privateKey: key })];
|
|
184
|
-
case 1:
|
|
185
|
-
privateKeyBytes = _c.sent();
|
|
186
|
-
ciphertext = (0, chacha_1.xchacha20)(privateKeyBytes, nonce, data);
|
|
187
|
-
return [2 /*return*/, ciphertext];
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
};
|
|
192
|
-
/**
|
|
193
|
-
* Encrypts the provided data using XChaCha20.
|
|
194
|
-
*
|
|
195
|
-
* @remarks
|
|
196
|
-
* This method performs XChaCha20 encryption on the given data using the specified key and nonce.
|
|
197
|
-
* The nonce must be 24 bytes long, ensuring a high level of security through a vast nonce space,
|
|
198
|
-
* reducing the risks associated with nonce reuse. The method returns the encrypted data as a
|
|
199
|
-
* Uint8Array.
|
|
200
|
-
*
|
|
201
|
-
* @example
|
|
202
|
-
* ```ts
|
|
203
|
-
* const data = new TextEncoder().encode('Messsage');
|
|
204
|
-
* const nonce = utils.randomBytes(24); // 24-byte nonce for XChaCha20
|
|
205
|
-
* const key = { ... }; // A Jwk object representing an XChaCha20 key
|
|
206
|
-
* const encryptedData = await XChaCha20.encrypt({
|
|
207
|
-
* data,
|
|
208
|
-
* nonce,
|
|
209
|
-
* key
|
|
210
|
-
* });
|
|
211
|
-
* ```
|
|
212
|
-
*
|
|
213
|
-
* @param params - The parameters for the encryption operation.
|
|
214
|
-
* @param params.data - The data to encrypt, represented as a Uint8Array.
|
|
215
|
-
* @param params.key - The key to use for encryption, represented in JWK format.
|
|
216
|
-
* @param params.nonce - A 24-byte nonce for the encryption process.
|
|
217
|
-
*
|
|
218
|
-
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
219
|
-
*/
|
|
220
|
-
XChaCha20.encrypt = function (_a) {
|
|
221
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
222
|
-
var privateKeyBytes, plaintext;
|
|
223
|
-
var data = _b.data, key = _b.key, nonce = _b.nonce;
|
|
224
|
-
return __generator(this, function (_c) {
|
|
225
|
-
switch (_c.label) {
|
|
226
|
-
case 0: return [4 /*yield*/, XChaCha20.privateKeyToBytes({ privateKey: key })];
|
|
227
|
-
case 1:
|
|
228
|
-
privateKeyBytes = _c.sent();
|
|
229
|
-
plaintext = (0, chacha_1.xchacha20)(privateKeyBytes, nonce, data);
|
|
230
|
-
return [2 /*return*/, plaintext];
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
};
|
|
235
|
-
/**
|
|
236
|
-
* Generates a symmetric key for XChaCha20 in JSON Web Key (JWK) format.
|
|
237
|
-
*
|
|
238
|
-
* @remarks
|
|
239
|
-
* This method creates a new symmetric key suitable for use with the XChaCha20 encryption
|
|
240
|
-
* algorithm. The key is generated using cryptographically secure random number generation
|
|
241
|
-
* to ensure its uniqueness and security. The XChaCha20 algorithm requires a 256-bit key
|
|
242
|
-
* (32 bytes), and this method adheres to that specification.
|
|
243
|
-
*
|
|
244
|
-
* Key components included in the JWK:
|
|
245
|
-
* - `kty`: Key Type, set to 'oct' for Octet Sequence.
|
|
246
|
-
* - `k`: The symmetric key component, base64url-encoded.
|
|
247
|
-
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
248
|
-
*
|
|
249
|
-
* @example
|
|
250
|
-
* ```ts
|
|
251
|
-
* const privateKey = await XChaCha20.generateKey();
|
|
252
|
-
* ```
|
|
253
|
-
*
|
|
254
|
-
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
255
|
-
*/
|
|
256
|
-
XChaCha20.generateKey = function () {
|
|
257
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
258
|
-
var webCrypto, webCryptoKey, _a, alg, ext, key_ops, privateKey, _b;
|
|
259
|
-
return __generator(this, function (_c) {
|
|
260
|
-
switch (_c.label) {
|
|
261
|
-
case 0:
|
|
262
|
-
webCrypto = (0, webcrypto_1.getWebcryptoSubtle)();
|
|
263
|
-
return [4 /*yield*/, webCrypto.generateKey({ name: 'AES-CTR', length: 256 }, true, ['encrypt'])];
|
|
264
|
-
case 1:
|
|
265
|
-
webCryptoKey = _c.sent();
|
|
266
|
-
return [4 /*yield*/, webCrypto.exportKey('jwk', webCryptoKey)];
|
|
267
|
-
case 2:
|
|
268
|
-
_a = _c.sent(), alg = _a.alg, ext = _a.ext, key_ops = _a.key_ops, privateKey = __rest(_a, ["alg", "ext", "key_ops"]);
|
|
269
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
270
|
-
_b = privateKey;
|
|
271
|
-
return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
|
|
272
|
-
case 3:
|
|
273
|
-
// Compute the JWK thumbprint and set as the key ID.
|
|
274
|
-
_b.kid = _c.sent();
|
|
275
|
-
return [2 /*return*/, privateKey];
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
});
|
|
279
|
-
};
|
|
280
|
-
/**
|
|
281
|
-
* Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
|
|
282
|
-
*
|
|
283
|
-
* @remarks
|
|
284
|
-
* This method takes a symmetric key in JWK format and extracts its raw byte representation.
|
|
285
|
-
* It decodes the 'k' parameter of the JWK value, which represents the symmetric key in base64url
|
|
286
|
-
* encoding, into a byte array.
|
|
287
|
-
*
|
|
288
|
-
* @example
|
|
289
|
-
* ```ts
|
|
290
|
-
* const privateKey = { ... }; // A symmetric key in JWK format
|
|
291
|
-
* const privateKeyBytes = await XChaCha20.privateKeyToBytes({ privateKey });
|
|
292
|
-
* ```
|
|
293
|
-
*
|
|
294
|
-
* @param params - The parameters for the symmetric key conversion.
|
|
295
|
-
* @param params.privateKey - The symmetric key in JWK format.
|
|
296
|
-
*
|
|
297
|
-
* @returns A Promise that resolves to the symmetric key as a Uint8Array.
|
|
298
|
-
*/
|
|
299
|
-
XChaCha20.privateKeyToBytes = function (_a) {
|
|
300
|
-
return __awaiter(this, arguments, void 0, function (_b) {
|
|
301
|
-
var privateKeyBytes;
|
|
302
|
-
var privateKey = _b.privateKey;
|
|
303
|
-
return __generator(this, function (_c) {
|
|
304
|
-
// Verify the provided JWK represents a valid oct private key.
|
|
305
|
-
if (!(0, jwk_js_1.isOctPrivateJwk)(privateKey)) {
|
|
306
|
-
throw new Error("XChaCha20: The provided key is not a valid oct private key.");
|
|
307
|
-
}
|
|
308
|
-
privateKeyBytes = common_1.Convert.base64Url(privateKey.k).toUint8Array();
|
|
309
|
-
return [2 /*return*/, privateKeyBytes];
|
|
310
|
-
});
|
|
311
|
-
});
|
|
312
|
-
};
|
|
313
|
-
return XChaCha20;
|
|
314
|
-
}());
|
|
315
|
-
exports.XChaCha20 = XChaCha20;
|
|
316
|
-
//# sourceMappingURL=xchacha20.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"xchacha20.js","sourceRoot":"","sources":["../../../src/primitives/xchacha20.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wCAAwC;AACxC,gDAAkD;AAClD,sDAA8D;AAI9D,yCAAuE;AAEvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH;IAAA;IAiMA,CAAC;IAhMC;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;I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