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