Ejemplo n.º 1
0
class SubBytes(_Logger):
    '''This class is made to do the subBytes Rijndael's non-linear
       substitution to provide confusion to the ciphertext, and its inverse.
       It uses a secondary object SBox as a builder pattern to allow the
       transformation from this operation.
       FIXME: The precalculated SBoxes shall be replaced by the calculations
       themselves specially to allow arbitrary word sizes and not only the
       original 8 bits and the two included here for 2 and 4 bits.
    '''
    def __init__(self, wordSize, loglevel=_Logger._info,
                 *args, **kwargs):
        super(SubBytes, self).__init__(*args, **kwargs)
        self.__wordSize = wordSize
        self.__sbox = SBox(wordSize, loglevel=loglevel)

    def __str__(self):
        parentesis = "%d" % (self.__wordSize)
        return "MixColumns(%s)" % (parentesis)

    def __repr__(self):
        return "%s Mu=%s, Nu=%s, ring=%s, field=%s" % (self.__str__(), self.Mu,
                                                       self.Nu, self.Ring,
                                                       self.Field)

    @property
    def Field(self):
        return _deepcopy(self.__sbox.getField())

    @property
    def Ring(self):
        return _deepcopy(self.__sbox.getRing())

    @property
    def Mu(self):
        return _deepcopy(self.__sbox.getMu())

    @property
    def Nu(self):
        return _deepcopy(self.__sbox.getNu())

    @property
    def SBox(self):
        return self.__sbox

    def do(self, input):
        output = self.__sbox.transform(input)
        self._debug_stream("%s -> %s" % (input, output), operation="subBytes")
        return output

    def invert(self, input):
        output = self.__sbox.transform(input, invert=True)
        self._debug_stream("%s -> %s" % (input, output),
                           operation="invSubBytes")
        return output
Ejemplo n.º 2
0
 def __init__(self, wordSize, loglevel=_Logger._info,
              *args, **kwargs):
     super(SubBytes, self).__init__(*args, **kwargs)
     self.__wordSize = wordSize
     self.__sbox = SBox(wordSize, loglevel=loglevel)