x448.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 cryptography.hazmat.backends.openssl.utils import _evp_pkey_derive
  5. from cryptography.hazmat.primitives import serialization
  6. from cryptography.hazmat.primitives.asymmetric.x448 import (
  7. X448PrivateKey,
  8. X448PublicKey,
  9. )
  10. _X448_KEY_SIZE = 56
  11. class _X448PublicKey(X448PublicKey):
  12. def __init__(self, backend, evp_pkey):
  13. self._backend = backend
  14. self._evp_pkey = evp_pkey
  15. def public_bytes(
  16. self,
  17. encoding: serialization.Encoding,
  18. format: serialization.PublicFormat,
  19. ) -> bytes:
  20. if (
  21. encoding is serialization.Encoding.Raw
  22. or format is serialization.PublicFormat.Raw
  23. ):
  24. if (
  25. encoding is not serialization.Encoding.Raw
  26. or format is not serialization.PublicFormat.Raw
  27. ):
  28. raise ValueError(
  29. "When using Raw both encoding and format must be Raw"
  30. )
  31. return self._raw_public_bytes()
  32. return self._backend._public_key_bytes(
  33. encoding, format, self, self._evp_pkey, None
  34. )
  35. def _raw_public_bytes(self) -> bytes:
  36. buf = self._backend._ffi.new("unsigned char []", _X448_KEY_SIZE)
  37. buflen = self._backend._ffi.new("size_t *", _X448_KEY_SIZE)
  38. res = self._backend._lib.EVP_PKEY_get_raw_public_key(
  39. self._evp_pkey, buf, buflen
  40. )
  41. self._backend.openssl_assert(res == 1)
  42. self._backend.openssl_assert(buflen[0] == _X448_KEY_SIZE)
  43. return self._backend._ffi.buffer(buf, _X448_KEY_SIZE)[:]
  44. class _X448PrivateKey(X448PrivateKey):
  45. def __init__(self, backend, evp_pkey):
  46. self._backend = backend
  47. self._evp_pkey = evp_pkey
  48. def public_key(self) -> X448PublicKey:
  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: X448PublicKey) -> bytes:
  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(
  62. self,
  63. encoding: serialization.Encoding,
  64. format: serialization.PrivateFormat,
  65. encryption_algorithm: serialization.KeySerializationEncryption,
  66. ) -> bytes:
  67. if (
  68. encoding is serialization.Encoding.Raw
  69. or format is serialization.PublicFormat.Raw
  70. ):
  71. if (
  72. format is not serialization.PrivateFormat.Raw
  73. or encoding is not serialization.Encoding.Raw
  74. or not isinstance(
  75. encryption_algorithm, serialization.NoEncryption
  76. )
  77. ):
  78. raise ValueError(
  79. "When using Raw both encoding and format must be Raw "
  80. "and encryption_algorithm must be NoEncryption()"
  81. )
  82. return self._raw_private_bytes()
  83. return self._backend._private_key_bytes(
  84. encoding, format, encryption_algorithm, self, self._evp_pkey, None
  85. )
  86. def _raw_private_bytes(self) -> bytes:
  87. buf = self._backend._ffi.new("unsigned char []", _X448_KEY_SIZE)
  88. buflen = self._backend._ffi.new("size_t *", _X448_KEY_SIZE)
  89. res = self._backend._lib.EVP_PKEY_get_raw_private_key(
  90. self._evp_pkey, buf, buflen
  91. )
  92. self._backend.openssl_assert(res == 1)
  93. self._backend.openssl_assert(buflen[0] == _X448_KEY_SIZE)
  94. return self._backend._ffi.buffer(buf, _X448_KEY_SIZE)[:]