crypto_secretbox.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_secretbox_KEYBYTES = lib.crypto_secretbox_keybytes()
  19. crypto_secretbox_NONCEBYTES = lib.crypto_secretbox_noncebytes()
  20. crypto_secretbox_ZEROBYTES = lib.crypto_secretbox_zerobytes()
  21. crypto_secretbox_BOXZEROBYTES = lib.crypto_secretbox_boxzerobytes()
  22. def crypto_secretbox(message, nonce, key):
  23. """
  24. Encrypts and returns the message ``message`` with the secret ``key`` and
  25. the nonce ``nonce``.
  26. :param message: bytes
  27. :param nonce: bytes
  28. :param key: bytes
  29. :rtype: bytes
  30. """
  31. if len(key) != crypto_secretbox_KEYBYTES:
  32. raise exc.ValueError("Invalid key")
  33. if len(nonce) != crypto_secretbox_NONCEBYTES:
  34. raise exc.ValueError("Invalid nonce")
  35. padded = b"\x00" * crypto_secretbox_ZEROBYTES + message
  36. ciphertext = ffi.new("unsigned char[]", len(padded))
  37. res = lib.crypto_secretbox(ciphertext, padded, len(padded), nonce, key)
  38. ensure(res == 0, "Encryption failed", raising=exc.CryptoError)
  39. ciphertext = ffi.buffer(ciphertext, len(padded))
  40. return ciphertext[crypto_secretbox_BOXZEROBYTES:]
  41. def crypto_secretbox_open(ciphertext, nonce, key):
  42. """
  43. Decrypt and returns the encrypted message ``ciphertext`` with the secret
  44. ``key`` and the nonce ``nonce``.
  45. :param ciphertext: bytes
  46. :param nonce: bytes
  47. :param key: bytes
  48. :rtype: bytes
  49. """
  50. if len(key) != crypto_secretbox_KEYBYTES:
  51. raise exc.ValueError("Invalid key")
  52. if len(nonce) != crypto_secretbox_NONCEBYTES:
  53. raise exc.ValueError("Invalid nonce")
  54. padded = b"\x00" * crypto_secretbox_BOXZEROBYTES + ciphertext
  55. plaintext = ffi.new("unsigned char[]", len(padded))
  56. res = lib.crypto_secretbox_open(
  57. plaintext, padded, len(padded), nonce, key)
  58. ensure(res == 0, "Decryption failed. Ciphertext failed verification",
  59. raising=exc.CryptoError)
  60. plaintext = ffi.buffer(plaintext, len(padded))
  61. return plaintext[crypto_secretbox_ZEROBYTES:]