-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
126 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/tribler-core/tribler_core/components/tag/community/tag_crypto.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import copy | ||
|
||
from cryptography.exceptions import InvalidSignature | ||
|
||
from ipv8.keyvault.crypto import default_eccrypto | ||
from ipv8.messaging.serialization import default_serializer | ||
from ipv8.types import Key | ||
from tribler_core.components.tag.community.tag_payload import TagOperationMessage | ||
|
||
|
||
class TagCrypto: | ||
def __init__(self): | ||
pass | ||
|
||
def _pack(self, message: TagOperationMessage) -> bytes: | ||
to_pack = copy.copy(message) | ||
to_pack.signature = b'' # this field is excluded from signing | ||
return default_serializer.pack_serializable(to_pack) | ||
|
||
def sign(self, message: TagOperationMessage, key: Key) -> bytes: | ||
return default_eccrypto.create_signature(key, self._pack(message)) | ||
|
||
def validate_signature(self, message: TagOperationMessage): | ||
assert message.creator_public_key | ||
key = default_eccrypto.key_from_public_bin(message.creator_public_key) | ||
if not default_eccrypto.is_valid_signature(key, self._pack(message), message.signature): | ||
raise InvalidSignature() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/tribler-core/tribler_core/components/tag/community/tests/test_tag_crypto.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
from cryptography.exceptions import InvalidSignature | ||
|
||
from ipv8.keyvault.crypto import ECCrypto | ||
from ipv8.types import Key | ||
from tribler_core.components.tag.community.tag_crypto import TagCrypto | ||
from tribler_core.components.tag.community.tag_payload import TagOperationMessage | ||
from tribler_core.components.tag.db.tag_db import Operation | ||
|
||
|
||
# pylint: disable=protected-access | ||
|
||
@pytest.fixture(name="tag_crypto") # this workaround implemented only for pylint | ||
def fixture_tag_crypto(): | ||
return TagCrypto() | ||
|
||
|
||
@pytest.fixture(name="random_message") # this workaround implemented only for pylint | ||
def fixture_random_message(): | ||
return TagOperationMessage(f'{1}'.encode() * 20, Operation.ADD, 1, f'{1}'.encode() * 74, f'{1}'.encode() * 64, | ||
''.encode()) | ||
|
||
|
||
@pytest.fixture(name="key") # this workaround implemented only for pylint | ||
def fixture_key(): | ||
return ECCrypto().generate_key(u"curve25519") | ||
|
||
|
||
pytestmark = pytest.mark.asyncio | ||
|
||
|
||
async def test_pack(tag_crypto: TagCrypto, random_message: TagOperationMessage): | ||
# ensure that signature field doesn't erase | ||
assert random_message.signature | ||
|
||
assert tag_crypto._pack(random_message) | ||
assert random_message.signature | ||
|
||
|
||
async def test_sign(tag_crypto: TagCrypto, random_message: TagOperationMessage, key: Key): | ||
assert tag_crypto.sign(random_message, key) | ||
|
||
|
||
async def test_is_signature_valid(tag_crypto: TagCrypto, random_message: TagOperationMessage, key: Key): | ||
random_message.creator_public_key = key.pub().key_to_bin() | ||
random_message.signature = tag_crypto.sign(random_message, key) | ||
tag_crypto.validate_signature(random_message) | ||
|
||
|
||
async def test_is_signature_invalid(tag_crypto: TagCrypto, random_message: TagOperationMessage, key: Key): | ||
random_message.creator_public_key = key.pub().key_to_bin() | ||
random_message.signature = tag_crypto.sign(random_message, key) | ||
with pytest.raises(InvalidSignature): | ||
random_message.tag = 'changed_tag'.encode() | ||
tag_crypto.validate_signature(random_message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters