Skip to content

Commit

Permalink
Replace use of pycrypto utils functions with custom implementations.
Browse files Browse the repository at this point in the history
 * ADD bridgedb.crypto.bytesToLong function.
 * ADD bridgedb.crypto.longToBytes function.
 * CHANGE bridgedb.bridges.Bridge._verifyExtraInfoSignature() to use
   bytesToLong and longToBytes.
 * REMOVE import of Crypto.Util.bytes_to_long from bridgedb.bridges.
 * REMOVE import of Crypto.Util.long_to_bytes from bridgedb.bridges.
  • Loading branch information
isislovecruft committed Feb 24, 2015
1 parent 335b0e5 commit 5ed5c42
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/bridgedb/bridges.py
Expand Up @@ -19,16 +19,15 @@
import os
import warnings

from Crypto.Util.number import bytes_to_long
from Crypto.Util.number import long_to_bytes

import pyasn1

import bridgedb.Storage

from bridgedb import geo
from bridgedb import safelog
from bridgedb import bridgerequest
from bridgedb.crypto import bytesToLong
from bridgedb.crypto import longToBytes
from bridgedb.crypto import removePKCS1Padding
from bridgedb.parse.addr import isIPAddress
from bridgedb.parse.addr import isIPv4
Expand Down Expand Up @@ -1479,13 +1478,13 @@ def _verifyExtraInfoSignature(self, descriptor):
signatureDecoded = base64.b64decode(signature)

# Convert the signature to a long:
signatureLong = bytes_to_long(signatureDecoded)
signatureLong = bytesToLong(signatureDecoded)

# Decrypt the long signature with the modulus and public exponent:
decryptedInt = pow(signatureLong, publicExponent, modulus)

# Then convert it back to a byte array:
decryptedBytes = long_to_bytes(decryptedInt, BLOCKSIZE)
decryptedBytes = longToBytes(decryptedInt, BLOCKSIZE)

# Remove the PKCS#1 padding from the signature:
unpadded = removePKCS1Padding(decryptedBytes)
Expand Down
73 changes: 73 additions & 0 deletions lib/bridgedb/crypto.py
Expand Up @@ -48,6 +48,7 @@
import logging
import os
import re
import struct
import urllib

import OpenSSL
Expand Down Expand Up @@ -94,6 +95,78 @@ class RSAKeyGenerationError(Exception):
"""Raised when there was an error creating an RSA keypair."""


def bytesToLong(bites):
"""Convert a byte string to a long integer.
>>> from bridgedb.crypto import bytesToLong
>>> bytesToLong('\x059')
1337L
>>> bytesToLong('I\x96\x02\xd2')
1234567890L
>>> bytesToLong('\x00\x00\x00\x00I\x96\x02\xd2')
1234567890L
>>> bytesToLong('\xabT\xa9\x8c\xeb\x1f\n\xd2')
12345678901234567890L
:param bytes bites: The byte string to convert.
:rtype: long
"""
length = len(bites)
if length % 4:
extra = (4 - length % 4)
bites = b'\000' * extra + bites
length = length + extra

acc = 0L
for index in range(0, length, 4):
acc = (acc << 32) + struct.unpack(b'>I', bites[index:index+4])[0]

return acc

def longToBytes(number, blocksize=0):
"""Convert a long integer to a byte string.
>>> from bridgedb.crypto import longToBytes
>>> longToBytes(1337L)
'\x059'
>>> longToBytes(1234567890L)
'I\x96\x02\xd2'
>>> longToBytes(1234567890L, blocksize=8)
'\x00\x00\x00\x00I\x96\x02\xd2'
>>> longToBytes(12345678901234567890L)
'\xabT\xa9\x8c\xeb\x1f\n\xd2'
:param int number: The long integer to convert.
:param int blocksize: If **blocksize** is given and greater than zero, pad
the front of the byte string with binary zeros so that the length is a
multiple of **blocksize**.
:rtype: bytes
"""
bites = b''
number = long(number)

# Convert the number to a byte string
while number > 0:
bites = struct.pack(b'>I', number & 0xffffffffL) + bites
number = number >> 32

# Strip off any leading zeros
for index in range(len(bites)):
if bites[index] != b'\000'[0]:
break
else:
# Only happens when number == 0:
bites = b'\000'
index = 0
bites = bites[index:]

# Add back some padding bytes. This could be done more efficiently
# w.r.t. the de-padding being done above, but sigh...
if blocksize > 0 and len(bites) % blocksize:
bites = (blocksize - len(bites) % blocksize) * b'\000' + bites

return bytes(bites)

def writeKeyToFile(key, filename):
"""Write **key** to **filename**, with ``0400`` permissions.
Expand Down

0 comments on commit 5ed5c42

Please sign in to comment.