From 325cd3126a106c97c7c6ed390ab26e621cdda33e Mon Sep 17 00:00:00 2001 From: haskal Date: Mon, 14 Jun 2021 05:14:43 -0400 Subject: [PATCH] add monocypher module --- .gitmodules | 3 +++ leylines-monocypher/.gitignore | 11 ++++++++ leylines-monocypher/Makefile | 9 +++++++ leylines-monocypher/monocypher-git | 1 + leylines-monocypher/monocypher/__init__.py | 25 +++++++++++++++++ leylines-monocypher/monocypher/py.typed | 0 leylines-monocypher/setup.py | 31 ++++++++++++++++++++++ 7 files changed, 80 insertions(+) create mode 100644 .gitmodules create mode 100644 leylines-monocypher/.gitignore create mode 100644 leylines-monocypher/Makefile create mode 160000 leylines-monocypher/monocypher-git create mode 100644 leylines-monocypher/monocypher/__init__.py create mode 100644 leylines-monocypher/monocypher/py.typed create mode 100644 leylines-monocypher/setup.py diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..76289aa --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "leylines-monocypher/monocypher-git"] + path = leylines-monocypher/monocypher-git + url = https://github.com/LoupVaillant/Monocypher diff --git a/leylines-monocypher/.gitignore b/leylines-monocypher/.gitignore new file mode 100644 index 0000000..84be8d9 --- /dev/null +++ b/leylines-monocypher/.gitignore @@ -0,0 +1,11 @@ +.venv +*.pyc +*.pyo +*.pyd +__pycache__ +*.egg-info +.env +.vimrc +.ycm_extra_conf.py +/build +*.so diff --git a/leylines-monocypher/Makefile b/leylines-monocypher/Makefile new file mode 100644 index 0000000..1b2beff --- /dev/null +++ b/leylines-monocypher/Makefile @@ -0,0 +1,9 @@ +.PHONY: check install + +check: + @mypy . || true + @flake8 . || true + @pyflakes . || true + +install: + pip install --user -e . diff --git a/leylines-monocypher/monocypher-git b/leylines-monocypher/monocypher-git new file mode 160000 index 0000000..baca5d3 --- /dev/null +++ b/leylines-monocypher/monocypher-git @@ -0,0 +1 @@ +Subproject commit baca5d31259c598540e4d1284bc8d8f793abf83a diff --git a/leylines-monocypher/monocypher/__init__.py b/leylines-monocypher/monocypher/__init__.py new file mode 100644 index 0000000..2543d69 --- /dev/null +++ b/leylines-monocypher/monocypher/__init__.py @@ -0,0 +1,25 @@ +import ctypes +import pathlib +import secrets + +_lib = ctypes.CDLL(pathlib.Path(__file__).parent / "ext.so") + +# crypto_key_exchange +x25519_ty = ctypes.c_uint8 * 32 +_lib.crypto_x25519_public_key.restype = None +_lib.crypto_x25519_public_key.argtypes = [x25519_ty, x25519_ty] + + +def crypto_key_exchange_make_key() -> bytes: + secret = bytearray(secrets.token_bytes(32)) + secret[0] &= 248 + secret[31] = (secret[31] & 127) | 64 + return bytes(secret) + + +def crypto_key_exchange_public_key(secret: bytes) -> bytes: + public = x25519_ty() + _lib.crypto_x25519_public_key(public, x25519_ty(*secret)) + return bytes(public) + +# and.... that's all for now diff --git a/leylines-monocypher/monocypher/py.typed b/leylines-monocypher/monocypher/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/leylines-monocypher/setup.py b/leylines-monocypher/setup.py new file mode 100644 index 0000000..bdbac27 --- /dev/null +++ b/leylines-monocypher/setup.py @@ -0,0 +1,31 @@ +from setuptools import setup, Distribution, Extension +from setuptools.command.build_ext import build_ext as build_ext_orig + +class CTypesExtension(Extension): + pass + +class build_ext(build_ext_orig): + def get_export_symbols(self, ext): + return ext.export_symbols + + def get_ext_filename(self, ext_name): + return ext_name.replace(".", "/") + '.so' + +setup(name='leylines-monocypher', + version='0.1', + description='Sample text', + url='https://awoo.systems', + author='haskal', + author_email='haskal@awoo.systems', + license='AGPLv3', + packages=['monocypher'], + package_data={"monocypher": ["py.typed"]}, + ext_modules=[ + CTypesExtension("monocypher.ext", + include_dirs=["monocypher-git/src/"], + sources=["monocypher-git/src/monocypher.c", + "monocypher-git/src/optional/monocypher-ed25519.c"]) + ], + cmdclass={"build_ext": build_ext}, + include_package_data=True, + zip_safe=False)