harbor0APIBeta

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

Decrypting file names

On an encrypted account, the name of every file and folder is a token like this:

h0e1.B7r1nFttVaFQsM3mFgUm5BaBSKCnUpjrErbITFm8HVIX

The format

"h0e1." + base64url( iv ‖ ciphertext ‖ tag )
PartSize
h0e1.5 charactersThe format, version 1.
iv12 bytesThe AES-GCM IV.
ciphertextas long as the name in UTF-8The name, encrypted.
tag16 bytesThe AES-GCM tag.

The name was encrypted with AES-256-GCM using the name key (the first 32 bytes of the harbor0/e2ee/v1/names key from Unlocking the account key) and the associated data harbor0/e2ee/v1/name. The plaintext is the name in Unicode NFC, as UTF-8, at most 255 bytes.

A name is encrypted when it matches ^h0e1\.[A-Za-z0-9_-]+$. Anything else is a plain name: leave it as it is.

Decrypting

  1. Drop the h0e1. prefix and base64url-decode the rest.
  2. The first 12 bytes are the IV; the rest is ciphertext followed by the tag.
  3. Open it with AES-256-GCM, the name key and the associated data harbor0/e2ee/v1/name, then decode the result as UTF-8.
import { createDecipheriv } from 'node:crypto';

export function decryptName(keys, token) {
  const bytes = Buffer.from(token.slice('h0e1.'.length), 'base64url');
  const iv = bytes.subarray(0, 12);
  const sealed = bytes.subarray(12);
  const d = createDecipheriv('aes-256-gcm', keys.nameEnc, iv);
  d.setAAD(Buffer.from('harbor0/e2ee/v1/name'));
  d.setAuthTag(sealed.subarray(-16));
  return Buffer.concat([d.update(sealed.subarray(0, -16)), d.final()]).toString('utf8');
}

If a name fails to decrypt (the tag check fails), show the token as it is rather than stopping. That is what the harbor0 apps do too.

Item objects also carry normalizedName, a lower-cased copy of name the server uses for comparisons. On encrypted items it is not a valid token, so ignore it and decrypt name.

Encrypting a name

You need encrypted names to create folders, upload files, rename and move, and to look items up by name. Names are encrypted deterministically: the IV is made from the name itself, so the same name always gives the same token. That is how the server keeps names unique in a folder without reading them.

  1. Normalize the name to Unicode NFC and encode it as UTF-8. It must be at most 255 bytes.
  2. Compute HMAC-SHA256 of those bytes with the name MAC key (the last 32 bytes of the harbor0/e2ee/v1/names key). The first 12 bytes of the HMAC are the IV.
  3. Encrypt the name with AES-256-GCM, the name key, that IV and the associated data harbor0/e2ee/v1/name.
  4. The token is h0e1. followed by the base64url (no padding) of IV, ciphertext and tag.
import { createCipheriv, createHmac } from 'node:crypto';

export function encryptName(keys, name) {
  const plain = Buffer.from(name.normalize('NFC'), 'utf8');
  if (plain.length > 255) throw new Error('Names on encrypted accounts are at most 255 bytes.');
  const iv = createHmac('sha256', keys.nameMac).update(plain).digest().subarray(0, 12);
  const c = createCipheriv('aes-256-gcm', keys.nameEnc, iv);
  c.setAAD(Buffer.from('harbor0/e2ee/v1/name'));
  const sealed = Buffer.concat([c.update(plain), c.final(), c.getAuthTag()]);
  return 'h0e1.' + Buffer.concat([iv, sealed]).toString('base64url');
}

Because the HMAC covers the exact bytes, names that differ only in case encrypt differently. On encrypted accounts, Report.pdf and report.pdf can sit in the same folder.

Looking up a name

To find receipt.pdf in a folder, encrypt the name and look the token up:

NAME=$(node -e "…encryptName(keys, 'receipt.pdf')…")
curl -s "https://api.harbor0.com/v1/drive/folders/$FOLDER/lookup?name=$NAME" \
  -H "Authorization: Bearer $HARBOR0_TOKEN" -H 'X-Harbor-E2EE: h0e1'

While the account is still re-encrypting old files (migration.complete is false in GET /v1/e2ee), or while it is turning encryption off (decryptingSince is set), an item may still be stored under its plain name. In those states, if the encrypted lookup finds nothing, look up the plain name too. A plain name over 240 characters cannot exist, and the lookup answers 400 VALIDATION_ERROR for one; treat that as not found.

Check your work

The test vectors include five names. For example, with the vectors' master key, Taxes encrypts to h0e1.B7r1nFttVaFQsM3mFgUm5BaBSKCnUpjrErbITFm8HVIX, and that token decrypts back to Taxes.