def test_reject_unicode(self, backend): poly = Poly1305(b"0" * 32) with pytest.raises(TypeError): poly.update("") # type:ignore[arg-type] with pytest.raises(TypeError): Poly1305.generate_tag(b"0" * 32, "") # type:ignore[arg-type]
def test_reject_unicode(self, backend): poly = Poly1305(b"0" * 32) with pytest.raises(TypeError): poly.update(u"") with pytest.raises(TypeError): Poly1305.generate_tag(b"0" * 32, u"")
def test_invalid_key_length(self, backend): with pytest.raises(ValueError): Poly1305(b"0" * 31) with pytest.raises(ValueError): Poly1305.generate_tag(b"0" * 31, b"msg") with pytest.raises(ValueError): Poly1305(b"0" * 33) with pytest.raises(ValueError): Poly1305.generate_tag(b"0" * 33, b"msg")
def test_vectors(self, vector, backend): key = binascii.unhexlify(vector["key"]) msg = binascii.unhexlify(vector["msg"]) tag = binascii.unhexlify(vector["tag"]) poly = Poly1305(key) poly.update(msg) assert poly.finalize() == tag assert Poly1305.generate_tag(key, msg) == tag Poly1305.verify_tag(key, msg, tag)
def test_buffer_protocol(self, backend): key = binascii.unhexlify( b"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cb" b"c207075c0") msg = binascii.unhexlify( b"2754776173206272696c6c69672c20616e642074686520736c69746" b"87920746f7665730a446964206779726520616e642067696d626c65" b"20696e2074686520776162653a0a416c6c206d696d7379207765726" b"52074686520626f726f676f7665732c0a416e6420746865206d6f6d" b"65207261746873206f757467726162652e") key = bytearray(key) poly = Poly1305(key) poly.update(bytearray(msg)) assert poly.finalize() == binascii.unhexlify( b"4541669a7eaaee61e708dc7cbcc5eb62") assert Poly1305.generate_tag( key, msg) == binascii.unhexlify(b"4541669a7eaaee61e708dc7cbcc5eb62")
def test_invalid_key_type(self, backend): with pytest.raises(TypeError): Poly1305(object()) with pytest.raises(TypeError): Poly1305.generate_tag(object(), b"msg")
def poly1305(key, data, nonce): """Compute a Poly1305 tag for a block of data""" return Poly1305.generate_tag(poly1305_key(key, nonce), data)