@effected/github 0.32.0 → 0.33.0
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/CheckRun.js +24 -5
- package/RepositorySecret.js +3 -2
- package/index.d.ts +42 -2
- package/internal/blake2b.js +407 -0
- package/internal/crypto.js +41 -24
- package/internal/x25519.js +248 -0
- package/internal/xsalsa20poly1305.js +432 -0
- package/package.json +2 -4
package/CheckRun.js
CHANGED
|
@@ -3,7 +3,7 @@ import { GitHubError } from "./GitHubError.js";
|
|
|
3
3
|
import { numericId } from "./internal/ids.js";
|
|
4
4
|
import { unstubbed } from "./internal/unstubbed.js";
|
|
5
5
|
import { Repo } from "./Repo.js";
|
|
6
|
-
import { Cause, Context, DateTime, Effect, Exit, Layer, Option, Ref, Schema } from "effect";
|
|
6
|
+
import { Cause, Context, DateTime, Duration, Effect, Exit, Layer, Option, Ref, Schedule, Schema } from "effect";
|
|
7
7
|
|
|
8
8
|
// src/CheckRun.ts
|
|
9
9
|
/** How a check run finished. @public */
|
|
@@ -369,6 +369,23 @@ const refOf = (operation, raw) => decodeRef(!isRecord(raw) ? raw : {
|
|
|
369
369
|
annotationsCount: output.annotations_count
|
|
370
370
|
})) } : {}
|
|
371
371
|
}).pipe(Effect.catchTag("SchemaError", (error) => Effect.fail(GitHubError.decode(operation, "GitHub returned an unexpected check run", error))));
|
|
372
|
+
/**
|
|
373
|
+
* Retry a check-run PATCH that GitHub answered 404, three times over 1.75s.
|
|
374
|
+
*
|
|
375
|
+
* @remarks
|
|
376
|
+
* GitHub's checks API reads after writes from a replica, so a PATCH sent
|
|
377
|
+
* within about a second of the POST that created the run can 404 on a run
|
|
378
|
+
* that exists. The client's `RetryPolicy` never retries a 404, rightly
|
|
379
|
+
* for every other route, so this one is scoped to the two check-run PATCH
|
|
380
|
+
* routes. The waits (250ms, 500ms, 1s) run on `Clock`, so `TestClock` drives
|
|
381
|
+
* them, and a genuinely wrong id still fails with its original 404, 1.75s
|
|
382
|
+
* later.
|
|
383
|
+
*/
|
|
384
|
+
const retryStaleNotFound = (self) => Effect.retry(self, {
|
|
385
|
+
schedule: Schedule.exponential(Duration.millis(250)),
|
|
386
|
+
times: 3,
|
|
387
|
+
while: (error) => error.kind === "notFound"
|
|
388
|
+
});
|
|
372
389
|
/** The current time as GitHub's ISO 8601 timestamp, from `Clock` so `TestClock` drives it. */
|
|
373
390
|
const isoNow = Effect.map(DateTime.now, DateTime.formatIso);
|
|
374
391
|
const make = (client) => {
|
|
@@ -412,7 +429,7 @@ const make = (client) => {
|
|
|
412
429
|
id,
|
|
413
430
|
conclusion
|
|
414
431
|
});
|
|
415
|
-
|
|
432
|
+
const body = {
|
|
416
433
|
owner,
|
|
417
434
|
repo,
|
|
418
435
|
check_run_id: id,
|
|
@@ -421,7 +438,8 @@ const make = (client) => {
|
|
|
421
438
|
completed_at: yield* isoNow,
|
|
422
439
|
...output !== void 0 ? { output: wireOutput(output) } : {},
|
|
423
440
|
...options?.detailsUrl !== void 0 ? { details_url: options.detailsUrl } : {}
|
|
424
|
-
}
|
|
441
|
+
};
|
|
442
|
+
return yield* retryStaleNotFound(client.request("PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}", body));
|
|
425
443
|
});
|
|
426
444
|
const complete = Effect.fn("CheckRun.complete")(function* (id, conclusion, output, options) {
|
|
427
445
|
yield* completePatch(id, conclusion, output, options);
|
|
@@ -437,7 +455,7 @@ const make = (client) => {
|
|
|
437
455
|
repo,
|
|
438
456
|
id
|
|
439
457
|
});
|
|
440
|
-
|
|
458
|
+
const body = {
|
|
441
459
|
owner,
|
|
442
460
|
repo,
|
|
443
461
|
check_run_id: id,
|
|
@@ -445,7 +463,8 @@ const make = (client) => {
|
|
|
445
463
|
...options?.status !== void 0 ? { status: options.status } : {},
|
|
446
464
|
...options?.status === "in_progress" ? { started_at: yield* isoNow } : {},
|
|
447
465
|
...options?.detailsUrl !== void 0 ? { details_url: options.detailsUrl } : {}
|
|
448
|
-
}
|
|
466
|
+
};
|
|
467
|
+
return yield* retryStaleNotFound(client.request("PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}", body));
|
|
449
468
|
});
|
|
450
469
|
return {
|
|
451
470
|
create,
|
package/RepositorySecret.js
CHANGED
|
@@ -88,14 +88,15 @@ const dies = unstubbed("RepositorySecret.makeTest");
|
|
|
88
88
|
* Seal a value, turning a malformed public key into a typed failure.
|
|
89
89
|
*
|
|
90
90
|
* @remarks
|
|
91
|
-
* `encryptSecret` returns a `Result` because
|
|
91
|
+
* `encryptSecret` returns a `Result` because the key can fail to decode, be the
|
|
92
|
+
* wrong length, or be a low-order X25519 point. A public
|
|
92
93
|
* key GitHub cannot have produced is still *input*, and input failures are
|
|
93
94
|
* typed rather than thrown — so this maps it onto the same `GitHubError` a
|
|
94
95
|
* caller already handles, naming the route it came from.
|
|
95
96
|
*/
|
|
96
97
|
const seal = (route, publicKey, value) => Result.match(encryptSecret(publicKey, Redacted.value(value)), {
|
|
97
98
|
onSuccess: (sealed) => Effect.succeed(sealed),
|
|
98
|
-
onFailure: () => Effect.fail(GitHubError.decode(route, "the secrets public key was not valid base64"))
|
|
99
|
+
onFailure: () => Effect.fail(GitHubError.decode(route, "the secrets public key was not a valid base64 X25519 public key (32 bytes, not low-order)"))
|
|
99
100
|
});
|
|
100
101
|
const make = (client) => {
|
|
101
102
|
return {
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed GitHub REST and GraphQL for Effect: a route-keyed client, one error
|
|
3
|
+
* taxonomy, GitHub App auth, and one resource service per GitHub noun.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* `GitHubClient.request` types both the parameters and the returned `data` from
|
|
7
|
+
* the route literal. Every REST failure is a `GitHubError` whose `kind` you
|
|
8
|
+
* branch on. Resource services (`GitBranch`, `GitTag`, `CheckRun`,
|
|
9
|
+
* `PullRequest`, `GitHubRelease` and others) turn multi-call sequences into one
|
|
10
|
+
* call, and a configuration tier writes secrets, variables, rulesets,
|
|
11
|
+
* deployment environments and security settings. `GitHubApp` mints and revokes
|
|
12
|
+
* installation tokens, `AuthenticatedApp` reads the app itself and its webhook
|
|
13
|
+
* deliveries, and `GitHubUserAuth` runs the OAuth web flow an app uses to act
|
|
14
|
+
* as a user.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { GitHubClient } from "@effected/github";
|
|
19
|
+
* import { Effect } from "effect";
|
|
20
|
+
*
|
|
21
|
+
* const program = Effect.gen(function* () {
|
|
22
|
+
* const client = yield* GitHubClient;
|
|
23
|
+
* const repo = yield* client.request("GET /repos/{owner}/{repo}", { owner: "effect-ts", repo: "effect" });
|
|
24
|
+
* return repo.default_branch;
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // GITHUB_TOKEN is read through the ambient ConfigProvider.
|
|
28
|
+
* Effect.runPromise(program.pipe(Effect.provide(GitHubClient.layerFromConfig())));
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @packageDocumentation
|
|
32
|
+
*/
|
|
1
33
|
import { BareLineReference, CLOSING_KEYWORDS, ClosingKeyword, IssueReference, harvestIssueReferences, parseBareLineReference } from "@effected/github-references";
|
|
2
34
|
import { JwksResolver, JwtError } from "@effected/jwt";
|
|
3
35
|
import { Cause, Config, Context, Duration, Effect, Layer, Option, Redacted, Result, Schedule, Schema, Scope, Stream } from "effect";
|
|
@@ -2085,8 +2117,11 @@ interface CheckRunShape {
|
|
|
2085
2117
|
*
|
|
2086
2118
|
* `update(id)` with neither still sends **one** PATCH, carrying nothing but
|
|
2087
2119
|
* the run's coordinates. GitHub accepts it and changes nothing; it is not
|
|
2088
|
-
* skipped, so every call is
|
|
2089
|
-
*
|
|
2120
|
+
* skipped, so every call is one request and a caller's error handling sees
|
|
2121
|
+
* a missing run or a revoked token the same way either way.
|
|
2122
|
+
*
|
|
2123
|
+
* A 404 is retried three times over 1.75s, as for `complete`: GitHub can
|
|
2124
|
+
* answer 404 for a run created moments earlier.
|
|
2090
2125
|
*/
|
|
2091
2126
|
readonly update: (id: number, output?: CheckRunOutput, options?: UpdateCheckRunOptions) => Effect.Effect<void, GitHubError, Repo>;
|
|
2092
2127
|
/**
|
|
@@ -2133,6 +2168,11 @@ interface CheckRunShape {
|
|
|
2133
2168
|
* rendered output; a given output is cut to GitHub's byte limits first.
|
|
2134
2169
|
* `options.detailsUrl` points the finished run somewhere, such as the
|
|
2135
2170
|
* workflow run that produced it.
|
|
2171
|
+
*
|
|
2172
|
+
* A 404 is retried three times over 1.75s (on `Clock`) before it fails:
|
|
2173
|
+
* GitHub's checks API can answer 404 for a run created moments earlier.
|
|
2174
|
+
* Every other failure follows the client's retry policy. A retry resends
|
|
2175
|
+
* the first attempt's `completed_at`.
|
|
2136
2176
|
*/
|
|
2137
2177
|
readonly complete: (id: number, conclusion: (typeof CheckConclusion.literals)[number], output?: CheckRunOutput, options?: CompleteCheckRunOptions) => Effect.Effect<void, GitHubError, Repo>;
|
|
2138
2178
|
/**
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
// src/internal/blake2b.ts
|
|
2
|
+
/**
|
|
3
|
+
* BLAKE2b (RFC 7693) with a variable output length, vendored from
|
|
4
|
+
* `@noble/hashes`, trimmed to an unkeyed one-shot `blake2b(message, outLen)`.
|
|
5
|
+
*
|
|
6
|
+
* Upstream: https://github.com/paulmillr/noble-hashes
|
|
7
|
+
* Version: 2.4.0, commit 663c2aeeffc308ac0cded59bd32f7c212adacfc2
|
|
8
|
+
* Files: `src/blake2.ts` (`G1b`, `G2b`, the compression round loop, the
|
|
9
|
+
* `_BLAKE2b` initialisation and digest layout), `src/_blake.ts` (`BSIGMA`),
|
|
10
|
+
* and the 64-bit helpers they use from `src/_ts`. `@noble/hashes` 1.0.0 was
|
|
11
|
+
* audited by Cure53 in 2022.
|
|
12
|
+
*
|
|
13
|
+
* Changes from upstream: merged into one file; removed the `TArg`/`TRet` type
|
|
14
|
+
* helpers, `@example` blocks, BLAKE2s, keyed, salted and personalised modes, and
|
|
15
|
+
* the incremental `update`/`digest`/`clone` class with its `Uint32Array` views
|
|
16
|
+
* over caller buffers, which the one-shot function replaces; message words are
|
|
17
|
+
* read with explicit little-endian loads, which drops the host-endianness and
|
|
18
|
+
* alignment handling; the `BSIGMA` table keeps only the twelve rows BLAKE2b
|
|
19
|
+
* uses. `G1b`, `G2b`, the u64 helpers, the IV and the compression function are
|
|
20
|
+
* unchanged.
|
|
21
|
+
*
|
|
22
|
+
* The MIT License (MIT)
|
|
23
|
+
*
|
|
24
|
+
* Copyright (c) 2022 Paul Miller (https://paulmillr.com)
|
|
25
|
+
*
|
|
26
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
27
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
28
|
+
* in the Software without restriction, including without limitation the rights
|
|
29
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
30
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
31
|
+
* furnished to do so, subject to the following conditions:
|
|
32
|
+
*
|
|
33
|
+
* The above copyright notice and this permission notice shall be included in
|
|
34
|
+
* all copies or substantial portions of the Software.
|
|
35
|
+
*
|
|
36
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
37
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
38
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
39
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
40
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
41
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
42
|
+
* THE SOFTWARE.
|
|
43
|
+
*
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
const BLOCK_BYTES = 128;
|
|
47
|
+
const MAX_OUT_BYTES = 64;
|
|
48
|
+
const rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
49
|
+
const rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
50
|
+
const rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
51
|
+
const rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
52
|
+
const rotr32H = (_h, l) => l;
|
|
53
|
+
const rotr32L = (h, _l) => h;
|
|
54
|
+
function add(Ah, Al, Bh, Bl) {
|
|
55
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
56
|
+
return {
|
|
57
|
+
h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
|
|
58
|
+
l: l | 0
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
62
|
+
const add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
63
|
+
const fromNumH = (n) => n / 2 ** 32 | 0;
|
|
64
|
+
const fromNumL = (n) => n >>> 0;
|
|
65
|
+
const BSIGMA = /* @__PURE__ */ Uint8Array.from([
|
|
66
|
+
0,
|
|
67
|
+
1,
|
|
68
|
+
2,
|
|
69
|
+
3,
|
|
70
|
+
4,
|
|
71
|
+
5,
|
|
72
|
+
6,
|
|
73
|
+
7,
|
|
74
|
+
8,
|
|
75
|
+
9,
|
|
76
|
+
10,
|
|
77
|
+
11,
|
|
78
|
+
12,
|
|
79
|
+
13,
|
|
80
|
+
14,
|
|
81
|
+
15,
|
|
82
|
+
14,
|
|
83
|
+
10,
|
|
84
|
+
4,
|
|
85
|
+
8,
|
|
86
|
+
9,
|
|
87
|
+
15,
|
|
88
|
+
13,
|
|
89
|
+
6,
|
|
90
|
+
1,
|
|
91
|
+
12,
|
|
92
|
+
0,
|
|
93
|
+
2,
|
|
94
|
+
11,
|
|
95
|
+
7,
|
|
96
|
+
5,
|
|
97
|
+
3,
|
|
98
|
+
11,
|
|
99
|
+
8,
|
|
100
|
+
12,
|
|
101
|
+
0,
|
|
102
|
+
5,
|
|
103
|
+
2,
|
|
104
|
+
15,
|
|
105
|
+
13,
|
|
106
|
+
10,
|
|
107
|
+
14,
|
|
108
|
+
3,
|
|
109
|
+
6,
|
|
110
|
+
7,
|
|
111
|
+
1,
|
|
112
|
+
9,
|
|
113
|
+
4,
|
|
114
|
+
7,
|
|
115
|
+
9,
|
|
116
|
+
3,
|
|
117
|
+
1,
|
|
118
|
+
13,
|
|
119
|
+
12,
|
|
120
|
+
11,
|
|
121
|
+
14,
|
|
122
|
+
2,
|
|
123
|
+
6,
|
|
124
|
+
5,
|
|
125
|
+
10,
|
|
126
|
+
4,
|
|
127
|
+
0,
|
|
128
|
+
15,
|
|
129
|
+
8,
|
|
130
|
+
9,
|
|
131
|
+
0,
|
|
132
|
+
5,
|
|
133
|
+
7,
|
|
134
|
+
2,
|
|
135
|
+
4,
|
|
136
|
+
10,
|
|
137
|
+
15,
|
|
138
|
+
14,
|
|
139
|
+
1,
|
|
140
|
+
11,
|
|
141
|
+
12,
|
|
142
|
+
6,
|
|
143
|
+
8,
|
|
144
|
+
3,
|
|
145
|
+
13,
|
|
146
|
+
2,
|
|
147
|
+
12,
|
|
148
|
+
6,
|
|
149
|
+
10,
|
|
150
|
+
0,
|
|
151
|
+
11,
|
|
152
|
+
8,
|
|
153
|
+
3,
|
|
154
|
+
4,
|
|
155
|
+
13,
|
|
156
|
+
7,
|
|
157
|
+
5,
|
|
158
|
+
15,
|
|
159
|
+
14,
|
|
160
|
+
1,
|
|
161
|
+
9,
|
|
162
|
+
12,
|
|
163
|
+
5,
|
|
164
|
+
1,
|
|
165
|
+
15,
|
|
166
|
+
14,
|
|
167
|
+
13,
|
|
168
|
+
4,
|
|
169
|
+
10,
|
|
170
|
+
0,
|
|
171
|
+
7,
|
|
172
|
+
6,
|
|
173
|
+
3,
|
|
174
|
+
9,
|
|
175
|
+
2,
|
|
176
|
+
8,
|
|
177
|
+
11,
|
|
178
|
+
13,
|
|
179
|
+
11,
|
|
180
|
+
7,
|
|
181
|
+
14,
|
|
182
|
+
12,
|
|
183
|
+
1,
|
|
184
|
+
3,
|
|
185
|
+
9,
|
|
186
|
+
5,
|
|
187
|
+
0,
|
|
188
|
+
15,
|
|
189
|
+
4,
|
|
190
|
+
8,
|
|
191
|
+
6,
|
|
192
|
+
2,
|
|
193
|
+
10,
|
|
194
|
+
6,
|
|
195
|
+
15,
|
|
196
|
+
14,
|
|
197
|
+
9,
|
|
198
|
+
11,
|
|
199
|
+
3,
|
|
200
|
+
0,
|
|
201
|
+
8,
|
|
202
|
+
12,
|
|
203
|
+
2,
|
|
204
|
+
13,
|
|
205
|
+
7,
|
|
206
|
+
1,
|
|
207
|
+
4,
|
|
208
|
+
10,
|
|
209
|
+
5,
|
|
210
|
+
10,
|
|
211
|
+
2,
|
|
212
|
+
8,
|
|
213
|
+
4,
|
|
214
|
+
7,
|
|
215
|
+
6,
|
|
216
|
+
1,
|
|
217
|
+
5,
|
|
218
|
+
15,
|
|
219
|
+
11,
|
|
220
|
+
9,
|
|
221
|
+
14,
|
|
222
|
+
3,
|
|
223
|
+
12,
|
|
224
|
+
13,
|
|
225
|
+
0,
|
|
226
|
+
0,
|
|
227
|
+
1,
|
|
228
|
+
2,
|
|
229
|
+
3,
|
|
230
|
+
4,
|
|
231
|
+
5,
|
|
232
|
+
6,
|
|
233
|
+
7,
|
|
234
|
+
8,
|
|
235
|
+
9,
|
|
236
|
+
10,
|
|
237
|
+
11,
|
|
238
|
+
12,
|
|
239
|
+
13,
|
|
240
|
+
14,
|
|
241
|
+
15,
|
|
242
|
+
14,
|
|
243
|
+
10,
|
|
244
|
+
4,
|
|
245
|
+
8,
|
|
246
|
+
9,
|
|
247
|
+
15,
|
|
248
|
+
13,
|
|
249
|
+
6,
|
|
250
|
+
1,
|
|
251
|
+
12,
|
|
252
|
+
0,
|
|
253
|
+
2,
|
|
254
|
+
11,
|
|
255
|
+
7,
|
|
256
|
+
5,
|
|
257
|
+
3
|
|
258
|
+
]);
|
|
259
|
+
const B2B_IV = /* @__PURE__ */ Uint32Array.from([
|
|
260
|
+
4089235720,
|
|
261
|
+
1779033703,
|
|
262
|
+
2227873595,
|
|
263
|
+
3144134277,
|
|
264
|
+
4271175723,
|
|
265
|
+
1013904242,
|
|
266
|
+
1595750129,
|
|
267
|
+
2773480762,
|
|
268
|
+
2917565137,
|
|
269
|
+
1359893119,
|
|
270
|
+
725511199,
|
|
271
|
+
2600822924,
|
|
272
|
+
4215389547,
|
|
273
|
+
528734635,
|
|
274
|
+
327033209,
|
|
275
|
+
1541459225
|
|
276
|
+
]);
|
|
277
|
+
const BBUF = /* @__PURE__ */ new Uint32Array(32);
|
|
278
|
+
function G1b(a, b, c, d, msg, x) {
|
|
279
|
+
const Xl = msg[x], Xh = msg[x + 1];
|
|
280
|
+
let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1];
|
|
281
|
+
let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1];
|
|
282
|
+
let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1];
|
|
283
|
+
let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1];
|
|
284
|
+
const ll = add3L(Al, Bl, Xl);
|
|
285
|
+
Ah = add3H(ll, Ah, Bh, Xh);
|
|
286
|
+
Al = ll | 0;
|
|
287
|
+
let xh = Dh ^ Ah, xl = Dl ^ Al;
|
|
288
|
+
Dh = rotr32H(xh, xl);
|
|
289
|
+
Dl = rotr32L(xh, xl);
|
|
290
|
+
({h: Ch, l: Cl} = add(Ch, Cl, Dh, Dl));
|
|
291
|
+
xh = Bh ^ Ch;
|
|
292
|
+
xl = Bl ^ Cl;
|
|
293
|
+
Bh = rotrSH(xh, xl, 24);
|
|
294
|
+
Bl = rotrSL(xh, xl, 24);
|
|
295
|
+
BBUF[2 * a] = Al;
|
|
296
|
+
BBUF[2 * a + 1] = Ah;
|
|
297
|
+
BBUF[2 * b] = Bl;
|
|
298
|
+
BBUF[2 * b + 1] = Bh;
|
|
299
|
+
BBUF[2 * c] = Cl;
|
|
300
|
+
BBUF[2 * c + 1] = Ch;
|
|
301
|
+
BBUF[2 * d] = Dl;
|
|
302
|
+
BBUF[2 * d + 1] = Dh;
|
|
303
|
+
}
|
|
304
|
+
function G2b(a, b, c, d, msg, x) {
|
|
305
|
+
const Xl = msg[x], Xh = msg[x + 1];
|
|
306
|
+
let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1];
|
|
307
|
+
let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1];
|
|
308
|
+
let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1];
|
|
309
|
+
let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1];
|
|
310
|
+
const ll = add3L(Al, Bl, Xl);
|
|
311
|
+
Ah = add3H(ll, Ah, Bh, Xh);
|
|
312
|
+
Al = ll | 0;
|
|
313
|
+
let xh = Dh ^ Ah, xl = Dl ^ Al;
|
|
314
|
+
Dh = rotrSH(xh, xl, 16);
|
|
315
|
+
Dl = rotrSL(xh, xl, 16);
|
|
316
|
+
({h: Ch, l: Cl} = add(Ch, Cl, Dh, Dl));
|
|
317
|
+
xh = Bh ^ Ch;
|
|
318
|
+
xl = Bl ^ Cl;
|
|
319
|
+
Bh = rotrBH(xh, xl, 63);
|
|
320
|
+
Bl = rotrBL(xh, xl, 63);
|
|
321
|
+
BBUF[2 * a] = Al;
|
|
322
|
+
BBUF[2 * a + 1] = Ah;
|
|
323
|
+
BBUF[2 * b] = Bl;
|
|
324
|
+
BBUF[2 * b + 1] = Bh;
|
|
325
|
+
BBUF[2 * c] = Cl;
|
|
326
|
+
BBUF[2 * c + 1] = Ch;
|
|
327
|
+
BBUF[2 * d] = Dl;
|
|
328
|
+
BBUF[2 * d + 1] = Dh;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* BLAKE2b digest of `message`, `outLen` bytes long (1..64). Unkeyed.
|
|
332
|
+
*
|
|
333
|
+
* @param message - The bytes to hash.
|
|
334
|
+
* @param outLen - The digest length in bytes; libsodium's sealed-box nonce is 24.
|
|
335
|
+
*
|
|
336
|
+
* @internal
|
|
337
|
+
*/
|
|
338
|
+
function blake2b(message, outLen) {
|
|
339
|
+
if (!Number.isInteger(outLen) || outLen < 1 || outLen > MAX_OUT_BYTES) throw new RangeError(`blake2b outLen must be 1..${MAX_OUT_BYTES}, got ${outLen}`);
|
|
340
|
+
const state = Uint32Array.from(B2B_IV.subarray(0, 16));
|
|
341
|
+
state[0] ^= outLen | 16842752;
|
|
342
|
+
const block = new Uint8Array(BLOCK_BYTES);
|
|
343
|
+
const msg = /* @__PURE__ */ new Uint32Array(32);
|
|
344
|
+
const total = message.length;
|
|
345
|
+
let pos = 0;
|
|
346
|
+
let length = 0;
|
|
347
|
+
for (;;) {
|
|
348
|
+
const take = Math.min(BLOCK_BYTES, total - pos);
|
|
349
|
+
const isLast = total - pos <= BLOCK_BYTES;
|
|
350
|
+
block.fill(0);
|
|
351
|
+
block.set(message.subarray(pos, pos + take));
|
|
352
|
+
for (let i = 0; i < 32; i++) {
|
|
353
|
+
const o = i * 4;
|
|
354
|
+
msg[i] = block[o] | block[o + 1] << 8 | block[o + 2] << 16 | block[o + 3] << 24 | 0;
|
|
355
|
+
}
|
|
356
|
+
pos += take;
|
|
357
|
+
length += take;
|
|
358
|
+
compress(state, msg, length, isLast);
|
|
359
|
+
if (isLast) break;
|
|
360
|
+
}
|
|
361
|
+
const out = new Uint8Array(MAX_OUT_BYTES);
|
|
362
|
+
for (let i = 0; i < 16; i++) {
|
|
363
|
+
const w = state[i];
|
|
364
|
+
out[i * 4] = w;
|
|
365
|
+
out[i * 4 + 1] = w >>> 8;
|
|
366
|
+
out[i * 4 + 2] = w >>> 16;
|
|
367
|
+
out[i * 4 + 3] = w >>> 24;
|
|
368
|
+
}
|
|
369
|
+
return out.slice(0, outLen);
|
|
370
|
+
}
|
|
371
|
+
/** One BLAKE2b compression: folds `msg` (32 LE words) into `state` (16 LE words). */
|
|
372
|
+
function compress(state, msg, length, isLast) {
|
|
373
|
+
BBUF.set(state);
|
|
374
|
+
BBUF.set(B2B_IV, 16);
|
|
375
|
+
const l = fromNumL(length);
|
|
376
|
+
const h = fromNumH(length);
|
|
377
|
+
BBUF[24] = B2B_IV[8] ^ l;
|
|
378
|
+
BBUF[25] = B2B_IV[9] ^ h;
|
|
379
|
+
if (isLast) {
|
|
380
|
+
BBUF[28] = ~BBUF[28];
|
|
381
|
+
BBUF[29] = ~BBUF[29];
|
|
382
|
+
}
|
|
383
|
+
let j = 0;
|
|
384
|
+
const s = BSIGMA;
|
|
385
|
+
for (let i = 0; i < 12; i++) {
|
|
386
|
+
G1b(0, 4, 8, 12, msg, 2 * s[j++]);
|
|
387
|
+
G2b(0, 4, 8, 12, msg, 2 * s[j++]);
|
|
388
|
+
G1b(1, 5, 9, 13, msg, 2 * s[j++]);
|
|
389
|
+
G2b(1, 5, 9, 13, msg, 2 * s[j++]);
|
|
390
|
+
G1b(2, 6, 10, 14, msg, 2 * s[j++]);
|
|
391
|
+
G2b(2, 6, 10, 14, msg, 2 * s[j++]);
|
|
392
|
+
G1b(3, 7, 11, 15, msg, 2 * s[j++]);
|
|
393
|
+
G2b(3, 7, 11, 15, msg, 2 * s[j++]);
|
|
394
|
+
G1b(0, 5, 10, 15, msg, 2 * s[j++]);
|
|
395
|
+
G2b(0, 5, 10, 15, msg, 2 * s[j++]);
|
|
396
|
+
G1b(1, 6, 11, 12, msg, 2 * s[j++]);
|
|
397
|
+
G2b(1, 6, 11, 12, msg, 2 * s[j++]);
|
|
398
|
+
G1b(2, 7, 8, 13, msg, 2 * s[j++]);
|
|
399
|
+
G2b(2, 7, 8, 13, msg, 2 * s[j++]);
|
|
400
|
+
G1b(3, 4, 9, 14, msg, 2 * s[j++]);
|
|
401
|
+
G2b(3, 4, 9, 14, msg, 2 * s[j++]);
|
|
402
|
+
}
|
|
403
|
+
for (let i = 0; i < 16; i++) state[i] ^= BBUF[i] ^ BBUF[i + 16];
|
|
404
|
+
BBUF.fill(0);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export { blake2b };
|
package/internal/crypto.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
+
import { blake2b } from "./blake2b.js";
|
|
2
|
+
import { x25519, x25519PublicKey } from "./x25519.js";
|
|
3
|
+
import { hsalsaKey, secretbox } from "./xsalsa20poly1305.js";
|
|
1
4
|
import { Result } from "effect";
|
|
2
5
|
import * as Base64 from "effect/encoding/Base64";
|
|
3
|
-
import
|
|
4
|
-
import nacl from "tweetnacl";
|
|
6
|
+
import * as EncodingError from "effect/encoding/EncodingError";
|
|
5
7
|
|
|
6
8
|
// src/internal/crypto.ts
|
|
7
|
-
const { blake2b } = blakejs;
|
|
8
9
|
/** A sealed box opens with the recipient's key; the nonce is derived, not sent. */
|
|
9
10
|
const NONCE_BYTES = 24;
|
|
10
11
|
const PUBLIC_KEY_BYTES = 32;
|
|
11
12
|
const utf8 = new TextEncoder();
|
|
13
|
+
const invalidKey = (publicKey, message) => new EncodingError.EncodingError({
|
|
14
|
+
kind: "Decode",
|
|
15
|
+
module: "SealedBox",
|
|
16
|
+
input: publicKey,
|
|
17
|
+
message
|
|
18
|
+
});
|
|
12
19
|
/**
|
|
13
20
|
* Encrypt a secret with libsodium's sealed-box algorithm, the format GitHub's
|
|
14
21
|
* secrets API requires.
|
|
@@ -29,16 +36,17 @@ const utf8 = new TextEncoder();
|
|
|
29
36
|
* corrupt secret rather than as an error here.
|
|
30
37
|
*
|
|
31
38
|
* ## Nothing here is Node-specific
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
* garbage from the API is input, and
|
|
39
|
+
|
|
40
|
+
Base64 goes through core's `Base64`, text through `TextEncoder`, and the
|
|
41
|
+
ephemeral secret key through `globalThis.crypto.getRandomValues`, so this
|
|
42
|
+
module's graph imports **no builtin** and runs on workerd. The primitives
|
|
43
|
+
(X25519, HSalsa20, XSalsa20-Poly1305 and BLAKE2b) are in-house copies of audited
|
|
44
|
+
`@noble` code, each carrying its provenance header; they are one-shot functions
|
|
45
|
+
with no dependency of their own.
|
|
46
|
+
|
|
47
|
+
A malformed `publicKey` (not base64, not 32 bytes, or a low-order point) fails as
|
|
48
|
+
* a typed `Result` rather than throwing: garbage from the API is input, and
|
|
49
|
+
* input failures are typed.
|
|
42
50
|
*
|
|
43
51
|
* @param publicKey - The repository, environment or organization public key,
|
|
44
52
|
* base64, as returned by GitHub's `.../secrets/public-key` endpoints.
|
|
@@ -47,17 +55,26 @@ const utf8 = new TextEncoder();
|
|
|
47
55
|
*
|
|
48
56
|
* @internal
|
|
49
57
|
*/
|
|
50
|
-
const encryptSecret = (publicKey, secretValue) => Result.
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
const encryptSecret = (publicKey, secretValue) => Result.flatMap(Base64.decode(publicKey), (publicKeyBytes) => {
|
|
59
|
+
if (publicKeyBytes.length !== PUBLIC_KEY_BYTES) return Result.fail(invalidKey(publicKey, `a sealed-box public key is ${PUBLIC_KEY_BYTES} bytes, got ${publicKeyBytes.length}`));
|
|
60
|
+
const ephemeralSecretKey = globalThis.crypto.getRandomValues(new Uint8Array(PUBLIC_KEY_BYTES));
|
|
61
|
+
try {
|
|
62
|
+
const ephemeralPublicKey = x25519PublicKey(ephemeralSecretKey);
|
|
63
|
+
const nonceInput = /* @__PURE__ */ new Uint8Array(64);
|
|
64
|
+
nonceInput.set(ephemeralPublicKey);
|
|
65
|
+
nonceInput.set(publicKeyBytes, PUBLIC_KEY_BYTES);
|
|
66
|
+
const nonce = blake2b(nonceInput, NONCE_BYTES);
|
|
67
|
+
const sharedKey = hsalsaKey(x25519(ephemeralSecretKey, publicKeyBytes));
|
|
68
|
+
const ciphertext = secretbox(utf8.encode(secretValue), nonce, sharedKey);
|
|
69
|
+
const sealed = new Uint8Array(ephemeralPublicKey.length + ciphertext.length);
|
|
70
|
+
sealed.set(ephemeralPublicKey);
|
|
71
|
+
sealed.set(ciphertext, ephemeralPublicKey.length);
|
|
72
|
+
return Result.succeed(Base64.encode(sealed));
|
|
73
|
+
} catch {
|
|
74
|
+
return Result.fail(invalidKey(publicKey, "the public key is a low-order curve point"));
|
|
75
|
+
} finally {
|
|
76
|
+
ephemeralSecretKey.fill(0);
|
|
77
|
+
}
|
|
61
78
|
});
|
|
62
79
|
|
|
63
80
|
export { encryptSecret };
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// src/internal/x25519.ts
|
|
2
|
+
/**
|
|
3
|
+
* X25519 (RFC 7748) scalar multiplication, vendored from `@noble/curves`,
|
|
4
|
+
* trimmed to the Montgomery ladder over curve25519 and the field helpers it
|
|
5
|
+
* needs.
|
|
6
|
+
*
|
|
7
|
+
* Upstream: https://github.com/paulmillr/noble-curves
|
|
8
|
+
* Version: 2.4.0, commit 656c4364dffa44c64aa0c49914b8000b278b67a9
|
|
9
|
+
* Files: `src/abstract/montgomery.ts` (`cmask`, `cswap`, `montgomeryLadder`,
|
|
10
|
+
* `decodeU`, `decodeScalar`, the low-order input check, `scalarMult`),
|
|
11
|
+
* `src/ed25519.ts` (`ed25519_pow_2_252_3`, `adjustScalarBytes`, the x25519
|
|
12
|
+
* `powPminus2`), `src/abstract/modular.ts` (`mod`, `pow2`), and the byte/number
|
|
13
|
+
* conversions from `src/utils.ts`. `@noble/curves` 1.0.0 was audited by Cure53
|
|
14
|
+
* in 2023.
|
|
15
|
+
*
|
|
16
|
+
* Changes from upstream: merged into one file; removed the `TArg`/`TRet` type
|
|
17
|
+
* helpers, `@example` blocks, X448, the generic `montgomery()` factory and its
|
|
18
|
+
* option validation, `keygen`/`randomSecretKey` (the caller draws secret keys
|
|
19
|
+
* from `globalThis.crypto.getRandomValues`), the Edwards-table fixed-base
|
|
20
|
+
* shortcut (the base point goes through the same ladder as any other point, which
|
|
21
|
+
* upstream does when no shortcut is supplied), and the `__TEST` hook. Byte and
|
|
22
|
+
* number conversions are inlined for fixed 32-byte little-endian values. The
|
|
23
|
+
* ladder, `cswap`/`cmask`, the `x^(p-2)` addition chain, scalar clamping and
|
|
24
|
+
* the low-order rejection are unchanged.
|
|
25
|
+
*
|
|
26
|
+
* The MIT License (MIT)
|
|
27
|
+
*
|
|
28
|
+
* Copyright (c) 2022 Paul Miller (https://paulmillr.com)
|
|
29
|
+
*
|
|
30
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
31
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
32
|
+
* in the Software without restriction, including without limitation the rights
|
|
33
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
34
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
35
|
+
* furnished to do so, subject to the following conditions:
|
|
36
|
+
*
|
|
37
|
+
* The above copyright notice and this permission notice shall be included in
|
|
38
|
+
* all copies or substantial portions of the Software.
|
|
39
|
+
*
|
|
40
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
41
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
42
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
43
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
44
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
45
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
46
|
+
* THE SOFTWARE.
|
|
47
|
+
*
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
const _0n = /* @__PURE__ */ BigInt(0);
|
|
51
|
+
const _1n = /* @__PURE__ */ BigInt(1);
|
|
52
|
+
const _2n = /* @__PURE__ */ BigInt(2);
|
|
53
|
+
const _3n = /* @__PURE__ */ BigInt(3);
|
|
54
|
+
const _5n = /* @__PURE__ */ BigInt(5);
|
|
55
|
+
const FIELD_BYTES = 32;
|
|
56
|
+
const P = /* @__PURE__ */ BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed");
|
|
57
|
+
const Gu = /* @__PURE__ */ BigInt(9);
|
|
58
|
+
const a24 = /* @__PURE__ */ BigInt(121665);
|
|
59
|
+
const minScalar = _2n ** BigInt(254);
|
|
60
|
+
const maxScalar = minScalar + BigInt(8) * (_2n ** BigInt(251) - _1n) + _1n;
|
|
61
|
+
const mod = (a, b) => {
|
|
62
|
+
const result = a % b;
|
|
63
|
+
return result >= _0n ? result : b + result;
|
|
64
|
+
};
|
|
65
|
+
function pow2(x, power, modulo) {
|
|
66
|
+
let res = x;
|
|
67
|
+
while (power-- > _0n) {
|
|
68
|
+
res *= res;
|
|
69
|
+
res %= modulo;
|
|
70
|
+
}
|
|
71
|
+
return res;
|
|
72
|
+
}
|
|
73
|
+
const modP = (n) => mod(n, P);
|
|
74
|
+
const bytesToNumberLE = (bytes) => {
|
|
75
|
+
let n = _0n;
|
|
76
|
+
for (let i = bytes.length - 1; i >= 0; i--) n = n << BigInt(8) | BigInt(bytes[i]);
|
|
77
|
+
return n;
|
|
78
|
+
};
|
|
79
|
+
const numberToBytesLE = (n, len) => {
|
|
80
|
+
const out = new Uint8Array(len);
|
|
81
|
+
let rest = n;
|
|
82
|
+
for (let i = 0; i < len; i++) {
|
|
83
|
+
out[i] = Number(rest & BigInt(255));
|
|
84
|
+
rest >>= BigInt(8);
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
};
|
|
88
|
+
function ed25519_pow_2_252_3(x) {
|
|
89
|
+
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
90
|
+
const b2 = x * x % P * x % P;
|
|
91
|
+
const b5 = pow2(pow2(b2, _2n, P) * b2 % P, _1n, P) * x % P;
|
|
92
|
+
const b10 = pow2(b5, _5n, P) * b5 % P;
|
|
93
|
+
const b20 = pow2(b10, _10n, P) * b10 % P;
|
|
94
|
+
const b40 = pow2(b20, _20n, P) * b20 % P;
|
|
95
|
+
const b80 = pow2(b40, _40n, P) * b40 % P;
|
|
96
|
+
return {
|
|
97
|
+
pow_p_5_8: pow2(pow2(pow2(pow2(b80, _80n, P) * b80 % P, _80n, P) * b80 % P, _10n, P) * b10 % P, _2n, P) * x % P,
|
|
98
|
+
b2
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function adjustScalarBytes(bytes) {
|
|
102
|
+
bytes[0] &= 248;
|
|
103
|
+
bytes[31] &= 127;
|
|
104
|
+
bytes[31] |= 64;
|
|
105
|
+
return bytes;
|
|
106
|
+
}
|
|
107
|
+
const powPminus2 = (x) => {
|
|
108
|
+
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
|
|
109
|
+
return mod(pow2(pow_p_5_8, _3n, P) * b2, P);
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Selector for cswap(): `P` to keep, `P + 1` to swap, chosen by the low bit of `swap`.
|
|
113
|
+
* Higher bits are ignored, and `swap` is passed in whole rather than as a {0n, 1n} bit on
|
|
114
|
+
* purpose: `P + (swap & _1n)` would short-circuit the addition whenever the bit is clear, which
|
|
115
|
+
* is the very leak this construction avoids, one round-trip further down. Subtracting `swap`
|
|
116
|
+
* with its low bit cleared keeps every operand full-width instead.
|
|
117
|
+
* @param P - Field modulus.
|
|
118
|
+
* @param swap - Value whose low bit selects; ignored above that bit.
|
|
119
|
+
* @returns `P` when the low bit is clear, `P + 1` when it is set.
|
|
120
|
+
*/
|
|
121
|
+
function cmask(P, swap) {
|
|
122
|
+
return P + swap - (swap >> _1n << _1n);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Swap two field elements when `mask` is `P + 1`, keep them when it is `P`:
|
|
126
|
+
*
|
|
127
|
+
* d = 6P + x_3 - x_2
|
|
128
|
+
* x_2' = d * mask + x_2 (mod P) x_3' = (x_2 + x_3) - x_2'
|
|
129
|
+
*
|
|
130
|
+
* The extra `6P * mask` vanishes modulo P, so `mask === P` leaves x_2 and `mask === P + 1`
|
|
131
|
+
* leaves x_3. Without the offset, the reduction dividend changes sign with input order and crosses
|
|
132
|
+
* BigInt limb boundaries; those classes measured differently on the tested Node/V8 build. For
|
|
133
|
+
* canonical inputs, the deliberately left-associative `offset + x_3 - x_2` is between 5P and 7P,
|
|
134
|
+
* keeping the dividend positive and in one word-count band for both RFC fields and masks. Six is
|
|
135
|
+
* the smallest coefficient `c` for which the shared offset `cP` has that property.
|
|
136
|
+
*
|
|
137
|
+
* This reduced the tested sign/size timing ratios, but JavaScript BigInt has no constant-time
|
|
138
|
+
* contract and the contents of the multiply and remainder still vary. Valid ladder states can
|
|
139
|
+
* contain genuine zero coordinates; this construction does not mask those value-shape effects.
|
|
140
|
+
* Computing `x_3'` independently as `((6P + x_2 - x_3) * mask + x_3) % P` is more symmetric.
|
|
141
|
+
* On the tested Node/V8 build, it reduced the timing difference between keeping `(0, v)` and
|
|
142
|
+
* swapping `(v, 0)`—both return `(0, v)`—from about 10%/13% for X25519/X448 to about 3%.
|
|
143
|
+
* Successful calls cannot reach that zero-in-the-first-output case. For the case they can reach,
|
|
144
|
+
* swapping `(0, v)` and keeping `(v, 0)` both return `(v, 0)`; the difference instead grew from
|
|
145
|
+
* about 0.7%/1.1% to 2.7%/2.8%. The extra multiply/remainder also made public
|
|
146
|
+
* `getSharedSecret()` about 16% slower. The retained one-remainder form measured about 2.5%
|
|
147
|
+
* slower than the prior helper for public X25519 `getSharedSecret()` in the same environment.
|
|
148
|
+
* x_3' falls out of the sum, which a swap leaves invariant: no second multiply or reduction is
|
|
149
|
+
* needed. Bind `6P` once per field so production and the timing regression exercise the same
|
|
150
|
+
* configured helper without paying for the multiplication in every ladder round.
|
|
151
|
+
*
|
|
152
|
+
* The returned function is called twice per ladder round, so it validates nothing. Both elements
|
|
153
|
+
* MUST already be reduced mod P; unreduced input silently corrupts the kept-side output.
|
|
154
|
+
* @param P - Field modulus.
|
|
155
|
+
* @returns A field-bound swap function taking mask, x_2, and x_3.
|
|
156
|
+
*/
|
|
157
|
+
function cswap(P) {
|
|
158
|
+
const offset = BigInt(6) * P;
|
|
159
|
+
return (mask, x_2, x_3) => {
|
|
160
|
+
const sum = x_2 + x_3;
|
|
161
|
+
const a = ((offset + x_3 - x_2) * mask + x_2) % P;
|
|
162
|
+
return {
|
|
163
|
+
x_2: a,
|
|
164
|
+
x_3: sum - a
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/** Internal helpers, exported for tests only. Not part of the public API. */
|
|
169
|
+
const lowOrderU = /* @__PURE__ */ new Set([
|
|
170
|
+
_0n,
|
|
171
|
+
_1n,
|
|
172
|
+
P - _1n,
|
|
173
|
+
BigInt("325606250916557431795983626356110631294008115727848805560023387167927233504"),
|
|
174
|
+
BigInt("39382357235489614581723060781553021112529911719440698176882885853963445705823")
|
|
175
|
+
]);
|
|
176
|
+
const swap = cswap(P);
|
|
177
|
+
function decodeU(u) {
|
|
178
|
+
const _u = Uint8Array.from(u);
|
|
179
|
+
_u[31] &= 127;
|
|
180
|
+
return modP(bytesToNumberLE(_u));
|
|
181
|
+
}
|
|
182
|
+
function decodeScalar(scalar) {
|
|
183
|
+
return bytesToNumberLE(adjustScalarBytes(Uint8Array.from(scalar)));
|
|
184
|
+
}
|
|
185
|
+
function inRange(title, n, min, max) {
|
|
186
|
+
if (!(min <= n && n < max)) throw new RangeError("expected valid " + title);
|
|
187
|
+
}
|
|
188
|
+
function montgomeryLadder(u, scalar) {
|
|
189
|
+
inRange("u", u, _0n, P);
|
|
190
|
+
inRange("scalar", scalar, minScalar, maxScalar);
|
|
191
|
+
const k = scalar;
|
|
192
|
+
const x_1 = u;
|
|
193
|
+
let x_2 = _1n;
|
|
194
|
+
let z_2 = _0n;
|
|
195
|
+
let x_3 = u;
|
|
196
|
+
let z_3 = _1n;
|
|
197
|
+
const kx = k ^ k >> _1n;
|
|
198
|
+
for (let t = BigInt(254); t >= _0n; t--) {
|
|
199
|
+
const mask = cmask(P, kx >> t);
|
|
200
|
+
({x_2, x_3} = swap(mask, x_2, x_3));
|
|
201
|
+
({x_2: z_2, x_3: z_3} = swap(mask, z_2, z_3));
|
|
202
|
+
const A = x_2 + z_2;
|
|
203
|
+
const AA = modP(A * A);
|
|
204
|
+
const B = x_2 - z_2;
|
|
205
|
+
const BB = modP(B * B);
|
|
206
|
+
const E = AA - BB;
|
|
207
|
+
const C = x_3 + z_3;
|
|
208
|
+
const D = x_3 - z_3;
|
|
209
|
+
const DA = modP(D * A);
|
|
210
|
+
const CB = modP(C * B);
|
|
211
|
+
const dacb = DA + CB;
|
|
212
|
+
const da_cb = DA - CB;
|
|
213
|
+
x_3 = modP(dacb * dacb);
|
|
214
|
+
z_3 = modP(x_1 * modP(da_cb * da_cb));
|
|
215
|
+
x_2 = modP(AA * BB);
|
|
216
|
+
z_2 = modP(E * (AA + modP(a24 * E)));
|
|
217
|
+
}
|
|
218
|
+
const mask = cmask(P, k);
|
|
219
|
+
({x_2, x_3} = swap(mask, x_2, x_3));
|
|
220
|
+
({x_2: z_2, x_3: z_3} = swap(mask, z_2, z_3));
|
|
221
|
+
const z2 = powPminus2(z_2);
|
|
222
|
+
return modP(x_2 * z2);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* X25519 scalar multiplication: the shared secret of `secretKey` and the peer's
|
|
226
|
+
* `u` coordinate, or a public key when `u` is the base point.
|
|
227
|
+
*
|
|
228
|
+
* @remarks
|
|
229
|
+
* Throws on a low-order `u`, which no honest peer produces: the check runs before
|
|
230
|
+
* the ladder so an attacker-chosen point never touches the secret scalar.
|
|
231
|
+
*
|
|
232
|
+
* @internal
|
|
233
|
+
*/
|
|
234
|
+
function x25519(secretKey, u) {
|
|
235
|
+
if (secretKey.length !== FIELD_BYTES) throw new RangeError(`x25519 scalar must be ${FIELD_BYTES} bytes`);
|
|
236
|
+
if (u.length !== FIELD_BYTES) throw new RangeError(`x25519 u coordinate must be ${FIELD_BYTES} bytes`);
|
|
237
|
+
const pointU = decodeU(u);
|
|
238
|
+
if (lowOrderU.has(pointU)) throw new Error("invalid private or public key received");
|
|
239
|
+
const pu = montgomeryLadder(pointU, decodeScalar(secretKey));
|
|
240
|
+
if (pu === _0n) throw new Error("invalid private or public key received");
|
|
241
|
+
return numberToBytesLE(modP(pu), FIELD_BYTES);
|
|
242
|
+
}
|
|
243
|
+
/** The base point's u-coordinate, encoded: `9` followed by 31 zero bytes. */
|
|
244
|
+
const BASE_POINT = /* @__PURE__ */ numberToBytesLE(Gu, FIELD_BYTES);
|
|
245
|
+
/** The X25519 public key for `secretKey`. */
|
|
246
|
+
const x25519PublicKey = (secretKey) => x25519(secretKey, BASE_POINT);
|
|
247
|
+
|
|
248
|
+
export { x25519, x25519PublicKey };
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
// src/internal/xsalsa20poly1305.ts
|
|
2
|
+
/**
|
|
3
|
+
* XSalsa20-Poly1305 (the NaCl / libsodium `secretbox`) and HSalsa20, vendored
|
|
4
|
+
* from `@noble/ciphers`, trimmed to the one-shot `crypto_box` building blocks a
|
|
5
|
+
* sealed box needs.
|
|
6
|
+
*
|
|
7
|
+
* Upstream: https://github.com/paulmillr/noble-ciphers
|
|
8
|
+
* Version: 2.4.0, commit d9e8a6a599e7ed729d9be03854c46a3c73bd9a79
|
|
9
|
+
* Files: `src/salsa.ts` (`salsaCore`, `hsalsa`, the `xsalsa20poly1305`
|
|
10
|
+
* construction), `src/_arx.ts` (`runCipher` and the sigma constant, the stream
|
|
11
|
+
* driver `createCipher` builds), `src/_poly1305.ts` (`Poly1305`). `@noble/ciphers`
|
|
12
|
+
* 1.0.0 was audited by Cure53 in 2024.
|
|
13
|
+
*
|
|
14
|
+
* Changes from upstream: merged into one file; removed the `TArg`/`TRet` type
|
|
15
|
+
* helpers, `@example` blocks, the plain `salsa20` and `secretbox` exports, the
|
|
16
|
+
* `createCipher` option machinery (short keys, counter layout options, PRGs) and
|
|
17
|
+
* the `wrapCipher` argument wrapper; the stream driver is specialised to
|
|
18
|
+
* XSalsa20 and works on explicit little-endian word loads and stores instead of
|
|
19
|
+
* `Uint32Array` views over the caller's buffers, which drops the host
|
|
20
|
+
* endianness, alignment and buffer-overlap handling; `Poly1305` is trimmed to a
|
|
21
|
+
* one-shot `poly1305(message, key)` and decrypt is not carried, since a sealed
|
|
22
|
+
* box is only ever written here. `salsaCore`, `hsalsa`, and the `Poly1305`
|
|
23
|
+
* `process` and `finalize` arithmetic are unchanged.
|
|
24
|
+
*
|
|
25
|
+
* The MIT License (MIT)
|
|
26
|
+
*
|
|
27
|
+
* Copyright (c) 2022 Paul Miller (https://paulmillr.com)
|
|
28
|
+
*
|
|
29
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
30
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
31
|
+
* in the Software without restriction, including without limitation the rights
|
|
32
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
33
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
34
|
+
* furnished to do so, subject to the following conditions:
|
|
35
|
+
*
|
|
36
|
+
* The above copyright notice and this permission notice shall be included in
|
|
37
|
+
* all copies or substantial portions of the Software.
|
|
38
|
+
*
|
|
39
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
40
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
41
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
42
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
43
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
44
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
45
|
+
* THE SOFTWARE.
|
|
46
|
+
*
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
/** XSalsa20 needs a 32-byte key and a 24-byte nonce; Poly1305 tags are 16 bytes. */
|
|
50
|
+
const KEY_BYTES = 32;
|
|
51
|
+
const NONCE_BYTES = 24;
|
|
52
|
+
const TAG_BYTES = 16;
|
|
53
|
+
const rotl = (a, b) => a << b | a >>> 32 - b;
|
|
54
|
+
/** Little-endian 32-bit load, independent of host endianness and alignment. */
|
|
55
|
+
const load32 = (bytes, offset) => bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] << 24 | 0;
|
|
56
|
+
/** The words of `bytes`, read little-endian. */
|
|
57
|
+
const words = (bytes) => {
|
|
58
|
+
const out = new Uint32Array(bytes.length / 4);
|
|
59
|
+
for (let i = 0; i < out.length; i++) out[i] = load32(bytes, i * 4);
|
|
60
|
+
return out;
|
|
61
|
+
};
|
|
62
|
+
const sigma32_32 = /* @__PURE__ */ (() => words(Uint8Array.from("expand 32-byte k", (c) => c.charCodeAt(0))))();
|
|
63
|
+
/** Uses only the low 32 bits of Salsa20's 64-bit counter state. */
|
|
64
|
+
function salsaCore(s, k, n, out, cnt, rounds = 20, cntHi = 0) {
|
|
65
|
+
const y00 = s[0], y01 = k[0], y02 = k[1], y03 = k[2], y04 = k[3], y05 = s[1], y06 = n[0], y07 = n[1], y08 = cnt, y09 = cntHi, y10 = s[2], y11 = k[4], y12 = k[5], y13 = k[6], y14 = k[7], y15 = s[3];
|
|
66
|
+
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;
|
|
67
|
+
for (let r = 0; r < rounds; r += 2) {
|
|
68
|
+
x04 ^= rotl(x00 + x12 | 0, 7);
|
|
69
|
+
x08 ^= rotl(x04 + x00 | 0, 9);
|
|
70
|
+
x12 ^= rotl(x08 + x04 | 0, 13);
|
|
71
|
+
x00 ^= rotl(x12 + x08 | 0, 18);
|
|
72
|
+
x09 ^= rotl(x05 + x01 | 0, 7);
|
|
73
|
+
x13 ^= rotl(x09 + x05 | 0, 9);
|
|
74
|
+
x01 ^= rotl(x13 + x09 | 0, 13);
|
|
75
|
+
x05 ^= rotl(x01 + x13 | 0, 18);
|
|
76
|
+
x14 ^= rotl(x10 + x06 | 0, 7);
|
|
77
|
+
x02 ^= rotl(x14 + x10 | 0, 9);
|
|
78
|
+
x06 ^= rotl(x02 + x14 | 0, 13);
|
|
79
|
+
x10 ^= rotl(x06 + x02 | 0, 18);
|
|
80
|
+
x03 ^= rotl(x15 + x11 | 0, 7);
|
|
81
|
+
x07 ^= rotl(x03 + x15 | 0, 9);
|
|
82
|
+
x11 ^= rotl(x07 + x03 | 0, 13);
|
|
83
|
+
x15 ^= rotl(x11 + x07 | 0, 18);
|
|
84
|
+
x01 ^= rotl(x00 + x03 | 0, 7);
|
|
85
|
+
x02 ^= rotl(x01 + x00 | 0, 9);
|
|
86
|
+
x03 ^= rotl(x02 + x01 | 0, 13);
|
|
87
|
+
x00 ^= rotl(x03 + x02 | 0, 18);
|
|
88
|
+
x06 ^= rotl(x05 + x04 | 0, 7);
|
|
89
|
+
x07 ^= rotl(x06 + x05 | 0, 9);
|
|
90
|
+
x04 ^= rotl(x07 + x06 | 0, 13);
|
|
91
|
+
x05 ^= rotl(x04 + x07 | 0, 18);
|
|
92
|
+
x11 ^= rotl(x10 + x09 | 0, 7);
|
|
93
|
+
x08 ^= rotl(x11 + x10 | 0, 9);
|
|
94
|
+
x09 ^= rotl(x08 + x11 | 0, 13);
|
|
95
|
+
x10 ^= rotl(x09 + x08 | 0, 18);
|
|
96
|
+
x12 ^= rotl(x15 + x14 | 0, 7);
|
|
97
|
+
x13 ^= rotl(x12 + x15 | 0, 9);
|
|
98
|
+
x14 ^= rotl(x13 + x12 | 0, 13);
|
|
99
|
+
x15 ^= rotl(x14 + x13 | 0, 18);
|
|
100
|
+
}
|
|
101
|
+
let oi = 0;
|
|
102
|
+
out[oi++] = y00 + x00 | 0;
|
|
103
|
+
out[oi++] = y01 + x01 | 0;
|
|
104
|
+
out[oi++] = y02 + x02 | 0;
|
|
105
|
+
out[oi++] = y03 + x03 | 0;
|
|
106
|
+
out[oi++] = y04 + x04 | 0;
|
|
107
|
+
out[oi++] = y05 + x05 | 0;
|
|
108
|
+
out[oi++] = y06 + x06 | 0;
|
|
109
|
+
out[oi++] = y07 + x07 | 0;
|
|
110
|
+
out[oi++] = y08 + x08 | 0;
|
|
111
|
+
out[oi++] = y09 + x09 | 0;
|
|
112
|
+
out[oi++] = y10 + x10 | 0;
|
|
113
|
+
out[oi++] = y11 + x11 | 0;
|
|
114
|
+
out[oi++] = y12 + x12 | 0;
|
|
115
|
+
out[oi++] = y13 + x13 | 0;
|
|
116
|
+
out[oi++] = y14 + x14 | 0;
|
|
117
|
+
out[oi++] = y15 + x15 | 0;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* HSalsa20: hashes key and nonce-prefix words into the 32-byte subkey used by
|
|
121
|
+
* XSalsa20 and by `crypto_box`'s shared-key derivation.
|
|
122
|
+
*/
|
|
123
|
+
function hsalsa(s, k, i, out) {
|
|
124
|
+
const t = /* @__PURE__ */ new Uint32Array(16);
|
|
125
|
+
salsaCore(s, k, i.subarray(0, 2), t, i[2], 20, i[3]);
|
|
126
|
+
let oi = 0;
|
|
127
|
+
out[oi++] = t[0] - s[0] | 0;
|
|
128
|
+
out[oi++] = t[5] - s[1] | 0;
|
|
129
|
+
out[oi++] = t[10] - s[2] | 0;
|
|
130
|
+
out[oi++] = t[15] - s[3] | 0;
|
|
131
|
+
out[oi++] = t[6] - i[0] | 0;
|
|
132
|
+
out[oi++] = t[7] - i[1] | 0;
|
|
133
|
+
out[oi++] = t[8] - i[2] | 0;
|
|
134
|
+
out[oi++] = t[9] - i[3] | 0;
|
|
135
|
+
t.fill(0);
|
|
136
|
+
}
|
|
137
|
+
/** `crypto_box_beforenm`'s second step: HSalsa20 of a shared secret with a zero nonce prefix. */
|
|
138
|
+
const hsalsaKey = (sharedSecret) => {
|
|
139
|
+
const out = /* @__PURE__ */ new Uint32Array(8);
|
|
140
|
+
hsalsa(sigma32_32, words(sharedSecret), /* @__PURE__ */ new Uint32Array(4), out);
|
|
141
|
+
const bytes = new Uint8Array(KEY_BYTES);
|
|
142
|
+
storeWords(out, bytes);
|
|
143
|
+
return bytes;
|
|
144
|
+
};
|
|
145
|
+
/** Little-endian store of `source` words into `target` bytes. */
|
|
146
|
+
const storeWords = (source, target) => {
|
|
147
|
+
for (let i = 0; i < source.length; i++) {
|
|
148
|
+
const w = source[i];
|
|
149
|
+
target[i * 4] = w;
|
|
150
|
+
target[i * 4 + 1] = w >>> 8;
|
|
151
|
+
target[i * 4 + 2] = w >>> 16;
|
|
152
|
+
target[i * 4 + 3] = w >>> 24;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* XSalsa20 keystream XORed over `data` in place, starting at block 0. The first
|
|
157
|
+
* 16 nonce bytes derive a subkey through HSalsa20; the last 8 are the Salsa20
|
|
158
|
+
* nonce.
|
|
159
|
+
*/
|
|
160
|
+
function xsalsa20(key, nonce, data) {
|
|
161
|
+
const k32 = words(key);
|
|
162
|
+
hsalsa(sigma32_32, k32, words(nonce.subarray(0, 16)), k32);
|
|
163
|
+
const n32 = /* @__PURE__ */ new Uint32Array(3);
|
|
164
|
+
n32.set(words(nonce.subarray(16, 24)));
|
|
165
|
+
const block = /* @__PURE__ */ new Uint8Array(64);
|
|
166
|
+
const b32 = /* @__PURE__ */ new Uint32Array(16);
|
|
167
|
+
for (let pos = 0, counter = 0; pos < data.length; counter++) {
|
|
168
|
+
salsaCore(sigma32_32, k32, n32, b32, counter);
|
|
169
|
+
storeWords(b32, block);
|
|
170
|
+
const take = Math.min(64, data.length - pos);
|
|
171
|
+
for (let j = 0; j < take; j++) data[pos + j] ^= block[j];
|
|
172
|
+
pos += take;
|
|
173
|
+
}
|
|
174
|
+
k32.fill(0);
|
|
175
|
+
block.fill(0);
|
|
176
|
+
b32.fill(0);
|
|
177
|
+
}
|
|
178
|
+
function u8to16(a, i) {
|
|
179
|
+
return a[i++] & 255 | (a[i++] & 255) << 8;
|
|
180
|
+
}
|
|
181
|
+
/** Incremental Poly1305 MAC state; `poly1305` below is its one-shot use. */
|
|
182
|
+
var Poly1305 = class {
|
|
183
|
+
blockLen = 16;
|
|
184
|
+
outputLen = 16;
|
|
185
|
+
buffer = /* @__PURE__ */ new Uint8Array(16);
|
|
186
|
+
r = /* @__PURE__ */ new Uint16Array(10);
|
|
187
|
+
h = /* @__PURE__ */ new Uint16Array(10);
|
|
188
|
+
pad = /* @__PURE__ */ new Uint16Array(8);
|
|
189
|
+
pos = 0;
|
|
190
|
+
constructor(key) {
|
|
191
|
+
const t0 = u8to16(key, 0);
|
|
192
|
+
const t1 = u8to16(key, 2);
|
|
193
|
+
const t2 = u8to16(key, 4);
|
|
194
|
+
const t3 = u8to16(key, 6);
|
|
195
|
+
const t4 = u8to16(key, 8);
|
|
196
|
+
const t5 = u8to16(key, 10);
|
|
197
|
+
const t6 = u8to16(key, 12);
|
|
198
|
+
const t7 = u8to16(key, 14);
|
|
199
|
+
this.r[0] = t0 & 8191;
|
|
200
|
+
this.r[1] = (t0 >>> 13 | t1 << 3) & 8191;
|
|
201
|
+
this.r[2] = (t1 >>> 10 | t2 << 6) & 7939;
|
|
202
|
+
this.r[3] = (t2 >>> 7 | t3 << 9) & 8191;
|
|
203
|
+
this.r[4] = (t3 >>> 4 | t4 << 12) & 255;
|
|
204
|
+
this.r[5] = t4 >>> 1 & 8190;
|
|
205
|
+
this.r[6] = (t4 >>> 14 | t5 << 2) & 8191;
|
|
206
|
+
this.r[7] = (t5 >>> 11 | t6 << 5) & 8065;
|
|
207
|
+
this.r[8] = (t6 >>> 8 | t7 << 8) & 8191;
|
|
208
|
+
this.r[9] = t7 >>> 5 & 127;
|
|
209
|
+
for (let i = 0; i < 8; i++) this.pad[i] = u8to16(key, 16 + 2 * i);
|
|
210
|
+
}
|
|
211
|
+
process(data, offset, isLast = false) {
|
|
212
|
+
const hibit = isLast ? 0 : 2048;
|
|
213
|
+
const { h, r } = this;
|
|
214
|
+
const r0 = r[0];
|
|
215
|
+
const r1 = r[1];
|
|
216
|
+
const r2 = r[2];
|
|
217
|
+
const r3 = r[3];
|
|
218
|
+
const r4 = r[4];
|
|
219
|
+
const r5 = r[5];
|
|
220
|
+
const r6 = r[6];
|
|
221
|
+
const r7 = r[7];
|
|
222
|
+
const r8 = r[8];
|
|
223
|
+
const r9 = r[9];
|
|
224
|
+
const t0 = u8to16(data, offset + 0);
|
|
225
|
+
const t1 = u8to16(data, offset + 2);
|
|
226
|
+
const t2 = u8to16(data, offset + 4);
|
|
227
|
+
const t3 = u8to16(data, offset + 6);
|
|
228
|
+
const t4 = u8to16(data, offset + 8);
|
|
229
|
+
const t5 = u8to16(data, offset + 10);
|
|
230
|
+
const t6 = u8to16(data, offset + 12);
|
|
231
|
+
const t7 = u8to16(data, offset + 14);
|
|
232
|
+
const h0 = h[0] + (t0 & 8191);
|
|
233
|
+
const h1 = h[1] + ((t0 >>> 13 | t1 << 3) & 8191);
|
|
234
|
+
const h2 = h[2] + ((t1 >>> 10 | t2 << 6) & 8191);
|
|
235
|
+
const h3 = h[3] + ((t2 >>> 7 | t3 << 9) & 8191);
|
|
236
|
+
const h4 = h[4] + ((t3 >>> 4 | t4 << 12) & 8191);
|
|
237
|
+
const h5 = h[5] + (t4 >>> 1 & 8191);
|
|
238
|
+
const h6 = h[6] + ((t4 >>> 14 | t5 << 2) & 8191);
|
|
239
|
+
const h7 = h[7] + ((t5 >>> 11 | t6 << 5) & 8191);
|
|
240
|
+
const h8 = h[8] + ((t6 >>> 8 | t7 << 8) & 8191);
|
|
241
|
+
const h9 = h[9] + (t7 >>> 5 | hibit);
|
|
242
|
+
let c = 0;
|
|
243
|
+
let d0 = c + h0 * r0 + h1 * (5 * r9) + h2 * (5 * r8) + h3 * (5 * r7) + h4 * (5 * r6);
|
|
244
|
+
c = d0 >>> 13;
|
|
245
|
+
d0 &= 8191;
|
|
246
|
+
d0 += h5 * (5 * r5) + h6 * (5 * r4) + h7 * (5 * r3) + h8 * (5 * r2) + h9 * (5 * r1);
|
|
247
|
+
c += d0 >>> 13;
|
|
248
|
+
d0 &= 8191;
|
|
249
|
+
let d1 = c + h0 * r1 + h1 * r0 + h2 * (5 * r9) + h3 * (5 * r8) + h4 * (5 * r7);
|
|
250
|
+
c = d1 >>> 13;
|
|
251
|
+
d1 &= 8191;
|
|
252
|
+
d1 += h5 * (5 * r6) + h6 * (5 * r5) + h7 * (5 * r4) + h8 * (5 * r3) + h9 * (5 * r2);
|
|
253
|
+
c += d1 >>> 13;
|
|
254
|
+
d1 &= 8191;
|
|
255
|
+
let d2 = c + h0 * r2 + h1 * r1 + h2 * r0 + h3 * (5 * r9) + h4 * (5 * r8);
|
|
256
|
+
c = d2 >>> 13;
|
|
257
|
+
d2 &= 8191;
|
|
258
|
+
d2 += h5 * (5 * r7) + h6 * (5 * r6) + h7 * (5 * r5) + h8 * (5 * r4) + h9 * (5 * r3);
|
|
259
|
+
c += d2 >>> 13;
|
|
260
|
+
d2 &= 8191;
|
|
261
|
+
let d3 = c + h0 * r3 + h1 * r2 + h2 * r1 + h3 * r0 + h4 * (5 * r9);
|
|
262
|
+
c = d3 >>> 13;
|
|
263
|
+
d3 &= 8191;
|
|
264
|
+
d3 += h5 * (5 * r8) + h6 * (5 * r7) + h7 * (5 * r6) + h8 * (5 * r5) + h9 * (5 * r4);
|
|
265
|
+
c += d3 >>> 13;
|
|
266
|
+
d3 &= 8191;
|
|
267
|
+
let d4 = c + h0 * r4 + h1 * r3 + h2 * r2 + h3 * r1 + h4 * r0;
|
|
268
|
+
c = d4 >>> 13;
|
|
269
|
+
d4 &= 8191;
|
|
270
|
+
d4 += h5 * (5 * r9) + h6 * (5 * r8) + h7 * (5 * r7) + h8 * (5 * r6) + h9 * (5 * r5);
|
|
271
|
+
c += d4 >>> 13;
|
|
272
|
+
d4 &= 8191;
|
|
273
|
+
let d5 = c + h0 * r5 + h1 * r4 + h2 * r3 + h3 * r2 + h4 * r1;
|
|
274
|
+
c = d5 >>> 13;
|
|
275
|
+
d5 &= 8191;
|
|
276
|
+
d5 += h5 * r0 + h6 * (5 * r9) + h7 * (5 * r8) + h8 * (5 * r7) + h9 * (5 * r6);
|
|
277
|
+
c += d5 >>> 13;
|
|
278
|
+
d5 &= 8191;
|
|
279
|
+
let d6 = c + h0 * r6 + h1 * r5 + h2 * r4 + h3 * r3 + h4 * r2;
|
|
280
|
+
c = d6 >>> 13;
|
|
281
|
+
d6 &= 8191;
|
|
282
|
+
d6 += h5 * r1 + h6 * r0 + h7 * (5 * r9) + h8 * (5 * r8) + h9 * (5 * r7);
|
|
283
|
+
c += d6 >>> 13;
|
|
284
|
+
d6 &= 8191;
|
|
285
|
+
let d7 = c + h0 * r7 + h1 * r6 + h2 * r5 + h3 * r4 + h4 * r3;
|
|
286
|
+
c = d7 >>> 13;
|
|
287
|
+
d7 &= 8191;
|
|
288
|
+
d7 += h5 * r2 + h6 * r1 + h7 * r0 + h8 * (5 * r9) + h9 * (5 * r8);
|
|
289
|
+
c += d7 >>> 13;
|
|
290
|
+
d7 &= 8191;
|
|
291
|
+
let d8 = c + h0 * r8 + h1 * r7 + h2 * r6 + h3 * r5 + h4 * r4;
|
|
292
|
+
c = d8 >>> 13;
|
|
293
|
+
d8 &= 8191;
|
|
294
|
+
d8 += h5 * r3 + h6 * r2 + h7 * r1 + h8 * r0 + h9 * (5 * r9);
|
|
295
|
+
c += d8 >>> 13;
|
|
296
|
+
d8 &= 8191;
|
|
297
|
+
let d9 = c + h0 * r9 + h1 * r8 + h2 * r7 + h3 * r6 + h4 * r5;
|
|
298
|
+
c = d9 >>> 13;
|
|
299
|
+
d9 &= 8191;
|
|
300
|
+
d9 += h5 * r4 + h6 * r3 + h7 * r2 + h8 * r1 + h9 * r0;
|
|
301
|
+
c += d9 >>> 13;
|
|
302
|
+
d9 &= 8191;
|
|
303
|
+
c = (c << 2) + c | 0;
|
|
304
|
+
c = c + d0 | 0;
|
|
305
|
+
d0 = c & 8191;
|
|
306
|
+
c = c >>> 13;
|
|
307
|
+
d1 += c;
|
|
308
|
+
h[0] = d0;
|
|
309
|
+
h[1] = d1;
|
|
310
|
+
h[2] = d2;
|
|
311
|
+
h[3] = d3;
|
|
312
|
+
h[4] = d4;
|
|
313
|
+
h[5] = d5;
|
|
314
|
+
h[6] = d6;
|
|
315
|
+
h[7] = d7;
|
|
316
|
+
h[8] = d8;
|
|
317
|
+
h[9] = d9;
|
|
318
|
+
}
|
|
319
|
+
finalize() {
|
|
320
|
+
const { h, pad } = this;
|
|
321
|
+
const g = /* @__PURE__ */ new Uint16Array(10);
|
|
322
|
+
let c = h[1] >>> 13;
|
|
323
|
+
h[1] &= 8191;
|
|
324
|
+
for (let i = 2; i < 10; i++) {
|
|
325
|
+
h[i] += c;
|
|
326
|
+
c = h[i] >>> 13;
|
|
327
|
+
h[i] &= 8191;
|
|
328
|
+
}
|
|
329
|
+
h[0] += c * 5;
|
|
330
|
+
c = h[0] >>> 13;
|
|
331
|
+
h[0] &= 8191;
|
|
332
|
+
h[1] += c;
|
|
333
|
+
c = h[1] >>> 13;
|
|
334
|
+
h[1] &= 8191;
|
|
335
|
+
h[2] += c;
|
|
336
|
+
g[0] = h[0] + 5;
|
|
337
|
+
c = g[0] >>> 13;
|
|
338
|
+
g[0] &= 8191;
|
|
339
|
+
for (let i = 1; i < 10; i++) {
|
|
340
|
+
g[i] = h[i] + c;
|
|
341
|
+
c = g[i] >>> 13;
|
|
342
|
+
g[i] &= 8191;
|
|
343
|
+
}
|
|
344
|
+
g[9] -= 8192;
|
|
345
|
+
let mask = (c ^ 1) - 1;
|
|
346
|
+
for (let i = 0; i < 10; i++) g[i] &= mask;
|
|
347
|
+
mask = ~mask;
|
|
348
|
+
for (let i = 0; i < 10; i++) h[i] = h[i] & mask | g[i];
|
|
349
|
+
h[0] = (h[0] | h[1] << 13) & 65535;
|
|
350
|
+
h[1] = (h[1] >>> 3 | h[2] << 10) & 65535;
|
|
351
|
+
h[2] = (h[2] >>> 6 | h[3] << 7) & 65535;
|
|
352
|
+
h[3] = (h[3] >>> 9 | h[4] << 4) & 65535;
|
|
353
|
+
h[4] = (h[4] >>> 12 | h[5] << 1 | h[6] << 14) & 65535;
|
|
354
|
+
h[5] = (h[6] >>> 2 | h[7] << 11) & 65535;
|
|
355
|
+
h[6] = (h[7] >>> 5 | h[8] << 8) & 65535;
|
|
356
|
+
h[7] = (h[8] >>> 8 | h[9] << 5) & 65535;
|
|
357
|
+
let f = h[0] + pad[0];
|
|
358
|
+
h[0] = f & 65535;
|
|
359
|
+
for (let i = 1; i < 8; i++) {
|
|
360
|
+
f = (h[i] + pad[i] | 0) + (f >>> 16) | 0;
|
|
361
|
+
h[i] = f & 65535;
|
|
362
|
+
}
|
|
363
|
+
g.fill(0);
|
|
364
|
+
}
|
|
365
|
+
update(data) {
|
|
366
|
+
const { buffer, blockLen } = this;
|
|
367
|
+
const len = data.length;
|
|
368
|
+
for (let pos = 0; pos < len;) {
|
|
369
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
370
|
+
if (take === blockLen) {
|
|
371
|
+
for (; blockLen <= len - pos; pos += blockLen) this.process(data, pos);
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
375
|
+
this.pos += take;
|
|
376
|
+
pos += take;
|
|
377
|
+
if (this.pos === blockLen) {
|
|
378
|
+
this.process(buffer, 0, false);
|
|
379
|
+
this.pos = 0;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return this;
|
|
383
|
+
}
|
|
384
|
+
digest() {
|
|
385
|
+
const { buffer, h } = this;
|
|
386
|
+
let { pos } = this;
|
|
387
|
+
if (pos) {
|
|
388
|
+
buffer[pos++] = 1;
|
|
389
|
+
for (; pos < 16; pos++) buffer[pos] = 0;
|
|
390
|
+
this.process(buffer, 0, true);
|
|
391
|
+
}
|
|
392
|
+
this.finalize();
|
|
393
|
+
const res = new Uint8Array(this.outputLen);
|
|
394
|
+
let opos = 0;
|
|
395
|
+
for (let i = 0; i < 8; i++) {
|
|
396
|
+
res[opos++] = h[i] >>> 0;
|
|
397
|
+
res[opos++] = h[i] >>> 8;
|
|
398
|
+
}
|
|
399
|
+
this.r.fill(0);
|
|
400
|
+
this.h.fill(0);
|
|
401
|
+
this.pad.fill(0);
|
|
402
|
+
buffer.fill(0);
|
|
403
|
+
return res;
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
/** Poly1305 MAC (RFC 8439 §2.5) of `message` under a 32-byte one-time `key`. */
|
|
407
|
+
const poly1305 = (message, key) => new Poly1305(key).update(message).digest();
|
|
408
|
+
/**
|
|
409
|
+
* Encrypt and authenticate `plaintext` with XSalsa20-Poly1305 (libsodium's
|
|
410
|
+
* `crypto_secretbox_easy`): returns `tag (16 bytes) || ciphertext`.
|
|
411
|
+
*
|
|
412
|
+
* @param plaintext - The message.
|
|
413
|
+
* @param nonce - 24 bytes.
|
|
414
|
+
* @param key - 32 bytes.
|
|
415
|
+
*
|
|
416
|
+
* @internal
|
|
417
|
+
*/
|
|
418
|
+
function secretbox(plaintext, nonce, key) {
|
|
419
|
+
if (key.length !== KEY_BYTES) throw new RangeError(`secretbox key must be ${KEY_BYTES} bytes, got ${key.length}`);
|
|
420
|
+
if (nonce.length !== NONCE_BYTES) throw new RangeError(`secretbox nonce must be ${NONCE_BYTES} bytes, got ${nonce.length}`);
|
|
421
|
+
const work = new Uint8Array(KEY_BYTES + plaintext.length);
|
|
422
|
+
work.set(plaintext, KEY_BYTES);
|
|
423
|
+
xsalsa20(key, nonce, work);
|
|
424
|
+
const tag = poly1305(work.subarray(KEY_BYTES), work.subarray(0, KEY_BYTES));
|
|
425
|
+
const sealed = new Uint8Array(TAG_BYTES + plaintext.length);
|
|
426
|
+
sealed.set(tag);
|
|
427
|
+
sealed.set(work.subarray(KEY_BYTES), TAG_BYTES);
|
|
428
|
+
work.fill(0);
|
|
429
|
+
return sealed;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
export { hsalsa, hsalsaKey, secretbox };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effected/github",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.33.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Typed GitHub REST and GraphQL services over the octokit core request surface, with app auth and resource helpers.",
|
|
6
6
|
"keywords": [
|
|
@@ -45,9 +45,7 @@
|
|
|
45
45
|
"@effected/semver": "^0.24.0",
|
|
46
46
|
"@octokit/core": "^7.0.6",
|
|
47
47
|
"@octokit/plugin-paginate-rest": "^16.0.0",
|
|
48
|
-
"@octokit/types": "^18.0.0"
|
|
49
|
-
"blakejs": "1.2.1",
|
|
50
|
-
"tweetnacl": "1.0.3"
|
|
48
|
+
"@octokit/types": "^18.0.0"
|
|
51
49
|
},
|
|
52
50
|
"peerDependencies": {
|
|
53
51
|
"effect": "~4.1.0"
|