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;
45 public static ECDsa Create (ECCurve curve)
47 throw new NotImplementedException ();
50 public static ECDsa Create (ECParameters parameters)
52 throw new NotImplementedException ();
55 public virtual ECParameters ExportExplicitParameters (bool includePrivateParameters)
57 throw new NotImplementedException ();
60 public virtual ECParameters ExportParameters (bool includePrivateParameters)
62 throw new NotImplementedException ();
65 public virtual void GenerateKey (ECCurve curve)
67 throw new NotImplementedException ();
70 public virtual void ImportParameters (ECParameters parameters)
72 throw new NotImplementedException ();
77 // Signature operations
80 // ECDsa does not encode the algorithm identifier into the signature blob, therefore SignHash and VerifyHash
81 // do not need the HashAlgorithmName value, only SignData and VerifyData do.
82 public abstract byte[] SignHash(byte[] hash);
83 public abstract bool VerifyHash(byte[] hash, byte[] signature);
85 protected virtual byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) {
86 throw DerivedClassMustOverride();
89 protected virtual byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) {
90 throw DerivedClassMustOverride();
93 public virtual byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm) {
95 throw new ArgumentNullException("data");
97 return SignData(data, 0, data.Length, hashAlgorithm);
100 public virtual byte[] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) {
101 if (data == null) { throw new ArgumentNullException("data"); }
102 if (offset < 0 || offset > data.Length) { throw new ArgumentOutOfRangeException("offset"); }
103 if (count < 0 || count > data.Length - offset) { throw new ArgumentOutOfRangeException("count"); }
104 if (String.IsNullOrEmpty(hashAlgorithm.Name)) { throw HashAlgorithmNameNullOrEmpty(); }
106 byte[] hash = HashData(data, offset, count, hashAlgorithm);
107 return SignHash(hash);
110 public virtual byte[] SignData(Stream data, HashAlgorithmName hashAlgorithm) {
112 throw new ArgumentNullException("data");
114 if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
115 throw HashAlgorithmNameNullOrEmpty();
118 byte[] hash = HashData(data, hashAlgorithm);
119 return SignHash(hash);
122 public bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm) {
124 throw new ArgumentNullException("data");
126 return VerifyData(data, 0, data.Length, signature, hashAlgorithm);
129 public virtual bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm) {
131 throw new ArgumentNullException("data");
133 if (offset < 0 || offset > data.Length) {
134 throw new ArgumentOutOfRangeException("offset");
136 if (count < 0 || count > data.Length - offset) {
137 throw new ArgumentOutOfRangeException("count");
139 if (signature == null) {
140 throw new ArgumentNullException("signature");
142 if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
143 throw HashAlgorithmNameNullOrEmpty();
146 byte[] hash = HashData(data, offset, count, hashAlgorithm);
147 return VerifyHash(hash, signature);
150 public bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm) {
152 throw new ArgumentNullException("data");
154 if (signature == null) {
155 throw new ArgumentNullException("signature");
157 if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
158 throw HashAlgorithmNameNullOrEmpty();
161 byte[] hash = HashData(data, hashAlgorithm);
162 return VerifyHash(hash, signature);
165 private static Exception DerivedClassMustOverride() {
166 return new NotImplementedException(SR.GetString(SR.NotSupported_SubclassOverride));
169 internal static Exception HashAlgorithmNameNullOrEmpty() {
170 return new ArgumentException(SR.GetString(SR.Cryptography_HashAlgorithmNameNullOrEmpty), "hashAlgorithm");