Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ECCngKey.cs
Go to the documentation of this file.
2
4
5internal sealed class ECCngKey
6{
8
9 private int _lastKeySize;
10
11 private string _lastAlgorithm;
12
13 private bool _disposed;
14
15 private readonly string _algorithmGroup;
16
17 private readonly string _disposedName;
18
19 internal int KeySize { get; private set; }
20
21 internal ECCngKey(string algorithmGroup, string disposedName)
22 {
23 _algorithmGroup = algorithmGroup;
24 _disposedName = disposedName;
25 }
26
27 internal string GetCurveName(int callerKeySizeProperty, out string oidValue)
28 {
29 using SafeNCryptKeyHandle ncryptHandle = GetDuplicatedKeyHandle(callerKeySizeProperty);
30 string lastAlgorithm = _lastAlgorithm;
31 if (ECCng.IsECNamedCurve(lastAlgorithm))
32 {
33 oidValue = null;
34 return CngKeyLite.GetCurveName(ncryptHandle);
35 }
36 return ECCng.SpecialNistAlgorithmToCurveName(lastAlgorithm, out oidValue);
37 }
38
39 internal SafeNCryptKeyHandle GetDuplicatedKeyHandle(int callerKeySizeProperty)
40 {
43 {
45 }
46 if (_lastKeySize != callerKeySizeProperty)
47 {
48 bool flag = _algorithmGroup == "ECDSA";
49 string text = callerKeySizeProperty switch
50 {
51 256 => flag ? "ECDSA_P256" : "ECDH_P256",
52 384 => flag ? "ECDSA_P384" : "ECDH_P384",
53 521 => flag ? "ECDSA_P521" : "ECDH_P521",
55 };
56 if (_keyHandle != null)
57 {
58 DisposeKey();
59 }
60 _keyHandle = CngKeyLite.GenerateNewExportableKey(text, callerKeySizeProperty);
61 _lastKeySize = callerKeySizeProperty;
63 KeySize = callerKeySizeProperty;
64 }
66 }
67
68 internal void GenerateKey(ECCurve curve)
69 {
70 curve.Validate();
72 if (_keyHandle != null)
73 {
74 DisposeKey();
75 }
76 int num = 0;
77 string text;
78 if (curve.IsNamed)
79 {
80 if (string.IsNullOrEmpty(curve.Oid.FriendlyName))
81 {
83 }
86 {
87 try
88 {
91 }
92 catch (CryptographicException ex)
93 {
94 global::Interop.NCrypt.ErrorCode hResult = (global::Interop.NCrypt.ErrorCode)ex.HResult;
95 if ((curve.IsNamed && hResult == global::Interop.NCrypt.ErrorCode.NTE_INVALID_PARAMETER) || hResult == global::Interop.NCrypt.ErrorCode.NTE_NOT_SUPPORTED)
96 {
98 }
99 throw;
100 }
101 }
102 else
103 {
104 switch (text)
105 {
106 case "ECDSA_P256":
107 case "ECDH_P256":
108 num = 256;
109 break;
110 case "ECDSA_P384":
111 case "ECDH_P384":
112 num = 384;
113 break;
114 case "ECDSA_P521":
115 case "ECDH_P521":
116 num = 521;
117 break;
118 default:
120 }
122 }
123 }
124 else
125 {
126 if (!curve.IsExplicit)
127 {
129 }
133 }
135 _lastKeySize = num;
136 KeySize = num;
137 }
138
139 internal void FullDispose()
140 {
141 DisposeKey();
142 _disposed = true;
143 }
144
145 internal void DisposeKey()
146 {
147 if (_keyHandle != null)
148 {
150 _keyHandle = null;
151 }
152 _lastAlgorithm = null;
153 _lastKeySize = 0;
154 }
155
156 internal void SetHandle(SafeNCryptKeyHandle keyHandle, string algorithmName)
157 {
160 _keyHandle = keyHandle;
161 _lastAlgorithm = algorithmName;
162 KeySize = CngKeyLite.GetKeyLength(keyHandle);
164 }
165
166 internal void ThrowIfDisposed()
167 {
168 if (_disposed)
169 {
171 }
172 }
173}
static string Cryptography_CurveNotSupported
Definition SR.cs:64
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Cryptography_InvalidCurveOid
Definition SR.cs:66
static string Cryptography_InvalidKeySize
Definition SR.cs:92
Definition SR.cs:7
static int GetKeyLength(SafeNCryptKeyHandle keyHandle)
static string GetCurveName(SafeNCryptHandle ncryptHandle)
static SafeNCryptKeyHandle GenerateNewExportableKey(string algorithm, int keySize)
void GenerateKey(ECCurve curve)
Definition ECCngKey.cs:68
ECCngKey(string algorithmGroup, string disposedName)
Definition ECCngKey.cs:21
string GetCurveName(int callerKeySizeProperty, out string oidValue)
Definition ECCngKey.cs:27
void SetHandle(SafeNCryptKeyHandle keyHandle, string algorithmName)
Definition ECCngKey.cs:156
SafeNCryptKeyHandle GetDuplicatedKeyHandle(int callerKeySizeProperty)
Definition ECCngKey.cs:39
SafeNCryptKeyHandle _keyHandle
Definition ECCngKey.cs:7
static string EcdhCurveNameToAlgorithm(string algorithm)
Definition ECCng.cs:367
static string EcdsaCurveNameToAlgorithm(string algorithm)
Definition ECCng.cs:349
static bool IsECNamedCurve(string algorithm)
Definition ECCng.cs:33
static string SpecialNistAlgorithmToCurveName(string algorithm, out string oidValue)
Definition ECCng.cs:42