Move randomBytes to highlevelcrypto

This commit is contained in:
Dmitri Bogomolov 2021-12-09 18:44:24 +02:00 committed by Lee Miller
parent feaee60632
commit c7a3bfacfa
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
4 changed files with 20 additions and 21 deletions

View File

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

View File

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

View File

@ -8,6 +8,7 @@ High level cryptographic functions based on `.pyelliptic` OpenSSL bindings.
"""
import hashlib
import os
from binascii import hexlify
import pyelliptic
@ -17,7 +18,8 @@ from pyelliptic import arithmetic as a
__all__ = [
'decodeWalletImportFormat', 'encodeWalletImportFormat',
'encrypt', 'makeCryptor', 'pointMult', 'privToPub', 'sign', 'verify']
'encrypt', 'makeCryptor', 'pointMult', 'privToPub', 'randomBytes',
'sign', 'verify']
# WIF (uses arithmetic ):
@ -49,6 +51,16 @@ def encodeWalletImportFormat(privKey):
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'):
"""Return a private `.pyelliptic.ECC` instance"""
private_key = a.changebase(privkey, 16, 256, minlen=32)

View File

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