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 // Signature operations
48 // ECDsa does not encode the algorithm identifier into the signature blob, therefore SignHash and VerifyHash
49 // do not need the HashAlgorithmName value, only SignData and VerifyData do.
50 public abstract byte[] SignHash(byte[] hash);
51 public abstract bool VerifyHash(byte[] hash, byte[] signature);
53 protected virtual byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) {
54 throw DerivedClassMustOverride();
57 protected virtual byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) {
58 throw DerivedClassMustOverride();
61 public virtual byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm) {
63 throw new ArgumentNullException("data");
65 return SignData(data, 0, data.Length, hashAlgorithm);
68 public virtual byte[] SignData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) {
69 if (data == null) { throw new ArgumentNullException("data"); }
70 if (offset < 0 || offset > data.Length) { throw new ArgumentOutOfRangeException("offset"); }
71 if (count < 0 || count > data.Length - offset) { throw new ArgumentOutOfRangeException("count"); }
72 if (String.IsNullOrEmpty(hashAlgorithm.Name)) { throw HashAlgorithmNameNullOrEmpty(); }
74 byte[] hash = HashData(data, offset, count, hashAlgorithm);
75 return SignHash(hash);
78 public virtual byte[] SignData(Stream data, HashAlgorithmName hashAlgorithm) {
80 throw new ArgumentNullException("data");
82 if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
83 throw HashAlgorithmNameNullOrEmpty();
86 byte[] hash = HashData(data, hashAlgorithm);
87 return SignHash(hash);
90 public bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm) {
92 throw new ArgumentNullException("data");
94 return VerifyData(data, 0, data.Length, signature, hashAlgorithm);
97 public virtual bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm) {
99 throw new ArgumentNullException("data");
101 if (offset < 0 || offset > data.Length) {
102 throw new ArgumentOutOfRangeException("offset");
104 if (count < 0 || count > data.Length - offset) {
105 throw new ArgumentOutOfRangeException("count");
107 if (signature == null) {
108 throw new ArgumentNullException("signature");
110 if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
111 throw HashAlgorithmNameNullOrEmpty();
114 byte[] hash = HashData(data, offset, count, hashAlgorithm);
115 return VerifyHash(hash, signature);
118 public bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm) {
120 throw new ArgumentNullException("data");
122 if (signature == null) {
123 throw new ArgumentNullException("signature");
125 if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
126 throw HashAlgorithmNameNullOrEmpty();
129 byte[] hash = HashData(data, hashAlgorithm);
130 return VerifyHash(hash, signature);
133 private static Exception DerivedClassMustOverride() {
134 return new NotImplementedException(SR.GetString(SR.NotSupported_SubclassOverride));
137 internal static Exception HashAlgorithmNameNullOrEmpty() {
138 return new ArgumentException(SR.GetString(SR.Cryptography_HashAlgorithmNameNullOrEmpty), "hashAlgorithm");