Unlocking the account key
Before you can read anything on an encrypted account, you unwrap its master key and derive the working keys from it. This happens once per run.
1. Fetch the wrapped keys
curl -s https://api.harbor0.com/v1/e2ee -H "Authorization: Bearer $HARBOR0_TOKEN"{
"e2ee": {
"enabled": true,
"enabledAt": "2026-10-25T08:14:51.120Z",
"passphrase": {
"v": 1,
"kdf": "argon2id",
"salt": "igLzw4VoOBRJyOOVhOfttQ",
"memoryKiB": 65536,
"iterations": 3,
"parallelism": 1,
"iv": "mo69IvzDurQ2suFq",
"ciphertext": "hdN0WYKM4_0sS4tW37mxPHl37Vdj6oGM30hMM2q29TZhbsT01aPeW-p379fNoDlL"
},
"recovery": {
"v": 1,
"kdf": "hkdf",
"salt": "ocTg9-NihaoRTkEsnaOKig",
"iv": "Fll0YBxs2KWqufrp",
"ciphertext": "xVrmbUqo9IgxysogM4kuZb2wk0QgILybjSp_k1IkhosC6JS_naHmWLb8ToNcmBVv"
},
"devices": [],
"migration": { "totalFiles": 412, "doneFiles": 412, "complete": true, "…": "…" },
"decryptingSince": null,
"…": "…"
}
}passphrase and recovery are envelopes: the same master key, wrapped two ways. All their binary fields are base64url without padding. devices is always empty for a token, because each app's own copy is for that app alone.
2. Unwrap the master key
With the passphrase
- Normalize the passphrase to Unicode NFKC and encode it as UTF-8.
- Run Argon2id over it with the envelope's
salt,iterations,memoryKiBandparallelism, producing 32 bytes. Use the values from the envelope rather than fixed ones: they can differ between accounts. - Use those 32 bytes as an AES-256-GCM key to open
ciphertextwith the envelope's 12-byteivand the associated dataharbor0/e2ee/v1/master(the ASCII text). The last 16 bytes ofciphertextare the GCM tag. - The result must be exactly 32 bytes: the master key.
A wrong passphrase makes the GCM tag check fail. Report it as a wrong passphrase.
With the recovery key
A recovery key is 20 random bytes, written as 32 characters of Crockford base32 in groups of four: S33W-DHE4-RF1C-3G5Z-QTYV-SEXT-Q6WB-FDNN.
- Upper-case it and remove spaces and dashes. Read
Oas0,IandLas1, andUasV, as people often mistype those. - Check that 32 characters from
0123456789ABCDEFGHJKMNPQRSTVWXYZremain, and decode them, 5 bits per character, most significant first, into 20 bytes. - Derive a key: HKDF-SHA256 with the 20 bytes as input, the envelope's
saltas salt,harbor0/e2ee/v1/recoveryas info, and 32 bytes of output. - Open
ciphertextwith that key exactly as in step 3 above (same associated data,harbor0/e2ee/v1/master).
import { hkdfSync, createDecipheriv } from 'node:crypto';
import { argon2id } from 'hash-wasm'; // npm install hash-wasm
const b64 = (s) => Buffer.from(s, 'base64url');
function open(key, iv, sealed, aad) {
const d = createDecipheriv('aes-256-gcm', key, iv);
d.setAAD(aad);
d.setAuthTag(sealed.subarray(-16));
return Buffer.concat([d.update(sealed.subarray(0, -16)), d.final()]); // throws on a wrong key
}
const MASTER_AAD = Buffer.from('harbor0/e2ee/v1/master');
export async function unlockWithPassphrase(envelope, passphrase) {
const kek = await argon2id({
password: passphrase.normalize('NFKC'),
salt: b64(envelope.salt),
iterations: envelope.iterations,
memorySize: envelope.memoryKiB,
parallelism: envelope.parallelism,
hashLength: 32,
outputType: 'binary',
});
return open(Buffer.from(kek), b64(envelope.iv), b64(envelope.ciphertext), MASTER_AAD);
}
export function unlockWithRecovery(envelope, recoveryKey) {
const recovery = parseRecoveryKey(recoveryKey); // 20 bytes, see the full example
const kek = Buffer.from(
hkdfSync('sha256', recovery, b64(envelope.salt), 'harbor0/e2ee/v1/recovery', 32),
);
return open(kek, b64(envelope.iv), b64(envelope.ciphertext), MASTER_AAD);
}import base64, unicodedata
from argon2.low_level import Type, hash_secret_raw # pip install argon2-cffi
from cryptography.hazmat.primitives import hashes # pip install cryptography
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
def b64(s): return base64.urlsafe_b64decode(s + "=" * (-len(s) % 4))
MASTER_AAD = b"harbor0/e2ee/v1/master"
def unlock_with_passphrase(envelope, passphrase):
kek = hash_secret_raw(
secret=unicodedata.normalize("NFKC", passphrase).encode(),
salt=b64(envelope["salt"]),
time_cost=envelope["iterations"],
memory_cost=envelope["memoryKiB"],
parallelism=envelope["parallelism"],
hash_len=32,
type=Type.ID,
)
# Raises cryptography.exceptions.InvalidTag on a wrong passphrase.
return AESGCM(kek).decrypt(b64(envelope["iv"]), b64(envelope["ciphertext"]), MASTER_AAD)
def unlock_with_recovery(envelope, recovery_key):
recovery = parse_recovery_key(recovery_key) # 20 bytes, see the full example
kek = HKDF(algorithm=hashes.SHA256(), length=32, salt=b64(envelope["salt"]),
info=b"harbor0/e2ee/v1/recovery").derive(recovery)
return AESGCM(kek).decrypt(b64(envelope["iv"]), b64(envelope["ciphertext"]), MASTER_AAD)The recovery key parser is in the complete examples.
3. Derive the working keys
Everything else uses keys derived from the master key with HKDF-SHA256, an empty salt (which HKDF treats as 32 zero bytes) and these info strings:
| Key | info | Length | Used for |
|---|---|---|---|
| File-wrapping key | harbor0/e2ee/v1/file-keys | 32 bytes | Unwrapping each file version's content key (Decrypting files) |
| Name keys | harbor0/e2ee/v1/names | 64 bytes | Bytes 0–31: AES-256-GCM key for names. Bytes 32–63: HMAC-SHA256 key for name IVs (Decrypting file names) |
const hkdf = (master, info, length) =>
Buffer.from(hkdfSync('sha256', master, Buffer.alloc(0), info, length));
export function accountKeys(master) {
const names = hkdf(master, 'harbor0/e2ee/v1/names', 64);
return {
fileWrap: hkdf(master, 'harbor0/e2ee/v1/file-keys', 32),
nameEnc: names.subarray(0, 32),
nameMac: names.subarray(32, 64),
};
}def hkdf(master, info, length):
return HKDF(algorithm=hashes.SHA256(), length=length, salt=None, info=info.encode()).derive(master)
class AccountKeys:
def __init__(self, master):
names = hkdf(master, "harbor0/e2ee/v1/names", 64)
self.file_wrap = hkdf(master, "harbor0/e2ee/v1/file-keys", 32)
self.name_enc, self.name_mac = names[:32], names[32:]Check your work
Use the test vectors: their passphrase correct horse fi battery (note the fi ligature, which NFKC turns into fi) and recovery key S33W-DHE4-RF1C-3G5Z-QTYV-SEXT-Q6WB-FDNN must both unwrap to the master key AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyA (the bytes 1 to 32).