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)| Bytes | Field | |
|---|---|---|
| 0–3 | Magic | The ASCII bytes H0E1. |
| 4 | Version | 1. |
| 5–7 | Reserved | Zero. |
| 8–11 | Chunk size | Unsigned 32-bit, big-endian. 4194304 (4 MiB) in files the apps write. |
| 12–19 | Nonce prefix | 8 random bytes, the start of every chunk's nonce. |
| 20–31 | Wrap IV | 12 random bytes, the IV for unwrapping the content key. |
| 32–79 | Wrapped content key | The 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:
1if this is the last chunk,0otherwise. - 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);
}import math, struct
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
HEADER, TAG = 80, 16
def decrypt_file(keys, stored: bytes) -> bytes:
"""Decrypts a whole stored file held in memory."""
header = stored[:HEADER]
if header[:4] != b"H0E1" or header[4] != 1:
raise ValueError("Not an h0e1 encrypted file.")
bound = header[:20]
chunk_size = struct.unpack(">I", header[8:12])[0]
content_key = AESGCM(keys.file_wrap).decrypt(header[20:32], header[32:80], bound)
body = len(stored) - HEADER
plain_total = body - TAG * math.ceil(body / (chunk_size + TAG))
count = max(1, math.ceil(plain_total / chunk_size))
out, at = [], HEADER
for i in range(count):
last = i == count - 1
length = min(chunk_size, plain_total - i * chunk_size) + TAG
nonce = header[12:20] + struct.pack(">I", i)
aad = bound + (b"\x01" if last else b"\x00")
out.append(AESGCM(content_key).decrypt(nonce, stored[at:at + length], aad))
at += length
return b"".join(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):
- Read the header (stored bytes 0–79) once and unwrap the content key.
- The first chunk you need is
first = floor(begin / C)and the last islast = ceil(end / C) − 1. - Fetch stored bytes from
H + first × (C + T)up tomin(stored size, H + (last + 1) × (C + T))with an HTTPRangerequest on the download link. - Decrypt chunks
firsttolast(remembering which chunk is the file's last), join them, and keep bytesbegin − first × Conward,end − beginbytes long.
This is how the harbor0 apps stream videos and how rclone mounts encrypted folders.