diff options
author | Alex Stapleton <alexs@prol.etari.at> | 2014-04-19 08:33:13 +0100 |
---|---|---|
committer | Alex Stapleton <alexs@prol.etari.at> | 2014-04-20 09:28:19 +0100 |
commit | 5823e85cbb74f5c5109a984fa88d18868b168a1c (patch) | |
tree | 8235d5788259d10dcc213c88b401aaed6703d71a /cryptography | |
parent | 9fa31c6353af3ab32edd6f6988725886d0e2aa4c (diff) | |
download | cryptography-5823e85cbb74f5c5109a984fa88d18868b168a1c.tar.gz cryptography-5823e85cbb74f5c5109a984fa88d18868b168a1c.tar.bz2 cryptography-5823e85cbb74f5c5109a984fa88d18868b168a1c.zip |
Inplace mode for _int_to_bn
Mostly useful for working with (EC)?DSA_SIG right now
Diffstat (limited to 'cryptography')
-rw-r--r-- | cryptography/hazmat/backends/openssl/backend.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/cryptography/hazmat/backends/openssl/backend.py b/cryptography/hazmat/backends/openssl/backend.py index 021ce8c4..6bf3787a 100644 --- a/cryptography/hazmat/backends/openssl/backend.py +++ b/cryptography/hazmat/backends/openssl/backend.py @@ -293,7 +293,7 @@ class Backend(object): self._lib.OPENSSL_free(hex_cdata) return int(hex_str, 16) - def _int_to_bn(self, num): + def _int_to_bn(self, num, bn=None): """ Converts a python integer to a BIGNUM. The returned BIGNUM will not be garbage collected (to support adding them to structs that take @@ -301,11 +301,14 @@ class Backend(object): be discarded after use. """ + if bn is None: + bn = self._ffi.NULL + if six.PY3: # Python 3 has constant time to_bytes, so use that. binary = num.to_bytes(int(num.bit_length() / 8.0 + 1), "big") - bn_ptr = self._lib.BN_bin2bn(binary, len(binary), self._ffi.NULL) + bn_ptr = self._lib.BN_bin2bn(binary, len(binary), bn) assert bn_ptr != self._ffi.NULL return bn_ptr @@ -314,6 +317,7 @@ class Backend(object): hex_num = hex(num).rstrip("L").lstrip("0x").encode("ascii") or b"0" bn_ptr = self._ffi.new("BIGNUM **") + bn_ptr[0] = bn res = self._lib.BN_hex2bn(bn_ptr, hex_num) assert res != 0 assert bn_ptr[0] != self._ffi.NULL |