|
| 1 | +""" _curves.py |
| 2 | +specify some additional curves that OpenSSL provides but cryptography doesn't explicitly expose |
| 3 | +""" |
| 4 | + |
| 5 | +from cryptography import utils |
| 6 | + |
| 7 | +from cryptography.hazmat.primitives.asymmetric import ec |
| 8 | + |
| 9 | +from cryptography.hazmat.bindings.openssl.binding import Binding |
| 10 | + |
| 11 | +__all__ = tuple() |
| 12 | + |
| 13 | +# TODO: investigate defining additional curves using EC_GROUP_new_curve |
| 14 | +# https://wiki.openssl.org/index.php/Elliptic_Curve_Cryptography#Defining_Curves |
| 15 | + |
| 16 | + |
| 17 | +def _openssl_get_supported_curves(): |
| 18 | + if hasattr(_openssl_get_supported_curves, '_curves'): |
| 19 | + return _openssl_get_supported_curves._curves |
| 20 | + |
| 21 | + # use cryptography's cffi bindings to get an array of curve names |
| 22 | + b = Binding() |
| 23 | + cn = b.lib.EC_get_builtin_curves(b.ffi.NULL, 0) |
| 24 | + cs = b.ffi.new('EC_builtin_curve[]', cn) |
| 25 | + b.lib.EC_get_builtin_curves(cs, cn) |
| 26 | + |
| 27 | + # store the result so we don't have to do all of this every time |
| 28 | + curves = { b.ffi.string(b.lib.OBJ_nid2sn(c.nid)).decode('utf-8') for c in cs } |
| 29 | + # Ed25519 and X25519 are always present in cryptography>=2.6 |
| 30 | + # The python cryptography lib provides a different interface for these curves, |
| 31 | + # so they are handled differently in the ECDHPriv/Pub and EdDSAPriv/Pub classes |
| 32 | + curves |= {'X25519', 'ed25519'} |
| 33 | + _openssl_get_supported_curves._curves = curves |
| 34 | + return curves |
| 35 | + |
| 36 | + |
| 37 | +def use_legacy_cryptography_decorator(): |
| 38 | + """ |
| 39 | + The decorator utils.register_interface was removed in version 38.0.0. Keep using it |
| 40 | + if the decorator exists, inherit from `ec.EllipticCurve` otherwise. |
| 41 | + """ |
| 42 | + return hasattr(utils, "register_interface") and callable(utils.register_interface) |
| 43 | + |
| 44 | + |
| 45 | +if use_legacy_cryptography_decorator(): |
| 46 | + @utils.register_interface(ec.EllipticCurve) |
| 47 | + class BrainpoolP256R1(object): |
| 48 | + name = 'brainpoolP256r1' |
| 49 | + key_size = 256 |
| 50 | + |
| 51 | + |
| 52 | + @utils.register_interface(ec.EllipticCurve) # noqa: E303 |
| 53 | + class BrainpoolP384R1(object): |
| 54 | + name = 'brainpoolP384r1' |
| 55 | + key_size = 384 |
| 56 | + |
| 57 | + |
| 58 | + @utils.register_interface(ec.EllipticCurve) # noqa: E303 |
| 59 | + class BrainpoolP512R1(object): |
| 60 | + name = 'brainpoolP512r1' |
| 61 | + key_size = 512 |
| 62 | + |
| 63 | + |
| 64 | + @utils.register_interface(ec.EllipticCurve) # noqa: E303 |
| 65 | + class X25519(object): |
| 66 | + name = 'X25519' |
| 67 | + key_size = 256 |
| 68 | + |
| 69 | + |
| 70 | + @utils.register_interface(ec.EllipticCurve) # noqa: E303 |
| 71 | + class Ed25519(object): |
| 72 | + name = 'ed25519' |
| 73 | + key_size = 256 |
| 74 | +else: |
| 75 | + class BrainpoolP256R1(ec.EllipticCurve): |
| 76 | + name = 'brainpoolP256r1' |
| 77 | + key_size = 256 |
| 78 | + |
| 79 | + |
| 80 | + class BrainpoolP384R1(ec.EllipticCurve): # noqa: E303 |
| 81 | + name = 'brainpoolP384r1' |
| 82 | + key_size = 384 |
| 83 | + |
| 84 | + |
| 85 | + class BrainpoolP512R1(ec.EllipticCurve): # noqa: E303 |
| 86 | + name = 'brainpoolP512r1' |
| 87 | + key_size = 512 |
| 88 | + |
| 89 | + |
| 90 | + class X25519(ec.EllipticCurve): # noqa: E303 |
| 91 | + name = 'X25519' |
| 92 | + key_size = 256 |
| 93 | + |
| 94 | + |
| 95 | + class Ed25519(ec.EllipticCurve): # noqa: E303 |
| 96 | + name = 'ed25519' |
| 97 | + key_size = 256 |
| 98 | + |
| 99 | + |
| 100 | +# add these curves to the _CURVE_TYPES list |
| 101 | +for curve in [BrainpoolP256R1, BrainpoolP384R1, BrainpoolP512R1, X25519, Ed25519]: |
| 102 | + if curve.name not in ec._CURVE_TYPES and curve.name in _openssl_get_supported_curves(): |
| 103 | + ec._CURVE_TYPES[curve.name] = curve |
0 commit comments