Module refinery.units.crypto.cipher.rc4mod

Expand source code Browse git
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from itertools import cycle

from . import arg, StreamCipherUnit


class rc4mod(StreamCipherUnit):
    """
    Implements a modified version of the RC4 stream cipher where the size of the RC4 SBox can be altered.
    """

    def __init__(
        self, key, stateful=False, *,
        size: arg.number('-t', help='Table size, {default} by default.', bound=(1, None)) = 0x100
    ):
        super().__init__(key=key, stateful=stateful, size=size)

    def keystream(self):
        size = self.args.size
        tablerange = range(max(size, 0x100))
        b, table = 0, bytearray(k & 0xFF for k in tablerange)
        for a, keybyte in zip(tablerange, cycle(self.args.key)):
            t = table[a]
            b = (b + keybyte + t) % size
            table[a] = table[b]
            table[b] = t
        self.log_debug(lambda: F'SBOX = {table.hex(" ").upper()}', clip=True)
        b, a = 0, 0
        while True:
            a = (a + 1) % size
            t = table[a]
            b = (b + t) % size
            table[a] = table[b]
            table[b] = t
            yield table[(table[a] + t) % size]

Classes

class rc4mod (key, stateful=False, *, size=256)

Implements a modified version of the RC4 stream cipher where the size of the RC4 SBox can be altered.

Expand source code Browse git
class rc4mod(StreamCipherUnit):
    """
    Implements a modified version of the RC4 stream cipher where the size of the RC4 SBox can be altered.
    """

    def __init__(
        self, key, stateful=False, *,
        size: arg.number('-t', help='Table size, {default} by default.', bound=(1, None)) = 0x100
    ):
        super().__init__(key=key, stateful=stateful, size=size)

    def keystream(self):
        size = self.args.size
        tablerange = range(max(size, 0x100))
        b, table = 0, bytearray(k & 0xFF for k in tablerange)
        for a, keybyte in zip(tablerange, cycle(self.args.key)):
            t = table[a]
            b = (b + keybyte + t) % size
            table[a] = table[b]
            table[b] = t
        self.log_debug(lambda: F'SBOX = {table.hex(" ").upper()}', clip=True)
        b, a = 0, 0
        while True:
            a = (a + 1) % size
            t = table[a]
            b = (b + t) % size
            table[a] = table[b]
            table[b] = t
            yield table[(table[a] + t) % size]

Ancestors

Class variables

var blocksize
var key_sizes

Methods

def keystream(self)
Expand source code Browse git
def keystream(self):
    size = self.args.size
    tablerange = range(max(size, 0x100))
    b, table = 0, bytearray(k & 0xFF for k in tablerange)
    for a, keybyte in zip(tablerange, cycle(self.args.key)):
        t = table[a]
        b = (b + keybyte + t) % size
        table[a] = table[b]
        table[b] = t
    self.log_debug(lambda: F'SBOX = {table.hex(" ").upper()}', clip=True)
    b, a = 0, 0
    while True:
        a = (a + 1) % size
        t = table[a]
        b = (b + t) % size
        table[a] = table[b]
        table[b] = t
        yield table[(table[a] + t) % size]

Inherited members