x448.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. from cryptography import utils
  6. from cryptography.hazmat.backends.openssl.utils import _evp_pkey_derive
  7. from cryptography.hazmat.primitives import serialization
  8. from cryptography.hazmat.primitives.asymmetric.x448 import (
  9. X448PrivateKey,
  10. X448PublicKey,
  11. )
  12. _X448_KEY_SIZE = 56
  13. @utils.register_interface(X448PublicKey)
  14. class _X448PublicKey(object):
  15. def __init__(self, backend, evp_pkey):
  16. self._backend = backend
  17. self._evp_pkey = evp_pkey
  18. def public_bytes(self, encoding, format):
  19. if (
  20. encoding is serialization.Encoding.Raw
  21. or format is serialization.PublicFormat.Raw
  22. ):
  23. if (
  24. encoding is not serialization.Encoding.Raw
  25. or format is not serialization.PublicFormat.Raw
  26. ):
  27. raise ValueError(
  28. "When using Raw both encoding and format must be Raw"
  29. )
  30. return self._raw_public_bytes()
  31. return self._backend._public_key_bytes(
  32. encoding, format, self, self._evp_pkey, None
  33. )
  34. def _raw_public_bytes(self):
  35. buf = self._backend._ffi.new("unsigned char []", _X448_KEY_SIZE)
  36. buflen = self._backend._ffi.new("size_t *", _X448_KEY_SIZE)
  37. res = self._backend._lib.EVP_PKEY_get_raw_public_key(
  38. self._evp_pkey, buf, buflen
  39. )
  40. self._backend.openssl_assert(res == 1)
  41. self._backend.openssl_assert(buflen[0] == _X448_KEY_SIZE)
  42. return self._backend._ffi.buffer(buf, _X448_KEY_SIZE)[:]
  43. @utils.register_interface(X448PrivateKey)
  44. class _X448PrivateKey(object):
  45. def __init__(self, backend, evp_pkey):
  46. self._backend = backend
  47. self._evp_pkey = evp_pkey
  48. def public_key(self):
  49. buf = self._backend._ffi.new("unsigned char []", _X448_KEY_SIZE)
  50. buflen = self._backend._ffi.new("size_t *", _X448_KEY_SIZE)
  51. res = self._backend._lib.EVP_PKEY_get_raw_public_key(
  52. self._evp_pkey, buf, buflen
  53. )
  54. self._backend.openssl_assert(res == 1)
  55. self._backend.openssl_assert(buflen[0] == _X448_KEY_SIZE)
  56. return self._backend.x448_load_public_bytes(buf)
  57. def exchange(self, peer_public_key):
  58. if not isinstance(peer_public_key, X448PublicKey):
  59. raise TypeError("peer_public_key must be X448PublicKey.")
  60. return _evp_pkey_derive(self._backend, self._evp_pkey, peer_public_key)
  61. def private_bytes(self, encoding, format, encryption_algorithm):
  62. if (
  63. encoding is serialization.Encoding.Raw
  64. or format is serialization.PublicFormat.Raw
  65. ):
  66. if (
  67. format is not serialization.PrivateFormat.Raw
  68. or encoding is not serialization.Encoding.Raw
  69. or not isinstance(
  70. encryption_algorithm, serialization.NoEncryption
  71. )
  72. ):
  73. raise ValueError(
  74. "When using Raw both encoding and format must be Raw "
  75. "and encryption_algorithm must be NoEncryption()"
  76. )
  77. return self._raw_private_bytes()
  78. return self._backend._private_key_bytes(
  79. encoding, format, encryption_algorithm, self, self._evp_pkey, None
  80. )
  81. def _raw_private_bytes(self):
  82. buf = self._backend._ffi.new("unsigned char []", _X448_KEY_SIZE)
  83. buflen = self._backend._ffi.new("size_t *", _X448_KEY_SIZE)
  84. res = self._backend._lib.EVP_PKEY_get_raw_private_key(
  85. self._evp_pkey, buf, buflen
  86. )
  87. self._backend.openssl_assert(res == 1)
  88. self._backend.openssl_assert(buflen[0] == _X448_KEY_SIZE)
  89. return self._backend._ffi.buffer(buf, _X448_KEY_SIZE)[:]