aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2015-12-26 20:15:14 -0600
committerPaul Kehrer <paul.l.kehrer@gmail.com>2015-12-26 20:15:14 -0600
commit66d976fd185c339cd294d4de3b7b41885b736703 (patch)
treef6387bcecb9a6f85411f90bfe41987fb1a445f76
parent3a6631c3db6d1c40912a6152b4c435e9fb99f422 (diff)
parenteefd3a8b51c8219f2c8a5d921b8266dee7d82d9b (diff)
downloadcryptography-66d976fd185c339cd294d4de3b7b41885b736703.tar.gz
cryptography-66d976fd185c339cd294d4de3b7b41885b736703.tar.bz2
cryptography-66d976fd185c339cd294d4de3b7b41885b736703.zip
Merge pull request #2590 from alex/simplify-bn
Simplify code slightly by adding a new binding
-rw-r--r--src/_cffi_src/openssl/bignum.py2
-rw-r--r--src/cryptography/hazmat/backends/openssl/backend.py5
2 files changed, 3 insertions, 4 deletions
diff --git a/src/_cffi_src/openssl/bignum.py b/src/_cffi_src/openssl/bignum.py
index ae035007..455afdc1 100644
--- a/src/_cffi_src/openssl/bignum.py
+++ b/src/_cffi_src/openssl/bignum.py
@@ -71,6 +71,8 @@ int BN_mask_bits(BIGNUM *, int);
"""
MACROS = """
+int BN_num_bytes(const BIGNUM *);
+
int BN_zero(BIGNUM *);
int BN_one(BIGNUM *);
int BN_mod(BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
diff --git a/src/cryptography/hazmat/backends/openssl/backend.py b/src/cryptography/hazmat/backends/openssl/backend.py
index 0dd9a2e3..c0c9ebe2 100644
--- a/src/cryptography/hazmat/backends/openssl/backend.py
+++ b/src/cryptography/hazmat/backends/openssl/backend.py
@@ -919,17 +919,14 @@ class Backend(object):
assert bn != self._ffi.NULL
if six.PY3:
# Python 3 has constant time from_bytes, so use that.
-
- bn_num_bytes = (self._lib.BN_num_bits(bn) + 7) // 8
+ bn_num_bytes = self._lib.BN_num_bytes(bn)
bin_ptr = self._ffi.new("unsigned char[]", bn_num_bytes)
bin_len = self._lib.BN_bn2bin(bn, bin_ptr)
# A zero length means the BN has value 0
self.openssl_assert(bin_len >= 0)
return int.from_bytes(self._ffi.buffer(bin_ptr)[:bin_len], "big")
-
else:
# Under Python 2 the best we can do is hex()
-
hex_cdata = self._lib.BN_bn2hex(bn)
self.openssl_assert(hex_cdata != self._ffi.NULL)
hex_str = self._ffi.string(hex_cdata)