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
This commit is contained in:
Dmitri Bogomolov 2021-02-02 20:10:02 +02:00
parent d05255625b
commit da8bd36614
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
7 changed files with 17 additions and 21 deletions

View File

@ -16,7 +16,7 @@ def inv(a, n):
lm, hm = 1, 0 lm, hm = 1, 0
low, high = a % n, n low, high = a % n, n
while low > 1: while low > 1:
r = high / low r = high // low
nm, new = hm - lm * r, high - low * r nm, new = hm - lm * r, high - low * r
lm, low, hm, high = nm, new, lm, low lm, low, hm, high = nm, new, lm, low
return lm % n return lm % n
@ -43,8 +43,8 @@ def encode(val, base, minlen=0):
code_string = get_code_string(base) code_string = get_code_string(base)
result = "" result = ""
while val > 0: while val > 0:
result = code_string[val % base] + result val, i = divmod(val, base)
val /= base result = code_string[i] + result
if len(result) < minlen: if len(result) < minlen:
result = code_string[0] * (minlen - len(result)) + result result = code_string[0] * (minlen - len(result)) + result
return result return result
@ -101,10 +101,11 @@ def base10_multiply(a, n):
return G return G
if n == 1: if n == 1:
return a return a
if (n % 2) == 0: n, m = divmod(n, 2)
return base10_double(base10_multiply(a, n / 2)) if m == 0:
if (n % 2) == 1: return base10_double(base10_multiply(a, n))
return base10_add(base10_double(base10_multiply(a, n / 2)), a) if m == 1:
return base10_add(base10_double(base10_multiply(a, n)), a)
return None return None

View File

@ -1,12 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" """
Symmetric Encryption Symmetric Encryption
""" """
# Copyright (C) 2011 Yann GUIBET <yannguibet@gmail.com> # Copyright (C) 2011 Yann GUIBET <yannguibet@gmail.com>
# See LICENSE for details. # See LICENSE for details.
from openssl import OpenSSL from .openssl import OpenSSL
# pylint: disable=redefined-builtin # pylint: disable=redefined-builtin

View File

@ -1,5 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" """
Asymmetric cryptography using elliptic curves Asymmetric cryptography using elliptic curves
""" """
@ -10,9 +8,9 @@ Asymmetric cryptography using elliptic curves
from hashlib import sha512 from hashlib import sha512
from struct import pack, unpack from struct import pack, unpack
from cipher import Cipher from .cipher import Cipher
from hash import equals, hmac_sha256 from .hash import equals, hmac_sha256
from openssl import OpenSSL from .openssl import OpenSSL
class ECC(object): class ECC(object):

View File

@ -1,4 +1,3 @@
#!/usr/bin/env python
""" """
ECC blind signature functionality based on ECC blind signature functionality based on
"An Efficient Blind Signature Scheme "An Efficient Blind Signature Scheme
@ -151,7 +150,7 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes
# padding manually # padding manually
bx = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(x)) bx = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(x))
OpenSSL.BN_bn2bin(x, bx) 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) return pack(EC, y_byte, out)
finally: finally:
@ -181,7 +180,7 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes
except AttributeError: except AttributeError:
o = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(bn)) o = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(bn))
OpenSSL.BN_bn2bin(bn, o) OpenSSL.BN_bn2bin(bn, o)
return o.raw.rjust(l_, chr(0)) return o.raw.rjust(l_, b'\x00')
def _bn_deserialize(self, data): def _bn_deserialize(self, data):
"""Make a BigNum out of string""" """Make a BigNum out of string"""

View File

@ -4,7 +4,7 @@ Wrappers for hash functions from OpenSSL.
# Copyright (C) 2011 Yann GUIBET <yannguibet@gmail.com> # Copyright (C) 2011 Yann GUIBET <yannguibet@gmail.com>
# See LICENSE for details. # See LICENSE for details.
from openssl import OpenSSL from .openssl import OpenSSL
# For python3 # For python3

View File

@ -83,7 +83,7 @@ class _OpenSSL(object):
""" """
self._lib = ctypes.CDLL(library) self._lib = ctypes.CDLL(library)
self._version, self._hexversion, self._cflags = get_version(self._lib) 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.pointer = ctypes.pointer
self.c_int = ctypes.c_int self.c_int = ctypes.c_int

View File

@ -49,6 +49,6 @@ class TestOpenSSL(unittest.TestCase):
c = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(a)) c = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(a))
OpenSSL.BN_bn2binpad(a, b, OpenSSL.BN_num_bytes(n)) OpenSSL.BN_bn2binpad(a, b, OpenSSL.BN_num_bytes(n))
OpenSSL.BN_bn2bin(a, c) 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 bad += 1
self.assertEqual(bad, 0) self.assertEqual(bad, 0)