Skip to content

Commit

Permalink
Added bindings for crypto_sign_ed25519_sk_to_pk (#488)
Browse files Browse the repository at this point in the history
* Added bindings for crypto_sign_ed25519_sk_to_pk

* Updated docstring

* Faster method of extracting the public key

* Typos

* Added bindings for crypto_sign_ed25519_sk_to_seed

* Cleaned up headers
  • Loading branch information
sfstpala authored and lmctv committed Oct 30, 2018
1 parent 067daf3 commit 13fb94e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/nacl/bindings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
crypto_sign, crypto_sign_BYTES, crypto_sign_PUBLICKEYBYTES,
crypto_sign_SECRETKEYBYTES, crypto_sign_SEEDBYTES,
crypto_sign_ed25519_pk_to_curve25519, crypto_sign_ed25519_sk_to_curve25519,
crypto_sign_ed25519_sk_to_pk, crypto_sign_ed25519_sk_to_seed,
crypto_sign_ed25519ph_STATEBYTES, crypto_sign_ed25519ph_final_create,
crypto_sign_ed25519ph_final_verify, crypto_sign_ed25519ph_state,
crypto_sign_ed25519ph_update, crypto_sign_keypair, crypto_sign_open,
Expand Down Expand Up @@ -297,6 +298,8 @@
"crypto_sign_open",
"crypto_sign_ed25519_pk_to_curve25519",
"crypto_sign_ed25519_sk_to_curve25519",
"crypto_sign_ed25519_sk_to_pk",
"crypto_sign_ed25519_sk_to_seed",
"crypto_sign_ed25519ph_STATEBYTES",
"crypto_sign_ed25519ph_final_create",
"crypto_sign_ed25519ph_final_verify",
Expand Down
36 changes: 35 additions & 1 deletion src/nacl/bindings/crypto_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def crypto_sign_ed25519_sk_to_curve25519(secret_key_bytes):
Raises a ValueError if ``secret_key_bytes``is not of length
``crypto_sign_SECRETKEYBYTES``
:param public_key_bytes: bytes
:param secret_key_bytes: bytes
:rtype: bytes
"""
if len(secret_key_bytes) != crypto_sign_SECRETKEYBYTES:
Expand All @@ -165,6 +165,40 @@ def crypto_sign_ed25519_sk_to_curve25519(secret_key_bytes):
return ffi.buffer(curve_secret_key, curve_secret_key_len)[:]


def crypto_sign_ed25519_sk_to_pk(secret_key_bytes):
"""
Extract the public Ed25519 key from a secret Ed25519 key (encoded
as bytes ``secret_key_bytes``).
Raises a ValueError if ``secret_key_bytes``is not of length
``crypto_sign_SECRETKEYBYTES``
:param secret_key_bytes: bytes
:rtype: bytes
"""
if len(secret_key_bytes) != crypto_sign_SECRETKEYBYTES:
raise exc.ValueError("Invalid secret key")

return secret_key_bytes[crypto_sign_SEEDBYTES:]


def crypto_sign_ed25519_sk_to_seed(secret_key_bytes):
"""
Extract the seed from a secret Ed25519 key (encoded
as bytes ``secret_key_bytes``).
Raises a ValueError if ``secret_key_bytes``is not of length
``crypto_sign_SECRETKEYBYTES``
:param secret_key_bytes: bytes
:rtype: bytes
"""
if len(secret_key_bytes) != crypto_sign_SECRETKEYBYTES:
raise exc.ValueError("Invalid secret key")

return secret_key_bytes[:crypto_sign_SEEDBYTES]


class crypto_sign_ed25519ph_state(object):
"""
State object wrapping the sha-512 state used in ed25519ph computation
Expand Down
8 changes: 8 additions & 0 deletions tests/test_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ def test_sign_test_key_conversion():
b"42318124095afabe4d1451a559faedee")
ed25519_pk, ed25519_sk = c.crypto_sign_seed_keypair(keypair_seed)

assert c.crypto_sign_ed25519_sk_to_pk(ed25519_sk) == ed25519_pk
with pytest.raises(ValueError):
c.crypto_sign_ed25519_sk_to_pk(unhexlify(b"12"))

assert c.crypto_sign_ed25519_sk_to_seed(ed25519_sk) == keypair_seed
with pytest.raises(ValueError):
c.crypto_sign_ed25519_sk_to_seed(unhexlify(b"12"))

curve25519_pk = c.crypto_sign_ed25519_pk_to_curve25519(ed25519_pk)

with pytest.raises(ValueError):
Expand Down

0 comments on commit 13fb94e

Please sign in to comment.