2 using System.Collections;
3 using System.Collections.Generic;
7 using Mono.Linker.Steps;
12 namespace Mono.Tuner {
14 public class PreserveCrypto : IStep {
16 AnnotationStore annotations;
18 public void Process (LinkContext context)
20 annotations = context.Annotations;
22 ProcessCorlib (context);
23 ProcessSystemCore (context);
26 void ProcessCorlib (LinkContext context)
28 AssemblyDefinition corlib;
29 if (!context.TryGetLinkedAssembly ("mscorlib", out corlib))
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");
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");
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");
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");
62 TryAddPreserveInfo (corlib, "Aes", "AesManaged");
64 var corlibAes = GetCryptoType (corlib, "Aes");
65 Preserve (corlibAes, GetCryptoType (corlib, "AesManaged"));
67 AssemblyDefinition syscore;
68 if (context.TryGetLinkedAssembly ("System.Core", out syscore))
69 Preserve (corlibAes, GetCryptoType (syscore, "AesCryptoServiceProvider"));
72 void ProcessSystemCore (LinkContext context)
74 AssemblyDefinition syscore;
75 if (!context.TryGetLinkedAssembly ("System.Core", out syscore))
78 // AddPreserveInfo (syscore, "Aes", "AesCryptoServiceProvider");
79 TryAddPreserveInfo (syscore, "Aes", "AesManaged");
82 bool TryAddPreserveInfo (AssemblyDefinition assembly, string name, string type)
84 var marker = GetCryptoType (assembly, name);
88 var implementation = GetCryptoType (assembly, type);
89 if (implementation == null)
92 Preserve (marker, implementation);
96 void AddPreserveInfo (AssemblyDefinition assembly, string name, string type)
98 var marker = GetCryptoType (assembly, name);
100 throw new ArgumentException (name);
102 var implementation = GetCryptoType (assembly, type);
103 if (implementation == null)
104 throw new ArgumentException (type);
106 Preserve (marker, implementation);
109 void Preserve (TypeDefinition marker, TypeDefinition implementation)
111 if (marker == null || implementation == null)
113 foreach (var constructor in implementation.GetConstructors ())
114 annotations.AddPreservedMethod (marker, constructor);
117 TypeDefinition GetCryptoType (AssemblyDefinition assembly, string name)
119 return assembly.MainModule.GetType ("System.Security.Cryptography." + name);