Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ECCng.cs
Go to the documentation of this file.
4
6
7internal static class ECCng
8{
9 internal static CngKey ImportKeyBlob(byte[] ecBlob, string curveName, bool includePrivateParameters)
10 {
11 CngKeyBlobFormat format = (includePrivateParameters ? CngKeyBlobFormat.EccPrivateBlob : CngKeyBlobFormat.EccPublicBlob);
12 CngKey cngKey = CngKey.Import(ecBlob, curveName, format);
13 cngKey.ExportPolicy |= CngExportPolicies.AllowPlaintextExport;
14 return cngKey;
15 }
16
17 internal static CngKey ImportFullKeyBlob(byte[] ecBlob, bool includePrivateParameters)
18 {
19 CngKeyBlobFormat format = (includePrivateParameters ? CngKeyBlobFormat.EccFullPrivateBlob : CngKeyBlobFormat.EccFullPublicBlob);
20 CngKey cngKey = CngKey.Import(ecBlob, format);
21 cngKey.ExportPolicy |= CngExportPolicies.AllowPlaintextExport;
22 return cngKey;
23 }
24
25 internal static byte[] ExportKeyBlob(CngKey key, bool includePrivateParameters)
26 {
27 CngKeyBlobFormat format = (includePrivateParameters ? CngKeyBlobFormat.EccPrivateBlob : CngKeyBlobFormat.EccPublicBlob);
28 return key.Export(format);
29 }
30
31 internal static byte[] ExportFullKeyBlob(CngKey key, bool includePrivateParameters)
32 {
33 CngKeyBlobFormat format = (includePrivateParameters ? CngKeyBlobFormat.EccFullPrivateBlob : CngKeyBlobFormat.EccFullPublicBlob);
34 return key.Export(format);
35 }
36
37 internal static byte[] ExportKeyBlob(CngKey key, bool includePrivateParameters, out CngKeyBlobFormat format, out string curveName)
38 {
39 curveName = key.GetCurveName(out var _);
40 bool flag = false;
41 if (string.IsNullOrEmpty(curveName))
42 {
43 curveName = null;
44 flag = true;
45 format = (includePrivateParameters ? CngKeyBlobFormat.EccFullPrivateBlob : CngKeyBlobFormat.EccFullPublicBlob);
46 }
47 else
48 {
49 format = (includePrivateParameters ? CngKeyBlobFormat.EccPrivateBlob : CngKeyBlobFormat.EccPublicBlob);
50 }
51 byte[] array = key.Export(format);
52 if (flag)
53 {
55 }
56 return array;
57 }
58
59 private unsafe static void FixupGenericBlob(byte[] blob)
60 {
61 if (blob.Length <= sizeof(global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB))
62 {
63 return;
64 }
65 fixed (byte* ptr = blob)
66 {
67 global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB* ptr2 = (global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB*)ptr;
68 switch (ptr2->Magic)
69 {
70 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P256_MAGIC:
71 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P384_MAGIC:
72 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P521_MAGIC:
73 ptr2->Magic = global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_GENERIC_MAGIC;
74 break;
75 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P256_MAGIC:
76 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P384_MAGIC:
77 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P521_MAGIC:
78 ptr2->Magic = global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_GENERIC_MAGIC;
79 break;
80 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P256_MAGIC:
81 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P384_MAGIC:
82 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P521_MAGIC:
83 ptr2->Magic = global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_GENERIC_MAGIC;
84 break;
85 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P256_MAGIC:
86 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P384_MAGIC:
87 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P521_MAGIC:
88 ptr2->Magic = global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_GENERIC_MAGIC;
89 break;
90 }
91 }
92 }
93
94 internal unsafe static byte[] GetNamedCurveBlob(ref ECParameters parameters, bool ecdh)
95 {
96 bool flag = parameters.D != null;
97 int num = sizeof(global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB) + parameters.Q.X.Length + parameters.Q.Y.Length;
98 if (flag)
99 {
100 num += parameters.D.Length;
101 }
102 byte[] array = new byte[num];
103 fixed (byte* ptr = &array[0])
104 {
105 global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB* ptr2 = (global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB*)ptr;
106 ptr2->Magic = (ecdh ? EcdhCurveNameToMagicNumber(parameters.Curve.Oid.FriendlyName, flag) : EcdsaCurveNameToMagicNumber(parameters.Curve.Oid.FriendlyName, flag));
107 ptr2->cbKey = parameters.Q.X.Length;
108 int offset = sizeof(global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB);
109 global::Interop.BCrypt.Emit(array, ref offset, parameters.Q.X);
110 global::Interop.BCrypt.Emit(array, ref offset, parameters.Q.Y);
111 if (flag)
112 {
113 global::Interop.BCrypt.Emit(array, ref offset, parameters.D);
114 }
115 }
116 return array;
117 }
118
119 internal unsafe static byte[] GetPrimeCurveBlob(ref ECParameters parameters, bool ecdh)
120 {
121 bool flag = parameters.D != null;
122 ECCurve curve = parameters.Curve;
123 int num = sizeof(global::Interop.BCrypt.BCRYPT_ECCFULLKEY_BLOB) + curve.Prime.Length + curve.A.Length + curve.B.Length + curve.G.X.Length + curve.G.Y.Length + curve.Order.Length + curve.Cofactor.Length + ((curve.Seed != null) ? curve.Seed.Length : 0) + parameters.Q.X.Length + parameters.Q.Y.Length;
124 if (flag)
125 {
126 num += parameters.D.Length;
127 }
128 byte[] array = new byte[num];
129 fixed (byte* ptr = &array[0])
130 {
131 global::Interop.BCrypt.BCRYPT_ECCFULLKEY_BLOB* ptr2 = (global::Interop.BCrypt.BCRYPT_ECCFULLKEY_BLOB*)ptr;
132 ptr2->Version = 1;
133 ptr2->Magic = ((!flag) ? (ecdh ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_GENERIC_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_GENERIC_MAGIC) : (ecdh ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_GENERIC_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_GENERIC_MAGIC));
134 ptr2->cbCofactor = curve.Cofactor.Length;
135 ptr2->cbFieldLength = parameters.Q.X.Length;
136 ptr2->cbSeed = ((curve.Seed != null) ? curve.Seed.Length : 0);
137 ptr2->cbSubgroupOrder = curve.Order.Length;
138 ptr2->CurveGenerationAlgId = GetHashAlgorithmId(curve.Hash);
139 ptr2->CurveType = ConvertToCurveTypeEnum(curve.CurveType);
140 int offset = sizeof(global::Interop.BCrypt.BCRYPT_ECCFULLKEY_BLOB);
141 global::Interop.BCrypt.Emit(array, ref offset, curve.Prime);
142 global::Interop.BCrypt.Emit(array, ref offset, curve.A);
143 global::Interop.BCrypt.Emit(array, ref offset, curve.B);
144 global::Interop.BCrypt.Emit(array, ref offset, curve.G.X);
145 global::Interop.BCrypt.Emit(array, ref offset, curve.G.Y);
146 global::Interop.BCrypt.Emit(array, ref offset, curve.Order);
147 global::Interop.BCrypt.Emit(array, ref offset, curve.Cofactor);
148 if (curve.Seed != null)
149 {
150 global::Interop.BCrypt.Emit(array, ref offset, curve.Seed);
151 }
152 global::Interop.BCrypt.Emit(array, ref offset, parameters.Q.X);
153 global::Interop.BCrypt.Emit(array, ref offset, parameters.Q.Y);
154 if (flag)
155 {
156 global::Interop.BCrypt.Emit(array, ref offset, parameters.D);
157 }
158 }
159 return array;
160 }
161
162 internal unsafe static void ExportNamedCurveParameters(ref ECParameters ecParams, byte[] ecBlob, bool includePrivateParameters)
163 {
164 global::Interop.BCrypt.KeyBlobMagicNumber magic = (global::Interop.BCrypt.KeyBlobMagicNumber)BitConverter.ToInt32(ecBlob, 0);
165 CheckMagicValueOfKey(magic, includePrivateParameters);
166 if (ecBlob.Length < sizeof(global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB))
167 {
168 throw global::Interop.NCrypt.ErrorCode.E_FAIL.ToCryptographicException();
169 }
170 fixed (byte* ptr = &ecBlob[0])
171 {
172 global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB* ptr2 = (global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB*)ptr;
173 int offset = sizeof(global::Interop.BCrypt.BCRYPT_ECCKEY_BLOB);
174 ecParams.Q = new ECPoint
175 {
176 X = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbKey),
177 Y = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbKey)
178 };
179 if (includePrivateParameters)
180 {
181 ecParams.D = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbKey);
182 }
183 }
184 }
185
186 internal unsafe static void ExportPrimeCurveParameters(ref ECParameters ecParams, byte[] ecBlob, bool includePrivateParameters)
187 {
188 global::Interop.BCrypt.KeyBlobMagicNumber magic = (global::Interop.BCrypt.KeyBlobMagicNumber)BitConverter.ToInt32(ecBlob, 0);
189 CheckMagicValueOfKey(magic, includePrivateParameters);
190 if (ecBlob.Length < sizeof(global::Interop.BCrypt.BCRYPT_ECCFULLKEY_BLOB))
191 {
192 throw global::Interop.NCrypt.ErrorCode.E_FAIL.ToCryptographicException();
193 }
194 fixed (byte* ptr = &ecBlob[0])
195 {
196 global::Interop.BCrypt.BCRYPT_ECCFULLKEY_BLOB* ptr2 = (global::Interop.BCrypt.BCRYPT_ECCFULLKEY_BLOB*)ptr;
197 ECCurve curve = default(ECCurve);
198 curve.CurveType = ConvertToCurveTypeEnum(ptr2->CurveType);
199 curve.Hash = GetHashAlgorithmName(ptr2->CurveGenerationAlgId);
200 int offset = sizeof(global::Interop.BCrypt.BCRYPT_ECCFULLKEY_BLOB);
201 curve.Prime = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbFieldLength);
202 curve.A = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbFieldLength);
203 curve.B = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbFieldLength);
204 curve.G = new ECPoint
205 {
206 X = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbFieldLength),
207 Y = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbFieldLength)
208 };
209 curve.Order = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbSubgroupOrder);
210 curve.Cofactor = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbCofactor);
211 curve.Seed = ((ptr2->cbSeed == 0) ? null : global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbSeed));
212 ecParams.Q = new ECPoint
213 {
214 X = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbFieldLength),
215 Y = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbFieldLength)
216 };
217 if (includePrivateParameters)
218 {
219 ecParams.D = global::Interop.BCrypt.Consume(ecBlob, ref offset, ptr2->cbSubgroupOrder);
220 }
221 ecParams.Curve = curve;
222 }
223 }
224
225 internal unsafe static byte[] GetPrimeCurveParameterBlob(ref ECCurve curve)
226 {
227 int num = sizeof(global::Interop.BCrypt.BCRYPT_ECC_PARAMETER_HEADER) + curve.Prime.Length + curve.A.Length + curve.B.Length + curve.G.X.Length + curve.G.Y.Length + curve.Order.Length + curve.Cofactor.Length + ((curve.Seed != null) ? curve.Seed.Length : 0);
228 byte[] array = new byte[num];
229 fixed (byte* ptr = &array[0])
230 {
231 global::Interop.BCrypt.BCRYPT_ECC_PARAMETER_HEADER* ptr2 = (global::Interop.BCrypt.BCRYPT_ECC_PARAMETER_HEADER*)ptr;
232 ptr2->Version = 1;
233 ptr2->cbCofactor = curve.Cofactor.Length;
234 ptr2->cbFieldLength = curve.A.Length;
235 ptr2->cbSeed = ((curve.Seed != null) ? curve.Seed.Length : 0);
236 ptr2->cbSubgroupOrder = curve.Order.Length;
237 ptr2->CurveGenerationAlgId = GetHashAlgorithmId(curve.Hash);
238 ptr2->CurveType = ConvertToCurveTypeEnum(curve.CurveType);
239 int offset = sizeof(global::Interop.BCrypt.BCRYPT_ECC_PARAMETER_HEADER);
240 global::Interop.BCrypt.Emit(array, ref offset, curve.Prime);
241 global::Interop.BCrypt.Emit(array, ref offset, curve.A);
242 global::Interop.BCrypt.Emit(array, ref offset, curve.B);
243 global::Interop.BCrypt.Emit(array, ref offset, curve.G.X);
244 global::Interop.BCrypt.Emit(array, ref offset, curve.G.Y);
245 global::Interop.BCrypt.Emit(array, ref offset, curve.Order);
246 global::Interop.BCrypt.Emit(array, ref offset, curve.Cofactor);
247 if (curve.Seed != null)
248 {
249 global::Interop.BCrypt.Emit(array, ref offset, curve.Seed);
250 }
251 }
252 return array;
253 }
254
255 private static void CheckMagicValueOfKey(global::Interop.BCrypt.KeyBlobMagicNumber magic, bool includePrivateParameters)
256 {
257 if (includePrivateParameters)
258 {
259 if (!IsMagicValueOfKeyPrivate(magic))
260 {
262 }
263 }
264 else if (!IsMagicValueOfKeyPublic(magic))
265 {
267 }
268 }
269
270 private static bool IsMagicValueOfKeyPrivate(global::Interop.BCrypt.KeyBlobMagicNumber magic)
271 {
272 switch (magic)
273 {
274 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P256_MAGIC:
275 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P256_MAGIC:
276 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P384_MAGIC:
277 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P384_MAGIC:
278 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P521_MAGIC:
279 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P521_MAGIC:
280 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_GENERIC_MAGIC:
281 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_GENERIC_MAGIC:
282 return true;
283 default:
284 return false;
285 }
286 }
287
288 private static bool IsMagicValueOfKeyPublic(global::Interop.BCrypt.KeyBlobMagicNumber magic)
289 {
290 switch (magic)
291 {
292 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P256_MAGIC:
293 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P256_MAGIC:
294 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P384_MAGIC:
295 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P384_MAGIC:
296 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P521_MAGIC:
297 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P521_MAGIC:
298 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_GENERIC_MAGIC:
299 case global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_GENERIC_MAGIC:
300 return true;
301 default:
302 return IsMagicValueOfKeyPrivate(magic);
303 }
304 }
305
306 private static global::Interop.BCrypt.KeyBlobMagicNumber EcdsaCurveNameToMagicNumber(string name, bool includePrivateParameters)
307 {
308 return EcdsaCurveNameToAlgorithm(name) switch
309 {
310 "ECDSA_P256" => includePrivateParameters ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P256_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P256_MAGIC,
311 "ECDSA_P384" => includePrivateParameters ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P384_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P384_MAGIC,
312 "ECDSA_P521" => includePrivateParameters ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_P521_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_P521_MAGIC,
313 _ => includePrivateParameters ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_GENERIC_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDSA_PUBLIC_GENERIC_MAGIC,
314 };
315 }
316
317 private static global::Interop.BCrypt.KeyBlobMagicNumber EcdhCurveNameToMagicNumber(string name, bool includePrivateParameters)
318 {
319 return EcdhCurveNameToAlgorithm(name) switch
320 {
321 "ECDH_P256" => includePrivateParameters ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P256_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P256_MAGIC,
322 "ECDH_P384" => includePrivateParameters ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P384_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P384_MAGIC,
323 "ECDH_P521" => includePrivateParameters ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_P521_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_P521_MAGIC,
324 _ => includePrivateParameters ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PRIVATE_GENERIC_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_ECDH_PUBLIC_GENERIC_MAGIC,
325 };
326 }
327
328 private static global::Interop.BCrypt.ECC_CURVE_TYPE_ENUM ConvertToCurveTypeEnum(ECCurve.ECCurveType value)
329 {
330 return (global::Interop.BCrypt.ECC_CURVE_TYPE_ENUM)value;
331 }
332
333 private static ECCurve.ECCurveType ConvertToCurveTypeEnum(global::Interop.BCrypt.ECC_CURVE_TYPE_ENUM value)
334 {
335 return (ECCurve.ECCurveType)value;
336 }
337
338 internal static SafeNCryptKeyHandle ImportKeyBlob(string blobType, ReadOnlySpan<byte> keyBlob, string curveName, SafeNCryptProviderHandle provider)
339 {
340 global::Interop.NCrypt.ErrorCode errorCode;
343 {
344 global::Interop.BCrypt.BCryptBufferDesc structure = default(global::Interop.BCrypt.BCryptBufferDesc);
345 global::Interop.BCrypt.BCryptBuffer structure2 = default(global::Interop.BCrypt.BCryptBuffer);
346 IntPtr intPtr = IntPtr.Zero;
347 IntPtr intPtr2 = IntPtr.Zero;
348 try
349 {
350 intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(structure));
351 intPtr2 = Marshal.AllocHGlobal(Marshal.SizeOf(structure2));
352 structure2.cbBuffer = (curveName.Length + 1) * 2;
353 structure2.BufferType = global::Interop.BCrypt.CngBufferDescriptors.NCRYPTBUFFER_ECC_CURVE_NAME;
354 structure2.pvBuffer = safeUnicodeStringHandle.DangerousGetHandle();
355 Marshal.StructureToPtr(structure2, intPtr2, fDeleteOld: false);
356 structure.cBuffers = 1;
357 structure.pBuffers = intPtr2;
358 structure.ulVersion = 0;
359 Marshal.StructureToPtr(structure, intPtr, fDeleteOld: false);
360 errorCode = global::Interop.NCrypt.NCryptImportKey(provider, IntPtr.Zero, blobType, intPtr, out phKey, ref MemoryMarshal.GetReference(keyBlob), keyBlob.Length, 0);
361 }
362 finally
363 {
364 Marshal.FreeHGlobal(intPtr);
365 Marshal.FreeHGlobal(intPtr2);
366 }
367 }
368 if (errorCode != 0)
369 {
370 Exception ex = errorCode.ToCryptographicException();
371 if (errorCode == global::Interop.NCrypt.ErrorCode.NTE_INVALID_PARAMETER)
372 {
374 }
375 throw ex;
376 }
377 return phKey;
378 }
379
380 internal static string EcdsaCurveNameToAlgorithm(string algorithm)
381 {
382 switch (algorithm)
383 {
384 case "nistP256":
385 case "ECDSA_P256":
386 return "ECDSA_P256";
387 case "nistP384":
388 case "ECDSA_P384":
389 return "ECDSA_P384";
390 case "nistP521":
391 case "ECDSA_P521":
392 return "ECDSA_P521";
393 default:
394 return "ECDSA";
395 }
396 }
397
398 internal static string EcdhCurveNameToAlgorithm(string algorithm)
399 {
400 switch (algorithm)
401 {
402 case "nistP256":
403 case "ECDH_P256":
404 case "ECDSA_P256":
405 return "ECDH_P256";
406 case "nistP384":
407 case "ECDH_P384":
408 case "ECDSA_P384":
409 return "ECDH_P384";
410 case "nistP521":
411 case "ECDH_P521":
412 case "ECDSA_P521":
413 return "ECDH_P521";
414 default:
415 return "ECDH";
416 }
417 }
418
419 internal static global::Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM GetHashAlgorithmId(HashAlgorithmName? name)
420 {
421 if (!name.HasValue || string.IsNullOrEmpty(name.Value.Name))
422 {
423 return global::Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM.BCRYPT_NO_CURVE_GENERATION_ALG_ID;
424 }
425 global::Interop.Crypt32.CRYPT_OID_INFO cRYPT_OID_INFO = global::Interop.Crypt32.FindOidInfo(global::Interop.Crypt32.CryptOidInfoKeyType.CRYPT_OID_INFO_NAME_KEY, name.Value.Name, OidGroup.HashAlgorithm, fallBackToAllGroups: false);
426 if (cRYPT_OID_INFO.AlgId == -1)
427 {
429 }
430 return (global::Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM)cRYPT_OID_INFO.AlgId;
431 }
432
433 internal static HashAlgorithmName? GetHashAlgorithmName(global::Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM hashId)
434 {
435 global::Interop.Crypt32.CRYPT_OID_INFO cRYPT_OID_INFO = global::Interop.Crypt32.FindAlgIdOidInfo(hashId);
436 if (cRYPT_OID_INFO.AlgId == -1)
437 {
438 return null;
439 }
440 return new HashAlgorithmName(cRYPT_OID_INFO.Name);
441 }
442}
static int ToInt32(byte[] value, int startIndex)
static void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld)
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static int SizeOf(object structure)
Definition Marshal.cs:697
static IntPtr AllocHGlobal(int cb)
Definition Marshal.cs:625
static string Cryptography_NotValidPrivateKey
Definition SR.cs:120
static string Cryptography_UnknownHashAlgorithm
Definition SR.cs:152
static string Cryptography_CurveNotSupported
Definition SR.cs:64
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Cryptography_NotValidPublicOrPrivateKey
Definition SR.cs:122
Definition SR.cs:7
static CngKey Import(ReadOnlySpan< byte > keyBlob, CngKeyBlobFormat format)
Definition CngKey.cs:525
static SafeNCryptKeyHandle ImportKeyBlob(string blobType, ReadOnlySpan< byte > keyBlob, string curveName, SafeNCryptProviderHandle provider)
Definition ECCng.cs:338
static string EcdhCurveNameToAlgorithm(string algorithm)
Definition ECCng.cs:367
static ? HashAlgorithmName GetHashAlgorithmName(global::Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM hashId)
Definition ECCng.cs:23
static global::Interop.BCrypt.KeyBlobMagicNumber EcdsaCurveNameToMagicNumber(string name, bool includePrivateParameters)
Definition ECCng.cs:275
static unsafe byte[] GetPrimeCurveParameterBlob(ref ECCurve curve)
Definition ECCng.cs:225
static unsafe byte[] GetPrimeCurveBlob(ref ECParameters parameters, bool ecdh)
Definition ECCng.cs:119
static CngKey ImportFullKeyBlob(byte[] ecBlob, bool includePrivateParameters)
Definition ECCng.cs:17
static CngKey ImportKeyBlob(byte[] ecBlob, string curveName, bool includePrivateParameters)
Definition ECCng.cs:9
static unsafe void ExportNamedCurveParameters(ref ECParameters ecParams, byte[] ecBlob, bool includePrivateParameters)
Definition ECCng.cs:162
static global::Interop.BCrypt.KeyBlobMagicNumber EcdhCurveNameToMagicNumber(string name, bool includePrivateParameters)
Definition ECCng.cs:286
static byte[] ExportKeyBlob(CngKey key, bool includePrivateParameters)
Definition ECCng.cs:25
static bool IsMagicValueOfKeyPrivate(global::Interop.BCrypt.KeyBlobMagicNumber magic)
Definition ECCng.cs:239
static string EcdsaCurveNameToAlgorithm(string algorithm)
Definition ECCng.cs:349
static ECCurve.ECCurveType ConvertToCurveTypeEnum(global::Interop.BCrypt.ECC_CURVE_TYPE_ENUM value)
Definition ECCng.cs:333
static global::Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM GetHashAlgorithmId(HashAlgorithmName? name)
Definition ECCng.cs:9
static void CheckMagicValueOfKey(global::Interop.BCrypt.KeyBlobMagicNumber magic, bool includePrivateParameters)
Definition ECCng.cs:224
static bool IsMagicValueOfKeyPublic(global::Interop.BCrypt.KeyBlobMagicNumber magic)
Definition ECCng.cs:257
static unsafe void FixupGenericBlob(byte[] blob)
Definition ECCng.cs:59
static unsafe byte[] GetNamedCurveBlob(ref ECParameters parameters, bool ecdh)
Definition ECCng.cs:94
static global::Interop.BCrypt.ECC_CURVE_TYPE_ENUM ConvertToCurveTypeEnum(ECCurve.ECCurveType value)
Definition ECCng.cs:297
static byte[] ExportKeyBlob(CngKey key, bool includePrivateParameters, out CngKeyBlobFormat format, out string curveName)
Definition ECCng.cs:37
static unsafe void ExportPrimeCurveParameters(ref ECParameters ecParams, byte[] ecBlob, bool includePrivateParameters)
Definition ECCng.cs:186
static byte[] ExportFullKeyBlob(CngKey key, bool includePrivateParameters)
Definition ECCng.cs:31
static readonly IntPtr Zero
Definition IntPtr.cs:18