コード例 #1
0
        public virtual AsymmetricCipherKeyPair GenerateKeyPair()
        {
            int        num2;
            BigInteger publicExponent;
            BigInteger bigInteger;
            BigInteger bigInteger2;
            BigInteger bigInteger4;
            BigInteger bigInteger6;
            BigInteger bigInteger7;
            BigInteger bigInteger8;

            do
            {
                int strength = this.parameters.Strength;
                int num      = (strength + 1) / 2;
                num2 = strength - num;
                int num3 = strength / 3;
                int num4 = strength >> 2;
                publicExponent = this.parameters.PublicExponent;
                bigInteger     = this.ChooseRandomPrime(num, publicExponent);
                while (true)
                {
                    bigInteger2 = this.ChooseRandomPrime(num2, publicExponent);
                    BigInteger bigInteger3 = bigInteger2.Subtract(bigInteger).Abs();
                    if (bigInteger3.BitLength >= num3)
                    {
                        bigInteger4 = bigInteger.Multiply(bigInteger2);
                        if (bigInteger4.BitLength != strength)
                        {
                            bigInteger = bigInteger.Max(bigInteger2);
                        }
                        else
                        {
                            if (WNafUtilities.GetNafWeight(bigInteger4) >= num4)
                            {
                                break;
                            }
                            bigInteger = this.ChooseRandomPrime(num, publicExponent);
                        }
                    }
                }
                if (bigInteger.CompareTo(bigInteger2) < 0)
                {
                    BigInteger bigInteger5 = bigInteger;
                    bigInteger  = bigInteger2;
                    bigInteger2 = bigInteger5;
                }
                bigInteger6 = bigInteger.Subtract(RsaKeyPairGenerator.One);
                bigInteger7 = bigInteger2.Subtract(RsaKeyPairGenerator.One);
                BigInteger val = bigInteger6.Gcd(bigInteger7);
                BigInteger m   = bigInteger6.Divide(val).Multiply(bigInteger7);
                bigInteger8 = publicExponent.ModInverse(m);
            }while (bigInteger8.BitLength <= num2);
            BigInteger dP   = bigInteger8.Remainder(bigInteger6);
            BigInteger dQ   = bigInteger8.Remainder(bigInteger7);
            BigInteger qInv = bigInteger2.ModInverse(bigInteger);

            return(new AsymmetricCipherKeyPair(new RsaKeyParameters(false, bigInteger4, publicExponent), new RsaPrivateCrtKeyParameters(bigInteger4, publicExponent, bigInteger8, bigInteger, bigInteger2, dP, dQ, qInv)));
        }
コード例 #2
0
        private static BigInteger GeneratePrivateKey(BigInteger q, SecureRandom random)
        {
            int        num = q.BitLength >> 2;
            BigInteger bigInteger;

            do
            {
                bigInteger = BigIntegers.CreateRandomInRange(DsaKeyPairGenerator.One, q.Subtract(DsaKeyPairGenerator.One), random);
            }while (WNafUtilities.GetNafWeight(bigInteger) < num);
            return(bigInteger);
        }
コード例 #3
0
        public virtual AsymmetricCipherKeyPair GenerateKeyPair()
        {
            int        num;
            BigInteger integer3;

Label_0005:
            num = this.parameters.Strength;
            int        bitlength      = (num + 1) / 2;
            int        num3           = num - bitlength;
            int        num4           = num / 3;
            int        num5           = num >> 2;
            BigInteger publicExponent = this.parameters.PublicExponent;
            BigInteger n = this.ChooseRandomPrime(bitlength, publicExponent);

Label_0041:
            integer3 = this.ChooseRandomPrime(num3, publicExponent);
            if (integer3.Subtract(n).Abs().BitLength < num4)
            {
                goto Label_0041;
            }
            BigInteger k = n.Multiply(integer3);

            if (k.BitLength != num)
            {
                n = n.Max(integer3);
                goto Label_0041;
            }
            if (WNafUtilities.GetNafWeight(k) < num5)
            {
                n = this.ChooseRandomPrime(bitlength, publicExponent);
                goto Label_0041;
            }
            if (n.CompareTo(integer3) < 0)
            {
                BigInteger integer6 = n;
                n        = integer3;
                integer3 = integer6;
            }
            BigInteger integer7        = n.Subtract(One);
            BigInteger integer8        = integer3.Subtract(One);
            BigInteger val             = integer7.Gcd(integer8);
            BigInteger m               = integer7.Divide(val).Multiply(integer8);
            BigInteger privateExponent = publicExponent.ModInverse(m);

            if (privateExponent.BitLength <= num3)
            {
                goto Label_0005;
            }
            BigInteger dP   = privateExponent.Remainder(integer7);
            BigInteger dQ   = privateExponent.Remainder(integer8);
            BigInteger qInv = integer3.ModInverse(n);

            return(new AsymmetricCipherKeyPair(new RsaKeyParameters(false, k, publicExponent), new RsaPrivateCrtKeyParameters(k, publicExponent, privateExponent, n, integer3, dP, dQ, qInv)));
        }
コード例 #4
0
        private static BigInteger GeneratePrivateKey(BigInteger q, SecureRandom random)
        {
            BigInteger integer;
            int        num = q.BitLength >> 2;

            do
            {
                integer = BigIntegers.CreateRandomInRange(One, q.Subtract(One), random);
            }while (WNafUtilities.GetNafWeight(integer) < num);
            return(integer);
        }
コード例 #5
0
        public AsymmetricCipherKeyPair GenerateKeyPair()
        {
            SecureRandom       random         = param.Random;
            Gost3410Parameters gost3410Params = param.Parameters;

            BigInteger q = gost3410Params.Q, x;

            int minWeight = 64;

            for (;;)
            {
                x = new BigInteger(256, random);

                if (x.SignValue < 1 || x.CompareTo(q) >= 0)
                {
                    continue;
                }

                /*
                 * Require a minimum weight of the NAF representation, since low-weight primes may be
                 * weak against a version of the number-field-sieve for the discrete-logarithm-problem.
                 *
                 * See "The number field sieve for integers of low weight", Oliver Schirokauer.
                 */
                if (WNafUtilities.GetNafWeight(x) < minWeight)
                {
                    continue;
                }

                break;
            }

            BigInteger p = gost3410Params.P;
            BigInteger a = gost3410Params.A;

            // calculate the public key.
            BigInteger y = a.ModPow(x, p);

            if (param.PublicKeyParamSet != null)
            {
                return(new AsymmetricCipherKeyPair(
                           new Gost3410PublicKeyParameters(y, param.PublicKeyParamSet),
                           new Gost3410PrivateKeyParameters(x, param.PublicKeyParamSet)));
            }

            return(new AsymmetricCipherKeyPair(
                       new Gost3410PublicKeyParameters(y, gost3410Params),
                       new Gost3410PrivateKeyParameters(x, gost3410Params)));
        }
コード例 #6
0
    protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
    {
        int             num             = Math.Max(2, Math.Min(16, GetWindowSize(k.BitLength)));
        WNafPreCompInfo wNafPreCompInfo = WNafUtilities.Precompute(p, num, includeNegated: true);

        ECPoint[] preComp    = wNafPreCompInfo.PreComp;
        ECPoint[] preCompNeg = wNafPreCompInfo.PreCompNeg;
        int[]     array      = WNafUtilities.GenerateCompactWindowNaf(num, k);
        ECPoint   eCPoint    = p.Curve.Infinity;
        int       num2       = array.Length;

        if (num2 > 1)
        {
            int       num3   = array[--num2];
            int       num4   = num3 >> 16;
            int       num5   = num3 & 0xFFFF;
            int       num6   = Math.Abs(num4);
            ECPoint[] array2 = (num4 < 0) ? preCompNeg : preComp;
            if (num6 << 2 < 1 << num)
            {
                int num7  = LongArray.BitLengths[num6];
                int num8  = num - num7;
                int num9  = num6 ^ (1 << num7 - 1);
                int num10 = (1 << num - 1) - 1;
                int num11 = (num9 << num8) + 1;
                eCPoint = array2[num10 >> 1].Add(array2[num11 >> 1]);
                num5   -= num8;
            }
            else
            {
                eCPoint = array2[num6 >> 1];
            }
            eCPoint = eCPoint.TimesPow2(num5);
        }
        while (num2 > 0)
        {
            int       num12  = array[--num2];
            int       num13  = num12 >> 16;
            int       e      = num12 & 0xFFFF;
            int       num14  = Math.Abs(num13);
            ECPoint[] array3 = (num13 < 0) ? preCompNeg : preComp;
            ECPoint   b      = array3[num14 >> 1];
            eCPoint = eCPoint.TwicePlus(b);
            eCPoint = eCPoint.TimesPow2(e);
        }
        return(eCPoint);
    }
コード例 #7
0
        internal BigInteger CalculatePrivate(
            DHParameters dhParams,
            SecureRandom random)
        {
            int limit = dhParams.L;

            if (limit != 0)
            {
                int minWeight = limit >> 2;
                for (;;)
                {
                    BigInteger x = new BigInteger(limit, random).SetBit(limit - 1);
                    if (WNafUtilities.GetNafWeight(x) >= minWeight)
                    {
                        return(x);
                    }
                }
            }

            BigInteger min = BigInteger.Two;
            int        m   = dhParams.M;

            if (m != 0)
            {
                min = BigInteger.One.ShiftLeft(m - 1);
            }

            BigInteger q = dhParams.Q;

            if (q == null)
            {
                q = dhParams.P;
            }
            BigInteger max = q.Subtract(BigInteger.Two);

            {
                int minWeight = max.BitLength >> 2;
                for (;;)
                {
                    BigInteger x = BigIntegers.CreateRandomInRange(min, max, random);
                    if (WNafUtilities.GetNafWeight(x) >= minWeight)
                    {
                        return(x);
                    }
                }
            }
        }
コード例 #8
0
        private static BigInteger GeneratePrivateKey(BigInteger q, SecureRandom random)
        {
            // B.1.2 Key Pair Generation by Testing Candidates
            int minWeight = q.BitLength >> 2;
            for (;;)
            {
                // TODO Prefer this method? (change test cases that used fixed random)
                // B.1.1 Key Pair Generation Using Extra Random Bits
                //BigInteger x = new BigInteger(q.BitLength + 64, random).Mod(q.Subtract(One)).Add(One);

                BigInteger x = BigIntegers.CreateRandomInRange(One, q.Subtract(One), random);
                if (WNafUtilities.GetNafWeight(x) >= minWeight)
                {
                    return x;
                }
            }
        }
コード例 #9
0
    protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
    {
        int[]   array    = WNafUtilities.GenerateCompactNaf(k);
        ECPoint eCPoint  = p.Curve.Infinity;
        ECPoint eCPoint2 = p;
        int     num      = 0;

        foreach (int num2 in array)
        {
            int num3 = num2 >> 16;
            num     += (num2 & 0xFFFF);
            eCPoint2 = eCPoint2.TimesPow2(num);
            eCPoint  = eCPoint.Add((num3 < 0) ? eCPoint2.Negate() : eCPoint2);
            num      = 1;
        }
        return(eCPoint);
    }
コード例 #10
0
    internal static ECPoint ImplSumOfMultiplies(ECPoint[] ps, BigInteger[] ks)
    {
        int num = ps.Length;

        bool[]            array  = new bool[num];
        WNafPreCompInfo[] array2 = new WNafPreCompInfo[num];
        byte[][]          array3 = new byte[num][];
        for (int i = 0; i < num; i++)
        {
            BigInteger bigInteger = ks[i];
            array[i]   = (bigInteger.SignValue < 0);
            bigInteger = bigInteger.Abs();
            int width = Math.Max(2, Math.Min(16, WNafUtilities.GetWindowSize(bigInteger.BitLength)));
            array2[i] = WNafUtilities.Precompute(ps[i], width, includeNegated: true);
            array3[i] = WNafUtilities.GenerateWindowNaf(width, bigInteger);
        }
        return(ImplSumOfMultiplies(array, array2, array3));
    }
コード例 #11
0
        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
        {
            int[]   array    = WNafUtilities.GenerateCompactNaf(k);
            ECPoint eCPoint  = p.Normalize();
            ECPoint eCPoint2 = eCPoint.Negate();
            ECPoint eCPoint3 = p.Curve.Infinity;
            int     num      = array.Length;

            while (--num >= 0)
            {
                int num2 = array[num];
                int num3 = num2 >> 16;
                int e    = num2 & 0xFFFF;
                eCPoint3 = eCPoint3.TwicePlus((num3 < 0) ? eCPoint2 : eCPoint);
                eCPoint3 = eCPoint3.TimesPow2(e);
            }
            return(eCPoint3);
        }
コード例 #12
0
        internal static ECPoint ImplSumOfMultiplies(ECPoint[] ps, BigInteger[] ks)
        {
            int length = ps.Length;

            bool[]            negs  = new bool[length];
            WNafPreCompInfo[] infos = new WNafPreCompInfo[length];
            byte[][]          wnafs = new byte[length][];
            for (int i = 0; i < length; i++)
            {
                BigInteger k = ks[i];
                negs[i] = k.SignValue < 0;
                k       = k.Abs();
                int width = Math.Max(2, Math.Min(0x10, WNafUtilities.GetWindowSize(k.BitLength)));
                infos[i] = WNafUtilities.Precompute(ps[i], width, true);
                wnafs[i] = WNafUtilities.GenerateWindowNaf(width, k);
            }
            return(ImplSumOfMultiplies(negs, infos, wnafs));
        }
コード例 #13
0
ファイル: ECAlgorithms.cs プロジェクト: checkymander/sshiva
        internal static ECPoint ImplSumOfMultiplies(ECPoint[] ps, BigInteger[] ks)
        {
            int count = ps.Length;

            bool[]            negs  = new bool[count];
            WNafPreCompInfo[] infos = new WNafPreCompInfo[count];
            byte[][]          wnafs = new byte[count][];

            for (int i = 0; i < count; ++i)
            {
                BigInteger ki = ks[i]; negs[i] = ki.SignValue < 0; ki = ki.Abs();

                int width = System.Math.Max(2, System.Math.Min(16, WNafUtilities.GetWindowSize(ki.BitLength)));
                infos[i] = WNafUtilities.Precompute(ps[i], width, true);
                wnafs[i] = WNafUtilities.GenerateWindowNaf(width, ki);
            }

            return(ImplSumOfMultiplies(negs, infos, wnafs));
        }
コード例 #14
0
        public AsymmetricCipherKeyPair GenerateKeyPair()
        {
            SecureRandom       random         = param.Random;
            Gost3410Parameters gost3410Params = param.Parameters;

            BigInteger q = gost3410Params.Q, x;

            int minWeight = 64;

            for (;;)
            {
                x = new BigInteger(256, random);

                if (x.SignValue < 1 || x.CompareTo(q) >= 0)
                {
                    continue;
                }

                if (WNafUtilities.GetNafWeight(x) < minWeight)
                {
                    continue;
                }

                break;
            }

            BigInteger p = gost3410Params.P;
            BigInteger a = gost3410Params.A;

            // calculate the public key.
            BigInteger y = a.ModPow(x, p);

            if (param.PublicKeyParamSet != null)
            {
                return(new AsymmetricCipherKeyPair(
                           new Gost3410PublicKeyParameters(y, param.PublicKeyParamSet),
                           new Gost3410PrivateKeyParameters(x, param.PublicKeyParamSet)));
            }

            return(new AsymmetricCipherKeyPair(
                       new Gost3410PublicKeyParameters(y, gost3410Params),
                       new Gost3410PrivateKeyParameters(x, gost3410Params)));
        }
コード例 #15
0
        public static byte[] GeneratePrivateKey(SecureRandom random = null)
        {
            if (random == null)
            {
                random = new SecureRandom();
            }

            // generates a private key that will always
            // have 32 bits of info and have a a positive
            // public key Y coordinate

            BigInteger n = domainParms.N;
            BigInteger d;
            int        minWeight = n.BitLength / 4;

            for (; ;)
            {
                var bytes = new byte[32];
                random.NextBytes(bytes);
                while (bytes[0] > 127 || bytes[0] == 0)
                {
                    bytes[0] = (byte)random.Next();
                }
                d = new BigInteger(bytes);
                var dbytes = d.ToByteArray();

                var nafWeight = WNafUtilities.GetNafWeight(d);
                if (nafWeight < minWeight)
                {
                    continue;
                }

                var     m = new FixedPointCombMultiplier();
                ECPoint q = m.Multiply(domainParms.G, d).Normalize();

                bool isOdd = q.AffineYCoord.TestBitZero();

                if (!isOdd)
                {
                    return(dbytes);
                }
            }
        }
コード例 #16
0
        /**
         * Given the domain parameters this routine generates an EC key
         * pair in accordance with X9.62 section 5.2.1 pages 26, 27.
         */
        public AsymmetricCipherKeyPair GenerateKeyPair()
        {
            BigInteger n = parameters.N;
            BigInteger d;
            int        minWeight = n.BitLength >> 2;

            for (;;)
            {
                d = new BigInteger(n.BitLength, random);

                if (d.CompareTo(BigInteger.Two) < 0 || d.CompareTo(n) >= 0)
                {
                    continue;
                }

                /*
                 * Require a minimum weight of the NAF representation, since low-weight primes may be
                 * weak against a version of the number-field-sieve for the discrete-logarithm-problem.
                 *
                 * See "The number field sieve for integers of low weight", Oliver Schirokauer.
                 */
                if (WNafUtilities.GetNafWeight(d) < minWeight)
                {
                    continue;
                }

                break;
            }

            ECPoint q = CreateBasePointMultiplier().Multiply(parameters.G, d);

            if (publicKeyParamSet != null)
            {
                return(new AsymmetricCipherKeyPair(
                           new ECPublicKeyParameters(algorithm, q, publicKeyParamSet),
                           new ECPrivateKeyParameters(algorithm, d, publicKeyParamSet)));
            }

            return(new AsymmetricCipherKeyPair(
                       new ECPublicKeyParameters(algorithm, q, parameters),
                       new ECPrivateKeyParameters(algorithm, d, parameters)));
        }
コード例 #17
0
    internal static ECPoint ImplShamirsTrickWNaf(ECPoint P, BigInteger k, ECPoint Q, BigInteger l)
    {
        bool flag  = k.SignValue < 0;
        bool flag2 = l.SignValue < 0;

        k = k.Abs();
        l = l.Abs();
        int             width            = Math.Max(2, Math.Min(16, WNafUtilities.GetWindowSize(k.BitLength)));
        int             width2           = Math.Max(2, Math.Min(16, WNafUtilities.GetWindowSize(l.BitLength)));
        WNafPreCompInfo wNafPreCompInfo  = WNafUtilities.Precompute(P, width, includeNegated: true);
        WNafPreCompInfo wNafPreCompInfo2 = WNafUtilities.Precompute(Q, width2, includeNegated: true);

        ECPoint[] preCompP    = flag ? wNafPreCompInfo.PreCompNeg : wNafPreCompInfo.PreComp;
        ECPoint[] preCompQ    = flag2 ? wNafPreCompInfo2.PreCompNeg : wNafPreCompInfo2.PreComp;
        ECPoint[] preCompNegP = flag ? wNafPreCompInfo.PreComp : wNafPreCompInfo.PreCompNeg;
        ECPoint[] preCompNegQ = flag2 ? wNafPreCompInfo2.PreComp : wNafPreCompInfo2.PreCompNeg;
        byte[]    wnafP       = WNafUtilities.GenerateWindowNaf(width, k);
        byte[]    wnafQ       = WNafUtilities.GenerateWindowNaf(width2, l);
        return(ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ));
    }
コード例 #18
0
        internal static ECPoint ImplShamirsTrickWNaf(ECPoint P, BigInteger k, ECPointMap pointMapQ, BigInteger l)
        {
            bool flag  = k.SignValue < 0;
            bool flag2 = l.SignValue < 0;

            k = k.Abs();
            l = l.Abs();
            int             width           = Math.Max(2, Math.Min(0x10, WNafUtilities.GetWindowSize(Math.Max(k.BitLength, l.BitLength))));
            ECPoint         p               = WNafUtilities.MapPointWithPrecomp(P, width, true, pointMapQ);
            WNafPreCompInfo wNafPreCompInfo = WNafUtilities.GetWNafPreCompInfo(P);
            WNafPreCompInfo info2           = WNafUtilities.GetWNafPreCompInfo(p);

            ECPoint[] preCompP    = !flag ? wNafPreCompInfo.PreComp : wNafPreCompInfo.PreCompNeg;
            ECPoint[] preCompQ    = !flag2 ? info2.PreComp : info2.PreCompNeg;
            ECPoint[] preCompNegP = !flag ? wNafPreCompInfo.PreCompNeg : wNafPreCompInfo.PreComp;
            ECPoint[] preCompNegQ = !flag2 ? info2.PreCompNeg : info2.PreComp;
            byte[]    wnafP       = WNafUtilities.GenerateWindowNaf(width, k);
            byte[]    wnafQ       = WNafUtilities.GenerateWindowNaf(width, l);
            return(ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ));
        }
コード例 #19
0
    internal static ECPoint ImplShamirsTrickJsf(ECPoint P, BigInteger k, ECPoint Q, BigInteger l)
    {
        ECCurve curve    = P.Curve;
        ECPoint infinity = curve.Infinity;
        ECPoint eCPoint  = P.Add(Q);
        ECPoint eCPoint2 = P.Subtract(Q);

        ECPoint[] array = new ECPoint[4]
        {
            Q,
            eCPoint2,
            P,
            eCPoint
        };
        curve.NormalizeAll(array);
        ECPoint[] array2 = new ECPoint[9]
        {
            array[3].Negate(),
            array[2].Negate(),
            array[1].Negate(),
            array[0].Negate(),
            infinity,
            array[0],
            array[1],
            array[2],
            array[3]
        };
        byte[]  array3   = WNafUtilities.GenerateJsf(k, l);
        ECPoint eCPoint3 = infinity;
        int     num      = array3.Length;

        while (--num >= 0)
        {
            int num2 = array3[num];
            int num3 = num2 << 24 >> 28;
            int num4 = num2 << 28 >> 28;
            int num5 = 4 + num3 * 3 + num4;
            eCPoint3 = eCPoint3.TwicePlus(array2[num5]);
        }
        return(eCPoint3);
    }
コード例 #20
0
        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
        {
            int[] naf = WNafUtilities.GenerateCompactNaf(k);

            ECPoint addP = p.Normalize(), subP = addP.Negate();

            ECPoint R = p.Curve.Infinity;

            int i = naf.Length;

            while (--i >= 0)
            {
                int ni = naf[i];
                int digit = ni >> 16, zeroes = ni & 0xFFFF;

                R = R.TwicePlus(digit < 0 ? subP : addP);
                R = R.TimesPow2(zeroes);
            }

            return(R);
        }
コード例 #21
0
        protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
        {
            int[] naf = WNafUtilities.GenerateCompactNaf(k);

            ECPoint R0 = p.Curve.Infinity, R1 = p;

            int zeroes = 0;

            for (int i = 0; i < naf.Length; ++i)
            {
                int ni    = naf[i];
                int digit = ni >> 16;
                zeroes += ni & 0xFFFF;

                R1 = R1.TimesPow2(zeroes);
                R0 = R0.Add(digit < 0 ? R1.Negate() : R1);

                zeroes = 1;
            }

            return(R0);
        }
コード例 #22
0
    public AsymmetricCipherKeyPair GenerateKeyPair()
    {
        SecureRandom       random     = param.Random;
        Gost3410Parameters parameters = param.Parameters;
        BigInteger         q          = parameters.Q;
        int        num = 64;
        BigInteger bigInteger;

        do
        {
            bigInteger = new BigInteger(256, random);
        }while (bigInteger.SignValue < 1 || bigInteger.CompareTo(q) >= 0 || WNafUtilities.GetNafWeight(bigInteger) < num);
        BigInteger p = parameters.P;
        BigInteger a = parameters.A;
        BigInteger y = a.ModPow(bigInteger, p);

        if (param.PublicKeyParamSet != null)
        {
            return(new AsymmetricCipherKeyPair(new Gost3410PublicKeyParameters(y, param.PublicKeyParamSet), new Gost3410PrivateKeyParameters(bigInteger, param.PublicKeyParamSet)));
        }
        return(new AsymmetricCipherKeyPair(new Gost3410PublicKeyParameters(y, parameters), new Gost3410PrivateKeyParameters(bigInteger, parameters)));
    }
コード例 #23
0
        internal static ECPoint ImplShamirsTrickJsf(ECPoint P, BigInteger k, ECPoint Q, BigInteger l)
        {
            var curve    = P.Curve;
            var infinity = curve.Infinity;

            // TODO conjugate co-Z addition (ZADDC) can return both of these
            var PaddQ = P.Add(Q);
            var PsubQ = P.Subtract(Q);

            var points = new[] { Q, PsubQ, P, PaddQ };

            curve.NormalizeAll(points);

            var table = new[]
            {
                points[3].Negate(), points[2].Negate(), points[1].Negate(),
                points[0].Negate(), infinity, points[0],
                points[1], points[2], points[3]
            };

            var jsf = WNafUtilities.GenerateJsf(k, l);

            var R = infinity;

            var i = jsf.Length;

            while (--i >= 0)
            {
                int jsfi = jsf[i];

                // NOTE: The shifting ensures the sign is extended correctly
                int kDigit = (jsfi << 24) >> 28, lDigit = (jsfi << 28) >> 28;

                var index = 4 + kDigit * 3 + lDigit;
                R = R.TwicePlus(table[index]);
            }

            return(R);
        }
コード例 #24
0
        /**
         * Given the domain parameters this routine generates an EC key
         * pair in accordance with X9.62 section 5.2.1 pages 26, 27.
         */
        public AsymmetricCipherKeyPair GenerateKeyPair()
        {
            BigInteger n     = parameters.N;
            BigInteger upper = n.Subtract(BigInteger.Two);
            BigInteger d;
            int        minWeight = n.BitLength >> 2;

            for (;;)
            {
                d = new BigInteger(n.BitLength, random);

                if (d.CompareTo(upper) > 0)
                {
                    continue;
                }

                if (WNafUtilities.GetNafWeight(d) < minWeight)
                {
                    continue;
                }

                break;
            }

            d = d.Add(BigInteger.One);

            ECPoint q = CreateBasePointMultiplier().Multiply(parameters.G, d);

            if (publicKeyParamSet != null)
            {
                return(new AsymmetricCipherKeyPair(
                           new ECPublicKeyParameters(algorithm, q, publicKeyParamSet),
                           new ECPrivateKeyParameters(algorithm, d, publicKeyParamSet)));
            }

            return(new AsymmetricCipherKeyPair(
                       new ECPublicKeyParameters(algorithm, q, parameters),
                       new ECPrivateKeyParameters(algorithm, d, parameters)));
        }
コード例 #25
0
        internal static ECPoint ImplShamirsTrickWNaf(ECPoint P, BigInteger k,
                                                     ECPoint Q, BigInteger l)
        {
            bool negK = k.SignValue < 0, negL = l.SignValue < 0;

            BigInteger kAbs = k.Abs(), lAbs = l.Abs();

            int minWidthP = WNafUtilities.GetWindowSize(kAbs.BitLength, 8);
            int minWidthQ = WNafUtilities.GetWindowSize(lAbs.BitLength, 8);

            WNafPreCompInfo infoP = WNafUtilities.Precompute(P, minWidthP, true);
            WNafPreCompInfo infoQ = WNafUtilities.Precompute(Q, minWidthQ, true);

            // When P, Q are 'promoted' (i.e. reused several times), switch to fixed-point algorithm
            {
                ECCurve c        = P.Curve;
                int     combSize = FixedPointUtilities.GetCombSize(c);
                if (!negK && !negL &&
                    k.BitLength <= combSize && l.BitLength <= combSize &&
                    infoP.IsPromoted && infoQ.IsPromoted)
                {
                    return(ImplShamirsTrickFixedPoint(P, k, Q, l));
                }
            }

            int widthP = System.Math.Min(8, infoP.Width);
            int widthQ = System.Math.Min(8, infoQ.Width);

            ECPoint[] preCompP    = negK ? infoP.PreCompNeg : infoP.PreComp;
            ECPoint[] preCompQ    = negL ? infoQ.PreCompNeg : infoQ.PreComp;
            ECPoint[] preCompNegP = negK ? infoP.PreComp : infoP.PreCompNeg;
            ECPoint[] preCompNegQ = negL ? infoQ.PreComp : infoQ.PreCompNeg;

            byte[] wnafP = WNafUtilities.GenerateWindowNaf(widthP, kAbs);
            byte[] wnafQ = WNafUtilities.GenerateWindowNaf(widthQ, lAbs);

            return(ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ));
        }
コード例 #26
0
ファイル: ECAlgorithms.cs プロジェクト: checkymander/sshiva
        internal static ECPoint ImplShamirsTrickWNaf(ECPoint P, BigInteger k, ECPointMap pointMapQ, BigInteger l)
        {
            bool negK = k.SignValue < 0, negL = l.SignValue < 0;

            k = k.Abs();
            l = l.Abs();

            int width = System.Math.Max(2, System.Math.Min(16, WNafUtilities.GetWindowSize(System.Math.Max(k.BitLength, l.BitLength))));

            ECPoint         Q     = WNafUtilities.MapPointWithPrecomp(P, width, true, pointMapQ);
            WNafPreCompInfo infoP = WNafUtilities.GetWNafPreCompInfo(P);
            WNafPreCompInfo infoQ = WNafUtilities.GetWNafPreCompInfo(Q);

            ECPoint[] preCompP    = negK ? infoP.PreCompNeg : infoP.PreComp;
            ECPoint[] preCompQ    = negL ? infoQ.PreCompNeg : infoQ.PreComp;
            ECPoint[] preCompNegP = negK ? infoP.PreComp : infoP.PreCompNeg;
            ECPoint[] preCompNegQ = negL ? infoQ.PreComp : infoQ.PreCompNeg;

            byte[] wnafP = WNafUtilities.GenerateWindowNaf(width, k);
            byte[] wnafQ = WNafUtilities.GenerateWindowNaf(width, l);

            return(ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ));
        }
コード例 #27
0
        internal BigInteger CalculatePrivate(DHParameters dhParams, SecureRandom random)
        {
            BigInteger integer5;
            int        l = dhParams.L;

            if (l != 0)
            {
                BigInteger integer;
                int        num2 = l >> 2;
                do
                {
                    integer = new BigInteger(l, random).SetBit(l - 1);
                }while (WNafUtilities.GetNafWeight(integer) < num2);
                return(integer);
            }
            BigInteger two = BigInteger.Two;
            int        m   = dhParams.M;

            if (m != 0)
            {
                two = BigInteger.One.ShiftLeft(m - 1);
            }
            BigInteger q = dhParams.Q;

            if (q == null)
            {
                q = dhParams.P;
            }
            BigInteger max  = q.Subtract(BigInteger.Two);
            int        num4 = max.BitLength >> 2;

            do
            {
                integer5 = BigIntegers.CreateRandomInRange(two, max, random);
            }while (WNafUtilities.GetNafWeight(integer5) < num4);
            return(integer5);
        }
コード例 #28
0
        internal BigInteger CalculatePrivate(DHParameters dhParams, SecureRandom random)
        {
            int l = dhParams.L;

            if (l != 0)
            {
                int        num = l >> 2;
                BigInteger bigInteger;
                do
                {
                    bigInteger = new BigInteger(l, random).SetBit(l - 1);
                }while (WNafUtilities.GetNafWeight(bigInteger) < num);
                return(bigInteger);
            }
            BigInteger min = BigInteger.Two;
            int        m   = dhParams.M;

            if (m != 0)
            {
                min = BigInteger.One.ShiftLeft(m - 1);
            }
            BigInteger bigInteger2 = dhParams.Q;

            if (bigInteger2 == null)
            {
                bigInteger2 = dhParams.P;
            }
            BigInteger bigInteger3 = bigInteger2.Subtract(BigInteger.Two);
            int        num2        = bigInteger3.BitLength >> 2;
            BigInteger bigInteger4;

            do
            {
                bigInteger4 = BigIntegers.CreateRandomInRange(min, bigInteger3, random);
            }while (WNafUtilities.GetNafWeight(bigInteger4) < num2);
            return(bigInteger4);
        }
コード例 #29
0
        internal static ECPoint ImplShamirsTrickJsf(ECPoint P, BigInteger k, ECPoint Q, BigInteger l)
        {
            ECCurve curve    = P.Curve;
            ECPoint infinity = curve.Infinity;
            ECPoint point2   = P.Add(Q);
            ECPoint point3   = P.Subtract(Q);

            ECPoint[] points = new ECPoint[] { Q, point3, P, point2 };
            curve.NormalizeAll(points);
            ECPoint[] pointArray2 = new ECPoint[] { points[3].Negate(), points[2].Negate(), points[1].Negate(), points[0].Negate(), infinity, points[0], points[1], points[2], points[3] };
            byte[]    buffer      = WNafUtilities.GenerateJsf(k, l);
            ECPoint   point4      = infinity;
            int       length      = buffer.Length;

            while (--length >= 0)
            {
                int num2  = buffer[length];
                int num3  = (num2 << 0x18) >> 0x1c;
                int num4  = (num2 << 0x1c) >> 0x1c;
                int index = (4 + (num3 * 3)) + num4;
                point4 = point4.TwicePlus(pointArray2[index]);
            }
            return(point4);
        }
コード例 #30
0
    public BigInteger GenerateRandom()
    {
        BigInteger d;
        int        minWeight = curve.N.BitLength >> 2;

        for (; ;)
        {
            d = new BigInteger(curve.N.BitLength, random);

            if (d.CompareTo(BigInteger.Two) < 0 || d.CompareTo(curve.N) >= 0)
            {
                continue;
            }

            if (WNafUtilities.GetNafWeight(d) < minWeight)
            {
                continue;
            }

            break;
        }

        return(d);
    }