75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
1 |
// dll.cpp - written and placed in the public domain by Wei Dai
|
2 |
||
3 |
#define CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
|
|
80
by weidai
fix GCC compile |
4 |
#define CRYPTOPP_DEFAULT_NO_DLL
|
75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
5 |
|
6 |
#include "dll.h" |
|
7 |
#pragma warning(default: 4660)
|
|
8 |
||
196
by weidai
port to GCC 4 |
9 |
#if defined(CRYPTOPP_EXPORTS) && defined(CRYPTOPP_WIN32_AVAILABLE)
|
75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
10 |
#include <windows.h> |
81
by weidai
fix for Unix |
11 |
#endif
|
75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
12 |
|
78
by weidai
merge in 5.0.4 changes (exclude DES and SHA-2 from DLL), |
13 |
#ifndef CRYPTOPP_IMPORTS
|
75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
14 |
|
15 |
NAMESPACE_BEGIN(CryptoPP) |
|
16 |
||
181
by weidai
changes done for FIPS-140 lab code drop |
17 |
template<> const byte PKCS_DigestDecoration<SHA1>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14}; |
18 |
template<> const unsigned int PKCS_DigestDecoration<SHA1>::length = sizeof(PKCS_DigestDecoration<SHA1>::decoration); |
|
19 |
||
20 |
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}; |
|
21 |
template<> const unsigned int PKCS_DigestDecoration<SHA224>::length = sizeof(PKCS_DigestDecoration<SHA224>::decoration); |
|
75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
22 |
|
179
by weidai
changes related to the next FIPS validation |
23 |
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}; |
24 |
template<> const unsigned int PKCS_DigestDecoration<SHA256>::length = sizeof(PKCS_DigestDecoration<SHA256>::decoration); |
|
25 |
||
26 |
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}; |
|
27 |
template<> const unsigned int PKCS_DigestDecoration<SHA384>::length = sizeof(PKCS_DigestDecoration<SHA384>::decoration); |
|
28 |
||
29 |
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}; |
|
30 |
template<> const unsigned int PKCS_DigestDecoration<SHA512>::length = sizeof(PKCS_DigestDecoration<SHA512>::decoration); |
|
31 |
||
32 |
template<> const byte EMSA2HashId<SHA>::id = 0x33; |
|
181
by weidai
changes done for FIPS-140 lab code drop |
33 |
template<> const byte EMSA2HashId<SHA224>::id = 0x38; |
179
by weidai
changes related to the next FIPS validation |
34 |
template<> const byte EMSA2HashId<SHA256>::id = 0x34; |
35 |
template<> const byte EMSA2HashId<SHA384>::id = 0x36; |
|
36 |
template<> const byte EMSA2HashId<SHA512>::id = 0x35; |
|
37 |
||
75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
38 |
NAMESPACE_END
|
39 |
||
40 |
#endif
|
|
41 |
||
42 |
#ifdef CRYPTOPP_EXPORTS
|
|
43 |
||
44 |
USING_NAMESPACE(CryptoPP) |
|
45 |
||
78
by weidai
merge in 5.0.4 changes (exclude DES and SHA-2 from DLL), |
46 |
#if !(defined(_MSC_VER) && (_MSC_VER < 1300))
|
47 |
using std::set_new_handler; |
|
48 |
#endif
|
|
49 |
||
75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
50 |
static PNew s_pNew = NULL; |
51 |
static PDelete s_pDelete = NULL; |
|
52 |
||
181
by weidai
changes done for FIPS-140 lab code drop |
53 |
static void * New (size_t size) |
78
by weidai
merge in 5.0.4 changes (exclude DES and SHA-2 from DLL), |
54 |
{
|
55 |
void *p; |
|
56 |
while (!(p = malloc(size))) |
|
116
by weidai
guard against potential integer overflow in allocators |
57 |
CallNewHandler(); |
78
by weidai
merge in 5.0.4 changes (exclude DES and SHA-2 from DLL), |
58 |
|
59 |
return p; |
|
60 |
}
|
|
61 |
||
62 |
static void SetNewAndDeleteFunctionPointers() |
|
63 |
{
|
|
64 |
void *p = NULL; |
|
65 |
HMODULE hModule = NULL; |
|
66 |
MEMORY_BASIC_INFORMATION mbi; |
|
67 |
||
68 |
while (true) |
|
69 |
{
|
|
70 |
VirtualQuery(p, &mbi, sizeof(mbi)); |
|
71 |
||
72 |
if (p >= (char *)mbi.BaseAddress + mbi.RegionSize) |
|
73 |
break; |
|
74 |
||
75 |
p = (char *)mbi.BaseAddress + mbi.RegionSize; |
|
76 |
||
77 |
if (!mbi.AllocationBase || mbi.AllocationBase == hModule) |
|
78 |
continue; |
|
79 |
||
80 |
hModule = HMODULE(mbi.AllocationBase); |
|
81 |
||
82 |
PGetNewAndDelete pGetNewAndDelete = (PGetNewAndDelete)GetProcAddress(hModule, "GetNewAndDeleteForCryptoPP"); |
|
83 |
if (pGetNewAndDelete) |
|
84 |
{
|
|
85 |
pGetNewAndDelete(s_pNew, s_pDelete); |
|
86 |
return; |
|
87 |
}
|
|
88 |
||
89 |
PSetNewAndDelete pSetNewAndDelete = (PSetNewAndDelete)GetProcAddress(hModule, "SetNewAndDeleteFromCryptoPP"); |
|
90 |
if (pSetNewAndDelete) |
|
91 |
{
|
|
92 |
s_pNew = &New; |
|
93 |
s_pDelete = &free; |
|
94 |
pSetNewAndDelete(s_pNew, s_pDelete, &set_new_handler); |
|
95 |
return; |
|
96 |
}
|
|
97 |
}
|
|
98 |
||
302
by weidai
add 64-bit mangled names of new and delete |
99 |
// try getting these directly using mangled names of new and delete operators
|
100 |
||
78
by weidai
merge in 5.0.4 changes (exclude DES and SHA-2 from DLL), |
101 |
hModule = GetModuleHandle("msvcrtd"); |
102 |
if (!hModule) |
|
103 |
hModule = GetModuleHandle("msvcrt"); |
|
104 |
if (hModule) |
|
105 |
{
|
|
302
by weidai
add 64-bit mangled names of new and delete |
106 |
// 32-bit versions
|
107 |
s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPAXI@Z"); |
|
108 |
s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPAX@Z"); |
|
109 |
if (s_pNew && s_pDelete) |
|
110 |
return; |
|
111 |
||
112 |
// 64-bit versions
|
|
113 |
s_pNew = (PNew)GetProcAddress(hModule, "??2@YAPEAX_K@Z"); |
|
114 |
s_pDelete = (PDelete)GetProcAddress(hModule, "??3@YAXPEAX@Z"); |
|
115 |
if (s_pNew && s_pDelete) |
|
116 |
return; |
|
78
by weidai
merge in 5.0.4 changes (exclude DES and SHA-2 from DLL), |
117 |
}
|
118 |
||
119 |
OutputDebugString("Crypto++ was not able to obtain new and delete function pointers.\n"); |
|
120 |
throw 0; |
|
121 |
}
|
|
122 |
||
181
by weidai
changes done for FIPS-140 lab code drop |
123 |
void * operator new (size_t size) |
75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
124 |
{
|
125 |
if (!s_pNew) |
|
78
by weidai
merge in 5.0.4 changes (exclude DES and SHA-2 from DLL), |
126 |
SetNewAndDeleteFunctionPointers(); |
75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
127 |
|
128 |
return s_pNew(size); |
|
129 |
}
|
|
130 |
||
181
by weidai
changes done for FIPS-140 lab code drop |
131 |
void operator delete (void * p) |
75
by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes |
132 |
{
|
133 |
s_pDelete(p); |
|
134 |
}
|
|
135 |
||
181
by weidai
changes done for FIPS-140 lab code drop |
136 |
void * operator new [] (size_t size) |
127
by weidai
add missing overrides for new [] and delete [] |
137 |
{
|
138 |
return operator new (size); |
|
139 |
}
|
|
140 |
||
181
by weidai
changes done for FIPS-140 lab code drop |
141 |
void operator delete [] (void * p) |
127
by weidai
add missing overrides for new [] and delete [] |
142 |
{
|
143 |
operator delete (p); |
|
144 |
}
|
|
145 |
||
78
by weidai
merge in 5.0.4 changes (exclude DES and SHA-2 from DLL), |
146 |
#endif // #ifdef CRYPTOPP_EXPORTS |