pax_global_header00006660000000000000000000000064147774153430014531gustar00rootroot0000000000000052 comment=a88829a9f6687ec74b57fa9657e1bdd70e798548 python-lib1305-20250415.1/000077500000000000000000000000001477741534300145325ustar00rootroot00000000000000python-lib1305-20250415.1/LICENSE000066400000000000000000000156101477741534300155420ustar00rootroot00000000000000Creative 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-lib1305-20250415.1/README.md000066400000000000000000000012021477741534300160040ustar00rootroot00000000000000Python wrapper around implementation of the Poly1305 one-time authenticator. Import library: from lib1305 import poly1305 Authenticating a message: a = poly1305.auth(m, k) Verifying an authenticator: poly1305.verify(a, m, k) The poly1305.auth function generates an 128-bit authenticator 'a' given a message 'm' and a 256-bit secret key 'k'. "One-time" means that the secret key must not be reused to generate an authenticator of another message. The poly1305.verify function verifies an 128-bit authenticator 'a' given a messagea 'm' and a 256-bit secret key 'k'. It raises an exception if the authenticator is not valid. python-lib1305-20250415.1/autogen/000077500000000000000000000000001477741534300161745ustar00rootroot00000000000000python-lib1305-20250415.1/autogen/init000077500000000000000000000003771477741534300170740ustar00rootroot00000000000000#!/usr/bin/env python3 helpstr = "'''\n" with open('README.md') as f: helpstr += f.read() helpstr += "'''\n" with open(f'src/lib1305/__init__.py', 'w') as f: f.write(helpstr) f.write('\n') f.write(f'from .onetimeauth import poly1305\n') python-lib1305-20250415.1/autogen/lib000077500000000000000000000012011477741534300166620ustar00rootroot00000000000000#!/usr/bin/env python3 project='lib1305' libname='1305' lib="""''' Import shared library. ''' from ctypes import CDLL as _CDLL from ctypes.util import find_library as _find_library _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') """ % (libname, libname) with open(f'src/{project}/_lib.py', 'w') as f: f.write(lib) python-lib1305-20250415.1/pyproject.toml000066400000000000000000000007621477741534300174530ustar00rootroot00000000000000[build-system] requires = ['setuptools'] build-backend = 'setuptools.build_meta' [project] name = 'lib1305' description = 'Python wrapper around implementation of the Poly1305 one-time authenticator' readme = 'README.md' requires-python = '>=3.7' license = {file = 'LICENSE'} authors = [ {name = 'Jan Mojžíš', email = 'jan.mojzis@gmail.com'}, ] version = '20250415.1' [tool.pytest.ini_options] minversion = '6.0' addopts = '-ra -v' testpaths = [ 'tests', ] pythonpath = [ 'src', ] python-lib1305-20250415.1/src/000077500000000000000000000000001477741534300153215ustar00rootroot00000000000000python-lib1305-20250415.1/src/lib1305/000077500000000000000000000000001477741534300164005ustar00rootroot00000000000000python-lib1305-20250415.1/src/lib1305/__init__.py000066400000000000000000000012551477741534300205140ustar00rootroot00000000000000''' Python wrapper around implementation of the Poly1305 one-time authenticator. Import library: from lib1305 import poly1305 Authenticating a message: a = poly1305.auth(m, k) Verifying an authenticator: poly1305.verify(a, m, k) The poly1305.auth function generates an 128-bit authenticator 'a' given a message 'm' and a 256-bit secret key 'k'. "One-time" means that the secret key must not be reused to generate an authenticator of another message. The poly1305.verify function verifies an 128-bit authenticator 'a' given a messagea 'm' and a 256-bit secret key 'k'. It raises an exception if the authenticator is not valid. ''' from .onetimeauth import poly1305 python-lib1305-20250415.1/src/lib1305/_lib.py000066400000000000000000000007521477741534300176630ustar00rootroot00000000000000''' Import shared library. ''' from ctypes import CDLL as _CDLL from ctypes.util import find_library as _find_library _libname = _find_library('1305') if _libname is None: raise FileNotFoundError("unable to locate library '1305'") _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-lib1305-20250415.1/src/lib1305/onetimeauth.py000066400000000000000000000042301477741534300212730ustar00rootroot00000000000000''' Onetimeauth: secret-key single-message authentication module. ''' from typing import Tuple as _Tuple import ctypes as _ct from ._lib import _lib, _check_input class Poly1305: ''' Poly1305 one-time authenticator. ''' KEYBYTES = 32 BYTES = 16 def __init__(self) -> None: ''' ''' self._c_auth = getattr(_lib, 'lib1305_onetimeauth_poly1305') self._c_auth.argtypes = [_ct.c_char_p, _ct.c_char_p, _ct.c_longlong, _ct.c_char_p] self._c_auth.restype = None self._c_verify = getattr(_lib, 'lib1305_onetimeauth_poly1305_verify') self._c_verify.argtypes = [_ct.c_char_p, _ct.c_char_p, _ct.c_longlong, _ct.c_char_p] self._c_verify.restype = _ct.c_int def auth(self, m: bytes, k: bytes) -> bytes: ''' Auth - generates an authenticator 'a' given a message 'm' and a secret key 'k'. Parameters: m (bytes): message k (bytes): secret key Returns: a (bytes): authenticator ''' _check_input(m, -1, 'm') _check_input(k, self.KEYBYTES, 'k') mlen = _ct.c_longlong(len(m)) m = _ct.create_string_buffer(m) k = _ct.create_string_buffer(k) a = _ct.create_string_buffer(self.BYTES) self._c_auth(a, m, mlen, k) return a.raw def verify(self, a: bytes, m: bytes, k: bytes) -> None: ''' Verify - verifies an authenticator 'a' given a message 'm' and a secret key 'k'. Parameters: a (bytes): authenticator m (bytes): message k (bytes): secret key Raises: ValueError: an authenticator doesn't match Returns: None ''' _check_input(a, self.BYTES, 'a') _check_input(m, -1, 'm') _check_input(k, self.KEYBYTES, 'k') mlen = _ct.c_longlong(len(m)) m = _ct.create_string_buffer(m) k = _ct.create_string_buffer(k) a = _ct.create_string_buffer(a) if self._c_verify(a, m, mlen, k): raise ValueError('verify failed') poly1305 = Poly1305() python-lib1305-20250415.1/tests/000077500000000000000000000000001477741534300156745ustar00rootroot00000000000000python-lib1305-20250415.1/tests/test_random.py000066400000000000000000000022501477741534300205640ustar00rootroot00000000000000''' random data test ''' import os from secrets import randbelow from lib1305 import poly1305 def test_random() -> None: ''' random data test ''' # generate a random 256-bit secret key k = os.urandom(32) # generate a random massage m = os.urandom(128 + randbelow(128)) # compute an 128-bit authenticator a = poly1305.auth(m, k) # verify and 128-bit authenticator poly1305.verify(a, m, k) # replace byte in a message and check if verification fails for i in range(len(m)): m1 = bytearray(m) m1[i] = (m1[i] + 1 + randbelow(255)) % 256 m1 = bytes(m1) try: poly1305.verify(a, m1, k) except ValueError: pass else: raise ValueError('message forgery not detected !!!') # replace byte in an authentificatior and check if verification fails for i in range(len(a)): a1 = bytearray(a) a1[i] = (a1[i] + 1 + randbelow(255)) % 256 a1 = bytes(a1) try: poly1305.verify(a1, m, k) except ValueError: pass else: raise ValueError('authenticator forgery not detected !!!') python-lib1305-20250415.1/tests/test_vector.py000066400000000000000000000031121477741534300206040ustar00rootroot00000000000000''' check test vectors ''' from lib1305 import poly1305 def test_vector1() -> None: ''' backported from NaCl library ''' m = b'\x8e\x99\x3b\x9f\x48\x68\x12\x73\xc2\x96\x50\xba\x32\xfc\x76\xce' m += b'\x48\x33\x2e\xa7\x16\x4d\x96\xa4\x47\x6f\xb8\xc5\x31\xa1\x18\x6a' m += b'\xc0\xdf\xc1\x7c\x98\xdc\xe8\x7b\x4d\xa7\xf0\x11\xec\x48\xc9\x72' m += b'\x71\xd2\xc2\x0f\x9b\x92\x8f\xe2\x27\x0d\x6f\xb8\x63\xd5\x17\x38' m += b'\xb4\x8e\xee\xe3\x14\xa7\xcc\x8a\xb9\x32\x16\x45\x48\xe5\x26\xae' m += b'\x90\x22\x43\x68\x51\x7a\xcf\xea\xbd\x6b\xb3\x73\x2b\xc0\xe9\xda' m += b'\x99\x83\x2b\x61\xca\x01\xb6\xde\x56\x24\x4a\x9e\x88\xd5\xf9\xb3' m += b'\x79\x73\xf6\x22\xa4\x3d\x14\xa6\x59\x9b\x1f\x65\x4c\xb4\x5a\x74' m += b'\xe3\x55\xa5' k = b'\xee\xa6\xa7\x25\x1c\x1e\x72\x91\x6d\x11\xc2\xcb\x21\x4d\x3c\x25' k += b'\x25\x39\x12\x1d\x8e\x23\x4e\x65\x2d\x65\x1f\xa4\xc8\xcf\xf8\x80' a = b'\xf3\xff\xc7\x70\x3f\x94\x00\xe5\x2a\x7d\xfb\x4b\x3d\x33\x05\xd9' poly1305.verify(a, m, k) assert a == poly1305.auth(m, k) def test_vector2() -> None: ''' This function primarily tests the internal freeze() function in lib1305. The freeze() function detects if the number is less than p (2**130-5) and is therefore finally reduced and if not, then p (2**130-5) is subtracted. And this test tests the rare condition where the number is greater than 'p' and needs to be subtracted. ''' m = 16 * b'\xff' k = b'\x02' + 31 * b'\x00' a = b'\x03' + 15 * b'\x00' poly1305.verify(a, m, k) assert a == poly1305.auth(m, k)