mirror of https://github.com/nucypher/nucypher.git
commit
b930929b54
|
@ -8,3 +8,4 @@ NO_DECRYPTION_PERFORMED = 455
|
|||
KFRAG_LENGTH = 194
|
||||
CFRAG_LENGTH = 131
|
||||
CAPSULE_LENGTH = 98
|
||||
PUBLIC_KEY_LENGTH = 33
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
from umbral import umbral
|
||||
|
||||
|
||||
class MessageKit:
|
||||
|
||||
def __init__(self, ciphertext, capsule, alice_pubkey=None):
|
||||
self.ciphertext = ciphertext
|
||||
self.capsule = capsule
|
||||
self.alice_pub_key = alice_pubkey
|
||||
|
||||
def decrypt(self, privkey):
|
||||
return umbral.decrypt(
|
||||
self.capsule,
|
||||
self.ciphertext,
|
||||
self.alice_pubkey
|
||||
)
|
||||
|
||||
|
||||
class MapKit(MessageKit):
|
||||
|
||||
def __init__(self, ciphertext, capsule, treasure_map, alice_pubkey=None):
|
||||
super().__init__(ciphertext, capsule, alice_pubkey)
|
||||
self.treasure_map = treasure_map
|
|
@ -1,13 +1,11 @@
|
|||
import inspect
|
||||
from typing import Iterable, List, Tuple, Type, Union
|
||||
from typing import Iterable, List, Tuple
|
||||
|
||||
from nkms.crypto import api as API
|
||||
from nkms.crypto.signature import Signature
|
||||
from nkms.keystore import keypairs
|
||||
from nkms.keystore.keypairs import SigningKeypair, EncryptingKeypair
|
||||
from nkms.keystore.keystore import KeyStore
|
||||
|
||||
from umbral.keys import UmbralPrivateKey, UmbralPublicKey
|
||||
from umbral.keys import UmbralPublicKey
|
||||
|
||||
|
||||
class PowerUpError(TypeError):
|
||||
|
@ -39,7 +37,8 @@ class CryptoPower(object):
|
|||
power_up_instance = power_up
|
||||
elif CryptoPowerUp in inspect.getmro(power_up):
|
||||
power_up_class = power_up
|
||||
power_up_instance = power_up(generate_keys_if_needed=self.generate_keys)
|
||||
power_up_instance = power_up(
|
||||
generate_keys_if_needed=self.generate_keys)
|
||||
else:
|
||||
raise TypeError(
|
||||
("power_up must be a subclass of CryptoPowerUp or an instance "
|
||||
|
@ -106,27 +105,23 @@ class CryptoPowerUp(object):
|
|||
|
||||
|
||||
class KeyPairBasedPower(CryptoPowerUp):
|
||||
def __init__(self,
|
||||
umbral_key: Union[UmbralPrivateKey, UmbralPublicKey]=None,
|
||||
generate_keys_if_needed=True,
|
||||
) -> None:
|
||||
_keypair_class = keypairs.Keypair
|
||||
|
||||
try:
|
||||
# Attmept to get pubkey from private key. If it's a pubkey, use it.
|
||||
self.pub_key = umbral_key.get_pub_key()
|
||||
self.priv_key = umbral_key
|
||||
except NotImplementedError:
|
||||
self.pub_key = umbral_key
|
||||
except AttributeError:
|
||||
# They didn't pass anything we recognize as a valid key.
|
||||
if generate_keys_if_needed:
|
||||
# Let's generate.
|
||||
self.priv_key = UmbralPrivateKey.gen_key()
|
||||
self.pub_key = self.priv_key.gen_key()
|
||||
else:
|
||||
raise ValueError("Either pass a valid key as umbral_key or, if you want to generate keys, set generate_keys_if_needed to True.")
|
||||
def __init__(self, keypair: keypairs.Keypair = None,
|
||||
pubkey_bytes: bytes = None,
|
||||
generate_keys_if_needed=True) -> None:
|
||||
if keypair and pubkey_bytes:
|
||||
raise ValueError(
|
||||
"Pass keypair or pubkey_bytes (or neither), but not both.")
|
||||
elif keypair:
|
||||
self.keypair = keypair
|
||||
else:
|
||||
raise
|
||||
# They didn't pass a keypair; we'll make one with the bytes (if any)
|
||||
# they provided.
|
||||
self.keypair = self._keypair_class.load_key(
|
||||
UmbralPublicKey(pubkey_bytes),
|
||||
generate_keys_if_needed=generate_keys_if_needed)
|
||||
|
||||
|
||||
class SigningPower(KeyPairBasedPower):
|
||||
confers_public_key = True
|
||||
|
@ -143,7 +138,7 @@ class SigningPower(KeyPairBasedPower):
|
|||
return self.keypair.sign(msghash)
|
||||
|
||||
def public_key(self):
|
||||
return self.pub_key
|
||||
return self.keypair.pubkey
|
||||
|
||||
|
||||
class EncryptingPower(KeyPairBasedPower):
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from typing import Tuple, Union
|
||||
from typing import Union
|
||||
|
||||
from nkms.crypto import api as API
|
||||
from umbral.keys import UmbralPrivateKey, UmbralPublicKey
|
||||
from umbral import umbral
|
||||
from nkms.crypto.kits import MessageKit
|
||||
|
||||
|
||||
class Keypair(object):
|
||||
|
@ -11,7 +12,7 @@ class Keypair(object):
|
|||
"""
|
||||
def __init__(self,
|
||||
umbral_key: Union[UmbralPrivateKey, UmbralPublicKey]=None,
|
||||
generate_keys_if_needed=True)
|
||||
generate_keys_if_needed=True):
|
||||
"""
|
||||
Initalizes a Keypair object with an Umbral key object.
|
||||
|
||||
|
@ -57,12 +58,7 @@ class EncryptingKeypair(Keypair):
|
|||
|
||||
:return: bytes
|
||||
"""
|
||||
return umbral.decrypt(
|
||||
message_kit.capsule,
|
||||
self.privkey,
|
||||
message_kit.ciphertext,
|
||||
message_kit.alice_pubkey
|
||||
)
|
||||
return message_kit.decypt(self.privkey)
|
||||
|
||||
|
||||
class SigningKeypair(Keypair):
|
||||
|
|
|
@ -2,15 +2,15 @@ from kademlia.node import Node
|
|||
from kademlia.protocol import KademliaProtocol
|
||||
from kademlia.utils import digest
|
||||
from nkms.crypto.api import keccak_digest
|
||||
from nkms.crypto.constants import HASH_DIGEST_LENGTH
|
||||
from nkms.crypto.constants import HASH_DIGEST_LENGTH, PUBLIC_KEY_LENGTH
|
||||
from nkms.crypto.signature import Signature
|
||||
from nkms.crypto.utils import BytestringSplitter
|
||||
from nkms.keystore.keypairs import PublicKey
|
||||
from nkms.network.constants import NODE_HAS_NO_STORAGE
|
||||
from nkms.network.node import NuCypherNode
|
||||
from nkms.network.routing import NuCypherRoutingTable
|
||||
from umbral.keys import UmbralPublicKey
|
||||
|
||||
dht_value_splitter = BytestringSplitter(Signature, PublicKey, (bytes, HASH_DIGEST_LENGTH))
|
||||
dht_value_splitter = BytestringSplitter(Signature, (UmbralPublicKey, PUBLIC_KEY_LENGTH), (bytes, HASH_DIGEST_LENGTH))
|
||||
|
||||
|
||||
class NuCypherHashProtocol(KademliaProtocol):
|
||||
|
|
|
@ -11,7 +11,6 @@ from nkms.crypto.constants import NOT_SIGNED, HASH_DIGEST_LENGTH
|
|||
from nkms.crypto.powers import SigningPower
|
||||
from nkms.crypto.signature import Signature
|
||||
from nkms.crypto.utils import BytestringSplitter
|
||||
from nkms.keystore.keypairs import PublicKey
|
||||
|
||||
|
||||
class Contract(object):
|
||||
|
|
|
@ -4,7 +4,6 @@ from nkms.characters import Ursula
|
|||
from nkms.crypto.api import keccak_digest
|
||||
from nkms.crypto.powers import SigningPower, EncryptingPower
|
||||
from nkms.crypto.utils import BytestringSplitter
|
||||
from nkms.keystore.keypairs import PublicKey
|
||||
from tests.utilities import MockNetworkyStuff
|
||||
from apistar.test import TestClient
|
||||
|
||||
|
|
|
@ -1,20 +1,6 @@
|
|||
import shutil
|
||||
import os
|
||||
import appdirs
|
||||
from .fixtures import *
|
||||
|
||||
from umbral.config import set_default_curve
|
||||
from cryptography.hazmat.primitives.asymmetric import ec
|
||||
|
||||
set_default_curve(ec.SECP256K1())
|
||||
|
||||
def pytest_runtest_setup(item):
|
||||
# Monkey-patching for tests so that we don't overwrite the default db
|
||||
nkms.db.DB_NAME = 'debug-rekeys-db'
|
||||
|
||||
|
||||
def pytest_runtest_teardown(item, nextitem):
|
||||
path = os.path.join(
|
||||
appdirs.user_data_dir(nkms.db.CONFIG_APPNAME), nkms.db.DB_NAME)
|
||||
if os.path.exists(path):
|
||||
shutil.rmtree(path)
|
||||
|
|
|
@ -5,7 +5,6 @@ import sha3
|
|||
from nacl.utils import EncryptedMessage
|
||||
|
||||
from nkms.crypto import api
|
||||
from nkms.keystore.keypairs import PublicKey
|
||||
from npre import elliptic_curve as ec
|
||||
from npre import umbral
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import unittest
|
||||
from nkms.crypto import api as API
|
||||
from nkms.keystore import keypairs
|
||||
from nkms.keystore.keypairs import PublicKey
|
||||
|
||||
|
||||
class TestKeypairs(unittest.TestCase):
|
||||
|
|
|
@ -21,9 +21,9 @@ class TestKeyStore(unittest.TestCase):
|
|||
self.assertEqual(bytes, type(keypair.pubkey))
|
||||
|
||||
def test_ecdsa_keypair_generation(self):
|
||||
# TODO: Make this test actually do something instead of just checking types.
|
||||
keypair = self.ks.generate_signing_keypair()
|
||||
self.assertEqual(keypairs.SigningKeypair, type(keypair))
|
||||
self.assertEqual(bytes, type(keypair.privkey))
|
||||
|
||||
def test_key_sqlite_keystore(self):
|
||||
keypair = self.ks.generate_encrypting_keypair()
|
||||
|
|
Loading…
Reference in New Issue