private void RunFuzzedTest(bool useSubgroupConstruction, string hashFunction, int numberOfAttributes, bool supportDevice, int numberOfTokens, int[] dArray, int[] cArray, int pseudonymIndex) { // Issuer setup IssuerSetupParameters isp = new IssuerSetupParameters(); isp.GroupConstruction = useSubgroupConstruction ? GroupType.Subgroup : GroupType.ECC; isp.UidP = GetRandomBytes(MaxByteArrayLength); isp.UidH = hashFunction; isp.E = IssuerSetupParameters.GetDefaultEValues(numberOfAttributes); isp.S = GetRandomBytes(MaxByteArrayLength); IssuerKeyAndParameters ikap = isp.Generate(supportDevice); IssuerParameters ip = ikap.IssuerParameters; ip.Verify(); IDevice device = null; GroupElement hd = null; if (supportDevice) { device = new VirtualDevice(ip); hd = device.GetDevicePublicKey(); } // Issuance byte[][] attributes = new byte[numberOfAttributes][]; for (int index = 0; index < numberOfAttributes; index++) { attributes[index] = GetRandomBytes(MaxByteArrayLength); } byte[] tokenInformation = GetRandomBytes(MaxByteArrayLength); byte[] proverInformation = GetRandomBytes(MaxByteArrayLength); IssuerProtocolParameters ipp = new IssuerProtocolParameters(ikap); ipp.Attributes = attributes; ipp.NumberOfTokens = numberOfTokens; ipp.TokenInformation = tokenInformation; ipp.DevicePublicKey = hd; Issuer issuer = ipp.CreateIssuer(); string msg1 = ip.Serialize(issuer.GenerateFirstMessage()); ProverProtocolParameters ppp = new ProverProtocolParameters(ip); ppp.Attributes = attributes; ppp.NumberOfTokens = numberOfTokens; ppp.TokenInformation = tokenInformation; ppp.ProverInformation = proverInformation; ppp.DevicePublicKey = hd; Prover prover = ppp.CreateProver(); string msg2 = ip.Serialize(prover.GenerateSecondMessage(ip.Deserialize<FirstIssuanceMessage>(msg1))); string msg3 = ip.Serialize(issuer.GenerateThirdMessage(ip.Deserialize<SecondIssuanceMessage>(msg2))); // issue token UProveKeyAndToken[] upkt = prover.GenerateTokens(ip.Deserialize<ThirdIssuanceMessage>(msg3)); // Presentation byte[] message = GetRandomBytes(MaxByteArrayLength); byte[] deviceMessage = null; IDevicePresentationContext deviceContext = null; if (supportDevice) { deviceMessage = GetRandomBytes(MaxByteArrayLength); deviceContext = device.GetPresentationContext(); } int tokenIndex = random.Next(upkt.Length); // generate the presentation proof PresentationProof proof = PresentationProof.Generate(ip, dArray, message, deviceMessage, deviceContext, upkt[tokenIndex], attributes); // verify the presentation proof proof.Verify(ip, dArray, message, deviceMessage, upkt[tokenIndex].Token); }
public void TestEndToEnd() { Random random = new Random(); int attributeLength = 10; foreach (GroupType groupConstruction in groupConstructions) { foreach (string hashFunction in supportedHashFunctions) { //Console.WriteLine("Hash = " + hashFunction); for (int numberOfAttribs = 0; numberOfAttribs <= 3; numberOfAttribs++) { //Console.WriteLine("NumberOfAttribs = " + numberOfAttribs); for (int e = 0; e <= 1; e++) { foreach (bool supportDevice in new bool[] { false, true }) { // Issuer setup IssuerSetupParameters isp = new IssuerSetupParameters(); isp.GroupConstruction = groupConstruction; isp.UidP = encoding.GetBytes("unique UID"); isp.UidH = hashFunction; isp.E = new byte[numberOfAttribs]; for (int i = 0; i < numberOfAttribs; i++) { isp.E[i] = (byte)e; } isp.S = encoding.GetBytes("specification"); IssuerKeyAndParameters ikap = isp.Generate(supportDevice); IssuerParameters ip = ikap.IssuerParameters; ip.Verify(); IDevice device = null; GroupElement hd = null; if (supportDevice) { device = new VirtualDevice(ip); hd = device.GetDevicePublicKey(); } // Issuance byte[][] attributes = new byte[numberOfAttribs][]; for (int index = 0; index < numberOfAttribs; index++) { attributes[index] = new byte[attributeLength]; random.NextBytes(attributes[index]); } byte[] tokenInformation = encoding.GetBytes("token information"); byte[] proverInformation = encoding.GetBytes("prover information"); int numberOfTokens = (int)Math.Pow(2, numberOfAttribs); IssuerProtocolParameters ipp = new IssuerProtocolParameters(ikap); ipp.Attributes = attributes; ipp.NumberOfTokens = numberOfTokens; ipp.TokenInformation = tokenInformation; ipp.DevicePublicKey = hd; Issuer issuer = ipp.CreateIssuer(); FirstIssuanceMessage msg1 = issuer.GenerateFirstMessage(); ProverProtocolParameters ppp = new ProverProtocolParameters(ip); ppp.Attributes = attributes; ppp.NumberOfTokens = numberOfTokens; ppp.TokenInformation = tokenInformation; ppp.ProverInformation = proverInformation; ppp.DevicePublicKey = hd; Prover prover = ppp.CreateProver(); SecondIssuanceMessage msg2 = prover.GenerateSecondMessage(msg1); ThirdIssuanceMessage msg3 = issuer.GenerateThirdMessage(msg2); // issue token UProveKeyAndToken[] upkt = prover.GenerateTokens(msg3); // Presentation for (int i = 0; i < numberOfTokens; i++) { List<int> disclosedList = new List<int>(); //Console.Write("Disclosed list = "); for (int index = 0; index < numberOfAttribs; index++) { if ((((int)Math.Pow(2, index)) & i) != 0) { //Console.Write((index + 1) + ", "); disclosedList.Add(index + 1); } } //Console.WriteLine(); int[] disclosed = disclosedList.ToArray(); byte[] message = encoding.GetBytes("message"); byte[] deviceMessage = null; IDevicePresentationContext deviceContext = null; if (supportDevice) { deviceMessage = encoding.GetBytes("message"); deviceContext = device.GetPresentationContext(); } // generate the presentation proof PresentationProof proof = PresentationProof.Generate(ip, disclosed, message, deviceMessage, deviceContext, upkt[i], attributes); // verify the presentation proof proof.Verify(ip, disclosed, message, deviceMessage, upkt[i].Token); // // negative cases // if (numberOfAttribs > 0) { // modify issuer params (change specification); IssuerParameters ip2 = new IssuerParameters(ip.UidP, ip.Gq, ip.UidH, ip.G, ip.Gd, ip.E, ip.S, ip.UsesRecommendedParameters); ip2.S = encoding.GetBytes("wrong issuer params"); try { proof.Verify(ip2, disclosed, message, null, upkt[i].Token); Assert.Fail(); } catch (InvalidUProveArtifactException) { } // modify disclosed list int[] disclosed2; if (disclosed.Length == 0) { disclosed2 = new int[] { 1 }; } else { disclosed2 = new int[] { }; } try { proof.Verify(ip, disclosed2, message, deviceMessage, upkt[i].Token); Assert.Fail(); } catch (InvalidUProveArtifactException) { } // modify message try { proof.Verify(ip, disclosed, encoding.GetBytes("wrong message"), deviceMessage, upkt[i].Token); Assert.Fail(); } catch (InvalidUProveArtifactException) { } // modify token try { proof.Verify(ip, disclosed, message, deviceMessage, upkt[(i + 1) % numberOfTokens].Token); Assert.Fail(); } catch (InvalidUProveArtifactException) { } // modify proof proof.A = encoding.GetBytes("wrong proof"); try { proof.Verify(ip, disclosed, message, deviceMessage, upkt[i].Token); Assert.Fail(); } catch (InvalidUProveArtifactException) { } } } } } } } } }
/// <summary> /// This sample illustrates how to issue and present device-protected U-Prove tokens. /// </summary> public static void DeviceSample() { WriteLine("U-Prove SDK Device Sample"); /* * issuer setup */ IssuerKeyAndParameters ikap = SetupUProveIssuer("sample device-protected issuer", 3, GroupType.ECC, true); string ipJSON = ikap.IssuerParameters.Serialize(); // the IssuerParameters instance needs to be distributed to the Prover, Device, and Verifier. // Each needs to verify the parameters before using them IssuerParameters ip = new IssuerParameters(ipJSON); ip.Verify(); /* * device provisioning */ // generate a new device IDevice device = new VirtualDevice(ip); // get the device public key GroupElement hd = device.GetDevicePublicKey(); /* * token issuance */ // specify the attribute values byte[][] attributes = new byte[][] { encoding.GetBytes("first attribute value"), encoding.GetBytes("second attribute value"), encoding.GetBytes("third attribute value"), }; // specify the special field values byte[] tokenInformation = encoding.GetBytes("token information value"); byte[] proverInformation = encoding.GetBytes("prover information value"); // specify the number of tokens to issue int numberOfTokens = 5; UProveKeyAndToken[] upkt = IssueUProveTokens(ikap, ip, attributes, numberOfTokens, tokenInformation, proverInformation); /* * token presentation */ // the indices of disclosed attributes int[] disclosed = new int[] { 2 }; // the application-specific messages that the prover and device will sign, respectively. Typically this // is a nonce combined with any application-specific transaction data to be signed. byte[] message = encoding.GetBytes("message"); byte[] deviceMessage = encoding.GetBytes("message for device"); PresentUProveToken(ip, upkt[0], attributes, disclosed, null, message, null, device, deviceMessage); WriteLine("Sample completed.\n*************************************************************\n"); }
public void DevicePseudonymTest() { // Issuer setup IssuerSetupParameters isp = new IssuerSetupParameters(); isp.UidP = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; isp.E = new byte[] { (byte)1, (byte)1, (byte)1, (byte)1 }; isp.UseRecommendedParameterSet = true; isp.S = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; IssuerKeyAndParameters ikap = isp.Generate(true); IssuerParameters ip = ikap.IssuerParameters; // Issuance byte[][] attributes = new byte[][] { encoding.GetBytes("Attribute 1"), encoding.GetBytes("Attribute 2"), encoding.GetBytes("Attribute 3"), encoding.GetBytes("Attribute 4") }; byte[] tokenInformation = new byte[] { }; byte[] proverInformation = new byte[] { }; int numberOfTokens = 1; IDevice device = new VirtualDevice(ip); GroupElement hd = device.GetDevicePublicKey(); IssuerProtocolParameters ipp = new IssuerProtocolParameters(ikap); ipp.Attributes = attributes; ipp.NumberOfTokens = numberOfTokens; ipp.TokenInformation = tokenInformation; ipp.DevicePublicKey = hd; Issuer issuer = ipp.CreateIssuer(); FirstIssuanceMessage msg1 = issuer.GenerateFirstMessage(); ProverProtocolParameters ppp = new ProverProtocolParameters(ip); ppp.Attributes = attributes; ppp.NumberOfTokens = numberOfTokens; ppp.TokenInformation = tokenInformation; ppp.ProverInformation = proverInformation; ppp.DevicePublicKey = hd; Prover prover = ppp.CreateProver(); SecondIssuanceMessage msg2 = prover.GenerateSecondMessage(msg1); ThirdIssuanceMessage msg3 = issuer.GenerateThirdMessage(msg2); UProveKeyAndToken[] upkt = prover.GenerateTokens(msg3); // Pseudonym int[] disclosed = new int[0]; byte[] message = encoding.GetBytes("this is the presentation message, this can be a very long message"); byte[] messageForDevice = encoding.GetBytes("message for Device"); byte[] scope = encoding.GetBytes("scope"); PresentationProof proof; FieldZqElement[] tildeO; // Valid presentation IDevicePresentationContext deviceCtx = device.GetPresentationContext(); proof = PresentationProof.Generate(ip, disclosed, null, PresentationProof.DeviceAttributeIndex, scope, message, messageForDevice, deviceCtx, upkt[0], attributes, out tildeO); proof.Verify(ip, disclosed, null, PresentationProof.DeviceAttributeIndex, scope, message, messageForDevice, upkt[0].Token); // Invalid pseudonym (wrong scope) deviceCtx = device.GetPresentationContext(); proof = PresentationProof.Generate(ip, disclosed, null, PresentationProof.DeviceAttributeIndex, scope, message, messageForDevice, deviceCtx, upkt[0], attributes, out tildeO); try { proof.Verify(ip, disclosed, null, PresentationProof.DeviceAttributeIndex, encoding.GetBytes("bad scope"), message, messageForDevice, upkt[0].Token); Assert.Fail(); } catch (InvalidUProveArtifactException) { } // Ensure tildeO is correct, in this case it should be empty because there are no comitted attributes Assert.IsTrue(tildeO == null || tildeO.Length == 0); }
public void ProtocolTest() { Stopwatch sw = new Stopwatch(); sw.Start(); bool[] bools = new bool[] { true, false }; foreach (bool isSubgroupConstruction in bools) { foreach (bool supportDevice in bools) { var vectors = supportDevice ? (isSubgroupConstruction ? GetTestVectors("testvectorssubgroup_Device_doc.txt") : GetTestVectors("testvectorsEC_Device_doc.txt")) : (isSubgroupConstruction ? GetTestVectors("testvectorssubgroup_doc.txt") : GetTestVectors("testvectorsEC_doc.txt")); IssuerKeyAndParameters ikap = LoadIssuerKeyAndParameters(isSubgroupConstruction, vectors["GroupName"], supportDevice, vectors); FieldZq Zq = ikap.IssuerParameters.Zq; // replace random y0/g0 with test vector values ikap.PrivateKey = Zq.GetElement(HexToBytes(vectors["y0"])); ikap.IssuerParameters.G[0] = CreateGroupElement(ikap.IssuerParameters.Gq, vectors["g0"]); Assert.AreEqual(ikap.IssuerParameters.G[0], ikap.IssuerParameters.Gq.G.Exponentiate(ikap.PrivateKey), "g0 computation"); IssuerParameters ip = ikap.IssuerParameters; ip.Verify(); /* * issuance */ byte[][] A = new byte[][] { HexToBytes(vectors["A1"]), HexToBytes(vectors["A2"]), HexToBytes(vectors["A3"]), HexToBytes(vectors["A4"]), HexToBytes(vectors["A5"]) }; Assert.AreEqual(Zq.GetElement(HexToBytes(vectors["x1"])), ProtocolHelper.ComputeXi(ip, 0, A[0]), "x1"); Assert.AreEqual(Zq.GetElement(HexToBytes(vectors["x2"])), ProtocolHelper.ComputeXi(ip, 1, A[1]), "x2"); Assert.AreEqual(Zq.GetElement(HexToBytes(vectors["x3"])), ProtocolHelper.ComputeXi(ip, 2, A[2]), "x3"); Assert.AreEqual(Zq.GetElement(HexToBytes(vectors["x4"])), ProtocolHelper.ComputeXi(ip, 3, A[3]), "x4"); Assert.AreEqual(Zq.GetElement(HexToBytes(vectors["x5"])), ProtocolHelper.ComputeXi(ip, 4, A[4]), "x5"); byte[] TI = HexToBytes(vectors["TI"]); Assert.IsTrue(HexToBytes(vectors["P"]).SequenceEqual(ip.Digest(supportDevice)), "P"); Assert.AreEqual(Zq.GetElement(HexToBytes(vectors["xt"])), ProtocolHelper.ComputeXt(ip, TI, supportDevice), "xt"); IDevice device = null; GroupElement hd = null; if (supportDevice) { device = new VirtualDevice(ip, Zq.GetElement(HexToBytes(vectors["xd"])), Zq.GetElement(HexToBytes(vectors["wdPrime"]))); IDevicePresentationContext context = device.GetPresentationContext(); // Test device responses Assert.AreEqual(CreateGroupElement(ip.Gq, vectors["hd"]), device.GetDevicePublicKey(), "hd"); Assert.AreEqual(CreateGroupElement(ip.Gq, vectors["ad"]), context.GetInitialWitness(), "ad"); Assert.AreEqual(Zq.GetElement(HexToBytes(vectors["rdPrime"])), context.GetDeviceResponse(HexToBytes(vectors["md"]), HexToBytes(vectors["mdPrime"]), ip.HashFunctionOID), "rdPrime"); hd = CreateGroupElement(ip.Gq, vectors["hd"]); } IssuerProtocolParameters ipp = new IssuerProtocolParameters(ikap); ipp.Attributes = A; ipp.NumberOfTokens = 1; ipp.TokenInformation = TI; ipp.DevicePublicKey = hd; ipp.PreGeneratedW = new FieldZqElement[] { Zq.GetElement(HexToBytes(vectors["w"])) }; Issuer issuer = ipp.CreateIssuer(); byte[] PI = HexToBytes(vectors["PI"]); ProverProtocolParameters ppp = new ProverProtocolParameters(ip); ppp.Attributes = A; ppp.NumberOfTokens = 1; ppp.TokenInformation = TI; ppp.ProverInformation = PI; ppp.DevicePublicKey = hd; ppp.ProverRandomData = new ProverRandomData( new FieldZqElement[] { Zq.GetElement(HexToBytes(vectors["alpha"])) }, new FieldZqElement[] { Zq.GetElement(HexToBytes(vectors["beta1"])) }, new FieldZqElement[] { Zq.GetElement(HexToBytes(vectors["beta2"])) }); Prover prover = ppp.CreateProver(); FirstIssuanceMessage msg1 = issuer.GenerateFirstMessage(); Assert.AreEqual(msg1.sigmaZ, CreateGroupElement(ip.Gq, vectors["sigmaZ"]), "sigmaZ"); Assert.AreEqual(msg1.sigmaA[0], CreateGroupElement(ip.Gq, vectors["sigmaA"]), "sigmaA"); Assert.AreEqual(msg1.sigmaB[0], CreateGroupElement(ip.Gq, vectors["sigmaB"]), "sigmaB"); SecondIssuanceMessage msg2 = prover.GenerateSecondMessage(msg1); Assert.AreEqual(msg2.sigmaC[0], Zq.GetElement(HexToBytes(vectors["sigmaC"])), "sigmaC"); ThirdIssuanceMessage msg3 = issuer.GenerateThirdMessage(msg2); Assert.AreEqual(msg3.sigmaR[0], Zq.GetElement(HexToBytes(vectors["sigmaR"])), "sigmaR"); UProveKeyAndToken[] upkt = prover.GenerateTokens(msg3); Assert.AreEqual(upkt[0].PrivateKey, Zq.GetElement(HexToBytes(vectors["alphaInverse"])), "alphaInverse"); UProveToken token = upkt[0].Token; Assert.AreEqual(token.H, CreateGroupElement(ip.Gq, vectors["h"]), "h"); Assert.AreEqual(token.SigmaZPrime, CreateGroupElement(ip.Gq, vectors["sigmaZPrime"]), "sigmaZPrime"); Assert.AreEqual(token.SigmaCPrime, Zq.GetElement(HexToBytes(vectors["sigmaCPrime"])), "sigmaCPrime"); Assert.AreEqual(token.SigmaRPrime, Zq.GetElement(HexToBytes(vectors["sigmaRPrime"])), "sigmaRPrime"); Assert.IsTrue(HexToBytes(vectors["UIDt"]).SequenceEqual(ProtocolHelper.ComputeTokenID(ip, token)), "UIDt"); Assert.IsTrue(supportDevice == token.IsDeviceProtected); /* * presentation */ int[] disclosed = Array.ConvertAll<string, int>(vectors["D"].Split(','), new Converter<string, int>(stringToInt)); int[] committed = Array.ConvertAll<string, int>(vectors["C"].Split(','), new Converter<string, int>(stringToInt)); byte[] m = HexToBytes(vectors["m"]); byte[] md = null; IDevicePresentationContext deviceContext = null; if (supportDevice) { md = HexToBytes(vectors["md"]); deviceContext = device.GetPresentationContext(); } int p; if (!int.TryParse(vectors["p"], out p)) { p = PresentationProof.DeviceAttributeIndex; } byte[] s = HexToBytes(vectors["s"]); int commitmentIndex = committed[0]; ProverPresentationProtocolParameters pppp = new ProverPresentationProtocolParameters(ip, disclosed, m, upkt[0], A); pppp.Committed = committed; pppp.PseudonymAttributeIndex = p; pppp.PseudonymScope = s; pppp.DeviceMessage = md; pppp.DeviceContext = deviceContext; pppp.RandomData = new ProofGenerationRandomData( Zq.GetElement(HexToBytes(vectors["w0"])), new FieldZqElement[] { Zq.GetElement(HexToBytes(vectors["w1"])), Zq.GetElement(HexToBytes(vectors["w3"])), Zq.GetElement(HexToBytes(vectors["w4"])) }, supportDevice ? Zq.GetElement(HexToBytes(vectors["wd"])) : null, new FieldZqElement[] { Zq.GetElement(HexToBytes(vectors["tildeO" + commitmentIndex])), }, new FieldZqElement[] { Zq.GetElement(HexToBytes(vectors["tildeW" + commitmentIndex])) }); CommitmentPrivateValues cpv; PresentationProof proof = PresentationProof.Generate(pppp, out cpv); Assert.IsTrue(HexToBytes(vectors["a"]).SequenceEqual(proof.A), "a"); Assert.AreEqual(ProtocolHelper.GenerateScopeElement(ip.Gq, s), CreateGroupElement(ip.Gq, vectors["gs"])); Assert.IsTrue(HexToBytes(vectors["ap"]).SequenceEqual(proof.Ap), "ap"); Assert.AreEqual(proof.Ps, CreateGroupElement(ip.Gq, vectors["Ps"]), "Ps"); Assert.IsTrue(HexToBytes(vectors["A2"]).SequenceEqual(proof.DisclosedAttributes[0]), "A2"); Assert.IsTrue(HexToBytes(vectors["A5"]).SequenceEqual(proof.DisclosedAttributes[1]), "A5"); Assert.AreEqual(proof.R[0], Zq.GetElement(HexToBytes(vectors["r0"])), "r0"); Assert.AreEqual(proof.R[1], Zq.GetElement(HexToBytes(vectors["r1"])), "r1"); Assert.AreEqual(proof.R[2], Zq.GetElement(HexToBytes(vectors["r3"])), "r3"); Assert.AreEqual(proof.R[3], Zq.GetElement(HexToBytes(vectors["r4"])), "r4"); if (supportDevice) { Assert.AreEqual(proof.R[4], Zq.GetElement(HexToBytes(vectors["rd"])), "rd"); } Assert.AreEqual(proof.Commitments[0].TildeR, Zq.GetElement(HexToBytes(vectors["tildeR" + commitmentIndex])), "tildeR" + commitmentIndex); Assert.IsTrue(cpv.TildeO.Length == 1); Assert.AreEqual(cpv.TildeO[0], Zq.GetElement(HexToBytes(vectors["tildeO" + commitmentIndex])), "tildeO" + commitmentIndex); VerifierPresentationProtocolParameters vppp = new VerifierPresentationProtocolParameters(ip, disclosed, m, upkt[0].Token); vppp.Committed = committed; vppp.PseudonymAttributeIndex = p; vppp.PseudonymScope = s; vppp.DeviceMessage = md; proof.Verify(vppp); } } sw.Stop(); Debug.WriteLine("Protocol Test Elapsed Time: " + sw.ElapsedMilliseconds + "ms"); }