diff options
Diffstat (limited to 'pyecsca/ec/key_agreement.py')
| -rw-r--r-- | pyecsca/ec/key_agreement.py | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/pyecsca/ec/key_agreement.py b/pyecsca/ec/key_agreement.py index 018b177..4b9a99e 100644 --- a/pyecsca/ec/key_agreement.py +++ b/pyecsca/ec/key_agreement.py @@ -3,6 +3,7 @@ from typing import Optional, Any from public import public +from .group import AbelianGroup from .mult import ScalarMultiplier from .point import Point @@ -11,16 +12,19 @@ from .point import Point class KeyAgreement(object): """An EC based key agreement primitive. (ECDH)""" mult: ScalarMultiplier + group: AbelianGroup pubkey: Point privkey: int hash_algo: Optional[Any] - def __init__(self, mult: ScalarMultiplier, pubkey: Point, privkey: int, + def __init__(self, mult: ScalarMultiplier, group: AbelianGroup, pubkey: Point, privkey: int, hash_algo: Optional[Any] = None): self.mult = mult + self.group = group self.pubkey = pubkey self.privkey = privkey self.hash_algo = hash_algo + self.mult.init(self.group, self.pubkey) def perform_raw(self) -> Point: """ @@ -28,7 +32,7 @@ class KeyAgreement(object): :return: The shared point. """ - point = self.mult.multiply(self.privkey, self.pubkey) + point = self.mult.multiply(self.privkey) return point.to_affine() # TODO: This conversion should be somehow added to the context def perform(self) -> bytes: @@ -39,7 +43,7 @@ class KeyAgreement(object): """ affine_point = self.perform_raw() x = int(affine_point.x) - p = self.mult.group.curve.prime + p = self.group.curve.prime n = (p.bit_length() + 7) // 8 result = x.to_bytes(n, byteorder="big") if self.hash_algo is not None: @@ -51,45 +55,45 @@ class KeyAgreement(object): class ECDH_NONE(KeyAgreement): """Raw x-coordinate ECDH.""" - def __init__(self, mult: ScalarMultiplier, pubkey: Point, privkey: int): - super().__init__(mult, pubkey, privkey) + def __init__(self, mult: ScalarMultiplier, group: AbelianGroup, pubkey: Point, privkey: int): + super().__init__(mult, group, pubkey, privkey) @public class ECDH_SHA1(KeyAgreement): """ECDH with SHA1 of x-coordinate.""" - def __init__(self, mult: ScalarMultiplier, pubkey: Point, privkey: int): - super().__init__(mult, pubkey, privkey, hashlib.sha1) + def __init__(self, mult: ScalarMultiplier, group: AbelianGroup, pubkey: Point, privkey: int): + super().__init__(mult, group, pubkey, privkey, hashlib.sha1) @public class ECDH_SHA224(KeyAgreement): """ECDH with SHA224 of x-coordinate.""" - def __init__(self, mult: ScalarMultiplier, pubkey: Point, privkey: int): - super().__init__(mult, pubkey, privkey, hashlib.sha224) + def __init__(self, mult: ScalarMultiplier, group: AbelianGroup, pubkey: Point, privkey: int): + super().__init__(mult, group, pubkey, privkey, hashlib.sha224) @public class ECDH_SHA256(KeyAgreement): """ECDH with SHA256 of x-coordinate.""" - def __init__(self, mult: ScalarMultiplier, pubkey: Point, privkey: int): - super().__init__(mult, pubkey, privkey, hashlib.sha256) + def __init__(self, mult: ScalarMultiplier, group: AbelianGroup, pubkey: Point, privkey: int): + super().__init__(mult, group, pubkey, privkey, hashlib.sha256) @public class ECDH_SHA384(KeyAgreement): """ECDH with SHA384 of x-coordinate.""" - def __init__(self, mult: ScalarMultiplier, pubkey: Point, privkey: int): - super().__init__(mult, pubkey, privkey, hashlib.sha384) + def __init__(self, mult: ScalarMultiplier, group: AbelianGroup, pubkey: Point, privkey: int): + super().__init__(mult, group, pubkey, privkey, hashlib.sha384) @public class ECDH_SHA512(KeyAgreement): """ECDH with SHA512 of x-coordinate.""" - def __init__(self, mult: ScalarMultiplier, pubkey: Point, privkey: int): - super().__init__(mult, pubkey, privkey, hashlib.sha512) + def __init__(self, mult: ScalarMultiplier, group: AbelianGroup, pubkey: Point, privkey: int): + super().__init__(mult, group, pubkey, privkey, hashlib.sha512) |
