Version 0.3.4, Rev656, CryptMessage plugin for AES and ECIES encryption, Added pyelliptic lib for OpenSSSL based encryption methods, Test CryptMessage plugin, Force reload content.json before signing and after write, Escaped Sql IN queries support, Test Sql parameter escaping, ui_websocket Test fixture, Plugin testing support, Always return websocket errors as dict, Wait for file on weboscket fileGet command if its already in bad_files queue, PushState and ReplaceState url manipulation support in wrapper API, Per auth-address localstorage, Longer timeout for udp tracker query
2015-12-10 21:36:20 +01:00
|
|
|
import hashlib
|
2019-03-16 02:23:00 +01:00
|
|
|
import base64
|
2019-12-15 18:30:42 +01:00
|
|
|
import binascii
|
Version 0.3.4, Rev656, CryptMessage plugin for AES and ECIES encryption, Added pyelliptic lib for OpenSSSL based encryption methods, Test CryptMessage plugin, Force reload content.json before signing and after write, Escaped Sql IN queries support, Test Sql parameter escaping, ui_websocket Test fixture, Plugin testing support, Always return websocket errors as dict, Wait for file on weboscket fileGet command if its already in bad_files queue, PushState and ReplaceState url manipulation support in wrapper API, Per auth-address localstorage, Longer timeout for udp tracker query
2015-12-10 21:36:20 +01:00
|
|
|
|
2019-12-15 18:30:42 +01:00
|
|
|
import lib.pybitcointools as btctools
|
|
|
|
from util import ThreadPool
|
|
|
|
from Crypt import Crypt
|
2019-12-15 12:46:06 +01:00
|
|
|
|
2019-12-15 18:30:42 +01:00
|
|
|
ecc_cache = {}
|
2019-12-15 12:46:06 +01:00
|
|
|
|
2019-12-15 18:30:42 +01:00
|
|
|
def eciesEncrypt(data, pubkey, ephemcurve=None, ciphername='aes-256-cbc'):
|
|
|
|
from lib import pyelliptic
|
|
|
|
pubkey_openssl = toOpensslPublickey(base64.b64decode(pubkey))
|
|
|
|
curve, pubkey_x, pubkey_y, i = pyelliptic.ECC._decode_pubkey(pubkey_openssl)
|
|
|
|
if ephemcurve is None:
|
|
|
|
ephemcurve = curve
|
|
|
|
ephem = pyelliptic.ECC(curve=ephemcurve)
|
|
|
|
key = hashlib.sha512(ephem.raw_get_ecdh_key(pubkey_x, pubkey_y)).digest()
|
|
|
|
key_e, key_m = key[:32], key[32:]
|
|
|
|
pubkey = ephem.get_pubkey()
|
|
|
|
iv = pyelliptic.OpenSSL.rand(pyelliptic.OpenSSL.get_cipher(ciphername).get_blocksize())
|
|
|
|
ctx = pyelliptic.Cipher(key_e, iv, 1, ciphername)
|
|
|
|
ciphertext = iv + pubkey + ctx.ciphering(data)
|
|
|
|
mac = pyelliptic.hmac_sha256(key_m, ciphertext)
|
|
|
|
return key_e, ciphertext + mac
|
Version 0.3.4, Rev656, CryptMessage plugin for AES and ECIES encryption, Added pyelliptic lib for OpenSSSL based encryption methods, Test CryptMessage plugin, Force reload content.json before signing and after write, Escaped Sql IN queries support, Test Sql parameter escaping, ui_websocket Test fixture, Plugin testing support, Always return websocket errors as dict, Wait for file on weboscket fileGet command if its already in bad_files queue, PushState and ReplaceState url manipulation support in wrapper API, Per auth-address localstorage, Longer timeout for udp tracker query
2015-12-10 21:36:20 +01:00
|
|
|
|
2019-11-25 14:37:55 +01:00
|
|
|
|
|
|
|
@Crypt.thread_pool_crypt.wrap
|
|
|
|
def eciesDecryptMulti(encrypted_datas, privatekey):
|
|
|
|
texts = [] # Decoded texts
|
|
|
|
for encrypted_data in encrypted_datas:
|
|
|
|
try:
|
|
|
|
text = eciesDecrypt(encrypted_data, privatekey).decode("utf8")
|
|
|
|
texts.append(text)
|
|
|
|
except:
|
|
|
|
texts.append(None)
|
|
|
|
return texts
|
|
|
|
|
|
|
|
|
2019-12-15 18:30:42 +01:00
|
|
|
def eciesDecrypt(encrypted_data, privatekey):
|
|
|
|
ecc_key = getEcc(privatekey)
|
|
|
|
return ecc_key.decrypt(base64.b64decode(encrypted_data))
|
|
|
|
|
|
|
|
def split(encrypted):
|
|
|
|
iv = encrypted[0:16]
|
|
|
|
ciphertext = encrypted[16 + 70:-32]
|
|
|
|
|
|
|
|
return iv, ciphertext
|
|
|
|
|
|
|
|
|
|
|
|
def getEcc(privatekey=None):
|
|
|
|
from lib import pyelliptic
|
|
|
|
global ecc_cache
|
|
|
|
if privatekey not in ecc_cache:
|
|
|
|
if privatekey:
|
|
|
|
publickey_bin = btctools.encode_pubkey(btctools.privtopub(privatekey), "bin")
|
|
|
|
publickey_openssl = toOpensslPublickey(publickey_bin)
|
|
|
|
privatekey_openssl = toOpensslPrivatekey(privatekey)
|
|
|
|
ecc_cache[privatekey] = pyelliptic.ECC(curve='secp256k1', privkey=privatekey_openssl, pubkey=publickey_openssl)
|
|
|
|
else:
|
|
|
|
ecc_cache[None] = pyelliptic.ECC()
|
|
|
|
return ecc_cache[privatekey]
|
|
|
|
|
|
|
|
|
|
|
|
def toOpensslPrivatekey(privatekey):
|
|
|
|
privatekey_bin = btctools.encode_privkey(privatekey, "bin")
|
|
|
|
return b'\x02\xca\x00\x20' + privatekey_bin
|
|
|
|
|
Version 0.3.4, Rev656, CryptMessage plugin for AES and ECIES encryption, Added pyelliptic lib for OpenSSSL based encryption methods, Test CryptMessage plugin, Force reload content.json before signing and after write, Escaped Sql IN queries support, Test Sql parameter escaping, ui_websocket Test fixture, Plugin testing support, Always return websocket errors as dict, Wait for file on weboscket fileGet command if its already in bad_files queue, PushState and ReplaceState url manipulation support in wrapper API, Per auth-address localstorage, Longer timeout for udp tracker query
2015-12-10 21:36:20 +01:00
|
|
|
|
2019-12-15 18:30:42 +01:00
|
|
|
def toOpensslPublickey(publickey):
|
|
|
|
publickey_bin = btctools.encode_pubkey(publickey, "bin")
|
|
|
|
publickey_bin = publickey_bin[1:]
|
|
|
|
publickey_openssl = b'\x02\xca\x00 ' + publickey_bin[:32] + b'\x00 ' + publickey_bin[32:]
|
|
|
|
return publickey_openssl
|