/// <summary> /// Compare params /// </summary> /// <param name="parameters"></param> /// <param name="other"></param> /// <returns></returns> public static bool SameAs(this EccParams parameters, EccParams other) { if (parameters == null) { return(other == null); } if (other == null) { return(false); } if (other.Curve != parameters.Curve) { return(false); } if (!KeyEx.SameNoLeadingZeros(other.X, parameters.X)) { return(false); } if (!KeyEx.SameNoLeadingZeros(other.Y, parameters.Y)) { return(false); } if (!KeyEx.SameNoLeadingZeros(other.T, parameters.T)) { return(false); } if (!KeyEx.SameNoLeadingZeros(other.D, parameters.D)) { return(false); } return(true); }
/// <summary> /// Returns the public part of the key /// </summary> /// <param name="key"></param> /// <returns></returns> public static Key GetPublicKey(this EccParams key) { return(new Key { Type = KeyType.ECC, Parameters = new EccParams { Curve = key.Curve, X = key.X, Y = key.Y, T = key.T } }); }
/// <summary> /// Clone params /// </summary> /// <param name="parameters"></param> /// <returns></returns> public static EccParams Clone(this EccParams parameters) { if (parameters == null) { return(null); } return(new EccParams { Curve = parameters.Curve, D = parameters.D, T = parameters.T, X = parameters.X, Y = parameters.Y }); }
/// <summary> /// Convert to public key /// </summary> /// <param name="ecc"></param> /// <returns></returns> public static PublicKey ToPublicKey(this EccParams ecc) { var ecParameters = ecc.ToECParameters(); var curveOid = ecParameters.Curve.Oid.Value; byte[] curveOidEncoded; if (string.IsNullOrEmpty(curveOid)) { var friendlyName = ecParameters.Curve.Oid.FriendlyName; switch (friendlyName) { case "nistP256": curveOid = Oids.Secp256r1; break; case "nistP384": curveOid = Oids.Secp384r1; break; case "nistP521": curveOid = Oids.Secp521r1; break; default: curveOid = new Oid(friendlyName).Value; break; } } using (var writer = new AsnWriter(AsnEncodingRules.DER)) { writer.WriteObjectIdentifier(curveOid); curveOidEncoded = writer.Encode(); } Debug.Assert(ecParameters.Q.X.Length == ecParameters.Q.Y.Length); var uncompressedPoint = new byte[1 + ecParameters.Q.X.Length + ecParameters.Q.Y.Length]; // Uncompressed point (0x04) uncompressedPoint[0] = 0x04; Buffer.BlockCopy(ecParameters.Q.X, 0, uncompressedPoint, 1, ecParameters.Q.X.Length); Buffer.BlockCopy(ecParameters.Q.Y, 0, uncompressedPoint, 1 + ecParameters.Q.X.Length, ecParameters.Q.Y.Length); var ecPublicKey = new Oid(Oids.EcPublicKey); return(new PublicKey( ecPublicKey, new AsnEncodedData(ecPublicKey, curveOidEncoded), new AsnEncodedData(ecPublicKey, uncompressedPoint))); }
/// <summary> /// Converts a WebKey of type EC or EC-HSM to an EC parameter object. /// </summary> /// <param name="ecParameters"></param> /// <param name="includePrivateParameters">private material must be /// included.</param> /// <returns>An EC parameter object</returns> public static ECParameters ToECParameters(this EccParams ecParameters, bool includePrivateParameters = true) { KeyEx.VerifyNonZero(ecParameters.X); KeyEx.VerifyNonZero(ecParameters.Y); var keyParameterSize = ecParameters.Curve.GetKeyParameterSize(); if (includePrivateParameters && ecParameters.D != null) { KeyEx.VerifyNonZero(ecParameters.D); ecParameters.D = KeyEx.ForceLength(ecParameters.D, keyParameterSize); } return(new ECParameters { Curve = ecParameters.Curve.ToECCurve(), D = ecParameters.D, Q = new ECPoint { X = KeyEx.ForceLength(ecParameters.X, keyParameterSize), Y = KeyEx.ForceLength(ecParameters.Y, keyParameterSize) } }); }
/// <summary> /// Verifies whether this object has a private key /// </summary> /// <returns> True if the object has private key; false otherwise.</returns> public static bool HasPrivateKey(this EccParams key) { return(key.D != null); }