Security Scol plugin
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
69using std::set_new_handler;
70
71static PNew s_pNew = NULLPTR;
72static PDelete s_pDelete = NULLPTR;
73
74static void * New (size_t size)
75{
76 void *p;
77 while ((p = malloc(size)) == NULLPTR)
78 CallNewHandler();
79
80 return p;
81}
82
83static 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
146void * operator new (size_t size)
147{
148 if (!s_pNew)
149 SetNewAndDeleteFunctionPointers();
150
151 return s_pNew(size);
152}
153
154void operator delete (void * p)
155{
156 s_pDelete(p);
157}
158
159void * operator new [] (size_t size)
160{
161 return operator new (size);
162}
163
164void operator delete [] (void * p)
165{
166 operator delete (p);
167}
168
169#endif // CRYPTOPP_EXPORTS
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.
Classes for PKCS padding schemes.