예제 #1
0
 public void TestDiffuser() {
     const string helloWorld = "Hello World!";
     var helloWorldBytes = Encoding.UTF8.GetBytes(helloWorld);
     BigInteger b = helloWorldBytes.ToBigIntegerFromBigEndianUnsignedBytes();
     Diffuser d = new XteaDiffuser();
     var scrambled = d.Scramble(b, helloWorldBytes.Length);
     var unscrambed = d.Unscramble(scrambled, helloWorldBytes.Length);
     var unscrambledBytes = unscrambed.ToUnsignedBigEndianBytes();
     string recovered = Encoding.UTF8.GetString(unscrambledBytes);
     Assert.AreEqual(helloWorld, recovered);
 }
예제 #2
0
        private static void Combine()
        {
            if (!Arguments.QuietMode) {
                Console.WriteLine("Enter {0} shares separated by newlines:", Arguments.Threshold);
            }

            int polynomialDegree = -1;

            var totalShares = new List<string>();

            for (int i = 0; i < Arguments.Threshold; i++) {
                if (!Arguments.QuietMode) {
                    Console.Write("Share [{0}/{1}]: ", i + 1, Arguments.Threshold);
                }

                string currentShare = String.Empty;

                try {
                    currentShare = Console.ReadLine();
                }
                catch {
                    Fatal("I/O error while reading shares");
                }

                SecretShare share;

                if (!SecretShare.TryParse(currentShare, out share)) {
                    Fatal("invalid syntax");
                }

                if (polynomialDegree < 0) {
                    polynomialDegree = share.Point.Y.PrimePolynomial.Degree;
                    if(!IrreduciblePolynomial.IsValidDegree(polynomialDegree)) {
                        Fatal("share has illegal length");
                    }
                }
                else if (polynomialDegree != share.Point.Y.PrimePolynomial.Degree) {
                    Fatal("shares have different security levels");
                }

                totalShares.Add(currentShare);
            }

            Diffuser diffuser = new NullDiffuser();

            if (Arguments.EnableDiffusion) {
                if (polynomialDegree >= 64) {
                    diffuser = new XteaDiffuser();
                }
                else {
                    Warning("security level too small for the diffusion layer");
                }
            }

            var recoveredSecret = SecretCombiner.Combine(totalShares, diffuser);

            if (!Arguments.QuietMode) {
                Console.Error.Write("Resulting secret: ");
            }

            Console.Error.WriteLine(Arguments.HexMode ? recoveredSecret.RecoveredHexString : recoveredSecret.RecoveredTextString);
        }
예제 #3
0
        private static void Split()
        {
            int degree = Arguments.HasSpecifiedSecurityLevel
                             ? Arguments.SecurityLevel
                             : IrreduciblePolynomial.MaxDegree;

            if (!Arguments.QuietMode) {
                Console.Write("Generating shares using a ({0},{1}) scheme with ", Arguments.Threshold, Arguments.Shares);

                if (Arguments.HasSpecifiedSecurityLevel) {
                    Console.Write("a {0} bit", Arguments.SecurityLevel);
                }
                else {
                    Console.Write("dynamic");
                }

                Console.WriteLine(" security level.");

                Console.Error.Write("Enter the secret, ");

                if (Arguments.HexMode) {
                    Console.Error.Write("at most {0} hex digits: ", degree/4);
                }
                else {
                    Console.Error.Write("at most {0} ASCII characters: ", degree/8);
                }
            }

            var secret = String.Empty;

            try {
                secret = ReadLineHidden();
            }
            catch {
                Fatal("I/O error while reading secret");
            }

            int securitySize = Arguments.SecurityLevel;

            if (!Arguments.HasSpecifiedSecurityLevel) {
                securitySize = Arguments.HexMode
                                   ? 4*(((secret.Length + 1) & ~1))
                                   : 8*secret.Length;

                if (!IrreduciblePolynomial.IsValidDegree(securitySize)) {
                    Fatal("security level invalid (secret too long?)");
                }

                if (!Arguments.QuietMode) {
                    Console.Error.WriteLine("Using a {0} bit security level.", securitySize);
                }
            }

            Diffuser diffuser = new NullDiffuser();

            if (Arguments.EnableDiffusion) {
                if (securitySize >= 64) {
                    diffuser = new XteaDiffuser();
                }
                else {
                    Warning("security level too small for the diffusion layer");
                }
            }

            var secretBytes = MakeSecretBytes(secret, Arguments.HexMode, degree);

            foreach (var share in SecretSplitter.Split(SecretShareType.Message, secretBytes, Arguments.Threshold, diffuser).GetShares(Arguments.Shares)) {
                Console.WriteLine(share.ToString(SecretShareFormattingOptions.None));
            }
        }