Bug 12493 - SymmetricAlgorithm processing block incorrectly
Summary: SymmetricAlgorithm processing block incorrectly
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: PC Mac OS
: High normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks:
 
Reported: 2013-05-31 12:52 UTC by Rodrigo Kumpera
Modified: 2013-05-31 15:46 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Rodrigo Kumpera 2013-05-31 12:52:46 UTC
The following test was extracted from the System.Web test suite and shows a problem with what looks like to be crypto related (might be something else, but I can't dig further into that code).

using System;
using System.Reflection;
using System.IO;
using System.Web.Configuration;
using System.Web.Util;


class Driver {

	static byte ChangeByte (byte b)
	{
		return (b == Byte.MaxValue) ? Byte.MinValue : (byte) (b + 1);
	}
	
	
	static void AreEqual (byte[] a, byte[] b, string zz) {
		if (a.Length != b.Length) {
			Console.WriteLine ("ON {0} EXPECTED len '{1}' but got: len '{2}'", zz, a.Length, b.Length);
			throw new Exception ();
		}

		for (int i = 0; i < a.Length; ++i) {
			if (a [i] != b[i]) {
				Console.WriteLine ("ON {0} pos {1} EXPECTED '{2}' but got: len '{3}'", zz, i, a [i], b [i]);
				throw new Exception ();				
			}
		}
	}

	static void IsNull (object a, string zz) {
		if (a == null)
			return;

		Console.WriteLine ("ON {0} EXPECTED NULL BUT GOT '{1}'", zz, a);
		throw new Exception ();
	}


	public static void Encrypt_RoundTrip (MachineKeySection section)
	{
		byte [] data = new byte [14];
		byte [] encdata = encrypt (section, data);
		byte [] decdata = decrypt (section, encdata);
		AreEqual (data, decdata, "roundtrip");

		// changing length (missing first byte)
		byte [] cut = new byte [encdata.Length - 1];
		Array.Copy (encdata, 1, cut, 0, cut.Length);
		IsNull (decrypt (section, cut), "bad length");

		// changing last byte (padding)
		byte be = encdata [encdata.Length - 1];
		encdata [encdata.Length - 1] = ChangeByte (be);
		var res = decrypt (section, encdata);
	
		IsNull (res, "bad padding");
	}

	static Func<MachineKeySection, byte[], byte[]> encrypt, decrypt;

	static void Main () 
	{
		var mm = typeof (MachineKeySection).Assembly.GetType ("System.Web.Util.MachineKeySectionUtils");
		encrypt = (Func<MachineKeySection, byte[], byte[]>)Delegate.CreateDelegate (typeof (Func<MachineKeySection, byte[], byte[]>), mm.GetMethod ("Encrypt", new Type[] {typeof (MachineKeySection), typeof (byte[]) }));//, BindingFlags.Public | BindingFlags.Static));
		decrypt = (Func<MachineKeySection, byte[], byte[]>)Delegate.CreateDelegate (typeof (Func<MachineKeySection, byte[], byte[]>), mm.GetMethod ("Decrypt", new Type[] {typeof (MachineKeySection), typeof (byte[]) }));//, BindingFlags.Public | BindingFlags.Static));
		
		
		for (int i = 0; i < 1000; ++i) {
			Encrypt_RoundTrip (new MachineKeySection ());
		}
	}
}
Comment 1 Rodrigo Kumpera 2013-05-31 12:53:33 UTC
Sebastien, since you're our crypto wizard, could you take a look on this bug?
Comment 2 Rodrigo Kumpera 2013-05-31 12:57:18 UTC
I get his here when running with latest mono:

ON bad padding EXPECTED NULL BUT GOT 'System.Byte[]'

Unhandled Exception:
System.Exception: Exception of type 'System.Exception' was thrown.
  at Driver.IsNull (System.Object a, System.String zz) [0x00000] in <filename unknown>:0 
  at Driver.Encrypt_RoundTrip (System.Web.Configuration.MachineKeySection section) [0x00000] in <filename unknown>:0 
  at Driver.Main () [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.Exception: Exception of type 'System.Exception' was thrown.
  at Driver.IsNull (System.Object a, System.String zz) [0x00000] in <filename unknown>:0 
  at Driver.Encrypt_RoundTrip (System.Web.Configuration.MachineKeySection section) [0x00000] in <filename unknown>:0 
  at Driver.Main () [0x00000] in <filename unknown>:0
Comment 3 Sebastien Pouliot 2013-05-31 14:07:11 UTC
Not a BCL bug, a test bug.

If there's no .config (with keys) then new ones are randomly generated (that's fine for the BCL). E.g.

	var mks = new MachineKeySection ();
	Console.WriteLine (mks.DecryptionKey);

gives:

	AutoGenerate,IsolateApps

That means that there's a possibility that decrypting bad data won't give you an invalid padding, just invalid data (GIGO, each byte only has 256 possible values). IOW it's normal for the above test case to fail some time (as it depends on random values).

In general we hardcode keys (to check for such defects). It won't be enough here since the IV is also random (and not accessible by the API). I'll adapt the test not to depend on random values for this part.
Comment 4 Sebastien Pouliot 2013-05-31 15:46:38 UTC
API was not very testing friendly but I added a 2nd check so it should not report this, randomly, on the bots unless something is really broken.

fa6625a8e7fb459d79cb7de57c9938d5447aaad7