/// <summary> /// Map a curve name to magic number. Maps the names of the curves that worked pre-Win10 /// to the pre-Win10 magic numbers to support import on pre-Win10 environments /// that don't have the named curve functionality. /// </summary> private static KeyBlobMagicNumber CurveNameToMagicNumber(string name, bool includePrivateParameters) { CngAlgorithm alg = CngKey.EcdsaCurveNameToAlgorithm(name); if (alg == CngAlgorithm.ECDsaP256) { return(includePrivateParameters ? KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P256_MAGIC : KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P256_MAGIC); } if (alg == CngAlgorithm.ECDsaP384) { return(includePrivateParameters ? KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P384_MAGIC : KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P384_MAGIC); } if (alg == CngAlgorithm.ECDsaP521) { return(includePrivateParameters ? KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P521_MAGIC : KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P521_MAGIC); } // all other curves are new in Win10 so use named curves return(includePrivateParameters ? KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_GENERIC_MAGIC : KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_GENERIC_MAGIC); }
/// <summary> /// Map a curve name to magic number. Maps the names of the curves that worked pre-Win10 /// to the pre-Win10 magic numbers to support import on pre-Win10 environments /// that don't have the named curve functionality. /// </summary> private static KeyBlobMagicNumber ECDsaCurveNameToMagicNumber(string name, bool includePrivateParameters) { switch (CngKey.EcdsaCurveNameToAlgorithm(name).Algorithm) { case AlgorithmName.ECDsaP256: return(includePrivateParameters ? KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P256_MAGIC : KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P256_MAGIC); case AlgorithmName.ECDsaP384: return(includePrivateParameters ? KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P384_MAGIC : KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P384_MAGIC); case AlgorithmName.ECDsaP521: return(includePrivateParameters ? KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P521_MAGIC : KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P521_MAGIC); default: // all other curves are new in Win10 so use named curves return(includePrivateParameters ? KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_GENERIC_MAGIC : KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_GENERIC_MAGIC); } }
public override void GenerateKey(ECCurve curve) { curve.Validate(); _core.DisposeKey(); if (curve.IsNamed) { if (string.IsNullOrEmpty(curve.Oid.FriendlyName)) { throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_InvalidCurveOid, curve.Oid.Value)); } // Map curve name to algorithm to support pre-Win10 curves CngAlgorithm alg = CngKey.EcdsaCurveNameToAlgorithm(curve.Oid.FriendlyName); if (CngKey.IsECNamedCurve(alg.Algorithm)) { CngKey key = _core.GetOrGenerateKey(curve); ForceSetKeySize(key.KeySize); } else { int keySize; // Get the proper KeySize from algorithm name if (alg == CngAlgorithm.ECDsaP256) { keySize = 256; } else if (alg == CngAlgorithm.ECDsaP384) { keySize = 384; } else if (alg == CngAlgorithm.ECDsaP521) { keySize = 521; } else { Debug.Fail($"Unknown algorithm {alg}"); throw new ArgumentException(SR.Cryptography_InvalidKeySize); } _core.GetOrGenerateKey(keySize, alg); ForceSetKeySize(keySize); } } else if (curve.IsExplicit) { CngKey key = _core.GetOrGenerateKey(curve); ForceSetKeySize(key.KeySize); } else { throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_CurveNotSupported, curve.CurveType.ToString())); } }
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.EcdsaCurveNameToAlgorithm(name)); m_key = newKey; KeySizeValue = newKey.KeySize; }
private CngKey GetKey( #if !NETNATIVE ECCurve?curve = null #endif ) { CngKey key = null; CngAlgorithm algorithm = null; int keySize = 0; #if !NETNATIVE if (curve != null) { if (curve.Value.IsNamed) { // Map curve name to algorithm to support pre-Win10 curves CngAlgorithm alg = CngKey.EcdsaCurveNameToAlgorithm(curve.Value.Oid.FriendlyName); if (CngKey.IsECNamedCurve(alg.Algorithm)) { key = _core.GetOrGenerateKey(curve.Value); } else { // Get the proper KeySize from algorithm name if (alg == CngAlgorithm.ECDsaP256) { keySize = 256; } else if (alg == CngAlgorithm.ECDsaP384) { keySize = 384; } else if (alg == CngAlgorithm.ECDsaP521) { keySize = 521; } else { Debug.Fail(string.Format("Unknown algorithm {0}", alg.ToString())); throw new ArgumentException(SR.Cryptography_InvalidKeySize); } key = _core.GetOrGenerateKey(keySize, alg); } ForceSetKeySize(key.KeySize); } else if (curve.Value.IsExplicit) { key = _core.GetOrGenerateKey(curve.Value); ForceSetKeySize(key.KeySize); } else { throw new PlatformNotSupportedException(string.Format(SR.Cryptography_CurveNotSupported, curve.Value.CurveType.ToString())); } } else if (_core.IsKeyGeneratedNamedCurve()) { key = _core.GetOrGenerateKey(null); } else #endif { // Map the current key size to a CNG algorithm name keySize = KeySize; switch (keySize) { case 256: algorithm = CngAlgorithm.ECDsaP256; break; case 384: algorithm = CngAlgorithm.ECDsaP384; break; case 521: algorithm = CngAlgorithm.ECDsaP521; break; default: Debug.Fail("Should not have invalid key size"); throw new ArgumentException(SR.Cryptography_InvalidKeySize); } key = _core.GetOrGenerateKey(keySize, algorithm); } return(key); }