[bcl] Add CommonCrypto to corlib, Mono.Security and System.Core.
[mono.git] / mcs / class / corlib / CommonCrypto / CommonCryptorGenerator.cs
1 //
2 // CommonCrypto code generator for symmetric block cipher algorithms
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@xamarin.com>
6 //
7 // Copyright 2012 Xamarin Inc.
8 //
9
10 using System;
11 using System.IO;
12
13 namespace Xamarin {
14
15         public static class CommonCryptor {
16                 
17                 static public void Generate (string namespaceName, string typeName, string baseTypeName, string ccAlgorithmName, string feedbackSize = "8", string ctorInitializers = null)
18                 {
19                         string template = @"// Generated file to bind CommonCrypto cipher algorithms - DO NOT EDIT
20 //
21 // Authors:
22 //      Sebastien Pouliot  <sebastien@xamarin.com>
23 //
24 // Copyright 2012-2014 Xamarin Inc.
25
26 using System;
27 using System.Security.Cryptography;
28
29 using Mono.Security.Cryptography;
30 using Crimson.CommonCrypto;
31
32 namespace %NAMESPACE% {
33
34         public sealed partial class %TYPE% : %BASE% {
35                 
36                 public %TYPE% ()
37                 {
38                         FeedbackSizeValue = %FEEDBACKSIZE%;
39                         %CTOR_INIT%
40                 }
41                 
42                 public override void GenerateIV ()
43                 {
44                         IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
45                 }
46                 
47                 public override void GenerateKey ()
48                 {
49                         KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
50                 }
51                 
52                 public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV) 
53                 {
54                         IntPtr decryptor = IntPtr.Zero;
55                         switch (Mode) {
56                         case CipherMode.CBC:
57                                 decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.None, rgbKey, rgbIV);
58                                 return new FastCryptorTransform (decryptor, this, false, rgbIV);
59                         case CipherMode.ECB:
60                                 decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
61                                 return new FastCryptorTransform (decryptor, this, false, rgbIV);
62                         case CipherMode.CFB:
63 #if MONOTOUCH || XAMMAC
64                                 IntPtr encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
65                                 decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
66                                 return new CryptorTransform (decryptor, encryptor, this, false, rgbIV);
67 #else
68                                 throw new CryptographicException (""CFB is not supported by Crimson.CommonCrypto"");
69 #endif
70                         default:
71                                 throw new CryptographicException (String.Format (""{0} is not supported by the .NET framework"", Mode));
72                         }
73                 }
74                 
75                 public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV) 
76                 {
77                         IntPtr encryptor = IntPtr.Zero;
78                         switch (Mode) {
79                         case CipherMode.CBC:
80                                 encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.None, rgbKey, rgbIV);
81                                 return new FastCryptorTransform (encryptor, this, true, rgbIV);
82                         case CipherMode.ECB:
83                                 encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
84                                 return new FastCryptorTransform (encryptor, this, true, rgbIV);
85                         case CipherMode.CFB:
86 #if MONOTOUCH || XAMMAC
87                                 encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
88                                 return new CryptorTransform (encryptor, IntPtr.Zero, this, true, rgbIV);
89 #else
90                                 throw new CryptographicException (""CFB is not supported by Crimson.CommonCrypto"");
91 #endif
92                         default:
93                                 throw new CryptographicException (String.Format (""{0} is not supported by the .NET framework"", Mode));
94                         }
95                 }
96         }
97 }";
98                         
99                         File.WriteAllText (typeName + ".g.cs", template.Replace ("%NAMESPACE%", namespaceName).
100                                 Replace ("%TYPE%", typeName).Replace ("%BASE%", baseTypeName).Replace("%FEEDBACKSIZE%", feedbackSize).Replace ("%CTOR_INIT%", ctorInitializers).
101                                 Replace ("%CCALGORITHM%", ccAlgorithmName.ToString ()));
102                 }
103         }
104 }