fix decryption to work in Python 3

Messages can be received and viewed now.

Remained problems:
* Message sending is not working yet.
This commit is contained in:
Kashiko Koibumi 2024-05-15 09:37:11 +09:00
parent 741f8dd461
commit 88b14977b8
No known key found for this signature in database
GPG Key ID: 8F06E069E37C40C4
10 changed files with 40 additions and 35 deletions

View File

@ -847,7 +847,7 @@ class MyForm(settingsmixin.SMainWindow):
else: else:
self.show() self.show()
self.setWindowState( self.setWindowState(
self.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive) self.windowState() & ~QtCore.Qt.WindowState.WindowMinimized | QtCore.Qt.WindowState.WindowActive)
self.raise_() self.raise_()
self.activateWindow() self.activateWindow()
@ -1664,16 +1664,16 @@ class MyForm(settingsmixin.SMainWindow):
self.init_sent_popup_menu(False) self.init_sent_popup_menu(False)
self.ui.blackwhitelist.init_blacklist_popup_menu(False) self.ui.blackwhitelist.init_blacklist_popup_menu(False)
if event.type() == QtCore.QEvent.Type.WindowStateChange: if event.type() == QtCore.QEvent.Type.WindowStateChange:
if self.windowState() & QtCore.Qt.WindowMinimized: if self.windowState() & QtCore.Qt.WindowState.WindowMinimized:
if config.getboolean('bitmessagesettings', 'minimizetotray') and not 'darwin' in sys.platform: if config.getboolean('bitmessagesettings', 'minimizetotray') and not 'darwin' in sys.platform:
QtCore.QTimer.singleShot(0, self.appIndicatorHide) QtCore.QTimer.singleShot(0, self.appIndicatorHide)
elif event.oldState() & QtCore.Qt.WindowMinimized: elif event.oldState() & QtCore.Qt.WindowState.WindowMinimized:
# The window state has just been changed to # The window state has just been changed to
# Normal/Maximised/FullScreen # Normal/Maximised/FullScreen
pass pass
def __icon_activated(self, reason): def __icon_activated(self, reason):
if reason == QtWidgets.QSystemTrayIcon.Trigger: if reason == QtWidgets.QSystemTrayIcon.ActivationReason.Trigger:
self.actionShow.setChecked(not self.actionShow.isChecked()) self.actionShow.setChecked(not self.actionShow.isChecked())
self.appIndicatorShowOrHideWindow() self.appIndicatorShowOrHideWindow()
@ -1756,7 +1756,7 @@ class MyForm(settingsmixin.SMainWindow):
# choose font and calculate font parameters # choose font and calculate font parameters
fontName = "Lucida" fontName = "Lucida"
fontSize = 10 fontSize = 10
font = QtGui.QFont(fontName, fontSize, QtGui.QFont.Bold) font = QtGui.QFont(fontName, fontSize, QtGui.QFont.Weight.Bold)
fontMetrics = QtGui.QFontMetrics(font) fontMetrics = QtGui.QFontMetrics(font)
# text # text
txt = str(inboxUnreadCount) txt = str(inboxUnreadCount)
@ -4071,7 +4071,7 @@ class MyForm(settingsmixin.SMainWindow):
messageTextedit.setCurrentFont(QtGui.QFont()) messageTextedit.setCurrentFont(QtGui.QFont())
messageTextedit.setTextColor(QtGui.QColor()) messageTextedit.setTextColor(QtGui.QColor())
messageTextedit.setContent(message) messageTextedit.setPlainText(message)
def tableWidgetAddressBookItemChanged(self, item): def tableWidgetAddressBookItemChanged(self, item):
if item.type == AccountMixin.CHAN: if item.type == AccountMixin.CHAN:

View File

@ -484,9 +484,9 @@ class MessageList_TimeWidget(BMTableWidgetItem):
""" """
data = super(MessageList_TimeWidget, self).data(role) data = super(MessageList_TimeWidget, self).data(role)
if role == TimestampRole: if role == TimestampRole:
return int(data.toPyObject()) return int(data)
if role == QtCore.Qt.ItemDataRole.UserRole: if role == QtCore.Qt.ItemDataRole.UserRole:
return str(data.toPyObject()) return data.data()
return data return data

View File

@ -356,9 +356,9 @@ class objectProcessor(threading.Thread):
' Sanity check failed.') ' Sanity check failed.')
return return
readPosition += 4 readPosition += 4
publicSigningKey = '\x04' + data[readPosition:readPosition + 64] publicSigningKey = b'\x04' + data[readPosition:readPosition + 64]
readPosition += 64 readPosition += 64
publicEncryptionKey = '\x04' + data[readPosition:readPosition + 64] publicEncryptionKey = b'\x04' + data[readPosition:readPosition + 64]
readPosition += 64 readPosition += 64
specifiedNonceTrialsPerByteLength = decodeVarint( specifiedNonceTrialsPerByteLength = decodeVarint(
data[readPosition:readPosition + 10])[1] data[readPosition:readPosition + 10])[1]
@ -517,9 +517,9 @@ class objectProcessor(threading.Thread):
return return
readPosition += sendersStreamNumberLength readPosition += sendersStreamNumberLength
readPosition += 4 readPosition += 4
pubSigningKey = '\x04' + decryptedData[readPosition:readPosition + 64] pubSigningKey = b'\x04' + decryptedData[readPosition:readPosition + 64]
readPosition += 64 readPosition += 64
pubEncryptionKey = '\x04' + decryptedData[readPosition:readPosition + 64] pubEncryptionKey = b'\x04' + decryptedData[readPosition:readPosition + 64]
readPosition += 64 readPosition += 64
if sendersAddressVersionNumber >= 3: if sendersAddressVersionNumber >= 3:
requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = \ requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = \
@ -568,7 +568,7 @@ class objectProcessor(threading.Thread):
readPosition += signatureLengthLength readPosition += signatureLengthLength
signature = decryptedData[ signature = decryptedData[
readPosition:readPosition + signatureLength] readPosition:readPosition + signatureLength]
signedData = data[8:20] + encodeVarint(1) + encodeVarint( signedData = bytes(data[8:20]) + encodeVarint(1) + encodeVarint(
streamNumberAsClaimedByMsg streamNumberAsClaimedByMsg
) + decryptedData[:positionOfBottomOfAckData] ) + decryptedData[:positionOfBottomOfAckData]
@ -803,11 +803,11 @@ class objectProcessor(threading.Thread):
# of the sender's address to verify that it was # of the sender's address to verify that it was
# encrypted by with their key rather than some # encrypted by with their key rather than some
# other key. # other key.
toRipe = key toRipe = unhexlify(key)
initialDecryptionSuccessful = True initialDecryptionSuccessful = True
logger.info( logger.info(
'EC decryption successful using key associated' 'EC decryption successful using key associated'
' with ripe hash: %s', hexlify(key)) ' with ripe hash: %s', key)
except Exception: except Exception:
logger.debug( logger.debug(
'cryptorObject.decrypt Exception:', exc_info=True) 'cryptorObject.decrypt Exception:', exc_info=True)
@ -820,13 +820,14 @@ class objectProcessor(threading.Thread):
elif broadcastVersion == 5: elif broadcastVersion == 5:
embeddedTag = data[readPosition:readPosition + 32] embeddedTag = data[readPosition:readPosition + 32]
readPosition += 32 readPosition += 32
if embeddedTag not in shared.MyECSubscriptionCryptorObjects: hex_tag = hexlify(embeddedTag).decode('ascii')
if hex_tag not in shared.MyECSubscriptionCryptorObjects:
logger.debug('We\'re not interested in this broadcast.') logger.debug('We\'re not interested in this broadcast.')
return return
# We are interested in this broadcast because of its tag. # We are interested in this broadcast because of its tag.
# We're going to add some more data which is signed further down. # We're going to add some more data which is signed further down.
signedData = data[8:readPosition] signedData = bytes(data[8:readPosition])
cryptorObject = shared.MyECSubscriptionCryptorObjects[embeddedTag] cryptorObject = shared.MyECSubscriptionCryptorObjects[hex_tag]
try: try:
decryptedData = cryptorObject.decrypt(data[readPosition:]) decryptedData = cryptorObject.decrypt(data[readPosition:])
logger.debug('EC decryption successful') logger.debug('EC decryption successful')
@ -866,10 +867,10 @@ class objectProcessor(threading.Thread):
) )
readPosition += sendersStreamLength readPosition += sendersStreamLength
readPosition += 4 readPosition += 4
sendersPubSigningKey = '\x04' + \ sendersPubSigningKey = b'\x04' + \
decryptedData[readPosition:readPosition + 64] decryptedData[readPosition:readPosition + 64]
readPosition += 64 readPosition += 64
sendersPubEncryptionKey = '\x04' + \ sendersPubEncryptionKey = b'\x04' + \
decryptedData[readPosition:readPosition + 64] decryptedData[readPosition:readPosition + 64]
readPosition += 64 readPosition += 64
if sendersAddressVersion >= 3: if sendersAddressVersion >= 3:

View File

@ -19,17 +19,17 @@ def calculateBitcoinAddressFromPubkey(pubkey):
sha = hashlib.new('sha256') sha = hashlib.new('sha256')
sha.update(pubkey) sha.update(pubkey)
ripe.update(sha.digest()) ripe.update(sha.digest())
ripeWithProdnetPrefix = '\x00' + ripe.digest() ripeWithProdnetPrefix = b'\x00' + ripe.digest()
checksum = hashlib.sha256(hashlib.sha256( checksum = hashlib.sha256(hashlib.sha256(
ripeWithProdnetPrefix).digest()).digest()[:4] ripeWithProdnetPrefix).digest()).digest()[:4]
binaryBitcoinAddress = ripeWithProdnetPrefix + checksum binaryBitcoinAddress = ripeWithProdnetPrefix + checksum
numberOfZeroBytesOnBinaryBitcoinAddress = 0 numberOfZeroBytesOnBinaryBitcoinAddress = 0
while binaryBitcoinAddress[0] == '\x00': while binaryBitcoinAddress[0] == b'\x00':
numberOfZeroBytesOnBinaryBitcoinAddress += 1 numberOfZeroBytesOnBinaryBitcoinAddress += 1
binaryBitcoinAddress = binaryBitcoinAddress[1:] binaryBitcoinAddress = binaryBitcoinAddress[1:]
base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58)
return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded return b"1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded
def calculateTestnetAddressFromPubkey(pubkey): def calculateTestnetAddressFromPubkey(pubkey):
@ -43,14 +43,14 @@ def calculateTestnetAddressFromPubkey(pubkey):
sha = hashlib.new('sha256') sha = hashlib.new('sha256')
sha.update(pubkey) sha.update(pubkey)
ripe.update(sha.digest()) ripe.update(sha.digest())
ripeWithProdnetPrefix = '\x6F' + ripe.digest() ripeWithProdnetPrefix = b'\x6F' + ripe.digest()
checksum = hashlib.sha256(hashlib.sha256( checksum = hashlib.sha256(hashlib.sha256(
ripeWithProdnetPrefix).digest()).digest()[:4] ripeWithProdnetPrefix).digest()).digest()[:4]
binaryBitcoinAddress = ripeWithProdnetPrefix + checksum binaryBitcoinAddress = ripeWithProdnetPrefix + checksum
numberOfZeroBytesOnBinaryBitcoinAddress = 0 numberOfZeroBytesOnBinaryBitcoinAddress = 0
while binaryBitcoinAddress[0] == '\x00': while binaryBitcoinAddress[0] == b'\x00':
numberOfZeroBytesOnBinaryBitcoinAddress += 1 numberOfZeroBytesOnBinaryBitcoinAddress += 1
binaryBitcoinAddress = binaryBitcoinAddress[1:] binaryBitcoinAddress = binaryBitcoinAddress[1:]
base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58)
return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded return b"1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded

View File

@ -142,7 +142,8 @@ class MsgDecode(object):
def decodeSimple(self, data): def decodeSimple(self, data):
"""Handle simple encoding""" """Handle simple encoding"""
bodyPositionIndex = string.find(data, '\nBody:') data = data.decode('utf-8', 'backslashreplace')
bodyPositionIndex = data.find('\nBody:')
if bodyPositionIndex > 1: if bodyPositionIndex > 1:
subject = data[8:bodyPositionIndex] subject = data[8:bodyPositionIndex]
# Only save and show the first 500 characters of the subject. # Only save and show the first 500 characters of the subject.

View File

@ -294,7 +294,7 @@ def isProofOfWorkSufficient(
if TTL < 300: if TTL < 300:
TTL = 300 TTL = 300
POW, = unpack('>Q', highlevelcrypto.double_sha512( POW, = unpack('>Q', highlevelcrypto.double_sha512(
data[:8] + hashlib.sha512(data[8:]).digest())[0:8]) bytes(data[:8]) + hashlib.sha512(data[8:]).digest())[0:8])
return POW <= 2 ** 64 / ( return POW <= 2 ** 64 / (
nonceTrialsPerByte * ( nonceTrialsPerByte * (
len(data) + payloadLengthExtraBytes len(data) + payloadLengthExtraBytes

View File

@ -30,7 +30,7 @@ class Cipher(object):
self.ctx = OpenSSL.EVP_CIPHER_CTX_new() self.ctx = OpenSSL.EVP_CIPHER_CTX_new()
if do == 1 or do == 0: if do == 1 or do == 0:
k = OpenSSL.malloc(key, len(key)) k = OpenSSL.malloc(key, len(key))
IV = OpenSSL.malloc(iv, len(iv)) IV = OpenSSL.malloc(bytes(iv), len(iv))
OpenSSL.EVP_CipherInit_ex( OpenSSL.EVP_CipherInit_ex(
self.ctx, self.cipher.get_pointer(), 0, k, IV, do) self.ctx, self.cipher.get_pointer(), 0, k, IV, do)
else: else:
@ -59,7 +59,7 @@ class Cipher(object):
"""Update result with more data""" """Update result with more data"""
i = OpenSSL.c_int(0) i = OpenSSL.c_int(0)
buffer = OpenSSL.malloc(b"", len(input) + self.cipher.get_blocksize()) buffer = OpenSSL.malloc(b"", len(input) + self.cipher.get_blocksize())
inp = OpenSSL.malloc(input, len(input)) inp = OpenSSL.malloc(bytes(input), len(input))
if OpenSSL.EVP_CipherUpdate(self.ctx, OpenSSL.byref(buffer), if OpenSSL.EVP_CipherUpdate(self.ctx, OpenSSL.byref(buffer),
OpenSSL.byref(i), inp, len(input)) == 0: OpenSSL.byref(i), inp, len(input)) == 0:
raise Exception("[OpenSSL] EVP_CipherUpdate FAIL ...") raise Exception("[OpenSSL] EVP_CipherUpdate FAIL ...")

View File

@ -7,6 +7,7 @@ Asymmetric cryptography using elliptic curves
from hashlib import sha512 from hashlib import sha512
from struct import pack, unpack from struct import pack, unpack
from ctypes import c_char_p
from .cipher import Cipher from .cipher import Cipher
from .hash import equals, hmac_sha256 from .hash import equals, hmac_sha256
@ -218,8 +219,8 @@ class ECC(object):
if other_key == 0: if other_key == 0:
raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...") raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")
other_pub_key_x = OpenSSL.BN_bin2bn(pubkey_x, len(pubkey_x), None) other_pub_key_x = OpenSSL.BN_bin2bn(c_char_p(bytes(pubkey_x)), len(pubkey_x), None)
other_pub_key_y = OpenSSL.BN_bin2bn(pubkey_y, len(pubkey_y), None) other_pub_key_y = OpenSSL.BN_bin2bn(c_char_p(bytes(pubkey_y)), len(pubkey_y), None)
other_group = OpenSSL.EC_KEY_get0_group(other_key) other_group = OpenSSL.EC_KEY_get0_group(other_key)
other_pub_key = OpenSSL.EC_POINT_new(other_group) other_pub_key = OpenSSL.EC_POINT_new(other_group)

View File

@ -38,7 +38,7 @@ def hmac_sha256(k, m):
Compute the key and the message with HMAC SHA5256 Compute the key and the message with HMAC SHA5256
""" """
key = OpenSSL.malloc(k, len(k)) key = OpenSSL.malloc(k, len(k))
d = OpenSSL.malloc(m, len(m)) d = OpenSSL.malloc(bytes(m), len(m))
md = OpenSSL.malloc(0, 32) md = OpenSSL.malloc(0, 32)
i = OpenSSL.pointer(OpenSSL.c_int(0)) i = OpenSSL.pointer(OpenSSL.c_int(0))
OpenSSL.HMAC(OpenSSL.EVP_sha256(), key, len(k), d, len(m), md, i) OpenSSL.HMAC(OpenSSL.EVP_sha256(), key, len(k), d, len(m), md, i)

View File

@ -151,7 +151,8 @@ def reloadBroadcastSendersForWhichImWatching():
encodeVarint(addressVersionNumber) encodeVarint(addressVersionNumber)
+ encodeVarint(streamNumber) + hashobj + encodeVarint(streamNumber) + hashobj
).digest()[:32] ).digest()[:32]
MyECSubscriptionCryptorObjects[hashobj] = \ hex_hash = hexlify(hashobj).decode('ascii')
MyECSubscriptionCryptorObjects[hex_hash] = \
highlevelcrypto.makeCryptor(hexlify(privEncryptionKey)) highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))
else: else:
doubleHashOfAddressData = highlevelcrypto.double_sha512( doubleHashOfAddressData = highlevelcrypto.double_sha512(
@ -160,7 +161,8 @@ def reloadBroadcastSendersForWhichImWatching():
) )
tag = doubleHashOfAddressData[32:] tag = doubleHashOfAddressData[32:]
privEncryptionKey = doubleHashOfAddressData[:32] privEncryptionKey = doubleHashOfAddressData[:32]
MyECSubscriptionCryptorObjects[tag] = \ hex_tag = hexlify(tag).decode('ascii')
MyECSubscriptionCryptorObjects[hex_tag] = \
highlevelcrypto.makeCryptor(hexlify(privEncryptionKey)) highlevelcrypto.makeCryptor(hexlify(privEncryptionKey))