Bug 6967 - Crytography produces unexpected results since the move to CommonCrypto MonoTouch 5.4
Summary: Crytography produces unexpected results since the move to CommonCrypto MonoTo...
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 5.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
: 6985 7096 ()
Depends on:
Reported: 2012-09-06 19:51 UTC by JohanO
Modified: 2012-09-12 11:41 UTC (History)
5 users (show)

Tags: Bad PKCS7 padding. Invalid length 19.
Is this bug a regression?: ---
Last known good build:

A class illustrating the problem. (2.22 KB, application/octet-stream)
2012-09-06 19:51 UTC, JohanO

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 Developer Community or GitHub 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:

Description JohanO 2012-09-06 19:51:32 UTC
Created attachment 2468 [details]
A class illustrating the problem.

Guys I just downloaded MonoTouch today and my application cannot authenticate. I have attached the bits of code that is not working so you can test it for yourself.

Apart from the result encrypted being different from previous versions the decrypt method throws the following error.

Bad PKCS7 padding. Invalid length 19.

Please advise as this is a show stopper for us!
Comment 1 Sebastien Pouliot 2012-09-07 08:34:16 UTC
It seems CC does not (directly) support 128 bits keys, i.e. two-key three-DES. That should be easy to fix (and to workaround in your application).

However I get an CryptographicUnexpectedOperationException, running your sample code (trying to create the encryptor), not a padding error (at decrypt time).

	result = desCrypt.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length);
Comment 2 Sebastien Pouliot 2012-09-07 13:14:36 UTC
The two-keys TripleDES issue is fixed in:
master: d8eed9f1ca2302592592bec5420261f40de3c697
monotouch-5.4-series: 2f7a30d7f0b2b7f453f1b3ff013a8a25deab8455

QA: unit tests added in both branches

note: Keeping open since I cannot duplicate the original exception with the provided code.
Comment 3 JohanO 2012-09-07 15:51:32 UTC
What is the verdict or what should I do... must I downgrade MonoTouch or what is the work-around? Keep in mind that my application uses web-services that runs on windows .Net and therefore expects the byte-array to look the same as if .Net encrypted it.
Comment 4 JohanO 2012-09-07 15:53:29 UTC
I have to mention that this code extract was copied from a class that did not change for 3 years as it's our enterprise authentication security module
Comment 5 Sebastien Pouliot 2012-09-07 16:08:02 UTC
> What is the verdict or what should I do...

The issue I can see, using both your code and mine, is fixed (with complete compatibility).

> Apart from the result encrypted being different from previous versions the

However when I use the code (with MonoTouch 5.4.0) you provided the encrypt part throws an exception (i.e. it does not return a different encrypted result like you mentioned). 

>decrypt method throws the following error.
> Bad PKCS7 padding. Invalid length 19.

and, as it does not return any data, it does not throw a padding exception on decryption.

Either the code you executed is different (than what's attached) or you're running into a different issue. Please check if the attached code and initial descriptions match. 

If they do match then please attach a complete, self-contained, test case (so that every options is already set to show your issue). That way I'll be able to see/duplicate/fix it and tell what you should do.
Comment 6 JohanO 2012-09-07 16:10:40 UTC
The only thing that is different is the key I provided for obvious reasons. Do you have any idea when the fix will be released?
Comment 7 JohanO 2012-09-07 16:13:50 UTC
Another Dev of mine also logged a bug ID 6985.... this is similar to your exception. I don't know why the error I get is different but you know what they say "If it smells like poo and it tastes like poo it's probably poo"
Comment 8 Sebastien Pouliot 2012-09-07 16:17:37 UTC
*** Bug 6985 has been marked as a duplicate of this bug. ***
Comment 9 Sebastien Pouliot 2012-09-07 16:22:40 UTC
A workaround for the issue (the one I see) would be to replace:

	desCrypt.Key = hash;


	byte[] key3 = new byte [24];
	Buffer.BlockCopy (hash, 0, key3, 0, 16);
	Buffer.BlockCopy (hash, 0, key3, 16, 8);
	desCrypt.Key = key3;

Ref: http://en.wikipedia.org/wiki/Triple_DES#Keying_options

That will work now (with 5.4.0) and in the future too. 

OTOH it does not explain how you could get encrypted results.
Comment 10 JohanO 2012-09-07 20:07:46 UTC
Comment 11 Sebastien Pouliot 2012-09-07 20:13:38 UTC
Heh, the first part was easy. The confusing part was your padding exception.
From your joyous comment I assume it disappeared ?

If you still have it then please re-open the bug with a test case. Thanks!
Comment 12 JohanO 2012-09-07 20:16:39 UTC
No it sorted for now and the results are the same as what it was previously.

Will the work-aound work with the "fixed" update or should I then change the code back ?
Comment 13 Sebastien Pouliot 2012-09-07 20:24:25 UTC

There will be no need to remove the fix for future MonoTouch releases. The Mono (and MonoTouch) are just doing the same, but only when a 128 bit key is used. Since you'll be providing a 192 bits key then nothing will be done.
Comment 14 Sebastien Pouliot 2012-09-12 11:41:34 UTC
*** Bug 7096 has been marked as a duplicate of this bug. ***