def hmac_sha1(key, message): """ Creates an HMAC using SHA-1. Args: key: The HMAC key. message: The message to generate the MAC for. Returns: The HMAC for the message under the given key """ # If the key is longer than the blocksize, # then truncate it by hashing it if (len(key) > 64): key = sha1(key).digest() # If the key is shorter than blocksize, # pad with 0s if (len(key) < 64): key = key + (b'\x00' * (64 - len(key))) o_pad = c2.xorstrs(key, b'\x5c' * 64) i_pad = c2.xorstrs(key, b'\x36' * 64) i_msg = i_pad + message o_msg = o_pad + sha1(i_msg).digest() return sha1(o_msg).digest()
def mac_sha1(message): """ Creates a message authentication code using SHA-1. Args: message: The message to create a code for. Returns: The MAC generated from the message by using SHA-1. """ return sha1(key + message).digest()
def forge_message(message, attack): """ Forge's a message with the associated MAC for a SHA-1 MAC Args: message: The untainted message attack: The message to inject using length extension Returns: The forged message, tag pair for a SHA-1 length extension attack. """ new_regs = get_state(message) glue_pad = glue_padding((b'\x00' * 16) + message) forged_message = glue_pad[16:] + attack forged_tag = sha1(attack, n_l=len(forged_message) + 16, n_h=new_regs).digest() return forged_message, forged_tag
def test_glue_padding(self): self.assertEqual(sha1(b'abc').message, glue_padding(b'abc'))