Merge pull request #3715 from kumpera/fix-44707
[mono.git] / mcs / tools / commoncryptogenerator / 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, string decryptorInitializers = null, string encryptorInitializers = null, string properties = 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                 %PROPERTIES%
43
44                 public override void GenerateIV ()
45                 {
46                         IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
47                 }
48                 
49                 public override void GenerateKey ()
50                 {
51                         KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
52                 }
53                 
54                 public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV) 
55                 {
56                         %CREATEDECRYPTOR_INIT%
57
58                         IntPtr decryptor = IntPtr.Zero;
59                         switch (Mode) {
60                         case CipherMode.CBC:
61                                 decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.None, rgbKey, rgbIV);
62                                 return new FastCryptorTransform (decryptor, this, false, rgbIV);
63                         case CipherMode.ECB:
64                                 decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
65                                 return new FastCryptorTransform (decryptor, this, false, rgbIV);
66                         case CipherMode.CFB:
67 #if MONOTOUCH || XAMMAC
68                                 IntPtr encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
69                                 decryptor = Cryptor.Create (CCOperation.Decrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
70                                 return new CryptorTransform (decryptor, encryptor, this, false, rgbIV);
71 #else
72                                 throw new CryptographicException (""CFB is not supported by Crimson.CommonCrypto"");
73 #endif
74                         default:
75                                 throw new CryptographicException (String.Format (""{0} is not supported by the .NET framework"", Mode));
76                         }
77                 }
78                 
79                 public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV) 
80                 {
81                         %CREATEENCRYPTOR_INIT%
82                         
83                         IntPtr encryptor = IntPtr.Zero;
84                         switch (Mode) {
85                         case CipherMode.CBC:
86                                 encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.None, rgbKey, rgbIV);
87                                 return new FastCryptorTransform (encryptor, this, true, rgbIV);
88                         case CipherMode.ECB:
89                                 encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
90                                 return new FastCryptorTransform (encryptor, this, true, rgbIV);
91                         case CipherMode.CFB:
92 #if MONOTOUCH || XAMMAC
93                                 encryptor = Cryptor.Create (CCOperation.Encrypt, CCAlgorithm.%CCALGORITHM%, CCOptions.ECBMode, rgbKey, rgbIV);
94                                 return new CryptorTransform (encryptor, IntPtr.Zero, this, true, rgbIV);
95 #else
96                                 throw new CryptographicException (""CFB is not supported by Crimson.CommonCrypto"");
97 #endif
98                         default:
99                                 throw new CryptographicException (String.Format (""{0} is not supported by the .NET framework"", Mode));
100                         }
101                 }
102         }
103 }";
104                         
105                         File.WriteAllText (typeName + ".g.cs", template.Replace ("%NAMESPACE%", namespaceName).
106                                 Replace ("%TYPE%", typeName).Replace ("%BASE%", baseTypeName).Replace("%FEEDBACKSIZE%", feedbackSize).Replace ("%CTOR_INIT%", ctorInitializers).
107                                 Replace ("%CREATEDECRYPTOR_INIT%", decryptorInitializers).
108                                 Replace ("%CREATEENCRYPTOR_INIT%", encryptorInitializers).
109                                 Replace ("%PROPERTIES%", properties).
110                                 Replace ("%CCALGORITHM%", ccAlgorithmName.ToString ()));
111                 }
112         }
113 }