Skip to content

Commit dd209c7

Browse files
authored
custom config added as an optional argument (#385)
1 parent 6e8de54 commit dd209c7

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

ecies/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from coincurve import PrivateKey, PublicKey
44

5-
from .config import ECIES_CONFIG
5+
from .config import ECIES_CONFIG, Config
66
from .utils import (
77
decapsulate,
88
encapsulate,
@@ -16,7 +16,7 @@
1616
__all__ = ["encrypt", "decrypt", "ECIES_CONFIG"]
1717

1818

19-
def encrypt(receiver_pk: Union[str, bytes], msg: bytes) -> bytes:
19+
def encrypt(receiver_pk: Union[str, bytes], msg: bytes, config: Config = ECIES_CONFIG) -> bytes:
2020
"""
2121
Encrypt with receiver's secp256k1 public key
2222
@@ -41,15 +41,15 @@ def encrypt(receiver_pk: Union[str, bytes], msg: bytes) -> bytes:
4141

4242
ephemeral_sk = generate_key()
4343
ephemeral_pk = ephemeral_sk.public_key.format(
44-
ECIES_CONFIG.is_ephemeral_key_compressed
44+
config.is_ephemeral_key_compressed
4545
)
4646

47-
sym_key = encapsulate(ephemeral_sk, pk)
48-
encrypted = sym_encrypt(sym_key, msg)
47+
sym_key = encapsulate(ephemeral_sk, pk, config)
48+
encrypted = sym_encrypt(sym_key, msg, config)
4949
return ephemeral_pk + encrypted
5050

5151

52-
def decrypt(receiver_sk: Union[str, bytes], msg: bytes) -> bytes:
52+
def decrypt(receiver_sk: Union[str, bytes], msg: bytes, config: Config = ECIES_CONFIG) -> bytes:
5353
"""
5454
Decrypt with receiver's secp256k1 private key
5555
@@ -72,8 +72,8 @@ def decrypt(receiver_sk: Union[str, bytes], msg: bytes) -> bytes:
7272
else:
7373
raise TypeError("Invalid secret key type")
7474

75-
key_size = ECIES_CONFIG.ephemeral_key_size
75+
key_size = config.ephemeral_key_size
7676
ephemeral_pk, encrypted = PublicKey(msg[0:key_size]), msg[key_size:]
7777

78-
sym_key = decapsulate(ephemeral_pk, sk)
79-
return sym_decrypt(sym_key, encrypted)
78+
sym_key = decapsulate(ephemeral_pk, sk, config)
79+
return sym_decrypt(sym_key, encrypted, config)

ecies/utils/elliptic.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from coincurve.utils import get_valid_secret
33
from eth_keys import keys
44

5-
from ..config import ECIES_CONFIG
5+
from ..config import ECIES_CONFIG, Config
66
from .hex import decode_hex
77
from .symmetric import derive_key
88

@@ -95,17 +95,17 @@ def hex2sk(sk_hex: str) -> PrivateKey:
9595

9696

9797
# private below
98-
def encapsulate(private_key: PrivateKey, peer_public_key: PublicKey) -> bytes:
99-
is_compressed = ECIES_CONFIG.is_hkdf_key_compressed
98+
def encapsulate(private_key: PrivateKey, peer_public_key: PublicKey, config: Config = ECIES_CONFIG) -> bytes:
99+
is_compressed = config.is_hkdf_key_compressed
100100
shared_point = peer_public_key.multiply(private_key.secret)
101101
master = private_key.public_key.format(is_compressed) + shared_point.format(
102102
is_compressed
103103
)
104104
return derive_key(master)
105105

106106

107-
def decapsulate(public_key: PublicKey, peer_private_key: PrivateKey) -> bytes:
108-
is_compressed = ECIES_CONFIG.is_hkdf_key_compressed
107+
def decapsulate(public_key: PublicKey, peer_private_key: PrivateKey, config: Config = ECIES_CONFIG) -> bytes:
108+
is_compressed = config.is_hkdf_key_compressed
109109
shared_point = public_key.multiply(peer_private_key.secret)
110110
master = public_key.format(is_compressed) + shared_point.format(is_compressed)
111111
return derive_key(master)

ecies/utils/symmetric.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from Crypto.Hash import SHA256
55
from Crypto.Protocol.KDF import HKDF
66

7-
from ..config import ECIES_CONFIG
7+
from ..config import ECIES_CONFIG, Config
88

99
AES_CIPHER_MODE = AES.MODE_GCM
1010
AEAD_TAG_LENGTH = 16
1111
XCHACHA20_NONCE_LENGTH = 24
1212

1313

14-
def sym_encrypt(key: bytes, plain_text: bytes) -> bytes:
14+
def sym_encrypt(key: bytes, plain_text: bytes, config: Config = ECIES_CONFIG) -> bytes:
1515
"""
1616
Symmetric encryption. AES-256-GCM or XChaCha20-Poly1305.
1717
@@ -29,9 +29,9 @@ def sym_encrypt(key: bytes, plain_text: bytes) -> bytes:
2929
bytes
3030
nonce + tag(16 bytes) + encrypted data
3131
"""
32-
algorithm = ECIES_CONFIG.symmetric_algorithm
32+
algorithm = config.symmetric_algorithm
3333
if algorithm == "aes-256-gcm":
34-
nonce_length = ECIES_CONFIG.symmetric_nonce_length
34+
nonce_length = config.symmetric_nonce_length
3535
nonce = os.urandom(nonce_length)
3636
cipher = AES.new(key, AES_CIPHER_MODE, nonce)
3737
elif algorithm == "xchacha20":
@@ -48,7 +48,7 @@ def sym_encrypt(key: bytes, plain_text: bytes) -> bytes:
4848
return bytes(cipher_text)
4949

5050

51-
def sym_decrypt(key: bytes, cipher_text: bytes) -> bytes:
51+
def sym_decrypt(key: bytes, cipher_text: bytes, config: Config = ECIES_CONFIG) -> bytes:
5252
"""
5353
AES-GCM decryption. AES-256-GCM or XChaCha20-Poly1305.
5454
@@ -84,9 +84,9 @@ def sym_decrypt(key: bytes, cipher_text: bytes) -> bytes:
8484
# If it's 12 bytes, the nonce can be incremented by 1 for each encryption
8585
# If it's 16 bytes, the nonce will be used to hash, so it's meaningless to increment
8686

87-
algorithm = ECIES_CONFIG.symmetric_algorithm
87+
algorithm = config.symmetric_algorithm
8888
if algorithm == "aes-256-gcm":
89-
nonce_length = ECIES_CONFIG.symmetric_nonce_length
89+
nonce_length = config.symmetric_nonce_length
9090
nonce_tag_length = nonce_length + AEAD_TAG_LENGTH
9191
nonce = cipher_text[:nonce_length]
9292
tag = cipher_text[nonce_length:nonce_tag_length]

0 commit comments

Comments
 (0)