57 protected override void Dispose(
bool disposing)
135 return version.Major > 6 || (version.Major == 6 && version.Minor >= 2);
140 if (parameters.
P ==
null || parameters.
Q ==
null || parameters.
G ==
null || parameters.
Y ==
null)
144 if (parameters.
J !=
null && parameters.
J.Length >= parameters.
P.Length)
148 bool flag = parameters.X !=
null;
149 int num = parameters.
P.Length;
151 if (parameters.
G.Length != num || parameters.
Y.Length != num)
155 if (flag && parameters.
X.Length != parameters.
Q.Length)
174 base.ImportEncryptedPkcs8PrivateKey(passwordBytes,
source, out bytesRead);
180 base.ImportEncryptedPkcs8PrivateKey(password,
source, out bytesRead);
185 if (pbeParameters ==
null)
194 if (pbeParameters ==
null)
208 if (pbeParameters ==
null)
218 if (pbeParameters ==
null)
232 int num =
sizeof(global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB) + cbKey + cbKey + cbKey;
237 blob =
new byte[num];
238 fixed (
byte* ptr = &blob[0])
240 global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB* ptr2 = (global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB*)ptr;
241 ptr2->Magic = (includePrivate ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PRIVATE_MAGIC : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PUBLIC_MAGIC);
244 if (parameters.
Seed !=
null)
246 if (parameters.
Seed.Length != 20)
250 global::Interop.BCrypt.EmitBigEndian(blob, ref
offset, parameters.
Counter);
251 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
Seed);
255 global::Interop.BCrypt.EmitByte(blob, ref
offset,
byte.MaxValue, 24);
257 if (parameters.
Q.Length != 20)
261 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
Q);
262 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
P);
263 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
G);
264 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
Y);
267 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
X);
274 int num =
sizeof(global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2) + ((parameters.
Seed ==
null) ? parameters.Q.Length : parameters.
Seed.Length) + parameters.
Q.Length + parameters.
P.Length + parameters.
G.Length + parameters.
Y.Length + (includePrivateParameters ? parameters.
X.Length : 0);
275 blob =
new byte[num];
276 fixed (
byte* ptr = &blob[0])
278 global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2* ptr2 = (global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2*)ptr;
279 ptr2->Magic = (includePrivateParameters ? global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PRIVATE_MAGIC_V2 : global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PUBLIC_MAGIC_V2);
281 ptr2->hashAlgorithm = parameters.Q.Length
switch
283 20 => global::Interop.BCrypt.HASHALGORITHM_ENUM.DSA_HASH_ALGORITHM_SHA1,
284 32 => global::Interop.BCrypt.HASHALGORITHM_ENUM.DSA_HASH_ALGORITHM_SHA256,
285 64 => global::Interop.BCrypt.HASHALGORITHM_ENUM.DSA_HASH_ALGORITHM_SHA512,
288 ptr2->standardVersion = global::Interop.BCrypt.DSAFIPSVERSION_ENUM.DSA_FIPS186_3;
289 int offset =
sizeof(global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2) - 4;
290 if (parameters.
Seed !=
null)
292 global::Interop.BCrypt.EmitBigEndian(blob, ref
offset, parameters.
Counter);
293 ptr2->cbSeedLength = parameters.
Seed.Length;
294 ptr2->cbGroupSize = parameters.
Q.Length;
295 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
Seed);
299 global::Interop.BCrypt.EmitByte(blob, ref
offset,
byte.MaxValue, 4);
300 int count = (ptr2->cbSeedLength = parameters.
Q.Length);
301 ptr2->cbGroupSize = parameters.
Q.Length;
302 global::Interop.BCrypt.EmitByte(blob, ref
offset,
byte.MaxValue,
count);
304 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
Q);
305 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
P);
306 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
G);
307 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
Y);
308 if (includePrivateParameters)
310 global::Interop.BCrypt.Emit(blob, ref
offset, parameters.
X);
318 global::Interop.BCrypt.KeyBlobMagicNumber keyBlobMagicNumber = (global::Interop.BCrypt.KeyBlobMagicNumber)
BitConverter.
ToInt32(
array, 0);
321 fixed (
byte* ptr =
array)
323 if (keyBlobMagicNumber == global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PUBLIC_MAGIC || keyBlobMagicNumber == global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PRIVATE_MAGIC)
325 if (
array.Length <
sizeof(global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB))
327 throw global::Interop.NCrypt.ErrorCode.E_FAIL.ToCryptographicException();
329 global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB* ptr2 = (global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB*)ptr;
332 result.Seed = global::Interop.BCrypt.Consume(
array, ref
offset, 20);
333 result.Q = global::Interop.BCrypt.Consume(
array, ref
offset, 20);
334 result.P = global::Interop.BCrypt.Consume(
array, ref
offset, ptr2->cbKey);
335 result.G = global::Interop.BCrypt.Consume(
array, ref
offset, ptr2->cbKey);
336 result.Y = global::Interop.BCrypt.Consume(
array, ref
offset, ptr2->cbKey);
337 if (includePrivateParameters)
339 result.X = global::Interop.BCrypt.Consume(
array, ref
offset, 20);
344 if (
array.Length <
sizeof(global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2))
346 throw global::Interop.NCrypt.ErrorCode.E_FAIL.ToCryptographicException();
348 global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2* ptr3 = (global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2*)ptr;
349 int offset =
sizeof(global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2) - 4;
351 result.Seed = global::Interop.BCrypt.Consume(
array, ref
offset, ptr3->cbSeedLength);
352 result.Q = global::Interop.BCrypt.Consume(
array, ref
offset, ptr3->cbGroupSize);
353 result.P = global::Interop.BCrypt.Consume(
array, ref
offset, ptr3->cbKey);
354 result.G = global::Interop.BCrypt.Consume(
array, ref
offset, ptr3->cbKey);
355 result.Y = global::Interop.BCrypt.Consume(
array, ref
offset, ptr3->cbKey);
356 if (includePrivateParameters)
358 result.X = global::Interop.BCrypt.Consume(
array, ref
offset, ptr3->cbGroupSize);
370 private static void CheckMagicValueOfKey(global::Interop.BCrypt.KeyBlobMagicNumber magic,
bool includePrivateParameters)
372 if (includePrivateParameters)
374 if (magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PRIVATE_MAGIC && magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PRIVATE_MAGIC_V2)
379 else if (magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PUBLIC_MAGIC && magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PUBLIC_MAGIC_V2)
394 return keyHandle.SignHash(hash, global::Interop.NCrypt.AsymmetricPaddingMode.None,
null, hash.
Length * 2);
403 if (!keyHandle.TrySignHash(hash2,
destination, global::Interop.NCrypt.AsymmetricPaddingMode.None,
null, out bytesWritten))
418 if (rgbSignature ==
null)
430 return keyHandle.VerifyHash(hash2, signature, global::Interop.NCrypt.AsymmetricPaddingMode.None,
null);
442 return hash.
Slice(0, num);
444 int num2 = num - hash.
Length;
445 stackBuf.
Slice(0, num2).Clear();
447 return stackBuf.
Slice(0, num);
457 if (
array.Length <
sizeof(global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2))
461 fixed (
byte* ptr =
array)
463 global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2* ptr2 = (global::Interop.BCrypt.BCRYPT_DSA_KEY_BLOB_V2*)ptr;
464 if (ptr2->Magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PUBLIC_MAGIC_V2 && ptr2->Magic != global::Interop.BCrypt.KeyBlobMagicNumber.BCRYPT_DSA_PRIVATE_MAGIC_V2)
468 return ptr2->cbGroupSize;
static byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
static bool TryHashData(ReadOnlySpan< byte > source, Span< byte > destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
static int ToInt32(byte[] value, int startIndex)
static int ReadInt32BigEndian(ReadOnlySpan< byte > source)
static OperatingSystem OSVersion
static string Cryptography_InvalidDsaParameters_MismatchedPJ
static string Cryptography_NotValidPrivateKey
static string Cryptography_InvalidDsaParameters_QRestriction_ShortKey
static string Cryptography_InvalidDsaParameters_QRestriction_LargeKey
static string Cryptography_ArgDSARequiresDSAKey
static string Cryptography_NotValidPublicOrPrivateKey
static string Cryptography_InvalidDsaParameters_MismatchedQX
static string Cryptography_InvalidDsaParameters_MismatchedPGY
static string Cryptography_InvalidDsaParameters_MissingFields
static string Cryptography_InvalidDsaParameters_SeedRestriction_ShortKey
KeySizes[] LegalKeySizesValue
static CngAlgorithmGroup Dsa
bool TryExportKeyBlob(string blobType, Span< byte > destination, out int bytesWritten)
byte[] Export(CngKeyBlobFormat format)
byte[] ExportPkcs8KeyBlob(ReadOnlySpan< char > password, int kdfCount)
static CngKey Import(ReadOnlySpan< byte > keyBlob, CngKeyBlobFormat format)
bool TryExportPkcs8KeyBlob(ReadOnlySpan< char > password, int kdfCount, Span< byte > destination, out int bytesWritten)
SafeNCryptKeyHandle Handle
static bool IsPlatformScheme(PbeParameters pbeParameters)
static byte[] ExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters)
static bool TryExportEncryptedPkcs8PrivateKey(AsymmetricAlgorithm key, ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
static bool Supports2048KeySize()
override? string KeyExchangeAlgorithm
static readonly KeySizes[] s_legalKeySizes
override byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters)
unsafe override byte[] CreateSignature(byte[] rgbHash)
static unsafe void GenerateV2DsaBlob(out byte[] blob, DSAParameters parameters, int cbKey, bool includePrivateParameters)
static readonly int s_defaultKeySize
override void Dispose(bool disposing)
override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
bool TryExportEncryptedPkcs8(ReadOnlySpan< char > pkcs8Password, int kdfCount, Span< byte > destination, out int bytesWritten)
override byte[] ExportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, PbeParameters pbeParameters)
override KeySizes[] LegalKeySizes
override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > source, out int bytesRead)
override void ImportParameters(DSAParameters parameters)
static void CheckMagicValueOfKey(global::Interop.BCrypt.KeyBlobMagicNumber magic, bool includePrivateParameters)
override string SignatureAlgorithm
ReadOnlySpan< byte > AdjustHashSizeIfNecessary(ReadOnlySpan< byte > hash, Span< byte > stackBuf)
override bool TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, PbeParameters pbeParameters, Span< byte > destination, out int bytesWritten)
unsafe override bool TryCreateSignature(ReadOnlySpan< byte > hash, Span< byte > destination, out int bytesWritten)
void ForceSetKeySize(int newKeySize)
override bool VerifySignature(byte[] rgbHash, byte[] rgbSignature)
readonly CngAlgorithm _dsnCng
SafeNCryptKeyHandle GetDuplicatedKeyHandle()
byte[] ExportEncryptedPkcs8(ReadOnlySpan< char > pkcs8Password, int kdfCount)
void ImportKeyBlob(byte[] dsaBlob, bool includePrivate)
unsafe override DSAParameters ExportParameters(bool includePrivateParameters)
override void ImportEncryptedPkcs8PrivateKey(ReadOnlySpan< char > password, ReadOnlySpan< byte > source, out int bytesRead)
unsafe override bool VerifySignature(ReadOnlySpan< byte > hash, ReadOnlySpan< byte > signature)
override bool TryHashData(ReadOnlySpan< byte > source, Span< byte > destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
byte[] ExportKeyBlob(bool includePrivateParameters)
override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
unsafe int ComputeQLength()
override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
static unsafe void GenerateV1DsaBlob(out byte[] blob, DSAParameters parameters, int cbKey, bool includePrivate)
override bool TryExportPkcs8PrivateKey(Span< byte > destination, out int bytesWritten)
static void ValidatePbeParameters(PbeParameters pbeParameters, ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes)
CngKey GetOrGenerateKey(int keySize, CngAlgorithm algorithm)
static CngKey Duplicate(CngKey key)
void CopyTo(Span< T > destination)
ReadOnlySpan< T > Slice(int start)
Span< T > Slice(int start)