shogun-core 5.2.0 → 5.2.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/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js +1220 -0
- package/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js +844 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js +2335 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js +999 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +1651 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js +825 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js +508 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js +747 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +1608 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js +2117 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js +86 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js.map +1 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js +539 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/shogun-core.js +160386 -0
- package/dist/browser/shogun-core.js.map +1 -0
- package/dist/config/simplified-config.js +236 -0
- package/dist/core.js +329 -0
- package/dist/crypto/asymmetric.js +99 -0
- package/dist/crypto/double-ratchet.js +370 -0
- package/dist/crypto/file-encryption.js +213 -0
- package/dist/crypto/hashing.js +87 -0
- package/dist/crypto/index.js +34 -0
- package/dist/crypto/mls-codec.js +202 -0
- package/dist/crypto/mls.js +550 -0
- package/dist/crypto/pgp.js +390 -0
- package/dist/crypto/random-generation.js +341 -0
- package/dist/crypto/sframe.js +350 -0
- package/dist/crypto/signal-protocol.js +376 -0
- package/dist/crypto/symmetric.js +91 -0
- package/dist/crypto/types.js +2 -0
- package/dist/crypto/utils.js +140 -0
- package/dist/examples/auth-test.js +253 -0
- package/dist/examples/crypto-identity-example.js +151 -0
- package/dist/examples/crypto-working-test.js +83 -0
- package/dist/examples/double-ratchet-test.js +155 -0
- package/dist/examples/mls-advanced-example.js +294 -0
- package/dist/examples/mls-sframe-test.js +304 -0
- package/dist/examples/pgp-example.js +200 -0
- package/dist/examples/quick-auth-test.js +61 -0
- package/dist/examples/random-generation-test.js +151 -0
- package/dist/examples/signal-protocol-test.js +38 -0
- package/dist/examples/simple-api-test.js +114 -0
- package/dist/examples/simple-crypto-identity-example.js +84 -0
- package/dist/examples/timeout-test.js +227 -0
- package/dist/examples/zkproof-credentials-example.js +212 -0
- package/dist/examples/zkproof-example.js +201 -0
- package/dist/gundb/api.js +435 -0
- package/dist/gundb/crypto.js +283 -0
- package/dist/gundb/db.js +1946 -0
- package/dist/gundb/derive.js +232 -0
- package/dist/gundb/errors.js +76 -0
- package/dist/gundb/index.js +22 -0
- package/dist/gundb/rxjs.js +447 -0
- package/dist/gundb/types.js +5 -0
- package/dist/index.js +58 -0
- package/dist/interfaces/common.js +2 -0
- package/dist/interfaces/events.js +40 -0
- package/dist/interfaces/plugin.js +2 -0
- package/dist/interfaces/shogun.js +37 -0
- package/dist/managers/AuthManager.js +226 -0
- package/dist/managers/CoreInitializer.js +228 -0
- package/dist/managers/CryptoIdentityManager.js +366 -0
- package/dist/managers/EventManager.js +70 -0
- package/dist/managers/PluginManager.js +299 -0
- package/dist/plugins/base.js +50 -0
- package/dist/plugins/index.js +32 -0
- package/dist/plugins/nostr/index.js +20 -0
- package/dist/plugins/nostr/nostrConnector.js +419 -0
- package/dist/plugins/nostr/nostrConnectorPlugin.js +453 -0
- package/dist/plugins/nostr/nostrSigner.js +319 -0
- package/dist/plugins/nostr/types.js +2 -0
- package/dist/plugins/smartwallet/index.js +18 -0
- package/dist/plugins/smartwallet/smartWalletPlugin.js +511 -0
- package/dist/plugins/smartwallet/types.js +2 -0
- package/dist/plugins/web3/index.js +20 -0
- package/dist/plugins/web3/types.js +2 -0
- package/dist/plugins/web3/web3Connector.js +533 -0
- package/dist/plugins/web3/web3ConnectorPlugin.js +455 -0
- package/dist/plugins/web3/web3Signer.js +314 -0
- package/dist/plugins/webauthn/index.js +19 -0
- package/dist/plugins/webauthn/types.js +14 -0
- package/dist/plugins/webauthn/webauthn.js +496 -0
- package/dist/plugins/webauthn/webauthnPlugin.js +489 -0
- package/dist/plugins/webauthn/webauthnSigner.js +310 -0
- package/dist/plugins/zkproof/index.js +53 -0
- package/dist/plugins/zkproof/types.js +2 -0
- package/dist/plugins/zkproof/zkCredentials.js +213 -0
- package/dist/plugins/zkproof/zkProofConnector.js +198 -0
- package/dist/plugins/zkproof/zkProofPlugin.js +272 -0
- package/dist/storage/storage.js +145 -0
- package/dist/types/config/simplified-config.d.ts +114 -0
- package/dist/types/core.d.ts +305 -0
- package/dist/types/crypto/asymmetric.d.ts +6 -0
- package/dist/types/crypto/double-ratchet.d.ts +22 -0
- package/dist/types/crypto/file-encryption.d.ts +19 -0
- package/dist/types/crypto/hashing.d.ts +9 -0
- package/dist/types/crypto/index.d.ts +13 -0
- package/dist/types/crypto/mls-codec.d.ts +39 -0
- package/dist/types/crypto/mls.d.ts +130 -0
- package/dist/types/crypto/pgp.d.ts +95 -0
- package/dist/types/crypto/random-generation.d.ts +35 -0
- package/dist/types/crypto/sframe.d.ts +102 -0
- package/dist/types/crypto/signal-protocol.d.ts +26 -0
- package/dist/types/crypto/symmetric.d.ts +9 -0
- package/dist/types/crypto/types.d.ts +144 -0
- package/dist/types/crypto/utils.d.ts +22 -0
- package/dist/types/examples/auth-test.d.ts +8 -0
- package/dist/types/examples/crypto-identity-example.d.ts +5 -0
- package/dist/types/examples/crypto-working-test.d.ts +1 -0
- package/dist/types/examples/double-ratchet-test.d.ts +1 -0
- package/dist/types/examples/mls-advanced-example.d.ts +53 -0
- package/dist/types/examples/mls-sframe-test.d.ts +1 -0
- package/dist/types/examples/pgp-example.d.ts +75 -0
- package/dist/types/examples/quick-auth-test.d.ts +8 -0
- package/dist/types/examples/random-generation-test.d.ts +1 -0
- package/dist/types/examples/signal-protocol-test.d.ts +1 -0
- package/dist/types/examples/simple-api-test.d.ts +10 -0
- package/dist/types/examples/simple-crypto-identity-example.d.ts +6 -0
- package/dist/types/examples/timeout-test.d.ts +8 -0
- package/dist/types/examples/zkproof-credentials-example.d.ts +12 -0
- package/dist/types/examples/zkproof-example.d.ts +11 -0
- package/dist/types/gundb/api.d.ts +185 -0
- package/dist/types/gundb/crypto.d.ts +95 -0
- package/dist/types/gundb/db.d.ts +397 -0
- package/dist/types/gundb/derive.d.ts +21 -0
- package/dist/types/gundb/errors.d.ts +42 -0
- package/dist/types/gundb/index.d.ts +3 -0
- package/dist/types/gundb/rxjs.d.ts +110 -0
- package/dist/types/gundb/types.d.ts +255 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/interfaces/common.d.ts +85 -0
- package/dist/types/interfaces/events.d.ts +131 -0
- package/dist/types/interfaces/plugin.d.ts +162 -0
- package/dist/types/interfaces/shogun.d.ts +208 -0
- package/dist/types/managers/AuthManager.d.ts +72 -0
- package/dist/types/managers/CoreInitializer.d.ts +40 -0
- package/dist/types/managers/CryptoIdentityManager.d.ts +102 -0
- package/dist/types/managers/EventManager.d.ts +49 -0
- package/dist/types/managers/PluginManager.d.ts +145 -0
- package/dist/types/plugins/base.d.ts +35 -0
- package/dist/types/plugins/index.d.ts +18 -0
- package/dist/types/plugins/nostr/index.d.ts +4 -0
- package/dist/types/plugins/nostr/nostrConnector.d.ts +119 -0
- package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +163 -0
- package/dist/types/plugins/nostr/nostrSigner.d.ts +105 -0
- package/dist/types/plugins/nostr/types.d.ts +122 -0
- package/dist/types/plugins/smartwallet/index.d.ts +2 -0
- package/dist/types/plugins/smartwallet/smartWalletPlugin.d.ts +67 -0
- package/dist/types/plugins/smartwallet/types.d.ts +80 -0
- package/dist/types/plugins/web3/index.d.ts +4 -0
- package/dist/types/plugins/web3/types.d.ts +107 -0
- package/dist/types/plugins/web3/web3Connector.d.ts +129 -0
- package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +160 -0
- package/dist/types/plugins/web3/web3Signer.d.ts +114 -0
- package/dist/types/plugins/webauthn/index.d.ts +3 -0
- package/dist/types/plugins/webauthn/types.d.ts +183 -0
- package/dist/types/plugins/webauthn/webauthn.d.ts +129 -0
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +179 -0
- package/dist/types/plugins/webauthn/webauthnSigner.d.ts +91 -0
- package/dist/types/plugins/zkproof/index.d.ts +48 -0
- package/dist/types/plugins/zkproof/types.d.ts +123 -0
- package/dist/types/plugins/zkproof/zkCredentials.d.ts +112 -0
- package/dist/types/plugins/zkproof/zkProofConnector.d.ts +46 -0
- package/dist/types/plugins/zkproof/zkProofPlugin.d.ts +76 -0
- package/dist/types/storage/storage.d.ts +51 -0
- package/dist/types/utils/errorHandler.d.ts +119 -0
- package/dist/types/utils/eventEmitter.d.ts +39 -0
- package/dist/types/utils/seedPhrase.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +27 -0
- package/dist/utils/errorHandler.js +246 -0
- package/dist/utils/eventEmitter.js +79 -0
- package/dist/utils/seedPhrase.js +97 -0
- package/dist/utils/validation.js +81 -0
- package/package.json +10 -1
|
@@ -0,0 +1,999 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(this["webpackChunkShogunCore"] = this["webpackChunkShogunCore"] || []).push([["defaultVendors-node_modules_noble_ciphers_chacha_js"],{
|
|
3
|
+
|
|
4
|
+
/***/ "./node_modules/@noble/ciphers/chacha.js":
|
|
5
|
+
/*!***********************************************************!*\
|
|
6
|
+
!*** ./node_modules/@noble/ciphers/chacha.js + 2 modules ***!
|
|
7
|
+
\***********************************************************/
|
|
8
|
+
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
|
|
9
|
+
|
|
10
|
+
// ESM COMPAT FLAG
|
|
11
|
+
__webpack_require__.r(__webpack_exports__);
|
|
12
|
+
|
|
13
|
+
// EXPORTS
|
|
14
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
15
|
+
_poly1305_aead: () => (/* binding */ _poly1305_aead),
|
|
16
|
+
chacha12: () => (/* binding */ chacha12),
|
|
17
|
+
chacha20: () => (/* binding */ chacha20),
|
|
18
|
+
chacha20orig: () => (/* binding */ chacha20orig),
|
|
19
|
+
chacha20poly1305: () => (/* binding */ chacha20poly1305),
|
|
20
|
+
chacha8: () => (/* binding */ chacha8),
|
|
21
|
+
hchacha: () => (/* binding */ hchacha),
|
|
22
|
+
rngChacha20: () => (/* binding */ rngChacha20),
|
|
23
|
+
rngChacha8: () => (/* binding */ rngChacha8),
|
|
24
|
+
xchacha20: () => (/* binding */ xchacha20),
|
|
25
|
+
xchacha20poly1305: () => (/* binding */ xchacha20poly1305)
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// EXTERNAL MODULE: ./node_modules/@noble/ciphers/utils.js
|
|
29
|
+
var utils = __webpack_require__("./node_modules/@noble/ciphers/utils.js");
|
|
30
|
+
;// ./node_modules/@noble/ciphers/_arx.js
|
|
31
|
+
/**
|
|
32
|
+
* Basic utils for ARX (add-rotate-xor) salsa and chacha ciphers.
|
|
33
|
+
|
|
34
|
+
RFC8439 requires multi-step cipher stream, where
|
|
35
|
+
authKey starts with counter: 0, actual msg with counter: 1.
|
|
36
|
+
|
|
37
|
+
For this, we need a way to re-use nonce / counter:
|
|
38
|
+
|
|
39
|
+
const counter = new Uint8Array(4);
|
|
40
|
+
chacha(..., counter, ...); // counter is now 1
|
|
41
|
+
chacha(..., counter, ...); // counter is now 2
|
|
42
|
+
|
|
43
|
+
This is complicated:
|
|
44
|
+
|
|
45
|
+
- 32-bit counters are enough, no need for 64-bit: max ArrayBuffer size in JS is 4GB
|
|
46
|
+
- Original papers don't allow mutating counters
|
|
47
|
+
- Counter overflow is undefined [^1]
|
|
48
|
+
- Idea A: allow providing (nonce | counter) instead of just nonce, re-use it
|
|
49
|
+
- Caveat: Cannot be re-used through all cases:
|
|
50
|
+
- * chacha has (counter | nonce)
|
|
51
|
+
- * xchacha has (nonce16 | counter | nonce16)
|
|
52
|
+
- Idea B: separate nonce / counter and provide separate API for counter re-use
|
|
53
|
+
- Caveat: there are different counter sizes depending on an algorithm.
|
|
54
|
+
- salsa & chacha also differ in structures of key & sigma:
|
|
55
|
+
salsa20: s[0] | k(4) | s[1] | nonce(2) | cnt(2) | s[2] | k(4) | s[3]
|
|
56
|
+
chacha: s(4) | k(8) | cnt(1) | nonce(3)
|
|
57
|
+
chacha20orig: s(4) | k(8) | cnt(2) | nonce(2)
|
|
58
|
+
- Idea C: helper method such as `setSalsaState(key, nonce, sigma, data)`
|
|
59
|
+
- Caveat: we can't re-use counter array
|
|
60
|
+
|
|
61
|
+
xchacha [^2] uses the subkey and remaining 8 byte nonce with ChaCha20 as normal
|
|
62
|
+
(prefixed by 4 NUL bytes, since [RFC8439] specifies a 12-byte nonce).
|
|
63
|
+
|
|
64
|
+
[^1]: https://mailarchive.ietf.org/arch/msg/cfrg/gsOnTJzcbgG6OqD8Sc0GO5aR_tU/
|
|
65
|
+
[^2]: https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha#appendix-A.2
|
|
66
|
+
|
|
67
|
+
* @module
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
// Replaces `TextEncoder`, which is not available in all environments
|
|
71
|
+
const encodeStr = (str) => Uint8Array.from(str.split(''), (c) => c.charCodeAt(0));
|
|
72
|
+
const sigma16 = encodeStr('expand 16-byte k');
|
|
73
|
+
const sigma32 = encodeStr('expand 32-byte k');
|
|
74
|
+
const sigma16_32 = (0,utils.u32)(sigma16);
|
|
75
|
+
const sigma32_32 = (0,utils.u32)(sigma32);
|
|
76
|
+
/** Rotate left. */
|
|
77
|
+
function rotl(a, b) {
|
|
78
|
+
return (a << b) | (a >>> (32 - b));
|
|
79
|
+
}
|
|
80
|
+
// Is byte array aligned to 4 byte offset (u32)?
|
|
81
|
+
function isAligned32(b) {
|
|
82
|
+
return b.byteOffset % 4 === 0;
|
|
83
|
+
}
|
|
84
|
+
// Salsa and Chacha block length is always 512-bit
|
|
85
|
+
const BLOCK_LEN = 64;
|
|
86
|
+
const BLOCK_LEN32 = 16;
|
|
87
|
+
// new Uint32Array([2**32]) // => Uint32Array(1) [ 0 ]
|
|
88
|
+
// new Uint32Array([2**32-1]) // => Uint32Array(1) [ 4294967295 ]
|
|
89
|
+
const MAX_COUNTER = 2 ** 32 - 1;
|
|
90
|
+
const U32_EMPTY = Uint32Array.of();
|
|
91
|
+
function runCipher(core, sigma, key, nonce, data, output, counter, rounds) {
|
|
92
|
+
const len = data.length;
|
|
93
|
+
const block = new Uint8Array(BLOCK_LEN);
|
|
94
|
+
const b32 = (0,utils.u32)(block);
|
|
95
|
+
// Make sure that buffers aligned to 4 bytes
|
|
96
|
+
const isAligned = isAligned32(data) && isAligned32(output);
|
|
97
|
+
const d32 = isAligned ? (0,utils.u32)(data) : U32_EMPTY;
|
|
98
|
+
const o32 = isAligned ? (0,utils.u32)(output) : U32_EMPTY;
|
|
99
|
+
for (let pos = 0; pos < len; counter++) {
|
|
100
|
+
core(sigma, key, nonce, b32, counter, rounds);
|
|
101
|
+
if (counter >= MAX_COUNTER)
|
|
102
|
+
throw new Error('arx: counter overflow');
|
|
103
|
+
const take = Math.min(BLOCK_LEN, len - pos);
|
|
104
|
+
// aligned to 4 bytes
|
|
105
|
+
if (isAligned && take === BLOCK_LEN) {
|
|
106
|
+
const pos32 = pos / 4;
|
|
107
|
+
if (pos % 4 !== 0)
|
|
108
|
+
throw new Error('arx: invalid block position');
|
|
109
|
+
for (let j = 0, posj; j < BLOCK_LEN32; j++) {
|
|
110
|
+
posj = pos32 + j;
|
|
111
|
+
o32[posj] = d32[posj] ^ b32[j];
|
|
112
|
+
}
|
|
113
|
+
pos += BLOCK_LEN;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
for (let j = 0, posj; j < take; j++) {
|
|
117
|
+
posj = pos + j;
|
|
118
|
+
output[posj] = data[posj] ^ block[j];
|
|
119
|
+
}
|
|
120
|
+
pos += take;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Creates ARX-like (ChaCha, Salsa) cipher stream from core function. */
|
|
124
|
+
function createCipher(core, opts) {
|
|
125
|
+
const { allowShortKeys, extendNonceFn, counterLength, counterRight, rounds } = (0,utils.checkOpts)({ allowShortKeys: false, counterLength: 8, counterRight: false, rounds: 20 }, opts);
|
|
126
|
+
if (typeof core !== 'function')
|
|
127
|
+
throw new Error('core must be a function');
|
|
128
|
+
(0,utils.anumber)(counterLength);
|
|
129
|
+
(0,utils.anumber)(rounds);
|
|
130
|
+
(0,utils.abool)(counterRight);
|
|
131
|
+
(0,utils.abool)(allowShortKeys);
|
|
132
|
+
return (key, nonce, data, output, counter = 0) => {
|
|
133
|
+
(0,utils.abytes)(key, undefined, 'key');
|
|
134
|
+
(0,utils.abytes)(nonce, undefined, 'nonce');
|
|
135
|
+
(0,utils.abytes)(data, undefined, 'data');
|
|
136
|
+
const len = data.length;
|
|
137
|
+
if (output === undefined)
|
|
138
|
+
output = new Uint8Array(len);
|
|
139
|
+
(0,utils.abytes)(output, undefined, 'output');
|
|
140
|
+
(0,utils.anumber)(counter);
|
|
141
|
+
if (counter < 0 || counter >= MAX_COUNTER)
|
|
142
|
+
throw new Error('arx: counter overflow');
|
|
143
|
+
if (output.length < len)
|
|
144
|
+
throw new Error(`arx: output (${output.length}) is shorter than data (${len})`);
|
|
145
|
+
const toClean = [];
|
|
146
|
+
// Key & sigma
|
|
147
|
+
// key=16 -> sigma16, k=key|key
|
|
148
|
+
// key=32 -> sigma32, k=key
|
|
149
|
+
let l = key.length;
|
|
150
|
+
let k;
|
|
151
|
+
let sigma;
|
|
152
|
+
if (l === 32) {
|
|
153
|
+
toClean.push((k = (0,utils.copyBytes)(key)));
|
|
154
|
+
sigma = sigma32_32;
|
|
155
|
+
}
|
|
156
|
+
else if (l === 16 && allowShortKeys) {
|
|
157
|
+
k = new Uint8Array(32);
|
|
158
|
+
k.set(key);
|
|
159
|
+
k.set(key, 16);
|
|
160
|
+
sigma = sigma16_32;
|
|
161
|
+
toClean.push(k);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
(0,utils.abytes)(key, 32, 'arx key');
|
|
165
|
+
throw new Error('invalid key size');
|
|
166
|
+
// throw new Error(`"arx key" expected Uint8Array of length 32, got length=${l}`);
|
|
167
|
+
}
|
|
168
|
+
// Nonce
|
|
169
|
+
// salsa20: 8 (8-byte counter)
|
|
170
|
+
// chacha20orig: 8 (8-byte counter)
|
|
171
|
+
// chacha20: 12 (4-byte counter)
|
|
172
|
+
// xsalsa20: 24 (16 -> hsalsa, 8 -> old nonce)
|
|
173
|
+
// xchacha20: 24 (16 -> hchacha, 8 -> old nonce)
|
|
174
|
+
// Align nonce to 4 bytes
|
|
175
|
+
if (!isAligned32(nonce))
|
|
176
|
+
toClean.push((nonce = (0,utils.copyBytes)(nonce)));
|
|
177
|
+
const k32 = (0,utils.u32)(k);
|
|
178
|
+
// hsalsa & hchacha: handle extended nonce
|
|
179
|
+
if (extendNonceFn) {
|
|
180
|
+
if (nonce.length !== 24)
|
|
181
|
+
throw new Error(`arx: extended nonce must be 24 bytes`);
|
|
182
|
+
extendNonceFn(sigma, k32, (0,utils.u32)(nonce.subarray(0, 16)), k32);
|
|
183
|
+
nonce = nonce.subarray(16);
|
|
184
|
+
}
|
|
185
|
+
// Handle nonce counter
|
|
186
|
+
const nonceNcLen = 16 - counterLength;
|
|
187
|
+
if (nonceNcLen !== nonce.length)
|
|
188
|
+
throw new Error(`arx: nonce must be ${nonceNcLen} or 16 bytes`);
|
|
189
|
+
// Pad counter when nonce is 64 bit
|
|
190
|
+
if (nonceNcLen !== 12) {
|
|
191
|
+
const nc = new Uint8Array(12);
|
|
192
|
+
nc.set(nonce, counterRight ? 0 : 12 - nonce.length);
|
|
193
|
+
nonce = nc;
|
|
194
|
+
toClean.push(nonce);
|
|
195
|
+
}
|
|
196
|
+
const n32 = (0,utils.u32)(nonce);
|
|
197
|
+
runCipher(core, sigma, k32, n32, data, output, counter, rounds);
|
|
198
|
+
(0,utils.clean)(...toClean);
|
|
199
|
+
return output;
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
/** Internal class which wraps chacha20 or chacha8 to create CSPRNG. */
|
|
203
|
+
class _XorStreamPRG {
|
|
204
|
+
blockLen;
|
|
205
|
+
keyLen;
|
|
206
|
+
nonceLen;
|
|
207
|
+
state;
|
|
208
|
+
buf;
|
|
209
|
+
key;
|
|
210
|
+
nonce;
|
|
211
|
+
pos;
|
|
212
|
+
ctr;
|
|
213
|
+
cipher;
|
|
214
|
+
constructor(cipher, blockLen, keyLen, nonceLen, seed) {
|
|
215
|
+
this.cipher = cipher;
|
|
216
|
+
this.blockLen = blockLen;
|
|
217
|
+
this.keyLen = keyLen;
|
|
218
|
+
this.nonceLen = nonceLen;
|
|
219
|
+
this.state = new Uint8Array(this.keyLen + this.nonceLen);
|
|
220
|
+
this.reseed(seed);
|
|
221
|
+
this.ctr = 0;
|
|
222
|
+
this.pos = this.blockLen;
|
|
223
|
+
this.buf = new Uint8Array(this.blockLen);
|
|
224
|
+
this.key = this.state.subarray(0, this.keyLen);
|
|
225
|
+
this.nonce = this.state.subarray(this.keyLen);
|
|
226
|
+
}
|
|
227
|
+
reseed(seed) {
|
|
228
|
+
(0,utils.abytes)(seed);
|
|
229
|
+
if (!seed || seed.length === 0)
|
|
230
|
+
throw new Error('entropy required');
|
|
231
|
+
for (let i = 0; i < seed.length; i++)
|
|
232
|
+
this.state[i % this.state.length] ^= seed[i];
|
|
233
|
+
this.ctr = 0;
|
|
234
|
+
this.pos = this.blockLen;
|
|
235
|
+
}
|
|
236
|
+
addEntropy(seed) {
|
|
237
|
+
this.state.set(this.randomBytes(this.state.length));
|
|
238
|
+
this.reseed(seed);
|
|
239
|
+
}
|
|
240
|
+
randomBytes(len) {
|
|
241
|
+
(0,utils.anumber)(len);
|
|
242
|
+
if (len === 0)
|
|
243
|
+
return new Uint8Array(0);
|
|
244
|
+
const out = new Uint8Array(len);
|
|
245
|
+
let outPos = 0;
|
|
246
|
+
// Leftovers
|
|
247
|
+
if (this.pos < this.blockLen) {
|
|
248
|
+
const take = Math.min(len, this.blockLen - this.pos);
|
|
249
|
+
out.set(this.buf.subarray(this.pos, this.pos + take), 0);
|
|
250
|
+
this.pos += take;
|
|
251
|
+
outPos += take;
|
|
252
|
+
if (outPos === len)
|
|
253
|
+
return out; // fast path
|
|
254
|
+
}
|
|
255
|
+
// Full blocks directly to out
|
|
256
|
+
const blocks = Math.floor((len - outPos) / this.blockLen);
|
|
257
|
+
if (blocks > 0) {
|
|
258
|
+
const blockBytes = blocks * this.blockLen;
|
|
259
|
+
const b = out.subarray(outPos, outPos + blockBytes);
|
|
260
|
+
this.cipher(this.key, this.nonce, b, b, this.ctr);
|
|
261
|
+
this.ctr += blocks;
|
|
262
|
+
outPos += blockBytes;
|
|
263
|
+
}
|
|
264
|
+
// Save leftovers
|
|
265
|
+
const left = len - outPos;
|
|
266
|
+
if (left > 0) {
|
|
267
|
+
this.buf.fill(0);
|
|
268
|
+
// NOTE: cipher will handle overflow
|
|
269
|
+
this.cipher(this.key, this.nonce, this.buf, this.buf, this.ctr++);
|
|
270
|
+
out.set(this.buf.subarray(0, left), outPos);
|
|
271
|
+
this.pos = left;
|
|
272
|
+
}
|
|
273
|
+
return out;
|
|
274
|
+
}
|
|
275
|
+
clone() {
|
|
276
|
+
return new _XorStreamPRG(this.cipher, this.blockLen, this.keyLen, this.nonceLen, this.randomBytes(this.state.length));
|
|
277
|
+
}
|
|
278
|
+
clean() {
|
|
279
|
+
this.pos = 0;
|
|
280
|
+
this.ctr = 0;
|
|
281
|
+
this.buf.fill(0);
|
|
282
|
+
this.state.fill(0);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const createPRG = (cipher, blockLen, keyLen, nonceLen) => {
|
|
286
|
+
return (seed = (0,utils.randomBytes)(32)) => new _XorStreamPRG(cipher, blockLen, keyLen, nonceLen, seed);
|
|
287
|
+
};
|
|
288
|
+
//# sourceMappingURL=_arx.js.map
|
|
289
|
+
;// ./node_modules/@noble/ciphers/_poly1305.js
|
|
290
|
+
/**
|
|
291
|
+
* Poly1305 ([PDF](https://cr.yp.to/mac/poly1305-20050329.pdf),
|
|
292
|
+
* [wiki](https://en.wikipedia.org/wiki/Poly1305))
|
|
293
|
+
* is a fast and parallel secret-key message-authentication code suitable for
|
|
294
|
+
* a wide variety of applications. It was standardized in
|
|
295
|
+
* [RFC 8439](https://www.rfc-editor.org/rfc/rfc8439) and is now used in TLS 1.3.
|
|
296
|
+
*
|
|
297
|
+
* Polynomial MACs are not perfect for every situation:
|
|
298
|
+
* they lack Random Key Robustness: the MAC can be forged, and can't be used in PAKE schemes.
|
|
299
|
+
* See [invisible salamanders attack](https://keymaterial.net/2020/09/07/invisible-salamanders-in-aes-gcm-siv/).
|
|
300
|
+
* To combat invisible salamanders, `hash(key)` can be included in ciphertext,
|
|
301
|
+
* however, this would violate ciphertext indistinguishability:
|
|
302
|
+
* an attacker would know which key was used - so `HKDF(key, i)`
|
|
303
|
+
* could be used instead.
|
|
304
|
+
*
|
|
305
|
+
* Check out [original website](https://cr.yp.to/mac.html).
|
|
306
|
+
* Based on Public Domain [poly1305-donna](https://github.com/floodyberry/poly1305-donna).
|
|
307
|
+
* @module
|
|
308
|
+
*/
|
|
309
|
+
// prettier-ignore
|
|
310
|
+
|
|
311
|
+
function u8to16(a, i) {
|
|
312
|
+
return (a[i++] & 0xff) | ((a[i++] & 0xff) << 8);
|
|
313
|
+
}
|
|
314
|
+
function bytesToNumberLE(bytes) {
|
|
315
|
+
return (0,utils.hexToNumber)((0,utils.bytesToHex)(Uint8Array.from(bytes).reverse()));
|
|
316
|
+
}
|
|
317
|
+
/** Small version of `poly1305` without loop unrolling. Unused, provided for auditability. */
|
|
318
|
+
function poly1305_small(msg, key) {
|
|
319
|
+
(0,utils.abytes)(msg);
|
|
320
|
+
(0,utils.abytes)(key, 32, 'key');
|
|
321
|
+
const POW_2_130_5 = BigInt(2) ** BigInt(130) - BigInt(5); // 2^130-5
|
|
322
|
+
const POW_2_128_1 = BigInt(2) ** BigInt(128) - BigInt(1); // 2^128-1
|
|
323
|
+
const CLAMP_R = BigInt('0x0ffffffc0ffffffc0ffffffc0fffffff');
|
|
324
|
+
const r = bytesToNumberLE(key.subarray(0, 16)) & CLAMP_R;
|
|
325
|
+
const s = bytesToNumberLE(key.subarray(16));
|
|
326
|
+
// Process by 16 byte chunks
|
|
327
|
+
let acc = BigInt(0);
|
|
328
|
+
for (let i = 0; i < msg.length; i += 16) {
|
|
329
|
+
const m = msg.subarray(i, i + 16);
|
|
330
|
+
const n = bytesToNumberLE(m) | (BigInt(1) << BigInt(8 * m.length));
|
|
331
|
+
acc = ((acc + n) * r) % POW_2_130_5;
|
|
332
|
+
}
|
|
333
|
+
const res = (acc + s) & POW_2_128_1;
|
|
334
|
+
return (0,utils.numberToBytesBE)(res, 16).reverse(); // LE
|
|
335
|
+
}
|
|
336
|
+
// Can be used to replace `computeTag` in chacha.ts. Unused, provided for auditability.
|
|
337
|
+
// @ts-expect-error
|
|
338
|
+
function poly1305_computeTag_small(authKey, lengths, ciphertext, AAD) {
|
|
339
|
+
const res = [];
|
|
340
|
+
const updatePadded2 = (msg) => {
|
|
341
|
+
res.push(msg);
|
|
342
|
+
const leftover = msg.length % 16;
|
|
343
|
+
if (leftover)
|
|
344
|
+
res.push(new Uint8Array(16).slice(leftover));
|
|
345
|
+
};
|
|
346
|
+
if (AAD)
|
|
347
|
+
updatePadded2(AAD);
|
|
348
|
+
updatePadded2(ciphertext);
|
|
349
|
+
res.push(lengths);
|
|
350
|
+
return poly1305_small((0,utils.concatBytes)(...res), authKey);
|
|
351
|
+
}
|
|
352
|
+
/** Poly1305 class. Prefer poly1305() function instead. */
|
|
353
|
+
class Poly1305 {
|
|
354
|
+
blockLen = 16;
|
|
355
|
+
outputLen = 16;
|
|
356
|
+
buffer = new Uint8Array(16);
|
|
357
|
+
r = new Uint16Array(10); // Allocating 1 array with .subarray() here is slower than 3
|
|
358
|
+
h = new Uint16Array(10);
|
|
359
|
+
pad = new Uint16Array(8);
|
|
360
|
+
pos = 0;
|
|
361
|
+
finished = false;
|
|
362
|
+
// Can be speed-up using BigUint64Array, at the cost of complexity
|
|
363
|
+
constructor(key) {
|
|
364
|
+
key = (0,utils.copyBytes)((0,utils.abytes)(key, 32, 'key'));
|
|
365
|
+
const t0 = u8to16(key, 0);
|
|
366
|
+
const t1 = u8to16(key, 2);
|
|
367
|
+
const t2 = u8to16(key, 4);
|
|
368
|
+
const t3 = u8to16(key, 6);
|
|
369
|
+
const t4 = u8to16(key, 8);
|
|
370
|
+
const t5 = u8to16(key, 10);
|
|
371
|
+
const t6 = u8to16(key, 12);
|
|
372
|
+
const t7 = u8to16(key, 14);
|
|
373
|
+
// https://github.com/floodyberry/poly1305-donna/blob/e6ad6e091d30d7f4ec2d4f978be1fcfcbce72781/poly1305-donna-16.h#L47
|
|
374
|
+
this.r[0] = t0 & 0x1fff;
|
|
375
|
+
this.r[1] = ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
|
|
376
|
+
this.r[2] = ((t1 >>> 10) | (t2 << 6)) & 0x1f03;
|
|
377
|
+
this.r[3] = ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
|
|
378
|
+
this.r[4] = ((t3 >>> 4) | (t4 << 12)) & 0x00ff;
|
|
379
|
+
this.r[5] = (t4 >>> 1) & 0x1ffe;
|
|
380
|
+
this.r[6] = ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
|
|
381
|
+
this.r[7] = ((t5 >>> 11) | (t6 << 5)) & 0x1f81;
|
|
382
|
+
this.r[8] = ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
|
|
383
|
+
this.r[9] = (t7 >>> 5) & 0x007f;
|
|
384
|
+
for (let i = 0; i < 8; i++)
|
|
385
|
+
this.pad[i] = u8to16(key, 16 + 2 * i);
|
|
386
|
+
}
|
|
387
|
+
process(data, offset, isLast = false) {
|
|
388
|
+
const hibit = isLast ? 0 : 1 << 11;
|
|
389
|
+
const { h, r } = this;
|
|
390
|
+
const r0 = r[0];
|
|
391
|
+
const r1 = r[1];
|
|
392
|
+
const r2 = r[2];
|
|
393
|
+
const r3 = r[3];
|
|
394
|
+
const r4 = r[4];
|
|
395
|
+
const r5 = r[5];
|
|
396
|
+
const r6 = r[6];
|
|
397
|
+
const r7 = r[7];
|
|
398
|
+
const r8 = r[8];
|
|
399
|
+
const r9 = r[9];
|
|
400
|
+
const t0 = u8to16(data, offset + 0);
|
|
401
|
+
const t1 = u8to16(data, offset + 2);
|
|
402
|
+
const t2 = u8to16(data, offset + 4);
|
|
403
|
+
const t3 = u8to16(data, offset + 6);
|
|
404
|
+
const t4 = u8to16(data, offset + 8);
|
|
405
|
+
const t5 = u8to16(data, offset + 10);
|
|
406
|
+
const t6 = u8to16(data, offset + 12);
|
|
407
|
+
const t7 = u8to16(data, offset + 14);
|
|
408
|
+
let h0 = h[0] + (t0 & 0x1fff);
|
|
409
|
+
let h1 = h[1] + (((t0 >>> 13) | (t1 << 3)) & 0x1fff);
|
|
410
|
+
let h2 = h[2] + (((t1 >>> 10) | (t2 << 6)) & 0x1fff);
|
|
411
|
+
let h3 = h[3] + (((t2 >>> 7) | (t3 << 9)) & 0x1fff);
|
|
412
|
+
let h4 = h[4] + (((t3 >>> 4) | (t4 << 12)) & 0x1fff);
|
|
413
|
+
let h5 = h[5] + ((t4 >>> 1) & 0x1fff);
|
|
414
|
+
let h6 = h[6] + (((t4 >>> 14) | (t5 << 2)) & 0x1fff);
|
|
415
|
+
let h7 = h[7] + (((t5 >>> 11) | (t6 << 5)) & 0x1fff);
|
|
416
|
+
let h8 = h[8] + (((t6 >>> 8) | (t7 << 8)) & 0x1fff);
|
|
417
|
+
let h9 = h[9] + ((t7 >>> 5) | hibit);
|
|
418
|
+
let c = 0;
|
|
419
|
+
let d0 = c + h0 * r0 + h1 * (5 * r9) + h2 * (5 * r8) + h3 * (5 * r7) + h4 * (5 * r6);
|
|
420
|
+
c = d0 >>> 13;
|
|
421
|
+
d0 &= 0x1fff;
|
|
422
|
+
d0 += h5 * (5 * r5) + h6 * (5 * r4) + h7 * (5 * r3) + h8 * (5 * r2) + h9 * (5 * r1);
|
|
423
|
+
c += d0 >>> 13;
|
|
424
|
+
d0 &= 0x1fff;
|
|
425
|
+
let d1 = c + h0 * r1 + h1 * r0 + h2 * (5 * r9) + h3 * (5 * r8) + h4 * (5 * r7);
|
|
426
|
+
c = d1 >>> 13;
|
|
427
|
+
d1 &= 0x1fff;
|
|
428
|
+
d1 += h5 * (5 * r6) + h6 * (5 * r5) + h7 * (5 * r4) + h8 * (5 * r3) + h9 * (5 * r2);
|
|
429
|
+
c += d1 >>> 13;
|
|
430
|
+
d1 &= 0x1fff;
|
|
431
|
+
let d2 = c + h0 * r2 + h1 * r1 + h2 * r0 + h3 * (5 * r9) + h4 * (5 * r8);
|
|
432
|
+
c = d2 >>> 13;
|
|
433
|
+
d2 &= 0x1fff;
|
|
434
|
+
d2 += h5 * (5 * r7) + h6 * (5 * r6) + h7 * (5 * r5) + h8 * (5 * r4) + h9 * (5 * r3);
|
|
435
|
+
c += d2 >>> 13;
|
|
436
|
+
d2 &= 0x1fff;
|
|
437
|
+
let d3 = c + h0 * r3 + h1 * r2 + h2 * r1 + h3 * r0 + h4 * (5 * r9);
|
|
438
|
+
c = d3 >>> 13;
|
|
439
|
+
d3 &= 0x1fff;
|
|
440
|
+
d3 += h5 * (5 * r8) + h6 * (5 * r7) + h7 * (5 * r6) + h8 * (5 * r5) + h9 * (5 * r4);
|
|
441
|
+
c += d3 >>> 13;
|
|
442
|
+
d3 &= 0x1fff;
|
|
443
|
+
let d4 = c + h0 * r4 + h1 * r3 + h2 * r2 + h3 * r1 + h4 * r0;
|
|
444
|
+
c = d4 >>> 13;
|
|
445
|
+
d4 &= 0x1fff;
|
|
446
|
+
d4 += h5 * (5 * r9) + h6 * (5 * r8) + h7 * (5 * r7) + h8 * (5 * r6) + h9 * (5 * r5);
|
|
447
|
+
c += d4 >>> 13;
|
|
448
|
+
d4 &= 0x1fff;
|
|
449
|
+
let d5 = c + h0 * r5 + h1 * r4 + h2 * r3 + h3 * r2 + h4 * r1;
|
|
450
|
+
c = d5 >>> 13;
|
|
451
|
+
d5 &= 0x1fff;
|
|
452
|
+
d5 += h5 * r0 + h6 * (5 * r9) + h7 * (5 * r8) + h8 * (5 * r7) + h9 * (5 * r6);
|
|
453
|
+
c += d5 >>> 13;
|
|
454
|
+
d5 &= 0x1fff;
|
|
455
|
+
let d6 = c + h0 * r6 + h1 * r5 + h2 * r4 + h3 * r3 + h4 * r2;
|
|
456
|
+
c = d6 >>> 13;
|
|
457
|
+
d6 &= 0x1fff;
|
|
458
|
+
d6 += h5 * r1 + h6 * r0 + h7 * (5 * r9) + h8 * (5 * r8) + h9 * (5 * r7);
|
|
459
|
+
c += d6 >>> 13;
|
|
460
|
+
d6 &= 0x1fff;
|
|
461
|
+
let d7 = c + h0 * r7 + h1 * r6 + h2 * r5 + h3 * r4 + h4 * r3;
|
|
462
|
+
c = d7 >>> 13;
|
|
463
|
+
d7 &= 0x1fff;
|
|
464
|
+
d7 += h5 * r2 + h6 * r1 + h7 * r0 + h8 * (5 * r9) + h9 * (5 * r8);
|
|
465
|
+
c += d7 >>> 13;
|
|
466
|
+
d7 &= 0x1fff;
|
|
467
|
+
let d8 = c + h0 * r8 + h1 * r7 + h2 * r6 + h3 * r5 + h4 * r4;
|
|
468
|
+
c = d8 >>> 13;
|
|
469
|
+
d8 &= 0x1fff;
|
|
470
|
+
d8 += h5 * r3 + h6 * r2 + h7 * r1 + h8 * r0 + h9 * (5 * r9);
|
|
471
|
+
c += d8 >>> 13;
|
|
472
|
+
d8 &= 0x1fff;
|
|
473
|
+
let d9 = c + h0 * r9 + h1 * r8 + h2 * r7 + h3 * r6 + h4 * r5;
|
|
474
|
+
c = d9 >>> 13;
|
|
475
|
+
d9 &= 0x1fff;
|
|
476
|
+
d9 += h5 * r4 + h6 * r3 + h7 * r2 + h8 * r1 + h9 * r0;
|
|
477
|
+
c += d9 >>> 13;
|
|
478
|
+
d9 &= 0x1fff;
|
|
479
|
+
c = ((c << 2) + c) | 0;
|
|
480
|
+
c = (c + d0) | 0;
|
|
481
|
+
d0 = c & 0x1fff;
|
|
482
|
+
c = c >>> 13;
|
|
483
|
+
d1 += c;
|
|
484
|
+
h[0] = d0;
|
|
485
|
+
h[1] = d1;
|
|
486
|
+
h[2] = d2;
|
|
487
|
+
h[3] = d3;
|
|
488
|
+
h[4] = d4;
|
|
489
|
+
h[5] = d5;
|
|
490
|
+
h[6] = d6;
|
|
491
|
+
h[7] = d7;
|
|
492
|
+
h[8] = d8;
|
|
493
|
+
h[9] = d9;
|
|
494
|
+
}
|
|
495
|
+
finalize() {
|
|
496
|
+
const { h, pad } = this;
|
|
497
|
+
const g = new Uint16Array(10);
|
|
498
|
+
let c = h[1] >>> 13;
|
|
499
|
+
h[1] &= 0x1fff;
|
|
500
|
+
for (let i = 2; i < 10; i++) {
|
|
501
|
+
h[i] += c;
|
|
502
|
+
c = h[i] >>> 13;
|
|
503
|
+
h[i] &= 0x1fff;
|
|
504
|
+
}
|
|
505
|
+
h[0] += c * 5;
|
|
506
|
+
c = h[0] >>> 13;
|
|
507
|
+
h[0] &= 0x1fff;
|
|
508
|
+
h[1] += c;
|
|
509
|
+
c = h[1] >>> 13;
|
|
510
|
+
h[1] &= 0x1fff;
|
|
511
|
+
h[2] += c;
|
|
512
|
+
g[0] = h[0] + 5;
|
|
513
|
+
c = g[0] >>> 13;
|
|
514
|
+
g[0] &= 0x1fff;
|
|
515
|
+
for (let i = 1; i < 10; i++) {
|
|
516
|
+
g[i] = h[i] + c;
|
|
517
|
+
c = g[i] >>> 13;
|
|
518
|
+
g[i] &= 0x1fff;
|
|
519
|
+
}
|
|
520
|
+
g[9] -= 1 << 13;
|
|
521
|
+
let mask = (c ^ 1) - 1;
|
|
522
|
+
for (let i = 0; i < 10; i++)
|
|
523
|
+
g[i] &= mask;
|
|
524
|
+
mask = ~mask;
|
|
525
|
+
for (let i = 0; i < 10; i++)
|
|
526
|
+
h[i] = (h[i] & mask) | g[i];
|
|
527
|
+
h[0] = (h[0] | (h[1] << 13)) & 0xffff;
|
|
528
|
+
h[1] = ((h[1] >>> 3) | (h[2] << 10)) & 0xffff;
|
|
529
|
+
h[2] = ((h[2] >>> 6) | (h[3] << 7)) & 0xffff;
|
|
530
|
+
h[3] = ((h[3] >>> 9) | (h[4] << 4)) & 0xffff;
|
|
531
|
+
h[4] = ((h[4] >>> 12) | (h[5] << 1) | (h[6] << 14)) & 0xffff;
|
|
532
|
+
h[5] = ((h[6] >>> 2) | (h[7] << 11)) & 0xffff;
|
|
533
|
+
h[6] = ((h[7] >>> 5) | (h[8] << 8)) & 0xffff;
|
|
534
|
+
h[7] = ((h[8] >>> 8) | (h[9] << 5)) & 0xffff;
|
|
535
|
+
let f = h[0] + pad[0];
|
|
536
|
+
h[0] = f & 0xffff;
|
|
537
|
+
for (let i = 1; i < 8; i++) {
|
|
538
|
+
f = (((h[i] + pad[i]) | 0) + (f >>> 16)) | 0;
|
|
539
|
+
h[i] = f & 0xffff;
|
|
540
|
+
}
|
|
541
|
+
(0,utils.clean)(g);
|
|
542
|
+
}
|
|
543
|
+
update(data) {
|
|
544
|
+
(0,utils.aexists)(this);
|
|
545
|
+
(0,utils.abytes)(data);
|
|
546
|
+
data = (0,utils.copyBytes)(data);
|
|
547
|
+
const { buffer, blockLen } = this;
|
|
548
|
+
const len = data.length;
|
|
549
|
+
for (let pos = 0; pos < len;) {
|
|
550
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
551
|
+
// Fast path: we have at least one block in input
|
|
552
|
+
if (take === blockLen) {
|
|
553
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
554
|
+
this.process(data, pos);
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
558
|
+
this.pos += take;
|
|
559
|
+
pos += take;
|
|
560
|
+
if (this.pos === blockLen) {
|
|
561
|
+
this.process(buffer, 0, false);
|
|
562
|
+
this.pos = 0;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
return this;
|
|
566
|
+
}
|
|
567
|
+
destroy() {
|
|
568
|
+
(0,utils.clean)(this.h, this.r, this.buffer, this.pad);
|
|
569
|
+
}
|
|
570
|
+
digestInto(out) {
|
|
571
|
+
(0,utils.aexists)(this);
|
|
572
|
+
(0,utils.aoutput)(out, this);
|
|
573
|
+
this.finished = true;
|
|
574
|
+
const { buffer, h } = this;
|
|
575
|
+
let { pos } = this;
|
|
576
|
+
if (pos) {
|
|
577
|
+
buffer[pos++] = 1;
|
|
578
|
+
for (; pos < 16; pos++)
|
|
579
|
+
buffer[pos] = 0;
|
|
580
|
+
this.process(buffer, 0, true);
|
|
581
|
+
}
|
|
582
|
+
this.finalize();
|
|
583
|
+
let opos = 0;
|
|
584
|
+
for (let i = 0; i < 8; i++) {
|
|
585
|
+
out[opos++] = h[i] >>> 0;
|
|
586
|
+
out[opos++] = h[i] >>> 8;
|
|
587
|
+
}
|
|
588
|
+
return out;
|
|
589
|
+
}
|
|
590
|
+
digest() {
|
|
591
|
+
const { buffer, outputLen } = this;
|
|
592
|
+
this.digestInto(buffer);
|
|
593
|
+
const res = buffer.slice(0, outputLen);
|
|
594
|
+
this.destroy();
|
|
595
|
+
return res;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
function wrapConstructorWithKey(hashCons) {
|
|
599
|
+
const hashC = (msg, key) => hashCons(key).update(msg).digest();
|
|
600
|
+
const tmp = hashCons(new Uint8Array(32)); // tmp array, used just once below
|
|
601
|
+
hashC.outputLen = tmp.outputLen;
|
|
602
|
+
hashC.blockLen = tmp.blockLen;
|
|
603
|
+
hashC.create = (key) => hashCons(key);
|
|
604
|
+
return hashC;
|
|
605
|
+
}
|
|
606
|
+
/** Poly1305 MAC from RFC 8439. */
|
|
607
|
+
const poly1305 = /** @__PURE__ */ (() => wrapConstructorWithKey((key) => new Poly1305(key)))();
|
|
608
|
+
//# sourceMappingURL=_poly1305.js.map
|
|
609
|
+
;// ./node_modules/@noble/ciphers/chacha.js
|
|
610
|
+
/**
|
|
611
|
+
* ChaCha stream cipher, released
|
|
612
|
+
* in 2008. Developed after Salsa20, ChaCha aims to increase diffusion per round.
|
|
613
|
+
* It was standardized in [RFC 8439](https://www.rfc-editor.org/rfc/rfc8439) and
|
|
614
|
+
* is now used in TLS 1.3.
|
|
615
|
+
*
|
|
616
|
+
* [XChaCha20](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha)
|
|
617
|
+
* extended-nonce variant is also provided. Similar to XSalsa, it's safe to use with
|
|
618
|
+
* randomly-generated nonces.
|
|
619
|
+
*
|
|
620
|
+
* Check out [PDF](http://cr.yp.to/chacha/chacha-20080128.pdf) and
|
|
621
|
+
* [wiki](https://en.wikipedia.org/wiki/Salsa20) and
|
|
622
|
+
* [website](https://cr.yp.to/chacha.html).
|
|
623
|
+
*
|
|
624
|
+
* @module
|
|
625
|
+
*/
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* ChaCha core function. It is implemented twice:
|
|
631
|
+
* 1. Simple loop (chachaCore_small, hchacha_small)
|
|
632
|
+
* 2. Unrolled loop (chachaCore, hchacha) - 4x faster, but larger & harder to read
|
|
633
|
+
* The specific implementation is selected in `createCipher` below.
|
|
634
|
+
*/
|
|
635
|
+
/** quarter-round */
|
|
636
|
+
// prettier-ignore
|
|
637
|
+
function chachaQR(x, a, b, c, d) {
|
|
638
|
+
x[a] = (x[a] + x[b]) | 0;
|
|
639
|
+
x[d] = rotl(x[d] ^ x[a], 16);
|
|
640
|
+
x[c] = (x[c] + x[d]) | 0;
|
|
641
|
+
x[b] = rotl(x[b] ^ x[c], 12);
|
|
642
|
+
x[a] = (x[a] + x[b]) | 0;
|
|
643
|
+
x[d] = rotl(x[d] ^ x[a], 8);
|
|
644
|
+
x[c] = (x[c] + x[d]) | 0;
|
|
645
|
+
x[b] = rotl(x[b] ^ x[c], 7);
|
|
646
|
+
}
|
|
647
|
+
/** single round */
|
|
648
|
+
function chachaRound(x, rounds = 20) {
|
|
649
|
+
for (let r = 0; r < rounds; r += 2) {
|
|
650
|
+
chachaQR(x, 0, 4, 8, 12);
|
|
651
|
+
chachaQR(x, 1, 5, 9, 13);
|
|
652
|
+
chachaQR(x, 2, 6, 10, 14);
|
|
653
|
+
chachaQR(x, 3, 7, 11, 15);
|
|
654
|
+
chachaQR(x, 0, 5, 10, 15);
|
|
655
|
+
chachaQR(x, 1, 6, 11, 12);
|
|
656
|
+
chachaQR(x, 2, 7, 8, 13);
|
|
657
|
+
chachaQR(x, 3, 4, 9, 14);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
const ctmp = /* @__PURE__ */ new Uint32Array(16);
|
|
661
|
+
/** Small version of chacha without loop unrolling. Unused, provided for auditability. */
|
|
662
|
+
// prettier-ignore
|
|
663
|
+
function chacha(s, k, i, out, isHChacha = true, rounds = 20) {
|
|
664
|
+
// Create initial array using common pattern
|
|
665
|
+
const y = Uint32Array.from([
|
|
666
|
+
s[0], s[1], s[2], s[3], // "expa" "nd 3" "2-by" "te k"
|
|
667
|
+
k[0], k[1], k[2], k[3], // Key Key Key Key
|
|
668
|
+
k[4], k[5], k[6], k[7], // Key Key Key Key
|
|
669
|
+
i[0], i[1], i[2], i[3], // Counter Counter Nonce Nonce
|
|
670
|
+
]);
|
|
671
|
+
const x = ctmp;
|
|
672
|
+
x.set(y);
|
|
673
|
+
chachaRound(x, rounds);
|
|
674
|
+
// hchacha extracts 8 specific bytes, chacha adds orig to result
|
|
675
|
+
if (isHChacha) {
|
|
676
|
+
const xindexes = [0, 1, 2, 3, 12, 13, 14, 15];
|
|
677
|
+
for (let i = 0; i < 8; i++)
|
|
678
|
+
out[i] = x[xindexes[i]];
|
|
679
|
+
}
|
|
680
|
+
else {
|
|
681
|
+
for (let i = 0; i < 16; i++)
|
|
682
|
+
out[i] = (y[i] + x[i]) | 0;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
/** Identical to `chachaCore`. Unused. */
|
|
686
|
+
// @ts-ignore
|
|
687
|
+
const chachaCore_small = (s, k, n, out, cnt, rounds) => chacha(s, k, Uint32Array.from([n[0], n[1], cnt, 0]), out, false, rounds);
|
|
688
|
+
/** Identical to `hchacha`. Unused. */
|
|
689
|
+
// @ts-ignore
|
|
690
|
+
const hchacha_small = chacha;
|
|
691
|
+
/** Identical to `chachaCore_small`. Unused. */
|
|
692
|
+
// prettier-ignore
|
|
693
|
+
function chachaCore(s, k, n, out, cnt, rounds = 20) {
|
|
694
|
+
let y00 = s[0], y01 = s[1], y02 = s[2], y03 = s[3], // "expa" "nd 3" "2-by" "te k"
|
|
695
|
+
y04 = k[0], y05 = k[1], y06 = k[2], y07 = k[3], // Key Key Key Key
|
|
696
|
+
y08 = k[4], y09 = k[5], y10 = k[6], y11 = k[7], // Key Key Key Key
|
|
697
|
+
y12 = cnt, y13 = n[0], y14 = n[1], y15 = n[2]; // Counter Counter Nonce Nonce
|
|
698
|
+
// Save state to temporary variables
|
|
699
|
+
let x00 = y00, x01 = y01, x02 = y02, x03 = y03, x04 = y04, x05 = y05, x06 = y06, x07 = y07, x08 = y08, x09 = y09, x10 = y10, x11 = y11, x12 = y12, x13 = y13, x14 = y14, x15 = y15;
|
|
700
|
+
for (let r = 0; r < rounds; r += 2) {
|
|
701
|
+
x00 = (x00 + x04) | 0;
|
|
702
|
+
x12 = rotl(x12 ^ x00, 16);
|
|
703
|
+
x08 = (x08 + x12) | 0;
|
|
704
|
+
x04 = rotl(x04 ^ x08, 12);
|
|
705
|
+
x00 = (x00 + x04) | 0;
|
|
706
|
+
x12 = rotl(x12 ^ x00, 8);
|
|
707
|
+
x08 = (x08 + x12) | 0;
|
|
708
|
+
x04 = rotl(x04 ^ x08, 7);
|
|
709
|
+
x01 = (x01 + x05) | 0;
|
|
710
|
+
x13 = rotl(x13 ^ x01, 16);
|
|
711
|
+
x09 = (x09 + x13) | 0;
|
|
712
|
+
x05 = rotl(x05 ^ x09, 12);
|
|
713
|
+
x01 = (x01 + x05) | 0;
|
|
714
|
+
x13 = rotl(x13 ^ x01, 8);
|
|
715
|
+
x09 = (x09 + x13) | 0;
|
|
716
|
+
x05 = rotl(x05 ^ x09, 7);
|
|
717
|
+
x02 = (x02 + x06) | 0;
|
|
718
|
+
x14 = rotl(x14 ^ x02, 16);
|
|
719
|
+
x10 = (x10 + x14) | 0;
|
|
720
|
+
x06 = rotl(x06 ^ x10, 12);
|
|
721
|
+
x02 = (x02 + x06) | 0;
|
|
722
|
+
x14 = rotl(x14 ^ x02, 8);
|
|
723
|
+
x10 = (x10 + x14) | 0;
|
|
724
|
+
x06 = rotl(x06 ^ x10, 7);
|
|
725
|
+
x03 = (x03 + x07) | 0;
|
|
726
|
+
x15 = rotl(x15 ^ x03, 16);
|
|
727
|
+
x11 = (x11 + x15) | 0;
|
|
728
|
+
x07 = rotl(x07 ^ x11, 12);
|
|
729
|
+
x03 = (x03 + x07) | 0;
|
|
730
|
+
x15 = rotl(x15 ^ x03, 8);
|
|
731
|
+
x11 = (x11 + x15) | 0;
|
|
732
|
+
x07 = rotl(x07 ^ x11, 7);
|
|
733
|
+
x00 = (x00 + x05) | 0;
|
|
734
|
+
x15 = rotl(x15 ^ x00, 16);
|
|
735
|
+
x10 = (x10 + x15) | 0;
|
|
736
|
+
x05 = rotl(x05 ^ x10, 12);
|
|
737
|
+
x00 = (x00 + x05) | 0;
|
|
738
|
+
x15 = rotl(x15 ^ x00, 8);
|
|
739
|
+
x10 = (x10 + x15) | 0;
|
|
740
|
+
x05 = rotl(x05 ^ x10, 7);
|
|
741
|
+
x01 = (x01 + x06) | 0;
|
|
742
|
+
x12 = rotl(x12 ^ x01, 16);
|
|
743
|
+
x11 = (x11 + x12) | 0;
|
|
744
|
+
x06 = rotl(x06 ^ x11, 12);
|
|
745
|
+
x01 = (x01 + x06) | 0;
|
|
746
|
+
x12 = rotl(x12 ^ x01, 8);
|
|
747
|
+
x11 = (x11 + x12) | 0;
|
|
748
|
+
x06 = rotl(x06 ^ x11, 7);
|
|
749
|
+
x02 = (x02 + x07) | 0;
|
|
750
|
+
x13 = rotl(x13 ^ x02, 16);
|
|
751
|
+
x08 = (x08 + x13) | 0;
|
|
752
|
+
x07 = rotl(x07 ^ x08, 12);
|
|
753
|
+
x02 = (x02 + x07) | 0;
|
|
754
|
+
x13 = rotl(x13 ^ x02, 8);
|
|
755
|
+
x08 = (x08 + x13) | 0;
|
|
756
|
+
x07 = rotl(x07 ^ x08, 7);
|
|
757
|
+
x03 = (x03 + x04) | 0;
|
|
758
|
+
x14 = rotl(x14 ^ x03, 16);
|
|
759
|
+
x09 = (x09 + x14) | 0;
|
|
760
|
+
x04 = rotl(x04 ^ x09, 12);
|
|
761
|
+
x03 = (x03 + x04) | 0;
|
|
762
|
+
x14 = rotl(x14 ^ x03, 8);
|
|
763
|
+
x09 = (x09 + x14) | 0;
|
|
764
|
+
x04 = rotl(x04 ^ x09, 7);
|
|
765
|
+
}
|
|
766
|
+
// Write output
|
|
767
|
+
let oi = 0;
|
|
768
|
+
out[oi++] = (y00 + x00) | 0;
|
|
769
|
+
out[oi++] = (y01 + x01) | 0;
|
|
770
|
+
out[oi++] = (y02 + x02) | 0;
|
|
771
|
+
out[oi++] = (y03 + x03) | 0;
|
|
772
|
+
out[oi++] = (y04 + x04) | 0;
|
|
773
|
+
out[oi++] = (y05 + x05) | 0;
|
|
774
|
+
out[oi++] = (y06 + x06) | 0;
|
|
775
|
+
out[oi++] = (y07 + x07) | 0;
|
|
776
|
+
out[oi++] = (y08 + x08) | 0;
|
|
777
|
+
out[oi++] = (y09 + x09) | 0;
|
|
778
|
+
out[oi++] = (y10 + x10) | 0;
|
|
779
|
+
out[oi++] = (y11 + x11) | 0;
|
|
780
|
+
out[oi++] = (y12 + x12) | 0;
|
|
781
|
+
out[oi++] = (y13 + x13) | 0;
|
|
782
|
+
out[oi++] = (y14 + x14) | 0;
|
|
783
|
+
out[oi++] = (y15 + x15) | 0;
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
* hchacha hashes key and nonce into key' and nonce' for xchacha20.
|
|
787
|
+
* Identical to `hchacha_small`.
|
|
788
|
+
* Need to find a way to merge it with `chachaCore` without 25% performance hit.
|
|
789
|
+
*/
|
|
790
|
+
// prettier-ignore
|
|
791
|
+
function hchacha(s, k, i, out) {
|
|
792
|
+
let x00 = s[0], x01 = s[1], x02 = s[2], x03 = s[3], x04 = k[0], x05 = k[1], x06 = k[2], x07 = k[3], x08 = k[4], x09 = k[5], x10 = k[6], x11 = k[7], x12 = i[0], x13 = i[1], x14 = i[2], x15 = i[3];
|
|
793
|
+
for (let r = 0; r < 20; r += 2) {
|
|
794
|
+
x00 = (x00 + x04) | 0;
|
|
795
|
+
x12 = rotl(x12 ^ x00, 16);
|
|
796
|
+
x08 = (x08 + x12) | 0;
|
|
797
|
+
x04 = rotl(x04 ^ x08, 12);
|
|
798
|
+
x00 = (x00 + x04) | 0;
|
|
799
|
+
x12 = rotl(x12 ^ x00, 8);
|
|
800
|
+
x08 = (x08 + x12) | 0;
|
|
801
|
+
x04 = rotl(x04 ^ x08, 7);
|
|
802
|
+
x01 = (x01 + x05) | 0;
|
|
803
|
+
x13 = rotl(x13 ^ x01, 16);
|
|
804
|
+
x09 = (x09 + x13) | 0;
|
|
805
|
+
x05 = rotl(x05 ^ x09, 12);
|
|
806
|
+
x01 = (x01 + x05) | 0;
|
|
807
|
+
x13 = rotl(x13 ^ x01, 8);
|
|
808
|
+
x09 = (x09 + x13) | 0;
|
|
809
|
+
x05 = rotl(x05 ^ x09, 7);
|
|
810
|
+
x02 = (x02 + x06) | 0;
|
|
811
|
+
x14 = rotl(x14 ^ x02, 16);
|
|
812
|
+
x10 = (x10 + x14) | 0;
|
|
813
|
+
x06 = rotl(x06 ^ x10, 12);
|
|
814
|
+
x02 = (x02 + x06) | 0;
|
|
815
|
+
x14 = rotl(x14 ^ x02, 8);
|
|
816
|
+
x10 = (x10 + x14) | 0;
|
|
817
|
+
x06 = rotl(x06 ^ x10, 7);
|
|
818
|
+
x03 = (x03 + x07) | 0;
|
|
819
|
+
x15 = rotl(x15 ^ x03, 16);
|
|
820
|
+
x11 = (x11 + x15) | 0;
|
|
821
|
+
x07 = rotl(x07 ^ x11, 12);
|
|
822
|
+
x03 = (x03 + x07) | 0;
|
|
823
|
+
x15 = rotl(x15 ^ x03, 8);
|
|
824
|
+
x11 = (x11 + x15) | 0;
|
|
825
|
+
x07 = rotl(x07 ^ x11, 7);
|
|
826
|
+
x00 = (x00 + x05) | 0;
|
|
827
|
+
x15 = rotl(x15 ^ x00, 16);
|
|
828
|
+
x10 = (x10 + x15) | 0;
|
|
829
|
+
x05 = rotl(x05 ^ x10, 12);
|
|
830
|
+
x00 = (x00 + x05) | 0;
|
|
831
|
+
x15 = rotl(x15 ^ x00, 8);
|
|
832
|
+
x10 = (x10 + x15) | 0;
|
|
833
|
+
x05 = rotl(x05 ^ x10, 7);
|
|
834
|
+
x01 = (x01 + x06) | 0;
|
|
835
|
+
x12 = rotl(x12 ^ x01, 16);
|
|
836
|
+
x11 = (x11 + x12) | 0;
|
|
837
|
+
x06 = rotl(x06 ^ x11, 12);
|
|
838
|
+
x01 = (x01 + x06) | 0;
|
|
839
|
+
x12 = rotl(x12 ^ x01, 8);
|
|
840
|
+
x11 = (x11 + x12) | 0;
|
|
841
|
+
x06 = rotl(x06 ^ x11, 7);
|
|
842
|
+
x02 = (x02 + x07) | 0;
|
|
843
|
+
x13 = rotl(x13 ^ x02, 16);
|
|
844
|
+
x08 = (x08 + x13) | 0;
|
|
845
|
+
x07 = rotl(x07 ^ x08, 12);
|
|
846
|
+
x02 = (x02 + x07) | 0;
|
|
847
|
+
x13 = rotl(x13 ^ x02, 8);
|
|
848
|
+
x08 = (x08 + x13) | 0;
|
|
849
|
+
x07 = rotl(x07 ^ x08, 7);
|
|
850
|
+
x03 = (x03 + x04) | 0;
|
|
851
|
+
x14 = rotl(x14 ^ x03, 16);
|
|
852
|
+
x09 = (x09 + x14) | 0;
|
|
853
|
+
x04 = rotl(x04 ^ x09, 12);
|
|
854
|
+
x03 = (x03 + x04) | 0;
|
|
855
|
+
x14 = rotl(x14 ^ x03, 8);
|
|
856
|
+
x09 = (x09 + x14) | 0;
|
|
857
|
+
x04 = rotl(x04 ^ x09, 7);
|
|
858
|
+
}
|
|
859
|
+
let oi = 0;
|
|
860
|
+
out[oi++] = x00;
|
|
861
|
+
out[oi++] = x01;
|
|
862
|
+
out[oi++] = x02;
|
|
863
|
+
out[oi++] = x03;
|
|
864
|
+
out[oi++] = x12;
|
|
865
|
+
out[oi++] = x13;
|
|
866
|
+
out[oi++] = x14;
|
|
867
|
+
out[oi++] = x15;
|
|
868
|
+
}
|
|
869
|
+
/** Original, non-RFC chacha20 from DJB. 8-byte nonce, 8-byte counter. */
|
|
870
|
+
const chacha20orig = /* @__PURE__ */ createCipher(chachaCore, {
|
|
871
|
+
counterRight: false,
|
|
872
|
+
counterLength: 8,
|
|
873
|
+
allowShortKeys: true,
|
|
874
|
+
});
|
|
875
|
+
/**
|
|
876
|
+
* ChaCha stream cipher. Conforms to RFC 8439 (IETF, TLS). 12-byte nonce, 4-byte counter.
|
|
877
|
+
* With smaller nonce, it's not safe to make it random (CSPRNG), due to collision chance.
|
|
878
|
+
*/
|
|
879
|
+
const chacha20 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
880
|
+
counterRight: false,
|
|
881
|
+
counterLength: 4,
|
|
882
|
+
allowShortKeys: false,
|
|
883
|
+
});
|
|
884
|
+
/**
|
|
885
|
+
* XChaCha eXtended-nonce ChaCha. With 24-byte nonce, it's safe to make it random (CSPRNG).
|
|
886
|
+
* See [IRTF draft](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha).
|
|
887
|
+
*/
|
|
888
|
+
const xchacha20 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
889
|
+
counterRight: false,
|
|
890
|
+
counterLength: 8,
|
|
891
|
+
extendNonceFn: hchacha,
|
|
892
|
+
allowShortKeys: false,
|
|
893
|
+
});
|
|
894
|
+
/** Reduced 8-round chacha, described in original paper. */
|
|
895
|
+
const chacha8 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
896
|
+
counterRight: false,
|
|
897
|
+
counterLength: 4,
|
|
898
|
+
rounds: 8,
|
|
899
|
+
});
|
|
900
|
+
/** Reduced 12-round chacha, described in original paper. */
|
|
901
|
+
const chacha12 = /* @__PURE__ */ createCipher(chachaCore, {
|
|
902
|
+
counterRight: false,
|
|
903
|
+
counterLength: 4,
|
|
904
|
+
rounds: 12,
|
|
905
|
+
});
|
|
906
|
+
const ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
|
|
907
|
+
// Pad to digest size with zeros
|
|
908
|
+
const updatePadded = (h, msg) => {
|
|
909
|
+
h.update(msg);
|
|
910
|
+
const leftover = msg.length % 16;
|
|
911
|
+
if (leftover)
|
|
912
|
+
h.update(ZEROS16.subarray(leftover));
|
|
913
|
+
};
|
|
914
|
+
const ZEROS32 = /* @__PURE__ */ new Uint8Array(32);
|
|
915
|
+
function computeTag(fn, key, nonce, ciphertext, AAD) {
|
|
916
|
+
if (AAD !== undefined)
|
|
917
|
+
(0,utils.abytes)(AAD, undefined, 'AAD');
|
|
918
|
+
const authKey = fn(key, nonce, ZEROS32);
|
|
919
|
+
const lengths = (0,utils.u64Lengths)(ciphertext.length, AAD ? AAD.length : 0, true);
|
|
920
|
+
// Methods below can be replaced with
|
|
921
|
+
// return poly1305_computeTag_small(authKey, lengths, ciphertext, AAD)
|
|
922
|
+
const h = poly1305.create(authKey);
|
|
923
|
+
if (AAD)
|
|
924
|
+
updatePadded(h, AAD);
|
|
925
|
+
updatePadded(h, ciphertext);
|
|
926
|
+
h.update(lengths);
|
|
927
|
+
const res = h.digest();
|
|
928
|
+
(0,utils.clean)(authKey, lengths);
|
|
929
|
+
return res;
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* AEAD algorithm from RFC 8439.
|
|
933
|
+
* Salsa20 and chacha (RFC 8439) use poly1305 differently.
|
|
934
|
+
* We could have composed them, but it's hard because of authKey:
|
|
935
|
+
* In salsa20, authKey changes position in salsa stream.
|
|
936
|
+
* In chacha, authKey can't be computed inside computeTag, it modifies the counter.
|
|
937
|
+
*/
|
|
938
|
+
const _poly1305_aead = (xorStream) => (key, nonce, AAD) => {
|
|
939
|
+
const tagLength = 16;
|
|
940
|
+
return {
|
|
941
|
+
encrypt(plaintext, output) {
|
|
942
|
+
const plength = plaintext.length;
|
|
943
|
+
output = (0,utils.getOutput)(plength + tagLength, output, false);
|
|
944
|
+
output.set(plaintext);
|
|
945
|
+
const oPlain = output.subarray(0, -tagLength);
|
|
946
|
+
// Actual encryption
|
|
947
|
+
xorStream(key, nonce, oPlain, oPlain, 1);
|
|
948
|
+
const tag = computeTag(xorStream, key, nonce, oPlain, AAD);
|
|
949
|
+
output.set(tag, plength); // append tag
|
|
950
|
+
(0,utils.clean)(tag);
|
|
951
|
+
return output;
|
|
952
|
+
},
|
|
953
|
+
decrypt(ciphertext, output) {
|
|
954
|
+
output = (0,utils.getOutput)(ciphertext.length - tagLength, output, false);
|
|
955
|
+
const data = ciphertext.subarray(0, -tagLength);
|
|
956
|
+
const passedTag = ciphertext.subarray(-tagLength);
|
|
957
|
+
const tag = computeTag(xorStream, key, nonce, data, AAD);
|
|
958
|
+
if (!(0,utils.equalBytes)(passedTag, tag))
|
|
959
|
+
throw new Error('invalid tag');
|
|
960
|
+
output.set(ciphertext.subarray(0, -tagLength));
|
|
961
|
+
// Actual decryption
|
|
962
|
+
xorStream(key, nonce, output, output, 1); // start stream with i=1
|
|
963
|
+
(0,utils.clean)(tag);
|
|
964
|
+
return output;
|
|
965
|
+
},
|
|
966
|
+
};
|
|
967
|
+
};
|
|
968
|
+
/**
|
|
969
|
+
* ChaCha20-Poly1305 from RFC 8439.
|
|
970
|
+
*
|
|
971
|
+
* Unsafe to use random nonces under the same key, due to collision chance.
|
|
972
|
+
* Prefer XChaCha instead.
|
|
973
|
+
*/
|
|
974
|
+
const chacha20poly1305 = /* @__PURE__ */ (0,utils.wrapCipher)({ blockSize: 64, nonceLength: 12, tagLength: 16 }, _poly1305_aead(chacha20));
|
|
975
|
+
/**
|
|
976
|
+
* XChaCha20-Poly1305 extended-nonce chacha.
|
|
977
|
+
*
|
|
978
|
+
* Can be safely used with random nonces (CSPRNG).
|
|
979
|
+
* See [IRTF draft](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha).
|
|
980
|
+
*/
|
|
981
|
+
const xchacha20poly1305 = /* @__PURE__ */ (0,utils.wrapCipher)({ blockSize: 64, nonceLength: 24, tagLength: 16 }, _poly1305_aead(xchacha20));
|
|
982
|
+
/**
|
|
983
|
+
* Chacha20 CSPRNG (cryptographically secure pseudorandom number generator).
|
|
984
|
+
* It's best to limit usage to non-production, non-critical cases: for example, test-only.
|
|
985
|
+
* Compatible with libtomcrypt. It does not have a specification, so unclear how secure it is.
|
|
986
|
+
*/
|
|
987
|
+
const rngChacha20 = /* @__PURE__ */ createPRG(chacha20orig, 64, 32, 8);
|
|
988
|
+
/**
|
|
989
|
+
* Chacha20/8 CSPRNG (cryptographically secure pseudorandom number generator).
|
|
990
|
+
* It's best to limit usage to non-production, non-critical cases: for example, test-only.
|
|
991
|
+
* Faster than `rngChacha20`.
|
|
992
|
+
*/
|
|
993
|
+
const rngChacha8 = /* @__PURE__ */ createPRG(chacha8, 64, 32, 12);
|
|
994
|
+
//# sourceMappingURL=chacha.js.map
|
|
995
|
+
|
|
996
|
+
/***/ })
|
|
997
|
+
|
|
998
|
+
}]);
|
|
999
|
+
//# sourceMappingURL=defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js.map
|