1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/*
Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* based on Wei Dai's misc.cpp from CryptoPP */
#include "runtime.hpp"
#include "misc.hpp"
#ifdef __GNUC__
#include <signal.h>
#include <setjmp.h>
#endif
#ifdef USE_SYS_STL
#include <algorithm>
#else
#include "algorithm.hpp"
#endif
namespace STL = STL_NAMESPACE;
#ifdef YASSL_PURE_C
void* operator new(size_t sz, TaoCrypt::new_t)
{
void* ptr = malloc(sz ? sz : 1);
if (!ptr) abort();
return ptr;
}
void operator delete(void* ptr, TaoCrypt::new_t)
{
if (ptr) free(ptr);
}
void* operator new[](size_t sz, TaoCrypt::new_t nt)
{
return ::operator new(sz, nt);
}
void operator delete[](void* ptr, TaoCrypt::new_t nt)
{
::operator delete(ptr, nt);
}
/* uncomment to test
// make sure not using globals anywhere by forgetting to use overloaded
void* operator new(size_t sz);
void operator delete(void* ptr);
void* operator new[](size_t sz);
void operator delete[](void* ptr);
*/
namespace TaoCrypt {
new_t tc; // for library new
}
#ifdef __sun
// Handler for pure virtual functions
namespace __Crun {
void pure_error() {
assert(!"Aborted: pure virtual method called.");
}
}
#endif
#if defined(__ICC) || defined(__INTEL_COMPILER) || (__GNUC__ > 2)
extern "C" {
int __cxa_pure_virtual() {
assert(!"Aborted: pure virtual method called.");
return 0;
}
} // extern "C"
#endif
#endif // YASSL_PURE_C
namespace TaoCrypt {
inline void XorWords(word* r, const word* a, unsigned int n)
{
for (unsigned int i=0; i<n; i++)
r[i] ^= a[i];
}
void xorbuf(byte* buf, const byte* mask, unsigned int count)
{
if (((size_t)buf | (size_t)mask | count) % WORD_SIZE == 0)
XorWords((word *)buf, (const word *)mask, count/WORD_SIZE);
else
{
for (unsigned int i=0; i<count; i++)
buf[i] ^= mask[i];
}
}
unsigned int BytePrecision(word value)
{
unsigned int i;
for (i=sizeof(value); i; --i)
if (value >> (i-1)*8)
break;
return i;
}
unsigned int BitPrecision(word value)
{
if (!value)
return 0;
unsigned int l = 0,
h = 8 * sizeof(value);
while (h-l > 1)
{
unsigned int t = (l+h)/2;
if (value >> t)
l = t;
else
h = t;
}
return h;
}
word Crop(word value, unsigned int size)
{
if (size < 8*sizeof(value))
return (value & ((1L << size) - 1));
else
return value;
}
#ifdef TAOCRYPT_X86ASM_AVAILABLE
bool HaveCpuId()
{
#ifdef _MSC_VER
__try
{
__asm
{
mov eax, 0
cpuid
}
}
__except (1)
{
return false;
}
return true;
#else
word32 eax, ebx;
__asm__ __volatile
(
/* Put EFLAGS in eax and ebx */
"pushf;"
"pushf;"
"pop %0;"
"movl %0,%1;"
/* Flip the cpuid bit and store back in EFLAGS */
"xorl $0x200000,%0;"
"push %0;"
"popf;"
/* Read EFLAGS again */
"pushf;"
"pop %0;"
"popf"
: "=r" (eax), "=r" (ebx)
:
: "cc"
);
if (eax == ebx)
return false;
return true;
#endif
}
void CpuId(word32 input, word32 *output)
{
#ifdef __GNUC__
__asm__
(
// save ebx in case -fPIC is being used
"push %%ebx; cpuid; mov %%ebx, %%edi; pop %%ebx"
: "=a" (output[0]), "=D" (output[1]), "=c" (output[2]), "=d"(output[3])
: "a" (input)
);
#else
__asm
{
mov eax, input
cpuid
mov edi, output
mov [edi], eax
mov [edi+4], ebx
mov [edi+8], ecx
mov [edi+12], edx
}
#endif
}
bool IsPentium()
{
if (!HaveCpuId())
return false;
word32 cpuid[4];
CpuId(0, cpuid);
STL::swap(cpuid[2], cpuid[3]);
if (memcmp(cpuid+1, "GenuineIntel", 12) != 0)
return false;
CpuId(1, cpuid);
byte family = ((cpuid[0] >> 8) & 0xf);
if (family < 5)
return false;
return true;
}
static bool IsMmx()
{
if (!IsPentium())
return false;
word32 cpuid[4];
CpuId(1, cpuid);
if ((cpuid[3] & (1 << 23)) == 0)
return false;
return true;
}
bool isMMX = IsMmx();
#endif // TAOCRYPT_X86ASM_AVAILABLE
} // namespace
|