示例#1
0
        public void Should_ProveTheProofOfSuccess_For_SpecifiedListOfParameters()
        {
            var phe = new PheCrypto();

            var skS = phe.DecodeSecretKey(Bytes.FromString("I4ETKFzr3QmUu+Olhp1L2KvRgjfseO530R/A+aQ80Go=", StringEncoding.BASE64));
            var c0  = Bytes.FromString("BKAH5Rww+a9+8lzO3oYE8zfz2Oxp3+Xrv2jp9EFGRlS7bWU5iYoWQHZqrkM+UYq2TYON3Gz8w3mzLSy3yS0XlJw=", StringEncoding.BASE64);
            var c1  = Bytes.FromString("BOkgpxdavc+CYqmeKboBPhEjgXCEKbb5HxFeJ1+rVoOm006Jbu5a/Ol4rK5bVbkhzGAT+gUZaodUsnEuucfIbJc=", StringEncoding.BASE64);

            var proof = new ProofOfSuccess
            {
                Term1  = Bytes.FromString("BJkkIassffljoINliNRNcR7J/PmYcAl9PPUBeIT2I6oBy6wKb5gZsIOchWEN5pNfLVPH1V0BuhhlTi7MZeBlPOY=", StringEncoding.BASE64),
                Term2  = Bytes.FromString("BIiZfmmzuffwmJmibNnpws9D5SkgkPMBYxbgaWS1274kbJKC7WakUp1Mzk9BWvah0kDomJhqzyV7/8ZBX1rGX9I=", StringEncoding.BASE64),
                Term3  = Bytes.FromString("BCBcx9GsjQcoKI0pW4t49WWS/Z1bmg62KlCjAzB1IsKVDYQAT4213UuIMBthNnxSOVUUHikZCUw01mX5XKGQD5A=", StringEncoding.BASE64),
                BlindX = Bytes.FromString("KFiLnVVliWdm3fdYcuFK1sTiw1hvbKSesy4sGlYO8Rs=", StringEncoding.BASE64)
            };

            var skC = phe.DecodeSecretKey(Bytes.FromString("gPdKsQRz9Vmc/DnbfxCHUioU6omEa0Sg7pncSHOhA7I=", StringEncoding.BASE64));

            var nS  = Bytes.FromString("POKVRG0nmZc9062v41TNFngibsgMKzt/BY6lZ/5pcZg=", StringEncoding.BASE64);
            var pkS = phe.ExtractPublicKey(skS);

            var isValid = phe.ValidateProofOfSuccess(proof, pkS, nS, c0, c1);

            Assert.True(isValid);
        }
示例#2
0
        public void Should_EnrollNewRecord_When_PasswordSpecified()
        {
            var rngMock = Substitute.For <IPheRandomGenerator>();
            var offset  = 0;

            rngMock.GenerateNonce(16).Returns(x =>
            {
                offset += 16;
                return(((Span <byte>) this.mockedRandomBytes).Slice(offset - 16, 16).ToArray());
            });
            rngMock.GenerateNonce(32).Returns(x =>
            {
                offset += 32;
                return(((Span <byte>) this.mockedRandomBytes).Slice(offset - 32, 32).ToArray());
            });

            var crypto = new PheCrypto();

            crypto.Rng = rngMock;

            var enrollmentRecordRight = EnrollmentRecord.Parser.ParseFrom(Google.Protobuf.ByteString.CopyFrom(this.enrollmentRecord));
            var appSecretKey          = crypto.DecodeSecretKey(this.clientPrivate);

            var servicePublicKey = crypto.DecodePublicKey(this.serverPublic);
            var pheClient        = new PheClient(appSecretKey, servicePublicKey);

            pheClient.Crypto = crypto;

            var(enrollmentRec, key) = pheClient.EnrollAccount(this.password, this.enrollmentResponse);
            var enrollmentRecordGot = EnrollmentRecord.Parser.ParseFrom(Google.Protobuf.ByteString.CopyFrom(enrollmentRec));

            Assert.Equal(Bytes.ToString(this.enrollmentRecord, StringEncoding.BASE64), Bytes.ToString(enrollmentRec, StringEncoding.BASE64));
            Assert.Equal(Bytes.ToString(this.recordKey, StringEncoding.BASE64), Bytes.ToString(key, StringEncoding.BASE64));
        }
        public void TestRotatePrivateClientKey()
        {
            var phe = new PheCrypto();
            var oldClientPrivateKey = phe.DecodeSecretKey(this.clientPrivate);
            var newClientSecretKey  = phe.RotateSecretKey(oldClientPrivateKey, this.token);

            Assert.Equal(this.rotatedClientSk, newClientSecretKey.Encode());
        }
        public void Should_ComputeIdenticalC0AndC1Values_When_TheSameKeyAndNonceArePassed()
        {
            var phe   = new PheCrypto();
            var skS   = phe.DecodeSecretKey(this.clientPrivate);
            var nonce = phe.GenerateNonce();

            var(c0, c1)   = phe.ComputeC(skS, nonce);
            var(c00, c11) = phe.ComputeC(skS, nonce);

            Assert.Equal(Bytes.ToString(c0, StringEncoding.HEX), Bytes.ToString(c00, StringEncoding.HEX));
            Assert.Equal(Bytes.ToString(c1, StringEncoding.HEX), Bytes.ToString(c11, StringEncoding.HEX));
        }
示例#5
0
        public void Should_ComputeTheSameC0Value_For_SpecifiedListOfParameters()
        {
            var phe = new PheCrypto();

            var pwd = Bytes.FromString("passw0rd");
            var skC = phe.DecodeSecretKey(Bytes.FromString("gPdKsQRz9Vmc/DnbfxCHUioU6omEa0Sg7pncSHOhA7I=", StringEncoding.BASE64));
            var nC  = Bytes.FromString("CTnPcIn2xcz+4/9jjLuFZNJiWLBiohnaGVTb3X1zPt0=", StringEncoding.BASE64);
            var c0  = Bytes.FromString("BKAH5Rww+a9+8lzO3oYE8zfz2Oxp3+Xrv2jp9EFGRlS7bWU5iYoWQHZqrkM+UYq2TYON3Gz8w3mzLSy3yS0XlJw=", StringEncoding.BASE64);
            var t0  = Bytes.FromString("BPGwrvLl3CAolVo1RIoNT7TqTC3T66eLykxa/Vw1gyof+3scfaiqTAAQXQ57q6zEkEeJHjNl4GBghQceI/UNl7c=", StringEncoding.BASE64);

            var c00 = phe.ComputeC0(skC, pwd, nC, t0);

            Assert.Equal(c0, c00);
        }
示例#6
0
        public void Should_ComputeIdenticalC0AndC1Values_When_TheSameKeyAndNonceArePassed()
        {
            var phe = new PheCrypto();

            var skS = phe.DecodeSecretKey(Bytes.FromString("I4ETKFzr3QmUu+Olhp1L2KvRgjfseO530R/A+aQ80Go=", StringEncoding.BASE64));
            var nS  = Bytes.FromString("4g1N7hTxVWEHesoGZ5eTwbufdSnRtzxzkEQaBkXWsL4=", StringEncoding.BASE64);
            var c0  = Bytes.FromString("BI4mieAp/rdVctneZnhj0Ucu8Sc4LGMu2P5z9j49iXtN3AhDBWgIS1A4kfLI1ktcQAJACK4vwgutomtuWSoYllc=", StringEncoding.BASE64);
            var c1  = Bytes.FromString("BHENDFDcDsaWwpZLAWXDvEXlrEpIwr1p+OESiRCSemnk41WdfObVsvGPsYNFopaCJN762vP4MINb9HGzjmbM+aU=", StringEncoding.BASE64);

            var(c00, c11) = phe.ComputeC(skS, nS);

            Assert.Equal(c0, c00);
            Assert.Equal(c1, c11);
        }
示例#7
0
        public void Should_RotateTheSameSecretKey_When_OldSecretKeyAndUpdateTokenAreGiven()
        {
            var a    = Bytes.FromString("T20buheJjFOg+rsxP5ADIS7G3htdY/MUt9VozMOgEfA=", StringEncoding.BASE64);
            var b    = Bytes.FromString("UbXPXPtmKuudthZXXjJTE9AxBEgZB7mTFD+TGViCgHU=", StringEncoding.BASE64);
            var skC  = Bytes.FromString("YOCs/LOx6hll3nUBC29xpNJuLXofpKaBUNHPDBMA7JI=", StringEncoding.BASE64);
            var skC1 = Bytes.FromString("Zd+YJUvpXKQIjMaeZiad4vFOoU+mH2Qldx/yqwmGg2I=", StringEncoding.BASE64);
            // var pkS = Bytes.FromString("BBqqpApF8EsvQtLQlcR1sBon9RbKDcrsNypYDGatbx5JxvdQfGaszDwen01xQVWxL0UvrLfmzTBJHpL+q5+kyWw=", StringEncoding.BASE64);

            var phe    = new PheCrypto();
            var pheSkC = phe.DecodeSecretKey(skC);

            var pheSkC1 = phe.RotateSecretKey(pheSkC, a, b);

            Assert.Equal(skC1, pheSkC1.Encode());
        }
示例#8
0
        public void Should_DecryptM_When_SpecifiedListOfParametersArePassed()
        {
            var phe = new PheCrypto();

            var pwd = Bytes.FromString("passw0rd");
            var skC = phe.DecodeSecretKey(Bytes.FromString("Ty+3kM7kPFQjZu1NKyJjHqdUnDl9/n7mlHu7VJMyLRw=", StringEncoding.BASE64));
            var nC  = Bytes.FromString("9Gr0Jrg+vux+xBOLpGLXemB7uuwq7xkIa4JVgAJG5Yw=", StringEncoding.BASE64);
            var c1  = Bytes.FromString("BMkSH2xpOLox46HQf0oq8vqtYApMezo7K83xCMDgsUzgpI75esc6KJTRrqf7Nq5+y2LiGwyfJ/X8wZ7IXywy+gg=", StringEncoding.BASE64);
            var t1  = Bytes.FromString("BPGSx7b84QaTFJoXJcQ70qPuppbGkh8udqSIZJ7R8AE6Br0CBvPw69exsa3jxeHmMN17vY9l9wXdZcQt7FST2fc=", StringEncoding.BASE64);
            var m   = Bytes.FromString("XslZXIrsVj1XYVF74gWfi0Lo4frLsDObirNl5mFwYeA=", StringEncoding.BASE64);

            var mm = phe.DecryptM(skC, pwd, nC, t1, c1);

            Assert.Equal(m, mm);
        }
        public void Should_RotateTheSameSecretKey_When_OldSecretKeyAndUpdateTokenAreGiven()
        {
            var a    = ByteString.CopyFrom(Bytes.FromString("T20buheJjFOg+rsxP5ADIS7G3htdY/MUt9VozMOgEfA=", StringEncoding.BASE64));
            var b    = ByteString.CopyFrom(Bytes.FromString("UbXPXPtmKuudthZXXjJTE9AxBEgZB7mTFD+TGViCgHU=", StringEncoding.BASE64));
            var skC  = Bytes.FromString("YOCs/LOx6hll3nUBC29xpNJuLXofpKaBUNHPDBMA7JI=", StringEncoding.BASE64);
            var skC1 = Bytes.FromString("Zd+YJUvpXKQIjMaeZiad4vFOoU+mH2Qldx/yqwmGg2I=", StringEncoding.BASE64);

            var phe    = new PheCrypto();
            var pheSkC = phe.DecodeSecretKey(skC);
            var token1 = new UpdateToken()
            {
                A = a,
                B = b,
            };
            var pheSkC1 = phe.RotateSecretKey(pheSkC, token1.ToByteArray());

            Assert.Equal(skC1, pheSkC1.Encode());
        }
        public void Should_ComputeTheSameC0Value_For_SpecifiedListOfParameters()
        {
            var phe = new PheCrypto();
            var ns  = new byte[]
            {
                0x04, 0x60, 0x41, 0x90, 0xea, 0xe3, 0x03, 0x48,
                0xc4, 0x67, 0xa2, 0x56, 0xaa, 0x20, 0xf0, 0xe1,
                0x22, 0xfd, 0x4c, 0x54, 0xb0, 0x2a, 0x03, 0x26,
                0x84, 0xf1, 0x22, 0x11, 0xfc, 0x9a, 0x8e, 0xe3,
            };
            var pwd        = Bytes.FromString("passw0rd");
            var skC        = phe.DecodeSecretKey(this.clientPrivate);
            var hash       = new Dictionary <string, int>();
            var expectedC0 = Bytes.FromString("047062653b3a156a0a211686506f86427f13cdbe3825ca4ee820a8f202b91cf76cd276cc2f506191b491e85f5ac412cc36b2502cfbf23b130b0808d93c37271651", StringEncoding.HEX);
            var t0         = Bytes.FromString("04f1b0aef2e5dc2028955a35448a0d4fb4ea4c2dd3eba78bca4c5afd5c35832a1ffb7b1c7da8aa4c00105d0e7babacc49047891e3365e0606085071e23f50d97b7", StringEncoding.HEX);

            var c0 = phe.ComputeC0(skC, pwd, ns, t0);

            Assert.Equal(expectedC0, c0);
        }
示例#11
0
        public void TestValidPasswordRequest()
        {
            var rngMock = Substitute.For <IPheRandomGenerator>();

            rngMock.GenerateNonce(16).Returns(this.mockedRandomBytes.Take(16).ToArray());
            rngMock.GenerateNonce(32).Returns(this.mockedRandomBytes.Take(32).ToArray());

            var crypto = new PheCrypto();

            crypto.Rng = rngMock;

            var appSecretKey = crypto.DecodeSecretKey(this.clientPrivate);

            var servicePublicKey = crypto.DecodePublicKey(this.serverPublic);
            var pheClient        = new PheClient(appSecretKey, servicePublicKey);

            pheClient.Crypto = crypto;
            var req = pheClient.CreateVerifyPasswordRequest(this.password, this.enrollmentRecord);

            Assert.Equal(this.verifyPasswordReq, req);
        }
示例#12
0
        public void TestRotateEnrollmentRecord()
        {
            var rngMock = Substitute.For <IPheRandomGenerator>();

            rngMock.GenerateNonce(16).Returns(this.mockedRandomBytes.Take(16).ToArray());
            rngMock.GenerateNonce(32).Returns(this.mockedRandomBytes.Take(32).ToArray());

            var crypto = new PheCrypto();

            crypto.Rng = rngMock;

            var appSecretKey = crypto.DecodeSecretKey(this.clientPrivate);

            var servicePublicKey = crypto.DecodePublicKey(this.serverPublic);
            var pheClient        = new PheClient();

            pheClient.Crypto = crypto;
            var updatedEnrollmentRecord = pheClient.UpdateEnrollmentRecord(this.token, this.enrollmentRecord);

            Assert.Equal(this.updatedRecord, updatedEnrollmentRecord);
        }
示例#13
0
        public void TestRotateClientKey()
        {
            var rngMock = Substitute.For <IPheRandomGenerator>();

            rngMock.GenerateNonce(16).Returns(this.mockedRandomBytes.Take(16).ToArray());
            rngMock.GenerateNonce(32).Returns(this.mockedRandomBytes.Take(32).ToArray());

            var crypto = new PheCrypto();

            crypto.Rng = rngMock;

            var appSecretKey = crypto.DecodeSecretKey(this.clientPrivate);

            var servicePublicKey = crypto.DecodePublicKey(this.serverPublic);
            var pheClient        = new PheClient(appSecretKey, servicePublicKey);

            pheClient.Crypto = crypto;
            var(rotatedAppSecretKey, rotatedServicePublicKey) = pheClient.RotateKeys(this.token);

            Assert.Equal(this.rotatedClientSk, rotatedAppSecretKey.Encode());
            Assert.Equal(this.rotatedServerPub, rotatedServicePublicKey.Encode());
        }
示例#14
0
        private static (int, SecretKey) EnsureClientSecretKey(string clientSecretKey, PheCrypto phe)
        {
            var keyParts = clientSecretKey.Split(".");

            if (keyParts.Length != 3 ||
                !Int32.TryParse(keyParts[1], out int version) ||
                !keyParts[0].ToUpper().Equals("SK"))
            {
                throw new ArgumentException("has incorrect format", nameof(clientSecretKey));
            }

            var keyBytes = Bytes.FromString(keyParts[2], StringEncoding.BASE64);

            if (keyBytes.Length != 32)
            {
                throw new ArgumentException("has incorrect length", nameof(clientSecretKey));
            }

            var secretKey = phe.DecodeSecretKey(keyBytes);

            return(version, secretKey);
        }