[Pkg-privacy-commits] [obfsproxy] 95/353: Put some useful obfs2 functions in modules.

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:01:45 UTC 2015


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository obfsproxy.

commit c32853f71569f432e4a5bfe59da58007cdb2f9de
Author: George Kadianakis <desnacked at riseup.net>
Date:   Mon Dec 3 17:01:07 2012 +0200

    Put some useful obfs2 functions in modules.
---
 obfsproxy/common/hmac_sha256.py | 10 ++++++++++
 obfsproxy/common/rand.py        |  7 +++++++
 obfsproxy/common/serialize.py   | 27 +++++++++++++++++++++++++++
 obfsproxy/transports/obfs2.py   | 26 ++++++++------------------
 4 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/obfsproxy/common/hmac_sha256.py b/obfsproxy/common/hmac_sha256.py
new file mode 100644
index 0000000..ef46543
--- /dev/null
+++ b/obfsproxy/common/hmac_sha256.py
@@ -0,0 +1,10 @@
+import hashlib
+import hmac
+
+def hmac_sha256_digest(key, msg):
+    """
+    Return the HMAC-SHA256 message authentication code of the message
+    'msg' with key 'key'.
+    """
+
+    return hmac.new(key, msg, hashlib.sha256).digest()
diff --git a/obfsproxy/common/rand.py b/obfsproxy/common/rand.py
new file mode 100644
index 0000000..08fa330
--- /dev/null
+++ b/obfsproxy/common/rand.py
@@ -0,0 +1,7 @@
+import os
+
+def random_bytes(n):
+    """ Returns n bytes of strong random data. """
+
+    return os.urandom(n)
+
diff --git a/obfsproxy/common/serialize.py b/obfsproxy/common/serialize.py
new file mode 100644
index 0000000..ec035ea
--- /dev/null
+++ b/obfsproxy/common/serialize.py
@@ -0,0 +1,27 @@
+"""Helper functions to go from integers to binary data and back."""
+
+import struct
+
+def htonl(n):
+    """
+    Convert integer in 'n' from host-byte order to network-byte order.
+    """
+    return struct.pack('!I', n)
+
+def ntohl(bs):
+    """
+    Convert integer in 'n' from network-byte order to host-byte order.
+    """
+    return struct.unpack('!I', bs)[0]
+
+def htons(n):
+    """
+    Convert integer in 'n' from host-byte order to network-byte order.
+    """
+    return struct.pack('!h', n)
+
+def ntohs(bs):
+    """
+    Convert integer in 'n' from network-byte order to host-byte order.
+    """
+    return struct.unpack('!h', bs)[0]
diff --git a/obfsproxy/transports/obfs2.py b/obfsproxy/transports/obfs2.py
index 31a12fd..4e61886 100644
--- a/obfsproxy/transports/obfs2.py
+++ b/obfsproxy/transports/obfs2.py
@@ -11,6 +11,9 @@ import hashlib
 import struct
 
 import obfsproxy.common.aes as aes
+import obfsproxy.common.serialize as srlz
+import obfsproxy.common.rand as rand
+
 import obfsproxy.transports.base as base
 
 import obfsproxy.common.log as logging
@@ -45,19 +48,6 @@ def hn(x, n):
         data = h(x)
     return data
 
-def htonl(n):
-    return struct.pack('!I', n)
-
-
-def ntohl(bs):
-    return struct.unpack('!I', bs)[0]
-
-
-def random_bytes(n):
-    """ Returns n bytes of strong random data. """
-
-    return os.urandom(n)
-
 def mac(s, x):
     """ # MAC(s, x) = H(s | x | s) """
 
@@ -75,11 +65,11 @@ class Obfs2Transport(base.BaseTransport):
         self.state = ST_WAIT_FOR_KEY
 
         if self.we_are_initiator:
-            self.initiator_seed = random_bytes(SEED_LENGTH) # Initiator's seed.
+            self.initiator_seed = rand.random_bytes(SEED_LENGTH) # Initiator's seed.
             self.responder_seed = None # Responder's seed.
         else:
             self.initiator_seed = None # Initiator's seed.
-            self.responder_seed = random_bytes(SEED_LENGTH) # Responder's seed
+            self.responder_seed = rand.random_bytes(SEED_LENGTH) # Responder's seed
 
         # Shared secret seed.
         self.secret_seed = None
@@ -113,7 +103,7 @@ class Obfs2Transport(base.BaseTransport):
         padding_length = random.randint(0, MAX_PADDING)
         seed = self.initiator_seed if self.we_are_initiator else self.responder_seed
 
-        handshake_message = seed + self.send_padding_crypto.crypt(htonl(MAGIC_VALUE) + htonl(padding_length) + random_bytes(padding_length))
+        handshake_message = seed + self.send_padding_crypto.crypt(srlz.htonl(MAGIC_VALUE) + srlz.htonl(padding_length) + rand.random_bytes(padding_length))
 
         log.debug("obfs2 handshake: %s queued %d bytes (padding_length: %d).",
                   "initiator" if self.we_are_initiator else "responder",
@@ -160,8 +150,8 @@ class Obfs2Transport(base.BaseTransport):
                                             self.recv_pad_keytype)
 
             # XXX maybe faster with a single d() instead of two.
-            magic = ntohl(self.recv_padding_crypto.crypt(data.read(4)))
-            padding_length = ntohl(self.recv_padding_crypto.crypt(data.read(4)))
+            magic = srlz.ntohl(self.recv_padding_crypto.crypt(data.read(4)))
+            padding_length = srlz.ntohl(self.recv_padding_crypto.crypt(data.read(4)))
 
             log.debug("%s: Got %d bytes of handshake data (padding_length: %d, magic: %s)" % \
                           (log_prefix, len(data), padding_length, hex(magic)))

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/obfsproxy.git



More information about the Pkg-privacy-commits mailing list