Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CngAlgorithmCore.cs
Go to the documentation of this file.
1using System;
4
6
7internal struct CngAlgorithmCore
8{
9 private readonly string _disposedName;
10
12
14
15 private bool _disposed;
16
17 public CngAlgorithmCore(string disposedName)
18 {
19 this = default(CngAlgorithmCore);
20 _disposedName = disposedName;
21 }
22
23 public static CngKey Duplicate(CngKey key)
24 {
25 using SafeNCryptKeyHandle keyHandle = key.Handle;
26 return CngKey.Open(keyHandle, key.IsEphemeral ? CngKeyHandleOpenOptions.EphemeralKey : CngKeyHandleOpenOptions.None);
27 }
28
30 {
32 if (_lazyKey != null)
33 {
34 return _lazyKey.IsECNamedCurve();
35 }
36 return false;
37 }
38
39 public void DisposeKey()
40 {
41 if (_lazyKey != null)
42 {
44 _lazyKey = null;
45 }
46 }
47
48 public CngKey GetOrGenerateKey(int keySize, CngAlgorithm algorithm)
49 {
51 if (_lazyKey != null && _lazyKey.KeySize != keySize)
52 {
53 DisposeKey();
54 }
55 if (_lazyKey == null)
56 {
57 CngKeyCreationParameters cngKeyCreationParameters = new CngKeyCreationParameters
58 {
59 ExportPolicy = CngExportPolicies.AllowPlaintextExport
60 };
62 cngKeyCreationParameters.Parameters.Add(item);
63 _lazyKey = CngKey.Create(algorithm, null, cngKeyCreationParameters);
64 }
65 return _lazyKey;
66 }
67
69 {
71 if (_lazyKey != null)
72 {
73 return _lazyKey;
74 }
75 CngKeyCreationParameters cngKeyCreationParameters = new CngKeyCreationParameters
76 {
77 ExportPolicy = CngExportPolicies.AllowPlaintextExport
78 };
79 if (curve.Value.IsNamed)
80 {
81 cngKeyCreationParameters.Parameters.Add(CngKey.GetPropertyFromNamedCurve(curve.Value));
82 }
83 else
84 {
85 if (!curve.Value.IsPrime)
86 {
88 }
89 ECCurve curve2 = curve.Value;
90 byte[] primeCurveParameterBlob = System.Security.Cryptography.ECCng.GetPrimeCurveParameterBlob(ref curve2);
91 CngProperty item = new CngProperty("ECCParameters", primeCurveParameterBlob, CngPropertyOptions.None);
92 cngKeyCreationParameters.Parameters.Add(item);
93 }
94 try
95 {
96 _lazyKey = CngKey.Create(DefaultKeyType ?? CngAlgorithm.ECDsa, null, cngKeyCreationParameters);
97 }
98 catch (CryptographicException ex)
99 {
100 global::Interop.NCrypt.ErrorCode hResult = (global::Interop.NCrypt.ErrorCode)ex.HResult;
101 if ((curve.Value.IsNamed && hResult == global::Interop.NCrypt.ErrorCode.NTE_INVALID_PARAMETER) || hResult == global::Interop.NCrypt.ErrorCode.NTE_NOT_SUPPORTED)
102 {
104 }
105 throw;
106 }
107 return _lazyKey;
108 }
109
110 public void SetKey(CngKey key)
111 {
113 DisposeKey();
114 _lazyKey = key;
115 }
116
117 public void Dispose()
118 {
119 DisposeKey();
120 _disposed = true;
121 }
122
123 internal void ThrowIfDisposed()
124 {
125 if (_disposed)
126 {
128 }
129 }
130}
static byte[] GetBytes(bool value)
static string Cryptography_CurveNotSupported
Definition SR.cs:64
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
static CngKey Create(CngAlgorithm algorithm)
Definition CngKey.cs:278
static CngProperty GetPropertyFromNamedCurve(ECCurve curve)
Definition CngKey.cs:481
static CngKey Open(string keyName)
Definition CngKey.cs:799
static unsafe byte[] GetPrimeCurveParameterBlob(ref ECCurve curve)
Definition ECCng.cs:194
CngKey GetOrGenerateKey(int keySize, CngAlgorithm algorithm)