commit 744eb9271b2ed416444b02f01bc7b5c0f94c657c Author: Damian Johnson atagar@torproject.org Date: Sun Oct 13 15:55:29 2019 -0700
Fix unit test for python 2.x
Ooph, this took a while to troubleshoot. Tests finally pass under python 2.7. --- stem/descriptor/ed25519_exts_ref.py | 4 ++-- stem/descriptor/hidden_service.py | 17 +++++++++-------- stem/descriptor/hsv3_crypto.py | 6 ++++++ stem/descriptor/slow_ed25519.py | 8 ++++---- 4 files changed, 21 insertions(+), 14 deletions(-)
diff --git a/stem/descriptor/ed25519_exts_ref.py b/stem/descriptor/ed25519_exts_ref.py index 8e622eb3..e966e4f7 100644 --- a/stem/descriptor/ed25519_exts_ref.py +++ b/stem/descriptor/ed25519_exts_ref.py @@ -40,7 +40,7 @@ def blindPK(pk, param): def expandSK(sk): h = H(sk) a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2)) - k = b''.join([bytes([h[i]]) for i in range(b//8,b//4)]) + k = b''.join([h[i:i+1] for i in range(b//8,b//4)]) assert len(k) == 32 return encodeint(a)+k
@@ -51,7 +51,7 @@ def publickeyFromESK(h):
def signatureWithESK(m,h,pk): a = decodeint(h[:32]) - r = Hint(b''.join([bytes([h[i]]) for i in range(b//8,b//4)]) + m) + r = Hint(b''.join([h[i:i+1] for i in range(b//8,b//4)]) + m) R = scalarmult(B,r) S = (r + Hint(encodepoint(R) + pk + m) * a) % l return encodepoint(R) + encodeint(S) diff --git a/stem/descriptor/hidden_service.py b/stem/descriptor/hidden_service.py index dc382ac0..b96c8a87 100644 --- a/stem/descriptor/hidden_service.py +++ b/stem/descriptor/hidden_service.py @@ -34,8 +34,9 @@ import binascii import collections import hashlib import io -import struct import os +import struct +import time
import stem.client.datatype import stem.prereq @@ -185,12 +186,12 @@ class IntroductionPointV3(object): # if not link_specifiers or not onion_key or not enc_key: # raise ValueError("Introduction point missing essential keys")
- # if not auth_key and not auth_key_cert: - # raise ValueError("Either auth key or auth key cert needs to be provided") + if not auth_key and not auth_key_cert: + raise ValueError("Either auth key or auth key cert needs to be provided")
# If we have an auth key cert but not an auth key, extract the key - # if auth_key_cert and not auth_key: - # auth_key = auth_key_cert.certified_ed25519_key() + if auth_key_cert and not auth_key: + auth_key = auth_key_cert.certified_ed25519_key()
self.link_specifiers = link_specifiers self.onion_key = enc_key @@ -212,9 +213,9 @@ class IntroductionPointV3(object): LSPEC (Link specifier) [LSLEN bytes] """ ls_block = b"" - ls_block += bytes([len(self.link_specifiers)]) + ls_block += chr(len(self.link_specifiers)) for ls in self.link_specifiers: - ls_block += ls.encode() + ls_block += ls.pack()
return base64.b64encode(ls_block)
@@ -822,7 +823,7 @@ def _get_descriptor_signing_cert(descriptor_signing_public_key, blinded_priv_key
def _get_descriptor_revision_counter(): # TODO replace with OPE scheme - return int(datetime.datetime.utcnow().timestamp()) + return int(time.time())
def b64_and_wrap_desc_layer(layer_bytes, prefix_bytes=b""): """ diff --git a/stem/descriptor/hsv3_crypto.py b/stem/descriptor/hsv3_crypto.py index 6665a880..dd64a95f 100644 --- a/stem/descriptor/hsv3_crypto.py +++ b/stem/descriptor/hsv3_crypto.py @@ -3,6 +3,8 @@ import hashlib import struct import os
+import stem.prereq + from stem.descriptor import ed25519_exts_ref from stem.descriptor import slow_ed25519
@@ -100,6 +102,10 @@ def encode_onion_address(ed25519_pub_key_bytes): """ Given the public key, return the onion address """ + + if not stem.prereq._is_sha3_available(): + raise ImportError('Encoding onion addresses requires python 3.6+ or the pysha3 module (https://pypi.org/project/pysha3/)') + version = 3 checksum_body = b"%s%s%d" % (CHECKSUM_CONSTANT, ed25519_pub_key_bytes, version) checksum = hashlib.sha3_256(checksum_body).digest()[:2] diff --git a/stem/descriptor/slow_ed25519.py b/stem/descriptor/slow_ed25519.py index fbe6e35f..a9bbd7c2 100644 --- a/stem/descriptor/slow_ed25519.py +++ b/stem/descriptor/slow_ed25519.py @@ -58,16 +58,16 @@ def scalarmult(P,e):
def encodeint(y): bits = [(y >> i) & 1 for i in range(b)] - return b''.join([bytes([sum([bits[i * 8 + j] << j for j in range(8)])]) for i in range(b//8)]) + return b''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in range(b//8)])
def encodepoint(P): x = P[0] y = P[1] bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1] - return b''.join([bytes([sum([bits[i * 8 + j] << j for j in range(8)])]) for i in range(b//8)]) + return b''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in range(b//8)])
def bit(h,i): - return (ord(h[i//8]) >> (i%8)) & 1 + return (ord(h[i//8:i//8+1]) >> (i%8)) & 1
def publickey(sk): h = H(sk) @@ -82,7 +82,7 @@ def Hint(m): def signature(m,sk,pk): h = H(sk) a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2)) - r = Hint(b''.join([bytes([h[i]]) for i in range(b//8,b//4)]) + m) + r = Hint(b''.join([h[i:i+1] for i in range(b//8,b//4)]) + m) R = scalarmult(B,r) S = (r + Hint(encodepoint(R) + pk + m) * a) % l return encodepoint(R) + encodeint(S)