From 48961ea9678d16dc48e55afa7c735fa32a519175 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Tue, 28 May 2013 20:11:46 -0400 Subject: [PATCH] Refactor the Mono.Security code to use more linker friendly ways to create hash algorithms --- .../AuthenticodeDeformatter.cs | 4 +-- .../Mono.Security.Cryptography/TlsHMAC.cs | 9 ++----- .../Mono.Security.Protocol.Tls/CipherSuite.cs | 30 +++++++++++++++------- .../Mono.Security.Protocol.Tls/SslCipherSuite.cs | 4 +-- .../Mono.Security.Protocol.Tls/SslHandshakeHash.cs | 4 +-- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/mcs/class/Mono.Security/Mono.Security.Authenticode/AuthenticodeDeformatter.cs b/mcs/class/Mono.Security/Mono.Security.Authenticode/AuthenticodeDeformatter.cs index cc74cca20ae..1efcec91abd 100644 --- a/mcs/class/Mono.Security/Mono.Security.Authenticode/AuthenticodeDeformatter.cs +++ b/mcs/class/Mono.Security/Mono.Security.Authenticode/AuthenticodeDeformatter.cs @@ -198,11 +198,11 @@ namespace Mono.Security.Authenticode { HashAlgorithm ha = null; switch (signedHash.Length) { case 16: - ha = HashAlgorithm.Create ("MD5"); + ha = MD5.Create (); hash = GetHash (ha); break; case 20: - ha = HashAlgorithm.Create ("SHA1"); + ha = SHA1.Create (); hash = GetHash (ha); break; default: diff --git a/mcs/class/Mono.Security/Mono.Security.Cryptography/TlsHMAC.cs b/mcs/class/Mono.Security/Mono.Security.Cryptography/TlsHMAC.cs index 722d2e53b4f..1e09d552a21 100644 --- a/mcs/class/Mono.Security/Mono.Security.Cryptography/TlsHMAC.cs +++ b/mcs/class/Mono.Security/Mono.Security.Cryptography/TlsHMAC.cs @@ -115,14 +115,9 @@ namespace Mono.Security.Cryptography this.Initialize(); } - public HMAC(string hashName, byte[] rgbKey) + public HMAC (HashAlgorithm ha, byte[] rgbKey) { - // Create the hash - if (hashName == null || hashName.Length == 0) - { - hashName = "MD5"; - } - hash = HashAlgorithm.Create(hashName); + hash = ha; // Set HashSizeValue HashSizeValue = hash.HashSize; diff --git a/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/CipherSuite.cs b/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/CipherSuite.cs index 52a39e0c25b..6d78fc9d386 100644 --- a/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/CipherSuite.cs +++ b/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/CipherSuite.cs @@ -115,6 +115,18 @@ namespace Mono.Security.Protocol.Tls } } + internal HashAlgorithm CreateHashAlgorithm () + { + switch (hashAlgorithmType) { + case HashAlgorithmType.Md5: + return MD5.Create (); + case HashAlgorithmType.Sha1: + return SHA1.Create (); + default: + return null; + } + } + public HashAlgorithmType HashAlgorithmType { get { return this.hashAlgorithmType; } @@ -389,10 +401,10 @@ namespace Mono.Security.Protocol.Tls Buffer.BlockCopy(secret, (secret.Length - secretLen), secret2, 0, secretLen); // Secret 1 processing - byte[] p_md5 = Expand("MD5", secret1, seed, length); + byte[] p_md5 = Expand (MD5.Create (), secret1, seed, length); // Secret 2 processing - byte[] p_sha = Expand("SHA1", secret2, seed, length); + byte[] p_sha = Expand (SHA1.Create (), secret2, seed, length); // Perfor XOR of both results byte[] masterSecret = new byte[length]; @@ -404,16 +416,16 @@ namespace Mono.Security.Protocol.Tls return masterSecret; } - public byte[] Expand(string hashName, byte[] secret, byte[] seed, int length) + public byte[] Expand (HashAlgorithm hash, byte[] secret, byte[] seed, int length) { - int hashLength = hashName == "MD5" ? 16 : 20; + int hashLength = hash.HashSize / 8; int iterations = (int)(length / hashLength); if ((length % hashLength) > 0) { iterations++; } - M.HMAC hmac = new M.HMAC(hashName, secret); + M.HMAC hmac = new M.HMAC (hash, secret); TlsStream resMacs = new TlsStream(); byte[][] hmacs = new byte[iterations + 1][]; @@ -503,13 +515,13 @@ namespace Mono.Security.Protocol.Tls if (this.context is ClientContext) { this.clientHMAC = new M.HMAC( - this.HashAlgorithmName, + CreateHashAlgorithm (), this.context.Negotiating.ClientWriteMAC); } else { this.serverHMAC = new M.HMAC( - this.HashAlgorithmName, + CreateHashAlgorithm (), this.context.Negotiating.ServerWriteMAC); } } @@ -574,13 +586,13 @@ namespace Mono.Security.Protocol.Tls if (this.context is ClientContext) { this.serverHMAC = new M.HMAC( - this.HashAlgorithmName, + CreateHashAlgorithm (), this.context.Negotiating.ServerWriteMAC); } else { this.clientHMAC = new M.HMAC( - this.HashAlgorithmName, + CreateHashAlgorithm (), this.context.Negotiating.ClientWriteMAC); } } diff --git a/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslCipherSuite.cs b/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslCipherSuite.cs index 14540520be2..ae9b9d56c19 100644 --- a/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslCipherSuite.cs +++ b/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslCipherSuite.cs @@ -74,7 +74,7 @@ namespace Mono.Security.Protocol.Tls public override byte[] ComputeServerRecordMAC(ContentType contentType, byte[] fragment) { - HashAlgorithm hash = HashAlgorithm.Create(this.HashAlgorithmName); + HashAlgorithm hash = CreateHashAlgorithm (); byte[] smac = this.Context.Read.ServerWriteMAC; hash.TransformBlock (smac, 0, smac.Length, smac, 0); @@ -107,7 +107,7 @@ namespace Mono.Security.Protocol.Tls public override byte[] ComputeClientRecordMAC(ContentType contentType, byte[] fragment) { - HashAlgorithm hash = HashAlgorithm.Create(this.HashAlgorithmName); + HashAlgorithm hash = CreateHashAlgorithm (); byte[] cmac = this.Context.Current.ClientWriteMAC; hash.TransformBlock (cmac, 0, cmac.Length, cmac, 0); diff --git a/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslHandshakeHash.cs b/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslHandshakeHash.cs index fba03390993..2a670b40d51 100644 --- a/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslHandshakeHash.cs +++ b/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslHandshakeHash.cs @@ -47,8 +47,8 @@ namespace Mono.Security.Protocol.Tls public SslHandshakeHash(byte[] secret) { // Create md5 and sha1 hashes - this.md5 = HashAlgorithm.Create("MD5"); - this.sha = HashAlgorithm.Create("SHA1"); + this.md5 = MD5.Create (); + this.sha = SHA1.Create (); // Set HashSizeValue this.HashSizeValue = md5.HashSize + sha.HashSize; -- 2.11.4.GIT