Merge pull request #2102 from AdamBurgess/master
[mono.git] / mcs / tools / tuner / Mono.Tuner / PreserveCrypto.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5
6 using Mono.Linker;
7 using Mono.Linker.Steps;
8
9 using Mono.Cecil;
10 using Mono.Cecil.Cil;
11
12 namespace Mono.Tuner {
13
14         public class PreserveCrypto : IStep {
15
16                 AnnotationStore annotations;
17
18                 public void Process (LinkContext context)
19                 {
20                         annotations = context.Annotations;
21
22                         ProcessCorlib (context);
23                         ProcessSystemCore (context);
24                 }
25
26                 void ProcessCorlib (LinkContext context)
27                 {
28                         AssemblyDefinition corlib;
29                         if (!context.TryGetLinkedAssembly ("mscorlib", out corlib))
30                                 return;
31
32                         AddPreserveInfo (corlib, "DES", "DESCryptoServiceProvider");
33                         AddPreserveInfo (corlib, "DSA", "DSACryptoServiceProvider");
34                         AddPreserveInfo (corlib, "RandomNumberGenerator", "RNGCryptoServiceProvider");
35                         AddPreserveInfo (corlib, "SHA1", "SHA1CryptoServiceProvider");
36                         AddPreserveInfo (corlib, "SHA1", "SHA1Managed");
37                         AddPreserveInfo (corlib, "MD5", "MD5CryptoServiceProvider");
38                         AddPreserveInfo (corlib, "RC2", "RC2CryptoServiceProvider");
39                         AddPreserveInfo (corlib, "TripleDES", "TripleDESCryptoServiceProvider");
40
41                         AddPreserveInfo (corlib, "Rijndael", "RijndaelManaged");
42                         AddPreserveInfo (corlib, "RIPEMD160", "RIPEMD160Managed");
43                         AddPreserveInfo (corlib, "SHA256", "SHA256Managed");
44                         AddPreserveInfo (corlib, "SHA384", "SHA384Managed");
45                         AddPreserveInfo (corlib, "SHA512", "SHA512Managed");
46
47                         AddPreserveInfo (corlib, "HMAC", "HMACMD5");
48                         AddPreserveInfo (corlib, "HMAC", "HMACRIPEMD160");
49                         AddPreserveInfo (corlib, "HMAC", "HMACSHA1");
50                         AddPreserveInfo (corlib, "HMAC", "HMACSHA256");
51                         AddPreserveInfo (corlib, "HMAC", "HMACSHA384");
52                         AddPreserveInfo (corlib, "HMAC", "HMACSHA512");
53
54                         AddPreserveInfo (corlib, "HMACMD5", "MD5CryptoServiceProvider");
55                         AddPreserveInfo (corlib, "HMACRIPEMD160", "RIPEMD160Managed");
56                         AddPreserveInfo (corlib, "HMACSHA1", "SHA1CryptoServiceProvider");
57                         AddPreserveInfo (corlib, "HMACSHA1", "SHA1Managed");
58                         AddPreserveInfo (corlib, "HMACSHA256", "SHA256Managed");
59                         AddPreserveInfo (corlib, "HMACSHA384", "SHA384Managed");
60                         AddPreserveInfo (corlib, "HMACSHA512", "SHA512Managed");
61
62                         TryAddPreserveInfo (corlib, "Aes", "AesManaged");
63
64                         var corlibAes = GetCryptoType (corlib, "Aes");
65                         Preserve (corlibAes, GetCryptoType (corlib, "AesManaged"));
66
67                         AssemblyDefinition syscore;
68                         if (context.TryGetLinkedAssembly ("System.Core", out syscore))
69                                 Preserve (corlibAes, GetCryptoType (syscore, "AesCryptoServiceProvider"));
70                 }
71
72                 void ProcessSystemCore (LinkContext context)
73                 {
74                         AssemblyDefinition syscore;
75                         if (!context.TryGetLinkedAssembly ("System.Core", out syscore))
76                                 return;
77
78                         // AddPreserveInfo (syscore, "Aes", "AesCryptoServiceProvider");
79                         TryAddPreserveInfo (syscore, "Aes", "AesManaged");
80                 }
81
82                 bool TryAddPreserveInfo (AssemblyDefinition assembly, string name, string type)
83                 {
84                         var marker = GetCryptoType (assembly, name);
85                         if (marker == null)
86                                 return false;
87
88                         var implementation = GetCryptoType (assembly, type);
89                         if (implementation == null)
90                                 return false;
91
92                         Preserve (marker, implementation);
93                         return true;
94                 }
95
96                 void AddPreserveInfo (AssemblyDefinition assembly, string name, string type)
97                 {
98                         var marker = GetCryptoType (assembly, name);
99                         if (marker == null)
100                                 throw new ArgumentException (name);
101
102                         var implementation = GetCryptoType (assembly, type);
103                         if (implementation == null)
104                                 throw new ArgumentException (type);
105
106                         Preserve (marker, implementation);
107                 }
108
109                 void Preserve (TypeDefinition marker, TypeDefinition implementation)
110                 {
111                         if (marker == null || implementation == null)
112                                 return;
113                         foreach (var constructor in implementation.GetConstructors ())
114                                 annotations.AddPreservedMethod (marker, constructor);
115                 }
116
117                 TypeDefinition GetCryptoType (AssemblyDefinition assembly, string name)
118                 {
119                         return assembly.MainModule.GetType ("System.Security.Cryptography." + name);
120                 }
121         }
122 }