3 // Copyright (c) Microsoft Corporation. All rights reserved.
10 namespace System.Security.Cryptography {
12 /// Base class for implementations of elliptic curve DSA
14 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
15 public abstract class ECDsa : AsymmetricAlgorithm {
16 public override string KeyExchangeAlgorithm {
20 public override string SignatureAlgorithm {
21 get { return "ECDsa"; }
25 // Creation factory methods
28 public static new ECDsa Create() {
30 throw new NotImplementedException ();
32 return Create(typeof(ECDsaCng).FullName);
36 public static new ECDsa Create(string algorithm) {
37 if (algorithm == null) {
38 throw new ArgumentNullException("algorithm");
41 return CryptoConfig.CreateFromName(algorithm) as ECDsa;
44 public static ECDsa Create (ECCurve curve)
46 throw new NotImplementedException ();
49 public static ECDsa Create (ECParameters parameters)
51 throw new NotImplementedException ();
54 public virtual ECParameters ExportExplicitParameters (bool includePrivateParameters)
56 throw new NotImplementedException ();
59 public virtual ECParameters ExportParameters (bool includePrivateParameters)
61 throw new NotImplementedException ();
64 public virtual void GenerateKey (ECCurve curve)
66 throw new NotImplementedException ();
69 public virtual void ImportParameters (ECParameters parameters)
71 throw new NotImplementedException ();
75 // Signature operations
78 // ECDsa does not encode the algorithm identifier into the signature blob, therefore SignHash and VerifyHash
79 // do not need the HashAlgorithmName value, only SignData and VerifyData do.
80 public abstract byte[] SignHash(byte[] hash);
81 public abstract bool VerifyHash(byte[] hash, byte[] signature);
83 protected virtual byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) {
84 throw DerivedClassMustOverride();
87 protected virtual byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) {
88 throw DerivedClassMustOverride();
91 public virtual byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm) {
93 throw new ArgumentNullException("data");
95 return SignData(data, 0, data.Length, hashAlgorithm);
98 public virtual byte[] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) {
99 if (data == null) { throw new ArgumentNullException("data"); }
100 if (offset < 0 || offset > data.Length) { throw new ArgumentOutOfRangeException("offset"); }
101 if (count < 0 || count > data.Length - offset) { throw new ArgumentOutOfRangeException("count"); }
102 if (String.IsNullOrEmpty(hashAlgorithm.Name)) { throw HashAlgorithmNameNullOrEmpty(); }
104 byte[] hash = HashData(data, offset, count, hashAlgorithm);
105 return SignHash(hash);
108 public virtual byte[] SignData(Stream data, HashAlgorithmName hashAlgorithm) {
110 throw new ArgumentNullException("data");
112 if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
113 throw HashAlgorithmNameNullOrEmpty();
116 byte[] hash = HashData(data, hashAlgorithm);
117 return SignHash(hash);
120 public bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm) {
122 throw new ArgumentNullException("data");
124 return VerifyData(data, 0, data.Length, signature, hashAlgorithm);
127 public virtual bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm) {
129 throw new ArgumentNullException("data");
131 if (offset < 0 || offset > data.Length) {
132 throw new ArgumentOutOfRangeException("offset");
134 if (count < 0 || count > data.Length - offset) {
135 throw new ArgumentOutOfRangeException("count");
137 if (signature == null) {
138 throw new ArgumentNullException("signature");
140 if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
141 throw HashAlgorithmNameNullOrEmpty();
144 byte[] hash = HashData(data, offset, count, hashAlgorithm);
145 return VerifyHash(hash, signature);
148 public bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm) {
150 throw new ArgumentNullException("data");
152 if (signature == null) {
153 throw new ArgumentNullException("signature");
155 if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
156 throw HashAlgorithmNameNullOrEmpty();
159 byte[] hash = HashData(data, hashAlgorithm);
160 return VerifyHash(hash, signature);
163 private static Exception DerivedClassMustOverride() {
164 return new NotImplementedException(SR.GetString(SR.NotSupported_SubclassOverride));
167 internal static Exception HashAlgorithmNameNullOrEmpty() {
168 return new ArgumentException(SR.GetString(SR.Cryptography_HashAlgorithmNameNullOrEmpty), "hashAlgorithm");