pax_global_header00006660000000000000000000000064147452371300014520gustar00rootroot0000000000000052 comment=c77c27109bdcac071974ec15c83c95167a3bf9aa python-ntruprime-20241021.4/000077500000000000000000000000001474523713000154635ustar00rootroot00000000000000python-ntruprime-20241021.4/LICENSE000066400000000000000000000156101474523713000164730ustar00rootroot00000000000000Creative Commons Legal Code CC0 1.0 Universal CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER. Statement of Purpose The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. 1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: i. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; ii. moral rights retained by the original author(s) and/or performer(s); iii. publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; iv. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; v. rights protecting the extraction, dissemination, use and reuse of data in a Work; vi. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and vii. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. 2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. 3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. 4. Limitations and Disclaimers. a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. python-ntruprime-20241021.4/README.md000066400000000000000000000013601474523713000167420ustar00rootroot00000000000000Python wrapper around implementation of the Streamlined NTRU Prime cryptosystem. To access the Python functions provided by ntruprime, import the library (for, e.g., sntrup1277): from ntruprime import sntrup1277 To generate a key pair: pk,sk = sntrup1277.keypair() To generate a ciphertext c encapsulating a randomly generated session key k: c,k = sntrup1277.enc(pk) To recover a session key from a ciphertext: k = sntrup1277.dec(c,sk) As a larger example, the following test script creates a key pair, creates a ciphertext and session key, and then recovers the session key from the ciphertext: import ntruprime kem = ntruprime.sntrup1277 pk,sk = kem.keypair() c,k = kem.enc(pk) assert k == kem.dec(c,sk) python-ntruprime-20241021.4/autogen/000077500000000000000000000000001474523713000171255ustar00rootroot00000000000000python-ntruprime-20241021.4/autogen/init000077500000000000000000000006241474523713000200200ustar00rootroot00000000000000#!/usr/bin/env python3 project='ntruprime' primitives = ['sntrup653', 'sntrup761', 'sntrup857', 'sntrup953', 'sntrup1013', 'sntrup1277'] helpstr = "'''\n" with open('README.md') as f: helpstr += f.read() helpstr += "'''\n" with open(f'src/{project}/__init__.py', 'w') as f: f.write(helpstr) f.write('\n') for primitive in primitives: f.write(f'from .kem import {primitive}\n') python-ntruprime-20241021.4/autogen/kem000077500000000000000000000066671474523713000176460ustar00rootroot00000000000000#!/usr/bin/env python3 project='ntruprime' primitives = { 'sntrup653': {'name': 'sntrup653', 'pkbytes': 994, 'skbytes': 1518, 'cbytes': 897}, 'sntrup761': {'name': 'sntrup761', 'pkbytes': 1158, 'skbytes': 1763, 'cbytes': 1039}, 'sntrup857': {'name': 'sntrup857', 'pkbytes': 1322, 'skbytes': 1999, 'cbytes': 1184}, 'sntrup953': {'name': 'sntrup953', 'pkbytes': 1505, 'skbytes': 2254, 'cbytes': 1349}, 'sntrup1013': {'name': 'sntrup1013', 'pkbytes': 1623, 'skbytes': 2417, 'cbytes': 1455}, 'sntrup1277': {'name': 'sntrup1277', 'pkbytes': 2067, 'skbytes': 3059, 'cbytes': 1847}, } kem="""from typing import Tuple as _Tuple import ctypes as _ct from ._lib import _lib, _check_input class _KEM: def __init__(self) -> None: ''' ''' self._c_keypair = getattr(_lib, '%s_keypair' % self._prefix) self._c_keypair.argtypes = [_ct.c_char_p, _ct.c_char_p] self._c_keypair.restype = None self._c_enc = getattr(_lib, '%s_enc' % self._prefix) self._c_enc.argtypes = [_ct.c_char_p, _ct.c_char_p, _ct.c_char_p] self._c_enc.restype = None self._c_dec = getattr(_lib, '%s_dec' % self._prefix) self._c_dec.argtypes = [_ct.c_char_p, _ct.c_char_p, _ct.c_char_p] self._c_dec.restype = None def keypair(self) -> _Tuple[bytes, bytes]: ''' Keypair - randomly generates secret key 'sk' and corresponding public key 'pk'. Returns: pk (bytes): public key sk (bytes): secret key ''' pk = _ct.create_string_buffer(self.PUBLICKEYBYTES) sk = _ct.create_string_buffer(self.SECRETKEYBYTES) self._c_keypair(pk, sk) return pk.raw, sk.raw def enc(self, pk: bytes) -> _Tuple[bytes, bytes]: ''' Encapsulation - randomly generates a ciphertext 'c' and the corresponding session key 'k' given Alice's public key 'pk'. Parameters: pk (bytes): public key Returns: c (bytes): ciphertext k (bytes): session key ''' _check_input(pk, self.PUBLICKEYBYTES, 'pk') c = _ct.create_string_buffer(self.CIPHERTEXTBYTES) k = _ct.create_string_buffer(self.BYTES) pk = _ct.create_string_buffer(pk) self._c_enc(c, k, pk) return c.raw, k.raw def dec(self, c: bytes, sk: bytes) -> bytes: ''' Decapsulation - given Alice's secret key 'sk' computes the session key 'k' corresponding to a ciphertext 'c'. Parameters: c (bytes): ciphertext sk (bytes): secret key Returns: k (bytes): session key ''' _check_input(c, self.CIPHERTEXTBYTES, 'c') _check_input(sk, self.SECRETKEYBYTES, 'sk') k = _ct.create_string_buffer(self.BYTES) c = _ct.create_string_buffer(c) sk = _ct.create_string_buffer(sk) self._c_dec(k, c, sk) return k.raw """ with open(f'src/{project}/kem.py', 'w') as f: f.write(kem) for primitive in primitives: f.write('\n\n') f.write(f'class {primitive}(_KEM):\n') f.write(f' PUBLICKEYBYTES = {primitives[primitive]["pkbytes"]}\n') f.write(f' SECRETKEYBYTES = {primitives[primitive]["skbytes"]}\n') f.write(f' CIPHERTEXTBYTES = {primitives[primitive]["cbytes"]}\n') f.write(f' BYTES = 32\n') f.write(f' _prefix = "{project}_kem_{primitives[primitive]["name"]}"\n\n\n') f.write(f'{primitive} = {primitive}()\n') python-ntruprime-20241021.4/autogen/lib000066400000000000000000000011231474523713000176130ustar00rootroot00000000000000#!/usr/bin/env python3 project='ntruprime' lib="""from ctypes.util import find_library as _find_library from ctypes import CDLL as _CDLL _libname = _find_library('%s') if _libname is None: raise FileNotFoundError("unable to locate library '%s'") _lib = _CDLL(_libname) def _check_input(x, xlen, name): if not isinstance(x, bytes): raise TypeError(f'{name} must be bytes') if xlen != -1 and xlen != len(x): raise ValueError(f'{name} length must have exactly {xlen} bytes') """ % (project, project) with open(f'src/{project}/_lib.py', 'w') as f: f.write(lib) python-ntruprime-20241021.4/autogen/tests000077500000000000000000000005631474523713000202210ustar00rootroot00000000000000#!/usr/bin/env python3 for size in '653','761','857','953','1013','1277': with open(f'tests/test_sntrup{size}.py', 'w') as f: template = f'''from ntruprime import sntrup{size} def test_sntrup{size}c(): pk, sk = sntrup{size}.keypair() c, k1 = sntrup{size}.enc(pk) k2 = sntrup{size}.dec(c, sk) assert (k1 == k2) ''' f.write(template) python-ntruprime-20241021.4/pyproject.toml000066400000000000000000000007351474523713000204040ustar00rootroot00000000000000[build-system] requires = ['setuptools'] build-backend = 'setuptools.build_meta' [project] name = 'ntruprime' description = 'Python wrapper around implementation of the Streamlined NTRU Prime cryptosystems' readme = 'README.md' requires-python = '>=3.7' license = {file = 'LICENSE'} authors = [ {name = 'Jan Mojžíš', email = 'jan.mojzis@gmail.com'}, ] version = '20241021.4' [tool.pytest.ini_options] minversion = '6.0' addopts = '-ra -v' testpaths = [ 'tests', ] python-ntruprime-20241021.4/src/000077500000000000000000000000001474523713000162525ustar00rootroot00000000000000python-ntruprime-20241021.4/src/ntruprime/000077500000000000000000000000001474523713000202775ustar00rootroot00000000000000python-ntruprime-20241021.4/src/ntruprime/__init__.py000066400000000000000000000016351474523713000224150ustar00rootroot00000000000000''' Python wrapper around implementation of the Streamlined NTRU Prime cryptosystem. To access the Python functions provided by ntruprime, import the library (for, e.g., sntrup1277): from ntruprime import sntrup1277 To generate a key pair: pk,sk = sntrup1277.keypair() To generate a ciphertext c encapsulating a randomly generated session key k: c,k = sntrup1277.enc(pk) To recover a session key from a ciphertext: k = sntrup1277.dec(c,sk) As a larger example, the following test script creates a key pair, creates a ciphertext and session key, and then recovers the session key from the ciphertext: import ntruprime kem = ntruprime.sntrup1277 pk,sk = kem.keypair() c,k = kem.enc(pk) assert k == kem.dec(c,sk) ''' from .kem import sntrup653 from .kem import sntrup761 from .kem import sntrup857 from .kem import sntrup953 from .kem import sntrup1013 from .kem import sntrup1277 python-ntruprime-20241021.4/src/ntruprime/_lib.py000066400000000000000000000007231474523713000215600ustar00rootroot00000000000000from ctypes.util import find_library as _find_library from ctypes import CDLL as _CDLL _libname = _find_library('ntruprime') if _libname is None: raise FileNotFoundError("unable to locate library 'ntruprime'") _lib = _CDLL(_libname) def _check_input(x, xlen, name): if not isinstance(x, bytes): raise TypeError(f'{name} must be bytes') if xlen != -1 and xlen != len(x): raise ValueError(f'{name} length must have exactly {xlen} bytes') python-ntruprime-20241021.4/src/ntruprime/kem.py000066400000000000000000000065651474523713000214410ustar00rootroot00000000000000from typing import Tuple as _Tuple import ctypes as _ct from ._lib import _lib, _check_input class _KEM: def __init__(self) -> None: ''' ''' self._c_keypair = getattr(_lib, '%s_keypair' % self._prefix) self._c_keypair.argtypes = [_ct.c_char_p, _ct.c_char_p] self._c_keypair.restype = None self._c_enc = getattr(_lib, '%s_enc' % self._prefix) self._c_enc.argtypes = [_ct.c_char_p, _ct.c_char_p, _ct.c_char_p] self._c_enc.restype = None self._c_dec = getattr(_lib, '%s_dec' % self._prefix) self._c_dec.argtypes = [_ct.c_char_p, _ct.c_char_p, _ct.c_char_p] self._c_dec.restype = None def keypair(self) -> _Tuple[bytes, bytes]: ''' Keypair - randomly generates secret key 'sk' and corresponding public key 'pk'. Returns: pk (bytes): public key sk (bytes): secret key ''' pk = _ct.create_string_buffer(self.PUBLICKEYBYTES) sk = _ct.create_string_buffer(self.SECRETKEYBYTES) self._c_keypair(pk, sk) return pk.raw, sk.raw def enc(self, pk: bytes) -> _Tuple[bytes, bytes]: ''' Encapsulation - randomly generates a ciphertext 'c' and the corresponding session key 'k' given Alice's public key 'pk'. Parameters: pk (bytes): public key Returns: c (bytes): ciphertext k (bytes): session key ''' _check_input(pk, self.PUBLICKEYBYTES, 'pk') c = _ct.create_string_buffer(self.CIPHERTEXTBYTES) k = _ct.create_string_buffer(self.BYTES) pk = _ct.create_string_buffer(pk) self._c_enc(c, k, pk) return c.raw, k.raw def dec(self, c: bytes, sk: bytes) -> bytes: ''' Decapsulation - given Alice's secret key 'sk' computes the session key 'k' corresponding to a ciphertext 'c'. Parameters: c (bytes): ciphertext sk (bytes): secret key Returns: k (bytes): session key ''' _check_input(c, self.CIPHERTEXTBYTES, 'c') _check_input(sk, self.SECRETKEYBYTES, 'sk') k = _ct.create_string_buffer(self.BYTES) c = _ct.create_string_buffer(c) sk = _ct.create_string_buffer(sk) self._c_dec(k, c, sk) return k.raw class sntrup653(_KEM): PUBLICKEYBYTES = 994 SECRETKEYBYTES = 1518 CIPHERTEXTBYTES = 897 BYTES = 32 _prefix = "ntruprime_kem_sntrup653" sntrup653 = sntrup653() class sntrup761(_KEM): PUBLICKEYBYTES = 1158 SECRETKEYBYTES = 1763 CIPHERTEXTBYTES = 1039 BYTES = 32 _prefix = "ntruprime_kem_sntrup761" sntrup761 = sntrup761() class sntrup857(_KEM): PUBLICKEYBYTES = 1322 SECRETKEYBYTES = 1999 CIPHERTEXTBYTES = 1184 BYTES = 32 _prefix = "ntruprime_kem_sntrup857" sntrup857 = sntrup857() class sntrup953(_KEM): PUBLICKEYBYTES = 1505 SECRETKEYBYTES = 2254 CIPHERTEXTBYTES = 1349 BYTES = 32 _prefix = "ntruprime_kem_sntrup953" sntrup953 = sntrup953() class sntrup1013(_KEM): PUBLICKEYBYTES = 1623 SECRETKEYBYTES = 2417 CIPHERTEXTBYTES = 1455 BYTES = 32 _prefix = "ntruprime_kem_sntrup1013" sntrup1013 = sntrup1013() class sntrup1277(_KEM): PUBLICKEYBYTES = 2067 SECRETKEYBYTES = 3059 CIPHERTEXTBYTES = 1847 BYTES = 32 _prefix = "ntruprime_kem_sntrup1277" sntrup1277 = sntrup1277() python-ntruprime-20241021.4/tests/000077500000000000000000000000001474523713000166255ustar00rootroot00000000000000python-ntruprime-20241021.4/tests/test_sntrup1013.py000066400000000000000000000002611474523713000220750ustar00rootroot00000000000000from ntruprime import sntrup1013 def test_sntrup1013c(): pk, sk = sntrup1013.keypair() c, k1 = sntrup1013.enc(pk) k2 = sntrup1013.dec(c, sk) assert (k1 == k2) python-ntruprime-20241021.4/tests/test_sntrup1277.py000066400000000000000000000002611474523713000221110ustar00rootroot00000000000000from ntruprime import sntrup1277 def test_sntrup1277c(): pk, sk = sntrup1277.keypair() c, k1 = sntrup1277.enc(pk) k2 = sntrup1277.dec(c, sk) assert (k1 == k2) python-ntruprime-20241021.4/tests/test_sntrup653.py000066400000000000000000000002541474523713000220300ustar00rootroot00000000000000from ntruprime import sntrup653 def test_sntrup653c(): pk, sk = sntrup653.keypair() c, k1 = sntrup653.enc(pk) k2 = sntrup653.dec(c, sk) assert (k1 == k2) python-ntruprime-20241021.4/tests/test_sntrup761.py000066400000000000000000000002541474523713000220300ustar00rootroot00000000000000from ntruprime import sntrup761 def test_sntrup761c(): pk, sk = sntrup761.keypair() c, k1 = sntrup761.enc(pk) k2 = sntrup761.dec(c, sk) assert (k1 == k2) python-ntruprime-20241021.4/tests/test_sntrup857.py000066400000000000000000000002541474523713000220360ustar00rootroot00000000000000from ntruprime import sntrup857 def test_sntrup857c(): pk, sk = sntrup857.keypair() c, k1 = sntrup857.enc(pk) k2 = sntrup857.dec(c, sk) assert (k1 == k2) python-ntruprime-20241021.4/tests/test_sntrup953.py000066400000000000000000000002541474523713000220330ustar00rootroot00000000000000from ntruprime import sntrup953 def test_sntrup953c(): pk, sk = sntrup953.keypair() c, k1 = sntrup953.enc(pk) k2 = sntrup953.dec(c, sk) assert (k1 == k2)