crypto_sign.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright 2013 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 __future__ import absolute_import, division, print_function
  15. from nacl import exceptions as exc
  16. from nacl._sodium import ffi, lib
  17. from nacl.exceptions import ensure
  18. crypto_sign_BYTES = lib.crypto_sign_bytes()
  19. # crypto_sign_SEEDBYTES = lib.crypto_sign_seedbytes()
  20. crypto_sign_SEEDBYTES = lib.crypto_sign_secretkeybytes() // 2
  21. crypto_sign_PUBLICKEYBYTES = lib.crypto_sign_publickeybytes()
  22. crypto_sign_SECRETKEYBYTES = lib.crypto_sign_secretkeybytes()
  23. crypto_sign_curve25519_BYTES = lib.crypto_box_secretkeybytes()
  24. def crypto_sign_keypair():
  25. """
  26. Returns a randomly generated public key and secret key.
  27. :rtype: (bytes(public_key), bytes(secret_key))
  28. """
  29. pk = ffi.new("unsigned char[]", crypto_sign_PUBLICKEYBYTES)
  30. sk = ffi.new("unsigned char[]", crypto_sign_SECRETKEYBYTES)
  31. rc = lib.crypto_sign_keypair(pk, sk)
  32. ensure(rc == 0,
  33. 'Unexpected library error',
  34. raising=exc.RuntimeError)
  35. return (
  36. ffi.buffer(pk, crypto_sign_PUBLICKEYBYTES)[:],
  37. ffi.buffer(sk, crypto_sign_SECRETKEYBYTES)[:],
  38. )
  39. def crypto_sign_seed_keypair(seed):
  40. """
  41. Computes and returns the public key and secret key using the seed ``seed``.
  42. :param seed: bytes
  43. :rtype: (bytes(public_key), bytes(secret_key))
  44. """
  45. if len(seed) != crypto_sign_SEEDBYTES:
  46. raise exc.ValueError("Invalid seed")
  47. pk = ffi.new("unsigned char[]", crypto_sign_PUBLICKEYBYTES)
  48. sk = ffi.new("unsigned char[]", crypto_sign_SECRETKEYBYTES)
  49. rc = lib.crypto_sign_seed_keypair(pk, sk, seed)
  50. ensure(rc == 0,
  51. 'Unexpected library error',
  52. raising=exc.RuntimeError)
  53. return (
  54. ffi.buffer(pk, crypto_sign_PUBLICKEYBYTES)[:],
  55. ffi.buffer(sk, crypto_sign_SECRETKEYBYTES)[:],
  56. )
  57. def crypto_sign(message, sk):
  58. """
  59. Signs the message ``message`` using the secret key ``sk`` and returns the
  60. signed message.
  61. :param message: bytes
  62. :param sk: bytes
  63. :rtype: bytes
  64. """
  65. signed = ffi.new("unsigned char[]", len(message) + crypto_sign_BYTES)
  66. signed_len = ffi.new("unsigned long long *")
  67. rc = lib.crypto_sign(signed, signed_len, message, len(message), sk)
  68. ensure(rc == 0,
  69. 'Unexpected library error',
  70. raising=exc.RuntimeError)
  71. return ffi.buffer(signed, signed_len[0])[:]
  72. def crypto_sign_open(signed, pk):
  73. """
  74. Verifies the signature of the signed message ``signed`` using the public
  75. key ``pk`` and returns the unsigned message.
  76. :param signed: bytes
  77. :param pk: bytes
  78. :rtype: bytes
  79. """
  80. message = ffi.new("unsigned char[]", len(signed))
  81. message_len = ffi.new("unsigned long long *")
  82. if lib.crypto_sign_open(
  83. message, message_len, signed, len(signed), pk) != 0:
  84. raise exc.BadSignatureError("Signature was forged or corrupt")
  85. return ffi.buffer(message, message_len[0])[:]
  86. def crypto_sign_ed25519_pk_to_curve25519(public_key_bytes):
  87. """
  88. Converts a public Ed25519 key (encoded as bytes ``public_key_bytes``) to
  89. a public Curve25519 key as bytes.
  90. Raises a ValueError if ``public_key_bytes`` is not of length
  91. ``crypto_sign_PUBLICKEYBYTES``
  92. :param public_key_bytes: bytes
  93. :rtype: bytes
  94. """
  95. if len(public_key_bytes) != crypto_sign_PUBLICKEYBYTES:
  96. raise exc.ValueError("Invalid curve public key")
  97. curve_public_key_len = crypto_sign_curve25519_BYTES
  98. curve_public_key = ffi.new("unsigned char[]", curve_public_key_len)
  99. rc = lib.crypto_sign_ed25519_pk_to_curve25519(curve_public_key,
  100. public_key_bytes)
  101. ensure(rc == 0,
  102. 'Unexpected library error',
  103. raising=exc.RuntimeError)
  104. return ffi.buffer(curve_public_key, curve_public_key_len)[:]
  105. def crypto_sign_ed25519_sk_to_curve25519(secret_key_bytes):
  106. """
  107. Converts a secret Ed25519 key (encoded as bytes ``secret_key_bytes``) to
  108. a secret Curve25519 key as bytes.
  109. Raises a ValueError if ``secret_key_bytes``is not of length
  110. ``crypto_sign_SECRETKEYBYTES``
  111. :param public_key_bytes: bytes
  112. :rtype: bytes
  113. """
  114. if len(secret_key_bytes) != crypto_sign_SECRETKEYBYTES:
  115. raise exc.ValueError("Invalid curve public key")
  116. curve_secret_key_len = crypto_sign_curve25519_BYTES
  117. curve_secret_key = ffi.new("unsigned char[]", curve_secret_key_len)
  118. rc = lib.crypto_sign_ed25519_sk_to_curve25519(curve_secret_key,
  119. secret_key_bytes)
  120. ensure(rc == 0,
  121. 'Unexpected library error',
  122. raising=exc.RuntimeError)
  123. return ffi.buffer(curve_secret_key, curve_secret_key_len)[:]