Crypto++  5.6.5
Free C++ class library of cryptographic schemes
dll.cpp
1 // dll.cpp - originally written and placed in the public domain by Wei Dai
2 
3 #define CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
4 #define CRYPTOPP_DEFAULT_NO_DLL
5 
6 #include "dll.h"
7 #include "config.h"
8 #include "iterhash.h"
9 
10 // Cast from FARPROC to funcptr with args, http://stackoverflow.com/q/4192058/608639
11 #pragma warning(disable: 4191)
12 
13 #if defined(CRYPTOPP_EXPORTS) && defined(CRYPTOPP_WIN32_AVAILABLE)
14 #include <windows.h>
15 #endif
16 
17 #ifndef CRYPTOPP_IMPORTS
18 
19 NAMESPACE_BEGIN(CryptoPP)
20 
21 // Guarding based on DLL due to Clang, http://github.com/weidai11/cryptopp/issues/300
22 #if defined(CRYPTOPP_IS_DLL)
23 template<> const byte PKCS_DigestDecoration<SHA1>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};
24 template<> const unsigned int PKCS_DigestDecoration<SHA1>::length = sizeof(PKCS_DigestDecoration<SHA1>::decoration);
25 
26 template<> const byte PKCS_DigestDecoration<SHA224>::decoration[] = {0x30,0x2d,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x04,0x05,0x00,0x04,0x1c};
27 template<> const unsigned int PKCS_DigestDecoration<SHA224>::length = sizeof(PKCS_DigestDecoration<SHA224>::decoration);
28 
29 template<> const byte PKCS_DigestDecoration<SHA256>::decoration[] = {0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01,0x05,0x00,0x04,0x20};
30 template<> const unsigned int PKCS_DigestDecoration<SHA256>::length = sizeof(PKCS_DigestDecoration<SHA256>::decoration);
31 
32 template<> const byte PKCS_DigestDecoration<SHA384>::decoration[] = {0x30,0x41,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x02,0x05,0x00,0x04,0x30};
33 template<> const unsigned int PKCS_DigestDecoration<SHA384>::length = sizeof(PKCS_DigestDecoration<SHA384>::decoration);
34 
35 template<> const byte PKCS_DigestDecoration<SHA512>::decoration[] = {0x30,0x51,0x30,0x0d,0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x03,0x05,0x00,0x04,0x40};
36 template<> const unsigned int PKCS_DigestDecoration<SHA512>::length = sizeof(PKCS_DigestDecoration<SHA512>::decoration);
37 
38 template<> const byte EMSA2HashId<SHA1>::id = 0x33;
39 template<> const byte EMSA2HashId<SHA224>::id = 0x38;
40 template<> const byte EMSA2HashId<SHA256>::id = 0x34;
41 template<> const byte EMSA2HashId<SHA384>::id = 0x36;
42 template<> const byte EMSA2HashId<SHA512>::id = 0x35;
43 
44 #endif // CRYPTOPP_IS_DLL
45 
46 NAMESPACE_END
47 
48 #endif
49 
50 #ifdef CRYPTOPP_EXPORTS
51 
52 USING_NAMESPACE(CryptoPP)
53 
54 using std::set_new_handler;
55 
56 static PNew s_pNew = NULLPTR;
57 static PDelete s_pDelete = NULLPTR;
58 
59 static void * New (size_t size)
60 {
61  void *p;
62  while ((p = malloc(size)) == NULLPTR)
64 
65  return p;
66 }
67 
68 static void SetNewAndDeleteFunctionPointers()
69 {
70  void *p = NULLPTR;
71  HMODULE hModule = NULLPTR;
72  MEMORY_BASIC_INFORMATION mbi;
73 
74  while (true)
75  {
76  VirtualQuery(p, &mbi, sizeof(mbi));
77 
78  if (p >= (char *)mbi.BaseAddress + mbi.RegionSize)
79  break;
80 
81  p = (char *)mbi.BaseAddress + mbi.RegionSize;
82 
83  if (!mbi.AllocationBase || mbi.AllocationBase == hModule)
84  continue;
85 
86  hModule = HMODULE(mbi.AllocationBase);
87  PGetNewAndDelete pGetNewAndDelete = (PGetNewAndDelete)GetProcAddress(hModule, "GetNewAndDeleteForCryptoPP");
88  if (pGetNewAndDelete)
89  {
90  pGetNewAndDelete(s_pNew, s_pDelete);
91  return;
92  }
93 
94  PSetNewAndDelete pSetNewAndDelete = (PSetNewAndDelete)GetProcAddress(hModule, "SetNewAndDeleteFromCryptoPP");
95  if (pSetNewAndDelete)
96  {
97  s_pNew = &New;
98  s_pDelete = &free;
99  pSetNewAndDelete(s_pNew, s_pDelete, &set_new_handler);
100  return;
101  }
102  }
103 
104  // try getting these directly using mangled names of new and delete operators
105 
106  hModule = GetModuleHandle("msvcrtd");
107  if (!hModule)
108  hModule = GetModuleHandle("msvcrt");
109  if (hModule)
110  {
111  // 32-bit versions
112  s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPAXI@Z");
113  s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPAX@Z");
114  if (s_pNew && s_pDelete)
115  return;
116 
117  // 64-bit versions
118  s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPEAX_K@Z");
119  s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPEAX@Z");
120  if (s_pNew && s_pDelete)
121  return;
122  }
123 
124  OutputDebugString("Crypto++ DLL was not able to obtain new and delete function pointers.\n");
125  throw 0;
126 }
127 
128 // Cast from FARPROC to funcptr with args
129 #pragma warning(default: 4191)
130 
131 void * operator new (size_t size)
132 {
133  if (!s_pNew)
134  SetNewAndDeleteFunctionPointers();
135 
136  return s_pNew(size);
137 }
138 
139 void operator delete (void * p)
140 {
141  s_pDelete(p);
142 }
143 
144 void * operator new [] (size_t size)
145 {
146  return operator new (size);
147 }
148 
149 void operator delete [] (void * p)
150 {
151  operator delete (p);
152 }
153 
154 #endif // CRYPTOPP_EXPORTS
Library configuration file.
void CallNewHandler()
Attempts to reclaim unused memory.
Definition: misc.cpp:264
PKCS#1 decoration data structure.
Definition: pkcspad.h:34
Crypto++ library namespace.
Functions and definitions required for building the FIPS-140 DLL on Windows.