Exemple #1
0
        public static Domain MapSeedToEntity(DomainSeed domainSeed)
        {
            var entity = new Domain();

            MapSeedToEntity(domainSeed, entity);
            return(entity);
        }
        /// <summary>
        /// A.2.1
        /// </summary>
        /// <param name="p"></param>
        /// <param name="q"></param>
        /// <param name="seed">Not needed</param>
        /// <param name="index">Not Needed</param>
        /// <returns></returns>
        public GGenerateResult Generate(BigInteger p, BigInteger q, DomainSeed seed = null, BitString index = null)
        {
            // 1 (always an integer)
            var e = (p - 1) / q;

            BigInteger h = 1;
            BigInteger g;

            do
            {
                // 2
                h++;
                if (h >= p - 1)
                {
                    // You couldn't feasibly reach this anyways...
                    return(new GGenerateResult("Too many h iterations"));
                }

                // 3
                g = BigInteger.ModPow(h, e, p);

                // 4
            } while (g == 1);

            return(new GGenerateResult(g, h));
        }
 public PQGenerateResult(BigInteger p, BigInteger q, DomainSeed seed, Counter count)
 {
     P     = p;
     Q     = q;
     Seed  = seed;
     Count = count;
 }
    public async Task <ActionResult> CreateData(
        [HttpTrigger(AuthorizationLevel.Anonymous, "POST", Route = "resources/data")] HttpRequest req,
        ILogger log)
    {
        log.LogInformation($"Creating all seed data: [{req}]");

        req.Query.TryGetValue("employeesCount", out var employeesCountQuery);
        req.Query.TryGetValue("payrollsMaxCount", out var payrollsMaxCountQuery);

        int employeesCount   = int.Parse(employeesCountQuery.FirstOrDefault() ?? "5");
        int payrollsMaxCount = int.Parse(payrollsMaxCountQuery.FirstOrDefault() ?? "10");

        var timer = Stopwatch.StartNew();

        var domainSeed = new DomainSeed(new EmployeeSeed());

        var employeesContainer   = client.GetContainer(Databases.PayrollProcessor.Name, Databases.PayrollProcessor.Containers.Employees);
        var departmentsContainer = client.GetContainer(Databases.PayrollProcessor.Name, Databases.PayrollProcessor.Containers.Departments);

        int totalEmployees = 0;
        int totalPayrolls  = 0;

        foreach (var employee in domainSeed.BuildAll(employeesCount, payrollsMaxCount))
        {
            var employeeRecord           = EmployeeRecord.Map.From(employee);
            var departmentEmployeeRecord = DepartmentEmployeeRecord.Map.CreateNewFrom(employee, Guid.NewGuid());

            await employeesContainer.CreateItemAsync(employeeRecord);

            await departmentsContainer.CreateItemAsync(departmentEmployeeRecord);

            foreach (var payroll in employee.Payrolls)
            {
                var departmentPayrollRecord = DepartmentPayrollRecord.Map.CreateNewFrom(employee, Guid.NewGuid(), payroll);
                var employeePayrollRecord   = EmployeePayrollRecord.Map.From(payroll);

                await departmentsContainer.CreateItemAsync(departmentPayrollRecord);

                await employeesContainer.CreateItemAsync(employeePayrollRecord);

                totalPayrolls++;
            }

            totalEmployees++;
        }

        timer.Stop();

        return(new JsonResult(new { totalEmployees, totalPayrolls, totalMilliseconds = timer.Elapsed.TotalMilliseconds }));
    }
        /// <summary>
        ///
        /// </summary>
        /// <param name="p"></param>
        /// <param name="q"></param>
        /// <param name="g"></param>
        /// <param name="seed">Not needed</param>
        /// <param name="index">Not needed</param>
        /// <returns></returns>
        public GValidateResult Validate(BigInteger p, BigInteger q, BigInteger g, DomainSeed seed = null, BitString index = null)
        {
            // 1
            if (2 > g || g > p - 1)
            {
                return(new GValidateResult("g not in required range"));
            }

            // 2
            if (BigInteger.ModPow(g, q, p) != 1)
            {
                return(new GValidateResult("g ^ q mod p != 1, invalid generator"));
            }

            return(new GValidateResult());
        }
        public void ShouldVerifyPQProperly(string hash, int counter, string pHex, string qHex, string seedHex, bool success, string testName)
        {
            var sha       = GetSha(hash);
            var firstSeed = new BitString(seedHex).ToPositiveBigInteger();
            var p         = new BitString(pHex).ToPositiveBigInteger();
            var q         = new BitString(qHex).ToPositiveBigInteger();

            var seed  = new DomainSeed(firstSeed);
            var count = new Counter(counter);

            var subject = new ProbablePQGeneratorValidator(sha);

            var result = subject.Validate(p, q, seed, count);

            Assert.AreEqual(success, result.Success, result.ErrorMessage);
        }
Exemple #7
0
 public static void MapSeedToEntity(DomainSeed domainSeed, Domain dbDomain)
 {
     dbDomain.FullText      = HttpUtility.HtmlDecode(domainSeed.full_text);
     dbDomain.GrantedPowers = domainSeed.granted_powers;
     dbDomain.Id            = domainSeed.Id;
     dbDomain.Name          = domainSeed.name;
     dbDomain.Reference     = domainSeed.reference;
     dbDomain.Spell1        = domainSeed.spell_1;
     dbDomain.Spell2        = domainSeed.spell_2;
     dbDomain.Spell3        = domainSeed.spell_3;
     dbDomain.Spell4        = domainSeed.spell_4;
     dbDomain.Spell5        = domainSeed.spell_5;
     dbDomain.Spell6        = domainSeed.spell_6;
     dbDomain.Spell7        = domainSeed.spell_7;
     dbDomain.Spell8        = domainSeed.spell_8;
     dbDomain.Spell9        = domainSeed.spell_9;
 }
        public GGenerateResult Generate(BigInteger p, BigInteger q, DomainSeed seed = null, BitString index = null)
        {
            // 1
            if (index.BitLength != 8)
            {
                return(new GGenerateResult("Invalid index"));
            }

            // 3
            var e = (p - 1) / q;

            // 4
            var        count = 0;
            BigInteger g;

            do
            {
                // 5
                count++;

                // 6
                if (count == 0)
                {
                    return(new GGenerateResult("Invalid count"));
                }

                // Need to make sure the BigInteger conversion doesn't drop leading 00 bytes, always have as much seed as q
                var seedBits = new BitString(seed.GetFullSeed()).PadToModulusMsb(32);

                // 7
                var countBS = new BitString(count, 16, false);      // value must be 16 bits long
                var U       = BitString.ConcatenateBits(seedBits, _ggen);
                U = BitString.ConcatenateBits(U, index);
                U = BitString.ConcatenateBits(U, countBS);

                // 8, 9
                var W = _sha.HashMessage(U).ToBigInteger();
                g = BigInteger.ModPow(W, e, p);

                // 10
            } while (g < 2);

            // 11
            return(new GGenerateResult(g));
        }
 public FfcDomainParametersGenerateResult(FfcDomainParameters pqgDomainParameters, DomainSeed seed, Counter count)
 {
     PqgDomainParameters = pqgDomainParameters;
     Seed  = seed;
     Count = count;
 }
Exemple #10
0
        /// <summary>
        /// A.1.2.2
        /// </summary>
        /// <param name="p"></param>
        /// <param name="q"></param>
        /// <param name="seed"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public PQValidateResult Validate(BigInteger p, BigInteger q, DomainSeed seed, Counter count)
        {
            // 0, domain type check
            if (seed.Mode != PrimeGenMode.Provable && count.Mode != PrimeGenMode.Provable)
            {
                return(new PQValidateResult("Invalid DomainSeed and Counter"));
            }

            // 1, 2
            var L = new BitString(p).BitLength;
            var N = new BitString(q).BitLength;

            // 3
            if (!DSAHelper.VerifyLenPair(L, N))
            {
                return(new PQValidateResult("Invalid L, N pair"));
            }

            // 4
            if (seed.Seed < NumberTheory.Pow2(N - 1))
            {
                return(new PQValidateResult("Bad first seed"));
            }

            // 5
            if (NumberTheory.Pow2(N) <= q)
            {
                return(new PQValidateResult("Bad q, too small"));
            }

            // 6
            if (NumberTheory.Pow2(L) <= p)
            {
                return(new PQValidateResult("Bad p, too large"));
            }

            // 7
            if ((p - 1) % q != 0)
            {
                return(new PQValidateResult("p - 1 % q != 0, bad values"));
            }

            // 8
            var computed_result = Generate(L, N, seed.Seed);

            if (!computed_result.Success)
            {
                return(new PQValidateResult("Failed to generate p and q"));
            }

            if (q != computed_result.Q || seed.QSeed != computed_result.Seed.QSeed || count.QCount != computed_result.Count.QCount)
            {
                return(new PQValidateResult("Failed to generate given q"));
            }

            if (p != computed_result.P || seed.PSeed != computed_result.Seed.PSeed || count.PCount != computed_result.Count.PCount)
            {
                return(new PQValidateResult("Failed to generate given p"));
            }

            return(new PQValidateResult());
        }
        public GValidateResult Validate(BigInteger p, BigInteger q, BigInteger g, DomainSeed seed = null, BitString index = null)
        {
            // 1
            if (index.BitLength != 8)
            {
                return(new GValidateResult("Invalid index"));
            }

            // 2
            if (2 > g || g > p - 1)
            {
                return(new GValidateResult("Invalid generator value, out of range"));
            }

            // 3
            if (BigInteger.ModPow(g, q, p) != 1)
            {
                return(new GValidateResult("Invalid generator value, g^q % p != 1"));
            }

            // 5
            var e = (p - 1) / q;

            // 6
            var        count = 0;
            BigInteger computed_g;

            // 7
            do
            {
                count++;

                // 8
                if (count == 0)
                {
                    return(new GValidateResult("Too many iterations to find g"));
                }

                // Need to make sure the BigInteger conversion doesn't drop leading 00 bytes, always have as much seed as q
                var seedBits = new BitString(seed.GetFullSeed()).PadToModulusMsb(32);

                // 9
                var countBS = new BitString(count, 16, false);      // value must be 16 bits long
                var U       = BitString.ConcatenateBits(seedBits, _ggen);
                U = BitString.ConcatenateBits(U, index);
                U = BitString.ConcatenateBits(U, countBS);

                // 10
                var W = _sha.HashMessage(U).ToBigInteger();

                // 11
                computed_g = BigInteger.ModPow(W, e, p);

                //12
            } while (computed_g < 2);

            // 13
            if (computed_g != g)
            {
                return(new GValidateResult("Incorrect g value obtained"));
            }

            return(new GValidateResult());
        }
Exemple #12
0
        /// <summary>
        /// A.1.1.3 from FIPS 186-4
        /// </summary>
        /// <param name="p"></param>
        /// <param name="q"></param>
        /// <param name="seed"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public PQValidateResult Validate(BigInteger p, BigInteger q, DomainSeed seed, Counter count)
        {
            if (seed.Mode != PrimeGenMode.Probable && count.Mode != PrimeGenMode.Probable)
            {
                return(new PQValidateResult("Invalid DomainSeed and Counter"));
            }

            // 1, 2
            var L = new BitString(p).BitLength;
            var N = new BitString(q).BitLength;

            // 3
            if (!DSAHelper.VerifyLenPair(L, N))
            {
                return(new PQValidateResult("Invalid L, N pair"));
            }

            // 4
            if (count.Count > (4 * L - 1))
            {
                return(new PQValidateResult("Invalid counter"));
            }

            // 5, 6

            /*
             *  Appending 0s to the bitstring representation of the seed as to make it mod 32 (if it isn't already), as this is the mod of the original seed that is hashed.
             *  In instances (as an example) when the chosen seed starts with eight zero bits in a row, the biginteger representation of said bitstring is 1 byte smaller than it should be,
             *  thus failing the check that it is at least the length of N
             */
            var seedBitString = new BitString(seed.Seed);

            if (seedBitString.BitLength % 32 != 0)
            {
                seedBitString = BitString.ConcatenateBits(BitString.Zeroes(32 - seedBitString.BitLength % 32), seedBitString);
            }
            var seedLen = seedBitString.BitLength;

            if (seedLen < N)
            {
                return(new PQValidateResult("Invalid seed"));
            }

            // 7
            var U = _sha.HashNumber(seed.Seed).ToBigInteger() % NumberTheory.Pow2(N - 1);

            // 8
            var computed_q = NumberTheory.Pow2(N - 1) + U + 1 - (U % 2);

            // 9
            if (!NumberTheory.MillerRabin(computed_q, DSAHelper.GetMillerRabinIterations(L, N)) || computed_q != q)
            {
                return(new PQValidateResult("Q not prime, or doesn't match expected value"));
            }

            // 10, 11, 12
            var outLen = _sha.HashFunction.OutputLen;
            var n      = L.CeilingDivide(outLen) - 1;
            var b      = L - 1 - (n * outLen);
            var offset = 1;

            // 13
            BigInteger computed_p = 0;
            int        i;

            for (i = 0; i <= count.Count; i++)
            {
                // 13.1, 13.2
                var W = _sha.HashNumber(seed.Seed + offset).ToBigInteger();
                for (var j = 1; j < n; j++)
                {
                    W += (_sha.HashNumber(seed.Seed + offset + j).ToBigInteger()) * NumberTheory.Pow2(j * outLen);
                }
                W += ((_sha.HashNumber(seed.Seed + offset + n).ToBigInteger()) % NumberTheory.Pow2(b)) * NumberTheory.Pow2(n * outLen);

                // 13.3
                var X = W + NumberTheory.Pow2(L - 1);

                // 13.4
                var c = X % (2 * q);

                // 13.5
                computed_p = X - (c - 1);

                // 13.6, 13.7, 13.8
                if (computed_p >= NumberTheory.Pow2(L - 1))
                {
                    // Check if p is prime, if so return
                    if (NumberTheory.MillerRabin(computed_p, DSAHelper.GetMillerRabinIterations(L, N)))
                    {
                        break;
                    }
                }

                // 13.9
                offset += n + 1;
            }

            // 14
            if (i != count.Count || computed_p != p || !NumberTheory.MillerRabin(computed_p, DSAHelper.GetMillerRabinIterations(L, N)))
            {
                return(new PQValidateResult($"Invalid p value or counter. computed_p = {new BitString(computed_p).ToHex()}"));
            }

            // 15
            return(new PQValidateResult());
        }