Beispiel #1
0
    def _forward(self, plainText):
        """
        Plaintext から、最終ラウンドの Hamming Distance (128ビット)
        を生成する関数
        """
        if len(plainText) !=8:
            raise Exception("Invalid length of the input array")
        self.data = utility.uint16listToUint32list(plainText)
        for i in range(4):
            self.data[i] ^= self.subkey[i]
        for i in range(1, 10):
            self.ShiftRows()
            self.SBMX()
            for j in range(4):
                self.data[j] ^= self.subkey[i*4+j]
        tmp1 = self.data[:]
        # Final round
        self.ShiftRows()
        self.subBytes()
        for i in range(4):
            self.data[i] ^= self.subkey[40+i]
        tmp2 = self.data[:]

        # Calculate Hamming distance
        result = 0 
        for i in range(4):
            result += utility.hammingWeight( tmp1[i] ^ tmp2[i] )
        return int(result)
Beispiel #2
0
    def _backward(self, cipherText):
        """
        Ciphertext から、最終ラウンドの Hamming Distance (128ビット)
        を生成する関数
        """
        if len(cipherText) !=8:
            raise Exception("Invalid length of the input array")
        self.data = utility.uint16listToUint32list(cipherText)
        tmp2 = self.data[:]
        for i in range(4):
            self.data[i] ^= self.subkey[40+i]
        self.invSubBytes()
        self.invShiftRows()
        tmp1 = self.data[:]

        # Calculate Hamming distance
        result = 0 
        for i in range(4):
            result += utility.hammingWeight( tmp1[i] ^ tmp2[i] )
        return int(result)
Beispiel #3
0
    def getByteSelectionFunction(self, cipherText, type="HD"):
        ct = utility.uint16listToUint32list( cipherText )
        data = [0, 0, 0, 0]

        # すべての鍵候補を探索する。
        # myAES が uint32 ベースで実装されているので、
        # それに合わせて並列で探索を行う。
        result = [None for i in range(256*16)]
        for candidate in range(256):
            parallelCandidate = (candidate << 24) | (candidate << 16) \
                              | (candidate << 8) | candidate
            for i in range(4):
                data[i] = ct[i] ^ parallelCandidate
            data = map(myAES.invSubWord, data)
            data = myAES.invShiftRow128(data)
            tmp = self.typeList[type](data, ct)
            byte_list = _uint32listToBytelist(tmp)
            for sbox in range(16):
                result[256*sbox + candidate] = utility.hammingWeight( byte_list[sbox] )
        return result
Beispiel #4
0
 def encrypt(self, plainText, pos=0):
     tmp1 = utility.uint16listToUint32list(plainText)
     tmp2 = self._encrypt32(tmp1, pos)
     return utility.uint32listToUint16list(tmp2)
Beispiel #5
0
 def keyExpansion(self, key):
     tmp1 = utility.uint16listToUint32list(key)
     super(FaultyAES, self).keyExpansion(tmp1)
Beispiel #6
0
 def decrypt(self, cipherText):
     tmp1 = utility.uint16listToUint32list(cipherText)
     tmp2 = super(AES, self).decrypt(tmp1)
     return utility.uint32listToUint16list(tmp2)
Beispiel #7
0
 def encrypt(self, plainText):
     tmp1 = utility.uint16listToUint32list(plainText)
     tmp2 = super(AES, self).encrypt(tmp1)
     return utility.uint32listToUint16list(tmp2)
Beispiel #8
0
 def decrypt(self, cipherText):
     tmp1 = utility.uint16listToUint32list(cipherText)
     tmp2 = self._decrypt(tmp1)
     return utility.uint32listToUint16list(tmp2)
Beispiel #9
0
 def keyExpansion(self, key):
     tmp1 = utility.uint16listToUint32list(key)
     self._keyExpansion(tmp1)