Esempio n. 1
0
            internal KeyPairGenerator(KeyGenerationParameters keyGenParameters, SecureRandom random) : base(keyGenParameters)
            {

                this.param = new DHKeyGenerationParameters(random, getDomainParams(keyGenParameters.DomainParameters));
                this.domainParameters = keyGenParameters.DomainParameters;
                this.engine.Init(param);
            }
Esempio n. 2
0
        public static AsymmetricKeyParameter CreateKey(
            SubjectPublicKeyInfo keyInfo)
        {
            AlgorithmIdentifier algID  = keyInfo.AlgorithmID;
            DerObjectIdentifier algOid = algID.Algorithm;

            // TODO See RSAUtil.isRsaOid in Java build
            if (algOid.Equals(PkcsObjectIdentifiers.RsaEncryption) ||
                algOid.Equals(X509ObjectIdentifiers.IdEARsa) ||
                algOid.Equals(PkcsObjectIdentifiers.IdRsassaPss) ||
                algOid.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
            {
                RsaPublicKeyStructure pubKey = RsaPublicKeyStructure.GetInstance(
                    keyInfo.ParsePublicKey());

                return(new RsaKeyParameters(false, pubKey.Modulus, pubKey.PublicExponent));
            }
            else if (algOid.Equals(X9ObjectIdentifiers.DHPublicNumber))
            {
                Asn1Sequence seq = Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object());

                DHPublicKey dhPublicKey = DHPublicKey.GetInstance(keyInfo.ParsePublicKey());

                BigInteger y = dhPublicKey.Y.Value;

                if (IsPkcsDHParam(seq))
                {
                    return(ReadPkcsDHParam(algOid, y, seq));
                }

                DHDomainParameters dhParams = DHDomainParameters.GetInstance(seq);

                BigInteger p = dhParams.P.Value;
                BigInteger g = dhParams.G.Value;
                BigInteger q = dhParams.Q.Value;

                BigInteger j = null;
                if (dhParams.J != null)
                {
                    j = dhParams.J.Value;
                }

                DHValidationParameters validation        = null;
                DHValidationParms      dhValidationParms = dhParams.ValidationParms;
                if (dhValidationParms != null)
                {
                    byte[]     seed        = dhValidationParms.Seed.GetBytes();
                    BigInteger pgenCounter = dhValidationParms.PgenCounter.Value;

                    // TODO Check pgenCounter size?

                    validation = new DHValidationParameters(seed, pgenCounter.IntValue);
                }

                return(new DHPublicKeyParameters(y, new DHParameters(p, g, q, j, validation)));
            }
            else if (algOid.Equals(PkcsObjectIdentifiers.DhKeyAgreement))
            {
                Asn1Sequence seq = Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object());

                DerInteger derY = (DerInteger)keyInfo.ParsePublicKey();

                return(ReadPkcsDHParam(algOid, derY.Value, seq));
            }
            else if (algOid.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
            {
                ElGamalParameter para = new ElGamalParameter(
                    Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object()));
                DerInteger derY = (DerInteger)keyInfo.ParsePublicKey();

                return(new ElGamalPublicKeyParameters(
                           derY.Value,
                           new ElGamalParameters(para.P, para.G)));
            }
            else if (algOid.Equals(X9ObjectIdentifiers.IdDsa) ||
                     algOid.Equals(OiwObjectIdentifiers.DsaWithSha1))
            {
                DerInteger    derY = (DerInteger)keyInfo.ParsePublicKey();
                Asn1Encodable ae   = algID.Parameters;

                DsaParameters parameters = null;
                if (ae != null)
                {
                    DsaParameter para = DsaParameter.GetInstance(ae.ToAsn1Object());
                    parameters = new DsaParameters(para.P, para.Q, para.G);
                }

                return(new DsaPublicKeyParameters(derY.Value, parameters));
            }
            else if (algOid.Equals(X9ObjectIdentifiers.IdECPublicKey))
            {
                X962Parameters para = X962Parameters.GetInstance(algID.Parameters.ToAsn1Object());

                X9ECParameters x9;
                if (para.IsNamedCurve)
                {
                    x9 = ECKeyPairGenerator.FindECCurveByOid((DerObjectIdentifier)para.Parameters);
                }
                else
                {
                    x9 = new X9ECParameters((Asn1Sequence)para.Parameters);
                }

                Asn1OctetString key  = new DerOctetString(keyInfo.PublicKeyData.GetBytes());
                X9ECPoint       derQ = new X9ECPoint(x9.Curve, key);
                ECPoint         q    = derQ.Point;

                if (para.IsNamedCurve)
                {
                    return(new ECPublicKeyParameters("EC", q, (DerObjectIdentifier)para.Parameters));
                }

                ECDomainParameters dParams = new ECDomainParameters(x9);
                return(new ECPublicKeyParameters(q, dParams));
            }
            else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
            {
                Gost3410PublicKeyAlgParameters gostParams        = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters);
                DerObjectIdentifier            publicKeyParamSet = gostParams.PublicKeyParamSet;

                X9ECParameters ecP = ECGost3410NamedCurves.GetByOidX9(publicKeyParamSet);
                if (ecP == null)
                {
                    return(null);
                }

                Asn1OctetString key;
                try
                {
                    key = (Asn1OctetString)keyInfo.ParsePublicKey();
                }
                catch (IOException e)
                {
                    throw new ArgumentException("error recovering GOST3410_2001 public key", e);
                }

                int fieldSize = 32;
                int keySize   = 2 * fieldSize;

                byte[] keyEnc = key.GetOctets();
                if (keyEnc.Length != keySize)
                {
                    throw new ArgumentException("invalid length for GOST3410_2001 public key");
                }

                byte[] x9Encoding = new byte[1 + keySize];
                x9Encoding[0] = 0x04;
                for (int i = 1; i <= fieldSize; ++i)
                {
                    x9Encoding[i]             = keyEnc[fieldSize - i];
                    x9Encoding[i + fieldSize] = keyEnc[keySize - i];
                }

                ECPoint q = ecP.Curve.DecodePoint(x9Encoding);

                return(new ECPublicKeyParameters("ECGOST3410", q, publicKeyParamSet));
            }
            else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x94))
            {
                Gost3410PublicKeyAlgParameters algParams = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters);

                Asn1OctetString key;
                try
                {
                    key = (Asn1OctetString)keyInfo.ParsePublicKey();
                }
                catch (IOException e)
                {
                    throw new ArgumentException("error recovering GOST3410_94 public key", e);
                }

                byte[] keyBytes = Arrays.Reverse(key.GetOctets()); // was little endian

                BigInteger y = new BigInteger(1, keyBytes);

                return(new Gost3410PublicKeyParameters(y, algParams.PublicKeyParamSet));
            }
            else if (algOid.Equals(EdECObjectIdentifiers.id_X25519))
            {
                return(new X25519PublicKeyParameters(GetRawKey(keyInfo)));
            }
            else if (algOid.Equals(EdECObjectIdentifiers.id_X448))
            {
                return(new X448PublicKeyParameters(GetRawKey(keyInfo)));
            }
            else if (algOid.Equals(EdECObjectIdentifiers.id_Ed25519))
            {
                return(new Ed25519PublicKeyParameters(GetRawKey(keyInfo)));
            }
            else if (algOid.Equals(EdECObjectIdentifiers.id_Ed448))
            {
                return(new Ed448PublicKeyParameters(GetRawKey(keyInfo)));
            }
            else if (algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256) ||
                     algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512))
            {
                Gost3410PublicKeyAlgParameters gostParams        = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters);
                DerObjectIdentifier            publicKeyParamSet = gostParams.PublicKeyParamSet;

                ECGost3410Parameters ecDomainParameters = new ECGost3410Parameters(
                    new ECNamedDomainParameters(publicKeyParamSet, ECGost3410NamedCurves.GetByOidX9(publicKeyParamSet)),
                    publicKeyParamSet,
                    gostParams.DigestParamSet,
                    gostParams.EncryptionParamSet);

                Asn1OctetString key;
                try
                {
                    key = (Asn1OctetString)keyInfo.ParsePublicKey();
                }
                catch (IOException e)
                {
                    throw new ArgumentException("error recovering GOST3410_2012 public key", e);
                }

                int fieldSize = 32;
                if (algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512))
                {
                    fieldSize = 64;
                }
                int keySize = 2 * fieldSize;

                byte[] keyEnc = key.GetOctets();
                if (keyEnc.Length != keySize)
                {
                    throw new ArgumentException("invalid length for GOST3410_2012 public key");
                }

                byte[] x9Encoding = new byte[1 + keySize];
                x9Encoding[0] = 0x04;
                for (int i = 1; i <= fieldSize; ++i)
                {
                    x9Encoding[i]             = keyEnc[fieldSize - i];
                    x9Encoding[i + fieldSize] = keyEnc[keySize - i];
                }

                ECPoint q = ecDomainParameters.Curve.DecodePoint(x9Encoding);

                return(new ECPublicKeyParameters(q, ecDomainParameters));
            }
            else
            {
                throw new SecurityUtilityException("algorithm identifier in public key not recognised: " + algOid);
            }
        }
        public static AsymmetricKeyParameter CreateKey(
            SubjectPublicKeyInfo keyInfo)
        {
            AlgorithmIdentifier algID  = keyInfo.AlgorithmID;
            DerObjectIdentifier algOid = algID.ObjectID;

            // TODO See RSAUtil.isRsaOid in Java build
            if (algOid.Equals(PkcsObjectIdentifiers.RsaEncryption) ||
                algOid.Equals(X509ObjectIdentifiers.IdEARsa) ||
                algOid.Equals(PkcsObjectIdentifiers.IdRsassaPss) ||
                algOid.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
            {
                RsaPublicKeyStructure pubKey = RsaPublicKeyStructure.GetInstance(
                    keyInfo.GetPublicKey());

                return(new RsaKeyParameters(false, pubKey.Modulus, pubKey.PublicExponent));
            }
            else if (algOid.Equals(X9ObjectIdentifiers.DHPublicNumber))
            {
                Asn1Sequence seq = Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object());

                DHPublicKey dhPublicKey = DHPublicKey.GetInstance(keyInfo.GetPublicKey());

                BigInteger y = dhPublicKey.Y.Value;

                if (IsPkcsDHParam(seq))
                {
                    return(ReadPkcsDHParam(algOid, y, seq));
                }

                DHDomainParameters dhParams = DHDomainParameters.GetInstance(seq);

                BigInteger p = dhParams.P.Value;
                BigInteger g = dhParams.G.Value;
                BigInteger q = dhParams.Q.Value;

                BigInteger j = null;
                if (dhParams.J != null)
                {
                    j = dhParams.J.Value;
                }

                DHValidationParameters validation        = null;
                DHValidationParms      dhValidationParms = dhParams.ValidationParms;
                if (dhValidationParms != null)
                {
                    byte[]     seed        = dhValidationParms.Seed.GetBytes();
                    BigInteger pgenCounter = dhValidationParms.PgenCounter.Value;

                    // TODO Check pgenCounter size?

                    validation = new DHValidationParameters(seed, pgenCounter.IntValue);
                }

                return(new DHPublicKeyParameters(y, new DHParameters(p, g, q, j, validation)));
            }
            else if (algOid.Equals(PkcsObjectIdentifiers.DhKeyAgreement))
            {
                Asn1Sequence seq = Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object());

                DerInteger derY = (DerInteger)keyInfo.GetPublicKey();

                return(ReadPkcsDHParam(algOid, derY.Value, seq));
            }
            else if (algOid.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
            {
                ElGamalParameter para = new ElGamalParameter(
                    Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object()));
                DerInteger derY = (DerInteger)keyInfo.GetPublicKey();

                return(new ElGamalPublicKeyParameters(
                           derY.Value,
                           new ElGamalParameters(para.P, para.G)));
            }
            else if (algOid.Equals(X9ObjectIdentifiers.IdDsa) ||
                     algOid.Equals(OiwObjectIdentifiers.DsaWithSha1))
            {
                DerInteger    derY = (DerInteger)keyInfo.GetPublicKey();
                Asn1Encodable ae   = algID.Parameters;

                DsaParameters parameters = null;
                if (ae != null)
                {
                    DsaParameter para = DsaParameter.GetInstance(ae.ToAsn1Object());
                    parameters = new DsaParameters(para.P, para.Q, para.G);
                }

                return(new DsaPublicKeyParameters(derY.Value, parameters));
            }
            else if (algOid.Equals(X9ObjectIdentifiers.IdECPublicKey))
            {
                X962Parameters para = new X962Parameters(
                    algID.Parameters.ToAsn1Object());
                X9ECParameters ecP;

                if (para.IsNamedCurve)
                {
                    ecP = ECKeyPairGenerator.FindECCurveByOid((DerObjectIdentifier)para.Parameters);
                }
                else
                {
                    ecP = new X9ECParameters((Asn1Sequence)para.Parameters);
                }

                ECDomainParameters dParams = new ECDomainParameters(
                    ecP.Curve,
                    ecP.G,
                    ecP.N,
                    ecP.H,
                    ecP.GetSeed());

                DerBitString    bits = keyInfo.PublicKeyData;
                byte[]          data = bits.GetBytes();
                Asn1OctetString key  = new DerOctetString(data);

                X9ECPoint derQ = new X9ECPoint(dParams.Curve, key);

                return(new ECPublicKeyParameters(derQ.Point, dParams));
            }
            else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
            {
                Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters(
                    (Asn1Sequence)algID.Parameters);

                Asn1OctetString key;
                try
                {
                    key = (Asn1OctetString)keyInfo.GetPublicKey();
                }
                catch (IOException)
                {
                    throw new ArgumentException("invalid info structure in GOST3410 public key");
                }

                byte[] keyEnc = key.GetOctets();
                byte[] x      = new byte[32];
                byte[] y      = new byte[32];

                for (int i = 0; i != y.Length; i++)
                {
                    x[i] = keyEnc[32 - 1 - i];
                }

                for (int i = 0; i != x.Length; i++)
                {
                    y[i] = keyEnc[64 - 1 - i];
                }

                ECDomainParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet);

                if (ecP == null)
                {
                    return(null);
                }

                ECPoint q = ecP.Curve.CreatePoint(new BigInteger(1, x), new BigInteger(1, y), false);

                return(new ECPublicKeyParameters("ECGOST3410", q, gostParams.PublicKeyParamSet));
            }
            else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x94))
            {
                Gost3410PublicKeyAlgParameters algParams = new Gost3410PublicKeyAlgParameters(
                    (Asn1Sequence)algID.Parameters);

                DerOctetString derY;
                try
                {
                    derY = (DerOctetString)keyInfo.GetPublicKey();
                }
                catch (IOException)
                {
                    throw new ArgumentException("invalid info structure in GOST3410 public key");
                }

                byte[] keyEnc   = derY.GetOctets();
                byte[] keyBytes = new byte[keyEnc.Length];

                for (int i = 0; i != keyEnc.Length; i++)
                {
                    keyBytes[i] = keyEnc[keyEnc.Length - 1 - i];                     // was little endian
                }

                BigInteger y = new BigInteger(1, keyBytes);

                return(new Gost3410PublicKeyParameters(y, algParams.PublicKeyParamSet));
            }
            else
            {
                throw new SecurityUtilityException("algorithm identifier in key not recognised: " + algOid);
            }
        }
Esempio n. 4
0
        public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo)
        {
            DerOctetString      publicKey;
            AlgorithmIdentifier algorithmID = keyInfo.AlgorithmID;
            DerObjectIdentifier algorithm   = algorithmID.Algorithm;

            if ((algorithm.Equals(PkcsObjectIdentifiers.RsaEncryption) || algorithm.Equals(X509ObjectIdentifiers.IdEARsa)) || (algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss) || algorithm.Equals(PkcsObjectIdentifiers.IdRsaesOaep)))
            {
                RsaPublicKeyStructure instance = RsaPublicKeyStructure.GetInstance(keyInfo.GetPublicKey());
                return(new RsaKeyParameters(false, instance.Modulus, instance.PublicExponent));
            }
            if (algorithm.Equals(X9ObjectIdentifiers.DHPublicNumber))
            {
                Asn1Sequence instance = Asn1Sequence.GetInstance(algorithmID.Parameters.ToAsn1Object());
                BigInteger   y        = DHPublicKey.GetInstance(keyInfo.GetPublicKey()).Y.Value;
                if (IsPkcsDHParam(instance))
                {
                    return(ReadPkcsDHParam(algorithm, y, instance));
                }
                DHDomainParameters parameters = DHDomainParameters.GetInstance(instance);
                BigInteger         p          = parameters.P.Value;
                BigInteger         g          = parameters.G.Value;
                BigInteger         q          = parameters.Q.Value;
                BigInteger         j          = null;
                if (parameters.J != null)
                {
                    j = parameters.J.Value;
                }
                DHValidationParameters validation      = null;
                DHValidationParms      validationParms = parameters.ValidationParms;
                if (validationParms != null)
                {
                    byte[]     seed     = validationParms.Seed.GetBytes();
                    BigInteger integer6 = validationParms.PgenCounter.Value;
                    validation = new DHValidationParameters(seed, integer6.IntValue);
                }
                return(new DHPublicKeyParameters(y, new DHParameters(p, g, q, j, validation)));
            }
            if (algorithm.Equals(PkcsObjectIdentifiers.DhKeyAgreement))
            {
                Asn1Sequence instance  = Asn1Sequence.GetInstance(algorithmID.Parameters.ToAsn1Object());
                DerInteger   publicKey = (DerInteger)keyInfo.GetPublicKey();
                return(ReadPkcsDHParam(algorithm, publicKey.Value, instance));
            }
            if (algorithm.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
            {
                ElGamalParameter parameter = new ElGamalParameter(Asn1Sequence.GetInstance(algorithmID.Parameters.ToAsn1Object()));
                DerInteger       publicKey = (DerInteger)keyInfo.GetPublicKey();
                return(new ElGamalPublicKeyParameters(publicKey.Value, new ElGamalParameters(parameter.P, parameter.G)));
            }
            if (algorithm.Equals(X9ObjectIdentifiers.IdDsa) || algorithm.Equals(OiwObjectIdentifiers.DsaWithSha1))
            {
                DerInteger    publicKey   = (DerInteger)keyInfo.GetPublicKey();
                Asn1Encodable parameters  = algorithmID.Parameters;
                DsaParameters parameters3 = null;
                if (parameters != null)
                {
                    DsaParameter instance = DsaParameter.GetInstance(parameters.ToAsn1Object());
                    parameters3 = new DsaParameters(instance.P, instance.Q, instance.G);
                }
                return(new DsaPublicKeyParameters(publicKey.Value, parameters3));
            }
            if (algorithm.Equals(X9ObjectIdentifiers.IdECPublicKey))
            {
                X9ECParameters parameters5;
                X962Parameters parameters4 = new X962Parameters(algorithmID.Parameters.ToAsn1Object());
                if (parameters4.IsNamedCurve)
                {
                    parameters5 = ECKeyPairGenerator.FindECCurveByOid((DerObjectIdentifier)parameters4.Parameters);
                }
                else
                {
                    parameters5 = new X9ECParameters((Asn1Sequence)parameters4.Parameters);
                }
                Asn1OctetString s     = new DerOctetString(keyInfo.PublicKeyData.GetBytes());
                X9ECPoint       point = new X9ECPoint(parameters5.Curve, s);
                ECPoint         q     = point.Point;
                if (parameters4.IsNamedCurve)
                {
                    return(new ECPublicKeyParameters("EC", q, (DerObjectIdentifier)parameters4.Parameters));
                }
                return(new ECPublicKeyParameters(q, new ECDomainParameters(parameters5.Curve, parameters5.G, parameters5.N, parameters5.H, parameters5.GetSeed())));
            }
            if (algorithm.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
            {
                Asn1OctetString publicKey;
                Gost3410PublicKeyAlgParameters parameters7 = new Gost3410PublicKeyAlgParameters((Asn1Sequence)algorithmID.Parameters);
                try
                {
                    publicKey = (Asn1OctetString)keyInfo.GetPublicKey();
                }
                catch (IOException)
                {
                    throw new ArgumentException("invalid info structure in GOST3410 public key");
                }
                byte[] buffer2 = publicKey.GetOctets();
                byte[] buffer3 = new byte[0x20];
                byte[] buffer4 = new byte[0x20];
                for (int j = 0; j != buffer4.Length; j++)
                {
                    buffer3[j] = buffer2[0x1f - j];
                }
                for (int k = 0; k != buffer3.Length; k++)
                {
                    buffer4[k] = buffer2[0x3f - k];
                }
                ECDomainParameters byOid = ECGost3410NamedCurves.GetByOid(parameters7.PublicKeyParamSet);
                if (byOid == null)
                {
                    return(null);
                }
                return(new ECPublicKeyParameters("ECGOST3410", byOid.Curve.CreatePoint(new BigInteger(1, buffer3), new BigInteger(1, buffer4)), parameters7.PublicKeyParamSet));
            }
            if (!algorithm.Equals(CryptoProObjectIdentifiers.GostR3410x94))
            {
                throw new SecurityUtilityException("algorithm identifier in key not recognised: " + algorithm);
            }
            Gost3410PublicKeyAlgParameters parameters9 = new Gost3410PublicKeyAlgParameters((Asn1Sequence)algorithmID.Parameters);

            try
            {
                publicKey = (DerOctetString)keyInfo.GetPublicKey();
            }
            catch (IOException)
            {
                throw new ArgumentException("invalid info structure in GOST3410 public key");
            }
            byte[] octets = publicKey.GetOctets();
            byte[] bytes  = new byte[octets.Length];
            for (int i = 0; i != octets.Length; i++)
            {
                bytes[i] = octets[(octets.Length - 1) - i];
            }
            return(new Gost3410PublicKeyParameters(new BigInteger(1, bytes), parameters9.PublicKeyParamSet));
        }
Esempio n. 5
0
    public static AsymmetricKeyParameter CreateKey(SubjectPublicKeyInfo keyInfo)
    {
        AlgorithmIdentifier algorithmID = keyInfo.AlgorithmID;
        DerObjectIdentifier algorithm   = algorithmID.Algorithm;

        if (algorithm.Equals(PkcsObjectIdentifiers.RsaEncryption) || algorithm.Equals(X509ObjectIdentifiers.IdEARsa) || algorithm.Equals(PkcsObjectIdentifiers.IdRsassaPss) || algorithm.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
        {
            RsaPublicKeyStructure instance = RsaPublicKeyStructure.GetInstance(keyInfo.GetPublicKey());
            return(new RsaKeyParameters(isPrivate: false, instance.Modulus, instance.PublicExponent));
        }
        if (algorithm.Equals(X9ObjectIdentifiers.DHPublicNumber))
        {
            Asn1Sequence instance2 = Asn1Sequence.GetInstance(algorithmID.Parameters.ToAsn1Object());
            DHPublicKey  instance3 = DHPublicKey.GetInstance(keyInfo.GetPublicKey());
            BigInteger   value     = instance3.Y.Value;
            if (IsPkcsDHParam(instance2))
            {
                return(ReadPkcsDHParam(algorithm, value, instance2));
            }
            DHDomainParameters instance4 = DHDomainParameters.GetInstance(instance2);
            BigInteger         value2    = instance4.P.Value;
            BigInteger         value3    = instance4.G.Value;
            BigInteger         value4    = instance4.Q.Value;
            BigInteger         j         = null;
            if (instance4.J != null)
            {
                j = instance4.J.Value;
            }
            DHValidationParameters validation      = null;
            DHValidationParms      validationParms = instance4.ValidationParms;
            if (validationParms != null)
            {
                byte[]     bytes  = validationParms.Seed.GetBytes();
                BigInteger value5 = validationParms.PgenCounter.Value;
                validation = new DHValidationParameters(bytes, value5.IntValue);
            }
            return(new DHPublicKeyParameters(value, new DHParameters(value2, value3, value4, j, validation)));
        }
        if (algorithm.Equals(PkcsObjectIdentifiers.DhKeyAgreement))
        {
            Asn1Sequence instance5  = Asn1Sequence.GetInstance(algorithmID.Parameters.ToAsn1Object());
            DerInteger   derInteger = (DerInteger)keyInfo.GetPublicKey();
            return(ReadPkcsDHParam(algorithm, derInteger.Value, instance5));
        }
        if (algorithm.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
        {
            ElGamalParameter elGamalParameter = new ElGamalParameter(Asn1Sequence.GetInstance(algorithmID.Parameters.ToAsn1Object()));
            DerInteger       derInteger2      = (DerInteger)keyInfo.GetPublicKey();
            return(new ElGamalPublicKeyParameters(derInteger2.Value, new ElGamalParameters(elGamalParameter.P, elGamalParameter.G)));
        }
        if (algorithm.Equals(X9ObjectIdentifiers.IdDsa) || algorithm.Equals(OiwObjectIdentifiers.DsaWithSha1))
        {
            DerInteger    derInteger3 = (DerInteger)keyInfo.GetPublicKey();
            Asn1Encodable parameters  = algorithmID.Parameters;
            DsaParameters parameters2 = null;
            if (parameters != null)
            {
                DsaParameter instance6 = DsaParameter.GetInstance(parameters.ToAsn1Object());
                parameters2 = new DsaParameters(instance6.P, instance6.Q, instance6.G);
            }
            return(new DsaPublicKeyParameters(derInteger3.Value, parameters2));
        }
        if (algorithm.Equals(X9ObjectIdentifiers.IdECPublicKey))
        {
            X962Parameters  x962Parameters = new X962Parameters(algorithmID.Parameters.ToAsn1Object());
            X9ECParameters  x9ECParameters = (!x962Parameters.IsNamedCurve) ? new X9ECParameters((Asn1Sequence)x962Parameters.Parameters) : ECKeyPairGenerator.FindECCurveByOid((DerObjectIdentifier)x962Parameters.Parameters);
            Asn1OctetString s         = new DerOctetString(keyInfo.PublicKeyData.GetBytes());
            X9ECPoint       x9ECPoint = new X9ECPoint(x9ECParameters.Curve, s);
            ECPoint         point     = x9ECPoint.Point;
            if (x962Parameters.IsNamedCurve)
            {
                return(new ECPublicKeyParameters("EC", point, (DerObjectIdentifier)x962Parameters.Parameters));
            }
            ECDomainParameters parameters3 = new ECDomainParameters(x9ECParameters.Curve, x9ECParameters.G, x9ECParameters.N, x9ECParameters.H, x9ECParameters.GetSeed());
            return(new ECPublicKeyParameters(point, parameters3));
        }
        if (algorithm.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
        {
            Gost3410PublicKeyAlgParameters gost3410PublicKeyAlgParameters = new Gost3410PublicKeyAlgParameters((Asn1Sequence)algorithmID.Parameters);
            Asn1OctetString asn1OctetString;
            try
            {
                asn1OctetString = (Asn1OctetString)keyInfo.GetPublicKey();
            }
            catch (IOException)
            {
                throw new ArgumentException("invalid info structure in GOST3410 public key");
            }
            byte[] octets = asn1OctetString.GetOctets();
            byte[] array  = new byte[32];
            byte[] array2 = new byte[32];
            for (int i = 0; i != array2.Length; i++)
            {
                array[i] = octets[31 - i];
            }
            for (int k = 0; k != array.Length; k++)
            {
                array2[k] = octets[63 - k];
            }
            ECDomainParameters byOid = ECGost3410NamedCurves.GetByOid(gost3410PublicKeyAlgParameters.PublicKeyParamSet);
            if (byOid == null)
            {
                return(null);
            }
            ECPoint q = byOid.Curve.CreatePoint(new BigInteger(1, array), new BigInteger(1, array2));
            return(new ECPublicKeyParameters("ECGOST3410", q, gost3410PublicKeyAlgParameters.PublicKeyParamSet));
        }
        if (algorithm.Equals(CryptoProObjectIdentifiers.GostR3410x94))
        {
            Gost3410PublicKeyAlgParameters gost3410PublicKeyAlgParameters2 = new Gost3410PublicKeyAlgParameters((Asn1Sequence)algorithmID.Parameters);
            DerOctetString derOctetString;
            try
            {
                derOctetString = (DerOctetString)keyInfo.GetPublicKey();
            }
            catch (IOException)
            {
                throw new ArgumentException("invalid info structure in GOST3410 public key");
            }
            byte[] octets2 = derOctetString.GetOctets();
            byte[] array3  = new byte[octets2.Length];
            for (int l = 0; l != octets2.Length; l++)
            {
                array3[l] = octets2[octets2.Length - 1 - l];
            }
            BigInteger y = new BigInteger(1, array3);
            return(new Gost3410PublicKeyParameters(y, gost3410PublicKeyAlgParameters2.PublicKeyParamSet));
        }
        throw new SecurityUtilityException("algorithm identifier in key not recognised: " + algorithm);
    }
Esempio n. 6
0
 /**
  * Constructor for specifying the ElGamal algorithm explicitly.
  *
  * @param parameters the particular parameter set to generate keys for.
  * @param domainParameters DH domain parameters representing the curve any generated keys will be for.
  */
 private KeyGenerationParameters(Parameters<GeneralAlgorithm> parameters, DHDomainParameters domainParameters) : base(parameters.Algorithm)
 {
     this.domainParameters = domainParameters;
 }
Esempio n. 7
0
 /**
  * Base constructor for specific domain parameters.
  *
  * @param domainParameters the DH domain parameters.
  */
 public KeyGenerationParameters(DHDomainParameters domainParameters) : base(Alg)
 {
     this.domainParameters = domainParameters;
 }
Esempio n. 8
0
 private static DHParameters getDomainParams(DHDomainParameters dhParameters)
 {
     return new DHParameters(dhParameters.P, dhParameters.G, dhParameters.Q, dhParameters.M, dhParameters.L, dhParameters.J, null);
 }
        public static AsymmetricKeyParameter CreateKey(
            SubjectPublicKeyInfo keyInfo)
        {
            var algID  = keyInfo.AlgorithmID;
            var algOid = algID.Algorithm;

            // TODO See RSAUtil.isRsaOid in Java build
            if (algOid.Equals(PkcsObjectIdentifiers.RsaEncryption) ||
                algOid.Equals(X509ObjectIdentifiers.IdEARsa) ||
                algOid.Equals(PkcsObjectIdentifiers.IdRsassaPss) ||
                algOid.Equals(PkcsObjectIdentifiers.IdRsaesOaep))
            {
                var pubKey = RsaPublicKeyStructure.GetInstance(
                    keyInfo.GetPublicKey());

                return(new RsaKeyParameters(false, pubKey.Modulus, pubKey.PublicExponent));
            }

            if (algOid.Equals(X9ObjectIdentifiers.DHPublicNumber))
            {
                var seq = Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object());

                var dhPublicKey = DHPublicKey.GetInstance(keyInfo.GetPublicKey());

                var y = dhPublicKey.Y.Value;

                if (IsPkcsDHParam(seq))
                {
                    return(ReadPkcsDHParam(algOid, y, seq));
                }

                var dhParams = DHDomainParameters.GetInstance(seq);

                var p = dhParams.P.Value;
                var g = dhParams.G.Value;
                var q = dhParams.Q.Value;

                BigInteger j = null;
                if (dhParams.J != null)
                {
                    j = dhParams.J.Value;
                }

                DHValidationParameters validation = null;
                var dhValidationParms             = dhParams.ValidationParms;
                if (dhValidationParms != null)
                {
                    var seed        = dhValidationParms.Seed.GetBytes();
                    var pgenCounter = dhValidationParms.PgenCounter.Value;

                    // TODO Check pgenCounter size?

                    validation = new DHValidationParameters(seed, pgenCounter.IntValue);
                }

                return(new DHPublicKeyParameters(y, new DHParameters(p, g, q, j, validation)));
            }

            if (algOid.Equals(PkcsObjectIdentifiers.DhKeyAgreement))
            {
                var seq = Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object());

                var derY = (DerInteger)keyInfo.GetPublicKey();

                return(ReadPkcsDHParam(algOid, derY.Value, seq));
            }

            if (algOid.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
            {
                var para = new ElGamalParameter(
                    Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object()));
                var derY = (DerInteger)keyInfo.GetPublicKey();

                return(new ElGamalPublicKeyParameters(
                           derY.Value,
                           new ElGamalParameters(para.P, para.G)));
            }

            if (algOid.Equals(X9ObjectIdentifiers.IdDsa) ||
                algOid.Equals(OiwObjectIdentifiers.DsaWithSha1))
            {
                var derY = (DerInteger)keyInfo.GetPublicKey();
                var ae   = algID.Parameters;

                DsaParameters parameters = null;
                if (ae != null)
                {
                    var para = DsaParameter.GetInstance(ae.ToAsn1Object());
                    parameters = new DsaParameters(para.P, para.Q, para.G);
                }

                return(new DsaPublicKeyParameters(derY.Value, parameters));
            }

            if (algOid.Equals(X9ObjectIdentifiers.IdECPublicKey))
            {
                var para = new X962Parameters(algID.Parameters.ToAsn1Object());

                X9ECParameters x9;
                if (para.IsNamedCurve)
                {
                    x9 = ECKeyPairGenerator.FindECCurveByOid((DerObjectIdentifier)para.Parameters);
                }
                else
                {
                    x9 = new X9ECParameters((Asn1Sequence)para.Parameters);
                }

                Asn1OctetString key  = new DerOctetString(keyInfo.PublicKeyData.GetBytes());
                var             derQ = new X9ECPoint(x9.Curve, key);
                var             q    = derQ.Point;

                if (para.IsNamedCurve)
                {
                    return(new ECPublicKeyParameters("EC", q, (DerObjectIdentifier)para.Parameters));
                }

                var dParams = new ECDomainParameters(x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed());
                return(new ECPublicKeyParameters(q, dParams));
            }

            if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
            {
                var gostParams = new Gost3410PublicKeyAlgParameters(
                    (Asn1Sequence)algID.Parameters);

                Asn1OctetString key;
                try
                {
                    key = (Asn1OctetString)keyInfo.GetPublicKey();
                }
                catch (IOException)
                {
                    throw new ArgumentException("invalid info structure in GOST3410 public key");
                }

                var keyEnc = key.GetOctets();
                var x      = new byte[32];
                var y      = new byte[32];

                for (var i = 0; i != y.Length; i++)
                {
                    x[i] = keyEnc[32 - 1 - i];
                }

                for (var i = 0; i != x.Length; i++)
                {
                    y[i] = keyEnc[64 - 1 - i];
                }

                var ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet);

                if (ecP == null)
                {
                    return(null);
                }

                var q = ecP.Curve.CreatePoint(new BigInteger(1, x), new BigInteger(1, y));

                return(new ECPublicKeyParameters("ECGOST3410", q, gostParams.PublicKeyParamSet));
            }

            if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x94))
            {
                var algParams = new Gost3410PublicKeyAlgParameters(
                    (Asn1Sequence)algID.Parameters);

                DerOctetString derY;
                try
                {
                    derY = (DerOctetString)keyInfo.GetPublicKey();
                }
                catch (IOException)
                {
                    throw new ArgumentException("invalid info structure in GOST3410 public key");
                }

                var keyEnc   = derY.GetOctets();
                var keyBytes = new byte[keyEnc.Length];

                for (var i = 0; i != keyEnc.Length; i++)
                {
                    keyBytes[i] = keyEnc[keyEnc.Length - 1 - i]; // was little endian
                }

                var y = new BigInteger(1, keyBytes);

                return(new Gost3410PublicKeyParameters(y, algParams.PublicKeyParamSet));
            }
            if (algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256) ||
                algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512))
            {
                var algParams = new Gost3410PublicKeyAlgParameters(
                    (Asn1Sequence)algID.Parameters);

                Asn1OctetString key;
                try
                {
                    key = (Asn1OctetString)keyInfo.GetPublicKey();
                }
                catch (IOException)
                {
                    throw new ArgumentException("invalid info structure in GOST3410-2012 public key");
                }

                var keyEnc = key.GetOctets();

                var fieldSize = 32;
                if (algOid.Equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512))
                {
                    fieldSize = 64;
                }

                var keySize = 2 * fieldSize;

                var x9Encoding = new byte[1 + keySize];
                x9Encoding[0] = 0x04;
                for (var i = 1; i <= fieldSize; ++i)
                {
                    x9Encoding[i]             = keyEnc[fieldSize - i];
                    x9Encoding[i + fieldSize] = keyEnc[keySize - i];
                }

                var ecP = ECGost3410NamedCurves.GetByOid(algParams.PublicKeyParamSet);

                if (ecP == null)
                {
                    return(null);
                }

                return(new ECPublicKeyParameters(ecP.Curve.DecodePoint(x9Encoding), ecP));
            }

            throw new SecurityUtilityException("algorithm identifier in key not recognised: " + algOid);
        }