How to use the pproxy.cipherpy.StreamCipher function in pproxy

To help you get started, we’ve selected a few pproxy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github qwj / python-proxy / pproxy / cipherpy.py View on Github external
class Salsa20_Cipher(StreamCipher):
    KEY_LENGTH = 32
    IV_LENGTH = 8
    def core(self):
        data = list(struct.unpack('<16I', b'expa' + self.key[:16] + b'nd 3' + self.iv.ljust(16, b'\x00') + b'2-by' + self.key[16:] + b'te k'))
        while 1:
            H = data[:]
            for a, b, c, d in ORDERS_SALSA20:
                H[a] ^= ROL(H[b]+H[c], 7)
                H[d] ^= ROL(H[a]+H[b], 9)
                H[c] ^= ROL(H[d]+H[a], 13)
                H[b] ^= ROL(H[c]+H[d], 18)
            yield from struct.pack('<16I', *(a+b&0xffffffff for a, b in zip(H, data)))
            data[8:10] = (0, data[9]+1) if data[8]==0xffffffff else (data[8]+1, data[9])

class CFBCipher(StreamCipher):
    def setup(self):
        segment_bit = getattr(self, 'SEGMENT_SIZE', self.IV_LENGTH*8)
        self.bit_mode = segment_bit % 8 != 0
        self.stream = self.core_bit(segment_bit) if self.bit_mode else self.core(segment_bit//8)
        self.last = None
        self.cipher = self.CIPHER.new(self.key)
    def process(self, s, inv=False):
        r = bytearray()
        for i in s:
            if self.bit_mode:
                j = 0
                for k in range(7,-1,-1):
                    ibit = i>>k & 1
                    jbit = ibit^self.stream.send(self.last)
                    j |= jbit<
github qwj / python-proxy / pproxy / cipherpy.py View on Github external
def core_bit(self, segment_bit):
        next_iv = int.from_bytes(self.iv, 'big')
        mask = (1 << self.IV_LENGTH*8) - 1
        while 1:
            data = self.cipher.encrypt(next_iv)
            next_iv = next_iv<>(7-i%8)&1)<<(segment_bit-1-i)

class CFB8Cipher(CFBCipher):
    SEGMENT_SIZE = 8

class CFB1Cipher(CFBCipher):
    SEGMENT_SIZE = 1

class CTRCipher(StreamCipher):
    def setup(self):
        self.stream = self.core()
        self.cipher = self.CIPHER.new(self.key)
    def core(self):
        next_iv = int.from_bytes(self.iv, 'big')
        while 1:
            yield from self.cipher.encrypt(next_iv)
            next_iv = 0 if next_iv >= (1<<(self.IV_LENGTH*8))-1 else next_iv+1

class OFBCipher(CTRCipher):
    def core(self):
        data = self.iv
        while 1:
            data = self.cipher.encrypt(data)
            yield from data
github qwj / python-proxy / pproxy / cipherpy.py View on Github external
assert tag == poly1305(self.cipher_encrypt, nonce, s)
        data = self.cipher_encrypt(nonce, s, counter=1)
        if tag is None:
            return data, poly1305(self.cipher_encrypt, nonce, data)
        else:
            return data
    encrypt_and_digest = decrypt_and_verify = process
    def setup(self):
        self.cipher_encrypt = lambda nonce, s, counter=0: ChaCha20_IETF_Cipher(self.key, setup_key=False, counter=counter).setup_iv(nonce).encrypt(s)

class XChaCha20_IETF_POLY1305_Cipher(ChaCha20_IETF_POLY1305_Cipher):
    NONCE_LENGTH = 16+12
    def setup(self):
        self.cipher_encrypt = lambda nonce, s, counter=0: XChaCha20_IETF_Cipher(self.key, setup_key=False, counter=counter).setup_iv(nonce).encrypt(s)

class Salsa20_Cipher(StreamCipher):
    KEY_LENGTH = 32
    IV_LENGTH = 8
    def core(self):
        data = list(struct.unpack('<16I', b'expa' + self.key[:16] + b'nd 3' + self.iv.ljust(16, b'\x00') + b'2-by' + self.key[16:] + b'te k'))
        while 1:
            H = data[:]
            for a, b, c, d in ORDERS_SALSA20:
                H[a] ^= ROL(H[b]+H[c], 7)
                H[d] ^= ROL(H[a]+H[b], 9)
                H[c] ^= ROL(H[d]+H[a], 13)
                H[b] ^= ROL(H[c]+H[d], 18)
            yield from struct.pack('<16I', *(a+b&0xffffffff for a, b in zip(H, data)))
            data[8:10] = (0, data[9]+1) if data[8]==0xffffffff else (data[8]+1, data[9])

class CFBCipher(StreamCipher):
    def setup(self):
github qwj / python-proxy / pproxy / cipherpy.py View on Github external
ROL = lambda a, b: a<>32-b
ORDERS_CHACHA20 = ((0,4,8,12),(1,5,9,13),(2,6,10,14),(3,7,11,15),(0,5,10,15),(1,6,11,12),(2,7,8,13),(3,4,9,14)) * 10
ORDERS_SALSA20 = ((4,0,12,8),(9,5,1,13),(14,10,6,2),(3,15,11,7),(1,0,3,2),(6,5,4,7),(11,10,9,8),(12,15,14,13)) * 10
def ChaCha20_round(H):
    for a, b, c, d in ORDERS_CHACHA20:
        H[a] += H[b]
        H[d] = ROL(H[d]^H[a], 16)
        H[c] += H[d]
        H[b] = ROL(H[b]^H[c], 12)
        H[a] += H[b]
        H[d] = ROL(H[d]^H[a], 8)
        H[c] += H[d]
        H[b] = ROL(H[b]^H[c], 7)
    return H

class ChaCha20_Cipher(StreamCipher):
    KEY_LENGTH = 32
    IV_LENGTH = 8
    def __init__(self, key, ota=False, setup_key=True, *, counter=0):
        super().__init__(key, ota, setup_key)
        self.counter = counter
    def core(self):
        data = list(struct.unpack('<16I', b'expand 32-byte k' + self.key + self.counter.to_bytes(4, 'little') + self.iv.rjust(12, b'\x00')))
        while 1:
            yield from struct.pack('<16I', *(a+b&0xffffffff for a, b in zip(ChaCha20_round(data[:]), data)))
            data[12:14] = (0, data[13]+1) if data[12]==0xffffffff else (data[12]+1, data[13])

class ChaCha20_IETF_Cipher(ChaCha20_Cipher):
    IV_LENGTH = 12

class XChaCha20_Cipher(ChaCha20_Cipher):
    IV_LENGTH = 16+8
github qwj / python-proxy / pproxy / cipherpy.py View on Github external
def encrypt(self, s):
        return bytes.translate(s, self.encrypt_table)

class StreamCipher(BaseCipher):
    PYTHON = True
    def setup(self):
        self.stream = self.core()
    def encrypt(self, s):
        ret = bytearray()
        for i in s:
            ret.append(i^next(self.stream))
        return bytes(ret)
        #return bytes(i^next(self.stream) for i in s)
    decrypt = encrypt

class RC4_Cipher(StreamCipher):
    KEY_LENGTH = 16
    IV_LENGTH = 0
    def core(self):
        data = list(range(256))
        y = 0
        for x in range(256):
            y = self.key[x%self.KEY_LENGTH]+data[x]+y & 0xff
            data[x], data[y] = data[y], data[x]
        x = y = 0
        while 1:
            x = x+1 & 0xff
            y = y+data[x] & 0xff
            data[x], data[y] = data[y], data[x]
            yield data[data[x]+data[y] & 0xff]

class RC4_MD5_Cipher(RC4_Cipher):