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