diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3e01f42..2233952 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,9 +3,30 @@ name: Build on: push: branches: [ main ] + pull_request: + branches: [ main ] workflow_dispatch: +# The workflow only needs to read the code. +permissions: + contents: read + jobs: + test: + runs-on: windows-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: '10.0.x' + + - name: Test (password known-answer tests) + run: dotnet test SecretKey.Core.Tests/SecretKey.Core.Tests.csproj --configuration Release + build: runs-on: windows-latest diff --git a/SecretKey.Core.Tests/CryptoTests.cs b/SecretKey.Core.Tests/CryptoTests.cs new file mode 100644 index 0000000..08dd304 --- /dev/null +++ b/SecretKey.Core.Tests/CryptoTests.cs @@ -0,0 +1,57 @@ +using System.Text; +using SecretKey.Core; + +namespace SecretKey.Core.Tests; + +/// +/// Known-answer tests for the building blocks in Crypto.cs. +/// If any of these fail, previously generated passwords can no longer be reproduced. +/// +public class CryptoTests +{ + [Fact] + public void MonthlyMasterKey_IsSha256OfRootKeyTextPlusDateCode() + { + byte[] key = Crypto.DeriveMonthlyMasterKey(V1TestHelper.TestRootKey, "2611"); + + Assert.Equal("4E0F0B5B77491DCA67A584A58EC1F6D4859775346265F8C03B51878FE2CF8A12", Convert.ToHexString(key)); + } + + [Fact] + public void HmacSha256_MatchesRfc4231TestCase2() + { + byte[] mac = Crypto.HmacSha256(Encoding.ASCII.GetBytes("Jefe"), Encoding.ASCII.GetBytes("what do ya want for nothing?")); + + Assert.Equal("5BDCC146BF60754E6A042426089575C75A003F089D2739839DEC58B964EC3843", Convert.ToHexString(mac)); + } + + [Fact] + public void HmacSha256_OfAccountBlock_IsStable() + { + byte[] key = Crypto.DeriveMonthlyMasterKey(V1TestHelper.TestRootKey, "2611"); + var block = new byte[32]; // v1: title + url + username, UTF-8, cut or zero-padded to 32 bytes + byte[] text = Encoding.UTF8.GetBytes("Bank" + "https://bank.example" + "ed"); + Array.Copy(text, block, text.Length); + + Assert.Equal("0F803F563CA1F44777139CCC4BB04B81DA0373509D3D28FC76E896BB234AD5C9", Convert.ToHexString(Crypto.HmacSha256(key, block))); + } + + [Fact] + public void MapBlobToPattern_MapsEachMaskTokenFromOneHashByte() + { + byte[] bytes = Enumerable.Range(0, 32).Select(i => (byte)i).ToArray(); + + // X -> A-Z, x -> a-z, N -> 0-9, S -> "!@#$%&*()-_=+[]{}<>?", anything else is copied as-is. + // A literal still uses up a hash byte (v1 behavior). + Assert.Equal("Ab2$-Fg7)", Crypto.MapBlobToPattern(bytes, "XxNS-XxNS")); + } + + [Fact] + public void MapBlobToPattern_WrapsAfter32Characters_V1Behavior() + { + byte[] bytes = Enumerable.Range(0, 32).Select(i => (byte)i).ToArray(); + + // Known v1 weakness: mask positions 33+ reuse the same 32 hash bytes. + Assert.Equal("0123456789012345678901234567890101234567", Crypto.MapBlobToPattern(bytes, new string('N', 40))); + } +} diff --git a/SecretKey.Core.Tests/PasswordDerivationV1Tests.cs b/SecretKey.Core.Tests/PasswordDerivationV1Tests.cs new file mode 100644 index 0000000..fb26de1 --- /dev/null +++ b/SecretKey.Core.Tests/PasswordDerivationV1Tests.cs @@ -0,0 +1,110 @@ +using static SecretKey.Core.Tests.V1TestHelper; + +namespace SecretKey.Core.Tests; + +/// +/// Locks the passwords SecretKey v1.0.0 produces, end to end (CSV in, CSV out). +/// +/// Why this exists: the whole point of SecretKey is that the same root key and date code +/// give the same passwords forever. If any test here fails, a code change has altered +/// users' passwords. Do not "fix" the expected values to make a test pass — change the +/// code back, or put the new behavior behind an explicit, opt-in new version. +/// +public class PasswordDerivationV1Tests +{ + [Theory] + [InlineData("Bhphotovideo", "https://www.bhphotovideo.com", "email@sample.com", DefaultMask, "2611", + "Bhphotovideo,https://www.bhphotovideo.com,email@sample.com,Wocqe8 + + + net10.0 + enable + enable + false + + + + + + + + + + + + + + + + + + + + + + diff --git a/SecretKey.Core.Tests/V1TestHelper.cs b/SecretKey.Core.Tests/V1TestHelper.cs new file mode 100644 index 0000000..3e018d0 --- /dev/null +++ b/SecretKey.Core.Tests/V1TestHelper.cs @@ -0,0 +1,57 @@ +using SecretKey.Core; + +namespace SecretKey.Core.Tests; + +/// +/// Shared setup for the v1 known-answer tests. +/// Every expected value in this project was produced by the v1.0.0 engine and +/// cross-checked against an independent implementation of the same algorithm. +/// +internal static class V1TestHelper +{ + // TEST-ONLY root key. Never use this for real passwords. + public const string TestRootKey = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"; + + public const string DefaultMask = "XxxxxNSxxxNN"; + public const string DateCode = "2611"; + + public const string InputHeader = "Title,Url,Username,Password,OTPAuth,Favorite,Archived,Tags,Notes"; + public const string OutputHeader = "Title,Url,Username,Password"; + + /// Builds an unquoted input row in the 1Password-style column order the engine reads. + public static string Row(string title, string url, string username) => + $"{title},{url},{username},,,false,false,,"; + + /// + /// Runs the real engine on the given input rows in a private temp folder and returns the output lines. + /// (The engine rewrites its input file in place, so it must never be pointed at a shared file.) + /// + public static string[] Run(IEnumerable inputRows, string mask = DefaultMask, string dateCode = DateCode) + { + string dir = Path.Combine(Path.GetTempPath(), "secretkey-tests-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(dir); + try + { + string input = Path.Combine(dir, "input.csv"); + string output = Path.Combine(dir, "output.csv"); + File.WriteAllLines(input, new[] { InputHeader }.Concat(inputRows)); + + Processor.Process(input, output, Crypto.DeriveMonthlyMasterKey(TestRootKey, dateCode), mask); + + return File.ReadAllLines(output); + } + finally + { + Directory.Delete(dir, recursive: true); + } + } + + /// Runs one account through the engine and returns its output line. + public static string OutputLineFor(string title, string url, string username, string mask = DefaultMask, string dateCode = DateCode) + { + string[] lines = Run(new[] { Row(title, url, username) }, mask, dateCode); + Assert.Equal(2, lines.Length); + Assert.Equal(OutputHeader, lines[0]); + return lines[1]; + } +} diff --git a/SecretKey.slnx b/SecretKey.slnx index d51ec9d..a612515 100644 --- a/SecretKey.slnx +++ b/SecretKey.slnx @@ -8,6 +8,7 @@ +