Public Access
Groundwork for a device key, and a spec change rather than a feature. ADR 0007 records the decision it clears the way for: a Windows Hello gesture guarding a protected blob, with the passphrase kept as a permanent fallback. The reason that decision needed this first is that a device key cannot open a session on its own. SessionOpener derived two things from the passphrase master key — the bundle, and the local cache key — and a device wrap is SealTo(device_x25519_pk), which yields the bundle and never computes a master key at all. A device unlock could therefore have opened the identity and still not read the cache it had itself written. So LocalCacheKey now derives from the bundle: dsh1/localcache/v1 → v2, specified in crypto.md §3.2. Every wrap that opens a vault ends up holding the bundle, so every door reaches the same cache. Extract-and-expand, not expand alone. Everything derived from the master key uses HKDF-Expand directly, which is sound because an Argon2id output is uniformly random over its whole length. The bundle's encoding is not — it opens with a fixed 14-byte label and carries a version, a generation and a timestamp before reaching any key material — so it needs the extract step to become a pseudorandom key first. Two consequences fell out, both improvements and neither the point: - A passphrase change no longer discards the local cache. The bundle is unchanged by a re-wrap, so the cache key is too. Under v1 changing a passphrase silently orphaned every cached row and the next launch re-pulled the whole vault. - Recovery-code unlock is fixed before it ships. It derives a different master key from a different secret and a different salt, so under v1 it would have had the same defect as the device path, and nobody would have noticed until it landed. The cache becomes unreadable exactly when the identity is rotated, which is the correct moment to discard it. Existing caches are discarded and re-pulled on upgrade — already the specified behaviour for a stale cache, and the reason the label is versioned rather than reused: a v1 cache must fail to open rather than decrypt to nonsense. One stated guarantee got weaker and now says so. crypto.md §10 claimed locking meant "nothing on disk can be read again without the passphrase." Where a device wrap exists that is no longer true, and it would have been untrue under either candidate design — the alternative was storing a copy of the cache key in the device blob, which is the same door with an extra key lying next to it. The wording now points at ADR 0007, because what guards the device key is a platform decision and not a property of this specification. A golden vector was quietly lying, which is the part worth reading twice. The "local-cache" entry pinned HKDF-SHA512-Expand over a fixed PRK — a construction the cache key no longer uses. Regenerating it would have produced a green suite describing a derivation this code does not perform. It is replaced by a vector over a bundle whose every byte is pinned: the label, version 1, generation 1, a fixed timestamp and two recognisable key scalars, all visible in the fixture so a second implementation can check itself against it. UserSecretBundle.TryDecode is internal for this, because Create draws fresh randomness and so can never produce a reproducible input. Mutation tested, and this one earns its keep: dropping the extract step now fails CommittedVectors_MatchCurrentImplementation. The vector it replaced could not have caught that, because it never touched the bundle at all. One test became false and says so. ARecordSealedUnderAnotherPassphrase is now ARecordSealedByAnotherIdentity: a different passphrase deliberately no longer changes the cache key, and TheLocalCacheKey_SurvivesAPassphraseChange pins that. What must still be unreadable is another user's cache. CacheHarness therefore generates an identity rather than deriving from a passphrase, and has no passphrase parameter left — the cache key is not a question about passphrases any more. SyncHarness's two simulated machines now derive the same cache key, which is what keying on the bundle means: they are the same user holding the same identity. They still have separate cache databases, so nothing is shared between them but the key that would open either. Both harnesses lost a MasterKey field that existed only to make a protector. 858 tests green. Zero warnings, dotnet format clean. Not done: the device key itself. Three pieces remain, and the middle one was a discovery rather than a plan — EnrollmentService.AddDevice runs only during enrollment, so every already-enrolled account, which is all of them, needs an endpoint to add a device wrap while unlocked. The client proves possession by producing the wrap, so that shape falls out of the crypto. After that: the protector seam with the wrap cached locally for offline unlock, then the Hello implementation and the unlock-screen UI, which is where the Windows TFM lands and where automated testing stops.
143 lines
5.4 KiB
C#
143 lines
5.4 KiB
C#
using DodoSSH.Crypto;
|
|
|
|
namespace DodoSSH.Crypto.Tests;
|
|
|
|
/// <summary>
|
|
/// Pins the specification constants that are written into stored data.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// These are not busywork. The envelope magic, AAD version and every enum value are persisted
|
|
/// in ciphertext rows or in the AAD they are bound to, and only clients can re-encrypt: if one
|
|
/// changes without a deliberate migration path, existing vaults stop decrypting and the server
|
|
/// cannot help.
|
|
/// </remarks>
|
|
public sealed class CryptoSpecTests
|
|
{
|
|
[Fact]
|
|
public void EnvelopeMagic_IsStable()
|
|
{
|
|
CryptoSpec.EnvelopeMagic.ToArray().ShouldBe("DSH1"u8.ToArray());
|
|
}
|
|
|
|
[Fact]
|
|
public void AadMagic_IsStable()
|
|
{
|
|
CryptoSpec.AadMagic.ToArray().ShouldBe("dsh1\n"u8.ToArray());
|
|
}
|
|
|
|
[Fact]
|
|
public void CurrentAadVersion_IsStable()
|
|
{
|
|
// Bumping this requires a lazy re-encrypt-on-write path in the client first.
|
|
CryptoSpec.CurrentAadVersion.ShouldBe((byte)1);
|
|
}
|
|
|
|
[Fact]
|
|
public void CurrentSchemaVersion_IsStable()
|
|
{
|
|
CryptoSpec.CurrentSchemaVersion.ShouldBe((ushort)1);
|
|
}
|
|
|
|
[Fact]
|
|
public void Sizes_MatchTheSpecification()
|
|
{
|
|
CryptoSpec.AadEncodedLength.ShouldBe(64);
|
|
CryptoSpec.SymmetricKeySize.ShouldBe(32);
|
|
CryptoSpec.PublicKeySize.ShouldBe(32);
|
|
CryptoSpec.SignatureSize.ShouldBe(64);
|
|
CryptoSpec.DigestSize.ShouldBe(32);
|
|
CryptoSpec.TagSize.ShouldBe(16);
|
|
CryptoSpec.SaltSize.ShouldBe(16);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(CryptoSpec.AlgorithmId.XChaCha20Poly1305, 1)]
|
|
[InlineData(CryptoSpec.AlgorithmId.Aes256Gcm, 2)]
|
|
[InlineData(CryptoSpec.AlgorithmId.SealToX25519, 3)]
|
|
public void AlgorithmId_HasStableWireValue(CryptoSpec.AlgorithmId algorithm, int expected)
|
|
{
|
|
((int)algorithm).ShouldBe(expected);
|
|
}
|
|
|
|
[Fact]
|
|
public void AlgorithmId_4_IsReservedForHybridPostQuantumSeal()
|
|
{
|
|
// Reserved for X25519 + ML-KEM-768. Claimed now so the identifier cannot be reused:
|
|
// store-now-decrypt-later is a real threat for long-lived SSH keys.
|
|
Enum.IsDefined(typeof(CryptoSpec.AlgorithmId), (byte)4).ShouldBeFalse();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(CryptoSpec.AadPurpose.UserSecretBundle, 1)]
|
|
[InlineData(CryptoSpec.AadPurpose.VaultKeyGrant, 2)]
|
|
[InlineData(CryptoSpec.AadPurpose.ItemDataKey, 3)]
|
|
[InlineData(CryptoSpec.AadPurpose.ItemPayload, 4)]
|
|
[InlineData(CryptoSpec.AadPurpose.ItemMetadata, 5)]
|
|
[InlineData(CryptoSpec.AadPurpose.LocalCache, 6)]
|
|
public void AadPurpose_HasStableWireValue(CryptoSpec.AadPurpose purpose, int expected)
|
|
{
|
|
((int)purpose).ShouldBe(expected);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(CryptoSpec.AadResourceType.User, 1)]
|
|
[InlineData(CryptoSpec.AadResourceType.Device, 2)]
|
|
[InlineData(CryptoSpec.AadResourceType.Vault, 3)]
|
|
[InlineData(CryptoSpec.AadResourceType.Host, 4)]
|
|
[InlineData(CryptoSpec.AadResourceType.Credential, 5)]
|
|
[InlineData(CryptoSpec.AadResourceType.SshKey, 6)]
|
|
[InlineData(CryptoSpec.AadResourceType.HostGroup, 7)]
|
|
[InlineData(CryptoSpec.AadResourceType.Tag, 8)]
|
|
[InlineData(CryptoSpec.AadResourceType.Snippet, 9)]
|
|
[InlineData(CryptoSpec.AadResourceType.PortForward, 10)]
|
|
[InlineData(CryptoSpec.AadResourceType.KnownHostKey, 11)]
|
|
[InlineData(CryptoSpec.AadResourceType.HostTag, 12)]
|
|
[InlineData(CryptoSpec.AadResourceType.HostCredential, 13)]
|
|
public void AadResourceType_HasStableWireValue(CryptoSpec.AadResourceType type, int expected)
|
|
{
|
|
((int)type).ShouldBe(expected);
|
|
}
|
|
|
|
[Fact]
|
|
public void DerivationLabels_AreStable()
|
|
{
|
|
// These are HKDF info strings; changing one silently derives a different key.
|
|
CryptoSpec.DerivationLabels.PassphraseKek.ToArray()
|
|
.ShouldBe("dsh1/kek/passphrase/v1"u8.ToArray());
|
|
// v2 since 2026-07-30: the cache key derives from the bundle rather than the master key, so that a
|
|
// device or recovery unlock reaches the same cache. docs/crypto.md §3.2. Bumping the label is what
|
|
// makes a v1 cache fail to open rather than decrypt to nonsense.
|
|
CryptoSpec.DerivationLabels.LocalCache.ToArray()
|
|
.ShouldBe("dsh1/localcache/v2"u8.ToArray());
|
|
CryptoSpec.DerivationLabels.SealTo.ToArray()
|
|
.ShouldBe("dsh1/sealto/v1|"u8.ToArray());
|
|
CryptoSpec.DerivationLabels.Fingerprint.ToArray()
|
|
.ShouldBe("dsh1/fp/v1"u8.ToArray());
|
|
}
|
|
|
|
[Fact]
|
|
public void SigningContexts_AreStable()
|
|
{
|
|
CryptoSpec.SigningContexts.KeyStatement.ToArray()
|
|
.ShouldBe("dsh1/sig/keystatement/v1"u8.ToArray());
|
|
CryptoSpec.SigningContexts.Grant.ToArray()
|
|
.ShouldBe("dsh1/sig/grant/v1"u8.ToArray());
|
|
CryptoSpec.SigningContexts.Attestation.ToArray()
|
|
.ShouldBe("dsh1/sig/attestation/v1"u8.ToArray());
|
|
}
|
|
|
|
[Fact]
|
|
public void SigningContexts_AreAllDistinct()
|
|
{
|
|
// A shared context would let a signature in one role be replayed in another.
|
|
string[] contexts =
|
|
[
|
|
System.Text.Encoding.UTF8.GetString(CryptoSpec.SigningContexts.KeyStatement),
|
|
System.Text.Encoding.UTF8.GetString(CryptoSpec.SigningContexts.Grant),
|
|
System.Text.Encoding.UTF8.GetString(CryptoSpec.SigningContexts.Attestation),
|
|
];
|
|
|
|
contexts.Distinct(StringComparer.Ordinal).Count().ShouldBe(contexts.Length);
|
|
}
|
|
}
|