Crypto++ 8.9
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
23NAMESPACE_BEGIN(CryptoPP)
24
25// Guarding based on DLL due to Clang, http://github.com/weidai11/cryptopp/issues/300
26#ifdef CRYPTOPP_IS_DLL
27template<> const byte PKCS_DigestDecoration<SHA1>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};
29
30template<> 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};
32
33template<> 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};
35
36template<> 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};
38
39template<> 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};
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
44template<> 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};
45template<> const unsigned int PKCS_DigestDecoration<SHA3_256>::length = (unsigned int)sizeof(PKCS_DigestDecoration<SHA3_256>::decoration);
46
47template<> 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};
48template<> const unsigned int PKCS_DigestDecoration<SHA3_384>::length = (unsigned int)sizeof(PKCS_DigestDecoration<SHA3_384>::decoration);
49
50template<> 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};
51template<> const unsigned int PKCS_DigestDecoration<SHA3_512>::length = (unsigned int)sizeof(PKCS_DigestDecoration<SHA3_512>::decoration);
52
53template<> const byte EMSA2HashId<SHA1>::id = 0x33;
54template<> const byte EMSA2HashId<SHA224>::id = 0x38;
55template<> const byte EMSA2HashId<SHA256>::id = 0x34;
56template<> const byte EMSA2HashId<SHA384>::id = 0x36;
57template<> const byte EMSA2HashId<SHA512>::id = 0x35;
58
59#endif // CRYPTOPP_IS_DLL
60
61NAMESPACE_END
62
63#endif
64
65#ifdef CRYPTOPP_EXPORTS
66
67USING_NAMESPACE(CryptoPP)
68
69static PNew s_pNew = NULLPTR;
70static PDelete s_pDelete = NULLPTR;
71
72static void * New (size_t size)
73{
74 void *p;
75 while ((p = malloc(size)) == NULLPTR)
77
78 return p;
79}
80
81static 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, &std::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
144void * operator new (size_t size)
145{
146 if (!s_pNew)
147 SetNewAndDeleteFunctionPointers();
148
149 return s_pNew(size);
150}
151
152void operator delete (void * p)
153{
154 s_pDelete(p);
155}
156
157void * operator new [] (size_t size)
158{
159 return operator new (size);
160}
161
162void operator delete [] (void * p)
163{
164 operator delete (p);
165}
166
167#endif // CRYPTOPP_EXPORTS
CRYPTOPP_DLL void CallNewHandler()
Attempts to reclaim unused memory.
EMSA2 hash identifier.
Definition emsa2.h:24
PKCS #1 decoration data structure.
Definition pkcspad.h:35
Library configuration file.
Functions and definitions required for building the FIPS-140 DLL on Windows.
Classes and functions for various padding schemes used in public key algorithms.
Base classes for iterated hashes.
Crypto++ library namespace.
Classes for PKCS padding schemes.