public ECKeyGenerationParameters(
     DerObjectIdentifier	publicKeyParamSet,
     SecureRandom		random)
     : this(ECKeyParameters.LookupParameters(publicKeyParamSet), random)
 {
     this.publicKeyParamSet = publicKeyParamSet;
 }
 public DsaKeyGenerationParameters(
     SecureRandom	random,
     DsaParameters	parameters)
     : base(random, parameters.P.BitLength - 1)
 {
     this.parameters = parameters;
 }
 public ElGamalKeyGenerationParameters(
     SecureRandom		random,
     ElGamalParameters	parameters)
     : base(random, GetStrength(parameters))
 {
     this.parameters = parameters;
 }
 public ECKeyGenerationParameters(
     ECDomainParameters	domainParameters,
     SecureRandom		random)
     : base(random, domainParameters.N.BitLength)
 {
     this.domainParams = domainParameters;
 }
        /**
        * Return a random BigInteger not less than 'min' and not greater than 'max'
        *
        * @param min the least value that may be generated
        * @param max the greatest value that may be generated
        * @param random the source of randomness
        * @return a random BigInteger value in the range [min,max]
        */
        public static BigInteger CreateRandomInRange(
            BigInteger		min,
            BigInteger		max,
            // TODO Should have been just Random class
            SecureRandom	random)
        {
            int cmp = min.CompareTo(max);
            if (cmp >= 0)
            {
                if (cmp > 0)
                    throw new ArgumentException("'min' may not be greater than 'max'");

                return min;
            }

            if (min.BitLength > max.BitLength / 2)
            {
                return CreateRandomInRange(BigInteger.Zero, max.Subtract(min), random).Add(min);
            }

            for (int i = 0; i < MaxIterations; ++i)
            {
                BigInteger x = new BigInteger(max.BitLength, random);
                if (x.CompareTo(min) >= 0 && x.CompareTo(max) <= 0)
                {
                    return x;
                }
            }

            // fall back to a faster (restricted) method
            return new BigInteger(max.Subtract(min).BitLength - 1, random).Add(min);
        }
        /**
        * initialise the ElGamal engine.
        *
        * @param forEncryption true if we are encrypting, false otherwise.
        * @param param the necessary ElGamal key parameters.
        */
        public void Init(
            bool				forEncryption,
            ICipherParameters	parameters)
        {
            if (parameters is ParametersWithRandom)
            {
                ParametersWithRandom p = (ParametersWithRandom) parameters;

                this.key = (ElGamalKeyParameters) p.Parameters;
                this.random = p.Random;
            }
            else
            {
                this.key = (ElGamalKeyParameters) parameters;
                this.random = new SecureRandom();
            }

            this.forEncryption = forEncryption;
            this.bitSize = key.Parameters.P.BitLength;

            if (forEncryption)
            {
                if (!(key is ElGamalPublicKeyParameters))
                {
                    throw new ArgumentException("ElGamalPublicKeyParameters are required for encryption.");
                }
            }
            else
            {
                if (!(key is ElGamalPrivateKeyParameters))
                {
                    throw new ArgumentException("ElGamalPrivateKeyParameters are required for decryption.");
                }
            }
        }
        public void Init(
            ICipherParameters parameters)
        {
            AsymmetricKeyParameter kParam;
            if (parameters is ParametersWithRandom)
            {
                ParametersWithRandom rParam = (ParametersWithRandom)parameters;

                this.random = rParam.Random;
                kParam = (AsymmetricKeyParameter)rParam.Parameters;
            }
            else
            {
                this.random = new SecureRandom();
                kParam = (AsymmetricKeyParameter)parameters;
            }

            if (!(kParam is DHPrivateKeyParameters))
            {
                throw new ArgumentException("DHEngine expects DHPrivateKeyParameters");
            }

            this.key = (DHPrivateKeyParameters)kParam;
            this.dhParams = key.Parameters;
        }
 public Gost3410KeyGenerationParameters(
     SecureRandom random,
     Gost3410Parameters parameters)
     : base(random, parameters.P.BitLength - 1)
 {
     this.parameters = parameters;
 }
 public Gost3410KeyGenerationParameters(
     SecureRandom		random,
     DerObjectIdentifier	publicKeyParamSet)
     : this(random, LookupParameters(publicKeyParamSet))
 {
     this.publicKeyParamSet = publicKeyParamSet;
 }
 /**
  * Parameters for generating a NaccacheStern KeyPair.
  *
  * @param random
  *            The source of randomness
  * @param strength
  *            The desired strength of the Key in Bits
  * @param certainty
  *            the probability that the generated primes are not really prime
  *            as integer: 2^(-certainty) is then the probability
  * @param countSmallPrimes
  *            How many small key factors are desired
  */
 public NaccacheSternKeyGenerationParameters(
     SecureRandom	random,
     int				strength,
     int				certainty,
     int				countSmallPrimes)
     : this(random, strength, certainty, countSmallPrimes, false)
 {
 }
 /**
  * Construct for a specific usage index - this has the effect of using verifiable canonical generation of G.
  *
  * @param L desired length of prime P in bits (the effective key size).
  * @param N desired length of prime Q in bits.
  * @param certainty certainty level for prime number generation.
  * @param random the source of randomness to use.
  * @param usageIndex a valid usage index.
  */
 public DsaParameterGenerationParameters(int L, int N, int certainty, SecureRandom random, int usageIndex)
 {
     this.l = L;
     this.n = N;
     this.certainty = certainty;
     this.random = random;
     this.usageIndex = usageIndex;
 }
 public virtual void Init(
     int				size,
     int				certainty,
     SecureRandom	random)
 {
     this.size = size;
     this.certainty = certainty;
     this.random = random;
 }
 public RsaKeyGenerationParameters(
     BigInteger		publicExponent,
     SecureRandom	random,
     int				strength,
     int				certainty)
     : base(random, strength)
 {
     this.publicExponent = publicExponent;
     this.certainty = certainty;
 }
        /**
         * initialise the generator with a source of randomness
         * and a strength (in bits).
         *
         * @param random the random byte source.
         * @param strength the size, in bits, of the keys we want to produce.
         */
        public KeyGenerationParameters(
            SecureRandom	random,
            int				strength)
        {
            if (random == null)
                throw new ArgumentNullException("random");
            if (strength < 1)
                throw new ArgumentException("strength must be a positive value", "strength");

            this.random = random;
            this.strength = strength;
        }
        public ParametersWithRandom(
            ICipherParameters	parameters,
            SecureRandom		random)
        {
            if (parameters == null)
                throw new ArgumentNullException("random");
            if (random == null)
                throw new ArgumentNullException("random");

            this.parameters = parameters;
            this.random = random;
        }
        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;
                }
            }
        }
        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;
                    }
                }
            }
        }
        /**
            * Method init
            *
            * @param forWrapping
            * @param param
            */
        public void Init(
            bool				forWrapping,
            ICipherParameters	parameters)
        {
            this.forWrapping = forWrapping;
            this.engine = new CbcBlockCipher(new RC2Engine());

            if (parameters is ParametersWithRandom)
            {
                ParametersWithRandom pWithR = (ParametersWithRandom)parameters;
                sr = pWithR.Random;
                parameters = pWithR.Parameters;
            }
            else
            {
                sr = new SecureRandom();
            }

            if (parameters is ParametersWithIV)
            {
                if (!forWrapping)
                    throw new ArgumentException("You should not supply an IV for unwrapping");

                this.paramPlusIV = (ParametersWithIV)parameters;
                this.iv = this.paramPlusIV.GetIV();
                this.parameters = this.paramPlusIV.Parameters;

                if (this.iv.Length != 8)
                    throw new ArgumentException("IV is not 8 octets");
            }
            else
            {
                this.parameters = parameters;

                if (this.forWrapping)
                {
                    // Hm, we have no IV but we want to wrap ?!?
                    // well, then we have to create our own IV.
                    this.iv = new byte[8];
                    sr.NextBytes(iv);
                    this.paramPlusIV = new ParametersWithIV(this.parameters, this.iv);
                }
            }
        }
        /**
        * Initialise the factor generator
        *
        * @param param the necessary RSA key parameters.
        */
        public void Init(
            ICipherParameters param)
        {
            if (param is ParametersWithRandom)
            {
                ParametersWithRandom rParam = (ParametersWithRandom)param;

                key = (RsaKeyParameters)rParam.Parameters;
                random = rParam.Random;
            }
            else
            {
                key = (RsaKeyParameters)param;
                random = new SecureRandom();
            }

            if (key.IsPrivate)
                throw new ArgumentException("generator requires RSA public key");
        }
 /**
  * Parameters for a NaccacheStern KeyPair.
  *
  * @param random
  *            The source of randomness
  * @param strength
  *            The desired strength of the Key in Bits
  * @param certainty
  *            the probability that the generated primes are not really prime
  *            as integer: 2^(-certainty) is then the probability
  * @param cntSmallPrimes
  *            How many small key factors are desired
  * @param debug
  *            Turn debugging on or off (reveals secret information, use with
  *            caution)
  */
 public NaccacheSternKeyGenerationParameters(SecureRandom random,
     int		strength,
     int		certainty,
     int		countSmallPrimes,
     bool	debug)
     : base(random, strength)
 {
     if (countSmallPrimes % 2 == 1)
     {
         throw new ArgumentException("countSmallPrimes must be a multiple of 2");
     }
     if (countSmallPrimes < 30)
     {
         throw new ArgumentException("countSmallPrimes must be >= 30 for security reasons");
     }
     this.certainty = certainty;
     this.countSmallPrimes = countSmallPrimes;
     this.debug = debug;
 }
        public void Init(
            bool				forWrapping,
            ICipherParameters	param)
        {
            this.forWrapping = forWrapping;

            if (param is ParametersWithRandom)
            {
                ParametersWithRandom p = (ParametersWithRandom) param;

                this.rand = p.Random;
                this.param = (ParametersWithIV) p.Parameters;
            }
            else
            {
                if (forWrapping)
                {
                    rand = new SecureRandom();
                }

                this.param = (ParametersWithIV) param;
            }
        }
 protected virtual void engineInit(
     KeyGenerationParameters parameters)
 {
     this.random = parameters.Random;
     this.strength = (parameters.Strength + 7) / 8;
 }
        public void Init(
            bool				forSigning,
            ICipherParameters	parameters)
        {
            this.forSigning = forSigning;

            if (forSigning)
            {
                if (parameters is ParametersWithRandom)
                {
                    ParametersWithRandom rParam = (ParametersWithRandom) parameters;

                    this.random = rParam.Random;
                    parameters = rParam.Parameters;
                }
                else
                {
                    this.random = new SecureRandom();
                }

                if (!(parameters is ECPrivateKeyParameters))
                    throw new InvalidKeyException("EC private key required for signing");

                this.key = (ECPrivateKeyParameters) parameters;
            }
            else
            {
                if (!(parameters is ECPublicKeyParameters))
                    throw new InvalidKeyException("EC public key required for verification");

                this.key = (ECPublicKeyParameters) parameters;
            }
        }
Example #24
0
        public virtual void Init(bool forSigning, ICipherParameters	parameters)
        {
            SecureRandom providedRandom = null;

            if (forSigning)
            {
                if (parameters is ParametersWithRandom)
                {
                    ParametersWithRandom rParam = (ParametersWithRandom)parameters;

                    providedRandom = rParam.Random;
                    parameters = rParam.Parameters;
                }

                if (!(parameters is DsaPrivateKeyParameters))
                    throw new InvalidKeyException("DSA private key required for signing");

                this.key = (DsaPrivateKeyParameters)parameters;
            }
            else
            {
                if (!(parameters is DsaPublicKeyParameters))
                    throw new InvalidKeyException("DSA public key required for verification");

                this.key = (DsaPublicKeyParameters)parameters;
            }

            this.random = InitSecureRandom(forSigning && !kCalculator.IsDeterministic, providedRandom);
        }
Example #25
0
 protected virtual SecureRandom InitSecureRandom(bool needed, SecureRandom provided)
 {
     return !needed ? null : (provided != null) ? provided : new SecureRandom();
 }
 /**
  * Construct without a usage index, this will do a random construction of G.
  *
  * @param L desired length of prime P in bits (the effective key size).
  * @param N desired length of prime Q in bits.
  * @param certainty certainty level for prime number generation.
  * @param random the source of randomness to use.
  */
 public DsaParameterGenerationParameters(int L, int N, int certainty, SecureRandom random)
     : this(L, N, certainty, random, -1)
 {
 }
        internal ECKeyGenerationParameters CreateKeyGenerationParameters(
            SecureRandom random)
        {
            if (publicKeyParamSet != null)
            {
                return new ECKeyGenerationParameters(publicKeyParamSet, random);
            }

            return new ECKeyGenerationParameters(parameters, random);
        }
 public virtual void Init(BigInteger n, SecureRandom random)
 {
     throw new InvalidOperationException("Operation not supported");
 }
 /**
  * initialise the key generator.
  *
  * @param size size of the key
  * @param typeProcedure type procedure A,B = 1;  A',B' - else
  * @param random random byte source.
  */
 public void Init(
     int             size,
     int             typeProcedure,
     SecureRandom    random)
 {
     this.size = size;
     this.typeproc = typeProcedure;
     this.init_random = random;
 }
        /**
         * initialise the RSA engine.
         *
         * @param forEncryption true if we are encrypting, false otherwise.
         * @param param the necessary RSA key parameters.
         */
        public void Init(
            bool				forEncryption,
            ICipherParameters	param)
        {
            core.Init(forEncryption, param);

            if (param is ParametersWithRandom)
            {
                ParametersWithRandom rParam = (ParametersWithRandom)param;

                key = (RsaKeyParameters)rParam.Parameters;
                random = rParam.Random;
            }
            else
            {
                key = (RsaKeyParameters)param;
                random = new SecureRandom();
            }
        }