/// <summary> /// ImportParameters will replace the existing key that ECDsaCng is working with by creating a /// new CngKey. If the parameters contains only Q, then only a public key will be imported. /// If the parameters also contains D, then a full key pair will be imported. /// The parameters Curve value specifies the type of the curve to import. /// </summary> /// <exception cref="CryptographicException"> /// if <paramref name="parameters" /> does not contain valid values. /// </exception> /// <exception cref="NotSupportedException"> /// if <paramref name="parameters" /> references a curve that cannot be imported. /// </exception> /// <exception cref="PlatformNotSupportedException"> /// if <paramref name="parameters" /> references a curve that is not supported by this platform. /// </exception> public override void ImportParameters(ECParameters parameters) { parameters.Validate(); bool includePrivateParameters = (parameters.D != null); ECCurve curve = parameters.Curve; if (curve.IsPrime) { Key = ECCng.ImportPrimeCurveParameters(parameters, includePrivateParameters); } else if (curve.IsNamed) { // FriendlyName is required; an attempt was already made to default it in ECCurve if (string.IsNullOrEmpty(curve.Oid.FriendlyName)) { throw new PlatformNotSupportedException(string.Format(SR.Cryptography_InvalidCurveOid, curve.Oid.Value.ToString())); } Key = ECCng.ImportNamedCurveParameters(parameters, includePrivateParameters); } else { throw new PlatformNotSupportedException(string.Format(SR.Cryptography_CurveNotSupported, curve.CurveType.ToString())); } }
internal static ECDiffieHellmanCngPublicKey FromKey(CngKey key) { CngKeyBlobFormat format; string curveName; byte[] blob = ECCng.ExportKeyBlob(key, false, out format, out curveName); return(new ECDiffieHellmanCngPublicKey(blob, curveName, format)); }
/// <summary> /// Exports the key and explicit curve parameters used by the ECC object into an <see cref="ECParameters"/> object. /// </summary> /// <exception cref="CryptographicException"> /// if there was an issue obtaining the curve values. /// </exception> /// <exception cref="PlatformNotSupportedException"> /// if explicit export is not supported by this platform. Windows 10 or higher is required. /// </exception> /// <returns>The key and explicit curve parameters used by the ECC object.</returns> public override ECParameters ExportExplicitParameters(bool includePrivateParameters) { byte[] blob = ExportFullKeyBlob(includePrivateParameters); ECParameters ecparams = new ECParameters(); ECCng.ExportPrimeCurveParameters(ref ecparams, blob, includePrivateParameters); return(ecparams); }
/// <summary> /// Exports the key and explicit curve parameters used by the ECC object into an <see cref="ECParameters"/> object. /// </summary> /// <exception cref="CryptographicException"> /// if there was an issue obtaining the curve values. /// </exception> /// <exception cref="PlatformNotSupportedException"> /// if explicit export is not supported by this platform. Windows 10 or higher is required. /// </exception> /// <returns>The key and explicit curve parameters used by the ECC object.</returns> public override ECParameters ExportExplicitParameters() { using (CngKey key = Import()) { ECParameters ecparams = new ECParameters(); byte[] blob = ECCng.ExportFullKeyBlob(key, includePrivateParameters: false); ECCng.ExportPrimeCurveParameters(ref ecparams, blob, includePrivateParameters: false); return(ecparams); } }
public CngKey GetOrGenerateKey(ECCurve?curve) { ThrowIfDisposed(); if (_lazyKey != null) { return(_lazyKey); } // We don't have a key yet so generate Debug.Assert(curve.HasValue); CngKeyCreationParameters creationParameters = new CngKeyCreationParameters() { ExportPolicy = CngExportPolicies.AllowPlaintextExport, }; if (curve.Value.IsNamed) { creationParameters.Parameters.Add(CngKey.GetPropertyFromNamedCurve(curve.Value)); } else if (curve.Value.IsPrime) { ECCurve eccurve = curve.Value; byte[] parametersBlob = ECCng.GetPrimeCurveParameterBlob(ref eccurve); CngProperty prop = new CngProperty( Interop.BCrypt.BCryptPropertyStrings.BCRYPT_ECC_PARAMETERS, parametersBlob, CngPropertyOptions.None); creationParameters.Parameters.Add(prop); } else { throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_CurveNotSupported, curve.Value.CurveType.ToString())); } try { _lazyKey = CngKey.Create(DefaultKeyType ?? CngAlgorithm.ECDsa, null, creationParameters); } catch (CryptographicException e) { // Map to PlatformNotSupportedException if appropriate ErrorCode errorCode = (ErrorCode)e.HResult; if (curve.Value.IsNamed && errorCode == ErrorCode.NTE_INVALID_PARAMETER || errorCode == ErrorCode.NTE_NOT_SUPPORTED) { throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_CurveNotSupported, curve.Value.Oid.FriendlyName), e); } throw; } return(_lazyKey); }
/// <summary> /// Exports the key used by the ECC object into an <see cref="ECParameters"/> object. /// If the curve has a name, the Curve property will contain named curve parameters otherwise it will contain explicit parameters. /// </summary> /// <exception cref="CryptographicException"> /// if there was an issue obtaining the curve values. /// </exception> /// <returns>The key and named curve parameters used by the ECC object.</returns> public override ECParameters ExportParameters(bool includePrivateParameters) { ECParameters ecparams; if (string.IsNullOrEmpty(Key.GetCurveName())) { ecparams = ECCng.ExportPrimeCurveParameters(Key, includePrivateParameters); } else { ecparams = ECCng.ExportNamedCurveParameters(Key, includePrivateParameters); } return(ecparams); }
public override ECParameters ExportExplicitParameters(bool includePrivateParameters) { byte[] blob = ExportFullKeyBlob(includePrivateParameters); try { ECParameters ecparams = new ECParameters(); ECCng.ExportPrimeCurveParameters(ref ecparams, blob, includePrivateParameters); return(ecparams); } finally { Array.Clear(blob, 0, blob.Length); } }
/// <summary> /// Exports the key used by the ECC object into an <see cref="ECParameters"/> object. /// If the curve has a name, the Curve property will contain named curve parameters otherwise it will contain explicit parameters. /// </summary> /// <exception cref="CryptographicException"> /// if there was an issue obtaining the curve values. /// </exception> /// <returns>The key and named curve parameters used by the ECC object.</returns> public override ECParameters ExportParameters(bool includePrivateParameters) { ECParameters ecparams = new ECParameters(); string curveName = GetCurveName(); if (string.IsNullOrEmpty(curveName)) { byte[] fullKeyBlob = ExportFullKeyBlob(includePrivateParameters); ECCng.ExportPrimeCurveParameters(ref ecparams, fullKeyBlob, includePrivateParameters); } else { byte[] keyBlob = ExportKeyBlob(includePrivateParameters); ECCng.ExportNamedCurveParameters(ref ecparams, keyBlob, includePrivateParameters); ecparams.Curve = ECCurve.CreateFromFriendlyName(curveName); } return(ecparams); }
/// <summary> /// Exports the key used by the ECC object into an <see cref="ECParameters"/> object. /// If the key was created as a named curve, the Curve property will contain named curve parameters /// otherwise it will contain explicit parameters. /// </summary> /// <exception cref="CryptographicException"> /// if there was an issue obtaining the curve values. /// </exception> /// <returns>The key and named curve parameters used by the ECC object.</returns> public override ECParameters ExportParameters() { using (CngKey key = Import()) { ECParameters ecparams = new ECParameters(); string curveName = key.GetCurveName(out _); if (string.IsNullOrEmpty(curveName)) { byte[] fullKeyBlob = ECCng.ExportFullKeyBlob(key, includePrivateParameters: false); ECCng.ExportPrimeCurveParameters(ref ecparams, fullKeyBlob, includePrivateParameters: false); } else { byte[] keyBlob = ECCng.ExportKeyBlob(key, includePrivateParameters: false); ECCng.ExportNamedCurveParameters(ref ecparams, keyBlob, includePrivateParameters: false); ecparams.Curve = ECCurve.CreateFromFriendlyName(curveName); } return(ecparams); } }
/// <summary> /// Exports the key and explicit curve parameters used by the ECC object into an <see cref="ECParameters"/> object. /// </summary> /// <exception cref="CryptographicException"> /// if there was an issue obtaining the curve values. /// </exception> /// <exception cref="PlatformNotSupportedException"> /// if explicit export is not supported by this platform. Windows 10 or higher is required. /// </exception> /// <returns>The key and explicit curve parameters used by the ECC object.</returns> public override ECParameters ExportExplicitParameters(bool includePrivateParameters) { return(ECCng.ExportPrimeCurveParameters(Key, includePrivateParameters)); }