ed25519.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. import typing
  5. from cryptography import exceptions
  6. from cryptography.hazmat.primitives import serialization
  7. from cryptography.hazmat.primitives.asymmetric.ed25519 import (
  8. Ed25519PrivateKey,
  9. Ed25519PublicKey,
  10. _ED25519_KEY_SIZE,
  11. _ED25519_SIG_SIZE,
  12. )
  13. if typing.TYPE_CHECKING:
  14. from cryptography.hazmat.backends.openssl.backend import Backend
  15. class _Ed25519PublicKey(Ed25519PublicKey):
  16. def __init__(self, backend: "Backend", evp_pkey):
  17. self._backend = backend
  18. self._evp_pkey = evp_pkey
  19. def public_bytes(
  20. self,
  21. encoding: serialization.Encoding,
  22. format: serialization.PublicFormat,
  23. ) -> bytes:
  24. if (
  25. encoding is serialization.Encoding.Raw
  26. or format is serialization.PublicFormat.Raw
  27. ):
  28. if (
  29. encoding is not serialization.Encoding.Raw
  30. or format is not serialization.PublicFormat.Raw
  31. ):
  32. raise ValueError(
  33. "When using Raw both encoding and format must be Raw"
  34. )
  35. return self._raw_public_bytes()
  36. return self._backend._public_key_bytes(
  37. encoding, format, self, self._evp_pkey, None
  38. )
  39. def _raw_public_bytes(self) -> bytes:
  40. buf = self._backend._ffi.new("unsigned char []", _ED25519_KEY_SIZE)
  41. buflen = self._backend._ffi.new("size_t *", _ED25519_KEY_SIZE)
  42. res = self._backend._lib.EVP_PKEY_get_raw_public_key(
  43. self._evp_pkey, buf, buflen
  44. )
  45. self._backend.openssl_assert(res == 1)
  46. self._backend.openssl_assert(buflen[0] == _ED25519_KEY_SIZE)
  47. return self._backend._ffi.buffer(buf, _ED25519_KEY_SIZE)[:]
  48. def verify(self, signature: bytes, data: bytes) -> None:
  49. evp_md_ctx = self._backend._lib.EVP_MD_CTX_new()
  50. self._backend.openssl_assert(evp_md_ctx != self._backend._ffi.NULL)
  51. evp_md_ctx = self._backend._ffi.gc(
  52. evp_md_ctx, self._backend._lib.EVP_MD_CTX_free
  53. )
  54. res = self._backend._lib.EVP_DigestVerifyInit(
  55. evp_md_ctx,
  56. self._backend._ffi.NULL,
  57. self._backend._ffi.NULL,
  58. self._backend._ffi.NULL,
  59. self._evp_pkey,
  60. )
  61. self._backend.openssl_assert(res == 1)
  62. res = self._backend._lib.EVP_DigestVerify(
  63. evp_md_ctx, signature, len(signature), data, len(data)
  64. )
  65. if res != 1:
  66. self._backend._consume_errors()
  67. raise exceptions.InvalidSignature
  68. class _Ed25519PrivateKey(Ed25519PrivateKey):
  69. def __init__(self, backend: "Backend", evp_pkey):
  70. self._backend = backend
  71. self._evp_pkey = evp_pkey
  72. def public_key(self) -> Ed25519PublicKey:
  73. buf = self._backend._ffi.new("unsigned char []", _ED25519_KEY_SIZE)
  74. buflen = self._backend._ffi.new("size_t *", _ED25519_KEY_SIZE)
  75. res = self._backend._lib.EVP_PKEY_get_raw_public_key(
  76. self._evp_pkey, buf, buflen
  77. )
  78. self._backend.openssl_assert(res == 1)
  79. self._backend.openssl_assert(buflen[0] == _ED25519_KEY_SIZE)
  80. public_bytes = self._backend._ffi.buffer(buf)[:]
  81. return self._backend.ed25519_load_public_bytes(public_bytes)
  82. def sign(self, data: bytes) -> bytes:
  83. evp_md_ctx = self._backend._lib.EVP_MD_CTX_new()
  84. self._backend.openssl_assert(evp_md_ctx != self._backend._ffi.NULL)
  85. evp_md_ctx = self._backend._ffi.gc(
  86. evp_md_ctx, self._backend._lib.EVP_MD_CTX_free
  87. )
  88. res = self._backend._lib.EVP_DigestSignInit(
  89. evp_md_ctx,
  90. self._backend._ffi.NULL,
  91. self._backend._ffi.NULL,
  92. self._backend._ffi.NULL,
  93. self._evp_pkey,
  94. )
  95. self._backend.openssl_assert(res == 1)
  96. buf = self._backend._ffi.new("unsigned char[]", _ED25519_SIG_SIZE)
  97. buflen = self._backend._ffi.new("size_t *", len(buf))
  98. res = self._backend._lib.EVP_DigestSign(
  99. evp_md_ctx, buf, buflen, data, len(data)
  100. )
  101. self._backend.openssl_assert(res == 1)
  102. self._backend.openssl_assert(buflen[0] == _ED25519_SIG_SIZE)
  103. return self._backend._ffi.buffer(buf, buflen[0])[:]
  104. def private_bytes(
  105. self,
  106. encoding: serialization.Encoding,
  107. format: serialization.PrivateFormat,
  108. encryption_algorithm: serialization.KeySerializationEncryption,
  109. ) -> bytes:
  110. if (
  111. encoding is serialization.Encoding.Raw
  112. or format is serialization.PublicFormat.Raw
  113. ):
  114. if (
  115. format is not serialization.PrivateFormat.Raw
  116. or encoding is not serialization.Encoding.Raw
  117. or not isinstance(
  118. encryption_algorithm, serialization.NoEncryption
  119. )
  120. ):
  121. raise ValueError(
  122. "When using Raw both encoding and format must be Raw "
  123. "and encryption_algorithm must be NoEncryption()"
  124. )
  125. return self._raw_private_bytes()
  126. return self._backend._private_key_bytes(
  127. encoding, format, encryption_algorithm, self, self._evp_pkey, None
  128. )
  129. def _raw_private_bytes(self) -> bytes:
  130. buf = self._backend._ffi.new("unsigned char []", _ED25519_KEY_SIZE)
  131. buflen = self._backend._ffi.new("size_t *", _ED25519_KEY_SIZE)
  132. res = self._backend._lib.EVP_PKEY_get_raw_private_key(
  133. self._evp_pkey, buf, buflen
  134. )
  135. self._backend.openssl_assert(res == 1)
  136. self._backend.openssl_assert(buflen[0] == _ED25519_KEY_SIZE)
  137. return self._backend._ffi.buffer(buf, _ED25519_KEY_SIZE)[:]