Update mcs/class/System.Core/System/TimeZoneInfo.cs
[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
65                 void ProcessSystemCore (LinkContext context)
66                 {
67                         AssemblyDefinition syscore;
68                         if (!context.TryGetLinkedAssembly ("System.Core", out syscore))
69                                 return;
70
71                         // AddPreserveInfo (syscore, "Aes", "AesCryptoServiceProvider");
72                         TryAddPreserveInfo (syscore, "Aes", "AesManaged");
73                 }
74
75                 bool TryAddPreserveInfo (AssemblyDefinition assembly, string name, string type)
76                 {
77                         var marker = GetCryptoType (assembly, name);
78                         if (marker == null)
79                                 return false;
80
81                         var implementation = GetCryptoType (assembly, type);
82                         if (implementation == null)
83                                 return false;
84
85                         Preserve (marker, implementation);
86                         return true;
87                 }
88
89                 void AddPreserveInfo (AssemblyDefinition assembly, string name, string type)
90                 {
91                         var marker = GetCryptoType (assembly, name);
92                         if (marker == null)
93                                 throw new ArgumentException (name);
94
95                         var implementation = GetCryptoType (assembly, type);
96                         if (implementation == null)
97                                 throw new ArgumentException (type);
98
99                         Preserve (marker, implementation);
100                 }
101
102                 void Preserve (TypeDefinition marker, TypeDefinition implementation)
103                 {
104                         foreach (var constructor in implementation.GetConstructors ())
105                                 annotations.AddPreservedMethod (marker, constructor);
106                 }
107
108                 TypeDefinition GetCryptoType (AssemblyDefinition assembly, string name)
109                 {
110                         return assembly.MainModule.GetType ("System.Security.Cryptography." + name);
111                 }
112         }
113 }