def test_validate_invalid_message_returns_false(self): plaintext = b"You turn yourself around. That's what it's all about." key = b"Hokey Pokey" authenticated = MAC.generate(plaintext, key, SHA1) wrong_key = b"Oh no this is the wrong key" self.assertFalse(MAC.validate(authenticated, wrong_key, SHA1))
def test_length_extension_nominal_case(self): key = b"I'm gettin' rid of Britta" plaintext = b"I'm gettin' rid of the B" message = MAC.generate(plaintext, key, SHA1) newtext = b"She's a no good B" newmessage = length_extension(message, newtext, lambda msg: MAC.validate(msg, key, SHA1)) self.assertTrue(newmessage.endswith(newtext)) self.assertTrue(MAC.validate(newmessage, key, SHA1))
def test_length_extension_keysize_not_found_returns_none(self): key = b"I'm gettin' rid of Britta" plaintext = b"I'm gettin' rid of the B" message = MAC.generate(plaintext, key, SHA1) newtext = b"She's a no good B" newmessage = length_extension(message, newtext, lambda msg: MAC.validate(msg, key, SHA1), 5) self.assertIsNone(newmessage)
def main(): """ append payload to message while maintaining a valid MAC """ cookie = (b"comment1=cooking%20MCs;userdata=foo;comment2=%20like%20a%20" b"pound%20of%20bacon") key = rand_bytes_gen(random.randint(0, 128)) authenticated_cookie = MAC.generate(cookie, key, SHA1) def validation_oracle(msg): return MAC.validate(msg, key, SHA1) payload = b";admin=true" attack_cookie = length_extension( authenticated_cookie, payload, validation_oracle ) assert MAC.validate(attack_cookie, key, SHA1) print(attack_cookie)
def validation_oracle(msg): return MAC.validate(msg, key, SHA1)
def test_generate_validate_integration(self): plaintext = b"You turn yourself around. That's what it's all about." key = b"Hokey Pokey" authenticated = MAC.generate(plaintext, key, SHA1) self.assertTrue(MAC.validate(authenticated, key, SHA1))
def test_validate_message_less_than_20_bytes_returns_false(self): self.assertFalse(MAC.validate(bytes(19), b'', SHA1))