Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Core / System / Security / Cryptography / ECDsa.cs
blobb0c7b521a880506ce7a3eb49ecef6aeb4968e4dc
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
7 using System;
8 using System.IO;
10 namespace System.Security.Cryptography {
11 /// <summary>
12 /// Base class for implementations of elliptic curve DSA
13 /// </summary>
14 [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
15 public abstract class ECDsa : AsymmetricAlgorithm {
16 public override string KeyExchangeAlgorithm {
17 get { return null; }
20 public override string SignatureAlgorithm {
21 get { return "ECDsa"; }
25 // Creation factory methods
28 public static new ECDsa Create() {
29 #if MONO
30 throw new NotImplementedException ();
31 #else
32 return Create(typeof(ECDsaCng).FullName);
33 #endif
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 /// <summary>
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.
47 /// </summary>
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();
53 if (ecdsa != null) {
54 try {
55 ecdsa.GenerateKey(curve);
57 catch {
58 ecdsa.Dispose();
59 throw;
63 return ecdsa;
66 /// <summary>
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.
69 /// </summary>
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();
75 if (ecdsa != null) {
76 try {
77 ecdsa.ImportParameters(parameters);
79 catch {
80 ecdsa.Dispose();
81 throw;
85 return ecdsa;
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) {
106 if (data == null) {
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) {
123 if (data == null) {
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) {
135 if (data == null) {
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) {
142 if (data == null) {
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) {
163 if (data == null) {
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);
177 /// <summary>
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.
181 /// </summary>
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));
188 /// <summary>
189 /// When overridden in a derived class, exports the explicit ECParameters for an ECCurve.
190 /// </summary>
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));
197 /// <summary>
198 /// When overridden in a derived class, imports the specified ECParameters.
199 /// </summary>
200 /// <param name="parameters">The curve parameters.</param>
201 public virtual void ImportParameters(ECParameters parameters) {
202 throw new NotSupportedException(SR.GetString(SR.NotSupported_SubclassOverride));
205 /// <summary>
206 /// When overridden in a derived class, generates a new public/private keypair for the specified curve.
207 /// </summary>
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");