Move randomBytes to highlevelcrypto #2209

Merged
PeterSurda merged 2 commits from gitea-84 into v0.6 2024-04-13 05:08:07 +02:00
5 changed files with 28 additions and 21 deletions

View File

@ -25,10 +25,10 @@ def genAckPayload(streamNumber=1, stealthLevel=0):
if stealthLevel == 2: # Generate privacy-enhanced payload if stealthLevel == 2: # Generate privacy-enhanced payload
# Generate a dummy privkey and derive the pubkey # Generate a dummy privkey and derive the pubkey
dummyPubKeyHex = highlevelcrypto.privToPub( dummyPubKeyHex = highlevelcrypto.privToPub(
hexlify(helper_random.randomBytes(32))) hexlify(highlevelcrypto.randomBytes(32)))
# Generate a dummy message of random length # Generate a dummy message of random length
# (the smallest possible standard-formatted message is 234 bytes) # (the smallest possible standard-formatted message is 234 bytes)
dummyMessage = helper_random.randomBytes( dummyMessage = highlevelcrypto.randomBytes(
helper_random.randomrandrange(234, 801)) helper_random.randomrandrange(234, 801))
# Encrypt the message using standard BM encryption (ECIES) # Encrypt the message using standard BM encryption (ECIES)
ackdata = highlevelcrypto.encrypt(dummyMessage, dummyPubKeyHex) ackdata = highlevelcrypto.encrypt(dummyMessage, dummyPubKeyHex)
@ -36,12 +36,12 @@ def genAckPayload(streamNumber=1, stealthLevel=0):
version = 1 version = 1
elif stealthLevel == 1: # Basic privacy payload (random getpubkey) elif stealthLevel == 1: # Basic privacy payload (random getpubkey)
ackdata = helper_random.randomBytes(32) ackdata = highlevelcrypto.randomBytes(32)
acktype = 0 # getpubkey acktype = 0 # getpubkey
version = 4 version = 4
else: # Minimum viable payload (non stealth) else: # Minimum viable payload (non stealth)
ackdata = helper_random.randomBytes(32) ackdata = highlevelcrypto.randomBytes(32)
acktype = 2 # message acktype = 2 # message
version = 1 version = 1

View File

@ -1,12 +1,7 @@
"""Convenience functions for random operations. Not suitable for security / cryptography operations.""" """Convenience functions for random operations. Not suitable for security / cryptography operations."""
import os
import random import random
try:
from pyelliptic.openssl import OpenSSL
except ImportError:
from .pyelliptic.openssl import OpenSSL
NoneType = type(None) NoneType = type(None)
@ -16,14 +11,6 @@ def seed():
random.seed() random.seed()
def randomBytes(n):
"""Method randomBytes."""
try:
return os.urandom(n)
except NotImplementedError:
return OpenSSL.rand(n)
def randomshuffle(population): def randomshuffle(population):
"""Method randomShuffle. """Method randomShuffle.

View File

@ -8,6 +8,7 @@ High level cryptographic functions based on `.pyelliptic` OpenSSL bindings.
""" """
import hashlib import hashlib
import os
from binascii import hexlify from binascii import hexlify
import pyelliptic import pyelliptic
@ -17,7 +18,8 @@ from pyelliptic import arithmetic as a
__all__ = [ __all__ = [
'decodeWalletImportFormat', 'encodeWalletImportFormat', 'decodeWalletImportFormat', 'encodeWalletImportFormat',
'encrypt', 'makeCryptor', 'pointMult', 'privToPub', 'sign', 'verify'] 'encrypt', 'makeCryptor', 'pointMult', 'privToPub', 'randomBytes',
'sign', 'verify']
# WIF (uses arithmetic ): # WIF (uses arithmetic ):
@ -49,6 +51,16 @@ def encodeWalletImportFormat(privKey):
return a.changebase(privKey + checksum, 256, 58) return a.changebase(privKey + checksum, 256, 58)
# Random
def randomBytes(n):
"""Get n random bytes"""
try:
return os.urandom(n)
except NotImplementedError:
return OpenSSL.rand(n)
def makeCryptor(privkey, curve='secp256k1'): def makeCryptor(privkey, curve='secp256k1'):
"""Return a private `.pyelliptic.ECC` instance""" """Return a private `.pyelliptic.ECC` instance"""
private_key = a.changebase(privkey, 16, 256, minlen=32) private_key = a.changebase(privkey, 16, 256, minlen=32)

View File

@ -16,7 +16,7 @@ import l10n
import protocol import protocol
import state import state
from bmconfigparser import config from bmconfigparser import config
from helper_random import randomBytes from highlevelcrypto import randomBytes
from inventory import Inventory from inventory import Inventory
from queues import invQueue, receiveDataQueue, UISignalQueue from queues import invQueue, receiveDataQueue, UISignalQueue
from tr import _translate from tr import _translate

View File

@ -66,6 +66,14 @@ class TestCrypto(RIPEMD160TestCase, unittest.TestCase):
class TestHighlevelcrypto(unittest.TestCase): class TestHighlevelcrypto(unittest.TestCase):
"""Test highlevelcrypto public functions""" """Test highlevelcrypto public functions"""
def test_randomBytes(self):
"""Dummy checks for random bytes"""
for n in (8, 32, 64):
data = highlevelcrypto.randomBytes(n)
self.assertEqual(len(data), n)
self.assertNotEqual(len(set(data)), 1)
self.assertNotEqual(data, highlevelcrypto.randomBytes(n))
def test_signatures(self): def test_signatures(self):
"""Verify sample signatures and newly generated ones""" """Verify sample signatures and newly generated ones"""
pubkey_hex = hexlify(sample_pubsigningkey) pubkey_hex = hexlify(sample_pubsigningkey)