aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/ec/key_agreement.py
blob: c15c0b6908634ec1bcbf0be66781b2d92e58974e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Provides an implementation of ECDH (Elliptic Curve Diffie-Hellman) and XDH (X25519, X448)."""

import hashlib
from abc import abstractmethod, ABC
from typing import Optional, Any

from public import public

from pyecsca.ec.context import ResultAction
from pyecsca.ec.mod import Mod
from pyecsca.ec.model import MontgomeryModel
from pyecsca.ec.mult import ScalarMultiplier
from pyecsca.ec.params import DomainParameters, get_params
from pyecsca.ec.point import Point


@public
class ECDHAction(ResultAction):
    """ECDH key exchange."""

    params: DomainParameters
    hash_algo: Optional[Any]
    privkey: Mod
    pubkey: Point

    def __init__(
        self,
        params: DomainParameters,
        hash_algo: Optional[Any],
        privkey: Mod,
        pubkey: Point,
    ):
        super().__init__()
        self.params = params
        self.hash_algo = hash_algo
        self.privkey = privkey
        self.pubkey = pubkey

    def __repr__(self):
        return f"{self.__class__.__name__}({self.params}, {self.hash_algo}, {self.privkey}, {self.pubkey})"


@public
class XDHAction(ResultAction):
    """XDH key exchange."""

    params: DomainParameters
    privkey: int
    pubkey: Point

    def __init__(self, params: DomainParameters, privkey: int, pubkey: Point):
        super().__init__()
        self.params = params
        self.privkey = privkey
        self.pubkey = pubkey

    def __repr__(self):
        return (
            f"{self.__class__.__name__}({self.params}, {self.privkey}, {self.pubkey})"
        )


@public
class KeyAgreement(ABC):
    """An abstract EC-based key agreement."""

    @abstractmethod
    def perform_raw(self) -> Point:
        """
        Perform the scalar-multiplication of the key agreement.

        :return: The shared point.
        """
        ...

    @abstractmethod
    def perform(self) -> bytes:
        """
        Perform the key agreement operation.

        :return: The shared secret.
        """
        ...


@public
class XDH(KeyAgreement):
    def __init__(
        self,
        mult: ScalarMultiplier,
        params: DomainParameters,
        pubkey: Point,
        privkey: int,
        bits: int,
        bytes: int,
    ):
        if "scl" not in mult.formulas:
            raise ValueError("ScalarMultiplier needs to have the scaling formula.")
        if not isinstance(params.curve.model, MontgomeryModel):
            raise ValueError("Invalid curve model.")
        self.mult = mult
        self.params = params
        self.pubkey = pubkey
        self.privkey = privkey
        self.bits = bits
        self.bytes = bytes
        self.mult.init(self.params, self.pubkey)

    def clamp(self, scalar: int) -> int:
        return scalar

    def perform_raw(self) -> Point:
        clamped = self.clamp(self.privkey)
        return self.mult.multiply(clamped)

    def perform(self) -> bytes:
        with XDHAction(self.params, self.privkey, self.pubkey) as action:
            point = self.perform_raw()
            return action.exit(int(point.X).to_bytes(self.bytes, "little"))


@public
class X25519(XDH):
    """
    X25519 (or Curve25519) from [RFC7748]_.

    .. warning::
        You need to clear the top bit of the point coordinate (pubkey) before converting to a point.
    """

    def __init__(self, mult: ScalarMultiplier, pubkey: Point, privkey: int):
        curve25519 = get_params(
            "other", "Curve25519", pubkey.coordinate_model.name, infty=False
        )
        super().__init__(mult, curve25519, pubkey, privkey, 255, 32)

    def clamp(self, scalar: int) -> int:
        scalar &= ~7
        scalar &= ~(128 << 8 * 31)
        scalar |= 64 << 8 * 31
        return scalar


@public
class X448(XDH):
    """
    X448 (or Curve448) from [RFC7748]_.
    """

    def __init__(self, mult: ScalarMultiplier, pubkey: Point, privkey: int):
        curve448 = get_params(
            "other", "Curve448", pubkey.coordinate_model.name, infty=False
        )
        super().__init__(mult, curve448, pubkey, privkey, 448, 56)

    def clamp(self, scalar: int) -> int:
        scalar &= ~3
        scalar |= 128 << 8 * 55
        return scalar


@public
class ECDH(KeyAgreement):
    """EC based key agreement primitive (ECDH)."""

    mult: ScalarMultiplier
    params: DomainParameters
    pubkey: Point
    privkey: Mod
    hash_algo: Optional[Any]

    def __init__(
        self,
        mult: ScalarMultiplier,
        params: DomainParameters,
        pubkey: Point,
        privkey: Mod,
        hash_algo: Optional[Any] = None,
    ):
        self.mult = mult
        self.params = params
        self.pubkey = pubkey
        self.privkey = privkey
        self.hash_algo = hash_algo
        self.mult.init(self.params, self.pubkey)

    def perform_raw(self) -> Point:
        point = self.mult.multiply(int(self.privkey))
        return point.to_affine()

    def perform(self) -> bytes:
        with ECDHAction(
            self.params, self.hash_algo, self.privkey, self.pubkey
        ) as action:
            affine_point = self.perform_raw()
            x = int(affine_point.x)
            p = self.params.curve.prime
            n = (p.bit_length() + 7) // 8
            result = x.to_bytes(n, byteorder="big")
            if self.hash_algo is not None:
                result = self.hash_algo(result).digest()
            return action.exit(result)


@public
class ECDH_NONE(ECDH):
    """Raw x-coordinate ECDH."""

    def __init__(
        self,
        mult: ScalarMultiplier,
        params: DomainParameters,
        pubkey: Point,
        privkey: Mod,
    ):
        super().__init__(mult, params, pubkey, privkey)


@public
class ECDH_SHA1(ECDH):
    """ECDH with SHA1 of x-coordinate."""

    def __init__(
        self,
        mult: ScalarMultiplier,
        params: DomainParameters,
        pubkey: Point,
        privkey: Mod,
    ):
        super().__init__(mult, params, pubkey, privkey, hashlib.sha1)


@public
class ECDH_SHA224(ECDH):
    """ECDH with SHA224 of x-coordinate."""

    def __init__(
        self,
        mult: ScalarMultiplier,
        params: DomainParameters,
        pubkey: Point,
        privkey: Mod,
    ):
        super().__init__(mult, params, pubkey, privkey, hashlib.sha224)


@public
class ECDH_SHA256(ECDH):
    """ECDH with SHA256 of x-coordinate."""

    def __init__(
        self,
        mult: ScalarMultiplier,
        params: DomainParameters,
        pubkey: Point,
        privkey: Mod,
    ):
        super().__init__(mult, params, pubkey, privkey, hashlib.sha256)


@public
class ECDH_SHA384(ECDH):
    """ECDH with SHA384 of x-coordinate."""

    def __init__(
        self,
        mult: ScalarMultiplier,
        params: DomainParameters,
        pubkey: Point,
        privkey: Mod,
    ):
        super().__init__(mult, params, pubkey, privkey, hashlib.sha384)


@public
class ECDH_SHA512(ECDH):
    """ECDH with SHA512 of x-coordinate."""

    def __init__(
        self,
        mult: ScalarMultiplier,
        params: DomainParameters,
        pubkey: Point,
        privkey: Mod,
    ):
        super().__init__(mult, params, pubkey, privkey, hashlib.sha512)