@shieldfive/cli 0.1.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/LICENSE +201 -0
- package/README.md +144 -0
- package/SECURITY.md +84 -0
- package/package.json +52 -0
- package/src/auth.mjs +35 -0
- package/src/cli.mjs +211 -0
- package/src/sfCrypto.mjs +77 -0
- package/src/sync.mjs +205 -0
- package/src/unlock.mjs +107 -0
- package/src/upload.mjs +342 -0
- package/src/uploadCrypto.mjs +207 -0
- package/src/vault.mjs +45 -0
package/src/upload.mjs
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
// ShieldFive CLI — upload: create-session -> chunk-encrypt -> Backblaze PUT ->
|
|
2
|
+
// finalize. Ported from the apps (mobile cloudApi.ts + syncService.ts).
|
|
3
|
+
//
|
|
4
|
+
// Two paths, chosen by the server:
|
|
5
|
+
// direct — a single-chunk file (<= chunkSize). One b2_upload_file POST.
|
|
6
|
+
// large — a multipart file (> chunkSize). b2_upload_part per chunk, then
|
|
7
|
+
// b2_finish_large_file (server-side) at finalize.
|
|
8
|
+
//
|
|
9
|
+
// Chunks are read from disk one at a time (never the whole file into memory),
|
|
10
|
+
// so a multi-gigabyte upload holds at most one chunk (~5 MiB) resident.
|
|
11
|
+
|
|
12
|
+
import { open } from 'node:fs/promises'
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
bytesToBase64,
|
|
16
|
+
encryptChunkWithDigest,
|
|
17
|
+
encryptMetadataV4,
|
|
18
|
+
generateNoncePrefixB64,
|
|
19
|
+
generateRandomKeyB64,
|
|
20
|
+
getCiphertextHashFromParts,
|
|
21
|
+
hashMetadataV4,
|
|
22
|
+
wrapKeyB64,
|
|
23
|
+
} from './uploadCrypto.mjs'
|
|
24
|
+
|
|
25
|
+
const CREATE_SESSION_PATH = '/api/files/create-upload-session'
|
|
26
|
+
const COMPLETE_PATH = '/api/files/complete-upload'
|
|
27
|
+
const PART_URL_PATH = '/api/files/upload-part-url'
|
|
28
|
+
const CHUNK_SIZE = 5 * 1024 * 1024
|
|
29
|
+
|
|
30
|
+
// Read a file in fixed-size chunks without holding it all in memory. Each yield
|
|
31
|
+
// is an independent copy (the read buffer is reused), so the caller may retain
|
|
32
|
+
// or transform a chunk across the next read safely.
|
|
33
|
+
export async function* readFileChunks(path, chunkSize) {
|
|
34
|
+
const handle = await open(path, 'r')
|
|
35
|
+
try {
|
|
36
|
+
const buffer = Buffer.allocUnsafe(chunkSize)
|
|
37
|
+
for (;;) {
|
|
38
|
+
// Fill up to chunkSize across as many reads as it takes, so every chunk
|
|
39
|
+
// but the last is exactly chunkSize. A single fs.read may return short
|
|
40
|
+
// (FUSE/network mounts, signals) — if a non-final part were short, its
|
|
41
|
+
// ciphertext length would desync the multipart proof and part manifest.
|
|
42
|
+
let filled = 0
|
|
43
|
+
while (filled < chunkSize) {
|
|
44
|
+
const { bytesRead } = await handle.read(
|
|
45
|
+
buffer,
|
|
46
|
+
filled,
|
|
47
|
+
chunkSize - filled,
|
|
48
|
+
null,
|
|
49
|
+
)
|
|
50
|
+
if (bytesRead === 0) break // EOF
|
|
51
|
+
filled += bytesRead
|
|
52
|
+
}
|
|
53
|
+
if (filled === 0) break
|
|
54
|
+
yield new Uint8Array(buffer.subarray(0, filled))
|
|
55
|
+
if (filled < chunkSize) break // last (short) chunk
|
|
56
|
+
}
|
|
57
|
+
} finally {
|
|
58
|
+
await handle.close()
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function createUploadSession({
|
|
63
|
+
apiBaseUrl,
|
|
64
|
+
accessToken,
|
|
65
|
+
rootKey,
|
|
66
|
+
name,
|
|
67
|
+
size,
|
|
68
|
+
}) {
|
|
69
|
+
const rootKeyB64 = bytesToBase64(rootKey)
|
|
70
|
+
const nameEnvelope = encryptMetadataV4(name, rootKeyB64)
|
|
71
|
+
const csk = generateRandomKeyB64()
|
|
72
|
+
const cskEnvelope = wrapKeyB64({ wrappingKeyB64: rootKeyB64, keyToWrapB64: csk })
|
|
73
|
+
const noncePrefix = generateNoncePrefixB64()
|
|
74
|
+
|
|
75
|
+
const res = await fetch(new URL(CREATE_SESSION_PATH, apiBaseUrl), {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
headers: {
|
|
78
|
+
Authorization: `Bearer ${accessToken}`,
|
|
79
|
+
'Content-Type': 'application/json',
|
|
80
|
+
},
|
|
81
|
+
body: JSON.stringify({
|
|
82
|
+
name_encrypted: JSON.stringify(nameEnvelope),
|
|
83
|
+
nameHash: hashMetadataV4(name.toLowerCase(), rootKeyB64),
|
|
84
|
+
csk_wrapped: cskEnvelope.wrapped,
|
|
85
|
+
csk_iv: cskEnvelope.iv,
|
|
86
|
+
cipher_nonce_prefix: noncePrefix,
|
|
87
|
+
sizeBytes: size,
|
|
88
|
+
chunkSize: CHUNK_SIZE,
|
|
89
|
+
folderId: null,
|
|
90
|
+
contentType: 'application/octet-stream',
|
|
91
|
+
thumbnail_ciphertext_b64: null,
|
|
92
|
+
thumbnail_nonce_prefix: null,
|
|
93
|
+
}),
|
|
94
|
+
})
|
|
95
|
+
const body = await res.json().catch(() => ({}))
|
|
96
|
+
if (!res.ok) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`create-upload-session failed (HTTP ${res.status}): ${JSON.stringify(body)}`,
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
return { session: body, csk, noncePrefix }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Direct upload: b2_upload_file. Returns the storage file id the server patches
|
|
105
|
+
// into the row before verifying the proof.
|
|
106
|
+
async function putDirectToBackblaze({ session, ciphertext, sha1 }) {
|
|
107
|
+
const res = await fetch(session.uploadUrl, {
|
|
108
|
+
method: 'POST', // Backblaze b2_upload_file is a POST
|
|
109
|
+
headers: {
|
|
110
|
+
Authorization: session.authToken,
|
|
111
|
+
'X-Bz-File-Name': encodeURIComponent(session.storagePath),
|
|
112
|
+
'X-Bz-Content-Sha1': sha1,
|
|
113
|
+
'Content-Type': 'application/octet-stream',
|
|
114
|
+
},
|
|
115
|
+
body: ciphertext,
|
|
116
|
+
})
|
|
117
|
+
if (!res.ok) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
`Backblaze upload failed (HTTP ${res.status}): ${await res.text().catch(() => '')}`,
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
const body = await res.json().catch(() => ({}))
|
|
123
|
+
if (!body.fileId) {
|
|
124
|
+
throw new Error(`Backblaze response missing fileId: ${JSON.stringify(body)}`)
|
|
125
|
+
}
|
|
126
|
+
return body.fileId
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Ask the server for a fresh b2_upload_part URL + token when the current one
|
|
130
|
+
// expires (Backblaze part tokens are short-lived and single-host).
|
|
131
|
+
async function refreshUploadPartUrl({ apiBaseUrl, accessToken, fileId }) {
|
|
132
|
+
const res = await fetch(new URL(PART_URL_PATH, apiBaseUrl), {
|
|
133
|
+
method: 'POST',
|
|
134
|
+
headers: {
|
|
135
|
+
Authorization: `Bearer ${accessToken}`,
|
|
136
|
+
'Content-Type': 'application/json',
|
|
137
|
+
},
|
|
138
|
+
body: JSON.stringify({ fileId }),
|
|
139
|
+
})
|
|
140
|
+
const body = await res.json().catch(() => ({}))
|
|
141
|
+
if (!res.ok || !body.uploadUrl || !body.authToken) {
|
|
142
|
+
throw new Error(
|
|
143
|
+
`upload-part-url failed (HTTP ${res.status}): ${JSON.stringify(body)}`,
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
return { uploadUrl: body.uploadUrl, authToken: body.authToken }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// One b2_upload_part POST. On an expired token (401/403) refresh the part URL
|
|
150
|
+
// once and retry, mirroring uploadMultipartCiphertextPart in the apps. Returns
|
|
151
|
+
// the (possibly refreshed) url/token so the caller reuses them for the next part.
|
|
152
|
+
async function putPartToBackblaze({
|
|
153
|
+
apiBaseUrl,
|
|
154
|
+
accessToken,
|
|
155
|
+
fileId,
|
|
156
|
+
uploadUrl,
|
|
157
|
+
authToken,
|
|
158
|
+
partNumber,
|
|
159
|
+
ciphertext,
|
|
160
|
+
sha1,
|
|
161
|
+
}) {
|
|
162
|
+
const attempt = (url, token) =>
|
|
163
|
+
fetch(url, {
|
|
164
|
+
method: 'POST', // Backblaze b2_upload_part is a POST
|
|
165
|
+
headers: {
|
|
166
|
+
Authorization: token,
|
|
167
|
+
'X-Bz-Part-Number': String(partNumber),
|
|
168
|
+
'X-Bz-Content-Sha1': sha1,
|
|
169
|
+
'Content-Type': 'application/octet-stream',
|
|
170
|
+
},
|
|
171
|
+
body: ciphertext,
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
let url = uploadUrl
|
|
175
|
+
let token = authToken
|
|
176
|
+
let res = await attempt(url, token)
|
|
177
|
+
|
|
178
|
+
if (res.status === 401 || res.status === 403) {
|
|
179
|
+
const refreshed = await refreshUploadPartUrl({ apiBaseUrl, accessToken, fileId })
|
|
180
|
+
url = refreshed.uploadUrl
|
|
181
|
+
token = refreshed.authToken
|
|
182
|
+
res = await attempt(url, token)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (!res.ok) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
`Backblaze part ${partNumber} upload failed (HTTP ${res.status}): ` +
|
|
188
|
+
`${await res.text().catch(() => '')}`,
|
|
189
|
+
)
|
|
190
|
+
}
|
|
191
|
+
return { uploadUrl: url, authToken: token }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function finalizeUpload({
|
|
195
|
+
apiBaseUrl,
|
|
196
|
+
accessToken,
|
|
197
|
+
fileId,
|
|
198
|
+
b2FileId,
|
|
199
|
+
partSha1Array,
|
|
200
|
+
proof,
|
|
201
|
+
ciphertextHash,
|
|
202
|
+
}) {
|
|
203
|
+
const res = await fetch(new URL(COMPLETE_PATH, apiBaseUrl), {
|
|
204
|
+
method: 'POST',
|
|
205
|
+
headers: {
|
|
206
|
+
Authorization: `Bearer ${accessToken}`,
|
|
207
|
+
'Content-Type': 'application/json',
|
|
208
|
+
},
|
|
209
|
+
body: JSON.stringify({ fileId, b2FileId, partSha1Array, proof, ciphertextHash }),
|
|
210
|
+
})
|
|
211
|
+
const body = await res.json().catch(() => ({}))
|
|
212
|
+
if (!res.ok) {
|
|
213
|
+
throw new Error(
|
|
214
|
+
`complete-upload failed (HTTP ${res.status}): ${JSON.stringify(body)}`,
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
return body
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Single-chunk file: encrypt the one chunk (AES-GCM, suite 0x01) + SHA-1 +
|
|
221
|
+
// proof, PUT it, finalize. b2FileId is the id Backblaze returns from the PUT.
|
|
222
|
+
async function uploadDirect({ apiBaseUrl, accessToken, session, csk, noncePrefix, path }) {
|
|
223
|
+
const chunkSize = session.chunkSize ?? CHUNK_SIZE
|
|
224
|
+
let plaintext = null
|
|
225
|
+
for await (const chunk of readFileChunks(path, chunkSize)) {
|
|
226
|
+
if (plaintext !== null) {
|
|
227
|
+
throw new Error('Direct upload session is larger than a single chunk.')
|
|
228
|
+
}
|
|
229
|
+
plaintext = chunk
|
|
230
|
+
}
|
|
231
|
+
if (plaintext === null) plaintext = new Uint8Array(0)
|
|
232
|
+
|
|
233
|
+
const { ciphertext, sha1Hex, proofHex } = await encryptChunkWithDigest({
|
|
234
|
+
cskB64: csk,
|
|
235
|
+
noncePrefixB64: noncePrefix,
|
|
236
|
+
chunkIndex: 0,
|
|
237
|
+
plaintext,
|
|
238
|
+
proofKeyHex: session.proofKey,
|
|
239
|
+
chunkSize,
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
const b2FileId = await putDirectToBackblaze({ session, ciphertext, sha1: sha1Hex })
|
|
243
|
+
|
|
244
|
+
return finalizeUpload({
|
|
245
|
+
apiBaseUrl,
|
|
246
|
+
accessToken,
|
|
247
|
+
fileId: session.fileId,
|
|
248
|
+
b2FileId,
|
|
249
|
+
partSha1Array: [sha1Hex],
|
|
250
|
+
proof: proofHex,
|
|
251
|
+
ciphertextHash: sha1Hex,
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Multipart file: stream each chunk, encrypt (per-chunk nonce via chunkIndex),
|
|
256
|
+
// b2_upload_part, collect part SHA-1s. The upload proof is computed from the
|
|
257
|
+
// FIRST chunk only — the server re-derives it from the first chunkSize+tag
|
|
258
|
+
// bytes of the stored object, which is exactly part 1. b2FileId is the
|
|
259
|
+
// large-file id from the session; the server calls b2_finish_large_file with
|
|
260
|
+
// the ordered part hashes at finalize.
|
|
261
|
+
async function uploadMultipart({ apiBaseUrl, accessToken, session, csk, noncePrefix, path }) {
|
|
262
|
+
if (!session.b2FileId) {
|
|
263
|
+
throw new Error('Multipart upload session is missing storage id (b2FileId).')
|
|
264
|
+
}
|
|
265
|
+
const chunkSize = session.chunkSize ?? CHUNK_SIZE
|
|
266
|
+
|
|
267
|
+
let uploadUrl = session.uploadUrl
|
|
268
|
+
let authToken = session.authToken
|
|
269
|
+
const partSha1Array = []
|
|
270
|
+
let proof
|
|
271
|
+
let chunkIndex = 0
|
|
272
|
+
|
|
273
|
+
for await (const plaintext of readFileChunks(path, chunkSize)) {
|
|
274
|
+
const encrypted = await encryptChunkWithDigest({
|
|
275
|
+
cskB64: csk,
|
|
276
|
+
noncePrefixB64: noncePrefix,
|
|
277
|
+
chunkIndex,
|
|
278
|
+
plaintext,
|
|
279
|
+
// Only the first chunk carries the proof; the server verifies over part 1.
|
|
280
|
+
proofKeyHex: chunkIndex === 0 ? session.proofKey : undefined,
|
|
281
|
+
chunkSize: chunkIndex === 0 ? chunkSize : undefined,
|
|
282
|
+
})
|
|
283
|
+
if (chunkIndex === 0) proof = encrypted.proofHex
|
|
284
|
+
|
|
285
|
+
const updated = await putPartToBackblaze({
|
|
286
|
+
apiBaseUrl,
|
|
287
|
+
accessToken,
|
|
288
|
+
fileId: session.fileId,
|
|
289
|
+
uploadUrl,
|
|
290
|
+
authToken,
|
|
291
|
+
partNumber: chunkIndex + 1, // Backblaze parts are 1-based
|
|
292
|
+
ciphertext: encrypted.ciphertext,
|
|
293
|
+
sha1: encrypted.sha1Hex,
|
|
294
|
+
})
|
|
295
|
+
uploadUrl = updated.uploadUrl
|
|
296
|
+
authToken = updated.authToken
|
|
297
|
+
partSha1Array.push(encrypted.sha1Hex)
|
|
298
|
+
chunkIndex += 1
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (!partSha1Array.length || !proof) {
|
|
302
|
+
throw new Error('Multipart upload produced no parts.')
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const ciphertextHash = await getCiphertextHashFromParts(partSha1Array)
|
|
306
|
+
|
|
307
|
+
return finalizeUpload({
|
|
308
|
+
apiBaseUrl,
|
|
309
|
+
accessToken,
|
|
310
|
+
fileId: session.fileId,
|
|
311
|
+
b2FileId: session.b2FileId,
|
|
312
|
+
partSha1Array,
|
|
313
|
+
proof,
|
|
314
|
+
ciphertextHash,
|
|
315
|
+
})
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export async function uploadFile({ apiBaseUrl, accessToken, rootKey, name, path, size }) {
|
|
319
|
+
const { session, csk, noncePrefix } = await createUploadSession({
|
|
320
|
+
apiBaseUrl,
|
|
321
|
+
accessToken,
|
|
322
|
+
rootKey,
|
|
323
|
+
name,
|
|
324
|
+
size,
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
if (session.uploadKind === 'direct') {
|
|
328
|
+
if (!session.uploadUrl || !session.authToken || !session.storagePath) {
|
|
329
|
+
throw new Error('Direct upload session is missing credentials.')
|
|
330
|
+
}
|
|
331
|
+
await uploadDirect({ apiBaseUrl, accessToken, session, csk, noncePrefix, path })
|
|
332
|
+
} else if (session.uploadKind === 'large') {
|
|
333
|
+
if (!session.uploadUrl || !session.authToken) {
|
|
334
|
+
throw new Error('Multipart upload session is missing credentials.')
|
|
335
|
+
}
|
|
336
|
+
await uploadMultipart({ apiBaseUrl, accessToken, session, csk, noncePrefix, path })
|
|
337
|
+
} else {
|
|
338
|
+
throw new Error(`Unknown uploadKind "${session.uploadKind}" for "${name}".`)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return { fileId: session.fileId }
|
|
342
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// ShieldFive CLI — upload field crypto, ported byte-for-byte from the apps
|
|
2
|
+
// (mobile/src/features/crypto/encryptionCompat.ts). These produce the fields the
|
|
3
|
+
// `create-upload-session` endpoint expects: the wrapped content key, the v4
|
|
4
|
+
// filename envelope, and the keyed name hash. Same @noble versions as the apps
|
|
5
|
+
// (@noble/ciphers@2, @noble/hashes@2) so the bytes match.
|
|
6
|
+
|
|
7
|
+
import { webcrypto } from 'node:crypto'
|
|
8
|
+
|
|
9
|
+
import { gcm } from '@noble/ciphers/aes.js'
|
|
10
|
+
import { argon2id } from '@noble/hashes/argon2.js'
|
|
11
|
+
import { hmac } from '@noble/hashes/hmac.js'
|
|
12
|
+
import { sha256 } from '@noble/hashes/sha2.js'
|
|
13
|
+
|
|
14
|
+
const AES_GCM_TAG_BYTES = 16
|
|
15
|
+
|
|
16
|
+
const utf8 = (s) => new TextEncoder().encode(s)
|
|
17
|
+
export const bytesToBase64 = (u8) => Buffer.from(u8).toString('base64')
|
|
18
|
+
export const base64ToBytes = (b64) => new Uint8Array(Buffer.from(b64, 'base64'))
|
|
19
|
+
const bytesToHex = (u8) => Buffer.from(u8).toString('hex')
|
|
20
|
+
const randomBytes = (n) => webcrypto.getRandomValues(new Uint8Array(n))
|
|
21
|
+
|
|
22
|
+
// gcm().encrypt returns ciphertext||tag; gcm().decrypt expects the same.
|
|
23
|
+
export function aesGcmEncrypt(key, iv, plaintext) {
|
|
24
|
+
return gcm(key, iv).encrypt(plaintext)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function generateRandomKeyB64(bytes = 32) {
|
|
28
|
+
return bytesToBase64(randomBytes(bytes))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function generateNoncePrefixB64() {
|
|
32
|
+
return bytesToBase64(randomBytes(4))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function wrapKeyB64({ wrappingKeyB64, keyToWrapB64 }) {
|
|
36
|
+
const iv = randomBytes(12)
|
|
37
|
+
const ciphertext = aesGcmEncrypt(
|
|
38
|
+
base64ToBytes(wrappingKeyB64),
|
|
39
|
+
iv,
|
|
40
|
+
base64ToBytes(keyToWrapB64),
|
|
41
|
+
)
|
|
42
|
+
return { wrapped: bytesToBase64(ciphertext), iv: bytesToBase64(iv) }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function unwrapKeyB64({ wrappingKeyB64, wrappedKeyB64, ivB64 }) {
|
|
46
|
+
const plaintext = gcm(
|
|
47
|
+
base64ToBytes(wrappingKeyB64),
|
|
48
|
+
base64ToBytes(ivB64),
|
|
49
|
+
).decrypt(base64ToBytes(wrappedKeyB64))
|
|
50
|
+
return bytesToBase64(plaintext)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function hashMetadataV4(input, rootKeySecret) {
|
|
54
|
+
const hmacKey = sha256(utf8(rootKeySecret))
|
|
55
|
+
const signature = hmac(sha256, hmacKey, utf8(input))
|
|
56
|
+
return `v4:${bytesToHex(signature)}`
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const ARGON2_PARAMS = {
|
|
60
|
+
interactive: { t: 2, m: 64 * 1024, p: 1, version: 0x13, dkLen: 32 },
|
|
61
|
+
moderate: { t: 3, m: 256 * 1024, p: 1, version: 0x13, dkLen: 32 },
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// v4 filename metadata (Argon2id "interactive" + AES-GCM) — the format the web
|
|
65
|
+
// reads. rootKeySecret is the base64 string of the vault root key.
|
|
66
|
+
export function encryptMetadataV4(plaintext, rootKeySecret) {
|
|
67
|
+
const salt = randomBytes(16)
|
|
68
|
+
const iv = randomBytes(12)
|
|
69
|
+
const metadataKey = argon2id(rootKeySecret, salt, ARGON2_PARAMS.interactive)
|
|
70
|
+
const ciphertextWithTag = aesGcmEncrypt(metadataKey, iv, utf8(plaintext))
|
|
71
|
+
const tagStart = ciphertextWithTag.length - AES_GCM_TAG_BYTES
|
|
72
|
+
return {
|
|
73
|
+
v: 4,
|
|
74
|
+
ct: bytesToBase64(ciphertextWithTag.slice(0, tagStart)),
|
|
75
|
+
iv: bytesToBase64(iv),
|
|
76
|
+
tag: bytesToBase64(ciphertextWithTag.slice(tagStart)),
|
|
77
|
+
salt: bytesToBase64(salt),
|
|
78
|
+
kdf: 'interactive',
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function decryptMetadataV4(payload, rootKeySecret) {
|
|
83
|
+
const params = ARGON2_PARAMS[payload.kdf === 'moderate' ? 'moderate' : 'interactive']
|
|
84
|
+
const metadataKey = argon2id(rootKeySecret, base64ToBytes(payload.salt), params)
|
|
85
|
+
const ct = base64ToBytes(payload.ct)
|
|
86
|
+
const tag = base64ToBytes(payload.tag)
|
|
87
|
+
const combined = new Uint8Array(ct.length + tag.length)
|
|
88
|
+
combined.set(ct, 0)
|
|
89
|
+
combined.set(tag, ct.length)
|
|
90
|
+
const plaintext = gcm(metadataKey, base64ToBytes(payload.iv)).decrypt(combined)
|
|
91
|
+
return new TextDecoder().decode(plaintext)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ── File chunk encryption (suite 0x01 AES-GCM) + Backblaze upload proof ───────
|
|
95
|
+
|
|
96
|
+
const UPLOAD_PROOF_VERSION = 1
|
|
97
|
+
|
|
98
|
+
function hexToBytes(hex) {
|
|
99
|
+
const out = new Uint8Array(hex.length / 2)
|
|
100
|
+
for (let i = 0; i < out.length; i += 1) {
|
|
101
|
+
out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16)
|
|
102
|
+
}
|
|
103
|
+
return out
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// nonce = 4-byte random prefix || 8-byte big-endian chunk counter.
|
|
107
|
+
export function encryptFileChunk({ cskB64, noncePrefixB64, chunkIndex, plaintext }) {
|
|
108
|
+
const noncePrefix = base64ToBytes(noncePrefixB64)
|
|
109
|
+
if (noncePrefix.length !== 4) throw new Error('Invalid nonce prefix')
|
|
110
|
+
const nonce = new Uint8Array(12)
|
|
111
|
+
nonce.set(noncePrefix, 0)
|
|
112
|
+
let counter = BigInt(chunkIndex)
|
|
113
|
+
for (let i = 0; i < 8; i += 1) {
|
|
114
|
+
nonce[11 - i] = Number(counter & 0xffn)
|
|
115
|
+
counter >>= 8n
|
|
116
|
+
}
|
|
117
|
+
return aesGcmEncrypt(base64ToBytes(cskB64), nonce, plaintext)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function buildUploadProofPrefix({ cipherVersion, chunkSize, noncePrefixBytes }) {
|
|
121
|
+
const size = Math.floor(chunkSize)
|
|
122
|
+
if (!Number.isFinite(size) || size <= 0) {
|
|
123
|
+
throw new Error('Invalid chunk size for upload proof')
|
|
124
|
+
}
|
|
125
|
+
if (noncePrefixBytes.length !== 4) {
|
|
126
|
+
throw new Error('Invalid nonce prefix length for upload proof')
|
|
127
|
+
}
|
|
128
|
+
const prefix = new Uint8Array(1 + 1 + 4 + 4)
|
|
129
|
+
prefix[0] = UPLOAD_PROOF_VERSION
|
|
130
|
+
prefix[1] = cipherVersion
|
|
131
|
+
new DataView(prefix.buffer).setUint32(2, size, false)
|
|
132
|
+
prefix.set(noncePrefixBytes, 6)
|
|
133
|
+
return prefix
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function computeAesGcmUploadProof({
|
|
137
|
+
proofKeyHex,
|
|
138
|
+
cipherVersion,
|
|
139
|
+
chunkSize,
|
|
140
|
+
noncePrefixB64,
|
|
141
|
+
ciphertextChunk,
|
|
142
|
+
}) {
|
|
143
|
+
const proofKey = hexToBytes(proofKeyHex)
|
|
144
|
+
const prefix = buildUploadProofPrefix({
|
|
145
|
+
cipherVersion,
|
|
146
|
+
chunkSize,
|
|
147
|
+
noncePrefixBytes: base64ToBytes(noncePrefixB64),
|
|
148
|
+
})
|
|
149
|
+
const payload = new Uint8Array(prefix.length + ciphertextChunk.length)
|
|
150
|
+
payload.set(prefix, 0)
|
|
151
|
+
payload.set(ciphertextChunk, prefix.length)
|
|
152
|
+
return bytesToHex(hmac(sha256, proofKey, payload))
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export async function sha1Hex(bytes) {
|
|
156
|
+
const digest = await webcrypto.subtle.digest('SHA-1', bytes)
|
|
157
|
+
return bytesToHex(new Uint8Array(digest))
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Encrypt one chunk and produce its SHA-1 (for Backblaze) and the upload proof.
|
|
161
|
+
export async function encryptChunkWithDigest({
|
|
162
|
+
cskB64,
|
|
163
|
+
noncePrefixB64,
|
|
164
|
+
chunkIndex,
|
|
165
|
+
plaintext,
|
|
166
|
+
proofKeyHex,
|
|
167
|
+
chunkSize,
|
|
168
|
+
}) {
|
|
169
|
+
const ciphertext = encryptFileChunk({
|
|
170
|
+
cskB64,
|
|
171
|
+
noncePrefixB64,
|
|
172
|
+
chunkIndex,
|
|
173
|
+
plaintext,
|
|
174
|
+
})
|
|
175
|
+
const digest = await sha1Hex(ciphertext)
|
|
176
|
+
const proofHex =
|
|
177
|
+
proofKeyHex && chunkSize
|
|
178
|
+
? computeAesGcmUploadProof({
|
|
179
|
+
proofKeyHex,
|
|
180
|
+
cipherVersion: 1,
|
|
181
|
+
chunkSize,
|
|
182
|
+
noncePrefixB64,
|
|
183
|
+
ciphertextChunk: ciphertext,
|
|
184
|
+
})
|
|
185
|
+
: undefined
|
|
186
|
+
return { ciphertext, sha1Hex: digest, proofHex }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Multipart ciphertextHash — the value `complete-upload` checks against the
|
|
190
|
+
// server's own computeLargeFileSha1. One part: the part's SHA-1, lowercased.
|
|
191
|
+
// Many parts: SHA-1 over the concatenated raw 20-byte part digests (NOT the
|
|
192
|
+
// hex strings). Ported from encryptionCompat.ts:getCiphertextHashFromParts.
|
|
193
|
+
export async function getCiphertextHashFromParts(partSha1Array) {
|
|
194
|
+
if (!partSha1Array.length) throw new Error('Missing part hashes')
|
|
195
|
+
if (partSha1Array.length === 1) return partSha1Array[0].trim().toLowerCase()
|
|
196
|
+
const bytes = new Uint8Array(partSha1Array.length * 20)
|
|
197
|
+
let offset = 0
|
|
198
|
+
for (const part of partSha1Array) {
|
|
199
|
+
const normalized = part.trim().toLowerCase()
|
|
200
|
+
if (!/^[0-9a-f]{40}$/.test(normalized)) {
|
|
201
|
+
throw new Error('Invalid part SHA1 value')
|
|
202
|
+
}
|
|
203
|
+
bytes.set(hexToBytes(normalized), offset)
|
|
204
|
+
offset += 20
|
|
205
|
+
}
|
|
206
|
+
return sha1Hex(bytes)
|
|
207
|
+
}
|
package/src/vault.mjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// ShieldFive CLI — fetch + unlock the vault key.
|
|
2
|
+
//
|
|
3
|
+
// One authenticated GET returns the vault_keys row (camelCase fields:
|
|
4
|
+
// rkWrappedByUk, ukSalt, ukIv, ukKdf, ukIterations? / ukArgon2Preset?), then the
|
|
5
|
+
// VERIFIED unlock crypto (unlock.mjs) unwraps the root key client-side. The GET
|
|
6
|
+
// is the only live part; the unwrap is offline and round-trip tested.
|
|
7
|
+
|
|
8
|
+
import { unlockRootKey } from './unlock.mjs'
|
|
9
|
+
|
|
10
|
+
const DEFAULT_VAULT_KEY_PATH = '/api/vault-key'
|
|
11
|
+
|
|
12
|
+
export async function fetchVaultKeyRecord({
|
|
13
|
+
apiBaseUrl,
|
|
14
|
+
accessToken,
|
|
15
|
+
vaultKeyPath = DEFAULT_VAULT_KEY_PATH,
|
|
16
|
+
}) {
|
|
17
|
+
const res = await fetch(new URL(vaultKeyPath, apiBaseUrl), {
|
|
18
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
19
|
+
})
|
|
20
|
+
if (res.status === 404) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
'No vault found for this account. Create your vault in the web app first.',
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
if (!res.ok) {
|
|
26
|
+
throw new Error(`Failed to fetch vault key (HTTP ${res.status}).`)
|
|
27
|
+
}
|
|
28
|
+
const record = await res.json()
|
|
29
|
+
for (const field of ['rkWrappedByUk', 'ukSalt', 'ukIv', 'ukKdf']) {
|
|
30
|
+
if (!record?.[field]) {
|
|
31
|
+
throw new Error(`Vault key response is missing "${field}".`)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return record
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Fetch the vault key with the access token, then unwrap the root key with the
|
|
39
|
+
* vault password.
|
|
40
|
+
* @returns {Promise<Uint8Array>} the 32-byte vault root key
|
|
41
|
+
*/
|
|
42
|
+
export async function fetchAndUnlockVault({ apiBaseUrl, accessToken, password }) {
|
|
43
|
+
const record = await fetchVaultKeyRecord({ apiBaseUrl, accessToken })
|
|
44
|
+
return unlockRootKey({ password, vaultKey: record })
|
|
45
|
+
}
|