| 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 |
import os |
| 33 |
import sys |
| 34 |
import struct |
| 35 |
import distutils |
| 36 |
from distutils import ccompiler |
| 37 |
from distutils.errors import CCompilerError |
| 38 |
|
| 39 |
|
| 40 |
def test_compilation(program, extra_cc_options=None, extra_libraries=None, |
| 41 |
msg=''): |
| 42 |
"""Test if a certain C program can be compiled.""" |
| 43 |
|
| 44 |
|
| 45 |
if not os.path.exists("build"): |
| 46 |
os.makedirs("build") |
| 47 |
fname = os.path.join("build", "test1.c") |
| 48 |
f = open(fname, 'w') |
| 49 |
f.write(program) |
| 50 |
f.close() |
| 51 |
|
| 52 |
|
| 53 |
oname = os.path.join("build", "test1.out") |
| 54 |
|
| 55 |
debug = bool(os.environ.get('PYCRYPTODOME_DEBUG', None)) |
| 56 |
|
| 57 |
if msg: |
| 58 |
print("Testing support for %s" % msg) |
| 59 |
if not (debug or os.name == 'nt'): |
| 60 |
old_stdout = os.dup(sys.stdout.fileno()) |
| 61 |
old_stderr = os.dup(sys.stderr.fileno()) |
| 62 |
dev_null = open(os.devnull, "w") |
| 63 |
os.dup2(dev_null.fileno(), sys.stdout.fileno()) |
| 64 |
os.dup2(dev_null.fileno(), sys.stderr.fileno()) |
| 65 |
|
| 66 |
objects = [] |
| 67 |
try: |
| 68 |
compiler = ccompiler.new_compiler() |
| 69 |
distutils.sysconfig.customize_compiler(compiler) |
| 70 |
|
| 71 |
if compiler.compiler_type in ['msvc']: |
| 72 |
|
| 73 |
|
| 74 |
extra_linker_options = ["/MANIFEST"] |
| 75 |
else: |
| 76 |
extra_linker_options = [] |
| 77 |
|
| 78 |
|
| 79 |
if compiler.compiler_type in ['unix']: |
| 80 |
compiler.set_executables(linker_exe=compiler.compiler) |
| 81 |
|
| 82 |
objects = compiler.compile([fname], extra_postargs=extra_cc_options) |
| 83 |
compiler.link_executable(objects, oname, libraries=extra_libraries, |
| 84 |
extra_preargs=extra_linker_options) |
| 85 |
result = True |
| 86 |
except (CCompilerError, OSError): |
| 87 |
result = False |
| 88 |
for f in objects + [fname, oname]: |
| 89 |
try: |
| 90 |
os.remove(f) |
| 91 |
except OSError: |
| 92 |
pass |
| 93 |
|
| 94 |
|
| 95 |
if not (debug or os.name == 'nt'): |
| 96 |
if old_stdout is not None: |
| 97 |
os.dup2(old_stdout, sys.stdout.fileno()) |
| 98 |
if old_stderr is not None: |
| 99 |
os.dup2(old_stderr, sys.stderr.fileno()) |
| 100 |
if dev_null is not None: |
| 101 |
dev_null.close() |
| 102 |
if msg: |
| 103 |
if result: |
| 104 |
x = "" |
| 105 |
else: |
| 106 |
x = " not" |
| 107 |
print("Target does%s support %s" % (x, msg)) |
| 108 |
|
| 109 |
return result |
| 110 |
|
| 111 |
|
| 112 |
def has_stdint_h(): |
| 113 |
source = """ |
| 114 |
#include <stdint.h> |
| 115 |
int main(void) { |
| 116 |
uint32_t u; |
| 117 |
u = 0; |
| 118 |
return u + 2; |
| 119 |
} |
| 120 |
""" |
| 121 |
return test_compilation(source, msg="stdint.h header") |
| 122 |
|
| 123 |
|
| 124 |
def compiler_supports_uint128(): |
| 125 |
source = """ |
| 126 |
int main(void) |
| 127 |
{ |
| 128 |
__uint128_t x; |
| 129 |
return 0; |
| 130 |
} |
| 131 |
""" |
| 132 |
return test_compilation(source, msg="128-bit integer") |
| 133 |
|
| 134 |
|
| 135 |
def compiler_has_intrin_h(): |
| 136 |
|
| 137 |
source = """ |
| 138 |
#include <intrin.h> |
| 139 |
int main(void) |
| 140 |
{ |
| 141 |
int a, b[4]; |
| 142 |
__cpuid(b, a); |
| 143 |
return a; |
| 144 |
} |
| 145 |
""" |
| 146 |
return test_compilation(source, msg="intrin.h header") |
| 147 |
|
| 148 |
|
| 149 |
def compiler_has_cpuid_h(): |
| 150 |
|
| 151 |
source = """ |
| 152 |
#include <cpuid.h> |
| 153 |
int main(void) |
| 154 |
{ |
| 155 |
unsigned int eax, ebx, ecx, edx; |
| 156 |
__get_cpuid(1, &eax, &ebx, &ecx, &edx); |
| 157 |
return eax; |
| 158 |
} |
| 159 |
""" |
| 160 |
return test_compilation(source, msg="cpuid.h header") |
| 161 |
|
| 162 |
|
| 163 |
def compiler_supports_aesni(): |
| 164 |
source = """ |
| 165 |
#include <wmmintrin.h> |
| 166 |
#include <string.h> |
| 167 |
__m128i f(__m128i x, __m128i y) { |
| 168 |
return _mm_aesenc_si128(x, y); |
| 169 |
} |
| 170 |
int main(void) { |
| 171 |
int ret; |
| 172 |
__m128i x; |
| 173 |
memset(&x, 0, sizeof(x)); |
| 174 |
x = f(x, x); |
| 175 |
memcpy(&ret, &x, sizeof(ret)); |
| 176 |
return ret; |
| 177 |
} |
| 178 |
""" |
| 179 |
|
| 180 |
if test_compilation(source): |
| 181 |
return {'extra_cc_options': [], 'extra_macros': []} |
| 182 |
|
| 183 |
if test_compilation(source, extra_cc_options=['-maes'], msg='AESNI intrinsics'): |
| 184 |
return {'extra_cc_options': ['-maes'], 'extra_macros': []} |
| 185 |
|
| 186 |
return False |
| 187 |
|
| 188 |
|
| 189 |
def compiler_supports_clmul(): |
| 190 |
result = {'extra_cc_options': [], 'extra_macros' : ['HAVE_WMMINTRIN_H', 'HAVE_TMMINTRIN_H']} |
| 191 |
|
| 192 |
source = """ |
| 193 |
#include <wmmintrin.h> |
| 194 |
#include <tmmintrin.h> |
| 195 |
|
| 196 |
__m128i f(__m128i x, __m128i y) { |
| 197 |
return _mm_clmulepi64_si128(x, y, 0x00); |
| 198 |
} |
| 199 |
|
| 200 |
__m128i g(__m128i a) { |
| 201 |
__m128i mask; |
| 202 |
|
| 203 |
mask = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); |
| 204 |
return _mm_shuffle_epi8(a, mask); |
| 205 |
} |
| 206 |
|
| 207 |
int main(void) { |
| 208 |
return 0; |
| 209 |
} |
| 210 |
""" |
| 211 |
|
| 212 |
if test_compilation(source): |
| 213 |
return result |
| 214 |
|
| 215 |
if test_compilation(source, extra_cc_options=['-mpclmul', '-mssse3'], msg='CLMUL intrinsics'): |
| 216 |
result['extra_cc_options'].extend(['-mpclmul', '-mssse3']) |
| 217 |
return result |
| 218 |
|
| 219 |
return False |
| 220 |
|
| 221 |
|
| 222 |
def compiler_has_posix_memalign(): |
| 223 |
source = """ |
| 224 |
#include <stdlib.h> |
| 225 |
int main(void) { |
| 226 |
void *new_mem; |
| 227 |
int res; |
| 228 |
res = posix_memalign((void**)&new_mem, 16, 101); |
| 229 |
return res == 0; |
| 230 |
} |
| 231 |
""" |
| 232 |
return test_compilation(source, msg="posix_memalign") |
| 233 |
|
| 234 |
|
| 235 |
def compiler_has_memalign(): |
| 236 |
source = """ |
| 237 |
#include <malloc.h> |
| 238 |
int main(void) { |
| 239 |
void *p; |
| 240 |
p = memalign(16, 101); |
| 241 |
return p != (void*)0; |
| 242 |
} |
| 243 |
""" |
| 244 |
return test_compilation(source, msg="memalign") |
| 245 |
|
| 246 |
|
| 247 |
def compiler_is_clang(): |
| 248 |
source = """ |
| 249 |
#if !defined(__clang__) |
| 250 |
#error Not clang |
| 251 |
#endif |
| 252 |
int main(void) |
| 253 |
{ |
| 254 |
return 0; |
| 255 |
} |
| 256 |
""" |
| 257 |
return test_compilation(source, msg="clang") |
| 258 |
|
| 259 |
|
| 260 |
def compiler_is_gcc(extra_cc_options=[]): |
| 261 |
source = """ |
| 262 |
#if defined(__clang__) || !defined(__GNUC__) |
| 263 |
#error Not GCC |
| 264 |
#endif |
| 265 |
int main(void) |
| 266 |
{ |
| 267 |
return 0; |
| 268 |
}""" |
| 269 |
return test_compilation(source, |
| 270 |
msg="gcc", |
| 271 |
extra_cc_options=extra_cc_options) |
| 272 |
|
| 273 |
|
| 274 |
def compiler_supports_sse2(): |
| 275 |
source_template = """ |
| 276 |
%s |
| 277 |
int main(void) |
| 278 |
{ |
| 279 |
__m128i r0; |
| 280 |
int mask; |
| 281 |
r0 = _mm_set1_epi32(0); |
| 282 |
mask = _mm_movemask_epi8(r0); |
| 283 |
return mask; |
| 284 |
} |
| 285 |
""" |
| 286 |
|
| 287 |
source_intrin_h = source_template % "#include <intrin.h>" |
| 288 |
source_x86intrin_h = source_template % "#include <x86intrin.h>" |
| 289 |
source_xemmintrin_h = source_template % "#include <xmmintrin.h>\n#include <emmintrin.h>" |
| 290 |
|
| 291 |
system_bits = 8 * struct.calcsize("P") |
| 292 |
|
| 293 |
result = None |
| 294 |
if test_compilation(source_intrin_h, msg="SSE2(intrin.h)"): |
| 295 |
result = {'extra_cc_options': [], 'extra_macros': ['HAVE_INTRIN_H', 'USE_SSE2']} |
| 296 |
elif test_compilation(source_x86intrin_h, extra_cc_options=['-msse2'], msg="SSE2(x86intrin.h)"): |
| 297 |
result = {'extra_cc_options': ['-msse2'], 'extra_macros': ['HAVE_X86INTRIN_H', 'USE_SSE2']} |
| 298 |
elif test_compilation(source_xemmintrin_h, extra_cc_options=['-msse2'], msg="SSE2(emmintrin.h)"): |
| 299 |
result = {'extra_cc_options': ['-msse2'], 'extra_macros': ['HAVE_EMMINTRIN_H', 'USE_SSE2']} |
| 300 |
else: |
| 301 |
result = False |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
if result and system_bits == 32 and compiler_is_gcc(extra_cc_options=['-mstackrealign']): |
| 308 |
result['extra_cc_options'].append('-mstackrealign') |
| 309 |
|
| 310 |
return result |
| 311 |
|
| 312 |
|
| 313 |
def remove_extension(extensions, name): |
| 314 |
idxs = [i for i, x in enumerate(extensions) if x.name == name] |
| 315 |
if len(idxs) != 1: |
| 316 |
raise ValueError("There is no or there are multiple extensions named '%s'" % name) |
| 317 |
del extensions[idxs[0]] |
| 318 |
|
| 319 |
|
| 320 |
def set_compiler_options(package_root, extensions): |
| 321 |
"""Environment specific settings for extension modules. |
| 322 |
|
| 323 |
This function modifies how each module gets compiled, to |
| 324 |
match the capabilities of the platform. |
| 325 |
Also, it removes existing modules when not supported, such as: |
| 326 |
- AESNI |
| 327 |
- CLMUL |
| 328 |
""" |
| 329 |
|
| 330 |
extra_cc_options = [] |
| 331 |
extra_macros = [] |
| 332 |
|
| 333 |
clang = compiler_is_clang() |
| 334 |
gcc = compiler_is_gcc() |
| 335 |
|
| 336 |
if has_stdint_h(): |
| 337 |
extra_macros.append(("HAVE_STDINT_H", None)) |
| 338 |
|
| 339 |
|
| 340 |
extra_macros.append(("PYCRYPTO_" + sys.byteorder.upper() + "_ENDIAN", None)) |
| 341 |
|
| 342 |
|
| 343 |
system_bits = 8 * struct.calcsize("P") |
| 344 |
extra_macros.append(("SYS_BITS", str(system_bits))) |
| 345 |
|
| 346 |
|
| 347 |
extra_macros.append(("LTC_NO_ASM", None)) |
| 348 |
|
| 349 |
|
| 350 |
if compiler_supports_uint128(): |
| 351 |
extra_macros.append(("HAVE_UINT128", None)) |
| 352 |
|
| 353 |
|
| 354 |
cpuid_h_present = compiler_has_cpuid_h() |
| 355 |
if cpuid_h_present: |
| 356 |
extra_macros.append(("HAVE_CPUID_H", None)) |
| 357 |
intrin_h_present = compiler_has_intrin_h() |
| 358 |
if intrin_h_present: |
| 359 |
extra_macros.append(("HAVE_INTRIN_H", None)) |
| 360 |
|
| 361 |
|
| 362 |
if compiler_has_posix_memalign(): |
| 363 |
extra_macros.append(("HAVE_POSIX_MEMALIGN", None)) |
| 364 |
elif compiler_has_memalign(): |
| 365 |
extra_macros.append(("HAVE_MEMALIGN", None)) |
| 366 |
|
| 367 |
|
| 368 |
sse2_result = compiler_supports_sse2() |
| 369 |
if sse2_result: |
| 370 |
extra_cc_options.extend(sse2_result['extra_cc_options']) |
| 371 |
for macro in sse2_result['extra_macros']: |
| 372 |
extra_macros.append((macro, None)) |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
aesni_result = (cpuid_h_present or intrin_h_present) and compiler_supports_aesni() |
| 378 |
aesni_mod_name = package_root + ".Cipher._raw_aesni" |
| 379 |
if aesni_result: |
| 380 |
print("Compiling support for AESNI instructions") |
| 381 |
aes_mods = [x for x in extensions if x.name == aesni_mod_name] |
| 382 |
for x in aes_mods: |
| 383 |
x.extra_compile_args.extend(aesni_result['extra_cc_options']) |
| 384 |
for macro in aesni_result['extra_macros']: |
| 385 |
x.define_macros.append((macro, None)) |
| 386 |
else: |
| 387 |
print("Warning: compiler does not support AESNI instructions") |
| 388 |
remove_extension(extensions, aesni_mod_name) |
| 389 |
|
| 390 |
|
| 391 |
clmul_result = (cpuid_h_present or intrin_h_present) and compiler_supports_clmul() |
| 392 |
clmul_mod_name = package_root + ".Hash._ghash_clmul" |
| 393 |
if clmul_result: |
| 394 |
print("Compiling support for CLMUL instructions") |
| 395 |
clmul_mods = [x for x in extensions if x.name == clmul_mod_name] |
| 396 |
for x in clmul_mods: |
| 397 |
x.extra_compile_args.extend(clmul_result['extra_cc_options']) |
| 398 |
for macro in clmul_result['extra_macros']: |
| 399 |
x.define_macros.append((macro, None)) |
| 400 |
else: |
| 401 |
print("Warning: compiler does not support CLMUL instructions") |
| 402 |
remove_extension(extensions, clmul_mod_name) |
| 403 |
|
| 404 |
for x in extensions: |
| 405 |
x.extra_compile_args.extend(extra_cc_options) |
| 406 |
x.define_macros.extend(extra_macros) |