Why 3-of-3 instead of 2-of-3 or HSM-only
Shamir’s Secret Sharing (SSS), introduced by Adi Shamir in “How to Share a Secret” (Communications of the ACM, 1979), splits a secret into n shares so that any k of them reconstruct it, and any k − 1 reveal nothing about it. The pair (k, n) is the threshold scheme.
In the standard desktop-only setup, the operating-system keychain holds s1 and the gateway stores encrypted s2 with the persona-key record. The default enrollment response does not return s3 or a recovery-vault URL. If you explicitly choose a vault provider and provide a passphrase, s3 is uploaded to that provider. Nextcloud recovery was not tested end to end before launch. Test a restore before relying on it. The alternatives, and where they land:
- (2, 3): any two of the three shares recover the key. More forgiving, since losing one location still lets you recover, but weaker, because compromising any two locations is enough. Storage depends on the enrollment configuration described below.
- HSM-only, without SSS: an HSM prevents software extraction of key material, but it is a single point of failure and not something a user has on their own device. SSS can spread shares across storage locations; which locations are used depends on enrollment and can include a device keychain, the gateway’s encrypted share and an explicitly selected recovery vault.
- (2, 5) or larger schemes: more operational complexity without a clear gain for this threat model. Three factors map onto the three places a desktop client can hold key material.
The mathematical limit of a 3-of-3 scheme is that all three shares are needed to reconstruct the key; a 2-of-3 scheme tolerates one missing share. Whether a recovery vault is available depends on explicit enrollment and on the share locations that were configured.
The library landscape and where it falls short
Before writing anything, we looked at the existing libraries:
- PHP (tightenco/shamir, dsprenkels/tss): PHP Shamir implementations typically use GF(256) but differ in polynomial representation, byte order and how they handle multi-byte secrets (byte by byte or word by word). tightenco/shamir has not been updated since 2019 and uses a non-standard prime-field implementation that is incompatible with standard GF(2^8) and the AES polynomial.
- Rust (sharks, secret-sharing): sharks uses GF(2^8) with the AES irreducible polynomial (0x11b), produces correct output and has a published test-vector suite. The desktop tokenizer core is Rust, so this was the natural starting point. But its shares must be reassembled byte for byte by the recovery layer, a separate code path that may also re-import a vault share, so the encoding has to be identical everywhere. No off-the-shelf pairing gave us that on both sides.
- JavaScript (@privy-io/shamir-secret-sharing, secrets.js): secrets.js uses a configurable field size and does not default to GF(2^8). The @privy-io implementation targets GF(2^8) but encodes shares as hex strings in a custom envelope, which PHP cannot reconstruct at the byte level.
These libraries are not wrong; most are correct implementations of SSS. The problem is that “Shamir’s Secret Sharing” underspecifies the encoding. Two correct implementations can produce shares the other cannot reconstruct if they differ in the irreducible polynomial, the byte order, how multi-byte secrets are chunked, how each share’s x-coordinate is encoded, or whether share indices start at 0 or 1.
The PHP and Rust paths use a shared byte format, so a share produced by one can be combined by the other. The rest of this post describes that format and the choices that keep the two paths compatible.
GF(2^8) arithmetic: a walkthrough
Shamir’s Secret Sharing needs arithmetic in a finite field. The usual choice for byte-level SSS is GF(2^8), the field of 256 elements in which each element is one byte.
In GF(2^8), addition is XOR: carry-free addition modulo 2, applied bitwise. Multiplication is polynomial multiplication modulo an irreducible polynomial of degree 8 over GF(2). We use the same polynomial as AES:
p(x) = x^8 + x^4 + x^3 + x + 1 — binary: 0x11b (decimal: 283)This polynomial is well studied and widely implemented, so our test vectors can be cross-checked against AES GF(2^8) implementations.
Multiplication in GF(2^8)
Multiplying two elements a and b with the “Russian peasant” algorithm:
def gf_mul(a: int, b: int) -> int:
"""Multiply a and b in GF(2^8) with polynomial 0x11b."""
result = 0
while b:
if b & 1:
result ^= a # add current a to result (XOR = addition in GF(2))
a <<= 1 # multiply a by x
if a & 0x100:
a ^= 0x11b # reduce modulo p(x)
b >>= 1
return result & 0xffDivision in GF(2^8) is multiplication by the multiplicative inverse. In the in-tree fallback, the inverse is computed as a^254 by repeated squaring, using Fermat’s little theorem.
Secret splitting
To split a single-byte secret s into 3 shares with threshold 3:
- Choose two coefficients
a1, a2from GF(2^8) uniformly at random, using a CSPRNG. - Define the polynomial
f(x) = s + a1*x + a2*x^2over GF(2^8). - Evaluate it at x = 1, 2, 3. The shares are (1, f(1)), (2, f(2)) and (3, f(3)).
For a multi-byte secret, such as a 32-byte AES-256 key, repeat this for each byte. The x-coordinates are the same for every byte, and each byte gets its own polynomial. The result is three 32-byte shares plus their x-coordinates.
Degree 2 means exactly three coefficients: the secret, a1 and a2. A polynomial of degree k − 1 over GF(2^8) needs k evaluation points to reconstruct, which matches a threshold of k = 3.
Reconstruction with Lagrange interpolation
Given three shares (x1, y1), (x2, y2) and (x3, y3), the secret f(0) is recovered by Lagrange interpolation:
f(0) = Σ yi * Π (0 - xj)/(xi - xj) for j ≠ iAll arithmetic is in GF(2^8), where subtraction equals addition equals XOR, and division is multiplication by the inverse described above. Interpolation is O(k^2) per byte, which is negligible for any practical secret size.
Test vectors and cross-validation
We validated the implementation against:
- The test vectors published with the dsprenkels/sss C library, which specifies GF(2^8) with the same polynomial.
- An independent Python reference implementation, compared byte for byte with the production code on 10,000 randomly sampled secrets.
- Round-trip tests: thousands of random secrets split and reconstructed across every permutation of the share subsets.
The splitting and reassembly paths produce identical byte sequences on all of these vectors. Byte-for-byte compatibility was the main design constraint, and the test suite checks it.
How recovery works
Reconstruction runs on your device once enough shares have been retrieved. Where each share is stored depends on the enrollment configuration:
- Default desktop-only setup: the OS keychain holds s1 and the gateway stores encrypted s2. The default enrollment response does not provide s3 or configure a recovery-vault URL, so no recovery-vault share is enrolled.
- Explicit vault enrollment: if you choose a supported vault provider and provide a passphrase, s3 is uploaded to that provider. The gateway still stores encrypted s2 with the persona-key record.
The app then combines the number of shares the selected threshold requires, in memory, using the Lagrange interpolation above. A 3-of-3 scheme needs all three shares; a 2-of-3 scheme needs any two.
Limits of the scheme
Shamir’s Secret Sharing is information-theoretically secure: k − 1 shares reveal nothing about the secret. The threat model still has practical limits this scheme does not address:
- Simultaneous compromise: if one attacker compromises all the share locations your threshold requires at the same time, the key is exposed. Keeping the vault and passphrase apart from the device reduces that risk; it does not remove it.
- Passphrase loss: if a recovery share depends on a passphrase, losing the passphrase can make that share unavailable. Protect the vault credentials and backups.
- Lost factors: losing more shares than the threshold tolerates prevents reconstruction. The threshold decides how many shares are required.
- Storage practices: a vault backup or passphrase stored carelessly weakens the whole scheme. Check where each configured share lives: the default desktop-only enrollment has no recovery-vault URL, and the gateway stores encrypted s2.