Skip to content

Commit

Permalink
Replace dem.ErrorInvalidTag and Capsule.NotValid with `GenericErr…
Browse files Browse the repository at this point in the history
…or`.
  • Loading branch information
fjarri committed Mar 27, 2021
1 parent d799179 commit f349de5
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 25 deletions.
5 changes: 2 additions & 3 deletions docs/examples/umbral_simple_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import random
from umbral import (
SecretKey, PublicKey,
SecretKey, PublicKey, GenericError,
encrypt, generate_kfrags, reencrypt, decrypt_original, decrypt_reencrypted)
from umbral.dem import ErrorInvalidTag

# Generate an Umbral key pair
# ---------------------------
Expand Down Expand Up @@ -46,7 +45,7 @@
# Attempt Bob's decryption (fail)
try:
fail_decrypted_data = decrypt_original(bobs_secret_key, bob_capsule, ciphertext)
except ErrorInvalidTag:
except GenericError:
print("Decryption failed! Bob doesn't has access granted yet.")

# Alice grants access to Bob by generating kfrags
Expand Down
4 changes: 2 additions & 2 deletions docs/notebooks/pyUmbral Simple API.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@
}
],
"source": [
"from umbral.dem import ErrorInvalidTag\n",
"from umbral import GenericError\n",
"\n",
"try:\n",
" fail_decrypted_data = decrypt_original(sk=bobs_private_key,\n",
" capsule=capsule,\n",
" ciphertext=ciphertext)\n",
"except ErrorInvalidTag:\n",
"except GenericError:\n",
" print(\"Decryption failed! Bob doesn't has access granted yet.\")\n"
]
},
Expand Down
3 changes: 3 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Encryption, re-encryption and decryption
Utilities
---------

.. autoclass:: umbral.GenericError
:show-inheritance:

.. autoclass:: umbral.serializable.Serializable
:members: from_bytes
:special-members: __bytes__
2 changes: 1 addition & 1 deletion docs/source/using_pyumbral.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ or re-encrypted for him by Ursula, he will not be able to open it.
... ciphertext=ciphertext)
Traceback (most recent call last):
...
umbral.dem.ErrorInvalidTag
umbral.GenericError


Ursulas perform re-encryption
Expand Down
5 changes: 3 additions & 2 deletions tests/test_capsule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Capsule,
SecretKey,
PublicKey,
GenericError,
encrypt,
decrypt_original,
reencrypt,
Expand All @@ -27,7 +28,7 @@ def test_capsule_serialization(alices_keys):
capsule.point_e = CurvePoint.random()
capsule_bytes = bytes(capsule)

with pytest.raises(Capsule.NotValid):
with pytest.raises(GenericError):
Capsule.from_bytes(capsule_bytes)


Expand Down Expand Up @@ -84,7 +85,7 @@ def test_open_reencrypted(alices_keys, bobs_keys):
capsule.open_reencrypted(receiving_sk, delegating_pk, [])

# Not enough cfrags
with pytest.raises(ValueError, match="Internal validation failed"):
with pytest.raises(GenericError, match="Internal validation failed"):
capsule.open_reencrypted(receiving_sk, delegating_pk, cfrags[:threshold-1])

# Repeating cfrags
Expand Down
7 changes: 4 additions & 3 deletions tests/test_dem.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
import os

from umbral.dem import DEM, ErrorInvalidTag
from umbral import GenericError
from umbral.dem import DEM


def test_encrypt_decrypt():
Expand Down Expand Up @@ -47,7 +48,7 @@ def test_malformed_ciphertext():
dem.decrypt(ciphertext[:DEM.NONCE_SIZE + DEM.TAG_SIZE - 1])

# Too long
with pytest.raises(ErrorInvalidTag):
with pytest.raises(GenericError):
dem.decrypt(ciphertext + b'abcd')


Expand Down Expand Up @@ -76,5 +77,5 @@ def test_encrypt_decrypt_associated_data():
assert cleartext1 == plaintext

# Attempt decryption with invalid associated data
with pytest.raises(ErrorInvalidTag):
with pytest.raises(GenericError):
cleartext2 = dem.decrypt(ciphertext0, authenticated_data=b'wrong data')
4 changes: 2 additions & 2 deletions tests/test_pre.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from umbral import (
SecretKey,
PublicKey,
GenericError,
encrypt,
generate_kfrags,
decrypt_original,
reencrypt,
decrypt_reencrypted,
)
from umbral.dem import ErrorInvalidTag


def test_public_key_encryption(alices_keys):
Expand All @@ -22,7 +22,7 @@ def test_public_key_encryption(alices_keys):

# Wrong secret key
sk = SecretKey.random()
with pytest.raises(ErrorInvalidTag):
with pytest.raises(GenericError):
decrypt_original(sk, capsule, ciphertext)


Expand Down
2 changes: 2 additions & 0 deletions umbral/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .capsule import Capsule
from .capsule_frag import CapsuleFrag
from .errors import GenericError
from .key_frag import KeyFrag, generate_kfrags
from .keys import SecretKey, PublicKey, SecretKeyFactory
from .pre import encrypt, decrypt_original, decrypt_reencrypted, reencrypt
Expand All @@ -23,6 +24,7 @@
"Capsule",
"KeyFrag",
"CapsuleFrag",
"GenericError",
"encrypt",
"decrypt_original",
"generate_kfrags",
Expand Down
10 changes: 3 additions & 7 deletions umbral/capsule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .curve_point import CurvePoint
from .curve_scalar import CurveScalar
from .errors import GenericError
from .hashing import hash_capsule_points, hash_to_polynomial_arg, hash_to_shared_secret
from .keys import PublicKey, SecretKey
from .params import PARAMETERS
Expand All @@ -24,11 +25,6 @@ class Capsule(Serializable):
Encapsulated symmetric key.
"""

class NotValid(ValueError):
"""
raised if the capsule does not pass verification.
"""

def __init__(self, point_e: CurvePoint, point_v: CurvePoint, signature: CurveScalar):
self.point_e = point_e
self.point_v = point_v
Expand All @@ -40,7 +36,7 @@ def __take__(cls, data: bytes) -> Tuple['Capsule', bytes]:

capsule = cls(e, v, sig)
if not capsule._verify():
raise cls.NotValid("Capsule verification failed.")
raise GenericError("Capsule self-verification failed. Serialized data may be damaged.")

return capsule, data

Expand Down Expand Up @@ -111,7 +107,7 @@ def open_reencrypted(self,
# TODO: check for d == 0? Or just let if fail?
inv_d = d.invert()
if orig_pub_key * (s * inv_d) != (e_prime * h) + v_prime:
raise ValueError("Internal validation failed")
raise GenericError("Internal validation failed")

return (e_prime + v_prime) * d

Expand Down
9 changes: 4 additions & 5 deletions umbral/dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)

from . import openssl
from .errors import GenericError


def kdf(data: bytes,
Expand All @@ -30,10 +31,6 @@ def kdf(data: bytes,
return hkdf.derive(data)


class ErrorInvalidTag(Exception):
pass


class DEM:

KEY_SIZE = XCHACHA_KEY_SIZE
Expand Down Expand Up @@ -67,4 +64,6 @@ def decrypt(self, nonce_and_ciphertext: bytes, authenticated_data: bytes = b"")
try:
return xchacha_decrypt(ciphertext, authenticated_data, nonce, self._key)
except nacl.exceptions.CryptoError:
raise ErrorInvalidTag
raise GenericError("Decryption of ciphertext failed: "
"either someone tampered with the ciphertext or "
"you are using an incorrect decryption key.")
5 changes: 5 additions & 0 deletions umbral/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class GenericError(Exception):
"""
An interal Umbral error, see the message for details.
"""
pass

0 comments on commit f349de5

Please sign in to comment.