2015-08-20 17:27:15 +02:00
|
|
|
import binascii
|
|
|
|
import logging
|
2017-09-07 17:54:39 +02:00
|
|
|
from twisted.internet import defer
|
2017-07-24 09:05:40 +02:00
|
|
|
from cryptography.hazmat.primitives.ciphers import Cipher, modes
|
|
|
|
from cryptography.hazmat.primitives.ciphers.algorithms import AES
|
|
|
|
from cryptography.hazmat.primitives.padding import PKCS7
|
|
|
|
from cryptography.hazmat.backends import default_backend
|
2016-12-21 20:55:43 +01:00
|
|
|
from lbrynet import conf
|
2015-08-20 17:27:15 +02:00
|
|
|
from lbrynet.core.BlobInfo import BlobInfo
|
|
|
|
|
|
|
|
|
2015-09-08 21:42:56 +02:00
|
|
|
log = logging.getLogger(__name__)
|
2017-07-24 09:05:40 +02:00
|
|
|
backend = default_backend()
|
2015-09-08 21:42:56 +02:00
|
|
|
|
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
class CryptBlobInfo(BlobInfo):
|
|
|
|
def __init__(self, blob_hash, blob_num, length, iv):
|
|
|
|
BlobInfo.__init__(self, blob_hash, blob_num, length)
|
|
|
|
self.iv = iv
|
|
|
|
|
|
|
|
|
|
|
|
class StreamBlobDecryptor(object):
|
|
|
|
def __init__(self, blob, key, iv, length):
|
2017-06-26 23:50:51 +02:00
|
|
|
"""
|
|
|
|
This class decrypts blob
|
|
|
|
|
|
|
|
blob - object which implements read() function.
|
|
|
|
key = encryption_key
|
|
|
|
iv = initialization vector
|
|
|
|
blob_num = blob number (has no effect on encryption)
|
|
|
|
length = length in bytes of blob
|
|
|
|
"""
|
2015-08-20 17:27:15 +02:00
|
|
|
self.blob = blob
|
|
|
|
self.key = key
|
|
|
|
self.iv = iv
|
|
|
|
self.length = length
|
|
|
|
self.buff = b''
|
|
|
|
self.len_read = 0
|
2017-07-24 09:05:40 +02:00
|
|
|
cipher = Cipher(AES(self.key), modes.CBC(self.iv), backend=backend)
|
|
|
|
self.unpadder = PKCS7(AES.block_size).unpadder()
|
|
|
|
self.cipher = cipher.decryptor()
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def decrypt(self, write_func):
|
2017-06-26 23:50:51 +02:00
|
|
|
"""
|
|
|
|
Decrypt blob and write its content useing write_func
|
|
|
|
|
|
|
|
write_func - function that takes decrypted string as
|
|
|
|
arugment and writes it somewhere
|
|
|
|
"""
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def remove_padding(data):
|
2017-07-24 09:05:40 +02:00
|
|
|
return self.unpadder.update(data) + self.unpadder.finalize()
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def write_bytes():
|
|
|
|
if self.len_read < self.length:
|
2017-07-24 09:05:40 +02:00
|
|
|
num_bytes_to_decrypt = greatest_multiple(len(self.buff), (AES.block_size / 8))
|
2016-11-30 21:20:45 +01:00
|
|
|
data_to_decrypt, self.buff = split(self.buff, num_bytes_to_decrypt)
|
2017-07-24 09:05:40 +02:00
|
|
|
write_func(self.cipher.update(data_to_decrypt))
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def finish_decrypt():
|
2017-09-07 17:55:18 +02:00
|
|
|
bytes_left = len(self.buff) % (AES.block_size / 8)
|
|
|
|
if bytes_left != 0:
|
|
|
|
log.warning(self.buff[-1 * (AES.block_size / 8):].encode('hex'))
|
|
|
|
raise Exception("blob %s has incorrect padding: %i bytes left" %
|
|
|
|
(self.blob.blob_hash, bytes_left))
|
2015-08-20 17:27:15 +02:00
|
|
|
data_to_decrypt, self.buff = self.buff, b''
|
2017-07-24 09:05:40 +02:00
|
|
|
last_chunk = self.cipher.update(data_to_decrypt) + self.cipher.finalize()
|
|
|
|
write_func(remove_padding(last_chunk))
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def decrypt_bytes(data):
|
|
|
|
self.buff += data
|
|
|
|
self.len_read += len(data)
|
|
|
|
write_bytes()
|
|
|
|
|
|
|
|
d = self.blob.read(decrypt_bytes)
|
|
|
|
d.addCallback(lambda _: finish_decrypt())
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
class CryptStreamBlobMaker(object):
|
|
|
|
def __init__(self, key, iv, blob_num, blob):
|
2017-06-26 23:50:51 +02:00
|
|
|
"""
|
|
|
|
This class encrypts data and writes it to a new blob
|
|
|
|
|
|
|
|
key = encryption_key
|
|
|
|
iv = initialization vector
|
|
|
|
blob_num = blob number (has no effect on encryption)
|
|
|
|
blob = object which implements write(), close() function , close() function must
|
|
|
|
be a deferred. (Will generally be of HashBlobCreator type)
|
|
|
|
"""
|
2015-08-20 17:27:15 +02:00
|
|
|
self.key = key
|
|
|
|
self.iv = iv
|
|
|
|
self.blob_num = blob_num
|
|
|
|
self.blob = blob
|
2017-07-24 09:05:40 +02:00
|
|
|
cipher = Cipher(AES(self.key), modes.CBC(self.iv), backend=backend)
|
|
|
|
self.padder = PKCS7(AES.block_size).padder()
|
|
|
|
self.cipher = cipher.encryptor()
|
2015-08-20 17:27:15 +02:00
|
|
|
self.length = 0
|
|
|
|
|
|
|
|
def write(self, data):
|
2017-06-26 23:50:51 +02:00
|
|
|
"""
|
|
|
|
encrypt and write string data
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
tuple (done, num_bytes_to_write) where done is True if
|
|
|
|
max bytes are written. num_bytes_to_write is the number
|
|
|
|
of bytes that will be written from data in this call
|
|
|
|
"""
|
2017-01-17 04:23:20 +01:00
|
|
|
max_bytes_to_write = conf.settings['BLOB_SIZE'] - self.length - 1
|
2015-08-20 17:27:15 +02:00
|
|
|
done = False
|
|
|
|
if max_bytes_to_write <= len(data):
|
|
|
|
num_bytes_to_write = max_bytes_to_write
|
|
|
|
done = True
|
|
|
|
else:
|
|
|
|
num_bytes_to_write = len(data)
|
|
|
|
data_to_write = data[:num_bytes_to_write]
|
2017-07-24 09:05:40 +02:00
|
|
|
self.length += len(data_to_write)
|
|
|
|
padded_data = self.padder.update(data_to_write)
|
|
|
|
encrypted_data = self.cipher.update(padded_data)
|
|
|
|
self.blob.write(encrypted_data)
|
2015-08-20 17:27:15 +02:00
|
|
|
return done, num_bytes_to_write
|
|
|
|
|
2017-09-07 17:54:39 +02:00
|
|
|
@defer.inlineCallbacks
|
2015-08-20 17:27:15 +02:00
|
|
|
def close(self):
|
2015-09-08 21:42:56 +02:00
|
|
|
log.debug("closing blob %s with plaintext len %s", str(self.blob_num), str(self.length))
|
2015-08-20 17:27:15 +02:00
|
|
|
if self.length != 0:
|
2017-09-07 17:54:39 +02:00
|
|
|
self.length += (AES.block_size / 8) - (self.length % (AES.block_size / 8))
|
|
|
|
padded_data = self.padder.finalize()
|
|
|
|
encrypted_data = self.cipher.update(padded_data) + self.cipher.finalize()
|
|
|
|
self.blob.write(encrypted_data)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2017-09-07 17:54:39 +02:00
|
|
|
blob_hash = yield self.blob.close()
|
|
|
|
log.debug("called the finished_callback from CryptStreamBlobMaker.close")
|
|
|
|
blob = CryptBlobInfo(blob_hash, self.blob_num, self.length, binascii.hexlify(self.iv))
|
|
|
|
defer.returnValue(blob)
|
2016-11-04 21:09:40 +01:00
|
|
|
|
2016-11-30 21:20:45 +01:00
|
|
|
|
|
|
|
def greatest_multiple(a, b):
|
|
|
|
"""return the largest value `c`, that is a multiple of `b` and is <= `a`"""
|
|
|
|
return (a // b) * b
|
|
|
|
|
|
|
|
|
2016-11-04 21:09:40 +01:00
|
|
|
def split(buff, cutoff):
|
|
|
|
return buff[:cutoff], buff[cutoff:]
|