harbor0APIBeta

Beta. The harbor0 API is still in beta, so routes and responses may change before it is final. What beta means

Decrypting files

An encrypted file is stored as an 80-byte header followed by the content, encrypted in chunks of 4 MiB. Each version of a file has its own random content key, wrapped inside its header, so you can decrypt any file with the account's file-wrapping key and nothing else.

Downloading

Download the stored bytes exactly as for any file (see Downloading files):

POST /v1/downloads
X-Harbor-E2EE: h0e1

{ "driveItemId": "83439bc4-…" }
{ "downloadUrl": "https://…", "sizeBytes": 9437313, "contentHash": "…", "encrypted": true }

When encrypted is true, sizeBytes and contentHash describe the stored (encrypted) bytes. Check the SHA-256 of what you download against contentHash, then decrypt.

The format

offset  0      4   5      8            12             20         32                      80
        ┌──────┬───┬──────┬────────────┬──────────────┬──────────┬───────────────────────┐
header  │ H0E1 │ 1 │ 0 0 0│ chunk size │ nonce prefix │ wrap IV  │ wrapped content key   │
        └──────┴───┴──────┴────────────┴──────────────┴──────────┴───────────────────────┘
        └──────────── bytes 0–19: associated data ────┘
then    chunk 0 │ chunk 1 │ … │ last chunk        (each: up to 4 MiB of content + 16-byte tag)
BytesField
0–3MagicThe ASCII bytes H0E1.
4Version1.
5–7ReservedZero.
8–11Chunk sizeUnsigned 32-bit, big-endian. 4194304 (4 MiB) in files the apps write.
12–19Nonce prefix8 random bytes, the start of every chunk's nonce.
20–31Wrap IV12 random bytes, the IV for unwrapping the content key.
32–79Wrapped content keyThe 32-byte content key encrypted with AES-256-GCM, then its 16-byte tag.

After the header come the chunks. Every chunk except the last holds exactly chunk size bytes of the original file, encrypted, plus a 16-byte tag. The last chunk holds the rest (which can be zero bytes) plus its tag. An empty file is a header and one 16-byte chunk.

Step 1: unwrap the content key

Check the magic and version, then open bytes 32–79 with AES-256-GCM, using the file-wrapping key from Unlocking the account key, the wrap IV from bytes 20–31, and bytes 0–19 of the header as associated data. The result is the file's 32-byte content key.

Binding bytes 0–19 means nobody can swap in another file's header pieces or change the chunk size without the unwrap failing.

Step 2: decrypt each chunk

For chunk number i (counting from 0):

  • Nonce (12 bytes): the 8-byte nonce prefix from the header, then i as an unsigned 32-bit big-endian number.
  • Associated data (21 bytes): bytes 0–19 of the header, then one byte: 1 if this is the last chunk, 0 otherwise.
  • Key: the content key.

Open the chunk with AES-256-GCM. If any tag check fails, the file is damaged or was tampered with; stop and report it.

The last-chunk byte is what stops someone from cutting a file short: a chunk that was sealed as "not last" will not open as the last one, and the other way round. Always work out which chunk is last from the stored size, as shown next.

Sizes

With H = 80 header bytes, T = 16 tag bytes and C = the chunk size:

chunks(plain)  = max(1, ceil(plain / C))
stored(plain)  = H + plain + T × chunks(plain)
plain(stored)  = (stored − H) − T × ceil((stored − H) / (C + T))

So a 9,437,185-byte file (3 chunks) is stored as 9,437,313 bytes, and plain(9437313) gives back 9,437,185. Folder listings report the stored size for encrypted items, so use plain(sizeBytes) to show real sizes.

Putting it together

import { createDecipheriv } from 'node:crypto';

const HEADER = 80, TAG = 16;

function open(key, iv, sealed, aad) {
  const d = createDecipheriv('aes-256-gcm', key, iv);
  d.setAAD(aad);
  d.setAuthTag(sealed.subarray(-TAG));
  return Buffer.concat([d.update(sealed.subarray(0, -TAG)), d.final()]);
}

/** Decrypts a whole stored file held in memory. */
export function decryptFile(keys, stored) {
  const header = stored.subarray(0, HEADER);
  if (header.toString('latin1', 0, 4) !== 'H0E1' || header[4] !== 1)
    throw new Error('Not an h0e1 encrypted file.');
  const bound = header.subarray(0, 20);
  const chunkSize = header.readUInt32BE(8);
  const contentKey = open(keys.fileWrap, header.subarray(20, 32), header.subarray(32, 80), bound);

  const body = stored.length - HEADER;
  const plainTotal = body - TAG * Math.ceil(body / (chunkSize + TAG));
  const count = Math.max(1, Math.ceil(plainTotal / chunkSize));
  const out = [];
  let at = HEADER;
  for (let i = 0; i < count; i++) {
    const last = i === count - 1;
    const length = Math.min(chunkSize, plainTotal - i * chunkSize) + TAG;
    const nonce = Buffer.alloc(12);
    header.copy(nonce, 0, 12, 20);
    nonce.writeUInt32BE(i, 8);
    const aad = Buffer.concat([bound, Buffer.from([last ? 1 : 0])]);
    out.push(open(contentKey, nonce, stored.subarray(at, at + length), aad));
    at += length;
  }
  return Buffer.concat(out);
}

These functions hold the whole file in memory, which is fine for documents and photos. For large files, decrypt as the download streams in: read the header first, then read and decrypt one chunk size + 16 piece at a time. The complete examples include a streaming decrypter for each language, used by their get command.

Reading part of a file

Because each chunk stands alone, you can decrypt any range of the original file without downloading all of it. To read original bytes [begin, end):

  1. Read the header (stored bytes 0–79) once and unwrap the content key.
  2. The first chunk you need is first = floor(begin / C) and the last is last = ceil(end / C) − 1.
  3. Fetch stored bytes from H + first × (C + T) up to min(stored size, H + (last + 1) × (C + T)) with an HTTP Range request on the download link.
  4. Decrypt chunks first to last (remembering which chunk is the file's last), join them, and keep bytes begin − first × C onward, end − begin bytes long.

This is how the harbor0 apps stream videos and how rclone mounts encrypted folders.