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 /// Creates a new instance of the default implementation of the Elliptic Curve Digital Signature Algorithm
46 /// (ECDSA) with a newly generated key over the specified curve.
48 /// <param name="curve">The curve to use for key generation.</param>
49 /// <returns>A new instance of the default implementation of this class.</returns>
50 public static ECDsa
Create(ECCurve curve
) {
51 ECDsa ecdsa
= Create();
55 ecdsa
.GenerateKey(curve
);
67 /// Creates a new instance of the default implementation of the Elliptic Curve Digital Signature Algorithm
68 /// (ECDSA) using the specified ECParameters as the key.
70 /// <param name="parameters">The parameters representing the key to use.</param>
71 /// <returns>A new instance of the default implementation of this class.</returns>
72 public static ECDsa
Create(ECParameters parameters
) {
73 ECDsa ecdsa
= Create();
77 ecdsa
.ImportParameters(parameters
);
89 // Signature operations
92 // ECDsa does not encode the algorithm identifier into the signature blob, therefore SignHash and VerifyHash
93 // do not need the HashAlgorithmName value, only SignData and VerifyData do.
94 public abstract byte[] SignHash(byte[] hash
);
95 public abstract bool VerifyHash(byte[] hash
, byte[] signature
);
97 protected virtual byte[] HashData(byte[] data
, int offset
, int count
, HashAlgorithmName hashAlgorithm
) {
98 throw DerivedClassMustOverride();
101 protected virtual byte[] HashData(Stream data
, HashAlgorithmName hashAlgorithm
) {
102 throw DerivedClassMustOverride();
105 public virtual byte[] SignData(byte[] data
, HashAlgorithmName hashAlgorithm
) {
107 throw new ArgumentNullException("data");
109 return SignData(data
, 0, data
.Length
, hashAlgorithm
);
112 public virtual byte[] SignData(byte[] data
, int offset
, int count
, HashAlgorithmName hashAlgorithm
) {
113 if (data
== null) { throw new ArgumentNullException("data"); }
114 if (offset
< 0 || offset
> data
.Length
) { throw new ArgumentOutOfRangeException("offset"); }
115 if (count
< 0 || count
> data
.Length
- offset
) { throw new ArgumentOutOfRangeException("count"); }
116 if (String
.IsNullOrEmpty(hashAlgorithm
.Name
)) { throw HashAlgorithmNameNullOrEmpty(); }
118 byte[] hash
= HashData(data
, offset
, count
, hashAlgorithm
);
119 return SignHash(hash
);
122 public virtual byte[] SignData(Stream data
, HashAlgorithmName hashAlgorithm
) {
124 throw new ArgumentNullException("data");
126 if (String
.IsNullOrEmpty(hashAlgorithm
.Name
)) {
127 throw HashAlgorithmNameNullOrEmpty();
130 byte[] hash
= HashData(data
, hashAlgorithm
);
131 return SignHash(hash
);
134 public bool VerifyData(byte[] data
, byte[] signature
, HashAlgorithmName hashAlgorithm
) {
136 throw new ArgumentNullException("data");
138 return VerifyData(data
, 0, data
.Length
, signature
, hashAlgorithm
);
141 public virtual bool VerifyData(byte[] data
, int offset
, int count
, byte[] signature
, HashAlgorithmName hashAlgorithm
) {
143 throw new ArgumentNullException("data");
145 if (offset
< 0 || offset
> data
.Length
) {
146 throw new ArgumentOutOfRangeException("offset");
148 if (count
< 0 || count
> data
.Length
- offset
) {
149 throw new ArgumentOutOfRangeException("count");
151 if (signature
== null) {
152 throw new ArgumentNullException("signature");
154 if (String
.IsNullOrEmpty(hashAlgorithm
.Name
)) {
155 throw HashAlgorithmNameNullOrEmpty();
158 byte[] hash
= HashData(data
, offset
, count
, hashAlgorithm
);
159 return VerifyHash(hash
, signature
);
162 public bool VerifyData(Stream data
, byte[] signature
, HashAlgorithmName hashAlgorithm
) {
164 throw new ArgumentNullException("data");
166 if (signature
== null) {
167 throw new ArgumentNullException("signature");
169 if (String
.IsNullOrEmpty(hashAlgorithm
.Name
)) {
170 throw HashAlgorithmNameNullOrEmpty();
173 byte[] hash
= HashData(data
, hashAlgorithm
);
174 return VerifyHash(hash
, signature
);
178 /// When overridden in a derived class, exports the named or explicit ECParameters for an ECCurve.
179 /// If the curve has a name, the Curve property will contain named curve parameters, otherwise it
180 /// will contain explicit parameters.
182 /// <param name="includePrivateParameters">true to include private parameters, otherwise, false.</param>
183 /// <returns>The ECParameters representing the point on the curve for this key.</returns>
184 public virtual ECParameters
ExportParameters(bool includePrivateParameters
) {
185 throw new NotSupportedException(SR
.GetString(SR
.NotSupported_SubclassOverride
));
189 /// When overridden in a derived class, exports the explicit ECParameters for an ECCurve.
191 /// <param name="includePrivateParameters">true to include private parameters, otherwise, false.</param>
192 /// <returns>The ECParameters representing the point on the curve for this key, using the explicit curve format.</returns>
193 public virtual ECParameters
ExportExplicitParameters(bool includePrivateParameters
) {
194 throw new NotSupportedException(SR
.GetString(SR
.NotSupported_SubclassOverride
));
198 /// When overridden in a derived class, imports the specified ECParameters.
200 /// <param name="parameters">The curve parameters.</param>
201 public virtual void ImportParameters(ECParameters parameters
) {
202 throw new NotSupportedException(SR
.GetString(SR
.NotSupported_SubclassOverride
));
206 /// When overridden in a derived class, generates a new public/private keypair for the specified curve.
208 /// <param name="curve">The curve to use.</param>
209 public virtual void GenerateKey(ECCurve curve
) {
210 throw new NotSupportedException(SR
.GetString(SR
.NotSupported_SubclassOverride
));
213 private static Exception
DerivedClassMustOverride() {
214 return new NotImplementedException(SR
.GetString(SR
.NotSupported_SubclassOverride
));
217 internal static Exception
HashAlgorithmNameNullOrEmpty() {
218 return new ArgumentException(SR
.GetString(SR
.Cryptography_HashAlgorithmNameNullOrEmpty
), "hashAlgorithm");