Skip to content

Commit e2bbffe

Browse files
committed
adjust to python2.6 syntax, add docstrings
1 parent ed63af6 commit e2bbffe

5 files changed

Lines changed: 87 additions & 20 deletions

File tree

src/ecdsa/der.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,18 @@ def encode_number(n):
152152
return b"".join([int2byte(d) for d in b128_digits])
153153

154154

155-
def encode_boolean(n):
156-
# True is a non-zero value, False is encoded as zero
157-
return b"\x01" + encode_length(1) + encode_number(1 if n else 0)
155+
def encode_boolean(b):
156+
"""
157+
Encodes BOOLEAN acording to ASN.1 DER format.
158+
The ASN.1 BOOLEAN type has two possible values: TRUE and FALSE.
159+
True is encoded as a non-zero value, False is encoded as a zero.
160+
161+
:param boolean b: the boolean value to be encoded
162+
:return: a byte string
163+
:rtype: bytes
164+
"""
165+
166+
return b"\x01" + encode_length(1) + encode_number(1 if b else 0)
158167

159168

160169
def is_sequence(string):
@@ -239,6 +248,15 @@ def remove_octet_string(string):
239248

240249

241250
def remove_boolean(string):
251+
"""
252+
Removes the ASN.1 BOOLEAN type.
253+
In DER, The BOOLEAN in ASN.1 DER encoding has the tag 01.
254+
True is encoded as a non-zero value, False is encoded as a zero.
255+
256+
:param bytes string: the boolean value to be encoded
257+
:return: a boolean value and the rest of the string
258+
:rtype: tuple(boolean, bytes)
259+
"""
242260
if not string:
243261
raise UnexpectedDER("Empty string is an invalid encoding of a boolean")
244262
if string[:1] != b"\x01":

src/ecdsa/ecdsa.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def __ne__(self, other):
181181
"""Return False if the keys are identical, True otherwise."""
182182
return not self == other
183183

184-
def verifies(self, hash, signature, accelerate=False):
184+
def verifies(self, hash, signature):
185185
"""Verify that signature is a valid signature of hash.
186186
Return True if the signature is valid.
187187
"""
@@ -194,7 +194,7 @@ def verifies(self, hash, signature, accelerate=False):
194194
s = signature.s
195195
if s < 1 or s > n - 1:
196196
return False
197-
if type(r) in {bytearray, bytes, memoryview}:
197+
if type(r) in set((bytearray, bytes, memoryview)):
198198
point = ellipticcurve.AbstractPoint.from_bytes(
199199
self.generator.curve(), r)
200200
r = point[0] % n

src/ecdsa/keys.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,7 @@ def verify(
632632
data,
633633
hashfunc=None,
634634
sigdecode=sigdecode_string,
635-
allow_truncate=True,
636-
accelerate=False
635+
allow_truncate=True
637636
):
638637
"""
639638
Verify a signature made over provided data.
@@ -684,15 +683,14 @@ def verify(
684683
hashfunc = hashfunc or self.default_hashfunc
685684
digest = hashfunc(data).digest()
686685
return self.verify_digest(
687-
signature, digest, sigdecode, allow_truncate, accelerate)
686+
signature, digest, sigdecode, allow_truncate)
688687

689688
def verify_digest(
690689
self,
691690
signature,
692691
digest,
693692
sigdecode=sigdecode_string,
694-
allow_truncate=False,
695-
accelerate=False
693+
allow_truncate=False
696694
):
697695
"""
698696
Verify a signature made over provided hash value.
@@ -739,7 +737,7 @@ def verify_digest(
739737
except (der.UnexpectedDER, MalformedSignature) as e:
740738
raise BadSignatureError("Malformed formatting of signature", e)
741739
sig = ecdsa.Signature(r, s)
742-
if self.pubkey.verifies(number, sig, accelerate):
740+
if self.pubkey.verifies(number, sig):
743741
return True
744742
raise BadSignatureError("Signature verification failed")
745743

src/ecdsa/test_pyecdsa.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def test_sigencode_der_full_r(self):
468468
sig = priv1.sign(data, sigencode=sigencode_der_full_r, accelerate=True)
469469

470470
self.assertEqual(type(sig), binary_type)
471-
self.assertTrue(pub1.verify(sig, data, sigdecode=sigdecode_der_full_r, accelerate=True))
471+
self.assertTrue(pub1.verify(sig, data, sigdecode=sigdecode_der_full_r))
472472

473473
def test_sigencode_der_full_r_uncompressed(self):
474474
priv1 = SigningKey.generate()
@@ -480,7 +480,7 @@ def test_sigencode_der_full_r_uncompressed(self):
480480
sig = sigencode_der_full_r(point, s, None, encoding="uncompressed")
481481

482482
self.assertEqual(type(sig), binary_type)
483-
self.assertTrue(pub1.verify(sig, data, sigdecode=sigdecode_der_full_r, accelerate=True))
483+
self.assertTrue(pub1.verify(sig, data, sigdecode=sigdecode_der_full_r))
484484

485485
def test_sigencode_der_full_r_compressed(self):
486486
priv1 = SigningKey.generate()
@@ -492,7 +492,7 @@ def test_sigencode_der_full_r_compressed(self):
492492
sig = sigencode_der_full_r(point, s, None, encoding="compressed")
493493

494494
self.assertEqual(type(sig), binary_type)
495-
self.assertTrue(pub1.verify(sig, data, sigdecode=sigdecode_der_full_r, accelerate=True))
495+
self.assertTrue(pub1.verify(sig, data, sigdecode=sigdecode_der_full_r))
496496

497497
def test_sigencode_der_sig_value_with_y_boolean(self):
498498
priv1 = SigningKey.generate()
@@ -501,7 +501,7 @@ def test_sigencode_der_sig_value_with_y_boolean(self):
501501
sig = priv1.sign(data, sigencode=sigencode_der_sig_value_y_boolean,
502502
accelerate=True)
503503
self.assertEqual(type(sig), binary_type)
504-
self.assertTrue(pub1.verify(sig, data, sigdecode=sigdecode_der_ecdsa_sig_value, accelerate=True))
504+
self.assertTrue(pub1.verify(sig, data, sigdecode=sigdecode_der_ecdsa_sig_value))
505505

506506
def test_sigencode_der_sig_value_with_y_field_elem(self):
507507
priv1 = SigningKey.generate()
@@ -510,7 +510,7 @@ def test_sigencode_der_sig_value_with_y_field_elem(self):
510510
sig = priv1.sign(data, sigencode=sigencode_der_sig_value_y_field_elem,
511511
accelerate=True)
512512
self.assertEqual(type(sig), binary_type)
513-
self.assertTrue(pub1.verify(sig, data, sigdecode=sigdecode_der_ecdsa_sig_value, accelerate=True))
513+
self.assertTrue(pub1.verify(sig, data, sigdecode=sigdecode_der_ecdsa_sig_value))
514514

515515
def test_sigencode_string_canonize_no_change(self):
516516
r = 12

src/ecdsa/util.py

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,31 @@ def sigencode_der(r, s, order):
303303

304304

305305
def sigencode_der_sig_value_y_field_elem(r, s, order):
306+
"""
307+
Encode the signature into the ECDSA-Sig-Value structure using :term:`DER`.
308+
309+
Encodes the signature to the following :term:`ASN.1` structure::
310+
311+
ECDSA-Sig-Value ::= SEQUENCE {
312+
r INTEGER,
313+
s INTEGER,
314+
a INTEGER OPTIONAL,
315+
y CHOICE { b BOOLEAN, f FieldElement } OPTIONAL
316+
}
317+
318+
with 'y' being a field element.
319+
320+
It's expected that this function will be used as a ``sigencode=`` parameter
321+
in :func:`ecdsa.keys.SigningKey.sign` method.
322+
323+
:param int r: first parameter of the signature
324+
:param int s: second parameter of the signature
325+
:param int order: the order of the curve over which the signature was
326+
computed
327+
328+
:return: DER encoding of ECDSA signature
329+
:rtype: bytes
330+
"""
306331
y = r.y()
307332
r = r.x() % order
308333
f = number_to_string(y, order)
@@ -313,6 +338,31 @@ def sigencode_der_sig_value_y_field_elem(r, s, order):
313338

314339

315340
def sigencode_der_sig_value_y_boolean(r, s, order):
341+
"""
342+
Encode the signature into the ECDSA-Sig-Value structure using :term:`DER`.
343+
344+
Encodes the signature to the following :term:`ASN.1` structure::
345+
346+
ECDSA-Sig-Value ::= SEQUENCE {
347+
r INTEGER,
348+
s INTEGER,
349+
a INTEGER OPTIONAL,
350+
y CHOICE { b BOOLEAN, f FieldElement } OPTIONAL
351+
}
352+
353+
with 'y' being a boolean.
354+
355+
It's expected that this function will be used as a ``sigencode=`` parameter
356+
in :func:`ecdsa.keys.SigningKey.sign` method.
357+
358+
:param int r: first parameter of the signature
359+
:param int s: second parameter of the signature
360+
:param int order: the order of the curve over which the signature was
361+
computed
362+
363+
:return: DER encoding of ECDSA signature
364+
:rtype: bytes
365+
"""
316366
y = r.y()
317367
r = r.x() % order
318368
b = False
@@ -330,16 +380,17 @@ def sigencode_der_full_r(r, s, order, encoding='raw'):
330380
Encodes the signature to the following :term:`ASN.1` structure::
331381
332382
ECDSA-Full-R ::= SEQUENCE {
333-
r ECPoint,
334-
s INTEGER
383+
r ECPoint,
384+
s INTEGER
335385
}
336386
337-
Encodes the ECPoint with 'uncompressed' encoding.
387+
Encodes the ECPoint with chosen encoding.
388+
If no encoding was provided, the 'raw' encoding will be used.
338389
339390
It's expected that this function will be used as a ``sigencode=`` parameter
340391
in :func:`ecdsa.keys.SigningKey.sign` method.
341392
342-
:param ECPoint r: first parameter of the signature
393+
:param ECPoint r: the point used to create the signature
343394
:param int s: second parameter of the signature
344395
:param int order: the order of the curve over which the signature was
345396
computed

0 commit comments

Comments
 (0)