1 /**
2 * Dynamically Loaded Engine
3 * 
4 * Copyright:
5 * (C) 2010 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.engine.dyn_engine;
12 
13 import botan.constants;
14 import botan.engine.engine;
15 import botan.utils.dyn_load.dyn_load;
16 
17 /**
18 * Dynamically_Loaded_Engine just proxies the requests to the underlying
19 * Engine object, and handles load/unload details
20 */
21 final class DynamicallyLoadedEngine : Engine
22 {
23 private:
24     DynamicallyLoadedLibrary m_lib;
25     Engine m_engine;
26 public:
27     /**
28     * Params:
29     *  library_path = full pathname to DLL to load
30     */
31     this(in string library_path) 
32     {
33         m_engine = null;
34         m_lib = new DynamicallyLoadedLibrary(library_path);
35         
36         try
37         {
38             ModuleVersionFunc get_version = m_lib.resolve!ModuleVersionFunc("module_version");
39             
40             const uint mod_version = get_version();
41             
42             if (mod_version != 20101003)
43                 throw new Exception("Incompatible version in " ~ library_path ~ " of " ~ to!string(mod_version));
44             
45             CreatorFunc creator = m_lib.resolve!CreatorFunc("create_engine");
46             
47             m_engine = creator();
48 
49             if (!m_engine)
50                 throw new Exception("Creator function in " ~ library_path ~ " failed");
51         }
52         catch (Exception)
53         {
54             destroy(m_lib);
55             m_lib = null;
56             throw new Exception("Error in dynamic library constructor");
57         }
58     }
59 
60 
61     @disable this(in DynamicallyLoadedEngine);
62 
63     @disable void opAssign(DynamicallyLoadedEngine);
64 
65     ~this()
66     {
67         destroy(m_engine);
68         destroy(m_lib);
69     }
70 
71     string providerName() const { return m_engine.providerName(); }
72 
73     BlockCipher findBlockCipher(in SCANToken algo_spec, AlgorithmFactory af) const
74     {
75         return m_engine.findBlockCipher(algo_spec, af);
76     }
77 
78     StreamCipher findStreamCipher(in SCANToken algo_spec, AlgorithmFactory af) const
79     {
80         return m_engine.findStreamCipher(algo_spec, af);
81     }
82 
83     HashFunction findHash(in SCANToken algo_spec, AlgorithmFactory af) const
84     {
85         return m_engine.findHash(algo_spec, af);
86     }
87 
88     MessageAuthenticationCode findMac(in SCANToken algo_spec, AlgorithmFactory af) const
89     {
90         return m_engine.findMac(algo_spec, af);
91     }
92 
93     PBKDF findPbkdf(in SCANToken algo_spec, AlgorithmFactory af) const
94     {
95         return m_engine.findPbkdf(algo_spec, af);
96     }
97 
98 
99     KeyedFilter getCipher(in string algo_spec, CipherDir dir, AlgorithmFactory af) const
100     {
101         return m_engine.getCipher(algo_spec, dir, af);
102     }
103 
104     static if (BOTAN_HAS_PUBLIC_KEY_CRYPTO):
105 
106     ModularExponentiator modExp(const ref BigInt n, PowerMod.UsageHints hints) const
107     {
108         return m_engine.modExp(n, hints);
109     }
110 
111     KeyAgreement getKeyAgreementOp(in PrivateKey key, RandomNumberGenerator rng) const
112     {
113         return m_engine.getKeyAgreementOp(key, rng);
114     }
115 
116     Signature getSignatureOp(in PrivateKey key, RandomNumberGenerator rng) const
117     {
118         return m_engine.getSignatureOp(key, rng);
119     }
120 
121     Verification getVerifyOp(in PublicKey key, RandomNumberGenerator rng) const
122     {
123         return m_engine.getVerifyOp(key, rng);
124     }
125 
126     Encryption getEncryptionOp(in PublicKey key, RandomNumberGenerator rng) const
127     {
128         return m_engine.getEncryptionOp(key, rng);
129     }
130 
131     Decryption getDecryptionOp(in PrivateKey key, RandomNumberGenerator rng) const
132     {
133         return m_engine.getDecryptionOp(key, rng);
134     }
135 
136 }
137 
138 private nothrow @nogc extern(C):
139 
140 alias CreatorFunc = Engine function();
141 alias ModuleVersionFunc = uint function();