Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatters fix (python 2.x) #7

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/ecpy/ecdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def sign(self, msg, pv_key, canonical=False):
return None

def sign_rfc6979(self, msg, pv_key, hasher, canonical=False):
""" Signs a message hash according to RFC6979
""" Signs a message hash according to RFC6979

Args:
msg (bytes) : the message hash to sign
Expand All @@ -62,12 +62,11 @@ def sign_rfc6979(self, msg, pv_key, hasher, canonical=False):
k,V = ecrand.rnd_rfc6979(msg, pv_key.d, field, hasher,V)
sig = self._do_sign(msg, pv_key, k, canonical)
if sig:
return sig

return sig
return None

def sign_k(self, msg, pv_key, k,canonical=False):
""" Signs a message hash with provided random
""" Signs a message hash with provided random

Args:
msg (bytes) : the hash of message to sign
Expand Down
16 changes: 16 additions & 0 deletions src/ecpy/ecschnorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ def sign(self, msg, pv_key):
return sig
return None

def sign_rfc6979(self, msg, pv_key, hasher, canonical=False):
""" Signs a message hash according to RFC6979
Args:
msg (bytes) : the message hash to sign
pv_key (ecpy.keys.ECPrivateKey): key to use for signing
hasher (hashlib) : hasher conform to hashlib interface
"""
field = pv_key.curve.field
V = None
for i in range(1, self.maxtries):
k,V = ecrand.rnd_rfc6979(msg, pv_key.d, field, hasher, V)
sig = self._do_sign(msg, pv_key, k)
if sig:
return sig
return None

def sign_k(self, msg, pv_key, k):
""" Signs a message hash with provided random

Expand Down
22 changes: 11 additions & 11 deletions src/ecpy/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

#python 2 compatibility

from builtins import int,pow

def list_formats():
Expand Down Expand Up @@ -82,30 +83,29 @@ def decode_sig(sig,fmt="DER") :
ints: (r,s)
"""


if fmt=="DER":
sig_len = sig[1]+2
sig_len = int.from_bytes(sig[1], 'big') + 2
r_offset = 4
r_len = sig[3]
r_len = int.from_bytes(sig[3], 'big')
s_offset = 4+r_len+2
s_len = sig[4+r_len+1]
if ( sig[0] != 0x30 or
sig_len != r_len+s_len+6 or
sig[r_offset-2] != 0x02 or
sig[s_offset-2] != 0x02 ):
s_len = int.from_bytes(sig[4+r_len+1], 'big')
if ( int.from_bytes(sig[0], 'big') != 0x30 or
sig_len != r_len+s_len+6 or
int.from_bytes(sig[r_offset-2], 'big') != 0x02 or
int.from_bytes(sig[s_offset-2], 'big') != 0x02 ):
return None,None
r = int.from_bytes(sig[r_offset:r_offset+r_len], 'big')
s = int.from_bytes(sig[s_offset:s_offset+s_len], 'big')
return r,s

if fmt=="ITUPLE":
return (sig[0],sig[1])

if fmt=="BTUPLE":
r = int.from_bytes(sig[0], 'big')
s = int.from_bytes(sig[1], 'big')
return r,s

if fmt=="BTUPLE":
return (sig[0],sig[1])

if fmt=="RAW":
l = len(sig)>>1
r = int.from_bytes(sig[0:l], 'big')
Expand Down