Beispiel #1
0
        public override bool Equals(object o)
        {
            CheckApprovedOnlyModeStatus();

            if (this == o)
            {
                return(true);
            }
            if (!(o is AsymmetricRsaPrivateKey))
            {
                return(false);
            }

            AsymmetricRsaPrivateKey other = (AsymmetricRsaPrivateKey)o;

            return(Modulus.Equals(other.Modulus) &&
                   privateExponent.Equals(other.privateExponent) && PublicExponent.Equals(other.PublicExponent) &&
                   p.Equals(other.p) && q.Equals(other.q) &&
                   dp.Equals(other.dp) && dq.Equals(other.dq) && qInv.Equals(other.qInv));
        }
Beispiel #2
0
        private void checkKeyPairForConsistency(TPub publicKey, TPriv privateKey)
        {
            if (publicKey is AsymmetricECKey && privateKey is AsymmetricECKey)
            {
                AsymmetricECPrivateKey priv = privateKey as AsymmetricECPrivateKey;
                AsymmetricECPublicKey  pub  = publicKey as AsymmetricECPublicKey;

                if (!priv.DomainParameters.Equals(pub.DomainParameters))
                {
                    // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                    throw new ArgumentException("EC keys do not have the same domain parameters");
                }
                if (!priv.DomainParameters.G.Multiply(priv.S).Normalize().Equals(pub.W))
                {
                    // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                    throw new ArgumentException("EC public key not consistent with EC private key");
                }
            }
            else if (publicKey is AsymmetricDsaKey && privateKey is AsymmetricDsaKey)
            {
                AsymmetricDsaPrivateKey priv = privateKey as AsymmetricDsaPrivateKey;
                AsymmetricDsaPublicKey  pub  = publicKey as AsymmetricDsaPublicKey;

                DsaDomainParameters dsaParameters = priv.DomainParameters;
                if (!dsaParameters.Equals(pub.DomainParameters))
                {
                    // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                    throw new ArgumentException("DSA keys do not have the same domain parameters");
                }
                if (!dsaParameters.G.ModPow(priv.X, dsaParameters.P).Equals(pub.Y))
                {
                    // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                    throw new ArgumentException("DSA public key not consistent with DSA private key");
                }
            }
            else if (publicKey is AsymmetricRsaKey && privateKey is AsymmetricRsaKey)
            {
                AsymmetricRsaPrivateKey priv = privateKey as AsymmetricRsaPrivateKey;
                AsymmetricRsaPublicKey  pub  = publicKey as AsymmetricRsaPublicKey;

                if (!priv.Modulus.Equals(pub.Modulus))
                {
                    // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                    throw new ArgumentException("RSA keys do not have the same modulus");
                }
                BigInteger val = BigInteger.Two;
                if (!val.ModPow(priv.PrivateExponent, priv.Modulus).ModPow(pub.PublicExponent, priv.Modulus).Equals(val))
                {
                    // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                    throw new ArgumentException("RSA public key not consistent with RSA private key");
                }
            }
            else if (publicKey is AsymmetricDHKey && privateKey is AsymmetricDHKey)
            {
                AsymmetricDHPrivateKey priv = privateKey as AsymmetricDHPrivateKey;
                AsymmetricDHPublicKey  pub  = publicKey as  AsymmetricDHPublicKey;

                DHDomainParameters dhParameters = priv.DomainParameters;
                if (!dhParameters.Equals(pub.DomainParameters))
                {
                    // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                    throw new ArgumentException("DH keys do not have the same domain parameters");
                }
                if (!dhParameters.G.ModPow(priv.X, dhParameters.P).Equals(pub.Y))
                {
                    // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                    throw new ArgumentException("DH public key not consistent with DH private key");
                }
            }
            else if (publicKey is AsymmetricSphincsKey && privateKey is AsymmetricSphincsKey)
            {
                AsymmetricSphincsPrivateKey priv = privateKey as AsymmetricSphincsPrivateKey;
                AsymmetricSphincsPublicKey  pub  = publicKey as AsymmetricSphincsPublicKey;

                if (priv.TreeDigestAlgorithm != pub.TreeDigestAlgorithm)
                {
                    // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                    throw new ArgumentException("Sphincs256 public key not consistent with Sphincs256 private key");
                }
                if (!IsRangeSame(priv.GetKeyData(), SPHINCS256Config.SEED_BYTES, pub.GetKeyData(), 0, Horst.N_MASKS * SPHINCS256Config.HASH_BYTES))
                {
                    // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                    throw new ArgumentException("Sphincs256 public key not consistent with Sphincs256 private key");
                }
            }
            else if (publicKey is AsymmetricNHKey && privateKey is AsymmetricNHKey)
            {
                AsymmetricNHPrivateKey priv = privateKey as AsymmetricNHPrivateKey;
                AsymmetricNHPublicKey  pub  = publicKey as AsymmetricNHPublicKey;
                // currently there doesn't either a good approach or much point to this one as the keys should be ephemeral and always generated locally.
            }
            else
            {
                // FSM_TRANS:5.IKP.2, "IMPORTED KEY PAIR CONSISTENCY TEST", "USER COMMAND REJECTED", "Consistency test on imported key pair failed"
                throw new ArgumentException("Key pair inconsistent");
            }
        }