Debian Codegraph @

setup.py

raw symbols SCIP
1 #! /usr/bin/env python
2 #
3 # setup.py : Distutils setup script
4 #
5 # ===================================================================
6 # The contents of this file are dedicated to the public domain. To
7 # the extent that dedication to the public domain is not available,
8 # everyone is granted a worldwide, perpetual, royalty-free,
9 # non-exclusive license to exercise all rights associated with the
10 # contents of this file for any purpose whatsoever.
11 # No rights are reserved.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
17 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
21 # ===================================================================
22
23 from __future__ import print_function
24
25 try:
26 from setuptools import Extension, Command, setup
27 from setuptools.command.build_ext import build_ext
28 from setuptools.command.build_py import build_py
29 except ImportError:
30 from distutils.core import Extension, Command, setup
31 from distutils.command.build_ext import build_ext
32 from distutils.command.build_py import build_py
33
34 import re
35 import os
36 import sys
37 import shutil
38 import struct
39
40 sys.path.append(os.getcwd())
41
42 from compiler_opt import set_compiler_options
43
44
45 use_separate_namespace = os.path.isfile(".separate_namespace")
46
47 project_name = "pycryptodome"
48 package_root = "Crypto"
49 other_project = "pycryptodomex"
50 other_root = "Cryptodome"
51
52 if use_separate_namespace:
53 project_name, other_project = other_project, project_name
54 package_root, other_root = other_root, package_root
55
56 longdesc = """
57 PyCryptodome
58 ============
59
60 PyCryptodome is a self-contained Python package of low-level
61 cryptographic primitives.
62
63 It supports Python 2.7, Python 3.5 and newer, and PyPy.
64
65 You can install it with::
66
67 pip install THIS_PROJECT
68
69 All modules are installed under the ``THIS_ROOT`` package.
70
71 Check the OTHER_PROJECT_ project for the equivalent library that
72 works under the ``OTHER_ROOT`` package.
73
74 PyCryptodome is a fork of PyCrypto. It brings several enhancements
75 with respect to the last official version of PyCrypto (2.6.1),
76 for instance:
77
78 * Authenticated encryption modes (GCM, CCM, EAX, SIV, OCB)
79 * Accelerated AES on Intel platforms via AES-NI
80 * First class support for PyPy
81 * Elliptic curves cryptography (NIST P-curves; Ed25519, Ed448)
82 * Better and more compact API (`nonce` and `iv` attributes for ciphers,
83 automatic generation of random nonces and IVs, simplified CTR cipher mode,
84 and more)
85 * SHA-3 (including SHAKE XOFs) and BLAKE2 hash algorithms
86 * Salsa20 and ChaCha20 stream ciphers
87 * scrypt and HKDF
88 * Deterministic (EC)DSA and EdDSA
89 * Password-protected PKCS#8 key containers
90 * Shamir's Secret Sharing scheme
91 * Random numbers get sourced directly from the OS (and not from a CSPRNG in userspace)
92 * Simplified install process, including better support for Windows
93 * Cleaner RSA and DSA key generation (largely based on FIPS 186-4)
94 * Major clean ups and simplification of the code base
95
96 PyCryptodome is not a wrapper to a separate C library like *OpenSSL*.
97 To the largest possible extent, algorithms are implemented in pure Python.
98 Only the pieces that are extremely critical to performance (e.g. block ciphers)
99 are implemented as C extensions.
100
101 For more information, see the `homepage`_.
102
103 All the code can be downloaded from `GitHub`_.
104
105 .. _OTHER_PROJECT: https://pypi.python.org/pypi/OTHER_PROJECT
106 .. _`homepage`: http://www.pycryptodome.org
107 .. _GitHub: https://github.com/Legrandin/pycryptodome
108 """.replace("THIS_PROJECT", project_name).\
109 replace("THIS_ROOT", package_root).\
110 replace("OTHER_PROJECT", other_project).\
111 replace("OTHER_ROOT", other_root)
112
113
114 class PCTBuildExt (build_ext):
115
116 # Avoid linking Python's dynamic library
117 def get_libraries(self, ext):
118 return []
119
120
121 class PCTBuildPy(build_py):
122 def find_package_modules(self, package, package_dir, *args, **kwargs):
123 modules = build_py.find_package_modules(self, package, package_dir,
124 *args, **kwargs)
125
126 # Exclude certain modules
127 retval = []
128 for item in modules:
129 pkg, module = item[:2]
130 retval.append(item)
131 return retval
132
133
134 class TestCommand(Command):
135 "Run self-test"
136
137 # Long option name, short option name, description
138 user_options = [
139 ('skip-slow-tests', None, 'Skip slow tests'),
140 ('wycheproof-warnings', None, 'Show warnings from wycheproof tests'),
141 ('module=', 'm', 'Test a single module (e.g. Cipher, PublicKey)'),
142 ]
143
144 def initialize_options(self):
145 self.build_dir = None
146 self.skip_slow_tests = None
147 self.wycheproof_warnings = None
148 self.module = None
149
150 def finalize_options(self):
151 self.set_undefined_options('install', ('build_lib', 'build_dir'))
152 self.config = {'slow_tests': not self.skip_slow_tests,
153 'wycheproof_warnings': self.wycheproof_warnings}
154
155 def run(self):
156 # Run sub commands
157 for cmd_name in self.get_sub_commands():
158 self.run_command(cmd_name)
159
160 # Run SelfTest
161 old_path = sys.path[:]
162 self.announce("running self-tests on " + package_root)
163 try:
164 sys.path.insert(0, self.build_dir)
165
166 if use_separate_namespace:
167 from Cryptodome import SelfTest
168 from Cryptodome.Math import Numbers
169 else:
170 from Crypto import SelfTest
171 from Crypto.Math import Numbers
172
173 moduleObj = None
174 if self.module:
175 if self.module.count('.') == 0:
176 # Test a whole a sub-package
177 full_module = package_root + ".SelfTest." + self.module
178 module_name = self.module
179 else:
180 # Test only a module
181 # Assume only one dot is present
182 comps = self.module.split('.')
183 module_name = "test_" + comps[1]
184 full_module = package_root + ".SelfTest." + comps[0] + "." + module_name
185 # Import sub-package or module
186 moduleObj = __import__(full_module, globals(), locals(), module_name)
187
188 print(package_root + ".Math implementation:",
189 str(Numbers._implementation))
190
191 SelfTest.run(module=moduleObj, verbosity=self.verbose, stream=sys.stdout, config=self.config)
192 finally:
193 # Restore sys.path
194 sys.path[:] = old_path
195
196 # Run slower self-tests
197 self.announce("running extended self-tests")
198
199 sub_commands = [('build', None)]
200
201
202 def create_cryptodome_lib():
203 assert os.path.isdir("lib/Crypto")
204
205 try:
206 shutil.rmtree("lib/Cryptodome")
207 except OSError:
208 pass
209 for root_src, dirs, files in os.walk("lib/Crypto"):
210
211 root_dst, nr_repl = re.subn('Crypto', 'Cryptodome', root_src)
212 assert nr_repl == 1
213
214 for dir_name in dirs:
215 full_dir_name_dst = os.path.join(root_dst, dir_name)
216 if not os.path.exists(full_dir_name_dst):
217 os.makedirs(full_dir_name_dst)
218
219 for file_name in files:
220 full_file_name_src = os.path.join(root_src, file_name)
221 full_file_name_dst = os.path.join(root_dst, file_name)
222
223 print("Copying file %s to %s" % (full_file_name_src, full_file_name_dst))
224 shutil.copy2(full_file_name_src, full_file_name_dst)
225
226 if full_file_name_src.split(".")[-1] not in ("py", "pyi"):
227 if full_file_name_src != "py.typed":
228 continue
229
230 if sys.version_info[0] > 2:
231 extra_param = { "encoding": "utf-8" }
232 else:
233 extra_param = {}
234 with open(full_file_name_dst, "rt", **extra_param) as fd:
235 content = (fd.read().
236 replace("Crypto.", "Cryptodome.").
237 replace("Crypto ", "Cryptodome ").
238 replace("'Crypto'", "'Cryptodome'").
239 replace('"Crypto"', '"Cryptodome"'))
240 os.remove(full_file_name_dst)
241 with open(full_file_name_dst, "wt", **extra_param) as fd:
242 fd.write(content)
243
244
245 # Parameters for setup
246 packages = [
247 "Crypto",
248 "Crypto.Cipher",
249 "Crypto.Hash",
250 "Crypto.IO",
251 "Crypto.PublicKey",
252 "Crypto.Protocol",
253 "Crypto.Random",
254 "Crypto.Signature",
255 "Crypto.Util",
256 "Crypto.Math",
257 "Crypto.SelfTest",
258 "Crypto.SelfTest.Cipher",
259 "Crypto.SelfTest.Hash",
260 "Crypto.SelfTest.IO",
261 "Crypto.SelfTest.Protocol",
262 "Crypto.SelfTest.PublicKey",
263 "Crypto.SelfTest.Random",
264 "Crypto.SelfTest.Signature",
265 "Crypto.SelfTest.Util",
266 "Crypto.SelfTest.Math",
267 ]
268 package_data = {
269 "Crypto" : [ "py.typed", "*.pyi" ],
270 "Crypto.Cipher" : [ "*.pyi" ],
271 "Crypto.Hash" : [ "*.pyi" ],
272 "Crypto.Math" : [ "*.pyi" ],
273 "Crypto.Protocol" : [ "*.pyi" ],
274 "Crypto.PublicKey" : [ "*.pyi" ],
275 "Crypto.Random" : [ "*.pyi" ],
276 "Crypto.Signature" : [ "*.pyi" ],
277 "Crypto.IO" : [ "*.pyi" ],
278 "Crypto.Util" : [ "*.pyi" ],
279 }
280
281 ext_modules = [
282 # Hash functions
283 Extension("Crypto.Hash._MD2",
284 include_dirs=['src/'],
285 sources=["src/MD2.c"],
286 py_limited_api=True),
287 Extension("Crypto.Hash._MD4",
288 include_dirs=['src/'],
289 sources=["src/MD4.c"],
290 py_limited_api=True),
291 Extension("Crypto.Hash._MD5",
292 include_dirs=['src/'],
293 sources=["src/MD5.c"],
294 py_limited_api=True),
295 Extension("Crypto.Hash._SHA1",
296 include_dirs=['src/'],
297 sources=["src/SHA1.c"],
298 py_limited_api=True),
299 Extension("Crypto.Hash._SHA256",
300 include_dirs=['src/'],
301 sources=["src/SHA256.c"],
302 py_limited_api=True),
303 Extension("Crypto.Hash._SHA224",
304 include_dirs=['src/'],
305 sources=["src/SHA224.c"],
306 py_limited_api=True),
307 Extension("Crypto.Hash._SHA384",
308 include_dirs=['src/'],
309 sources=["src/SHA384.c"],
310 py_limited_api=True),
311 Extension("Crypto.Hash._SHA512",
312 include_dirs=['src/'],
313 sources=["src/SHA512.c"],
314 py_limited_api=True),
315 Extension("Crypto.Hash._RIPEMD160",
316 include_dirs=['src/'],
317 sources=["src/RIPEMD160.c"],
318 py_limited_api=True),
319 Extension("Crypto.Hash._keccak",
320 include_dirs=['src/'],
321 sources=["src/keccak.c"],
322 py_limited_api=True),
323 Extension("Crypto.Hash._BLAKE2b",
324 include_dirs=['src/'],
325 sources=["src/blake2b.c"],
326 py_limited_api=True),
327 Extension("Crypto.Hash._BLAKE2s",
328 include_dirs=['src/'],
329 sources=["src/blake2s.c"],
330 py_limited_api=True),
331 Extension("Crypto.Hash._ghash_portable",
332 include_dirs=['src/'],
333 sources=['src/ghash_portable.c'],
334 py_limited_api=True),
335 Extension("Crypto.Hash._ghash_clmul",
336 include_dirs=['src/'],
337 sources=['src/ghash_clmul.c'],
338 py_limited_api=True),
339
340 # MACs
341 Extension("Crypto.Hash._poly1305",
342 include_dirs=['src/'],
343 sources=["src/poly1305.c"],
344 py_limited_api=True),
345
346 # Block encryption algorithms
347 Extension("Crypto.Cipher._raw_aes",
348 include_dirs=['src/'],
349 sources=["src/AES.c"],
350 py_limited_api=True),
351 Extension("Crypto.Cipher._raw_aesni",
352 include_dirs=['src/'],
353 sources=["src/AESNI.c"],
354 py_limited_api=True),
355 Extension("Crypto.Cipher._raw_arc2",
356 include_dirs=['src/'],
357 sources=["src/ARC2.c"],
358 py_limited_api=True),
359 Extension("Crypto.Cipher._raw_blowfish",
360 include_dirs=['src/'],
361 sources=["src/blowfish.c"],
362 py_limited_api=True),
363 Extension("Crypto.Cipher._raw_eksblowfish",
364 include_dirs=['src/'],
365 sources=["src/blowfish_eks.c"],
366 py_limited_api=True),
367 Extension("Crypto.Cipher._raw_cast",
368 include_dirs=['src/'],
369 sources=["src/CAST.c"],
370 py_limited_api=True),
371 Extension("Crypto.Cipher._raw_des",
372 include_dirs=['src/', 'src/libtom/'],
373 sources=["src/DES.c"],
374 py_limited_api=True),
375 Extension("Crypto.Cipher._raw_des3",
376 include_dirs=['src/', 'src/libtom/'],
377 sources=["src/DES3.c"],
378 py_limited_api=True),
379 Extension("Crypto.Util._cpuid_c",
380 include_dirs=['src/'],
381 sources=['src/cpuid.c'],
382 py_limited_api=True),
383
384 Extension("Crypto.Cipher._pkcs1_decode",
385 include_dirs=['src/'],
386 sources=['src/pkcs1_decode.c'],
387 py_limited_api=True),
388
389 # Chaining modes
390 Extension("Crypto.Cipher._raw_ecb",
391 include_dirs=['src/'],
392 sources=["src/raw_ecb.c"],
393 py_limited_api=True),
394 Extension("Crypto.Cipher._raw_cbc",
395 include_dirs=['src/'],
396 sources=["src/raw_cbc.c"],
397 py_limited_api=True),
398 Extension("Crypto.Cipher._raw_cfb",
399 include_dirs=['src/'],
400 sources=["src/raw_cfb.c"],
401 py_limited_api=True),
402 Extension("Crypto.Cipher._raw_ofb",
403 include_dirs=['src/'],
404 sources=["src/raw_ofb.c"],
405 py_limited_api=True),
406 Extension("Crypto.Cipher._raw_ctr",
407 include_dirs=['src/'],
408 sources=["src/raw_ctr.c"],
409 py_limited_api=True),
410 Extension("Crypto.Cipher._raw_ocb",
411 sources=["src/raw_ocb.c"],
412 py_limited_api=True),
413
414 # Stream ciphers
415 Extension("Crypto.Cipher._ARC4",
416 include_dirs=['src/'],
417 sources=["src/ARC4.c"],
418 py_limited_api=True),
419 Extension("Crypto.Cipher._Salsa20",
420 include_dirs=['src/', 'src/libtom/'],
421 sources=["src/Salsa20.c"],
422 py_limited_api=True),
423 Extension("Crypto.Cipher._chacha20",
424 include_dirs=['src/'],
425 sources=["src/chacha20.c"],
426 py_limited_api=True),
427
428 # Others
429 Extension("Crypto.Protocol._scrypt",
430 include_dirs=['src/'],
431 sources=["src/scrypt.c"],
432 py_limited_api=True),
433
434 # Utility modules
435 Extension("Crypto.Util._strxor",
436 include_dirs=['src/'],
437 sources=['src/strxor.c'],
438 py_limited_api=True),
439
440 # ECC
441 Extension("Crypto.PublicKey._ec_ws",
442 include_dirs=['src/'],
443 sources=['src/ec_ws.c',
444 'src/mont.c', 'src/p256_table.c', 'src/p384_table.c',
445 'src/p521_table.c'],
446 py_limited_api=True,
447 ),
448 Extension("Crypto.PublicKey._x25519",
449 include_dirs=['src/'],
450 sources=['src/x25519.c'],
451 py_limited_api=True,
452 ),
453 Extension("Crypto.PublicKey._ed25519",
454 include_dirs=['src/'],
455 sources=['src/ed25519.c'],
456 py_limited_api=True,
457 ),
458 Extension("Crypto.PublicKey._ed448",
459 include_dirs=['src/'],
460 sources=['src/ed448.c', 'src/mont1.c'],
461 py_limited_api=True,
462 ),
463
464 # Math
465 Extension("Crypto.Math._modexp",
466 include_dirs=['src/'],
467 sources=['src/modexp.c', 'src/mont2.c'],
468 py_limited_api=True,
469 ),
470 ]
471
472 if use_separate_namespace:
473
474 # Fix-up setup information
475 for i in range(len(packages)):
476 packages[i] = packages[i].replace("Crypto", "Cryptodome")
477 new_package_data = {}
478 for k, v in package_data.items():
479 new_package_data[k.replace("Crypto", "Cryptodome")] = v
480 package_data = new_package_data
481 for ext in ext_modules:
482 ext.name = ext.name.replace("Crypto", "Cryptodome")
483
484 # Recreate lib/Cryptodome from scratch, unless it is the only
485 # directory available
486 if os.path.isdir("lib/Crypto"):
487 create_cryptodome_lib()
488
489 # Add compiler specific options.
490 set_compiler_options(package_root, ext_modules)
491
492 # By doing this we need to change version information in a single file
493 with open(os.path.join("lib", package_root, "__init__.py")) as init_root:
494 for line in init_root:
495 if line.startswith("version_info"):
496 version_tuple = eval(line.split("=")[1])
497
498 version_string = ".".join([str(x) for x in version_tuple])
499
500 setup(
501 name=project_name,
502 version=version_string,
503 description="Cryptographic library for Python",
504 long_description=longdesc,
505 author="Helder Eijs",
506 author_email="[email protected]",
507 url="https://www.pycryptodome.org",
508 platforms='Posix; MacOS X; Windows',
509 zip_safe=False,
510 python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*',
511 classifiers=[
512 'Development Status :: 5 - Production/Stable',
513 'License :: OSI Approved :: BSD License',
514 'License :: OSI Approved :: Apache Software License',
515 'License :: Public Domain',
516 'Intended Audience :: Developers',
517 'Operating System :: Unix',
518 'Operating System :: Microsoft :: Windows',
519 'Operating System :: MacOS :: MacOS X',
520 'Topic :: Security :: Cryptography',
521 'Programming Language :: Python :: 2',
522 'Programming Language :: Python :: 2.7',
523 'Programming Language :: Python :: 3',
524 'Programming Language :: Python :: 3.5',
525 'Programming Language :: Python :: 3.6',
526 'Programming Language :: Python :: 3.7',
527 'Programming Language :: Python :: 3.8',
528 'Programming Language :: Python :: 3.9',
529 'Programming Language :: Python :: 3.10',
530 'Programming Language :: Python :: 3.11',
531 'Programming Language :: Python :: 3.12',
532 ],
533 license="BSD, Public Domain",
534 packages=packages,
535 package_dir={"": "lib"},
536 package_data=package_data,
537 cmdclass={
538 'build_ext': PCTBuildExt,
539 'build_py': PCTBuildPy,
540 'test': TestCommand,
541 },
542 ext_modules=ext_modules,
543 )