11# This file is part of Scapy
22# Copyright (C) 2017 Maxence Tury
3+ # 2019 Romain Perez
34# This program is published under a GPLv2 license
45
56"""
2627 from cryptography .hazmat .primitives .asymmetric import dh , ec
2728if conf .crypto_valid_advanced :
2829 from cryptography .hazmat .primitives .asymmetric import x25519
30+ from cryptography .hazmat .primitives .asymmetric import x448
2931
3032
3133class KeyShareEntry (Packet ):
@@ -67,16 +69,19 @@ def create_privkey(self):
6769 pubkey = privkey .public_key ()
6870 self .key_exchange = pubkey .public_numbers ().y
6971 elif self .group in _tls_named_curves :
70- if _tls_named_curves [self .group ] == "x25519" :
72+ if _tls_named_curves [self .group ] in [ "x25519" , "x448" ] :
7173 if conf .crypto_valid_advanced :
72- privkey = x25519 .X25519PrivateKey .generate ()
74+ if _tls_named_curves [self .group ] == "x25519" :
75+ privkey = x25519 .X25519PrivateKey .generate ()
76+ else :
77+ privkey = x448 .X448PrivateKey .generate ()
7378 self .privkey = privkey
7479 pubkey = privkey .public_key ()
7580 self .key_exchange = pubkey .public_bytes (
7681 serialization .Encoding .Raw ,
7782 serialization .PublicFormat .Raw
7883 )
79- elif _tls_named_curves [ self . group ] != "x448" :
84+ else :
8085 curve = ec ._CURVE_TYPES [_tls_named_curves [self .group ]]()
8186 privkey = ec .generate_private_key (curve , default_backend ())
8287 self .privkey = privkey
@@ -116,11 +121,14 @@ def register_pubkey(self):
116121 public_numbers = dh .DHPublicNumbers (self .key_exchange , pn )
117122 self .pubkey = public_numbers .public_key (default_backend ())
118123 elif self .group in _tls_named_curves :
119- if _tls_named_curves [self .group ] == "x25519" :
124+ if _tls_named_curves [self .group ] in [ "x25519" , "x448" ] :
120125 if conf .crypto_valid_advanced :
121- import_point = x25519 .X25519PublicKey .from_public_bytes
126+ if _tls_named_curves [self .group ] == "x25519" :
127+ import_point = x25519 .X25519PublicKey .from_public_bytes
128+ else :
129+ import_point = x448 .X448PublicKey .from_public_bytes
122130 self .pubkey = import_point (self .key_exchange )
123- elif _tls_named_curves [ self . group ] != "x448" :
131+ else :
124132 curve = ec ._CURVE_TYPES [_tls_named_curves [self .group ]]()
125133 try : # cryptography >= 2.5
126134 import_point = ec .EllipticCurvePublicKey .from_encoded_point # noqa: E501
@@ -203,7 +211,7 @@ def post_build(self, pkt, pay):
203211 if group_name in six .itervalues (_tls_named_ffdh_groups ):
204212 pms = privkey .exchange (pubkey )
205213 elif group_name in six .itervalues (_tls_named_curves ):
206- if group_name == "x25519" :
214+ if group_name in [ "x25519" , "x448" ] :
207215 pms = privkey .exchange (pubkey )
208216 else :
209217 pms = privkey .exchange (ec .ECDH (), pubkey )
@@ -226,7 +234,7 @@ def post_dissection(self, r):
226234 if group_name in six .itervalues (_tls_named_ffdh_groups ):
227235 pms = privkey .exchange (pubkey )
228236 elif group_name in six .itervalues (_tls_named_curves ):
229- if group_name == "x25519" :
237+ if group_name in [ "x25519" , "x448" ] :
230238 pms = privkey .exchange (pubkey )
231239 else :
232240 pms = privkey .exchange (ec .ECDH (), pubkey )
@@ -237,7 +245,7 @@ def post_dissection(self, r):
237245 if group_name in six .itervalues (_tls_named_ffdh_groups ):
238246 pms = privkey .exchange (pubkey )
239247 elif group_name in six .itervalues (_tls_named_curves ):
240- if group_name == "x25519" :
248+ if group_name in [ "x25519" , "x448" ] :
241249 pms = privkey .exchange (pubkey )
242250 else :
243251 pms = privkey .exchange (ec .ECDH (), pubkey )
0 commit comments