-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Api updates corresponding to post-0.2 rust-umbral
PRs
#272
Changes from all commits
7abd5c0
2e046fd
d6d3a27
7e75ecb
a9050f4
a63b1ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,10 @@ | |
from .curve_point import CurvePoint | ||
from .dem import kdf | ||
from .hashing import Hash | ||
from .serializable import Serializable, Deserializable | ||
from .serializable import Serializable, SerializableSecret, Deserializable | ||
|
||
|
||
class SecretKey(Serializable, Deserializable): | ||
class SecretKey(SerializableSecret, Deserializable): | ||
""" | ||
Umbral secret (private) key. | ||
""" | ||
|
@@ -33,9 +33,6 @@ def public_key(self) -> 'PublicKey': | |
""" | ||
return self._public_key | ||
|
||
def __eq__(self, other): | ||
return self._scalar_key == other._scalar_key | ||
|
||
def __str__(self): | ||
return f"{self.__class__.__name__}:..." | ||
|
||
|
@@ -53,7 +50,7 @@ def serialized_size(cls): | |
def _from_exact_bytes(cls, data: bytes): | ||
return cls(CurveScalar._from_exact_bytes(data)) | ||
|
||
def __bytes__(self) -> bytes: | ||
def to_secret_bytes(self) -> bytes: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return bytes(self._scalar_key) | ||
|
||
|
||
|
@@ -91,15 +88,15 @@ def __hash__(self) -> int: | |
return hash((self.__class__, bytes(self))) | ||
|
||
|
||
class SecretKeyFactory(Serializable, Deserializable): | ||
class SecretKeyFactory(SerializableSecret, Deserializable): | ||
""" | ||
This class handles keyring material for Umbral, by allowing deterministic | ||
derivation of :py:class:`SecretKey` objects based on labels. | ||
|
||
Don't use this key material directly as a key. | ||
""" | ||
|
||
_KEY_SEED_SIZE = 64 | ||
_KEY_SEED_SIZE = 32 | ||
_DERIVED_KEY_SIZE = 64 | ||
|
||
def __init__(self, key_seed: bytes): | ||
|
@@ -112,9 +109,32 @@ def random(cls) -> 'SecretKeyFactory': | |
""" | ||
return cls(os.urandom(cls._KEY_SEED_SIZE)) | ||
|
||
@classmethod | ||
def seed_size(cls): | ||
""" | ||
Returns the seed size required by | ||
:py:meth:`~SecretKeyFactory.from_secure_randomness`. | ||
""" | ||
return cls._KEY_SEED_SIZE | ||
|
||
@classmethod | ||
def from_secure_randomness(cls, seed: bytes) -> 'SecretKeyFactory': | ||
""" | ||
Creates a secret key factory using the given random bytes | ||
(of size :py:meth:`~SecretKeyFactory.seed_size`). | ||
|
||
.. warning:: | ||
|
||
Make sure the given seed has been obtained | ||
from a cryptographically secure source of randomness! | ||
""" | ||
if len(seed) != cls.seed_size(): | ||
raise ValueError(f"Expected {cls.seed_size()} bytes, got {len(seed)}") | ||
return cls(seed) | ||
|
||
def secret_key_by_label(self, label: bytes) -> SecretKey: | ||
""" | ||
Creates a :py:class:`SecretKey` from the given label. | ||
Creates a :py:class:`SecretKey` deterministically from the given label. | ||
""" | ||
tag = b"KEY_DERIVATION/" + label | ||
key = kdf(self.__key_seed, self._DERIVED_KEY_SIZE, info=tag) | ||
|
@@ -125,6 +145,14 @@ def secret_key_by_label(self, label: bytes) -> SecretKey: | |
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related to this PR but we discussed this extra hoop before in discord (https://discord.com/channels/411401661714792449/411401661714792451/860193967923527730) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I remember - see my concerns in nucypher/rust-umbral#64 |
||
return SecretKey(scalar_key) | ||
|
||
def secret_key_factory_by_label(self, label: bytes) -> 'SecretKeyFactory': | ||
""" | ||
Creates a :py:class:`SecretKeyFactory` deterministically from the given label. | ||
""" | ||
tag = b"FACTORY_DERIVATION/" + label | ||
key_seed = kdf(self.__key_seed, self._KEY_SEED_SIZE, info=tag) | ||
return SecretKeyFactory(key_seed) | ||
|
||
@classmethod | ||
def serialized_size(cls): | ||
return cls._KEY_SEED_SIZE | ||
|
@@ -133,7 +161,7 @@ def serialized_size(cls): | |
def _from_exact_bytes(cls, data: bytes): | ||
return cls(data) | ||
|
||
def __bytes__(self) -> bytes: | ||
def to_secret_bytes(self) -> bytes: | ||
return bytes(self.__key_seed) | ||
|
||
def __str__(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be
by_label
orfrom_label
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would also be my guess, but I think
from
prefix is widely accepted as a naming convention forFrom<>
trait implementation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"by_label" is a leftover from PyUmbral 0.1.
from
is indeed used generally for constructors.Come to think of it, perhaps
make_secret_key()
/make_secret_key_factory()
(orderive_
, borrowing from PyUmbral 0.1) would be a better name for these methods.by_label
in my mind implies that there is some lookup going on and not creation.