Skip to content

Commit 6c2a6d7

Browse files
committed
More work
1 parent cf94319 commit 6c2a6d7

File tree

5 files changed

+54
-46
lines changed

5 files changed

+54
-46
lines changed

src/josepy/jwa.py

+14-21
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
"""
66
import abc
77
import logging
8-
from typing import Dict, Type, Union
8+
from typing import Dict, Type
99

1010
import cryptography.exceptions
1111
from cryptography.hazmat.backends import default_backend
1212
from cryptography.hazmat.primitives import hashes
1313
from cryptography.hazmat.primitives import hmac
14-
from cryptography.hazmat.primitives.asymmetric import ec, x25519, x448
15-
from cryptography.hazmat.primitives.asymmetric import ed25519
16-
from cryptography.hazmat.primitives.asymmetric import ed448
14+
from cryptography.hazmat.primitives.asymmetric import ec
1715
from cryptography.hazmat.primitives.asymmetric import padding
1816
from cryptography.hazmat.primitives.asymmetric.utils import decode_dss_signature
1917
from cryptography.hazmat.primitives.asymmetric.utils import encode_dss_signature
@@ -233,20 +231,15 @@ def __init__(self, name, hash_):
233231
super().__init__(name)
234232
self.hash = hash_()
235233

236-
def sign(self, key: Union[
237-
ed25519.Ed25519PrivateKey,
238-
ed448.Ed448PrivateKey,
239-
x25519.X25519PrivateKey,
240-
x448.X448PrivateKey,
241-
], msg: bytes):
234+
@classmethod
235+
def register(cls, signature_cls):
236+
# might need to overwrite this, so I can get the argument in
237+
return super().register(signature_cls)
238+
239+
def sign(self, key, msg: bytes):
242240
return key.sign(msg)
243241

244-
def verify(self, key: Union[
245-
ed25519.Ed25519PublicKey,
246-
ed448.Ed448PublicKey,
247-
x25519.X25519PrivateKey,
248-
x448.X448PrivateKey,
249-
], msg: bytes, sig: bytes):
242+
def verify(self, key, msg: bytes, sig: bytes):
250243
try:
251244
key.verify(signature=sig, data=msg)
252245
except cryptography.exceptions.InvalidSignature as error:
@@ -287,8 +280,8 @@ def verify(self, key: Union[
287280
#: Ed25519 uses SHA512
288281
ES25519 = JWASignature.register(_JWAOKP('ES25519', hashes.SHA512))
289282
#: Ed448 uses SHA3/SHAKE256
290-
ES448 = JWASignature.register(_JWAOKP('ES448', hashes.SHAKE256))
291-
#: X25519 uses
292-
X22519 = JWASignature.register(_JWAOKP('X22519', hashes.SHAKE256))
293-
#: X448 uses
294-
X448 = JWASignature.register(_JWAOKP('X448', hashes.SHAKE256))
283+
# ES448 = JWASignature.register(_JWAOKP('ES448', hashes.SHAKE256))
284+
# #: X25519 uses SHA3/SHAKE256
285+
# X22519 = JWASignature.register(_JWAOKP('X22519', hashes.SHAKE256))
286+
# #: X448 uses SHA3/SHAKE256
287+
# X448 = JWASignature.register(_JWAOKP('X448', hashes.SHAKE256))

src/josepy/jwa_test.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from unittest import mock
44

55
from josepy import errors, test_util
6-
from josepy.jwa import ES25519
76

87
RSA256_KEY = test_util.load_rsa_private_key('rsa256_key.pem')
98
RSA512_KEY = test_util.load_rsa_private_key('rsa512_key.pem')
@@ -231,18 +230,18 @@ def test_signature_size(self):
231230
self.assertEqual(len(sig), 2 * 66)
232231

233232

234-
class JWAOKPTests(JWASignatureTest):
235-
# look up the signature sizes in the RFC
236-
237-
def test_sign_no_private_part(self):
238-
from josepy.jwa import ES25519
239-
self.assertRaises(errors.Error, ES25519.sign, OKP_ED25519_KEY, b'foo')
240-
241-
def test_can_size_ed25519(self):
242-
ES25519.sign(b'foo'), OKP_ED25519_KEY,
243-
244-
def test_signature_size(self):
245-
pass
233+
# class JWAOKPTests(JWASignatureTest):
234+
# # look up the signature sizes in the RFC
235+
#
236+
# def test_sign_no_private_part(self):
237+
# from josepy.jwa import ES25519
238+
# self.assertRaises(errors.Error, ES25519.sign, OKP_ED25519_KEY, b'foo')
239+
#
240+
# # def test_can_size_ed25519(self):
241+
# # ES25519.sign(b'foo'), OKP_ED25519_KEY,
242+
#
243+
# def test_signature_size(self):
244+
# pass
246245

247246

248247
if __name__ == '__main__':

src/josepy/jwk.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import math
66

7-
from typing import Dict, Optional, Sequence, Tuple, Type, Union
7+
from typing import Dict, Optional, Sequence, Type, Union
88

99
import cryptography.exceptions
1010
from cryptography.hazmat.backends import default_backend
@@ -402,7 +402,7 @@ class JWKOKP(JWK):
402402
x448.X448PrivateKey, x448.X448PublicKey,
403403
)
404404
required = ('crv', JWK.type_field_name, 'x')
405-
crv_to_pub_priv: Dict[str, Tuple] = {
405+
crv_to_pub_priv = {
406406
"Ed25519": (ed25519.Ed25519PublicKey, ed25519.Ed25519PrivateKey),
407407
"Ed448": (ed448.Ed448PublicKey, ed448.Ed448PrivateKey),
408408
"X25519": (x25519.X25519PublicKey, x25519.X25519PrivateKey),
@@ -442,8 +442,8 @@ def fields_to_partial_json(self) -> Dict:
442442
)
443443
else:
444444
params['x'] = json_util.encode_b64jose(self.key.public_bytes(
445-
serialization.Encoding.Raw,
446-
serialization.PublicFormat.Raw,
445+
encoding=serialization.Encoding.Raw,
446+
format=serialization.PublicFormat.Raw,
447447
))
448448
params['crv'] = self._key_to_crv()
449449
return params
@@ -473,21 +473,21 @@ def fields_from_json(cls, jobj):
473473

474474
try:
475475
if "d" not in obj: # public key
476-
pub_class: Union[
476+
pub_class: Type[Union[
477477
ed25519.Ed25519PublicKey,
478478
ed448.Ed448PublicKey,
479479
x25519.X25519PublicKey,
480480
x448.X448PublicKey,
481-
] = cls.crv_to_pub_priv[curve][0]
481+
]] = cls.crv_to_pub_priv[curve][0]
482482
return cls(key=pub_class.from_public_bytes(x))
483483
else: # private key
484484
d = json_util.decode_b64jose(obj.get("d"))
485-
priv_key_class: Union[
485+
priv_key_class: Type[Union[
486486
ed25519.Ed25519PrivateKey,
487487
ed448.Ed448PrivateKey,
488488
x25519.X25519PrivateKey,
489489
x448.X448PrivateKey,
490-
] = cls.crv_to_pub_priv[curve][1]
490+
]] = cls.crv_to_pub_priv[curve][1]
491491
return cls(key=priv_key_class.from_private_bytes(d))
492492
except ValueError as err:
493493
raise errors.DeserializationError("Invalid key parameter") from err

src/josepy/jwk_test.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,14 @@ def test_encode_ed25519(self):
387387
x = josepy.json_util.encode_b64jose(data['x'])
388388
self.assertEqual(x, "9ujoz88QZL05w2lhaqUbBaBpwmM12Y7Y8Ybfwjibk-I")
389389

390-
def test_from_json_ed25519(self):
390+
def test_from_json(self):
391391
from josepy.jwk import JWK
392392
key = JWK.from_json(self.jwked25519json)
393393
with self.subTest(key=[
394-
self.jwked448json, self.jwked25519json,
395-
self.jwkx25519json, self.jwkx448json,
394+
self.jwked448json,
395+
self.jwked25519json,
396+
self.jwkx25519json,
397+
self.jwkx448json,
396398
]):
397399
self.assertIsInstance(key.key, util.ComparableOKPKey)
398400

@@ -419,6 +421,10 @@ def test_unknown_crv_name(self):
419421
}
420422
)
421423

424+
def test_from_json_hashable(self):
425+
from josepy.jwk import JWK
426+
hash(JWK.from_json(self.jwked25519json))
427+
422428

423429
if __name__ == '__main__':
424430
unittest.main() # pragma: no cover

src/josepy/util.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import OpenSSL
55
from cryptography.hazmat.backends import default_backend
6+
from cryptography.hazmat.primitives import serialization
67
from cryptography.hazmat.primitives.asymmetric import (
78
ec,
89
ed25519, ed448,
@@ -178,7 +179,16 @@ class ComparableOKPKey(ComparableKey):
178179
"""
179180

180181
def __hash__(self):
181-
return hash((self.__class__, self._wrapped.curve.name, self._wrapped.x))
182+
if self.is_private():
183+
priv = self._wrapped.private_bytes(
184+
encoding=serialization.Encoding.PEM,
185+
format=serialization.PrivateFormat.PKCS8,
186+
)
187+
pub = priv.public_key
188+
return hash((self.__class__, pub.curve.name, priv))
189+
else:
190+
pub = self._wrapped.public_key()
191+
return hash((self.__class__, pub.curve.name, pub))
182192

183193
def is_private(self) -> bool:
184194
return isinstance(

0 commit comments

Comments
 (0)