From da8bd3661445bf0423f4815baf006531d3ef51dd Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Tue, 2 Feb 2021 20:10:02 +0200 Subject: [PATCH] Fix python3 issues in pyelliptic: - use dotted imports, remove unneeded shebangs - openssl._OpenSSL._version is of type bytes - use b'\x00' literal instead of chr(0) in eccblind and test_openssl - use // and divmod in arithmetic to fit PEP238: https://docs.python.org/3/whatsnew/2.2.html#pep-238-changing-the-division-operator --- src/pyelliptic/arithmetic.py | 15 ++++++++------- src/pyelliptic/cipher.py | 4 +--- src/pyelliptic/ecc.py | 8 +++----- src/pyelliptic/eccblind.py | 5 ++--- src/pyelliptic/hash.py | 2 +- src/pyelliptic/openssl.py | 2 +- src/tests/test_openssl.py | 2 +- 7 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/pyelliptic/arithmetic.py b/src/pyelliptic/arithmetic.py index 83e634ad..cb3049c0 100644 --- a/src/pyelliptic/arithmetic.py +++ b/src/pyelliptic/arithmetic.py @@ -16,7 +16,7 @@ def inv(a, n): lm, hm = 1, 0 low, high = a % n, n while low > 1: - r = high / low + r = high // low nm, new = hm - lm * r, high - low * r lm, low, hm, high = nm, new, lm, low return lm % n @@ -43,8 +43,8 @@ def encode(val, base, minlen=0): code_string = get_code_string(base) result = "" while val > 0: - result = code_string[val % base] + result - val /= base + val, i = divmod(val, base) + result = code_string[i] + result if len(result) < minlen: result = code_string[0] * (minlen - len(result)) + result return result @@ -101,10 +101,11 @@ def base10_multiply(a, n): return G if n == 1: return a - if (n % 2) == 0: - return base10_double(base10_multiply(a, n / 2)) - if (n % 2) == 1: - return base10_add(base10_double(base10_multiply(a, n / 2)), a) + n, m = divmod(n, 2) + if m == 0: + return base10_double(base10_multiply(a, n)) + if m == 1: + return base10_add(base10_double(base10_multiply(a, n)), a) return None diff --git a/src/pyelliptic/cipher.py b/src/pyelliptic/cipher.py index 4057e169..af6c08ca 100644 --- a/src/pyelliptic/cipher.py +++ b/src/pyelliptic/cipher.py @@ -1,12 +1,10 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- """ Symmetric Encryption """ # Copyright (C) 2011 Yann GUIBET # See LICENSE for details. -from openssl import OpenSSL +from .openssl import OpenSSL # pylint: disable=redefined-builtin diff --git a/src/pyelliptic/ecc.py b/src/pyelliptic/ecc.py index a7f5a6b7..388227c7 100644 --- a/src/pyelliptic/ecc.py +++ b/src/pyelliptic/ecc.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- """ Asymmetric cryptography using elliptic curves """ @@ -10,9 +8,9 @@ Asymmetric cryptography using elliptic curves from hashlib import sha512 from struct import pack, unpack -from cipher import Cipher -from hash import equals, hmac_sha256 -from openssl import OpenSSL +from .cipher import Cipher +from .hash import equals, hmac_sha256 +from .openssl import OpenSSL class ECC(object): diff --git a/src/pyelliptic/eccblind.py b/src/pyelliptic/eccblind.py index a417451e..83bc7632 100644 --- a/src/pyelliptic/eccblind.py +++ b/src/pyelliptic/eccblind.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python """ ECC blind signature functionality based on "An Efficient Blind Signature Scheme @@ -151,7 +150,7 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes # padding manually bx = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(x)) OpenSSL.BN_bn2bin(x, bx) - out = bx.raw.rjust(l_, chr(0)) + out = bx.raw.rjust(l_, b'\x00') return pack(EC, y_byte, out) finally: @@ -181,7 +180,7 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes except AttributeError: o = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(bn)) OpenSSL.BN_bn2bin(bn, o) - return o.raw.rjust(l_, chr(0)) + return o.raw.rjust(l_, b'\x00') def _bn_deserialize(self, data): """Make a BigNum out of string""" diff --git a/src/pyelliptic/hash.py b/src/pyelliptic/hash.py index f098d631..70c9a6ce 100644 --- a/src/pyelliptic/hash.py +++ b/src/pyelliptic/hash.py @@ -4,7 +4,7 @@ Wrappers for hash functions from OpenSSL. # Copyright (C) 2011 Yann GUIBET # See LICENSE for details. -from openssl import OpenSSL +from .openssl import OpenSSL # For python3 diff --git a/src/pyelliptic/openssl.py b/src/pyelliptic/openssl.py index accaaa94..abc6ac13 100644 --- a/src/pyelliptic/openssl.py +++ b/src/pyelliptic/openssl.py @@ -83,7 +83,7 @@ class _OpenSSL(object): """ self._lib = ctypes.CDLL(library) self._version, self._hexversion, self._cflags = get_version(self._lib) - self._libreSSL = self._version.startswith("LibreSSL") + self._libreSSL = self._version.startswith(b"LibreSSL") self.pointer = ctypes.pointer self.c_int = ctypes.c_int diff --git a/src/tests/test_openssl.py b/src/tests/test_openssl.py index e947fff3..c62bb8b3 100644 --- a/src/tests/test_openssl.py +++ b/src/tests/test_openssl.py @@ -49,6 +49,6 @@ class TestOpenSSL(unittest.TestCase): c = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(a)) OpenSSL.BN_bn2binpad(a, b, OpenSSL.BN_num_bytes(n)) OpenSSL.BN_bn2bin(a, c) - if b.raw != c.raw.rjust(OpenSSL.BN_num_bytes(n), chr(0)): + if b.raw != c.raw.rjust(OpenSSL.BN_num_bytes(n), b'\x00'): bad += 1 self.assertEqual(bad, 0)