-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement python interface for sign and gimli_hash
- Loading branch information
Showing
18 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
/.eggs/ | ||
/.sconsign.dblite | ||
/build/ | ||
/dist/ | ||
/tags | ||
*.egg-info/ | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
FROM ubuntu:18.04 | ||
|
||
RUN apt-get update \ | ||
&& apt-get install -y software-properties-common \ | ||
&& add-apt-repository ppa:deadsnakes/ppa | ||
|
||
RUN apt-get update \ | ||
&& apt-get install -y \ | ||
clang \ | ||
gcc-arm-none-eabi \ | ||
gcc-multilib \ | ||
git \ | ||
llvm \ | ||
locales \ | ||
python3-pip \ | ||
python3.6-dev \ | ||
python3.7-dev \ | ||
scons | ||
|
||
RUN locale-gen en_US.UTF-8 | ||
ENV LANG en_US.UTF-8 | ||
ENV LANGUAGE en_US:en | ||
ENV LC_ALL en_US.UTF-8 | ||
|
||
RUN python3 -m pip install --upgrade pip | ||
RUN python3 -m pip install virtualenv | ||
|
||
WORKDIR /src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
black --check --include "\\.pyi?$|SCons" --exclude "/(\\.git|venv.*|build|.eggs)/" py ffibuilder.py setup.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/bin/bash | ||
|
||
set -xe | ||
|
||
virtualenv -p python3.7 venv_black | ||
source venv_black/bin/activate | ||
pip install black==19.10b0 | ||
./black-check.sh | ||
deactivate | ||
|
||
scons --jobs "$(nproc)" | ||
|
||
function pybuild { | ||
version="python$1" | ||
venv="venv$1" | ||
virtualenv -p "$version" "$venv" | ||
source "$venv/bin/activate" | ||
pip install . pytest | ||
pytest --verbose --color=yes test | ||
python setup.py sdist bdist_wheel | ||
deactivate | ||
rm -rf "test/__pycache__" | ||
} | ||
|
||
pybuild 3.6 | ||
pybuild 3.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash | ||
|
||
set -x | ||
|
||
name="lithium" | ||
image="$name-builder" | ||
|
||
docker build --tag "$image" . | ||
|
||
cidfile="$(mktemp -u cidfile.XXXXXX)" | ||
|
||
docker run --interactive --tty \ | ||
--volume "$(pwd):/src/$name" \ | ||
--cidfile "$cidfile" \ | ||
--cap-add SYS_PTRACE \ | ||
"$image" \ | ||
"bash" "-c" \ | ||
"git clone $name $name-docker && cd $name-docker && ./build.sh" | ||
|
||
container="$(cat "$cidfile")" | ||
docker cp "$container:/src/$name-docker/dist" "dist" | ||
docker rm "$container" | ||
rm -f "$cidfile" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import cffi | ||
import codecs | ||
|
||
ffibuilder = cffi.FFI() | ||
|
||
c_headers = [ | ||
"lithium/gimli.h", | ||
"lithium/gimli_state.h", | ||
"lithium/gimli_hash.h", | ||
"lithium/sign.h", | ||
] | ||
|
||
ffibuilder.set_source( | ||
"lithium._lithium", | ||
"\n".join(["#include <%s>" % h for h in c_headers]), | ||
include_dirs=["include"], | ||
sources=[ | ||
"src/gimli.c", | ||
"src/gimli_common.c", | ||
"src/gimli_hash.c", | ||
"src/fe.c", | ||
"src/x25519.c", | ||
"src/sign.c", | ||
], | ||
) | ||
|
||
cdef = "" | ||
|
||
for h in c_headers: | ||
with codecs.open("include/%s" % h, encoding="utf-8") as f: | ||
cdef_active = False | ||
for line in f.read().splitlines(): | ||
if "cffi:begin" in line: | ||
cdef_active = True | ||
if "cffi:end" in line: | ||
cdef_active = False | ||
if cdef_active: | ||
cdef += line + "\n" | ||
|
||
cdef += """ | ||
extern "Python+C" void lith_random_bytes(unsigned char *buf, size_t len); | ||
""" | ||
|
||
ffibuilder.cdef(cdef.encode("ascii", "ignore").decode()) | ||
|
||
if __name__ == "__main__": | ||
ffibuilder.compile(verbose=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from . import gimli_hash | ||
from . import sign | ||
|
||
from . import _random |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from lithium._lithium import ffi | ||
|
||
from secrets import token_bytes | ||
|
||
|
||
@ffi.def_extern() | ||
def lith_random_bytes(buf, n): | ||
ffi.memmove(buf, token_bytes(n), n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from lithium._lithium import ffi, lib | ||
|
||
|
||
class GimliHash: | ||
""" | ||
An object for incrementally hashing a message. | ||
""" | ||
|
||
def __init__(self): | ||
self._hash_state = ffi.new("gimli_hash_state *") | ||
lib.gimli_hash_init(self._hash_state) | ||
|
||
def update(self, data): | ||
""" | ||
Feed new data to be hashed. | ||
""" | ||
buf = ffi.from_buffer("unsigned char[%s]" % len(data), data) | ||
lib.gimli_hash_update(self._hash_state, buf, len(buf)) | ||
|
||
def final(self, n=lib.GIMLI_HASH_DEFAULT_LEN): | ||
""" | ||
Emit an n-byte hash of the data given to update. | ||
""" | ||
buf = ffi.new("unsigned char[%s]" % n) | ||
lib.gimli_hash_final(self._hash_state, buf, n) | ||
return bytes(buf) | ||
|
||
|
||
def gimli_hash(data, n=lib.GIMLI_HASH_DEFAULT_LEN): | ||
""" | ||
Compute an n-byte GimliHash of data in one shot. | ||
""" | ||
g = GimliHash() | ||
g.update(data) | ||
return g.final(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from lithium._lithium import ffi, lib | ||
|
||
_sig_cdecl = "unsigned char[LITH_SIGN_LEN]" | ||
_secret_key_cdecl = "unsigned char[LITH_SIGN_SECRET_KEY_LEN]" | ||
_public_key_cdecl = "unsigned char[LITH_SIGN_PUBLIC_KEY_LEN]" | ||
_prehash_cdecl = "unsigned char[LITH_SIGN_PREHASH_LEN]" | ||
|
||
|
||
class Sign: | ||
""" | ||
An object for computing or verifying a lithium signature. | ||
""" | ||
|
||
def __init__(self): | ||
self._sign_state = ffi.new("lith_sign_state *") | ||
lib.lith_sign_init(self._sign_state) | ||
|
||
def update(self, data): | ||
""" | ||
Feed new data to the signature computation. | ||
""" | ||
buf = ffi.from_buffer(data) | ||
lib.lith_sign_update(self._sign_state, buf, len(buf)) | ||
|
||
def final_create(self, secret_key): | ||
""" | ||
Create a signature over the data given to update. | ||
""" | ||
sk_buf = ffi.from_buffer(_secret_key_cdecl, secret_key) | ||
sig_buf = ffi.new(_sig_cdecl) | ||
lib.lith_sign_final_create(self._sign_state, sig_buf, sk_buf) | ||
return bytes(sig_buf) | ||
|
||
def final_verify(self, sig, public_key): | ||
""" | ||
Verify a signature over the data given to update. | ||
""" | ||
sig_buf = ffi.from_buffer(_sig_cdecl, sig) | ||
pk_buf = ffi.from_buffer(_public_key_cdecl, public_key) | ||
return lib.lith_sign_final_verify(self._sign_state, sig_buf, pk_buf) | ||
|
||
def final_prehash(self): | ||
""" | ||
Compute a prehash blob from the data given to update, to be signed or verified separately. | ||
""" | ||
prehash_buf = ffi.new(_prehash_cdecl) | ||
lib.lith_sign_final_prehash(self._sign_state, prehash_buf) | ||
return bytes(prehash_buf) | ||
|
||
|
||
def keygen(): | ||
""" | ||
Generate a public and secret keypair. | ||
""" | ||
pk_buf = ffi.new(_public_key_cdecl) | ||
sk_buf = ffi.new(_secret_key_cdecl) | ||
lib.lith_sign_keygen(pk_buf, sk_buf) | ||
return (bytes(pk_buf), bytes(sk_buf)) | ||
|
||
|
||
def create_from_prehash(prehash, secret_key): | ||
""" | ||
Create a signature from a prehash blob generated by Sign.final_prehash. | ||
""" | ||
sk_buf = ffi.from_buffer(_secret_key_cdecl, secret_key) | ||
prehash_buf = ffi.from_buffer(_prehash_cdecl, prehash) | ||
sig_buf = ffi.new(_sig_cdecl) | ||
lib.lith_sign_create_from_prehash(sig_buf, prehash_buf, sk_buf) | ||
return bytes(sig_buf) | ||
|
||
|
||
def verify_prehash(sig, prehash, public_key): | ||
""" | ||
Verify a signature on a prehash blob generated by Sign.final_prehash. | ||
""" | ||
sig_buf = ffi.from_buffer(_sig_cdecl, sig) | ||
prehash_buf = ffi.from_buffer(_prehash_cdecl, prehash) | ||
pk_buf = ffi.from_buffer(_public_key_cdecl, public_key) | ||
return lib.lith_sign_verify_prehash(sig_buf, prehash_buf, pk_buf) | ||
|
||
|
||
def create(data, secret_key): | ||
""" | ||
Create a signature over data. | ||
""" | ||
s = Sign() | ||
s.update(data) | ||
return s.final_create(secret_key) | ||
|
||
|
||
def verify(data, sig, public_key): | ||
""" | ||
Verify a signature over data. | ||
""" | ||
s = Sign() | ||
s.update(data) | ||
return s.final_verify(sig, public_key) |
Oops, something went wrong.