You've already forked linux-packaging-mono
Imported Upstream version 4.6.0.125
Former-commit-id: a2155e9bd80020e49e72e86c44da02a8ac0e57a4
This commit is contained in:
parent
a569aebcfd
commit
e79aa3c0ed
104
mcs/tools/commoncryptogenerator/CommonCryptorGenerator.cs
Normal file
104
mcs/tools/commoncryptogenerator/CommonCryptorGenerator.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// 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 ()));
|
||||
}
|
||||
}
|
||||
}
|
167
mcs/tools/commoncryptogenerator/CommonDigestGenerator.cs
Normal file
167
mcs/tools/commoncryptogenerator/CommonDigestGenerator.cs
Normal file
@@ -0,0 +1,167 @@
|
||||
//
|
||||
// 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 ()));
|
||||
}
|
||||
}
|
||||
}
|
11
mcs/tools/commoncryptogenerator/Makefile
Normal file
11
mcs/tools/commoncryptogenerator/Makefile
Normal file
@@ -0,0 +1,11 @@
|
||||
thisdir = tools/commoncryptogenerator
|
||||
SUBDIRS =
|
||||
include ../../build/rules.make
|
||||
|
||||
LIB_REFS =
|
||||
LOCAL_MCS_FLAGS =
|
||||
|
||||
PROGRAM = commoncryptogenerator.exe
|
||||
NO_INSTALL = yes
|
||||
|
||||
include ../../build/executable.make
|
@@ -0,0 +1,3 @@
|
||||
CommonCryptorGenerator.cs
|
||||
generator.cs
|
||||
CommonDigestGenerator.cs
|
57
mcs/tools/commoncryptogenerator/generator.cs
Normal file
57
mcs/tools/commoncryptogenerator/generator.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user