This is an automated email from the ASF dual-hosted git repository. kmccusker pushed a commit to branch issue39 in repository https://gitbox.apache.org/repos/asf/incubator-milagro-crypto-c.git
commit fcd5d5a2b76b5fa26ea87c19dcef0b966b735806 Author: Kealan McCusker <[email protected]> AuthorDate: Mon Sep 2 09:55:13 2019 +0100 upgrade to Python3 --- CMakeLists.txt | 2 +- Dockerfile | 40 ++- Makefile | 2 +- README.md | 32 ++- cmake/PythonSiteDirs.cmake | 11 - examples/example_bls_ZZZ.c.in | 8 +- examples/example_bls_sss_ZZZ.c.in | 40 +-- {wrappers/python => python}/CMakeLists.txt | 6 +- {wrappers/python => python}/README.md | 0 .../python => python}/TestMPINInstall_ZZZ.py.in | 37 ++- {wrappers/python => python}/TimeMPIN_ZZZ.py.in | 221 +++++++-------- {wrappers/python => python}/bls_ZZZ.py.in | 28 +- {wrappers/python => python}/mpin_ZZZ.py.in | 301 +++++++++++---------- {wrappers/python => python}/wcc_ZZZ.py.in | 124 +++++---- src/bls.c.in | 14 +- src/bls192.c.in | 14 +- src/bls256.c.in | 14 +- test/test_bls_sss_ZZZ.c.in | 40 +-- 18 files changed, 465 insertions(+), 469 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f161106..8b7486b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -724,7 +724,7 @@ endif() if(BUILD_PYTHON) message(STATUS "Build Python wrappers") - add_subdirectory(wrappers/python) + add_subdirectory(python) endif() if(BUILD_TESTING) diff --git a/Dockerfile b/Dockerfile index 878184f..5f4e797 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,32 +1,24 @@ -FROM ubuntu:latest +FROM ubuntu:bionic -RUN dpkg --add-architecture i386 \ - && apt-get update && apt-get install -y --no-install-recommends \ - astyle \ - ca-certificates \ +RUN dpkg --add-architecture i386 + +# add repositories cache +RUN apt-get update -y + +# install packages +RUN apt-get install -y \ + build-essential \ cmake \ doxygen \ - doxygen-latex \ - g++ \ - g++-multilib \ - gcc \ - git \ - lcov \ - make \ - mingw-w64 \ parallel \ - python-pip \ - python-setuptools\ - python3-pip \ - python3-setuptools\ - wine-stable \ + mingw-w64 \ wine64 \ wine32 \ - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \ - && pip install \ - autopep8 \ - cffi \ - wheel + lcov \ + python3-dev \ + python3-pip \ + gcc-multilib + +RUN pip3 install cffi autopep8 CMD ["/bin/bash"] diff --git a/Makefile b/Makefile index 591b5a5..6460a91 100644 --- a/Makefile +++ b/Makefile @@ -243,7 +243,7 @@ format: astyle --style=allman --recursive --suffix=none 'examples/*.c' astyle --style=allman --recursive --suffix=none 'examples/*.c.in' astyle --style=allman --recursive --suffix=none 'benchmark/*.c.in' - autopep8 --in-place --aggressive --aggressive ./wrappers/python/*.py.in + autopep8 --in-place --aggressive --aggressive ./python/*.py.in # Remove any build artifact clean: diff --git a/README.md b/README.md index f4080f4..9d6683e 100644 --- a/README.md +++ b/README.md @@ -57,17 +57,29 @@ The above packages can be installed in different ways, depending on the Operatin ``` sudo dpkg --add-architecture i386 -sudo apt-get install -y astyle ca-certificates cmake doxygen doxygen-latex g++ \ - g++-multilib gcc git lcov make mingw-w64 parallel python-pip python-setuptools\ - python3-pip python3-setuptools wine-stable wine64 -sudo pip install cffi autopep8 -sudo pip3 install cffi + +echo "add repositories cache" +sudo apt-get update -y + +echo "install packages" +sudo apt-get install -y \ + build-essential \ + cmake \ + doxygen \ + parallel \ + mingw-w64 \ + wine64 \ + wine32 \ + lcov \ + python3-dev \ + python3-pip \ + gcc-multilib + +sudo pip3 install cffi autopep8 ``` * **Fedora 30 Linux** -Python is not supported for this operating systems. - ``` sudo dnf config-manager --add-repo https://dl.winehq.org/wine-builds/fedora/30/winehq.repo sudo dnf install -y @development-tools cmake libffi-devel libasan parallel mingw64-gcc \ @@ -123,8 +135,8 @@ program. To build and run the example use this script; NOTE: The default build is for 64 bit machines ``` -git clone https://github.com/milagro-crypto/milagro-crypto-c -cd milagro-crypto-c +git clone https://github.com/apache/incubator-milagro-crypto-c.git +cd incubator-milagro-crypto-c mkdir -p target/build cd target/build cmake ../.. @@ -182,7 +194,7 @@ make package Start a command prompt as an administrator ``` -git clone https://github.com/milagro-crypto/milagro-crypto-c +git clone https://github.com/apache/incubator-milagro-crypto-c.git cd milagro-crypto-c mkdir target\build cd target\build diff --git a/cmake/PythonSiteDirs.cmake b/cmake/PythonSiteDirs.cmake index bf0f968..cc6bbe7 100644 --- a/cmake/PythonSiteDirs.cmake +++ b/cmake/PythonSiteDirs.cmake @@ -18,17 +18,6 @@ cmake_minimum_required(VERSION 3.1) execute_process(COMMAND - python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" - OUTPUT_VARIABLE PYTHON_SITE_PACKAGES - OUTPUT_STRIP_TRAILING_WHITESPACE -) - -execute_process(COMMAND - python -c "from distutils.sysconfig import get_python_lib; from os.path import dirname; print dirname(get_python_lib())" - OUTPUT_VARIABLE PYTHON_SITE_LIB -) - -execute_process(COMMAND python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())" OUTPUT_VARIABLE PYTHON3_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE diff --git a/examples/example_bls_ZZZ.c.in b/examples/example_bls_ZZZ.c.in index d171a9d..87074b6 100644 --- a/examples/example_bls_ZZZ.c.in +++ b/examples/example_bls_ZZZ.c.in @@ -16,13 +16,13 @@ specific language governing permissions and limitations under the License. */ -/* - BLS example code. - +/* + BLS example code. + Build executable after installation: 1. Change headers; - + #include "bls_ZZZ.h" -> #include <amcl/bls_ZZZ.h> 2. Build code diff --git a/examples/example_bls_sss_ZZZ.c.in b/examples/example_bls_sss_ZZZ.c.in index c325d68..e2085e9 100644 --- a/examples/example_bls_sss_ZZZ.c.in +++ b/examples/example_bls_sss_ZZZ.c.in @@ -16,13 +16,13 @@ specific language governing permissions and limitations under the License. */ -/* - BLS Shamir Secret Sharing example code. - +/* + BLS Shamir Secret Sharing example code. + Build executable after installation: 1. Change headers; - + #include "bls_ZZZ.h" -> #include <amcl/bls_ZZZ.h> 2. Build code @@ -82,7 +82,7 @@ int bls_sss(csprng *RNG) octet X[n]; char y[n][BGS_ZZZ]; octet Y[n]; - + for(int i=0; i<n; i++) { memset(&y[i], 0, sizeof(y[i])); @@ -94,7 +94,7 @@ int bls_sss(csprng *RNG) X[i].len = BGS_ZZZ; X[i].val = x[i]; } - + // Generate key pair BLS_ZZZ_KEY_PAIR_GENERATE(RNG,&SKI,&PKI); @@ -115,7 +115,7 @@ int bls_sss(csprng *RNG) if (rc!=BLS_OK) { printf("Error: Invalid Signature\n"); - return 1; + return 1; } printf("Success: Signature is valid\n"); @@ -124,8 +124,8 @@ int bls_sss(csprng *RNG) if (rc!=BLS_OK) { printf("Error: BLS_ZZZ_MAKE_SHARES\n"); - return 1; - } + return 1; + } for(int i=0; i<n; i++) { @@ -134,32 +134,32 @@ int bls_sss(csprng *RNG) printf("Y[%d] ", i); OCT_output(&Y[i]); } - + printf("SKO: "); OCT_output(&SKO); - + if (!OCT_comp(&SKI,&SKO)) { printf("FAILURE SKI != SKO"); return 1; } - + // Recover BLS secret key rc = BLS_ZZZ_RECOVER_SECRET(k, X, Y, &SKR); if (rc!=BLS_OK) { printf("Error: BLS_ZZZ_RECOVER_SECRET\n"); - return 1; - } + return 1; + } printf("SKR: "); OCT_output(&SKR); - + if (!OCT_comp(&SKR,&SKO)) { printf("FAILURE SKR != SKO"); return 1; } - + // Generate public keys and signatures using shares char pks[n][G2LEN]; octet PKS[n]; @@ -197,17 +197,17 @@ int bls_sss(csprng *RNG) if (rc!=BLS_OK) { printf("Error: BLS_ZZZ_RECOVER_SIGNATURE\n"); - return 1; - } + return 1; + } printf("SIGR: "); OCT_output(&SIGR); - + if (!OCT_comp(&SIGR,&SIGI)) { printf("FAILURE SIGR != SIGI"); return 1; } - + return 0; } diff --git a/wrappers/python/CMakeLists.txt b/python/CMakeLists.txt similarity index 95% rename from wrappers/python/CMakeLists.txt rename to python/CMakeLists.txt index a0a7961..cd41aa1 100644 --- a/wrappers/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -28,9 +28,9 @@ foreach(curve ${AMCL_CURVE}) amcl_configure_file_curve(TimeMPIN_ZZZ.py.in TimeMPIN_${TC}.py ${curve} __no_install) amcl_configure_file_curve(TestMPINInstall_ZZZ.py.in TestMPINInstall_${TC}.py ${curve} __no_install) - install(FILES "${amcl_mpin_${TC}_GEN_PYS}" DESTINATION ${PYTHON_SITE_PACKAGES}) + install(FILES "${amcl_mpin_${TC}_GEN_PYS}" DESTINATION ${PYTHON3_SITE_PACKAGES}) - add_test(test_python_mpin_install_${TC} python TestMPINInstall_${TC}.py) + add_test(test_python_mpin_install_${TC} python3 TestMPINInstall_${TC}.py) # Set the LD_LIBRARY_PATH or equivalent to the libraries can be # loaded when running the test. @@ -48,7 +48,7 @@ foreach(curve ${AMCL_CURVE}) if(TARGET amcl_wcc_${TC}) amcl_configure_file_curve(wcc_ZZZ.py.in wcc_${TC}.py ${curve} amcl_wcc_${TC}_GEN_PYS) - install(FILES "${amcl_wcc_${TC}_GEN_PYS}" DESTINATION ${PYTHON_SITE_PACKAGES}) + install(FILES "${amcl_wcc_${TC}_GEN_PYS}" DESTINATION ${PYTHON3_SITE_PACKAGES}) endif() if(TARGET amcl_bls_${TC}) diff --git a/wrappers/python/README.md b/python/README.md similarity index 100% rename from wrappers/python/README.md rename to python/README.md diff --git a/wrappers/python/TestMPINInstall_ZZZ.py.in b/python/TestMPINInstall_ZZZ.py.in similarity index 93% rename from wrappers/python/TestMPINInstall_ZZZ.py.in rename to python/TestMPINInstall_ZZZ.py.in index c02936c..ac96fc9 100644 --- a/wrappers/python/TestMPINInstall_ZZZ.py.in +++ b/python/TestMPINInstall_ZZZ.py.in @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Licensed to the Apache Software Foundation (ASF) under one @@ -20,7 +20,6 @@ under the License. """ import os import unittest -import json import hashlib import mpin_ZZZ @@ -30,22 +29,15 @@ class TestMPIN(unittest.TestCase): def setUp(self): - # Form MPin ID - endUserData = { - "issued": "2013-10-19T06:12:28Z", - "userID": "[email protected]", - "mobile": 1, - "salt": "e985da112a378c222cfc2f7226097b0c" - } - self.mpin_id = json.dumps(endUserData) + self.mpin_id = b'[email protected]' # Hash value of MPIN_ID self.hash_mpin_id = mpin_ZZZ.hash_id( mpin_ZZZ.HASH_TYPE_ZZZ, self.mpin_id) # Assign a seed value - seedHex = "3ade3d4a5c698e8910bf92f25d97ceeb7c25ed838901a5cb5db2cf25434c1fe76c7f79b7af2e5e1e4988e4294dbd9bd9fa3960197fb7aec373609fb890d74b16a4b14b2ae7e23b75f15d36c21791272372863c4f8af39980283ae69a79cf4e48e908f9e0" - self.seed = seedHex.decode("hex") + seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30" + self.seed = bytes.fromhex(seed_hex) self.date = 16238 @@ -287,7 +279,7 @@ class TestMPIN(unittest.TestCase): # Generate client secret shares rtn, cs1 = mpin_ZZZ.get_client_secret(ms1, hash_mpin_id) self.assertEqual(rtn, 0) - cs1Hex = cs1.encode("hex") + cs1Hex = cs1.hex() if cs1Hex in s: match = 1 self.assertEqual(match, 0) @@ -318,7 +310,6 @@ class TestMPIN(unittest.TestCase): match = 0 for i in range(1, 208900): random = mpin_ZZZ.generate_random(rng, 4) - # print i, " ", random.encode("hex") if random in s: match = 1 break @@ -338,7 +329,7 @@ class TestMPIN(unittest.TestCase): header = os.urandom(32) # Plaintext input - plaintext1 = "A test message" + plaintext1 = b"A test message" ciphertext, tag1 = mpin_ZZZ.aes_gcm_encrypt( key, iv, header, plaintext1) @@ -362,14 +353,16 @@ class TestMPIN(unittest.TestCase): header = os.urandom(32) # Plaintext input - plaintext1 = "A test message" + plaintext1 = b"A test message" ciphertext, tag1 = mpin_ZZZ.aes_gcm_encrypt( key, iv, header, plaintext1) - new = list(ciphertext) + ciphertext_hex = ciphertext.hex() + new = list(ciphertext_hex) new[0] = "a" if (new[0] != "a") else "b" - ciphertext_bad = ''.join(new) + ciphertext_bad_hex = ''.join(new) + ciphertext_bad = bytes.fromhex(ciphertext_bad_hex) plaintext2, tag2 = mpin_ZZZ.aes_gcm_decrypt( key, iv, header, ciphertext_bad) @@ -390,14 +383,16 @@ class TestMPIN(unittest.TestCase): header = os.urandom(32) # Plaintext input - plaintext1 = "A test message" + plaintext1 = b"A test message" ciphertext, tag1 = mpin_ZZZ.aes_gcm_encrypt( key, iv, header, plaintext1) - new = list(header) + header_hex = header.hex() + new = list(header_hex) new[0] = "a" if (new[0] != "a") else "b" - header_bad = ''.join(new) + header_bad_hex = ''.join(new) + header_bad = bytes.fromhex(header_bad_hex) plaintext2, tag2 = mpin_ZZZ.aes_gcm_decrypt( key, iv, header_bad, ciphertext) diff --git a/wrappers/python/TimeMPIN_ZZZ.py.in b/python/TimeMPIN_ZZZ.py.in similarity index 58% rename from wrappers/python/TimeMPIN_ZZZ.py.in rename to python/TimeMPIN_ZZZ.py.in index 7faf631..6204878 100755 --- a/wrappers/python/TimeMPIN_ZZZ.py.in +++ b/python/TimeMPIN_ZZZ.py.in @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Licensed to the Apache Software Foundation (ASF) under one @@ -33,8 +33,8 @@ def time_func(stmt, n=10, setup='from __main__ import *'): total_time = t.timeit(n) iter_time = total_time / n iter_per_sec = n / total_time - print "func:%s nIter:%s total_time:%s iter_time:%s iter_per_sec: %s" % ( - stmt, n, total_time, iter_time, iter_per_sec) + print("func:{} nIter:{} total_time:{} iter_time:{} iter_per_sec: %s".format( + stmt, n, total_time, iter_time, iter_per_sec)) nIter = 100 @@ -66,8 +66,8 @@ if __name__ == "__main__": # Hash mpin_id hash_mpin_id = mpin_ZZZ.hash_id(mpin_ZZZ.HASH_TYPE_ZZZ, mpin_id) if DEBUG: - print "mpin_id: %s" % mpin_id.encode("hex") - print "hash_mpin_id: %s" % hash_mpin_id.encode("hex") + print("mpin_id: {}".format(mpin_id.hex())) + print("hash_mpin_id: {}".format(hash_mpin_id.hex())) mpin_id = mpin_id @@ -75,296 +75,297 @@ if __name__ == "__main__": time_func('rtn, ms1 = mpin_ZZZ.random_generate(rng)', nIter) rtn, ms1 = mpin_ZZZ.random_generate(rng) if rtn != 0: - print "random_generate(rng) Error %s", rtn + print("random_generate(rng) Error {}".format(rtn)) rtn, ms2 = mpin_ZZZ.random_generate(rng) if rtn != 0: - print "random_generate(rng) Error %s", rtn + print("random_generate(rng) Error {}".format(rtn)) if DEBUG: - print "ms1: %s" % ms1.encode("hex") - print "ms2: %s" % ms2.encode("hex") + print("ms1: {}".format(ms1.hex()) + print("ms2: {}".format(ms2.hex()) # Generate server secret shares time_func('rtn, ss1 = mpin_ZZZ.get_server_secret(ms1)', nIter) - rtn, ss1 = mpin_ZZZ.get_server_secret(ms1) + rtn, ss1=mpin_ZZZ.get_server_secret(ms1) if rtn != 0: - print "get_server_secret(ms1) Error %s" % rtn - rtn, ss2 = mpin_ZZZ.get_server_secret(ms2) + print("get_server_secret(ms1) Error {}".format(rtn)) + rtn, ss2=mpin_ZZZ.get_server_secret(ms2) if rtn != 0: - print "get_server_secret(ms2) Error %s" % rtn + print("get_server_secret(ms2) Error {}".format(rtn)) if DEBUG: - print "ss1: %s" % ss1.encode("hex") - print "ss2: %s" % ss2.encode("hex") + print("ss1: {}".format(ss1.hex())) + print("ss2: {}".format(ss2.hex())) # Combine server secret shares time_func('rtn, server_secret = mpin_ZZZ.recombine_G2(ss1, ss2)', nIter) - rtn, server_secret = mpin_ZZZ.recombine_G2(ss1, ss2) + rtn, server_secret=mpin_ZZZ.recombine_G2(ss1, ss2) if rtn != 0: - print "recombine_G2(ss1, ss2) Error %s" % rtn + print("recombine_G2(ss1, ss2) Error {}".format(rtn)) if DEBUG: - print "server_secret: %s" % mpin_ZZZ.server_secret.encode("hex") + print("server_secret: {}".format(mpin_ZZZ.server_secret.hex())) # Generate client secret shares time_func('rtn, cs1 = mpin_ZZZ.get_client_secret(ms1, hash_mpin_id)', nIter) - rtn, cs1 = mpin_ZZZ.get_client_secret(ms1, hash_mpin_id) + rtn, cs1=mpin_ZZZ.get_client_secret(ms1, hash_mpin_id) if rtn != 0: - print "get_client_secret(ms1, hash_mpin_id) Error %s" % rtn - rtn, cs2 = mpin_ZZZ.get_client_secret(ms2, hash_mpin_id) + print("get_client_secret(ms1, hash_mpin_id) Error {}".format(rtn)) + rtn, cs2=mpin_ZZZ.get_client_secret(ms2, hash_mpin_id) if rtn != 0: - print "get_client_secret(ms2, hash_mpin_id) Error %s" % rtn + print("get_client_secret(ms2, hash_mpin_id) Error {}".format(rtn)) if DEBUG: - print "cs1: %s" % cs1.encode("hex") - print "cs2: %s" % cs2.encode("hex") + print("cs1: {}".format(cs1.hex())) + print("cs2: {}".format(cs2.hex())) # Combine client secret shares time_func('rtn, client_secret = mpin_ZZZ.recombine_G1(cs1, cs2)', nIter) - rtn, client_secret = mpin_ZZZ.recombine_G1(cs1, cs2) + rtn, client_secret=mpin_ZZZ.recombine_G1(cs1, cs2) if rtn != 0: - print "recombine_G1(cs1, cs2) Error %s" % rtn - print "Client Secret: %s" % client_secret.encode("hex") + print("recombine_G1(cs1, cs2) Error {}".format(rtn)) + print("Client Secret: {}".format(client_secret.hex())) # Generate Time Permit shares if DEBUG: - print "Date %s" % date + print("Date {}".format(date)) time_func( 'rtn, tp1 = mpin_ZZZ.get_client_permit(mpin_ZZZ.HASH_TYPE_ZZZ, date, ms1, hash_mpin_id)', nIter) - rtn, tp1 = mpin_ZZZ.get_client_permit( + rtn, tp1=mpin_ZZZ.get_client_permit( mpin_ZZZ.HASH_TYPE_ZZZ, date, ms1, hash_mpin_id) if rtn != 0: - print "get_client_permit(mpin_ZZZ.HASH_TYPE_ZZZ, date, ms1, hash_mpin_id) Error %s" % rtn - rtn, tp2 = mpin_ZZZ.get_client_permit( + print("get_client_permit(mpin_ZZZ.HASH_TYPE_ZZZ, date, ms1, hash_mpin_id) Error {}".format(rtn)) + rtn, tp2=mpin_ZZZ.get_client_permit( mpin_ZZZ.HASH_TYPE_ZZZ, date, ms2, hash_mpin_id) if rtn != 0: - print "get_client_permit(mpin_ZZZ.HASH_TYPE_ZZZ, date, ms2, hash_mpin_id) Error %s" % rtn + print("get_client_permit(mpin_ZZZ.HASH_TYPE_ZZZ, date, ms2, hash_mpin_id) Error {}".format(rtn)) if DEBUG: - print "tp1: %s" % tp1.encode("hex") - print "tp2: %s" % tp2.encode("hex") + print("tp1: {}".format(tp1.hex())) + print("tp2: {}".format(tp2.hex())) # Combine Time Permit shares - rtn, time_permit = mpin_ZZZ.recombine_G1(tp1, tp2) + rtn, time_permit=mpin_ZZZ.recombine_G1(tp1, tp2) if rtn != 0: - print "recombine_G1(tp1, tp2) Error %s" % rtn + print("recombine_G1(tp1, tp2) Error {}".format(rtn)) if DEBUG: - print "time_permit: %s" % time_permit.encode("hex") + print("time_permit: {}".format(time_permit.hex())) # Client extracts PIN from secret to create Token - PIN = 1234 + PIN=1234 time_func( 'rtn, token = mpin_ZZZ.extract_pin(mpin_ZZZ.HASH_TYPE_ZZZ, mpin_id, PIN, client_secret)', nIter) - rtn, token = mpin_ZZZ.extract_pin( + rtn, token=mpin_ZZZ.extract_pin( mpin_ZZZ.HASH_TYPE_ZZZ, mpin_id, PIN, client_secret) if rtn != 0: - print "extract_pin(mpin_ZZZ.HASH_TYPE_ZZZ, mpin_id, PIN, token) Error %s" % rtn - print "Token: %s" % token.encode("hex") + print( + "extract_pin(mpin_ZZZ.HASH_TYPE_ZZZ, mpin_id, PIN, token) Error {}".format(rtn)) + print("Token: {}".format(token.hex()) if ONE_PASS: - print "M-Pin One Pass" - PIN = 1234 + print("M-Pin One Pass") + PIN=1234 time_func('epoch_time = mpin_ZZZ.get_time()', nIter) - epoch_time = mpin_ZZZ.get_time() + epoch_time=mpin_ZZZ.get_time() if DEBUG: - print "epoch_time %s" % epoch_time + print("epoch_time {}".format(epoch_time)) # Client precomputation if MPIN_FULL: time_func( 'rtn, pc1, pc2 = mpin_ZZZ.precompute(token, hash_mpin_id)', nIter) - rtn, pc1, pc2 = mpin_ZZZ.precompute(token, hash_mpin_id) + rtn, pc1, pc2=mpin_ZZZ.precompute(token, hash_mpin_id) # Client MPIN time_func( 'rtn, x, u, ut, v, y = mpin_ZZZ.client(mpin_ZZZ.HASH_TYPE_ZZZ, date, mpin_id, rng, None, PIN, token, time_permit, None, epoch_time)', nIter) - rtn, x, u, ut, v, y = mpin_ZZZ.client( + rtn, x, u, ut, v, y=mpin_ZZZ.client( mpin_ZZZ.HASH_TYPE_ZZZ, date, mpin_id, rng, None, PIN, token, time_permit, None, epoch_time) if rtn != 0: - print "MPIN_CLIENT ERROR %s" % rtn + print("MPIN_CLIENT ERROR {}".format(rtn)) # Client sends Z=r.ID to Server if MPIN_FULL: time_func( 'rtn, r, Z = mpin_ZZZ.get_G1_multiple(rng, 1, None, hash_mpin_id)', nIter) - rtn, r, Z = mpin_ZZZ.get_G1_multiple(rng, 1, None, hash_mpin_id) + rtn, r, Z=mpin_ZZZ.get_G1_multiple(rng, 1, None, hash_mpin_id) # Server MPIN time_func( 'rtn, HID, HTID, E, F, y2 = mpin_ZZZ.server(mpin_ZZZ.HASH_TYPE_ZZZ, date, server_secret, u, ut, v, mpin_id, None, epoch_time, None)', nIter) - rtn, HID, HTID, E, F, y2 = mpin_ZZZ.server( + rtn, HID, HTID, E, F, y2=mpin_ZZZ.server( mpin_ZZZ.HASH_TYPE_ZZZ, date, server_secret, u, ut, v, mpin_id, None, epoch_time, None) if DEBUG: - print "y2 ", y2.encode("hex") + print("y2 ", y2.hex()) if rtn != 0: - print "ERROR: %s is not authenticated" % mpin_id + print("ERROR: {} is not authenticated".format(mpin_id)) if PIN_ERROR: time_func('err = mpin_ZZZ.kangaroo(E, F)', nIter) - err = mpin_ZZZ.kangaroo(E, F) - print "Client PIN error %d " % err + err=mpin_ZZZ.kangaroo(E, F) + print("Client PIN error {} ".format(err)) raise SystemExit(0) else: - print "SUCCESS: %s is authenticated" % mpin_id + print("SUCCESS: {} is authenticated".format(mpin_id)) if date: - prHID = HTID + prHID=HTID else: - prHID = HID - ut = None + prHID=HID + ut=None # Server sends T=w.ID to client if MPIN_FULL: time_func( 'rtn, w, T = mpin_ZZZ.get_G1_multiple(rng, 0, None, prHID)', nIter) - rtn, w, T = mpin_ZZZ.get_G1_multiple(rng, 0, None, prHID) + rtn, w, T=mpin_ZZZ.get_G1_multiple(rng, 0, None, prHID) if rtn != 0: - print "ERROR: Generating T %s" % rtn + print("ERROR: Generating T {}".format(rtn)) if MPIN_FULL: time_func( 'HM = mpin_ZZZ.hash_all(mpin_ZZZ.HASH_TYPE_ZZZ, hash_mpin_id, u, ut, v, y, Z, T)', nIter) - HM = mpin_ZZZ.hash_all( + HM=mpin_ZZZ.hash_all( mpin_ZZZ.HASH_TYPE_ZZZ, hash_mpin_id, u, ut, v, y, Z, T) time_func( 'rtn, client_aes_key = mpin_ZZZ.client_key(mpin_ZZZ.HASH_TYPE_ZZZ, pc1, pc2, PIN, r, x, HM, T)', nIter) - rtn, client_aes_key = mpin_ZZZ.client_key( + rtn, client_aes_key=mpin_ZZZ.client_key( mpin_ZZZ.HASH_TYPE_ZZZ, pc1, pc2, PIN, r, x, HM, T) if rtn != 0: - print "ERROR: Generating client_aes_key %s" % rtn - print "Client AES Key: %s" % client_aes_key.encode("hex") + print("ERROR: Generating client_aes_key {}".format(rtn)) + print("Client AES Key: {}".format(client_aes_key.hex())) - rtn, server_aes_key = mpin_ZZZ.server_key( + rtn, server_aes_key=mpin_ZZZ.server_key( mpin_ZZZ.HASH_TYPE_ZZZ, Z, server_secret, w, HM, HID, u, ut) if rtn != 0: - print "ERROR: Generating server_aes_key %s" % rtn - print "Server AES Key: %s" % server_aes_key.encode("hex") + print("ERROR: Generating server_aes_key {}".format(rtn)) + print("Server AES Key: {}".format(server_aes_key.hex())) else: - print "M-Pin Three Pass" - PIN = 1234 + print("M-Pin Three Pass") + PIN=1234 if MPIN_FULL: time_func( 'rtn, pc1, pc2 = mpin_ZZZ.precompute(token, hash_mpin_id)', nIter) - rtn, pc1, pc2 = mpin_ZZZ.precompute(token, hash_mpin_id) + rtn, pc1, pc2=mpin_ZZZ.precompute(token, hash_mpin_id) if rtn != 0: - print "precompute(token, hash_mpin_id) ERROR %s" % rtn + print("precompute(token, hash_mpin_id) ERROR {}".format(rtn)) # Client first pass time_func( 'rtn, x, u, ut, sec = mpin_ZZZ.client_1(mpin_ZZZ.HASH_TYPE_ZZZ, date, mpin_id, rng, None, PIN, token, time_permit)', nIter) - rtn, x, u, ut, sec = mpin_ZZZ.client_1( + rtn, x, u, ut, sec=mpin_ZZZ.client_1( mpin_ZZZ.HASH_TYPE_ZZZ, date, mpin_id, rng, None, PIN, token, time_permit) if rtn != 0: - print "client_1 ERROR %s" % rtn + print("client_1 ERROR {}".format(rtn)) if DEBUG: - print "x: %s" % x.encode("hex") + print("x: {}".format(x.hex())) # Server calculates H(ID) and H(T|H(ID)) (if time permits enabled), # and maps them to points on the curve HID and HTID resp. time_func( 'HID, HTID = mpin_ZZZ.server_1(mpin_ZZZ.HASH_TYPE_ZZZ, date, mpin_id)', nIter) - HID, HTID = mpin_ZZZ.server_1(mpin_ZZZ.HASH_TYPE_ZZZ, date, mpin_id) + HID, HTID=mpin_ZZZ.server_1(mpin_ZZZ.HASH_TYPE_ZZZ, date, mpin_id) # Server generates Random number y and sends it to Client time_func('rtn, y = mpin_ZZZ.random_generate(rng)', nIter) - rtn, y = mpin_ZZZ.random_generate(rng) + rtn, y=mpin_ZZZ.random_generate(rng) if rtn != 0: - print "random_generate(rng) Error %s" % rtn + print("random_generate(rng) Error {}".format(rtn)) # Client second pass time_func('rtn, v = mpin_ZZZ.client_2(x, y, sec)', nIter) - rtn, v = mpin_ZZZ.client_2(x, y, sec) + rtn, v=mpin_ZZZ.client_2(x, y, sec) if rtn != 0: - print "client_2(x, y, sec) Error %s" % rtn + print("client_2(x, y, sec) Error {}".format(rtn)) # Server second pass time_func( 'rtn, E, F = mpin_ZZZ.server_2(date, HID, HTID, y, server_secret, u, ut, v, None)', nIter) - rtn, E, F = mpin_ZZZ.server_2( + rtn, E, F=mpin_ZZZ.server_2( date, HID, HTID, y, server_secret, u, ut, v, None) if rtn != 0: - print "ERROR: %s is not authenticated" % mpin_id + print("ERROR: {} is not authenticated".format(mpin_id)) if PIN_ERROR: time_func('err = mpin_ZZZ.kangaroo(E, F)', nIter) - err = mpin_ZZZ.kangaroo(E, F) - print "Client PIN error %d " % err + err=mpin_ZZZ.kangaroo(E, F) + print("Client PIN error {} ".format(err)) raise SystemExit(0) else: - print "SUCCESS: %s is authenticated" % mpin_id + print("SUCCESS: {} is authenticated".format(mpin_id)) # Client sends Z=r.ID to Server if MPIN_FULL: - rtn, r, Z = mpin_ZZZ.get_G1_multiple(rng, 1, None, hash_mpin_id) + rtn, r, Z=mpin_ZZZ.get_G1_multiple(rng, 1, None, hash_mpin_id) if rtn != 0: - print "ERROR: Generating Z %s" % rtn + print("ERROR: Generating Z {}".format(rtn)) if date: - prHID = HTID + prHID=HTID else: - prHID = HID - ut = None + prHID=HID + ut=None # Server sends T=w.ID to client if MPIN_FULL: time_func( 'rtn, w, T = mpin_ZZZ.get_G1_multiple(rng, 0, None, prHID)', nIter) - rtn, w, T = mpin_ZZZ.get_G1_multiple(rng, 0, None, prHID) + rtn, w, T=mpin_ZZZ.get_G1_multiple(rng, 0, None, prHID) if rtn != 0: - print "ERROR: Generating T %s" % rtn + print("ERROR: Generating T {}".format(rtn)) time_func( 'HM = mpin_ZZZ.hash_all(mpin_ZZZ.HASH_TYPE_ZZZ, hash_mpin_id, u, ut, v, y, Z, T)', nIter) - HM = mpin_ZZZ.hash_all( + HM=mpin_ZZZ.hash_all( mpin_ZZZ.HASH_TYPE_ZZZ, hash_mpin_id, u, ut, v, y, Z, T) time_func( 'rtn, client_aes_key = mpin_ZZZ.client_key(mpin_ZZZ.HASH_TYPE_ZZZ, pc1, pc2, PIN, r, x, HM, T)', nIter) - rtn, client_aes_key = mpin_ZZZ.client_key( + rtn, client_aes_key=mpin_ZZZ.client_key( mpin_ZZZ.HASH_TYPE_ZZZ, pc1, pc2, PIN, r, x, HM, T) if rtn != 0: - print "ERROR: Generating client_aes_key %s" % rtn - print "Client AES Key: %s" % client_aes_key.encode("hex") + print("ERROR: Generating client_aes_key {}".format(rtn)) + print("Client AES Key: {}".format(client_aes_key.hex())) time_func( 'rtn, server_aes_key = mpin_ZZZ.server_key(mpin_ZZZ.HASH_TYPE_ZZZ, Z, server_secret, w, HM, HID, u, ut)', nIter) - rtn, server_aes_key = mpin_ZZZ.server_key( + rtn, server_aes_key=mpin_ZZZ.server_key( mpin_ZZZ.HASH_TYPE_ZZZ, Z, server_secret, w, HM, HID, u, ut) if rtn != 0: - print "ERROR: Generating server_aes_key %s" % rtn - print "Server AES Key: %s" % server_aes_key.encode("hex") + print("ERROR: Generating server_aes_key {}".format(rtn)) + print("Server AES Key: {}".format(server_aes_key.hex())) if MPIN_FULL: - plaintext = "A test message" - print "message to encrypt: ", plaintext - header_hex = "1554a69ecbf04e507eb6985a234613246206c85f8af73e61ab6e2382a26f457d" - header = header_hex.decode("hex") - iv_hex = "2b213af6b0edf6972bf996fb" - iv = iv_hex.decode("hex") + plaintext="A test message" + print("message to encrypt: ", plaintext) + header_hex="1554a69ecbf04e507eb6985a234613246206c85f8af73e61ab6e2382a26f457d" + header=header_hex.decode("hex") + iv_hex="2b213af6b0edf6972bf996fb" + iv=iv_hex.decode("hex") time_func( 'ciphertext, tag = mpin_ZZZ.aes_gcm_encrypt(client_aes_key, iv, header, plaintext)', nIter) - ciphertext, tag = mpin_ZZZ.aes_gcm_encrypt( + ciphertext, tag=mpin_ZZZ.aes_gcm_encrypt( client_aes_key, iv, header, plaintext) - print "ciphertext ", ciphertext.encode("hex") - print "tag1 ", tag.encode("hex") + print("ciphertext ", ciphertext.hex()) + print("tag1 ", tag.hex()) time_func( 'plaintext2, tag2 = mpin_ZZZ.aes_gcm_decrypt(server_aes_key, iv, header, ciphertext)', nIter) - plaintext2, tag2 = mpin_ZZZ.aes_gcm_decrypt( + plaintext2, tag2=mpin_ZZZ.aes_gcm_decrypt( server_aes_key, iv, header, ciphertext) - print "decrypted message: ", plaintext2 - print "tag2 ", tag2.encode("hex") + print("decrypted message: ".format(plaintext2)) + print("tag2 {}".format(tag2.hex())) diff --git a/wrappers/python/bls_ZZZ.py.in b/python/bls_ZZZ.py.in similarity index 95% rename from wrappers/python/bls_ZZZ.py.in rename to python/bls_ZZZ.py.in index 62903dc..20618fc 100755 --- a/wrappers/python/bls_ZZZ.py.in +++ b/python/bls_ZZZ.py.in @@ -93,11 +93,10 @@ if CURVE_SECURITY == 256: G2LEN = 16 * BFS -def to_hex(octet_value): +def to_str(octet_value): """Converts an octet type into a string - Add all the values in an octet into an array. This arrays is then - converted to a string and hex encoded. + Add all the values in an octet into an array. Args:: @@ -118,7 +117,7 @@ def to_hex(octet_value): out = b'' for x in val: out = out + x - return out.hex() + return out def make_octet(length, value=None): @@ -228,15 +227,13 @@ def key_pair_generate(rng, sk=None): pk1, pk1val = make_octet(G2LEN) error_code = libamcl_bls_ZZZ.BLS_ZZZ_KEY_PAIR_GENERATE(rng, sk1, pk1) - sk_hex = to_hex(sk1) - pk_hex = to_hex(pk1) + sk = to_str(sk1) + pk = to_str(pk1) # clear memory libamcl_core.OCT_clear(sk1) libamcl_core.OCT_clear(pk1) - sk = bytes.fromhex(sk_hex) - pk = bytes.fromhex(pk_hex) return error_code, sk, pk @@ -262,13 +259,12 @@ def sign(message, sk): signature1, signature1_val = make_octet(G1LEN) error_code = libamcl_bls_ZZZ.BLS_ZZZ_SIGN(signature1, message, sk1) - signature_hex = to_hex(signature1) + signature = to_str(signature1) # clear memory libamcl_core.OCT_clear(sk1) libamcl_core.OCT_clear(signature1) - signature = bytes.fromhex(signature_hex) return error_code, signature @@ -324,14 +320,13 @@ def add_G1(R1, R2): R1, R1_val = make_octet(G1LEN) error_code = libamcl_bls_ZZZ.BLS_ZZZ_ADD_G1(R11, R21, R1) - R_hex = to_hex(R1) + R = to_str(R1) # clear memory libamcl_core.OCT_clear(R11) libamcl_core.OCT_clear(R21) libamcl_core.OCT_clear(R1) - R = bytes.fromhex(R_hex) return error_code, R @@ -358,14 +353,13 @@ def add_G2(R1, R2): R1, R1_val = make_octet(G2LEN) error_code = libamcl_bls_ZZZ.BLS_ZZZ_ADD_G2(R11, R21, R1) - R_hex = to_hex(R1) + R = to_str(R1) # clear memory libamcl_core.OCT_clear(R11) libamcl_core.OCT_clear(R21) libamcl_core.OCT_clear(R1) - R = bytes.fromhex(R_hex) return error_code, R @@ -392,14 +386,13 @@ def mult_G1(P, R): T1, T1_val = make_octet(G1LEN) error_code = libamcl_bls_ZZZ.BLS_ZZZ_MUL_G1(P1, R1, T1) - T_hex = to_hex(T1) + T = to_str(T1) # clear memory libamcl_core.OCT_clear(P1) libamcl_core.OCT_clear(R1) libamcl_core.OCT_clear(T1) - T = bytes.fromhex(T_hex) return error_code, T @@ -426,14 +419,13 @@ def mult_G2(P, R): T1, T1_val = make_octet(G2LEN) error_code = libamcl_bls_ZZZ.BLS_ZZZ_MUL_G2(P1, R1, T1) - T_hex = to_hex(T1) + T = to_str(T1) # clear memory libamcl_core.OCT_clear(P1) libamcl_core.OCT_clear(R1) libamcl_core.OCT_clear(T1) - T = bytes.fromhex(T_hex) return error_code, T diff --git a/wrappers/python/mpin_ZZZ.py.in b/python/mpin_ZZZ.py.in similarity index 85% rename from wrappers/python/mpin_ZZZ.py.in rename to python/mpin_ZZZ.py.in index 944a609..6e84805 100644 --- a/wrappers/python/mpin_ZZZ.py.in +++ b/python/mpin_ZZZ.py.in @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Licensed to the Apache Software Foundation (ASF) under one @@ -129,11 +129,10 @@ GT = 3 * G2 IVL = 12 -def to_hex(octet_value): +def to_str(octet_value): """Converts an octet type into a string - Add all the values in an octet into an array. This arrays is then - converted to a string and hex encoded. + Add all the values in an octet into an array. Args:: @@ -151,7 +150,10 @@ def to_hex(octet_value): while i < octet_value.len: val.append(octet_value.val[i]) i = i + 1 - return ''.join(val).encode("hex") + out = b'' + for x in val: + out = out + x + return out def make_octet(length, value=None): @@ -288,13 +290,13 @@ def hash_id(hash_type, mpin_id): hash_mpin_id1, hash_mpin_id1_val = make_octet(PFS) libamcl_core.HASH_ID(hash_type, mpin_id1, hash_mpin_id1) - hash_mpin_id_hex = to_hex(hash_mpin_id1) + hash_mpin_id = to_str(hash_mpin_id1) # clear memory libamcl_core.OCT_clear(mpin_id1) libamcl_core.OCT_clear(hash_mpin_id1) - return hash_mpin_id_hex.decode("hex") + return hash_mpin_id def random_generate(rng): @@ -317,12 +319,12 @@ def random_generate(rng): s1, s_val = make_octet(PGS) error_code = libamcl_mpin_ZZZ.MPIN_ZZZ_RANDOM_GENERATE(rng, s1) - s_hex = to_hex(s1) + s = to_str(s1) # clear memory libamcl_core.OCT_clear(s1) - return error_code, s_hex.decode("hex") + return error_code, s def get_dvs_keypair(rng): @@ -348,14 +350,14 @@ def get_dvs_keypair(rng): error_code = libamcl_mpin_ZZZ.MPIN_ZZZ_GET_DVS_KEYPAIR(rng, z, pa) - pa_hex = to_hex(pa) - z_hex = to_hex(z) + pa = to_str(pa) + z = to_str(z) # clear memory libamcl_core.OCT_clear(z) libamcl_core.OCT_clear(pa) - return error_code, z_hex.decode("hex"), pa_hex.decode("hex") + return error_code, z, pa def get_server_secret(master_secret): @@ -380,13 +382,13 @@ def get_server_secret(master_secret): error_code = libamcl_mpin_ZZZ.MPIN_ZZZ_GET_SERVER_SECRET( master_secret1, server_secret1) - server_secret_hex = to_hex(server_secret1) + server_secret = to_str(server_secret1) # clear memory libamcl_core.OCT_clear(master_secret1) libamcl_core.OCT_clear(server_secret1) - return error_code, server_secret_hex.decode("hex") + return error_code, server_secret def recombine_G2(W1, W2): @@ -412,14 +414,14 @@ def recombine_G2(W1, W2): w1, w1_val = make_octet(G2) error_code = libamcl_mpin_ZZZ.MPIN_ZZZ_RECOMBINE_G2(w11, w21, w1) - w_hex = to_hex(w1) + w = to_str(w1) # clear memory libamcl_core.OCT_clear(w11) libamcl_core.OCT_clear(w21) libamcl_core.OCT_clear(w1) - return error_code, w_hex.decode("hex") + return error_code, w def get_client_secret(master_secret, hash_mpin_id): @@ -446,14 +448,14 @@ def get_client_secret(master_secret, hash_mpin_id): error_code = libamcl_mpin_ZZZ.MPIN_ZZZ_GET_CLIENT_SECRET( master_secret1, hash_mpin_id1, client_secret1) - client_secret_hex = to_hex(client_secret1) + client_secret = to_str(client_secret1) # clear memory libamcl_core.OCT_clear(master_secret1) libamcl_core.OCT_clear(hash_mpin_id1) libamcl_core.OCT_clear(client_secret1) - return error_code, client_secret_hex.decode("hex") + return error_code, client_secret def recombine_G1(q1, q2): @@ -479,14 +481,14 @@ def recombine_G1(q1, q2): q1, q1_val = make_octet(G1) error_code = libamcl_mpin_ZZZ.MPIN_ZZZ_RECOMBINE_G1(q11, q21, q1) - q_hex = to_hex(q1) + q = to_str(q1) # clear memory libamcl_core.OCT_clear(q11) libamcl_core.OCT_clear(q21) libamcl_core.OCT_clear(q1) - return error_code, q_hex.decode("hex") + return error_code, q def get_client_permit(hash_type, epoch_date, master_secret, hash_mpin_id): @@ -518,14 +520,14 @@ def get_client_permit(hash_type, epoch_date, master_secret, hash_mpin_id): hash_mpin_id1, time_permit1) - time_permit_hex = to_hex(time_permit1) + time_permit = to_str(time_permit1) # clear memory libamcl_core.OCT_clear(master_secret1) libamcl_core.OCT_clear(hash_mpin_id1) libamcl_core.OCT_clear(time_permit1) - return error_code, time_permit_hex.decode("hex") + return error_code, time_permit def extract_pin(hash_type, mpin_id, pin, client_secret): @@ -553,13 +555,13 @@ def extract_pin(hash_type, mpin_id, pin, client_secret): error_code = libamcl_mpin_ZZZ.MPIN_ZZZ_EXTRACT_PIN( hash_type, mpin_id1, pin, client_secret1) - client_secret_hex = to_hex(client_secret1) + client_secret = to_str(client_secret1) # clear memory libamcl_core.OCT_clear(mpin_id1) libamcl_core.OCT_clear(client_secret1) - return error_code, client_secret_hex.decode("hex") + return error_code, client_secret def precompute(token, hash_mpin_id): @@ -588,8 +590,8 @@ def precompute(token, hash_mpin_id): error_code = libamcl_mpin_ZZZ.MPIN_ZZZ_PRECOMPUTE( token1, hash_mpin_id1, ffi.NULL, pc11, pc21) - pc1_hex = to_hex(pc11) - pc2_hex = to_hex(pc21) + pc1 = to_str(pc11) + pc2 = to_str(pc21) # clear memory libamcl_core.OCT_clear(token1) @@ -597,7 +599,7 @@ def precompute(token, hash_mpin_id): libamcl_core.OCT_clear(pc11) libamcl_core.OCT_clear(pc21) - return error_code, pc1_hex.decode("hex"), pc2_hex.decode("hex") + return error_code, pc1, pc2 def client_1(hash_type, epoch_date, mpin_id, rng, x, pin, token, time_permit): @@ -660,10 +662,10 @@ def client_1(hash_type, epoch_date, mpin_id, rng, x, pin, token, time_permit): ut1, time_permit1) - x_hex = to_hex(x1) - u_hex = to_hex(u1) - ut_hex = to_hex(ut1) - v_hex = to_hex(v1) + x = to_str(x1) + u = to_str(u1) + ut = to_str(ut1) + v = to_str(v1) # clear memory if time_permit: @@ -675,8 +677,7 @@ def client_1(hash_type, epoch_date, mpin_id, rng, x, pin, token, time_permit): libamcl_core.OCT_clear(ut1) libamcl_core.OCT_clear(v1) - return error_code, x_hex.decode("hex"), u_hex.decode( - "hex"), ut_hex.decode("hex"), v_hex.decode("hex") + return error_code, x, u, ut, v def client_2(x, y, sec): @@ -703,14 +704,14 @@ def client_2(x, y, sec): sec1, sec1_val = make_octet(None, sec) error_code = libamcl_mpin_ZZZ.MPIN_ZZZ_CLIENT_2(x1, y1, sec1) - sec_hex = to_hex(sec1) + sec = to_str(sec1) # clear memory libamcl_core.OCT_clear(x1) libamcl_core.OCT_clear(y1) libamcl_core.OCT_clear(sec1) - return error_code, sec_hex.decode("hex") + return error_code, sec def client(hash_type, epoch_date, mpin_id, rng, x, pin, token, @@ -784,11 +785,11 @@ def client(hash_type, epoch_date, mpin_id, rng, x, pin, token, epoch_time, y1) - x_hex = to_hex(x1) - u_hex = to_hex(u1) - ut_hex = to_hex(ut1) - v_hex = to_hex(v1) - y_hex = to_hex(y1) + x = to_str(x1) + u = to_str(u1) + ut = to_str(ut1) + v = to_str(v1) + y = to_str(y1) # clear memory if time_permit: @@ -803,8 +804,7 @@ def client(hash_type, epoch_date, mpin_id, rng, x, pin, token, libamcl_core.OCT_clear(v1) libamcl_core.OCT_clear(y1) - return error_code, x_hex.decode("hex"), u_hex.decode( - "hex"), ut_hex.decode("hex"), v_hex.decode("hex"), y_hex.decode("hex") + return error_code, x, u, ut, v, y def get_G1_multiple(rng, type, x, P): @@ -842,15 +842,15 @@ def get_G1_multiple(rng, type, x, P): error_code = libamcl_mpin_ZZZ.MPIN_ZZZ_GET_G1_MULTIPLE( rng_in, type, x1, P1, W1) - x_hex = to_hex(x1) - W_hex = to_hex(W1) + x = to_str(x1) + W = to_str(W1) # clear memory libamcl_core.OCT_clear(x1) libamcl_core.OCT_clear(P1) libamcl_core.OCT_clear(W1) - return error_code, x_hex.decode("hex"), W_hex.decode("hex") + return error_code, x, W def server_1(hash_type, epoch_date, mpin_id): @@ -882,15 +882,15 @@ def server_1(hash_type, epoch_date, mpin_id): libamcl_mpin_ZZZ.MPIN_ZZZ_SERVER_1( hash_type, epoch_date, mpin_id1, HID1, HTID1) - HID_hex = to_hex(HID1) - HTID_hex = to_hex(HTID1) + HID = to_str(HID1) + HTID = to_str(HTID1) # clear memory libamcl_core.OCT_clear(mpin_id1) libamcl_core.OCT_clear(HTID1) libamcl_core.OCT_clear(HID1) - return HID_hex.decode("hex"), HTID_hex.decode("hex") + return HID, HTID def server_2(epoch_date, HID, HTID, y, server_secret, u, ut, v, pa): @@ -951,8 +951,8 @@ def server_2(epoch_date, HID, HTID, y, server_secret, u, ut, v, pa): f1, pa1) - e_hex = to_hex(e1) - f_hex = to_hex(f1) + e = to_str(e1) + f = to_str(f1) # clear memory libamcl_core.OCT_clear(HID1) @@ -965,7 +965,7 @@ def server_2(epoch_date, HID, HTID, y, server_secret, u, ut, v, pa): libamcl_core.OCT_clear(e1) libamcl_core.OCT_clear(f1) - return error_code, e_hex.decode("hex"), f_hex.decode("hex") + return error_code, e, f def server( @@ -1049,11 +1049,11 @@ def server( epoch_time, pa1) - HID_hex = to_hex(HID1) - HTID_hex = to_hex(HTID1) - e_hex = to_hex(e1) - f_hex = to_hex(f1) - y_hex = to_hex(y1) + HID = to_str(HID1) + HTID = to_str(HTID1) + e = to_str(e1) + f = to_str(f1) + y = to_str(y1) # clear memory if message: @@ -1069,8 +1069,7 @@ def server( libamcl_core.OCT_clear(f1) libamcl_core.OCT_clear(y1) - return error_code, HID_hex.decode("hex"), HTID_hex.decode( - "hex"), e_hex.decode("hex"), f_hex.decode("hex"), y_hex.decode("hex") + return error_code, HID, HTID, e, f, y def kangaroo(e, f): @@ -1139,7 +1138,7 @@ def hash_all(hash_type, hash_mpin_id, u, ut, v, y, z, t): libamcl_core.HASH_ALL(hash_type, hash_mpin_id1, u1, ut1, v1, y1, z1, t1, hm1) - hm_hex = to_hex(hm1) + hm = to_str(hm1) # clear memory if ut: @@ -1151,7 +1150,7 @@ def hash_all(hash_type, hash_mpin_id, u, ut, v, y, z, t): libamcl_core.OCT_clear(z1) libamcl_core.OCT_clear(t1) - return hm_hex.decode("hex") + return hm def client_key(hash_type, pc1, pc2, pin, r, x, hm, t): @@ -1195,7 +1194,7 @@ def client_key(hash_type, pc1, pc2, pin, r, x, hm, t): t1, client_aes_key1) - client_aes_key_hex = to_hex(client_aes_key1) + client_aes_key = to_str(client_aes_key1) # clear memory libamcl_core.OCT_clear(pc11) @@ -1206,7 +1205,7 @@ def client_key(hash_type, pc1, pc2, pin, r, x, hm, t): libamcl_core.OCT_clear(t1) libamcl_core.OCT_clear(client_aes_key1) - return error_code, client_aes_key_hex.decode("hex") + return error_code, client_aes_key def server_key(hash_type, z, server_secret, w, hm, HID, u, ut): @@ -1256,7 +1255,7 @@ def server_key(hash_type, z, server_secret, w, hm, HID, u, ut): ut1, server_aes_key1) - server_aes_key_hex = to_hex(server_aes_key1) + server_aes_key = to_str(server_aes_key1) # clear memory if ut: @@ -1269,7 +1268,7 @@ def server_key(hash_type, z, server_secret, w, hm, HID, u, ut): libamcl_core.OCT_clear(u1) libamcl_core.OCT_clear(server_aes_key1) - return error_code, server_aes_key_hex.decode("hex") + return error_code, server_aes_key def aes_gcm_encrypt(aes_key, iv, header, plaintext): @@ -1307,8 +1306,8 @@ def aes_gcm_encrypt(aes_key, iv, header, plaintext): plaintext1, ciphertext1, tag1) - tag = to_hex(tag1) - ciphertext = to_hex(ciphertext1) + tag = to_str(tag1) + ciphertext = to_str(ciphertext1) # clear memory libamcl_core.OCT_clear(aes_key1) @@ -1318,7 +1317,7 @@ def aes_gcm_encrypt(aes_key, iv, header, plaintext): libamcl_core.OCT_clear(tag1) libamcl_core.OCT_clear(ciphertext1) - return ciphertext.decode("hex"), tag.decode("hex") + return ciphertext, tag def aes_gcm_decrypt(aes_key, iv, header, ciphertext): @@ -1356,8 +1355,8 @@ def aes_gcm_decrypt(aes_key, iv, header, ciphertext): plaintext1, tag1) - tag = to_hex(tag1) - plaintext = to_hex(plaintext1) + tag = to_str(tag1) + plaintext = to_str(plaintext1) # clear memory libamcl_core.OCT_clear(aes_key1) @@ -1367,7 +1366,7 @@ def aes_gcm_decrypt(aes_key, iv, header, ciphertext): libamcl_core.OCT_clear(tag1) libamcl_core.OCT_clear(ciphertext1) - return plaintext.decode("hex"), tag.decode("hex") + return plaintext, tag def generate_otp(rng): @@ -1411,12 +1410,12 @@ def generate_random(rng, length): random_value1, random_value1_val = make_octet(length) libamcl_mpin_ZZZ.generateRandom(rng, random_value1) - random_value_hex = to_hex(random_value1) + random_value = to_str(random_value1) # clear memory libamcl_core.OCT_clear(random_value1) - return random_value_hex.decode("hex") + return random_value if __name__ == "__main__": @@ -1437,127 +1436,129 @@ if __name__ == "__main__": date = 0 # Seed - seedHex = "b75e7857fa17498c333d3c8d42e10f8c3cb8a66f7a84d85f86cd5acb537fa211" - seed = seedHex.decode("hex") + seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30" + seed = bytes.fromhex(seed_hex) # random number generator rng = create_csprng(seed) # Identity if INPUT: - mpin_id = raw_input("Please enter identity:") + mpin_id = input("Please enter identity:") else: mpin_id = "[email protected]" # Hash mpin_id - hash_mpin_id = hash_id(HASH_TYPE_ZZZ, mpin_id) + hash_mpin_id = hash_id(HASH_TYPE_ZZZ, mpin_id.encode("utf-8")) if DEBUG: - print "mpin_id: %s" % mpin_id.encode("hex") - print "hash_mpin_id: %s" % hash_mpin_id.encode("hex") + print("mpin_id: {}".format(mpin_id.encode("utf-8").hex())) + print("hash_mpin_id: {}".format(hash_mpin_id.hex())) # Generate master secret for MILAGRO and Customer rtn, ms1 = random_generate(rng) if rtn != 0: - print "random_generate(rng) Error %s", rtn + print("random_generate(rng) Error {}".format(rtn)) rtn, ms2 = random_generate(rng) if rtn != 0: - print "random_generate(rng) Error %s", rtn + print("random_generate(rng) Error {}".format(rtn)) if DEBUG: - print "ms1: %s" % ms1.encode("hex") - print "ms2: %s" % ms2.encode("hex") + print("ms1: {}".format(ms1.hex())) + print("ms2: {}".format(ms2.hex())) # Generate server secret shares rtn, ss1 = get_server_secret(ms1) if rtn != 0: - print "get_server_secret(ms1) Error %s" % rtn + print("get_server_secret(ms1) Error {}".format(rtn)) rtn, ss2 = get_server_secret(ms2) if rtn != 0: - print "get_server_secret(ms2) Error %s" % rtn + print("get_server_secret(ms2) Error {}".format(rtn)) if DEBUG: - print "ss1: %s" % ss1.encode("hex") - print "ss2: %s" % ss2.encode("hex") + print("ss1: {}".format(ss1.hex())) + print("ss2: {}".format(ss2.hex())) # Combine server secret shares rtn, server_secret = recombine_G2(ss1, ss2) if rtn != 0: - print "recombine_G2(ss1, ss2) Error %s" % rtn + print("recombine_G2(ss1, ss2) Error {}".format(rtn)) if DEBUG: - print "server_secret: %s" % server_secret.encode("hex") + print("server_secret: {}".format(server_secret.hex())) # Generate client secret shares rtn, cs1 = get_client_secret(ms1, hash_mpin_id) if rtn != 0: - print "get_client_secret(ms1, hash_mpin_id) Error %s" % rtn + print("get_client_secret(ms1, hash_mpin_id) Error {}".format(rtn)) rtn, cs2 = get_client_secret(ms2, hash_mpin_id) if rtn != 0: - print "get_client_secret(ms2, hash_mpin_id) Error %s" % rtn + print("get_client_secret(ms2, hash_mpin_id) Error {}".format(rtn)) if DEBUG: - print "cs1: %s" % cs1.encode("hex") - print "cs2: %s" % cs2.encode("hex") + print("cs1: {}".format(cs1.hex())) + print("cs2: {}".format(cs2.hex())) # Combine client secret shares rtn, client_secret = recombine_G1(cs1, cs2) if rtn != 0: - print "recombine_G1(cs1, cs2) Error %s" % rtn - print "Client Secret: %s" % client_secret.encode("hex") + print("recombine_G1(cs1, cs2) Error {}".format(rtn)) + print("Client Secret: {}".format(client_secret.hex())) if TIME_PERMITS: # Generate Time Permit shares if DEBUG: - print "Date %s" % date + print("Date {}".format(date)) rtn, tp1 = get_client_permit( HASH_TYPE_ZZZ, date, ms1, hash_mpin_id) if rtn != 0: - print "get_client_permit(HASH_TYPE_ZZZ, date, ms1, hash_mpin_id) Error %s" % rtn + print( + "get_client_permit(HASH_TYPE_ZZZ, date, ms1, hash_mpin_id) Error {}".format(rtn)) rtn, tp2 = get_client_permit( HASH_TYPE_ZZZ, date, ms2, hash_mpin_id) if rtn != 0: - print "get_client_permit(HASH_TYPE_ZZZ, date, ms2, hash_mpin_id) Error %s" % rtn + print( + "get_client_permit(HASH_TYPE_ZZZ, date, ms2, hash_mpin_id) Error {}".format(rtn)) if DEBUG: - print "tp1: %s" % tp1.encode("hex") - print "tp2: %s" % tp2.encode("hex") + print("tp1: {}".format(tp1.hex())) + print("tp2: {}".format(tp2.hex())) # Combine Time Permit shares rtn, time_permit = recombine_G1(tp1, tp2) if rtn != 0: - print "recombine_G1(tp1, tp2) Error %s" % rtn + print("recombine_G1(tp1, tp2) Error {}".format(rtn)) if DEBUG: - print "time_permit: %s" % time_permit.encode("hex") + print("time_permit: {}".format(time_permit.hex())) else: time_permit = None # Client extracts PIN from secret to create Token if INPUT: - PIN = int( - raw_input("Please enter four digit PIN to create M-Pin Token:")) + PIN = int(input("Please enter four digit PIN to create M-Pin Token:")) else: PIN = 1234 - rtn, token = extract_pin(HASH_TYPE_ZZZ, mpin_id, PIN, client_secret) + rtn, token = extract_pin( + HASH_TYPE_ZZZ, mpin_id.encode("utf-8"), PIN, client_secret) if rtn != 0: - print "extract_pin(HASH_TYPE_ZZZ, mpin_id, PIN, token) Error %s" % rtn - print "Token: %s" % token.encode("hex") + print("extract_pin(HASH_TYPE_ZZZ, mpin_id, PIN, token) Error {}".format(rtn)) + print("Token: {}".format(token.hex())) if ONE_PASS: - print "M-Pin One Pass" + print("M-Pin One Pass") if INPUT: - PIN = int(raw_input("Please enter PIN to authenticate:")) + PIN = int(input("Please enter PIN to authenticate:")) else: PIN = 1234 epoch_time = get_time() if DEBUG: - print "epoch_time %s" % epoch_time + print("epoch_time {}".format(epoch_time)) # Client precomputation if MPIN_ZZZ_FULL: rtn, pc1, pc2 = precompute(token, hash_mpin_id) # Client MPIN - rtn, x, u, ut, v, y = client( - HASH_TYPE_ZZZ, date, mpin_id, rng, None, PIN, token, time_permit, None, epoch_time) + rtn, x, u, ut, v, y = client(HASH_TYPE_ZZZ, date, mpin_id.encode( + "utf-8"), rng, None, PIN, token, time_permit, None, epoch_time) if DEBUG: - print "y1 ", y.encode("hex") + print("y1 {}".format(y.hex())) if rtn != 0: - print "MPIN_ZZZ_CLIENT ERROR %s" % rtn + print("MPIN_ZZZ_CLIENT ERROR {}".format(rtn)) # Client sends Z=r.ID to Server if MPIN_ZZZ_FULL: @@ -1565,17 +1566,17 @@ if __name__ == "__main__": # Server MPIN rtn, HID, HTID, E, F, y2 = server( - HASH_TYPE_ZZZ, date, server_secret, u, ut, v, mpin_id, None, epoch_time, None) + HASH_TYPE_ZZZ, date, server_secret, u, ut, v, mpin_id.encode("utf-8"), None, epoch_time, None) if DEBUG: - print "y2 ", y2.encode("hex") + print("y2 {}".format(y2.hex())) if rtn != 0: - print "ERROR: %s is not authenticated" % mpin_id + print("ERROR: {} is not authenticated".format(mpin_id)) if PIN_ERROR: err = kangaroo(E, F) - print "Client PIN error %d " % err + print("Client PIN error {} ".format(err)) raise SystemExit(0) else: - print "SUCCESS: %s is authenticated" % mpin_id + print("SUCCESS: {} is authenticated".format(mpin_id)) if date: prHID = HTID @@ -1587,7 +1588,7 @@ if __name__ == "__main__": if MPIN_ZZZ_FULL: rtn, w, T = get_G1_multiple(rng, 0, None, prHID) if rtn != 0: - print "ERROR: Generating T %s" % rtn + print("ERROR: Generating T {}".format(rtn)) if MPIN_ZZZ_FULL: HM = hash_all(HASH_TYPE_ZZZ, hash_mpin_id, u, ut, v, y, Z, T) @@ -1595,64 +1596,64 @@ if __name__ == "__main__": rtn, client_aes_key = client_key( HASH_TYPE_ZZZ, pc1, pc2, PIN, r, x, HM, T) if rtn != 0: - print "ERROR: Generating client_aes_key %s" % rtn - print "Client AES Key: %s" % client_aes_key.encode("hex") + print("ERROR: Generating client_aes_key {}".format(rtn)) + print("Client AES Key: {}".format(client_aes_key.hex())) rtn, server_aes_key = server_key( HASH_TYPE_ZZZ, Z, server_secret, w, HM, HID, u, ut) if rtn != 0: - print "ERROR: Generating server_aes_key %s" % rtn - print "Server AES Key: %s" % server_aes_key.encode("hex") + print("ERROR: Generating server_aes_key {}".format(rtn)) + print("Server AES Key: {}".format(server_aes_key.hex())) else: - print "M-Pin Three Pass" + print("M-Pin Three Pass") if INPUT: - PIN = int(raw_input("Please enter PIN to authenticate:")) + PIN = int(input("Please enter PIN to authenticate:")) else: PIN = 1234 if MPIN_ZZZ_FULL: rtn, pc1, pc2 = precompute(token, hash_mpin_id) if rtn != 0: - print "precompute(token, hash_mpin_id) ERROR %s" % rtn + print("precompute(token, hash_mpin_id) ERROR {}".format(rtn)) # Client first pass - rtn, x, u, ut, sec = client_1( - HASH_TYPE_ZZZ, date, mpin_id, rng, None, PIN, token, time_permit) + rtn, x, u, ut, sec = client_1(HASH_TYPE_ZZZ, date, mpin_id.encode( + "utf-8"), rng, None, PIN, token, time_permit) if rtn != 0: - print "client_1 ERROR %s" % rtn + print("client_1 ERROR {}".format(rtn)) if DEBUG: - print "x: %s" % x.encode("hex") + print("x: {}".format(x.hex())) # Server calculates H(ID) and H(T|H(ID)) (if time permits enabled), # and maps them to points on the curve HID and HTID resp. - HID, HTID = server_1(HASH_TYPE_ZZZ, date, mpin_id) + HID, HTID = server_1(HASH_TYPE_ZZZ, date, mpin_id.encode("utf-8")) # Server generates Random number y and sends it to Client rtn, y = random_generate(rng) if rtn != 0: - print "random_generate(rng) Error %s" % rtn + print("random_generate(rng) Error {}".format(rtn)) # Client second pass rtn, v = client_2(x, y, sec) if rtn != 0: - print "client_2(x, y, sec) Error %s" % rtn + print("client_2(x, y, sec) Error {}".format(rtn)) # Server second pass rtn, E, F = server_2(date, HID, HTID, y, server_secret, u, ut, v, None) if rtn != 0: - print "ERROR: %s is not authenticated" % mpin_id + print("ERROR: {} is not authenticated".format(mpin_id)) if PIN_ERROR: err = kangaroo(E, F) - print "Client PIN error %d " % err + print("Client PIN error {}".format(err)) raise SystemExit(0) else: - print "SUCCESS: %s is authenticated" % mpin_id + print("SUCCESS: {} is authenticated".format(mpin_id)) # Client sends Z=r.ID to Server if MPIN_ZZZ_FULL: rtn, r, Z = get_G1_multiple(rng, 1, None, hash_mpin_id) if rtn != 0: - print "ERROR: Generating Z %s" % rtn + print("ERROR: Generating Z {}".format(rtn)) if date: prHID = HTID @@ -1664,38 +1665,38 @@ if __name__ == "__main__": if MPIN_ZZZ_FULL: rtn, w, T = get_G1_multiple(rng, 0, None, prHID) if rtn != 0: - print "ERROR: Generating T %s" % rtn + print("ERROR: Generating T {}".format(rtn)) HM = hash_all(HASH_TYPE_ZZZ, hash_mpin_id, u, ut, v, y, Z, T) rtn, client_aes_key = client_key( HASH_TYPE_ZZZ, pc1, pc2, PIN, r, x, HM, T) if rtn != 0: - print "ERROR: Generating client_aes_key %s" % rtn - print "Client AES Key: %s" % client_aes_key.encode("hex") + print("ERROR: Generating client_aes_key {}".format(rtn)) + print("Client AES Key: {}".format(client_aes_key.hex())) rtn, server_aes_key = server_key( HASH_TYPE_ZZZ, Z, server_secret, w, HM, HID, u, ut) if rtn != 0: - print "ERROR: Generating server_aes_key %s" % rtn - print "Server AES Key: %s" % server_aes_key.encode("hex") + print("ERROR: Generating server_aes_key {}".format(rtn)) + print("Server AES Key: {}".format(server_aes_key.hex())) if MPIN_ZZZ_FULL: plaintext = "A test message" - print "message to encrypt: ", plaintext + print("message to encrypt: {}".format(plaintext)) header_hex = "1554a69ecbf04e507eb6985a234613246206c85f8af73e61ab6e2382a26f457d" - header = header_hex.decode("hex") + header = bytes.fromhex(header_hex) iv_hex = "2b213af6b0edf6972bf996fb" - iv = iv_hex.decode("hex") + iv = bytes.fromhex(iv_hex) ciphertext, tag = aes_gcm_encrypt( - client_aes_key, iv, header, plaintext) - print "ciphertext ", ciphertext.encode("hex") - print "tag1 ", tag.encode("hex") + client_aes_key, iv, header, plaintext.encode("utf-8")) + print("ciphertext {}".format(ciphertext.hex())) + print("tag1 {}".format(tag.hex())) plaintext2, tag2 = aes_gcm_decrypt( server_aes_key, iv, header, ciphertext) - print "decrypted message: ", plaintext2 - print "tag2 ", tag2.encode("hex") + print("decrypted message: {}".format(plaintext2.decode("utf-8"))) + print("tag2 {}".format(tag2.hex())) # Clear memory del seed diff --git a/wrappers/python/wcc_ZZZ.py.in b/python/wcc_ZZZ.py.in similarity index 72% rename from wrappers/python/wcc_ZZZ.py.in rename to python/wcc_ZZZ.py.in index 3ab6611..d899eea 100644 --- a/wrappers/python/wcc_ZZZ.py.in +++ b/python/wcc_ZZZ.py.in @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ Licensed to the Apache Software Foundation (ASF) under one @@ -103,7 +103,7 @@ G1 = 2 * PFS + 1 IVL = 12 -def toHex(octetValue): +def to_hex(octet_value): """Converts an octet type into a string Add all the values in an octet into an array. This arrays is then @@ -111,7 +111,7 @@ def toHex(octetValue): Args:: - octetValue. An octet type + octet_value. An octet pointer type Returns:: @@ -122,10 +122,13 @@ def toHex(octetValue): """ i = 0 val = [] - while i < octetValue[0].len: - val.append(octetValue[0].val[i]) + while i < octet_value.len: + val.append(octet_value.val[i]) i = i + 1 - return ''.join(val).encode("hex") + out = b'' + for x in val: + out = out + x + return out.hex() if __name__ == "__main__": @@ -133,8 +136,8 @@ if __name__ == "__main__": DEBUG = False # Seed - seedHex = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20" - seed = seedHex.decode("hex") + seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30" + seed = bytes.fromhex(seed_hex) # Master Secret Shares MS1 = ffi.new("octet*") @@ -150,12 +153,13 @@ if __name__ == "__main__": MS2[0].len = PGS # Alice Identity - alice_id = raw_input("Please enter Alice's identity:") + alice_id = input("Please enter Alice's identity:") + alice_id_b = alice_id.encode("utf-8") IdA = ffi.new("octet*") - IdAval = ffi.new("char [%s]" % len(alice_id), alice_id) + IdAval = ffi.new("char [%s]" % len(alice_id_b), alice_id_b) IdA[0].val = IdAval - IdA[0].max = len(alice_id) - IdA[0].len = len(alice_id) + IdA[0].max = len(alice_id_b) + IdA[0].len = len(alice_id_b) # Hash value of IdA HIdA = ffi.new("octet*") @@ -165,12 +169,13 @@ if __name__ == "__main__": HIdA[0].len = PFS # Bob Identity - bob_id = raw_input("Please enter Bob's identity:") + bob_id = input("Please enter Bob's identity:") + bob_id_b = bob_id.encode("utf-8") IdB = ffi.new("octet*") - IdBval = ffi.new("char [%s]" % len(bob_id), bob_id) + IdBval = ffi.new("char [%s]" % len(bob_id_b), bob_id_b) IdB[0].val = IdBval - IdB[0].max = len(bob_id) - IdB[0].len = len(bob_id) + IdB[0].max = len(bob_id_b) + IdB[0].len = len(bob_id_b) # Hash value of IdB HIdB = ffi.new("octet*") @@ -285,7 +290,7 @@ if __name__ == "__main__": SEED[0].len = len(seed) SEED[0].max = len(seed) if DEBUG: - print "SEED: %s" % toHex(SEED) + print("SEED: %s" % to_hex(SEED)) # random number generator RNG = ffi.new("csprng*") @@ -294,105 +299,114 @@ if __name__ == "__main__": # Hash IdA libamcl_core.HASH_ID(HASH_TYPE_ZZZ, IdA, HIdA) if DEBUG: - print "IdA: %s" % toHex(IdA) - print "HIdA: %s" % toHex(HIdA) + print("IdA: {}".format(to_hex(IdA))) + print("HIdA: {}".format(to_hex(HIdA))) # Hash IdB libamcl_core.HASH_ID(HASH_TYPE_ZZZ, IdB, HIdB) if DEBUG: - print "IdB: %s" % toHex(IdB) - print "HIdB: %s" % toHex(HIdB) + print("IdB: {}".format(to_hex(IdB))) + print("HIdB: {}".format(to_hex(HIdB))) # Generate master secret for MILAGRO and Customer rtn = libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG, MS1) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG,MS1) Error %s", rtn + print("libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG,MS1) Error {}".format(rtn)) rtn = libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG, MS2) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG,MS2) Error %s" % rtn + print("libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG,MS2) Error {}".format(rtn)) if DEBUG: - print "MS1: %s" % toHex(MS1) - print "MS2: %s" % toHex(MS2) + print("MS1: {}".format(to_hex(MS1))) + print("MS2: {}".format(to_hex(MS2))) # Generate Alice's sender key shares rtn = libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(MS1, HIdA, A1KeyG1) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(MS1,HIdA,A1KeyG1) Error %s" % rtn + print( + "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(MS1,HIdA,A1KeyG1) Error {}".format(rtn)) rtn = libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(MS2, HIdA, A2KeyG1) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(MS2,HIdA,A2KeyG1) Error %s" % rtn + print( + "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(MS2,HIdA,A2KeyG1) Error {}".format(rtn)) if DEBUG: - print "A1KeyG1: %s" % toHex(A1KeyG1) - print "A2KeyG1: %s" % toHex(A2KeyG1) + print("A1KeyG1: {}".format(to_hex(A1KeyG1))) + print("A2KeyG1: {}".format(to_hex(A2KeyG1))) # Combine Alices's sender key shares rtn = libamcl_wcc_ZZZ.WCC_ZZZ_RECOMBINE_G1(A1KeyG1, A2KeyG1, AKeyG1) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_RECOMBINE_G1(A1KeyG1, A2KeyG1, AKeyG1) Error %s" % rtn - print "AKeyG1: %s" % toHex(AKeyG1) + print( + "libamcl_wcc_ZZZ.WCC_ZZZ_RECOMBINE_G1(A1KeyG1, A2KeyG1, AKeyG1) Error {}".format(rtn)) + print("AKeyG1: {}".format(to_hex(AKeyG1))) # Generate Bob's receiver secret key shares rtn = libamcl_wcc_ZZZ.WCC_ZZZ_GET_G2_MULTIPLE(MS1, HIdB, B1KeyG2) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G2_MULTIPLE(MS1,HIdB,B1KeyG2) Error %s" % rtn + print( + "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G2_MULTIPLE(MS1,HIdB,B1KeyG2) Error {}".format(rtn)) rtn = libamcl_wcc_ZZZ.WCC_ZZZ_GET_G2_MULTIPLE(MS2, HIdB, B2KeyG2) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G2_MULTIPLE(MS2,HIdB,B2KeyG2) Error %s" % rtn + print( + "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G2_MULTIPLE(MS2,HIdB,B2KeyG2) Error {}".format(rtn)) if DEBUG: - print "B1KeyG2: %s" % toHex(B1KeyG2) - print "B2KeyG2: %s" % toHex(B2KeyG2) + print("B1KeyG2: {}".format(to_hex(B1KeyG2))) + print("B2KeyG2: {}".format(to_hex(B2KeyG2))) # Combine Bobs's receiver secret key shares rtn = libamcl_wcc_ZZZ.WCC_ZZZ_RECOMBINE_G2(B1KeyG2, B2KeyG2, BKeyG2) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_RECOMBINE_G2(B1KeyG2, B2KeyG2, BKeyG2) Error %s" % rtn - print "BKeyG2: %s" % toHex(BKeyG2) + print( + "libamcl_wcc_ZZZ.WCC_ZZZ_RECOMBINE_G2(B1KeyG2, B2KeyG2, BKeyG2) Error {}".format(rtn)) + print("BKeyG2: {}".format(to_hex(BKeyG2))) rtn = libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG, X) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG,X) Error %s", rtn + print("libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG,X) Error {}".format(rtn)) if DEBUG: - print "X: %s" % toHex(X) + print("X: {}".format(to_hex(X))) rtn = libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(X, HIdA, PaG1) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(X,HIdA,PaG1) Error %s", rtn + print( + "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(X,HIdA,PaG1) Error {}".format(rtn)) if DEBUG: - print "PaG1: %s" % toHex(PaG1) + print("PaG1: {}".format(to_hex(PaG1))) rtn = libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG, W) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG,W) Error %s", rtn + print("libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG,W) Error {}".format(rtn)) if DEBUG: - print "W: %s" % toHex(W) + print("W: {}".format(to_hex(W))) rtn = libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(W, HIdA, PgG1) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(W, HIdA, PgG1) Error %s", rtn + print( + "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G1_MULTIPLE(W, HIdA, PgG1) Error {}".format(rtn)) if DEBUG: - print "PgG1: %s" % toHex(PgG1) + print("PgG1: {}".format(to_hex(PgG1))) rtn = libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG, Y) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG,Y) Error %s", rtn + print("libamcl_wcc_ZZZ.WCC_ZZZ_RANDOM_GENERATE(RNG,Y) Error {}".format(rtn)) if DEBUG: - print "Y: %s" % toHex(Y) + print("Y: {}".format(to_hex(Y))) rtn = libamcl_wcc_ZZZ.WCC_ZZZ_GET_G2_MULTIPLE(Y, HIdB, PbG2) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G2_MULTIPLE(Y, HIdB, PbG2) Error %s", rtn + print( + "libamcl_wcc_ZZZ.WCC_ZZZ_GET_G2_MULTIPLE(Y, HIdB, PbG2) Error {}".format(rtn)) if DEBUG: - print "PbG2: %s" % toHex(PbG2) + print("PbG2: {}".format(to_hex(PbG2))) # PIA = Hq(PaG1,PbG2,PgG1,IdB) libamcl_wcc_ZZZ.WCC_ZZZ_Hq(HASH_TYPE_ZZZ, PaG1, PbG2, PgG1, IdB, PIA) if DEBUG: - print "PIA: %s" % toHex(PIA) + print("PIA: {}".format(to_hex(PIA))) # PIB = Hq(PbG2,PaG1,PgG1,IdA) libamcl_wcc_ZZZ.WCC_ZZZ_Hq(HASH_TYPE_ZZZ, PbG2, PaG1, PgG1, IdA, PIB) if DEBUG: - print "PIB: %s" % toHex(PIB) + print("PIB: {}".format(to_hex(PIB))) # Alice calculates AES Key rtn = libamcl_wcc_ZZZ.WCC_ZZZ_SENDER_KEY( @@ -406,8 +420,8 @@ if __name__ == "__main__": IdB, KEY1) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_SENDER_KEY(HASH_TYPE_ZZZ, X, PIA, PIB, PbG2, PgG1, AKeyG1, IdB, KEY1) Error %s" % rtn - print "{0}'s AES Key: {1}".format(alice_id, toHex(KEY1)) + print("libamcl_wcc_ZZZ.WCC_ZZZ_SENDER_KEY(HASH_TYPE_ZZZ, X, PIA, PIB, PbG2, PgG1, AKeyG1, IdB, KEY1) Error {}".format(rtn)) + print("{}'s AES Key: {}".format(alice_id, to_hex(KEY1))) # Bob calculates AES Key rtn = libamcl_wcc_ZZZ.WCC_ZZZ_RECEIVER_KEY( @@ -422,7 +436,7 @@ if __name__ == "__main__": IdA, KEY2) if rtn != 0: - print "libamcl_wcc_ZZZ.WCC_ZZZ_RECEIVER_KEY(HASH_TYPE_ZZZ, Y, W, PIA, PIB, PaG1, PgG1, BKeyG2, IdA, KEY2) Error %s" % rtn - print "{0}'s AES Key: {1}".format(bob_id, toHex(KEY2)) + print("libamcl_wcc_ZZZ.WCC_ZZZ_RECEIVER_KEY(HASH_TYPE_ZZZ, Y, W, PIA, PIB, PaG1, PgG1, BKeyG2, IdA, KEY2) Error {}".format(rtn)) + print("{}'s AES Key: {}".format(bob_id, to_hex(KEY2))) libamcl_core.KILL_CSPRNG(RNG) diff --git a/src/bls.c.in b/src/bls.c.in index 1903a58..95e7f54 100644 --- a/src/bls.c.in +++ b/src/bls.c.in @@ -325,9 +325,9 @@ int BLS_ZZZ_RECOVER_SECRET(int k, octet* X, octet* Y, octet* SK) rc = recover_coefficients(k, X, COEFS); if (rc) - { - return rc; - } + { + return rc; + } for(int i=0; i<k; i++) { @@ -366,11 +366,11 @@ int BLS_ZZZ_RECOVER_SIGNATURE(int k, octet* X, octet* Y, octet* SIG) rc = recover_coefficients(k, X, COEFS); if (rc) - { - return rc; - } + { + return rc; + } + - for(int i=0; i<k; i++) { memset(&product[i], 0, sizeof(product[i])); diff --git a/src/bls192.c.in b/src/bls192.c.in index 1c86954..88a7056 100644 --- a/src/bls192.c.in +++ b/src/bls192.c.in @@ -325,9 +325,9 @@ int BLS_ZZZ_RECOVER_SECRET(int k, octet* X, octet* Y, octet* SK) rc = recover_coefficients(k, X, COEFS); if (rc) - { - return rc; - } + { + return rc; + } for(int i=0; i<k; i++) { @@ -366,11 +366,11 @@ int BLS_ZZZ_RECOVER_SIGNATURE(int k, octet* X, octet* Y, octet* SIG) rc = recover_coefficients(k, X, COEFS); if (rc) - { - return rc; - } + { + return rc; + } + - for(int i=0; i<k; i++) { memset(&product[i], 0, sizeof(product[i])); diff --git a/src/bls256.c.in b/src/bls256.c.in index 8e3ca1e..f03a3bb 100644 --- a/src/bls256.c.in +++ b/src/bls256.c.in @@ -325,9 +325,9 @@ int BLS_ZZZ_RECOVER_SECRET(int k, octet* X, octet* Y, octet* SK) rc = recover_coefficients(k, X, COEFS); if (rc) - { - return rc; - } + { + return rc; + } for(int i=0; i<k; i++) { @@ -366,11 +366,11 @@ int BLS_ZZZ_RECOVER_SIGNATURE(int k, octet* X, octet* Y, octet* SIG) rc = recover_coefficients(k, X, COEFS); if (rc) - { - return rc; - } + { + return rc; + } + - for(int i=0; i<k; i++) { memset(&product[i], 0, sizeof(product[i])); diff --git a/test/test_bls_sss_ZZZ.c.in b/test/test_bls_sss_ZZZ.c.in index d7c6fc1..e869c4c 100644 --- a/test/test_bls_sss_ZZZ.c.in +++ b/test/test_bls_sss_ZZZ.c.in @@ -70,7 +70,7 @@ int test(csprng *RNG) octet X[n]; char y[n][BGS_ZZZ]; octet Y[n]; - + for(int i=0; i<n; i++) { memset(&y[i], 0, sizeof(y[i])); @@ -82,7 +82,7 @@ int test(csprng *RNG) X[i].len = BGS_ZZZ; X[i].val = x[i]; } - + // Generate key pair BLS_ZZZ_KEY_PAIR_GENERATE(RNG,&SKI,&PKI); @@ -103,7 +103,7 @@ int test(csprng *RNG) if (rc!=BLS_OK) { printf("Test Failed Invalid Signature\n"); - return 1; + return 1; } printf("Test Passed valid signature is valid\n"); @@ -111,10 +111,10 @@ int test(csprng *RNG) rc = BLS_ZZZ_MAKE_SHARES(k, n, RNG, X, Y, &SKI, &SKO); if (rc!=BLS_OK) { - printf("Test Failed BLS_ZZZ_MAKE_SHARES\n"); - return 1; + printf("Test Failed BLS_ZZZ_MAKE_SHARES\n"); + return 1; } - printf("Test Passed BLS_ZZZ_MAKE_SHARES\n"); + printf("Test Passed BLS_ZZZ_MAKE_SHARES\n"); for(int i=0; i<n; i++) { @@ -123,35 +123,35 @@ int test(csprng *RNG) printf("Y[%d] ", i); OCT_output(&Y[i]); } - + printf("SKO: "); OCT_output(&SKO); - + if (!OCT_comp(&SKI,&SKO)) { - printf("Test Failed SKI != SKO\n"); + printf("Test Failed SKI != SKO\n"); return 1; } printf("Test Passed SKI == SKO\n"); - + // Recover BLS secret key rc = BLS_ZZZ_RECOVER_SECRET(k, X, Y, &SKR); if (rc!=BLS_OK) { printf("Test Failed BLS_ZZZ_RECOVER_SECRET\n"); - return 1; + return 1; } - printf("Test Passed BLS_ZZZ_RECOVER_SECRET\n"); + printf("Test Passed BLS_ZZZ_RECOVER_SECRET\n"); printf("SKR: "); OCT_output(&SKR); - + if (!OCT_comp(&SKR,&SKO)) { - printf("Test Failed SKR != SKO\n"); + printf("Test Failed SKR != SKO\n"); return 1; } printf("Test Passed SKR == SKO\n"); - + // Generate public keys and signatures using shares char pks[n][G2LEN]; octet PKS[n]; @@ -189,19 +189,19 @@ int test(csprng *RNG) if (rc!=BLS_OK) { printf("Test Failed BLS_ZZZ_RECOVER_SIGNATURE\n"); - return 1; + return 1; } - printf("Test Passed BLS_ZZZ_RECOVER_SIGNATURE\n"); + printf("Test Passed BLS_ZZZ_RECOVER_SIGNATURE\n"); printf("SIGR: "); OCT_output(&SIGR); - + if (!OCT_comp(&SIGR,&SIGI)) { - printf("Test Failed SIGR != SIGI\n"); + printf("Test Failed SIGR != SIGI\n"); return 1; } printf("Test Passed SIGR == SIGI\n"); - + return 0; }
