Crypto: primitives and key derivation
Every export of primitives.ts and seedkey.ts: BIP-39 and SLIP-10 derivation, Ed25519, account id encoding, base64, HKDF, XChaCha20-Poly1305, UUIDs, wire timestamps and the .seedkey format.

src/crypto/primitives.ts is the bottom layer of the app's cryptography: thin, exact wrappers over @noble/curves, @noble/hashes, @noble/ciphers, @scure/bip39 and multiformats (Dependencies). seedkey.ts adds the desktop key file format.

Key derivation

Function

Takes

Returns

generateMnemonic(entropy)

exactly 16 bytes, supplied by the caller

12 English BIP-39 words; throws on any other length

validateMnemonic(mnemonic)

a phrase

true when the words and checksum are valid

keyPairFromMnemonic(mnemonic, passphrase = '')

phrase, optional passphrase

KeyPair (synchronous)

keyPairFromMnemonicAsync(...)

same

the same KeyPair, yielding between rounds

keyPairFromPrivateKey(seed)

a 32-byte Ed25519 seed

KeyPair, with the seed copied

KeyPair is {privateKey, publicKey, accountId}: the raw 32-byte seed, the raw 32-byte public key, and the account id string.

The algorithm, "matching RN and Go":

    BIP-39 seed: PBKDF2-HMAC-SHA512, 2048 rounds, salt "mnemonic" + passphrase, one 64-byte block.

    SLIP-10 master: HMAC-SHA512(key = "ed25519 seed", seed).

    Three hardened steps along m/44'/104109'/0': HMAC-SHA512(chainCode, 0x00 || key || uint32be(index + 0x80000000)).

    The first 32 bytes are the Ed25519 seed.

The async variant re-implements PBKDF2 by hand so it can call yieldCryptoTurn() every 64 rounds. It normalizes to NFKD and zeroes its buffers in a finally. Sources that import a phrase must use it (see Me: sources).

yieldCryptoTurn() awaits globalThis.scheduler?.yield(). On Hermes that starts a fresh turn of the 100 ms turn budget; under Bun, where scheduler is absent, it continues at once.

Signing and account ids

"Principal" and "account id" are defined in the Glossary.

Randomness, hashing and base64

    randomBytes(n): crypto.getRandomValues. The comment adds that "the caller supplies time separately".

    sha256: re-exported from @noble/hashes.

    b64url: URL-safe, no padding on encode (Go RawURLEncoding). b64std: standard alphabet with padding (Go JSON []byte). Both decoders trim whitespace and throw Invalid base64 on a wrong alphabet or impossible length. base64urlEncode and base64urlDecode are aliases.

Vault keys and encryption

Constant or function

Value

SECRET_AUTH_KEY_INFO

seed-hypermedia-vault-secret-authentication

VAULT_SECRET_SIZE

32 bytes (remote secrets and local wrapping keys)

DEK_SIZE

64 bytes (the AEAD uses the first 32)

deriveSecretCredentialAuthKey(secret)

HKDF-SHA256, no salt, the info string above, 32 bytes out; throws unless the secret is 32 bytes

vaultAuthorization(credentialId, secret)

Bearer <id>:<b64url(authKey)>

encrypt(plaintext, key, nonce?)

XChaCha20-Poly1305; the output is the 24-byte nonce, then the ciphertext, then the 16-byte tag. Random nonce by default

decrypt(data, key)

authenticates and decrypts; throws on a wrong key or altered bytes

Both AEAD functions take key.subarray(0, 32), which is how a 64-byte data key works. These are the building blocks of the vault codec and the bearer used against the vault server.

UUIDs and timestamps

    randomUUID(entropy?): RFC 4122 version 4 from 16 bytes.

    wireTimestamp(ms): floors to whole milliseconds and throws on a negative or non-finite value. The comment explains why: Exact clocks "are fractional, while RN signs Date.now() integers and BigInt() throws on fractions". Every blob and envelope builder passes its time through this.

The .seedkey file

buildSeedKeyExport({seed, createTime}) returns the desktop format: two-space indented JSON {createTime, publicKey, keyB64} plus a newline, where createTime is RFC 3339, publicKey the account id and keyB64 the seed in unpadded base64url. parseSeedKeyImport(json) returns {seed, publicKey} and throws on invalid JSON, a missing or malformed keyB64, a seed that is not 32 bytes, a publicKey that does not match, or an encryption field. The last case reports ENCRYPTED_SEEDKEY_ERROR: password-protected exports need Argon2id, "which Hermes cannot run".

The running app uses the equivalent functions in Identity; this copy exists for byte parity with the earlier client and is exercised only by tests.

Tests

crypto.test.ts: 13 derivation and account tests, 2 seedkey tests, the base64, HKDF (also checked against WebCrypto) and AEAD tests, 1 UUID test and 4 async-derivation tests. review.test.ts: 3 tests on fractional and non-finite clocks. See Parity vectors.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime

Comments