crypto_kx.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # Copyright 2018 Donald Stufft and individual contributors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Tuple
  15. from nacl import exceptions as exc
  16. from nacl._sodium import ffi, lib
  17. from nacl.exceptions import ensure
  18. __all__ = [
  19. "crypto_kx_keypair",
  20. "crypto_kx_client_session_keys",
  21. "crypto_kx_server_session_keys",
  22. "crypto_kx_PUBLIC_KEY_BYTES",
  23. "crypto_kx_SECRET_KEY_BYTES",
  24. "crypto_kx_SEED_BYTES",
  25. "crypto_kx_SESSION_KEY_BYTES",
  26. ]
  27. """
  28. Implementations of client, server key exchange
  29. """
  30. crypto_kx_PUBLIC_KEY_BYTES: int = lib.crypto_kx_publickeybytes()
  31. crypto_kx_SECRET_KEY_BYTES: int = lib.crypto_kx_secretkeybytes()
  32. crypto_kx_SEED_BYTES: int = lib.crypto_kx_seedbytes()
  33. crypto_kx_SESSION_KEY_BYTES: int = lib.crypto_kx_sessionkeybytes()
  34. def crypto_kx_keypair() -> Tuple[bytes, bytes]:
  35. """
  36. Generate a keypair.
  37. This is a duplicate crypto_box_keypair, but
  38. is included for api consistency.
  39. :return: (public_key, secret_key)
  40. :rtype: (bytes, bytes)
  41. """
  42. public_key = ffi.new("unsigned char[]", crypto_kx_PUBLIC_KEY_BYTES)
  43. secret_key = ffi.new("unsigned char[]", crypto_kx_SECRET_KEY_BYTES)
  44. res = lib.crypto_kx_keypair(public_key, secret_key)
  45. ensure(res == 0, "Key generation failed.", raising=exc.CryptoError)
  46. return (
  47. ffi.buffer(public_key, crypto_kx_PUBLIC_KEY_BYTES)[:],
  48. ffi.buffer(secret_key, crypto_kx_SECRET_KEY_BYTES)[:],
  49. )
  50. def crypto_kx_seed_keypair(seed: bytes) -> Tuple[bytes, bytes]:
  51. """
  52. Generate a keypair with a given seed.
  53. This is functionally the same as crypto_box_seed_keypair, however
  54. it uses the blake2b hash primitive instead of sha512.
  55. It is included mainly for api consistency when using crypto_kx.
  56. :param seed: random seed
  57. :type seed: bytes
  58. :return: (public_key, secret_key)
  59. :rtype: (bytes, bytes)
  60. """
  61. public_key = ffi.new("unsigned char[]", crypto_kx_PUBLIC_KEY_BYTES)
  62. secret_key = ffi.new("unsigned char[]", crypto_kx_SECRET_KEY_BYTES)
  63. ensure(
  64. isinstance(seed, bytes) and len(seed) == crypto_kx_SEED_BYTES,
  65. "Seed must be a {} byte long bytes sequence".format(
  66. crypto_kx_SEED_BYTES
  67. ),
  68. raising=exc.TypeError,
  69. )
  70. res = lib.crypto_kx_seed_keypair(public_key, secret_key, seed)
  71. ensure(res == 0, "Key generation failed.", raising=exc.CryptoError)
  72. return (
  73. ffi.buffer(public_key, crypto_kx_PUBLIC_KEY_BYTES)[:],
  74. ffi.buffer(secret_key, crypto_kx_SECRET_KEY_BYTES)[:],
  75. )
  76. def crypto_kx_client_session_keys(
  77. client_public_key: bytes,
  78. client_secret_key: bytes,
  79. server_public_key: bytes,
  80. ) -> Tuple[bytes, bytes]:
  81. """
  82. Generate session keys for the client.
  83. :param client_public_key:
  84. :type client_public_key: bytes
  85. :param client_secret_key:
  86. :type client_secret_key: bytes
  87. :param server_public_key:
  88. :type server_public_key: bytes
  89. :return: (rx_key, tx_key)
  90. :rtype: (bytes, bytes)
  91. """
  92. ensure(
  93. isinstance(client_public_key, bytes)
  94. and len(client_public_key) == crypto_kx_PUBLIC_KEY_BYTES,
  95. "Client public key must be a {} bytes long bytes sequence".format(
  96. crypto_kx_PUBLIC_KEY_BYTES
  97. ),
  98. raising=exc.TypeError,
  99. )
  100. ensure(
  101. isinstance(client_secret_key, bytes)
  102. and len(client_secret_key) == crypto_kx_SECRET_KEY_BYTES,
  103. "Client secret key must be a {} bytes long bytes sequence".format(
  104. crypto_kx_PUBLIC_KEY_BYTES
  105. ),
  106. raising=exc.TypeError,
  107. )
  108. ensure(
  109. isinstance(server_public_key, bytes)
  110. and len(server_public_key) == crypto_kx_PUBLIC_KEY_BYTES,
  111. "Server public key must be a {} bytes long bytes sequence".format(
  112. crypto_kx_PUBLIC_KEY_BYTES
  113. ),
  114. raising=exc.TypeError,
  115. )
  116. rx_key = ffi.new("unsigned char[]", crypto_kx_SESSION_KEY_BYTES)
  117. tx_key = ffi.new("unsigned char[]", crypto_kx_SESSION_KEY_BYTES)
  118. res = lib.crypto_kx_client_session_keys(
  119. rx_key, tx_key, client_public_key, client_secret_key, server_public_key
  120. )
  121. ensure(
  122. res == 0,
  123. "Client session key generation failed.",
  124. raising=exc.CryptoError,
  125. )
  126. return (
  127. ffi.buffer(rx_key, crypto_kx_SESSION_KEY_BYTES)[:],
  128. ffi.buffer(tx_key, crypto_kx_SESSION_KEY_BYTES)[:],
  129. )
  130. def crypto_kx_server_session_keys(
  131. server_public_key: bytes,
  132. server_secret_key: bytes,
  133. client_public_key: bytes,
  134. ) -> Tuple[bytes, bytes]:
  135. """
  136. Generate session keys for the server.
  137. :param server_public_key:
  138. :type server_public_key: bytes
  139. :param server_secret_key:
  140. :type server_secret_key: bytes
  141. :param client_public_key:
  142. :type client_public_key: bytes
  143. :return: (rx_key, tx_key)
  144. :rtype: (bytes, bytes)
  145. """
  146. ensure(
  147. isinstance(server_public_key, bytes)
  148. and len(server_public_key) == crypto_kx_PUBLIC_KEY_BYTES,
  149. "Server public key must be a {} bytes long bytes sequence".format(
  150. crypto_kx_PUBLIC_KEY_BYTES
  151. ),
  152. raising=exc.TypeError,
  153. )
  154. ensure(
  155. isinstance(server_secret_key, bytes)
  156. and len(server_secret_key) == crypto_kx_SECRET_KEY_BYTES,
  157. "Server secret key must be a {} bytes long bytes sequence".format(
  158. crypto_kx_PUBLIC_KEY_BYTES
  159. ),
  160. raising=exc.TypeError,
  161. )
  162. ensure(
  163. isinstance(client_public_key, bytes)
  164. and len(client_public_key) == crypto_kx_PUBLIC_KEY_BYTES,
  165. "Client public key must be a {} bytes long bytes sequence".format(
  166. crypto_kx_PUBLIC_KEY_BYTES
  167. ),
  168. raising=exc.TypeError,
  169. )
  170. rx_key = ffi.new("unsigned char[]", crypto_kx_SESSION_KEY_BYTES)
  171. tx_key = ffi.new("unsigned char[]", crypto_kx_SESSION_KEY_BYTES)
  172. res = lib.crypto_kx_server_session_keys(
  173. rx_key, tx_key, server_public_key, server_secret_key, client_public_key
  174. )
  175. ensure(
  176. res == 0,
  177. "Server session key generation failed.",
  178. raising=exc.CryptoError,
  179. )
  180. return (
  181. ffi.buffer(rx_key, crypto_kx_SESSION_KEY_BYTES)[:],
  182. ffi.buffer(tx_key, crypto_kx_SESSION_KEY_BYTES)[:],
  183. )