Exemple #1
0
        private static KeyBlobMagicNumber EcdhCurveNameToMagicNumber(string name, bool includePrivateParameters)
        {
            switch (CngKey.EcdhCurveNameToAlgorithm(name).Algorithm)
            {
            case AlgorithmName.ECDHP256:
                return(includePrivateParameters ?
                       KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P256_MAGIC :
                       KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P256_MAGIC);

            case AlgorithmName.ECDHP384:
                return(includePrivateParameters ?
                       KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P384_MAGIC :
                       KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P384_MAGIC);

            case AlgorithmName.ECDHP521:
                return(includePrivateParameters ?
                       KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P521_MAGIC :
                       KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P521_MAGIC);

            default:
                // all other curves are new in Win10 so use named curves
                return(includePrivateParameters ?
                       KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_GENERIC_MAGIC :
                       KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_GENERIC_MAGIC);
            }
        }
Exemple #2
0
        public override void GenerateKey(ECCurve curve)
        {
            curve.Validate();
            _core.DisposeKey();

            if (curve.IsNamed)
            {
                if (string.IsNullOrEmpty(curve.Oid.FriendlyName))
                {
                    throw new PlatformNotSupportedException(string.Format(SR.Cryptography_InvalidCurveOid, curve.Oid.Value));
                }

                // Map curve name to algorithm to support pre-Win10 curves
                CngAlgorithm alg = CngKey.EcdhCurveNameToAlgorithm(curve.Oid.FriendlyName);
                if (CngKey.IsECNamedCurve(alg.Algorithm))
                {
                    CngKey key = _core.GetOrGenerateKey(curve);
                    ForceSetKeySize(key.KeySize);
                }
                else
                {
                    int keySize = 0;
                    // Get the proper KeySize from algorithm name
                    if (alg == CngAlgorithm.ECDiffieHellmanP256)
                    {
                        keySize = 256;
                    }
                    else if (alg == CngAlgorithm.ECDiffieHellmanP384)
                    {
                        keySize = 384;
                    }
                    else if (alg == CngAlgorithm.ECDiffieHellmanP521)
                    {
                        keySize = 521;
                    }
                    else
                    {
                        Debug.Fail(string.Format("Unknown algorithm {0}", alg.ToString()));
                        throw new ArgumentException(SR.Cryptography_InvalidKeySize);
                    }
                    CngKey key = _core.GetOrGenerateKey(keySize, alg);
                    ForceSetKeySize(keySize);
                }
            }
            else if (curve.IsExplicit)
            {
                CngKey key = _core.GetOrGenerateKey(curve);
                ForceSetKeySize(key.KeySize);
            }
            else
            {
                throw new PlatformNotSupportedException(string.Format(SR.Cryptography_CurveNotSupported, curve.CurveType.ToString()));
            }
        }
Exemple #3
0
        public override void GenerateKey(ECCurve curve)
        {
            curve.Validate();

            if (m_key != null)
            {
                m_key.Dispose();
                m_key = null;
            }

            CngKey newKey = CngKey.Create(curve, name => CngKey.EcdhCurveNameToAlgorithm(name));

            m_key        = newKey;
            KeySizeValue = newKey.KeySize;
        }