/// <summary> /// Parses an elliptic curve point. /// </summary> /// <param name="data">octet data</param> /// <param name="ec">elliptic curve domain parameters</param> /// <param name="p">an elliptic curve point object</param> /// <returns>true if parsing and validation succeeded</returns> public static bool Parse(byte[] data, EllipticCurve ec, out ECPoint p) { if (IsZero(data)) { // point at infinity p = null; return false; } if (data.Length < 2) { // invalid length p = null; return false; } if (data[0] == 0x04) { ECPoint tmp = ParseUncompressed(data); if (tmp == null) { p = null; return false; } if (!ec.ValidatePoint(tmp)) { p = null; return false; } p = tmp; p.Validated = true; return true; } // Compressed form of EC point is not supported. // OpenSSL, which is used by OpenSSH for cryptography, disables // EC point compression by default due to the patent reason. p = null; return false; }
/// <summary> /// Chooses a hashing algorithm. /// </summary> /// <remarks> /// Hashing algorithm is determined according to the curve size as described in RFC5656. /// </remarks> /// <param name="curve">elliptic curve</param> /// <returns>new instance of the hashing algorithm</returns> public static HashAlgorithm Choose(EllipticCurve curve) { int orderBits = curve.Order.BitCount(); if (orderBits <= 256) { return new SHA256CryptoServiceProvider(); } else if (orderBits <= 384) { return new SHA384CryptoServiceProvider(); } else { return new SHA512CryptoServiceProvider(); } }
/// <summary> /// Hash data for signing /// </summary> /// <param name="data">data to be hashed</param> /// <param name="curve">elliptic curve for determining hashing algorithm</param> /// <returns></returns> internal byte[] HashForSigning(byte[] data, EllipticCurve curve) { byte[] hash; using (var hashAlgorithm = ECDSAHashAlgorithmChooser.Choose(curve)) { hash = hashAlgorithm.ComputeHash(data); } return ExtractLeftBits(hash, curve.Order.BitCount()); }
/// <summary> /// Constructor /// </summary> /// <param name="curve">elliptic curve</param> /// <param name="publicKey">public key</param> /// <param name="privateKey">private key</param> public ECDSAKeyPair(EllipticCurve curve, ECDSAPublicKey publicKey, BigInteger privateKey) { _curve = curve; _publicKey = publicKey; _privateKey = privateKey; }
public ECDSAPublicKey(EllipticCurve curve, ECPoint point) { _curve = curve; _point = point; }