class Md4_u(Utility): """utility class for L{MD4<Md4>} cryptographic hash""" constants = [hsqrt(i) for i in [0, 2, 3]] shifts = [[3, 7, 11, 19], [3, 5, 9, 13], [3, 9, 11, 15]] r = [ [ i, ((i * 4) + (i / 4)) % 16, # 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15][i] ] for i in range(16) ] @staticmethod def f(b, c, d): return (b & c) | ((~b) & d) @staticmethod def g(b, c, d): return (b & c) | (b & d) | (c & d) @staticmethod def h(b, c, d): return (b ^ c ^ d) hex = "0123456789ABCDEF" IVhex = hex + hex[::-1] IVs = dwords(struct.unpack("<4L", hex2bin(IVhex)))
def decrypt(self, ciphertext, key): #L = ciphertext[:self.middle] #R = ciphertext[self.middle:] #L, R = dwords([0x41ea3a0a, 0x94baa940]) L, R = dwords([0xdee9d4d8, 0xf7131ed9]) # for xtea for F, extra in self.round_parameters(backward=True): L, R = self.out_f(R, F(L, key, *extra)), L return L, R
class Sha224_u(Utility): """utility class for sha-224""" pickled = get_variables("sha224", ["IVs"]) if pickled is None: # lowest 32 bits of sha384 IVs IVs = dwords(Sha384_u.IVs) save_variables("sha224", {"IVs": IVs}) else: IVs = pickled["IVs"]
class Md5_u(Utility): """utility class for MD5 cryptographic hash""" g_coefficients = [[1, 0], [5, 1], [3, 5], [7, 0]] K = dwords([abs(sin(index + 1)) * (2**32) for index in range(16 * 4)]) shifts = [[7, 12, 17, 22], [5, 9, 14, 20], [4, 11, 16, 23], [6, 10, 15, 21]] @staticmethod def g(b, c, d): return (b & d) | (c & (~d)) @staticmethod def i(b, c, d): return c ^ (b | (~d))
""" def F(self, R, key, *extra): delta, shift = extra return (((R << 4) ^ (R >> 5)) + R) ^ (delta + key[(delta >> shift) & 3]) def round_parameters(self, backward): if not backward: delta = 0 for _ in xrange(self.cycles): yield self.F, [delta, 0] delta += Tea_u.constant yield self.F, [delta, 11] else: delta = self.cycles * Tea_u.constant for _ in xrange(self.cycles): yield self.F, [delta, 11] delta -= Tea_u.constant yield self.F, [delta, 0] if __name__ == "__main__": print [str(i) for i in Tea(64).crypt("\x00" * 8, dwords([0, 0, 0, 0]))] print [str(i) for i in Tea(64).decrypt("\x41\xea\x3a\x0a\x94\xba\xa9\x40", dwords([0, 0, 0, 0]))] print #assert tea().crypt("\x00" * 8, Dwords([0, 0, 0, 0])) == Dwords([0x41ea3a0a, 0x94baa940]) #assert tea().decrypt("\x41\xea\x3a\x0a\x94\xba\xa9\x40", Dwords([0, 0, 0, 0])) == [0, 0] print [str(i) for i in Xtea(64).crypt("\x00" * 8, dwords([0, 0, 0, 0]))] #XTEA ['\xdee9d4d8', '\xf7131ed9'] print [str(i) for i in Xtea(64).decrypt("\xDE\xE9\xD4\xD8\xF7\x13\x1E\xD9" , dwords([0, 0, 0, 0]))]