public bool VerifyMac(ReadOnlySpan <char> password) { if (IntegrityMode != Pkcs12IntegrityMode.Password) { throw new InvalidOperationException( SR.Format( SR.Cryptography_Pkcs12_WrongModeForVerify, Pkcs12IntegrityMode.Password, IntegrityMode)); } Debug.Assert(_decoded.MacData.HasValue); HashAlgorithmName hashAlgorithm; int expectedOutputSize; string algorithmValue = _decoded.MacData.Value.Mac.DigestAlgorithm.Algorithm.Value; switch (algorithmValue) { case Oids.Md5: expectedOutputSize = 128 >> 3; hashAlgorithm = HashAlgorithmName.MD5; break; case Oids.Sha1: expectedOutputSize = 160 >> 3; hashAlgorithm = HashAlgorithmName.SHA1; break; case Oids.Sha256: expectedOutputSize = 256 >> 3; hashAlgorithm = HashAlgorithmName.SHA256; break; case Oids.Sha384: expectedOutputSize = 384 >> 3; hashAlgorithm = HashAlgorithmName.SHA384; break; case Oids.Sha512: expectedOutputSize = 512 >> 3; hashAlgorithm = HashAlgorithmName.SHA512; break; default: throw new CryptographicException( SR.Format(SR.Cryptography_UnknownHashAlgorithm, algorithmValue)); } if (_decoded.MacData.Value.Mac.Digest.Length != expectedOutputSize) { throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding); } // Cannot use the ArrayPool or stackalloc here because CreateHMAC needs a properly bounded array. byte[] derived = new byte[expectedOutputSize]; int iterationCount = PasswordBasedEncryption.NormalizeIterationCount(_decoded.MacData.Value.IterationCount); Pkcs12Kdf.DeriveMacKey( password, hashAlgorithm, iterationCount, _decoded.MacData.Value.MacSalt.Span, derived); using (IncrementalHash hmac = IncrementalHash.CreateHMAC(hashAlgorithm, derived)) { hmac.AppendData(_authSafeContents.Span); if (!hmac.TryGetHashAndReset(derived, out int bytesWritten) || bytesWritten != expectedOutputSize) { Debug.Fail($"TryGetHashAndReset wrote {bytesWritten} bytes when {expectedOutputSize} was expected"); throw new CryptographicException(); } return(CryptographicOperations.FixedTimeEquals( derived, _decoded.MacData.Value.Mac.Digest.Span)); } }
public void InitalizeCipherSuite() { byte[] clientMAC; byte[] serverMAC; byte[] clientEncryptionKey; byte[] serverEncryptionKey; GenerateKeys(out clientMAC, out serverMAC, out clientEncryptionKey, out serverEncryptionKey); if (SecurityParameters.BulkCipherAlgorithm == BulkCipherAlgorithm.AES) { m_decryptionBulkAlgorithm = Aes.Create(); //new AesCryptoServiceProvider //{ m_decryptionBulkAlgorithm.Padding = PaddingMode.None; m_decryptionBulkAlgorithm.KeySize = SecurityParameters.EncKeyLength * 8; m_decryptionBulkAlgorithm.BlockSize = SecurityParameters.BlockLength * 8; //}; m_encryptionBulkAlgorithm = Aes.Create(); //new AesCryptoServiceProvider //{ m_encryptionBulkAlgorithm.Padding = PaddingMode.None; m_encryptionBulkAlgorithm.KeySize = SecurityParameters.EncKeyLength * 8; m_encryptionBulkAlgorithm.BlockSize = SecurityParameters.BlockLength * 8; //}; if (SecurityParameters.Entity == ConnectionEnd.Client) { m_encryptionBulkAlgorithm.Key = clientEncryptionKey; m_decryptionBulkAlgorithm.Key = serverEncryptionKey; } else { m_decryptionBulkAlgorithm.Key = clientEncryptionKey; m_encryptionBulkAlgorithm.Key = serverEncryptionKey; } } else { m_decryptionBulkAlgorithm = m_encryptionBulkAlgorithm = null; } if (SecurityParameters.MACAlgorithm == MACAlgorithm.HMACSha1) { if (SecurityParameters.Entity == ConnectionEnd.Client) { m_encryptionHMAC = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA1, clientMAC); //new HMACSHA1(clientMAC); m_decryptionHMAC = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA1, serverMAC); //new HMACSHA1(serverMAC); } else { m_encryptionHMAC = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA1, serverMAC); //new HMACSHA1(serverMAC); m_decryptionHMAC = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA1, clientMAC); //new HMACSHA1(clientMAC); } } else if (SecurityParameters.MACAlgorithm == MACAlgorithm.HMACSha256) { if (SecurityParameters.Entity == ConnectionEnd.Client) { m_encryptionHMAC = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA256, clientMAC); //ew HMACSHA256(clientMAC); m_decryptionHMAC = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA256, serverMAC); //new HMACSHA256(serverMAC); } else { m_encryptionHMAC = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA256, serverMAC); // new HMACSHA256(serverMAC); m_decryptionHMAC = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA256, clientMAC); // new HMACSHA256(clientMAC); } } else { m_encryptionHMAC = m_decryptionHMAC = null; } }