LIB_MCS_FLAGS = -unsafe -nowarn:1030,3009
TEST_MCS_FLAGS = $(LIB_MCS_FLAGS) -nowarn:169,219,618,672
+CC_PROFILE := $(filter monotouch% xammac, $(PROFILE))
+ifdef CC_PROFILE
+
+BUILT_SOURCES = \
+ ../corlib/CommonCrypto/SHA224Managed.g.cs \
+ ../corlib/CommonCrypto/MD2Managed.g.cs \
+ ../corlib/CommonCrypto/MD4Managed.g.cs
+
+CommonCrypto/%.g.cs:
+ $(MAKE) -C ../corlib/CommonCrypto
+
+endif
+
include ../../build/library.make
$(build_lib): $(the_libdir_base)bare/System.dll
#include mobile_Mono.Security.dll.sources
../corlib/CommonCrypto/CommonCrypto.cs
../corlib/CommonCrypto/RC4CommonCrypto.cs
-../corlib/CommonCrypto/MD2Managed.g.cs
-../corlib/CommonCrypto/MD4Managed.g.cs
-../corlib/CommonCrypto/SHA224Managed.g.cs
#include monotouch_Mono.Security.dll.sources
../corlib/CommonCrypto/CommonCrypto.cs
../corlib/CommonCrypto/RC4CommonCrypto.cs
-../corlib/CommonCrypto/MD2Managed.g.cs
-../corlib/CommonCrypto/MD4Managed.g.cs
-../corlib/CommonCrypto/SHA224Managed.g.cs
+++ /dev/null
-//
-// CommonCrypto code generator for symmetric block cipher algorithms
-//
-// Authors:
-// Sebastien Pouliot <sebastien@xamarin.com>
-//
-// Copyright 2012 Xamarin Inc.
-//
-
-using System;
-using System.IO;
-
-namespace Xamarin {
-
- public static class CommonCryptor {
-
- static public void Generate (string namespaceName, string typeName, string baseTypeName, string ccAlgorithmName, string feedbackSize = "8", string ctorInitializers = null)
- {
- string template = @"// Generated file to bind CommonCrypto cipher algorithms - DO NOT EDIT
-//
-// Authors:
-// Sebastien Pouliot <sebastien@xamarin.com>
-//
-// Copyright 2012-2014 Xamarin Inc.
-
-using System;
-using System.Security.Cryptography;
-
-using Mono.Security.Cryptography;
-using Crimson.CommonCrypto;
-
-namespace %NAMESPACE% {
-
- public sealed partial class %TYPE% : %BASE% {
-
- public %TYPE% ()
- {
- FeedbackSizeValue = %FEEDBACKSIZE%;
- %CTOR_INIT%
- }
-
- public override void GenerateIV ()
- {
- IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
- }
-
- public override void GenerateKey ()
- {
- KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
- }
-
- public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV)
- {
- IntPtr decryptor = IntPtr.Zero;
- switch (Mode) {
- case CipherMode.CBC:
- decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.None, rgbKey, rgbIV);
- return new FastCryptorTransform (decryptor, this, false, rgbIV);
- case CipherMode.ECB:
- decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
- return new FastCryptorTransform (decryptor, this, false, rgbIV);
- case CipherMode.CFB:
-#if MONOTOUCH || XAMMAC
- IntPtr encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
- decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
- return new CryptorTransform (decryptor, encryptor, this, false, rgbIV);
-#else
- throw new CryptographicException (""CFB is not supported by Crimson.CommonCrypto"");
-#endif
- default:
- throw new CryptographicException (String.Format (""{0} is not supported by the .NET framework"", Mode));
- }
- }
-
- public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV)
- {
- IntPtr encryptor = IntPtr.Zero;
- switch (Mode) {
- case CipherMode.CBC:
- encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.None, rgbKey, rgbIV);
- return new FastCryptorTransform (encryptor, this, true, rgbIV);
- case CipherMode.ECB:
- encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
- return new FastCryptorTransform (encryptor, this, true, rgbIV);
- case CipherMode.CFB:
-#if MONOTOUCH || XAMMAC
- encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
- return new CryptorTransform (encryptor, IntPtr.Zero, this, true, rgbIV);
-#else
- throw new CryptographicException (""CFB is not supported by Crimson.CommonCrypto"");
-#endif
- default:
- throw new CryptographicException (String.Format (""{0} is not supported by the .NET framework"", Mode));
- }
- }
- }
-}";
-
- File.WriteAllText (typeName + ".g.cs", template.Replace ("%NAMESPACE%", namespaceName).
- Replace ("%TYPE%", typeName).Replace ("%BASE%", baseTypeName).Replace("%FEEDBACKSIZE%", feedbackSize).Replace ("%CTOR_INIT%", ctorInitializers).
- Replace ("%CCALGORITHM%", ccAlgorithmName.ToString ()));
- }
- }
-}
+++ /dev/null
-//
-// CommonCrypto code generator for digest algorithms
-//
-// Authors:
-// Sebastien Pouliot <sebastien@xamarin.com>
-//
-// Copyright 2012-2014 Xamarin Inc.
-//
-
-using System;
-using System.IO;
-
-namespace Xamarin {
-
- public static class CommonDigest {
-
-#if !MONOTOUCH && !XAMMAC
- // we do not add anything in MonoTouch, just replacing, so this is not needed
- // however we can avoid a dependency on Mono.Security for Crimson.CommonCrypto.dll by including the base classes
- static public void GenerateBaseClass (string namespaceName, string typeName, string baseTypeName, int hashSize,
- string visibilityStart = "", string visibilityEnd = "")
- {
- string template = @"// Generated file to bind CommonCrypto/CommonDigest - DO NOT EDIT
-//
-// Authors:
-// Sebastien Pouliot <sebastien@xamarin.com>
-//
-// Copyright 2012-2014 Xamarin Inc.
-
-using System;
-using System.Security.Cryptography;
-using System.Runtime.InteropServices;
-
-namespace %NAMESPACE% {
-
-%VISIBILITY_START%
- public
-%VISIBILITY_END%
- abstract class %BASE% : HashAlgorithm {
-
- protected %BASE% ()
- {
- HashSizeValue = %HASHSIZE%;
- }
-
- public static new %BASE% Create ()
- {
- return Create (""%BASE%"");
- }
-
- public static new %BASE% Create (string hashName)
- {
- object o = CryptoConfig.CreateFromName (hashName);
- return (%BASE%) o ?? new %TYPE% ();
- }
- }
-}";
-
- File.WriteAllText (baseTypeName + ".g.cs", template.Replace ("%NAMESPACE%", namespaceName).
- Replace ("%TYPE%", typeName).Replace ("%BASE%", baseTypeName).
- Replace ("%VISIBILITY_START%", visibilityStart).Replace ("%VISIBILITY_END%", visibilityEnd).
- Replace ("%HASHSIZE%", hashSize.ToString ()));
- }
-#endif
- static public void Generate (string namespaceName, string typeName, string baseTypeName, int contextSize,
- string visibilityStart = "", string visibilityEnd = "")
- {
- string template = @"// Generated file to bind CommonCrypto/CommonDigest - DO NOT EDIT
-//
-// Authors:
-// Sebastien Pouliot <sebastien@xamarin.com>
-//
-// Copyright 2011-2014 Xamarin Inc.
-
-using System;
-using System.Security.Cryptography;
-using System.Runtime.InteropServices;
-
-using Mono.Security.Cryptography;
-
-namespace %NAMESPACE% {
-
-%VISIBILITY_START%
- public
-%VISIBILITY_END%
- sealed class %TYPE% : %BASE% {
-
- IntPtr ctx;
-
- [DllImport (""/usr/lib/libSystem.dylib"", EntryPoint=""CC_%BASE%_Init"")]
- static extern int Init (/* CC_%BASE%_CTX */ IntPtr c);
-
- [DllImport (""/usr/lib/libSystem.dylib"", EntryPoint=""CC_%BASE%_Update"")]
- static extern int Update (/* CC_%BASE%_CTX */ IntPtr c, /* const void * */ IntPtr data, /* uint32_t */ uint len);
-
- [DllImport (""/usr/lib/libSystem.dylib"", EntryPoint=""CC_%BASE%_Final"")]
- static extern int Final (/* unsigned char * */ byte [] md, /* CC_%BASE%_CTX */ IntPtr c);
-
- public %TYPE% ()
- {
- ctx = IntPtr.Zero;
- }
-
- ~%TYPE% ()
- {
- Dispose (false);
- }
-
- protected override void Dispose (bool disposing)
- {
- if (ctx != IntPtr.Zero) {
- Marshal.FreeHGlobal (ctx);
- ctx = IntPtr.Zero;
- }
- base.Dispose (disposing);
- GC.SuppressFinalize (this);
- }
-
- public override void Initialize ()
- {
- if (ctx == IntPtr.Zero)
- ctx = Marshal.AllocHGlobal (%CTX_SIZE%);
-
- int hr = Init (ctx);
- if (hr != 1)
- throw new CryptographicException (hr);
- }
-
- protected override void HashCore (byte[] data, int start, int length)
- {
- if (ctx == IntPtr.Zero)
- Initialize ();
-
- if (data.Length == 0)
- return;
-
- unsafe {
- fixed (byte* p = &data [0]) {
- int hr = Update (ctx, (IntPtr) (p + start), (uint) length);
- if (hr != 1)
- throw new CryptographicException (hr);
- }
- }
- }
-
- protected override byte[] HashFinal ()
- {
- if (ctx == IntPtr.Zero)
- Initialize ();
-
- byte[] data = new byte [HashSize >> 3];
- int hr = Final (data, ctx);
- if (hr != 1)
- throw new CryptographicException (hr);
-
- return data;
- }
- }
-}";
-
- File.WriteAllText (typeName + ".g.cs", template.Replace ("%NAMESPACE%", namespaceName).
- Replace ("%TYPE%", typeName).Replace ("%BASE%", baseTypeName).
- Replace ("%VISIBILITY_START%", visibilityStart).Replace ("%VISIBILITY_END%", visibilityEnd).
- Replace ("%CTX_SIZE%", contextSize.ToString ()));
- }
- }
-}
--- /dev/null
+thisdir = class/corlib/CommonCrypto
+SUBDIRS =
+include ../../../build/rules.make
+
+.NOTPARALLEL:
+all:
+ MONO_PATH="$(topdir)/class/lib/$(BUILD_TOOLS_PROFILE)$(PLATFORM_PATH_SEPARATOR)$$MONO_PATH" $(RUNTIME) $(RUNTIME_FLAGS) $(topdir)/class/lib/$(BUILD_TOOLS_PROFILE)/commoncryptogenerator.exe
+
+++ /dev/null
-CommonCrypto/%.g.cs: CommonCrypto/generator.exe
- cd CommonCrypto && mono --debug generator.exe
-
-CommonCrypto/generator.exe: CommonCrypto/generator.cs CommonCrypto/CommonDigestGenerator.cs CommonCrypto/CommonCryptorGenerator.cs
- mcs CommonCrypto/generator.cs CommonCrypto/CommonDigestGenerator.cs CommonCrypto/CommonCryptorGenerator.cs -o $@
+++ /dev/null
-//
-// CommonCrypto Code Generator
-//
-// Authors:
-// Sebastien Pouliot <sebastien@xamarin.com>
-//
-// Copyright 2012 Xamarin Inc.
-//
-
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Text;
-
-namespace Xamarin {
-
- class Program {
- static void Main (string [] args)
- {
- // mscorlib replacements
- CommonDigest.Generate ("System.Security.Cryptography", "MD5CryptoServiceProvider", "MD5", 1000);
- CommonDigest.Generate ("System.Security.Cryptography", "SHA1CryptoServiceProvider", "SHA1", 1000);
- CommonDigest.Generate ("System.Security.Cryptography", "SHA1Managed", "SHA1", 1000);
- CommonDigest.Generate ("System.Security.Cryptography", "SHA256Managed", "SHA256", 1000);
- CommonDigest.Generate ("System.Security.Cryptography", "SHA384Managed", "SHA384", 1000);
- CommonDigest.Generate ("System.Security.Cryptography", "SHA512Managed", "SHA512", 1000);
-
- // System.Core replacements - not yet in MT profile (4.0 - functional dupes anyway)
- //CommonDigest.Generate ("System.Security.Cryptography", "MD5Cng", "MD5", 1000);
- //CommonDigest.Generate ("System.Security.Cryptography", "SHA256Cng", "SHA256", 1000);
- //CommonDigest.Generate ("System.Security.Cryptography", "SHA384Cng", "SHA384", 1000);
- //CommonDigest.Generate ("System.Security.Cryptography", "SHA512Cng", "SHA512", 1000);
- //CommonDigest.Generate ("System.Security.Cryptography", "SHA256CryptoServiceProvider", "SHA256", 1000);
- //CommonDigest.Generate ("System.Security.Cryptography", "SHA384CryptoServiceProvider", "SHA384", 1000);
- //CommonDigest.Generate ("System.Security.Cryptography", "SHA512CryptoServiceProvider", "SHA512", 1000);
-
- // Mono.Security replacements
- CommonDigest.Generate ("Mono.Security.Cryptography", "MD2Managed", "MD2", 1000, "#if !INSIDE_CORLIB", "#endif");
- CommonDigest.Generate ("Mono.Security.Cryptography", "MD4Managed", "MD4", 1000, "#if !INSIDE_CORLIB", "#endif");
- CommonDigest.Generate ("Mono.Security.Cryptography", "SHA224Managed", "SHA224", 1000);
-
- // mscorlib replacements
- CommonCryptor.Generate ("System.Security.Cryptography", "DESCryptoServiceProvider", "DES", "DES");
- CommonCryptor.Generate ("System.Security.Cryptography", "TripleDESCryptoServiceProvider", "TripleDES", "TripleDES");
- CommonCryptor.Generate ("System.Security.Cryptography", "RC2CryptoServiceProvider", "RC2", "RC2", ctorInitializers: "LegalKeySizesValue = new[] { new KeySizes(40, 128, 8) };");
- // Rijndael supports block sizes that are not available in AES - as such it does not use the same generated code
- // but has it's own version, using AES (128 bits block size) and falling back to managed (192/256 bits block size)
-
- // System.Core replacements
- CommonCryptor.Generate ("System.Security.Cryptography", "AesManaged", "Aes", "AES128", "128");
- CommonCryptor.Generate ("System.Security.Cryptography", "AesCryptoServiceProvider", "Aes", "AES128");
-
- // Mono.Security replacements
- // RC4 is a stream (not a block) cipher so it can not reuse the generated code
- }
- }
-}
\ No newline at end of file
thisdir = class/corlib
SUBDIRS =
include ../../build/rules.make
-include CommonCrypto/Makefile.include
export __SECURITY_BOOTSTRAP_DB=$(topdir)/class/corlib
LIBRARY = corlib.dll
TEST_MCS_FLAGS += -debug -nowarn:168,219,618,672 -unsafe \
-define:MONO_DATACONVERTER_STATIC_METHODS $(TEST_RESX_RESOURCES:%=-resource:%)
+
+CC_PROFILE := $(filter monotouch% xammac, $(PROFILE))
+ifdef CC_PROFILE
+
+BUILT_SOURCES = \
+ CommonCrypto/AesCryptoServiceProvider.g.cs \
+ CommonCrypto/MD5CryptoServiceProvider.g.cs \
+ CommonCrypto/SHA256Managed.g.cs \
+ CommonCrypto/AesManaged.g.cs \
+ CommonCrypto/RC2CryptoServiceProvider.g.cs \
+ CommonCrypto/SHA384Managed.g.cs \
+ CommonCrypto/DESCryptoServiceProvider.g.cs \
+ CommonCrypto/SHA1CryptoServiceProvider.g.cs \
+ CommonCrypto/SHA512Managed.g.cs \
+ CommonCrypto/MD2Managed.g.cs \
+ CommonCrypto/SHA1Managed.g.cs \
+ CommonCrypto/TripleDESCryptoServiceProvider.g.cs \
+ CommonCrypto/MD4Managed.g.cs
+
+CommonCrypto/%.g.cs:
+ $(MAKE) -C CommonCrypto
+
+endif
+
EXTRA_DISTFILES = \
Test/resources/MyResources.resources \
Test/resources/MyResources.de.resources \
CommonCrypto/CryptorTransform.cs
CommonCrypto/FastCryptorTransform.cs
CommonCrypto/CorlibExtras.cs
-CommonCrypto/MD5CryptoServiceProvider.g.cs
-CommonCrypto/SHA1CryptoServiceProvider.g.cs
-CommonCrypto/SHA1CryptoServiceProvider.g.cs
-CommonCrypto/SHA1Managed.g.cs
-CommonCrypto/SHA256Managed.g.cs
-CommonCrypto/SHA384Managed.g.cs
-CommonCrypto/SHA512Managed.g.cs
-CommonCrypto/TripleDESCryptoServiceProvider.g.cs
-CommonCrypto/DESCryptoServiceProvider.g.cs
-CommonCrypto/RC2CryptoServiceProvider.g.cs
CommonCrypto/RijndaelManaged.cs
CommonCrypto/SecRandom.cs
CommonCrypto/RC4CommonCrypto.cs
-CommonCrypto/MD2Managed.g.cs
-CommonCrypto/MD4Managed.g.cs
System/Environment.iOS.cs
System/Guid.MonoTouch.cs
System/NotSupportedException.iOS.cs
CommonCrypto/CryptorTransform.cs
CommonCrypto/FastCryptorTransform.cs
CommonCrypto/CorlibExtras.cs
-CommonCrypto/MD5CryptoServiceProvider.g.cs
-CommonCrypto/SHA1CryptoServiceProvider.g.cs
-CommonCrypto/SHA1CryptoServiceProvider.g.cs
-CommonCrypto/SHA1Managed.g.cs
-CommonCrypto/SHA256Managed.g.cs
-CommonCrypto/SHA384Managed.g.cs
-CommonCrypto/SHA512Managed.g.cs
-CommonCrypto/TripleDESCryptoServiceProvider.g.cs
-CommonCrypto/DESCryptoServiceProvider.g.cs
-CommonCrypto/RC2CryptoServiceProvider.g.cs
CommonCrypto/RijndaelManaged.cs
CommonCrypto/SecRandom.cs
CommonCrypto/RC4CommonCrypto.cs
-CommonCrypto/MD2Managed.g.cs
-CommonCrypto/MD4Managed.g.cs
System/Environment.iOS.cs
System/Guid.MonoTouch.cs
System/NotSupportedException.iOS.cs
CommonCrypto/CryptorTransform.cs
CommonCrypto/FastCryptorTransform.cs
CommonCrypto/CorlibExtras.cs
-CommonCrypto/MD5CryptoServiceProvider.g.cs
-CommonCrypto/SHA1CryptoServiceProvider.g.cs
-CommonCrypto/SHA1CryptoServiceProvider.g.cs
-CommonCrypto/SHA1Managed.g.cs
-CommonCrypto/SHA256Managed.g.cs
-CommonCrypto/SHA384Managed.g.cs
-CommonCrypto/SHA512Managed.g.cs
-CommonCrypto/TripleDESCryptoServiceProvider.g.cs
-CommonCrypto/DESCryptoServiceProvider.g.cs
-CommonCrypto/RC2CryptoServiceProvider.g.cs
CommonCrypto/RijndaelManaged.cs
CommonCrypto/SecRandom.cs
CommonCrypto/RC4CommonCrypto.cs
-CommonCrypto/MD2Managed.g.cs
-CommonCrypto/MD4Managed.g.cs
System/Environment.iOS.cs
System/Guid.MonoTouch.cs
System/NotSupportedException.iOS.cs
CommonCrypto/CryptorTransform.cs
CommonCrypto/FastCryptorTransform.cs
CommonCrypto/CorlibExtras.cs
-CommonCrypto/MD5CryptoServiceProvider.g.cs
-CommonCrypto/SHA1CryptoServiceProvider.g.cs
-CommonCrypto/SHA1CryptoServiceProvider.g.cs
-CommonCrypto/SHA1Managed.g.cs
-CommonCrypto/SHA256Managed.g.cs
-CommonCrypto/SHA384Managed.g.cs
-CommonCrypto/SHA512Managed.g.cs
-CommonCrypto/TripleDESCryptoServiceProvider.g.cs
-CommonCrypto/DESCryptoServiceProvider.g.cs
-CommonCrypto/RC2CryptoServiceProvider.g.cs
CommonCrypto/RijndaelManaged.cs
CommonCrypto/RC4CommonCrypto.cs
-CommonCrypto/MD2Managed.g.cs
-CommonCrypto/MD4Managed.g.cs
CoreFoundation/CFHelpers.cs
System.Security.Cryptography.X509Certificates/X509CertificateImplApple.cs
System.Security.Cryptography.X509Certificates/X509Helper.Apple.cs
mono-symbolicate \
linker-analyzer
-build_SUBDIRS = gacutil security culevel cil-stringreplacer
+build_SUBDIRS = gacutil security culevel cil-stringreplacer commoncryptogenerator
net_4_5_SUBDIRS = gacutil
net_4_5_PARALLEL_SUBDIRS = $(net_4_5_dirs)
--- /dev/null
+//
+// CommonCrypto code generator for symmetric block cipher algorithms
+//
+// Authors:
+// Sebastien Pouliot <sebastien@xamarin.com>
+//
+// Copyright 2012 Xamarin Inc.
+//
+
+using System;
+using System.IO;
+
+namespace Xamarin {
+
+ public static class CommonCryptor {
+
+ static public void Generate (string namespaceName, string typeName, string baseTypeName, string ccAlgorithmName, string feedbackSize = "8", string ctorInitializers = null)
+ {
+ string template = @"// Generated file to bind CommonCrypto cipher algorithms - DO NOT EDIT
+//
+// Authors:
+// Sebastien Pouliot <sebastien@xamarin.com>
+//
+// Copyright 2012-2014 Xamarin Inc.
+
+using System;
+using System.Security.Cryptography;
+
+using Mono.Security.Cryptography;
+using Crimson.CommonCrypto;
+
+namespace %NAMESPACE% {
+
+ public sealed partial class %TYPE% : %BASE% {
+
+ public %TYPE% ()
+ {
+ FeedbackSizeValue = %FEEDBACKSIZE%;
+ %CTOR_INIT%
+ }
+
+ public override void GenerateIV ()
+ {
+ IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
+ }
+
+ public override void GenerateKey ()
+ {
+ KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
+ }
+
+ public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV)
+ {
+ IntPtr decryptor = IntPtr.Zero;
+ switch (Mode) {
+ case CipherMode.CBC:
+ decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.None, rgbKey, rgbIV);
+ return new FastCryptorTransform (decryptor, this, false, rgbIV);
+ case CipherMode.ECB:
+ decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
+ return new FastCryptorTransform (decryptor, this, false, rgbIV);
+ case CipherMode.CFB:
+#if MONOTOUCH || XAMMAC
+ IntPtr encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
+ decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
+ return new CryptorTransform (decryptor, encryptor, this, false, rgbIV);
+#else
+ throw new CryptographicException (""CFB is not supported by Crimson.CommonCrypto"");
+#endif
+ default:
+ throw new CryptographicException (String.Format (""{0} is not supported by the .NET framework"", Mode));
+ }
+ }
+
+ public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV)
+ {
+ IntPtr encryptor = IntPtr.Zero;
+ switch (Mode) {
+ case CipherMode.CBC:
+ encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.None, rgbKey, rgbIV);
+ return new FastCryptorTransform (encryptor, this, true, rgbIV);
+ case CipherMode.ECB:
+ encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
+ return new FastCryptorTransform (encryptor, this, true, rgbIV);
+ case CipherMode.CFB:
+#if MONOTOUCH || XAMMAC
+ encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
+ return new CryptorTransform (encryptor, IntPtr.Zero, this, true, rgbIV);
+#else
+ throw new CryptographicException (""CFB is not supported by Crimson.CommonCrypto"");
+#endif
+ default:
+ throw new CryptographicException (String.Format (""{0} is not supported by the .NET framework"", Mode));
+ }
+ }
+ }
+}";
+
+ File.WriteAllText (typeName + ".g.cs", template.Replace ("%NAMESPACE%", namespaceName).
+ Replace ("%TYPE%", typeName).Replace ("%BASE%", baseTypeName).Replace("%FEEDBACKSIZE%", feedbackSize).Replace ("%CTOR_INIT%", ctorInitializers).
+ Replace ("%CCALGORITHM%", ccAlgorithmName.ToString ()));
+ }
+ }
+}
--- /dev/null
+//
+// CommonCrypto code generator for digest algorithms
+//
+// Authors:
+// Sebastien Pouliot <sebastien@xamarin.com>
+//
+// Copyright 2012-2014 Xamarin Inc.
+//
+
+using System;
+using System.IO;
+
+namespace Xamarin {
+
+ public static class CommonDigest {
+
+#if !MONOTOUCH && !XAMMAC
+ // we do not add anything in MonoTouch, just replacing, so this is not needed
+ // however we can avoid a dependency on Mono.Security for Crimson.CommonCrypto.dll by including the base classes
+ static public void GenerateBaseClass (string namespaceName, string typeName, string baseTypeName, int hashSize,
+ string visibilityStart = "", string visibilityEnd = "")
+ {
+ string template = @"// Generated file to bind CommonCrypto/CommonDigest - DO NOT EDIT
+//
+// Authors:
+// Sebastien Pouliot <sebastien@xamarin.com>
+//
+// Copyright 2012-2014 Xamarin Inc.
+
+using System;
+using System.Security.Cryptography;
+using System.Runtime.InteropServices;
+
+namespace %NAMESPACE% {
+
+%VISIBILITY_START%
+ public
+%VISIBILITY_END%
+ abstract class %BASE% : HashAlgorithm {
+
+ protected %BASE% ()
+ {
+ HashSizeValue = %HASHSIZE%;
+ }
+
+ public static new %BASE% Create ()
+ {
+ return Create (""%BASE%"");
+ }
+
+ public static new %BASE% Create (string hashName)
+ {
+ object o = CryptoConfig.CreateFromName (hashName);
+ return (%BASE%) o ?? new %TYPE% ();
+ }
+ }
+}";
+
+ File.WriteAllText (baseTypeName + ".g.cs", template.Replace ("%NAMESPACE%", namespaceName).
+ Replace ("%TYPE%", typeName).Replace ("%BASE%", baseTypeName).
+ Replace ("%VISIBILITY_START%", visibilityStart).Replace ("%VISIBILITY_END%", visibilityEnd).
+ Replace ("%HASHSIZE%", hashSize.ToString ()));
+ }
+#endif
+ static public void Generate (string namespaceName, string typeName, string baseTypeName, int contextSize,
+ string visibilityStart = "", string visibilityEnd = "")
+ {
+ string template = @"// Generated file to bind CommonCrypto/CommonDigest - DO NOT EDIT
+//
+// Authors:
+// Sebastien Pouliot <sebastien@xamarin.com>
+//
+// Copyright 2011-2014 Xamarin Inc.
+
+using System;
+using System.Security.Cryptography;
+using System.Runtime.InteropServices;
+
+using Mono.Security.Cryptography;
+
+namespace %NAMESPACE% {
+
+%VISIBILITY_START%
+ public
+%VISIBILITY_END%
+ sealed class %TYPE% : %BASE% {
+
+ IntPtr ctx;
+
+ [DllImport (""/usr/lib/libSystem.dylib"", EntryPoint=""CC_%BASE%_Init"")]
+ static extern int Init (/* CC_%BASE%_CTX */ IntPtr c);
+
+ [DllImport (""/usr/lib/libSystem.dylib"", EntryPoint=""CC_%BASE%_Update"")]
+ static extern int Update (/* CC_%BASE%_CTX */ IntPtr c, /* const void * */ IntPtr data, /* uint32_t */ uint len);
+
+ [DllImport (""/usr/lib/libSystem.dylib"", EntryPoint=""CC_%BASE%_Final"")]
+ static extern int Final (/* unsigned char * */ byte [] md, /* CC_%BASE%_CTX */ IntPtr c);
+
+ public %TYPE% ()
+ {
+ ctx = IntPtr.Zero;
+ }
+
+ ~%TYPE% ()
+ {
+ Dispose (false);
+ }
+
+ protected override void Dispose (bool disposing)
+ {
+ if (ctx != IntPtr.Zero) {
+ Marshal.FreeHGlobal (ctx);
+ ctx = IntPtr.Zero;
+ }
+ base.Dispose (disposing);
+ GC.SuppressFinalize (this);
+ }
+
+ public override void Initialize ()
+ {
+ if (ctx == IntPtr.Zero)
+ ctx = Marshal.AllocHGlobal (%CTX_SIZE%);
+
+ int hr = Init (ctx);
+ if (hr != 1)
+ throw new CryptographicException (hr);
+ }
+
+ protected override void HashCore (byte[] data, int start, int length)
+ {
+ if (ctx == IntPtr.Zero)
+ Initialize ();
+
+ if (data.Length == 0)
+ return;
+
+ unsafe {
+ fixed (byte* p = &data [0]) {
+ int hr = Update (ctx, (IntPtr) (p + start), (uint) length);
+ if (hr != 1)
+ throw new CryptographicException (hr);
+ }
+ }
+ }
+
+ protected override byte[] HashFinal ()
+ {
+ if (ctx == IntPtr.Zero)
+ Initialize ();
+
+ byte[] data = new byte [HashSize >> 3];
+ int hr = Final (data, ctx);
+ if (hr != 1)
+ throw new CryptographicException (hr);
+
+ return data;
+ }
+ }
+}";
+
+ File.WriteAllText (typeName + ".g.cs", template.Replace ("%NAMESPACE%", namespaceName).
+ Replace ("%TYPE%", typeName).Replace ("%BASE%", baseTypeName).
+ Replace ("%VISIBILITY_START%", visibilityStart).Replace ("%VISIBILITY_END%", visibilityEnd).
+ Replace ("%CTX_SIZE%", contextSize.ToString ()));
+ }
+ }
+}
--- /dev/null
+thisdir = tools/commoncryptogenerator
+SUBDIRS =
+include ../../build/rules.make
+
+LIB_REFS =
+LOCAL_MCS_FLAGS =
+
+PROGRAM = commoncryptogenerator.exe
+NO_INSTALL = yes
+
+include ../../build/executable.make
--- /dev/null
+CommonCryptorGenerator.cs
+generator.cs
+CommonDigestGenerator.cs
--- /dev/null
+//
+// CommonCrypto Code Generator
+//
+// Authors:
+// Sebastien Pouliot <sebastien@xamarin.com>
+//
+// Copyright 2012 Xamarin Inc.
+//
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+
+namespace Xamarin {
+
+ class Program {
+ static void Main (string [] args)
+ {
+ // mscorlib replacements
+ CommonDigest.Generate ("System.Security.Cryptography", "MD5CryptoServiceProvider", "MD5", 1000);
+ CommonDigest.Generate ("System.Security.Cryptography", "SHA1CryptoServiceProvider", "SHA1", 1000);
+ CommonDigest.Generate ("System.Security.Cryptography", "SHA1Managed", "SHA1", 1000);
+ CommonDigest.Generate ("System.Security.Cryptography", "SHA256Managed", "SHA256", 1000);
+ CommonDigest.Generate ("System.Security.Cryptography", "SHA384Managed", "SHA384", 1000);
+ CommonDigest.Generate ("System.Security.Cryptography", "SHA512Managed", "SHA512", 1000);
+
+ // System.Core replacements - not yet in MT profile (4.0 - functional dupes anyway)
+ //CommonDigest.Generate ("System.Security.Cryptography", "MD5Cng", "MD5", 1000);
+ //CommonDigest.Generate ("System.Security.Cryptography", "SHA256Cng", "SHA256", 1000);
+ //CommonDigest.Generate ("System.Security.Cryptography", "SHA384Cng", "SHA384", 1000);
+ //CommonDigest.Generate ("System.Security.Cryptography", "SHA512Cng", "SHA512", 1000);
+ //CommonDigest.Generate ("System.Security.Cryptography", "SHA256CryptoServiceProvider", "SHA256", 1000);
+ //CommonDigest.Generate ("System.Security.Cryptography", "SHA384CryptoServiceProvider", "SHA384", 1000);
+ //CommonDigest.Generate ("System.Security.Cryptography", "SHA512CryptoServiceProvider", "SHA512", 1000);
+
+ // Mono.Security replacements
+ CommonDigest.Generate ("Mono.Security.Cryptography", "MD2Managed", "MD2", 1000, "#if !INSIDE_CORLIB", "#endif");
+ CommonDigest.Generate ("Mono.Security.Cryptography", "MD4Managed", "MD4", 1000, "#if !INSIDE_CORLIB", "#endif");
+ CommonDigest.Generate ("Mono.Security.Cryptography", "SHA224Managed", "SHA224", 1000);
+
+ // mscorlib replacements
+ CommonCryptor.Generate ("System.Security.Cryptography", "DESCryptoServiceProvider", "DES", "DES");
+ CommonCryptor.Generate ("System.Security.Cryptography", "TripleDESCryptoServiceProvider", "TripleDES", "TripleDES");
+ CommonCryptor.Generate ("System.Security.Cryptography", "RC2CryptoServiceProvider", "RC2", "RC2", ctorInitializers: "LegalKeySizesValue = new[] { new KeySizes(40, 128, 8) };");
+ // Rijndael supports block sizes that are not available in AES - as such it does not use the same generated code
+ // but has it's own version, using AES (128 bits block size) and falling back to managed (192/256 bits block size)
+
+ // System.Core replacements
+ CommonCryptor.Generate ("System.Security.Cryptography", "AesManaged", "Aes", "AES128", "128");
+ CommonCryptor.Generate ("System.Security.Cryptography", "AesCryptoServiceProvider", "Aes", "AES128");
+
+ // Mono.Security replacements
+ // RC4 is a stream (not a block) cipher so it can not reuse the generated code
+ }
+ }
+}
\ No newline at end of file