[threads] Don't ignore abort requests in abort protected blocks
[mono.git] / mcs / class / referencesource / System.Core / System / Security / Cryptography / ECDsa.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6
7 using System;
8 using System.IO;
9
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; }
18         }
19
20         public override string SignatureAlgorithm {
21             get { return "ECDsa"; }
22         }
23
24         //
25         // Creation factory methods
26         //
27
28         public static new ECDsa Create() {
29 #if MONO
30             throw new NotImplementedException ();
31 #else
32             return Create(typeof(ECDsaCng).FullName);
33 #endif
34         }
35
36         public static new ECDsa Create(string algorithm) {
37             if (algorithm == null) {
38                 throw new ArgumentNullException("algorithm");
39             }
40
41             return CryptoConfig.CreateFromName(algorithm) as ECDsa;
42         }
43
44 #if NETSTANDARD
45         public static ECDsa Create (ECCurve curve)
46         {
47             throw new NotImplementedException ();
48         }
49
50         public static ECDsa Create (ECParameters parameters)
51         {
52             throw new NotImplementedException ();
53         }
54
55         public virtual ECParameters ExportExplicitParameters (bool includePrivateParameters)
56         {
57             throw new NotImplementedException ();
58         }
59
60         public virtual ECParameters ExportParameters (bool includePrivateParameters)
61         {
62             throw new NotImplementedException ();
63         }
64
65         public virtual void GenerateKey (ECCurve curve)
66         {
67             throw new NotImplementedException ();
68         }
69
70         public virtual void ImportParameters (ECParameters parameters)
71         {
72             throw new NotImplementedException ();
73         }
74 #endif
75
76         //
77         // Signature operations
78         //
79
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);
84
85         protected virtual byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) {
86             throw DerivedClassMustOverride();
87         }
88
89         protected virtual byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) {
90             throw DerivedClassMustOverride();
91         }
92
93         public virtual byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm) {
94             if (data == null) {
95                 throw new ArgumentNullException("data");
96             }
97             return SignData(data, 0, data.Length, hashAlgorithm);
98         }
99
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(); }
105
106             byte[] hash = HashData(data, offset, count, hashAlgorithm);
107             return SignHash(hash);
108         }
109
110         public virtual byte[] SignData(Stream data, HashAlgorithmName hashAlgorithm) {
111             if (data == null) {
112                 throw new ArgumentNullException("data");
113             }
114             if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
115                 throw HashAlgorithmNameNullOrEmpty();
116             }
117
118             byte[] hash = HashData(data, hashAlgorithm);
119             return SignHash(hash);
120         }
121
122         public bool VerifyData(byte[] data, byte[] signature, HashAlgorithmName hashAlgorithm) {
123             if (data == null) {
124                 throw new ArgumentNullException("data");
125             }
126             return VerifyData(data, 0, data.Length, signature, hashAlgorithm);
127         }
128
129         public virtual bool VerifyData(byte[] data, int offset, int count, byte[] signature, HashAlgorithmName hashAlgorithm) {
130             if (data == null) {
131                 throw new ArgumentNullException("data");
132             }
133             if (offset < 0 || offset > data.Length) {
134                 throw new ArgumentOutOfRangeException("offset");
135             }
136             if (count < 0 || count > data.Length - offset) {
137                 throw new ArgumentOutOfRangeException("count");
138             }
139             if (signature == null) {
140                 throw new ArgumentNullException("signature");
141             }
142             if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
143                 throw HashAlgorithmNameNullOrEmpty();
144             }
145
146             byte[] hash = HashData(data, offset, count, hashAlgorithm);
147             return VerifyHash(hash, signature);
148         }
149
150         public bool VerifyData(Stream data, byte[] signature, HashAlgorithmName hashAlgorithm) {
151             if (data == null) {
152                 throw new ArgumentNullException("data");
153             }
154             if (signature == null) {
155                 throw new ArgumentNullException("signature");
156             }
157             if (String.IsNullOrEmpty(hashAlgorithm.Name)) {
158                 throw HashAlgorithmNameNullOrEmpty();
159             }
160
161             byte[] hash = HashData(data, hashAlgorithm);
162             return VerifyHash(hash, signature);
163         }
164
165         private static Exception DerivedClassMustOverride() {
166             return new NotImplementedException(SR.GetString(SR.NotSupported_SubclassOverride));
167         }
168
169         internal static Exception HashAlgorithmNameNullOrEmpty() {
170             return new ArgumentException(SR.GetString(SR.Cryptography_HashAlgorithmNameNullOrEmpty), "hashAlgorithm");
171         }
172     }
173 }