Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ECDiffieHellman.cs
Go to the documentation of this file.
5
7
8[UnsupportedOSPlatform("browser")]
9public abstract class ECDiffieHellman : AsymmetricAlgorithm
10{
11 private static readonly string[] s_validOids = new string[1] { "1.2.840.10045.2.1" };
12
13 public override string KeyExchangeAlgorithm => "ECDiffieHellman";
14
15 public override string? SignatureAlgorithm => null;
16
17 public abstract ECDiffieHellmanPublicKey PublicKey { get; }
18
19 [RequiresUnreferencedCode("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
20 public new static ECDiffieHellman? Create(string algorithm)
21 {
22 if (algorithm == null)
23 {
24 throw new ArgumentNullException("algorithm");
25 }
26 return CryptoConfig.CreateFromName(algorithm) as ECDiffieHellman;
27 }
28
29 public virtual byte[] DeriveKeyMaterial(ECDiffieHellmanPublicKey otherPartyPublicKey)
30 {
32 }
33
34 public byte[] DeriveKeyFromHash(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm)
35 {
36 return DeriveKeyFromHash(otherPartyPublicKey, hashAlgorithm, null, null);
37 }
38
39 public virtual byte[] DeriveKeyFromHash(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, byte[]? secretPrepend, byte[]? secretAppend)
40 {
42 }
43
44 public byte[] DeriveKeyFromHmac(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, byte[]? hmacKey)
45 {
46 return DeriveKeyFromHmac(otherPartyPublicKey, hashAlgorithm, hmacKey, null, null);
47 }
48
49 public virtual byte[] DeriveKeyFromHmac(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, byte[]? hmacKey, byte[]? secretPrepend, byte[]? secretAppend)
50 {
52 }
53
54 public virtual byte[] DeriveKeyTls(ECDiffieHellmanPublicKey otherPartyPublicKey, byte[] prfLabel, byte[] prfSeed)
55 {
57 }
58
63
64 public virtual ECParameters ExportParameters(bool includePrivateParameters)
65 {
67 }
68
69 public virtual ECParameters ExportExplicitParameters(bool includePrivateParameters)
70 {
72 }
73
74 public virtual void ImportParameters(ECParameters parameters)
75 {
77 }
78
79 public virtual void GenerateKey(ECCurve curve)
80 {
82 }
83
84 public unsafe override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> passwordBytes, PbeParameters pbeParameters, Span<byte> destination, out int bytesWritten)
85 {
86 if (pbeParameters == null)
87 {
88 throw new ArgumentNullException("pbeParameters");
89 }
91 ECParameters ecParameters = ExportParameters(includePrivateParameters: true);
92 fixed (byte* ptr = ecParameters.D)
93 {
94 try
95 {
96 AsnWriter pkcs8Writer = EccKeyFormatHelper.WritePkcs8PrivateKey(ecParameters);
97 AsnWriter asnWriter = KeyFormatHelper.WriteEncryptedPkcs8(passwordBytes, pkcs8Writer, pbeParameters);
98 return asnWriter.TryEncode(destination, out bytesWritten);
99 }
100 finally
101 {
103 }
104 }
105 }
106
107 public unsafe override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, PbeParameters pbeParameters, Span<byte> destination, out int bytesWritten)
108 {
109 if (pbeParameters == null)
110 {
111 throw new ArgumentNullException("pbeParameters");
112 }
114 ECParameters ecParameters = ExportParameters(includePrivateParameters: true);
115 fixed (byte* ptr = ecParameters.D)
116 {
117 try
118 {
119 AsnWriter pkcs8Writer = EccKeyFormatHelper.WritePkcs8PrivateKey(ecParameters);
120 AsnWriter asnWriter = KeyFormatHelper.WriteEncryptedPkcs8(password, pkcs8Writer, pbeParameters);
121 return asnWriter.TryEncode(destination, out bytesWritten);
122 }
123 finally
124 {
126 }
127 }
128 }
129
130 public unsafe override bool TryExportPkcs8PrivateKey(Span<byte> destination, out int bytesWritten)
131 {
132 ECParameters ecParameters = ExportParameters(includePrivateParameters: true);
133 fixed (byte* ptr = ecParameters.D)
134 {
135 try
136 {
137 AsnWriter asnWriter = EccKeyFormatHelper.WritePkcs8PrivateKey(ecParameters);
138 return asnWriter.TryEncode(destination, out bytesWritten);
139 }
140 finally
141 {
143 }
144 }
145 }
146
147 public override bool TryExportSubjectPublicKeyInfo(Span<byte> destination, out int bytesWritten)
148 {
149 ECParameters ecParameters = ExportParameters(includePrivateParameters: false);
151 return asnWriter.TryEncode(destination, out bytesWritten);
152 }
153
154 public unsafe override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<byte> passwordBytes, ReadOnlySpan<byte> source, out int bytesRead)
155 {
156 KeyFormatHelper.ReadEncryptedPkcs8(s_validOids, source, passwordBytes, (KeyFormatHelper.KeyReader<ECParameters>)EccKeyFormatHelper.FromECPrivateKey, out int bytesRead2, out ECParameters ret);
157 fixed (byte* ptr = ret.D)
158 {
159 try
160 {
161 ImportParameters(ret);
162 bytesRead = bytesRead2;
163 }
164 finally
165 {
167 }
168 }
169 }
170
171 public unsafe override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<char> password, ReadOnlySpan<byte> source, out int bytesRead)
172 {
173 KeyFormatHelper.ReadEncryptedPkcs8(s_validOids, source, password, (KeyFormatHelper.KeyReader<ECParameters>)EccKeyFormatHelper.FromECPrivateKey, out int bytesRead2, out ECParameters ret);
174 fixed (byte* ptr = ret.D)
175 {
176 try
177 {
178 ImportParameters(ret);
179 bytesRead = bytesRead2;
180 }
181 finally
182 {
184 }
185 }
186 }
187
188 public unsafe override void ImportPkcs8PrivateKey(ReadOnlySpan<byte> source, out int bytesRead)
189 {
191 fixed (byte* ptr = ret.D)
192 {
193 try
194 {
195 ImportParameters(ret);
196 bytesRead = bytesRead2;
197 }
198 finally
199 {
201 }
202 }
203 }
204
205 public override void ImportSubjectPublicKeyInfo(ReadOnlySpan<byte> source, out int bytesRead)
206 {
208 ImportParameters(ret);
209 bytesRead = bytesRead2;
210 }
211
212 public unsafe virtual void ImportECPrivateKey(ReadOnlySpan<byte> source, out int bytesRead)
213 {
214 int bytesRead2;
215 ECParameters parameters = EccKeyFormatHelper.FromECPrivateKey(source, out bytesRead2);
216 fixed (byte* ptr = parameters.D)
217 {
218 try
219 {
220 ImportParameters(parameters);
221 bytesRead = bytesRead2;
222 }
223 finally
224 {
226 }
227 }
228 }
229
230 public unsafe virtual byte[] ExportECPrivateKey()
231 {
232 ECParameters ecParameters = ExportParameters(includePrivateParameters: true);
233 fixed (byte* ptr = ecParameters.D)
234 {
235 try
236 {
237 AsnWriter asnWriter = EccKeyFormatHelper.WriteECPrivateKey(in ecParameters);
238 return asnWriter.Encode();
239 }
240 finally
241 {
243 }
244 }
245 }
246
247 public unsafe virtual bool TryExportECPrivateKey(Span<byte> destination, out int bytesWritten)
248 {
249 ECParameters ecParameters = ExportParameters(includePrivateParameters: true);
250 fixed (byte* ptr = ecParameters.D)
251 {
252 try
253 {
254 AsnWriter asnWriter = EccKeyFormatHelper.WriteECPrivateKey(in ecParameters);
255 return asnWriter.TryEncode(destination, out bytesWritten);
256 }
257 finally
258 {
260 }
261 }
262 }
263
265 {
267 {
268 if (label.SequenceEqual("PRIVATE KEY"))
269 {
270 return ImportPkcs8PrivateKey;
271 }
272 if (label.SequenceEqual("PUBLIC KEY"))
273 {
274 return ImportSubjectPublicKeyInfo;
275 }
276 return label.SequenceEqual("EC PRIVATE KEY") ? new PemKeyImportHelpers.ImportKeyAction(ImportECPrivateKey) : null;
277 });
278 }
279
281 {
282 PemKeyImportHelpers.ImportEncryptedPem(input, password, ImportEncryptedPkcs8PrivateKey);
283 }
284
286 {
287 PemKeyImportHelpers.ImportEncryptedPem(input, passwordBytes, ImportEncryptedPkcs8PrivateKey);
288 }
289
290 public override void FromXmlString(string xmlString)
291 {
293 }
294
295 public override string ToXmlString(bool includePrivateParameters)
296 {
298 }
299
300 public new static ECDiffieHellman Create()
301 {
303 }
304
305 public static ECDiffieHellman Create(ECCurve curve)
306 {
308 }
309
310 public static ECDiffieHellman Create(ECParameters parameters)
311 {
313 try
314 {
315 eCDiffieHellman.ImportParameters(parameters);
316 return eCDiffieHellman;
317 }
318 catch
319 {
320 eCDiffieHellman.Dispose();
321 throw;
322 }
323 }
324}
static void ImportPem(ReadOnlySpan< char > input, FindImportActionFunc callback)
delegate void ImportKeyAction(ReadOnlySpan< byte > source, out int bytesRead)
bool TryEncode(Span< byte > destination, out int bytesWritten)
Definition AsnWriter.cs:173
int Encode(Span< byte > destination)
Definition AsnWriter.cs:195
static string Cryptography_ECXmlSerializationFormatRequired
Definition SR.cs:54
static string NotSupported_SubclassOverride
Definition SR.cs:1714
Definition SR.cs:7
static ? object CreateFromName(string name, params object?[]? args)
unsafe override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
override void ImportFromEncryptedPem(ReadOnlySpan< char > input, ReadOnlySpan< byte > passwordBytes)
virtual unsafe bool TryExportECPrivateKey(Span< byte > destination, out int bytesWritten)
virtual byte[] DeriveKeyMaterial(ECDiffieHellmanPublicKey otherPartyPublicKey)
unsafe override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
override void FromXmlString(string xmlString)
unsafe override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
unsafe override bool TryExportPkcs8PrivateKey(Span< byte > destination, out int bytesWritten)
virtual byte[] DeriveKeyTls(ECDiffieHellmanPublicKey otherPartyPublicKey, byte[] prfLabel, byte[] prfSeed)
byte[] DeriveKeyFromHash(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm)
virtual byte[] DeriveKeyFromHash(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, byte[]? secretPrepend, byte[]? secretAppend)
static ECDiffieHellman Create(ECCurve curve)
virtual unsafe void ImportECPrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
virtual ECParameters ExportExplicitParameters(bool includePrivateParameters)
unsafe override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, ReadOnlySpan< byte > source, out int bytesRead)
virtual void ImportParameters(ECParameters parameters)
byte[] DeriveKeyFromHmac(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, byte[]? hmacKey)
override bool TryExportSubjectPublicKeyInfo(Span< byte > destination, out int bytesWritten)
virtual ECParameters ExportParameters(bool includePrivateParameters)
static ECDiffieHellman Create(ECParameters parameters)
unsafe override void ImportPkcs8PrivateKey(ReadOnlySpan< byte > source, out int bytesRead)
override string ToXmlString(bool includePrivateParameters)
virtual byte[] DeriveKeyFromHmac(ECDiffieHellmanPublicKey otherPartyPublicKey, HashAlgorithmName hashAlgorithm, byte[]? hmacKey, byte[]? secretPrepend, byte[]? secretAppend)
override void ImportSubjectPublicKeyInfo(ReadOnlySpan< byte > source, out int bytesRead)
override void ImportFromEncryptedPem(ReadOnlySpan< char > input, ReadOnlySpan< char > password)
static new? ECDiffieHellman Create(string algorithm)
override void ImportFromPem(ReadOnlySpan< char > input)
static void FromECPublicKey(ReadOnlyMemory< byte > key, in AlgorithmIdentifierAsn algId, out ECParameters ret)
static AsnWriter WriteSubjectPublicKeyInfo(ECParameters ecParameters)
static AsnWriter WriteECPrivateKey(in ECParameters ecParameters)
static unsafe ECParameters FromECPrivateKey(ReadOnlySpan< byte > key, out int bytesRead)
static AsnWriter WritePkcs8PrivateKey(ECParameters ecParameters, AttributeAsn[] attributes=null)
static ReadOnlyMemory< byte > ReadSubjectPublicKeyInfo(string[] validOids, ReadOnlyMemory< byte > source, out int bytesRead)
static ReadOnlyMemory< byte > ReadPkcs8(string[] validOids, ReadOnlyMemory< byte > source, out int bytesRead)
static AsnWriter WriteEncryptedPkcs8(ReadOnlySpan< char > password, AsnWriter pkcs8Writer, PbeParameters pbeParameters)
static void ValidatePbeParameters(PbeParameters pbeParameters, ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes)