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