Salsa20.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/Salsa20.py : Salsa20 stream cipher (http://cr.yp.to/snuffle.html)
  4. #
  5. # Contributed by Fabrizio Tarizzo <fabrizio@fabriziotarizzo.org>.
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. from tls.Crypto.Util.py3compat import _copy_bytes
  25. from tls.Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
  26. create_string_buffer,
  27. get_raw_buffer, VoidPointer,
  28. SmartPointer, c_size_t,
  29. c_uint8_ptr, is_writeable_buffer)
  30. from tls.Crypto.Random import get_random_bytes
  31. _raw_salsa20_lib = load_pycryptodome_raw_lib("Crypto.Cipher._Salsa20",
  32. """
  33. int Salsa20_stream_init(uint8_t *key, size_t keylen,
  34. uint8_t *nonce, size_t nonce_len,
  35. void **pSalsaState);
  36. int Salsa20_stream_destroy(void *salsaState);
  37. int Salsa20_stream_encrypt(void *salsaState,
  38. const uint8_t in[],
  39. uint8_t out[], size_t len);
  40. """)
  41. class Salsa20Cipher:
  42. """Salsa20 cipher object. Do not create it directly. Use :py:func:`new`
  43. instead.
  44. :var nonce: The nonce with length 8
  45. :vartype nonce: byte string
  46. """
  47. def __init__(self, key, nonce):
  48. """Initialize a Salsa20 cipher object
  49. See also `new()` at the module level."""
  50. if len(key) not in key_size:
  51. raise ValueError("Incorrect key length for Salsa20 (%d bytes)" % len(key))
  52. if len(nonce) != 8:
  53. raise ValueError("Incorrect nonce length for Salsa20 (%d bytes)" %
  54. len(nonce))
  55. self.nonce = _copy_bytes(None, None, nonce)
  56. self._state = VoidPointer()
  57. result = _raw_salsa20_lib.Salsa20_stream_init(
  58. c_uint8_ptr(key),
  59. c_size_t(len(key)),
  60. c_uint8_ptr(nonce),
  61. c_size_t(len(nonce)),
  62. self._state.address_of())
  63. if result:
  64. raise ValueError("Error %d instantiating a Salsa20 cipher")
  65. self._state = SmartPointer(self._state.get(),
  66. _raw_salsa20_lib.Salsa20_stream_destroy)
  67. self.block_size = 1
  68. self.key_size = len(key)
  69. def encrypt(self, plaintext, output=None):
  70. """Encrypt a piece of data.
  71. Args:
  72. plaintext(bytes/bytearray/memoryview): The data to encrypt, of any size.
  73. Keyword Args:
  74. output(bytes/bytearray/memoryview): The location where the ciphertext
  75. is written to. If ``None``, the ciphertext is returned.
  76. Returns:
  77. If ``output`` is ``None``, the ciphertext is returned as ``bytes``.
  78. Otherwise, ``None``.
  79. """
  80. if output is None:
  81. ciphertext = create_string_buffer(len(plaintext))
  82. else:
  83. ciphertext = output
  84. if not is_writeable_buffer(output):
  85. raise TypeError("output must be a bytearray or a writeable memoryview")
  86. if len(plaintext) != len(output):
  87. raise ValueError("output must have the same length as the input"
  88. " (%d bytes)" % len(plaintext))
  89. result = _raw_salsa20_lib.Salsa20_stream_encrypt(
  90. self._state.get(),
  91. c_uint8_ptr(plaintext),
  92. c_uint8_ptr(ciphertext),
  93. c_size_t(len(plaintext)))
  94. if result:
  95. raise ValueError("Error %d while encrypting with Salsa20" % result)
  96. if output is None:
  97. return get_raw_buffer(ciphertext)
  98. else:
  99. return None
  100. def decrypt(self, ciphertext, output=None):
  101. """Decrypt a piece of data.
  102. Args:
  103. ciphertext(bytes/bytearray/memoryview): The data to decrypt, of any size.
  104. Keyword Args:
  105. output(bytes/bytearray/memoryview): The location where the plaintext
  106. is written to. If ``None``, the plaintext is returned.
  107. Returns:
  108. If ``output`` is ``None``, the plaintext is returned as ``bytes``.
  109. Otherwise, ``None``.
  110. """
  111. try:
  112. return self.encrypt(ciphertext, output=output)
  113. except ValueError as e:
  114. raise ValueError(str(e).replace("enc", "dec"))
  115. def new(key, nonce=None):
  116. """Create a new Salsa20 cipher
  117. :keyword key: The secret key to use. It must be 16 or 32 bytes long.
  118. :type key: bytes/bytearray/memoryview
  119. :keyword nonce:
  120. A value that must never be reused for any other encryption
  121. done with this key. It must be 8 bytes long.
  122. If not provided, a random byte string will be generated (you can read
  123. it back via the ``nonce`` attribute of the returned object).
  124. :type nonce: bytes/bytearray/memoryview
  125. :Return: a :class:`Crypto.Cipher.Salsa20.Salsa20Cipher` object
  126. """
  127. if nonce is None:
  128. nonce = get_random_bytes(8)
  129. return Salsa20Cipher(key, nonce)
  130. # Size of a data block (in bytes)
  131. block_size = 1
  132. # Size of a key (in bytes)
  133. key_size = (16, 32)